DEADSOFTWARE

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