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}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
23 e_graphics
, g_basic
, MAPDEF
, g_textures
, Classes
,
24 g_phys
, wadreader
, BinEditor
, g_panel
, g_grid
, md5
, binheap
, xprofiler
, xparser
, xdynrec
;
38 PRespawnPoint
= ^TRespawnPoint
;
39 TRespawnPoint
= record
41 Direction
: TDirection
;
45 PFlagPoint
= ^TFlagPoint
;
46 TFlagPoint
= TRespawnPoint
;
54 CaptureTime
: LongWord;
55 Animation
: TAnimation
;
56 Direction
: TDirection
;
59 function g_Map_Load(Res
: String): Boolean;
60 function g_Map_GetMapInfo(Res
: String): TMapInfo
;
61 function g_Map_GetMapsList(WADName
: String): SArray
;
62 function g_Map_Exist(Res
: String): Boolean;
63 procedure g_Map_Free(freeTextures
: Boolean=true);
64 procedure g_Map_Update();
66 procedure g_Map_DrawPanels (PanelType
: Word); // unaccelerated
67 procedure g_Map_CollectDrawPanels (x0
, y0
, wdt
, hgt
: Integer);
69 procedure g_Map_DrawBack(dx
, dy
: Integer);
70 function g_Map_CollidePanel(X
, Y
: Integer; Width
, Height
: Word;
71 PanelType
: Word; b1x3
: Boolean=false): Boolean;
72 function g_Map_CollideLiquid_Texture(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
73 procedure g_Map_EnableWall(ID
: DWORD
);
74 procedure g_Map_DisableWall(ID
: DWORD
);
75 procedure g_Map_SwitchTexture(PanelType
: Word; ID
: DWORD
; AnimLoop
: Byte = 0);
76 procedure g_Map_SetLift(ID
: DWORD
; t
: Integer);
77 procedure g_Map_ReAdd_DieTriggers();
78 function g_Map_IsSpecialTexture(Texture
: String): Boolean;
80 function g_Map_GetPoint(PointType
: Byte; var RespawnPoint
: TRespawnPoint
): Boolean;
81 function g_Map_GetPointCount(PointType
: Byte): Word;
83 function g_Map_HaveFlagPoints(): Boolean;
85 procedure g_Map_ResetFlag(Flag
: Byte);
86 procedure g_Map_DrawFlags();
88 function g_Map_PanelForPID(PanelID
: Integer; var PanelArrayID
: Integer): PPanel
;
90 procedure g_Map_SaveState(Var Mem
: TBinMemoryWriter
);
91 procedure g_Map_LoadState(Var Mem
: TBinMemoryReader
);
93 procedure g_Map_DrawPanelShadowVolumes(lightX
: Integer; lightY
: Integer; radius
: Integer);
95 // returns panel or nil
96 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
97 function g_Map_traceToNearestWall (x0
, y0
, x1
, y1
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): TPanel
;
99 // returns panel or nil
100 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
101 function g_Map_traceToNearest (x0
, y0
, x1
, y1
: Integer; tag
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): TPanel
;
104 TForEachPanelCB
= function (pan
: TPanel
): Boolean; // return `true` to stop
106 function g_Map_HasAnyPanelAtPoint (x
, y
: Integer; panelType
: Word): Boolean;
107 function g_Map_PanelAtPoint (x
, y
: Integer; tagmask
: Integer=-1): TPanel
;
109 // trace liquid, stepping by `dx` and `dy`
110 // return last seen liquid coords, and `false` if we're started outside of the liquid
111 function g_Map_TraceLiquidNonPrecise (x
, y
, dx
, dy
: Integer; out topx
, topy
: Integer): Boolean;
114 procedure g_Map_ProfilersBegin ();
115 procedure g_Map_ProfilersEnd ();
118 function g_Map_ParseMap (data
: Pointer; dataLen
: Integer): TDynRecord
;
126 function g_Texture_NumNameFindStart(name
: String): Boolean;
127 function g_Texture_NumNameFindNext(var newName
: String): Byte;
130 RESPAWNPOINT_PLAYER1
= 1;
131 RESPAWNPOINT_PLAYER2
= 2;
133 RESPAWNPOINT_RED
= 4;
134 RESPAWNPOINT_BLUE
= 5;
142 FLAG_STATE_NORMAL
= 1;
143 FLAG_STATE_DROPPED
= 2;
144 FLAG_STATE_CAPTURED
= 3;
145 FLAG_STATE_SCORED
= 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
146 FLAG_STATE_RETURNED
= 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
148 FLAG_TIME
= 720; // 20 seconds
150 SKY_STRETCH
: Single = 1.5;
165 // sorted by draw priority
166 GridTagBack
= 1 shl 0;
167 GridTagStep
= 1 shl 1;
168 GridTagWall
= 1 shl 2;
169 GridTagDoor
= 1 shl 3;
170 GridTagAcid1
= 1 shl 4;
171 GridTagAcid2
= 1 shl 5;
172 GridTagWater
= 1 shl 6;
173 GridTagFore
= 1 shl 7;
174 // the following are invisible
175 GridTagLift
= 1 shl 8;
176 GridTagBlockMon
= 1 shl 9;
178 GridDrawableMask
= (GridTagBack
or GridTagStep
or GridTagWall
or GridTagDoor
or GridTagAcid1
or GridTagAcid2
or GridTagWater
or GridTagFore
);
183 gRenderBackgrounds
: TPanelArray
;
184 gRenderForegrounds
: TPanelArray
;
185 gWater
, gAcid1
, gAcid2
: TPanelArray
;
188 gBlockMon
: TPanelArray
;
189 gFlags
: array [FLAG_RED
..FLAG_BLUE
] of TFlag
;
190 //gDOMFlags: array of TFlag;
193 gDoorMap
: array of array of DWORD
;
194 gLiftMap
: array of array of DWORD
;
195 gWADHash
: TMD5Digest
;
196 BackID
: DWORD
= DWORD(-1);
197 gExternalResources
: TStringList
;
199 gdbg_map_use_accel_render
: Boolean = true;
200 gdbg_map_use_accel_coldet
: Boolean = true;
201 profMapCollision
: TProfiler
= nil; //WARNING: FOR DEBUGGING ONLY!
202 gDrawPanelList
: TBinaryHeapObj
= nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
204 gCurrentMap
: TDynRecord
= nil;
205 gCurrentMapFileName
: AnsiString = ''; // so we can skip texture reloading
208 function panelTypeToTag (panelType
: Word): Integer; // returns GridTagXXX
212 TPanelGrid
= specialize TBodyGridBase
<TPanel
>;
215 mapGrid
: TPanelGrid
= nil; // DO NOT USE! public for debugging only!
221 e_input
, g_main
, e_log
, SysUtils
, g_items
, g_gfx
, g_console
,
222 GL
, GLExt
, g_weapons
, g_game
, g_sound
, e_sound
, CONFIG
,
223 g_options
, g_triggers
, g_player
,
224 Math
, g_monsters
, g_saveload
, g_language
, g_netmsg
,
225 utils
, sfs
, xstreams
, hashtable
,
226 ImagingTypes
, Imaging
, ImagingUtility
,
227 ImagingGif
, ImagingNetworkGraphics
;
230 FLAGRECT
: TRectWH
= (X
:15; Y
:12; Width
:33; Height
:52);
231 MUSIC_SIGNATURE
= $4953554D; // 'MUSI'
232 FLAG_SIGNATURE
= $47414C46; // 'FLAG'
236 dfmapdef
: TDynMapDef
= nil;
238 procedure loadMapDefinition ();
240 pr
: TTextParser
= nil;
244 if (dfmapdef
<> nil) then exit
;
246 e_LogWritefln('parsing "mapdef.txt"...', []);
247 st
:= openDiskFileRO(DataDir
+'mapdef.txt');
250 e_LogWritefln('local "%smapdef.txt" not found', [DataDir
]);
254 WAD
:= TWADFile
.Create();
255 if not WAD
.ReadFile(GameWAD
) then
257 //raise Exception.Create('cannot load "game.wad"');
262 st
:= WAD
.openFileStream('mapdef.txt');
268 //raise Exception.Create('cannot open "mapdef.txt"');
269 e_LogWritefln('using default "mapdef.txt"...', [], MSG_WARNING
);
270 pr
:= TStrTextParser
.Create(defaultMapDef
);
274 pr
:= TFileTextParser
.Create(st
);
278 dfmapdef
:= TDynMapDef
.Create(pr
);
279 except on e
: Exception
do
280 raise Exception
.Create(Format('ERROR in "mapdef.txt" at (%s,%s): %s', [pr
.line
, pr
.col
, e
.message]));
288 function g_Map_ParseMap (data
: Pointer; dataLen
: Integer): TDynRecord
;
290 wst
: TSFSMemoryChunkStream
= nil;
291 pr
: TTextParser
= nil;
294 if (dataLen
< 4) then exit
;
296 if (dfmapdef
= nil) then raise Exception
.Create('internal map loader error');
298 wst
:= TSFSMemoryChunkStream
.Create(data
, dataLen
);
300 if (PAnsiChar(data
)[0] = 'M') and (PAnsiChar(data
)[1] = 'A') and (PAnsiChar(data
)[2] = 'P') and (PByte(data
)[3] = 1) then
304 result
:= dfmapdef
.parseBinMap(wst
);
305 except on e
: Exception
do
307 e_LogWritefln('ERROR: %s', [e
.message]);
318 pr
:= TFileTextParser
.Create(wst
);
320 result
:= dfmapdef
.parseMap(pr
);
321 except on e
: Exception
do
323 if (pr
<> nil) then e_LogWritefln('ERROR at (%s,%s): %s', [pr
.line
, pr
.col
, e
.message])
324 else e_LogWritefln('ERROR: %s', [e
.message]);
325 pr
.Free(); // will free `wst`
330 pr
.Free(); // will free `wst`
336 NNF_PureName
: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
337 NNF_FirstNum
: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
338 NNF_CurrentNum
: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
341 function g_Texture_NumNameFindStart(name
: String): Boolean;
349 NNF_CurrentNum
:= -1;
351 for i
:= Length(name
) downto 1 do
352 if (name
[i
] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
354 if i
= Length(name
) then
355 begin // Íåò öèôð â êîíöå ñòðîêè
360 NNF_PureName
:= Copy(name
, 1, i
);
366 // Íå ïåðåâåñòè â ÷èñëî:
367 if not TryStrToInt(name
, NNF_FirstNum
) then
376 function g_Texture_NumNameFindNext(var newName
: String): Byte;
378 if (NNF_PureName
= '') or (NNF_CurrentNum
< 0) then
381 Result
:= NNF_NO_NAME
;
385 newName
:= NNF_PureName
+ IntToStr(NNF_CurrentNum
);
387 if NNF_CurrentNum
< NNF_FirstNum
then
388 Result
:= NNF_NAME_BEFORE
390 if NNF_CurrentNum
> NNF_FirstNum
then
391 Result
:= NNF_NAME_AFTER
393 Result
:= NNF_NAME_EQUALS
;
399 function panelTypeToTag (panelType
: Word): Integer;
402 PANEL_WALL
: result
:= GridTagWall
; // gWalls
403 PANEL_OPENDOOR
, PANEL_CLOSEDOOR
: result
:= GridTagDoor
; // gWalls
404 PANEL_BACK
: result
:= GridTagBack
; // gRenderBackgrounds
405 PANEL_FORE
: result
:= GridTagFore
; // gRenderForegrounds
406 PANEL_WATER
: result
:= GridTagWater
; // gWater
407 PANEL_ACID1
: result
:= GridTagAcid1
; // gAcid1
408 PANEL_ACID2
: result
:= GridTagAcid2
; // gAcid2
409 PANEL_STEP
: result
:= GridTagStep
; // gSteps
410 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
: result
:= GridTagLift
; // gLifts -- this is for all lifts
411 PANEL_BLOCKMON
: result
:= GridTagBlockMon
; // gBlockMon -- this is for all blockmons
412 else result
:= GridTagInvalid
;
417 function dplLess (a
, b
: TObject
): Boolean;
423 if (pa
.tag
< pb
.tag
) then begin result
:= true; exit
; end;
424 if (pa
.tag
> pb
.tag
) then begin result
:= false; exit
; end;
425 result
:= (pa
.arrIdx
< pb
.arrIdx
);
428 procedure dplClear ();
430 if (gDrawPanelList
= nil) then gDrawPanelList
:= TBinaryHeapObj
.Create(@dplLess
) else gDrawPanelList
.clear();
436 PWhere
: ^TPanelArray
;
441 PanelById
: array of TPanelID
;
442 Textures
: TLevelTextureArray
= nil;
443 TextNameHash
: THashStrInt
= nil; // key: texture name; value: index in `Textures`
444 BadTextNameHash
: THashStrInt
= nil; // set; so we won't spam with non-existing texture messages
445 RespawnPoints
: Array of TRespawnPoint
;
446 FlagPoints
: Array [FLAG_RED
..FLAG_BLUE
] of PFlagPoint
;
447 //DOMFlagPoints: Array of TFlagPoint;
450 procedure g_Map_ProfilersBegin ();
452 if (profMapCollision
= nil) then profMapCollision
:= TProfiler
.Create('COLSOLID', g_profile_history_size
);
453 profMapCollision
.mainBegin(g_profile_collision
);
455 if g_profile_collision
then
457 profMapCollision
.sectionBegin('*solids');
458 profMapCollision
.sectionEnd();
459 profMapCollision
.sectionBegin('liquids');
460 profMapCollision
.sectionEnd();
464 procedure g_Map_ProfilersEnd ();
466 if (profMapCollision
<> nil) then profMapCollision
.mainEnd();
470 // wall index in `gWalls` or -1
471 function g_Map_traceToNearestWall (x0
, y0
, x1
, y1
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): TPanel
;
475 result
:= mapGrid
.traceRay(ex
, ey
, x0
, y0
, x1
, y1
, nil, (GridTagWall
or GridTagDoor
));
476 if (result
<> nil) then
478 if (hitx
<> nil) then hitx
^ := ex
;
479 if (hity
<> nil) then hity
^ := ey
;
483 if (hitx
<> nil) then hitx
^ := x1
;
484 if (hity
<> nil) then hity
^ := y1
;
488 // returns panel or nil
489 function g_Map_traceToNearest (x0
, y0
, x1
, y1
: Integer; tag
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): TPanel
;
493 result
:= mapGrid
.traceRay(ex
, ey
, x0
, y0
, x1
, y1
, nil, tag
);
494 if (result
<> nil) then
496 if (hitx
<> nil) then hitx
^ := ex
;
497 if (hity
<> nil) then hity
^ := ey
;
501 if (hitx
<> nil) then hitx
^ := x1
;
502 if (hity
<> nil) then hity
^ := y1
;
507 function g_Map_HasAnyPanelAtPoint (x
, y
: Integer; panelType
: Word): Boolean;
509 function checker (pan
: TPanel
; tag
: Integer): Boolean;
512 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
514 result := pan.Enabled; // stop if wall is enabled
519 if ((tag
and GridTagLift
) <> 0) then
521 // stop if the lift of the right type
523 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
524 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
525 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
526 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3)));
530 result
:= true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
534 tagmask
: Integer = 0;
538 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
539 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
540 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
541 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
542 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
543 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
544 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
546 if (tagmask
= 0) then exit
;// just in case
547 if ((tagmask
and GridTagLift
) <> 0) then
550 result
:= (mapGrid
.forEachAtPoint(x
, y
, checker
, tagmask
) <> nil);
555 result
:= (mapGrid
.forEachAtPoint(x
, y
, nil, tagmask
) <> nil);
560 function g_Map_PanelAtPoint (x
, y
: Integer; tagmask
: Integer=-1): TPanel
;
563 if (tagmask
= 0) then exit
;
564 result
:= mapGrid
.forEachAtPoint(x
, y
, nil, tagmask
);
568 function g_Map_IsSpecialTexture(Texture
: String): Boolean;
570 Result
:= (Texture
= TEXTURE_NAME_WATER
) or
571 (Texture
= TEXTURE_NAME_ACID1
) or
572 (Texture
= TEXTURE_NAME_ACID2
);
575 procedure CreateDoorMap();
577 PanelArray
: Array of record
583 a
, b
, c
, m
, i
, len
: Integer;
591 SetLength(PanelArray
, len
);
593 for a
:= 0 to High(gWalls
) do
594 if gWalls
[a
].Door
then
596 PanelArray
[i
].X
:= gWalls
[a
].X
;
597 PanelArray
[i
].Y
:= gWalls
[a
].Y
;
598 PanelArray
[i
].Width
:= gWalls
[a
].Width
;
599 PanelArray
[i
].Height
:= gWalls
[a
].Height
;
600 PanelArray
[i
].Active
:= True;
601 PanelArray
[i
].PanelID
:= a
;
607 SetLength(PanelArray
, len
);
618 SetLength(gDoorMap
, 0);
620 g_Game_SetLoadingText(_lc
[I_LOAD_DOOR_MAP
], i
-1, False);
623 if PanelArray
[a
].Active
then
625 PanelArray
[a
].Active
:= False;
626 m
:= Length(gDoorMap
);
627 SetLength(gDoorMap
, m
+1);
628 SetLength(gDoorMap
[m
], 1);
629 gDoorMap
[m
, 0] := PanelArray
[a
].PanelID
;
637 if PanelArray
[b
].Active
then
638 for c
:= 0 to High(gDoorMap
[m
]) do
639 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
640 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
641 g_CollideAround(PanelArray
[b
].X
, PanelArray
[b
].Y
,
642 PanelArray
[b
].Width
, PanelArray
[b
].Height
,
643 gWalls
[gDoorMap
[m
, c
]].X
,
644 gWalls
[gDoorMap
[m
, c
]].Y
,
645 gWalls
[gDoorMap
[m
, c
]].Width
,
646 gWalls
[gDoorMap
[m
, c
]].Height
) then
648 PanelArray
[b
].Active
:= False;
649 SetLength(gDoorMap
[m
],
650 Length(gDoorMap
[m
])+1);
651 gDoorMap
[m
, High(gDoorMap
[m
])] := PanelArray
[b
].PanelID
;
657 g_Game_StepLoading();
663 procedure CreateLiftMap();
665 PanelArray
: Array of record
670 a
, b
, c
, len
, i
, j
: Integer;
676 len
:= Length(gLifts
);
677 SetLength(PanelArray
, len
);
679 for a
:= 0 to len
-1 do
681 PanelArray
[a
].X
:= gLifts
[a
].X
;
682 PanelArray
[a
].Y
:= gLifts
[a
].Y
;
683 PanelArray
[a
].Width
:= gLifts
[a
].Width
;
684 PanelArray
[a
].Height
:= gLifts
[a
].Height
;
685 PanelArray
[a
].Active
:= True;
688 SetLength(gLiftMap
, len
);
691 g_Game_SetLoadingText(_lc
[I_LOAD_LIFT_MAP
], len
-1, False);
693 for a
:= 0 to len
-1 do
694 if PanelArray
[a
].Active
then
696 PanelArray
[a
].Active
:= False;
697 SetLength(gLiftMap
[i
], 32);
705 for b
:= 0 to len
-1 do
706 if PanelArray
[b
].Active
then
708 if g_CollideAround(PanelArray
[b
].X
,
711 PanelArray
[b
].Height
,
712 PanelArray
[gLiftMap
[i
, c
]].X
,
713 PanelArray
[gLiftMap
[i
, c
]].Y
,
714 PanelArray
[gLiftMap
[i
, c
]].Width
,
715 PanelArray
[gLiftMap
[i
, c
]].Height
) then
717 PanelArray
[b
].Active
:= False;
719 if j
> High(gLiftMap
[i
]) then
720 SetLength(gLiftMap
[i
],
721 Length(gLiftMap
[i
])+32);
730 SetLength(gLiftMap
[i
], j
+1);
733 g_Game_StepLoading();
736 SetLength(gLiftMap
, i
);
741 function CreatePanel(PanelRec
: TDynRecord
; AddTextures
: TAddTextureArray
;
742 CurTex
: Integer; sav
: Boolean): Integer;
745 panels
: ^TPanelArray
;
749 case PanelRec
.PanelType
of
750 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
753 panels
:= @gRenderBackgrounds
;
755 panels
:= @gRenderForegrounds
;
764 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
:
767 panels
:= @gBlockMon
;
772 len
:= Length(panels
^);
773 SetLength(panels
^, len
+ 1);
775 panels
^[len
] := TPanel
.Create(PanelRec
, AddTextures
, CurTex
, Textures
);
776 panels
^[len
].arrIdx
:= len
;
777 panels
^[len
].proxyId
:= -1;
778 panels
^[len
].tag
:= panelTypeToTag(PanelRec
.PanelType
);
780 panels
^[len
].SaveIt
:= True;
784 len
:= Length(PanelByID
);
785 SetLength(PanelByID
, len
+ 1);
786 PanelByID
[len
].PWhere
:= panels
;
787 PanelByID
[len
].PArrID
:= Result
;
791 function CreateNullTexture(RecName
: String): Integer;
793 RecName
:= toLowerCase1251(RecName
);
794 if (TextNameHash
= nil) then TextNameHash
:= hashNewStrInt();
795 if TextNameHash
.get(RecName
, result
) then exit
; // i found her!
797 SetLength(Textures
, Length(Textures
)+1);
798 result
:= High(Textures
);
800 with Textures
[High(Textures
)] do
802 TextureName
:= RecName
;
806 TextureID
:= LongWord(TEXTURE_NONE
);
809 TextNameHash
.put(RecName
, result
);
813 function CreateTexture(RecName
: AnsiString; Map
: string; log
: Boolean): Integer;
816 TextureData
: Pointer;
818 a
, ResLength
: Integer;
820 RecName
:= toLowerCase1251(RecName
);
821 if (TextNameHash
= nil) then TextNameHash
:= hashNewStrInt();
822 if TextNameHash
.get(RecName
, result
) then
825 //e_LogWritefln('texture ''%s'' already loaded', [RecName]);
831 if (BadTextNameHash
<> nil) and BadTextNameHash
.has(RecName
) then exit
; // don't do it again and again
834 if Textures <> nil then
836 for a := 0 to High(Textures) do
838 if (Textures[a].TextureName = RecName) then
839 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
840 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
848 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
849 if (RecName
= TEXTURE_NAME_WATER
) or
850 (RecName
= TEXTURE_NAME_ACID1
) or
851 (RecName
= TEXTURE_NAME_ACID2
) then
853 SetLength(Textures
, Length(Textures
)+1);
855 with Textures
[High(Textures
)] do
857 TextureName
:= RecName
;
858 if (TextureName
= TEXTURE_NAME_WATER
) then TextureID
:= LongWord(TEXTURE_SPECIAL_WATER
)
859 else if (TextureName
= TEXTURE_NAME_ACID1
) then TextureID
:= LongWord(TEXTURE_SPECIAL_ACID1
)
860 else if (TextureName
= TEXTURE_NAME_ACID2
) then TextureID
:= LongWord(TEXTURE_SPECIAL_ACID2
);
865 result
:= High(Textures
);
866 TextNameHash
.put(RecName
, result
);
870 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
871 WADName
:= g_ExtractWadName(RecName
);
873 WAD
:= TWADFile
.Create();
875 if WADName
<> '' then WADName
:= GameDir
+'/wads/'+WADName
else WADName
:= Map
;
877 WAD
.ReadFile(WADName
);
881 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
883 FreeMem(TextureData);
884 RecName := 'COMMON\ALIEN';
888 if WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureData
, ResLength
, log
) then
890 SetLength(Textures
, Length(Textures
)+1);
891 if not e_CreateTextureMem(TextureData
, ResLength
, Textures
[High(Textures
)].TextureID
) then
893 SetLength(Textures
, Length(Textures
)-1);
896 e_GetTextureSize(Textures
[High(Textures
)].TextureID
, @Textures
[High(Textures
)].Width
, @Textures
[High(Textures
)].Height
);
897 FreeMem(TextureData
);
898 Textures
[High(Textures
)].TextureName
:= RecName
;
899 Textures
[High(Textures
)].Anim
:= False;
901 result
:= High(Textures
);
902 TextNameHash
.put(RecName
, result
);
904 else // Íåò òàêîãî ðåóñðñà â WAD'å
906 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
907 if (BadTextNameHash
= nil) then BadTextNameHash
:= hashNewStrInt();
908 if log
and (not BadTextNameHash
.get(RecName
, a
)) then
910 e_WriteLog(Format('Error loading texture %s', [RecName
]), MSG_WARNING
);
911 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
913 BadTextNameHash
.put(RecName
, -1);
920 function CreateAnimTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
923 TextureWAD
: PChar = nil;
924 TextData
: Pointer = nil;
925 TextureData
: Pointer = nil;
929 TextureResource
: String;
930 _width
, _height
, _framecount
, _speed
: Integer;
931 _backanimation
: Boolean;
933 ia
: TDynImageDataArray
= nil;
934 f
, c
, frdelay
, frloop
: Integer;
936 RecName
:= toLowerCase1251(RecName
);
937 if (TextNameHash
= nil) then TextNameHash
:= hashNewStrInt();
938 if TextNameHash
.get(RecName
, result
) then
941 //e_LogWritefln('animated texture ''%s'' already loaded', [RecName]);
947 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
949 if (BadTextNameHash
= nil) then BadTextNameHash
:= hashNewStrInt();
950 if BadTextNameHash
.get(RecName
, f
) then
952 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
956 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
957 WADName
:= g_ExtractWadName(RecName
);
959 WAD
:= TWADFile
.Create();
961 if WADName
<> '' then WADName
:= GameDir
+'/wads/'+WADName
else WADName
:= Map
;
963 WAD
.ReadFile(WADName
);
965 if not WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureWAD
, ResLength
, log
) then
967 if (BadTextNameHash
= nil) then BadTextNameHash
:= hashNewStrInt();
968 if log
and (not BadTextNameHash
.get(RecName
, f
)) then
970 e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
971 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
973 BadTextNameHash
.put(RecName
, -1);
978 if WADName = Map then
980 //FreeMem(TextureWAD);
981 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
987 if ResLength
< 6 then
989 e_WriteLog(Format('Animated texture file "%s" too short', [RecName
]), MSG_WARNING
);
990 BadTextNameHash
.put(RecName
, -1);
994 // ýòî ïòèöà? ýòî ñàìîë¸ò?
995 if (TextureWAD
[0] = 'D') and (TextureWAD
[1] = 'F') and
996 (TextureWAD
[2] = 'W') and (TextureWAD
[3] = 'A') and (TextureWAD
[4] = 'D') then
998 // íåò, ýòî ñóïåðìåí!
999 if not WAD
.ReadMemory(TextureWAD
, ResLength
) then
1001 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName
]), MSG_WARNING
);
1002 BadTextNameHash
.put(RecName
, -1);
1006 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1007 if not WAD
.GetResource('TEXT/ANIM', TextData
, ResLength
) then
1009 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName
]), MSG_WARNING
);
1010 BadTextNameHash
.put(RecName
, -1);
1014 cfg
:= TConfig
.CreateMem(TextData
, ResLength
);
1016 TextureResource
:= cfg
.ReadStr('', 'resource', '');
1017 if TextureResource
= '' then
1019 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName
]), MSG_WARNING
);
1020 BadTextNameHash
.put(RecName
, -1);
1024 _width
:= cfg
.ReadInt('', 'framewidth', 0);
1025 _height
:= cfg
.ReadInt('', 'frameheight', 0);
1026 _framecount
:= cfg
.ReadInt('', 'framecount', 0);
1027 _speed
:= cfg
.ReadInt('', 'waitcount', 0);
1028 _backanimation
:= cfg
.ReadBool('', 'backanimation', False);
1033 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1034 if not WAD
.GetResource('TEXTURES/'+TextureResource
, TextureData
, ResLength
) then
1036 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName
, 'TEXTURES/'+TextureResource
]), MSG_WARNING
);
1037 BadTextNameHash
.put(RecName
, -1);
1044 SetLength(Textures
, Length(Textures
)+1);
1045 with Textures
[High(Textures
)] do
1047 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1048 if g_Frames_CreateMemory(@FramesID
, '', TextureData
, ResLength
, _width
, _height
, _framecount
, _backanimation
) then
1050 TextureName
:= RecName
;
1054 FramesCount
:= _framecount
;
1056 result
:= High(Textures
);
1057 TextNameHash
.put(RecName
, result
);
1061 if (BadTextNameHash
= nil) then BadTextNameHash
:= hashNewStrInt();
1062 if log
and (not BadTextNameHash
.get(RecName
, f
)) then
1064 e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
1066 BadTextNameHash
.put(RecName
, -1);
1072 // try animated image
1074 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1075 if length(imgfmt) = 0 then
1077 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1081 GlobalMetadata
.ClearMetaItems();
1082 GlobalMetadata
.ClearMetaItemsForSaving();
1083 if not LoadMultiImageFromMemory(TextureWAD
, ResLength
, ia
) then
1085 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName
]), MSG_WARNING
);
1086 BadTextNameHash
.put(RecName
, -1);
1089 if length(ia
) = 0 then
1091 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName
]), MSG_WARNING
);
1092 BadTextNameHash
.put(RecName
, -1);
1099 _width
:= ia
[0].width
;
1100 _height
:= ia
[0].height
;
1101 _framecount
:= length(ia
);
1103 _backanimation
:= false;
1106 if GlobalMetadata
.HasMetaItem(SMetaFrameDelay
) then
1108 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1110 f
:= GlobalMetadata
.MetaItems
[SMetaFrameDelay
];
1112 if f
< 0 then f
:= 0;
1115 if c
< 13 then c
:= 0 else c
:= 1;
1117 if f
< 1 then f
:= 1 else if f
> 255 then f
:= 255;
1122 if GlobalMetadata
.HasMetaItem(SMetaAnimationLoops
) then
1124 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1126 f
:= GlobalMetadata
.MetaItems
[SMetaAnimationLoops
];
1128 if f
<> 0 then _backanimation
:= true; // non-infinite looping == forth-and-back
1132 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1133 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1134 f
:= ord(_backanimation
);
1135 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
);
1137 SetLength(Textures
, Length(Textures
)+1);
1138 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1139 if g_CreateFramesImg(ia
, @Textures
[High(Textures
)].FramesID
, '', _backanimation
) then
1141 Textures
[High(Textures
)].TextureName
:= RecName
;
1142 Textures
[High(Textures
)].Width
:= _width
;
1143 Textures
[High(Textures
)].Height
:= _height
;
1144 Textures
[High(Textures
)].Anim
:= True;
1145 Textures
[High(Textures
)].FramesCount
:= length(ia
);
1146 Textures
[High(Textures
)].Speed
:= _speed
;
1147 result
:= High(Textures
);
1148 TextNameHash
.put(RecName
, result
);
1149 //writeln(' CREATED!');
1153 if (BadTextNameHash
= nil) then BadTextNameHash
:= hashNewStrInt();
1154 if log
and (not BadTextNameHash
.get(RecName
, f
)) then
1156 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName
]), MSG_WARNING
);
1158 BadTextNameHash
.put(RecName
, -1);
1162 for f
:= 0 to High(ia
) do FreeImage(ia
[f
]);
1165 if (TextureWAD
<> nil) then FreeMem(TextureWAD
);
1166 if (TextData
<> nil) then FreeMem(TextData
);
1167 if (TextureData
<> nil) then FreeMem(TextureData
);
1171 procedure CreateItem(Item
: TDynRecord
);
1173 if g_Game_IsClient
then Exit
;
1175 if (not (gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
])) and
1176 ByteBool(Item
.Options
and ITEM_OPTION_ONLYDM
) then
1179 g_Items_Create(Item
.X
, Item
.Y
, Item
.ItemType
, ByteBool(Item
.Options
and ITEM_OPTION_FALL
),
1180 gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
, GM_COOP
]);
1183 procedure CreateArea(Area
: TDynRecord
);
1188 case Area
.AreaType
of
1189 AREA_DMPOINT
, AREA_PLAYERPOINT1
, AREA_PLAYERPOINT2
,
1190 AREA_REDTEAMPOINT
, AREA_BLUETEAMPOINT
:
1192 SetLength(RespawnPoints
, Length(RespawnPoints
)+1);
1193 with RespawnPoints
[High(RespawnPoints
)] do
1197 Direction
:= TDirection(Area
.Direction
);
1199 case Area
.AreaType
of
1200 AREA_DMPOINT
: PointType
:= RESPAWNPOINT_DM
;
1201 AREA_PLAYERPOINT1
: PointType
:= RESPAWNPOINT_PLAYER1
;
1202 AREA_PLAYERPOINT2
: PointType
:= RESPAWNPOINT_PLAYER2
;
1203 AREA_REDTEAMPOINT
: PointType
:= RESPAWNPOINT_RED
;
1204 AREA_BLUETEAMPOINT
: PointType
:= RESPAWNPOINT_BLUE
;
1209 AREA_REDFLAG
, AREA_BLUEFLAG
:
1211 if Area
.AreaType
= AREA_REDFLAG
then a
:= FLAG_RED
else a
:= FLAG_BLUE
;
1213 if FlagPoints
[a
] <> nil then Exit
;
1217 with FlagPoints
[a
]^ do
1219 X
:= Area
.X
-FLAGRECT
.X
;
1220 Y
:= Area
.Y
-FLAGRECT
.Y
;
1221 Direction
:= TDirection(Area
.Direction
);
1227 FLAG_RED
: g_Frames_Get(id
, 'FRAMES_FLAG_RED');
1228 FLAG_BLUE
: g_Frames_Get(id
, 'FRAMES_FLAG_BLUE');
1231 Animation
:= TAnimation
.Create(id
, True, 8);
1232 Obj
.Rect
:= FLAGRECT
;
1240 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1241 with DOMFlagPoints[High(DOMFlagPoints)] do
1245 Direction := TDirection(Area.Direction);
1248 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1253 procedure CreateTrigger (amapIdx
: Integer; Trigger
: TDynRecord
; atpanid
, atrigpanid
: Integer; fTexturePanel1Type
, fTexturePanel2Type
: Word);
1257 if g_Game_IsClient
and not (Trigger
.TriggerType
in [TRIGGER_SOUND
, TRIGGER_MUSIC
]) then Exit
;
1261 mapId
:= Trigger
.id
;
1262 mapIndex
:= amapIdx
;
1265 Width
:= Trigger
.Width
;
1266 Height
:= Trigger
.Height
;
1267 Enabled
:= Trigger
.Enabled
;
1268 //TexturePanel := Trigger.TexturePanel;
1269 TexturePanel
:= atpanid
;
1270 TexturePanelType
:= fTexturePanel1Type
;
1271 ShotPanelType
:= fTexturePanel2Type
;
1272 TriggerType
:= Trigger
.TriggerType
;
1273 ActivateType
:= Trigger
.ActivateType
;
1274 Keys
:= Trigger
.Keys
;
1275 trigPanelId
:= atrigpanid
;
1276 //trigShotPanelId := ashotpanid;
1277 //Data.Default := Trigger.DATA;
1278 if (Trigger
.trigRec
= nil) then
1281 if (TriggerType
<> TRIGGER_SECRET
) then
1283 e_LogWritefln('trigger of type %s has no triggerdata; wtf?!', [TriggerType
], MSG_WARNING
);
1288 trigData
:= Trigger
.trigRec
.clone();
1292 g_Triggers_Create(_trigger
);
1295 procedure CreateMonster(monster
: TDynRecord
);
1300 if g_Game_IsClient
then Exit
;
1302 if (gGameSettings
.GameType
= GT_SINGLE
)
1303 or LongBool(gGameSettings
.Options
and GAME_OPTION_MONSTERS
) then
1305 mon
:= g_Monsters_Create(monster
.MonsterType
, monster
.X
, monster
.Y
, TDirection(monster
.Direction
));
1307 if gTriggers
<> nil then
1309 for a
:= 0 to High(gTriggers
) do
1311 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1313 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1314 if (gTriggers
[a
].trigData
.trigMonsterId
) = Integer(mon
.StartID
) then mon
.AddTrigger(a
);
1319 if monster
.MonsterType
<> MONSTER_BARREL
then Inc(gTotalMonsters
);
1323 procedure g_Map_ReAdd_DieTriggers();
1325 function monsDieTrig (mon
: TMonster
): Boolean;
1328 //tw: TStrTextWriter;
1330 result
:= false; // don't stop
1331 mon
.ClearTriggers();
1332 for a
:= 0 to High(gTriggers
) do
1334 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1336 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1338 tw := TStrTextWriter.Create();
1340 gTriggers[a].trigData.writeTo(tw);
1341 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1346 if (gTriggers
[a
].trigData
.trigMonsterId
) = Integer(mon
.StartID
) then mon
.AddTrigger(a
);
1352 if g_Game_IsClient
then Exit
;
1354 g_Mons_ForEach(monsDieTrig
);
1357 function extractWadName(resourceName
: string): string;
1361 posN
:= Pos(':', resourceName
);
1363 Result
:= Copy(resourceName
, 0, posN
-1)
1368 procedure addResToExternalResList(res
: string);
1370 res
:= extractWadName(res
);
1371 if (res
<> '') and (gExternalResources
.IndexOf(res
) = -1) then
1372 gExternalResources
.Add(res
);
1375 procedure generateExternalResourcesList({mapReader: TMapReader_1}map
: TDynRecord
);
1377 //textures: TTexturesRec1Array;
1378 //textures: TDynField;
1380 //mapHeader: TMapHeaderRec_1;
1382 //resFile: String = '';
1384 if gExternalResources
= nil then
1385 gExternalResources
:= TStringList
.Create
;
1387 gExternalResources
.Clear
;
1391 textures := GetTextures(map);
1392 for i := 0 to High(textures) do
1394 addResToExternalResList(resFile);
1398 textures := map['texture'];
1399 if (textures <> nil) then
1401 for trec in textures do
1403 addResToExternalResList(resFile);
1410 //mapHeader := GetMapHeader(map);
1412 addResToExternalResList(map
.MusicName
);
1413 addResToExternalResList(map
.SkyName
);
1417 procedure mapCreateGrid ();
1419 mapX0
: Integer = $3fffffff;
1420 mapY0
: Integer = $3fffffff;
1421 mapX1
: Integer = -$3fffffff;
1422 mapY1
: Integer = -$3fffffff;
1424 procedure calcBoundingBox (constref panels
: TPanelArray
);
1429 for idx
:= 0 to High(panels
) do
1432 if not pan
.visvalid
then continue
;
1433 if (pan
.Width
< 1) or (pan
.Height
< 1) then continue
;
1434 if (mapX0
> pan
.x0
) then mapX0
:= pan
.x0
;
1435 if (mapY0
> pan
.y0
) then mapY0
:= pan
.y0
;
1436 if (mapX1
< pan
.x1
) then mapX1
:= pan
.x1
;
1437 if (mapY1
< pan
.y1
) then mapY1
:= pan
.y1
;
1441 procedure addPanelsToGrid (constref panels
: TPanelArray
);
1447 //tag := panelTypeToTag(tag);
1448 for idx
:= 0 to High(panels
) do
1451 if not pan
.visvalid
then continue
;
1452 if (pan
.proxyId
<> -1) then
1454 {$IF DEFINED(D2F_DEBUG)}
1455 e_WriteLog(Format('DUPLICATE wall #%d(%d) enabled (%d); type:%08x', [Integer(idx
), Integer(pan
.proxyId
), Integer(mapGrid
.proxyEnabled
[pan
.proxyId
]), pan
.PanelType
]), MSG_NOTIFY
);
1459 case pan
.PanelType
of
1460 PANEL_WALL
: newtag
:= GridTagWall
;
1461 PANEL_OPENDOOR
, PANEL_CLOSEDOOR
: newtag
:= GridTagDoor
;
1462 PANEL_BACK
: newtag
:= GridTagBack
;
1463 PANEL_FORE
: newtag
:= GridTagFore
;
1464 PANEL_WATER
: newtag
:= GridTagWater
;
1465 PANEL_ACID1
: newtag
:= GridTagAcid1
;
1466 PANEL_ACID2
: newtag
:= GridTagAcid2
;
1467 PANEL_STEP
: newtag
:= GridTagStep
;
1468 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
: newtag
:= GridTagLift
;
1469 PANEL_BLOCKMON
: newtag
:= GridTagBlockMon
;
1470 else continue
; // oops
1474 pan
.proxyId
:= mapGrid
.insertBody(pan
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
, newtag
);
1475 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1476 mapGrid
.proxyEnabled
[pan
.proxyId
] := pan
.Enabled
;
1477 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1479 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1481 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1492 calcBoundingBox(gWalls
);
1493 calcBoundingBox(gRenderBackgrounds
);
1494 calcBoundingBox(gRenderForegrounds
);
1495 calcBoundingBox(gWater
);
1496 calcBoundingBox(gAcid1
);
1497 calcBoundingBox(gAcid2
);
1498 calcBoundingBox(gSteps
);
1499 calcBoundingBox(gLifts
);
1500 calcBoundingBox(gBlockMon
);
1502 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0
, mapY0
, mapX1
, mapY1
, gMapInfo
.Width
, gMapInfo
.Height
]);
1504 if (mapX0
> 0) then mapX0
:= 0;
1505 if (mapY0
> 0) then mapY0
:= 0;
1507 if (mapX1
< gMapInfo
.Width
-1) then mapX1
:= gMapInfo
.Width
-1;
1508 if (mapY1
< gMapInfo
.Height
-1) then mapY1
:= gMapInfo
.Height
-1;
1510 mapGrid
:= TPanelGrid
.Create(mapX0
-128, mapY0
-128, mapX1
-mapX0
+1+128*2, mapY1
-mapY0
+1+128*2);
1511 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1513 addPanelsToGrid(gWalls
);
1514 addPanelsToGrid(gRenderBackgrounds
);
1515 addPanelsToGrid(gRenderForegrounds
);
1516 addPanelsToGrid(gWater
);
1517 addPanelsToGrid(gAcid1
);
1518 addPanelsToGrid(gAcid2
);
1519 addPanelsToGrid(gSteps
);
1520 addPanelsToGrid(gLifts
); // it doesn't matter which LIFT type is used here
1521 addPanelsToGrid(gBlockMon
);
1523 mapGrid
.dumpStats();
1525 g_Mons_InitTree(mapGrid
.gridX0
, mapGrid
.gridY0
, mapGrid
.gridWidth
, mapGrid
.gridHeight
);
1529 function g_Map_Load(Res
: String): Boolean;
1531 DefaultMusRes
= 'Standart.wad:STDMUS\MUS1';
1532 DefaultSkyRes
= 'Standart.wad:STDSKY\SKY0';
1536 //TexturePanel: Integer;
1538 LiftPanelIdx
: Integer;
1539 DoorPanelIdx
: Integer;
1540 ShotPanelIdx
: Integer;
1541 trigrec
: TDynRecord
;
1543 liftPan
: TDynRecord
;
1544 doorPan
: TDynRecord
;
1545 shotPan
: TDynRecord
;
1549 mapReader
: TDynRecord
= nil;
1550 mapTextureList
: TDynField
= nil; //TTexturesRec1Array; tagInt: texture index
1551 panels
: TDynField
= nil; //TPanelsRec1Array;
1552 items
: TDynField
= nil; //TItemsRec1Array;
1553 monsters
: TDynField
= nil; //TMonsterRec1Array;
1554 areas
: TDynField
= nil; //TAreasRec1Array;
1555 triggers
: TDynField
= nil; //TTriggersRec1Array;
1558 AddTextures
: TAddTextureArray
;
1559 TriggersTable
: array of TTRec
;
1560 FileName
, mapResName
, s
, TexName
: String;
1563 ok
, isAnim
, trigRef
: Boolean;
1564 CurTex
, ntn
: Integer;
1565 rec
, texrec
: TDynRecord
;
1567 pannum
, trignum
, cnt
, tgpid
: Integer;
1577 gMapInfo
.Map
:= Res
;
1578 TriggersTable
:= nil;
1581 sfsGCDisable(); // temporary disable removing of temporary volumes
1584 FileName
:= g_ExtractWadName(Res
);
1585 e_WriteLog('Loading map WAD: '+FileName
, MSG_NOTIFY
);
1586 g_Game_SetLoadingText(_lc
[I_LOAD_WAD_FILE
], 0, False);
1588 WAD
:= TWADFile
.Create();
1589 if not WAD
.ReadFile(FileName
) then
1591 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_WAD
], [FileName
]));
1596 //k8: why loader ignores path here?
1597 mapResName
:= g_ExtractFileName(Res
);
1598 if not WAD
.GetMapResource(mapResName
, Data
, Len
) then
1600 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_RES
], [mapResName
]));
1609 e_LogWritefln('invalid map file: ''%s''', [mapResName
]);
1615 e_LogWritefln('Loading map: %s', [mapResName
], MSG_NOTIFY
);
1616 g_Game_SetLoadingText(_lc
[I_LOAD_MAP
], 0, False);
1618 stt
:= curTimeMicro();
1621 mapReader
:= g_Map_ParseMap(Data
, Len
);
1624 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]));
1631 generateExternalResourcesList(mapReader
);
1632 mapTextureList
:= mapReader
['texture'];
1633 // get all other lists here too
1634 panels
:= mapReader
['panel'];
1635 triggers
:= mapReader
['trigger'];
1636 items
:= mapReader
['item'];
1637 areas
:= mapReader
['area'];
1638 monsters
:= mapReader
['monster'];
1640 // Çàãðóçêà îïèñàíèÿ êàðòû:
1641 e_WriteLog(' Reading map info...', MSG_NOTIFY
);
1642 g_Game_SetLoadingText(_lc
[I_LOAD_MAP_HEADER
], 0, False);
1646 Name
:= mapReader
.MapName
;
1647 Description
:= mapReader
.MapDesc
;
1648 Author
:= mapReader
.MapAuthor
;
1649 MusicName
:= mapReader
.MusicName
;
1650 SkyName
:= mapReader
.SkyName
;
1651 Height
:= mapReader
.Height
;
1652 Width
:= mapReader
.Width
;
1655 // Çàãðóçêà òåêñòóð:
1656 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], 0, False);
1657 // Äîáàâëåíèå òåêñòóð â Textures[]:
1658 if (mapTextureList
<> nil) and (mapTextureList
.count
> 0) then
1660 e_WriteLog(' Loading textures:', MSG_NOTIFY
);
1661 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], mapTextureList
.count
-1, False);
1664 for rec
in mapTextureList
do
1668 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1669 e_WriteLog(Format(' Loading texture #%d: %s', [cnt
, s
]), MSG_NOTIFY
);
1671 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1674 // Àíèìèðîâàííàÿ òåêñòóðà
1675 ntn
:= CreateAnimTexture(rec
.Resource
, FileName
, True);
1676 if (ntn
< 0) then g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_ANIM
], [s
]));
1681 ntn
:= CreateTexture(rec
.Resource
, FileName
, True);
1682 if (ntn
< 0) then g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_SIMPLE
], [s
]));
1684 if (ntn
< 0) then ntn
:= CreateNullTexture(rec
.Resource
);
1686 rec
.tagInt
:= ntn
; // remember texture number
1687 g_Game_StepLoading();
1690 // set panel tagInt to texture index
1691 if (panels
<> nil) then
1693 for rec
in panels
do
1695 texrec
:= rec
.TextureRec
;
1696 if (texrec
= nil) then rec
.tagInt
:= -1 else rec
.tagInt
:= texrec
.tagInt
;
1701 // Çàãðóçêà òðèããåðîâ
1702 gTriggerClientID
:= 0;
1703 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1704 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS
], 0, False);
1707 e_WriteLog(' Loading panels...', MSG_NOTIFY
);
1708 g_Game_SetLoadingText(_lc
[I_LOAD_PANELS
], 0, False);
1710 // check texture numbers for panels
1711 if (panels
<> nil) and (panels
.count
> 0) then
1713 for rec
in panels
do
1715 if (rec
.tagInt
< 0) then
1717 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR
);
1724 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1725 if (triggers
<> nil) and (triggers
.count
> 0) then
1727 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY
);
1728 //SetLength(TriggersTable, triggers.count);
1729 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS_TABLE
], triggers
.count
-1, False);
1731 for rec
in triggers
do
1733 SetLength(TriggersTable
, Length(TriggersTable
)+1);
1734 pttit
:= @TriggersTable
[High(TriggersTable
)];
1735 pttit
.trigrec
:= rec
;
1736 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1737 pttit
.texPan
:= mapReader
.panel
[rec
.TexturePanel
];
1738 pttit
.liftPan
:= nil;
1739 pttit
.doorPan
:= nil;
1740 pttit
.shotPan
:= nil;
1741 pttit
.texPanIdx
:= -1;
1742 pttit
.LiftPanelIdx
:= -1;
1743 pttit
.DoorPanelIdx
:= -1;
1744 pttit
.ShotPanelIdx
:= -1;
1746 if rec
.TriggerType
in [TRIGGER_LIFTUP
, TRIGGER_LIFTDOWN
, TRIGGER_LIFT
] then
1748 pttit
.liftPan
:= mapReader
.panel
[rec
.trigRec
.tgPanelID
];
1751 if rec
.TriggerType
in [TRIGGER_OPENDOOR
, TRIGGER_CLOSEDOOR
, TRIGGER_DOOR
, TRIGGER_DOOR5
, TRIGGER_CLOSETRAP
, TRIGGER_TRAP
] then
1753 pttit
.doorPan
:= mapReader
.panel
[rec
.trigRec
.tgPanelID
];
1756 if (rec
.TriggerType
= TRIGGER_SHOT
) then
1758 pttit
.shotPan
:= mapReader
.panel
[rec
.trigRec
.tgShotPanelID
];
1761 if (pttit
.texPan
<> nil) then pttit
.texPan
.userPanelTrigRef
:= true;
1762 if (pttit
.liftPan
<> nil) then pttit
.liftPan
.userPanelTrigRef
:= true;
1763 if (pttit
.doorPan
<> nil) then pttit
.doorPan
.userPanelTrigRef
:= true;
1764 if (pttit
.shotPan
<> nil) then pttit
.shotPan
.userPanelTrigRef
:= true;
1766 g_Game_StepLoading();
1771 if (panels
<> nil) and (panels
.count
> 0) then
1773 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY
);
1774 g_Game_SetLoadingText(_lc
[I_LOAD_LINK_TRIGGERS
], panels
.count
-1, False);
1777 for rec
in panels
do
1780 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1782 SetLength(AddTextures
, 0);
1787 if (mapTextureList
<> nil) then
1789 texrec
:= rec
.TextureRec
;
1790 ok
:= (texrec
<> nil);
1795 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1796 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1798 if (TriggersTable
<> nil) and (mapTextureList
<> nil) then
1801 for b := 0 to High(TriggersTable) do
1803 if (TriggersTable[b].texPan = rec) or (TriggersTable[b].shotPan = rec) then
1811 if rec
.userPanelTrigRef
then
1813 // e_LogWritefln('trigref for panel %s', [pannum]);
1822 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1823 s
:= texrec
.Resource
;
1825 // Ñïåö-òåêñòóðû çàïðåùåíû
1826 if g_Map_IsSpecialTexture(s
) then
1832 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1833 ok
:= g_Texture_NumNameFindStart(s
);
1836 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1837 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1840 k
:= NNF_NAME_BEFORE
;
1841 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1842 while ok
or (k
= NNF_NAME_BEFORE
) or (k
= NNF_NAME_EQUALS
) do
1844 k
:= g_Texture_NumNameFindNext(TexName
);
1846 if (k
= NNF_NAME_BEFORE
) or (k
= NNF_NAME_AFTER
) then
1848 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1851 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1853 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1854 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1855 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1858 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1860 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1861 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1862 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1867 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1869 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1870 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1871 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1874 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1876 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1877 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1878 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1882 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1886 for c := 0 to High(Textures) do
1888 if (Textures[c].TextureName = TexName) then
1890 SetLength(AddTextures, Length(AddTextures)+1);
1891 AddTextures[High(AddTextures)].Texture := c;
1892 AddTextures[High(AddTextures)].Anim := isAnim;
1897 if (TextNameHash
<> nil) and TextNameHash
.get(toLowerCase1251(TexName
), c
) then
1899 SetLength(AddTextures
, Length(AddTextures
)+1);
1900 AddTextures
[High(AddTextures
)].Texture
:= c
;
1901 AddTextures
[High(AddTextures
)].Anim
:= isAnim
;
1907 if k
= NNF_NAME_EQUALS
then
1909 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1910 SetLength(AddTextures
, Length(AddTextures
)+1);
1911 AddTextures
[High(AddTextures
)].Texture
:= rec
.tagInt
; // internal texture number, not map index
1912 AddTextures
[High(AddTextures
)].Anim
:= texrec
.Anim
;
1913 CurTex
:= High(AddTextures
);
1924 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1925 end; // if ok - ññûëàþòñÿ òðèããåðû
1929 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1930 SetLength(AddTextures
, 1);
1931 AddTextures
[0].Texture
:= rec
.tagInt
; // internal texture number, not map index
1932 AddTextures
[0].Anim
:= false;
1933 if (texrec
<> nil) then AddTextures
[0].Anim
:= texrec
.Anim
;
1937 //e_WriteLog(Format('panel #%d: TextureNum=%d; ht=%d; ht1=%d; atl=%d', [a, panels[a].TextureNum, High(mapTextureList), High(Textures), High(AddTextures)]), MSG_NOTIFY);
1939 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1941 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð
1942 PanelID
:= CreatePanel(rec
, AddTextures
, CurTex
, trigRef
);
1943 //e_LogWritefln('panel #%s of type %s got id #%s', [pannum, rec.PanelType, PanelID]);
1944 // set 'gamePanelId' field to panel id
1945 rec
.userPanelId
:= PanelID
; // remember game panel id, we'll fix triggers later
1947 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
1949 g_Game_StepLoading();
1953 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
1954 for b
:= 0 to High(TriggersTable
) do
1956 if (TriggersTable
[b
].texPan
<> nil) then TriggersTable
[b
].texPanIdx
:= TriggersTable
[b
].texPan
.userPanelId
;
1957 if (TriggersTable
[b
].liftPan
<> nil) then TriggersTable
[b
].LiftPanelIdx
:= TriggersTable
[b
].liftPan
.userPanelId
;
1958 if (TriggersTable
[b
].doorPan
<> nil) then TriggersTable
[b
].DoorPanelIdx
:= TriggersTable
[b
].doorPan
.userPanelId
;
1959 if (TriggersTable
[b
].shotPan
<> nil) then TriggersTable
[b
].ShotPanelIdx
:= TriggersTable
[b
].shotPan
.userPanelId
;
1962 // create map grid, init other grids (for monsters, for example)
1963 e_WriteLog('Creating map grid', MSG_NOTIFY
);
1966 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
1967 if (triggers
<> nil) and (panels
<> nil) and (not gLoadGameMode
) then
1969 e_LogWritefln(' Creating triggers (%d)...', [triggers
.count
]);
1970 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_TRIGGERS
], 0, False);
1971 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
1973 for rec
in triggers
do
1976 if (TriggersTable
[trignum
].texPan
<> nil) then b
:= TriggersTable
[trignum
].texPan
.PanelType
else b
:= 0;
1977 if (TriggersTable
[trignum
].shotPan
<> nil) then c
:= TriggersTable
[trignum
].shotPan
.PanelType
else c
:= 0;
1978 // we can have only one of those
1979 if (TriggersTable
[trignum
].LiftPanelIdx
<> -1) then tgpid
:= TriggersTable
[trignum
].LiftPanelIdx
1980 else if (TriggersTable
[trignum
].DoorPanelIdx
<> -1) then tgpid
:= TriggersTable
[trignum
].DoorPanelIdx
1981 else if (TriggersTable
[trignum
].ShotPanelIdx
<> -1) then tgpid
:= TriggersTable
[trignum
].ShotPanelIdx
1983 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
1984 CreateTrigger(trignum
, rec
, TriggersTable
[trignum
].texPanIdx
, tgpid
, Word(b
), Word(c
));
1988 // Çàãðóçêà ïðåäìåòîâ
1989 e_WriteLog(' Loading items...', MSG_NOTIFY
);
1990 g_Game_SetLoadingText(_lc
[I_LOAD_ITEMS
], 0, False);
1992 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
1993 if (items
<> nil) and not gLoadGameMode
then
1995 e_WriteLog(' Spawning items...', MSG_NOTIFY
);
1996 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_ITEMS
], 0, False);
1997 for rec
in items
do CreateItem(rec
);
2000 // Çàãðóçêà îáëàñòåé
2001 e_WriteLog(' Loading areas...', MSG_NOTIFY
);
2002 g_Game_SetLoadingText(_lc
[I_LOAD_AREAS
], 0, False);
2004 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2005 if areas
<> nil then
2007 e_WriteLog(' Creating areas...', MSG_NOTIFY
);
2008 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_AREAS
], 0, False);
2009 for rec
in areas
do CreateArea(rec
);
2012 // Çàãðóçêà ìîíñòðîâ
2013 e_WriteLog(' Loading monsters...', MSG_NOTIFY
);
2014 g_Game_SetLoadingText(_lc
[I_LOAD_MONSTERS
], 0, False);
2016 gTotalMonsters
:= 0;
2018 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2019 if (monsters
<> nil) and not gLoadGameMode
then
2021 e_WriteLog(' Spawning monsters...', MSG_NOTIFY
);
2022 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_MONSTERS
], 0, False);
2023 for rec
in monsters
do CreateMonster(rec
);
2026 gCurrentMap
:= mapReader
; // this will be our current map now
2027 gCurrentMapFileName
:= Res
;
2031 if gMapInfo
.SkyName
<> '' then
2033 e_WriteLog(' Loading sky: ' + gMapInfo
.SkyName
, MSG_NOTIFY
);
2034 g_Game_SetLoadingText(_lc
[I_LOAD_SKY
], 0, False);
2035 FileName
:= g_ExtractWadName(gMapInfo
.SkyName
);
2037 if FileName
<> '' then
2038 FileName
:= GameDir
+'/wads/'+FileName
2041 FileName
:= g_ExtractWadName(Res
);
2044 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.SkyName
);
2045 if g_Texture_CreateWAD(BackID
, s
) then
2047 g_Game_SetupScreenSize();
2050 g_FatalError(Format(_lc
[I_GAME_ERROR_SKY
], [s
]));
2055 if gMapInfo
.MusicName
<> '' then
2057 e_WriteLog(' Loading music: ' + gMapInfo
.MusicName
, MSG_NOTIFY
);
2058 g_Game_SetLoadingText(_lc
[I_LOAD_MUSIC
], 0, False);
2059 FileName
:= g_ExtractWadName(gMapInfo
.MusicName
);
2061 if FileName
<> '' then
2062 FileName
:= GameDir
+'/wads/'+FileName
2065 FileName
:= g_ExtractWadName(Res
);
2068 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.MusicName
);
2069 if g_Sound_CreateWADEx(gMapInfo
.MusicName
, s
, True) then
2072 g_FatalError(Format(_lc
[I_GAME_ERROR_MUSIC
], [s
]));
2075 // Îñòàëüíûå óñòàíâêè
2083 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2084 if not gLoadGameMode
then g_GFX_Init();
2086 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2087 mapTextureList
:= nil;
2092 TriggersTable
:= nil;
2095 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2096 if ok
and (not gLoadGameMode
) then
2098 gMusic
.SetByName(gMapInfo
.MusicName
);
2103 gMusic
.SetByName('');
2106 stt
:= curTimeMicro()-stt
;
2107 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt
div 1000), Integer(stt
mod 1000)]);
2109 sfsGCEnable(); // enable releasing unused volumes
2111 e_ClearInputBuffer(); // why not?
2114 e_WriteLog('Done loading map.', MSG_NOTIFY
);
2119 function g_Map_GetMapInfo(Res
: String): TMapInfo
;
2122 mapReader
: TDynRecord
;
2123 //Header: TMapHeaderRec_1;
2128 FillChar(Result
, SizeOf(Result
), 0);
2129 FileName
:= g_ExtractWadName(Res
);
2131 WAD
:= TWADFile
.Create();
2132 if not WAD
.ReadFile(FileName
) then
2138 //k8: it ignores path again
2139 if not WAD
.GetMapResource(g_ExtractFileName(Res
), Data
, Len
) then
2148 mapReader
:= g_Map_ParseMap(Data
, Len
);
2155 if (mapReader
.Width
> 0) and (mapReader
.Height
> 0) then
2157 Result
.Name
:= mapReader
.MapName
;
2158 Result
.Description
:= mapReader
.MapDesc
;
2160 Result
.Author
:= mapReader
.MapAuthor
;
2161 Result
.Height
:= mapReader
.Height
;
2162 Result
.Width
:= mapReader
.Width
;
2166 g_Console_Add(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]), True);
2167 //ZeroMemory(@Header, SizeOf(Header));
2168 Result
.Name
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
2169 Result
.Description
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
2171 Result
.Author
:= '';
2179 function g_Map_GetMapsList(WADName
: string): SArray
;
2186 WAD
:= TWADFile
.Create();
2187 if not WAD
.ReadFile(WADName
) then
2192 ResList
:= WAD
.GetMapResources();
2193 if ResList
<> nil then
2195 for a
:= 0 to High(ResList
) do
2197 SetLength(Result
, Length(Result
)+1);
2198 Result
[High(Result
)] := ResList
[a
];
2204 function g_Map_Exist(Res
: string): Boolean;
2207 FileName
, mnn
: string;
2213 FileName
:= addWadExtension(g_ExtractWadName(Res
));
2215 WAD
:= TWADFile
.Create
;
2216 if not WAD
.ReadFile(FileName
) then
2222 ResList
:= WAD
.GetMapResources();
2225 mnn
:= g_ExtractFileName(Res
);
2226 if ResList
<> nil then
2227 for a
:= 0 to High(ResList
) do if StrEquCI1251(ResList
[a
], mnn
) then
2234 procedure g_Map_Free(freeTextures
: Boolean=true);
2238 procedure FreePanelArray(var panels
: TPanelArray
);
2243 if panels
<> nil then
2245 for i
:= 0 to High(panels
) do
2258 RespawnPoints
:= nil;
2259 if FlagPoints
[FLAG_RED
] <> nil then
2261 Dispose(FlagPoints
[FLAG_RED
]);
2262 FlagPoints
[FLAG_RED
] := nil;
2264 if FlagPoints
[FLAG_BLUE
] <> nil then
2266 Dispose(FlagPoints
[FLAG_BLUE
]);
2267 FlagPoints
[FLAG_BLUE
] := nil;
2269 //DOMFlagPoints := nil;
2273 if (Length(gCurrentMapFileName
) <> 0) then
2275 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName
]);
2279 e_LogWritefln('g_Map_Free: no previous map.', []);
2281 if freeTextures
then
2283 e_LogWritefln('g_Map_Free: clearing textures...', []);
2284 if (Textures
<> nil) then
2286 for a
:= 0 to High(Textures
) do
2288 if not g_Map_IsSpecialTexture(Textures
[a
].TextureName
) then
2290 if Textures
[a
].Anim
then
2292 g_Frames_DeleteByID(Textures
[a
].FramesID
)
2296 if (Textures
[a
].TextureID
<> LongWord(TEXTURE_NONE
)) then
2298 e_DeleteTexture(Textures
[a
].TextureID
);
2305 TextNameHash
.Free();
2306 TextNameHash
:= nil;
2307 BadTextNameHash
.Free();
2308 BadTextNameHash
:= nil;
2311 FreePanelArray(gWalls
);
2312 FreePanelArray(gRenderBackgrounds
);
2313 FreePanelArray(gRenderForegrounds
);
2314 FreePanelArray(gWater
);
2315 FreePanelArray(gAcid1
);
2316 FreePanelArray(gAcid2
);
2317 FreePanelArray(gSteps
);
2318 FreePanelArray(gLifts
);
2319 FreePanelArray(gBlockMon
);
2321 if BackID
<> DWORD(-1) then
2325 e_DeleteTexture(BackID
);
2326 BackID
:= DWORD(-1);
2329 g_Game_StopAllSounds(False);
2331 g_Sound_Delete(gMapInfo
.MusicName
);
2333 gMapInfo
.Name
:= '';
2334 gMapInfo
.Description
:= '';
2335 gMapInfo
.MusicName
:= '';
2336 gMapInfo
.Height
:= 0;
2337 gMapInfo
.Width
:= 0;
2345 procedure g_Map_Update();
2351 procedure UpdatePanelArray(var panels
: TPanelArray
);
2356 if panels
<> nil then
2357 for i
:= 0 to High(panels
) do
2362 UpdatePanelArray(gWalls
);
2363 UpdatePanelArray(gRenderBackgrounds
);
2364 UpdatePanelArray(gRenderForegrounds
);
2365 UpdatePanelArray(gWater
);
2366 UpdatePanelArray(gAcid1
);
2367 UpdatePanelArray(gAcid2
);
2368 UpdatePanelArray(gSteps
);
2370 if gGameSettings
.GameMode
= GM_CTF
then
2372 for a
:= FLAG_RED
to FLAG_BLUE
do
2373 if not (gFlags
[a
].State
in [FLAG_STATE_NONE
, FLAG_STATE_CAPTURED
]) then
2376 if gFlags
[a
].Animation
<> nil then
2377 gFlags
[a
].Animation
.Update();
2379 m
:= g_Obj_Move(@Obj
, True, True);
2381 if gTime
mod (GAME_TICK
*2) <> 0 then
2384 // Ñîïðîòèâëåíèå âîçäóõà:
2385 Obj
.Vel
.X
:= z_dec(Obj
.Vel
.X
, 1);
2387 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2388 if ((Count
= 0) or ByteBool(m
and MOVE_FALLOUT
)) and g_Game_IsServer
then
2391 gFlags
[a
].CaptureTime
:= 0;
2392 if a
= FLAG_RED
then
2393 s
:= _lc
[I_PLAYER_FLAG_RED
]
2395 s
:= _lc
[I_PLAYER_FLAG_BLUE
];
2396 g_Game_Message(Format(_lc
[I_MESSAGE_FLAG_RETURN
], [AnsiUpperCase(s
)]), 144);
2398 if g_Game_IsNet
then
2399 MH_SEND_FlagEvent(FLAG_STATE_RETURNED
, a
, 0);
2406 // Èãðîê áåðåò ôëàã:
2407 if gPlayers
<> nil then
2409 j
:= Random(Length(gPlayers
)) - 1;
2411 for d
:= 0 to High(gPlayers
) do
2414 if j
> High(gPlayers
) then
2417 if gPlayers
[j
] <> nil then
2418 if gPlayers
[j
].Live
and
2419 g_Obj_Collide(@Obj
, @gPlayers
[j
].Obj
) then
2421 if gPlayers
[j
].GetFlag(a
) then
2432 procedure g_Map_DrawPanels (PanelType
: Word);
2434 procedure DrawPanels (constref panels
: TPanelArray
; drawDoors
: Boolean=False);
2438 if (panels
<> nil) then
2440 // alas, no visible set
2441 for idx
:= 0 to High(panels
) do
2443 if not (drawDoors
xor panels
[idx
].Door
) then panels
[idx
].Draw();
2450 PANEL_WALL
: DrawPanels(gWalls
);
2451 PANEL_CLOSEDOOR
: DrawPanels(gWalls
, True);
2452 PANEL_BACK
: DrawPanels(gRenderBackgrounds
);
2453 PANEL_FORE
: DrawPanels(gRenderForegrounds
);
2454 PANEL_WATER
: DrawPanels(gWater
);
2455 PANEL_ACID1
: DrawPanels(gAcid1
);
2456 PANEL_ACID2
: DrawPanels(gAcid2
);
2457 PANEL_STEP
: DrawPanels(gSteps
);
2463 procedure g_Map_CollectDrawPanels (x0
, y0
, wdt
, hgt
: Integer);
2465 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2467 result
:= false; // don't stop, ever
2468 if ((tag
and GridTagDoor
) <> 0) <> pan
.Door
then exit
;
2469 gDrawPanelList
.insert(pan
);
2474 //tagmask := panelTypeToTag(PanelType);
2475 mapGrid
.forEachInAABB(x0
, y0
, wdt
, hgt
, checker
, GridDrawableMask
);
2476 // list will be rendered in `g_game.DrawPlayer()`
2480 procedure g_Map_DrawPanelShadowVolumes(lightX
: Integer; lightY
: Integer; radius
: Integer);
2482 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2484 result
:= false; // don't stop, ever
2485 pan
.DrawShadowVolume(lightX
, lightY
, radius
);
2489 mapGrid
.forEachInAABB(lightX
-radius
, lightY
-radius
, radius
*2, radius
*2, checker
, (GridTagWall
or GridTagDoor
));
2493 procedure g_Map_DrawBack(dx
, dy
: Integer);
2495 if gDrawBackGround
and (BackID
<> DWORD(-1)) then
2496 e_DrawSize(BackID
, dx
, dy
, 0, False, False, gBackSize
.X
, gBackSize
.Y
)
2498 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
2501 function g_Map_CollidePanelOld(X
, Y
: Integer; Width
, Height
: Word;
2502 PanelType
: Word; b1x3
: Boolean=false): Boolean;
2508 if WordBool(PanelType
and PANEL_WALL
) then
2509 if gWalls
<> nil then
2514 if gWalls
[a
].Enabled
and
2515 g_Collide(X
, Y
, Width
, Height
,
2516 gWalls
[a
].X
, gWalls
[a
].Y
,
2517 gWalls
[a
].Width
, gWalls
[a
].Height
) then
2524 if WordBool(PanelType
and PANEL_WATER
) then
2525 if gWater
<> nil then
2530 if g_Collide(X
, Y
, Width
, Height
,
2531 gWater
[a
].X
, gWater
[a
].Y
,
2532 gWater
[a
].Width
, gWater
[a
].Height
) then
2539 if WordBool(PanelType
and PANEL_ACID1
) then
2540 if gAcid1
<> nil then
2545 if g_Collide(X
, Y
, Width
, Height
,
2546 gAcid1
[a
].X
, gAcid1
[a
].Y
,
2547 gAcid1
[a
].Width
, gAcid1
[a
].Height
) then
2554 if WordBool(PanelType
and PANEL_ACID2
) then
2555 if gAcid2
<> nil then
2560 if g_Collide(X
, Y
, Width
, Height
,
2561 gAcid2
[a
].X
, gAcid2
[a
].Y
,
2562 gAcid2
[a
].Width
, gAcid2
[a
].Height
) then
2569 if WordBool(PanelType
and PANEL_STEP
) then
2570 if gSteps
<> nil then
2575 if g_Collide(X
, Y
, Width
, Height
,
2576 gSteps
[a
].X
, gSteps
[a
].Y
,
2577 gSteps
[a
].Width
, gSteps
[a
].Height
) then
2584 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then
2585 if gLifts
<> nil then
2590 if ((WordBool(PanelType
and (PANEL_LIFTUP
)) and (gLifts
[a
].LiftType
= 0)) or
2591 (WordBool(PanelType
and (PANEL_LIFTDOWN
)) and (gLifts
[a
].LiftType
= 1)) or
2592 (WordBool(PanelType
and (PANEL_LIFTLEFT
)) and (gLifts
[a
].LiftType
= 2)) or
2593 (WordBool(PanelType
and (PANEL_LIFTRIGHT
)) and (gLifts
[a
].LiftType
= 3))) and
2594 g_Collide(X
, Y
, Width
, Height
,
2595 gLifts
[a
].X
, gLifts
[a
].Y
,
2596 gLifts
[a
].Width
, gLifts
[a
].Height
) then
2603 if WordBool(PanelType
and PANEL_BLOCKMON
) then
2604 if gBlockMon
<> nil then
2606 h
:= High(gBlockMon
);
2610 ((gBlockMon
[a
].Width
+ gBlockMon
[a
].Height
) >= 64) ) and
2611 g_Collide(X
, Y
, Width
, Height
,
2612 gBlockMon
[a
].X
, gBlockMon
[a
].Y
,
2613 gBlockMon
[a
].Width
, gBlockMon
[a
].Height
) then
2621 function g_Map_CollideLiquid_TextureOld(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2625 function checkPanels (constref panels
: TPanelArray
): Boolean;
2630 if panels
= nil then exit
;
2631 for a
:= 0 to High(panels
) do
2633 if g_Collide(X
, Y
, Width
, Height
, panels
[a
].X
, panels
[a
].Y
, panels
[a
].Width
, panels
[a
].Height
) then
2636 texid
:= panels
[a
].GetTextureID();
2643 texid
:= LongWord(TEXTURE_NONE
);
2645 if not checkPanels(gWater
) then
2646 if not checkPanels(gAcid1
) then
2647 if not checkPanels(gAcid2
) then exit
;
2652 function g_Map_CollidePanel(X
, Y
: Integer; Width
, Height
: Word; PanelType
: Word; b1x3
: Boolean): Boolean;
2654 SlowMask
= GridTagLift
or GridTagBlockMon
;
2655 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2658 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2660 result := pan.Enabled;
2665 if ((tag
and GridTagLift
) <> 0) then
2668 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
2669 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
2670 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
2671 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3))) {and
2672 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2676 if ((tag
and GridTagBlockMon
) <> 0) then
2678 result
:= ((not b1x3
) or (pan
.Width
+pan
.Height
>= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2683 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2684 result
:= true; // i found her!
2688 tagmask
: Integer = 0;
2690 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
2691 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
2692 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
2693 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
2694 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
2695 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
2696 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
2698 if (tagmask
= 0) then begin result
:= false; exit
; end; // just in case
2700 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('*solids');
2701 if gdbg_map_use_accel_coldet
then
2703 if (Width
= 1) and (Height
= 1) then
2705 if ((tagmask
and SlowMask
) <> 0) then
2708 result
:= (mapGrid
.forEachAtPoint(X
, Y
, checker
, tagmask
) <> nil);
2713 result
:= (mapGrid
.forEachAtPoint(X
, Y
, nil, tagmask
) <> nil);
2718 if ((tagmask
and SlowMask
) <> 0) then
2721 result
:= (mapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, tagmask
) <> nil);
2726 result
:= (mapGrid
.forEachInAABB(X
, Y
, Width
, Height
, nil, tagmask
) <> nil);
2732 result
:= g_Map_CollidePanelOld(X
, Y
, Width
, Height
, PanelType
, b1x3
);
2734 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2738 function g_Map_CollideLiquid_Texture(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2740 cctype
: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2743 // slightly different from the old code, but meh...
2744 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2746 result
:= false; // don't stop, ever
2747 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2750 0: if ((tag
and GridTagWater
) = 0) then exit
; // allowed: water
2751 1: if ((tag
and (GridTagWater
or GridTagAcid1
)) = 0) then exit
; // allowed: water, acid1
2752 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2755 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2757 texid
:= pan
.GetTextureID();
2758 // water? water has the highest priority, so stop right here
2759 if ((tag
and GridTagWater
) <> 0) then begin cctype
:= 0; result
:= true; exit
; end;
2761 if ((tag
and GridTagAcid2
) <> 0) then cctype
:= 2;
2763 if ((tag
and GridTagAcid1
) <> 0) then cctype
:= 1;
2767 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('liquids');
2768 if gdbg_map_use_accel_coldet
then
2770 texid
:= LongWord(TEXTURE_NONE
);
2771 if (Width
= 1) and (Height
= 1) then
2773 mapGrid
.forEachAtPoint(X
, Y
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2777 mapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2783 result
:= g_Map_CollideLiquid_TextureOld(X
, Y
, Width
, Height
);
2785 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2789 procedure g_Map_EnableWall(ID
: DWORD
);
2794 pan
.Enabled
:= True;
2795 g_Mark(pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
, MARK_DOOR
, True);
2797 mapGrid
.proxyEnabled
[pan
.proxyId
] := true;
2798 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2799 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2801 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(gWalls
[ID
].PanelType
, ID
);
2803 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2804 //e_WriteLog(Format('ENABLE: wall #%d(%d) enabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY);
2808 procedure g_Map_DisableWall(ID
: DWORD
);
2813 pan
.Enabled
:= False;
2814 g_Mark(pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
, MARK_DOOR
, False);
2816 mapGrid
.proxyEnabled
[pan
.proxyId
] := false;
2817 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2819 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(pan
.PanelType
, ID
);
2821 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2822 //e_WriteLog(Format('DISABLE: wall #%d(%d) disabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY);
2826 procedure g_Map_SwitchTexture(PanelType
: Word; ID
: DWORD
; AnimLoop
: Byte = 0);
2831 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
2834 tp
:= gRenderForegrounds
[ID
];
2836 tp
:= gRenderBackgrounds
[ID
];
2849 tp
.NextTexture(AnimLoop
);
2850 if g_Game_IsServer
and g_Game_IsNet
then
2851 MH_SEND_PanelTexture(PanelType
, ID
, AnimLoop
);
2854 procedure g_Map_SetLift(ID
: DWORD
; t
: Integer);
2856 if gLifts
[ID
].LiftType
= t
then
2863 g_Mark(X
, Y
, Width
, Height
, MARK_LIFT
, False);
2864 //TODO: make separate lift tags, and change tag here
2866 if LiftType
= 0 then
2867 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTUP
, True)
2868 else if LiftType
= 1 then
2869 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTDOWN
, True)
2870 else if LiftType
= 2 then
2871 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTLEFT
, True)
2872 else if LiftType
= 3 then
2873 g_Mark(X
, Y
, Width
, Height
, MARK_LIFTRIGHT
, True);
2875 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2879 function g_Map_GetPoint(PointType
: Byte; var RespawnPoint
: TRespawnPoint
): Boolean;
2882 PointsArray
: Array of TRespawnPoint
;
2885 SetLength(PointsArray
, 0);
2887 if RespawnPoints
= nil then
2890 for a
:= 0 to High(RespawnPoints
) do
2891 if RespawnPoints
[a
].PointType
= PointType
then
2893 SetLength(PointsArray
, Length(PointsArray
)+1);
2894 PointsArray
[High(PointsArray
)] := RespawnPoints
[a
];
2897 if PointsArray
= nil then
2900 RespawnPoint
:= PointsArray
[Random(Length(PointsArray
))];
2904 function g_Map_GetPointCount(PointType
: Byte): Word;
2910 if RespawnPoints
= nil then
2913 for a
:= 0 to High(RespawnPoints
) do
2914 if RespawnPoints
[a
].PointType
= PointType
then
2915 Result
:= Result
+ 1;
2918 function g_Map_HaveFlagPoints(): Boolean;
2920 Result
:= (FlagPoints
[FLAG_RED
] <> nil) and (FlagPoints
[FLAG_BLUE
] <> nil);
2923 procedure g_Map_ResetFlag(Flag
: Byte);
2925 with gFlags
[Flag
] do
2931 Direction
:= D_LEFT
;
2932 State
:= FLAG_STATE_NONE
;
2933 if FlagPoints
[Flag
] <> nil then
2935 Obj
.X
:= FlagPoints
[Flag
]^.X
;
2936 Obj
.Y
:= FlagPoints
[Flag
]^.Y
;
2937 Direction
:= FlagPoints
[Flag
]^.Direction
;
2938 State
:= FLAG_STATE_NORMAL
;
2944 procedure g_Map_DrawFlags();
2947 Mirror
: TMirrorType
;
2949 if gGameSettings
.GameMode
<> GM_CTF
then
2952 for i
:= FLAG_RED
to FLAG_BLUE
do
2954 if State
<> FLAG_STATE_CAPTURED
then
2956 if State
= FLAG_STATE_NONE
then
2959 if Direction
= D_LEFT
then
2961 Mirror
:= M_HORIZONTAL
;
2970 Animation
.Draw(Obj
.X
+dx
, Obj
.Y
+1, Mirror
);
2972 if g_debug_Frames
then
2974 e_DrawQuad(Obj
.X
+Obj
.Rect
.X
,
2976 Obj
.X
+Obj
.Rect
.X
+Obj
.Rect
.Width
-1,
2977 Obj
.Y
+Obj
.Rect
.Y
+Obj
.Rect
.Height
-1,
2983 procedure g_Map_SaveState(Var Mem
: TBinMemoryWriter
);
2990 procedure SavePanelArray(var panels
: TPanelArray
);
2992 PAMem
: TBinMemoryWriter
;
2995 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2996 PAMem
:= TBinMemoryWriter
.Create((Length(panels
)+1) * 40);
2999 while i
< Length(panels
) do
3001 if panels
[i
].SaveIt
then
3005 // Ñîõðàíÿåì ïàíåëü:
3006 panels
[i
].SaveState(PAMem
);
3011 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
3012 PAMem
.SaveToMemory(Mem
);
3016 procedure SaveFlag(flag
: PFlag
);
3019 dw
:= FLAG_SIGNATURE
; // 'FLAG'
3021 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
3022 Mem
.WriteByte(flag
^.RespawnType
);
3024 Mem
.WriteByte(flag
^.State
);
3025 // Íàïðàâëåíèå ôëàãà:
3026 if flag
^.Direction
= D_LEFT
then
3032 Obj_SaveState(@flag
^.Obj
, Mem
);
3036 Mem
:= TBinMemoryWriter
.Create(1024 * 1024); // 1 MB
3038 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
3039 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
3040 SavePanelArray(gWalls
);
3041 // Ñîõðàíÿåì ïàíåëè ôîíà:
3042 SavePanelArray(gRenderBackgrounds
);
3043 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
3044 SavePanelArray(gRenderForegrounds
);
3045 // Ñîõðàíÿåì ïàíåëè âîäû:
3046 SavePanelArray(gWater
);
3047 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
3048 SavePanelArray(gAcid1
);
3049 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
3050 SavePanelArray(gAcid2
);
3051 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
3052 SavePanelArray(gSteps
);
3053 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
3054 SavePanelArray(gLifts
);
3057 ///// Ñîõðàíÿåì ìóçûêó: /////
3058 // Ñèãíàòóðà ìóçûêè:
3059 dw
:= MUSIC_SIGNATURE
; // 'MUSI'
3062 Assert(gMusic
<> nil, 'g_Map_SaveState: gMusic = nil');
3063 if gMusic
.NoMusic
then
3067 Mem
.WriteString(str
, 64);
3068 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
3069 dw
:= gMusic
.GetPosition();
3071 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
3072 boo
:= gMusic
.SpecPause
;
3073 Mem
.WriteBoolean(boo
);
3076 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3077 Mem
.WriteInt(gTotalMonsters
);
3080 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3081 if gGameSettings
.GameMode
= GM_CTF
then
3083 // Ôëàã Êðàñíîé êîìàíäû:
3084 SaveFlag(@gFlags
[FLAG_RED
]);
3085 // Ôëàã Ñèíåé êîìàíäû:
3086 SaveFlag(@gFlags
[FLAG_BLUE
]);
3090 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3091 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
3093 // Î÷êè Êðàñíîé êîìàíäû:
3094 Mem
.WriteSmallInt(gTeamStat
[TEAM_RED
].Goals
);
3095 // Î÷êè Ñèíåé êîìàíäû:
3096 Mem
.WriteSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
3101 procedure g_Map_LoadState(Var Mem
: TBinMemoryReader
);
3108 procedure LoadPanelArray(var panels
: TPanelArray
);
3110 PAMem
: TBinMemoryReader
;
3113 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
3114 PAMem
:= TBinMemoryReader
.Create();
3115 PAMem
.LoadFromMemory(Mem
);
3117 for i
:= 0 to Length(panels
)-1 do
3119 if panels
[i
].SaveIt
then
3125 raise EBinSizeError
.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
3127 // Çàãðóæàåì ïàíåëü:
3128 panels
[i
].LoadState(PAMem
);
3129 if (panels
[i
].arrIdx
<> i
) then raise Exception
.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
3130 if (panels
[i
].proxyId
>= 0) then mapGrid
.proxyEnabled
[panels
[i
].proxyId
] := panels
[i
].Enabled
;
3134 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
3138 procedure LoadFlag(flag
: PFlag
);
3142 if dw
<> FLAG_SIGNATURE
then // 'FLAG'
3144 raise EBinSizeError
.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
3146 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
3147 Mem
.ReadByte(flag
^.RespawnType
);
3149 Mem
.ReadByte(flag
^.State
);
3150 // Íàïðàâëåíèå ôëàãà:
3153 flag
^.Direction
:= D_LEFT
3155 flag
^.Direction
:= D_RIGHT
;
3157 Obj_LoadState(@flag
^.Obj
, Mem
);
3164 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3165 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
3166 LoadPanelArray(gWalls
);
3167 // Çàãðóæàåì ïàíåëè ôîíà:
3168 LoadPanelArray(gRenderBackgrounds
);
3169 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
3170 LoadPanelArray(gRenderForegrounds
);
3171 // Çàãðóæàåì ïàíåëè âîäû:
3172 LoadPanelArray(gWater
);
3173 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
3174 LoadPanelArray(gAcid1
);
3175 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
3176 LoadPanelArray(gAcid2
);
3177 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
3178 LoadPanelArray(gSteps
);
3179 // Çàãðóæàåì ïàíåëè ëèôòîâ:
3180 LoadPanelArray(gLifts
);
3183 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
3187 ///// Çàãðóæàåì ìóçûêó: /////
3188 // Ñèãíàòóðà ìóçûêè:
3190 if dw
<> MUSIC_SIGNATURE
then // 'MUSI'
3192 raise EBinSizeError
.Create('g_Map_LoadState: Wrong Music Signature');
3195 Assert(gMusic
<> nil, 'g_Map_LoadState: gMusic = nil');
3196 Mem
.ReadString(str
);
3197 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
3199 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
3200 Mem
.ReadBoolean(boo
);
3201 // Çàïóñêàåì ýòó ìóçûêó:
3202 gMusic
.SetByName(str
);
3203 gMusic
.SpecPause
:= boo
;
3206 gMusic
.SetPosition(dw
);
3209 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3210 Mem
.ReadInt(gTotalMonsters
);
3213 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3214 if gGameSettings
.GameMode
= GM_CTF
then
3216 // Ôëàã Êðàñíîé êîìàíäû:
3217 LoadFlag(@gFlags
[FLAG_RED
]);
3218 // Ôëàã Ñèíåé êîìàíäû:
3219 LoadFlag(@gFlags
[FLAG_BLUE
]);
3223 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3224 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
3226 // Î÷êè Êðàñíîé êîìàíäû:
3227 Mem
.ReadSmallInt(gTeamStat
[TEAM_RED
].Goals
);
3228 // Î÷êè Ñèíåé êîìàíäû:
3229 Mem
.ReadSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
3234 function g_Map_PanelForPID(PanelID
: Integer; var PanelArrayID
: Integer): PPanel
;
3239 if (PanelID
< 0) or (PanelID
> High(PanelByID
)) then Exit
;
3240 Arr
:= PanelByID
[PanelID
].PWhere
^;
3241 PanelArrayID
:= PanelByID
[PanelID
].PArrID
;
3242 Result
:= Addr(Arr
[PanelByID
[PanelID
].PArrID
]);
3246 // trace liquid, stepping by `dx` and `dy`
3247 // return last seen liquid coords, and `false` if we're started outside of the liquid
3248 function g_Map_TraceLiquidNonPrecise (x
, y
, dx
, dy
: Integer; out topx
, topy
: Integer): Boolean;
3250 MaskLiquid
= GridTagWater
or GridTagAcid1
or GridTagAcid2
;
3254 // started outside of the liquid?
3255 if (mapGrid
.forEachAtPoint(x
, y
, nil, MaskLiquid
) = nil) then begin result
:= false; exit
; end;
3256 if (dx
= 0) and (dy
= 0) then begin result
:= false; exit
; end; // sanity check
3262 if (mapGrid
.forEachAtPoint(x
, y
, nil, MaskLiquid
) = nil) then exit
; // out of the water, just exit