1 (* Copyright (C) DooM 2D:Forever Developers
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.
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.
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/>.
16 {$INCLUDE ../shared/a_modes.inc}
23 MAPDEF
, BinEditor
, g_textures
, xdynrec
;
26 TAddTextureArray
= Array of
32 TPanel
= Class (TObject
)
36 mGUID
: Integer; // will be assigned in "g_map.pas"
44 False: (Tex
: Cardinal);
45 True: (AnTex
: TAnimation
);
48 mMovingSpeed
: TDFPoint
;
49 mMovingStart
: TDFPoint
;
51 mMovingActive
: Boolean;
54 mOldMovingActive
: Boolean;
60 mEndSizeTrig
: Integer;
62 mNeedSend
: Boolean; // for network
65 function getx1 (): Integer; inline;
66 function gety1 (): Integer; inline;
67 function getvisvalid (): Boolean; inline;
69 function getMovingSpeedX (): Integer; inline;
70 procedure setMovingSpeedX (v
: Integer); inline;
71 function getMovingSpeedY (): Integer; inline;
72 procedure setMovingSpeedY (v
: Integer); inline;
74 function getMovingStartX (): Integer; inline;
75 procedure setMovingStartX (v
: Integer); inline;
76 function getMovingStartY (): Integer; inline;
77 procedure setMovingStartY (v
: Integer); inline;
79 function getMovingEndX (): Integer; inline;
80 procedure setMovingEndX (v
: Integer); inline;
81 function getMovingEndY (): Integer; inline;
82 procedure setMovingEndY (v
: Integer); inline;
84 function getSizeSpeedX (): Integer; inline;
85 procedure setSizeSpeedX (v
: Integer); inline;
86 function getSizeSpeedY (): Integer; inline;
87 procedure setSizeSpeedY (v
: Integer); inline;
89 function getSizeEndX (): Integer; inline;
90 procedure setSizeEndX (v
: Integer); inline;
91 function getSizeEndY (): Integer; inline;
92 procedure setSizeEndY (v
: Integer); inline;
95 FCurTexture
: Integer; // Íîìåð òåêóùåé òåêñòóðû
99 FWidth
, FHeight
: Word;
105 // sorry, there fields are public to allow setting 'em in g_map; this should be fixed later
106 // for now, PLEASE, don't modify 'em, or all hell will break loose
107 arrIdx
: Integer; // index in one of internal arrays; sorry
108 tag
: Integer; // used in coldets and such; sorry; see g_map.GridTagXXX
109 proxyId
: Integer; // proxy id in map grid (DO NOT USE!)
110 mapId
: AnsiString; // taken directly from map file; dunno why it is here
112 constructor Create(PanelRec
: TDynRecord
;
113 AddTextures
: TAddTextureArray
;
115 var Textures
: TLevelTextureArray
; aguid
: Integer);
116 destructor Destroy(); override;
118 procedure Draw (hasAmbient
: Boolean; constref ambColor
: TDFColor
);
119 procedure DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
121 procedure SetFrame(Frame
: Integer; Count
: Byte);
122 procedure NextTexture(AnimLoop
: Byte = 0);
123 procedure SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
124 function GetTextureID(): Cardinal;
125 function GetTextureCount(): Integer;
127 procedure SaveState(var Mem
: TBinMemoryWriter
);
128 procedure LoadState(var Mem
: TBinMemoryReader
);
130 procedure positionChanged (); inline;
132 function getIsGBack (): Boolean; inline; // gRenderBackgrounds
133 function getIsGStep (): Boolean; inline; // gSteps
134 function getIsGWall (): Boolean; inline; // gWalls
135 function getIsGAcid1 (): Boolean; inline; // gAcid1
136 function getIsGAcid2 (): Boolean; inline; // gAcid2
137 function getIsGWater (): Boolean; inline; // gWater
138 function getIsGFore (): Boolean; inline; // gRenderForegrounds
139 function getIsGLift (): Boolean; inline; // gLifts
140 function getIsGBlockMon (): Boolean; inline; // gBlockMon
143 function gncNeedSend (): Boolean; inline;
144 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
147 property visvalid
: Boolean read getvisvalid
; // panel is "visvalid" when it's width and height are positive
150 property guid
: Integer read mGUID
; // will be assigned in "g_map.pas"
151 property x0
: Integer read FX
;
152 property y0
: Integer read FY
;
153 property x1
: Integer read getx1
; // inclusive!
154 property y1
: Integer read gety1
; // inclusive!
155 property x
: Integer read FX write FX
;
156 property y
: Integer read FY write FY
;
157 property width
: Word read FWidth write FWidth
;
158 property height
: Word read FHeight write FHeight
;
159 property panelType
: Word read FPanelType write FPanelType
;
160 property enabled
: Boolean read FEnabled write FEnabled
; // Ñîõðàíÿòü ïðè SaveState?
161 property door
: Boolean read FDoor write FDoor
; // Ñîõðàíÿòü ïðè SaveState?
162 property liftType
: Byte read FLiftType write FLiftType
; // Ñîõðàíÿòü ïðè SaveState?
163 property lastAnimLoop
: Byte read FLastAnimLoop write FLastAnimLoop
; // Ñîõðàíÿòü ïðè SaveState?
165 property movingSpeedX
: Integer read getMovingSpeedX write setMovingSpeedX
;
166 property movingSpeedY
: Integer read getMovingSpeedY write setMovingSpeedY
;
167 property movingStartX
: Integer read getMovingStartX write setMovingStartX
;
168 property movingStartY
: Integer read getMovingStartY write setMovingStartY
;
169 property movingEndX
: Integer read getMovingEndX write setMovingEndX
;
170 property movingEndY
: Integer read getMovingEndY write setMovingEndY
;
171 property movingActive
: Boolean read mMovingActive write mMovingActive
;
172 property moveOnce
: Boolean read mMoveOnce write mMoveOnce
;
174 property sizeSpeedX
: Integer read getSizeSpeedX write setSizeSpeedX
;
175 property sizeSpeedY
: Integer read getSizeSpeedY write setSizeSpeedY
;
176 property sizeEndX
: Integer read getSizeEndX write setSizeEndX
;
177 property sizeEndY
: Integer read getSizeEndY write setSizeEndY
;
179 property isGBack
: Boolean read getIsGBack
;
180 property isGStep
: Boolean read getIsGStep
;
181 property isGWall
: Boolean read getIsGWall
;
182 property isGAcid1
: Boolean read getIsGAcid1
;
183 property isGAcid2
: Boolean read getIsGAcid2
;
184 property isGWater
: Boolean read getIsGWater
;
185 property isGFore
: Boolean read getIsGFore
;
186 property isGLift
: Boolean read getIsGLift
;
187 property isGBlockMon
: Boolean read getIsGBlockMon
;
190 property movingSpeed
: TDFPoint read mMovingSpeed write mMovingSpeed
;
191 property movingStart
: TDFPoint read mMovingStart write mMovingStart
;
192 property movingEnd
: TDFPoint read mMovingEnd write mMovingEnd
;
194 property sizeSpeed
: TDFSize read mSizeSpeed write mSizeSpeed
;
195 property sizeEnd
: TDFSize read mSizeEnd write mSizeEnd
;
197 property endPosTrigId
: Integer read mEndPosTrig write mEndPosTrig
;
198 property endSizeTrigId
: Integer read mEndSizeTrig write mEndSizeTrig
;
201 TPanelArray
= Array of TPanel
;
204 g_dbgpan_mplat_active
: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
205 g_dbgpan_mplat_step
: Boolean = false; // one step, and stop
211 SysUtils
, g_basic
, g_map
, g_game
, g_gfx
, e_graphics
, g_weapons
, g_triggers
,
212 g_console
, g_language
, g_monsters
, g_player
, g_grid
, e_log
, GL
, utils
;
215 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
219 constructor TPanel
.Create(PanelRec
: TDynRecord
;
220 AddTextures
: TAddTextureArray
;
222 var Textures
: TLevelTextureArray
; aguid
: Integer);
228 Width
:= PanelRec
.Width
;
229 Height
:= PanelRec
.Height
;
236 mapId
:= PanelRec
.id
;
239 mMovingSpeed
:= PanelRec
.moveSpeed
;
240 mMovingStart
:= PanelRec
.moveStart
;
241 mMovingEnd
:= PanelRec
.moveEnd
;
242 mMovingActive
:= PanelRec
['move_active'].value
;
243 mOldMovingActive
:= mMovingActive
;
244 mMoveOnce
:= PanelRec
.moveOnce
;
246 mSizeSpeed
:= PanelRec
.sizeSpeed
;
247 mSizeEnd
:= PanelRec
.sizeEnd
;
249 mEndPosTrig
:= PanelRec
.endPosTrig
;
250 mEndSizeTrig
:= PanelRec
.endSizeTrig
;
255 PanelType
:= PanelRec
.PanelType
;
261 PANEL_OPENDOOR
: begin Enabled
:= False; Door
:= True; end;
262 PANEL_CLOSEDOOR
: Door
:= True;
263 PANEL_LIFTUP
: LiftType
:= 0; //???
264 PANEL_LIFTDOWN
: LiftType
:= 1;
265 PANEL_LIFTLEFT
: LiftType
:= 2;
266 PANEL_LIFTRIGHT
: LiftType
:= 3;
270 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
272 SetLength(FTextureIDs
, 0);
276 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
277 if ByteBool(PanelType
and
282 PANEL_BLOCKMON
)) then
284 SetLength(FTextureIDs
, 0);
289 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
290 if WordBool(PanelType
and (PANEL_WATER
or PANEL_ACID1
or PANEL_ACID2
)) and
291 (not ByteBool(PanelRec
.Flags
and PANEL_FLAG_WATERTEXTURES
)) then
293 SetLength(FTextureIDs
, 1);
294 FTextureIDs
[0].Anim
:= False;
296 case PanelRec
.PanelType
of
298 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_WATER
);
300 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_ACID1
);
302 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_ACID2
);
309 SetLength(FTextureIDs
, Length(AddTextures
));
314 if CurTex
>= Length(FTextureIDs
) then
315 FCurTexture
:= Length(FTextureIDs
) - 1
317 FCurTexture
:= CurTex
;
319 for i
:= 0 to Length(FTextureIDs
)-1 do
321 FTextureIDs
[i
].Anim
:= AddTextures
[i
].Anim
;
322 if FTextureIDs
[i
].Anim
then
323 begin // Àíèìèðîâàííàÿ òåêñòóðà
324 FTextureIDs
[i
].AnTex
:=
325 TAnimation
.Create(Textures
[AddTextures
[i
].Texture
].FramesID
,
326 True, Textures
[AddTextures
[i
].Texture
].Speed
);
327 FTextureIDs
[i
].AnTex
.Blending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
328 FTextureIDs
[i
].AnTex
.Alpha
:= PanelRec
.Alpha
;
331 begin // Îáû÷íàÿ òåêñòóðà
332 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
336 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
337 //if Length(FTextureIDs) > 1 then SaveIt := True;
339 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
340 if PanelRec
.TextureNum
> High(Textures
) then
342 e_WriteLog(Format('WTF?! PanelRec.TextureNum is out of limits! (%d : %d)', [PanelRec
.TextureNum
, High(Textures
)]), MSG_FATALERROR
);
346 FBlending
:= ByteBool(0);
348 else if not g_Map_IsSpecialTexture(Textures
[PanelRec
.TextureNum
].TextureName
) then
350 FTextureWidth
:= Textures
[PanelRec
.TextureNum
].Width
;
351 FTextureHeight
:= Textures
[PanelRec
.TextureNum
].Height
;
352 FAlpha
:= PanelRec
.Alpha
;
353 FBlending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
357 destructor TPanel
.Destroy();
361 for i
:= 0 to High(FTextureIDs
) do
362 if FTextureIDs
[i
].Anim
then
363 FTextureIDs
[i
].AnTex
.Free();
364 SetLength(FTextureIDs
, 0);
369 function TPanel
.getx1 (): Integer; inline; begin result
:= X
+Width
-1; end;
370 function TPanel
.gety1 (): Integer; inline; begin result
:= Y
+Height
-1; end;
371 function TPanel
.getvisvalid (): Boolean; inline; begin result
:= (Width
> 0) and (Height
> 0); end;
373 function TPanel
.getMovingSpeedX (): Integer; inline; begin result
:= mMovingSpeed
.X
; end;
374 procedure TPanel
.setMovingSpeedX (v
: Integer); inline; begin mMovingSpeed
.X
:= v
; end;
375 function TPanel
.getMovingSpeedY (): Integer; inline; begin result
:= mMovingSpeed
.Y
; end;
376 procedure TPanel
.setMovingSpeedY (v
: Integer); inline; begin mMovingSpeed
.Y
:= v
; end;
378 function TPanel
.getMovingStartX (): Integer; inline; begin result
:= mMovingStart
.X
; end;
379 procedure TPanel
.setMovingStartX (v
: Integer); inline; begin mMovingStart
.X
:= v
; end;
380 function TPanel
.getMovingStartY (): Integer; inline; begin result
:= mMovingStart
.Y
; end;
381 procedure TPanel
.setMovingStartY (v
: Integer); inline; begin mMovingStart
.Y
:= v
; end;
383 function TPanel
.getMovingEndX (): Integer; inline; begin result
:= mMovingEnd
.X
; end;
384 procedure TPanel
.setMovingEndX (v
: Integer); inline; begin mMovingEnd
.X
:= v
; end;
385 function TPanel
.getMovingEndY (): Integer; inline; begin result
:= mMovingEnd
.Y
; end;
386 procedure TPanel
.setMovingEndY (v
: Integer); inline; begin mMovingEnd
.Y
:= v
; end;
388 function TPanel
.getSizeSpeedX (): Integer; inline; begin result
:= mSizeSpeed
.w
; end;
389 procedure TPanel
.setSizeSpeedX (v
: Integer); inline; begin mSizeSpeed
.w
:= v
; end;
390 function TPanel
.getSizeSpeedY (): Integer; inline; begin result
:= mSizeSpeed
.h
; end;
391 procedure TPanel
.setSizeSpeedY (v
: Integer); inline; begin mSizeSpeed
.h
:= v
; end;
393 function TPanel
.getSizeEndX (): Integer; inline; begin result
:= mSizeEnd
.w
; end;
394 procedure TPanel
.setSizeEndX (v
: Integer); inline; begin mSizeEnd
.w
:= v
; end;
395 function TPanel
.getSizeEndY (): Integer; inline; begin result
:= mSizeEnd
.h
; end;
396 procedure TPanel
.setSizeEndY (v
: Integer); inline; begin mSizeEnd
.h
:= v
; end;
398 function TPanel
.getIsGBack (): Boolean; inline; begin result
:= ((tag
and GridTagBack
) <> 0); end;
399 function TPanel
.getIsGStep (): Boolean; inline; begin result
:= ((tag
and GridTagStep
) <> 0); end;
400 function TPanel
.getIsGWall (): Boolean; inline; begin result
:= ((tag
and (GridTagWall
or GridTagDoor
)) <> 0); end;
401 function TPanel
.getIsGAcid1 (): Boolean; inline; begin result
:= ((tag
and GridTagAcid1
) <> 0); end;
402 function TPanel
.getIsGAcid2 (): Boolean; inline; begin result
:= ((tag
and GridTagAcid2
) <> 0); end;
403 function TPanel
.getIsGWater (): Boolean; inline; begin result
:= ((tag
and GridTagWater
) <> 0); end;
404 function TPanel
.getIsGFore (): Boolean; inline; begin result
:= ((tag
and GridTagFore
) <> 0); end;
405 function TPanel
.getIsGLift (): Boolean; inline; begin result
:= ((tag
and GridTagLift
) <> 0); end;
406 function TPanel
.getIsGBlockMon (): Boolean; inline; begin result
:= ((tag
and GridTagBlockMon
) <> 0); end;
408 function TPanel
.gncNeedSend (): Boolean; inline; begin result
:= mNeedSend
; mNeedSend
:= false; end;
409 procedure TPanel
.setDirty (); inline; begin mNeedSend
:= true; end;
412 procedure TPanel
.Draw (hasAmbient
: Boolean; constref ambColor
: TDFColor
);
418 if {Enabled and} (FCurTexture
>= 0) and
419 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) {and
420 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
422 if FTextureIDs
[FCurTexture
].Anim
then
423 begin // Àíèìèðîâàííàÿ òåêñòóðà
424 if FTextureIDs
[FCurTexture
].AnTex
= nil then
427 for xx
:= 0 to (Width
div FTextureWidth
)-1 do
428 for yy
:= 0 to (Height
div FTextureHeight
)-1 do
429 FTextureIDs
[FCurTexture
].AnTex
.Draw(
430 X
+ xx
*FTextureWidth
,
431 Y
+ yy
*FTextureHeight
, M_NONE
);
434 begin // Îáû÷íàÿ òåêñòóðà
435 case FTextureIDs
[FCurTexture
].Tex
of
436 LongWord(TEXTURE_SPECIAL_WATER
): e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1, 0, 0, 255, 0, B_FILTER
);
437 LongWord(TEXTURE_SPECIAL_ACID1
): e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1, 0, 128, 0, 0, B_FILTER
);
438 LongWord(TEXTURE_SPECIAL_ACID2
): e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1, 128, 0, 0, 0, B_FILTER
);
439 LongWord(TEXTURE_NONE
):
440 if g_Texture_Get('NOTEXTURE', NoTextureID
) then
442 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
443 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
, 0, False, False);
447 xx
:= X
+ (Width
div 2);
448 yy
:= Y
+ (Height
div 2);
449 e_DrawFillQuad(X
, Y
, xx
, yy
, 255, 0, 255, 0);
450 e_DrawFillQuad(xx
, Y
, X
+Width
-1, yy
, 255, 255, 0, 0);
451 e_DrawFillQuad(X
, yy
, xx
, Y
+Height
-1, 255, 255, 0, 0);
452 e_DrawFillQuad(xx
, yy
, X
+Width
-1, Y
+Height
-1, 255, 0, 255, 0);
456 if not mMovingActive
then
457 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
, Width
div FTextureWidth
, Height
div FTextureHeight
, FAlpha
, True, FBlending
, hasAmbient
)
459 e_DrawFillX(FTextureIDs
[FCurTexture
].Tex
, X
, Y
, Width
, Height
, FAlpha
, True, FBlending
, g_dbg_scale
, hasAmbient
);
460 if hasAmbient
then e_AmbientQuad(X
, Y
, Width
, Height
, ambColor
.r
, ambColor
.g
, ambColor
.b
, ambColor
.a
);
467 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
468 procedure extrude (x
: Integer; y
: Integer);
470 glVertex2i(x
+(x
-lightX
)*500, y
+(y
-lightY
)*500);
471 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
474 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
476 // does this side facing the light?
477 if ((x1
-x0
)*(lightY
-y0
)-(lightX
-x0
)*(y1
-y0
) >= 0) then exit
;
478 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
479 // this edge is facing the light, extrude and draw it
487 if radius
< 4 then exit
;
488 if Enabled
and (FCurTexture
>= 0) and (Width
> 0) and (Height
> 0) and (FAlpha
< 255) {and
489 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
491 if not FTextureIDs
[FCurTexture
].Anim
then
493 case FTextureIDs
[FCurTexture
].Tex
of
494 LongWord(TEXTURE_SPECIAL_WATER
): exit
;
495 LongWord(TEXTURE_SPECIAL_ACID1
): exit
;
496 LongWord(TEXTURE_SPECIAL_ACID2
): exit
;
497 LongWord(TEXTURE_NONE
): exit
;
500 if (X
+Width
< lightX
-radius
) then exit
;
501 if (Y
+Height
< lightY
-radius
) then exit
;
502 if (X
> lightX
+radius
) then exit
;
503 if (Y
> lightY
+radius
) then exit
;
504 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
507 drawLine(x
, y
, x
+width
, y
); // top
508 drawLine(x
+width
, y
, x
+width
, y
+height
); // right
509 drawLine(x
+width
, y
+height
, x
, y
+height
); // bottom
510 drawLine(x
, y
+height
, x
, y
); // left
516 procedure TPanel
.positionChanged (); inline;
518 px
, py
, pw
, ph
: Integer;
520 if (proxyId
>= 0) then
522 mapGrid
.getBodyDims(proxyId
, px
, py
, pw
, ph
);
523 if (px
<> x
) or (py
<> y
) or (pw
<> Width
) or (ph
<> Height
) then
526 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
527 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
529 g_Mark(px
, py
, pw
, ph
, MARK_WALL
, false);
530 if (Width
< 1) or (Height
< 1) then
532 mapGrid
.proxyEnabled
[proxyId
] := false;
536 mapGrid
.proxyEnabled
[proxyId
] := Enabled
;
537 if (pw
<> Width
) or (ph
<> Height
) then
539 //writeln('panel resize!');
540 mapGrid
.moveResizeBody(proxyId
, X
, Y
, Width
, Height
)
544 mapGrid
.moveBody(proxyId
, X
, Y
);
546 g_Mark(X
, Y
, Width
, Height
, MARK_WALL
);
554 monCheckList
: array of TMonster
= nil;
555 monCheckListUsed
: Integer = 0;
557 procedure TPanel
.Update();
560 nx
, ny
, nw
, nh
: Integer;
561 ex
, ey
, nex
, ney
: Integer;
564 // return `true` if we should move by dx,dy
565 function tryMPlatMove (px
, py
, pw
, ph
: Integer; out dx
, dy
: Integer; out squash
: Boolean; ontop
: PBoolean=nil): Boolean;
576 pdx
:= mMovingSpeed
.X
;
577 pdy
:= mMovingSpeed
.Y
;
578 // standing on the platform?
581 if (ontop
<> nil) then ontop
^ := true;
582 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
583 mapGrid
.traceBox(tex
, tey
, px
, py
, pw
, ph
, pdx
, pdy
, nil, (GridTagWall
or GridTagDoor
));
587 if (ontop
<> nil) then ontop
^ := false;
588 // not standing on the platform: trace platform to see if it hits the entity
589 // first, process size change (as we cannot sweeptest both move and size change)
590 // but we don't have to check for pushing if the panel is shrinking
593 if (szdx
> 0) or (szdy
> 0) then
595 // ignore shrinking dimension
596 if (szdx
< 0) then szdx
:= 0;
597 if (szdy
< 0) then szdy
:= 0;
598 // move platform by szd* back, and check for szd* movement
599 if sweepAABB(ox
-szdx
, oy
-szdy
, nw
, nh
, szdx
, szdy
, px
, py
, pw
, ph
, @u0
) then
601 // yes, platform hits the entity, push the entity in the resizing direction
602 u0
:= 1.0-u0
; // how much path left?
603 szdx
:= trunc(szdx
*u0
);
604 szdy
:= trunc(szdy
*u0
);
605 if (szdx
<> 0) or (szdy
<> 0) then
607 // has some path to go, trace the entity
608 trtag
:= (GridTagWall
or GridTagDoor
);
609 // if we're moving down, consider steps too
610 if (szdy
> 0) then trtag
:= trtag
or GridTagStep
;
611 mapGrid
.traceBox(tex
, tey
, px
, py
, pw
, ph
, szdx
, szdy
, nil, trtag
);
615 // second, process platform movement, using te* as entity starting point
616 if sweepAABB(ox
, oy
, nw
, nh
, pdx
, pdy
, tex
, tey
, pw
, ph
, @u0
) then
618 //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]);
619 // yes, platform hits the entity, push the entity in the direction of the platform
620 u0
:= 1.0-u0
; // how much path left?
621 pdx
:= trunc(pdx
*u0
);
622 pdy
:= trunc(pdy
*u0
);
623 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
624 if (pdx
<> 0) or (pdy
<> 0) then
626 // has some path to go, trace the entity
627 trtag
:= (GridTagWall
or GridTagDoor
);
628 // if we're moving down, consider steps too
629 if (pdy
> 0) then trtag
:= trtag
or GridTagStep
;
630 mapGrid
.traceBox(tex
, tey
, px
, py
, pw
, ph
, pdx
, pdy
, nil, trtag
);
634 // done with entity movement, new coords are in te*
637 result
:= (dx
<> 0) or (dy
<> 0);
638 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
640 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
641 squash
:= g_Collide(tex
, tey
, pw
, ph
, nx
, ny
, nw
, nh
); // squash, if still in platform
645 function monCollect (mon
: TMonster
): Boolean;
647 result
:= false; // don't stop
648 if (monCheckListUsed
>= Length(monCheckList
)) then SetLength(monCheckList
, monCheckListUsed
+128);
649 monCheckList
[monCheckListUsed
] := mon
;
650 Inc(monCheckListUsed
);
654 cx0
, cy0
, cx1
, cy1
, cw
, ch
: Integer;
656 px
, py
, pw
, ph
, pdx
, pdy
: Integer;
664 actMoveTrig
: Boolean;
665 actSizeTrig
: Boolean;
667 if (not Enabled
) or (Width
< 1) or (Height
< 1) then exit
;
669 if (FCurTexture
>= 0) and
670 (FTextureIDs
[FCurTexture
].Anim
) and
671 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
674 FTextureIDs
[FCurTexture
].AnTex
.Update();
675 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
676 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
679 if not g_dbgpan_mplat_active
then exit
;
681 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
682 mOldMovingActive
:= mMovingActive
;
684 if not mMovingActive
then exit
;
685 if mMovingSpeed
.isZero
and mSizeSpeed
.isZero
then exit
;
687 //TODO: write wall size change processing
692 * collect all monsters and players (aka entities) along the possible platform path
693 * if entity is standing on a platform:
694 * try to move it along the platform path, checking wall collisions
695 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
697 * if we can't push entity all the way, squash it
704 nw
:= mpw
+mSizeSpeed
.w
;
705 nh
:= mph
+mSizeSpeed
.h
;
706 nx
:= ox
+mMovingSpeed
.X
;
707 ny
:= oy
+mMovingSpeed
.Y
;
709 // force network updates only if some sudden change happened
710 // set the flag here, so we can sync affected monsters
711 if not mSizeSpeed
.isZero
and (nw
= mSizeEnd
.w
) and (nh
= mSizeEnd
.h
) then
715 else if ((mMovingSpeed
.X
< 0) and (nx
<= mMovingStart
.X
)) or ((mMovingSpeed
.X
> 0) and (nx
>= mMovingEnd
.X
)) then
719 else if ((mMovingSpeed
.Y
< 0) and (ny
<= mMovingStart
.Y
)) or ((mMovingSpeed
.Y
> 0) and (ny
>= mMovingEnd
.Y
)) then
724 // if pannel disappeared, we don't have to do anything
725 if (nw
> 0) and (nh
> 0) then
736 cx1
:= nmax(ex
, nex
);
737 cy1
:= nmax(ey
, ney
);
746 // process "obstacle" panels
747 if ((tag
and GridTagObstacle
) <> 0) then
749 // temporarily turn off this panel, so it won't interfere with collision checks
750 mapGrid
.proxyEnabled
[proxyId
] := false;
753 for f
:= 0 to High(gPlayers
) do
756 if (plr
= nil) or (not plr
.alive
) then continue
;
757 plr
.getMapBox(px
, py
, pw
, ph
);
758 if not g_Collide(px
, py
, pw
, ph
, cx0
, cy0
, cw
, ch
) then continue
;
759 if tryMPlatMove(px
, py
, pw
, ph
, pdx
, pdy
, squash
) then
762 plr
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
764 // squash player, if necessary
765 if not g_Game_IsClient
and squash
then plr
.Damage(15000, 0, 0, 0, HIT_TRAP
);
769 for f
:= 0 to High(gGibs
) do
772 if not gib
.alive
then continue
;
773 gib
.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
, @ontop
) then
778 gib
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
782 // move and push corpses
783 for f
:= 0 to High(gCorpses
) do
786 if (cor
= nil) then continue
;
787 cor
.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
792 cor
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
797 monCheckListUsed
:= 0;
798 g_Mons_ForEachAt(cx0
, cy0
, cw
, ch
, monCollect
);
800 // process collected monsters
801 if (monCheckListUsed
> 0) then
803 mpfrid
:= g_Mons_getNewMPlatFrameId();
804 for f
:= 0 to monCheckListUsed
do
806 mon
:= monCheckList
[f
];
807 if (mon
= nil) or (not mon
.alive
) or (mon
.mplatCheckFrameId
= mpfrid
) then continue
;
808 mon
.mplatCheckFrameId
:= mpfrid
;
809 mon
.getMapBox(px
, py
, pw
, ph
);
810 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
811 if tryMPlatMove(px
, py
, pw
, ph
, pdx
, pdy
, squash
) then
814 mon
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
815 //???FIXME: do we really need to send monsters over the net?
816 // i don't think so, as dead reckoning should take care of 'em
817 // ok, send new monster position only if platform is going to change it's direction
818 if mNeedSend
then mon
.setDirty();
820 // squash monster, if necessary
821 if not g_Game_IsClient
and squash
then mon
.Damage(15000, 0, 0, 0, HIT_TRAP
);
825 // restore panel state
826 mapGrid
.proxyEnabled
[proxyId
] := true;
837 actMoveTrig
:= false;
838 actSizeTrig
:= false;
840 // `mNeedSend` was set above
843 if not mSizeSpeed
.isZero
and (nw
= mSizeEnd
.w
) and (nh
= mSizeEnd
.h
) then
848 if (nw
< 1) or (nh
< 1) then mMovingActive
:= false; //HACK!
851 // reverse moving direction, if necessary
852 if ((mMovingSpeed
.X
< 0) and (nx
<= mMovingStart
.X
)) or ((mMovingSpeed
.X
> 0) and (nx
>= mMovingEnd
.X
)) then
854 if mMoveOnce
then mMovingActive
:= false else mMovingSpeed
.X
:= -mMovingSpeed
.X
;
858 if ((mMovingSpeed
.Y
< 0) and (ny
<= mMovingStart
.Y
)) or ((mMovingSpeed
.Y
> 0) and (ny
>= mMovingEnd
.Y
)) then
860 if mMoveOnce
then mMovingActive
:= false else mMovingSpeed
.Y
:= -mMovingSpeed
.Y
;
864 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
865 mOldMovingActive
:= mMovingActive
;
867 if not g_Game_IsClient
then
869 if actMoveTrig
then g_Triggers_Press(mEndPosTrig
, ACTIVATE_CUSTOM
);
870 if actSizeTrig
then g_Triggers_Press(mEndSizeTrig
, ACTIVATE_CUSTOM
);
873 // some triggers may activate this, don't delay sending
874 //TODO: when triggers will be able to control speed and size, check that here too
875 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
876 mOldMovingActive
:= mMovingActive
;
881 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
883 function ClampInt(X
, A
, B
: Integer): Integer;
886 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
890 if Enabled
and (FCurTexture
>= 0) and
891 (FTextureIDs
[FCurTexture
].Anim
) and
892 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
893 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
895 FCurFrame
:= ClampInt(Frame
, 0, FTextureIDs
[FCurTexture
].AnTex
.TotalFrames
);
896 FCurFrameCount
:= Count
;
897 FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
:= FCurFrame
;
898 FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
:= FCurFrameCount
;
902 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
904 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
907 if Length(FTextureIDs
) = 0 then
910 // Òîëüêî îäíà òåêñòóðà:
911 if Length(FTextureIDs
) = 1 then
913 if FCurTexture
= 0 then
919 // Áîëüøå îäíîé òåêñòóðû:
923 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
924 if FCurTexture
>= Length(FTextureIDs
) then
928 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
929 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
931 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
933 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
938 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
941 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
943 FTextureIDs
[FCurTexture
].AnTex
.Reset();
946 LastAnimLoop
:= AnimLoop
;
949 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
952 if Length(FTextureIDs
) = 0 then
955 // Òîëüêî îäíà òåêñòóðà:
956 if Length(FTextureIDs
) = 1 then
958 if (ID
= 0) or (ID
= -1) then
962 // Áîëüøå îäíîé òåêñòóðû:
964 if (ID
>= -1) and (ID
<= High(FTextureIDs
)) then
968 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
969 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
971 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
973 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
978 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
981 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
983 FTextureIDs
[FCurTexture
].AnTex
.Reset();
986 LastAnimLoop
:= AnimLoop
;
989 function TPanel
.GetTextureID(): DWORD
;
991 Result
:= LongWord(TEXTURE_NONE
);
993 if (FCurTexture
>= 0) then
995 if FTextureIDs
[FCurTexture
].Anim
then
996 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
998 Result
:= FTextureIDs
[FCurTexture
].Tex
;
1002 function TPanel
.GetTextureCount(): Integer;
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;
1014 PAN_SAVE_VERSION
= 1;
1016 procedure TPanel
.SaveState (var Mem
: TBinMemoryWriter
);
1022 if (Mem
= nil) then exit
;
1025 sig
:= PANEL_SIGNATURE
; // 'PANL'
1026 Mem
.WriteDWORD(sig
);
1027 ver
:= PAN_SAVE_VERSION
;
1029 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1030 Mem
.WriteBoolean(FEnabled
);
1031 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1032 Mem
.WriteByte(FLiftType
);
1033 // Íîìåð òåêóùåé òåêñòóðû
1034 Mem
.WriteInt(FCurTexture
);
1038 Mem
.WriteWord(FWidth
);
1039 Mem
.WriteWord(FHeight
);
1040 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1041 if (FCurTexture
>= 0) and (FTextureIDs
[FCurTexture
].Anim
) then
1043 assert(FTextureIDs
[FCurTexture
].AnTex
<> nil, 'TPanel.SaveState: No animation object');
1050 Mem
.WriteBoolean(anim
);
1051 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1052 if anim
then FTextureIDs
[FCurTexture
].AnTex
.SaveState(Mem
);
1054 // moving platform state
1055 Mem
.WriteInt(mMovingSpeed
.X
);
1056 Mem
.WriteInt(mMovingSpeed
.Y
);
1057 Mem
.WriteInt(mMovingStart
.X
);
1058 Mem
.WriteInt(mMovingStart
.Y
);
1059 Mem
.WriteInt(mMovingEnd
.X
);
1060 Mem
.WriteInt(mMovingEnd
.Y
);
1062 Mem
.WriteInt(mSizeSpeed
.w
);
1063 Mem
.WriteInt(mSizeSpeed
.h
);
1064 Mem
.WriteInt(mSizeEnd
.w
);
1065 Mem
.WriteInt(mSizeEnd
.h
);
1067 Mem
.WriteBoolean(mMovingActive
);
1068 Mem
.WriteBoolean(mMoveOnce
);
1070 Mem
.WriteInt(mEndPosTrig
);
1071 Mem
.WriteInt(mEndSizeTrig
);
1075 procedure TPanel
.LoadState (var Mem
: TBinMemoryReader
);
1081 if (Mem
= nil) then exit
;
1085 if (sig
<> PANEL_SIGNATURE
) then raise EBinSizeError
.Create('TPanel.LoadState: wrong panel signature'); // 'PANL'
1087 if (ver
<> PAN_SAVE_VERSION
) then raise EBinSizeError
.Create('TPanel.LoadState: invalid panel version');
1088 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1089 Mem
.ReadBoolean(FEnabled
);
1090 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1091 Mem
.ReadByte(FLiftType
);
1092 // Íîìåð òåêóùåé òåêñòóðû
1093 Mem
.ReadInt(FCurTexture
);
1097 Mem
.ReadWord(FWidth
);
1098 Mem
.ReadWord(FHeight
);
1099 //e_LogWritefln('panel %s(%s): old=(%s,%s); new=(%s,%s); delta=(%s,%s)', [arrIdx, proxyId, ox, oy, FX, FY, FX-ox, FY-oy]);
1100 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1101 Mem
.ReadBoolean(anim
);
1102 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1105 Assert((FCurTexture
>= 0) and
1106 (FTextureIDs
[FCurTexture
].Anim
) and
1107 (FTextureIDs
[FCurTexture
].AnTex
<> nil),
1108 'TPanel.LoadState: No animation object');
1109 FTextureIDs
[FCurTexture
].AnTex
.LoadState(Mem
);
1112 // moving platform state
1113 Mem
.ReadInt(mMovingSpeed
.X
);
1114 Mem
.ReadInt(mMovingSpeed
.Y
);
1115 Mem
.ReadInt(mMovingStart
.X
);
1116 Mem
.ReadInt(mMovingStart
.Y
);
1117 Mem
.ReadInt(mMovingEnd
.X
);
1118 Mem
.ReadInt(mMovingEnd
.Y
);
1120 Mem
.ReadInt(mSizeSpeed
.w
);
1121 Mem
.ReadInt(mSizeSpeed
.h
);
1122 Mem
.ReadInt(mSizeEnd
.w
);
1123 Mem
.ReadInt(mSizeEnd
.h
);
1125 Mem
.ReadBoolean(mMovingActive
);
1126 Mem
.ReadBoolean(mMoveOnce
);
1128 Mem
.ReadInt(mEndPosTrig
);
1129 Mem
.ReadInt(mEndSizeTrig
);
1132 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas