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