DEADSOFTWARE

Panel: Fix SetFrame out of index
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$M+}
18 unit g_panel;
20 interface
22 uses
23 SysUtils, Classes,
24 MAPDEF, g_textures, xdynrec;
26 type
27 TAddTextureArray = Array of
28 record
29 Texture: Cardinal;
30 Anim: Boolean;
31 end;
33 PPanel = ^TPanel;
34 TPanel = Class (TObject)
35 private
36 const
37 private
38 mGUID: Integer; // will be assigned in "g_map.pas"
39 FTextureWidth: Word;
40 FTextureHeight: Word;
41 FAlpha: Byte;
42 FBlending: Boolean;
43 FTextureIDs: Array of
44 record
45 case Anim: Boolean of
46 False: (Tex: Cardinal);
47 True: (AnTex: TAnimation);
48 end;
50 mMovingSpeed: TDFPoint;
51 mMovingStart: TDFPoint;
52 mMovingEnd: TDFPoint;
53 mMovingActive: Boolean;
54 mMoveOnce: Boolean;
56 mOldMovingActive: Boolean;
58 mSizeSpeed: TDFSize;
59 mSizeEnd: TDFSize;
61 mEndPosTrig: Integer;
62 mEndSizeTrig: Integer;
64 mNeedSend: Boolean; // for network
66 private
67 function getx1 (): Integer; inline;
68 function gety1 (): Integer; inline;
69 function getvisvalid (): Boolean; inline;
71 function getMovingSpeedX (): Integer; inline;
72 procedure setMovingSpeedX (v: Integer); inline;
73 function getMovingSpeedY (): Integer; inline;
74 procedure setMovingSpeedY (v: Integer); inline;
76 function getMovingStartX (): Integer; inline;
77 procedure setMovingStartX (v: Integer); inline;
78 function getMovingStartY (): Integer; inline;
79 procedure setMovingStartY (v: Integer); inline;
81 function getMovingEndX (): Integer; inline;
82 procedure setMovingEndX (v: Integer); inline;
83 function getMovingEndY (): Integer; inline;
84 procedure setMovingEndY (v: Integer); inline;
86 function getSizeSpeedX (): Integer; inline;
87 procedure setSizeSpeedX (v: Integer); inline;
88 function getSizeSpeedY (): Integer; inline;
89 procedure setSizeSpeedY (v: Integer); inline;
91 function getSizeEndX (): Integer; inline;
92 procedure setSizeEndX (v: Integer); inline;
93 function getSizeEndY (): Integer; inline;
94 procedure setSizeEndY (v: Integer); inline;
96 public
97 FCurTexture: Integer; // Íîìåð òåêóùåé òåêñòóðû
98 FCurFrame: Integer;
99 FCurFrameCount: Byte;
100 FX, FY: Integer;
101 FWidth, FHeight: Word;
102 FPanelType: Word;
103 FEnabled: Boolean;
104 FDoor: Boolean;
105 FLiftType: Byte;
106 FLastAnimLoop: Byte;
107 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
108 // for now, PLEASE, don't modify 'em, or all hell will break loose
109 arrIdx: Integer; // index in one of internal arrays; sorry
110 tag: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
111 proxyId: Integer; // proxy id in map grid (DO NOT USE!)
112 mapId: AnsiString; // taken directly from map file; dunno why it is here
113 hasTexTrigger: Boolean; // HACK: true when there's a trigger than can change my texture
115 constructor Create(PanelRec: TDynRecord;
116 AddTextures: TAddTextureArray;
117 CurTex: Integer;
118 var Textures: TLevelTextureArray; aguid: Integer);
119 destructor Destroy(); override;
121 procedure Draw (hasAmbient: Boolean; constref ambColor: TDFColor);
122 procedure DrawShadowVolume(lightX: Integer; lightY: Integer; radius: Integer);
123 procedure Update();
124 procedure SetFrame(Frame: Integer; Count: Byte);
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 panelType: Word read FPanelType write FPanelType;
164 property enabled: Boolean read FEnabled write FEnabled;
165 property door: Boolean read FDoor write FDoor;
166 property liftType: Byte read FLiftType write FLiftType;
167 property lastAnimLoop: Byte read FLastAnimLoop write FLastAnimLoop;
169 property movingSpeedX: Integer read getMovingSpeedX write setMovingSpeedX;
170 property movingSpeedY: Integer read getMovingSpeedY write setMovingSpeedY;
171 property movingStartX: Integer read getMovingStartX write setMovingStartX;
172 property movingStartY: Integer read getMovingStartY write setMovingStartY;
173 property movingEndX: Integer read getMovingEndX write setMovingEndX;
174 property movingEndY: Integer read getMovingEndY write setMovingEndY;
175 property movingActive: Boolean read mMovingActive write mMovingActive;
176 property moveOnce: Boolean read mMoveOnce write mMoveOnce;
178 property sizeSpeedX: Integer read getSizeSpeedX write setSizeSpeedX;
179 property sizeSpeedY: Integer read getSizeSpeedY write setSizeSpeedY;
180 property sizeEndX: Integer read getSizeEndX write setSizeEndX;
181 property sizeEndY: Integer read getSizeEndY write setSizeEndY;
183 property isGBack: Boolean read getIsGBack;
184 property isGStep: Boolean read getIsGStep;
185 property isGWall: Boolean read getIsGWall;
186 property isGAcid1: Boolean read getIsGAcid1;
187 property isGAcid2: Boolean read getIsGAcid2;
188 property isGWater: Boolean read getIsGWater;
189 property isGFore: Boolean read getIsGFore;
190 property isGLift: Boolean read getIsGLift;
191 property isGBlockMon: Boolean read getIsGBlockMon;
193 public
194 property movingSpeed: TDFPoint read mMovingSpeed write mMovingSpeed;
195 property movingStart: TDFPoint read mMovingStart write mMovingStart;
196 property movingEnd: TDFPoint read mMovingEnd write mMovingEnd;
198 property sizeSpeed: TDFSize read mSizeSpeed write mSizeSpeed;
199 property sizeEnd: TDFSize read mSizeEnd write mSizeEnd;
201 property endPosTrigId: Integer read mEndPosTrig write mEndPosTrig;
202 property endSizeTrigId: Integer read mEndSizeTrig write mEndSizeTrig;
203 end;
205 TPanelArray = Array of TPanel;
207 var
208 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
209 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
212 implementation
214 uses
215 {$IFDEF USE_NANOGL}
216 nanoGL,
217 {$ELSE}
218 GL,
219 {$ENDIF}
220 e_texture, g_basic, g_map, g_game, g_gfx, e_graphics, g_weapons, g_triggers,
221 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
223 const
224 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
226 { T P a n e l : }
228 constructor TPanel.Create(PanelRec: TDynRecord;
229 AddTextures: TAddTextureArray;
230 CurTex: Integer;
231 var Textures: TLevelTextureArray; aguid: Integer);
232 var
233 i: Integer;
234 tnum: Integer;
235 begin
236 X := PanelRec.X;
237 Y := PanelRec.Y;
238 Width := PanelRec.Width;
239 Height := PanelRec.Height;
240 FAlpha := 0;
241 FBlending := False;
242 FCurFrame := 0;
243 FCurFrameCount := 0;
244 LastAnimLoop := 0;
246 mapId := PanelRec.id;
247 mGUID := aguid;
249 mMovingSpeed := PanelRec.moveSpeed;
250 mMovingStart := PanelRec.moveStart;
251 mMovingEnd := PanelRec.moveEnd;
252 mMovingActive := PanelRec['move_active'].value;
253 mOldMovingActive := mMovingActive;
254 mMoveOnce := PanelRec.moveOnce;
256 mSizeSpeed := PanelRec.sizeSpeed;
257 mSizeEnd := PanelRec.sizeEnd;
259 mEndPosTrig := PanelRec.endPosTrig;
260 mEndSizeTrig := PanelRec.endSizeTrig;
262 mNeedSend := false;
264 // Òèï ïàíåëè:
265 PanelType := PanelRec.PanelType;
266 Enabled := True;
267 Door := False;
268 LiftType := 0;
269 hasTexTrigger := False;
271 case PanelType of
272 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
273 PANEL_CLOSEDOOR: Door := True;
274 PANEL_LIFTUP: LiftType := 0; //???
275 PANEL_LIFTDOWN: LiftType := 1;
276 PANEL_LIFTLEFT: LiftType := 2;
277 PANEL_LIFTRIGHT: LiftType := 3;
278 end;
280 // Íåâèäèìàÿ:
281 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
282 begin
283 SetLength(FTextureIDs, 0);
284 FCurTexture := -1;
285 Exit;
286 end;
287 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
288 if ByteBool(PanelType and
289 (PANEL_LIFTUP or
290 PANEL_LIFTDOWN or
291 PANEL_LIFTLEFT or
292 PANEL_LIFTRIGHT or
293 PANEL_BLOCKMON)) then
294 begin
295 SetLength(FTextureIDs, 0);
296 FCurTexture := -1;
297 Exit;
298 end;
300 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
301 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
302 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
303 begin
304 SetLength(FTextureIDs, 1);
305 FTextureIDs[0].Anim := False;
307 case PanelRec.PanelType of
308 PANEL_WATER:
309 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_WATER);
310 PANEL_ACID1:
311 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID1);
312 PANEL_ACID2:
313 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID2);
314 end;
316 FCurTexture := 0;
317 Exit;
318 end;
320 SetLength(FTextureIDs, Length(AddTextures));
322 if CurTex < 0 then
323 FCurTexture := -1
324 else
325 if CurTex >= Length(FTextureIDs) then
326 FCurTexture := Length(FTextureIDs) - 1
327 else
328 FCurTexture := CurTex;
330 for i := 0 to Length(FTextureIDs)-1 do
331 begin
332 FTextureIDs[i].Anim := AddTextures[i].Anim;
333 if FTextureIDs[i].Anim then
334 begin // Àíèìèðîâàííàÿ òåêñòóðà
335 FTextureIDs[i].AnTex :=
336 TAnimation.Create(Textures[AddTextures[i].Texture].FramesID,
337 True, Textures[AddTextures[i].Texture].Speed);
338 FTextureIDs[i].AnTex.Blending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
339 FTextureIDs[i].AnTex.Alpha := PanelRec.Alpha;
340 end
341 else
342 begin // Îáû÷íàÿ òåêñòóðà
343 FTextureIDs[i].Tex := Textures[AddTextures[i].Texture].TextureID;
344 end;
345 end;
347 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
348 //if Length(FTextureIDs) > 1 then SaveIt := True;
350 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
351 if (tnum < 0) then tnum := Length(Textures);
353 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
354 if ({PanelRec.TextureNum}tnum > High(Textures)) then
355 begin
356 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
357 FTextureWidth := 2;
358 FTextureHeight := 2;
359 FAlpha := 0;
360 FBlending := ByteBool(0);
361 end
362 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
363 begin
364 FTextureWidth := Textures[{PanelRec.TextureNum}tnum].Width;
365 FTextureHeight := Textures[{PanelRec.TextureNum}tnum].Height;
366 FAlpha := PanelRec.Alpha;
367 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
368 end;
369 end;
371 destructor TPanel.Destroy();
372 var
373 i: Integer;
374 begin
375 for i := 0 to High(FTextureIDs) do
376 if FTextureIDs[i].Anim then
377 FTextureIDs[i].AnTex.Free();
378 SetLength(FTextureIDs, 0);
380 Inherited;
381 end;
383 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
384 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
385 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
387 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
388 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
389 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
390 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
392 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
393 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
394 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
395 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
397 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
398 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
399 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
400 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
402 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
403 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
404 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
405 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
407 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
408 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
409 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
410 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
412 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
413 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
414 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
415 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
416 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
417 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
418 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
419 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
420 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
422 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
423 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
426 procedure TPanel.Draw (hasAmbient: Boolean; constref ambColor: TDFColor);
427 var
428 xx, yy: Integer;
429 NoTextureID: DWORD;
430 NW, NH: Word;
431 begin
432 if {Enabled and} (FCurTexture >= 0) and
433 (Width > 0) and (Height > 0) and (FAlpha < 255) {and
434 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
435 begin
436 if FTextureIDs[FCurTexture].Anim then
437 begin // Àíèìèðîâàííàÿ òåêñòóðà
438 if FTextureIDs[FCurTexture].AnTex = nil then
439 Exit;
441 for xx := 0 to (Width div FTextureWidth)-1 do
442 for yy := 0 to (Height div FTextureHeight)-1 do
443 FTextureIDs[FCurTexture].AnTex.Draw(
444 X + xx*FTextureWidth,
445 Y + yy*FTextureHeight, TMirrorType.None);
446 end
447 else
448 begin // Îáû÷íàÿ òåêñòóðà
449 case FTextureIDs[FCurTexture].Tex of
450 LongWord(TEXTURE_SPECIAL_WATER): e_DrawFillQuad(X, Y, X+Width-1, Y+Height-1, 0, 0, 255, 0, TBlending.Filter);
451 LongWord(TEXTURE_SPECIAL_ACID1): e_DrawFillQuad(X, Y, X+Width-1, Y+Height-1, 0, 128, 0, 0, TBlending.Filter);
452 LongWord(TEXTURE_SPECIAL_ACID2): e_DrawFillQuad(X, Y, X+Width-1, Y+Height-1, 128, 0, 0, 0, TBlending.Filter);
453 LongWord(TEXTURE_NONE):
454 if g_Texture_Get('NOTEXTURE', NoTextureID) then
455 begin
456 e_GetTextureSize(NoTextureID, @NW, @NH);
457 e_DrawFill(NoTextureID, X, Y, Width div NW, Height div NH, 0, False, False);
458 end
459 else
460 begin
461 xx := X + (Width div 2);
462 yy := Y + (Height div 2);
463 e_DrawFillQuad(X, Y, xx, yy, 255, 0, 255, 0);
464 e_DrawFillQuad(xx, Y, X+Width-1, yy, 255, 255, 0, 0);
465 e_DrawFillQuad(X, yy, xx, Y+Height-1, 255, 255, 0, 0);
466 e_DrawFillQuad(xx, yy, X+Width-1, Y+Height-1, 255, 0, 255, 0);
467 end;
468 else
469 begin
470 if not mMovingActive then
471 e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending, hasAmbient)
472 else
473 e_DrawFillX(FTextureIDs[FCurTexture].Tex, X, Y, Width, Height, FAlpha, True, FBlending, g_dbg_scale, hasAmbient);
474 if hasAmbient then e_AmbientQuad(X, Y, Width, Height, ambColor.r, ambColor.g, ambColor.b, ambColor.a);
475 end;
476 end;
477 end;
478 end;
479 end;
481 procedure TPanel.DrawShadowVolume(lightX: Integer; lightY: Integer; radius: Integer);
482 procedure extrude (x: Integer; y: Integer);
483 begin
484 glVertex2i(x+(x-lightX)*500, y+(y-lightY)*500);
485 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
486 end;
488 procedure drawLine (x0: Integer; y0: Integer; x1: Integer; y1: Integer);
489 begin
490 // does this side facing the light?
491 if ((x1-x0)*(lightY-y0)-(lightX-x0)*(y1-y0) >= 0) then exit;
492 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
493 // this edge is facing the light, extrude and draw it
494 glVertex2i(x0, y0);
495 glVertex2i(x1, y1);
496 extrude(x1, y1);
497 extrude(x0, y0);
498 end;
500 begin
501 if radius < 4 then exit;
502 if Enabled and (FCurTexture >= 0) and (Width > 0) and (Height > 0) and (FAlpha < 255) {and
503 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
504 begin
505 if not FTextureIDs[FCurTexture].Anim then
506 begin
507 case FTextureIDs[FCurTexture].Tex of
508 LongWord(TEXTURE_SPECIAL_WATER): exit;
509 LongWord(TEXTURE_SPECIAL_ACID1): exit;
510 LongWord(TEXTURE_SPECIAL_ACID2): exit;
511 LongWord(TEXTURE_NONE): exit;
512 end;
513 end;
514 if (X+Width < lightX-radius) then exit;
515 if (Y+Height < lightY-radius) then exit;
516 if (X > lightX+radius) then exit;
517 if (Y > lightY+radius) then exit;
518 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
520 glBegin(GL_QUADS);
521 drawLine(x, y, x+width, y); // top
522 drawLine(x+width, y, x+width, y+height); // right
523 drawLine(x+width, y+height, x, y+height); // bottom
524 drawLine(x, y+height, x, y); // left
525 glEnd();
526 end;
527 end;
530 procedure TPanel.positionChanged (); inline;
531 var
532 px, py, pw, ph: Integer;
533 begin
534 if (proxyId >= 0) then
535 begin
536 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
537 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
538 begin
540 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
541 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
543 g_Mark(px, py, pw, ph, MARK_WALL, false);
544 if (Width < 1) or (Height < 1) then
545 begin
546 mapGrid.proxyEnabled[proxyId] := false;
547 end
548 else
549 begin
550 mapGrid.proxyEnabled[proxyId] := Enabled;
551 if (pw <> Width) or (ph <> Height) then
552 begin
553 //writeln('panel resize!');
554 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
555 end
556 else
557 begin
558 mapGrid.moveBody(proxyId, X, Y);
559 end;
560 g_Mark(X, Y, Width, Height, MARK_WALL);
561 end;
562 end;
563 end;
564 end;
567 var
568 monCheckList: array of TMonster = nil;
569 monCheckListUsed: Integer = 0;
571 procedure TPanel.Update();
572 var
573 ox, oy: Integer;
574 nx, ny, nw, nh: Integer;
575 ex, ey, nex, ney: Integer;
576 mpw, mph: Integer;
578 // return `true` if we should move by dx,dy
579 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
580 var
581 u0: Single;
582 tex, tey: Integer;
583 pdx, pdy: Integer;
584 trtag: Integer;
585 szdx, szdy: Integer;
586 begin
587 squash := false;
588 tex := px;
589 tey := py;
590 pdx := mMovingSpeed.X;
591 pdy := mMovingSpeed.Y;
592 // standing on the platform?
593 if (py+ph = oy) then
594 begin
595 if (ontop <> nil) then ontop^ := true;
596 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
597 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
598 end
599 else
600 begin
601 if (ontop <> nil) then ontop^ := false;
602 // not standing on the platform: trace platform to see if it hits the entity
603 // first, process size change (as we cannot sweeptest both move and size change)
604 // but we don't have to check for pushing if the panel is shrinking
605 szdx := nw-mpw;
606 szdy := nh-mph;
607 if (szdx > 0) or (szdy > 0) then
608 begin
609 // ignore shrinking dimension
610 if (szdx < 0) then szdx := 0;
611 if (szdy < 0) then szdy := 0;
612 // move platform by szd* back, and check for szd* movement
613 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
614 begin
615 // yes, platform hits the entity, push the entity in the resizing direction
616 u0 := 1.0-u0; // how much path left?
617 szdx := trunc(szdx*u0);
618 szdy := trunc(szdy*u0);
619 if (szdx <> 0) or (szdy <> 0) then
620 begin
621 // has some path to go, trace the entity
622 trtag := (GridTagWall or GridTagDoor);
623 // if we're moving down, consider steps too
624 if (szdy > 0) then trtag := trtag or GridTagStep;
625 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
626 end;
627 end;
628 end;
629 // second, process platform movement, using te* as entity starting point
630 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
631 begin
632 //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]);
633 // yes, platform hits the entity, push the entity in the direction of the platform
634 u0 := 1.0-u0; // how much path left?
635 pdx := trunc(pdx*u0);
636 pdy := trunc(pdy*u0);
637 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
638 if (pdx <> 0) or (pdy <> 0) then
639 begin
640 // has some path to go, trace the entity
641 trtag := (GridTagWall or GridTagDoor);
642 // if we're moving down, consider steps too
643 if (pdy > 0) then trtag := trtag or GridTagStep;
644 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
645 end;
646 end;
647 end;
648 // done with entity movement, new coords are in te*
649 dx := tex-px;
650 dy := tey-py;
651 result := (dx <> 0) or (dy <> 0);
652 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
653 begin
654 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
655 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
656 end;
657 end;
659 function monCollect (mon: TMonster): Boolean;
660 begin
661 result := false; // don't stop
662 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
663 monCheckList[monCheckListUsed] := mon;
664 Inc(monCheckListUsed);
665 end;
667 var
668 cx0, cy0, cx1, cy1, cw, ch: Integer;
669 f: Integer;
670 px, py, pw, ph, pdx, pdy: Integer;
671 squash: Boolean;
672 plr: TPlayer;
673 gib: PGib;
674 cor: TCorpse;
675 mon: TMonster;
676 mpfrid: LongWord;
677 ontop: Boolean;
678 actMoveTrig: Boolean;
679 actSizeTrig: Boolean;
680 begin
681 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
683 if (FCurTexture >= 0) and
684 (FTextureIDs[FCurTexture].Anim) and
685 (FTextureIDs[FCurTexture].AnTex <> nil) and
686 (FAlpha < 255) then
687 begin
688 FTextureIDs[FCurTexture].AnTex.Update();
689 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
690 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
691 end;
693 if not g_dbgpan_mplat_active then exit;
695 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
696 mOldMovingActive := mMovingActive;
698 if not mMovingActive then exit;
699 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
701 //TODO: write wall size change processing
703 // moving platform?
704 begin
705 (*
706 * collect all monsters and players (aka entities) along the possible platform path
707 * if entity is standing on a platform:
708 * try to move it along the platform path, checking wall collisions
709 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
710 * try to push entity
711 * if we can't push entity all the way, squash it
712 *)
713 ox := X;
714 oy := Y;
715 mpw := Width;
716 mph := Height;
718 nw := mpw+mSizeSpeed.w;
719 nh := mph+mSizeSpeed.h;
720 nx := ox+mMovingSpeed.X;
721 ny := oy+mMovingSpeed.Y;
723 // force network updates only if some sudden change happened
724 // set the flag here, so we can sync affected monsters
725 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
726 begin
727 mNeedSend := true;
728 end
729 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
730 begin
731 mNeedSend := true;
732 end
733 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
734 begin
735 mNeedSend := true;
736 end;
738 // if pannel disappeared, we don't have to do anything
739 if (nw > 0) and (nh > 0) then
740 begin
741 // old rect
742 ex := ox+mpw-1;
743 ey := ox+mph-1;
744 // new rect
745 nex := nx+nw-1;
746 ney := ny+nh-1;
747 // full rect
748 cx0 := nmin(ox, nx);
749 cy0 := nmin(oy, ny);
750 cx1 := nmax(ex, nex);
751 cy1 := nmax(ey, ney);
752 // extrude
753 cx0 -= 1;
754 cy0 -= 1;
755 cx1 += 1;
756 cy1 += 1;
757 cw := cx1-cx0+1;
758 ch := cy1-cy0+1;
760 // process "obstacle" panels
761 if ((tag and GridTagObstacle) <> 0) then
762 begin
763 // temporarily turn off this panel, so it won't interfere with collision checks
764 mapGrid.proxyEnabled[proxyId] := false;
766 // process players
767 for f := 0 to High(gPlayers) do
768 begin
769 plr := gPlayers[f];
770 if (plr = nil) or (not plr.alive) then continue;
771 plr.getMapBox(px, py, pw, ph);
772 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
773 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
774 begin
775 // set new position
776 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
777 end;
778 // squash player, if necessary
779 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
780 end;
782 // process gibs
783 for f := 0 to High(gGibs) do
784 begin
785 gib := @gGibs[f];
786 if not gib.alive then continue;
787 gib.getMapBox(px, py, pw, ph);
788 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
789 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
790 begin
791 // set new position
792 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
793 end;
794 end;
796 // move and push corpses
797 for f := 0 to High(gCorpses) do
798 begin
799 cor := gCorpses[f];
800 if (cor = nil) then continue;
801 cor.getMapBox(px, py, pw, ph);
802 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
803 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
804 begin
805 // set new position
806 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
807 end;
808 end;
810 // collect monsters
811 monCheckListUsed := 0;
812 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
814 // process collected monsters
815 if (monCheckListUsed > 0) then
816 begin
817 mpfrid := g_Mons_getNewMPlatFrameId();
818 for f := 0 to monCheckListUsed do
819 begin
820 mon := monCheckList[f];
821 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
822 mon.mplatCheckFrameId := mpfrid;
823 mon.getMapBox(px, py, pw, ph);
824 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
825 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
826 begin
827 // set new position
828 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
829 //???FIXME: do we really need to send monsters over the net?
830 // i don't think so, as dead reckoning should take care of 'em
831 // ok, send new monster position only if platform is going to change it's direction
832 if mNeedSend then mon.setDirty();
833 end;
834 // squash monster, if necessary
835 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
836 end;
837 end;
839 // restore panel state
840 mapGrid.proxyEnabled[proxyId] := true;
841 end;
842 end;
844 // move panel
845 X := nx;
846 Y := ny;
847 FWidth := nw;
848 FHeight := nh;
849 positionChanged();
851 actMoveTrig := false;
852 actSizeTrig := false;
854 // `mNeedSend` was set above
856 // check "size stop"
857 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
858 begin
859 mSizeSpeed.w := 0;
860 mSizeSpeed.h := 0;
861 actSizeTrig := true;
862 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
863 end;
865 // reverse moving direction, if necessary
866 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
867 begin
868 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
869 actMoveTrig := true;
870 end;
872 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
873 begin
874 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
875 actMoveTrig := true;
876 end;
878 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
879 mOldMovingActive := mMovingActive;
881 if not g_Game_IsClient then
882 begin
883 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
884 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
885 end;
887 // some triggers may activate this, don't delay sending
888 //TODO: when triggers will be able to control speed and size, check that here too
889 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
890 mOldMovingActive := mMovingActive;
891 end;
892 end;
895 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
897 function ClampInt(X, A, B: Integer): Integer;
898 begin
899 Result := X;
900 if X < A then Result := A else if X > B then Result := B;
901 end;
903 begin
904 if Enabled and (FCurTexture >= 0) and
905 (FTextureIDs[FCurTexture].Anim) and
906 (FTextureIDs[FCurTexture].AnTex <> nil) and
907 (Width > 0) and (Height > 0) and (FAlpha < 255) then
908 begin
909 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
910 FCurFrameCount := Count;
911 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
912 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
913 end;
914 end;
916 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
917 begin
918 Assert(FCurTexture >= -1, 'FCurTexture < -1');
920 // Íåò òåêñòóð:
921 if Length(FTextureIDs) = 0 then
922 FCurTexture := -1
923 else
924 // Òîëüêî îäíà òåêñòóðà:
925 if Length(FTextureIDs) = 1 then
926 begin
927 if FCurTexture = 0 then
928 FCurTexture := -1
929 else
930 FCurTexture := 0;
931 end
932 else
933 // Áîëüøå îäíîé òåêñòóðû:
934 begin
935 // Ñëåäóþùàÿ:
936 Inc(FCurTexture);
937 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
938 if FCurTexture >= Length(FTextureIDs) then
939 FCurTexture := 0;
940 end;
942 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
943 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
944 begin
945 if (FTextureIDs[FCurTexture].AnTex = nil) then
946 begin
947 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
948 Exit;
949 end;
951 if AnimLoop = 1 then
952 FTextureIDs[FCurTexture].AnTex.Loop := True
953 else
954 if AnimLoop = 2 then
955 FTextureIDs[FCurTexture].AnTex.Loop := False;
957 FTextureIDs[FCurTexture].AnTex.Reset();
958 end;
960 LastAnimLoop := AnimLoop;
961 end;
963 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
964 begin
965 if (ID >= -1) and (ID < Length(FTextureIDs)) then
966 FCurTexture := ID;
968 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
969 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
970 begin
971 if (FTextureIDs[FCurTexture].AnTex = nil) then
972 begin
973 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
974 Exit;
975 end;
977 if AnimLoop = 1 then
978 FTextureIDs[FCurTexture].AnTex.Loop := True
979 else
980 if AnimLoop = 2 then
981 FTextureIDs[FCurTexture].AnTex.Loop := False;
983 FTextureIDs[FCurTexture].AnTex.Reset();
984 end;
986 LastAnimLoop := AnimLoop;
987 end;
989 function TPanel.GetTextureID(): DWORD;
990 begin
991 Result := LongWord(TEXTURE_NONE);
993 if (FCurTexture >= 0) then
994 begin
995 if FTextureIDs[FCurTexture].Anim then
996 Result := FTextureIDs[FCurTexture].AnTex.FramesID
997 else
998 Result := FTextureIDs[FCurTexture].Tex;
999 end;
1000 end;
1002 function TPanel.GetTextureCount(): Integer;
1003 begin
1004 Result := Length(FTextureIDs);
1005 if Enabled and (FCurTexture >= 0) then
1006 if (FTextureIDs[FCurTexture].Anim) and
1007 (FTextureIDs[FCurTexture].AnTex <> nil) and
1008 (Width > 0) and (Height > 0) and (FAlpha < 255) then
1009 Result := Result + 100;
1010 end;
1012 function TPanel.CanChangeTexture(): Boolean;
1013 begin
1014 Result := (GetTextureCount() > 1) or hasTexTrigger;
1015 end;
1017 const
1018 PAN_SAVE_VERSION = 1;
1020 procedure TPanel.SaveState (st: TStream);
1021 var
1022 anim: Boolean;
1023 begin
1024 if (st = nil) then exit;
1026 // Ñèãíàòóðà ïàíåëè
1027 utils.writeSign(st, 'PANL');
1028 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
1029 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1030 utils.writeBool(st, FEnabled);
1031 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1032 utils.writeInt(st, Byte(FLiftType));
1033 // Íîìåð òåêóùåé òåêñòóðû
1034 utils.writeInt(st, Integer(FCurTexture));
1035 // Êîîðäèíàòû è ðàçìåð
1036 utils.writeInt(st, Integer(FX));
1037 utils.writeInt(st, Integer(FY));
1038 utils.writeInt(st, Word(FWidth));
1039 utils.writeInt(st, Word(FHeight));
1040 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
1041 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
1042 begin
1043 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
1044 anim := true;
1045 end
1046 else
1047 begin
1048 anim := false;
1049 end;
1050 utils.writeBool(st, anim);
1051 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1052 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st);
1054 // moving platform state
1055 utils.writeInt(st, Integer(mMovingSpeed.X));
1056 utils.writeInt(st, Integer(mMovingSpeed.Y));
1057 utils.writeInt(st, Integer(mMovingStart.X));
1058 utils.writeInt(st, Integer(mMovingStart.Y));
1059 utils.writeInt(st, Integer(mMovingEnd.X));
1060 utils.writeInt(st, Integer(mMovingEnd.Y));
1062 utils.writeInt(st, Integer(mSizeSpeed.w));
1063 utils.writeInt(st, Integer(mSizeSpeed.h));
1064 utils.writeInt(st, Integer(mSizeEnd.w));
1065 utils.writeInt(st, Integer(mSizeEnd.h));
1067 utils.writeBool(st, mMovingActive);
1068 utils.writeBool(st, mMoveOnce);
1070 utils.writeInt(st, Integer(mEndPosTrig));
1071 utils.writeInt(st, Integer(mEndSizeTrig));
1072 end;
1075 procedure TPanel.LoadState (st: TStream);
1076 begin
1077 if (st = nil) then exit;
1079 // Ñèãíàòóðà ïàíåëè
1080 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1081 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1082 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1083 FEnabled := utils.readBool(st);
1084 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1085 FLiftType := utils.readByte(st);
1086 // Íîìåð òåêóùåé òåêñòóðû
1087 FCurTexture := utils.readLongInt(st);
1088 // Êîîðäèíàòû è ðàçìåð
1089 FX := utils.readLongInt(st);
1090 FY := utils.readLongInt(st);
1091 FWidth := utils.readWord(st);
1092 FHeight := utils.readWord(st);
1093 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1094 if utils.readBool(st) then
1095 begin
1096 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1097 Assert((FCurTexture >= 0) and
1098 (FTextureIDs[FCurTexture].Anim) and
1099 (FTextureIDs[FCurTexture].AnTex <> nil),
1100 'TPanel.LoadState: No animation object');
1101 FTextureIDs[FCurTexture].AnTex.LoadState(st);
1102 end;
1104 // moving platform state
1105 mMovingSpeed.X := utils.readLongInt(st);
1106 mMovingSpeed.Y := utils.readLongInt(st);
1107 mMovingStart.X := utils.readLongInt(st);
1108 mMovingStart.Y := utils.readLongInt(st);
1109 mMovingEnd.X := utils.readLongInt(st);
1110 mMovingEnd.Y := utils.readLongInt(st);
1112 mSizeSpeed.w := utils.readLongInt(st);
1113 mSizeSpeed.h := utils.readLongInt(st);
1114 mSizeEnd.w := utils.readLongInt(st);
1115 mSizeEnd.h := utils.readLongInt(st);
1117 mMovingActive := utils.readBool(st);
1118 mMoveOnce := utils.readBool(st);
1120 mEndPosTrig := utils.readLongInt(st);
1121 mEndSizeTrig := utils.readLongInt(st);
1123 positionChanged();
1124 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1125 end;
1128 end.