DEADSOFTWARE

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