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 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 {$INCLUDE ../nogl/noGLuses.inc}
226 e_texture, g_basic, g_map, g_game, g_gfx, g_weapons, g_triggers,
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 Width := PanelRec.Width;
245 Height := PanelRec.Height;
246 FAlpha := 0;
247 FBlending := False;
248 FCurFrame := 0;
249 FCurFrameCount := 0;
250 LastAnimLoop := 0;
252 mapId := PanelRec.id;
253 mGUID := aguid;
255 mMovingSpeed := PanelRec.moveSpeed;
256 mMovingStart := PanelRec.moveStart;
257 mMovingEnd := PanelRec.moveEnd;
258 mMovingActive := PanelRec['move_active'].value;
259 mOldMovingActive := mMovingActive;
260 mMoveOnce := PanelRec.moveOnce;
262 mSizeSpeed := PanelRec.sizeSpeed;
263 mSizeEnd := PanelRec.sizeEnd;
265 mEndPosTrig := PanelRec.endPosTrig;
266 mEndSizeTrig := PanelRec.endSizeTrig;
268 mNeedSend := false;
270 // Òèï ïàíåëè:
271 PanelType := PanelRec.PanelType;
272 Enabled := True;
273 Door := False;
274 LiftType := LIFTTYPE_UP;
275 hasTexTrigger := False;
277 case PanelType of
278 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
279 PANEL_CLOSEDOOR: Door := True;
280 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
281 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
282 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
283 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
284 end;
286 // Íåâèäèìàÿ:
287 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
288 begin
289 SetLength(FTextureIDs, 0);
290 FCurTexture := -1;
291 Exit;
292 end;
293 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
294 if ByteBool(PanelType and
295 (PANEL_LIFTUP or
296 PANEL_LIFTDOWN or
297 PANEL_LIFTLEFT or
298 PANEL_LIFTRIGHT or
299 PANEL_BLOCKMON)) then
300 begin
301 SetLength(FTextureIDs, 0);
302 FCurTexture := -1;
303 Exit;
304 end;
306 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
307 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
308 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
309 begin
310 SetLength(FTextureIDs, 1);
311 FTextureIDs[0].Anim := False;
313 case PanelRec.PanelType of
314 PANEL_WATER:
315 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_WATER);
316 PANEL_ACID1:
317 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID1);
318 PANEL_ACID2:
319 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID2);
320 end;
322 FCurTexture := 0;
323 Exit;
324 end;
326 SetLength(FTextureIDs, Length(AddTextures));
328 if CurTex < 0 then
329 FCurTexture := -1
330 else
331 if CurTex >= Length(FTextureIDs) then
332 FCurTexture := Length(FTextureIDs) - 1
333 else
334 FCurTexture := CurTex;
336 for i := 0 to Length(FTextureIDs)-1 do
337 begin
338 FTextureIDs[i].Anim := AddTextures[i].Anim;
339 if FTextureIDs[i].Anim then
340 begin // Àíèìèðîâàííàÿ òåêñòóðà
341 FTextureIDs[i].AnTex :=
342 TAnimation.Create(Textures[AddTextures[i].Texture].FramesID,
343 True, Textures[AddTextures[i].Texture].Speed);
344 FTextureIDs[i].AnTex.Blending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
345 FTextureIDs[i].AnTex.Alpha := PanelRec.Alpha;
346 end
347 else
348 begin // Îáû÷íàÿ òåêñòóðà
349 FTextureIDs[i].Tex := Textures[AddTextures[i].Texture].TextureID;
350 end;
351 end;
353 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
354 //if Length(FTextureIDs) > 1 then SaveIt := True;
356 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
357 if (tnum < 0) then tnum := Length(Textures);
359 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
360 if ({PanelRec.TextureNum}tnum > High(Textures)) then
361 begin
362 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
363 FTextureWidth := 2;
364 FTextureHeight := 2;
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 FTextureWidth := Textures[{PanelRec.TextureNum}tnum].Width;
371 FTextureHeight := Textures[{PanelRec.TextureNum}tnum].Height;
372 FAlpha := PanelRec.Alpha;
373 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
374 end;
375 end;
377 destructor TPanel.Destroy();
378 var
379 i: Integer;
380 begin
381 for i := 0 to High(FTextureIDs) do
382 if FTextureIDs[i].Anim then
383 FTextureIDs[i].AnTex.Free();
384 SetLength(FTextureIDs, 0);
386 Inherited;
387 end;
389 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
390 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
391 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
393 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
394 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
395 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
396 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
398 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
399 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
400 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
401 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
403 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
404 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
405 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
406 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
408 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
409 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
410 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
411 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
413 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
414 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
415 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
416 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
418 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
419 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
420 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
421 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
422 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
423 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
424 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
425 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
426 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
428 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
429 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
431 procedure TPanel.positionChanged (); inline;
432 var
433 px, py, pw, ph: Integer;
434 begin
435 if (proxyId >= 0) then
436 begin
437 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
438 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
439 begin
441 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
442 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
444 g_Mark(px, py, pw, ph, MARK_WALL, false);
445 if (Width < 1) or (Height < 1) then
446 begin
447 mapGrid.proxyEnabled[proxyId] := false;
448 end
449 else
450 begin
451 mapGrid.proxyEnabled[proxyId] := Enabled;
452 if (pw <> Width) or (ph <> Height) then
453 begin
454 //writeln('panel resize!');
455 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
456 end
457 else
458 begin
459 mapGrid.moveBody(proxyId, X, Y);
460 end;
461 g_Mark(X, Y, Width, Height, MARK_WALL);
462 end;
463 end;
464 end;
465 end;
468 var
469 monCheckList: array of TMonster = nil;
470 monCheckListUsed: Integer = 0;
472 procedure TPanel.Update();
473 var
474 ox, oy: Integer;
475 nx, ny, nw, nh: Integer;
476 ex, ey, nex, ney: Integer;
477 mpw, mph: Integer;
479 // return `true` if we should move by dx,dy
480 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
481 var
482 u0: Single;
483 tex, tey: Integer;
484 pdx, pdy: Integer;
485 trtag: Integer;
486 szdx, szdy: Integer;
487 begin
488 squash := false;
489 tex := px;
490 tey := py;
491 pdx := mMovingSpeed.X;
492 pdy := mMovingSpeed.Y;
493 // standing on the platform?
494 if (py+ph = oy) then
495 begin
496 if (ontop <> nil) then ontop^ := true;
497 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
498 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
499 end
500 else
501 begin
502 if (ontop <> nil) then ontop^ := false;
503 // not standing on the platform: trace platform to see if it hits the entity
504 // first, process size change (as we cannot sweeptest both move and size change)
505 // but we don't have to check for pushing if the panel is shrinking
506 szdx := nw-mpw;
507 szdy := nh-mph;
508 if (szdx > 0) or (szdy > 0) then
509 begin
510 // ignore shrinking dimension
511 if (szdx < 0) then szdx := 0;
512 if (szdy < 0) then szdy := 0;
513 // move platform by szd* back, and check for szd* movement
514 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
515 begin
516 // yes, platform hits the entity, push the entity in the resizing direction
517 u0 := 1.0-u0; // how much path left?
518 szdx := trunc(szdx*u0);
519 szdy := trunc(szdy*u0);
520 if (szdx <> 0) or (szdy <> 0) then
521 begin
522 // has some path to go, trace the entity
523 trtag := (GridTagWall or GridTagDoor);
524 // if we're moving down, consider steps too
525 if (szdy > 0) then trtag := trtag or GridTagStep;
526 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
527 end;
528 end;
529 end;
530 // second, process platform movement, using te* as entity starting point
531 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
532 begin
533 //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]);
534 // yes, platform hits the entity, push the entity in the direction of the platform
535 u0 := 1.0-u0; // how much path left?
536 pdx := trunc(pdx*u0);
537 pdy := trunc(pdy*u0);
538 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
539 if (pdx <> 0) or (pdy <> 0) then
540 begin
541 // has some path to go, trace the entity
542 trtag := (GridTagWall or GridTagDoor);
543 // if we're moving down, consider steps too
544 if (pdy > 0) then trtag := trtag or GridTagStep;
545 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
546 end;
547 end;
548 end;
549 // done with entity movement, new coords are in te*
550 dx := tex-px;
551 dy := tey-py;
552 result := (dx <> 0) or (dy <> 0);
553 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
554 begin
555 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
556 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
557 end;
558 end;
560 function monCollect (mon: TMonster): Boolean;
561 begin
562 result := false; // don't stop
563 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
564 monCheckList[monCheckListUsed] := mon;
565 Inc(monCheckListUsed);
566 end;
568 var
569 cx0, cy0, cx1, cy1, cw, ch: Integer;
570 f: Integer;
571 px, py, pw, ph, pdx, pdy: Integer;
572 squash: Boolean;
573 plr: TPlayer;
574 gib: PGib;
575 cor: TCorpse;
576 mon: TMonster;
577 mpfrid: LongWord;
578 ontop: Boolean;
579 actMoveTrig: Boolean;
580 actSizeTrig: Boolean;
581 begin
582 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
584 if (FCurTexture >= 0) and
585 (FTextureIDs[FCurTexture].Anim) and
586 (FTextureIDs[FCurTexture].AnTex <> nil) and
587 (FAlpha < 255) then
588 begin
589 FTextureIDs[FCurTexture].AnTex.Update();
590 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
591 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
592 end;
594 if not g_dbgpan_mplat_active then exit;
596 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
597 mOldMovingActive := mMovingActive;
599 if not mMovingActive then exit;
600 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
602 //TODO: write wall size change processing
604 // moving platform?
605 begin
606 (*
607 * collect all monsters and players (aka entities) along the possible platform path
608 * if entity is standing on a platform:
609 * try to move it along the platform path, checking wall collisions
610 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
611 * try to push entity
612 * if we can't push entity all the way, squash it
613 *)
614 ox := X;
615 oy := Y;
616 mpw := Width;
617 mph := Height;
619 nw := mpw+mSizeSpeed.w;
620 nh := mph+mSizeSpeed.h;
621 nx := ox+mMovingSpeed.X;
622 ny := oy+mMovingSpeed.Y;
624 // force network updates only if some sudden change happened
625 // set the flag here, so we can sync affected monsters
626 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
627 begin
628 mNeedSend := true;
629 end
630 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
631 begin
632 mNeedSend := true;
633 end
634 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
635 begin
636 mNeedSend := true;
637 end;
639 // if pannel disappeared, we don't have to do anything
640 if (nw > 0) and (nh > 0) then
641 begin
642 // old rect
643 ex := ox+mpw-1;
644 ey := ox+mph-1;
645 // new rect
646 nex := nx+nw-1;
647 ney := ny+nh-1;
648 // full rect
649 cx0 := nmin(ox, nx);
650 cy0 := nmin(oy, ny);
651 cx1 := nmax(ex, nex);
652 cy1 := nmax(ey, ney);
653 // extrude
654 cx0 -= 1;
655 cy0 -= 1;
656 cx1 += 1;
657 cy1 += 1;
658 cw := cx1-cx0+1;
659 ch := cy1-cy0+1;
661 // process "obstacle" panels
662 if ((tag and GridTagObstacle) <> 0) then
663 begin
664 // temporarily turn off this panel, so it won't interfere with collision checks
665 mapGrid.proxyEnabled[proxyId] := false;
667 // process players
668 for f := 0 to High(gPlayers) do
669 begin
670 plr := gPlayers[f];
671 if (plr = nil) or (not plr.alive) then continue;
672 plr.getMapBox(px, py, pw, ph);
673 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
674 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
675 begin
676 // set new position
677 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
678 end;
679 // squash player, if necessary
680 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
681 end;
683 // process gibs
684 for f := 0 to High(gGibs) do
685 begin
686 gib := @gGibs[f];
687 if not gib.alive then continue;
688 gib.getMapBox(px, py, pw, ph);
689 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
690 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
691 begin
692 // set new position
693 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
694 end;
695 end;
697 // move and push corpses
698 for f := 0 to High(gCorpses) do
699 begin
700 cor := gCorpses[f];
701 if (cor = nil) then continue;
702 cor.getMapBox(px, py, pw, ph);
703 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
704 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
705 begin
706 // set new position
707 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
708 end;
709 end;
711 // collect monsters
712 monCheckListUsed := 0;
713 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
715 // process collected monsters
716 if (monCheckListUsed > 0) then
717 begin
718 mpfrid := g_Mons_getNewMPlatFrameId();
719 for f := 0 to monCheckListUsed do
720 begin
721 mon := monCheckList[f];
722 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
723 mon.mplatCheckFrameId := mpfrid;
724 mon.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) then
727 begin
728 // set new position
729 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
730 //???FIXME: do we really need to send monsters over the net?
731 // i don't think so, as dead reckoning should take care of 'em
732 // ok, send new monster position only if platform is going to change it's direction
733 if mNeedSend then mon.setDirty();
734 end;
735 // squash monster, if necessary
736 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
737 end;
738 end;
740 // restore panel state
741 mapGrid.proxyEnabled[proxyId] := true;
742 end;
743 end;
745 // move panel
746 X := nx;
747 Y := ny;
748 FWidth := nw;
749 FHeight := nh;
750 positionChanged();
752 actMoveTrig := false;
753 actSizeTrig := false;
755 // `mNeedSend` was set above
757 // check "size stop"
758 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
759 begin
760 mSizeSpeed.w := 0;
761 mSizeSpeed.h := 0;
762 actSizeTrig := true;
763 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
764 end;
766 // reverse moving direction, if necessary
767 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
768 begin
769 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
770 actMoveTrig := true;
771 end;
773 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
774 begin
775 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
776 actMoveTrig := true;
777 end;
779 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
780 mOldMovingActive := mMovingActive;
782 if not g_Game_IsClient then
783 begin
784 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
785 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
786 end;
788 // some triggers may activate this, don't delay sending
789 //TODO: when triggers will be able to control speed and size, check that here too
790 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
791 mOldMovingActive := mMovingActive;
792 end;
793 end;
796 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
798 function ClampInt(X, A, B: Integer): Integer;
799 begin
800 Result := X;
801 if X < A then Result := A else if X > B then Result := B;
802 end;
804 begin
805 if Enabled and (FCurTexture >= 0) and
806 (FTextureIDs[FCurTexture].Anim) and
807 (FTextureIDs[FCurTexture].AnTex <> nil) and
808 (Width > 0) and (Height > 0) and (FAlpha < 255) then
809 begin
810 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
811 FCurFrameCount := Count;
812 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
813 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
814 end;
815 end;
817 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
818 begin
819 Assert(FCurTexture >= -1, 'FCurTexture < -1');
821 // Íåò òåêñòóð:
822 if Length(FTextureIDs) = 0 then
823 FCurTexture := -1
824 else
825 // Òîëüêî îäíà òåêñòóðà:
826 if Length(FTextureIDs) = 1 then
827 begin
828 if FCurTexture = 0 then
829 FCurTexture := -1
830 else
831 FCurTexture := 0;
832 end
833 else
834 // Áîëüøå îäíîé òåêñòóðû:
835 begin
836 // Ñëåäóþùàÿ:
837 Inc(FCurTexture);
838 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
839 if FCurTexture >= Length(FTextureIDs) then
840 FCurTexture := 0;
841 end;
843 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
844 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
845 begin
846 if (FTextureIDs[FCurTexture].AnTex = nil) then
847 begin
848 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
849 Exit;
850 end;
852 if AnimLoop = 1 then
853 FTextureIDs[FCurTexture].AnTex.Loop := True
854 else
855 if AnimLoop = 2 then
856 FTextureIDs[FCurTexture].AnTex.Loop := False;
858 FTextureIDs[FCurTexture].AnTex.Reset();
859 end;
861 LastAnimLoop := AnimLoop;
862 end;
864 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
865 begin
866 if (ID >= -1) and (ID < Length(FTextureIDs)) then
867 FCurTexture := ID;
869 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
870 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
871 begin
872 if (FTextureIDs[FCurTexture].AnTex = nil) then
873 begin
874 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
875 Exit;
876 end;
878 if AnimLoop = 1 then
879 FTextureIDs[FCurTexture].AnTex.Loop := True
880 else
881 if AnimLoop = 2 then
882 FTextureIDs[FCurTexture].AnTex.Loop := False;
884 FTextureIDs[FCurTexture].AnTex.Reset();
885 end;
887 LastAnimLoop := AnimLoop;
888 end;
890 function TPanel.GetTextureID(): DWORD;
891 begin
892 Result := LongWord(TEXTURE_NONE);
894 if (FCurTexture >= 0) then
895 begin
896 if FTextureIDs[FCurTexture].Anim then
897 Result := FTextureIDs[FCurTexture].AnTex.FramesID
898 else
899 Result := FTextureIDs[FCurTexture].Tex;
900 end;
901 end;
903 function TPanel.GetTextureCount(): Integer;
904 begin
905 Result := Length(FTextureIDs);
906 if Enabled and (FCurTexture >= 0) then
907 if (FTextureIDs[FCurTexture].Anim) and
908 (FTextureIDs[FCurTexture].AnTex <> nil) and
909 (Width > 0) and (Height > 0) and (FAlpha < 255) then
910 Result := Result + 100;
911 end;
913 function TPanel.CanChangeTexture(): Boolean;
914 begin
915 Result := (GetTextureCount() > 1) or hasTexTrigger;
916 end;
918 const
919 PAN_SAVE_VERSION = 1;
921 procedure TPanel.SaveState (st: TStream);
922 var
923 anim: Boolean;
924 begin
925 if (st = nil) then exit;
927 // Ñèãíàòóðà ïàíåëè
928 utils.writeSign(st, 'PANL');
929 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
930 // Îòêðûòà/çàêðûòà, åñëè äâåðü
931 utils.writeBool(st, FEnabled);
932 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
933 utils.writeInt(st, Byte(FLiftType));
934 // Íîìåð òåêóùåé òåêñòóðû
935 utils.writeInt(st, Integer(FCurTexture));
936 // Êîîðäèíàòû è ðàçìåð
937 utils.writeInt(st, Integer(FX));
938 utils.writeInt(st, Integer(FY));
939 utils.writeInt(st, Word(FWidth));
940 utils.writeInt(st, Word(FHeight));
941 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
942 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
943 begin
944 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
945 anim := true;
946 end
947 else
948 begin
949 anim := false;
950 end;
951 utils.writeBool(st, anim);
952 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
953 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st);
955 // moving platform state
956 utils.writeInt(st, Integer(mMovingSpeed.X));
957 utils.writeInt(st, Integer(mMovingSpeed.Y));
958 utils.writeInt(st, Integer(mMovingStart.X));
959 utils.writeInt(st, Integer(mMovingStart.Y));
960 utils.writeInt(st, Integer(mMovingEnd.X));
961 utils.writeInt(st, Integer(mMovingEnd.Y));
963 utils.writeInt(st, Integer(mSizeSpeed.w));
964 utils.writeInt(st, Integer(mSizeSpeed.h));
965 utils.writeInt(st, Integer(mSizeEnd.w));
966 utils.writeInt(st, Integer(mSizeEnd.h));
968 utils.writeBool(st, mMovingActive);
969 utils.writeBool(st, mMoveOnce);
971 utils.writeInt(st, Integer(mEndPosTrig));
972 utils.writeInt(st, Integer(mEndSizeTrig));
973 end;
976 procedure TPanel.LoadState (st: TStream);
977 begin
978 if (st = nil) then exit;
980 // Ñèãíàòóðà ïàíåëè
981 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
982 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
983 // Îòêðûòà/çàêðûòà, åñëè äâåðü
984 FEnabled := utils.readBool(st);
985 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
986 FLiftType := utils.readByte(st);
987 // Íîìåð òåêóùåé òåêñòóðû
988 FCurTexture := utils.readLongInt(st);
989 // Êîîðäèíàòû è ðàçìåð
990 FX := utils.readLongInt(st);
991 FY := utils.readLongInt(st);
992 FWidth := utils.readWord(st);
993 FHeight := utils.readWord(st);
994 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
995 if utils.readBool(st) then
996 begin
997 // Åñëè äà - çàãðóæàåì àíèìàöèþ
998 Assert((FCurTexture >= 0) and
999 (FTextureIDs[FCurTexture].Anim) and
1000 (FTextureIDs[FCurTexture].AnTex <> nil),
1001 'TPanel.LoadState: No animation object');
1002 FTextureIDs[FCurTexture].AnTex.LoadState(st);
1003 end;
1005 // moving platform state
1006 mMovingSpeed.X := utils.readLongInt(st);
1007 mMovingSpeed.Y := utils.readLongInt(st);
1008 mMovingStart.X := utils.readLongInt(st);
1009 mMovingStart.Y := utils.readLongInt(st);
1010 mMovingEnd.X := utils.readLongInt(st);
1011 mMovingEnd.Y := utils.readLongInt(st);
1013 mSizeSpeed.w := utils.readLongInt(st);
1014 mSizeSpeed.h := utils.readLongInt(st);
1015 mSizeEnd.w := utils.readLongInt(st);
1016 mSizeEnd.h := utils.readLongInt(st);
1018 mMovingActive := utils.readBool(st);
1019 mMoveOnce := utils.readBool(st);
1021 mEndPosTrig := utils.readLongInt(st);
1022 mEndSizeTrig := utils.readLongInt(st);
1024 positionChanged();
1025 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1026 end;
1029 end.