DEADSOFTWARE

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