DEADSOFTWARE

render: separate panel logic and drawing
[d2df-sdl.git] / src / game / g_panel.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$M+}
17 unit g_panel;
19 interface
21 uses
22 SysUtils, Classes,
23 MAPDEF, g_textures, xdynrec;
25 type
26 TAddTextureArray = Array of
27 record
28 Texture: Cardinal;
29 Anim: Boolean;
30 end;
32 ATextureID = array of record
33 case Anim: Boolean of
34 False: (Tex: Cardinal);
35 True: (AnTex: TAnimation);
36 end;
38 PPanel = ^TPanel;
39 TPanel = Class (TObject)
40 private
41 const
42 private
43 mGUID: Integer; // will be assigned in "g_map.pas"
44 FTextureWidth: Word;
45 FTextureHeight: Word;
46 FAlpha: Byte;
47 FBlending: Boolean;
48 FTextureIDs: ATextureID;
49 mMovingSpeed: TDFPoint;
50 mMovingStart: TDFPoint;
51 mMovingEnd: TDFPoint;
52 mMovingActive: Boolean;
53 mMoveOnce: Boolean;
55 mOldMovingActive: Boolean;
57 mSizeSpeed: TDFSize;
58 mSizeEnd: TDFSize;
60 mEndPosTrig: Integer;
61 mEndSizeTrig: Integer;
63 mNeedSend: Boolean; // for network
65 private
66 function getx1 (): Integer; inline;
67 function gety1 (): Integer; inline;
68 function getvisvalid (): Boolean; inline;
70 function getMovingSpeedX (): Integer; inline;
71 procedure setMovingSpeedX (v: Integer); inline;
72 function getMovingSpeedY (): Integer; inline;
73 procedure setMovingSpeedY (v: Integer); inline;
75 function getMovingStartX (): Integer; inline;
76 procedure setMovingStartX (v: Integer); inline;
77 function getMovingStartY (): Integer; inline;
78 procedure setMovingStartY (v: Integer); inline;
80 function getMovingEndX (): Integer; inline;
81 procedure setMovingEndX (v: Integer); inline;
82 function getMovingEndY (): Integer; inline;
83 procedure setMovingEndY (v: Integer); inline;
85 function getSizeSpeedX (): Integer; inline;
86 procedure setSizeSpeedX (v: Integer); inline;
87 function getSizeSpeedY (): Integer; inline;
88 procedure setSizeSpeedY (v: Integer); inline;
90 function getSizeEndX (): Integer; inline;
91 procedure setSizeEndX (v: Integer); inline;
92 function getSizeEndY (): Integer; inline;
93 procedure setSizeEndY (v: Integer); inline;
95 public
96 FCurTexture: Integer; // Íîìåð òåêóùåé òåêñòóðû
97 FCurFrame: Integer;
98 FCurFrameCount: Byte;
99 FX, FY: Integer;
100 FOldX, FOldY: Integer;
101 FWidth, FHeight: Word;
102 FOldW, FOldH: Word;
103 FPanelType: Word;
104 FEnabled: Boolean;
105 FDoor: Boolean;
106 FLiftType: Byte;
107 FLastAnimLoop: Byte;
108 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
109 // for now, PLEASE, don't modify 'em, or all hell will break loose
110 arrIdx: Integer; // index in one of internal arrays; sorry
111 tag: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
112 proxyId: Integer; // proxy id in map grid (DO NOT USE!)
113 mapId: AnsiString; // taken directly from map file; dunno why it is here
114 hasTexTrigger: Boolean; // HACK: true when there's a trigger than can change my texture
116 constructor Create(PanelRec: TDynRecord;
117 AddTextures: TAddTextureArray;
118 CurTex: Integer;
119 var Textures: TLevelTextureArray; aguid: Integer);
120 destructor Destroy(); override;
122 procedure Update();
123 procedure SetFrame(Frame: Integer; Count: Byte);
124 procedure NextTexture(AnimLoop: Byte = 0);
125 procedure SetTexture(ID: Integer; AnimLoop: Byte = 0);
126 function GetTextureID(): Cardinal;
127 function GetTextureCount(): Integer;
128 function CanChangeTexture(): Boolean;
130 procedure SaveState (st: TStream);
131 procedure LoadState (st: TStream);
133 procedure positionChanged (); inline;
135 function getIsGBack (): Boolean; inline; // gRenderBackgrounds
136 function getIsGStep (): Boolean; inline; // gSteps
137 function getIsGWall (): Boolean; inline; // gWalls
138 function getIsGAcid1 (): Boolean; inline; // gAcid1
139 function getIsGAcid2 (): Boolean; inline; // gAcid2
140 function getIsGWater (): Boolean; inline; // gWater
141 function getIsGFore (): Boolean; inline; // gRenderForegrounds
142 function getIsGLift (): Boolean; inline; // gLifts
143 function getIsGBlockMon (): Boolean; inline; // gBlockMon
145 // get-and-clear
146 function gncNeedSend (): Boolean; inline;
147 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
149 public
150 property visvalid: Boolean read getvisvalid; // panel is "visvalid" when it's width and height are positive
152 published
153 property guid: Integer read mGUID; // will be assigned in "g_map.pas"
154 property x0: Integer read FX;
155 property y0: Integer read FY;
156 property x1: Integer read getx1; // inclusive!
157 property y1: Integer read gety1; // inclusive!
158 property x: Integer read FX write FX;
159 property y: Integer read FY write FY;
160 property width: Word read FWidth write FWidth;
161 property height: Word read FHeight write FHeight;
162 property oldX: Integer read FOldX;
163 property oldY: Integer read FOldY;
164 property oldWidth: Word read FOldW;
165 property oldHeight: Word read FOldH;
166 property panelType: Word read FPanelType write FPanelType;
167 property enabled: Boolean read FEnabled write FEnabled;
168 property door: Boolean read FDoor write FDoor;
169 property liftType: Byte read FLiftType write FLiftType;
170 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
172 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
173 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
174 property movingStartX: Integer read getMovingStartX write setMovingStartX;
175 property movingStartY: Integer read getMovingStartY write setMovingStartY;
176 property movingEndX: Integer read getMovingEndX write setMovingEndX;
177 property movingEndY: Integer read getMovingEndY write setMovingEndY;
178 property movingActive: Boolean read mMovingActive write mMovingActive;
179 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
181 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
182 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
183 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
184 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
186 property isGBack: Boolean read getIsGBack;
187 property isGStep: Boolean read getIsGStep;
188 property isGWall: Boolean read getIsGWall;
189 property isGAcid1: Boolean read getIsGAcid1;
190 property isGAcid2: Boolean read getIsGAcid2;
191 property isGWater: Boolean read getIsGWater;
192 property isGFore: Boolean read getIsGFore;
193 property isGLift: Boolean read getIsGLift;
194 property isGBlockMon: Boolean read getIsGBlockMon;
196 (* private state *)
197 property Alpha: Byte read FAlpha;
198 property TextureWidth: Word read FTextureWidth;
199 property TextureHeight: Word read FTextureHeight;
200 property Blending: Boolean read FBlending;
201 property TextureIDs: ATextureID read FTextureIDs;
203 public
204 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
205 property movingStart: TDFPoint read mMovingStart write mMovingStart;
206 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
208 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
209 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
211 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
212 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
213 end;
215 TPanelArray = Array of TPanel;
217 const
218 LIFTTYPE_UP = 0;
219 LIFTTYPE_DOWN = 1;
220 LIFTTYPE_LEFT = 2;
221 LIFTTYPE_RIGHT = 3;
223 var
224 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
225 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
228 implementation
230 uses
231 {$INCLUDE ../nogl/noGLuses.inc}
232 e_texture, g_basic, g_map, g_game, g_gfx, g_weapons, g_triggers, g_items,
233 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
235 const
236 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
238 { T P a n e l : }
240 constructor TPanel.Create(PanelRec: TDynRecord;
241 AddTextures: TAddTextureArray;
242 CurTex: Integer;
243 var Textures: TLevelTextureArray; aguid: Integer);
244 var
245 i: Integer;
246 tnum: Integer;
247 begin
248 X := PanelRec.X;
249 Y := PanelRec.Y;
250 FOldX := X;
251 FOldY := Y;
252 Width := PanelRec.Width;
253 Height := PanelRec.Height;
254 FOldW := Width;
255 FOldH := Height;
256 FAlpha := 0;
257 FBlending := False;
258 FCurFrame := 0;
259 FCurFrameCount := 0;
260 LastAnimLoop := 0;
262 mapId := PanelRec.id;
263 mGUID := aguid;
265 mMovingSpeed := PanelRec.moveSpeed;
266 mMovingStart := PanelRec.moveStart;
267 mMovingEnd := PanelRec.moveEnd;
268 mMovingActive := PanelRec['move_active'].value;
269 mOldMovingActive := mMovingActive;
270 mMoveOnce := PanelRec.moveOnce;
272 mSizeSpeed := PanelRec.sizeSpeed;
273 mSizeEnd := PanelRec.sizeEnd;
275 mEndPosTrig := PanelRec.endPosTrig;
276 mEndSizeTrig := PanelRec.endSizeTrig;
278 mNeedSend := false;
280 // Òèï ïàíåëè:
281 PanelType := PanelRec.PanelType;
282 Enabled := True;
283 Door := False;
284 LiftType := LIFTTYPE_UP;
285 hasTexTrigger := False;
287 case PanelType of
288 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
289 PANEL_CLOSEDOOR: Door := True;
290 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
291 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
292 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
293 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
294 end;
296 // Íåâèäèìàÿ:
297 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
298 begin
299 SetLength(FTextureIDs, 0);
300 FCurTexture := -1;
301 Exit;
302 end;
303 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
304 if ByteBool(PanelType and
305 (PANEL_LIFTUP or
306 PANEL_LIFTDOWN or
307 PANEL_LIFTLEFT or
308 PANEL_LIFTRIGHT or
309 PANEL_BLOCKMON)) then
310 begin
311 SetLength(FTextureIDs, 0);
312 FCurTexture := -1;
313 Exit;
314 end;
316 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
317 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
318 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
319 begin
320 SetLength(FTextureIDs, 1);
321 FTextureIDs[0].Anim := False;
323 case PanelRec.PanelType of
324 PANEL_WATER:
325 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_WATER);
326 PANEL_ACID1:
327 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID1);
328 PANEL_ACID2:
329 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID2);
330 end;
332 FCurTexture := 0;
333 Exit;
334 end;
336 SetLength(FTextureIDs, Length(AddTextures));
338 if CurTex < 0 then
339 FCurTexture := -1
340 else
341 if CurTex >= Length(FTextureIDs) then
342 FCurTexture := Length(FTextureIDs) - 1
343 else
344 FCurTexture := CurTex;
346 for i := 0 to Length(FTextureIDs)-1 do
347 begin
348 FTextureIDs[i].Anim := AddTextures[i].Anim;
349 if FTextureIDs[i].Anim then
350 begin // Àíèìèðîâàííàÿ òåêñòóðà
351 FTextureIDs[i].AnTex :=
352 TAnimation.Create(Textures[AddTextures[i].Texture].FramesID,
353 True, Textures[AddTextures[i].Texture].Speed);
354 FTextureIDs[i].AnTex.Blending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
355 FTextureIDs[i].AnTex.Alpha := PanelRec.Alpha;
356 end
357 else
358 begin // Îáû÷íàÿ òåêñòóðà
359 FTextureIDs[i].Tex := Textures[AddTextures[i].Texture].TextureID;
360 end;
361 end;
363 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
364 //if Length(FTextureIDs) > 1 then SaveIt := True;
366 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
367 if (tnum < 0) then tnum := Length(Textures);
369 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
370 if ({PanelRec.TextureNum}tnum > High(Textures)) then
371 begin
372 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
373 FTextureWidth := 2;
374 FTextureHeight := 2;
375 FAlpha := 0;
376 FBlending := ByteBool(0);
377 end
378 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
379 begin
380 FTextureWidth := Textures[{PanelRec.TextureNum}tnum].Width;
381 FTextureHeight := Textures[{PanelRec.TextureNum}tnum].Height;
382 FAlpha := PanelRec.Alpha;
383 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
384 end;
385 end;
387 destructor TPanel.Destroy();
388 var
389 i: Integer;
390 begin
391 for i := 0 to High(FTextureIDs) do
392 if FTextureIDs[i].Anim then
393 FTextureIDs[i].AnTex.Free();
394 SetLength(FTextureIDs, 0);
396 Inherited;
397 end;
399 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
400 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
401 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
403 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
404 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
405 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
406 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
408 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
409 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
410 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
411 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
413 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
414 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
415 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
416 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
418 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
419 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
420 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
421 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
423 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
424 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
425 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
426 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
428 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
429 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
430 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
431 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
432 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
433 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
434 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
435 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
436 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
438 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
439 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
441 procedure TPanel.positionChanged (); inline;
442 var
443 px, py, pw, ph: Integer;
444 begin
445 if (proxyId >= 0) then
446 begin
447 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
448 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
449 begin
451 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
452 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
454 g_Mark(px, py, pw, ph, MARK_WALL, false);
455 if (Width < 1) or (Height < 1) then
456 begin
457 mapGrid.proxyEnabled[proxyId] := false;
458 end
459 else
460 begin
461 mapGrid.proxyEnabled[proxyId] := Enabled;
462 if (pw <> Width) or (ph <> Height) then
463 begin
464 //writeln('panel resize!');
465 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
466 end
467 else
468 begin
469 mapGrid.moveBody(proxyId, X, Y);
470 end;
471 g_Mark(X, Y, Width, Height, MARK_WALL);
472 end;
473 end;
474 end;
475 end;
478 var
479 monCheckList: array of TMonster = nil;
480 monCheckListUsed: Integer = 0;
482 procedure TPanel.Update();
483 var
484 ox, oy: Integer;
485 nx, ny, nw, nh: Integer;
486 ex, ey, nex, ney: Integer;
487 mpw, mph: Integer;
488 conveyor: Boolean;
490 // return `true` if we should move by dx,dy
491 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
492 var
493 u0: Single;
494 tex, tey: Integer;
495 pdx, pdy: Integer;
496 trtag: Integer;
497 szdx, szdy: Integer;
498 begin
499 squash := false;
500 tex := px;
501 tey := py;
502 pdx := mMovingSpeed.X;
503 pdy := mMovingSpeed.Y;
504 // standing on the platform?
505 if (py+ph = oy) then
506 begin
507 if (ontop <> nil) then ontop^ := true;
508 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
509 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
510 end
511 else
512 begin
513 if (ontop <> nil) then ontop^ := false;
514 // not standing on the platform: trace platform to see if it hits the entity
515 // first, process size change (as we cannot sweeptest both move and size change)
516 // but we don't have to check for pushing if the panel is shrinking
517 szdx := nw-mpw;
518 szdy := nh-mph;
519 if (szdx > 0) or (szdy > 0) then
520 begin
521 // ignore shrinking dimension
522 if (szdx < 0) then szdx := 0;
523 if (szdy < 0) then szdy := 0;
524 // move platform by szd* back, and check for szd* movement
525 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
526 begin
527 // yes, platform hits the entity, push the entity in the resizing direction
528 u0 := 1.0-u0; // how much path left?
529 szdx := trunc(szdx*u0);
530 szdy := trunc(szdy*u0);
531 if (szdx <> 0) or (szdy <> 0) then
532 begin
533 // has some path to go, trace the entity
534 trtag := (GridTagWall or GridTagDoor);
535 // if we're moving down, consider steps too
536 if (szdy > 0) then trtag := trtag or GridTagStep;
537 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
538 end;
539 end;
540 end;
541 // second, process platform movement, using te* as entity starting point
542 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
543 begin
544 //e_LogWritefln('T: platsweep; u0=%s; u1=%s; hedge=%s; sweepAABB(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', [u0, u1, hedge, ox, oy, mpw, mph, pdx, pdy, px-1, py-1, pw+2, ph+2]);
545 // yes, platform hits the entity, push the entity in the direction of the platform
546 u0 := 1.0-u0; // how much path left?
547 pdx := trunc(pdx*u0);
548 pdy := trunc(pdy*u0);
549 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
550 if (pdx <> 0) or (pdy <> 0) then
551 begin
552 // has some path to go, trace the entity
553 trtag := (GridTagWall or GridTagDoor);
554 // if we're moving down, consider steps too
555 if (pdy > 0) then trtag := trtag or GridTagStep;
556 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
557 end;
558 end;
559 end;
560 // done with entity movement, new coords are in te*
561 dx := tex-px;
562 dy := tey-py;
563 result := (dx <> 0) or (dy <> 0);
564 if not conveyor and ((tag and (GridTagWall or GridTagDoor)) <> 0) then
565 begin
566 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
567 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
568 end;
569 end;
571 function monCollect (mon: TMonster): Boolean;
572 begin
573 result := false; // don't stop
574 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
575 monCheckList[monCheckListUsed] := mon;
576 Inc(monCheckListUsed);
577 end;
579 var
580 cx0, cy0, cx1, cy1, cw, ch: Integer;
581 f: Integer;
582 px, py, pw, ph, pdx, pdy: Integer;
583 squash: Boolean;
584 plr: TPlayer;
585 gib: PGib;
586 cor: TCorpse;
587 mon: TMonster;
588 flg: PFlag;
589 itm: PItem;
590 mpfrid: LongWord;
591 ontop: Boolean;
592 actMoveTrig: Boolean;
593 actSizeTrig: Boolean;
594 begin
595 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
597 if (FCurTexture >= 0) and
598 (FTextureIDs[FCurTexture].Anim) and
599 (FTextureIDs[FCurTexture].AnTex <> nil) and
600 (FAlpha < 255) then
601 begin
602 FTextureIDs[FCurTexture].AnTex.Update();
603 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
604 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
605 end;
607 if not g_dbgpan_mplat_active then exit;
609 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
610 mOldMovingActive := mMovingActive;
612 if not mMovingActive then exit;
613 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
615 //TODO: write wall size change processing
617 // moving platform?
618 begin
619 (*
620 * collect all monsters and players (aka entities) along the possible platform path
621 * if entity is standing on a platform:
622 * try to move it along the platform path, checking wall collisions
623 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
624 * try to push entity
625 * if we can't push entity all the way, squash it
626 *)
627 ox := X;
628 oy := Y;
629 mpw := Width;
630 mph := Height;
632 // the mplat acts as a stationary conveyor belt when it's locked within a movement rect of zero area
633 conveyor := (mMovingEnd.X = mMovingStart.X) and (mMovingEnd.Y = mMovingStart.Y)
634 and (mMovingEnd.X = X) and (mMovingEnd.Y = Y);
636 nw := mpw+mSizeSpeed.w;
637 nh := mph+mSizeSpeed.h;
638 nx := ox;
639 ny := oy;
640 if not conveyor then
641 begin
642 nx += mMovingSpeed.X;
643 ny += mMovingSpeed.Y;
644 end;
646 // force network updates only if some sudden change happened
647 // set the flag here, so we can sync affected monsters
648 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
649 begin
650 mNeedSend := true;
651 end
652 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
653 begin
654 mNeedSend := true;
655 end
656 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
657 begin
658 mNeedSend := true;
659 end;
661 // if pannel disappeared, we don't have to do anything
662 if (nw > 0) and (nh > 0) then
663 begin
664 // old rect
665 ex := ox+mpw-1;
666 ey := ox+mph-1;
667 // new rect
668 nex := nx+nw-1;
669 ney := ny+nh-1;
670 // full rect
671 cx0 := nmin(ox, nx);
672 cy0 := nmin(oy, ny);
673 cx1 := nmax(ex, nex);
674 cy1 := nmax(ey, ney);
675 // extrude
676 cx0 -= 1;
677 cy0 -= 1;
678 cx1 += 1;
679 cy1 += 1;
680 cw := cx1-cx0+1;
681 ch := cy1-cy0+1;
683 // process "obstacle" panels
684 if ((tag and GridTagObstacle) <> 0) then
685 begin
686 // temporarily turn off this panel, so it won't interfere with collision checks
687 mapGrid.proxyEnabled[proxyId] := false;
689 // process players
690 for f := 0 to High(gPlayers) do
691 begin
692 plr := gPlayers[f];
693 if (plr = nil) or (not plr.alive) then continue;
694 plr.getMapBox(px, py, pw, ph);
695 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
696 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
697 begin
698 // set new position
699 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
700 end;
701 // squash player, if necessary
702 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
703 end;
705 // process gibs
706 for f := 0 to High(gGibs) do
707 begin
708 gib := @gGibs[f];
709 if not gib.alive then continue;
710 gib.getMapBox(px, py, pw, ph);
711 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
712 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
713 begin
714 // set new position
715 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
716 end;
717 end;
719 // move and push corpses
720 for f := 0 to High(gCorpses) do
721 begin
722 cor := gCorpses[f];
723 if (cor = nil) then continue;
724 cor.getMapBox(px, py, pw, ph);
725 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
726 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
727 begin
728 // set new position
729 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
730 end;
731 end;
733 // move and push flags
734 if gGameSettings.GameMode = GM_CTF then
735 for f := FLAG_RED to FLAG_BLUE do
736 begin
737 flg := @gFlags[f];
738 if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
739 px := flg.Obj.X+flg.Obj.Rect.X;
740 py := flg.Obj.Y+flg.Obj.Rect.Y;
741 pw := flg.Obj.Rect.Width;
742 ph := flg.Obj.Rect.Height;
743 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
744 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
745 if (pdx <> 0) or (pdy <> 0) then
746 begin
747 flg.Obj.X := flg.Obj.X + pdx;
748 flg.Obj.Y := flg.Obj.Y + pdy;
749 flg.NeedSend := true;
750 end;
751 end;
753 // move and push items
754 itm := g_Items_NextAlive(-1);
755 while itm <> nil do
756 begin
757 if itm.Fall then
758 begin
759 itm.getMapBox(px, py, pw, ph);
760 if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
761 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
762 itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
763 end;
764 itm := g_Items_NextAlive(itm.myId);
765 end;
767 // collect monsters
768 monCheckListUsed := 0;
769 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
771 // process collected monsters
772 if (monCheckListUsed > 0) then
773 begin
774 mpfrid := g_Mons_getNewMPlatFrameId();
775 for f := 0 to monCheckListUsed do
776 begin
777 mon := monCheckList[f];
778 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
779 mon.mplatCheckFrameId := mpfrid;
780 mon.getMapBox(px, py, pw, ph);
781 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
782 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
783 begin
784 // set new position
785 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
786 //???FIXME: do we really need to send monsters over the net?
787 // i don't think so, as dead reckoning should take care of 'em
788 // ok, send new monster position only if platform is going to change it's direction
789 if mNeedSend then mon.setDirty();
790 end;
791 // squash monster, if necessary
792 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
793 end;
794 end;
796 // restore panel state
797 mapGrid.proxyEnabled[proxyId] := true;
798 end;
799 end;
801 // move panel
802 FOldX := X;
803 FOldY := Y;
804 X := nx;
805 Y := ny;
806 FOldW := FWidth;
807 FOldH := FHeight;
808 FWidth := nw;
809 FHeight := nh;
810 positionChanged();
812 actMoveTrig := false;
813 actSizeTrig := false;
815 // `mNeedSend` was set above
817 // check "size stop"
818 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
819 begin
820 mSizeSpeed.w := 0;
821 mSizeSpeed.h := 0;
822 actSizeTrig := true;
823 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
824 end;
826 if not conveyor then
827 begin
828 // reverse moving direction, if necessary
829 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
830 begin
831 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
832 actMoveTrig := true;
833 end;
835 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
836 begin
837 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
838 actMoveTrig := true;
839 end;
841 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
842 mOldMovingActive := mMovingActive;
843 end;
845 if not g_Game_IsClient then
846 begin
847 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
848 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
849 end;
851 // some triggers may activate this, don't delay sending
852 //TODO: when triggers will be able to control speed and size, check that here too
853 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
854 mOldMovingActive := mMovingActive;
855 end;
856 end;
859 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
861 function ClampInt(X, A, B: Integer): Integer;
862 begin
863 Result := X;
864 if X < A then Result := A else if X > B then Result := B;
865 end;
867 begin
868 if Enabled and (FCurTexture >= 0) and
869 (FTextureIDs[FCurTexture].Anim) and
870 (FTextureIDs[FCurTexture].AnTex <> nil) and
871 (Width > 0) and (Height > 0) and (FAlpha < 255) then
872 begin
873 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
874 FCurFrameCount := Count;
875 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
876 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
877 end;
878 end;
880 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
881 begin
882 Assert(FCurTexture >= -1, 'FCurTexture < -1');
884 // Íåò òåêñòóð:
885 if Length(FTextureIDs) = 0 then
886 FCurTexture := -1
887 else
888 // Òîëüêî îäíà òåêñòóðà:
889 if Length(FTextureIDs) = 1 then
890 begin
891 if FCurTexture = 0 then
892 FCurTexture := -1
893 else
894 FCurTexture := 0;
895 end
896 else
897 // Áîëüøå îäíîé òåêñòóðû:
898 begin
899 // Ñëåäóþùàÿ:
900 Inc(FCurTexture);
901 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
902 if FCurTexture >= Length(FTextureIDs) then
903 FCurTexture := 0;
904 end;
906 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
907 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
908 begin
909 if (FTextureIDs[FCurTexture].AnTex = nil) then
910 begin
911 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
912 Exit;
913 end;
915 if AnimLoop = 1 then
916 FTextureIDs[FCurTexture].AnTex.Loop := True
917 else
918 if AnimLoop = 2 then
919 FTextureIDs[FCurTexture].AnTex.Loop := False;
921 FTextureIDs[FCurTexture].AnTex.Reset();
922 end;
924 LastAnimLoop := AnimLoop;
925 end;
927 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
928 begin
929 if (ID >= -1) and (ID < Length(FTextureIDs)) then
930 FCurTexture := ID;
932 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
933 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
934 begin
935 if (FTextureIDs[FCurTexture].AnTex = nil) then
936 begin
937 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
938 Exit;
939 end;
941 if AnimLoop = 1 then
942 FTextureIDs[FCurTexture].AnTex.Loop := True
943 else
944 if AnimLoop = 2 then
945 FTextureIDs[FCurTexture].AnTex.Loop := False;
947 FTextureIDs[FCurTexture].AnTex.Reset();
948 end;
950 LastAnimLoop := AnimLoop;
951 end;
953 function TPanel.GetTextureID(): DWORD;
954 begin
955 Result := LongWord(TEXTURE_NONE);
957 if (FCurTexture >= 0) then
958 begin
959 if FTextureIDs[FCurTexture].Anim then
960 Result := FTextureIDs[FCurTexture].AnTex.FramesID
961 else
962 Result := FTextureIDs[FCurTexture].Tex;
963 end;
964 end;
966 function TPanel.GetTextureCount(): Integer;
967 begin
968 Result := Length(FTextureIDs);
969 if Enabled and (FCurTexture >= 0) then
970 if (FTextureIDs[FCurTexture].Anim) and
971 (FTextureIDs[FCurTexture].AnTex <> nil) and
972 (Width > 0) and (Height > 0) and (FAlpha < 255) then
973 Result := Result + 100;
974 end;
976 function TPanel.CanChangeTexture(): Boolean;
977 begin
978 Result := (GetTextureCount() > 1) or hasTexTrigger;
979 end;
981 const
982 PAN_SAVE_VERSION = 1;
984 procedure TPanel.SaveState (st: TStream);
985 var
986 anim: Boolean;
987 begin
988 if (st = nil) then exit;
990 // Ñèãíàòóðà ïàíåëè
991 utils.writeSign(st, 'PANL');
992 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
993 // Îòêðûòà/çàêðûòà, åñëè äâåðü
994 utils.writeBool(st, FEnabled);
995 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
996 utils.writeInt(st, Byte(FLiftType));
997 // Íîìåð òåêóùåé òåêñòóðû
998 utils.writeInt(st, Integer(FCurTexture));
999 // Êîîðäèíàòû è ðàçìåð
1000 utils.writeInt(st, Integer(FX));
1001 utils.writeInt(st, Integer(FY));
1002 utils.writeInt(st, Word(FWidth));
1003 utils.writeInt(st, Word(FHeight));
1004 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
1005 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
1006 begin
1007 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
1008 anim := true;
1009 end
1010 else
1011 begin
1012 anim := false;
1013 end;
1014 utils.writeBool(st, anim);
1015 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1016 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st);
1018 // moving platform state
1019 utils.writeInt(st, Integer(mMovingSpeed.X));
1020 utils.writeInt(st, Integer(mMovingSpeed.Y));
1021 utils.writeInt(st, Integer(mMovingStart.X));
1022 utils.writeInt(st, Integer(mMovingStart.Y));
1023 utils.writeInt(st, Integer(mMovingEnd.X));
1024 utils.writeInt(st, Integer(mMovingEnd.Y));
1026 utils.writeInt(st, Integer(mSizeSpeed.w));
1027 utils.writeInt(st, Integer(mSizeSpeed.h));
1028 utils.writeInt(st, Integer(mSizeEnd.w));
1029 utils.writeInt(st, Integer(mSizeEnd.h));
1031 utils.writeBool(st, mMovingActive);
1032 utils.writeBool(st, mMoveOnce);
1034 utils.writeInt(st, Integer(mEndPosTrig));
1035 utils.writeInt(st, Integer(mEndSizeTrig));
1036 end;
1039 procedure TPanel.LoadState (st: TStream);
1040 begin
1041 if (st = nil) then exit;
1043 // Ñèãíàòóðà ïàíåëè
1044 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1045 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1046 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1047 FEnabled := utils.readBool(st);
1048 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1049 FLiftType := utils.readByte(st);
1050 // Íîìåð òåêóùåé òåêñòóðû
1051 FCurTexture := utils.readLongInt(st);
1052 // Êîîðäèíàòû è ðàçìåð
1053 FX := utils.readLongInt(st);
1054 FY := utils.readLongInt(st);
1055 FOldX := FX;
1056 FOldY := FY;
1057 FWidth := utils.readWord(st);
1058 FHeight := utils.readWord(st);
1059 FOldW := FWidth;
1060 FOldH := FHeight;
1061 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1062 if utils.readBool(st) then
1063 begin
1064 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1065 Assert((FCurTexture >= 0) and
1066 (FTextureIDs[FCurTexture].Anim) and
1067 (FTextureIDs[FCurTexture].AnTex <> nil),
1068 'TPanel.LoadState: No animation object');
1069 FTextureIDs[FCurTexture].AnTex.LoadState(st);
1070 end;
1072 // moving platform state
1073 mMovingSpeed.X := utils.readLongInt(st);
1074 mMovingSpeed.Y := utils.readLongInt(st);
1075 mMovingStart.X := utils.readLongInt(st);
1076 mMovingStart.Y := utils.readLongInt(st);
1077 mMovingEnd.X := utils.readLongInt(st);
1078 mMovingEnd.Y := utils.readLongInt(st);
1080 mSizeSpeed.w := utils.readLongInt(st);
1081 mSizeSpeed.h := utils.readLongInt(st);
1082 mSizeEnd.w := utils.readLongInt(st);
1083 mSizeEnd.h := utils.readLongInt(st);
1085 mMovingActive := utils.readBool(st);
1086 mMoveOnce := utils.readBool(st);
1088 mEndPosTrig := utils.readLongInt(st);
1089 mEndSizeTrig := utils.readLongInt(st);
1091 positionChanged();
1092 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1093 end;
1096 end.