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;
103 procedure g_Map_ProfilersBegin ();
104 procedure g_Map_ProfilersEnd ();
107 RESPAWNPOINT_PLAYER1
= 1;
108 RESPAWNPOINT_PLAYER2
= 2;
110 RESPAWNPOINT_RED
= 4;
111 RESPAWNPOINT_BLUE
= 5;
119 FLAG_STATE_NORMAL
= 1;
120 FLAG_STATE_DROPPED
= 2;
121 FLAG_STATE_CAPTURED
= 3;
122 FLAG_STATE_SCORED
= 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
123 FLAG_STATE_RETURNED
= 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
125 FLAG_TIME
= 720; // 20 seconds
127 SKY_STRETCH
: Single = 1.5;
142 // sorted by draw priority
143 GridTagBack
= 1 shl 0;
144 GridTagStep
= 1 shl 1;
145 GridTagWall
= 1 shl 2;
146 GridTagDoor
= 1 shl 3;
147 GridTagAcid1
= 1 shl 4;
148 GridTagAcid2
= 1 shl 5;
149 GridTagWater
= 1 shl 6;
150 GridTagFore
= 1 shl 7;
151 // the following are invisible
152 GridTagLift
= 1 shl 8;
153 GridTagBlockMon
= 1 shl 9;
158 gRenderBackgrounds
: TPanelArray
;
159 gRenderForegrounds
: TPanelArray
;
160 gWater
, gAcid1
, gAcid2
: TPanelArray
;
163 gBlockMon
: TPanelArray
;
164 gFlags
: array [FLAG_RED
..FLAG_BLUE
] of TFlag
;
165 //gDOMFlags: array of TFlag;
168 gDoorMap
: array of array of DWORD
;
169 gLiftMap
: array of array of DWORD
;
170 gWADHash
: TMD5Digest
;
171 BackID
: DWORD
= DWORD(-1);
172 gExternalResources
: TStringList
;
174 gdbg_map_use_accel_render
: Boolean = true;
175 gdbg_map_use_accel_coldet
: Boolean = true;
176 //gdbg_map_use_tree_draw: Boolean = false;
177 //gdbg_map_use_tree_coldet: Boolean = false;
178 //gdbg_map_dump_coldet_tree_queries: Boolean = false;
179 profMapCollision
: TProfiler
= nil; //WARNING: FOR DEBUGGING ONLY!
180 gDrawPanelList
: TBinaryHeapObj
= nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
182 function panelTypeToTag (panelType
: Word): Integer; // returns GridTagXXX
188 g_main
, e_log
, SysUtils
, g_items
, g_gfx
, g_console
,
189 GL
, GLExt
, g_weapons
, g_game
, g_sound
, e_sound
, CONFIG
,
190 g_options
, MAPREADER
, g_triggers
, g_player
, MAPDEF
,
191 Math
, g_monsters
, g_saveload
, g_language
, g_netmsg
,
193 ImagingTypes
, Imaging
, ImagingUtility
,
194 ImagingGif
, ImagingNetworkGraphics
;
197 FLAGRECT
: TRectWH
= (X
:15; Y
:12; Width
:33; Height
:52);
198 MUSIC_SIGNATURE
= $4953554D; // 'MUSI'
199 FLAG_SIGNATURE
= $47414C46; // 'FLAG'
203 TPanelGrid
= specialize TBodyGridBase
<TPanel
>;
206 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
208 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
209 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
214 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
217 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
218 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
219 if (pan.Width < 1) or (pan.Height < 1) then exit;
220 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
226 function panelTypeToTag (panelType
: Word): Integer;
229 PANEL_WALL
: result
:= GridTagWall
; // gWalls
230 PANEL_OPENDOOR
, PANEL_CLOSEDOOR
: result
:= GridTagDoor
; // gWalls
231 PANEL_BACK
: result
:= GridTagBack
; // gRenderBackgrounds
232 PANEL_FORE
: result
:= GridTagFore
; // gRenderForegrounds
233 PANEL_WATER
: result
:= GridTagWater
; // gWater
234 PANEL_ACID1
: result
:= GridTagAcid1
; // gAcid1
235 PANEL_ACID2
: result
:= GridTagAcid2
; // gAcid2
236 PANEL_STEP
: result
:= GridTagStep
; // gSteps
237 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
: result
:= GridTagLift
; // gLifts -- this is for all lifts
238 PANEL_BLOCKMON
: result
:= GridTagBlockMon
; // gBlockMon -- this is for all blockmons
239 else result
:= GridTagInvalid
;
244 function dplLess (a
, b
: TObject
): Boolean;
248 //result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
251 if (pa
.tag
< pb
.tag
) then begin result
:= true; exit
; end;
252 if (pa
.tag
> pb
.tag
) then begin result
:= false; exit
; end;
253 result
:= (pa
.ArrIdx
< pb
.ArrIdx
);
256 procedure dplClear ();
258 if (gDrawPanelList
= nil) then gDrawPanelList
:= TBinaryHeapObj
.Create(@dplLess
) else gDrawPanelList
.clear();
264 PWhere
: ^TPanelArray
;
269 PanelById
: array of TPanelID
;
270 Textures
: TLevelTextureArray
;
271 RespawnPoints
: Array of TRespawnPoint
;
272 FlagPoints
: Array [FLAG_RED
..FLAG_BLUE
] of PFlagPoint
;
273 //DOMFlagPoints: Array of TFlagPoint;
274 gMapGrid
: TPanelGrid
= nil;
275 //mapTree: TDynAABBTreeMap = nil;
278 procedure g_Map_ProfilersBegin ();
280 if (profMapCollision
= nil) then profMapCollision
:= TProfiler
.Create('COLSOLID', g_profile_history_size
);
281 profMapCollision
.mainBegin(g_profile_collision
);
283 if g_profile_collision
then
285 profMapCollision
.sectionBegin('*solids');
286 profMapCollision
.sectionEnd();
287 profMapCollision
.sectionBegin('liquids');
288 profMapCollision
.sectionEnd();
292 procedure g_Map_ProfilersEnd ();
294 if (profMapCollision
<> nil) then profMapCollision
.mainEnd();
298 // wall index in `gWalls` or -1
300 function g_Map_traceToNearestWallOld (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
302 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
307 result := -666.0; // invalid
308 if not pan.Enabled then exit;
309 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
310 if not aabb.valid then exit;
311 if aabb.intersects(ray, @tmin) then
313 //if (tmin*tmin > maxDistSq) then exit;
314 if (tmin >= 0.0) then
316 //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);
317 //if (tmin < 0.0) then tmin := 0.0;
324 qr: TDynAABBTreeMap.TSegmentQueryResult;
331 if (mapTree = nil) then exit;
332 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
334 maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
335 if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
337 result := qr.flesh.arrIdx;
338 if (hitx <> nil) or (hity <> nil) then
340 ray := Ray2D.Create(x0, y0, x1, y1);
341 hxf := ray.origX+ray.dirX*qr.time;
342 hyf := ray.origY+ray.dirY*qr.time;
347 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
358 if (hitx <> nil) then hitx^ := hx;
359 if (hity <> nil) then hity^ := hy;
367 // wall index in `gWalls` or -1
368 function g_Map_traceToNearestWall (x0
, y0
, x1
, y1
: Integer; hitx
: PInteger=nil; hity
: PInteger=nil): Boolean;
370 lastX
, lastY
, lastDistSq
: Integer;
371 wasHit
: Boolean = false;
373 // pan=nil: before processing new tile
374 function sqchecker (pan
: TPanel
; tag
: Integer; x
, y
, prevx
, prevy
: Integer): Boolean;
380 // stop if something was hit at the previous tile
386 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
388 if not pan
.Enabled
then exit
;
390 distSq
:= (prevx
-x0
)*(prevx
-x0
)+(prevy
-y0
)*(prevy
-y0
);
391 if (distSq
< lastDistSq
) then
394 lastDistSq
:= distSq
;
403 if (gMapGrid
= nil) then exit
;
404 lastDistSq
:= (x1
-x0
)*(x1
-x0
)+(y1
-y0
)*(y1
-y0
)+1;
407 result
:= gMapGrid
.traceRay(x0
, y0
, x1
, y1
, sqchecker
, (GridTagWall
or GridTagDoor
));
408 if (hitx
<> nil) then hitx
^ := lastX
;
409 if (hity
<> nil) then hity
^ := lastY
;
413 function g_Map_HasAnyPanelAtPoint (x
, y
: Integer; panelType
: Word): Boolean;
415 function checker (pan
: TPanel
; tag
: Integer): Boolean;
417 result
:= false; // don't stop, ever
419 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
421 if not pan
.Enabled
then exit
;
424 result
:= (x
>= pan
.X
) and (y
>= pan
.Y
) and (x
< pan
.X
+pan
.Width
) and (y
< pan
.Y
+pan
.Height
);
425 if not result
then exit
;
427 if ((tag
and GridTagLift
) <> 0) then
430 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
431 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
432 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
433 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3)));
438 tagmask
: Integer = 0;
442 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
443 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
444 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
445 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
446 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
447 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
448 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
450 if (tagmask
= 0) then exit
;// just in case
451 result
:= gMapGrid
.forEachAtPoint(x
, y
, checker
, tagmask
);
455 function g_Map_IsSpecialTexture(Texture
: String): Boolean;
457 Result
:= (Texture
= TEXTURE_NAME_WATER
) or
458 (Texture
= TEXTURE_NAME_ACID1
) or
459 (Texture
= TEXTURE_NAME_ACID2
);
462 procedure CreateDoorMap();
464 PanelArray
: Array of record
470 a
, b
, c
, m
, i
, len
: Integer;
478 SetLength(PanelArray
, len
);
480 for a
:= 0 to High(gWalls
) do
481 if gWalls
[a
].Door
then
483 PanelArray
[i
].X
:= gWalls
[a
].X
;
484 PanelArray
[i
].Y
:= gWalls
[a
].Y
;
485 PanelArray
[i
].Width
:= gWalls
[a
].Width
;
486 PanelArray
[i
].Height
:= gWalls
[a
].Height
;
487 PanelArray
[i
].Active
:= True;
488 PanelArray
[i
].PanelID
:= a
;
494 SetLength(PanelArray
, len
);
505 SetLength(gDoorMap
, 0);
507 g_Game_SetLoadingText(_lc
[I_LOAD_DOOR_MAP
], i
-1, False);
510 if PanelArray
[a
].Active
then
512 PanelArray
[a
].Active
:= False;
513 m
:= Length(gDoorMap
);
514 SetLength(gDoorMap
, m
+1);
515 SetLength(gDoorMap
[m
], 1);
516 gDoorMap
[m
, 0] := PanelArray
[a
].PanelID
;
524 if PanelArray
[b
].Active
then
525 for c
:= 0 to High(gDoorMap
[m
]) do
526 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
527 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
528 g_CollideAround(PanelArray
[b
].X
, PanelArray
[b
].Y
,
529 PanelArray
[b
].Width
, PanelArray
[b
].Height
,
530 gWalls
[gDoorMap
[m
, c
]].X
,
531 gWalls
[gDoorMap
[m
, c
]].Y
,
532 gWalls
[gDoorMap
[m
, c
]].Width
,
533 gWalls
[gDoorMap
[m
, c
]].Height
) then
535 PanelArray
[b
].Active
:= False;
536 SetLength(gDoorMap
[m
],
537 Length(gDoorMap
[m
])+1);
538 gDoorMap
[m
, High(gDoorMap
[m
])] := PanelArray
[b
].PanelID
;
544 g_Game_StepLoading();
550 procedure CreateLiftMap();
552 PanelArray
: Array of record
557 a
, b
, c
, len
, i
, j
: Integer;
563 len
:= Length(gLifts
);
564 SetLength(PanelArray
, len
);
566 for a
:= 0 to len
-1 do
568 PanelArray
[a
].X
:= gLifts
[a
].X
;
569 PanelArray
[a
].Y
:= gLifts
[a
].Y
;
570 PanelArray
[a
].Width
:= gLifts
[a
].Width
;
571 PanelArray
[a
].Height
:= gLifts
[a
].Height
;
572 PanelArray
[a
].Active
:= True;
575 SetLength(gLiftMap
, len
);
578 g_Game_SetLoadingText(_lc
[I_LOAD_LIFT_MAP
], len
-1, False);
580 for a
:= 0 to len
-1 do
581 if PanelArray
[a
].Active
then
583 PanelArray
[a
].Active
:= False;
584 SetLength(gLiftMap
[i
], 32);
592 for b
:= 0 to len
-1 do
593 if PanelArray
[b
].Active
then
595 if g_CollideAround(PanelArray
[b
].X
,
598 PanelArray
[b
].Height
,
599 PanelArray
[gLiftMap
[i
, c
]].X
,
600 PanelArray
[gLiftMap
[i
, c
]].Y
,
601 PanelArray
[gLiftMap
[i
, c
]].Width
,
602 PanelArray
[gLiftMap
[i
, c
]].Height
) then
604 PanelArray
[b
].Active
:= False;
606 if j
> High(gLiftMap
[i
]) then
607 SetLength(gLiftMap
[i
],
608 Length(gLiftMap
[i
])+32);
617 SetLength(gLiftMap
[i
], j
+1);
620 g_Game_StepLoading();
623 SetLength(gLiftMap
, i
);
628 function CreatePanel(PanelRec
: TPanelRec_1
; AddTextures
: TAddTextureArray
;
629 CurTex
: Integer; sav
: Boolean): Integer;
632 panels
: ^TPanelArray
;
636 case PanelRec
.PanelType
of
637 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
640 panels
:= @gRenderBackgrounds
;
642 panels
:= @gRenderForegrounds
;
651 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
:
654 panels
:= @gBlockMon
;
659 len
:= Length(panels
^);
660 SetLength(panels
^, len
+ 1);
662 panels
^[len
] := TPanel
.Create(PanelRec
, AddTextures
, CurTex
, Textures
);
663 panels
^[len
].ArrIdx
:= len
;
664 panels
^[len
].tag
:= panelTypeToTag(PanelRec
.PanelType
);
666 panels
^[len
].SaveIt
:= True;
670 len
:= Length(PanelByID
);
671 SetLength(PanelByID
, len
+ 1);
672 PanelByID
[len
].PWhere
:= panels
;
673 PanelByID
[len
].PArrID
:= Result
;
676 function CreateNullTexture(RecName
: String): Integer;
678 SetLength(Textures
, Length(Textures
)+1);
679 result
:= High(Textures
);
681 with Textures
[High(Textures
)] do
683 TextureName
:= RecName
;
687 TextureID
:= TEXTURE_NONE
;
691 function CreateTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
694 TextureData
: Pointer;
695 WADName
, txname
: String;
696 a
, ResLength
: Integer;
700 if Textures
<> nil then
701 for a
:= 0 to High(Textures
) do
702 if Textures
[a
].TextureName
= RecName
then
703 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
708 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
709 if (RecName
= TEXTURE_NAME_WATER
) or
710 (RecName
= TEXTURE_NAME_ACID1
) or
711 (RecName
= TEXTURE_NAME_ACID2
) then
713 SetLength(Textures
, Length(Textures
)+1);
715 with Textures
[High(Textures
)] do
717 TextureName
:= RecName
;
719 if TextureName
= TEXTURE_NAME_WATER
then
720 TextureID
:= TEXTURE_SPECIAL_WATER
722 if TextureName
= TEXTURE_NAME_ACID1
then
723 TextureID
:= TEXTURE_SPECIAL_ACID1
725 if TextureName
= TEXTURE_NAME_ACID2
then
726 TextureID
:= TEXTURE_SPECIAL_ACID2
;
731 result
:= High(Textures
);
735 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
736 WADName
:= g_ExtractWadName(RecName
);
738 WAD
:= TWADFile
.Create();
740 if WADName
<> '' then
741 WADName
:= GameDir
+'/wads/'+WADName
745 WAD
.ReadFile(WADName
);
749 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
751 FreeMem(TextureData);
752 RecName := 'COMMON\ALIEN';
756 if WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureData
, ResLength
) then
758 SetLength(Textures
, Length(Textures
)+1);
759 if not e_CreateTextureMem(TextureData
, ResLength
, Textures
[High(Textures
)].TextureID
) then
761 e_GetTextureSize(Textures
[High(Textures
)].TextureID
,
762 @Textures
[High(Textures
)].Width
,
763 @Textures
[High(Textures
)].Height
);
764 FreeMem(TextureData
);
765 Textures
[High(Textures
)].TextureName
:= {RecName}txname
;
766 Textures
[High(Textures
)].Anim
:= False;
768 result
:= High(Textures
);
770 else // Íåò òàêîãî ðåóñðñà â WAD'å
772 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
775 e_WriteLog(Format('Error loading texture %s', [RecName
]), MSG_WARNING
);
776 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
783 function CreateAnimTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
786 TextureWAD
: PChar = nil;
787 TextData
: Pointer = nil;
788 TextureData
: Pointer = nil;
792 TextureResource
: String;
793 _width
, _height
, _framecount
, _speed
: Integer;
794 _backanimation
: Boolean;
796 ia
: TDynImageDataArray
= nil;
797 f
, c
, frdelay
, frloop
: Integer;
801 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
803 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
804 WADName
:= g_ExtractWadName(RecName
);
806 WAD
:= TWADFile
.Create();
808 if WADName
<> '' then
809 WADName
:= GameDir
+'/wads/'+WADName
813 WAD
.ReadFile(WADName
);
815 if not WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureWAD
, ResLength
) then
819 e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
820 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
826 if WADName = Map then
828 //FreeMem(TextureWAD);
829 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
835 if ResLength
< 6 then
837 e_WriteLog(Format('Animated texture file "%s" too short', [RecName
]), MSG_WARNING
);
841 // ýòî ïòèöà? ýòî ñàìîë¸ò?
842 if (TextureWAD
[0] = 'D') and (TextureWAD
[1] = 'F') and
843 (TextureWAD
[2] = 'W') and (TextureWAD
[3] = 'A') and (TextureWAD
[4] = 'D') then
845 // íåò, ýòî ñóïåðìåí!
846 if not WAD
.ReadMemory(TextureWAD
, ResLength
) then
848 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName
]), MSG_WARNING
);
852 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
853 if not WAD
.GetResource('TEXT/ANIM', TextData
, ResLength
) then
855 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName
]), MSG_WARNING
);
859 cfg
:= TConfig
.CreateMem(TextData
, ResLength
);
861 TextureResource
:= cfg
.ReadStr('', 'resource', '');
862 if TextureResource
= '' then
864 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName
]), MSG_WARNING
);
868 _width
:= cfg
.ReadInt('', 'framewidth', 0);
869 _height
:= cfg
.ReadInt('', 'frameheight', 0);
870 _framecount
:= cfg
.ReadInt('', 'framecount', 0);
871 _speed
:= cfg
.ReadInt('', 'waitcount', 0);
872 _backanimation
:= cfg
.ReadBool('', 'backanimation', False);
877 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
878 if not WAD
.GetResource('TEXTURES/'+TextureResource
, TextureData
, ResLength
) then
880 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName
, 'TEXTURES/'+TextureResource
]), MSG_WARNING
);
887 SetLength(Textures
, Length(Textures
)+1);
888 with Textures
[High(Textures
)] do
890 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
891 if g_Frames_CreateMemory(@FramesID
, '', TextureData
, ResLength
, _width
, _height
, _framecount
, _backanimation
) then
893 TextureName
:= RecName
;
897 FramesCount
:= _framecount
;
899 result
:= High(Textures
);
903 if log
then e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
909 // try animated image
911 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
912 if length(imgfmt) = 0 then
914 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
918 GlobalMetadata
.ClearMetaItems();
919 GlobalMetadata
.ClearMetaItemsForSaving();
920 if not LoadMultiImageFromMemory(TextureWAD
, ResLength
, ia
) then
922 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName
]), MSG_WARNING
);
925 if length(ia
) = 0 then
927 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName
]), MSG_WARNING
);
934 _width
:= ia
[0].width
;
935 _height
:= ia
[0].height
;
936 _framecount
:= length(ia
);
938 _backanimation
:= false;
941 if GlobalMetadata
.HasMetaItem(SMetaFrameDelay
) then
943 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
945 f
:= GlobalMetadata
.MetaItems
[SMetaFrameDelay
];
947 if f
< 0 then f
:= 0;
950 if c
< 13 then c
:= 0 else c
:= 1;
952 if f
< 1 then f
:= 1 else if f
> 255 then f
:= 255;
957 if GlobalMetadata
.HasMetaItem(SMetaAnimationLoops
) then
959 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
961 f
:= GlobalMetadata
.MetaItems
[SMetaAnimationLoops
];
963 if f
<> 0 then _backanimation
:= true; // non-infinite looping == forth-and-back
967 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
968 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
969 f
:= ord(_backanimation
);
970 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
);
972 SetLength(Textures
, Length(Textures
)+1);
973 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
974 if g_CreateFramesImg(ia
, @Textures
[High(Textures
)].FramesID
, '', _backanimation
) then
976 Textures
[High(Textures
)].TextureName
:= RecName
;
977 Textures
[High(Textures
)].Width
:= _width
;
978 Textures
[High(Textures
)].Height
:= _height
;
979 Textures
[High(Textures
)].Anim
:= True;
980 Textures
[High(Textures
)].FramesCount
:= length(ia
);
981 Textures
[High(Textures
)].Speed
:= _speed
;
982 result
:= High(Textures
);
983 //writeln(' CREATED!');
987 if log
then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName
]), MSG_WARNING
);
991 for f
:= 0 to High(ia
) do FreeImage(ia
[f
]);
994 if TextureWAD
<> nil then FreeMem(TextureWAD
);
995 if TextData
<> nil then FreeMem(TextData
);
996 if TextureData
<> nil then FreeMem(TextureData
);
1000 procedure CreateItem(Item
: TItemRec_1
);
1002 if g_Game_IsClient
then Exit
;
1004 if (not (gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
])) and
1005 ByteBool(Item
.Options
and ITEM_OPTION_ONLYDM
) then
1008 g_Items_Create(Item
.X
, Item
.Y
, Item
.ItemType
, ByteBool(Item
.Options
and ITEM_OPTION_FALL
),
1009 gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
, GM_COOP
]);
1012 procedure CreateArea(Area
: TAreaRec_1
);
1017 case Area
.AreaType
of
1018 AREA_DMPOINT
, AREA_PLAYERPOINT1
, AREA_PLAYERPOINT2
,
1019 AREA_REDTEAMPOINT
, AREA_BLUETEAMPOINT
:
1021 SetLength(RespawnPoints
, Length(RespawnPoints
)+1);
1022 with RespawnPoints
[High(RespawnPoints
)] do
1026 Direction
:= TDirection(Area
.Direction
);
1028 case Area
.AreaType
of
1029 AREA_DMPOINT
: PointType
:= RESPAWNPOINT_DM
;
1030 AREA_PLAYERPOINT1
: PointType
:= RESPAWNPOINT_PLAYER1
;
1031 AREA_PLAYERPOINT2
: PointType
:= RESPAWNPOINT_PLAYER2
;
1032 AREA_REDTEAMPOINT
: PointType
:= RESPAWNPOINT_RED
;
1033 AREA_BLUETEAMPOINT
: PointType
:= RESPAWNPOINT_BLUE
;
1038 AREA_REDFLAG
, AREA_BLUEFLAG
:
1040 if Area
.AreaType
= AREA_REDFLAG
then a
:= FLAG_RED
else a
:= FLAG_BLUE
;
1042 if FlagPoints
[a
] <> nil then Exit
;
1046 with FlagPoints
[a
]^ do
1048 X
:= Area
.X
-FLAGRECT
.X
;
1049 Y
:= Area
.Y
-FLAGRECT
.Y
;
1050 Direction
:= TDirection(Area
.Direction
);
1056 FLAG_RED
: g_Frames_Get(id
, 'FRAMES_FLAG_RED');
1057 FLAG_BLUE
: g_Frames_Get(id
, 'FRAMES_FLAG_BLUE');
1060 Animation
:= TAnimation
.Create(id
, True, 8);
1061 Obj
.Rect
:= FLAGRECT
;
1069 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1070 with DOMFlagPoints[High(DOMFlagPoints)] do
1074 Direction := TDirection(Area.Direction);
1077 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1082 procedure CreateTrigger(Trigger
: TTriggerRec_1
; fTexturePanel1Type
, fTexturePanel2Type
: Word);
1086 if g_Game_IsClient
and not (Trigger
.TriggerType
in [TRIGGER_SOUND
, TRIGGER_MUSIC
]) then Exit
;
1092 Width
:= Trigger
.Width
;
1093 Height
:= Trigger
.Height
;
1094 Enabled
:= ByteBool(Trigger
.Enabled
);
1095 TexturePanel
:= Trigger
.TexturePanel
;
1096 TexturePanelType
:= fTexturePanel1Type
;
1097 ShotPanelType
:= fTexturePanel2Type
;
1098 TriggerType
:= Trigger
.TriggerType
;
1099 ActivateType
:= Trigger
.ActivateType
;
1100 Keys
:= Trigger
.Keys
;
1101 Data
.Default
:= Trigger
.DATA
;
1104 g_Triggers_Create(_trigger
);
1107 procedure CreateMonster(monster
: TMonsterRec_1
);
1112 if g_Game_IsClient
then Exit
;
1114 if (gGameSettings
.GameType
= GT_SINGLE
)
1115 or LongBool(gGameSettings
.Options
and GAME_OPTION_MONSTERS
) then
1117 mon
:= g_Monsters_Create(monster
.MonsterType
, monster
.X
, monster
.Y
, TDirection(monster
.Direction
));
1119 if gTriggers
<> nil then
1121 for a
:= 0 to High(gTriggers
) do
1123 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1125 if (gTriggers
[a
].Data
.MonsterID
-1) = mon
.StartID
then mon
.AddTrigger(a
);
1130 if monster
.MonsterType
<> MONSTER_BARREL
then Inc(gTotalMonsters
);
1134 procedure g_Map_ReAdd_DieTriggers();
1136 function monsDieTrig (mon
: TMonster
): Boolean;
1140 result
:= false; // don't stop
1141 mon
.ClearTriggers();
1142 for a
:= 0 to High(gTriggers
) do
1144 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1146 if (gTriggers
[a
].Data
.MonsterID
-1) = mon
.StartID
then mon
.AddTrigger(a
);
1152 if g_Game_IsClient
then Exit
;
1154 g_Mons_ForEach(monsDieTrig
);
1157 function extractWadName(resourceName
: string): string;
1161 posN
:= Pos(':', resourceName
);
1163 Result
:= Copy(resourceName
, 0, posN
-1)
1168 procedure addResToExternalResList(res
: string);
1170 res
:= extractWadName(res
);
1171 if (res
<> '') and (gExternalResources
.IndexOf(res
) = -1) then
1172 gExternalResources
.Add(res
);
1175 procedure generateExternalResourcesList(mapReader
: TMapReader_1
);
1177 textures
: TTexturesRec1Array
;
1178 mapHeader
: TMapHeaderRec_1
;
1180 resFile
: String = '';
1182 if gExternalResources
= nil then
1183 gExternalResources
:= TStringList
.Create
;
1185 gExternalResources
.Clear
;
1186 textures
:= mapReader
.GetTextures();
1187 for i
:= 0 to High(textures
) do
1189 addResToExternalResList(resFile
);
1194 mapHeader
:= mapReader
.GetMapHeader
;
1196 addResToExternalResList(mapHeader
.MusicName
);
1197 addResToExternalResList(mapHeader
.SkyName
);
1200 procedure mapCreateGrid ();
1202 mapX0
: Integer = $3fffffff;
1203 mapY0
: Integer = $3fffffff;
1204 mapX1
: Integer = -$3fffffff;
1205 mapY1
: Integer = -$3fffffff;
1207 procedure calcBoundingBox (constref panels
: TPanelArray
);
1212 for idx
:= 0 to High(panels
) do
1215 if not pan
.visvalid
then continue
;
1216 if (pan
.Width
< 1) or (pan
.Height
< 1) then continue
;
1217 if (mapX0
> pan
.x0
) then mapX0
:= pan
.x0
;
1218 if (mapY0
> pan
.y0
) then mapY0
:= pan
.y0
;
1219 if (mapX1
< pan
.x1
) then mapX1
:= pan
.x1
;
1220 if (mapY1
< pan
.y1
) then mapY1
:= pan
.y1
;
1224 procedure addPanelsToGrid (constref panels
: TPanelArray
; tag
: Integer);
1229 tag
:= panelTypeToTag(tag
);
1230 for idx
:= High(panels
) downto 0 do
1234 if not pan
.visvalid
then continue
;
1235 gMapGrid
.insertBody(pan
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
, tag
);
1236 //mapTree.insertObject(pan, tag, true); // as static object
1246 calcBoundingBox(gWalls
);
1247 calcBoundingBox(gRenderBackgrounds
);
1248 calcBoundingBox(gRenderForegrounds
);
1249 calcBoundingBox(gWater
);
1250 calcBoundingBox(gAcid1
);
1251 calcBoundingBox(gAcid2
);
1252 calcBoundingBox(gSteps
);
1253 calcBoundingBox(gLifts
);
1254 calcBoundingBox(gBlockMon
);
1256 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0
, mapY0
, mapX1
, mapY1
]), MSG_WARNING
);
1258 gMapGrid
:= TPanelGrid
.Create(mapX0
, mapY0
, mapX1
-mapX0
+1, mapY1
-mapY0
+1);
1259 //mapTree := TDynAABBTreeMap.Create();
1261 addPanelsToGrid(gWalls
, PANEL_WALL
);
1262 addPanelsToGrid(gWalls
, PANEL_CLOSEDOOR
);
1263 addPanelsToGrid(gWalls
, PANEL_OPENDOOR
);
1264 addPanelsToGrid(gRenderBackgrounds
, PANEL_BACK
);
1265 addPanelsToGrid(gRenderForegrounds
, PANEL_FORE
);
1266 addPanelsToGrid(gWater
, PANEL_WATER
);
1267 addPanelsToGrid(gAcid1
, PANEL_ACID1
);
1268 addPanelsToGrid(gAcid2
, PANEL_ACID2
);
1269 addPanelsToGrid(gSteps
, PANEL_STEP
);
1270 addPanelsToGrid(gLifts
, PANEL_LIFTUP
); // it doesn't matter which LIFT type is used here
1271 addPanelsToGrid(gBlockMon
, PANEL_BLOCKMON
);
1273 gMapGrid
.dumpStats();
1274 //e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1275 //mapTree.forEachLeaf(nil);
1278 function g_Map_Load(Res
: String): Boolean;
1280 DefaultMusRes
= 'Standart.wad:STDMUS\MUS1';
1281 DefaultSkyRes
= 'Standart.wad:STDSKY\SKY0';
1284 MapReader
: TMapReader_1
;
1285 Header
: TMapHeaderRec_1
;
1286 _textures
: TTexturesRec1Array
;
1287 _texnummap
: array of Integer; // `_textures` -> `Textures`
1288 panels
: TPanelsRec1Array
;
1289 items
: TItemsRec1Array
;
1290 monsters
: TMonsterRec1Array
;
1291 areas
: TAreasRec1Array
;
1292 triggers
: TTriggersRec1Array
;
1293 a
, b
, c
, k
: Integer;
1295 AddTextures
: TAddTextureArray
;
1296 texture
: TTextureRec_1
;
1297 TriggersTable
: Array of record
1298 TexturePanel
: Integer;
1303 FileName
, mapResName
, s
, TexName
: String;
1306 ok
, isAnim
, trigRef
: Boolean;
1307 CurTex
, ntn
: Integer;
1316 gMapInfo
.Map
:= Res
;
1317 TriggersTable
:= nil;
1318 FillChar(texture
, SizeOf(texture
), 0);
1320 sfsGCDisable(); // temporary disable removing of temporary volumes
1323 FileName
:= g_ExtractWadName(Res
);
1324 e_WriteLog('Loading map WAD: '+FileName
, MSG_NOTIFY
);
1325 g_Game_SetLoadingText(_lc
[I_LOAD_WAD_FILE
], 0, False);
1327 WAD
:= TWADFile
.Create();
1328 if not WAD
.ReadFile(FileName
) then
1330 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_WAD
], [FileName
]));
1334 //k8: why loader ignores path here?
1335 mapResName
:= g_ExtractFileName(Res
);
1336 if not WAD
.GetMapResource(mapResName
, Data
, Len
) then
1338 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_RES
], [mapResName
]));
1346 e_WriteLog('Loading map: '+mapResName
, MSG_NOTIFY
);
1347 g_Game_SetLoadingText(_lc
[I_LOAD_MAP
], 0, False);
1348 MapReader
:= TMapReader_1
.Create();
1350 if not MapReader
.LoadMap(Data
) then
1352 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]));
1359 generateExternalResourcesList(MapReader
);
1360 // Çàãðóçêà òåêñòóð:
1361 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], 0, False);
1362 _textures
:= MapReader
.GetTextures();
1365 // Äîáàâëåíèå òåêñòóð â Textures[]:
1366 if _textures
<> nil then
1368 e_WriteLog(' Loading textures:', MSG_NOTIFY
);
1369 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], High(_textures
), False);
1370 SetLength(_texnummap
, length(_textures
));
1372 for a
:= 0 to High(_textures
) do
1375 CopyMemory(@s
[1], @_textures
[a
].Resource
[0], 64);
1376 for b
:= 1 to Length(s
) do
1382 e_WriteLog(Format(' Loading texture #%d: %s', [a
, s
]), MSG_NOTIFY
);
1383 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1384 // Àíèìèðîâàííàÿ òåêñòóðà:
1385 if ByteBool(_textures
[a
].Anim
) then
1387 ntn
:= CreateAnimTexture(_textures
[a
].Resource
, FileName
, True);
1390 g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_ANIM
], [s
]));
1391 ntn
:= CreateNullTexture(_textures
[a
].Resource
);
1394 else // Îáû÷íàÿ òåêñòóðà:
1395 ntn
:= CreateTexture(_textures
[a
].Resource
, FileName
, True);
1398 g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_SIMPLE
], [s
]));
1399 ntn
:= CreateNullTexture(_textures
[a
].Resource
);
1402 _texnummap
[a
] := ntn
; // fix texture number
1403 g_Game_StepLoading();
1407 // Çàãðóçêà òðèããåðîâ:
1408 gTriggerClientID
:= 0;
1409 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1410 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS
], 0, False);
1411 triggers
:= MapReader
.GetTriggers();
1413 // Çàãðóçêà ïàíåëåé:
1414 e_WriteLog(' Loading panels...', MSG_NOTIFY
);
1415 g_Game_SetLoadingText(_lc
[I_LOAD_PANELS
], 0, False);
1416 panels
:= MapReader
.GetPanels();
1418 // check texture numbers for panels
1419 for a
:= 0 to High(panels
) do
1421 if panels
[a
].TextureNum
> High(_textures
) then
1423 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR
);
1427 panels
[a
].TextureNum
:= _texnummap
[panels
[a
].TextureNum
];
1430 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1431 if triggers
<> nil then
1433 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY
);
1434 SetLength(TriggersTable
, Length(triggers
));
1435 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS_TABLE
], High(TriggersTable
), False);
1437 for a
:= 0 to High(TriggersTable
) do
1439 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1440 TriggersTable
[a
].TexturePanel
:= triggers
[a
].TexturePanel
;
1442 if triggers
[a
].TriggerType
in [TRIGGER_LIFTUP
, TRIGGER_LIFTDOWN
, TRIGGER_LIFT
] then
1443 TriggersTable
[a
].LiftPanel
:= TTriggerData(triggers
[a
].DATA
).PanelID
1445 TriggersTable
[a
].LiftPanel
:= -1;
1447 if triggers
[a
].TriggerType
in [TRIGGER_OPENDOOR
,
1448 TRIGGER_CLOSEDOOR
, TRIGGER_DOOR
, TRIGGER_DOOR5
,
1449 TRIGGER_CLOSETRAP
, TRIGGER_TRAP
] then
1450 TriggersTable
[a
].DoorPanel
:= TTriggerData(triggers
[a
].DATA
).PanelID
1452 TriggersTable
[a
].DoorPanel
:= -1;
1454 if triggers
[a
].TriggerType
= TRIGGER_SHOT
then
1455 TriggersTable
[a
].ShotPanel
:= TTriggerData(triggers
[a
].DATA
).ShotPanelID
1457 TriggersTable
[a
].ShotPanel
:= -1;
1459 g_Game_StepLoading();
1464 if panels
<> nil then
1466 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY
);
1467 g_Game_SetLoadingText(_lc
[I_LOAD_LINK_TRIGGERS
], High(panels
), False);
1469 for a
:= 0 to High(panels
) do
1471 SetLength(AddTextures
, 0);
1474 if _textures
<> nil then
1476 texture
:= _textures
[panels
[a
].TextureNum
];
1484 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1485 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1487 if (TriggersTable
<> nil) and (_textures
<> nil) then
1488 for b
:= 0 to High(TriggersTable
) do
1489 if (TriggersTable
[b
].TexturePanel
= a
)
1490 or (TriggersTable
[b
].ShotPanel
= a
) then
1499 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1501 CopyMemory(@s
[1], @texture
.Resource
[0], 64);
1504 for c
:= Len
downto 1 do
1512 // Ñïåö-òåêñòóðû çàïðåùåíû:
1513 if g_Map_IsSpecialTexture(s
) then
1516 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1517 ok
:= g_Texture_NumNameFindStart(s
);
1519 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1520 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1523 k
:= NNF_NAME_BEFORE
;
1524 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1525 while ok
or (k
= NNF_NAME_BEFORE
) or
1526 (k
= NNF_NAME_EQUALS
) do
1528 k
:= g_Texture_NumNameFindNext(TexName
);
1530 if (k
= NNF_NAME_BEFORE
) or
1531 (k
= NNF_NAME_AFTER
) then
1533 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1534 if ByteBool(texture
.Anim
) then
1535 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1537 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1539 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1541 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1545 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1547 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1549 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1551 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1555 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1558 for c
:= 0 to High(Textures
) do
1559 if Textures
[c
].TextureName
= TexName
then
1561 SetLength(AddTextures
, Length(AddTextures
)+1);
1562 AddTextures
[High(AddTextures
)].Texture
:= c
;
1563 AddTextures
[High(AddTextures
)].Anim
:= isAnim
;
1569 if k
= NNF_NAME_EQUALS
then
1571 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1572 SetLength(AddTextures
, Length(AddTextures
)+1);
1573 AddTextures
[High(AddTextures
)].Texture
:= panels
[a
].TextureNum
;
1574 AddTextures
[High(AddTextures
)].Anim
:= ByteBool(texture
.Anim
);
1575 CurTex
:= High(AddTextures
);
1583 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1584 end; // if ok - ññûëàþòñÿ òðèããåðû
1588 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1589 SetLength(AddTextures
, 1);
1590 AddTextures
[0].Texture
:= panels
[a
].TextureNum
;
1591 AddTextures
[0].Anim
:= ByteBool(texture
.Anim
);
1595 //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);
1597 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1598 PanelID
:= CreatePanel(panels
[a
], AddTextures
, CurTex
, trigRef
);
1600 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1601 if TriggersTable
<> nil then
1602 for b
:= 0 to High(TriggersTable
) do
1604 // Òðèããåð äâåðè/ëèôòà:
1605 if (TriggersTable
[b
].LiftPanel
= a
) or
1606 (TriggersTable
[b
].DoorPanel
= a
) then
1607 TTriggerData(triggers
[b
].DATA
).PanelID
:= PanelID
;
1608 // Òðèããåð ñìåíû òåêñòóðû:
1609 if TriggersTable
[b
].TexturePanel
= a
then
1610 triggers
[b
].TexturePanel
:= PanelID
;
1611 // Òðèããåð "Òóðåëü":
1612 if TriggersTable
[b
].ShotPanel
= a
then
1613 TTriggerData(triggers
[b
].DATA
).ShotPanelID
:= PanelID
;
1616 g_Game_StepLoading();
1620 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1621 if (triggers
<> nil) and not gLoadGameMode
then
1623 e_WriteLog(' Creating triggers...', MSG_NOTIFY
);
1624 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_TRIGGERS
], 0, False);
1625 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1626 for a
:= 0 to High(triggers
) do
1628 if triggers
[a
].TexturePanel
<> -1 then
1629 b
:= panels
[TriggersTable
[a
].TexturePanel
].PanelType
1632 if (triggers
[a
].TriggerType
= TRIGGER_SHOT
) and
1633 (TTriggerData(triggers
[a
].DATA
).ShotPanelID
<> -1) then
1634 c
:= panels
[TriggersTable
[a
].ShotPanel
].PanelType
1637 CreateTrigger(triggers
[a
], b
, c
);
1641 // Çàãðóçêà ïðåäìåòîâ:
1642 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1643 g_Game_SetLoadingText(_lc
[I_LOAD_ITEMS
], 0, False);
1644 items
:= MapReader
.GetItems();
1646 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1647 if (items
<> nil) and not gLoadGameMode
then
1649 e_WriteLog(' Spawning items...', MSG_NOTIFY
);
1650 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_ITEMS
], 0, False);
1651 for a
:= 0 to High(items
) do
1652 CreateItem(Items
[a
]);
1655 // Çàãðóçêà îáëàñòåé:
1656 e_WriteLog(' Loading areas...', MSG_NOTIFY
);
1657 g_Game_SetLoadingText(_lc
[I_LOAD_AREAS
], 0, False);
1658 areas
:= MapReader
.GetAreas();
1660 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1661 if areas
<> nil then
1663 e_WriteLog(' Creating areas...', MSG_NOTIFY
);
1664 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_AREAS
], 0, False);
1665 for a
:= 0 to High(areas
) do
1666 CreateArea(areas
[a
]);
1669 // Çàãðóçêà ìîíñòðîâ:
1670 e_WriteLog(' Loading monsters...', MSG_NOTIFY
);
1671 g_Game_SetLoadingText(_lc
[I_LOAD_MONSTERS
], 0, False);
1672 monsters
:= MapReader
.GetMonsters();
1674 gTotalMonsters
:= 0;
1676 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1677 if (monsters
<> nil) and not gLoadGameMode
then
1679 e_WriteLog(' Spawning monsters...', MSG_NOTIFY
);
1680 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_MONSTERS
], 0, False);
1681 for a
:= 0 to High(monsters
) do
1682 CreateMonster(monsters
[a
]);
1685 // Çàãðóçêà îïèñàíèÿ êàðòû:
1686 e_WriteLog(' Reading map info...', MSG_NOTIFY
);
1687 g_Game_SetLoadingText(_lc
[I_LOAD_MAP_HEADER
], 0, False);
1688 Header
:= MapReader
.GetMapHeader();
1694 Name
:= Header
.MapName
;
1695 Description
:= Header
.MapDescription
;
1696 Author
:= Header
.MapAuthor
;
1697 MusicName
:= Header
.MusicName
;
1698 SkyName
:= Header
.SkyName
;
1699 Height
:= Header
.Height
;
1700 Width
:= Header
.Width
;
1704 if gMapInfo
.SkyName
<> '' then
1706 e_WriteLog(' Loading sky: ' + gMapInfo
.SkyName
, MSG_NOTIFY
);
1707 g_Game_SetLoadingText(_lc
[I_LOAD_SKY
], 0, False);
1708 FileName
:= g_ExtractWadName(gMapInfo
.SkyName
);
1710 if FileName
<> '' then
1711 FileName
:= GameDir
+'/wads/'+FileName
1714 FileName
:= g_ExtractWadName(Res
);
1717 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.SkyName
);
1718 if g_Texture_CreateWAD(BackID
, s
) then
1720 g_Game_SetupScreenSize();
1723 g_FatalError(Format(_lc
[I_GAME_ERROR_SKY
], [s
]));
1728 if gMapInfo
.MusicName
<> '' then
1730 e_WriteLog(' Loading music: ' + gMapInfo
.MusicName
, MSG_NOTIFY
);
1731 g_Game_SetLoadingText(_lc
[I_LOAD_MUSIC
], 0, False);
1732 FileName
:= g_ExtractWadName(gMapInfo
.MusicName
);
1734 if FileName
<> '' then
1735 FileName
:= GameDir
+'/wads/'+FileName
1738 FileName
:= g_ExtractWadName(Res
);
1741 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.MusicName
);
1742 if g_Sound_CreateWADEx(gMapInfo
.MusicName
, s
, True) then
1745 g_FatalError(Format(_lc
[I_GAME_ERROR_MUSIC
], [s
]));
1748 // Îñòàëüíûå óñòàíâêè:
1756 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1757 if not gLoadGameMode
then
1760 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1766 TriggersTable
:= nil;
1769 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1770 if ok
and (not gLoadGameMode
) then
1772 gMusic
.SetByName(gMapInfo
.MusicName
);
1776 gMusic
.SetByName('');
1778 sfsGCEnable(); // enable releasing unused volumes
1781 e_WriteLog('Creating map grid', MSG_NOTIFY
);
1784 e_WriteLog('Done loading map.', MSG_NOTIFY
);
1788 function g_Map_GetMapInfo(Res
: String): TMapInfo
;
1791 MapReader
: TMapReader_1
;
1792 Header
: TMapHeaderRec_1
;
1797 FillChar(Result
, SizeOf(Result
), 0);
1798 FileName
:= g_ExtractWadName(Res
);
1800 WAD
:= TWADFile
.Create();
1801 if not WAD
.ReadFile(FileName
) then
1807 //k8: it ignores path again
1808 if not WAD
.GetMapResource(g_ExtractFileName(Res
), Data
, Len
) then
1816 MapReader
:= TMapReader_1
.Create();
1818 if not MapReader
.LoadMap(Data
) then
1820 g_Console_Add(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]), True);
1821 ZeroMemory(@Header
, SizeOf(Header
));
1822 Result
.Name
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
1823 Result
.Description
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
1827 Header
:= MapReader
.GetMapHeader();
1828 Result
.Name
:= Header
.MapName
;
1829 Result
.Description
:= Header
.MapDescription
;
1836 Result
.Author
:= Header
.MapAuthor
;
1837 Result
.Height
:= Header
.Height
;
1838 Result
.Width
:= Header
.Width
;
1841 function g_Map_GetMapsList(WADName
: string): SArray
;
1848 WAD
:= TWADFile
.Create();
1849 if not WAD
.ReadFile(WADName
) then
1854 ResList
:= WAD
.GetMapResources();
1855 if ResList
<> nil then
1857 for a
:= 0 to High(ResList
) do
1859 SetLength(Result
, Length(Result
)+1);
1860 Result
[High(Result
)] := ResList
[a
];
1866 function g_Map_Exist(Res
: string): Boolean;
1869 FileName
, mnn
: string;
1875 FileName
:= addWadExtension(g_ExtractWadName(Res
));
1877 WAD
:= TWADFile
.Create
;
1878 if not WAD
.ReadFile(FileName
) then
1884 ResList
:= WAD
.GetMapResources();
1887 mnn
:= g_ExtractFileName(Res
);
1888 if ResList
<> nil then
1889 for a
:= 0 to High(ResList
) do if StrEquCI1251(ResList
[a
], mnn
) then
1896 procedure g_Map_Free();
1900 procedure FreePanelArray(var panels
: TPanelArray
);
1905 if panels
<> nil then
1907 for i
:= 0 to High(panels
) do
1920 RespawnPoints
:= nil;
1921 if FlagPoints
[FLAG_RED
] <> nil then
1923 Dispose(FlagPoints
[FLAG_RED
]);
1924 FlagPoints
[FLAG_RED
] := nil;
1926 if FlagPoints
[FLAG_BLUE
] <> nil then
1928 Dispose(FlagPoints
[FLAG_BLUE
]);
1929 FlagPoints
[FLAG_BLUE
] := nil;
1931 //DOMFlagPoints := nil;
1935 if Textures
<> nil then
1937 for a
:= 0 to High(Textures
) do
1938 if not g_Map_IsSpecialTexture(Textures
[a
].TextureName
) then
1939 if Textures
[a
].Anim
then
1940 g_Frames_DeleteByID(Textures
[a
].FramesID
)
1942 if Textures
[a
].TextureID
<> TEXTURE_NONE
then
1943 e_DeleteTexture(Textures
[a
].TextureID
);
1948 FreePanelArray(gWalls
);
1949 FreePanelArray(gRenderBackgrounds
);
1950 FreePanelArray(gRenderForegrounds
);
1951 FreePanelArray(gWater
);
1952 FreePanelArray(gAcid1
);
1953 FreePanelArray(gAcid2
);
1954 FreePanelArray(gSteps
);
1955 FreePanelArray(gLifts
);
1956 FreePanelArray(gBlockMon
);
1958 if BackID
<> DWORD(-1) then
1962 e_DeleteTexture(BackID
);
1963 BackID
:= DWORD(-1);
1966 g_Game_StopAllSounds(False);
1968 g_Sound_Delete(gMapInfo
.MusicName
);
1970 gMapInfo
.Name
:= '';
1971 gMapInfo
.Description
:= '';
1972 gMapInfo
.MusicName
:= '';
1973 gMapInfo
.Height
:= 0;
1974 gMapInfo
.Width
:= 0;
1982 procedure g_Map_Update();
1988 procedure UpdatePanelArray(var panels
: TPanelArray
);
1993 if panels
<> nil then
1994 for i
:= 0 to High(panels
) do
1999 UpdatePanelArray(gWalls
);
2000 UpdatePanelArray(gRenderBackgrounds
);
2001 UpdatePanelArray(gRenderForegrounds
);
2002 UpdatePanelArray(gWater
);
2003 UpdatePanelArray(gAcid1
);
2004 UpdatePanelArray(gAcid2
);
2005 UpdatePanelArray(gSteps
);
2007 if gGameSettings
.GameMode
= GM_CTF
then
2009 for a
:= FLAG_RED
to FLAG_BLUE
do
2010 if not (gFlags
[a
].State
in [FLAG_STATE_NONE
, FLAG_STATE_CAPTURED
]) then
2013 if gFlags
[a
].Animation
<> nil then
2014 gFlags
[a
].Animation
.Update();
2016 m
:= g_Obj_Move(@Obj
, True, True);
2018 if gTime
mod (GAME_TICK
*2) <> 0 then
2021 // Ñîïðîòèâëåíèå âîçäóõà:
2022 Obj
.Vel
.X
:= z_dec(Obj
.Vel
.X
, 1);
2024 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2025 if ((Count
= 0) or ByteBool(m
and MOVE_FALLOUT
)) and g_Game_IsServer
then
2028 gFlags
[a
].CaptureTime
:= 0;
2029 if a
= FLAG_RED
then
2030 s
:= _lc
[I_PLAYER_FLAG_RED
]
2032 s
:= _lc
[I_PLAYER_FLAG_BLUE
];
2033 g_Game_Message(Format(_lc
[I_MESSAGE_FLAG_RETURN
], [AnsiUpperCase(s
)]), 144);
2035 if g_Game_IsNet
then
2036 MH_SEND_FlagEvent(FLAG_STATE_RETURNED
, a
, 0);
2043 // Èãðîê áåðåò ôëàã:
2044 if gPlayers
<> nil then
2046 j
:= Random(Length(gPlayers
)) - 1;
2048 for d
:= 0 to High(gPlayers
) do
2051 if j
> High(gPlayers
) then
2054 if gPlayers
[j
] <> nil then
2055 if gPlayers
[j
].Live
and
2056 g_Obj_Collide(@Obj
, @gPlayers
[j
].Obj
) then
2058 if gPlayers
[j
].GetFlag(a
) then
2069 procedure g_Map_DrawPanels (PanelType
: Word);
2071 procedure DrawPanels (constref panels
: TPanelArray
; drawDoors
: Boolean=False);
2075 if (panels
<> nil) then
2077 // alas, no visible set
2078 for idx
:= 0 to High(panels
) do
2080 if not (drawDoors
xor panels
[idx
].Door
) then panels
[idx
].Draw();
2087 PANEL_WALL
: DrawPanels(gWalls
);
2088 PANEL_CLOSEDOOR
: DrawPanels(gWalls
, True);
2089 PANEL_BACK
: DrawPanels(gRenderBackgrounds
);
2090 PANEL_FORE
: DrawPanels(gRenderForegrounds
);
2091 PANEL_WATER
: DrawPanels(gWater
);
2092 PANEL_ACID1
: DrawPanels(gAcid1
);
2093 PANEL_ACID2
: DrawPanels(gAcid2
);
2094 PANEL_STEP
: DrawPanels(gSteps
);
2100 procedure g_Map_CollectDrawPanels (x0
, y0
, wdt
, hgt
: Integer);
2101 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2103 result
:= false; // don't stop, ever
2104 if ((tag
and GridTagDoor
) <> 0) <> pan
.Door
then exit
;
2105 gDrawPanelList
.insert(pan
);
2110 //tagmask := panelTypeToTag(PanelType);
2112 {if gdbg_map_use_tree_draw then
2114 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2118 gMapGrid
.forEachInAABB(x0
, y0
, wdt
, hgt
, checker
, (GridTagBack
or GridTagStep
or GridTagWall
or GridTagDoor
or GridTagAcid1
or GridTagAcid2
or GridTagWater
or GridTagFore
));
2120 // list will be rendered in `g_game.DrawPlayer()`
2124 procedure g_Map_DrawPanelShadowVolumes(lightX
: Integer; lightY
: Integer; radius
: Integer);
2125 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2127 result
:= false; // don't stop, ever
2128 pan
.DrawShadowVolume(lightX
, lightY
, radius
);
2132 {if gdbg_map_use_tree_draw then
2134 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2138 gMapGrid
.forEachInAABB(lightX
-radius
, lightY
-radius
, radius
*2, radius
*2, checker
, (GridTagWall
or GridTagDoor
));
2143 procedure g_Map_DrawBack(dx
, dy
: Integer);
2145 if gDrawBackGround
and (BackID
<> DWORD(-1)) then
2146 e_DrawSize(BackID
, dx
, dy
, 0, False, False, gBackSize
.X
, gBackSize
.Y
)
2148 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
2151 function g_Map_CollidePanelOld(X
, Y
: Integer; Width
, Height
: Word;
2152 PanelType
: Word; b1x3
: Boolean=false): Boolean;
2158 if WordBool(PanelType
and PANEL_WALL
) then
2159 if gWalls
<> nil then
2164 if gWalls
[a
].Enabled
and
2165 g_Collide(X
, Y
, Width
, Height
,
2166 gWalls
[a
].X
, gWalls
[a
].Y
,
2167 gWalls
[a
].Width
, gWalls
[a
].Height
) then
2174 if WordBool(PanelType
and PANEL_WATER
) then
2175 if gWater
<> nil then
2180 if g_Collide(X
, Y
, Width
, Height
,
2181 gWater
[a
].X
, gWater
[a
].Y
,
2182 gWater
[a
].Width
, gWater
[a
].Height
) then
2189 if WordBool(PanelType
and PANEL_ACID1
) then
2190 if gAcid1
<> nil then
2195 if g_Collide(X
, Y
, Width
, Height
,
2196 gAcid1
[a
].X
, gAcid1
[a
].Y
,
2197 gAcid1
[a
].Width
, gAcid1
[a
].Height
) then
2204 if WordBool(PanelType
and PANEL_ACID2
) then
2205 if gAcid2
<> nil then
2210 if g_Collide(X
, Y
, Width
, Height
,
2211 gAcid2
[a
].X
, gAcid2
[a
].Y
,
2212 gAcid2
[a
].Width
, gAcid2
[a
].Height
) then
2219 if WordBool(PanelType
and PANEL_STEP
) then
2220 if gSteps
<> nil then
2225 if g_Collide(X
, Y
, Width
, Height
,
2226 gSteps
[a
].X
, gSteps
[a
].Y
,
2227 gSteps
[a
].Width
, gSteps
[a
].Height
) then
2234 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then
2235 if gLifts
<> nil then
2240 if ((WordBool(PanelType
and (PANEL_LIFTUP
)) and (gLifts
[a
].LiftType
= 0)) or
2241 (WordBool(PanelType
and (PANEL_LIFTDOWN
)) and (gLifts
[a
].LiftType
= 1)) or
2242 (WordBool(PanelType
and (PANEL_LIFTLEFT
)) and (gLifts
[a
].LiftType
= 2)) or
2243 (WordBool(PanelType
and (PANEL_LIFTRIGHT
)) and (gLifts
[a
].LiftType
= 3))) and
2244 g_Collide(X
, Y
, Width
, Height
,
2245 gLifts
[a
].X
, gLifts
[a
].Y
,
2246 gLifts
[a
].Width
, gLifts
[a
].Height
) then
2253 if WordBool(PanelType
and PANEL_BLOCKMON
) then
2254 if gBlockMon
<> nil then
2256 h
:= High(gBlockMon
);
2260 ((gBlockMon
[a
].Width
+ gBlockMon
[a
].Height
) >= 64) ) and
2261 g_Collide(X
, Y
, Width
, Height
,
2262 gBlockMon
[a
].X
, gBlockMon
[a
].Y
,
2263 gBlockMon
[a
].Width
, gBlockMon
[a
].Height
) then
2271 function g_Map_CollideLiquid_TextureOld(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2275 function checkPanels (constref panels
: TPanelArray
): Boolean;
2280 if panels
= nil then exit
;
2281 for a
:= 0 to High(panels
) do
2283 if g_Collide(X
, Y
, Width
, Height
, panels
[a
].X
, panels
[a
].Y
, panels
[a
].Width
, panels
[a
].Height
) then
2286 texid
:= panels
[a
].GetTextureID();
2293 texid
:= TEXTURE_NONE
;
2295 if not checkPanels(gWater
) then
2296 if not checkPanels(gAcid1
) then
2297 if not checkPanels(gAcid2
) then exit
;
2302 function g_Map_CollidePanel(X
, Y
: Integer; Width
, Height
: Word; PanelType
: Word; b1x3
: Boolean): Boolean;
2303 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2305 result
:= false; // don't stop, ever
2307 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
2309 if not pan
.Enabled
then exit
;
2312 if ((tag
and GridTagLift
) <> 0) then
2315 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
2316 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
2317 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
2318 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3))) {and
2319 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2323 if ((tag
and GridTagBlockMon
) <> 0) then
2325 result
:= ((not b1x3
) or (pan
.Width
+pan
.Height
>= 64)) and g_Collide(X
, Y
, Width
, Height
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
);
2330 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2335 tagmask
: Integer = 0;
2337 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
2338 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
2339 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
2340 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
2341 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
2342 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
2343 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
2345 if (tagmask
= 0) then begin result
:= false; exit
; end; // just in case
2347 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('*solids');
2348 if gdbg_map_use_accel_coldet
then
2350 {if gdbg_map_use_tree_coldet then
2352 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2353 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2354 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2356 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2357 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2362 if (Width
= 1) and (Height
= 1) then
2364 result
:= gMapGrid
.forEachAtPoint(X
, Y
, checker
, tagmask
);
2368 result
:= gMapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, tagmask
);
2374 result
:= g_Map_CollidePanelOld(X
, Y
, Width
, Height
, PanelType
, b1x3
);
2376 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2380 function g_Map_CollideLiquid_Texture(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2382 cctype
: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2385 // slightly different from the old code, but meh...
2386 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2388 result
:= false; // don't stop, ever
2389 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2392 0: if ((tag
and GridTagWater
) = 0) then exit
; // allowed: water
2393 1: if ((tag
and (GridTagWater
or GridTagAcid1
)) = 0) then exit
; // allowed: water, acid1
2394 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2397 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2399 texid
:= pan
.GetTextureID();
2400 // water? water has the highest priority, so stop right here
2401 if ((tag
and GridTagWater
) <> 0) then begin cctype
:= 0; result
:= true; exit
; end;
2403 if ((tag
and GridTagAcid2
) <> 0) then cctype
:= 2;
2405 if ((tag
and GridTagAcid1
) <> 0) then cctype
:= 1;
2409 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('liquids');
2410 if gdbg_map_use_accel_coldet
then
2412 texid
:= TEXTURE_NONE
;
2413 {if gdbg_map_use_tree_coldet then
2415 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2419 if (Width
= 1) and (Height
= 1) then
2421 gMapGrid
.forEachAtPoint(X
, Y
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2425 gMapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2432 result
:= g_Map_CollideLiquid_TextureOld(X
, Y
, Width
, Height
);
2434 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2437 procedure g_Map_EnableWall(ID
: DWORD
);
2442 //g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2444 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2448 procedure g_Map_DisableWall(ID
: DWORD
);
2453 //g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2455 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2459 procedure g_Map_SwitchTexture(PanelType
: Word; ID
: DWORD
; AnimLoop
: Byte = 0);
2464 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
2467 tp
:= gRenderForegrounds
[ID
];
2469 tp
:= gRenderBackgrounds
[ID
];
2482 tp
.NextTexture(AnimLoop
);
2483 if g_Game_IsServer
and g_Game_IsNet
then
2484 MH_SEND_PanelTexture(PanelType
, ID
, AnimLoop
);
2487 procedure g_Map_SetLift(ID
: DWORD
; t
: Integer);
2489 if gLifts
[ID
].LiftType
= t
then
2497 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2499 if LiftType = 0 then
2500 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2501 else if LiftType = 1 then
2502 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2503 else if LiftType = 2 then
2504 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2505 else if LiftType = 3 then
2506 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2509 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2513 function g_Map_GetPoint(PointType
: Byte; var RespawnPoint
: TRespawnPoint
): Boolean;
2516 PointsArray
: Array of TRespawnPoint
;
2519 SetLength(PointsArray
, 0);
2521 if RespawnPoints
= nil then
2524 for a
:= 0 to High(RespawnPoints
) do
2525 if RespawnPoints
[a
].PointType
= PointType
then
2527 SetLength(PointsArray
, Length(PointsArray
)+1);
2528 PointsArray
[High(PointsArray
)] := RespawnPoints
[a
];
2531 if PointsArray
= nil then
2534 RespawnPoint
:= PointsArray
[Random(Length(PointsArray
))];
2538 function g_Map_GetPointCount(PointType
: Byte): Word;
2544 if RespawnPoints
= nil then
2547 for a
:= 0 to High(RespawnPoints
) do
2548 if RespawnPoints
[a
].PointType
= PointType
then
2549 Result
:= Result
+ 1;
2552 function g_Map_HaveFlagPoints(): Boolean;
2554 Result
:= (FlagPoints
[FLAG_RED
] <> nil) and (FlagPoints
[FLAG_BLUE
] <> nil);
2557 procedure g_Map_ResetFlag(Flag
: Byte);
2559 with gFlags
[Flag
] do
2565 Direction
:= D_LEFT
;
2566 State
:= FLAG_STATE_NONE
;
2567 if FlagPoints
[Flag
] <> nil then
2569 Obj
.X
:= FlagPoints
[Flag
]^.X
;
2570 Obj
.Y
:= FlagPoints
[Flag
]^.Y
;
2571 Direction
:= FlagPoints
[Flag
]^.Direction
;
2572 State
:= FLAG_STATE_NORMAL
;
2578 procedure g_Map_DrawFlags();
2581 Mirror
: TMirrorType
;
2583 if gGameSettings
.GameMode
<> GM_CTF
then
2586 for i
:= FLAG_RED
to FLAG_BLUE
do
2588 if State
<> FLAG_STATE_CAPTURED
then
2590 if State
= FLAG_STATE_NONE
then
2593 if Direction
= D_LEFT
then
2595 Mirror
:= M_HORIZONTAL
;
2604 Animation
.Draw(Obj
.X
+dx
, Obj
.Y
+1, Mirror
);
2606 if g_debug_Frames
then
2608 e_DrawQuad(Obj
.X
+Obj
.Rect
.X
,
2610 Obj
.X
+Obj
.Rect
.X
+Obj
.Rect
.Width
-1,
2611 Obj
.Y
+Obj
.Rect
.Y
+Obj
.Rect
.Height
-1,
2617 procedure g_Map_SaveState(Var Mem
: TBinMemoryWriter
);
2624 procedure SavePanelArray(var panels
: TPanelArray
);
2626 PAMem
: TBinMemoryWriter
;
2629 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2630 PAMem
:= TBinMemoryWriter
.Create((Length(panels
)+1) * 40);
2633 while i
< Length(panels
) do
2635 if panels
[i
].SaveIt
then
2639 // Ñîõðàíÿåì ïàíåëü:
2640 panels
[i
].SaveState(PAMem
);
2645 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2646 PAMem
.SaveToMemory(Mem
);
2650 procedure SaveFlag(flag
: PFlag
);
2653 dw
:= FLAG_SIGNATURE
; // 'FLAG'
2655 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2656 Mem
.WriteByte(flag
^.RespawnType
);
2658 Mem
.WriteByte(flag
^.State
);
2659 // Íàïðàâëåíèå ôëàãà:
2660 if flag
^.Direction
= D_LEFT
then
2666 Obj_SaveState(@flag
^.Obj
, Mem
);
2670 Mem
:= TBinMemoryWriter
.Create(1024 * 1024); // 1 MB
2672 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2673 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2674 SavePanelArray(gWalls
);
2675 // Ñîõðàíÿåì ïàíåëè ôîíà:
2676 SavePanelArray(gRenderBackgrounds
);
2677 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2678 SavePanelArray(gRenderForegrounds
);
2679 // Ñîõðàíÿåì ïàíåëè âîäû:
2680 SavePanelArray(gWater
);
2681 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2682 SavePanelArray(gAcid1
);
2683 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2684 SavePanelArray(gAcid2
);
2685 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2686 SavePanelArray(gSteps
);
2687 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2688 SavePanelArray(gLifts
);
2691 ///// Ñîõðàíÿåì ìóçûêó: /////
2692 // Ñèãíàòóðà ìóçûêè:
2693 dw
:= MUSIC_SIGNATURE
; // 'MUSI'
2696 Assert(gMusic
<> nil, 'g_Map_SaveState: gMusic = nil');
2697 if gMusic
.NoMusic
then
2701 Mem
.WriteString(str
, 64);
2702 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2703 dw
:= gMusic
.GetPosition();
2705 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2706 boo
:= gMusic
.SpecPause
;
2707 Mem
.WriteBoolean(boo
);
2710 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2711 Mem
.WriteInt(gTotalMonsters
);
2714 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2715 if gGameSettings
.GameMode
= GM_CTF
then
2717 // Ôëàã Êðàñíîé êîìàíäû:
2718 SaveFlag(@gFlags
[FLAG_RED
]);
2719 // Ôëàã Ñèíåé êîìàíäû:
2720 SaveFlag(@gFlags
[FLAG_BLUE
]);
2724 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2725 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
2727 // Î÷êè Êðàñíîé êîìàíäû:
2728 Mem
.WriteSmallInt(gTeamStat
[TEAM_RED
].Goals
);
2729 // Î÷êè Ñèíåé êîìàíäû:
2730 Mem
.WriteSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
2735 procedure g_Map_LoadState(Var Mem
: TBinMemoryReader
);
2742 procedure LoadPanelArray(var panels
: TPanelArray
);
2744 PAMem
: TBinMemoryReader
;
2747 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2748 PAMem
:= TBinMemoryReader
.Create();
2749 PAMem
.LoadFromMemory(Mem
);
2751 for i
:= 0 to Length(panels
)-1 do
2752 if panels
[i
].SaveIt
then
2758 raise EBinSizeError
.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2760 // Çàãðóæàåì ïàíåëü:
2761 panels
[i
].LoadState(PAMem
);
2764 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2768 procedure LoadFlag(flag
: PFlag
);
2772 if dw
<> FLAG_SIGNATURE
then // 'FLAG'
2774 raise EBinSizeError
.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2776 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2777 Mem
.ReadByte(flag
^.RespawnType
);
2779 Mem
.ReadByte(flag
^.State
);
2780 // Íàïðàâëåíèå ôëàãà:
2783 flag
^.Direction
:= D_LEFT
2785 flag
^.Direction
:= D_RIGHT
;
2787 Obj_LoadState(@flag
^.Obj
, Mem
);
2794 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2795 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2796 LoadPanelArray(gWalls
);
2797 // Çàãðóæàåì ïàíåëè ôîíà:
2798 LoadPanelArray(gRenderBackgrounds
);
2799 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2800 LoadPanelArray(gRenderForegrounds
);
2801 // Çàãðóæàåì ïàíåëè âîäû:
2802 LoadPanelArray(gWater
);
2803 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2804 LoadPanelArray(gAcid1
);
2805 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2806 LoadPanelArray(gAcid2
);
2807 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2808 LoadPanelArray(gSteps
);
2809 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2810 LoadPanelArray(gLifts
);
2813 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2817 ///// Çàãðóæàåì ìóçûêó: /////
2818 // Ñèãíàòóðà ìóçûêè:
2820 if dw
<> MUSIC_SIGNATURE
then // 'MUSI'
2822 raise EBinSizeError
.Create('g_Map_LoadState: Wrong Music Signature');
2825 Assert(gMusic
<> nil, 'g_Map_LoadState: gMusic = nil');
2826 Mem
.ReadString(str
);
2827 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2829 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2830 Mem
.ReadBoolean(boo
);
2831 // Çàïóñêàåì ýòó ìóçûêó:
2832 gMusic
.SetByName(str
);
2833 gMusic
.SpecPause
:= boo
;
2836 gMusic
.SetPosition(dw
);
2839 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2840 Mem
.ReadInt(gTotalMonsters
);
2843 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2844 if gGameSettings
.GameMode
= GM_CTF
then
2846 // Ôëàã Êðàñíîé êîìàíäû:
2847 LoadFlag(@gFlags
[FLAG_RED
]);
2848 // Ôëàã Ñèíåé êîìàíäû:
2849 LoadFlag(@gFlags
[FLAG_BLUE
]);
2853 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2854 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
2856 // Î÷êè Êðàñíîé êîìàíäû:
2857 Mem
.ReadSmallInt(gTeamStat
[TEAM_RED
].Goals
);
2858 // Î÷êè Ñèíåé êîìàíäû:
2859 Mem
.ReadSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
2864 function g_Map_PanelForPID(PanelID
: Integer; var PanelArrayID
: Integer): PPanel
;
2869 if (PanelID
< 0) or (PanelID
> High(PanelByID
)) then Exit
;
2870 Arr
:= PanelByID
[PanelID
].PWhere
^;
2871 PanelArrayID
:= PanelByID
[PanelID
].PArrID
;
2872 Result
:= Addr(Arr
[PanelByID
[PanelID
].PArrID
]);