DEADSOFTWARE

game: disable gfx for server
[d2df-sdl.git] / src / game / g_panel.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$M+}
17 unit g_panel;
19 interface
21 uses
22 SysUtils, Classes,
23 MAPDEF, g_textures, xdynrec;
25 type
26 TAddTextureArray = array of record
27 Texture: Cardinal; // Textures[Texture]
28 end;
30 ATextureID = array of record
31 Texture: Cardinal; // Textures[Texture]
32 case Anim: Boolean of
33 False: ();
34 True: (AnTex: TAnimationState);
35 end;
37 PPanel = ^TPanel;
38 TPanel = Class (TObject)
39 private
40 const
41 private
42 mGUID: Integer; // will be assigned in "g_map.pas"
43 FAlpha: Byte;
44 FBlending: Boolean;
45 FTextureIDs: ATextureID;
46 mMovingSpeed: TDFPoint;
47 mMovingStart: TDFPoint;
48 mMovingEnd: TDFPoint;
49 mMovingActive: Boolean;
50 mMoveOnce: Boolean;
52 mOldMovingActive: Boolean;
54 mSizeSpeed: TDFSize;
55 mSizeEnd: TDFSize;
57 mEndPosTrig: Integer;
58 mEndSizeTrig: Integer;
60 mNeedSend: Boolean; // for network
62 private
63 function getx1 (): Integer; inline;
64 function gety1 (): Integer; inline;
65 function getvisvalid (): Boolean; inline;
67 function getMovingSpeedX (): Integer; inline;
68 procedure setMovingSpeedX (v: Integer); inline;
69 function getMovingSpeedY (): Integer; inline;
70 procedure setMovingSpeedY (v: Integer); inline;
72 function getMovingStartX (): Integer; inline;
73 procedure setMovingStartX (v: Integer); inline;
74 function getMovingStartY (): Integer; inline;
75 procedure setMovingStartY (v: Integer); inline;
77 function getMovingEndX (): Integer; inline;
78 procedure setMovingEndX (v: Integer); inline;
79 function getMovingEndY (): Integer; inline;
80 procedure setMovingEndY (v: Integer); inline;
82 function getSizeSpeedX (): Integer; inline;
83 procedure setSizeSpeedX (v: Integer); inline;
84 function getSizeSpeedY (): Integer; inline;
85 procedure setSizeSpeedY (v: Integer); inline;
87 function getSizeEndX (): Integer; inline;
88 procedure setSizeEndX (v: Integer); inline;
89 function getSizeEndY (): Integer; inline;
90 procedure setSizeEndY (v: Integer); inline;
92 public
93 FCurTexture: Integer; // Íîìåð òåêóùåé òåêñòóðû
94 FCurFrame: Integer;
95 FCurFrameCount: Byte;
96 FX, FY: Integer;
97 FWidth, FHeight: Word;
98 FPanelType: Word;
99 FEnabled: Boolean;
100 FDoor: Boolean;
101 FLiftType: Byte;
102 FLastAnimLoop: Byte;
103 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
104 // for now, PLEASE, don't modify 'em, or all hell will break loose
105 arrIdx: Integer; // index in one of internal arrays; sorry
106 tag: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
107 proxyId: Integer; // proxy id in map grid (DO NOT USE!)
108 mapId: AnsiString; // taken directly from map file; dunno why it is here
109 hasTexTrigger: Boolean; // HACK: true when there's a trigger than can change my texture
111 constructor Create(PanelRec: TDynRecord;
112 AddTextures: TAddTextureArray;
113 CurTex: Integer;
114 var Textures: TLevelTextureArray; aguid: Integer);
115 destructor Destroy(); override;
117 procedure Update();
118 procedure SetFrame(Frame: Integer; Count: Byte);
119 procedure NextTexture(AnimLoop: Byte = 0);
120 procedure SetTexture(ID: Integer; AnimLoop: Byte = 0);
121 function GetTextureID(): Cardinal;
122 function GetTextureCount(): Integer;
123 function CanChangeTexture(): Boolean;
125 procedure SaveState (st: TStream);
126 procedure LoadState (st: TStream);
128 procedure positionChanged (); inline;
130 function getIsGBack (): Boolean; inline; // gRenderBackgrounds
131 function getIsGStep (): Boolean; inline; // gSteps
132 function getIsGWall (): Boolean; inline; // gWalls
133 function getIsGAcid1 (): Boolean; inline; // gAcid1
134 function getIsGAcid2 (): Boolean; inline; // gAcid2
135 function getIsGWater (): Boolean; inline; // gWater
136 function getIsGFore (): Boolean; inline; // gRenderForegrounds
137 function getIsGLift (): Boolean; inline; // gLifts
138 function getIsGBlockMon (): Boolean; inline; // gBlockMon
140 // get-and-clear
141 function gncNeedSend (): Boolean; inline;
142 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
144 public
145 property visvalid: Boolean read getvisvalid; // panel is "visvalid" when it's width and height are positive
147 published
148 property guid: Integer read mGUID; // will be assigned in "g_map.pas"
149 property x0: Integer read FX;
150 property y0: Integer read FY;
151 property x1: Integer read getx1; // inclusive!
152 property y1: Integer read gety1; // inclusive!
153 property x: Integer read FX write FX;
154 property y: Integer read FY write FY;
155 property width: Word read FWidth write FWidth;
156 property height: Word read FHeight write FHeight;
157 property panelType: Word read FPanelType write FPanelType;
158 property enabled: Boolean read FEnabled write FEnabled;
159 property door: Boolean read FDoor write FDoor;
160 property liftType: Byte read FLiftType write FLiftType;
161 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
163 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
164 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
165 property movingStartX: Integer read getMovingStartX write setMovingStartX;
166 property movingStartY: Integer read getMovingStartY write setMovingStartY;
167 property movingEndX: Integer read getMovingEndX write setMovingEndX;
168 property movingEndY: Integer read getMovingEndY write setMovingEndY;
169 property movingActive: Boolean read mMovingActive write mMovingActive;
170 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
172 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
173 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
174 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
175 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
177 property isGBack: Boolean read getIsGBack;
178 property isGStep: Boolean read getIsGStep;
179 property isGWall: Boolean read getIsGWall;
180 property isGAcid1: Boolean read getIsGAcid1;
181 property isGAcid2: Boolean read getIsGAcid2;
182 property isGWater: Boolean read getIsGWater;
183 property isGFore: Boolean read getIsGFore;
184 property isGLift: Boolean read getIsGLift;
185 property isGBlockMon: Boolean read getIsGBlockMon;
187 (* private state *)
188 property Alpha: Byte read FAlpha;
189 property Blending: Boolean read FBlending;
190 property TextureIDs: ATextureID read FTextureIDs;
192 public
193 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
194 property movingStart: TDFPoint read mMovingStart write mMovingStart;
195 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
197 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
198 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
200 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
201 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
202 end;
204 TPanelArray = Array of TPanel;
206 const
207 LIFTTYPE_UP = 0;
208 LIFTTYPE_DOWN = 1;
209 LIFTTYPE_LEFT = 2;
210 LIFTTYPE_RIGHT = 3;
212 var
213 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
214 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
217 implementation
219 uses
220 {$IFDEF ENABLE_GFX}
221 g_gfx,
222 {$ENDIF}
223 g_basic, g_map, g_game, g_weapons, g_triggers,
224 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams
227 const
228 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
230 { T P a n e l : }
232 function FindTextureByName (const name: String): Integer;
233 var i: Integer;
234 begin
235 Result := -1;
236 if Textures <> nil then
237 begin
238 for i := 0 to High(Textures) do
239 begin
240 if Textures[i].TextureName = name then
241 begin
242 Result := i;
243 break;
244 end
245 end
246 end
247 end;
249 constructor TPanel.Create(PanelRec: TDynRecord;
250 AddTextures: TAddTextureArray;
251 CurTex: Integer;
252 var Textures: TLevelTextureArray; aguid: Integer);
253 var
254 i: Integer;
255 tnum: Integer;
256 begin
257 X := PanelRec.X;
258 Y := PanelRec.Y;
259 Width := PanelRec.Width;
260 Height := PanelRec.Height;
261 FAlpha := 0;
262 FBlending := False;
263 FCurFrame := 0;
264 FCurFrameCount := 0;
265 LastAnimLoop := 0;
267 mapId := PanelRec.id;
268 mGUID := aguid;
270 mMovingSpeed := PanelRec.moveSpeed;
271 mMovingStart := PanelRec.moveStart;
272 mMovingEnd := PanelRec.moveEnd;
273 mMovingActive := PanelRec['move_active'].value;
274 mOldMovingActive := mMovingActive;
275 mMoveOnce := PanelRec.moveOnce;
277 mSizeSpeed := PanelRec.sizeSpeed;
278 mSizeEnd := PanelRec.sizeEnd;
280 mEndPosTrig := PanelRec.endPosTrig;
281 mEndSizeTrig := PanelRec.endSizeTrig;
283 mNeedSend := false;
285 // Òèï ïàíåëè:
286 PanelType := PanelRec.PanelType;
287 Enabled := True;
288 Door := False;
289 LiftType := LIFTTYPE_UP;
290 hasTexTrigger := False;
292 case PanelType of
293 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
294 PANEL_CLOSEDOOR: Door := True;
295 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
296 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
297 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
298 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
299 end;
301 // Íåâèäèìàÿ:
302 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
303 begin
304 SetLength(FTextureIDs, 0);
305 FCurTexture := -1;
306 Exit;
307 end;
308 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
309 if ByteBool(PanelType and
310 (PANEL_LIFTUP or
311 PANEL_LIFTDOWN or
312 PANEL_LIFTLEFT or
313 PANEL_LIFTRIGHT or
314 PANEL_BLOCKMON)) then
315 begin
316 SetLength(FTextureIDs, 0);
317 FCurTexture := -1;
318 Exit;
319 end;
321 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
322 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
323 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
324 begin
325 SetLength(FTextureIDs, 1);
326 FTextureIDs[0].Anim := False;
327 case PanelRec.PanelType of
328 PANEL_WATER: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_WATER);
329 PANEL_ACID1: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID1);
330 PANEL_ACID2: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID2);
331 end;
332 FCurTexture := 0;
333 Exit;
334 end;
336 SetLength(FTextureIDs, Length(AddTextures));
338 if CurTex < 0 then
339 FCurTexture := -1
340 else
341 if CurTex >= Length(FTextureIDs) then
342 FCurTexture := Length(FTextureIDs) - 1
343 else
344 FCurTexture := CurTex;
346 for i := 0 to Length(FTextureIDs)-1 do
347 begin
348 FTextureIDs[i].Texture := AddTextures[i].Texture;
349 FTextureIDs[i].Anim := Textures[AddTextures[i].Texture].FramesCount > 0;
350 if FTextureIDs[i].Anim then
351 begin // Àíèìèðîâàííàÿ òåêñòóðà
352 FTextureIDs[i].AnTex := TAnimationState.Create(True, Textures[AddTextures[i].Texture].Speed, Textures[AddTextures[i].Texture].FramesCount);
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 {$IFDEF ENABLE_GFX}
444 g_Mark(px, py, pw, ph, MARK_WALL, false);
445 {$ENDIF}
446 if (Width < 1) or (Height < 1) then
447 begin
448 mapGrid.proxyEnabled[proxyId] := false;
449 end
450 else
451 begin
452 mapGrid.proxyEnabled[proxyId] := Enabled;
453 if (pw <> Width) or (ph <> Height) then
454 begin
455 //writeln('panel resize!');
456 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
457 end
458 else
459 begin
460 mapGrid.moveBody(proxyId, X, Y);
461 end;
462 {$IFDEF ENABLE_GFX}
463 g_Mark(X, Y, Width, Height, MARK_WALL);
464 {$ENDIF}
465 end;
466 end;
467 end;
468 end;
471 var
472 monCheckList: array of TMonster = nil;
473 monCheckListUsed: Integer = 0;
475 procedure TPanel.Update();
476 var
477 ox, oy: Integer;
478 nx, ny, nw, nh: Integer;
479 ex, ey, nex, ney: Integer;
480 mpw, mph: Integer;
482 // return `true` if we should move by dx,dy
483 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
484 var
485 u0: Single;
486 tex, tey: Integer;
487 pdx, pdy: Integer;
488 trtag: Integer;
489 szdx, szdy: Integer;
490 begin
491 squash := false;
492 tex := px;
493 tey := py;
494 pdx := mMovingSpeed.X;
495 pdy := mMovingSpeed.Y;
496 // standing on the platform?
497 if (py+ph = oy) then
498 begin
499 if (ontop <> nil) then ontop^ := true;
500 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
501 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
502 end
503 else
504 begin
505 if (ontop <> nil) then ontop^ := false;
506 // not standing on the platform: trace platform to see if it hits the entity
507 // first, process size change (as we cannot sweeptest both move and size change)
508 // but we don't have to check for pushing if the panel is shrinking
509 szdx := nw-mpw;
510 szdy := nh-mph;
511 if (szdx > 0) or (szdy > 0) then
512 begin
513 // ignore shrinking dimension
514 if (szdx < 0) then szdx := 0;
515 if (szdy < 0) then szdy := 0;
516 // move platform by szd* back, and check for szd* movement
517 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
518 begin
519 // yes, platform hits the entity, push the entity in the resizing direction
520 u0 := 1.0-u0; // how much path left?
521 szdx := trunc(szdx*u0);
522 szdy := trunc(szdy*u0);
523 if (szdx <> 0) or (szdy <> 0) then
524 begin
525 // has some path to go, trace the entity
526 trtag := (GridTagWall or GridTagDoor);
527 // if we're moving down, consider steps too
528 if (szdy > 0) then trtag := trtag or GridTagStep;
529 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
530 end;
531 end;
532 end;
533 // second, process platform movement, using te* as entity starting point
534 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
535 begin
536 //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]);
537 // yes, platform hits the entity, push the entity in the direction of the platform
538 u0 := 1.0-u0; // how much path left?
539 pdx := trunc(pdx*u0);
540 pdy := trunc(pdy*u0);
541 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
542 if (pdx <> 0) or (pdy <> 0) then
543 begin
544 // has some path to go, trace the entity
545 trtag := (GridTagWall or GridTagDoor);
546 // if we're moving down, consider steps too
547 if (pdy > 0) then trtag := trtag or GridTagStep;
548 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
549 end;
550 end;
551 end;
552 // done with entity movement, new coords are in te*
553 dx := tex-px;
554 dy := tey-py;
555 result := (dx <> 0) or (dy <> 0);
556 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
557 begin
558 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
559 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
560 end;
561 end;
563 function monCollect (mon: TMonster): Boolean;
564 begin
565 result := false; // don't stop
566 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
567 monCheckList[monCheckListUsed] := mon;
568 Inc(monCheckListUsed);
569 end;
571 var
572 cx0, cy0, cx1, cy1, cw, ch: Integer;
573 f: Integer;
574 px, py, pw, ph, pdx, pdy: Integer;
575 squash: Boolean;
576 plr: TPlayer;
577 gib: PGib;
578 cor: TCorpse;
579 mon: TMonster;
580 mpfrid: LongWord;
581 ontop: Boolean;
582 actMoveTrig: Boolean;
583 actSizeTrig: Boolean;
584 begin
585 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
587 if (FCurTexture >= 0) and
588 (FTextureIDs[FCurTexture].Anim) and
589 (FTextureIDs[FCurTexture].AnTex <> nil) and
590 (FAlpha < 255) then
591 begin
592 FTextureIDs[FCurTexture].AnTex.Update();
593 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
594 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
595 end;
597 if not g_dbgpan_mplat_active then exit;
599 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
600 mOldMovingActive := mMovingActive;
602 if not mMovingActive then exit;
603 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
605 //TODO: write wall size change processing
607 // moving platform?
608 begin
609 (*
610 * collect all monsters and players (aka entities) along the possible platform path
611 * if entity is standing on a platform:
612 * try to move it along the platform path, checking wall collisions
613 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
614 * try to push entity
615 * if we can't push entity all the way, squash it
616 *)
617 ox := X;
618 oy := Y;
619 mpw := Width;
620 mph := Height;
622 nw := mpw+mSizeSpeed.w;
623 nh := mph+mSizeSpeed.h;
624 nx := ox+mMovingSpeed.X;
625 ny := oy+mMovingSpeed.Y;
627 // force network updates only if some sudden change happened
628 // set the flag here, so we can sync affected monsters
629 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
630 begin
631 mNeedSend := true;
632 end
633 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
634 begin
635 mNeedSend := true;
636 end
637 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
638 begin
639 mNeedSend := true;
640 end;
642 // if pannel disappeared, we don't have to do anything
643 if (nw > 0) and (nh > 0) then
644 begin
645 // old rect
646 ex := ox+mpw-1;
647 ey := ox+mph-1;
648 // new rect
649 nex := nx+nw-1;
650 ney := ny+nh-1;
651 // full rect
652 cx0 := nmin(ox, nx);
653 cy0 := nmin(oy, ny);
654 cx1 := nmax(ex, nex);
655 cy1 := nmax(ey, ney);
656 // extrude
657 cx0 -= 1;
658 cy0 -= 1;
659 cx1 += 1;
660 cy1 += 1;
661 cw := cx1-cx0+1;
662 ch := cy1-cy0+1;
664 // process "obstacle" panels
665 if ((tag and GridTagObstacle) <> 0) then
666 begin
667 // temporarily turn off this panel, so it won't interfere with collision checks
668 mapGrid.proxyEnabled[proxyId] := false;
670 // process players
671 for f := 0 to High(gPlayers) do
672 begin
673 plr := gPlayers[f];
674 if (plr = nil) or (not plr.alive) then continue;
675 plr.getMapBox(px, py, pw, ph);
676 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
677 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
678 begin
679 // set new position
680 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
681 end;
682 // squash player, if necessary
683 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
684 end;
686 // process gibs
687 for f := 0 to High(gGibs) do
688 begin
689 gib := @gGibs[f];
690 if not gib.alive then continue;
691 gib.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 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
697 end;
698 end;
700 // move and push corpses
701 for f := 0 to High(gCorpses) do
702 begin
703 cor := gCorpses[f];
704 if (cor = nil) then continue;
705 cor.getMapBox(px, py, pw, ph);
706 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
707 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
708 begin
709 // set new position
710 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
711 end;
712 end;
714 // collect monsters
715 monCheckListUsed := 0;
716 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
718 // process collected monsters
719 if (monCheckListUsed > 0) then
720 begin
721 mpfrid := g_Mons_getNewMPlatFrameId();
722 for f := 0 to monCheckListUsed do
723 begin
724 mon := monCheckList[f];
725 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
726 mon.mplatCheckFrameId := mpfrid;
727 mon.getMapBox(px, py, pw, ph);
728 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
729 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
730 begin
731 // set new position
732 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
733 //???FIXME: do we really need to send monsters over the net?
734 // i don't think so, as dead reckoning should take care of 'em
735 // ok, send new monster position only if platform is going to change it's direction
736 if mNeedSend then mon.setDirty();
737 end;
738 // squash monster, if necessary
739 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
740 end;
741 end;
743 // restore panel state
744 mapGrid.proxyEnabled[proxyId] := true;
745 end;
746 end;
748 // move panel
749 X := nx;
750 Y := ny;
751 FWidth := nw;
752 FHeight := nh;
753 positionChanged();
755 actMoveTrig := false;
756 actSizeTrig := false;
758 // `mNeedSend` was set above
760 // check "size stop"
761 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
762 begin
763 mSizeSpeed.w := 0;
764 mSizeSpeed.h := 0;
765 actSizeTrig := true;
766 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
767 end;
769 // reverse moving direction, if necessary
770 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
771 begin
772 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
773 actMoveTrig := true;
774 end;
776 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
777 begin
778 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
779 actMoveTrig := true;
780 end;
782 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
783 mOldMovingActive := mMovingActive;
785 if not g_Game_IsClient then
786 begin
787 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
788 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
789 end;
791 // some triggers may activate this, don't delay sending
792 //TODO: when triggers will be able to control speed and size, check that here too
793 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
794 mOldMovingActive := mMovingActive;
795 end;
796 end;
799 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
801 function ClampInt(X, A, B: Integer): Integer;
802 begin
803 Result := X;
804 if X < A then Result := A else if X > B then Result := B;
805 end;
807 begin
808 if Enabled and (FCurTexture >= 0) and
809 (FTextureIDs[FCurTexture].Anim) and
810 (FTextureIDs[FCurTexture].AnTex <> nil) and
811 (Width > 0) and (Height > 0) and (FAlpha < 255) then
812 begin
813 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
814 FCurFrameCount := Count;
815 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
816 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
817 end;
818 end;
820 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
821 begin
822 Assert(FCurTexture >= -1, 'FCurTexture < -1');
824 // Íåò òåêñòóð:
825 if Length(FTextureIDs) = 0 then
826 FCurTexture := -1
827 else
828 // Òîëüêî îäíà òåêñòóðà:
829 if Length(FTextureIDs) = 1 then
830 begin
831 if FCurTexture = 0 then
832 FCurTexture := -1
833 else
834 FCurTexture := 0;
835 end
836 else
837 // Áîëüøå îäíîé òåêñòóðû:
838 begin
839 // Ñëåäóþùàÿ:
840 Inc(FCurTexture);
841 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
842 if FCurTexture >= Length(FTextureIDs) then
843 FCurTexture := 0;
844 end;
846 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
847 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
848 begin
849 if (FTextureIDs[FCurTexture].AnTex = nil) then
850 begin
851 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
852 Exit;
853 end;
855 if AnimLoop = 1 then
856 FTextureIDs[FCurTexture].AnTex.Loop := True
857 else
858 if AnimLoop = 2 then
859 FTextureIDs[FCurTexture].AnTex.Loop := False;
861 FTextureIDs[FCurTexture].AnTex.Reset();
862 end;
864 LastAnimLoop := AnimLoop;
865 end;
867 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
868 begin
869 if (ID >= -1) and (ID < Length(FTextureIDs)) then
870 FCurTexture := ID;
872 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
873 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
874 begin
875 if (FTextureIDs[FCurTexture].AnTex = nil) then
876 begin
877 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
878 Exit;
879 end;
881 if AnimLoop = 1 then
882 FTextureIDs[FCurTexture].AnTex.Loop := True
883 else
884 if AnimLoop = 2 then
885 FTextureIDs[FCurTexture].AnTex.Loop := False;
887 FTextureIDs[FCurTexture].AnTex.Reset();
888 end;
890 LastAnimLoop := AnimLoop;
891 end;
893 function TPanel.GetTextureID(): DWORD;
894 var Texture: Integer;
895 begin
896 Result := LongWord(TEXTURE_NONE);
897 if (FCurTexture >= 0) then
898 begin
899 Texture := FTextureIDs[FCurTexture].Texture;
900 case Textures[Texture].TextureName of
901 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
902 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
903 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
904 end
905 end
906 end;
908 function TPanel.GetTextureCount(): Integer;
909 begin
910 Result := Length(FTextureIDs);
911 if Enabled and (FCurTexture >= 0) then
912 if (FTextureIDs[FCurTexture].Anim) and
913 (FTextureIDs[FCurTexture].AnTex <> nil) and
914 (Width > 0) and (Height > 0) and (FAlpha < 255) then
915 Result := Result + 100;
916 end;
918 function TPanel.CanChangeTexture(): Boolean;
919 begin
920 Result := (GetTextureCount() > 1) or hasTexTrigger;
921 end;
923 const
924 PAN_SAVE_VERSION = 1;
926 procedure TPanel.SaveState (st: TStream);
927 var
928 anim: Boolean;
929 begin
930 if (st = nil) then exit;
932 // Ñèãíàòóðà ïàíåëè
933 utils.writeSign(st, 'PANL');
934 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
935 // Îòêðûòà/çàêðûòà, åñëè äâåðü
936 utils.writeBool(st, FEnabled);
937 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
938 utils.writeInt(st, Byte(FLiftType));
939 // Íîìåð òåêóùåé òåêñòóðû
940 utils.writeInt(st, Integer(FCurTexture));
941 // Êîîðäèíàòû è ðàçìåð
942 utils.writeInt(st, Integer(FX));
943 utils.writeInt(st, Integer(FY));
944 utils.writeInt(st, Word(FWidth));
945 utils.writeInt(st, Word(FHeight));
946 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
947 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
948 begin
949 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
950 anim := true;
951 end
952 else
953 begin
954 anim := false;
955 end;
956 utils.writeBool(st, anim);
957 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
958 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st, FAlpha, FBlending);
960 // moving platform state
961 utils.writeInt(st, Integer(mMovingSpeed.X));
962 utils.writeInt(st, Integer(mMovingSpeed.Y));
963 utils.writeInt(st, Integer(mMovingStart.X));
964 utils.writeInt(st, Integer(mMovingStart.Y));
965 utils.writeInt(st, Integer(mMovingEnd.X));
966 utils.writeInt(st, Integer(mMovingEnd.Y));
968 utils.writeInt(st, Integer(mSizeSpeed.w));
969 utils.writeInt(st, Integer(mSizeSpeed.h));
970 utils.writeInt(st, Integer(mSizeEnd.w));
971 utils.writeInt(st, Integer(mSizeEnd.h));
973 utils.writeBool(st, mMovingActive);
974 utils.writeBool(st, mMoveOnce);
976 utils.writeInt(st, Integer(mEndPosTrig));
977 utils.writeInt(st, Integer(mEndSizeTrig));
978 end;
981 procedure TPanel.LoadState (st: TStream);
982 begin
983 if (st = nil) then exit;
985 // Ñèãíàòóðà ïàíåëè
986 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
987 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
988 // Îòêðûòà/çàêðûòà, åñëè äâåðü
989 FEnabled := utils.readBool(st);
990 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
991 FLiftType := utils.readByte(st);
992 // Íîìåð òåêóùåé òåêñòóðû
993 FCurTexture := utils.readLongInt(st);
994 // Êîîðäèíàòû è ðàçìåð
995 FX := utils.readLongInt(st);
996 FY := utils.readLongInt(st);
997 FWidth := utils.readWord(st);
998 FHeight := utils.readWord(st);
999 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1000 if utils.readBool(st) then
1001 begin
1002 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1003 Assert((FCurTexture >= 0) and
1004 (FTextureIDs[FCurTexture].Anim) and
1005 (FTextureIDs[FCurTexture].AnTex <> nil),
1006 'TPanel.LoadState: No animation object');
1007 FTextureIDs[FCurTexture].AnTex.LoadState(st, FAlpha, FBlending);
1008 end;
1010 // moving platform state
1011 mMovingSpeed.X := utils.readLongInt(st);
1012 mMovingSpeed.Y := utils.readLongInt(st);
1013 mMovingStart.X := utils.readLongInt(st);
1014 mMovingStart.Y := utils.readLongInt(st);
1015 mMovingEnd.X := utils.readLongInt(st);
1016 mMovingEnd.Y := utils.readLongInt(st);
1018 mSizeSpeed.w := utils.readLongInt(st);
1019 mSizeSpeed.h := utils.readLongInt(st);
1020 mSizeEnd.w := utils.readLongInt(st);
1021 mSizeEnd.h := utils.readLongInt(st);
1023 mMovingActive := utils.readBool(st);
1024 mMoveOnce := utils.readBool(st);
1026 mEndPosTrig := utils.readLongInt(st);
1027 mEndSizeTrig := utils.readLongInt(st);
1029 positionChanged();
1030 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1031 end;
1034 end.