DEADSOFTWARE

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