DEADSOFTWARE

render: use TAnimationState instead of TAnimation
[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 Anim: Boolean;
29 end;
31 ATextureID = array of record
32 Texture: Cardinal; // Textures[Texture]
33 case Anim: Boolean of
34 False: ();
35 True: (AnTex: TAnimationState);
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 FWidth, FHeight: Word;
101 FPanelType: Word;
102 FEnabled: Boolean;
103 FDoor: Boolean;
104 FLiftType: Byte;
105 FLastAnimLoop: Byte;
106 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
107 // for now, PLEASE, don't modify 'em, or all hell will break loose
108 arrIdx: Integer; // index in one of internal arrays; sorry
109 tag: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
110 proxyId: Integer; // proxy id in map grid (DO NOT USE!)
111 mapId: AnsiString; // taken directly from map file; dunno why it is here
112 hasTexTrigger: Boolean; // HACK: true when there's a trigger than can change my texture
114 constructor Create(PanelRec: TDynRecord;
115 AddTextures: TAddTextureArray;
116 CurTex: Integer;
117 var Textures: TLevelTextureArray; aguid: Integer);
118 destructor Destroy(); override;
120 procedure Update();
121 procedure SetFrame(Frame: Integer; Count: Byte);
122 procedure NextTexture(AnimLoop: Byte = 0);
123 procedure SetTexture(ID: Integer; AnimLoop: Byte = 0);
124 function GetTextureID(): Cardinal;
125 function GetTextureCount(): Integer;
126 function CanChangeTexture(): Boolean;
128 procedure SaveState (st: TStream);
129 procedure LoadState (st: TStream);
131 procedure positionChanged (); inline;
133 function getIsGBack (): Boolean; inline; // gRenderBackgrounds
134 function getIsGStep (): Boolean; inline; // gSteps
135 function getIsGWall (): Boolean; inline; // gWalls
136 function getIsGAcid1 (): Boolean; inline; // gAcid1
137 function getIsGAcid2 (): Boolean; inline; // gAcid2
138 function getIsGWater (): Boolean; inline; // gWater
139 function getIsGFore (): Boolean; inline; // gRenderForegrounds
140 function getIsGLift (): Boolean; inline; // gLifts
141 function getIsGBlockMon (): Boolean; inline; // gBlockMon
143 // get-and-clear
144 function gncNeedSend (): Boolean; inline;
145 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
147 public
148 property visvalid: Boolean read getvisvalid; // panel is "visvalid" when it's width and height are positive
150 published
151 property guid: Integer read mGUID; // will be assigned in "g_map.pas"
152 property x0: Integer read FX;
153 property y0: Integer read FY;
154 property x1: Integer read getx1; // inclusive!
155 property y1: Integer read gety1; // inclusive!
156 property x: Integer read FX write FX;
157 property y: Integer read FY write FY;
158 property width: Word read FWidth write FWidth;
159 property height: Word read FHeight write FHeight;
160 property panelType: Word read FPanelType write FPanelType;
161 property enabled: Boolean read FEnabled write FEnabled;
162 property door: Boolean read FDoor write FDoor;
163 property liftType: Byte read FLiftType write FLiftType;
164 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
166 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
167 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
168 property movingStartX: Integer read getMovingStartX write setMovingStartX;
169 property movingStartY: Integer read getMovingStartY write setMovingStartY;
170 property movingEndX: Integer read getMovingEndX write setMovingEndX;
171 property movingEndY: Integer read getMovingEndY write setMovingEndY;
172 property movingActive: Boolean read mMovingActive write mMovingActive;
173 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
175 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
176 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
177 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
178 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
180 property isGBack: Boolean read getIsGBack;
181 property isGStep: Boolean read getIsGStep;
182 property isGWall: Boolean read getIsGWall;
183 property isGAcid1: Boolean read getIsGAcid1;
184 property isGAcid2: Boolean read getIsGAcid2;
185 property isGWater: Boolean read getIsGWater;
186 property isGFore: Boolean read getIsGFore;
187 property isGLift: Boolean read getIsGLift;
188 property isGBlockMon: Boolean read getIsGBlockMon;
190 (* private state *)
191 property Alpha: Byte read FAlpha;
192 property TextureWidth: Word read FTextureWidth;
193 property TextureHeight: Word read FTextureHeight;
194 property Blending: Boolean read FBlending;
195 property TextureIDs: ATextureID read FTextureIDs;
197 public
198 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
199 property movingStart: TDFPoint read mMovingStart write mMovingStart;
200 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
202 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
203 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
205 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
206 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
207 end;
209 TPanelArray = Array of TPanel;
211 const
212 LIFTTYPE_UP = 0;
213 LIFTTYPE_DOWN = 1;
214 LIFTTYPE_LEFT = 2;
215 LIFTTYPE_RIGHT = 3;
217 var
218 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
219 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
222 implementation
224 uses
225 g_basic, g_map, g_game, g_gfx, g_weapons, g_triggers,
226 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
228 const
229 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
231 { T P a n e l : }
233 constructor TPanel.Create(PanelRec: TDynRecord;
234 AddTextures: TAddTextureArray;
235 CurTex: Integer;
236 var Textures: TLevelTextureArray; aguid: Integer);
237 var
238 i: Integer;
239 tnum: Integer;
240 begin
241 X := PanelRec.X;
242 Y := PanelRec.Y;
243 Width := PanelRec.Width;
244 Height := PanelRec.Height;
245 FAlpha := 0;
246 FBlending := False;
247 FCurFrame := 0;
248 FCurFrameCount := 0;
249 LastAnimLoop := 0;
251 mapId := PanelRec.id;
252 mGUID := aguid;
254 mMovingSpeed := PanelRec.moveSpeed;
255 mMovingStart := PanelRec.moveStart;
256 mMovingEnd := PanelRec.moveEnd;
257 mMovingActive := PanelRec['move_active'].value;
258 mOldMovingActive := mMovingActive;
259 mMoveOnce := PanelRec.moveOnce;
261 mSizeSpeed := PanelRec.sizeSpeed;
262 mSizeEnd := PanelRec.sizeEnd;
264 mEndPosTrig := PanelRec.endPosTrig;
265 mEndSizeTrig := PanelRec.endSizeTrig;
267 mNeedSend := false;
269 // Òèï ïàíåëè:
270 PanelType := PanelRec.PanelType;
271 Enabled := True;
272 Door := False;
273 LiftType := LIFTTYPE_UP;
274 hasTexTrigger := False;
276 case PanelType of
277 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
278 PANEL_CLOSEDOOR: Door := True;
279 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
280 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
281 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
282 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
283 end;
285 // Íåâèäèìàÿ:
286 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
287 begin
288 SetLength(FTextureIDs, 0);
289 FCurTexture := -1;
290 Exit;
291 end;
292 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
293 if ByteBool(PanelType and
294 (PANEL_LIFTUP or
295 PANEL_LIFTDOWN or
296 PANEL_LIFTLEFT or
297 PANEL_LIFTRIGHT or
298 PANEL_BLOCKMON)) then
299 begin
300 SetLength(FTextureIDs, 0);
301 FCurTexture := -1;
302 Exit;
303 end;
305 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
306 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
307 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
308 begin
309 SetLength(FTextureIDs, 1);
310 FTextureIDs[0].Anim := False;
313 // !!! set this
314 case PanelRec.PanelType of
315 PANEL_WATER:
316 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_WATER);
317 PANEL_ACID1:
318 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID1);
319 PANEL_ACID2:
320 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID2);
321 end;
324 FCurTexture := 0;
325 Exit;
326 end;
328 SetLength(FTextureIDs, Length(AddTextures));
330 if CurTex < 0 then
331 FCurTexture := -1
332 else
333 if CurTex >= Length(FTextureIDs) then
334 FCurTexture := Length(FTextureIDs) - 1
335 else
336 FCurTexture := CurTex;
338 for i := 0 to Length(FTextureIDs)-1 do
339 begin
340 FTextureIDs[i].Texture := AddTextures[i].Texture;
341 FTextureIDs[i].Anim := AddTextures[i].Anim;
342 if FTextureIDs[i].Anim then
343 begin // Àíèìèðîâàííàÿ òåêñòóðà
344 FTextureIDs[i].AnTex := TAnimationState.Create(True, Textures[AddTextures[i].Texture].Speed, Textures[AddTextures[i].Texture].FramesCount);
345 FTextureIDs[i].AnTex.Blending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
346 FTextureIDs[i].AnTex.Alpha := PanelRec.Alpha;
347 end
348 end;
350 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
351 //if Length(FTextureIDs) > 1 then SaveIt := True;
353 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
354 if (tnum < 0) then tnum := Length(Textures);
356 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
357 if ({PanelRec.TextureNum}tnum > High(Textures)) then
358 begin
359 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
360 FTextureWidth := 2;
361 FTextureHeight := 2;
362 FAlpha := 0;
363 FBlending := ByteBool(0);
364 end
365 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
366 begin
367 FTextureWidth := Textures[{PanelRec.TextureNum}tnum].Width;
368 FTextureHeight := Textures[{PanelRec.TextureNum}tnum].Height;
369 FAlpha := PanelRec.Alpha;
370 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
371 end;
372 end;
374 destructor TPanel.Destroy();
375 var
376 i: Integer;
377 begin
378 for i := 0 to High(FTextureIDs) do
379 if FTextureIDs[i].Anim then
380 FTextureIDs[i].AnTex.Free();
381 SetLength(FTextureIDs, 0);
383 Inherited;
384 end;
386 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
387 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
388 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
390 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
391 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
392 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
393 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
395 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
396 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
397 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
398 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
400 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
401 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
402 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
403 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
405 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
406 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
407 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
408 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
410 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
411 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
412 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
413 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
415 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
416 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
417 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
418 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
419 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
420 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
421 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
422 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
423 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
425 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
426 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
428 procedure TPanel.positionChanged (); inline;
429 var
430 px, py, pw, ph: Integer;
431 begin
432 if (proxyId >= 0) then
433 begin
434 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
435 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
436 begin
438 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
439 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
441 g_Mark(px, py, pw, ph, MARK_WALL, false);
442 if (Width < 1) or (Height < 1) then
443 begin
444 mapGrid.proxyEnabled[proxyId] := false;
445 end
446 else
447 begin
448 mapGrid.proxyEnabled[proxyId] := Enabled;
449 if (pw <> Width) or (ph <> Height) then
450 begin
451 //writeln('panel resize!');
452 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
453 end
454 else
455 begin
456 mapGrid.moveBody(proxyId, X, Y);
457 end;
458 g_Mark(X, Y, Width, Height, MARK_WALL);
459 end;
460 end;
461 end;
462 end;
465 var
466 monCheckList: array of TMonster = nil;
467 monCheckListUsed: Integer = 0;
469 procedure TPanel.Update();
470 var
471 ox, oy: Integer;
472 nx, ny, nw, nh: Integer;
473 ex, ey, nex, ney: Integer;
474 mpw, mph: Integer;
476 // return `true` if we should move by dx,dy
477 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
478 var
479 u0: Single;
480 tex, tey: Integer;
481 pdx, pdy: Integer;
482 trtag: Integer;
483 szdx, szdy: Integer;
484 begin
485 squash := false;
486 tex := px;
487 tey := py;
488 pdx := mMovingSpeed.X;
489 pdy := mMovingSpeed.Y;
490 // standing on the platform?
491 if (py+ph = oy) then
492 begin
493 if (ontop <> nil) then ontop^ := true;
494 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
495 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
496 end
497 else
498 begin
499 if (ontop <> nil) then ontop^ := false;
500 // not standing on the platform: trace platform to see if it hits the entity
501 // first, process size change (as we cannot sweeptest both move and size change)
502 // but we don't have to check for pushing if the panel is shrinking
503 szdx := nw-mpw;
504 szdy := nh-mph;
505 if (szdx > 0) or (szdy > 0) then
506 begin
507 // ignore shrinking dimension
508 if (szdx < 0) then szdx := 0;
509 if (szdy < 0) then szdy := 0;
510 // move platform by szd* back, and check for szd* movement
511 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
512 begin
513 // yes, platform hits the entity, push the entity in the resizing direction
514 u0 := 1.0-u0; // how much path left?
515 szdx := trunc(szdx*u0);
516 szdy := trunc(szdy*u0);
517 if (szdx <> 0) or (szdy <> 0) then
518 begin
519 // has some path to go, trace the entity
520 trtag := (GridTagWall or GridTagDoor);
521 // if we're moving down, consider steps too
522 if (szdy > 0) then trtag := trtag or GridTagStep;
523 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
524 end;
525 end;
526 end;
527 // second, process platform movement, using te* as entity starting point
528 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
529 begin
530 //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]);
531 // yes, platform hits the entity, push the entity in the direction of the platform
532 u0 := 1.0-u0; // how much path left?
533 pdx := trunc(pdx*u0);
534 pdy := trunc(pdy*u0);
535 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
536 if (pdx <> 0) or (pdy <> 0) then
537 begin
538 // has some path to go, trace the entity
539 trtag := (GridTagWall or GridTagDoor);
540 // if we're moving down, consider steps too
541 if (pdy > 0) then trtag := trtag or GridTagStep;
542 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
543 end;
544 end;
545 end;
546 // done with entity movement, new coords are in te*
547 dx := tex-px;
548 dy := tey-py;
549 result := (dx <> 0) or (dy <> 0);
550 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
551 begin
552 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
553 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
554 end;
555 end;
557 function monCollect (mon: TMonster): Boolean;
558 begin
559 result := false; // don't stop
560 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
561 monCheckList[monCheckListUsed] := mon;
562 Inc(monCheckListUsed);
563 end;
565 var
566 cx0, cy0, cx1, cy1, cw, ch: Integer;
567 f: Integer;
568 px, py, pw, ph, pdx, pdy: Integer;
569 squash: Boolean;
570 plr: TPlayer;
571 gib: PGib;
572 cor: TCorpse;
573 mon: TMonster;
574 mpfrid: LongWord;
575 ontop: Boolean;
576 actMoveTrig: Boolean;
577 actSizeTrig: Boolean;
578 begin
579 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
581 if (FCurTexture >= 0) and
582 (FTextureIDs[FCurTexture].Anim) and
583 (FTextureIDs[FCurTexture].AnTex <> nil) and
584 (FAlpha < 255) then
585 begin
586 FTextureIDs[FCurTexture].AnTex.Update();
587 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
588 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
589 end;
591 if not g_dbgpan_mplat_active then exit;
593 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
594 mOldMovingActive := mMovingActive;
596 if not mMovingActive then exit;
597 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
599 //TODO: write wall size change processing
601 // moving platform?
602 begin
603 (*
604 * collect all monsters and players (aka entities) along the possible platform path
605 * if entity is standing on a platform:
606 * try to move it along the platform path, checking wall collisions
607 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
608 * try to push entity
609 * if we can't push entity all the way, squash it
610 *)
611 ox := X;
612 oy := Y;
613 mpw := Width;
614 mph := Height;
616 nw := mpw+mSizeSpeed.w;
617 nh := mph+mSizeSpeed.h;
618 nx := ox+mMovingSpeed.X;
619 ny := oy+mMovingSpeed.Y;
621 // force network updates only if some sudden change happened
622 // set the flag here, so we can sync affected monsters
623 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
624 begin
625 mNeedSend := true;
626 end
627 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
628 begin
629 mNeedSend := true;
630 end
631 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
632 begin
633 mNeedSend := true;
634 end;
636 // if pannel disappeared, we don't have to do anything
637 if (nw > 0) and (nh > 0) then
638 begin
639 // old rect
640 ex := ox+mpw-1;
641 ey := ox+mph-1;
642 // new rect
643 nex := nx+nw-1;
644 ney := ny+nh-1;
645 // full rect
646 cx0 := nmin(ox, nx);
647 cy0 := nmin(oy, ny);
648 cx1 := nmax(ex, nex);
649 cy1 := nmax(ey, ney);
650 // extrude
651 cx0 -= 1;
652 cy0 -= 1;
653 cx1 += 1;
654 cy1 += 1;
655 cw := cx1-cx0+1;
656 ch := cy1-cy0+1;
658 // process "obstacle" panels
659 if ((tag and GridTagObstacle) <> 0) then
660 begin
661 // temporarily turn off this panel, so it won't interfere with collision checks
662 mapGrid.proxyEnabled[proxyId] := false;
664 // process players
665 for f := 0 to High(gPlayers) do
666 begin
667 plr := gPlayers[f];
668 if (plr = nil) or (not plr.alive) then continue;
669 plr.getMapBox(px, py, pw, ph);
670 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
671 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
672 begin
673 // set new position
674 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
675 end;
676 // squash player, if necessary
677 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
678 end;
680 // process gibs
681 for f := 0 to High(gGibs) do
682 begin
683 gib := @gGibs[f];
684 if not gib.alive then continue;
685 gib.getMapBox(px, py, pw, ph);
686 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
687 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
688 begin
689 // set new position
690 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
691 end;
692 end;
694 // move and push corpses
695 for f := 0 to High(gCorpses) do
696 begin
697 cor := gCorpses[f];
698 if (cor = nil) then continue;
699 cor.getMapBox(px, py, pw, ph);
700 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
701 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
702 begin
703 // set new position
704 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
705 end;
706 end;
708 // collect monsters
709 monCheckListUsed := 0;
710 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
712 // process collected monsters
713 if (monCheckListUsed > 0) then
714 begin
715 mpfrid := g_Mons_getNewMPlatFrameId();
716 for f := 0 to monCheckListUsed do
717 begin
718 mon := monCheckList[f];
719 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
720 mon.mplatCheckFrameId := mpfrid;
721 mon.getMapBox(px, py, pw, ph);
722 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
723 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
724 begin
725 // set new position
726 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
727 //???FIXME: do we really need to send monsters over the net?
728 // i don't think so, as dead reckoning should take care of 'em
729 // ok, send new monster position only if platform is going to change it's direction
730 if mNeedSend then mon.setDirty();
731 end;
732 // squash monster, if necessary
733 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
734 end;
735 end;
737 // restore panel state
738 mapGrid.proxyEnabled[proxyId] := true;
739 end;
740 end;
742 // move panel
743 X := nx;
744 Y := ny;
745 FWidth := nw;
746 FHeight := nh;
747 positionChanged();
749 actMoveTrig := false;
750 actSizeTrig := false;
752 // `mNeedSend` was set above
754 // check "size stop"
755 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
756 begin
757 mSizeSpeed.w := 0;
758 mSizeSpeed.h := 0;
759 actSizeTrig := true;
760 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
761 end;
763 // reverse moving direction, if necessary
764 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
765 begin
766 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
767 actMoveTrig := true;
768 end;
770 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
771 begin
772 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
773 actMoveTrig := true;
774 end;
776 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
777 mOldMovingActive := mMovingActive;
779 if not g_Game_IsClient then
780 begin
781 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
782 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
783 end;
785 // some triggers may activate this, don't delay sending
786 //TODO: when triggers will be able to control speed and size, check that here too
787 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
788 mOldMovingActive := mMovingActive;
789 end;
790 end;
793 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
795 function ClampInt(X, A, B: Integer): Integer;
796 begin
797 Result := X;
798 if X < A then Result := A else if X > B then Result := B;
799 end;
801 begin
802 if Enabled and (FCurTexture >= 0) and
803 (FTextureIDs[FCurTexture].Anim) and
804 (FTextureIDs[FCurTexture].AnTex <> nil) and
805 (Width > 0) and (Height > 0) and (FAlpha < 255) then
806 begin
807 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
808 FCurFrameCount := Count;
809 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
810 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
811 end;
812 end;
814 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
815 begin
816 Assert(FCurTexture >= -1, 'FCurTexture < -1');
818 // Íåò òåêñòóð:
819 if Length(FTextureIDs) = 0 then
820 FCurTexture := -1
821 else
822 // Òîëüêî îäíà òåêñòóðà:
823 if Length(FTextureIDs) = 1 then
824 begin
825 if FCurTexture = 0 then
826 FCurTexture := -1
827 else
828 FCurTexture := 0;
829 end
830 else
831 // Áîëüøå îäíîé òåêñòóðû:
832 begin
833 // Ñëåäóþùàÿ:
834 Inc(FCurTexture);
835 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
836 if FCurTexture >= Length(FTextureIDs) then
837 FCurTexture := 0;
838 end;
840 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
841 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
842 begin
843 if (FTextureIDs[FCurTexture].AnTex = nil) then
844 begin
845 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
846 Exit;
847 end;
849 if AnimLoop = 1 then
850 FTextureIDs[FCurTexture].AnTex.Loop := True
851 else
852 if AnimLoop = 2 then
853 FTextureIDs[FCurTexture].AnTex.Loop := False;
855 FTextureIDs[FCurTexture].AnTex.Reset();
856 end;
858 LastAnimLoop := AnimLoop;
859 end;
861 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
862 begin
863 if (ID >= -1) and (ID < Length(FTextureIDs)) then
864 FCurTexture := ID;
866 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
867 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
868 begin
869 if (FTextureIDs[FCurTexture].AnTex = nil) then
870 begin
871 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
872 Exit;
873 end;
875 if AnimLoop = 1 then
876 FTextureIDs[FCurTexture].AnTex.Loop := True
877 else
878 if AnimLoop = 2 then
879 FTextureIDs[FCurTexture].AnTex.Loop := False;
881 FTextureIDs[FCurTexture].AnTex.Reset();
882 end;
884 LastAnimLoop := AnimLoop;
885 end;
887 function TPanel.GetTextureID(): DWORD;
888 begin
889 Result := LongWord(TEXTURE_NONE);
890 if (FCurTexture >= 0) then
891 begin
892 if FTextureIDs[FCurTexture].Anim then
893 Result := Textures[FTextureIDs[FCurTexture].Texture].FramesID
894 else
895 Result := Textures[FTextureIDs[FCurTexture].Texture].TextureID
897 // !!! old behavior
898 if FTextureIDs[FCurTexture].Anim then
899 Result := FTextureIDs[FCurTexture].AnTex.FramesID
900 else
901 Result := FTextureIDs[FCurTexture].Tex;
903 end
904 end;
906 function TPanel.GetTextureCount(): Integer;
907 begin
908 Result := Length(FTextureIDs);
909 if Enabled and (FCurTexture >= 0) then
910 if (FTextureIDs[FCurTexture].Anim) and
911 (FTextureIDs[FCurTexture].AnTex <> nil) and
912 (Width > 0) and (Height > 0) and (FAlpha < 255) then
913 Result := Result + 100;
914 end;
916 function TPanel.CanChangeTexture(): Boolean;
917 begin
918 Result := (GetTextureCount() > 1) or hasTexTrigger;
919 end;
921 const
922 PAN_SAVE_VERSION = 1;
924 procedure TPanel.SaveState (st: TStream);
925 var
926 anim: Boolean;
927 begin
928 if (st = nil) then exit;
930 // Ñèãíàòóðà ïàíåëè
931 utils.writeSign(st, 'PANL');
932 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
933 // Îòêðûòà/çàêðûòà, åñëè äâåðü
934 utils.writeBool(st, FEnabled);
935 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
936 utils.writeInt(st, Byte(FLiftType));
937 // Íîìåð òåêóùåé òåêñòóðû
938 utils.writeInt(st, Integer(FCurTexture));
939 // Êîîðäèíàòû è ðàçìåð
940 utils.writeInt(st, Integer(FX));
941 utils.writeInt(st, Integer(FY));
942 utils.writeInt(st, Word(FWidth));
943 utils.writeInt(st, Word(FHeight));
944 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
945 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
946 begin
947 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
948 anim := true;
949 end
950 else
951 begin
952 anim := false;
953 end;
954 utils.writeBool(st, anim);
955 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
956 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st);
958 // moving platform state
959 utils.writeInt(st, Integer(mMovingSpeed.X));
960 utils.writeInt(st, Integer(mMovingSpeed.Y));
961 utils.writeInt(st, Integer(mMovingStart.X));
962 utils.writeInt(st, Integer(mMovingStart.Y));
963 utils.writeInt(st, Integer(mMovingEnd.X));
964 utils.writeInt(st, Integer(mMovingEnd.Y));
966 utils.writeInt(st, Integer(mSizeSpeed.w));
967 utils.writeInt(st, Integer(mSizeSpeed.h));
968 utils.writeInt(st, Integer(mSizeEnd.w));
969 utils.writeInt(st, Integer(mSizeEnd.h));
971 utils.writeBool(st, mMovingActive);
972 utils.writeBool(st, mMoveOnce);
974 utils.writeInt(st, Integer(mEndPosTrig));
975 utils.writeInt(st, Integer(mEndSizeTrig));
976 end;
979 procedure TPanel.LoadState (st: TStream);
980 begin
981 if (st = nil) then exit;
983 // Ñèãíàòóðà ïàíåëè
984 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
985 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
986 // Îòêðûòà/çàêðûòà, åñëè äâåðü
987 FEnabled := utils.readBool(st);
988 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
989 FLiftType := utils.readByte(st);
990 // Íîìåð òåêóùåé òåêñòóðû
991 FCurTexture := utils.readLongInt(st);
992 // Êîîðäèíàòû è ðàçìåð
993 FX := utils.readLongInt(st);
994 FY := utils.readLongInt(st);
995 FWidth := utils.readWord(st);
996 FHeight := utils.readWord(st);
997 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
998 if utils.readBool(st) then
999 begin
1000 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1001 Assert((FCurTexture >= 0) and
1002 (FTextureIDs[FCurTexture].Anim) and
1003 (FTextureIDs[FCurTexture].AnTex <> nil),
1004 'TPanel.LoadState: No animation object');
1005 FTextureIDs[FCurTexture].AnTex.LoadState(st);
1006 end;
1008 // moving platform state
1009 mMovingSpeed.X := utils.readLongInt(st);
1010 mMovingSpeed.Y := utils.readLongInt(st);
1011 mMovingStart.X := utils.readLongInt(st);
1012 mMovingStart.Y := utils.readLongInt(st);
1013 mMovingEnd.X := utils.readLongInt(st);
1014 mMovingEnd.Y := utils.readLongInt(st);
1016 mSizeSpeed.w := utils.readLongInt(st);
1017 mSizeSpeed.h := utils.readLongInt(st);
1018 mSizeEnd.w := utils.readLongInt(st);
1019 mSizeEnd.h := utils.readLongInt(st);
1021 mMovingActive := utils.readBool(st);
1022 mMoveOnce := utils.readBool(st);
1024 mEndPosTrig := utils.readLongInt(st);
1025 mEndSizeTrig := utils.readLongInt(st);
1027 positionChanged();
1028 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1029 end;
1032 end.