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 FWidth, FHeight: Word;
98 FPanelType: Word;
99 FEnabled: Boolean;
100 FDoor: Boolean;
101 FLiftType: Byte;
102 FLastAnimLoop: Byte;
103 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
104 // for now, PLEASE, don't modify 'em, or all hell will break loose
105 arrIdx: Integer; // index in one of internal arrays; sorry
106 tag: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
107 proxyId: Integer; // proxy id in map grid (DO NOT USE!)
108 mapId: AnsiString; // taken directly from map file; dunno why it is here
109 hasTexTrigger: Boolean; // HACK: true when there's a trigger than can change my texture
111 constructor Create(PanelRec: TDynRecord;
112 AddTextures: TAddTextureArray;
113 CurTex: Integer;
114 var Textures: TLevelTextureArray; aguid: Integer);
115 destructor Destroy(); override;
117 procedure Update();
118 procedure SetFrame(Frame: Integer; Count: Byte);
119 procedure NextTexture(AnimLoop: Byte = 0);
120 procedure SetTexture(ID: Integer; AnimLoop: Byte = 0);
121 function GetTextureID(): Cardinal;
122 function GetTextureCount(): Integer;
123 function CanChangeTexture(): Boolean;
125 procedure SaveState (st: TStream);
126 procedure LoadState (st: TStream);
128 procedure positionChanged (); inline;
130 function getIsGBack (): Boolean; inline; // gRenderBackgrounds
131 function getIsGStep (): Boolean; inline; // gSteps
132 function getIsGWall (): Boolean; inline; // gWalls
133 function getIsGAcid1 (): Boolean; inline; // gAcid1
134 function getIsGAcid2 (): Boolean; inline; // gAcid2
135 function getIsGWater (): Boolean; inline; // gWater
136 function getIsGFore (): Boolean; inline; // gRenderForegrounds
137 function getIsGLift (): Boolean; inline; // gLifts
138 function getIsGBlockMon (): Boolean; inline; // gBlockMon
140 // get-and-clear
141 function gncNeedSend (): Boolean; inline;
142 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
144 public
145 property visvalid: Boolean read getvisvalid; // panel is "visvalid" when it's width and height are positive
147 published
148 property guid: Integer read mGUID; // will be assigned in "g_map.pas"
149 property x0: Integer read FX;
150 property y0: Integer read FY;
151 property x1: Integer read getx1; // inclusive!
152 property y1: Integer read gety1; // inclusive!
153 property x: Integer read FX write FX;
154 property y: Integer read FY write FY;
155 property width: Word read FWidth write FWidth;
156 property height: Word read FHeight write FHeight;
157 property panelType: Word read FPanelType write FPanelType;
158 property enabled: Boolean read FEnabled write FEnabled;
159 property door: Boolean read FDoor write FDoor;
160 property liftType: Byte read FLiftType write FLiftType;
161 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
163 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
164 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
165 property movingStartX: Integer read getMovingStartX write setMovingStartX;
166 property movingStartY: Integer read getMovingStartY write setMovingStartY;
167 property movingEndX: Integer read getMovingEndX write setMovingEndX;
168 property movingEndY: Integer read getMovingEndY write setMovingEndY;
169 property movingActive: Boolean read mMovingActive write mMovingActive;
170 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
172 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
173 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
174 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
175 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
177 property isGBack: Boolean read getIsGBack;
178 property isGStep: Boolean read getIsGStep;
179 property isGWall: Boolean read getIsGWall;
180 property isGAcid1: Boolean read getIsGAcid1;
181 property isGAcid2: Boolean read getIsGAcid2;
182 property isGWater: Boolean read getIsGWater;
183 property isGFore: Boolean read getIsGFore;
184 property isGLift: Boolean read getIsGLift;
185 property isGBlockMon: Boolean read getIsGBlockMon;
187 (* private state *)
188 property Alpha: Byte read FAlpha;
189 property Blending: Boolean read FBlending;
190 property TextureIDs: ATextureID read FTextureIDs;
192 public
193 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
194 property movingStart: TDFPoint read mMovingStart write mMovingStart;
195 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
197 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
198 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
200 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
201 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
202 end;
204 TPanelArray = Array of TPanel;
206 const
207 LIFTTYPE_UP = 0;
208 LIFTTYPE_DOWN = 1;
209 LIFTTYPE_LEFT = 2;
210 LIFTTYPE_RIGHT = 3;
212 var
213 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
214 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
217 implementation
219 uses
220 g_basic, g_map, g_game, g_gfx, g_weapons, g_triggers,
221 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
223 const
224 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
226 { T P a n e l : }
228 function FindTextureByName (const name: String): Integer;
229 var i: Integer;
230 begin
231 Result := -1;
232 if Textures <> nil then
233 begin
234 for i := 0 to High(Textures) do
235 begin
236 if Textures[i].TextureName = name then
237 begin
238 Result := i;
239 break;
240 end
241 end
242 end
243 end;
245 constructor TPanel.Create(PanelRec: TDynRecord;
246 AddTextures: TAddTextureArray;
247 CurTex: Integer;
248 var Textures: TLevelTextureArray; aguid: Integer);
249 var
250 i: Integer;
251 tnum: Integer;
252 begin
253 X := PanelRec.X;
254 Y := PanelRec.Y;
255 Width := PanelRec.Width;
256 Height := PanelRec.Height;
257 FAlpha := 0;
258 FBlending := False;
259 FCurFrame := 0;
260 FCurFrameCount := 0;
261 LastAnimLoop := 0;
263 mapId := PanelRec.id;
264 mGUID := aguid;
266 mMovingSpeed := PanelRec.moveSpeed;
267 mMovingStart := PanelRec.moveStart;
268 mMovingEnd := PanelRec.moveEnd;
269 mMovingActive := PanelRec['move_active'].value;
270 mOldMovingActive := mMovingActive;
271 mMoveOnce := PanelRec.moveOnce;
273 mSizeSpeed := PanelRec.sizeSpeed;
274 mSizeEnd := PanelRec.sizeEnd;
276 mEndPosTrig := PanelRec.endPosTrig;
277 mEndSizeTrig := PanelRec.endSizeTrig;
279 mNeedSend := false;
281 // Òèï ïàíåëè:
282 PanelType := PanelRec.PanelType;
283 Enabled := True;
284 Door := False;
285 LiftType := LIFTTYPE_UP;
286 hasTexTrigger := False;
288 case PanelType of
289 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
290 PANEL_CLOSEDOOR: Door := True;
291 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
292 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
293 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
294 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
295 end;
297 // Íåâèäèìàÿ:
298 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
299 begin
300 SetLength(FTextureIDs, 0);
301 FCurTexture := -1;
302 Exit;
303 end;
304 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
305 if ByteBool(PanelType and
306 (PANEL_LIFTUP or
307 PANEL_LIFTDOWN or
308 PANEL_LIFTLEFT or
309 PANEL_LIFTRIGHT or
310 PANEL_BLOCKMON)) then
311 begin
312 SetLength(FTextureIDs, 0);
313 FCurTexture := -1;
314 Exit;
315 end;
317 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
318 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
319 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
320 begin
321 SetLength(FTextureIDs, 1);
322 FTextureIDs[0].Anim := False;
323 case PanelRec.PanelType of
324 PANEL_WATER: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_WATER);
325 PANEL_ACID1: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID1);
326 PANEL_ACID2: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID2);
327 end;
328 FCurTexture := 0;
329 Exit;
330 end;
332 SetLength(FTextureIDs, Length(AddTextures));
334 if CurTex < 0 then
335 FCurTexture := -1
336 else
337 if CurTex >= Length(FTextureIDs) then
338 FCurTexture := Length(FTextureIDs) - 1
339 else
340 FCurTexture := CurTex;
342 for i := 0 to Length(FTextureIDs)-1 do
343 begin
344 FTextureIDs[i].Texture := AddTextures[i].Texture;
345 FTextureIDs[i].Anim := Textures[AddTextures[i].Texture].FramesCount > 0;
346 if FTextureIDs[i].Anim then
347 begin // Àíèìèðîâàííàÿ òåêñòóðà
348 FTextureIDs[i].AnTex := TAnimationState.Create(True, Textures[AddTextures[i].Texture].Speed, Textures[AddTextures[i].Texture].FramesCount);
349 end
350 end;
352 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
353 //if Length(FTextureIDs) > 1 then SaveIt := True;
355 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
356 if (tnum < 0) then tnum := Length(Textures);
358 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
359 if ({PanelRec.TextureNum}tnum > High(Textures)) then
360 begin
361 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
362 FAlpha := 0;
363 FBlending := ByteBool(0);
364 end
365 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
366 begin
367 FAlpha := PanelRec.Alpha;
368 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
369 end;
370 end;
372 destructor TPanel.Destroy();
373 var
374 i: Integer;
375 begin
376 for i := 0 to High(FTextureIDs) do
377 if FTextureIDs[i].Anim then
378 FTextureIDs[i].AnTex.Free();
379 SetLength(FTextureIDs, 0);
381 Inherited;
382 end;
384 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
385 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
386 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
388 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
389 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
390 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
391 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
393 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
394 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
395 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
396 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
398 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
399 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
400 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
401 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
403 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
404 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
405 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
406 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
408 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
409 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
410 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
411 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
413 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
414 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
415 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
416 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
417 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
418 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
419 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
420 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
421 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
423 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
424 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
426 procedure TPanel.positionChanged (); inline;
427 var
428 px, py, pw, ph: Integer;
429 begin
430 if (proxyId >= 0) then
431 begin
432 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
433 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
434 begin
436 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
437 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
439 g_Mark(px, py, pw, ph, MARK_WALL, false);
440 if (Width < 1) or (Height < 1) then
441 begin
442 mapGrid.proxyEnabled[proxyId] := false;
443 end
444 else
445 begin
446 mapGrid.proxyEnabled[proxyId] := Enabled;
447 if (pw <> Width) or (ph <> Height) then
448 begin
449 //writeln('panel resize!');
450 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
451 end
452 else
453 begin
454 mapGrid.moveBody(proxyId, X, Y);
455 end;
456 g_Mark(X, Y, Width, Height, MARK_WALL);
457 end;
458 end;
459 end;
460 end;
463 var
464 monCheckList: array of TMonster = nil;
465 monCheckListUsed: Integer = 0;
467 procedure TPanel.Update();
468 var
469 ox, oy: Integer;
470 nx, ny, nw, nh: Integer;
471 ex, ey, nex, ney: Integer;
472 mpw, mph: Integer;
474 // return `true` if we should move by dx,dy
475 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
476 var
477 u0: Single;
478 tex, tey: Integer;
479 pdx, pdy: Integer;
480 trtag: Integer;
481 szdx, szdy: Integer;
482 begin
483 squash := false;
484 tex := px;
485 tey := py;
486 pdx := mMovingSpeed.X;
487 pdy := mMovingSpeed.Y;
488 // standing on the platform?
489 if (py+ph = oy) then
490 begin
491 if (ontop <> nil) then ontop^ := true;
492 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
493 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
494 end
495 else
496 begin
497 if (ontop <> nil) then ontop^ := false;
498 // not standing on the platform: trace platform to see if it hits the entity
499 // first, process size change (as we cannot sweeptest both move and size change)
500 // but we don't have to check for pushing if the panel is shrinking
501 szdx := nw-mpw;
502 szdy := nh-mph;
503 if (szdx > 0) or (szdy > 0) then
504 begin
505 // ignore shrinking dimension
506 if (szdx < 0) then szdx := 0;
507 if (szdy < 0) then szdy := 0;
508 // move platform by szd* back, and check for szd* movement
509 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
510 begin
511 // yes, platform hits the entity, push the entity in the resizing direction
512 u0 := 1.0-u0; // how much path left?
513 szdx := trunc(szdx*u0);
514 szdy := trunc(szdy*u0);
515 if (szdx <> 0) or (szdy <> 0) then
516 begin
517 // has some path to go, trace the entity
518 trtag := (GridTagWall or GridTagDoor);
519 // if we're moving down, consider steps too
520 if (szdy > 0) then trtag := trtag or GridTagStep;
521 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
522 end;
523 end;
524 end;
525 // second, process platform movement, using te* as entity starting point
526 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
527 begin
528 //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]);
529 // yes, platform hits the entity, push the entity in the direction of the platform
530 u0 := 1.0-u0; // how much path left?
531 pdx := trunc(pdx*u0);
532 pdy := trunc(pdy*u0);
533 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
534 if (pdx <> 0) or (pdy <> 0) then
535 begin
536 // has some path to go, trace the entity
537 trtag := (GridTagWall or GridTagDoor);
538 // if we're moving down, consider steps too
539 if (pdy > 0) then trtag := trtag or GridTagStep;
540 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
541 end;
542 end;
543 end;
544 // done with entity movement, new coords are in te*
545 dx := tex-px;
546 dy := tey-py;
547 result := (dx <> 0) or (dy <> 0);
548 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
549 begin
550 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
551 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
552 end;
553 end;
555 function monCollect (mon: TMonster): Boolean;
556 begin
557 result := false; // don't stop
558 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
559 monCheckList[monCheckListUsed] := mon;
560 Inc(monCheckListUsed);
561 end;
563 var
564 cx0, cy0, cx1, cy1, cw, ch: Integer;
565 f: Integer;
566 px, py, pw, ph, pdx, pdy: Integer;
567 squash: Boolean;
568 plr: TPlayer;
569 gib: PGib;
570 cor: TCorpse;
571 mon: TMonster;
572 mpfrid: LongWord;
573 ontop: Boolean;
574 actMoveTrig: Boolean;
575 actSizeTrig: Boolean;
576 begin
577 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
579 if (FCurTexture >= 0) and
580 (FTextureIDs[FCurTexture].Anim) and
581 (FTextureIDs[FCurTexture].AnTex <> nil) and
582 (FAlpha < 255) then
583 begin
584 FTextureIDs[FCurTexture].AnTex.Update();
585 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
586 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
587 end;
589 if not g_dbgpan_mplat_active then exit;
591 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
592 mOldMovingActive := mMovingActive;
594 if not mMovingActive then exit;
595 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
597 //TODO: write wall size change processing
599 // moving platform?
600 begin
601 (*
602 * collect all monsters and players (aka entities) along the possible platform path
603 * if entity is standing on a platform:
604 * try to move it along the platform path, checking wall collisions
605 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
606 * try to push entity
607 * if we can't push entity all the way, squash it
608 *)
609 ox := X;
610 oy := Y;
611 mpw := Width;
612 mph := Height;
614 nw := mpw+mSizeSpeed.w;
615 nh := mph+mSizeSpeed.h;
616 nx := ox+mMovingSpeed.X;
617 ny := oy+mMovingSpeed.Y;
619 // force network updates only if some sudden change happened
620 // set the flag here, so we can sync affected monsters
621 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
622 begin
623 mNeedSend := true;
624 end
625 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
626 begin
627 mNeedSend := true;
628 end
629 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
630 begin
631 mNeedSend := true;
632 end;
634 // if pannel disappeared, we don't have to do anything
635 if (nw > 0) and (nh > 0) then
636 begin
637 // old rect
638 ex := ox+mpw-1;
639 ey := ox+mph-1;
640 // new rect
641 nex := nx+nw-1;
642 ney := ny+nh-1;
643 // full rect
644 cx0 := nmin(ox, nx);
645 cy0 := nmin(oy, ny);
646 cx1 := nmax(ex, nex);
647 cy1 := nmax(ey, ney);
648 // extrude
649 cx0 -= 1;
650 cy0 -= 1;
651 cx1 += 1;
652 cy1 += 1;
653 cw := cx1-cx0+1;
654 ch := cy1-cy0+1;
656 // process "obstacle" panels
657 if ((tag and GridTagObstacle) <> 0) then
658 begin
659 // temporarily turn off this panel, so it won't interfere with collision checks
660 mapGrid.proxyEnabled[proxyId] := false;
662 // process players
663 for f := 0 to High(gPlayers) do
664 begin
665 plr := gPlayers[f];
666 if (plr = nil) or (not plr.alive) then continue;
667 plr.getMapBox(px, py, pw, ph);
668 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
669 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
670 begin
671 // set new position
672 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
673 end;
674 // squash player, if necessary
675 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
676 end;
678 // process gibs
679 for f := 0 to High(gGibs) do
680 begin
681 gib := @gGibs[f];
682 if not gib.alive then continue;
683 gib.getMapBox(px, py, pw, ph);
684 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
685 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
686 begin
687 // set new position
688 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
689 end;
690 end;
692 // move and push corpses
693 for f := 0 to High(gCorpses) do
694 begin
695 cor := gCorpses[f];
696 if (cor = nil) then continue;
697 cor.getMapBox(px, py, pw, ph);
698 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
699 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
700 begin
701 // set new position
702 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
703 end;
704 end;
706 // collect monsters
707 monCheckListUsed := 0;
708 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
710 // process collected monsters
711 if (monCheckListUsed > 0) then
712 begin
713 mpfrid := g_Mons_getNewMPlatFrameId();
714 for f := 0 to monCheckListUsed do
715 begin
716 mon := monCheckList[f];
717 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
718 mon.mplatCheckFrameId := mpfrid;
719 mon.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) then
722 begin
723 // set new position
724 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
725 //???FIXME: do we really need to send monsters over the net?
726 // i don't think so, as dead reckoning should take care of 'em
727 // ok, send new monster position only if platform is going to change it's direction
728 if mNeedSend then mon.setDirty();
729 end;
730 // squash monster, if necessary
731 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
732 end;
733 end;
735 // restore panel state
736 mapGrid.proxyEnabled[proxyId] := true;
737 end;
738 end;
740 // move panel
741 X := nx;
742 Y := ny;
743 FWidth := nw;
744 FHeight := nh;
745 positionChanged();
747 actMoveTrig := false;
748 actSizeTrig := false;
750 // `mNeedSend` was set above
752 // check "size stop"
753 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
754 begin
755 mSizeSpeed.w := 0;
756 mSizeSpeed.h := 0;
757 actSizeTrig := true;
758 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
759 end;
761 // reverse moving direction, if necessary
762 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
763 begin
764 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
765 actMoveTrig := true;
766 end;
768 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
769 begin
770 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
771 actMoveTrig := true;
772 end;
774 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
775 mOldMovingActive := mMovingActive;
777 if not g_Game_IsClient then
778 begin
779 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
780 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
781 end;
783 // some triggers may activate this, don't delay sending
784 //TODO: when triggers will be able to control speed and size, check that here too
785 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
786 mOldMovingActive := mMovingActive;
787 end;
788 end;
791 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
793 function ClampInt(X, A, B: Integer): Integer;
794 begin
795 Result := X;
796 if X < A then Result := A else if X > B then Result := B;
797 end;
799 begin
800 if Enabled and (FCurTexture >= 0) and
801 (FTextureIDs[FCurTexture].Anim) and
802 (FTextureIDs[FCurTexture].AnTex <> nil) and
803 (Width > 0) and (Height > 0) and (FAlpha < 255) then
804 begin
805 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
806 FCurFrameCount := Count;
807 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
808 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
809 end;
810 end;
812 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
813 begin
814 Assert(FCurTexture >= -1, 'FCurTexture < -1');
816 // Íåò òåêñòóð:
817 if Length(FTextureIDs) = 0 then
818 FCurTexture := -1
819 else
820 // Òîëüêî îäíà òåêñòóðà:
821 if Length(FTextureIDs) = 1 then
822 begin
823 if FCurTexture = 0 then
824 FCurTexture := -1
825 else
826 FCurTexture := 0;
827 end
828 else
829 // Áîëüøå îäíîé òåêñòóðû:
830 begin
831 // Ñëåäóþùàÿ:
832 Inc(FCurTexture);
833 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
834 if FCurTexture >= Length(FTextureIDs) then
835 FCurTexture := 0;
836 end;
838 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
839 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
840 begin
841 if (FTextureIDs[FCurTexture].AnTex = nil) then
842 begin
843 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
844 Exit;
845 end;
847 if AnimLoop = 1 then
848 FTextureIDs[FCurTexture].AnTex.Loop := True
849 else
850 if AnimLoop = 2 then
851 FTextureIDs[FCurTexture].AnTex.Loop := False;
853 FTextureIDs[FCurTexture].AnTex.Reset();
854 end;
856 LastAnimLoop := AnimLoop;
857 end;
859 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
860 begin
861 if (ID >= -1) and (ID < Length(FTextureIDs)) then
862 FCurTexture := ID;
864 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
865 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
866 begin
867 if (FTextureIDs[FCurTexture].AnTex = nil) then
868 begin
869 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
870 Exit;
871 end;
873 if AnimLoop = 1 then
874 FTextureIDs[FCurTexture].AnTex.Loop := True
875 else
876 if AnimLoop = 2 then
877 FTextureIDs[FCurTexture].AnTex.Loop := False;
879 FTextureIDs[FCurTexture].AnTex.Reset();
880 end;
882 LastAnimLoop := AnimLoop;
883 end;
885 function TPanel.GetTextureID(): DWORD;
886 var Texture: Integer;
887 begin
888 Result := LongWord(TEXTURE_NONE);
889 if (FCurTexture >= 0) then
890 begin
891 Texture := FTextureIDs[FCurTexture].Texture;
892 case Textures[Texture].TextureName of
893 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
894 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
895 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
896 end
897 end
898 end;
900 function TPanel.GetTextureCount(): Integer;
901 begin
902 Result := Length(FTextureIDs);
903 if Enabled and (FCurTexture >= 0) then
904 if (FTextureIDs[FCurTexture].Anim) and
905 (FTextureIDs[FCurTexture].AnTex <> nil) and
906 (Width > 0) and (Height > 0) and (FAlpha < 255) then
907 Result := Result + 100;
908 end;
910 function TPanel.CanChangeTexture(): Boolean;
911 begin
912 Result := (GetTextureCount() > 1) or hasTexTrigger;
913 end;
915 const
916 PAN_SAVE_VERSION = 1;
918 procedure TPanel.SaveState (st: TStream);
919 var
920 anim: Boolean;
921 begin
922 if (st = nil) then exit;
924 // Ñèãíàòóðà ïàíåëè
925 utils.writeSign(st, 'PANL');
926 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
927 // Îòêðûòà/çàêðûòà, åñëè äâåðü
928 utils.writeBool(st, FEnabled);
929 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
930 utils.writeInt(st, Byte(FLiftType));
931 // Íîìåð òåêóùåé òåêñòóðû
932 utils.writeInt(st, Integer(FCurTexture));
933 // Êîîðäèíàòû è ðàçìåð
934 utils.writeInt(st, Integer(FX));
935 utils.writeInt(st, Integer(FY));
936 utils.writeInt(st, Word(FWidth));
937 utils.writeInt(st, Word(FHeight));
938 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
939 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
940 begin
941 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
942 anim := true;
943 end
944 else
945 begin
946 anim := false;
947 end;
948 utils.writeBool(st, anim);
949 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
950 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st, FAlpha, FBlending);
952 // moving platform state
953 utils.writeInt(st, Integer(mMovingSpeed.X));
954 utils.writeInt(st, Integer(mMovingSpeed.Y));
955 utils.writeInt(st, Integer(mMovingStart.X));
956 utils.writeInt(st, Integer(mMovingStart.Y));
957 utils.writeInt(st, Integer(mMovingEnd.X));
958 utils.writeInt(st, Integer(mMovingEnd.Y));
960 utils.writeInt(st, Integer(mSizeSpeed.w));
961 utils.writeInt(st, Integer(mSizeSpeed.h));
962 utils.writeInt(st, Integer(mSizeEnd.w));
963 utils.writeInt(st, Integer(mSizeEnd.h));
965 utils.writeBool(st, mMovingActive);
966 utils.writeBool(st, mMoveOnce);
968 utils.writeInt(st, Integer(mEndPosTrig));
969 utils.writeInt(st, Integer(mEndSizeTrig));
970 end;
973 procedure TPanel.LoadState (st: TStream);
974 begin
975 if (st = nil) then exit;
977 // Ñèãíàòóðà ïàíåëè
978 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
979 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
980 // Îòêðûòà/çàêðûòà, åñëè äâåðü
981 FEnabled := utils.readBool(st);
982 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
983 FLiftType := utils.readByte(st);
984 // Íîìåð òåêóùåé òåêñòóðû
985 FCurTexture := utils.readLongInt(st);
986 // Êîîðäèíàòû è ðàçìåð
987 FX := utils.readLongInt(st);
988 FY := utils.readLongInt(st);
989 FWidth := utils.readWord(st);
990 FHeight := utils.readWord(st);
991 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
992 if utils.readBool(st) then
993 begin
994 // Åñëè äà - çàãðóæàåì àíèìàöèþ
995 Assert((FCurTexture >= 0) and
996 (FTextureIDs[FCurTexture].Anim) and
997 (FTextureIDs[FCurTexture].AnTex <> nil),
998 'TPanel.LoadState: No animation object');
999 FTextureIDs[FCurTexture].AnTex.LoadState(st, FAlpha, FBlending);
1000 end;
1002 // moving platform state
1003 mMovingSpeed.X := utils.readLongInt(st);
1004 mMovingSpeed.Y := utils.readLongInt(st);
1005 mMovingStart.X := utils.readLongInt(st);
1006 mMovingStart.Y := utils.readLongInt(st);
1007 mMovingEnd.X := utils.readLongInt(st);
1008 mMovingEnd.Y := utils.readLongInt(st);
1010 mSizeSpeed.w := utils.readLongInt(st);
1011 mSizeSpeed.h := utils.readLongInt(st);
1012 mSizeEnd.w := utils.readLongInt(st);
1013 mSizeEnd.h := utils.readLongInt(st);
1015 mMovingActive := utils.readBool(st);
1016 mMoveOnce := utils.readBool(st);
1018 mEndPosTrig := utils.readLongInt(st);
1019 mEndSizeTrig := utils.readLongInt(st);
1021 positionChanged();
1022 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1023 end;
1026 end.