DEADSOFTWARE

aa68298361c09555e490a5b73c0beb3fcc7a8d0d
[d2df-sdl.git] / src / game / g_panel.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$M+}
17 unit g_panel;
19 interface
21 uses
22 SysUtils, Classes,
23 MAPDEF, g_textures, xdynrec;
25 type
26 TAddTextureArray = Array of
27 record
28 Texture: Cardinal;
29 Anim: Boolean;
30 end;
32 ATextureID = array of record
33 case Anim: Boolean of
34 False: (Tex: Cardinal);
35 True: (AnTex: TAnimation);
36 end;
38 PPanel = ^TPanel;
39 TPanel = Class (TObject)
40 private
41 const
42 private
43 mGUID: Integer; // will be assigned in "g_map.pas"
44 FTextureWidth: Word;
45 FTextureHeight: Word;
46 FAlpha: Byte;
47 FBlending: Boolean;
48 FTextureIDs: ATextureID;
49 mMovingSpeed: TDFPoint;
50 mMovingStart: TDFPoint;
51 mMovingEnd: TDFPoint;
52 mMovingActive: Boolean;
53 mMoveOnce: Boolean;
55 mOldMovingActive: Boolean;
57 mSizeSpeed: TDFSize;
58 mSizeEnd: TDFSize;
60 mEndPosTrig: Integer;
61 mEndSizeTrig: Integer;
63 mNeedSend: Boolean; // for network
65 private
66 function getx1 (): Integer; inline;
67 function gety1 (): Integer; inline;
68 function getvisvalid (): Boolean; inline;
70 function getMovingSpeedX (): Integer; inline;
71 procedure setMovingSpeedX (v: Integer); inline;
72 function getMovingSpeedY (): Integer; inline;
73 procedure setMovingSpeedY (v: Integer); inline;
75 function getMovingStartX (): Integer; inline;
76 procedure setMovingStartX (v: Integer); inline;
77 function getMovingStartY (): Integer; inline;
78 procedure setMovingStartY (v: Integer); inline;
80 function getMovingEndX (): Integer; inline;
81 procedure setMovingEndX (v: Integer); inline;
82 function getMovingEndY (): Integer; inline;
83 procedure setMovingEndY (v: Integer); inline;
85 function getSizeSpeedX (): Integer; inline;
86 procedure setSizeSpeedX (v: Integer); inline;
87 function getSizeSpeedY (): Integer; inline;
88 procedure setSizeSpeedY (v: Integer); inline;
90 function getSizeEndX (): Integer; inline;
91 procedure setSizeEndX (v: Integer); inline;
92 function getSizeEndY (): Integer; inline;
93 procedure setSizeEndY (v: Integer); inline;
95 public
96 FCurTexture: Integer; // Íîìåð òåêóùåé òåêñòóðû
97 FCurFrame: Integer;
98 FCurFrameCount: Byte;
99 FX, FY: Integer;
100 FWidth, FHeight: 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 panelType: Word read FPanelType write FPanelType;
161 property enabled: Boolean read FEnabled write FEnabled;
162 property door: Boolean read FDoor write FDoor;
163 property liftType: Byte read FLiftType write FLiftType;
164 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
166 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
167 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
168 property movingStartX: Integer read getMovingStartX write setMovingStartX;
169 property movingStartY: Integer read getMovingStartY write setMovingStartY;
170 property movingEndX: Integer read getMovingEndX write setMovingEndX;
171 property movingEndY: Integer read getMovingEndY write setMovingEndY;
172 property movingActive: Boolean read mMovingActive write mMovingActive;
173 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
175 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
176 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
177 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
178 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
180 property isGBack: Boolean read getIsGBack;
181 property isGStep: Boolean read getIsGStep;
182 property isGWall: Boolean read getIsGWall;
183 property isGAcid1: Boolean read getIsGAcid1;
184 property isGAcid2: Boolean read getIsGAcid2;
185 property isGWater: Boolean read getIsGWater;
186 property isGFore: Boolean read getIsGFore;
187 property isGLift: Boolean read getIsGLift;
188 property isGBlockMon: Boolean read getIsGBlockMon;
190 (* private state *)
191 property Alpha: Byte read FAlpha;
192 property TextureWidth: Word read FTextureWidth;
193 property TextureHeight: Word read FTextureHeight;
194 property Blending: Boolean read FBlending;
195 property TextureIDs: ATextureID read FTextureIDs;
197 public
198 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
199 property movingStart: TDFPoint read mMovingStart write mMovingStart;
200 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
202 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
203 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
205 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
206 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
207 end;
209 TPanelArray = Array of TPanel;
211 const
212 LIFTTYPE_UP = 0;
213 LIFTTYPE_DOWN = 1;
214 LIFTTYPE_LEFT = 2;
215 LIFTTYPE_RIGHT = 3;
217 var
218 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
219 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
222 implementation
224 uses
225 g_basic, g_map, g_game, g_gfx, g_weapons, g_triggers,
226 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
228 const
229 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
231 { T P a n e l : }
233 constructor TPanel.Create(PanelRec: TDynRecord;
234 AddTextures: TAddTextureArray;
235 CurTex: Integer;
236 var Textures: TLevelTextureArray; aguid: Integer);
237 var
238 i: Integer;
239 tnum: Integer;
240 begin
241 X := PanelRec.X;
242 Y := PanelRec.Y;
243 Width := PanelRec.Width;
244 Height := PanelRec.Height;
245 FAlpha := 0;
246 FBlending := False;
247 FCurFrame := 0;
248 FCurFrameCount := 0;
249 LastAnimLoop := 0;
251 mapId := PanelRec.id;
252 mGUID := aguid;
254 mMovingSpeed := PanelRec.moveSpeed;
255 mMovingStart := PanelRec.moveStart;
256 mMovingEnd := PanelRec.moveEnd;
257 mMovingActive := PanelRec['move_active'].value;
258 mOldMovingActive := mMovingActive;
259 mMoveOnce := PanelRec.moveOnce;
261 mSizeSpeed := PanelRec.sizeSpeed;
262 mSizeEnd := PanelRec.sizeEnd;
264 mEndPosTrig := PanelRec.endPosTrig;
265 mEndSizeTrig := PanelRec.endSizeTrig;
267 mNeedSend := false;
269 // Òèï ïàíåëè:
270 PanelType := PanelRec.PanelType;
271 Enabled := True;
272 Door := False;
273 LiftType := LIFTTYPE_UP;
274 hasTexTrigger := False;
276 case PanelType of
277 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
278 PANEL_CLOSEDOOR: Door := True;
279 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
280 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
281 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
282 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
283 end;
285 // Íåâèäèìàÿ:
286 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
287 begin
288 SetLength(FTextureIDs, 0);
289 FCurTexture := -1;
290 Exit;
291 end;
292 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
293 if ByteBool(PanelType and
294 (PANEL_LIFTUP or
295 PANEL_LIFTDOWN or
296 PANEL_LIFTLEFT or
297 PANEL_LIFTRIGHT or
298 PANEL_BLOCKMON)) then
299 begin
300 SetLength(FTextureIDs, 0);
301 FCurTexture := -1;
302 Exit;
303 end;
305 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
306 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
307 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
308 begin
309 SetLength(FTextureIDs, 1);
310 FTextureIDs[0].Anim := False;
312 case PanelRec.PanelType of
313 PANEL_WATER:
314 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_WATER);
315 PANEL_ACID1:
316 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID1);
317 PANEL_ACID2:
318 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID2);
319 end;
321 FCurTexture := 0;
322 Exit;
323 end;
325 SetLength(FTextureIDs, Length(AddTextures));
327 if CurTex < 0 then
328 FCurTexture := -1
329 else
330 if CurTex >= Length(FTextureIDs) then
331 FCurTexture := Length(FTextureIDs) - 1
332 else
333 FCurTexture := CurTex;
335 for i := 0 to Length(FTextureIDs)-1 do
336 begin
337 FTextureIDs[i].Anim := AddTextures[i].Anim;
338 if FTextureIDs[i].Anim then
339 begin // Àíèìèðîâàííàÿ òåêñòóðà
340 FTextureIDs[i].AnTex :=
341 TAnimation.Create(Textures[AddTextures[i].Texture].FramesID,
342 True, Textures[AddTextures[i].Texture].Speed);
343 FTextureIDs[i].AnTex.Blending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
344 FTextureIDs[i].AnTex.Alpha := PanelRec.Alpha;
345 end
346 else
347 begin // Îáû÷íàÿ òåêñòóðà
348 FTextureIDs[i].Tex := Textures[AddTextures[i].Texture].TextureID;
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 FTextureWidth := 2;
363 FTextureHeight := 2;
364 FAlpha := 0;
365 FBlending := ByteBool(0);
366 end
367 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
368 begin
369 FTextureWidth := Textures[{PanelRec.TextureNum}tnum].Width;
370 FTextureHeight := Textures[{PanelRec.TextureNum}tnum].Height;
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;
478 // return `true` if we should move by dx,dy
479 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
480 var
481 u0: Single;
482 tex, tey: Integer;
483 pdx, pdy: Integer;
484 trtag: Integer;
485 szdx, szdy: Integer;
486 begin
487 squash := false;
488 tex := px;
489 tey := py;
490 pdx := mMovingSpeed.X;
491 pdy := mMovingSpeed.Y;
492 // standing on the platform?
493 if (py+ph = oy) then
494 begin
495 if (ontop <> nil) then ontop^ := true;
496 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
497 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
498 end
499 else
500 begin
501 if (ontop <> nil) then ontop^ := false;
502 // not standing on the platform: trace platform to see if it hits the entity
503 // first, process size change (as we cannot sweeptest both move and size change)
504 // but we don't have to check for pushing if the panel is shrinking
505 szdx := nw-mpw;
506 szdy := nh-mph;
507 if (szdx > 0) or (szdy > 0) then
508 begin
509 // ignore shrinking dimension
510 if (szdx < 0) then szdx := 0;
511 if (szdy < 0) then szdy := 0;
512 // move platform by szd* back, and check for szd* movement
513 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
514 begin
515 // yes, platform hits the entity, push the entity in the resizing direction
516 u0 := 1.0-u0; // how much path left?
517 szdx := trunc(szdx*u0);
518 szdy := trunc(szdy*u0);
519 if (szdx <> 0) or (szdy <> 0) then
520 begin
521 // has some path to go, trace the entity
522 trtag := (GridTagWall or GridTagDoor);
523 // if we're moving down, consider steps too
524 if (szdy > 0) then trtag := trtag or GridTagStep;
525 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
526 end;
527 end;
528 end;
529 // second, process platform movement, using te* as entity starting point
530 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
531 begin
532 //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]);
533 // yes, platform hits the entity, push the entity in the direction of the platform
534 u0 := 1.0-u0; // how much path left?
535 pdx := trunc(pdx*u0);
536 pdy := trunc(pdy*u0);
537 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
538 if (pdx <> 0) or (pdy <> 0) then
539 begin
540 // has some path to go, trace the entity
541 trtag := (GridTagWall or GridTagDoor);
542 // if we're moving down, consider steps too
543 if (pdy > 0) then trtag := trtag or GridTagStep;
544 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
545 end;
546 end;
547 end;
548 // done with entity movement, new coords are in te*
549 dx := tex-px;
550 dy := tey-py;
551 result := (dx <> 0) or (dy <> 0);
552 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
553 begin
554 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
555 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
556 end;
557 end;
559 function monCollect (mon: TMonster): Boolean;
560 begin
561 result := false; // don't stop
562 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
563 monCheckList[monCheckListUsed] := mon;
564 Inc(monCheckListUsed);
565 end;
567 var
568 cx0, cy0, cx1, cy1, cw, ch: Integer;
569 f: Integer;
570 px, py, pw, ph, pdx, pdy: Integer;
571 squash: Boolean;
572 plr: TPlayer;
573 gib: PGib;
574 cor: TCorpse;
575 mon: TMonster;
576 mpfrid: LongWord;
577 ontop: Boolean;
578 actMoveTrig: Boolean;
579 actSizeTrig: Boolean;
580 begin
581 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
583 if (FCurTexture >= 0) and
584 (FTextureIDs[FCurTexture].Anim) and
585 (FTextureIDs[FCurTexture].AnTex <> nil) and
586 (FAlpha < 255) then
587 begin
588 FTextureIDs[FCurTexture].AnTex.Update();
589 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
590 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
591 end;
593 if not g_dbgpan_mplat_active then exit;
595 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
596 mOldMovingActive := mMovingActive;
598 if not mMovingActive then exit;
599 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
601 //TODO: write wall size change processing
603 // moving platform?
604 begin
605 (*
606 * collect all monsters and players (aka entities) along the possible platform path
607 * if entity is standing on a platform:
608 * try to move it along the platform path, checking wall collisions
609 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
610 * try to push entity
611 * if we can't push entity all the way, squash it
612 *)
613 ox := X;
614 oy := Y;
615 mpw := Width;
616 mph := Height;
618 nw := mpw+mSizeSpeed.w;
619 nh := mph+mSizeSpeed.h;
620 nx := ox+mMovingSpeed.X;
621 ny := oy+mMovingSpeed.Y;
623 // force network updates only if some sudden change happened
624 // set the flag here, so we can sync affected monsters
625 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
626 begin
627 mNeedSend := true;
628 end
629 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
630 begin
631 mNeedSend := true;
632 end
633 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
634 begin
635 mNeedSend := true;
636 end;
638 // if pannel disappeared, we don't have to do anything
639 if (nw > 0) and (nh > 0) then
640 begin
641 // old rect
642 ex := ox+mpw-1;
643 ey := ox+mph-1;
644 // new rect
645 nex := nx+nw-1;
646 ney := ny+nh-1;
647 // full rect
648 cx0 := nmin(ox, nx);
649 cy0 := nmin(oy, ny);
650 cx1 := nmax(ex, nex);
651 cy1 := nmax(ey, ney);
652 // extrude
653 cx0 -= 1;
654 cy0 -= 1;
655 cx1 += 1;
656 cy1 += 1;
657 cw := cx1-cx0+1;
658 ch := cy1-cy0+1;
660 // process "obstacle" panels
661 if ((tag and GridTagObstacle) <> 0) then
662 begin
663 // temporarily turn off this panel, so it won't interfere with collision checks
664 mapGrid.proxyEnabled[proxyId] := false;
666 // process players
667 for f := 0 to High(gPlayers) do
668 begin
669 plr := gPlayers[f];
670 if (plr = nil) or (not plr.alive) then continue;
671 plr.getMapBox(px, py, pw, ph);
672 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
673 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
674 begin
675 // set new position
676 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
677 end;
678 // squash player, if necessary
679 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
680 end;
682 // process gibs
683 for f := 0 to High(gGibs) do
684 begin
685 gib := @gGibs[f];
686 if not gib.alive then continue;
687 gib.getMapBox(px, py, pw, ph);
688 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
689 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
690 begin
691 // set new position
692 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
693 end;
694 end;
696 // move and push corpses
697 for f := 0 to High(gCorpses) do
698 begin
699 cor := gCorpses[f];
700 if (cor = nil) then continue;
701 cor.getMapBox(px, py, pw, ph);
702 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
703 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
704 begin
705 // set new position
706 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
707 end;
708 end;
710 // collect monsters
711 monCheckListUsed := 0;
712 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
714 // process collected monsters
715 if (monCheckListUsed > 0) then
716 begin
717 mpfrid := g_Mons_getNewMPlatFrameId();
718 for f := 0 to monCheckListUsed do
719 begin
720 mon := monCheckList[f];
721 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
722 mon.mplatCheckFrameId := mpfrid;
723 mon.getMapBox(px, py, pw, ph);
724 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
725 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
726 begin
727 // set new position
728 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
729 //???FIXME: do we really need to send monsters over the net?
730 // i don't think so, as dead reckoning should take care of 'em
731 // ok, send new monster position only if platform is going to change it's direction
732 if mNeedSend then mon.setDirty();
733 end;
734 // squash monster, if necessary
735 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
736 end;
737 end;
739 // restore panel state
740 mapGrid.proxyEnabled[proxyId] := true;
741 end;
742 end;
744 // move panel
745 X := nx;
746 Y := ny;
747 FWidth := nw;
748 FHeight := nh;
749 positionChanged();
751 actMoveTrig := false;
752 actSizeTrig := false;
754 // `mNeedSend` was set above
756 // check "size stop"
757 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
758 begin
759 mSizeSpeed.w := 0;
760 mSizeSpeed.h := 0;
761 actSizeTrig := true;
762 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
763 end;
765 // reverse moving direction, if necessary
766 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
767 begin
768 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
769 actMoveTrig := true;
770 end;
772 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
773 begin
774 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
775 actMoveTrig := true;
776 end;
778 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
779 mOldMovingActive := mMovingActive;
781 if not g_Game_IsClient then
782 begin
783 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
784 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
785 end;
787 // some triggers may activate this, don't delay sending
788 //TODO: when triggers will be able to control speed and size, check that here too
789 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
790 mOldMovingActive := mMovingActive;
791 end;
792 end;
795 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
797 function ClampInt(X, A, B: Integer): Integer;
798 begin
799 Result := X;
800 if X < A then Result := A else if X > B then Result := B;
801 end;
803 begin
804 if Enabled and (FCurTexture >= 0) and
805 (FTextureIDs[FCurTexture].Anim) and
806 (FTextureIDs[FCurTexture].AnTex <> nil) and
807 (Width > 0) and (Height > 0) and (FAlpha < 255) then
808 begin
809 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
810 FCurFrameCount := Count;
811 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
812 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
813 end;
814 end;
816 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
817 begin
818 Assert(FCurTexture >= -1, 'FCurTexture < -1');
820 // Íåò òåêñòóð:
821 if Length(FTextureIDs) = 0 then
822 FCurTexture := -1
823 else
824 // Òîëüêî îäíà òåêñòóðà:
825 if Length(FTextureIDs) = 1 then
826 begin
827 if FCurTexture = 0 then
828 FCurTexture := -1
829 else
830 FCurTexture := 0;
831 end
832 else
833 // Áîëüøå îäíîé òåêñòóðû:
834 begin
835 // Ñëåäóþùàÿ:
836 Inc(FCurTexture);
837 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
838 if FCurTexture >= Length(FTextureIDs) then
839 FCurTexture := 0;
840 end;
842 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
843 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
844 begin
845 if (FTextureIDs[FCurTexture].AnTex = nil) then
846 begin
847 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
848 Exit;
849 end;
851 if AnimLoop = 1 then
852 FTextureIDs[FCurTexture].AnTex.Loop := True
853 else
854 if AnimLoop = 2 then
855 FTextureIDs[FCurTexture].AnTex.Loop := False;
857 FTextureIDs[FCurTexture].AnTex.Reset();
858 end;
860 LastAnimLoop := AnimLoop;
861 end;
863 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
864 begin
865 if (ID >= -1) and (ID < Length(FTextureIDs)) then
866 FCurTexture := ID;
868 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
869 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
870 begin
871 if (FTextureIDs[FCurTexture].AnTex = nil) then
872 begin
873 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
874 Exit;
875 end;
877 if AnimLoop = 1 then
878 FTextureIDs[FCurTexture].AnTex.Loop := True
879 else
880 if AnimLoop = 2 then
881 FTextureIDs[FCurTexture].AnTex.Loop := False;
883 FTextureIDs[FCurTexture].AnTex.Reset();
884 end;
886 LastAnimLoop := AnimLoop;
887 end;
889 function TPanel.GetTextureID(): DWORD;
890 begin
891 Result := LongWord(TEXTURE_NONE);
893 if (FCurTexture >= 0) then
894 begin
895 if FTextureIDs[FCurTexture].Anim then
896 Result := FTextureIDs[FCurTexture].AnTex.FramesID
897 else
898 Result := FTextureIDs[FCurTexture].Tex;
899 end;
900 end;
902 function TPanel.GetTextureCount(): Integer;
903 begin
904 Result := Length(FTextureIDs);
905 if Enabled and (FCurTexture >= 0) then
906 if (FTextureIDs[FCurTexture].Anim) and
907 (FTextureIDs[FCurTexture].AnTex <> nil) and
908 (Width > 0) and (Height > 0) and (FAlpha < 255) then
909 Result := Result + 100;
910 end;
912 function TPanel.CanChangeTexture(): Boolean;
913 begin
914 Result := (GetTextureCount() > 1) or hasTexTrigger;
915 end;
917 const
918 PAN_SAVE_VERSION = 1;
920 procedure TPanel.SaveState (st: TStream);
921 var
922 anim: Boolean;
923 begin
924 if (st = nil) then exit;
926 // Ñèãíàòóðà ïàíåëè
927 utils.writeSign(st, 'PANL');
928 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
929 // Îòêðûòà/çàêðûòà, åñëè äâåðü
930 utils.writeBool(st, FEnabled);
931 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
932 utils.writeInt(st, Byte(FLiftType));
933 // Íîìåð òåêóùåé òåêñòóðû
934 utils.writeInt(st, Integer(FCurTexture));
935 // Êîîðäèíàòû è ðàçìåð
936 utils.writeInt(st, Integer(FX));
937 utils.writeInt(st, Integer(FY));
938 utils.writeInt(st, Word(FWidth));
939 utils.writeInt(st, Word(FHeight));
940 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
941 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
942 begin
943 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
944 anim := true;
945 end
946 else
947 begin
948 anim := false;
949 end;
950 utils.writeBool(st, anim);
951 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
952 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st);
954 // moving platform state
955 utils.writeInt(st, Integer(mMovingSpeed.X));
956 utils.writeInt(st, Integer(mMovingSpeed.Y));
957 utils.writeInt(st, Integer(mMovingStart.X));
958 utils.writeInt(st, Integer(mMovingStart.Y));
959 utils.writeInt(st, Integer(mMovingEnd.X));
960 utils.writeInt(st, Integer(mMovingEnd.Y));
962 utils.writeInt(st, Integer(mSizeSpeed.w));
963 utils.writeInt(st, Integer(mSizeSpeed.h));
964 utils.writeInt(st, Integer(mSizeEnd.w));
965 utils.writeInt(st, Integer(mSizeEnd.h));
967 utils.writeBool(st, mMovingActive);
968 utils.writeBool(st, mMoveOnce);
970 utils.writeInt(st, Integer(mEndPosTrig));
971 utils.writeInt(st, Integer(mEndSizeTrig));
972 end;
975 procedure TPanel.LoadState (st: TStream);
976 begin
977 if (st = nil) then exit;
979 // Ñèãíàòóðà ïàíåëè
980 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
981 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
982 // Îòêðûòà/çàêðûòà, åñëè äâåðü
983 FEnabled := utils.readBool(st);
984 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
985 FLiftType := utils.readByte(st);
986 // Íîìåð òåêóùåé òåêñòóðû
987 FCurTexture := utils.readLongInt(st);
988 // Êîîðäèíàòû è ðàçìåð
989 FX := utils.readLongInt(st);
990 FY := utils.readLongInt(st);
991 FWidth := utils.readWord(st);
992 FHeight := utils.readWord(st);
993 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
994 if utils.readBool(st) then
995 begin
996 // Åñëè äà - çàãðóæàåì àíèìàöèþ
997 Assert((FCurTexture >= 0) and
998 (FTextureIDs[FCurTexture].Anim) and
999 (FTextureIDs[FCurTexture].AnTex <> nil),
1000 'TPanel.LoadState: No animation object');
1001 FTextureIDs[FCurTexture].AnTex.LoadState(st);
1002 end;
1004 // moving platform state
1005 mMovingSpeed.X := utils.readLongInt(st);
1006 mMovingSpeed.Y := utils.readLongInt(st);
1007 mMovingStart.X := utils.readLongInt(st);
1008 mMovingStart.Y := utils.readLongInt(st);
1009 mMovingEnd.X := utils.readLongInt(st);
1010 mMovingEnd.Y := utils.readLongInt(st);
1012 mSizeSpeed.w := utils.readLongInt(st);
1013 mSizeSpeed.h := utils.readLongInt(st);
1014 mSizeEnd.w := utils.readLongInt(st);
1015 mSizeEnd.h := utils.readLongInt(st);
1017 mMovingActive := utils.readBool(st);
1018 mMoveOnce := utils.readBool(st);
1020 mEndPosTrig := utils.readLongInt(st);
1021 mEndSizeTrig := utils.readLongInt(st);
1023 positionChanged();
1024 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1025 end;
1028 end.