DEADSOFTWARE

render: remove graphics data from TAnimationState
[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 g_basic, g_map, g_game, g_gfx, g_weapons, g_triggers, g_items,
227 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
229 const
230 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
232 { T P a n e l : }
234 function FindTextureByName (const name: String): Integer;
235 var i: Integer;
236 begin
237 Result := -1;
238 if Textures <> nil then
239 begin
240 for i := 0 to High(Textures) do
241 begin
242 if Textures[i].TextureName = name then
243 begin
244 Result := i;
245 break;
246 end
247 end
248 end
249 end;
251 constructor TPanel.Create(PanelRec: TDynRecord;
252 AddTextures: TAddTextureArray;
253 CurTex: Integer;
254 var Textures: TLevelTextureArray; aguid: Integer);
255 var
256 i: Integer;
257 tnum: Integer;
258 begin
259 X := PanelRec.X;
260 Y := PanelRec.Y;
261 FOldX := X;
262 FOldY := Y;
263 Width := PanelRec.Width;
264 Height := PanelRec.Height;
265 FOldW := Width;
266 FOldH := Height;
267 FAlpha := 0;
268 FBlending := False;
269 FCurFrame := 0;
270 FCurFrameCount := 0;
271 LastAnimLoop := 0;
273 mapId := PanelRec.id;
274 mGUID := aguid;
276 mMovingSpeed := PanelRec.moveSpeed;
277 mMovingStart := PanelRec.moveStart;
278 mMovingEnd := PanelRec.moveEnd;
279 mMovingActive := PanelRec['move_active'].value;
280 mOldMovingActive := mMovingActive;
281 mMoveOnce := PanelRec.moveOnce;
283 mSizeSpeed := PanelRec.sizeSpeed;
284 mSizeEnd := PanelRec.sizeEnd;
286 mEndPosTrig := PanelRec.endPosTrig;
287 mEndSizeTrig := PanelRec.endSizeTrig;
289 mNeedSend := false;
291 // Òèï ïàíåëè:
292 PanelType := PanelRec.PanelType;
293 Enabled := True;
294 Door := False;
295 LiftType := LIFTTYPE_UP;
296 hasTexTrigger := False;
298 case PanelType of
299 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
300 PANEL_CLOSEDOOR: Door := True;
301 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
302 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
303 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
304 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
305 end;
307 // Íåâèäèìàÿ:
308 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
309 begin
310 SetLength(FTextureIDs, 0);
311 FCurTexture := -1;
312 Exit;
313 end;
314 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
315 if ByteBool(PanelType and
316 (PANEL_LIFTUP or
317 PANEL_LIFTDOWN or
318 PANEL_LIFTLEFT or
319 PANEL_LIFTRIGHT or
320 PANEL_BLOCKMON)) then
321 begin
322 SetLength(FTextureIDs, 0);
323 FCurTexture := -1;
324 Exit;
325 end;
327 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
328 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
329 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
330 begin
331 SetLength(FTextureIDs, 1);
332 FTextureIDs[0].Anim := False;
333 case PanelRec.PanelType of
334 PANEL_WATER: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_WATER);
335 PANEL_ACID1: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID1);
336 PANEL_ACID2: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID2);
337 end;
338 FCurTexture := 0;
339 Exit;
340 end;
342 SetLength(FTextureIDs, Length(AddTextures));
344 if CurTex < 0 then
345 FCurTexture := -1
346 else
347 if CurTex >= Length(FTextureIDs) then
348 FCurTexture := Length(FTextureIDs) - 1
349 else
350 FCurTexture := CurTex;
352 for i := 0 to Length(FTextureIDs)-1 do
353 begin
354 FTextureIDs[i].Texture := AddTextures[i].Texture;
355 FTextureIDs[i].Anim := Textures[AddTextures[i].Texture].FramesCount > 0;
356 if FTextureIDs[i].Anim then
357 begin // Àíèìèðîâàííàÿ òåêñòóðà
358 FTextureIDs[i].AnTex := TAnimationState.Create(True, Textures[AddTextures[i].Texture].Speed, Textures[AddTextures[i].Texture].FramesCount);
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 FAlpha := 0;
373 FBlending := ByteBool(0);
374 end
375 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
376 begin
377 FAlpha := PanelRec.Alpha;
378 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
379 end;
380 end;
382 destructor TPanel.Destroy();
383 var
384 i: Integer;
385 begin
386 for i := 0 to High(FTextureIDs) do
387 if FTextureIDs[i].Anim then
388 FTextureIDs[i].AnTex.Free();
389 SetLength(FTextureIDs, 0);
391 Inherited;
392 end;
394 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
395 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
396 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
398 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
399 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
400 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
401 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
403 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
404 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
405 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
406 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
408 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
409 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
410 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
411 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
413 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
414 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
415 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
416 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
418 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
419 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
420 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
421 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
423 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
424 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
425 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
426 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
427 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
428 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
429 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
430 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
431 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
433 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
434 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
436 procedure TPanel.positionChanged (); inline;
437 var
438 px, py, pw, ph: Integer;
439 begin
440 if (proxyId >= 0) then
441 begin
442 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
443 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
444 begin
446 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
447 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
449 g_Mark(px, py, pw, ph, MARK_WALL, false);
450 if (Width < 1) or (Height < 1) then
451 begin
452 mapGrid.proxyEnabled[proxyId] := false;
453 end
454 else
455 begin
456 mapGrid.proxyEnabled[proxyId] := Enabled;
457 if (pw <> Width) or (ph <> Height) then
458 begin
459 //writeln('panel resize!');
460 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
461 end
462 else
463 begin
464 mapGrid.moveBody(proxyId, X, Y);
465 end;
466 g_Mark(X, Y, Width, Height, MARK_WALL);
467 end;
468 end;
469 end;
470 end;
473 var
474 monCheckList: array of TMonster = nil;
475 monCheckListUsed: Integer = 0;
477 procedure TPanel.Update();
478 var
479 ox, oy: Integer;
480 nx, ny, nw, nh: Integer;
481 ex, ey, nex, ney: Integer;
482 mpw, mph: Integer;
483 conveyor: Boolean;
485 // return `true` if we should move by dx,dy
486 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
487 var
488 u0: Single;
489 tex, tey: Integer;
490 pdx, pdy: Integer;
491 trtag: Integer;
492 szdx, szdy: Integer;
493 begin
494 squash := false;
495 tex := px;
496 tey := py;
497 pdx := mMovingSpeed.X;
498 pdy := mMovingSpeed.Y;
499 // standing on the platform?
500 if (py+ph = oy) then
501 begin
502 if (ontop <> nil) then ontop^ := true;
503 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
504 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
505 end
506 else
507 begin
508 if (ontop <> nil) then ontop^ := false;
509 // not standing on the platform: trace platform to see if it hits the entity
510 // first, process size change (as we cannot sweeptest both move and size change)
511 // but we don't have to check for pushing if the panel is shrinking
512 szdx := nw-mpw;
513 szdy := nh-mph;
514 if (szdx > 0) or (szdy > 0) then
515 begin
516 // ignore shrinking dimension
517 if (szdx < 0) then szdx := 0;
518 if (szdy < 0) then szdy := 0;
519 // move platform by szd* back, and check for szd* movement
520 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
521 begin
522 // yes, platform hits the entity, push the entity in the resizing direction
523 u0 := 1.0-u0; // how much path left?
524 szdx := trunc(szdx*u0);
525 szdy := trunc(szdy*u0);
526 if (szdx <> 0) or (szdy <> 0) then
527 begin
528 // has some path to go, trace the entity
529 trtag := (GridTagWall or GridTagDoor);
530 // if we're moving down, consider steps too
531 if (szdy > 0) then trtag := trtag or GridTagStep;
532 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
533 end;
534 end;
535 end;
536 // second, process platform movement, using te* as entity starting point
537 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
538 begin
539 //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]);
540 // yes, platform hits the entity, push the entity in the direction of the platform
541 u0 := 1.0-u0; // how much path left?
542 pdx := trunc(pdx*u0);
543 pdy := trunc(pdy*u0);
544 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
545 if (pdx <> 0) or (pdy <> 0) then
546 begin
547 // has some path to go, trace the entity
548 trtag := (GridTagWall or GridTagDoor);
549 // if we're moving down, consider steps too
550 if (pdy > 0) then trtag := trtag or GridTagStep;
551 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
552 end;
553 end;
554 end;
555 // done with entity movement, new coords are in te*
556 dx := tex-px;
557 dy := tey-py;
558 result := (dx <> 0) or (dy <> 0);
559 if not conveyor and ((tag and (GridTagWall or GridTagDoor)) <> 0) then
560 begin
561 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
562 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
563 end;
564 end;
566 function monCollect (mon: TMonster): Boolean;
567 begin
568 result := false; // don't stop
569 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
570 monCheckList[monCheckListUsed] := mon;
571 Inc(monCheckListUsed);
572 end;
574 var
575 cx0, cy0, cx1, cy1, cw, ch: Integer;
576 f: Integer;
577 px, py, pw, ph, pdx, pdy: Integer;
578 squash: Boolean;
579 plr: TPlayer;
580 gib: PGib;
581 cor: TCorpse;
582 mon: TMonster;
583 flg: PFlag;
584 itm: PItem;
585 mpfrid: LongWord;
586 ontop: Boolean;
587 actMoveTrig: Boolean;
588 actSizeTrig: Boolean;
589 begin
590 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
592 if (FCurTexture >= 0) and
593 (FTextureIDs[FCurTexture].Anim) and
594 (FTextureIDs[FCurTexture].AnTex <> nil) and
595 (FAlpha < 255) then
596 begin
597 FTextureIDs[FCurTexture].AnTex.Update();
598 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
599 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
600 end;
602 if not g_dbgpan_mplat_active then exit;
604 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
605 mOldMovingActive := mMovingActive;
607 if not mMovingActive then exit;
608 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
610 //TODO: write wall size change processing
612 // moving platform?
613 begin
614 (*
615 * collect all monsters and players (aka entities) along the possible platform path
616 * if entity is standing on a platform:
617 * try to move it along the platform path, checking wall collisions
618 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
619 * try to push entity
620 * if we can't push entity all the way, squash it
621 *)
622 ox := X;
623 oy := Y;
624 mpw := Width;
625 mph := Height;
627 // the mplat acts as a stationary conveyor belt when it's locked within a movement rect of zero area
628 conveyor := (mMovingEnd.X = mMovingStart.X) and (mMovingEnd.Y = mMovingStart.Y)
629 and (mMovingEnd.X = X) and (mMovingEnd.Y = Y);
631 nw := mpw+mSizeSpeed.w;
632 nh := mph+mSizeSpeed.h;
633 nx := ox;
634 ny := oy;
635 if not conveyor then
636 begin
637 nx += mMovingSpeed.X;
638 ny += mMovingSpeed.Y;
639 end;
641 // force network updates only if some sudden change happened
642 // set the flag here, so we can sync affected monsters
643 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
644 begin
645 mNeedSend := true;
646 end
647 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
648 begin
649 mNeedSend := true;
650 end
651 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
652 begin
653 mNeedSend := true;
654 end;
656 // if pannel disappeared, we don't have to do anything
657 if (nw > 0) and (nh > 0) then
658 begin
659 // old rect
660 ex := ox+mpw-1;
661 ey := ox+mph-1;
662 // new rect
663 nex := nx+nw-1;
664 ney := ny+nh-1;
665 // full rect
666 cx0 := nmin(ox, nx);
667 cy0 := nmin(oy, ny);
668 cx1 := nmax(ex, nex);
669 cy1 := nmax(ey, ney);
670 // extrude
671 cx0 -= 1;
672 cy0 -= 1;
673 cx1 += 1;
674 cy1 += 1;
675 cw := cx1-cx0+1;
676 ch := cy1-cy0+1;
678 // process "obstacle" panels
679 if ((tag and GridTagObstacle) <> 0) then
680 begin
681 // temporarily turn off this panel, so it won't interfere with collision checks
682 mapGrid.proxyEnabled[proxyId] := false;
684 // process players
685 for f := 0 to High(gPlayers) do
686 begin
687 plr := gPlayers[f];
688 if (plr = nil) or (not plr.alive) then continue;
689 plr.getMapBox(px, py, pw, ph);
690 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
691 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
692 begin
693 // set new position
694 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
695 end;
696 // squash player, if necessary
697 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
698 end;
700 // process gibs
701 for f := 0 to High(gGibs) do
702 begin
703 gib := @gGibs[f];
704 if not gib.alive then continue;
705 gib.getMapBox(px, py, pw, ph);
706 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
707 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
708 begin
709 // set new position
710 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
711 end;
712 end;
714 // move and push corpses
715 for f := 0 to High(gCorpses) do
716 begin
717 cor := gCorpses[f];
718 if (cor = nil) then continue;
719 cor.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 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
725 end;
726 end;
728 // move and push flags
729 if gGameSettings.GameMode = GM_CTF then
730 for f := FLAG_RED to FLAG_BLUE do
731 begin
732 flg := @gFlags[f];
733 if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
734 px := flg.Obj.X+flg.Obj.Rect.X;
735 py := flg.Obj.Y+flg.Obj.Rect.Y;
736 pw := flg.Obj.Rect.Width;
737 ph := flg.Obj.Rect.Height;
738 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
739 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
740 if (pdx <> 0) or (pdy <> 0) then
741 begin
742 flg.Obj.X := flg.Obj.X + pdx;
743 flg.Obj.Y := flg.Obj.Y + pdy;
744 flg.NeedSend := true;
745 end;
746 end;
748 // move and push items
749 itm := g_Items_NextAlive(-1);
750 while itm <> nil do
751 begin
752 if itm.Fall then
753 begin
754 itm.getMapBox(px, py, pw, ph);
755 if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
756 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
757 itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
758 end;
759 itm := g_Items_NextAlive(itm.myId);
760 end;
762 // collect monsters
763 monCheckListUsed := 0;
764 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
766 // process collected monsters
767 if (monCheckListUsed > 0) then
768 begin
769 mpfrid := g_Mons_getNewMPlatFrameId();
770 for f := 0 to monCheckListUsed do
771 begin
772 mon := monCheckList[f];
773 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
774 mon.mplatCheckFrameId := mpfrid;
775 mon.getMapBox(px, py, pw, ph);
776 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
777 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
778 begin
779 // set new position
780 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
781 //???FIXME: do we really need to send monsters over the net?
782 // i don't think so, as dead reckoning should take care of 'em
783 // ok, send new monster position only if platform is going to change it's direction
784 if mNeedSend then mon.setDirty();
785 end;
786 // squash monster, if necessary
787 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
788 end;
789 end;
791 // restore panel state
792 mapGrid.proxyEnabled[proxyId] := true;
793 end;
794 end;
796 // move panel
797 FOldX := X;
798 FOldY := Y;
799 X := nx;
800 Y := ny;
801 FOldW := FWidth;
802 FOldH := FHeight;
803 FWidth := nw;
804 FHeight := nh;
805 positionChanged();
807 actMoveTrig := false;
808 actSizeTrig := false;
810 // `mNeedSend` was set above
812 // check "size stop"
813 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
814 begin
815 mSizeSpeed.w := 0;
816 mSizeSpeed.h := 0;
817 actSizeTrig := true;
818 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
819 end;
821 if not conveyor then
822 begin
823 // reverse moving direction, if necessary
824 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
825 begin
826 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
827 actMoveTrig := true;
828 end;
830 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
831 begin
832 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
833 actMoveTrig := true;
834 end;
836 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
837 mOldMovingActive := mMovingActive;
838 end;
840 if not g_Game_IsClient then
841 begin
842 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
843 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
844 end;
846 // some triggers may activate this, don't delay sending
847 //TODO: when triggers will be able to control speed and size, check that here too
848 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
849 mOldMovingActive := mMovingActive;
850 end;
851 end;
854 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
856 function ClampInt(X, A, B: Integer): Integer;
857 begin
858 Result := X;
859 if X < A then Result := A else if X > B then Result := B;
860 end;
862 begin
863 if Enabled and (FCurTexture >= 0) and
864 (FTextureIDs[FCurTexture].Anim) and
865 (FTextureIDs[FCurTexture].AnTex <> nil) and
866 (Width > 0) and (Height > 0) and (FAlpha < 255) then
867 begin
868 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
869 FCurFrameCount := Count;
870 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
871 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
872 end;
873 end;
875 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
876 begin
877 Assert(FCurTexture >= -1, 'FCurTexture < -1');
879 // Íåò òåêñòóð:
880 if Length(FTextureIDs) = 0 then
881 FCurTexture := -1
882 else
883 // Òîëüêî îäíà òåêñòóðà:
884 if Length(FTextureIDs) = 1 then
885 begin
886 if FCurTexture = 0 then
887 FCurTexture := -1
888 else
889 FCurTexture := 0;
890 end
891 else
892 // Áîëüøå îäíîé òåêñòóðû:
893 begin
894 // Ñëåäóþùàÿ:
895 Inc(FCurTexture);
896 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
897 if FCurTexture >= Length(FTextureIDs) then
898 FCurTexture := 0;
899 end;
901 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
902 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
903 begin
904 if (FTextureIDs[FCurTexture].AnTex = nil) then
905 begin
906 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
907 Exit;
908 end;
910 if AnimLoop = 1 then
911 FTextureIDs[FCurTexture].AnTex.Loop := True
912 else
913 if AnimLoop = 2 then
914 FTextureIDs[FCurTexture].AnTex.Loop := False;
916 FTextureIDs[FCurTexture].AnTex.Reset();
917 end;
919 LastAnimLoop := AnimLoop;
920 end;
922 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
923 begin
924 if (ID >= -1) and (ID < Length(FTextureIDs)) then
925 FCurTexture := ID;
927 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
928 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
929 begin
930 if (FTextureIDs[FCurTexture].AnTex = nil) then
931 begin
932 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
933 Exit;
934 end;
936 if AnimLoop = 1 then
937 FTextureIDs[FCurTexture].AnTex.Loop := True
938 else
939 if AnimLoop = 2 then
940 FTextureIDs[FCurTexture].AnTex.Loop := False;
942 FTextureIDs[FCurTexture].AnTex.Reset();
943 end;
945 LastAnimLoop := AnimLoop;
946 end;
948 function TPanel.GetTextureID(): DWORD;
949 var Texture: Integer;
950 begin
951 Result := LongWord(TEXTURE_NONE);
952 if (FCurTexture >= 0) then
953 begin
954 Texture := FTextureIDs[FCurTexture].Texture;
955 case Textures[Texture].TextureName of
956 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
957 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
958 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
959 end
960 end
961 end;
963 function TPanel.GetTextureCount(): Integer;
964 begin
965 Result := Length(FTextureIDs);
966 if Enabled and (FCurTexture >= 0) then
967 if (FTextureIDs[FCurTexture].Anim) and
968 (FTextureIDs[FCurTexture].AnTex <> nil) and
969 (Width > 0) and (Height > 0) and (FAlpha < 255) then
970 Result := Result + 100;
971 end;
973 function TPanel.CanChangeTexture(): Boolean;
974 begin
975 Result := (GetTextureCount() > 1) or hasTexTrigger;
976 end;
978 const
979 PAN_SAVE_VERSION = 1;
981 procedure TPanel.SaveState (st: TStream);
982 var
983 anim: Boolean;
984 begin
985 if (st = nil) then exit;
987 // Ñèãíàòóðà ïàíåëè
988 utils.writeSign(st, 'PANL');
989 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
990 // Îòêðûòà/çàêðûòà, åñëè äâåðü
991 utils.writeBool(st, FEnabled);
992 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
993 utils.writeInt(st, Byte(FLiftType));
994 // Íîìåð òåêóùåé òåêñòóðû
995 utils.writeInt(st, Integer(FCurTexture));
996 // Êîîðäèíàòû è ðàçìåð
997 utils.writeInt(st, Integer(FX));
998 utils.writeInt(st, Integer(FY));
999 utils.writeInt(st, Word(FWidth));
1000 utils.writeInt(st, Word(FHeight));
1001 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
1002 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
1003 begin
1004 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
1005 anim := true;
1006 end
1007 else
1008 begin
1009 anim := false;
1010 end;
1011 utils.writeBool(st, anim);
1012 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1013 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st, FAlpha, FBlending);
1015 // moving platform state
1016 utils.writeInt(st, Integer(mMovingSpeed.X));
1017 utils.writeInt(st, Integer(mMovingSpeed.Y));
1018 utils.writeInt(st, Integer(mMovingStart.X));
1019 utils.writeInt(st, Integer(mMovingStart.Y));
1020 utils.writeInt(st, Integer(mMovingEnd.X));
1021 utils.writeInt(st, Integer(mMovingEnd.Y));
1023 utils.writeInt(st, Integer(mSizeSpeed.w));
1024 utils.writeInt(st, Integer(mSizeSpeed.h));
1025 utils.writeInt(st, Integer(mSizeEnd.w));
1026 utils.writeInt(st, Integer(mSizeEnd.h));
1028 utils.writeBool(st, mMovingActive);
1029 utils.writeBool(st, mMoveOnce);
1031 utils.writeInt(st, Integer(mEndPosTrig));
1032 utils.writeInt(st, Integer(mEndSizeTrig));
1033 end;
1036 procedure TPanel.LoadState (st: TStream);
1037 begin
1038 if (st = nil) then exit;
1040 // Ñèãíàòóðà ïàíåëè
1041 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1042 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1043 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1044 FEnabled := utils.readBool(st);
1045 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1046 FLiftType := utils.readByte(st);
1047 // Íîìåð òåêóùåé òåêñòóðû
1048 FCurTexture := utils.readLongInt(st);
1049 // Êîîðäèíàòû è ðàçìåð
1050 FX := utils.readLongInt(st);
1051 FY := utils.readLongInt(st);
1052 FOldX := FX;
1053 FOldY := FY;
1054 FWidth := utils.readWord(st);
1055 FHeight := utils.readWord(st);
1056 FOldW := FWidth;
1057 FOldH := FHeight;
1058 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1059 if utils.readBool(st) then
1060 begin
1061 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1062 Assert((FCurTexture >= 0) and
1063 (FTextureIDs[FCurTexture].Anim) and
1064 (FTextureIDs[FCurTexture].AnTex <> nil),
1065 'TPanel.LoadState: No animation object');
1066 FTextureIDs[FCurTexture].AnTex.LoadState(st, FAlpha, FBlending);
1067 end;
1069 // moving platform state
1070 mMovingSpeed.X := utils.readLongInt(st);
1071 mMovingSpeed.Y := utils.readLongInt(st);
1072 mMovingStart.X := utils.readLongInt(st);
1073 mMovingStart.Y := utils.readLongInt(st);
1074 mMovingEnd.X := utils.readLongInt(st);
1075 mMovingEnd.Y := utils.readLongInt(st);
1077 mSizeSpeed.w := utils.readLongInt(st);
1078 mSizeSpeed.h := utils.readLongInt(st);
1079 mSizeEnd.w := utils.readLongInt(st);
1080 mSizeEnd.h := utils.readLongInt(st);
1082 mMovingActive := utils.readBool(st);
1083 mMoveOnce := utils.readBool(st);
1085 mEndPosTrig := utils.readLongInt(st);
1086 mEndSizeTrig := utils.readLongInt(st);
1088 positionChanged();
1089 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1090 end;
1093 end.