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}
24 MAPDEF
, g_textures
, xdynrec
;
27 TAddTextureArray
= Array of
34 TPanel
= Class (TObject
)
38 mGUID
: Integer; // will be assigned in "g_map.pas"
46 False: (Tex
: Cardinal);
47 True: (AnTex
: TAnimation
);
50 mMovingSpeed
: TDFPoint
;
51 mMovingStart
: TDFPoint
;
53 mMovingActive
: Boolean;
56 mOldMovingActive
: Boolean;
62 mEndSizeTrig
: Integer;
64 mNeedSend
: Boolean; // for network
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;
97 FCurTexture
: Integer; // Íîìåð òåêóùåé òåêñòóðû
101 FWidth
, FHeight
: Word;
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
;
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);
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
147 function gncNeedSend (): Boolean; inline;
148 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
151 property visvalid
: Boolean read getvisvalid
; // panel is "visvalid" when it's width and height are positive
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
;
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
;
205 TPanelArray
= Array of TPanel
;
208 g_dbgpan_mplat_active
: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF};
209 g_dbgpan_mplat_step
: Boolean = false; // one step, and stop
215 {$INCLUDE ../nogl/noGLuses.inc}
216 e_texture
, g_basic
, g_map
, g_game
, g_gfx
, e_graphics
, g_weapons
, g_triggers
,
217 g_console
, g_language
, g_monsters
, g_player
, g_grid
, e_log
, geom
, utils
, xstreams
;
220 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
224 constructor TPanel
.Create(PanelRec
: TDynRecord
;
225 AddTextures
: TAddTextureArray
;
227 var Textures
: TLevelTextureArray
; aguid
: Integer);
234 Width
:= PanelRec
.Width
;
235 Height
:= PanelRec
.Height
;
242 mapId
:= PanelRec
.id
;
245 mMovingSpeed
:= PanelRec
.moveSpeed
;
246 mMovingStart
:= PanelRec
.moveStart
;
247 mMovingEnd
:= PanelRec
.moveEnd
;
248 mMovingActive
:= PanelRec
['move_active'].value
;
249 mOldMovingActive
:= mMovingActive
;
250 mMoveOnce
:= PanelRec
.moveOnce
;
252 mSizeSpeed
:= PanelRec
.sizeSpeed
;
253 mSizeEnd
:= PanelRec
.sizeEnd
;
255 mEndPosTrig
:= PanelRec
.endPosTrig
;
256 mEndSizeTrig
:= PanelRec
.endSizeTrig
;
261 PanelType
:= PanelRec
.PanelType
;
265 hasTexTrigger
:= False;
268 PANEL_OPENDOOR
: begin Enabled
:= False; Door
:= True; end;
269 PANEL_CLOSEDOOR
: Door
:= True;
270 PANEL_LIFTUP
: LiftType
:= 0; //???
271 PANEL_LIFTDOWN
: LiftType
:= 1;
272 PANEL_LIFTLEFT
: LiftType
:= 2;
273 PANEL_LIFTRIGHT
: LiftType
:= 3;
277 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
279 SetLength(FTextureIDs
, 0);
283 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
284 if ByteBool(PanelType
and
289 PANEL_BLOCKMON
)) then
291 SetLength(FTextureIDs
, 0);
296 // Åñëè ýòî æèäêîñòü áåç òåêñòóðû - ñïåöòåêñòóðó:
297 if WordBool(PanelType
and (PANEL_WATER
or PANEL_ACID1
or PANEL_ACID2
)) and
298 (not ByteBool(PanelRec
.Flags
and PANEL_FLAG_WATERTEXTURES
)) then
300 SetLength(FTextureIDs
, 1);
301 FTextureIDs
[0].Anim
:= False;
303 case PanelRec
.PanelType
of
305 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_WATER
);
307 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_ACID1
);
309 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_ACID2
);
316 SetLength(FTextureIDs
, Length(AddTextures
));
321 if CurTex
>= Length(FTextureIDs
) then
322 FCurTexture
:= Length(FTextureIDs
) - 1
324 FCurTexture
:= CurTex
;
326 for i
:= 0 to Length(FTextureIDs
)-1 do
328 FTextureIDs
[i
].Anim
:= AddTextures
[i
].Anim
;
329 if FTextureIDs
[i
].Anim
then
330 begin // Àíèìèðîâàííàÿ òåêñòóðà
331 FTextureIDs
[i
].AnTex
:=
332 TAnimation
.Create(Textures
[AddTextures
[i
].Texture
].FramesID
,
333 True, Textures
[AddTextures
[i
].Texture
].Speed
);
334 FTextureIDs
[i
].AnTex
.Blending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
335 FTextureIDs
[i
].AnTex
.Alpha
:= PanelRec
.Alpha
;
338 begin // Îáû÷íàÿ òåêñòóðà
339 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
343 // Òåêñòóð íåñêîëüêî - íóæíî ñîõðàíÿòü òåêóùóþ:
344 //if Length(FTextureIDs) > 1 then SaveIt := True;
346 if (PanelRec
.TextureRec
= nil) then tnum
:= -1 else tnum
:= PanelRec
.tagInt
;
347 if (tnum
< 0) then tnum
:= Length(Textures
);
349 // Åñëè íå ñïåöòåêñòóðà, òî çàäàåì ðàçìåðû:
350 if ({PanelRec.TextureNum}tnum
> High(Textures
)) then
352 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum
, High(Textures
)]), TMsgType
.Warning
);
356 FBlending
:= ByteBool(0);
358 else if not g_Map_IsSpecialTexture(Textures
[{PanelRec.TextureNum}tnum
].TextureName
) then
360 FTextureWidth
:= Textures
[{PanelRec.TextureNum}tnum
].Width
;
361 FTextureHeight
:= Textures
[{PanelRec.TextureNum}tnum
].Height
;
362 FAlpha
:= PanelRec
.Alpha
;
363 FBlending
:= ByteBool(PanelRec
.Flags
and PANEL_FLAG_BLENDING
);
367 destructor TPanel
.Destroy();
371 for i
:= 0 to High(FTextureIDs
) do
372 if FTextureIDs
[i
].Anim
then
373 FTextureIDs
[i
].AnTex
.Free();
374 SetLength(FTextureIDs
, 0);
379 function TPanel
.getx1 (): Integer; inline; begin result
:= X
+Width
-1; end;
380 function TPanel
.gety1 (): Integer; inline; begin result
:= Y
+Height
-1; end;
381 function TPanel
.getvisvalid (): Boolean; inline; begin result
:= (Width
> 0) and (Height
> 0); end;
383 function TPanel
.getMovingSpeedX (): Integer; inline; begin result
:= mMovingSpeed
.X
; end;
384 procedure TPanel
.setMovingSpeedX (v
: Integer); inline; begin mMovingSpeed
.X
:= v
; end;
385 function TPanel
.getMovingSpeedY (): Integer; inline; begin result
:= mMovingSpeed
.Y
; end;
386 procedure TPanel
.setMovingSpeedY (v
: Integer); inline; begin mMovingSpeed
.Y
:= v
; end;
388 function TPanel
.getMovingStartX (): Integer; inline; begin result
:= mMovingStart
.X
; end;
389 procedure TPanel
.setMovingStartX (v
: Integer); inline; begin mMovingStart
.X
:= v
; end;
390 function TPanel
.getMovingStartY (): Integer; inline; begin result
:= mMovingStart
.Y
; end;
391 procedure TPanel
.setMovingStartY (v
: Integer); inline; begin mMovingStart
.Y
:= v
; end;
393 function TPanel
.getMovingEndX (): Integer; inline; begin result
:= mMovingEnd
.X
; end;
394 procedure TPanel
.setMovingEndX (v
: Integer); inline; begin mMovingEnd
.X
:= v
; end;
395 function TPanel
.getMovingEndY (): Integer; inline; begin result
:= mMovingEnd
.Y
; end;
396 procedure TPanel
.setMovingEndY (v
: Integer); inline; begin mMovingEnd
.Y
:= v
; end;
398 function TPanel
.getSizeSpeedX (): Integer; inline; begin result
:= mSizeSpeed
.w
; end;
399 procedure TPanel
.setSizeSpeedX (v
: Integer); inline; begin mSizeSpeed
.w
:= v
; end;
400 function TPanel
.getSizeSpeedY (): Integer; inline; begin result
:= mSizeSpeed
.h
; end;
401 procedure TPanel
.setSizeSpeedY (v
: Integer); inline; begin mSizeSpeed
.h
:= v
; end;
403 function TPanel
.getSizeEndX (): Integer; inline; begin result
:= mSizeEnd
.w
; end;
404 procedure TPanel
.setSizeEndX (v
: Integer); inline; begin mSizeEnd
.w
:= v
; end;
405 function TPanel
.getSizeEndY (): Integer; inline; begin result
:= mSizeEnd
.h
; end;
406 procedure TPanel
.setSizeEndY (v
: Integer); inline; begin mSizeEnd
.h
:= v
; end;
408 function TPanel
.getIsGBack (): Boolean; inline; begin result
:= ((tag
and GridTagBack
) <> 0); end;
409 function TPanel
.getIsGStep (): Boolean; inline; begin result
:= ((tag
and GridTagStep
) <> 0); end;
410 function TPanel
.getIsGWall (): Boolean; inline; begin result
:= ((tag
and (GridTagWall
or GridTagDoor
)) <> 0); end;
411 function TPanel
.getIsGAcid1 (): Boolean; inline; begin result
:= ((tag
and GridTagAcid1
) <> 0); end;
412 function TPanel
.getIsGAcid2 (): Boolean; inline; begin result
:= ((tag
and GridTagAcid2
) <> 0); end;
413 function TPanel
.getIsGWater (): Boolean; inline; begin result
:= ((tag
and GridTagWater
) <> 0); end;
414 function TPanel
.getIsGFore (): Boolean; inline; begin result
:= ((tag
and GridTagFore
) <> 0); end;
415 function TPanel
.getIsGLift (): Boolean; inline; begin result
:= ((tag
and GridTagLift
) <> 0); end;
416 function TPanel
.getIsGBlockMon (): Boolean; inline; begin result
:= ((tag
and GridTagBlockMon
) <> 0); end;
418 function TPanel
.gncNeedSend (): Boolean; inline; begin result
:= mNeedSend
; mNeedSend
:= false; end;
419 procedure TPanel
.setDirty (); inline; begin mNeedSend
:= true; end;
422 procedure TPanel
.Draw (hasAmbient
: Boolean; constref ambColor
: TDFColor
);
428 if {Enabled and} (FCurTexture
>= 0) and
429 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) {and
430 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
432 if FTextureIDs
[FCurTexture
].Anim
then
433 begin // Àíèìèðîâàííàÿ òåêñòóðà
434 if FTextureIDs
[FCurTexture
].AnTex
= nil then
437 for xx
:= 0 to (Width
div FTextureWidth
)-1 do
438 for yy
:= 0 to (Height
div FTextureHeight
)-1 do
439 FTextureIDs
[FCurTexture
].AnTex
.Draw(
440 X
+ xx
*FTextureWidth
,
441 Y
+ yy
*FTextureHeight
, TMirrorType
.None
);
444 begin // Îáû÷íàÿ òåêñòóðà
445 case FTextureIDs
[FCurTexture
].Tex
of
446 LongWord(TEXTURE_SPECIAL_WATER
): e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1, 0, 0, 255, 0, TBlending
.Filter
);
447 LongWord(TEXTURE_SPECIAL_ACID1
): e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1, 0, 128, 0, 0, TBlending
.Filter
);
448 LongWord(TEXTURE_SPECIAL_ACID2
): e_DrawFillQuad(X
, Y
, X
+Width
-1, Y
+Height
-1, 128, 0, 0, 0, TBlending
.Filter
);
449 LongWord(TEXTURE_NONE
):
450 if g_Texture_Get('NOTEXTURE', NoTextureID
) then
452 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
453 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
, 0, False, False);
457 xx
:= X
+ (Width
div 2);
458 yy
:= Y
+ (Height
div 2);
459 e_DrawFillQuad(X
, Y
, xx
, yy
, 255, 0, 255, 0);
460 e_DrawFillQuad(xx
, Y
, X
+Width
-1, yy
, 255, 255, 0, 0);
461 e_DrawFillQuad(X
, yy
, xx
, Y
+Height
-1, 255, 255, 0, 0);
462 e_DrawFillQuad(xx
, yy
, X
+Width
-1, Y
+Height
-1, 255, 0, 255, 0);
466 if not mMovingActive
then
467 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
, Width
div FTextureWidth
, Height
div FTextureHeight
, FAlpha
, True, FBlending
, hasAmbient
)
469 e_DrawFillX(FTextureIDs
[FCurTexture
].Tex
, X
, Y
, Width
, Height
, FAlpha
, True, FBlending
, g_dbg_scale
, hasAmbient
);
470 if hasAmbient
then e_AmbientQuad(X
, Y
, Width
, Height
, ambColor
.r
, ambColor
.g
, ambColor
.b
, ambColor
.a
);
477 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
478 procedure extrude (x
: Integer; y
: Integer);
480 glVertex2i(x
+(x
-lightX
)*500, y
+(y
-lightY
)*500);
481 //e_WriteLog(Format(' : (%d,%d)', [x+(x-lightX)*300, y+(y-lightY)*300]), MSG_WARNING);
484 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
486 // does this side facing the light?
487 if ((x1
-x0
)*(lightY
-y0
)-(lightX
-x0
)*(y1
-y0
) >= 0) then exit
;
488 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
489 // this edge is facing the light, extrude and draw it
497 if radius
< 4 then exit
;
498 if Enabled
and (FCurTexture
>= 0) and (Width
> 0) and (Height
> 0) and (FAlpha
< 255) {and
499 g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
501 if not FTextureIDs
[FCurTexture
].Anim
then
503 case FTextureIDs
[FCurTexture
].Tex
of
504 LongWord(TEXTURE_SPECIAL_WATER
): exit
;
505 LongWord(TEXTURE_SPECIAL_ACID1
): exit
;
506 LongWord(TEXTURE_SPECIAL_ACID2
): exit
;
507 LongWord(TEXTURE_NONE
): exit
;
510 if (X
+Width
< lightX
-radius
) then exit
;
511 if (Y
+Height
< lightY
-radius
) then exit
;
512 if (X
> lightX
+radius
) then exit
;
513 if (Y
> lightY
+radius
) then exit
;
514 //e_DrawFill(FTextureIDs[FCurTexture].Tex, X, Y, Width div FTextureWidth, Height div FTextureHeight, FAlpha, True, FBlending);
517 drawLine(x
, y
, x
+width
, y
); // top
518 drawLine(x
+width
, y
, x
+width
, y
+height
); // right
519 drawLine(x
+width
, y
+height
, x
, y
+height
); // bottom
520 drawLine(x
, y
+height
, x
, y
); // left
526 procedure TPanel
.positionChanged (); inline;
528 px
, py
, pw
, ph
: Integer;
530 if (proxyId
>= 0) then
532 mapGrid
.getBodyDims(proxyId
, px
, py
, pw
, ph
);
533 if (px
<> x
) or (py
<> y
) or (pw
<> Width
) or (ph
<> Height
) then
536 e_LogWritefln('panel moved: arridx=%s; guid=%s; proxyid=%s; old:(%s,%s)-(%sx%s); new:(%s,%s)-(%sx%s)',
537 [arrIdx, mGUID, proxyId, px, py, pw, ph, x, y, width, height]);
539 g_Mark(px
, py
, pw
, ph
, MARK_WALL
, false);
540 if (Width
< 1) or (Height
< 1) then
542 mapGrid
.proxyEnabled
[proxyId
] := false;
546 mapGrid
.proxyEnabled
[proxyId
] := Enabled
;
547 if (pw
<> Width
) or (ph
<> Height
) then
549 //writeln('panel resize!');
550 mapGrid
.moveResizeBody(proxyId
, X
, Y
, Width
, Height
)
554 mapGrid
.moveBody(proxyId
, X
, Y
);
556 g_Mark(X
, Y
, Width
, Height
, MARK_WALL
);
564 monCheckList
: array of TMonster
= nil;
565 monCheckListUsed
: Integer = 0;
567 procedure TPanel
.Update();
570 nx
, ny
, nw
, nh
: Integer;
571 ex
, ey
, nex
, ney
: Integer;
574 // return `true` if we should move by dx,dy
575 function tryMPlatMove (px
, py
, pw
, ph
: Integer; out dx
, dy
: Integer; out squash
: Boolean; ontop
: PBoolean
=nil): Boolean;
586 pdx
:= mMovingSpeed
.X
;
587 pdy
:= mMovingSpeed
.Y
;
588 // standing on the platform?
591 if (ontop
<> nil) then ontop
^ := true;
592 // yes, move with it; but skip steps (no need to process size change here, 'cause platform top cannot be changed with it)
593 mapGrid
.traceBox(tex
, tey
, px
, py
, pw
, ph
, pdx
, pdy
, (GridTagWall
or GridTagDoor
));
597 if (ontop
<> nil) then ontop
^ := false;
598 // not standing on the platform: trace platform to see if it hits the entity
599 // first, process size change (as we cannot sweeptest both move and size change)
600 // but we don't have to check for pushing if the panel is shrinking
603 if (szdx
> 0) or (szdy
> 0) then
605 // ignore shrinking dimension
606 if (szdx
< 0) then szdx
:= 0;
607 if (szdy
< 0) then szdy
:= 0;
608 // move platform by szd* back, and check for szd* movement
609 if sweepAABB(ox
-szdx
, oy
-szdy
, nw
, nh
, szdx
, szdy
, px
, py
, pw
, ph
, @u0
) then
611 // yes, platform hits the entity, push the entity in the resizing direction
612 u0
:= 1.0-u0
; // how much path left?
613 szdx
:= trunc(szdx
*u0
);
614 szdy
:= trunc(szdy
*u0
);
615 if (szdx
<> 0) or (szdy
<> 0) then
617 // has some path to go, trace the entity
618 trtag
:= (GridTagWall
or GridTagDoor
);
619 // if we're moving down, consider steps too
620 if (szdy
> 0) then trtag
:= trtag
or GridTagStep
;
621 mapGrid
.traceBox(tex
, tey
, px
, py
, pw
, ph
, szdx
, szdy
, trtag
);
625 // second, process platform movement, using te* as entity starting point
626 if sweepAABB(ox
, oy
, nw
, nh
, pdx
, pdy
, tex
, tey
, pw
, ph
, @u0
) then
628 //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]);
629 // yes, platform hits the entity, push the entity in the direction of the platform
630 u0
:= 1.0-u0
; // how much path left?
631 pdx
:= trunc(pdx
*u0
);
632 pdy
:= trunc(pdy
*u0
);
633 //e_LogWritefln(' platsweep; uleft=%s; pd=(%s,%s)', [u0, pdx, pdy]);
634 if (pdx
<> 0) or (pdy
<> 0) then
636 // has some path to go, trace the entity
637 trtag
:= (GridTagWall
or GridTagDoor
);
638 // if we're moving down, consider steps too
639 if (pdy
> 0) then trtag
:= trtag
or GridTagStep
;
640 mapGrid
.traceBox(tex
, tey
, px
, py
, pw
, ph
, pdx
, pdy
, trtag
);
644 // done with entity movement, new coords are in te*
647 result
:= (dx
<> 0) or (dy
<> 0);
648 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
650 // check for squashing; as entity cannot be pushed into a wall, check only collision with the platform itself
651 squash
:= g_Collide(tex
, tey
, pw
, ph
, nx
, ny
, nw
, nh
); // squash, if still in platform
655 function monCollect (mon
: TMonster
): Boolean;
657 result
:= false; // don't stop
658 if (monCheckListUsed
>= Length(monCheckList
)) then SetLength(monCheckList
, monCheckListUsed
+128);
659 monCheckList
[monCheckListUsed
] := mon
;
660 Inc(monCheckListUsed
);
664 cx0
, cy0
, cx1
, cy1
, cw
, ch
: Integer;
666 px
, py
, pw
, ph
, pdx
, pdy
: Integer;
674 actMoveTrig
: Boolean;
675 actSizeTrig
: Boolean;
677 if (not Enabled
) or (Width
< 1) or (Height
< 1) then exit
;
679 if (FCurTexture
>= 0) and
680 (FTextureIDs
[FCurTexture
].Anim
) and
681 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
684 FTextureIDs
[FCurTexture
].AnTex
.Update();
685 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
686 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
689 if not g_dbgpan_mplat_active
then exit
;
691 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
692 mOldMovingActive
:= mMovingActive
;
694 if not mMovingActive
then exit
;
695 if mMovingSpeed
.isZero
and mSizeSpeed
.isZero
then exit
;
697 //TODO: write wall size change processing
702 * collect all monsters and players (aka entities) along the possible platform path
703 * if entity is standing on a platform:
704 * try to move it along the platform path, checking wall collisions
705 * if entity is NOT standing on a platform, but hit with sweeped platform aabb:
707 * if we can't push entity all the way, squash it
714 nw
:= mpw
+mSizeSpeed
.w
;
715 nh
:= mph
+mSizeSpeed
.h
;
716 nx
:= ox
+mMovingSpeed
.X
;
717 ny
:= oy
+mMovingSpeed
.Y
;
719 // force network updates only if some sudden change happened
720 // set the flag here, so we can sync affected monsters
721 if not mSizeSpeed
.isZero
and (nw
= mSizeEnd
.w
) and (nh
= mSizeEnd
.h
) then
725 else if ((mMovingSpeed
.X
< 0) and (nx
<= mMovingStart
.X
)) or ((mMovingSpeed
.X
> 0) and (nx
>= mMovingEnd
.X
)) then
729 else if ((mMovingSpeed
.Y
< 0) and (ny
<= mMovingStart
.Y
)) or ((mMovingSpeed
.Y
> 0) and (ny
>= mMovingEnd
.Y
)) then
734 // if pannel disappeared, we don't have to do anything
735 if (nw
> 0) and (nh
> 0) then
746 cx1
:= nmax(ex
, nex
);
747 cy1
:= nmax(ey
, ney
);
756 // process "obstacle" panels
757 if ((tag
and GridTagObstacle
) <> 0) then
759 // temporarily turn off this panel, so it won't interfere with collision checks
760 mapGrid
.proxyEnabled
[proxyId
] := false;
763 for f
:= 0 to High(gPlayers
) do
766 if (plr
= nil) or (not plr
.alive
) then continue
;
767 plr
.getMapBox(px
, py
, pw
, ph
);
768 if not g_Collide(px
, py
, pw
, ph
, cx0
, cy0
, cw
, ch
) then continue
;
769 if tryMPlatMove(px
, py
, pw
, ph
, pdx
, pdy
, squash
) then
772 plr
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
774 // squash player, if necessary
775 if not g_Game_IsClient
and squash
then plr
.Damage(15000, 0, 0, 0, HIT_TRAP
);
779 for f
:= 0 to High(gGibs
) do
782 if not gib
.alive
then continue
;
783 gib
.getMapBox(px
, py
, pw
, ph
);
784 if not g_Collide(px
, py
, pw
, ph
, cx0
, cy0
, cw
, ch
) then continue
;
785 if tryMPlatMove(px
, py
, pw
, ph
, pdx
, pdy
, squash
, @ontop
) then
788 gib
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
792 // move and push corpses
793 for f
:= 0 to High(gCorpses
) do
796 if (cor
= nil) then continue
;
797 cor
.getMapBox(px
, py
, pw
, ph
);
798 if not g_Collide(px
, py
, pw
, ph
, cx0
, cy0
, cw
, ch
) then continue
;
799 if tryMPlatMove(px
, py
, pw
, ph
, pdx
, pdy
, squash
, @ontop
) then
802 cor
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
807 monCheckListUsed
:= 0;
808 g_Mons_ForEachAt(cx0
, cy0
, cw
, ch
, monCollect
);
810 // process collected monsters
811 if (monCheckListUsed
> 0) then
813 mpfrid
:= g_Mons_getNewMPlatFrameId();
814 for f
:= 0 to monCheckListUsed
do
816 mon
:= monCheckList
[f
];
817 if (mon
= nil) or (not mon
.alive
) or (mon
.mplatCheckFrameId
= mpfrid
) then continue
;
818 mon
.mplatCheckFrameId
:= mpfrid
;
819 mon
.getMapBox(px
, py
, pw
, ph
);
820 //if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
821 if tryMPlatMove(px
, py
, pw
, ph
, pdx
, pdy
, squash
) then
824 mon
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
825 //???FIXME: do we really need to send monsters over the net?
826 // i don't think so, as dead reckoning should take care of 'em
827 // ok, send new monster position only if platform is going to change it's direction
828 if mNeedSend
then mon
.setDirty();
830 // squash monster, if necessary
831 if not g_Game_IsClient
and squash
then mon
.Damage(15000, 0, 0, 0, HIT_TRAP
);
835 // restore panel state
836 mapGrid
.proxyEnabled
[proxyId
] := true;
847 actMoveTrig
:= false;
848 actSizeTrig
:= false;
850 // `mNeedSend` was set above
853 if not mSizeSpeed
.isZero
and (nw
= mSizeEnd
.w
) and (nh
= mSizeEnd
.h
) then
858 if (nw
< 1) or (nh
< 1) then mMovingActive
:= false; //HACK!
861 // reverse moving direction, if necessary
862 if ((mMovingSpeed
.X
< 0) and (nx
<= mMovingStart
.X
)) or ((mMovingSpeed
.X
> 0) and (nx
>= mMovingEnd
.X
)) then
864 if mMoveOnce
then mMovingActive
:= false else mMovingSpeed
.X
:= -mMovingSpeed
.X
;
868 if ((mMovingSpeed
.Y
< 0) and (ny
<= mMovingStart
.Y
)) or ((mMovingSpeed
.Y
> 0) and (ny
>= mMovingEnd
.Y
)) then
870 if mMoveOnce
then mMovingActive
:= false else mMovingSpeed
.Y
:= -mMovingSpeed
.Y
;
874 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
875 mOldMovingActive
:= mMovingActive
;
877 if not g_Game_IsClient
then
879 if actMoveTrig
then g_Triggers_Press(mEndPosTrig
, ACTIVATE_CUSTOM
);
880 if actSizeTrig
then g_Triggers_Press(mEndSizeTrig
, ACTIVATE_CUSTOM
);
883 // some triggers may activate this, don't delay sending
884 //TODO: when triggers will be able to control speed and size, check that here too
885 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
886 mOldMovingActive
:= mMovingActive
;
891 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
893 function ClampInt(X
, A
, B
: Integer): Integer;
896 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
900 if Enabled
and (FCurTexture
>= 0) and
901 (FTextureIDs
[FCurTexture
].Anim
) and
902 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
903 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
905 FCurFrame
:= ClampInt(Frame
, 0, FTextureIDs
[FCurTexture
].AnTex
.TotalFrames
- 1);
906 FCurFrameCount
:= Count
;
907 FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
:= FCurFrame
;
908 FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
:= FCurFrameCount
;
912 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
914 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
917 if Length(FTextureIDs
) = 0 then
920 // Òîëüêî îäíà òåêñòóðà:
921 if Length(FTextureIDs
) = 1 then
923 if FCurTexture
= 0 then
929 // Áîëüøå îäíîé òåêñòóðû:
933 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
934 if FCurTexture
>= Length(FTextureIDs
) then
938 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
939 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
941 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
943 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
948 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
951 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
953 FTextureIDs
[FCurTexture
].AnTex
.Reset();
956 LastAnimLoop
:= AnimLoop
;
959 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
961 if (ID
>= -1) and (ID
< Length(FTextureIDs
)) then
964 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
965 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
967 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
969 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
974 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
977 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
979 FTextureIDs
[FCurTexture
].AnTex
.Reset();
982 LastAnimLoop
:= AnimLoop
;
985 function TPanel
.GetTextureID(): DWORD
;
987 Result
:= LongWord(TEXTURE_NONE
);
989 if (FCurTexture
>= 0) then
991 if FTextureIDs
[FCurTexture
].Anim
then
992 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
994 Result
:= FTextureIDs
[FCurTexture
].Tex
;
998 function TPanel
.GetTextureCount(): Integer;
1000 Result
:= Length(FTextureIDs
);
1001 if Enabled
and (FCurTexture
>= 0) then
1002 if (FTextureIDs
[FCurTexture
].Anim
) and
1003 (FTextureIDs
[FCurTexture
].AnTex
<> nil) and
1004 (Width
> 0) and (Height
> 0) and (FAlpha
< 255) then
1005 Result
:= Result
+ 100;
1008 function TPanel
.CanChangeTexture(): Boolean;
1010 Result
:= (GetTextureCount() > 1) or hasTexTrigger
;
1014 PAN_SAVE_VERSION
= 1;
1016 procedure TPanel
.SaveState (st
: TStream
);
1020 if (st
= nil) then exit
;
1023 utils
.writeSign(st
, 'PANL');
1024 utils
.writeInt(st
, Byte(PAN_SAVE_VERSION
));
1025 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1026 utils
.writeBool(st
, FEnabled
);
1027 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1028 utils
.writeInt(st
, Byte(FLiftType
));
1029 // Íîìåð òåêóùåé òåêñòóðû
1030 utils
.writeInt(st
, Integer(FCurTexture
));
1031 // Êîîðäèíàòû è ðàçìåð
1032 utils
.writeInt(st
, Integer(FX
));
1033 utils
.writeInt(st
, Integer(FY
));
1034 utils
.writeInt(st
, Word(FWidth
));
1035 utils
.writeInt(st
, Word(FHeight
));
1036 // Àíèìèðîâàíà ëè òåêóùàÿ òåêñòóðà
1037 if (FCurTexture
>= 0) and (FTextureIDs
[FCurTexture
].Anim
) then
1039 assert(FTextureIDs
[FCurTexture
].AnTex
<> nil, 'TPanel.SaveState: No animation object');
1046 utils
.writeBool(st
, anim
);
1047 // Åñëè äà - ñîõðàíÿåì àíèìàöèþ
1048 if anim
then FTextureIDs
[FCurTexture
].AnTex
.SaveState(st
);
1050 // moving platform state
1051 utils
.writeInt(st
, Integer(mMovingSpeed
.X
));
1052 utils
.writeInt(st
, Integer(mMovingSpeed
.Y
));
1053 utils
.writeInt(st
, Integer(mMovingStart
.X
));
1054 utils
.writeInt(st
, Integer(mMovingStart
.Y
));
1055 utils
.writeInt(st
, Integer(mMovingEnd
.X
));
1056 utils
.writeInt(st
, Integer(mMovingEnd
.Y
));
1058 utils
.writeInt(st
, Integer(mSizeSpeed
.w
));
1059 utils
.writeInt(st
, Integer(mSizeSpeed
.h
));
1060 utils
.writeInt(st
, Integer(mSizeEnd
.w
));
1061 utils
.writeInt(st
, Integer(mSizeEnd
.h
));
1063 utils
.writeBool(st
, mMovingActive
);
1064 utils
.writeBool(st
, mMoveOnce
);
1066 utils
.writeInt(st
, Integer(mEndPosTrig
));
1067 utils
.writeInt(st
, Integer(mEndSizeTrig
));
1071 procedure TPanel
.LoadState (st
: TStream
);
1073 if (st
= nil) then exit
;
1076 if not utils
.checkSign(st
, 'PANL') then raise XStreamError
.create('wrong panel signature');
1077 if (utils
.readByte(st
) <> PAN_SAVE_VERSION
) then raise XStreamError
.create('wrong panel version');
1078 // Îòêðûòà/çàêðûòà, åñëè äâåðü
1079 FEnabled
:= utils
.readBool(st
);
1080 // Íàïðàâëåíèå ëèôòà, åñëè ëèôò
1081 FLiftType
:= utils
.readByte(st
);
1082 // Íîìåð òåêóùåé òåêñòóðû
1083 FCurTexture
:= utils
.readLongInt(st
);
1084 // Êîîðäèíàòû è ðàçìåð
1085 FX
:= utils
.readLongInt(st
);
1086 FY
:= utils
.readLongInt(st
);
1087 FWidth
:= utils
.readWord(st
);
1088 FHeight
:= utils
.readWord(st
);
1089 // Àíèìèðîâàííàÿ ëè òåêóùàÿ òåêñòóðà
1090 if utils
.readBool(st
) then
1092 // Åñëè äà - çàãðóæàåì àíèìàöèþ
1093 Assert((FCurTexture
>= 0) and
1094 (FTextureIDs
[FCurTexture
].Anim
) and
1095 (FTextureIDs
[FCurTexture
].AnTex
<> nil),
1096 'TPanel.LoadState: No animation object');
1097 FTextureIDs
[FCurTexture
].AnTex
.LoadState(st
);
1100 // moving platform state
1101 mMovingSpeed
.X
:= utils
.readLongInt(st
);
1102 mMovingSpeed
.Y
:= utils
.readLongInt(st
);
1103 mMovingStart
.X
:= utils
.readLongInt(st
);
1104 mMovingStart
.Y
:= utils
.readLongInt(st
);
1105 mMovingEnd
.X
:= utils
.readLongInt(st
);
1106 mMovingEnd
.Y
:= utils
.readLongInt(st
);
1108 mSizeSpeed
.w
:= utils
.readLongInt(st
);
1109 mSizeSpeed
.h
:= utils
.readLongInt(st
);
1110 mSizeEnd
.w
:= utils
.readLongInt(st
);
1111 mSizeEnd
.h
:= utils
.readLongInt(st
);
1113 mMovingActive
:= utils
.readBool(st
);
1114 mMoveOnce
:= utils
.readBool(st
);
1116 mEndPosTrig
:= utils
.readLongInt(st
);
1117 mEndSizeTrig
:= utils
.readLongInt(st
);
1120 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas