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}
22 e_graphics
, g_basic
, MAPSTRUCT
, g_textures
, Classes
,
23 g_phys
, wadreader
, BinEditor
, g_panel
, g_grid
, z_aabbtree
, md5
, binheap
, xprofiler
;
37 PRespawnPoint
= ^TRespawnPoint
;
38 TRespawnPoint
= record
40 Direction
: TDirection
;
44 PFlagPoint
= ^TFlagPoint
;
45 TFlagPoint
= TRespawnPoint
;
53 CaptureTime
: LongWord;
54 Animation
: TAnimation
;
55 Direction
: TDirection
;
58 function g_Map_Load(Res
: String): Boolean;
59 function g_Map_GetMapInfo(Res
: String): TMapInfo
;
60 function g_Map_GetMapsList(WADName
: String): SArray
;
61 function g_Map_Exist(Res
: String): Boolean;
62 procedure g_Map_Free();
63 procedure g_Map_Update();
65 procedure g_Map_DrawPanels (PanelType
: Word); // unaccelerated
66 procedure g_Map_CollectDrawPanels (x0
, y0
, wdt
, hgt
: Integer);
68 procedure g_Map_DrawBack(dx
, dy
: Integer);
69 function g_Map_CollidePanel(X
, Y
: Integer; Width
, Height
: Word;
70 PanelType
: Word; b1x3
: Boolean=false): Boolean;
71 function g_Map_CollideLiquid_Texture(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
72 procedure g_Map_EnableWall(ID
: DWORD
);
73 procedure g_Map_DisableWall(ID
: DWORD
);
74 procedure g_Map_SwitchTexture(PanelType
: Word; ID
: DWORD
; AnimLoop
: Byte = 0);
75 procedure g_Map_SetLift(ID
: DWORD
; t
: Integer);
76 procedure g_Map_ReAdd_DieTriggers();
77 function g_Map_IsSpecialTexture(Texture
: String): Boolean;
79 function g_Map_GetPoint(PointType
: Byte; var RespawnPoint
: TRespawnPoint
): Boolean;
80 function g_Map_GetPointCount(PointType
: Byte): Word;
82 function g_Map_HaveFlagPoints(): Boolean;
84 procedure g_Map_ResetFlag(Flag
: Byte);
85 procedure g_Map_DrawFlags();
87 function g_Map_PanelForPID(PanelID
: Integer; var PanelArrayID
: Integer): PPanel
;
89 procedure g_Map_SaveState(Var Mem
: TBinMemoryWriter
);
90 procedure g_Map_LoadState(Var Mem
: TBinMemoryReader
);
92 procedure g_Map_DrawPanelShadowVolumes(lightX
: Integer; lightY
: Integer; radius
: Integer);
94 // returns wall index in `gWalls` or -1
95 function g_Map_traceToNearestWall (x0
, y0
, x1
, y1
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): Boolean;
98 TForEachPanelCB
= function (pan
: TPanel
): Boolean; // return `true` to stop
100 function g_Map_HasAnyPanelAtPoint (x
, y
: Integer; panelType
: Word): Boolean;
102 // trace liquid, stepping by `dx` and `dy`
103 // return last seen liquid coords, and `false` if we're started outside of the liquid
104 function g_Map_TraceLiquid (x
, y
, dx
, dy
: Integer; out topx
, topy
: Integer): Boolean;
107 procedure g_Map_ProfilersBegin ();
108 procedure g_Map_ProfilersEnd ();
112 RESPAWNPOINT_PLAYER1
= 1;
113 RESPAWNPOINT_PLAYER2
= 2;
115 RESPAWNPOINT_RED
= 4;
116 RESPAWNPOINT_BLUE
= 5;
124 FLAG_STATE_NORMAL
= 1;
125 FLAG_STATE_DROPPED
= 2;
126 FLAG_STATE_CAPTURED
= 3;
127 FLAG_STATE_SCORED
= 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
128 FLAG_STATE_RETURNED
= 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
130 FLAG_TIME
= 720; // 20 seconds
132 SKY_STRETCH
: Single = 1.5;
147 // sorted by draw priority
148 GridTagBack
= 1 shl 0;
149 GridTagStep
= 1 shl 1;
150 GridTagWall
= 1 shl 2;
151 GridTagDoor
= 1 shl 3;
152 GridTagAcid1
= 1 shl 4;
153 GridTagAcid2
= 1 shl 5;
154 GridTagWater
= 1 shl 6;
155 GridTagFore
= 1 shl 7;
156 // the following are invisible
157 GridTagLift
= 1 shl 8;
158 GridTagBlockMon
= 1 shl 9;
163 gRenderBackgrounds
: TPanelArray
;
164 gRenderForegrounds
: TPanelArray
;
165 gWater
, gAcid1
, gAcid2
: TPanelArray
;
168 gBlockMon
: TPanelArray
;
169 gFlags
: array [FLAG_RED
..FLAG_BLUE
] of TFlag
;
170 //gDOMFlags: array of TFlag;
173 gDoorMap
: array of array of DWORD
;
174 gLiftMap
: array of array of DWORD
;
175 gWADHash
: TMD5Digest
;
176 BackID
: DWORD
= DWORD(-1);
177 gExternalResources
: TStringList
;
179 gdbg_map_use_accel_render
: Boolean = true;
180 gdbg_map_use_accel_coldet
: Boolean = true;
181 //gdbg_map_use_tree_draw: Boolean = false;
182 //gdbg_map_use_tree_coldet: Boolean = false;
183 //gdbg_map_dump_coldet_tree_queries: Boolean = false;
184 profMapCollision
: TProfiler
= nil; //WARNING: FOR DEBUGGING ONLY!
185 gDrawPanelList
: TBinaryHeapObj
= nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
187 function panelTypeToTag (panelType
: Word): Integer; // returns GridTagXXX
193 g_main
, e_log
, SysUtils
, g_items
, g_gfx
, g_console
,
194 GL
, GLExt
, g_weapons
, g_game
, g_sound
, e_sound
, CONFIG
,
195 g_options
, MAPREADER
, g_triggers
, g_player
, MAPDEF
,
196 Math
, g_monsters
, g_saveload
, g_language
, g_netmsg
,
198 ImagingTypes
, Imaging
, ImagingUtility
,
199 ImagingGif
, ImagingNetworkGraphics
;
202 FLAGRECT
: TRectWH
= (X
:15; Y
:12; Width
:33; Height
:52);
203 MUSIC_SIGNATURE
= $4953554D; // 'MUSI'
204 FLAG_SIGNATURE
= $47414C46; // 'FLAG'
208 TPanelGrid
= specialize TBodyGridBase
<TPanel
>;
211 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
213 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
214 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
219 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
222 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
223 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
224 if (pan.Width < 1) or (pan.Height < 1) then exit;
225 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
231 function panelTypeToTag (panelType
: Word): Integer;
234 PANEL_WALL
: result
:= GridTagWall
; // gWalls
235 PANEL_OPENDOOR
, PANEL_CLOSEDOOR
: result
:= GridTagDoor
; // gWalls
236 PANEL_BACK
: result
:= GridTagBack
; // gRenderBackgrounds
237 PANEL_FORE
: result
:= GridTagFore
; // gRenderForegrounds
238 PANEL_WATER
: result
:= GridTagWater
; // gWater
239 PANEL_ACID1
: result
:= GridTagAcid1
; // gAcid1
240 PANEL_ACID2
: result
:= GridTagAcid2
; // gAcid2
241 PANEL_STEP
: result
:= GridTagStep
; // gSteps
242 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
: result
:= GridTagLift
; // gLifts -- this is for all lifts
243 PANEL_BLOCKMON
: result
:= GridTagBlockMon
; // gBlockMon -- this is for all blockmons
244 else result
:= GridTagInvalid
;
249 function dplLess (a
, b
: TObject
): Boolean;
255 if (pa
.tag
< pb
.tag
) then begin result
:= true; exit
; end;
256 if (pa
.tag
> pb
.tag
) then begin result
:= false; exit
; end;
257 result
:= (pa
.arrIdx
< pb
.arrIdx
);
260 procedure dplClear ();
262 if (gDrawPanelList
= nil) then gDrawPanelList
:= TBinaryHeapObj
.Create(@dplLess
) else gDrawPanelList
.clear();
268 PWhere
: ^TPanelArray
;
273 PanelById
: array of TPanelID
;
274 Textures
: TLevelTextureArray
;
275 RespawnPoints
: Array of TRespawnPoint
;
276 FlagPoints
: Array [FLAG_RED
..FLAG_BLUE
] of PFlagPoint
;
277 //DOMFlagPoints: Array of TFlagPoint;
278 mapGrid
: TPanelGrid
= nil;
279 //mapTree: TDynAABBTreeMap = nil;
282 procedure g_Map_ProfilersBegin ();
284 if (profMapCollision
= nil) then profMapCollision
:= TProfiler
.Create('COLSOLID', g_profile_history_size
);
285 profMapCollision
.mainBegin(g_profile_collision
);
287 if g_profile_collision
then
289 profMapCollision
.sectionBegin('*solids');
290 profMapCollision
.sectionEnd();
291 profMapCollision
.sectionBegin('liquids');
292 profMapCollision
.sectionEnd();
296 procedure g_Map_ProfilersEnd ();
298 if (profMapCollision
<> nil) then profMapCollision
.mainEnd();
302 // wall index in `gWalls` or -1
304 function g_Map_traceToNearestWallOld (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
306 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
311 result := -666.0; // invalid
312 if not pan.Enabled then exit;
313 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
314 if not aabb.valid then exit;
315 if aabb.intersects(ray, @tmin) then
317 //if (tmin*tmin > maxDistSq) then exit;
318 if (tmin >= 0.0) then
320 //e_WriteLog(Format('sqchecker(%d,%d,%d,%d): panel #%d (%d,%d)-(%d,%d); tmin=%f', [x0, y0, x1, y1, pan.arrIdx, pan.X, pan.Y, pan.Width, pan.Height, tmin]), MSG_NOTIFY);
321 //if (tmin < 0.0) then tmin := 0.0;
328 qr: TDynAABBTreeMap.TSegmentQueryResult;
335 if (mapTree = nil) then exit;
336 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
338 maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
339 if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
341 result := qr.flesh.arrIdx;
342 if (hitx <> nil) or (hity <> nil) then
344 ray := Ray2D.Create(x0, y0, x1, y1);
345 hxf := ray.origX+ray.dirX*qr.time;
346 hyf := ray.origY+ray.dirY*qr.time;
351 if (hx >= qr.flesh.X) and (hy >= qr.flesh.Y) and (hx < qr.flesh.X+qr.flesh.Width) and (hy < qr.flesh.Y+qr.flesh.Height) then
362 if (hitx <> nil) then hitx^ := hx;
363 if (hity <> nil) then hity^ := hy;
371 // wall index in `gWalls` or -1
372 function g_Map_traceToNearestWall (x0
, y0
, x1
, y1
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): Boolean;
375 lastX, lastY, lastDistSq: Integer;
376 wasHit: Boolean = false;
378 // pan=nil: before processing new tile
379 function sqchecker (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
385 // stop if something was hit at the previous tile
391 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
393 if not pan.Enabled then exit;
395 distSq := (prevx-x0)*(prevx-x0)+(prevy-y0)*(prevy-y0);
396 if (distSq < lastDistSq) then
399 lastDistSq := distSq;
411 if (mapGrid = nil) then exit;
412 lastDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+1;
415 result := mapGrid.traceRay(x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor));
416 if (hitx <> nil) then hitx^ := lastX;
417 if (hity <> nil) then hity^ := lastY;
419 result
:= mapGrid
.traceRay(ex
, ey
, x0
, y0
, x1
, y1
, nil, (GridTagWall
or GridTagDoor
));
423 function g_Map_HasAnyPanelAtPoint (x
, y
: Integer; panelType
: Word): Boolean;
425 function checker (pan
: TPanel
; tag
: Integer): Boolean;
428 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
430 result := pan.Enabled; // stop if wall is enabled
435 if ((tag
and GridTagLift
) <> 0) then
437 // stop if the lift of the right type
439 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
440 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
441 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
442 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3)));
446 result
:= true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
450 tagmask
: Integer = 0;
454 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
455 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
456 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
457 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
458 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
459 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
460 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
462 if (tagmask
= 0) then exit
;// just in case
463 if ((tagmask
and GridTagLift
) <> 0) then
466 result
:= mapGrid
.forEachAtPoint(x
, y
, checker
, tagmask
);
471 result
:= mapGrid
.forEachAtPoint(x
, y
, nil, tagmask
);
476 function g_Map_IsSpecialTexture(Texture
: String): Boolean;
478 Result
:= (Texture
= TEXTURE_NAME_WATER
) or
479 (Texture
= TEXTURE_NAME_ACID1
) or
480 (Texture
= TEXTURE_NAME_ACID2
);
483 procedure CreateDoorMap();
485 PanelArray
: Array of record
491 a
, b
, c
, m
, i
, len
: Integer;
499 SetLength(PanelArray
, len
);
501 for a
:= 0 to High(gWalls
) do
502 if gWalls
[a
].Door
then
504 PanelArray
[i
].X
:= gWalls
[a
].X
;
505 PanelArray
[i
].Y
:= gWalls
[a
].Y
;
506 PanelArray
[i
].Width
:= gWalls
[a
].Width
;
507 PanelArray
[i
].Height
:= gWalls
[a
].Height
;
508 PanelArray
[i
].Active
:= True;
509 PanelArray
[i
].PanelID
:= a
;
515 SetLength(PanelArray
, len
);
526 SetLength(gDoorMap
, 0);
528 g_Game_SetLoadingText(_lc
[I_LOAD_DOOR_MAP
], i
-1, False);
531 if PanelArray
[a
].Active
then
533 PanelArray
[a
].Active
:= False;
534 m
:= Length(gDoorMap
);
535 SetLength(gDoorMap
, m
+1);
536 SetLength(gDoorMap
[m
], 1);
537 gDoorMap
[m
, 0] := PanelArray
[a
].PanelID
;
545 if PanelArray
[b
].Active
then
546 for c
:= 0 to High(gDoorMap
[m
]) do
547 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
548 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
549 g_CollideAround(PanelArray
[b
].X
, PanelArray
[b
].Y
,
550 PanelArray
[b
].Width
, PanelArray
[b
].Height
,
551 gWalls
[gDoorMap
[m
, c
]].X
,
552 gWalls
[gDoorMap
[m
, c
]].Y
,
553 gWalls
[gDoorMap
[m
, c
]].Width
,
554 gWalls
[gDoorMap
[m
, c
]].Height
) then
556 PanelArray
[b
].Active
:= False;
557 SetLength(gDoorMap
[m
],
558 Length(gDoorMap
[m
])+1);
559 gDoorMap
[m
, High(gDoorMap
[m
])] := PanelArray
[b
].PanelID
;
565 g_Game_StepLoading();
571 procedure CreateLiftMap();
573 PanelArray
: Array of record
578 a
, b
, c
, len
, i
, j
: Integer;
584 len
:= Length(gLifts
);
585 SetLength(PanelArray
, len
);
587 for a
:= 0 to len
-1 do
589 PanelArray
[a
].X
:= gLifts
[a
].X
;
590 PanelArray
[a
].Y
:= gLifts
[a
].Y
;
591 PanelArray
[a
].Width
:= gLifts
[a
].Width
;
592 PanelArray
[a
].Height
:= gLifts
[a
].Height
;
593 PanelArray
[a
].Active
:= True;
596 SetLength(gLiftMap
, len
);
599 g_Game_SetLoadingText(_lc
[I_LOAD_LIFT_MAP
], len
-1, False);
601 for a
:= 0 to len
-1 do
602 if PanelArray
[a
].Active
then
604 PanelArray
[a
].Active
:= False;
605 SetLength(gLiftMap
[i
], 32);
613 for b
:= 0 to len
-1 do
614 if PanelArray
[b
].Active
then
616 if g_CollideAround(PanelArray
[b
].X
,
619 PanelArray
[b
].Height
,
620 PanelArray
[gLiftMap
[i
, c
]].X
,
621 PanelArray
[gLiftMap
[i
, c
]].Y
,
622 PanelArray
[gLiftMap
[i
, c
]].Width
,
623 PanelArray
[gLiftMap
[i
, c
]].Height
) then
625 PanelArray
[b
].Active
:= False;
627 if j
> High(gLiftMap
[i
]) then
628 SetLength(gLiftMap
[i
],
629 Length(gLiftMap
[i
])+32);
638 SetLength(gLiftMap
[i
], j
+1);
641 g_Game_StepLoading();
644 SetLength(gLiftMap
, i
);
649 function CreatePanel(PanelRec
: TPanelRec_1
; AddTextures
: TAddTextureArray
;
650 CurTex
: Integer; sav
: Boolean): Integer;
653 panels
: ^TPanelArray
;
657 case PanelRec
.PanelType
of
658 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
661 panels
:= @gRenderBackgrounds
;
663 panels
:= @gRenderForegrounds
;
672 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
:
675 panels
:= @gBlockMon
;
680 len
:= Length(panels
^);
681 SetLength(panels
^, len
+ 1);
683 panels
^[len
] := TPanel
.Create(PanelRec
, AddTextures
, CurTex
, Textures
);
684 panels
^[len
].arrIdx
:= len
;
685 panels
^[len
].proxyId
:= -1;
686 panels
^[len
].tag
:= panelTypeToTag(PanelRec
.PanelType
);
688 panels
^[len
].SaveIt
:= True;
692 len
:= Length(PanelByID
);
693 SetLength(PanelByID
, len
+ 1);
694 PanelByID
[len
].PWhere
:= panels
;
695 PanelByID
[len
].PArrID
:= Result
;
698 function CreateNullTexture(RecName
: String): Integer;
700 SetLength(Textures
, Length(Textures
)+1);
701 result
:= High(Textures
);
703 with Textures
[High(Textures
)] do
705 TextureName
:= RecName
;
709 TextureID
:= TEXTURE_NONE
;
713 function CreateTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
716 TextureData
: Pointer;
717 WADName
, txname
: String;
718 a
, ResLength
: Integer;
722 if Textures
<> nil then
723 for a
:= 0 to High(Textures
) do
724 if Textures
[a
].TextureName
= RecName
then
725 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
730 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
731 if (RecName
= TEXTURE_NAME_WATER
) or
732 (RecName
= TEXTURE_NAME_ACID1
) or
733 (RecName
= TEXTURE_NAME_ACID2
) then
735 SetLength(Textures
, Length(Textures
)+1);
737 with Textures
[High(Textures
)] do
739 TextureName
:= RecName
;
741 if TextureName
= TEXTURE_NAME_WATER
then
742 TextureID
:= TEXTURE_SPECIAL_WATER
744 if TextureName
= TEXTURE_NAME_ACID1
then
745 TextureID
:= TEXTURE_SPECIAL_ACID1
747 if TextureName
= TEXTURE_NAME_ACID2
then
748 TextureID
:= TEXTURE_SPECIAL_ACID2
;
753 result
:= High(Textures
);
757 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
758 WADName
:= g_ExtractWadName(RecName
);
760 WAD
:= TWADFile
.Create();
762 if WADName
<> '' then
763 WADName
:= GameDir
+'/wads/'+WADName
767 WAD
.ReadFile(WADName
);
771 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
773 FreeMem(TextureData);
774 RecName := 'COMMON\ALIEN';
778 if WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureData
, ResLength
) then
780 SetLength(Textures
, Length(Textures
)+1);
781 if not e_CreateTextureMem(TextureData
, ResLength
, Textures
[High(Textures
)].TextureID
) then
783 e_GetTextureSize(Textures
[High(Textures
)].TextureID
,
784 @Textures
[High(Textures
)].Width
,
785 @Textures
[High(Textures
)].Height
);
786 FreeMem(TextureData
);
787 Textures
[High(Textures
)].TextureName
:= {RecName}txname
;
788 Textures
[High(Textures
)].Anim
:= False;
790 result
:= High(Textures
);
792 else // Íåò òàêîãî ðåóñðñà â WAD'å
794 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
797 e_WriteLog(Format('Error loading texture %s', [RecName
]), MSG_WARNING
);
798 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
805 function CreateAnimTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
808 TextureWAD
: PChar = nil;
809 TextData
: Pointer = nil;
810 TextureData
: Pointer = nil;
814 TextureResource
: String;
815 _width
, _height
, _framecount
, _speed
: Integer;
816 _backanimation
: Boolean;
818 ia
: TDynImageDataArray
= nil;
819 f
, c
, frdelay
, frloop
: Integer;
823 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
825 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
826 WADName
:= g_ExtractWadName(RecName
);
828 WAD
:= TWADFile
.Create();
830 if WADName
<> '' then
831 WADName
:= GameDir
+'/wads/'+WADName
835 WAD
.ReadFile(WADName
);
837 if not WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureWAD
, ResLength
) then
841 e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
842 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
848 if WADName = Map then
850 //FreeMem(TextureWAD);
851 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
857 if ResLength
< 6 then
859 e_WriteLog(Format('Animated texture file "%s" too short', [RecName
]), MSG_WARNING
);
863 // ýòî ïòèöà? ýòî ñàìîë¸ò?
864 if (TextureWAD
[0] = 'D') and (TextureWAD
[1] = 'F') and
865 (TextureWAD
[2] = 'W') and (TextureWAD
[3] = 'A') and (TextureWAD
[4] = 'D') then
867 // íåò, ýòî ñóïåðìåí!
868 if not WAD
.ReadMemory(TextureWAD
, ResLength
) then
870 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName
]), MSG_WARNING
);
874 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
875 if not WAD
.GetResource('TEXT/ANIM', TextData
, ResLength
) then
877 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName
]), MSG_WARNING
);
881 cfg
:= TConfig
.CreateMem(TextData
, ResLength
);
883 TextureResource
:= cfg
.ReadStr('', 'resource', '');
884 if TextureResource
= '' then
886 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName
]), MSG_WARNING
);
890 _width
:= cfg
.ReadInt('', 'framewidth', 0);
891 _height
:= cfg
.ReadInt('', 'frameheight', 0);
892 _framecount
:= cfg
.ReadInt('', 'framecount', 0);
893 _speed
:= cfg
.ReadInt('', 'waitcount', 0);
894 _backanimation
:= cfg
.ReadBool('', 'backanimation', False);
899 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
900 if not WAD
.GetResource('TEXTURES/'+TextureResource
, TextureData
, ResLength
) then
902 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName
, 'TEXTURES/'+TextureResource
]), MSG_WARNING
);
909 SetLength(Textures
, Length(Textures
)+1);
910 with Textures
[High(Textures
)] do
912 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
913 if g_Frames_CreateMemory(@FramesID
, '', TextureData
, ResLength
, _width
, _height
, _framecount
, _backanimation
) then
915 TextureName
:= RecName
;
919 FramesCount
:= _framecount
;
921 result
:= High(Textures
);
925 if log
then e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
931 // try animated image
933 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
934 if length(imgfmt) = 0 then
936 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
940 GlobalMetadata
.ClearMetaItems();
941 GlobalMetadata
.ClearMetaItemsForSaving();
942 if not LoadMultiImageFromMemory(TextureWAD
, ResLength
, ia
) then
944 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName
]), MSG_WARNING
);
947 if length(ia
) = 0 then
949 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName
]), MSG_WARNING
);
956 _width
:= ia
[0].width
;
957 _height
:= ia
[0].height
;
958 _framecount
:= length(ia
);
960 _backanimation
:= false;
963 if GlobalMetadata
.HasMetaItem(SMetaFrameDelay
) then
965 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
967 f
:= GlobalMetadata
.MetaItems
[SMetaFrameDelay
];
969 if f
< 0 then f
:= 0;
972 if c
< 13 then c
:= 0 else c
:= 1;
974 if f
< 1 then f
:= 1 else if f
> 255 then f
:= 255;
979 if GlobalMetadata
.HasMetaItem(SMetaAnimationLoops
) then
981 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
983 f
:= GlobalMetadata
.MetaItems
[SMetaAnimationLoops
];
985 if f
<> 0 then _backanimation
:= true; // non-infinite looping == forth-and-back
989 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
990 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
991 f
:= ord(_backanimation
);
992 e_WriteLog(Format('Animated texture file "%s": %d frames (delay:%d; back:%d; frdelay:%d; frloop:%d), %dx%d', [RecName
, length(ia
), _speed
, f
, frdelay
, frloop
, _width
, _height
]), MSG_NOTIFY
);
994 SetLength(Textures
, Length(Textures
)+1);
995 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
996 if g_CreateFramesImg(ia
, @Textures
[High(Textures
)].FramesID
, '', _backanimation
) then
998 Textures
[High(Textures
)].TextureName
:= RecName
;
999 Textures
[High(Textures
)].Width
:= _width
;
1000 Textures
[High(Textures
)].Height
:= _height
;
1001 Textures
[High(Textures
)].Anim
:= True;
1002 Textures
[High(Textures
)].FramesCount
:= length(ia
);
1003 Textures
[High(Textures
)].Speed
:= _speed
;
1004 result
:= High(Textures
);
1005 //writeln(' CREATED!');
1009 if log
then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName
]), MSG_WARNING
);
1013 for f
:= 0 to High(ia
) do FreeImage(ia
[f
]);
1016 if TextureWAD
<> nil then FreeMem(TextureWAD
);
1017 if TextData
<> nil then FreeMem(TextData
);
1018 if TextureData
<> nil then FreeMem(TextureData
);
1022 procedure CreateItem(Item
: TItemRec_1
);
1024 if g_Game_IsClient
then Exit
;
1026 if (not (gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
])) and
1027 ByteBool(Item
.Options
and ITEM_OPTION_ONLYDM
) then
1030 g_Items_Create(Item
.X
, Item
.Y
, Item
.ItemType
, ByteBool(Item
.Options
and ITEM_OPTION_FALL
),
1031 gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
, GM_COOP
]);
1034 procedure CreateArea(Area
: TAreaRec_1
);
1039 case Area
.AreaType
of
1040 AREA_DMPOINT
, AREA_PLAYERPOINT1
, AREA_PLAYERPOINT2
,
1041 AREA_REDTEAMPOINT
, AREA_BLUETEAMPOINT
:
1043 SetLength(RespawnPoints
, Length(RespawnPoints
)+1);
1044 with RespawnPoints
[High(RespawnPoints
)] do
1048 Direction
:= TDirection(Area
.Direction
);
1050 case Area
.AreaType
of
1051 AREA_DMPOINT
: PointType
:= RESPAWNPOINT_DM
;
1052 AREA_PLAYERPOINT1
: PointType
:= RESPAWNPOINT_PLAYER1
;
1053 AREA_PLAYERPOINT2
: PointType
:= RESPAWNPOINT_PLAYER2
;
1054 AREA_REDTEAMPOINT
: PointType
:= RESPAWNPOINT_RED
;
1055 AREA_BLUETEAMPOINT
: PointType
:= RESPAWNPOINT_BLUE
;
1060 AREA_REDFLAG
, AREA_BLUEFLAG
:
1062 if Area
.AreaType
= AREA_REDFLAG
then a
:= FLAG_RED
else a
:= FLAG_BLUE
;
1064 if FlagPoints
[a
] <> nil then Exit
;
1068 with FlagPoints
[a
]^ do
1070 X
:= Area
.X
-FLAGRECT
.X
;
1071 Y
:= Area
.Y
-FLAGRECT
.Y
;
1072 Direction
:= TDirection(Area
.Direction
);
1078 FLAG_RED
: g_Frames_Get(id
, 'FRAMES_FLAG_RED');
1079 FLAG_BLUE
: g_Frames_Get(id
, 'FRAMES_FLAG_BLUE');
1082 Animation
:= TAnimation
.Create(id
, True, 8);
1083 Obj
.Rect
:= FLAGRECT
;
1091 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1092 with DOMFlagPoints[High(DOMFlagPoints)] do
1096 Direction := TDirection(Area.Direction);
1099 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1104 procedure CreateTrigger(Trigger
: TTriggerRec_1
; fTexturePanel1Type
, fTexturePanel2Type
: Word);
1108 if g_Game_IsClient
and not (Trigger
.TriggerType
in [TRIGGER_SOUND
, TRIGGER_MUSIC
]) then Exit
;
1114 Width
:= Trigger
.Width
;
1115 Height
:= Trigger
.Height
;
1116 Enabled
:= ByteBool(Trigger
.Enabled
);
1117 TexturePanel
:= Trigger
.TexturePanel
;
1118 TexturePanelType
:= fTexturePanel1Type
;
1119 ShotPanelType
:= fTexturePanel2Type
;
1120 TriggerType
:= Trigger
.TriggerType
;
1121 ActivateType
:= Trigger
.ActivateType
;
1122 Keys
:= Trigger
.Keys
;
1123 Data
.Default
:= Trigger
.DATA
;
1126 g_Triggers_Create(_trigger
);
1129 procedure CreateMonster(monster
: TMonsterRec_1
);
1134 if g_Game_IsClient
then Exit
;
1136 if (gGameSettings
.GameType
= GT_SINGLE
)
1137 or LongBool(gGameSettings
.Options
and GAME_OPTION_MONSTERS
) then
1139 mon
:= g_Monsters_Create(monster
.MonsterType
, monster
.X
, monster
.Y
, TDirection(monster
.Direction
));
1141 if gTriggers
<> nil then
1143 for a
:= 0 to High(gTriggers
) do
1145 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1147 if (gTriggers
[a
].Data
.MonsterID
-1) = mon
.StartID
then mon
.AddTrigger(a
);
1152 if monster
.MonsterType
<> MONSTER_BARREL
then Inc(gTotalMonsters
);
1156 procedure g_Map_ReAdd_DieTriggers();
1158 function monsDieTrig (mon
: TMonster
): Boolean;
1162 result
:= false; // don't stop
1163 mon
.ClearTriggers();
1164 for a
:= 0 to High(gTriggers
) do
1166 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1168 if (gTriggers
[a
].Data
.MonsterID
-1) = mon
.StartID
then mon
.AddTrigger(a
);
1174 if g_Game_IsClient
then Exit
;
1176 g_Mons_ForEach(monsDieTrig
);
1179 function extractWadName(resourceName
: string): string;
1183 posN
:= Pos(':', resourceName
);
1185 Result
:= Copy(resourceName
, 0, posN
-1)
1190 procedure addResToExternalResList(res
: string);
1192 res
:= extractWadName(res
);
1193 if (res
<> '') and (gExternalResources
.IndexOf(res
) = -1) then
1194 gExternalResources
.Add(res
);
1197 procedure generateExternalResourcesList(mapReader
: TMapReader_1
);
1199 textures
: TTexturesRec1Array
;
1200 mapHeader
: TMapHeaderRec_1
;
1202 resFile
: String = '';
1204 if gExternalResources
= nil then
1205 gExternalResources
:= TStringList
.Create
;
1207 gExternalResources
.Clear
;
1208 textures
:= mapReader
.GetTextures();
1209 for i
:= 0 to High(textures
) do
1211 addResToExternalResList(resFile
);
1216 mapHeader
:= mapReader
.GetMapHeader
;
1218 addResToExternalResList(mapHeader
.MusicName
);
1219 addResToExternalResList(mapHeader
.SkyName
);
1222 procedure mapCreateGrid ();
1224 mapX0
: Integer = $3fffffff;
1225 mapY0
: Integer = $3fffffff;
1226 mapX1
: Integer = -$3fffffff;
1227 mapY1
: Integer = -$3fffffff;
1229 procedure calcBoundingBox (constref panels
: TPanelArray
);
1234 for idx
:= 0 to High(panels
) do
1237 if not pan
.visvalid
then continue
;
1238 if (pan
.Width
< 1) or (pan
.Height
< 1) then continue
;
1239 if (mapX0
> pan
.x0
) then mapX0
:= pan
.x0
;
1240 if (mapY0
> pan
.y0
) then mapY0
:= pan
.y0
;
1241 if (mapX1
< pan
.x1
) then mapX1
:= pan
.x1
;
1242 if (mapY1
< pan
.y1
) then mapY1
:= pan
.y1
;
1246 procedure addPanelsToGrid (constref panels
: TPanelArray
; tag
: Integer);
1251 tag
:= panelTypeToTag(tag
);
1252 for idx
:= High(panels
) downto 0 do
1256 if not pan
.visvalid
then continue
;
1257 pan
.proxyId
:= mapGrid
.insertBody(pan
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
, tag
);
1258 mapGrid
.proxyEnabled
[pan
.proxyId
] := pan
.Enabled
;
1259 //mapTree.insertObject(pan, tag, true); // as static object
1269 calcBoundingBox(gWalls
);
1270 calcBoundingBox(gRenderBackgrounds
);
1271 calcBoundingBox(gRenderForegrounds
);
1272 calcBoundingBox(gWater
);
1273 calcBoundingBox(gAcid1
);
1274 calcBoundingBox(gAcid2
);
1275 calcBoundingBox(gSteps
);
1276 calcBoundingBox(gLifts
);
1277 calcBoundingBox(gBlockMon
);
1279 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0
, mapY0
, mapX1
, mapY1
]), MSG_WARNING
);
1281 mapGrid
:= TPanelGrid
.Create(mapX0
-512, mapY0
-512, mapX1
-mapX0
+1+512*2, mapY1
-mapY0
+1+512*2);
1282 //mapTree := TDynAABBTreeMap.Create();
1284 addPanelsToGrid(gWalls
, PANEL_WALL
);
1285 addPanelsToGrid(gWalls
, PANEL_CLOSEDOOR
);
1286 addPanelsToGrid(gWalls
, PANEL_OPENDOOR
);
1287 addPanelsToGrid(gRenderBackgrounds
, PANEL_BACK
);
1288 addPanelsToGrid(gRenderForegrounds
, PANEL_FORE
);
1289 addPanelsToGrid(gWater
, PANEL_WATER
);
1290 addPanelsToGrid(gAcid1
, PANEL_ACID1
);
1291 addPanelsToGrid(gAcid2
, PANEL_ACID2
);
1292 addPanelsToGrid(gSteps
, PANEL_STEP
);
1293 addPanelsToGrid(gLifts
, PANEL_LIFTUP
); // it doesn't matter which LIFT type is used here
1294 addPanelsToGrid(gBlockMon
, PANEL_BLOCKMON
);
1296 mapGrid
.dumpStats();
1297 //e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1298 //mapTree.forEachLeaf(nil);
1301 function g_Map_Load(Res
: String): Boolean;
1303 DefaultMusRes
= 'Standart.wad:STDMUS\MUS1';
1304 DefaultSkyRes
= 'Standart.wad:STDSKY\SKY0';
1307 MapReader
: TMapReader_1
;
1308 Header
: TMapHeaderRec_1
;
1309 _textures
: TTexturesRec1Array
;
1310 _texnummap
: array of Integer; // `_textures` -> `Textures`
1311 panels
: TPanelsRec1Array
;
1312 items
: TItemsRec1Array
;
1313 monsters
: TMonsterRec1Array
;
1314 areas
: TAreasRec1Array
;
1315 triggers
: TTriggersRec1Array
;
1316 a
, b
, c
, k
: Integer;
1318 AddTextures
: TAddTextureArray
;
1319 texture
: TTextureRec_1
;
1320 TriggersTable
: Array of record
1321 TexturePanel
: Integer;
1326 FileName
, mapResName
, s
, TexName
: String;
1329 ok
, isAnim
, trigRef
: Boolean;
1330 CurTex
, ntn
: Integer;
1339 gMapInfo
.Map
:= Res
;
1340 TriggersTable
:= nil;
1341 FillChar(texture
, SizeOf(texture
), 0);
1343 sfsGCDisable(); // temporary disable removing of temporary volumes
1346 FileName
:= g_ExtractWadName(Res
);
1347 e_WriteLog('Loading map WAD: '+FileName
, MSG_NOTIFY
);
1348 g_Game_SetLoadingText(_lc
[I_LOAD_WAD_FILE
], 0, False);
1350 WAD
:= TWADFile
.Create();
1351 if not WAD
.ReadFile(FileName
) then
1353 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_WAD
], [FileName
]));
1357 //k8: why loader ignores path here?
1358 mapResName
:= g_ExtractFileName(Res
);
1359 if not WAD
.GetMapResource(mapResName
, Data
, Len
) then
1361 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_RES
], [mapResName
]));
1369 e_WriteLog('Loading map: '+mapResName
, MSG_NOTIFY
);
1370 g_Game_SetLoadingText(_lc
[I_LOAD_MAP
], 0, False);
1371 MapReader
:= TMapReader_1
.Create();
1373 if not MapReader
.LoadMap(Data
) then
1375 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]));
1382 generateExternalResourcesList(MapReader
);
1383 // Çàãðóçêà òåêñòóð:
1384 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], 0, False);
1385 _textures
:= MapReader
.GetTextures();
1388 // Äîáàâëåíèå òåêñòóð â Textures[]:
1389 if _textures
<> nil then
1391 e_WriteLog(' Loading textures:', MSG_NOTIFY
);
1392 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], High(_textures
), False);
1393 SetLength(_texnummap
, length(_textures
));
1395 for a
:= 0 to High(_textures
) do
1398 CopyMemory(@s
[1], @_textures
[a
].Resource
[0], 64);
1399 for b
:= 1 to Length(s
) do
1405 e_WriteLog(Format(' Loading texture #%d: %s', [a
, s
]), MSG_NOTIFY
);
1406 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1407 // Àíèìèðîâàííàÿ òåêñòóðà:
1408 if ByteBool(_textures
[a
].Anim
) then
1410 ntn
:= CreateAnimTexture(_textures
[a
].Resource
, FileName
, True);
1413 g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_ANIM
], [s
]));
1414 ntn
:= CreateNullTexture(_textures
[a
].Resource
);
1417 else // Îáû÷íàÿ òåêñòóðà:
1418 ntn
:= CreateTexture(_textures
[a
].Resource
, FileName
, True);
1421 g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_SIMPLE
], [s
]));
1422 ntn
:= CreateNullTexture(_textures
[a
].Resource
);
1425 _texnummap
[a
] := ntn
; // fix texture number
1426 g_Game_StepLoading();
1430 // Çàãðóçêà òðèããåðîâ:
1431 gTriggerClientID
:= 0;
1432 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1433 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS
], 0, False);
1434 triggers
:= MapReader
.GetTriggers();
1436 // Çàãðóçêà ïàíåëåé:
1437 e_WriteLog(' Loading panels...', MSG_NOTIFY
);
1438 g_Game_SetLoadingText(_lc
[I_LOAD_PANELS
], 0, False);
1439 panels
:= MapReader
.GetPanels();
1441 // check texture numbers for panels
1442 for a
:= 0 to High(panels
) do
1444 if panels
[a
].TextureNum
> High(_textures
) then
1446 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR
);
1450 panels
[a
].TextureNum
:= _texnummap
[panels
[a
].TextureNum
];
1453 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1454 if triggers
<> nil then
1456 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY
);
1457 SetLength(TriggersTable
, Length(triggers
));
1458 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS_TABLE
], High(TriggersTable
), False);
1460 for a
:= 0 to High(TriggersTable
) do
1462 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1463 TriggersTable
[a
].TexturePanel
:= triggers
[a
].TexturePanel
;
1465 if triggers
[a
].TriggerType
in [TRIGGER_LIFTUP
, TRIGGER_LIFTDOWN
, TRIGGER_LIFT
] then
1466 TriggersTable
[a
].LiftPanel
:= TTriggerData(triggers
[a
].DATA
).PanelID
1468 TriggersTable
[a
].LiftPanel
:= -1;
1470 if triggers
[a
].TriggerType
in [TRIGGER_OPENDOOR
,
1471 TRIGGER_CLOSEDOOR
, TRIGGER_DOOR
, TRIGGER_DOOR5
,
1472 TRIGGER_CLOSETRAP
, TRIGGER_TRAP
] then
1473 TriggersTable
[a
].DoorPanel
:= TTriggerData(triggers
[a
].DATA
).PanelID
1475 TriggersTable
[a
].DoorPanel
:= -1;
1477 if triggers
[a
].TriggerType
= TRIGGER_SHOT
then
1478 TriggersTable
[a
].ShotPanel
:= TTriggerData(triggers
[a
].DATA
).ShotPanelID
1480 TriggersTable
[a
].ShotPanel
:= -1;
1482 g_Game_StepLoading();
1487 if panels
<> nil then
1489 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY
);
1490 g_Game_SetLoadingText(_lc
[I_LOAD_LINK_TRIGGERS
], High(panels
), False);
1492 for a
:= 0 to High(panels
) do
1494 SetLength(AddTextures
, 0);
1497 if _textures
<> nil then
1499 texture
:= _textures
[panels
[a
].TextureNum
];
1507 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1508 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1510 if (TriggersTable
<> nil) and (_textures
<> nil) then
1511 for b
:= 0 to High(TriggersTable
) do
1512 if (TriggersTable
[b
].TexturePanel
= a
)
1513 or (TriggersTable
[b
].ShotPanel
= a
) then
1522 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1524 CopyMemory(@s
[1], @texture
.Resource
[0], 64);
1527 for c
:= Len
downto 1 do
1535 // Ñïåö-òåêñòóðû çàïðåùåíû:
1536 if g_Map_IsSpecialTexture(s
) then
1539 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1540 ok
:= g_Texture_NumNameFindStart(s
);
1542 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1543 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1546 k
:= NNF_NAME_BEFORE
;
1547 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1548 while ok
or (k
= NNF_NAME_BEFORE
) or
1549 (k
= NNF_NAME_EQUALS
) do
1551 k
:= g_Texture_NumNameFindNext(TexName
);
1553 if (k
= NNF_NAME_BEFORE
) or
1554 (k
= NNF_NAME_AFTER
) then
1556 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1557 if ByteBool(texture
.Anim
) then
1558 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1560 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1562 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1564 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1568 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1570 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1572 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1574 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1578 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1581 for c
:= 0 to High(Textures
) do
1582 if Textures
[c
].TextureName
= TexName
then
1584 SetLength(AddTextures
, Length(AddTextures
)+1);
1585 AddTextures
[High(AddTextures
)].Texture
:= c
;
1586 AddTextures
[High(AddTextures
)].Anim
:= isAnim
;
1592 if k
= NNF_NAME_EQUALS
then
1594 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1595 SetLength(AddTextures
, Length(AddTextures
)+1);
1596 AddTextures
[High(AddTextures
)].Texture
:= panels
[a
].TextureNum
;
1597 AddTextures
[High(AddTextures
)].Anim
:= ByteBool(texture
.Anim
);
1598 CurTex
:= High(AddTextures
);
1606 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1607 end; // if ok - ññûëàþòñÿ òðèããåðû
1611 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1612 SetLength(AddTextures
, 1);
1613 AddTextures
[0].Texture
:= panels
[a
].TextureNum
;
1614 AddTextures
[0].Anim
:= ByteBool(texture
.Anim
);
1618 //e_WriteLog(Format('panel #%d: TextureNum=%d; ht=%d; ht1=%d; atl=%d', [a, panels[a].TextureNum, High(_textures), High(Textures), High(AddTextures)]), MSG_NOTIFY);
1620 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1621 PanelID
:= CreatePanel(panels
[a
], AddTextures
, CurTex
, trigRef
);
1623 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1624 if TriggersTable
<> nil then
1625 for b
:= 0 to High(TriggersTable
) do
1627 // Òðèããåð äâåðè/ëèôòà:
1628 if (TriggersTable
[b
].LiftPanel
= a
) or
1629 (TriggersTable
[b
].DoorPanel
= a
) then
1630 TTriggerData(triggers
[b
].DATA
).PanelID
:= PanelID
;
1631 // Òðèããåð ñìåíû òåêñòóðû:
1632 if TriggersTable
[b
].TexturePanel
= a
then
1633 triggers
[b
].TexturePanel
:= PanelID
;
1634 // Òðèããåð "Òóðåëü":
1635 if TriggersTable
[b
].ShotPanel
= a
then
1636 TTriggerData(triggers
[b
].DATA
).ShotPanelID
:= PanelID
;
1639 g_Game_StepLoading();
1643 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1644 if (triggers
<> nil) and not gLoadGameMode
then
1646 e_WriteLog(' Creating triggers...', MSG_NOTIFY
);
1647 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_TRIGGERS
], 0, False);
1648 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1649 for a
:= 0 to High(triggers
) do
1651 if triggers
[a
].TexturePanel
<> -1 then
1652 b
:= panels
[TriggersTable
[a
].TexturePanel
].PanelType
1655 if (triggers
[a
].TriggerType
= TRIGGER_SHOT
) and
1656 (TTriggerData(triggers
[a
].DATA
).ShotPanelID
<> -1) then
1657 c
:= panels
[TriggersTable
[a
].ShotPanel
].PanelType
1660 CreateTrigger(triggers
[a
], b
, c
);
1664 // Çàãðóçêà ïðåäìåòîâ:
1665 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1666 g_Game_SetLoadingText(_lc
[I_LOAD_ITEMS
], 0, False);
1667 items
:= MapReader
.GetItems();
1669 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1670 if (items
<> nil) and not gLoadGameMode
then
1672 e_WriteLog(' Spawning items...', MSG_NOTIFY
);
1673 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_ITEMS
], 0, False);
1674 for a
:= 0 to High(items
) do
1675 CreateItem(Items
[a
]);
1678 // Çàãðóçêà îáëàñòåé:
1679 e_WriteLog(' Loading areas...', MSG_NOTIFY
);
1680 g_Game_SetLoadingText(_lc
[I_LOAD_AREAS
], 0, False);
1681 areas
:= MapReader
.GetAreas();
1683 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1684 if areas
<> nil then
1686 e_WriteLog(' Creating areas...', MSG_NOTIFY
);
1687 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_AREAS
], 0, False);
1688 for a
:= 0 to High(areas
) do
1689 CreateArea(areas
[a
]);
1692 // Çàãðóçêà ìîíñòðîâ:
1693 e_WriteLog(' Loading monsters...', MSG_NOTIFY
);
1694 g_Game_SetLoadingText(_lc
[I_LOAD_MONSTERS
], 0, False);
1695 monsters
:= MapReader
.GetMonsters();
1697 gTotalMonsters
:= 0;
1699 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1700 if (monsters
<> nil) and not gLoadGameMode
then
1702 e_WriteLog(' Spawning monsters...', MSG_NOTIFY
);
1703 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_MONSTERS
], 0, False);
1704 for a
:= 0 to High(monsters
) do
1705 CreateMonster(monsters
[a
]);
1708 // Çàãðóçêà îïèñàíèÿ êàðòû:
1709 e_WriteLog(' Reading map info...', MSG_NOTIFY
);
1710 g_Game_SetLoadingText(_lc
[I_LOAD_MAP_HEADER
], 0, False);
1711 Header
:= MapReader
.GetMapHeader();
1717 Name
:= Header
.MapName
;
1718 Description
:= Header
.MapDescription
;
1719 Author
:= Header
.MapAuthor
;
1720 MusicName
:= Header
.MusicName
;
1721 SkyName
:= Header
.SkyName
;
1722 Height
:= Header
.Height
;
1723 Width
:= Header
.Width
;
1727 if gMapInfo
.SkyName
<> '' then
1729 e_WriteLog(' Loading sky: ' + gMapInfo
.SkyName
, MSG_NOTIFY
);
1730 g_Game_SetLoadingText(_lc
[I_LOAD_SKY
], 0, False);
1731 FileName
:= g_ExtractWadName(gMapInfo
.SkyName
);
1733 if FileName
<> '' then
1734 FileName
:= GameDir
+'/wads/'+FileName
1737 FileName
:= g_ExtractWadName(Res
);
1740 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.SkyName
);
1741 if g_Texture_CreateWAD(BackID
, s
) then
1743 g_Game_SetupScreenSize();
1746 g_FatalError(Format(_lc
[I_GAME_ERROR_SKY
], [s
]));
1751 if gMapInfo
.MusicName
<> '' then
1753 e_WriteLog(' Loading music: ' + gMapInfo
.MusicName
, MSG_NOTIFY
);
1754 g_Game_SetLoadingText(_lc
[I_LOAD_MUSIC
], 0, False);
1755 FileName
:= g_ExtractWadName(gMapInfo
.MusicName
);
1757 if FileName
<> '' then
1758 FileName
:= GameDir
+'/wads/'+FileName
1761 FileName
:= g_ExtractWadName(Res
);
1764 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.MusicName
);
1765 if g_Sound_CreateWADEx(gMapInfo
.MusicName
, s
, True) then
1768 g_FatalError(Format(_lc
[I_GAME_ERROR_MUSIC
], [s
]));
1771 // Îñòàëüíûå óñòàíâêè:
1779 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1780 if not gLoadGameMode
then
1783 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1789 TriggersTable
:= nil;
1792 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1793 if ok
and (not gLoadGameMode
) then
1795 gMusic
.SetByName(gMapInfo
.MusicName
);
1799 gMusic
.SetByName('');
1801 sfsGCEnable(); // enable releasing unused volumes
1804 e_WriteLog('Creating map grid', MSG_NOTIFY
);
1807 e_WriteLog('Done loading map.', MSG_NOTIFY
);
1811 function g_Map_GetMapInfo(Res
: String): TMapInfo
;
1814 MapReader
: TMapReader_1
;
1815 Header
: TMapHeaderRec_1
;
1820 FillChar(Result
, SizeOf(Result
), 0);
1821 FileName
:= g_ExtractWadName(Res
);
1823 WAD
:= TWADFile
.Create();
1824 if not WAD
.ReadFile(FileName
) then
1830 //k8: it ignores path again
1831 if not WAD
.GetMapResource(g_ExtractFileName(Res
), Data
, Len
) then
1839 MapReader
:= TMapReader_1
.Create();
1841 if not MapReader
.LoadMap(Data
) then
1843 g_Console_Add(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]), True);
1844 ZeroMemory(@Header
, SizeOf(Header
));
1845 Result
.Name
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
1846 Result
.Description
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
1850 Header
:= MapReader
.GetMapHeader();
1851 Result
.Name
:= Header
.MapName
;
1852 Result
.Description
:= Header
.MapDescription
;
1859 Result
.Author
:= Header
.MapAuthor
;
1860 Result
.Height
:= Header
.Height
;
1861 Result
.Width
:= Header
.Width
;
1864 function g_Map_GetMapsList(WADName
: string): SArray
;
1871 WAD
:= TWADFile
.Create();
1872 if not WAD
.ReadFile(WADName
) then
1877 ResList
:= WAD
.GetMapResources();
1878 if ResList
<> nil then
1880 for a
:= 0 to High(ResList
) do
1882 SetLength(Result
, Length(Result
)+1);
1883 Result
[High(Result
)] := ResList
[a
];
1889 function g_Map_Exist(Res
: string): Boolean;
1892 FileName
, mnn
: string;
1898 FileName
:= addWadExtension(g_ExtractWadName(Res
));
1900 WAD
:= TWADFile
.Create
;
1901 if not WAD
.ReadFile(FileName
) then
1907 ResList
:= WAD
.GetMapResources();
1910 mnn
:= g_ExtractFileName(Res
);
1911 if ResList
<> nil then
1912 for a
:= 0 to High(ResList
) do if StrEquCI1251(ResList
[a
], mnn
) then
1919 procedure g_Map_Free();
1923 procedure FreePanelArray(var panels
: TPanelArray
);
1928 if panels
<> nil then
1930 for i
:= 0 to High(panels
) do
1943 RespawnPoints
:= nil;
1944 if FlagPoints
[FLAG_RED
] <> nil then
1946 Dispose(FlagPoints
[FLAG_RED
]);
1947 FlagPoints
[FLAG_RED
] := nil;
1949 if FlagPoints
[FLAG_BLUE
] <> nil then
1951 Dispose(FlagPoints
[FLAG_BLUE
]);
1952 FlagPoints
[FLAG_BLUE
] := nil;
1954 //DOMFlagPoints := nil;
1958 if Textures
<> nil then
1960 for a
:= 0 to High(Textures
) do
1961 if not g_Map_IsSpecialTexture(Textures
[a
].TextureName
) then
1962 if Textures
[a
].Anim
then
1963 g_Frames_DeleteByID(Textures
[a
].FramesID
)
1965 if Textures
[a
].TextureID
<> TEXTURE_NONE
then
1966 e_DeleteTexture(Textures
[a
].TextureID
);
1971 FreePanelArray(gWalls
);
1972 FreePanelArray(gRenderBackgrounds
);
1973 FreePanelArray(gRenderForegrounds
);
1974 FreePanelArray(gWater
);
1975 FreePanelArray(gAcid1
);
1976 FreePanelArray(gAcid2
);
1977 FreePanelArray(gSteps
);
1978 FreePanelArray(gLifts
);
1979 FreePanelArray(gBlockMon
);
1981 if BackID
<> DWORD(-1) then
1985 e_DeleteTexture(BackID
);
1986 BackID
:= DWORD(-1);
1989 g_Game_StopAllSounds(False);
1991 g_Sound_Delete(gMapInfo
.MusicName
);
1993 gMapInfo
.Name
:= '';
1994 gMapInfo
.Description
:= '';
1995 gMapInfo
.MusicName
:= '';
1996 gMapInfo
.Height
:= 0;
1997 gMapInfo
.Width
:= 0;
2005 procedure g_Map_Update();
2011 procedure UpdatePanelArray(var panels
: TPanelArray
);
2016 if panels
<> nil then
2017 for i
:= 0 to High(panels
) do
2022 UpdatePanelArray(gWalls
);
2023 UpdatePanelArray(gRenderBackgrounds
);
2024 UpdatePanelArray(gRenderForegrounds
);
2025 UpdatePanelArray(gWater
);
2026 UpdatePanelArray(gAcid1
);
2027 UpdatePanelArray(gAcid2
);
2028 UpdatePanelArray(gSteps
);
2030 if gGameSettings
.GameMode
= GM_CTF
then
2032 for a
:= FLAG_RED
to FLAG_BLUE
do
2033 if not (gFlags
[a
].State
in [FLAG_STATE_NONE
, FLAG_STATE_CAPTURED
]) then
2036 if gFlags
[a
].Animation
<> nil then
2037 gFlags
[a
].Animation
.Update();
2039 m
:= g_Obj_Move(@Obj
, True, True);
2041 if gTime
mod (GAME_TICK
*2) <> 0 then
2044 // Ñîïðîòèâëåíèå âîçäóõà:
2045 Obj
.Vel
.X
:= z_dec(Obj
.Vel
.X
, 1);
2047 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2048 if ((Count
= 0) or ByteBool(m
and MOVE_FALLOUT
)) and g_Game_IsServer
then
2051 gFlags
[a
].CaptureTime
:= 0;
2052 if a
= FLAG_RED
then
2053 s
:= _lc
[I_PLAYER_FLAG_RED
]
2055 s
:= _lc
[I_PLAYER_FLAG_BLUE
];
2056 g_Game_Message(Format(_lc
[I_MESSAGE_FLAG_RETURN
], [AnsiUpperCase(s
)]), 144);
2058 if g_Game_IsNet
then
2059 MH_SEND_FlagEvent(FLAG_STATE_RETURNED
, a
, 0);
2066 // Èãðîê áåðåò ôëàã:
2067 if gPlayers
<> nil then
2069 j
:= Random(Length(gPlayers
)) - 1;
2071 for d
:= 0 to High(gPlayers
) do
2074 if j
> High(gPlayers
) then
2077 if gPlayers
[j
] <> nil then
2078 if gPlayers
[j
].Live
and
2079 g_Obj_Collide(@Obj
, @gPlayers
[j
].Obj
) then
2081 if gPlayers
[j
].GetFlag(a
) then
2092 procedure g_Map_DrawPanels (PanelType
: Word);
2094 procedure DrawPanels (constref panels
: TPanelArray
; drawDoors
: Boolean=False);
2098 if (panels
<> nil) then
2100 // alas, no visible set
2101 for idx
:= 0 to High(panels
) do
2103 if not (drawDoors
xor panels
[idx
].Door
) then panels
[idx
].Draw();
2110 PANEL_WALL
: DrawPanels(gWalls
);
2111 PANEL_CLOSEDOOR
: DrawPanels(gWalls
, True);
2112 PANEL_BACK
: DrawPanels(gRenderBackgrounds
);
2113 PANEL_FORE
: DrawPanels(gRenderForegrounds
);
2114 PANEL_WATER
: DrawPanels(gWater
);
2115 PANEL_ACID1
: DrawPanels(gAcid1
);
2116 PANEL_ACID2
: DrawPanels(gAcid2
);
2117 PANEL_STEP
: DrawPanels(gSteps
);
2123 procedure g_Map_CollectDrawPanels (x0
, y0
, wdt
, hgt
: Integer);
2125 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2127 result
:= false; // don't stop, ever
2128 if ((tag
and GridTagDoor
) <> 0) <> pan
.Door
then exit
;
2129 gDrawPanelList
.insert(pan
);
2134 //tagmask := panelTypeToTag(PanelType);
2136 {if gdbg_map_use_tree_draw then
2138 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2142 mapGrid
.forEachInAABB(x0
, y0
, wdt
, hgt
, checker
, (GridTagBack
or GridTagStep
or GridTagWall
or GridTagDoor
or GridTagAcid1
or GridTagAcid2
or GridTagWater
or GridTagFore
));
2144 // list will be rendered in `g_game.DrawPlayer()`
2148 procedure g_Map_DrawPanelShadowVolumes(lightX
: Integer; lightY
: Integer; radius
: Integer);
2150 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2152 result
:= false; // don't stop, ever
2153 pan
.DrawShadowVolume(lightX
, lightY
, radius
);
2157 {if gdbg_map_use_tree_draw then
2159 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2163 mapGrid
.forEachInAABB(lightX
-radius
, lightY
-radius
, radius
*2, radius
*2, checker
, (GridTagWall
or GridTagDoor
));
2168 procedure g_Map_DrawBack(dx
, dy
: Integer);
2170 if gDrawBackGround
and (BackID
<> DWORD(-1)) then
2171 e_DrawSize(BackID
, dx
, dy
, 0, False, False, gBackSize
.X
, gBackSize
.Y
)
2173 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
2176 function g_Map_CollidePanelOld(X
, Y
: Integer; Width
, Height
: Word;
2177 PanelType
: Word; b1x3
: Boolean=false): Boolean;
2183 if WordBool(PanelType
and PANEL_WALL
) then
2184 if gWalls
<> nil then
2189 if gWalls
[a
].Enabled
and
2190 g_Collide(X
, Y
, Width
, Height
,
2191 gWalls
[a
].X
, gWalls
[a
].Y
,
2192 gWalls
[a
].Width
, gWalls
[a
].Height
) then
2199 if WordBool(PanelType
and PANEL_WATER
) then
2200 if gWater
<> nil then
2205 if g_Collide(X
, Y
, Width
, Height
,
2206 gWater
[a
].X
, gWater
[a
].Y
,
2207 gWater
[a
].Width
, gWater
[a
].Height
) then
2214 if WordBool(PanelType
and PANEL_ACID1
) then
2215 if gAcid1
<> nil then
2220 if g_Collide(X
, Y
, Width
, Height
,
2221 gAcid1
[a
].X
, gAcid1
[a
].Y
,
2222 gAcid1
[a
].Width
, gAcid1
[a
].Height
) then
2229 if WordBool(PanelType
and PANEL_ACID2
) then
2230 if gAcid2
<> nil then
2235 if g_Collide(X
, Y
, Width
, Height
,
2236 gAcid2
[a
].X
, gAcid2
[a
].Y
,
2237 gAcid2
[a
].Width
, gAcid2
[a
].Height
) then
2244 if WordBool(PanelType
and PANEL_STEP
) then
2245 if gSteps
<> nil then
2250 if g_Collide(X
, Y
, Width
, Height
,
2251 gSteps
[a
].X
, gSteps
[a
].Y
,
2252 gSteps
[a
].Width
, gSteps
[a
].Height
) then
2259 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then
2260 if gLifts
<> nil then
2265 if ((WordBool(PanelType
and (PANEL_LIFTUP
)) and (gLifts
[a
].LiftType
= 0)) or
2266 (WordBool(PanelType
and (PANEL_LIFTDOWN
)) and (gLifts
[a
].LiftType
= 1)) or
2267 (WordBool(PanelType
and (PANEL_LIFTLEFT
)) and (gLifts
[a
].LiftType
= 2)) or
2268 (WordBool(PanelType
and (PANEL_LIFTRIGHT
)) and (gLifts
[a
].LiftType
= 3))) and
2269 g_Collide(X
, Y
, Width
, Height
,
2270 gLifts
[a
].X
, gLifts
[a
].Y
,
2271 gLifts
[a
].Width
, gLifts
[a
].Height
) then
2278 if WordBool(PanelType
and PANEL_BLOCKMON
) then
2279 if gBlockMon
<> nil then
2281 h
:= High(gBlockMon
);
2285 ((gBlockMon
[a
].Width
+ gBlockMon
[a
].Height
) >= 64) ) and
2286 g_Collide(X
, Y
, Width
, Height
,
2287 gBlockMon
[a
].X
, gBlockMon
[a
].Y
,
2288 gBlockMon
[a
].Width
, gBlockMon
[a
].Height
) then
2296 function g_Map_CollideLiquid_TextureOld(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2300 function checkPanels (constref panels
: TPanelArray
): Boolean;
2305 if panels
= nil then exit
;
2306 for a
:= 0 to High(panels
) do
2308 if g_Collide(X
, Y
, Width
, Height
, panels
[a
].X
, panels
[a
].Y
, panels
[a
].Width
, panels
[a
].Height
) then
2311 texid
:= panels
[a
].GetTextureID();
2318 texid
:= TEXTURE_NONE
;
2320 if not checkPanels(gWater
) then
2321 if not checkPanels(gAcid1
) then
2322 if not checkPanels(gAcid2
) then exit
;
2327 function g_Map_CollidePanel(X
, Y
: Integer; Width
, Height
: Word; PanelType
: Word; b1x3
: Boolean): Boolean;
2328 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2330 result
:= false; // don't stop, ever
2333 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2335 result := pan.Enabled;
2340 if ((tag
and GridTagLift
) <> 0) then
2343 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
2344 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
2345 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
2346 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3))) {and
2347 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2351 if ((tag
and GridTagBlockMon
) <> 0) then
2353 result
:= ((not b1x3
) or (pan
.Width
+pan
.Height
>= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2358 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2363 tagmask
: Integer = 0;
2365 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
2366 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
2367 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
2368 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
2369 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
2370 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
2371 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
2373 if (tagmask
= 0) then begin result
:= false; exit
; end; // just in case
2375 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('*solids');
2376 if gdbg_map_use_accel_coldet
then
2378 {if gdbg_map_use_tree_coldet then
2380 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2381 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2382 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2384 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2385 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2390 if (Width
= 1) and (Height
= 1) then
2392 if ((tagmask
and (GridTagLift
or GridTagBlockMon
)) <> 0) then
2395 result
:= mapGrid
.forEachAtPoint(X
, Y
, checker
, tagmask
);
2400 result
:= mapGrid
.forEachAtPoint(X
, Y
, nil, tagmask
);
2405 if ((tagmask
and (GridTagLift
or GridTagBlockMon
)) <> 0) then
2408 result
:= mapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, tagmask
);
2413 result
:= mapGrid
.forEachInAABB(X
, Y
, Width
, Height
, nil, tagmask
);
2420 result
:= g_Map_CollidePanelOld(X
, Y
, Width
, Height
, PanelType
, b1x3
);
2422 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2426 function g_Map_CollideLiquid_Texture(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2428 cctype
: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2431 // slightly different from the old code, but meh...
2432 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2434 result
:= false; // don't stop, ever
2435 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2438 0: if ((tag
and GridTagWater
) = 0) then exit
; // allowed: water
2439 1: if ((tag
and (GridTagWater
or GridTagAcid1
)) = 0) then exit
; // allowed: water, acid1
2440 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2443 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2445 texid
:= pan
.GetTextureID();
2446 // water? water has the highest priority, so stop right here
2447 if ((tag
and GridTagWater
) <> 0) then begin cctype
:= 0; result
:= true; exit
; end;
2449 if ((tag
and GridTagAcid2
) <> 0) then cctype
:= 2;
2451 if ((tag
and GridTagAcid1
) <> 0) then cctype
:= 1;
2455 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('liquids');
2456 if gdbg_map_use_accel_coldet
then
2458 texid
:= TEXTURE_NONE
;
2459 {if gdbg_map_use_tree_coldet then
2461 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2465 if (Width
= 1) and (Height
= 1) then
2467 mapGrid
.forEachAtPoint(X
, Y
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2471 mapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2478 result
:= g_Map_CollideLiquid_TextureOld(X
, Y
, Width
, Height
);
2480 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2484 procedure g_Map_EnableWall(ID
: DWORD
);
2489 g_Mark(X
, Y
, Width
, Height
, MARK_DOOR
, True);
2490 mapGrid
.proxyEnabled
[proxyId
] := true;
2492 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2496 procedure g_Map_DisableWall(ID
: DWORD
);
2501 g_Mark(X
, Y
, Width
, Height
, MARK_DOOR
, False);
2502 mapGrid
.proxyEnabled
[proxyId
] := false;
2504 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2508 procedure g_Map_SwitchTexture(PanelType
: Word; ID
: DWORD
; AnimLoop
: Byte = 0);
2513 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
2516 tp
:= gRenderForegrounds
[ID
];
2518 tp
:= gRenderBackgrounds
[ID
];
2531 tp
.NextTexture(AnimLoop
);
2532 if g_Game_IsServer
and g_Game_IsNet
then
2533 MH_SEND_PanelTexture(PanelType
, ID
, AnimLoop
);
2536 procedure g_Map_SetLift(ID
: DWORD
; t
: Integer);
2538 if gLifts
[ID
].LiftType
= t
then
2545 g_Mark(X
, Y
, Width
, Height
, MARK_LIFT
, False);
2546 //TODO: make separate lift tags, and change tag here
2548 if LiftType
= 0 then
2549 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTUP
, True)
2550 else if LiftType
= 1 then
2551 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTDOWN
, True)
2552 else if LiftType
= 2 then
2553 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTLEFT
, True)
2554 else if LiftType
= 3 then
2555 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTRIGHT
, True);
2557 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2561 function g_Map_GetPoint(PointType
: Byte; var RespawnPoint
: TRespawnPoint
): Boolean;
2564 PointsArray
: Array of TRespawnPoint
;
2567 SetLength(PointsArray
, 0);
2569 if RespawnPoints
= nil then
2572 for a
:= 0 to High(RespawnPoints
) do
2573 if RespawnPoints
[a
].PointType
= PointType
then
2575 SetLength(PointsArray
, Length(PointsArray
)+1);
2576 PointsArray
[High(PointsArray
)] := RespawnPoints
[a
];
2579 if PointsArray
= nil then
2582 RespawnPoint
:= PointsArray
[Random(Length(PointsArray
))];
2586 function g_Map_GetPointCount(PointType
: Byte): Word;
2592 if RespawnPoints
= nil then
2595 for a
:= 0 to High(RespawnPoints
) do
2596 if RespawnPoints
[a
].PointType
= PointType
then
2597 Result
:= Result
+ 1;
2600 function g_Map_HaveFlagPoints(): Boolean;
2602 Result
:= (FlagPoints
[FLAG_RED
] <> nil) and (FlagPoints
[FLAG_BLUE
] <> nil);
2605 procedure g_Map_ResetFlag(Flag
: Byte);
2607 with gFlags
[Flag
] do
2613 Direction
:= D_LEFT
;
2614 State
:= FLAG_STATE_NONE
;
2615 if FlagPoints
[Flag
] <> nil then
2617 Obj
.X
:= FlagPoints
[Flag
]^.X
;
2618 Obj
.Y
:= FlagPoints
[Flag
]^.Y
;
2619 Direction
:= FlagPoints
[Flag
]^.Direction
;
2620 State
:= FLAG_STATE_NORMAL
;
2626 procedure g_Map_DrawFlags();
2629 Mirror
: TMirrorType
;
2631 if gGameSettings
.GameMode
<> GM_CTF
then
2634 for i
:= FLAG_RED
to FLAG_BLUE
do
2636 if State
<> FLAG_STATE_CAPTURED
then
2638 if State
= FLAG_STATE_NONE
then
2641 if Direction
= D_LEFT
then
2643 Mirror
:= M_HORIZONTAL
;
2652 Animation
.Draw(Obj
.X
+dx
, Obj
.Y
+1, Mirror
);
2654 if g_debug_Frames
then
2656 e_DrawQuad(Obj
.X
+Obj
.Rect
.X
,
2658 Obj
.X
+Obj
.Rect
.X
+Obj
.Rect
.Width
-1,
2659 Obj
.Y
+Obj
.Rect
.Y
+Obj
.Rect
.Height
-1,
2665 procedure g_Map_SaveState(Var Mem
: TBinMemoryWriter
);
2672 procedure SavePanelArray(var panels
: TPanelArray
);
2674 PAMem
: TBinMemoryWriter
;
2677 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2678 PAMem
:= TBinMemoryWriter
.Create((Length(panels
)+1) * 40);
2681 while i
< Length(panels
) do
2683 if panels
[i
].SaveIt
then
2687 // Ñîõðàíÿåì ïàíåëü:
2688 panels
[i
].SaveState(PAMem
);
2693 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2694 PAMem
.SaveToMemory(Mem
);
2698 procedure SaveFlag(flag
: PFlag
);
2701 dw
:= FLAG_SIGNATURE
; // 'FLAG'
2703 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2704 Mem
.WriteByte(flag
^.RespawnType
);
2706 Mem
.WriteByte(flag
^.State
);
2707 // Íàïðàâëåíèå ôëàãà:
2708 if flag
^.Direction
= D_LEFT
then
2714 Obj_SaveState(@flag
^.Obj
, Mem
);
2718 Mem
:= TBinMemoryWriter
.Create(1024 * 1024); // 1 MB
2720 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2721 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2722 SavePanelArray(gWalls
);
2723 // Ñîõðàíÿåì ïàíåëè ôîíà:
2724 SavePanelArray(gRenderBackgrounds
);
2725 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2726 SavePanelArray(gRenderForegrounds
);
2727 // Ñîõðàíÿåì ïàíåëè âîäû:
2728 SavePanelArray(gWater
);
2729 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2730 SavePanelArray(gAcid1
);
2731 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2732 SavePanelArray(gAcid2
);
2733 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2734 SavePanelArray(gSteps
);
2735 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2736 SavePanelArray(gLifts
);
2739 ///// Ñîõðàíÿåì ìóçûêó: /////
2740 // Ñèãíàòóðà ìóçûêè:
2741 dw
:= MUSIC_SIGNATURE
; // 'MUSI'
2744 Assert(gMusic
<> nil, 'g_Map_SaveState: gMusic = nil');
2745 if gMusic
.NoMusic
then
2749 Mem
.WriteString(str
, 64);
2750 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2751 dw
:= gMusic
.GetPosition();
2753 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2754 boo
:= gMusic
.SpecPause
;
2755 Mem
.WriteBoolean(boo
);
2758 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2759 Mem
.WriteInt(gTotalMonsters
);
2762 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2763 if gGameSettings
.GameMode
= GM_CTF
then
2765 // Ôëàã Êðàñíîé êîìàíäû:
2766 SaveFlag(@gFlags
[FLAG_RED
]);
2767 // Ôëàã Ñèíåé êîìàíäû:
2768 SaveFlag(@gFlags
[FLAG_BLUE
]);
2772 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2773 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
2775 // Î÷êè Êðàñíîé êîìàíäû:
2776 Mem
.WriteSmallInt(gTeamStat
[TEAM_RED
].Goals
);
2777 // Î÷êè Ñèíåé êîìàíäû:
2778 Mem
.WriteSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
2783 procedure g_Map_LoadState(Var Mem
: TBinMemoryReader
);
2790 procedure LoadPanelArray(var panels
: TPanelArray
);
2792 PAMem
: TBinMemoryReader
;
2795 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2796 PAMem
:= TBinMemoryReader
.Create();
2797 PAMem
.LoadFromMemory(Mem
);
2799 for i
:= 0 to Length(panels
)-1 do
2800 if panels
[i
].SaveIt
then
2806 raise EBinSizeError
.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2808 // Çàãðóæàåì ïàíåëü:
2809 panels
[i
].LoadState(PAMem
);
2812 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2816 procedure LoadFlag(flag
: PFlag
);
2820 if dw
<> FLAG_SIGNATURE
then // 'FLAG'
2822 raise EBinSizeError
.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2824 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2825 Mem
.ReadByte(flag
^.RespawnType
);
2827 Mem
.ReadByte(flag
^.State
);
2828 // Íàïðàâëåíèå ôëàãà:
2831 flag
^.Direction
:= D_LEFT
2833 flag
^.Direction
:= D_RIGHT
;
2835 Obj_LoadState(@flag
^.Obj
, Mem
);
2842 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2843 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2844 LoadPanelArray(gWalls
);
2845 // Çàãðóæàåì ïàíåëè ôîíà:
2846 LoadPanelArray(gRenderBackgrounds
);
2847 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2848 LoadPanelArray(gRenderForegrounds
);
2849 // Çàãðóæàåì ïàíåëè âîäû:
2850 LoadPanelArray(gWater
);
2851 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2852 LoadPanelArray(gAcid1
);
2853 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2854 LoadPanelArray(gAcid2
);
2855 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2856 LoadPanelArray(gSteps
);
2857 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2858 LoadPanelArray(gLifts
);
2861 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2865 ///// Çàãðóæàåì ìóçûêó: /////
2866 // Ñèãíàòóðà ìóçûêè:
2868 if dw
<> MUSIC_SIGNATURE
then // 'MUSI'
2870 raise EBinSizeError
.Create('g_Map_LoadState: Wrong Music Signature');
2873 Assert(gMusic
<> nil, 'g_Map_LoadState: gMusic = nil');
2874 Mem
.ReadString(str
);
2875 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2877 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2878 Mem
.ReadBoolean(boo
);
2879 // Çàïóñêàåì ýòó ìóçûêó:
2880 gMusic
.SetByName(str
);
2881 gMusic
.SpecPause
:= boo
;
2884 gMusic
.SetPosition(dw
);
2887 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2888 Mem
.ReadInt(gTotalMonsters
);
2891 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2892 if gGameSettings
.GameMode
= GM_CTF
then
2894 // Ôëàã Êðàñíîé êîìàíäû:
2895 LoadFlag(@gFlags
[FLAG_RED
]);
2896 // Ôëàã Ñèíåé êîìàíäû:
2897 LoadFlag(@gFlags
[FLAG_BLUE
]);
2901 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2902 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
2904 // Î÷êè Êðàñíîé êîìàíäû:
2905 Mem
.ReadSmallInt(gTeamStat
[TEAM_RED
].Goals
);
2906 // Î÷êè Ñèíåé êîìàíäû:
2907 Mem
.ReadSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
2912 function g_Map_PanelForPID(PanelID
: Integer; var PanelArrayID
: Integer): PPanel
;
2917 if (PanelID
< 0) or (PanelID
> High(PanelByID
)) then Exit
;
2918 Arr
:= PanelByID
[PanelID
].PWhere
^;
2919 PanelArrayID
:= PanelByID
[PanelID
].PArrID
;
2920 Result
:= Addr(Arr
[PanelByID
[PanelID
].PArrID
]);
2924 // trace liquid, stepping by `dx` and `dy`
2925 // return last seen liquid coords, and `false` if we're started outside of the liquid
2926 function g_Map_TraceLiquid (x
, y
, dx
, dy
: Integer; out topx
, topy
: Integer): Boolean;
2928 MaskLiquid
= GridTagWater
or GridTagAcid1
or GridTagAcid2
;
2932 // started outside of the liquid?
2933 if not mapGrid
.forEachAtPoint(x
, y
, nil, MaskLiquid
) then begin result
:= false; exit
; end;
2934 if (dx
= 0) and (dy
= 0) then begin result
:= false; exit
; end; // sanity check
2940 if not mapGrid
.forEachAtPoint(x
, y
, nil, MaskLiquid
) then exit
; // out of the water, just exit