DEADSOFTWARE

game: disable gibs for server
[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: TAnimationState);
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 g_basic, g_map, g_game, g_weapons, g_triggers, g_items,
233 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams
236 const
237 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
239 { T P a n e l : }
241 function FindTextureByName (const name: String): Integer;
242 var i: Integer;
243 begin
244 Result := -1;
245 if Textures <> nil then
246 begin
247 for i := 0 to High(Textures) do
248 begin
249 if Textures[i].TextureName = name then
250 begin
251 Result := i;
252 break;
253 end
254 end
255 end
256 end;
258 constructor TPanel.Create(PanelRec: TDynRecord;
259 AddTextures: TAddTextureArray;
260 CurTex: Integer;
261 var Textures: TLevelTextureArray; aguid: Integer);
262 var
263 i: Integer;
264 tnum: Integer;
265 begin
266 X := PanelRec.X;
267 Y := PanelRec.Y;
268 FOldX := X;
269 FOldY := Y;
270 Width := PanelRec.Width;
271 Height := PanelRec.Height;
272 FOldW := Width;
273 FOldH := Height;
274 FAlpha := 0;
275 FBlending := False;
276 FCurFrame := 0;
277 FCurFrameCount := 0;
278 LastAnimLoop := 0;
280 mapId := PanelRec.id;
281 mGUID := aguid;
283 mMovingSpeed := PanelRec.moveSpeed;
284 mMovingStart := PanelRec.moveStart;
285 mMovingEnd := PanelRec.moveEnd;
286 mMovingActive := PanelRec['move_active'].value;
287 mOldMovingActive := mMovingActive;
288 mMoveOnce := PanelRec.moveOnce;
290 mSizeSpeed := PanelRec.sizeSpeed;
291 mSizeEnd := PanelRec.sizeEnd;
293 mEndPosTrig := PanelRec.endPosTrig;
294 mEndSizeTrig := PanelRec.endSizeTrig;
296 mNeedSend := false;
298 // Òèï ïàíåëè:
299 PanelType := PanelRec.PanelType;
300 Enabled := True;
301 Door := False;
302 LiftType := LIFTTYPE_UP;
303 hasTexTrigger := False;
305 case PanelType of
306 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
307 PANEL_CLOSEDOOR: Door := True;
308 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
309 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
310 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
311 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
312 end;
314 // Íåâèäèìàÿ:
315 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
316 begin
317 SetLength(FTextureIDs, 0);
318 FCurTexture := -1;
319 Exit;
320 end;
321 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
322 if ByteBool(PanelType and
323 (PANEL_LIFTUP or
324 PANEL_LIFTDOWN or
325 PANEL_LIFTLEFT or
326 PANEL_LIFTRIGHT or
327 PANEL_BLOCKMON)) then
328 begin
329 SetLength(FTextureIDs, 0);
330 FCurTexture := -1;
331 Exit;
332 end;
334 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
335 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
336 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
337 begin
338 SetLength(FTextureIDs, 1);
339 FTextureIDs[0].Anim := False;
340 case PanelRec.PanelType of
341 PANEL_WATER: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_WATER);
342 PANEL_ACID1: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID1);
343 PANEL_ACID2: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID2);
344 end;
345 FCurTexture := 0;
346 Exit;
347 end;
349 SetLength(FTextureIDs, Length(AddTextures));
351 if CurTex < 0 then
352 FCurTexture := -1
353 else
354 if CurTex >= Length(FTextureIDs) then
355 FCurTexture := Length(FTextureIDs) - 1
356 else
357 FCurTexture := CurTex;
359 for i := 0 to Length(FTextureIDs)-1 do
360 begin
361 FTextureIDs[i].Texture := AddTextures[i].Texture;
362 FTextureIDs[i].Anim := Textures[AddTextures[i].Texture].FramesCount > 0;
363 if FTextureIDs[i].Anim then
364 begin // Àíèìèðîâàííàÿ òåêñòóðà
365 FTextureIDs[i].AnTex := TAnimationState.Create(True, Textures[AddTextures[i].Texture].Speed, Textures[AddTextures[i].Texture].FramesCount);
366 end
367 end;
369 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
370 //if Length(FTextureIDs) > 1 then SaveIt := True;
372 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
373 if (tnum < 0) then tnum := Length(Textures);
375 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
376 if ({PanelRec.TextureNum}tnum > High(Textures)) then
377 begin
378 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
379 FAlpha := 0;
380 FBlending := ByteBool(0);
381 end
382 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
383 begin
384 FAlpha := PanelRec.Alpha;
385 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
386 end;
387 end;
389 destructor TPanel.Destroy();
390 var
391 i: Integer;
392 begin
393 for i := 0 to High(FTextureIDs) do
394 if FTextureIDs[i].Anim then
395 FTextureIDs[i].AnTex.Free();
396 SetLength(FTextureIDs, 0);
398 Inherited;
399 end;
401 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
402 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
403 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
405 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
406 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
407 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
408 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
410 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
411 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
412 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
413 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
415 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
416 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
417 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
418 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
420 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
421 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
422 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
423 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
425 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
426 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
427 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
428 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
430 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
431 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
432 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
433 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
434 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
435 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
436 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
437 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
438 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
440 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
441 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
443 procedure TPanel.positionChanged (); inline;
444 var
445 px, py, pw, ph: Integer;
446 begin
447 if (proxyId >= 0) then
448 begin
449 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
450 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
451 begin
453 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
454 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
456 {$IFDEF ENABLE_GFX}
457 g_Mark(px, py, pw, ph, MARK_WALL, false);
458 {$ENDIF}
459 if (Width < 1) or (Height < 1) then
460 begin
461 mapGrid.proxyEnabled[proxyId] := false;
462 end
463 else
464 begin
465 mapGrid.proxyEnabled[proxyId] := Enabled;
466 if (pw <> Width) or (ph <> Height) then
467 begin
468 //writeln('panel resize!');
469 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
470 end
471 else
472 begin
473 mapGrid.moveBody(proxyId, X, Y);
474 end;
475 {$IFDEF ENABLE_GFX}
476 g_Mark(X, Y, Width, Height, MARK_WALL);
477 {$ENDIF}
478 end;
479 end;
480 end;
481 end;
484 var
485 monCheckList: array of TMonster = nil;
486 monCheckListUsed: Integer = 0;
488 procedure TPanel.Update();
489 var
490 ox, oy: Integer;
491 nx, ny, nw, nh: Integer;
492 ex, ey, nex, ney: Integer;
493 mpw, mph: Integer;
494 conveyor: Boolean;
496 // return `true` if we should move by dx,dy
497 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
498 var
499 u0: Single;
500 tex, tey: Integer;
501 pdx, pdy: Integer;
502 trtag: Integer;
503 szdx, szdy: Integer;
504 begin
505 squash := false;
506 tex := px;
507 tey := py;
508 pdx := mMovingSpeed.X;
509 pdy := mMovingSpeed.Y;
510 // standing on the platform?
511 if (py+ph = oy) then
512 begin
513 if (ontop <> nil) then ontop^ := true;
514 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
515 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
516 end
517 else
518 begin
519 if (ontop <> nil) then ontop^ := false;
520 // not standing on the platform: trace platform to see if it hits the entity
521 // first, process size change (as we cannot sweeptest both move and size change)
522 // but we don't have to check for pushing if the panel is shrinking
523 szdx := nw-mpw;
524 szdy := nh-mph;
525 if (szdx > 0) or (szdy > 0) then
526 begin
527 // ignore shrinking dimension
528 if (szdx < 0) then szdx := 0;
529 if (szdy < 0) then szdy := 0;
530 // move platform by szd* back, and check for szd* movement
531 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
532 begin
533 // yes, platform hits the entity, push the entity in the resizing direction
534 u0 := 1.0-u0; // how much path left?
535 szdx := trunc(szdx*u0);
536 szdy := trunc(szdy*u0);
537 if (szdx <> 0) or (szdy <> 0) then
538 begin
539 // has some path to go, trace the entity
540 trtag := (GridTagWall or GridTagDoor);
541 // if we're moving down, consider steps too
542 if (szdy > 0) then trtag := trtag or GridTagStep;
543 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
544 end;
545 end;
546 end;
547 // second, process platform movement, using te* as entity starting point
548 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
549 begin
550 //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]);
551 // yes, platform hits the entity, push the entity in the direction of the platform
552 u0 := 1.0-u0; // how much path left?
553 pdx := trunc(pdx*u0);
554 pdy := trunc(pdy*u0);
555 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
556 if (pdx <> 0) or (pdy <> 0) then
557 begin
558 // has some path to go, trace the entity
559 trtag := (GridTagWall or GridTagDoor);
560 // if we're moving down, consider steps too
561 if (pdy > 0) then trtag := trtag or GridTagStep;
562 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
563 end;
564 end;
565 end;
566 // done with entity movement, new coords are in te*
567 dx := tex-px;
568 dy := tey-py;
569 result := (dx <> 0) or (dy <> 0);
570 if not conveyor and ((tag and (GridTagWall or GridTagDoor)) <> 0) then
571 begin
572 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
573 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
574 end;
575 end;
577 function monCollect (mon: TMonster): Boolean;
578 begin
579 result := false; // don't stop
580 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
581 monCheckList[monCheckListUsed] := mon;
582 Inc(monCheckListUsed);
583 end;
585 var
586 cx0, cy0, cx1, cy1, cw, ch: Integer;
587 f: Integer;
588 px, py, pw, ph, pdx, pdy: Integer;
589 squash: Boolean;
590 plr: TPlayer;
591 {$IFDEF ENABLE_GIBS}
592 gib: PGib;
593 {$ENDIF}
594 cor: TCorpse;
595 mon: TMonster;
596 flg: PFlag;
597 itm: PItem;
598 mpfrid: LongWord;
599 ontop: Boolean;
600 actMoveTrig: Boolean;
601 actSizeTrig: Boolean;
602 begin
603 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
605 if (FCurTexture >= 0) and
606 (FTextureIDs[FCurTexture].Anim) and
607 (FTextureIDs[FCurTexture].AnTex <> nil) and
608 (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 // move and push corpses
730 for f := 0 to High(gCorpses) do
731 begin
732 cor := gCorpses[f];
733 if (cor = nil) then continue;
734 cor.getMapBox(px, py, pw, ph);
735 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
736 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
737 begin
738 // set new position
739 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
740 end;
741 end;
743 // move and push flags
744 if gGameSettings.GameMode = GM_CTF then
745 for f := FLAG_RED to FLAG_BLUE do
746 begin
747 flg := @gFlags[f];
748 if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
749 px := flg.Obj.X+flg.Obj.Rect.X;
750 py := flg.Obj.Y+flg.Obj.Rect.Y;
751 pw := flg.Obj.Rect.Width;
752 ph := flg.Obj.Rect.Height;
753 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
754 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
755 if (pdx <> 0) or (pdy <> 0) then
756 begin
757 flg.Obj.X := flg.Obj.X + pdx;
758 flg.Obj.Y := flg.Obj.Y + pdy;
759 flg.NeedSend := true;
760 end;
761 end;
763 // move and push items
764 itm := g_Items_NextAlive(-1);
765 while itm <> nil do
766 begin
767 if itm.Fall then
768 begin
769 itm.getMapBox(px, py, pw, ph);
770 if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
771 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
772 itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
773 end;
774 itm := g_Items_NextAlive(itm.myId);
775 end;
777 // collect monsters
778 monCheckListUsed := 0;
779 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
781 // process collected monsters
782 if (monCheckListUsed > 0) then
783 begin
784 mpfrid := g_Mons_getNewMPlatFrameId();
785 for f := 0 to monCheckListUsed do
786 begin
787 mon := monCheckList[f];
788 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
789 mon.mplatCheckFrameId := mpfrid;
790 mon.getMapBox(px, py, pw, ph);
791 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
792 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
793 begin
794 // set new position
795 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
796 //???FIXME: do we really need to send monsters over the net?
797 // i don't think so, as dead reckoning should take care of 'em
798 // ok, send new monster position only if platform is going to change it's direction
799 if mNeedSend then mon.setDirty();
800 end;
801 // squash monster, if necessary
802 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
803 end;
804 end;
806 // restore panel state
807 mapGrid.proxyEnabled[proxyId] := true;
808 end;
809 end;
811 // move panel
812 FOldX := X;
813 FOldY := Y;
814 X := nx;
815 Y := ny;
816 FOldW := FWidth;
817 FOldH := FHeight;
818 FWidth := nw;
819 FHeight := nh;
820 positionChanged();
822 actMoveTrig := false;
823 actSizeTrig := false;
825 // `mNeedSend` was set above
827 // check "size stop"
828 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
829 begin
830 mSizeSpeed.w := 0;
831 mSizeSpeed.h := 0;
832 actSizeTrig := true;
833 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
834 end;
836 if not conveyor then
837 begin
838 // reverse moving direction, if necessary
839 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
840 begin
841 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
842 actMoveTrig := true;
843 end;
845 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
846 begin
847 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
848 actMoveTrig := true;
849 end;
851 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
852 mOldMovingActive := mMovingActive;
853 end;
855 if not g_Game_IsClient then
856 begin
857 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
858 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
859 end;
861 // some triggers may activate this, don't delay sending
862 //TODO: when triggers will be able to control speed and size, check that here too
863 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
864 mOldMovingActive := mMovingActive;
865 end;
866 end;
869 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
871 function ClampInt(X, A, B: Integer): Integer;
872 begin
873 Result := X;
874 if X < A then Result := A else if X > B then Result := B;
875 end;
877 begin
878 if Enabled and (FCurTexture >= 0) and
879 (FTextureIDs[FCurTexture].Anim) and
880 (FTextureIDs[FCurTexture].AnTex <> nil) and
881 (Width > 0) and (Height > 0) and (FAlpha < 255) then
882 begin
883 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
884 FCurFrameCount := Count;
885 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
886 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
887 end;
888 end;
890 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
891 begin
892 Assert(FCurTexture >= -1, 'FCurTexture < -1');
894 // Íåò òåêñòóð:
895 if Length(FTextureIDs) = 0 then
896 FCurTexture := -1
897 else
898 // Òîëüêî îäíà òåêñòóðà:
899 if Length(FTextureIDs) = 1 then
900 begin
901 if FCurTexture = 0 then
902 FCurTexture := -1
903 else
904 FCurTexture := 0;
905 end
906 else
907 // Áîëüøå îäíîé òåêñòóðû:
908 begin
909 // Ñëåäóþùàÿ:
910 Inc(FCurTexture);
911 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
912 if FCurTexture >= Length(FTextureIDs) then
913 FCurTexture := 0;
914 end;
916 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
917 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
918 begin
919 if (FTextureIDs[FCurTexture].AnTex = nil) then
920 begin
921 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
922 Exit;
923 end;
925 if AnimLoop = 1 then
926 FTextureIDs[FCurTexture].AnTex.Loop := True
927 else
928 if AnimLoop = 2 then
929 FTextureIDs[FCurTexture].AnTex.Loop := False;
931 FTextureIDs[FCurTexture].AnTex.Reset();
932 end;
934 LastAnimLoop := AnimLoop;
935 end;
937 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
938 begin
939 if (ID >= -1) and (ID < Length(FTextureIDs)) then
940 FCurTexture := ID;
942 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
943 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
944 begin
945 if (FTextureIDs[FCurTexture].AnTex = nil) then
946 begin
947 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
948 Exit;
949 end;
951 if AnimLoop = 1 then
952 FTextureIDs[FCurTexture].AnTex.Loop := True
953 else
954 if AnimLoop = 2 then
955 FTextureIDs[FCurTexture].AnTex.Loop := False;
957 FTextureIDs[FCurTexture].AnTex.Reset();
958 end;
960 LastAnimLoop := AnimLoop;
961 end;
963 function TPanel.GetTextureID(): DWORD;
964 var Texture: Integer;
965 begin
966 Result := LongWord(TEXTURE_NONE);
967 if (FCurTexture >= 0) then
968 begin
969 Texture := FTextureIDs[FCurTexture].Texture;
970 case Textures[Texture].TextureName of
971 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
972 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
973 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
974 end
975 end
976 end;
978 function TPanel.GetTextureCount(): Integer;
979 begin
980 Result := Length(FTextureIDs);
981 if Enabled and (FCurTexture >= 0) then
982 if (FTextureIDs[FCurTexture].Anim) and
983 (FTextureIDs[FCurTexture].AnTex <> nil) and
984 (Width > 0) and (Height > 0) and (FAlpha < 255) then
985 Result := Result + 100;
986 end;
988 function TPanel.CanChangeTexture(): Boolean;
989 begin
990 Result := (GetTextureCount() > 1) or hasTexTrigger;
991 end;
993 const
994 PAN_SAVE_VERSION = 1;
996 procedure TPanel.SaveState (st: TStream);
997 var
998 anim: Boolean;
999 begin
1000 if (st = nil) then exit;
1002 // Ñèãíàòóðà ïàíåëè
1003 utils.writeSign(st, 'PANL');
1004 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
1005 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1006 utils.writeBool(st, FEnabled);
1007 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1008 utils.writeInt(st, Byte(FLiftType));
1009 // Íîìåð òåêóùåé òåêñòóðû
1010 utils.writeInt(st, Integer(FCurTexture));
1011 // Êîîðäèíàòû è ðàçìåð
1012 utils.writeInt(st, Integer(FX));
1013 utils.writeInt(st, Integer(FY));
1014 utils.writeInt(st, Word(FWidth));
1015 utils.writeInt(st, Word(FHeight));
1016 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
1017 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
1018 begin
1019 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
1020 anim := true;
1021 end
1022 else
1023 begin
1024 anim := false;
1025 end;
1026 utils.writeBool(st, anim);
1027 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1028 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st, FAlpha, FBlending);
1030 // moving platform state
1031 utils.writeInt(st, Integer(mMovingSpeed.X));
1032 utils.writeInt(st, Integer(mMovingSpeed.Y));
1033 utils.writeInt(st, Integer(mMovingStart.X));
1034 utils.writeInt(st, Integer(mMovingStart.Y));
1035 utils.writeInt(st, Integer(mMovingEnd.X));
1036 utils.writeInt(st, Integer(mMovingEnd.Y));
1038 utils.writeInt(st, Integer(mSizeSpeed.w));
1039 utils.writeInt(st, Integer(mSizeSpeed.h));
1040 utils.writeInt(st, Integer(mSizeEnd.w));
1041 utils.writeInt(st, Integer(mSizeEnd.h));
1043 utils.writeBool(st, mMovingActive);
1044 utils.writeBool(st, mMoveOnce);
1046 utils.writeInt(st, Integer(mEndPosTrig));
1047 utils.writeInt(st, Integer(mEndSizeTrig));
1048 end;
1051 procedure TPanel.LoadState (st: TStream);
1052 begin
1053 if (st = nil) then exit;
1055 // Ñèãíàòóðà ïàíåëè
1056 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1057 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1058 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1059 FEnabled := utils.readBool(st);
1060 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1061 FLiftType := utils.readByte(st);
1062 // Íîìåð òåêóùåé òåêñòóðû
1063 FCurTexture := utils.readLongInt(st);
1064 // Êîîðäèíàòû è ðàçìåð
1065 FX := utils.readLongInt(st);
1066 FY := utils.readLongInt(st);
1067 FOldX := FX;
1068 FOldY := FY;
1069 FWidth := utils.readWord(st);
1070 FHeight := utils.readWord(st);
1071 FOldW := FWidth;
1072 FOldH := FHeight;
1073 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1074 if utils.readBool(st) then
1075 begin
1076 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1077 Assert((FCurTexture >= 0) and
1078 (FTextureIDs[FCurTexture].Anim) and
1079 (FTextureIDs[FCurTexture].AnTex <> nil),
1080 'TPanel.LoadState: No animation object');
1081 FTextureIDs[FCurTexture].AnTex.LoadState(st, FAlpha, FBlending);
1082 end;
1084 // moving platform state
1085 mMovingSpeed.X := utils.readLongInt(st);
1086 mMovingSpeed.Y := utils.readLongInt(st);
1087 mMovingStart.X := utils.readLongInt(st);
1088 mMovingStart.Y := utils.readLongInt(st);
1089 mMovingEnd.X := utils.readLongInt(st);
1090 mMovingEnd.Y := utils.readLongInt(st);
1092 mSizeSpeed.w := utils.readLongInt(st);
1093 mSizeSpeed.h := utils.readLongInt(st);
1094 mSizeEnd.w := utils.readLongInt(st);
1095 mSizeEnd.h := utils.readLongInt(st);
1097 mMovingActive := utils.readBool(st);
1098 mMoveOnce := utils.readBool(st);
1100 mEndPosTrig := utils.readLongInt(st);
1101 mEndSizeTrig := utils.readLongInt(st);
1103 positionChanged();
1104 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1105 end;
1108 end.