DEADSOFTWARE

gl: fix fluid textures
[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 GetSpecialTexture (const name: String): Integer;
250 (* HACK: get texture id, if not present -> insert it into list *)
251 (* required for older maps *)
252 var i, len: Integer;
253 begin
254 i := 0; len := 0;
255 if Textures <> nil then
256 begin
257 len := Length(Textures);
258 while (i < len) and (Textures[i].TextureName <> name) do
259 Inc(i);
260 end;
261 if i >= len then
262 begin
263 i := len;
264 SetLength(Textures, len + 1);
265 Textures[i].TextureName := name;
266 end;
267 result := i;
268 end;
270 constructor TPanel.Create(PanelRec: TDynRecord;
271 AddTextures: TAddTextureArray;
272 CurTex: Integer;
273 var Textures: TLevelTextureArray; aguid: Integer);
274 var
275 i: Integer;
276 tnum: Integer;
277 begin
278 X := PanelRec.X;
279 Y := PanelRec.Y;
280 FOldX := X;
281 FOldY := Y;
282 Width := PanelRec.Width;
283 Height := PanelRec.Height;
284 FOldW := Width;
285 FOldH := Height;
286 FAlpha := 0;
287 FBlending := False;
288 LastAnimLoop := 0;
290 mapId := PanelRec.id;
291 mGUID := aguid;
293 mMovingSpeed := PanelRec.moveSpeed;
294 mMovingStart := PanelRec.moveStart;
295 mMovingEnd := PanelRec.moveEnd;
296 mMovingActive := PanelRec['move_active'].value;
297 mOldMovingActive := mMovingActive;
298 mMoveOnce := PanelRec.moveOnce;
300 mSizeSpeed := PanelRec.sizeSpeed;
301 mSizeEnd := PanelRec.sizeEnd;
303 mEndPosTrig := PanelRec.endPosTrig;
304 mEndSizeTrig := PanelRec.endSizeTrig;
306 mNeedSend := false;
308 // Тип панели:
309 PanelType := PanelRec.PanelType;
310 Enabled := True;
311 Door := False;
312 LiftType := LIFTTYPE_UP;
313 hasTexTrigger := False;
315 case PanelType of
316 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
317 PANEL_CLOSEDOOR: Door := True;
318 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
319 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
320 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
321 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
322 end;
324 // Невидимая:
325 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
326 begin
327 SetLength(FTextureIDs, 0);
328 FCurTexture := -1;
329 Exit;
330 end;
331 // Панели, не использующие текстуры:
332 if ByteBool(PanelType and
333 (PANEL_LIFTUP or
334 PANEL_LIFTDOWN or
335 PANEL_LIFTLEFT or
336 PANEL_LIFTRIGHT or
337 PANEL_BLOCKMON)) then
338 begin
339 SetLength(FTextureIDs, 0);
340 FCurTexture := -1;
341 Exit;
342 end;
344 // Если это жидкость без текстуры - спецтекстуру:
345 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
346 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
347 begin
348 SetLength(FTextureIDs, 1);
349 case PanelRec.PanelType of
350 PANEL_WATER: FTextureIDs[0].Texture := GetSpecialTexture(TEXTURE_NAME_WATER);
351 PANEL_ACID1: FTextureIDs[0].Texture := GetSpecialTexture(TEXTURE_NAME_ACID1);
352 PANEL_ACID2: FTextureIDs[0].Texture := GetSpecialTexture(TEXTURE_NAME_ACID2);
353 end;
354 FCurTexture := 0;
355 Exit;
356 end;
358 SetLength(FTextureIDs, Length(AddTextures));
360 if CurTex < 0 then
361 FCurTexture := -1
362 else
363 if CurTex >= Length(FTextureIDs) then
364 FCurTexture := Length(FTextureIDs) - 1
365 else
366 FCurTexture := CurTex;
368 for i := 0 to Length(FTextureIDs) - 1 do
369 FTextureIDs[i].Texture := AddTextures[i].Texture;
371 FAnimTime := gTime;
372 FAnimLoop := true;
374 // Текстур несколько - нужно сохранять текущую:
375 //if Length(FTextureIDs) > 1 then SaveIt := True;
377 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
378 if (tnum < 0) then tnum := Length(Textures);
380 // Если не спецтекстура, то задаем размеры:
381 if ({PanelRec.TextureNum}tnum > High(Textures)) then
382 begin
383 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
384 FAlpha := 0;
385 FBlending := ByteBool(0);
386 end
387 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
388 begin
389 FAlpha := PanelRec.Alpha;
390 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
391 end;
392 end;
394 destructor TPanel.Destroy();
395 begin
396 SetLength(FTextureIDs, 0);
397 Inherited;
398 end;
400 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
401 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
402 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
404 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
405 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
406 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
407 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
409 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
410 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
411 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
412 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
414 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
415 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
416 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
417 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
419 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
420 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
421 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
422 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
424 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
425 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
426 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
427 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
429 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
430 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
431 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
432 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
433 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
434 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
435 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
436 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
437 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
439 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
440 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
442 procedure TPanel.positionChanged (); inline;
443 var
444 px, py, pw, ph: Integer;
445 begin
446 if (proxyId >= 0) then
447 begin
448 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
449 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
450 begin
452 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
453 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
455 {$IFDEF ENABLE_GFX}
456 g_Mark(px, py, pw, ph, MARK_WALL, false);
457 {$ENDIF}
458 if (Width < 1) or (Height < 1) then
459 begin
460 mapGrid.proxyEnabled[proxyId] := false;
461 end
462 else
463 begin
464 mapGrid.proxyEnabled[proxyId] := Enabled;
465 if (pw <> Width) or (ph <> Height) then
466 begin
467 //writeln('panel resize!');
468 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
469 end
470 else
471 begin
472 mapGrid.moveBody(proxyId, X, Y);
473 end;
474 {$IFDEF ENABLE_GFX}
475 g_Mark(X, Y, Width, Height, MARK_WALL);
476 {$ENDIF}
477 end;
478 end;
479 end;
480 end;
483 var
484 monCheckList: array of TMonster = nil;
485 monCheckListUsed: Integer = 0;
487 procedure TPanel.Update();
488 var
489 ox, oy: Integer;
490 nx, ny, nw, nh: Integer;
491 ex, ey, nex, ney: Integer;
492 mpw, mph: Integer;
493 conveyor: Boolean;
495 // return `true` if we should move by dx,dy
496 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
497 var
498 u0: Single;
499 tex, tey: Integer;
500 pdx, pdy: Integer;
501 trtag: Integer;
502 szdx, szdy: Integer;
503 begin
504 squash := false;
505 tex := px;
506 tey := py;
507 pdx := mMovingSpeed.X;
508 pdy := mMovingSpeed.Y;
509 // standing on the platform?
510 if (py+ph = oy) then
511 begin
512 if (ontop <> nil) then ontop^ := true;
513 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
514 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
515 end
516 else
517 begin
518 if (ontop <> nil) then ontop^ := false;
519 // not standing on the platform: trace platform to see if it hits the entity
520 // first, process size change (as we cannot sweeptest both move and size change)
521 // but we don't have to check for pushing if the panel is shrinking
522 szdx := nw-mpw;
523 szdy := nh-mph;
524 if (szdx > 0) or (szdy > 0) then
525 begin
526 // ignore shrinking dimension
527 if (szdx < 0) then szdx := 0;
528 if (szdy < 0) then szdy := 0;
529 // move platform by szd* back, and check for szd* movement
530 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
531 begin
532 // yes, platform hits the entity, push the entity in the resizing direction
533 u0 := 1.0-u0; // how much path left?
534 szdx := trunc(szdx*u0);
535 szdy := trunc(szdy*u0);
536 if (szdx <> 0) or (szdy <> 0) then
537 begin
538 // has some path to go, trace the entity
539 trtag := (GridTagWall or GridTagDoor);
540 // if we're moving down, consider steps too
541 if (szdy > 0) then trtag := trtag or GridTagStep;
542 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
543 end;
544 end;
545 end;
546 // second, process platform movement, using te* as entity starting point
547 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
548 begin
549 //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]);
550 // yes, platform hits the entity, push the entity in the direction of the platform
551 u0 := 1.0-u0; // how much path left?
552 pdx := trunc(pdx*u0);
553 pdy := trunc(pdy*u0);
554 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
555 if (pdx <> 0) or (pdy <> 0) then
556 begin
557 // has some path to go, trace the entity
558 trtag := (GridTagWall or GridTagDoor);
559 // if we're moving down, consider steps too
560 if (pdy > 0) then trtag := trtag or GridTagStep;
561 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
562 end;
563 end;
564 end;
565 // done with entity movement, new coords are in te*
566 dx := tex-px;
567 dy := tey-py;
568 result := (dx <> 0) or (dy <> 0);
569 if not conveyor and ((tag and (GridTagWall or GridTagDoor)) <> 0) then
570 begin
571 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
572 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
573 end;
574 end;
576 function monCollect (mon: TMonster): Boolean;
577 begin
578 result := false; // don't stop
579 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
580 monCheckList[monCheckListUsed] := mon;
581 Inc(monCheckListUsed);
582 end;
584 var
585 cx0, cy0, cx1, cy1, cw, ch: Integer;
586 f: Integer;
587 px, py, pw, ph, pdx, pdy: Integer;
588 squash: Boolean;
589 plr: TPlayer;
590 {$IFDEF ENABLE_GIBS}
591 gib: PGib;
592 {$ENDIF}
593 {$IFDEF ENABLE_CORPSES}
594 cor: TCorpse;
595 {$ENDIF}
596 ontop: Boolean;
597 mon: TMonster;
598 flg: PFlag;
599 itm: PItem;
600 mpfrid: LongWord;
601 actMoveTrig: Boolean;
602 actSizeTrig: Boolean;
603 begin
604 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
606 if not g_dbgpan_mplat_active then exit;
608 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
609 mOldMovingActive := mMovingActive;
611 if not mMovingActive then exit;
612 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
614 //TODO: write wall size change processing
616 // moving platform?
617 begin
618 (*
619 * collect all monsters and players (aka entities) along the possible platform path
620 * if entity is standing on a platform:
621 * try to move it along the platform path, checking wall collisions
622 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
623 * try to push entity
624 * if we can't push entity all the way, squash it
625 *)
626 ox := X;
627 oy := Y;
628 mpw := Width;
629 mph := Height;
631 // the mplat acts as a stationary conveyor belt when it's locked within a movement rect of zero area
632 conveyor := (mMovingEnd.X = mMovingStart.X) and (mMovingEnd.Y = mMovingStart.Y)
633 and (mMovingEnd.X = X) and (mMovingEnd.Y = Y);
635 nw := mpw+mSizeSpeed.w;
636 nh := mph+mSizeSpeed.h;
637 nx := ox;
638 ny := oy;
639 if not conveyor then
640 begin
641 nx += mMovingSpeed.X;
642 ny += mMovingSpeed.Y;
643 end;
645 // force network updates only if some sudden change happened
646 // set the flag here, so we can sync affected monsters
647 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
648 begin
649 mNeedSend := true;
650 end
651 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
652 begin
653 mNeedSend := true;
654 end
655 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
656 begin
657 mNeedSend := true;
658 end;
660 // if pannel disappeared, we don't have to do anything
661 if (nw > 0) and (nh > 0) then
662 begin
663 // old rect
664 ex := ox+mpw-1;
665 ey := ox+mph-1;
666 // new rect
667 nex := nx+nw-1;
668 ney := ny+nh-1;
669 // full rect
670 cx0 := nmin(ox, nx);
671 cy0 := nmin(oy, ny);
672 cx1 := nmax(ex, nex);
673 cy1 := nmax(ey, ney);
674 // extrude
675 cx0 -= 1;
676 cy0 -= 1;
677 cx1 += 1;
678 cy1 += 1;
679 cw := cx1-cx0+1;
680 ch := cy1-cy0+1;
682 // process "obstacle" panels
683 if ((tag and GridTagObstacle) <> 0) then
684 begin
685 // temporarily turn off this panel, so it won't interfere with collision checks
686 mapGrid.proxyEnabled[proxyId] := false;
688 // process players
689 for f := 0 to High(gPlayers) do
690 begin
691 plr := gPlayers[f];
692 if (plr = nil) or (not plr.alive) then continue;
693 plr.getMapBox(px, py, pw, ph);
694 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
695 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
696 begin
697 // set new position
698 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
699 end;
700 // squash player, if necessary
701 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
702 end;
704 {$IFDEF ENABLE_GIBS}
705 // process gibs
706 for f := 0 to High(gGibs) do
707 begin
708 gib := @gGibs[f];
709 if not gib.alive then continue;
710 gib.getMapBox(px, py, pw, ph);
711 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
712 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
713 begin
714 // set new position
715 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
716 end;
717 end;
718 {$ENDIF}
720 {$IFDEF ENABLE_CORPSES}
721 // move and push corpses
722 for f := 0 to High(gCorpses) do
723 begin
724 cor := gCorpses[f];
725 if (cor = nil) then continue;
726 cor.getMapBox(px, py, pw, ph);
727 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
728 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
729 begin
730 // set new position
731 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
732 end;
733 end;
734 {$ENDIF}
736 // move and push flags
737 if gGameSettings.GameMode = GM_CTF then
738 for f := FLAG_RED to FLAG_BLUE do
739 begin
740 flg := @gFlags[f];
741 if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
742 px := flg.Obj.X+flg.Obj.Rect.X;
743 py := flg.Obj.Y+flg.Obj.Rect.Y;
744 pw := flg.Obj.Rect.Width;
745 ph := flg.Obj.Rect.Height;
746 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
747 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
748 if (pdx <> 0) or (pdy <> 0) then
749 begin
750 flg.Obj.X := flg.Obj.X + pdx;
751 flg.Obj.Y := flg.Obj.Y + pdy;
752 flg.NeedSend := true;
753 end;
754 end;
756 // move and push items
757 itm := g_Items_NextAlive(-1);
758 while itm <> nil do
759 begin
760 if itm.Fall then
761 begin
762 itm.getMapBox(px, py, pw, ph);
763 if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
764 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
765 itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
766 end;
767 itm := g_Items_NextAlive(itm.myId);
768 end;
770 // collect monsters
771 monCheckListUsed := 0;
772 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
774 // process collected monsters
775 if (monCheckListUsed > 0) then
776 begin
777 mpfrid := g_Mons_getNewMPlatFrameId();
778 for f := 0 to monCheckListUsed do
779 begin
780 mon := monCheckList[f];
781 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
782 mon.mplatCheckFrameId := mpfrid;
783 mon.getMapBox(px, py, pw, ph);
784 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
785 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
786 begin
787 // set new position
788 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
789 //???FIXME: do we really need to send monsters over the net?
790 // i don't think so, as dead reckoning should take care of 'em
791 // ok, send new monster position only if platform is going to change it's direction
792 if mNeedSend then mon.setDirty();
793 end;
794 // squash monster, if necessary
795 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
796 end;
797 end;
799 // restore panel state
800 mapGrid.proxyEnabled[proxyId] := true;
801 end;
802 end;
804 // move panel
805 FOldX := X;
806 FOldY := Y;
807 X := nx;
808 Y := ny;
809 FOldW := FWidth;
810 FOldH := FHeight;
811 FWidth := nw;
812 FHeight := nh;
813 positionChanged();
815 actMoveTrig := false;
816 actSizeTrig := false;
818 // `mNeedSend` was set above
820 // check "size stop"
821 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
822 begin
823 mSizeSpeed.w := 0;
824 mSizeSpeed.h := 0;
825 actSizeTrig := true;
826 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
827 end;
829 if not conveyor then
830 begin
831 // reverse moving direction, if necessary
832 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
833 begin
834 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
835 actMoveTrig := true;
836 end;
838 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
839 begin
840 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
841 actMoveTrig := true;
842 end;
844 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
845 mOldMovingActive := mMovingActive;
846 end;
848 if not g_Game_IsClient then
849 begin
850 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
851 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
852 end;
854 // some triggers may activate this, don't delay sending
855 //TODO: when triggers will be able to control speed and size, check that here too
856 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
857 mOldMovingActive := mMovingActive;
858 end;
859 end;
861 procedure TPanel.SetFrame (StartTime: LongWord);
862 begin
863 if Enabled and (FCurTexture >= 0) and (Width > 0) and (Height > 0) and (FAlpha < 255) then
864 FAnimTime := StartTime;
865 end;
867 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
868 begin
869 Assert(FCurTexture >= -1, 'FCurTexture < -1');
871 // Нет текстур:
872 if Length(FTextureIDs) = 0 then
873 FCurTexture := -1
874 else
875 // Только одна текстура:
876 if Length(FTextureIDs) = 1 then
877 begin
878 if FCurTexture = 0 then
879 FCurTexture := -1
880 else
881 FCurTexture := 0;
882 end
883 else
884 // Больше одной текстуры:
885 begin
886 // Следующая:
887 Inc(FCurTexture);
888 // Следующей нет - возврат к началу:
889 if FCurTexture >= Length(FTextureIDs) then
890 FCurTexture := 0;
891 end;
893 if FCurTexture >= 0 then
894 begin
895 case AnimLoop of
896 1: FAnimLoop := true;
897 2: FAnimLoop := false;
898 end;
899 FAnimTime := gTime;
900 end;
902 LastAnimLoop := AnimLoop;
903 end;
905 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
906 begin
907 if (ID >= -1) and (ID < Length(FTextureIDs)) then
908 FCurTexture := ID;
910 if FCurTexture >= 0 then
911 begin
912 case AnimLoop of
913 1: FAnimLoop := true;
914 2: FAnimLoop := false;
915 end;
916 FAnimTime := gTime;
917 end;
919 LastAnimLoop := AnimLoop;
920 end;
922 function TPanel.GetTextureID(): DWORD;
923 var Texture: Integer;
924 begin
925 Result := LongWord(TEXTURE_NONE);
926 if (FCurTexture >= 0) then
927 begin
928 Texture := FTextureIDs[FCurTexture].Texture;
929 if Texture >= 0 then
930 begin
931 case Textures[Texture].TextureName of (* TODO: optimize it *)
932 TEXTURE_NAME_WATER: Result := DWORD(TEXTURE_SPECIAL_WATER);
933 TEXTURE_NAME_ACID1: Result := DWORD(TEXTURE_SPECIAL_ACID1);
934 TEXTURE_NAME_ACID2: Result := DWORD(TEXTURE_SPECIAL_ACID2);
935 end
936 end
937 end
938 end;
940 function TPanel.GetTextureCount(): Integer;
941 begin
942 Result := Length(FTextureIDs);
943 if Enabled and (FCurTexture >= 0) and (Width > 0) and (Height > 0) and (FAlpha < 255) then
944 Result := Result + 100; // ???
945 end;
947 function TPanel.CanChangeTexture(): Boolean;
948 begin
949 Result := (GetTextureCount() > 1) or hasTexTrigger;
950 end;
952 const
953 PAN_SAVE_VERSION = 1;
955 procedure TPanel.SaveState (st: TStream);
956 var anim: Boolean; stub: TAnimState;
957 begin
958 if (st = nil) then exit;
960 // Сигнатура панели
961 utils.writeSign(st, 'PANL');
962 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
963 // Открыта/закрыта, если дверь
964 utils.writeBool(st, FEnabled);
965 // Направление лифта, если лифт
966 utils.writeInt(st, Byte(FLiftType));
967 // Номер текущей текстуры
968 utils.writeInt(st, Integer(FCurTexture));
969 // Координаты и размер
970 utils.writeInt(st, Integer(FX));
971 utils.writeInt(st, Integer(FY));
972 utils.writeInt(st, Word(FWidth));
973 utils.writeInt(st, Word(FHeight));
974 // Анимирована ли текущая текстура
975 anim := FCurTexture >= 0;
976 utils.writeBool(st, anim);
977 // Если да - сохраняем анимацию
978 if anim then
979 begin
980 stub := TAnimState.Create(FAnimLoop, 1, 1);
981 stub.SaveState(st, FAlpha, FBlending);
982 stub.Invalidate;
983 end;
985 // moving platform state
986 utils.writeInt(st, Integer(mMovingSpeed.X));
987 utils.writeInt(st, Integer(mMovingSpeed.Y));
988 utils.writeInt(st, Integer(mMovingStart.X));
989 utils.writeInt(st, Integer(mMovingStart.Y));
990 utils.writeInt(st, Integer(mMovingEnd.X));
991 utils.writeInt(st, Integer(mMovingEnd.Y));
993 utils.writeInt(st, Integer(mSizeSpeed.w));
994 utils.writeInt(st, Integer(mSizeSpeed.h));
995 utils.writeInt(st, Integer(mSizeEnd.w));
996 utils.writeInt(st, Integer(mSizeEnd.h));
998 utils.writeBool(st, mMovingActive);
999 utils.writeBool(st, mMoveOnce);
1001 utils.writeInt(st, Integer(mEndPosTrig));
1002 utils.writeInt(st, Integer(mEndSizeTrig));
1003 end;
1006 procedure TPanel.LoadState (st: TStream);
1007 var stub: TAnimState;
1008 begin
1009 if (st = nil) then exit;
1011 // Сигнатура панели
1012 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1013 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1014 // Открыта/закрыта, если дверь
1015 FEnabled := utils.readBool(st);
1016 // Направление лифта, если лифт
1017 FLiftType := utils.readByte(st);
1018 // Номер текущей текстуры
1019 FCurTexture := utils.readLongInt(st);
1020 // Координаты и размер
1021 FX := utils.readLongInt(st);
1022 FY := utils.readLongInt(st);
1023 FOldX := FX;
1024 FOldY := FY;
1025 FWidth := utils.readWord(st);
1026 FHeight := utils.readWord(st);
1027 FOldW := FWidth;
1028 FOldH := FHeight;
1029 // Анимированная ли текущая текстура
1030 if utils.readBool(st) then
1031 begin
1032 // Если да - загружаем анимацию
1033 Assert(FCurTexture >= 0, 'TPanel.LoadState: No animation object');
1034 stub := TAnimState.Create(FAnimLoop, 1, 1);
1035 stub.LoadState(st, FAlpha, FBlending);
1036 stub.Invalidate;
1037 end;
1039 // moving platform state
1040 mMovingSpeed.X := utils.readLongInt(st);
1041 mMovingSpeed.Y := utils.readLongInt(st);
1042 mMovingStart.X := utils.readLongInt(st);
1043 mMovingStart.Y := utils.readLongInt(st);
1044 mMovingEnd.X := utils.readLongInt(st);
1045 mMovingEnd.Y := utils.readLongInt(st);
1047 mSizeSpeed.w := utils.readLongInt(st);
1048 mSizeSpeed.h := utils.readLongInt(st);
1049 mSizeEnd.w := utils.readLongInt(st);
1050 mSizeEnd.h := utils.readLongInt(st);
1052 mMovingActive := utils.readBool(st);
1053 mMoveOnce := utils.readBool(st);
1055 mEndPosTrig := utils.readLongInt(st);
1056 mEndSizeTrig := utils.readLongInt(st);
1058 positionChanged();
1059 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1060 end;
1063 end.