DEADSOFTWARE

Refactor: Define constants for LiftType
[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 const
208 LIFTTYPE_UP = 0;
209 LIFTTYPE_DOWN = 1;
210 LIFTTYPE_LEFT = 2;
211 LIFTTYPE_RIGHT = 3;
213 var
214 g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
215 g_dbgpan_mplat_step: Boolean = false; // one step, and stop
218 implementation
220 uses
221 {$INCLUDE ../nogl/noGLuses.inc}
222 e_texture, g_basic, g_map, g_game, g_gfx, e_graphics, g_weapons, g_triggers,
223 g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
225 const
226 PANEL_SIGNATURE = $4C4E4150; // 'PANL'
228 { T P a n e l : }
230 constructor TPanel.Create(PanelRec: TDynRecord;
231 AddTextures: TAddTextureArray;
232 CurTex: Integer;
233 var Textures: TLevelTextureArray; aguid: Integer);
234 var
235 i: Integer;
236 tnum: Integer;
237 begin
238 X := PanelRec.X;
239 Y := PanelRec.Y;
240 Width := PanelRec.Width;
241 Height := PanelRec.Height;
242 FAlpha := 0;
243 FBlending := False;
244 FCurFrame := 0;
245 FCurFrameCount := 0;
246 LastAnimLoop := 0;
248 mapId := PanelRec.id;
249 mGUID := aguid;
251 mMovingSpeed := PanelRec.moveSpeed;
252 mMovingStart := PanelRec.moveStart;
253 mMovingEnd := PanelRec.moveEnd;
254 mMovingActive := PanelRec['move_active'].value;
255 mOldMovingActive := mMovingActive;
256 mMoveOnce := PanelRec.moveOnce;
258 mSizeSpeed := PanelRec.sizeSpeed;
259 mSizeEnd := PanelRec.sizeEnd;
261 mEndPosTrig := PanelRec.endPosTrig;
262 mEndSizeTrig := PanelRec.endSizeTrig;
264 mNeedSend := false;
266 // Òèï ïàíåëè:
267 PanelType := PanelRec.PanelType;
268 Enabled := True;
269 Door := False;
270 LiftType := LIFTTYPE_UP;
271 hasTexTrigger := False;
273 case PanelType of
274 PANEL_OPENDOOR: begin Enabled := False; Door := True; end;
275 PANEL_CLOSEDOOR: Door := True;
276 PANEL_LIFTUP: LiftType := LIFTTYPE_UP; //???
277 PANEL_LIFTDOWN: LiftType := LIFTTYPE_DOWN;
278 PANEL_LIFTLEFT: LiftType := LIFTTYPE_LEFT;
279 PANEL_LIFTRIGHT: LiftType := LIFTTYPE_RIGHT;
280 end;
282 // Íåâèäèìàÿ:
283 if ByteBool(PanelRec.Flags and PANEL_FLAG_HIDE) then
284 begin
285 SetLength(FTextureIDs, 0);
286 FCurTexture := -1;
287 Exit;
288 end;
289 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
290 if ByteBool(PanelType and
291 (PANEL_LIFTUP or
292 PANEL_LIFTDOWN or
293 PANEL_LIFTLEFT or
294 PANEL_LIFTRIGHT or
295 PANEL_BLOCKMON)) then
296 begin
297 SetLength(FTextureIDs, 0);
298 FCurTexture := -1;
299 Exit;
300 end;
302 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
303 if WordBool(PanelType and (PANEL_WATER or PANEL_ACID1 or PANEL_ACID2)) and
304 (not ByteBool(PanelRec.Flags and PANEL_FLAG_WATERTEXTURES)) then
305 begin
306 SetLength(FTextureIDs, 1);
307 FTextureIDs[0].Anim := False;
309 case PanelRec.PanelType of
310 PANEL_WATER:
311 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_WATER);
312 PANEL_ACID1:
313 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID1);
314 PANEL_ACID2:
315 FTextureIDs[0].Tex := LongWord(TEXTURE_SPECIAL_ACID2);
316 end;
318 FCurTexture := 0;
319 Exit;
320 end;
322 SetLength(FTextureIDs, Length(AddTextures));
324 if CurTex < 0 then
325 FCurTexture := -1
326 else
327 if CurTex >= Length(FTextureIDs) then
328 FCurTexture := Length(FTextureIDs) - 1
329 else
330 FCurTexture := CurTex;
332 for i := 0 to Length(FTextureIDs)-1 do
333 begin
334 FTextureIDs[i].Anim := AddTextures[i].Anim;
335 if FTextureIDs[i].Anim then
336 begin // Àíèìèðîâàííàÿ òåêñòóðà
337 FTextureIDs[i].AnTex :=
338 TAnimation.Create(Textures[AddTextures[i].Texture].FramesID,
339 True, Textures[AddTextures[i].Texture].Speed);
340 FTextureIDs[i].AnTex.Blending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
341 FTextureIDs[i].AnTex.Alpha := PanelRec.Alpha;
342 end
343 else
344 begin // Îáû÷íàÿ òåêñòóðà
345 FTextureIDs[i].Tex := Textures[AddTextures[i].Texture].TextureID;
346 end;
347 end;
349 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
350 //if Length(FTextureIDs) > 1 then SaveIt := True;
352 if (PanelRec.TextureRec = nil) then tnum := -1 else tnum := PanelRec.tagInt;
353 if (tnum < 0) then tnum := Length(Textures);
355 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
356 if ({PanelRec.TextureNum}tnum > High(Textures)) then
357 begin
358 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum, High(Textures)]), TMsgType.Warning);
359 FTextureWidth := 2;
360 FTextureHeight := 2;
361 FAlpha := 0;
362 FBlending := ByteBool(0);
363 end
364 else if not g_Map_IsSpecialTexture(Textures[{PanelRec.TextureNum}tnum].TextureName) then
365 begin
366 FTextureWidth := Textures[{PanelRec.TextureNum}tnum].Width;
367 FTextureHeight := Textures[{PanelRec.TextureNum}tnum].Height;
368 FAlpha := PanelRec.Alpha;
369 FBlending := ByteBool(PanelRec.Flags and PANEL_FLAG_BLENDING);
370 end;
371 end;
373 destructor TPanel.Destroy();
374 var
375 i: Integer;
376 begin
377 for i := 0 to High(FTextureIDs) do
378 if FTextureIDs[i].Anim then
379 FTextureIDs[i].AnTex.Free();
380 SetLength(FTextureIDs, 0);
382 Inherited;
383 end;
385 function TPanel.getx1 (): Integer; inline; begin result := X+Width-1; end;
386 function TPanel.gety1 (): Integer; inline; begin result := Y+Height-1; end;
387 function TPanel.getvisvalid (): Boolean; inline; begin result := (Width > 0) and (Height > 0); end;
389 function TPanel.getMovingSpeedX (): Integer; inline; begin result := mMovingSpeed.X; end;
390 procedure TPanel.setMovingSpeedX (v: Integer); inline; begin mMovingSpeed.X := v; end;
391 function TPanel.getMovingSpeedY (): Integer; inline; begin result := mMovingSpeed.Y; end;
392 procedure TPanel.setMovingSpeedY (v: Integer); inline; begin mMovingSpeed.Y := v; end;
394 function TPanel.getMovingStartX (): Integer; inline; begin result := mMovingStart.X; end;
395 procedure TPanel.setMovingStartX (v: Integer); inline; begin mMovingStart.X := v; end;
396 function TPanel.getMovingStartY (): Integer; inline; begin result := mMovingStart.Y; end;
397 procedure TPanel.setMovingStartY (v: Integer); inline; begin mMovingStart.Y := v; end;
399 function TPanel.getMovingEndX (): Integer; inline; begin result := mMovingEnd.X; end;
400 procedure TPanel.setMovingEndX (v: Integer); inline; begin mMovingEnd.X := v; end;
401 function TPanel.getMovingEndY (): Integer; inline; begin result := mMovingEnd.Y; end;
402 procedure TPanel.setMovingEndY (v: Integer); inline; begin mMovingEnd.Y := v; end;
404 function TPanel.getSizeSpeedX (): Integer; inline; begin result := mSizeSpeed.w; end;
405 procedure TPanel.setSizeSpeedX (v: Integer); inline; begin mSizeSpeed.w := v; end;
406 function TPanel.getSizeSpeedY (): Integer; inline; begin result := mSizeSpeed.h; end;
407 procedure TPanel.setSizeSpeedY (v: Integer); inline; begin mSizeSpeed.h := v; end;
409 function TPanel.getSizeEndX (): Integer; inline; begin result := mSizeEnd.w; end;
410 procedure TPanel.setSizeEndX (v: Integer); inline; begin mSizeEnd.w := v; end;
411 function TPanel.getSizeEndY (): Integer; inline; begin result := mSizeEnd.h; end;
412 procedure TPanel.setSizeEndY (v: Integer); inline; begin mSizeEnd.h := v; end;
414 function TPanel.getIsGBack (): Boolean; inline; begin result := ((tag and GridTagBack) <> 0); end;
415 function TPanel.getIsGStep (): Boolean; inline; begin result := ((tag and GridTagStep) <> 0); end;
416 function TPanel.getIsGWall (): Boolean; inline; begin result := ((tag and (GridTagWall or GridTagDoor)) <> 0); end;
417 function TPanel.getIsGAcid1 (): Boolean; inline; begin result := ((tag and GridTagAcid1) <> 0); end;
418 function TPanel.getIsGAcid2 (): Boolean; inline; begin result := ((tag and GridTagAcid2) <> 0); end;
419 function TPanel.getIsGWater (): Boolean; inline; begin result := ((tag and GridTagWater) <> 0); end;
420 function TPanel.getIsGFore (): Boolean; inline; begin result := ((tag and GridTagFore) <> 0); end;
421 function TPanel.getIsGLift (): Boolean; inline; begin result := ((tag and GridTagLift) <> 0); end;
422 function TPanel.getIsGBlockMon (): Boolean; inline; begin result := ((tag and GridTagBlockMon) <> 0); end;
424 function TPanel.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end;
425 procedure TPanel.setDirty (); inline; begin mNeedSend := true; end;
428 procedure TPanel.Draw (hasAmbient: Boolean; constref ambColor: TDFColor);
429 var
430 xx, yy: Integer;
431 NoTextureID: DWORD;
432 NW, NH: Word;
433 begin
434 if {Enabled and} (FCurTexture >= 0) and
435 (Width > 0) and (Height > 0) and (FAlpha < 255) {and
436 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
437 begin
438 if FTextureIDs[FCurTexture].Anim then
439 begin // Àíèìèðîâàííàÿ òåêñòóðà
440 if FTextureIDs[FCurTexture].AnTex = nil then
441 Exit;
443 for xx := 0 to (Width div FTextureWidth)-1 do
444 for yy := 0 to (Height div FTextureHeight)-1 do
445 FTextureIDs[FCurTexture].AnTex.Draw(
446 X + xx*FTextureWidth,
447 Y + yy*FTextureHeight, TMirrorType.None);
448 end
449 else
450 begin // Îáû÷íàÿ òåêñòóðà
451 case FTextureIDs[FCurTexture].Tex of
452 LongWord(TEXTURE_SPECIAL_WATER): e_DrawFillQuad(X, Y, X+Width-1, Y+Height-1, 0, 0, 255, 0, TBlending.Filter);
453 LongWord(TEXTURE_SPECIAL_ACID1): e_DrawFillQuad(X, Y, X+Width-1, Y+Height-1, 0, 128, 0, 0, TBlending.Filter);
454 LongWord(TEXTURE_SPECIAL_ACID2): e_DrawFillQuad(X, Y, X+Width-1, Y+Height-1, 128, 0, 0, 0, TBlending.Filter);
455 LongWord(TEXTURE_NONE):
456 if g_Texture_Get('NOTEXTURE', NoTextureID) then
457 begin
458 e_GetTextureSize(NoTextureID, @NW, @NH);
459 e_DrawFill(NoTextureID, X, Y, Width div NW, Height div NH, 0, False, False);
460 end
461 else
462 begin
463 xx := X + (Width div 2);
464 yy := Y + (Height div 2);
465 e_DrawFillQuad(X, Y, xx, yy, 255, 0, 255, 0);
466 e_DrawFillQuad(xx, Y, X+Width-1, yy, 255, 255, 0, 0);
467 e_DrawFillQuad(X, yy, xx, Y+Height-1, 255, 255, 0, 0);
468 e_DrawFillQuad(xx, yy, X+Width-1, Y+Height-1, 255, 0, 255, 0);
469 end;
470 else
471 begin
472 if not mMovingActive then
473 e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending, hasAmbient)
474 else
475 e_DrawFillX(FTextureIDs[FCurTexture].Tex, X, Y, Width, Height, FAlpha, True, FBlending, g_dbg_scale, hasAmbient);
476 if hasAmbient then e_AmbientQuad(X, Y, Width, Height, ambColor.r, ambColor.g, ambColor.b, ambColor.a);
477 end;
478 end;
479 end;
480 end;
481 end;
483 procedure TPanel.DrawShadowVolume(lightX: Integer; lightY: Integer; radius: Integer);
484 procedure extrude (x: Integer; y: Integer);
485 begin
486 glVertex2i(x+(x-lightX)*500, y+(y-lightY)*500);
487 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
488 end;
490 procedure drawLine (x0: Integer; y0: Integer; x1: Integer; y1: Integer);
491 begin
492 // does this side facing the light?
493 if ((x1-x0)*(lightY-y0)-(lightX-x0)*(y1-y0) >= 0) then exit;
494 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
495 // this edge is facing the light, extrude and draw it
496 glVertex2i(x0, y0);
497 glVertex2i(x1, y1);
498 extrude(x1, y1);
499 extrude(x0, y0);
500 end;
502 begin
503 if radius < 4 then exit;
504 if Enabled and (FCurTexture >= 0) and (Width > 0) and (Height > 0) and (FAlpha < 255) {and
505 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
506 begin
507 if not FTextureIDs[FCurTexture].Anim then
508 begin
509 case FTextureIDs[FCurTexture].Tex of
510 LongWord(TEXTURE_SPECIAL_WATER): exit;
511 LongWord(TEXTURE_SPECIAL_ACID1): exit;
512 LongWord(TEXTURE_SPECIAL_ACID2): exit;
513 LongWord(TEXTURE_NONE): exit;
514 end;
515 end;
516 if (X+Width < lightX-radius) then exit;
517 if (Y+Height < lightY-radius) then exit;
518 if (X > lightX+radius) then exit;
519 if (Y > lightY+radius) then exit;
520 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
522 glBegin(GL_QUADS);
523 drawLine(x, y, x+width, y); // top
524 drawLine(x+width, y, x+width, y+height); // right
525 drawLine(x+width, y+height, x, y+height); // bottom
526 drawLine(x, y+height, x, y); // left
527 glEnd();
528 end;
529 end;
532 procedure TPanel.positionChanged (); inline;
533 var
534 px, py, pw, ph: Integer;
535 begin
536 if (proxyId >= 0) then
537 begin
538 mapGrid.getBodyDims(proxyId, px, py, pw, ph);
539 if (px <> x) or (py <> y) or (pw <> Width) or (ph <> Height) then
540 begin
542 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
543 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
545 g_Mark(px, py, pw, ph, MARK_WALL, false);
546 if (Width < 1) or (Height < 1) then
547 begin
548 mapGrid.proxyEnabled[proxyId] := false;
549 end
550 else
551 begin
552 mapGrid.proxyEnabled[proxyId] := Enabled;
553 if (pw <> Width) or (ph <> Height) then
554 begin
555 //writeln('panel resize!');
556 mapGrid.moveResizeBody(proxyId, X, Y, Width, Height)
557 end
558 else
559 begin
560 mapGrid.moveBody(proxyId, X, Y);
561 end;
562 g_Mark(X, Y, Width, Height, MARK_WALL);
563 end;
564 end;
565 end;
566 end;
569 var
570 monCheckList: array of TMonster = nil;
571 monCheckListUsed: Integer = 0;
573 procedure TPanel.Update();
574 var
575 ox, oy: Integer;
576 nx, ny, nw, nh: Integer;
577 ex, ey, nex, ney: Integer;
578 mpw, mph: Integer;
580 // return `true` if we should move by dx,dy
581 function tryMPlatMove (px, py, pw, ph: Integer; out dx, dy: Integer; out squash: Boolean; ontop: PBoolean=nil): Boolean;
582 var
583 u0: Single;
584 tex, tey: Integer;
585 pdx, pdy: Integer;
586 trtag: Integer;
587 szdx, szdy: Integer;
588 begin
589 squash := false;
590 tex := px;
591 tey := py;
592 pdx := mMovingSpeed.X;
593 pdy := mMovingSpeed.Y;
594 // standing on the platform?
595 if (py+ph = oy) then
596 begin
597 if (ontop <> nil) then ontop^ := true;
598 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
599 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, (GridTagWall or GridTagDoor));
600 end
601 else
602 begin
603 if (ontop <> nil) then ontop^ := false;
604 // not standing on the platform: trace platform to see if it hits the entity
605 // first, process size change (as we cannot sweeptest both move and size change)
606 // but we don't have to check for pushing if the panel is shrinking
607 szdx := nw-mpw;
608 szdy := nh-mph;
609 if (szdx > 0) or (szdy > 0) then
610 begin
611 // ignore shrinking dimension
612 if (szdx < 0) then szdx := 0;
613 if (szdy < 0) then szdy := 0;
614 // move platform by szd* back, and check for szd* movement
615 if sweepAABB(ox-szdx, oy-szdy, nw, nh, szdx, szdy, px, py, pw, ph, @u0) then
616 begin
617 // yes, platform hits the entity, push the entity in the resizing direction
618 u0 := 1.0-u0; // how much path left?
619 szdx := trunc(szdx*u0);
620 szdy := trunc(szdy*u0);
621 if (szdx <> 0) or (szdy <> 0) then
622 begin
623 // has some path to go, trace the entity
624 trtag := (GridTagWall or GridTagDoor);
625 // if we're moving down, consider steps too
626 if (szdy > 0) then trtag := trtag or GridTagStep;
627 mapGrid.traceBox(tex, tey, px, py, pw, ph, szdx, szdy, trtag);
628 end;
629 end;
630 end;
631 // second, process platform movement, using te* as entity starting point
632 if sweepAABB(ox, oy, nw, nh, pdx, pdy, tex, tey, pw, ph, @u0) then
633 begin
634 //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]);
635 // yes, platform hits the entity, push the entity in the direction of the platform
636 u0 := 1.0-u0; // how much path left?
637 pdx := trunc(pdx*u0);
638 pdy := trunc(pdy*u0);
639 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
640 if (pdx <> 0) or (pdy <> 0) then
641 begin
642 // has some path to go, trace the entity
643 trtag := (GridTagWall or GridTagDoor);
644 // if we're moving down, consider steps too
645 if (pdy > 0) then trtag := trtag or GridTagStep;
646 mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, trtag);
647 end;
648 end;
649 end;
650 // done with entity movement, new coords are in te*
651 dx := tex-px;
652 dy := tey-py;
653 result := (dx <> 0) or (dy <> 0);
654 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
655 begin
656 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
657 squash := g_Collide(tex, tey, pw, ph, nx, ny, nw, nh); // squash, if still in platform
658 end;
659 end;
661 function monCollect (mon: TMonster): Boolean;
662 begin
663 result := false; // don't stop
664 if (monCheckListUsed >= Length(monCheckList)) then SetLength(monCheckList, monCheckListUsed+128);
665 monCheckList[monCheckListUsed] := mon;
666 Inc(monCheckListUsed);
667 end;
669 var
670 cx0, cy0, cx1, cy1, cw, ch: Integer;
671 f: Integer;
672 px, py, pw, ph, pdx, pdy: Integer;
673 squash: Boolean;
674 plr: TPlayer;
675 gib: PGib;
676 cor: TCorpse;
677 mon: TMonster;
678 mpfrid: LongWord;
679 ontop: Boolean;
680 actMoveTrig: Boolean;
681 actSizeTrig: Boolean;
682 begin
683 if (not Enabled) or (Width < 1) or (Height < 1) then exit;
685 if (FCurTexture >= 0) and
686 (FTextureIDs[FCurTexture].Anim) and
687 (FTextureIDs[FCurTexture].AnTex <> nil) and
688 (FAlpha < 255) then
689 begin
690 FTextureIDs[FCurTexture].AnTex.Update();
691 FCurFrame := FTextureIDs[FCurTexture].AnTex.CurrentFrame;
692 FCurFrameCount := FTextureIDs[FCurTexture].AnTex.CurrentCounter;
693 end;
695 if not g_dbgpan_mplat_active then exit;
697 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
698 mOldMovingActive := mMovingActive;
700 if not mMovingActive then exit;
701 if mMovingSpeed.isZero and mSizeSpeed.isZero then exit;
703 //TODO: write wall size change processing
705 // moving platform?
706 begin
707 (*
708 * collect all monsters and players (aka entities) along the possible platform path
709 * if entity is standing on a platform:
710 * try to move it along the platform path, checking wall collisions
711 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
712 * try to push entity
713 * if we can't push entity all the way, squash it
714 *)
715 ox := X;
716 oy := Y;
717 mpw := Width;
718 mph := Height;
720 nw := mpw+mSizeSpeed.w;
721 nh := mph+mSizeSpeed.h;
722 nx := ox+mMovingSpeed.X;
723 ny := oy+mMovingSpeed.Y;
725 // force network updates only if some sudden change happened
726 // set the flag here, so we can sync affected monsters
727 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
728 begin
729 mNeedSend := true;
730 end
731 else if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
732 begin
733 mNeedSend := true;
734 end
735 else if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
736 begin
737 mNeedSend := true;
738 end;
740 // if pannel disappeared, we don't have to do anything
741 if (nw > 0) and (nh > 0) then
742 begin
743 // old rect
744 ex := ox+mpw-1;
745 ey := ox+mph-1;
746 // new rect
747 nex := nx+nw-1;
748 ney := ny+nh-1;
749 // full rect
750 cx0 := nmin(ox, nx);
751 cy0 := nmin(oy, ny);
752 cx1 := nmax(ex, nex);
753 cy1 := nmax(ey, ney);
754 // extrude
755 cx0 -= 1;
756 cy0 -= 1;
757 cx1 += 1;
758 cy1 += 1;
759 cw := cx1-cx0+1;
760 ch := cy1-cy0+1;
762 // process "obstacle" panels
763 if ((tag and GridTagObstacle) <> 0) then
764 begin
765 // temporarily turn off this panel, so it won't interfere with collision checks
766 mapGrid.proxyEnabled[proxyId] := false;
768 // process players
769 for f := 0 to High(gPlayers) do
770 begin
771 plr := gPlayers[f];
772 if (plr = nil) or (not plr.alive) then continue;
773 plr.getMapBox(px, py, pw, ph);
774 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
775 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
776 begin
777 // set new position
778 plr.moveBy(pdx, pdy); // this will call `positionChanged()` for us
779 end;
780 // squash player, if necessary
781 if not g_Game_IsClient and squash then plr.Damage(15000, 0, 0, 0, HIT_TRAP);
782 end;
784 // process gibs
785 for f := 0 to High(gGibs) do
786 begin
787 gib := @gGibs[f];
788 if not gib.alive then continue;
789 gib.getMapBox(px, py, pw, ph);
790 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
791 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
792 begin
793 // set new position
794 gib.moveBy(pdx, pdy); // this will call `positionChanged()` for us
795 end;
796 end;
798 // move and push corpses
799 for f := 0 to High(gCorpses) do
800 begin
801 cor := gCorpses[f];
802 if (cor = nil) then continue;
803 cor.getMapBox(px, py, pw, ph);
804 if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
805 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
806 begin
807 // set new position
808 cor.moveBy(pdx, pdy); // this will call `positionChanged()` for us
809 end;
810 end;
812 // collect monsters
813 monCheckListUsed := 0;
814 g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
816 // process collected monsters
817 if (monCheckListUsed > 0) then
818 begin
819 mpfrid := g_Mons_getNewMPlatFrameId();
820 for f := 0 to monCheckListUsed do
821 begin
822 mon := monCheckList[f];
823 if (mon = nil) or (not mon.alive) or (mon.mplatCheckFrameId = mpfrid) then continue;
824 mon.mplatCheckFrameId := mpfrid;
825 mon.getMapBox(px, py, pw, ph);
826 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
827 if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash) then
828 begin
829 // set new position
830 mon.moveBy(pdx, pdy); // this will call `positionChanged()` for us
831 //???FIXME: do we really need to send monsters over the net?
832 // i don't think so, as dead reckoning should take care of 'em
833 // ok, send new monster position only if platform is going to change it's direction
834 if mNeedSend then mon.setDirty();
835 end;
836 // squash monster, if necessary
837 if not g_Game_IsClient and squash then mon.Damage(15000, 0, 0, 0, HIT_TRAP);
838 end;
839 end;
841 // restore panel state
842 mapGrid.proxyEnabled[proxyId] := true;
843 end;
844 end;
846 // move panel
847 X := nx;
848 Y := ny;
849 FWidth := nw;
850 FHeight := nh;
851 positionChanged();
853 actMoveTrig := false;
854 actSizeTrig := false;
856 // `mNeedSend` was set above
858 // check "size stop"
859 if not mSizeSpeed.isZero and (nw = mSizeEnd.w) and (nh = mSizeEnd.h) then
860 begin
861 mSizeSpeed.w := 0;
862 mSizeSpeed.h := 0;
863 actSizeTrig := true;
864 if (nw < 1) or (nh < 1) then mMovingActive := false; //HACK!
865 end;
867 // reverse moving direction, if necessary
868 if ((mMovingSpeed.X < 0) and (nx <= mMovingStart.X)) or ((mMovingSpeed.X > 0) and (nx >= mMovingEnd.X)) then
869 begin
870 if mMoveOnce then mMovingActive := false else mMovingSpeed.X := -mMovingSpeed.X;
871 actMoveTrig := true;
872 end;
874 if ((mMovingSpeed.Y < 0) and (ny <= mMovingStart.Y)) or ((mMovingSpeed.Y > 0) and (ny >= mMovingEnd.Y)) then
875 begin
876 if mMoveOnce then mMovingActive := false else mMovingSpeed.Y := -mMovingSpeed.Y;
877 actMoveTrig := true;
878 end;
880 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
881 mOldMovingActive := mMovingActive;
883 if not g_Game_IsClient then
884 begin
885 if actMoveTrig then g_Triggers_Press(mEndPosTrig, ACTIVATE_CUSTOM);
886 if actSizeTrig then g_Triggers_Press(mEndSizeTrig, ACTIVATE_CUSTOM);
887 end;
889 // some triggers may activate this, don't delay sending
890 //TODO: when triggers will be able to control speed and size, check that here too
891 if (mOldMovingActive <> mMovingActive) then mNeedSend := true;
892 mOldMovingActive := mMovingActive;
893 end;
894 end;
897 procedure TPanel.SetFrame(Frame: Integer; Count: Byte);
899 function ClampInt(X, A, B: Integer): Integer;
900 begin
901 Result := X;
902 if X < A then Result := A else if X > B then Result := B;
903 end;
905 begin
906 if Enabled and (FCurTexture >= 0) and
907 (FTextureIDs[FCurTexture].Anim) and
908 (FTextureIDs[FCurTexture].AnTex <> nil) and
909 (Width > 0) and (Height > 0) and (FAlpha < 255) then
910 begin
911 FCurFrame := ClampInt(Frame, 0, FTextureIDs[FCurTexture].AnTex.TotalFrames - 1);
912 FCurFrameCount := Count;
913 FTextureIDs[FCurTexture].AnTex.CurrentFrame := FCurFrame;
914 FTextureIDs[FCurTexture].AnTex.CurrentCounter := FCurFrameCount;
915 end;
916 end;
918 procedure TPanel.NextTexture(AnimLoop: Byte = 0);
919 begin
920 Assert(FCurTexture >= -1, 'FCurTexture < -1');
922 // Íåò òåêñòóð:
923 if Length(FTextureIDs) = 0 then
924 FCurTexture := -1
925 else
926 // Òîëüêî îäíà òåêñòóðà:
927 if Length(FTextureIDs) = 1 then
928 begin
929 if FCurTexture = 0 then
930 FCurTexture := -1
931 else
932 FCurTexture := 0;
933 end
934 else
935 // Áîëüøå îäíîé òåêñòóðû:
936 begin
937 // Ñëåäóþùàÿ:
938 Inc(FCurTexture);
939 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
940 if FCurTexture >= Length(FTextureIDs) then
941 FCurTexture := 0;
942 end;
944 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
945 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
946 begin
947 if (FTextureIDs[FCurTexture].AnTex = nil) then
948 begin
949 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
950 Exit;
951 end;
953 if AnimLoop = 1 then
954 FTextureIDs[FCurTexture].AnTex.Loop := True
955 else
956 if AnimLoop = 2 then
957 FTextureIDs[FCurTexture].AnTex.Loop := False;
959 FTextureIDs[FCurTexture].AnTex.Reset();
960 end;
962 LastAnimLoop := AnimLoop;
963 end;
965 procedure TPanel.SetTexture(ID: Integer; AnimLoop: Byte = 0);
966 begin
967 if (ID >= -1) and (ID < Length(FTextureIDs)) then
968 FCurTexture := ID;
970 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
971 if (FCurTexture >= 0) and FTextureIDs[FCurTexture].Anim then
972 begin
973 if (FTextureIDs[FCurTexture].AnTex = nil) then
974 begin
975 g_FatalError(_lc[I_GAME_ERROR_SWITCH_TEXTURE]);
976 Exit;
977 end;
979 if AnimLoop = 1 then
980 FTextureIDs[FCurTexture].AnTex.Loop := True
981 else
982 if AnimLoop = 2 then
983 FTextureIDs[FCurTexture].AnTex.Loop := False;
985 FTextureIDs[FCurTexture].AnTex.Reset();
986 end;
988 LastAnimLoop := AnimLoop;
989 end;
991 function TPanel.GetTextureID(): DWORD;
992 begin
993 Result := LongWord(TEXTURE_NONE);
995 if (FCurTexture >= 0) then
996 begin
997 if FTextureIDs[FCurTexture].Anim then
998 Result := FTextureIDs[FCurTexture].AnTex.FramesID
999 else
1000 Result := FTextureIDs[FCurTexture].Tex;
1001 end;
1002 end;
1004 function TPanel.GetTextureCount(): Integer;
1005 begin
1006 Result := Length(FTextureIDs);
1007 if Enabled and (FCurTexture >= 0) then
1008 if (FTextureIDs[FCurTexture].Anim) and
1009 (FTextureIDs[FCurTexture].AnTex <> nil) and
1010 (Width > 0) and (Height > 0) and (FAlpha < 255) then
1011 Result := Result + 100;
1012 end;
1014 function TPanel.CanChangeTexture(): Boolean;
1015 begin
1016 Result := (GetTextureCount() > 1) or hasTexTrigger;
1017 end;
1019 const
1020 PAN_SAVE_VERSION = 1;
1022 procedure TPanel.SaveState (st: TStream);
1023 var
1024 anim: Boolean;
1025 begin
1026 if (st = nil) then exit;
1028 // Ñèãíàòóðà ïàíåëè
1029 utils.writeSign(st, 'PANL');
1030 utils.writeInt(st, Byte(PAN_SAVE_VERSION));
1031 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1032 utils.writeBool(st, FEnabled);
1033 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1034 utils.writeInt(st, Byte(FLiftType));
1035 // Íîìåð òåêóùåé òåêñòóðû
1036 utils.writeInt(st, Integer(FCurTexture));
1037 // Êîîðäèíàòû è ðàçìåð
1038 utils.writeInt(st, Integer(FX));
1039 utils.writeInt(st, Integer(FY));
1040 utils.writeInt(st, Word(FWidth));
1041 utils.writeInt(st, Word(FHeight));
1042 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
1043 if (FCurTexture >= 0) and (FTextureIDs[FCurTexture].Anim) then
1044 begin
1045 assert(FTextureIDs[FCurTexture].AnTex <> nil, 'TPanel.SaveState: No animation object');
1046 anim := true;
1047 end
1048 else
1049 begin
1050 anim := false;
1051 end;
1052 utils.writeBool(st, anim);
1053 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1054 if anim then FTextureIDs[FCurTexture].AnTex.SaveState(st);
1056 // moving platform state
1057 utils.writeInt(st, Integer(mMovingSpeed.X));
1058 utils.writeInt(st, Integer(mMovingSpeed.Y));
1059 utils.writeInt(st, Integer(mMovingStart.X));
1060 utils.writeInt(st, Integer(mMovingStart.Y));
1061 utils.writeInt(st, Integer(mMovingEnd.X));
1062 utils.writeInt(st, Integer(mMovingEnd.Y));
1064 utils.writeInt(st, Integer(mSizeSpeed.w));
1065 utils.writeInt(st, Integer(mSizeSpeed.h));
1066 utils.writeInt(st, Integer(mSizeEnd.w));
1067 utils.writeInt(st, Integer(mSizeEnd.h));
1069 utils.writeBool(st, mMovingActive);
1070 utils.writeBool(st, mMoveOnce);
1072 utils.writeInt(st, Integer(mEndPosTrig));
1073 utils.writeInt(st, Integer(mEndSizeTrig));
1074 end;
1077 procedure TPanel.LoadState (st: TStream);
1078 begin
1079 if (st = nil) then exit;
1081 // Ñèãíàòóðà ïàíåëè
1082 if not utils.checkSign(st, 'PANL') then raise XStreamError.create('wrong panel signature');
1083 if (utils.readByte(st) <> PAN_SAVE_VERSION) then raise XStreamError.create('wrong panel version');
1084 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1085 FEnabled := utils.readBool(st);
1086 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1087 FLiftType := utils.readByte(st);
1088 // Íîìåð òåêóùåé òåêñòóðû
1089 FCurTexture := utils.readLongInt(st);
1090 // Êîîðäèíàòû è ðàçìåð
1091 FX := utils.readLongInt(st);
1092 FY := utils.readLongInt(st);
1093 FWidth := utils.readWord(st);
1094 FHeight := utils.readWord(st);
1095 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1096 if utils.readBool(st) then
1097 begin
1098 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1099 Assert((FCurTexture >= 0) and
1100 (FTextureIDs[FCurTexture].Anim) and
1101 (FTextureIDs[FCurTexture].AnTex <> nil),
1102 'TPanel.LoadState: No animation object');
1103 FTextureIDs[FCurTexture].AnTex.LoadState(st);
1104 end;
1106 // moving platform state
1107 mMovingSpeed.X := utils.readLongInt(st);
1108 mMovingSpeed.Y := utils.readLongInt(st);
1109 mMovingStart.X := utils.readLongInt(st);
1110 mMovingStart.Y := utils.readLongInt(st);
1111 mMovingEnd.X := utils.readLongInt(st);
1112 mMovingEnd.Y := utils.readLongInt(st);
1114 mSizeSpeed.w := utils.readLongInt(st);
1115 mSizeSpeed.h := utils.readLongInt(st);
1116 mSizeEnd.w := utils.readLongInt(st);
1117 mSizeEnd.h := utils.readLongInt(st);
1119 mMovingActive := utils.readBool(st);
1120 mMoveOnce := utils.readBool(st);
1122 mEndPosTrig := utils.readLongInt(st);
1123 mEndSizeTrig := utils.readLongInt(st);
1125 positionChanged();
1126 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas
1127 end;
1130 end.