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, version 3 of the License ONLY.
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.
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/>.
15 {$INCLUDE ../shared/a_modes.inc}
23 MAPDEF
, g_textures
, xdynrec
;
26 TAddTextureArray
= Array of
33 TPanel
= Class (TObject
)
37 mGUID
: Integer; // will be assigned in "g_map.pas"
45 False: (Tex
: Cardinal);
46 True: (AnTex
: TAnimation
);
49 mMovingSpeed
: TDFPoint
;
50 mMovingStart
: TDFPoint
;
52 mMovingActive
: Boolean;
55 mOldMovingActive
: Boolean;
61 mEndSizeTrig
: Integer;
63 mNeedSend
: Boolean; // for network
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;
96 FCurTexture
: Integer; // Íîìåð òåêóùåé òåêñòóðû
100 FWidth
, FHeight
: Word;
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
;
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);
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
146 function gncNeedSend (): Boolean; inline;
147 procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later
150 property visvalid
: Boolean read getvisvalid
; // panel is "visvalid" when it's width and height are positive
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
;
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
;
204 TPanelArray
= Array of TPanel
;
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
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
;
225 PANEL_SIGNATURE
= $4C4E4150; // 'PANL'
229 constructor TPanel
.Create(PanelRec
: TDynRecord
;
230 AddTextures
: TAddTextureArray
;
232 var Textures
: TLevelTextureArray
; aguid
: Integer);
239 Width
:= PanelRec
.Width
;
240 Height
:= PanelRec
.Height
;
247 mapId
:= PanelRec
.id
;
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
;
266 PanelType
:= PanelRec
.PanelType
;
269 LiftType
:= LIFTTYPE_UP
;
270 hasTexTrigger
:= False;
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
;
282 if ByteBool(PanelRec
.Flags
and PANEL_FLAG_HIDE
) then
284 SetLength(FTextureIDs
, 0);
288 // Ïàíåëè, íå èñïîëüçóþùèå òåêñòóðû:
289 if ByteBool(PanelType
and
294 PANEL_BLOCKMON
)) then
296 SetLength(FTextureIDs
, 0);
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
305 SetLength(FTextureIDs
, 1);
306 FTextureIDs
[0].Anim
:= False;
308 case PanelRec
.PanelType
of
310 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_WATER
);
312 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_ACID1
);
314 FTextureIDs
[0].Tex
:= LongWord(TEXTURE_SPECIAL_ACID2
);
321 SetLength(FTextureIDs
, Length(AddTextures
));
326 if CurTex
>= Length(FTextureIDs
) then
327 FCurTexture
:= Length(FTextureIDs
) - 1
329 FCurTexture
:= CurTex
;
331 for i
:= 0 to Length(FTextureIDs
)-1 do
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
;
343 begin // Îáû÷íàÿ òåêñòóðà
344 FTextureIDs
[i
].Tex
:= Textures
[AddTextures
[i
].Texture
].TextureID
;
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
357 e_WriteLog(Format('WTF?! tnum is out of limits! (%d : %d)', [tnum
, High(Textures
)]), TMsgType
.Warning
);
361 FBlending
:= ByteBool(0);
363 else if not g_Map_IsSpecialTexture(Textures
[{PanelRec.TextureNum}tnum
].TextureName
) then
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
);
372 destructor TPanel
.Destroy();
376 for i
:= 0 to High(FTextureIDs
) do
377 if FTextureIDs
[i
].Anim
then
378 FTextureIDs
[i
].AnTex
.Free();
379 SetLength(FTextureIDs
, 0);
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
);
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
437 if FTextureIDs
[FCurTexture
].Anim
then
438 begin // Àíèìèðîâàííàÿ òåêñòóðà
439 if FTextureIDs
[FCurTexture
].AnTex
= nil then
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
);
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
457 e_GetTextureSize(NoTextureID
, @NW
, @NH
);
458 e_DrawFill(NoTextureID
, X
, Y
, Width
div NW
, Height
div NH
, 0, False, False);
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);
471 if not mMovingActive
then
472 e_DrawFill(FTextureIDs
[FCurTexture
].Tex
, X
, Y
, Width
div FTextureWidth
, Height
div FTextureHeight
, FAlpha
, True, FBlending
, hasAmbient
)
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
);
482 procedure TPanel
.DrawShadowVolume(lightX
: Integer; lightY
: Integer; radius
: Integer);
483 procedure extrude (x
: Integer; y
: Integer);
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);
489 procedure drawLine (x0
: Integer; y0
: Integer; x1
: Integer; y1
: Integer);
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
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
506 if not FTextureIDs
[FCurTexture
].Anim
then
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
;
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);
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
531 procedure TPanel
.positionChanged (); inline;
533 px
, py
, pw
, ph
: Integer;
535 if (proxyId
>= 0) then
537 mapGrid
.getBodyDims(proxyId
, px
, py
, pw
, ph
);
538 if (px
<> x
) or (py
<> y
) or (pw
<> Width
) or (ph
<> Height
) then
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
547 mapGrid
.proxyEnabled
[proxyId
] := false;
551 mapGrid
.proxyEnabled
[proxyId
] := Enabled
;
552 if (pw
<> Width
) or (ph
<> Height
) then
554 //writeln('panel resize!');
555 mapGrid
.moveResizeBody(proxyId
, X
, Y
, Width
, Height
)
559 mapGrid
.moveBody(proxyId
, X
, Y
);
561 g_Mark(X
, Y
, Width
, Height
, MARK_WALL
);
569 monCheckList
: array of TMonster
= nil;
570 monCheckListUsed
: Integer = 0;
572 procedure TPanel
.Update();
575 nx
, ny
, nw
, nh
: Integer;
576 ex
, ey
, nex
, ney
: 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;
591 pdx
:= mMovingSpeed
.X
;
592 pdy
:= mMovingSpeed
.Y
;
593 // standing on the platform?
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
));
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
608 if (szdx
> 0) or (szdy
> 0) then
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
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
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
);
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
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
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
);
649 // done with entity movement, new coords are in te*
652 result
:= (dx
<> 0) or (dy
<> 0);
653 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
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
660 function monCollect (mon
: TMonster
): Boolean;
662 result
:= false; // don't stop
663 if (monCheckListUsed
>= Length(monCheckList
)) then SetLength(monCheckList
, monCheckListUsed
+128);
664 monCheckList
[monCheckListUsed
] := mon
;
665 Inc(monCheckListUsed
);
669 cx0
, cy0
, cx1
, cy1
, cw
, ch
: Integer;
671 px
, py
, pw
, ph
, pdx
, pdy
: Integer;
679 actMoveTrig
: Boolean;
680 actSizeTrig
: Boolean;
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
689 FTextureIDs
[FCurTexture
].AnTex
.Update();
690 FCurFrame
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentFrame
;
691 FCurFrameCount
:= FTextureIDs
[FCurTexture
].AnTex
.CurrentCounter
;
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
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:
712 * if we can't push entity all the way, squash it
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
730 else if ((mMovingSpeed
.X
< 0) and (nx
<= mMovingStart
.X
)) or ((mMovingSpeed
.X
> 0) and (nx
>= mMovingEnd
.X
)) then
734 else if ((mMovingSpeed
.Y
< 0) and (ny
<= mMovingStart
.Y
)) or ((mMovingSpeed
.Y
> 0) and (ny
>= mMovingEnd
.Y
)) then
739 // if pannel disappeared, we don't have to do anything
740 if (nw
> 0) and (nh
> 0) then
751 cx1
:= nmax(ex
, nex
);
752 cy1
:= nmax(ey
, ney
);
761 // process "obstacle" panels
762 if ((tag
and GridTagObstacle
) <> 0) then
764 // temporarily turn off this panel, so it won't interfere with collision checks
765 mapGrid
.proxyEnabled
[proxyId
] := false;
768 for f
:= 0 to High(gPlayers
) do
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
777 plr
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
779 // squash player, if necessary
780 if not g_Game_IsClient
and squash
then plr
.Damage(15000, 0, 0, 0, HIT_TRAP
);
784 for f
:= 0 to High(gGibs
) do
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
793 gib
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
797 // move and push corpses
798 for f
:= 0 to High(gCorpses
) do
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
807 cor
.moveBy(pdx
, pdy
); // this will call `positionChanged()` for us
812 monCheckListUsed
:= 0;
813 g_Mons_ForEachAt(cx0
, cy0
, cw
, ch
, monCollect
);
815 // process collected monsters
816 if (monCheckListUsed
> 0) then
818 mpfrid
:= g_Mons_getNewMPlatFrameId();
819 for f
:= 0 to monCheckListUsed
do
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
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();
835 // squash monster, if necessary
836 if not g_Game_IsClient
and squash
then mon
.Damage(15000, 0, 0, 0, HIT_TRAP
);
840 // restore panel state
841 mapGrid
.proxyEnabled
[proxyId
] := true;
852 actMoveTrig
:= false;
853 actSizeTrig
:= false;
855 // `mNeedSend` was set above
858 if not mSizeSpeed
.isZero
and (nw
= mSizeEnd
.w
) and (nh
= mSizeEnd
.h
) then
863 if (nw
< 1) or (nh
< 1) then mMovingActive
:= false; //HACK!
866 // reverse moving direction, if necessary
867 if ((mMovingSpeed
.X
< 0) and (nx
<= mMovingStart
.X
)) or ((mMovingSpeed
.X
> 0) and (nx
>= mMovingEnd
.X
)) then
869 if mMoveOnce
then mMovingActive
:= false else mMovingSpeed
.X
:= -mMovingSpeed
.X
;
873 if ((mMovingSpeed
.Y
< 0) and (ny
<= mMovingStart
.Y
)) or ((mMovingSpeed
.Y
> 0) and (ny
>= mMovingEnd
.Y
)) then
875 if mMoveOnce
then mMovingActive
:= false else mMovingSpeed
.Y
:= -mMovingSpeed
.Y
;
879 if (mOldMovingActive
<> mMovingActive
) then mNeedSend
:= true;
880 mOldMovingActive
:= mMovingActive
;
882 if not g_Game_IsClient
then
884 if actMoveTrig
then g_Triggers_Press(mEndPosTrig
, ACTIVATE_CUSTOM
);
885 if actSizeTrig
then g_Triggers_Press(mEndSizeTrig
, ACTIVATE_CUSTOM
);
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
;
896 procedure TPanel
.SetFrame(Frame
: Integer; Count
: Byte);
898 function ClampInt(X
, A
, B
: Integer): Integer;
901 if X
< A
then Result
:= A
else if X
> B
then Result
:= B
;
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
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
;
917 procedure TPanel
.NextTexture(AnimLoop
: Byte = 0);
919 Assert(FCurTexture
>= -1, 'FCurTexture < -1');
922 if Length(FTextureIDs
) = 0 then
925 // Òîëüêî îäíà òåêñòóðà:
926 if Length(FTextureIDs
) = 1 then
928 if FCurTexture
= 0 then
934 // Áîëüøå îäíîé òåêñòóðû:
938 // Ñëåäóþùåé íåò - âîçâðàò ê íà÷àëó:
939 if FCurTexture
>= Length(FTextureIDs
) then
943 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
944 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
946 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
948 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
953 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
956 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
958 FTextureIDs
[FCurTexture
].AnTex
.Reset();
961 LastAnimLoop
:= AnimLoop
;
964 procedure TPanel
.SetTexture(ID
: Integer; AnimLoop
: Byte = 0);
966 if (ID
>= -1) and (ID
< Length(FTextureIDs
)) then
969 // Ïåðåêëþ÷èëèñü íà âèäèìóþ àíèì. òåêñòóðó:
970 if (FCurTexture
>= 0) and FTextureIDs
[FCurTexture
].Anim
then
972 if (FTextureIDs
[FCurTexture
].AnTex
= nil) then
974 g_FatalError(_lc
[I_GAME_ERROR_SWITCH_TEXTURE
]);
979 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= True
982 FTextureIDs
[FCurTexture
].AnTex
.Loop
:= False;
984 FTextureIDs
[FCurTexture
].AnTex
.Reset();
987 LastAnimLoop
:= AnimLoop
;
990 function TPanel
.GetTextureID(): DWORD
;
992 Result
:= LongWord(TEXTURE_NONE
);
994 if (FCurTexture
>= 0) then
996 if FTextureIDs
[FCurTexture
].Anim
then
997 Result
:= FTextureIDs
[FCurTexture
].AnTex
.FramesID
999 Result
:= FTextureIDs
[FCurTexture
].Tex
;
1003 function TPanel
.GetTextureCount(): Integer;
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;
1013 function TPanel
.CanChangeTexture(): Boolean;
1015 Result
:= (GetTextureCount() > 1) or hasTexTrigger
;
1019 PAN_SAVE_VERSION
= 1;
1021 procedure TPanel
.SaveState (st
: TStream
);
1025 if (st
= nil) then exit
;
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
1044 assert(FTextureIDs
[FCurTexture
].AnTex
<> nil, 'TPanel.SaveState: No animation object');
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
));
1076 procedure TPanel
.LoadState (st
: TStream
);
1078 if (st
= nil) then exit
;
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
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
);
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
);
1125 //mapGrid.proxyEnabled[proxyId] := FEnabled; // done in g_map.pas