DEADSOFTWARE

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