DEADSOFTWARE

gl: draw weapon with player model
[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 AnTex: TAnimState;
33 end;
35 PPanel = ^TPanel;
36 TPanel = Class (TObject)
37 private
38 const
39 private
40 mGUID: Integer; // will be assigned in "g_map.pas"
41 FAlpha: Byte;
42 FBlending: Boolean;
43 FTextureIDs: ATextureID;
44 mMovingSpeed: TDFPoint;
45 mMovingStart: TDFPoint;
46 mMovingEnd: TDFPoint;
47 mMovingActive: Boolean;
48 mMoveOnce: Boolean;
50 mOldMovingActive: Boolean;
52 mSizeSpeed: TDFSize;
53 mSizeEnd: TDFSize;
55 mEndPosTrig: Integer;
56 mEndSizeTrig: Integer;
58 mNeedSend: Boolean; // for network
60 private
61 function getx1 (): Integer; inline;
62 function gety1 (): Integer; inline;
63 function getvisvalid (): Boolean; inline;
65 function getMovingSpeedX (): Integer; inline;
66 procedure setMovingSpeedX (v: Integer); inline;
67 function getMovingSpeedY (): Integer; inline;
68 procedure setMovingSpeedY (v: Integer); inline;
70 function getMovingStartX (): Integer; inline;
71 procedure setMovingStartX (v: Integer); inline;
72 function getMovingStartY (): Integer; inline;
73 procedure setMovingStartY (v: Integer); inline;
75 function getMovingEndX (): Integer; inline;
76 procedure setMovingEndX (v: Integer); inline;
77 function getMovingEndY (): Integer; inline;
78 procedure setMovingEndY (v: Integer); inline;
80 function getSizeSpeedX (): Integer; inline;
81 procedure setSizeSpeedX (v: Integer); inline;
82 function getSizeSpeedY (): Integer; inline;
83 procedure setSizeSpeedY (v: Integer); inline;
85 function getSizeEndX (): Integer; inline;
86 procedure setSizeEndX (v: Integer); inline;
87 function getSizeEndY (): Integer; inline;
88 procedure setSizeEndY (v: Integer); inline;
90 public
91 FCurTexture: Integer; // Íîìåð òåêóùåé òåêñòóðû
92 FCurFrame: Integer;
93 FCurFrameCount: Byte;
94 FX, FY: Integer;
95 FOldX, FOldY: Integer;
96 FWidth, FHeight: Word;
97 FOldW, FOldH: 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 oldX: Integer read FOldX;
158 property oldY: Integer read FOldY;
159 property oldWidth: Word read FOldW;
160 property oldHeight: Word read FOldH;
161 property panelType: Word read FPanelType write FPanelType;
162 property enabled: Boolean read FEnabled write FEnabled;
163 property door: Boolean read FDoor write FDoor;
164 property liftType: Byte read FLiftType write FLiftType;
165 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
167 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
168 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
169 property movingStartX: Integer read getMovingStartX write setMovingStartX;
170 property movingStartY: Integer read getMovingStartY write setMovingStartY;
171 property movingEndX: Integer read getMovingEndX write setMovingEndX;
172 property movingEndY: Integer read getMovingEndY write setMovingEndY;
173 property movingActive: Boolean read mMovingActive write mMovingActive;
174 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
176 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
177 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
178 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
179 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
181 property isGBack: Boolean read getIsGBack;
182 property isGStep: Boolean read getIsGStep;
183 property isGWall: Boolean read getIsGWall;
184 property isGAcid1: Boolean read getIsGAcid1;
185 property isGAcid2: Boolean read getIsGAcid2;
186 property isGWater: Boolean read getIsGWater;
187 property isGFore: Boolean read getIsGFore;
188 property isGLift: Boolean read getIsGLift;
189 property isGBlockMon: Boolean read getIsGBlockMon;
191 (* private state *)
192 property Alpha: Byte read FAlpha;
193 property Blending: Boolean read FBlending;
194 property TextureIDs: ATextureID read FTextureIDs;
196 public
197 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
198 property movingStart: TDFPoint read mMovingStart write mMovingStart;
199 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
201 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
202 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
204 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
205 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
206 end;
208 TPanelArray = Array of TPanel;
210 const
211 LIFTTYPE_UP = 0;
212 LIFTTYPE_DOWN = 1;
213 LIFTTYPE_LEFT = 2;
214 LIFTTYPE_RIGHT = 3;
216 var
217 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
218 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
221 implementation
223 uses
224 {$IFDEF ENABLE_GFX}
225 g_gfx,
226 {$ENDIF}
227 {$IFDEF ENABLE_GIBS}
228 g_gibs,
229 {$ENDIF}
230 {$IFDEF ENABLE_CORPSES}
231 g_corpses,
232 {$ENDIF}
233 g_basic, g_map, g_game, g_weapons, g_triggers, g_items,
234 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams
237 const
238 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
240 { T P a n e l : }
242 function FindTextureByName (const name: String): Integer;
243 var i: Integer;
244 begin
245 Result := -1;
246 if Textures <> nil then
247 begin
248 for i := 0 to High(Textures) do
249 begin
250 if Textures[i].TextureName = name then
251 begin
252 Result := i;
253 break;
254 end
255 end
256 end
257 end;
259 constructor TPanel.Create(PanelRec: TDynRecord;
260 AddTextures: TAddTextureArray;
261 CurTex: Integer;
262 var Textures: TLevelTextureArray; aguid: Integer);
263 var
264 i: Integer;
265 tnum: Integer;
266 begin
267 X := PanelRec.X;
268 Y := PanelRec.Y;
269 FOldX := X;
270 FOldY := Y;
271 Width := PanelRec.Width;
272 Height := PanelRec.Height;
273 FOldW := Width;
274 FOldH := Height;
275 FAlpha := 0;
276 FBlending := False;
277 FCurFrame := 0;
278 FCurFrameCount := 0;
279 LastAnimLoop := 0;
281 mapId := PanelRec.id;
282 mGUID := aguid;
284 mMovingSpeed := PanelRec.moveSpeed;
285 mMovingStart := PanelRec.moveStart;
286 mMovingEnd := PanelRec.moveEnd;
287 mMovingActive := PanelRec['move_active'].value;
288 mOldMovingActive := mMovingActive;
289 mMoveOnce := PanelRec.moveOnce;
291 mSizeSpeed := PanelRec.sizeSpeed;
292 mSizeEnd := PanelRec.sizeEnd;
294 mEndPosTrig := PanelRec.endPosTrig;
295 mEndSizeTrig := PanelRec.endSizeTrig;
297 mNeedSend := false;
299 // Òèï ïàíåëè:
300 PanelType := PanelRec.PanelType;
301 Enabled := True;
302 Door := False;
303 LiftType := LIFTTYPE_UP;
304 hasTexTrigger := False;
306 case PanelType of
307 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
308 PANEL_CLOSEDOOR: Door := True;
309 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
310 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
311 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
312 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
313 end;
315 // Íåâèäèìàÿ:
316 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
317 begin
318 SetLength(FTextureIDs, 0);
319 FCurTexture := -1;
320 Exit;
321 end;
322 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
323 if ByteBool(PanelType and
324 (PANEL_LIFTUP or
325 PANEL_LIFTDOWN or
326 PANEL_LIFTLEFT or
327 PANEL_LIFTRIGHT or
328 PANEL_BLOCKMON)) then
329 begin
330 SetLength(FTextureIDs, 0);
331 FCurTexture := -1;
332 Exit;
333 end;
335 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
336 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
337 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
338 begin
339 SetLength(FTextureIDs, 1);
340 case PanelRec.PanelType of
341 PANEL_WATER: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_WATER);
342 PANEL_ACID1: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID1);
343 PANEL_ACID2: FTextureIDs[0].Texture := FindTextureByName(TEXTURE_NAME_ACID2);
344 end;
345 FCurTexture := 0;
346 Exit;
347 end;
349 SetLength(FTextureIDs, Length(AddTextures));
351 if CurTex < 0 then
352 FCurTexture := -1
353 else
354 if CurTex >= Length(FTextureIDs) then
355 FCurTexture := Length(FTextureIDs) - 1
356 else
357 FCurTexture := CurTex;
359 for i := 0 to Length(FTextureIDs)-1 do
360 begin
361 FTextureIDs[i].Texture := AddTextures[i].Texture;
362 if Textures[AddTextures[i].Texture].FramesCount > 0 then
363 FTextureIDs[i].AnTex := TAnimState.Create(True, Textures[AddTextures[i].Texture].Speed, Textures[AddTextures[i].Texture].FramesCount)
364 else
365 FTextureIDs[i].AnTex.Invalidate;
366 end;
368 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
369 //if Length(FTextureIDs) > 1 then SaveIt := True;
371 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
372 if (tnum < 0) then tnum := Length(Textures);
374 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
375 if ({PanelRec.TextureNum}tnum > High(Textures)) then
376 begin
377 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
378 FAlpha := 0;
379 FBlending := ByteBool(0);
380 end
381 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
382 begin
383 FAlpha := PanelRec.Alpha;
384 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
385 end;
386 end;
388 destructor TPanel.Destroy();
389 begin
390 SetLength(FTextureIDs, 0);
391 Inherited;
392 end;
394 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
395 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
396 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
398 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
399 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
400 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
401 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
403 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
404 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
405 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
406 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
408 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
409 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
410 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
411 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
413 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
414 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
415 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
416 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
418 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
419 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
420 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
421 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
423 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
424 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
425 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
426 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
427 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
428 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
429 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
430 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
431 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
433 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
434 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
436 procedure TPanel.positionChanged (); inline;
437 var
438 px, py, pw, ph: Integer;
439 begin
440 if (proxyId >= 0) then
441 begin
442 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
443 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
444 begin
446 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
447 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
449 {$IFDEF ENABLE_GFX}
450 g_Mark(px, py, pw, ph, MARK_WALL, false);
451 {$ENDIF}
452 if (Width < 1) or (Height < 1) then
453 begin
454 mapGrid.proxyEnabled[proxyId] := false;
455 end
456 else
457 begin
458 mapGrid.proxyEnabled[proxyId] := Enabled;
459 if (pw <> Width) or (ph <> Height) then
460 begin
461 //writeln('panel resize!');
462 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
463 end
464 else
465 begin
466 mapGrid.moveBody(proxyId, X, Y);
467 end;
468 {$IFDEF ENABLE_GFX}
469 g_Mark(X, Y, Width, Height, MARK_WALL);
470 {$ENDIF}
471 end;
472 end;
473 end;
474 end;
477 var
478 monCheckList: array of TMonster = nil;
479 monCheckListUsed: Integer = 0;
481 procedure TPanel.Update();
482 var
483 ox, oy: Integer;
484 nx, ny, nw, nh: Integer;
485 ex, ey, nex, ney: Integer;
486 mpw, mph: Integer;
487 conveyor: Boolean;
489 // return `true` if we should move by dx,dy
490 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
491 var
492 u0: Single;
493 tex, tey: Integer;
494 pdx, pdy: Integer;
495 trtag: Integer;
496 szdx, szdy: Integer;
497 begin
498 squash := false;
499 tex := px;
500 tey := py;
501 pdx := mMovingSpeed.X;
502 pdy := mMovingSpeed.Y;
503 // standing on the platform?
504 if (py+ph = oy) then
505 begin
506 if (ontop <> nil) then ontop^ := true;
507 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
508 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
509 end
510 else
511 begin
512 if (ontop <> nil) then ontop^ := false;
513 // not standing on the platform: trace platform to see if it hits the entity
514 // first, process size change (as we cannot sweeptest both move and size change)
515 // but we don't have to check for pushing if the panel is shrinking
516 szdx := nw-mpw;
517 szdy := nh-mph;
518 if (szdx > 0) or (szdy > 0) then
519 begin
520 // ignore shrinking dimension
521 if (szdx < 0) then szdx := 0;
522 if (szdy < 0) then szdy := 0;
523 // move platform by szd* back, and check for szd* movement
524 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
525 begin
526 // yes, platform hits the entity, push the entity in the resizing direction
527 u0 := 1.0-u0; // how much path left?
528 szdx := trunc(szdx*u0);
529 szdy := trunc(szdy*u0);
530 if (szdx <> 0) or (szdy <> 0) then
531 begin
532 // has some path to go, trace the entity
533 trtag := (GridTagWall or GridTagDoor);
534 // if we're moving down, consider steps too
535 if (szdy > 0) then trtag := trtag or GridTagStep;
536 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
537 end;
538 end;
539 end;
540 // second, process platform movement, using te* as entity starting point
541 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
542 begin
543 //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]);
544 // yes, platform hits the entity, push the entity in the direction of the platform
545 u0 := 1.0-u0; // how much path left?
546 pdx := trunc(pdx*u0);
547 pdy := trunc(pdy*u0);
548 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
549 if (pdx <> 0) or (pdy <> 0) then
550 begin
551 // has some path to go, trace the entity
552 trtag := (GridTagWall or GridTagDoor);
553 // if we're moving down, consider steps too
554 if (pdy > 0) then trtag := trtag or GridTagStep;
555 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
556 end;
557 end;
558 end;
559 // done with entity movement, new coords are in te*
560 dx := tex-px;
561 dy := tey-py;
562 result := (dx <> 0) or (dy <> 0);
563 if not conveyor and ((tag and (GridTagWall or GridTagDoor)) <> 0) then
564 begin
565 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
566 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
567 end;
568 end;
570 function monCollect (mon: TMonster): Boolean;
571 begin
572 result := false; // don't stop
573 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
574 monCheckList[monCheckListUsed] := mon;
575 Inc(monCheckListUsed);
576 end;
578 var
579 cx0, cy0, cx1, cy1, cw, ch: Integer;
580 f: Integer;
581 px, py, pw, ph, pdx, pdy: Integer;
582 squash: Boolean;
583 plr: TPlayer;
584 {$IFDEF ENABLE_GIBS}
585 gib: PGib;
586 {$ENDIF}
587 {$IFDEF ENABLE_CORPSES}
588 cor: TCorpse;
589 {$ENDIF}
590 ontop: Boolean;
591 mon: TMonster;
592 flg: PFlag;
593 itm: PItem;
594 mpfrid: LongWord;
595 actMoveTrig: Boolean;
596 actSizeTrig: Boolean;
597 begin
598 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
600 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].AnTex.IsValid()) and (FAlpha < 255) then
601 begin
602 FTextureIDs[FCurTexture].AnTex.Update();
603 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
604 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
605 end;
607 if not g_dbgpan_mplat_active then exit;
609 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
610 mOldMovingActive := mMovingActive;
612 if not mMovingActive then exit;
613 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
615 //TODO: write wall size change processing
617 // moving platform?
618 begin
619 (*
620 * collect all monsters and players (aka entities) along the possible platform path
621 * if entity is standing on a platform:
622 * try to move it along the platform path, checking wall collisions
623 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
624 * try to push entity
625 * if we can't push entity all the way, squash it
626 *)
627 ox := X;
628 oy := Y;
629 mpw := Width;
630 mph := Height;
632 // the mplat acts as a stationary conveyor belt when it's locked within a movement rect of zero area
633 conveyor := (mMovingEnd.X = mMovingStart.X) and (mMovingEnd.Y = mMovingStart.Y)
634 and (mMovingEnd.X = X) and (mMovingEnd.Y = Y);
636 nw := mpw+mSizeSpeed.w;
637 nh := mph+mSizeSpeed.h;
638 nx := ox;
639 ny := oy;
640 if not conveyor then
641 begin
642 nx += mMovingSpeed.X;
643 ny += mMovingSpeed.Y;
644 end;
646 // force network updates only if some sudden change happened
647 // set the flag here, so we can sync affected monsters
648 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
649 begin
650 mNeedSend := true;
651 end
652 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
653 begin
654 mNeedSend := true;
655 end
656 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
657 begin
658 mNeedSend := true;
659 end;
661 // if pannel disappeared, we don't have to do anything
662 if (nw > 0) and (nh > 0) then
663 begin
664 // old rect
665 ex := ox+mpw-1;
666 ey := ox+mph-1;
667 // new rect
668 nex := nx+nw-1;
669 ney := ny+nh-1;
670 // full rect
671 cx0 := nmin(ox, nx);
672 cy0 := nmin(oy, ny);
673 cx1 := nmax(ex, nex);
674 cy1 := nmax(ey, ney);
675 // extrude
676 cx0 -= 1;
677 cy0 -= 1;
678 cx1 += 1;
679 cy1 += 1;
680 cw := cx1-cx0+1;
681 ch := cy1-cy0+1;
683 // process "obstacle" panels
684 if ((tag and GridTagObstacle) <> 0) then
685 begin
686 // temporarily turn off this panel, so it won't interfere with collision checks
687 mapGrid.proxyEnabled[proxyId] := false;
689 // process players
690 for f := 0 to High(gPlayers) do
691 begin
692 plr := gPlayers[f];
693 if (plr = nil) or (not plr.alive) then continue;
694 plr.getMapBox(px, py, pw, ph);
695 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
696 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
697 begin
698 // set new position
699 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
700 end;
701 // squash player, if necessary
702 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
703 end;
705 {$IFDEF ENABLE_GIBS}
706 // process gibs
707 for f := 0 to High(gGibs) do
708 begin
709 gib := @gGibs[f];
710 if not gib.alive then continue;
711 gib.getMapBox(px, py, pw, ph);
712 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
713 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
714 begin
715 // set new position
716 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
717 end;
718 end;
719 {$ENDIF}
721 {$IFDEF ENABLE_CORPSES}
722 // move and push corpses
723 for f := 0 to High(gCorpses) do
724 begin
725 cor := gCorpses[f];
726 if (cor = nil) then continue;
727 cor.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, @ontop) then
730 begin
731 // set new position
732 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
733 end;
734 end;
735 {$ENDIF}
737 // move and push flags
738 if gGameSettings.GameMode = GM_CTF then
739 for f := FLAG_RED to FLAG_BLUE do
740 begin
741 flg := @gFlags[f];
742 if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
743 px := flg.Obj.X+flg.Obj.Rect.X;
744 py := flg.Obj.Y+flg.Obj.Rect.Y;
745 pw := flg.Obj.Rect.Width;
746 ph := flg.Obj.Rect.Height;
747 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
748 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
749 if (pdx <> 0) or (pdy <> 0) then
750 begin
751 flg.Obj.X := flg.Obj.X + pdx;
752 flg.Obj.Y := flg.Obj.Y + pdy;
753 flg.NeedSend := true;
754 end;
755 end;
757 // move and push items
758 itm := g_Items_NextAlive(-1);
759 while itm <> nil do
760 begin
761 if itm.Fall then
762 begin
763 itm.getMapBox(px, py, pw, ph);
764 if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
765 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
766 itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
767 end;
768 itm := g_Items_NextAlive(itm.myId);
769 end;
771 // collect monsters
772 monCheckListUsed := 0;
773 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
775 // process collected monsters
776 if (monCheckListUsed > 0) then
777 begin
778 mpfrid := g_Mons_getNewMPlatFrameId();
779 for f := 0 to monCheckListUsed do
780 begin
781 mon := monCheckList[f];
782 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
783 mon.mplatCheckFrameId := mpfrid;
784 mon.getMapBox(px, py, pw, ph);
785 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
786 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
787 begin
788 // set new position
789 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
790 //???FIXME: do we really need to send monsters over the net?
791 // i don't think so, as dead reckoning should take care of 'em
792 // ok, send new monster position only if platform is going to change it's direction
793 if mNeedSend then mon.setDirty();
794 end;
795 // squash monster, if necessary
796 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
797 end;
798 end;
800 // restore panel state
801 mapGrid.proxyEnabled[proxyId] := true;
802 end;
803 end;
805 // move panel
806 FOldX := X;
807 FOldY := Y;
808 X := nx;
809 Y := ny;
810 FOldW := FWidth;
811 FOldH := FHeight;
812 FWidth := nw;
813 FHeight := nh;
814 positionChanged();
816 actMoveTrig := false;
817 actSizeTrig := false;
819 // `mNeedSend` was set above
821 // check "size stop"
822 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
823 begin
824 mSizeSpeed.w := 0;
825 mSizeSpeed.h := 0;
826 actSizeTrig := true;
827 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
828 end;
830 if not conveyor then
831 begin
832 // reverse moving direction, if necessary
833 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
834 begin
835 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
836 actMoveTrig := true;
837 end;
839 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
840 begin
841 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
842 actMoveTrig := true;
843 end;
845 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
846 mOldMovingActive := mMovingActive;
847 end;
849 if not g_Game_IsClient then
850 begin
851 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
852 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
853 end;
855 // some triggers may activate this, don't delay sending
856 //TODO: when triggers will be able to control speed and size, check that here too
857 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
858 mOldMovingActive := mMovingActive;
859 end;
860 end;
863 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
865 function ClampInt(X, A, B: Integer): Integer;
866 begin
867 Result := X;
868 if X < A then Result := A else if X > B then Result := B;
869 end;
871 begin
872 if Enabled and (FCurTexture >= 0) and (FTextureIDs[FCurTexture].AnTex.IsValid()) and (Width > 0) and (Height > 0) and (FAlpha < 255) then
873 begin
874 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
875 FCurFrameCount := Count;
876 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
877 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
878 end;
879 end;
881 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
882 begin
883 Assert(FCurTexture >= -1, 'FCurTexture < -1');
885 // Íåò òåêñòóð:
886 if Length(FTextureIDs) = 0 then
887 FCurTexture := -1
888 else
889 // Òîëüêî îäíà òåêñòóðà:
890 if Length(FTextureIDs) = 1 then
891 begin
892 if FCurTexture = 0 then
893 FCurTexture := -1
894 else
895 FCurTexture := 0;
896 end
897 else
898 // Áîëüøå îäíîé òåêñòóðû:
899 begin
900 // Ñëåäóþùàÿ:
901 Inc(FCurTexture);
902 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
903 if FCurTexture >= Length(FTextureIDs) then
904 FCurTexture := 0;
905 end;
907 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
908 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].AnTex.IsValid() then
909 begin
910 if AnimLoop = 1 then
911 FTextureIDs[FCurTexture].AnTex.Loop := True
912 else if AnimLoop = 2 then
913 FTextureIDs[FCurTexture].AnTex.Loop := False;
914 FTextureIDs[FCurTexture].AnTex.Reset();
915 end;
917 LastAnimLoop := AnimLoop;
918 end;
920 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
921 begin
922 if (ID >= -1) and (ID < Length(FTextureIDs)) then
923 FCurTexture := ID;
925 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
926 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].AnTex.IsValid() then
927 begin
928 if AnimLoop = 1 then
929 FTextureIDs[FCurTexture].AnTex.Loop := True
930 else if AnimLoop = 2 then
931 FTextureIDs[FCurTexture].AnTex.Loop := False;
932 FTextureIDs[FCurTexture].AnTex.Reset();
933 end;
935 LastAnimLoop := AnimLoop;
936 end;
938 function TPanel.GetTextureID(): DWORD;
939 var Texture: Integer;
940 begin
941 Result := LongWord(TEXTURE_NONE);
942 if (FCurTexture >= 0) then
943 begin
944 Texture := FTextureIDs[FCurTexture].Texture;
945 case Textures[Texture].TextureName of
946 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
947 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
948 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
949 end
950 end
951 end;
953 function TPanel.GetTextureCount(): Integer;
954 begin
955 Result := Length(FTextureIDs);
956 if Enabled and (FCurTexture >= 0) then
957 if (FTextureIDs[FCurTexture].AnTex.IsValid()) and (Width > 0) and (Height > 0) and (FAlpha < 255) then
958 Result := Result + 100;
959 end;
961 function TPanel.CanChangeTexture(): Boolean;
962 begin
963 Result := (GetTextureCount() > 1) or hasTexTrigger;
964 end;
966 const
967 PAN_SAVE_VERSION = 1;
969 procedure TPanel.SaveState (st: TStream);
970 var
971 anim: Boolean;
972 begin
973 if (st = nil) then exit;
975 // Ñèãíàòóðà ïàíåëè
976 utils.writeSign(st, 'PANL');
977 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
978 // Îòêðûòà/çàêðûòà, åñëè äâåðü
979 utils.writeBool(st, FEnabled);
980 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
981 utils.writeInt(st, Byte(FLiftType));
982 // Íîìåð òåêóùåé òåêñòóðû
983 utils.writeInt(st, Integer(FCurTexture));
984 // Êîîðäèíàòû è ðàçìåð
985 utils.writeInt(st, Integer(FX));
986 utils.writeInt(st, Integer(FY));
987 utils.writeInt(st, Word(FWidth));
988 utils.writeInt(st, Word(FHeight));
989 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
990 anim := (FCurTexture >= 0) and (FTextureIDs[FCurTexture].AnTex.IsValid());
991 utils.writeBool(st, anim);
992 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
993 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st, FAlpha, FBlending);
995 // moving platform state
996 utils.writeInt(st, Integer(mMovingSpeed.X));
997 utils.writeInt(st, Integer(mMovingSpeed.Y));
998 utils.writeInt(st, Integer(mMovingStart.X));
999 utils.writeInt(st, Integer(mMovingStart.Y));
1000 utils.writeInt(st, Integer(mMovingEnd.X));
1001 utils.writeInt(st, Integer(mMovingEnd.Y));
1003 utils.writeInt(st, Integer(mSizeSpeed.w));
1004 utils.writeInt(st, Integer(mSizeSpeed.h));
1005 utils.writeInt(st, Integer(mSizeEnd.w));
1006 utils.writeInt(st, Integer(mSizeEnd.h));
1008 utils.writeBool(st, mMovingActive);
1009 utils.writeBool(st, mMoveOnce);
1011 utils.writeInt(st, Integer(mEndPosTrig));
1012 utils.writeInt(st, Integer(mEndSizeTrig));
1013 end;
1016 procedure TPanel.LoadState (st: TStream);
1017 begin
1018 if (st = nil) then exit;
1020 // Ñèãíàòóðà ïàíåëè
1021 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1022 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1023 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1024 FEnabled := utils.readBool(st);
1025 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1026 FLiftType := utils.readByte(st);
1027 // Íîìåð òåêóùåé òåêñòóðû
1028 FCurTexture := utils.readLongInt(st);
1029 // Êîîðäèíàòû è ðàçìåð
1030 FX := utils.readLongInt(st);
1031 FY := utils.readLongInt(st);
1032 FOldX := FX;
1033 FOldY := FY;
1034 FWidth := utils.readWord(st);
1035 FHeight := utils.readWord(st);
1036 FOldW := FWidth;
1037 FOldH := FHeight;
1038 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1039 if utils.readBool(st) then
1040 begin
1041 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1042 Assert((FCurTexture >= 0) and (FTextureIDs[FCurTexture].AnTex.IsValid()), 'TPanel.LoadState: No animation object');
1043 FTextureIDs[FCurTexture].AnTex.LoadState(st, FAlpha, FBlending);
1044 end;
1046 // moving platform state
1047 mMovingSpeed.X := utils.readLongInt(st);
1048 mMovingSpeed.Y := utils.readLongInt(st);
1049 mMovingStart.X := utils.readLongInt(st);
1050 mMovingStart.Y := utils.readLongInt(st);
1051 mMovingEnd.X := utils.readLongInt(st);
1052 mMovingEnd.Y := utils.readLongInt(st);
1054 mSizeSpeed.w := utils.readLongInt(st);
1055 mSizeSpeed.h := utils.readLongInt(st);
1056 mSizeEnd.w := utils.readLongInt(st);
1057 mSizeEnd.h := utils.readLongInt(st);
1059 mMovingActive := utils.readBool(st);
1060 mMoveOnce := utils.readBool(st);
1062 mEndPosTrig := utils.readLongInt(st);
1063 mEndSizeTrig := utils.readLongInt(st);
1065 positionChanged();
1066 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1067 end;
1070 end.