DEADSOFTWARE

f259aa3d18492575f0547abf1aae34aa18ec4780
[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_animations, xdynrec;
25 type
26 TLevelTexture = record
27 TextureName: AnsiString; // as stored in wad
28 FullName: AnsiString; // full path to texture // !!! merge it with TextureName
29 end;
31 TLevelTextureArray = array of TLevelTexture;
33 TAddTextureArray = array of record
34 Texture: Cardinal; // Textures[Texture]
35 end;
37 ATextureID = array of record
38 Texture: Cardinal; // Textures[Texture]
39 end;
41 PPanel = ^TPanel;
42 TPanel = Class (TObject)
43 private
44 const
45 private
46 mGUID: Integer; // will be assigned in "g_map.pas"
47 FAlpha: Byte;
48 FBlending: Boolean;
49 FTextureIDs: ATextureID;
50 FAnimTime: LongWord;
51 FAnimLoop: Boolean;
52 mMovingSpeed: TDFPoint;
53 mMovingStart: TDFPoint;
54 mMovingEnd: TDFPoint;
55 mMovingActive: Boolean;
56 mMoveOnce: Boolean;
58 mOldMovingActive: Boolean;
60 mSizeSpeed: TDFSize;
61 mSizeEnd: TDFSize;
63 mEndPosTrig: Integer;
64 mEndSizeTrig: Integer;
66 mNeedSend: Boolean; // for network
68 private
69 function getx1 (): Integer; inline;
70 function gety1 (): Integer; inline;
71 function getvisvalid (): Boolean; inline;
73 function getMovingSpeedX (): Integer; inline;
74 procedure setMovingSpeedX (v: Integer); inline;
75 function getMovingSpeedY (): Integer; inline;
76 procedure setMovingSpeedY (v: Integer); inline;
78 function getMovingStartX (): Integer; inline;
79 procedure setMovingStartX (v: Integer); inline;
80 function getMovingStartY (): Integer; inline;
81 procedure setMovingStartY (v: Integer); inline;
83 function getMovingEndX (): Integer; inline;
84 procedure setMovingEndX (v: Integer); inline;
85 function getMovingEndY (): Integer; inline;
86 procedure setMovingEndY (v: Integer); inline;
88 function getSizeSpeedX (): Integer; inline;
89 procedure setSizeSpeedX (v: Integer); inline;
90 function getSizeSpeedY (): Integer; inline;
91 procedure setSizeSpeedY (v: Integer); inline;
93 function getSizeEndX (): Integer; inline;
94 procedure setSizeEndX (v: Integer); inline;
95 function getSizeEndY (): Integer; inline;
96 procedure setSizeEndY (v: Integer); inline;
98 public
99 FCurTexture: Integer; // Номер текущей текстуры
100 FX, FY: Integer;
101 FOldX, FOldY: Integer;
102 FWidth, FHeight: Word;
103 FOldW, FOldH: Word;
104 FPanelType: Word;
105 FEnabled: Boolean;
106 FDoor: Boolean;
107 FLiftType: Byte;
108 FLastAnimLoop: Byte;
109 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
110 // for now, PLEASE, don't modify 'em, or all hell will break loose
111 arrIdx: Integer; // index in one of internal arrays; sorry
112 tag: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
113 proxyId: Integer; // proxy id in map grid (DO NOT USE!)
114 mapId: AnsiString; // taken directly from map file; dunno why it is here
115 hasTexTrigger: Boolean; // HACK: true when there's a trigger than can change my texture
117 constructor Create(PanelRec: TDynRecord;
118 AddTextures: TAddTextureArray;
119 CurTex: Integer;
120 var Textures: TLevelTextureArray; aguid: Integer);
121 destructor Destroy(); override;
123 procedure Update();
124 procedure SetFrame(StartTime: LongWord);
125 procedure NextTexture(AnimLoop: Byte = 0);
126 procedure SetTexture(ID: Integer; AnimLoop: Byte = 0);
127 function GetTextureID(): Cardinal;
128 function GetTextureCount(): Integer;
129 function CanChangeTexture(): Boolean;
131 procedure SaveState (st: TStream);
132 procedure LoadState (st: TStream);
134 procedure positionChanged (); inline;
136 function getIsGBack (): Boolean; inline; // gRenderBackgrounds
137 function getIsGStep (): Boolean; inline; // gSteps
138 function getIsGWall (): Boolean; inline; // gWalls
139 function getIsGAcid1 (): Boolean; inline; // gAcid1
140 function getIsGAcid2 (): Boolean; inline; // gAcid2
141 function getIsGWater (): Boolean; inline; // gWater
142 function getIsGFore (): Boolean; inline; // gRenderForegrounds
143 function getIsGLift (): Boolean; inline; // gLifts
144 function getIsGBlockMon (): Boolean; inline; // gBlockMon
146 // get-and-clear
147 function gncNeedSend (): Boolean; inline;
148 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
150 public
151 property visvalid: Boolean read getvisvalid; // panel is "visvalid" when it's width and height are positive
153 published
154 property guid: Integer read mGUID; // will be assigned in "g_map.pas"
155 property x0: Integer read FX;
156 property y0: Integer read FY;
157 property x1: Integer read getx1; // inclusive!
158 property y1: Integer read gety1; // inclusive!
159 property x: Integer read FX write FX;
160 property y: Integer read FY write FY;
161 property width: Word read FWidth write FWidth;
162 property height: Word read FHeight write FHeight;
163 property oldX: Integer read FOldX;
164 property oldY: Integer read FOldY;
165 property oldWidth: Word read FOldW;
166 property oldHeight: Word read FOldH;
167 property panelType: Word read FPanelType write FPanelType;
168 property enabled: Boolean read FEnabled write FEnabled;
169 property door: Boolean read FDoor write FDoor;
170 property liftType: Byte read FLiftType write FLiftType;
171 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
173 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
174 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
175 property movingStartX: Integer read getMovingStartX write setMovingStartX;
176 property movingStartY: Integer read getMovingStartY write setMovingStartY;
177 property movingEndX: Integer read getMovingEndX write setMovingEndX;
178 property movingEndY: Integer read getMovingEndY write setMovingEndY;
179 property movingActive: Boolean read mMovingActive write mMovingActive;
180 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
182 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
183 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
184 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
185 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
187 property isGBack: Boolean read getIsGBack;
188 property isGStep: Boolean read getIsGStep;
189 property isGWall: Boolean read getIsGWall;
190 property isGAcid1: Boolean read getIsGAcid1;
191 property isGAcid2: Boolean read getIsGAcid2;
192 property isGWater: Boolean read getIsGWater;
193 property isGFore: Boolean read getIsGFore;
194 property isGLift: Boolean read getIsGLift;
195 property isGBlockMon: Boolean read getIsGBlockMon;
197 property Alpha: Byte read FAlpha;
198 property Blending: Boolean read FBlending;
199 property TextureIDs: ATextureID read FTextureIDs;
200 property AnimTime: LongWord read FAnimTime;
201 property AnimLoop: Boolean read FAnimLoop;
203 public
204 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
205 property movingStart: TDFPoint read mMovingStart write mMovingStart;
206 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
208 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
209 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
211 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
212 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
213 end;
215 TPanelArray = Array of TPanel;
217 const
218 LIFTTYPE_UP = 0;
219 LIFTTYPE_DOWN = 1;
220 LIFTTYPE_LEFT = 2;
221 LIFTTYPE_RIGHT = 3;
223 var
224 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
225 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
228 implementation
230 uses
231 {$IFDEF ENABLE_GFX}
232 g_gfx,
233 {$ENDIF}
234 {$IFDEF ENABLE_GIBS}
235 g_gibs,
236 {$ENDIF}
237 {$IFDEF ENABLE_CORPSES}
238 g_corpses,
239 {$ENDIF}
240 g_basic, g_map, g_game, g_weapons, g_triggers, g_items,
241 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams
244 const
245 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
247 { T P a n e l : }
249 function FindTextureByName (const name: String): Integer;
250 var i: Integer;
251 begin
252 Result := -1;
253 if Textures <> nil then
254 begin
255 for i := 0 to High(Textures) do
256 begin
257 if Textures[i].TextureName = name then
258 begin
259 Result := i;
260 break;
261 end
262 end
263 end
264 end;
266 constructor TPanel.Create(PanelRec: TDynRecord;
267 AddTextures: TAddTextureArray;
268 CurTex: Integer;
269 var Textures: TLevelTextureArray; aguid: Integer);
270 var
271 i: Integer;
272 tnum: Integer;
273 begin
274 X := PanelRec.X;
275 Y := PanelRec.Y;
276 FOldX := X;
277 FOldY := Y;
278 Width := PanelRec.Width;
279 Height := PanelRec.Height;
280 FOldW := Width;
281 FOldH := Height;
282 FAlpha := 0;
283 FBlending := False;
284 LastAnimLoop := 0;
286 mapId := PanelRec.id;
287 mGUID := aguid;
289 mMovingSpeed := PanelRec.moveSpeed;
290 mMovingStart := PanelRec.moveStart;
291 mMovingEnd := PanelRec.moveEnd;
292 mMovingActive := PanelRec['move_active'].value;
293 mOldMovingActive := mMovingActive;
294 mMoveOnce := PanelRec.moveOnce;
296 mSizeSpeed := PanelRec.sizeSpeed;
297 mSizeEnd := PanelRec.sizeEnd;
299 mEndPosTrig := PanelRec.endPosTrig;
300 mEndSizeTrig := PanelRec.endSizeTrig;
302 mNeedSend := false;
304 // Тип панели:
305 PanelType := PanelRec.PanelType;
306 Enabled := True;
307 Door := False;
308 LiftType := LIFTTYPE_UP;
309 hasTexTrigger := False;
311 case PanelType of
312 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
313 PANEL_CLOSEDOOR: Door := True;
314 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
315 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
316 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
317 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
318 end;
320 // Невидимая:
321 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
322 begin
323 SetLength(FTextureIDs, 0);
324 FCurTexture := -1;
325 Exit;
326 end;
327 // Панели, не использующие текстуры:
328 if ByteBool(PanelType and
329 (PANEL_LIFTUP or
330 PANEL_LIFTDOWN or
331 PANEL_LIFTLEFT or
332 PANEL_LIFTRIGHT or
333 PANEL_BLOCKMON)) then
334 begin
335 SetLength(FTextureIDs, 0);
336 FCurTexture := -1;
337 Exit;
338 end;
340 // Если это жидкость без текстуры - спецтекстуру:
341 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
342 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
343 begin
344 SetLength(FTextureIDs, 1);
345 case PanelRec.PanelType of
346 PANEL_WATER: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_WATER);
347 PANEL_ACID1: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID1);
348 PANEL_ACID2: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID2);
349 end;
350 FCurTexture := 0;
351 Exit;
352 end;
354 SetLength(FTextureIDs, Length(AddTextures));
356 if CurTex < 0 then
357 FCurTexture := -1
358 else
359 if CurTex >= Length(FTextureIDs) then
360 FCurTexture := Length(FTextureIDs) - 1
361 else
362 FCurTexture := CurTex;
364 for i := 0 to Length(FTextureIDs) - 1 do
365 FTextureIDs[i].Texture := AddTextures[i].Texture;
367 FAnimTime := gTime;
368 FAnimLoop := true;
370 // Текстур несколько - нужно сохранять текущую:
371 //if Length(FTextureIDs) > 1 then SaveIt := True;
373 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
374 if (tnum < 0) then tnum := Length(Textures);
376 // Если не спецтекстура, то задаем размеры:
377 if ({PanelRec.TextureNum}tnum > High(Textures)) then
378 begin
379 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
380 FAlpha := 0;
381 FBlending := ByteBool(0);
382 end
383 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
384 begin
385 FAlpha := PanelRec.Alpha;
386 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
387 end;
388 end;
390 destructor TPanel.Destroy();
391 begin
392 SetLength(FTextureIDs, 0);
393 Inherited;
394 end;
396 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
397 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
398 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
400 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
401 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
402 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
403 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
405 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
406 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
407 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
408 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
410 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
411 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
412 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
413 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
415 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
416 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
417 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
418 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
420 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
421 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
422 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
423 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
425 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
426 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
427 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
428 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
429 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
430 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
431 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
432 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
433 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
435 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
436 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
438 procedure TPanel.positionChanged (); inline;
439 var
440 px, py, pw, ph: Integer;
441 begin
442 if (proxyId >= 0) then
443 begin
444 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
445 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
446 begin
448 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
449 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
451 {$IFDEF ENABLE_GFX}
452 g_Mark(px, py, pw, ph, MARK_WALL, false);
453 {$ENDIF}
454 if (Width < 1) or (Height < 1) then
455 begin
456 mapGrid.proxyEnabled[proxyId] := false;
457 end
458 else
459 begin
460 mapGrid.proxyEnabled[proxyId] := Enabled;
461 if (pw <> Width) or (ph <> Height) then
462 begin
463 //writeln('panel resize!');
464 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
465 end
466 else
467 begin
468 mapGrid.moveBody(proxyId, X, Y);
469 end;
470 {$IFDEF ENABLE_GFX}
471 g_Mark(X, Y, Width, Height, MARK_WALL);
472 {$ENDIF}
473 end;
474 end;
475 end;
476 end;
479 var
480 monCheckList: array of TMonster = nil;
481 monCheckListUsed: Integer = 0;
483 procedure TPanel.Update();
484 var
485 ox, oy: Integer;
486 nx, ny, nw, nh: Integer;
487 ex, ey, nex, ney: Integer;
488 mpw, mph: Integer;
489 conveyor: Boolean;
491 // return `true` if we should move by dx,dy
492 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
493 var
494 u0: Single;
495 tex, tey: Integer;
496 pdx, pdy: Integer;
497 trtag: Integer;
498 szdx, szdy: Integer;
499 begin
500 squash := false;
501 tex := px;
502 tey := py;
503 pdx := mMovingSpeed.X;
504 pdy := mMovingSpeed.Y;
505 // standing on the platform?
506 if (py+ph = oy) then
507 begin
508 if (ontop <> nil) then ontop^ := true;
509 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
510 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
511 end
512 else
513 begin
514 if (ontop <> nil) then ontop^ := false;
515 // not standing on the platform: trace platform to see if it hits the entity
516 // first, process size change (as we cannot sweeptest both move and size change)
517 // but we don't have to check for pushing if the panel is shrinking
518 szdx := nw-mpw;
519 szdy := nh-mph;
520 if (szdx > 0) or (szdy > 0) then
521 begin
522 // ignore shrinking dimension
523 if (szdx < 0) then szdx := 0;
524 if (szdy < 0) then szdy := 0;
525 // move platform by szd* back, and check for szd* movement
526 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
527 begin
528 // yes, platform hits the entity, push the entity in the resizing direction
529 u0 := 1.0-u0; // how much path left?
530 szdx := trunc(szdx*u0);
531 szdy := trunc(szdy*u0);
532 if (szdx <> 0) or (szdy <> 0) then
533 begin
534 // has some path to go, trace the entity
535 trtag := (GridTagWall or GridTagDoor);
536 // if we're moving down, consider steps too
537 if (szdy > 0) then trtag := trtag or GridTagStep;
538 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
539 end;
540 end;
541 end;
542 // second, process platform movement, using te* as entity starting point
543 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
544 begin
545 //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]);
546 // yes, platform hits the entity, push the entity in the direction of the platform
547 u0 := 1.0-u0; // how much path left?
548 pdx := trunc(pdx*u0);
549 pdy := trunc(pdy*u0);
550 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
551 if (pdx <> 0) or (pdy <> 0) then
552 begin
553 // has some path to go, trace the entity
554 trtag := (GridTagWall or GridTagDoor);
555 // if we're moving down, consider steps too
556 if (pdy > 0) then trtag := trtag or GridTagStep;
557 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
558 end;
559 end;
560 end;
561 // done with entity movement, new coords are in te*
562 dx := tex-px;
563 dy := tey-py;
564 result := (dx <> 0) or (dy <> 0);
565 if not conveyor and ((tag and (GridTagWall or GridTagDoor)) <> 0) then
566 begin
567 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
568 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
569 end;
570 end;
572 function monCollect (mon: TMonster): Boolean;
573 begin
574 result := false; // don't stop
575 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
576 monCheckList[monCheckListUsed] := mon;
577 Inc(monCheckListUsed);
578 end;
580 var
581 cx0, cy0, cx1, cy1, cw, ch: Integer;
582 f: Integer;
583 px, py, pw, ph, pdx, pdy: Integer;
584 squash: Boolean;
585 plr: TPlayer;
586 {$IFDEF ENABLE_GIBS}
587 gib: PGib;
588 {$ENDIF}
589 {$IFDEF ENABLE_CORPSES}
590 cor: TCorpse;
591 {$ENDIF}
592 ontop: Boolean;
593 mon: TMonster;
594 flg: PFlag;
595 itm: PItem;
596 mpfrid: LongWord;
597 actMoveTrig: Boolean;
598 actSizeTrig: Boolean;
599 begin
600 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
602 if not g_dbgpan_mplat_active then exit;
604 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
605 mOldMovingActive := mMovingActive;
607 if not mMovingActive then exit;
608 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
610 //TODO: write wall size change processing
612 // moving platform?
613 begin
614 (*
615 * collect all monsters and players (aka entities) along the possible platform path
616 * if entity is standing on a platform:
617 * try to move it along the platform path, checking wall collisions
618 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
619 * try to push entity
620 * if we can't push entity all the way, squash it
621 *)
622 ox := X;
623 oy := Y;
624 mpw := Width;
625 mph := Height;
627 // the mplat acts as a stationary conveyor belt when it's locked within a movement rect of zero area
628 conveyor := (mMovingEnd.X = mMovingStart.X) and (mMovingEnd.Y = mMovingStart.Y)
629 and (mMovingEnd.X = X) and (mMovingEnd.Y = Y);
631 nw := mpw+mSizeSpeed.w;
632 nh := mph+mSizeSpeed.h;
633 nx := ox;
634 ny := oy;
635 if not conveyor then
636 begin
637 nx += mMovingSpeed.X;
638 ny += mMovingSpeed.Y;
639 end;
641 // force network updates only if some sudden change happened
642 // set the flag here, so we can sync affected monsters
643 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
644 begin
645 mNeedSend := true;
646 end
647 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
648 begin
649 mNeedSend := true;
650 end
651 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
652 begin
653 mNeedSend := true;
654 end;
656 // if pannel disappeared, we don't have to do anything
657 if (nw > 0) and (nh > 0) then
658 begin
659 // old rect
660 ex := ox+mpw-1;
661 ey := ox+mph-1;
662 // new rect
663 nex := nx+nw-1;
664 ney := ny+nh-1;
665 // full rect
666 cx0 := nmin(ox, nx);
667 cy0 := nmin(oy, ny);
668 cx1 := nmax(ex, nex);
669 cy1 := nmax(ey, ney);
670 // extrude
671 cx0 -= 1;
672 cy0 -= 1;
673 cx1 += 1;
674 cy1 += 1;
675 cw := cx1-cx0+1;
676 ch := cy1-cy0+1;
678 // process "obstacle" panels
679 if ((tag and GridTagObstacle) <> 0) then
680 begin
681 // temporarily turn off this panel, so it won't interfere with collision checks
682 mapGrid.proxyEnabled[proxyId] := false;
684 // process players
685 for f := 0 to High(gPlayers) do
686 begin
687 plr := gPlayers[f];
688 if (plr = nil) or (not plr.alive) then continue;
689 plr.getMapBox(px, py, pw, ph);
690 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
691 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
692 begin
693 // set new position
694 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
695 end;
696 // squash player, if necessary
697 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
698 end;
700 {$IFDEF ENABLE_GIBS}
701 // process gibs
702 for f := 0 to High(gGibs) do
703 begin
704 gib := @gGibs[f];
705 if not gib.alive then continue;
706 gib.getMapBox(px, py, pw, ph);
707 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
708 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
709 begin
710 // set new position
711 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
712 end;
713 end;
714 {$ENDIF}
716 {$IFDEF ENABLE_CORPSES}
717 // move and push corpses
718 for f := 0 to High(gCorpses) do
719 begin
720 cor := gCorpses[f];
721 if (cor = nil) then continue;
722 cor.getMapBox(px, py, pw, ph);
723 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
724 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
725 begin
726 // set new position
727 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
728 end;
729 end;
730 {$ENDIF}
732 // move and push flags
733 if gGameSettings.GameMode = GM_CTF then
734 for f := FLAG_RED to FLAG_BLUE do
735 begin
736 flg := @gFlags[f];
737 if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
738 px := flg.Obj.X+flg.Obj.Rect.X;
739 py := flg.Obj.Y+flg.Obj.Rect.Y;
740 pw := flg.Obj.Rect.Width;
741 ph := flg.Obj.Rect.Height;
742 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
743 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
744 if (pdx <> 0) or (pdy <> 0) then
745 begin
746 flg.Obj.X := flg.Obj.X + pdx;
747 flg.Obj.Y := flg.Obj.Y + pdy;
748 flg.NeedSend := true;
749 end;
750 end;
752 // move and push items
753 itm := g_Items_NextAlive(-1);
754 while itm <> nil do
755 begin
756 if itm.Fall then
757 begin
758 itm.getMapBox(px, py, pw, ph);
759 if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
760 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
761 itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
762 end;
763 itm := g_Items_NextAlive(itm.myId);
764 end;
766 // collect monsters
767 monCheckListUsed := 0;
768 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
770 // process collected monsters
771 if (monCheckListUsed > 0) then
772 begin
773 mpfrid := g_Mons_getNewMPlatFrameId();
774 for f := 0 to monCheckListUsed do
775 begin
776 mon := monCheckList[f];
777 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
778 mon.mplatCheckFrameId := mpfrid;
779 mon.getMapBox(px, py, pw, ph);
780 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
781 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
782 begin
783 // set new position
784 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
785 //???FIXME: do we really need to send monsters over the net?
786 // i don't think so, as dead reckoning should take care of 'em
787 // ok, send new monster position only if platform is going to change it's direction
788 if mNeedSend then mon.setDirty();
789 end;
790 // squash monster, if necessary
791 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
792 end;
793 end;
795 // restore panel state
796 mapGrid.proxyEnabled[proxyId] := true;
797 end;
798 end;
800 // move panel
801 FOldX := X;
802 FOldY := Y;
803 X := nx;
804 Y := ny;
805 FOldW := FWidth;
806 FOldH := FHeight;
807 FWidth := nw;
808 FHeight := nh;
809 positionChanged();
811 actMoveTrig := false;
812 actSizeTrig := false;
814 // `mNeedSend` was set above
816 // check "size stop"
817 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
818 begin
819 mSizeSpeed.w := 0;
820 mSizeSpeed.h := 0;
821 actSizeTrig := true;
822 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
823 end;
825 if not conveyor then
826 begin
827 // reverse moving direction, if necessary
828 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
829 begin
830 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
831 actMoveTrig := true;
832 end;
834 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
835 begin
836 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
837 actMoveTrig := true;
838 end;
840 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
841 mOldMovingActive := mMovingActive;
842 end;
844 if not g_Game_IsClient then
845 begin
846 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
847 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
848 end;
850 // some triggers may activate this, don't delay sending
851 //TODO: when triggers will be able to control speed and size, check that here too
852 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
853 mOldMovingActive := mMovingActive;
854 end;
855 end;
857 procedure TPanel.SetFrame (StartTime: LongWord);
858 begin
859 if Enabled and (FCurTexture >= 0) and (Width > 0) and (Height > 0) and (FAlpha < 255) then
860 FAnimTime := StartTime;
861 end;
863 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
864 begin
865 Assert(FCurTexture >= -1, 'FCurTexture < -1');
867 // Нет текстур:
868 if Length(FTextureIDs) = 0 then
869 FCurTexture := -1
870 else
871 // Только одна текстура:
872 if Length(FTextureIDs) = 1 then
873 begin
874 if FCurTexture = 0 then
875 FCurTexture := -1
876 else
877 FCurTexture := 0;
878 end
879 else
880 // Больше одной текстуры:
881 begin
882 // Следующая:
883 Inc(FCurTexture);
884 // Следующей нет - возврат к началу:
885 if FCurTexture >= Length(FTextureIDs) then
886 FCurTexture := 0;
887 end;
889 if FCurTexture >= 0 then
890 begin
891 case AnimLoop of
892 1: FAnimLoop := true;
893 2: FAnimLoop := false;
894 end;
895 FAnimTime := gTime;
896 end;
898 LastAnimLoop := AnimLoop;
899 end;
901 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
902 begin
903 if (ID >= -1) and (ID < Length(FTextureIDs)) then
904 FCurTexture := ID;
906 if FCurTexture >= 0 then
907 begin
908 case AnimLoop of
909 1: FAnimLoop := true;
910 2: FAnimLoop := false;
911 end;
912 FAnimTime := gTime;
913 end;
915 LastAnimLoop := AnimLoop;
916 end;
918 function TPanel.GetTextureID(): DWORD;
919 var Texture: Integer;
920 begin
921 Result := LongWord(TEXTURE_NONE);
922 if (FCurTexture >= 0) then
923 begin
924 Texture := FTextureIDs[FCurTexture].Texture;
925 if Texture >= 0 then
926 begin
927 case Textures[Texture].TextureName of (* TODO: optimize it *)
928 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
929 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
930 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
931 end
932 end
933 end
934 end;
936 function TPanel.GetTextureCount(): Integer;
937 begin
938 Result := Length(FTextureIDs);
939 if Enabled and (FCurTexture >= 0) and (Width > 0) and (Height > 0) and (FAlpha < 255) then
940 Result := Result + 100; // ???
941 end;
943 function TPanel.CanChangeTexture(): Boolean;
944 begin
945 Result := (GetTextureCount() > 1) or hasTexTrigger;
946 end;
948 const
949 PAN_SAVE_VERSION = 1;
951 procedure TPanel.SaveState (st: TStream);
952 var anim: Boolean; stub: TAnimState;
953 begin
954 if (st = nil) then exit;
956 // Сигнатура панели
957 utils.writeSign(st, 'PANL');
958 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
959 // Открыта/закрыта, если дверь
960 utils.writeBool(st, FEnabled);
961 // Направление лифта, если лифт
962 utils.writeInt(st, Byte(FLiftType));
963 // Номер текущей текстуры
964 utils.writeInt(st, Integer(FCurTexture));
965 // Координаты и размер
966 utils.writeInt(st, Integer(FX));
967 utils.writeInt(st, Integer(FY));
968 utils.writeInt(st, Word(FWidth));
969 utils.writeInt(st, Word(FHeight));
970 // Анимирована ли текущая текстура
971 anim := FCurTexture >= 0;
972 utils.writeBool(st, anim);
973 // Если да - сохраняем анимацию
974 if anim then
975 begin
976 stub := TAnimState.Create(FAnimLoop, 1, 1);
977 stub.SaveState(st, FAlpha, FBlending);
978 stub.Invalidate;
979 end;
981 // moving platform state
982 utils.writeInt(st, Integer(mMovingSpeed.X));
983 utils.writeInt(st, Integer(mMovingSpeed.Y));
984 utils.writeInt(st, Integer(mMovingStart.X));
985 utils.writeInt(st, Integer(mMovingStart.Y));
986 utils.writeInt(st, Integer(mMovingEnd.X));
987 utils.writeInt(st, Integer(mMovingEnd.Y));
989 utils.writeInt(st, Integer(mSizeSpeed.w));
990 utils.writeInt(st, Integer(mSizeSpeed.h));
991 utils.writeInt(st, Integer(mSizeEnd.w));
992 utils.writeInt(st, Integer(mSizeEnd.h));
994 utils.writeBool(st, mMovingActive);
995 utils.writeBool(st, mMoveOnce);
997 utils.writeInt(st, Integer(mEndPosTrig));
998 utils.writeInt(st, Integer(mEndSizeTrig));
999 end;
1002 procedure TPanel.LoadState (st: TStream);
1003 var stub: TAnimState;
1004 begin
1005 if (st = nil) then exit;
1007 // Сигнатура панели
1008 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1009 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1010 // Открыта/закрыта, если дверь
1011 FEnabled := utils.readBool(st);
1012 // Направление лифта, если лифт
1013 FLiftType := utils.readByte(st);
1014 // Номер текущей текстуры
1015 FCurTexture := utils.readLongInt(st);
1016 // Координаты и размер
1017 FX := utils.readLongInt(st);
1018 FY := utils.readLongInt(st);
1019 FOldX := FX;
1020 FOldY := FY;
1021 FWidth := utils.readWord(st);
1022 FHeight := utils.readWord(st);
1023 FOldW := FWidth;
1024 FOldH := FHeight;
1025 // Анимированная ли текущая текстура
1026 if utils.readBool(st) then
1027 begin
1028 // Если да - загружаем анимацию
1029 Assert(FCurTexture >= 0, 'TPanel.LoadState: No animation object');
1030 stub := TAnimState.Create(FAnimLoop, 1, 1);
1031 stub.LoadState(st, FAlpha, FBlending);
1032 stub.Invalidate;
1033 end;
1035 // moving platform state
1036 mMovingSpeed.X := utils.readLongInt(st);
1037 mMovingSpeed.Y := utils.readLongInt(st);
1038 mMovingStart.X := utils.readLongInt(st);
1039 mMovingStart.Y := utils.readLongInt(st);
1040 mMovingEnd.X := utils.readLongInt(st);
1041 mMovingEnd.Y := utils.readLongInt(st);
1043 mSizeSpeed.w := utils.readLongInt(st);
1044 mSizeSpeed.h := utils.readLongInt(st);
1045 mSizeEnd.w := utils.readLongInt(st);
1046 mSizeEnd.h := utils.readLongInt(st);
1048 mMovingActive := utils.readBool(st);
1049 mMoveOnce := utils.readBool(st);
1051 mEndPosTrig := utils.readLongInt(st);
1052 mEndSizeTrig := utils.readLongInt(st);
1054 positionChanged();
1055 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1056 end;
1059 end.