DEADSOFTWARE

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