DEADSOFTWARE

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