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_ForEachPanelAt (x
, y
: Integer; cb
: TForEachPanelCB
; 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
, lastDist
: 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 dist
:= (prevx
-x0
)*(prevx
-x0
)+(prevy
-y0
)*(prevy
-y0
);
391 if (dist
< lastDist
) then
403 if (gMapGrid
= nil) then exit
;
404 lastDist
:= (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_ForEachPanelAt (x
, y
: Integer; cb
: TForEachPanelCB
; 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)));
437 if result
then result
:= cb(pan
);
441 tagmask
: Integer = 0;
444 if not assigned(cb
) then exit
;
446 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
447 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
448 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
449 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
450 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
451 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
452 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
454 if (tagmask
= 0) then exit
;// just in case
455 result
:= gMapGrid
.forEachInAABB(x
, y
, 1, 1, checker
, tagmask
);
459 function g_Map_IsSpecialTexture(Texture
: String): Boolean;
461 Result
:= (Texture
= TEXTURE_NAME_WATER
) or
462 (Texture
= TEXTURE_NAME_ACID1
) or
463 (Texture
= TEXTURE_NAME_ACID2
);
466 procedure CreateDoorMap();
468 PanelArray
: Array of record
474 a
, b
, c
, m
, i
, len
: Integer;
482 SetLength(PanelArray
, len
);
484 for a
:= 0 to High(gWalls
) do
485 if gWalls
[a
].Door
then
487 PanelArray
[i
].X
:= gWalls
[a
].X
;
488 PanelArray
[i
].Y
:= gWalls
[a
].Y
;
489 PanelArray
[i
].Width
:= gWalls
[a
].Width
;
490 PanelArray
[i
].Height
:= gWalls
[a
].Height
;
491 PanelArray
[i
].Active
:= True;
492 PanelArray
[i
].PanelID
:= a
;
498 SetLength(PanelArray
, len
);
509 SetLength(gDoorMap
, 0);
511 g_Game_SetLoadingText(_lc
[I_LOAD_DOOR_MAP
], i
-1, False);
514 if PanelArray
[a
].Active
then
516 PanelArray
[a
].Active
:= False;
517 m
:= Length(gDoorMap
);
518 SetLength(gDoorMap
, m
+1);
519 SetLength(gDoorMap
[m
], 1);
520 gDoorMap
[m
, 0] := PanelArray
[a
].PanelID
;
528 if PanelArray
[b
].Active
then
529 for c
:= 0 to High(gDoorMap
[m
]) do
530 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
531 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
532 g_CollideAround(PanelArray
[b
].X
, PanelArray
[b
].Y
,
533 PanelArray
[b
].Width
, PanelArray
[b
].Height
,
534 gWalls
[gDoorMap
[m
, c
]].X
,
535 gWalls
[gDoorMap
[m
, c
]].Y
,
536 gWalls
[gDoorMap
[m
, c
]].Width
,
537 gWalls
[gDoorMap
[m
, c
]].Height
) then
539 PanelArray
[b
].Active
:= False;
540 SetLength(gDoorMap
[m
],
541 Length(gDoorMap
[m
])+1);
542 gDoorMap
[m
, High(gDoorMap
[m
])] := PanelArray
[b
].PanelID
;
548 g_Game_StepLoading();
554 procedure CreateLiftMap();
556 PanelArray
: Array of record
561 a
, b
, c
, len
, i
, j
: Integer;
567 len
:= Length(gLifts
);
568 SetLength(PanelArray
, len
);
570 for a
:= 0 to len
-1 do
572 PanelArray
[a
].X
:= gLifts
[a
].X
;
573 PanelArray
[a
].Y
:= gLifts
[a
].Y
;
574 PanelArray
[a
].Width
:= gLifts
[a
].Width
;
575 PanelArray
[a
].Height
:= gLifts
[a
].Height
;
576 PanelArray
[a
].Active
:= True;
579 SetLength(gLiftMap
, len
);
582 g_Game_SetLoadingText(_lc
[I_LOAD_LIFT_MAP
], len
-1, False);
584 for a
:= 0 to len
-1 do
585 if PanelArray
[a
].Active
then
587 PanelArray
[a
].Active
:= False;
588 SetLength(gLiftMap
[i
], 32);
596 for b
:= 0 to len
-1 do
597 if PanelArray
[b
].Active
then
599 if g_CollideAround(PanelArray
[b
].X
,
602 PanelArray
[b
].Height
,
603 PanelArray
[gLiftMap
[i
, c
]].X
,
604 PanelArray
[gLiftMap
[i
, c
]].Y
,
605 PanelArray
[gLiftMap
[i
, c
]].Width
,
606 PanelArray
[gLiftMap
[i
, c
]].Height
) then
608 PanelArray
[b
].Active
:= False;
610 if j
> High(gLiftMap
[i
]) then
611 SetLength(gLiftMap
[i
],
612 Length(gLiftMap
[i
])+32);
621 SetLength(gLiftMap
[i
], j
+1);
624 g_Game_StepLoading();
627 SetLength(gLiftMap
, i
);
632 function CreatePanel(PanelRec
: TPanelRec_1
; AddTextures
: TAddTextureArray
;
633 CurTex
: Integer; sav
: Boolean): Integer;
636 panels
: ^TPanelArray
;
640 case PanelRec
.PanelType
of
641 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
644 panels
:= @gRenderBackgrounds
;
646 panels
:= @gRenderForegrounds
;
655 PANEL_LIFTUP
, PANEL_LIFTDOWN
, PANEL_LIFTLEFT
, PANEL_LIFTRIGHT
:
658 panels
:= @gBlockMon
;
663 len
:= Length(panels
^);
664 SetLength(panels
^, len
+ 1);
666 panels
^[len
] := TPanel
.Create(PanelRec
, AddTextures
, CurTex
, Textures
);
667 panels
^[len
].ArrIdx
:= len
;
668 panels
^[len
].tag
:= panelTypeToTag(PanelRec
.PanelType
);
670 panels
^[len
].SaveIt
:= True;
674 len
:= Length(PanelByID
);
675 SetLength(PanelByID
, len
+ 1);
676 PanelByID
[len
].PWhere
:= panels
;
677 PanelByID
[len
].PArrID
:= Result
;
680 function CreateNullTexture(RecName
: String): Integer;
682 SetLength(Textures
, Length(Textures
)+1);
683 result
:= High(Textures
);
685 with Textures
[High(Textures
)] do
687 TextureName
:= RecName
;
691 TextureID
:= TEXTURE_NONE
;
695 function CreateTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
698 TextureData
: Pointer;
699 WADName
, txname
: String;
700 a
, ResLength
: Integer;
704 if Textures
<> nil then
705 for a
:= 0 to High(Textures
) do
706 if Textures
[a
].TextureName
= RecName
then
707 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
712 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
713 if (RecName
= TEXTURE_NAME_WATER
) or
714 (RecName
= TEXTURE_NAME_ACID1
) or
715 (RecName
= TEXTURE_NAME_ACID2
) then
717 SetLength(Textures
, Length(Textures
)+1);
719 with Textures
[High(Textures
)] do
721 TextureName
:= RecName
;
723 if TextureName
= TEXTURE_NAME_WATER
then
724 TextureID
:= TEXTURE_SPECIAL_WATER
726 if TextureName
= TEXTURE_NAME_ACID1
then
727 TextureID
:= TEXTURE_SPECIAL_ACID1
729 if TextureName
= TEXTURE_NAME_ACID2
then
730 TextureID
:= TEXTURE_SPECIAL_ACID2
;
735 result
:= High(Textures
);
739 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
740 WADName
:= g_ExtractWadName(RecName
);
742 WAD
:= TWADFile
.Create();
744 if WADName
<> '' then
745 WADName
:= GameDir
+'/wads/'+WADName
749 WAD
.ReadFile(WADName
);
753 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
755 FreeMem(TextureData);
756 RecName := 'COMMON\ALIEN';
760 if WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureData
, ResLength
) then
762 SetLength(Textures
, Length(Textures
)+1);
763 if not e_CreateTextureMem(TextureData
, ResLength
, Textures
[High(Textures
)].TextureID
) then
765 e_GetTextureSize(Textures
[High(Textures
)].TextureID
,
766 @Textures
[High(Textures
)].Width
,
767 @Textures
[High(Textures
)].Height
);
768 FreeMem(TextureData
);
769 Textures
[High(Textures
)].TextureName
:= {RecName}txname
;
770 Textures
[High(Textures
)].Anim
:= False;
772 result
:= High(Textures
);
774 else // Íåò òàêîãî ðåóñðñà â WAD'å
776 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
779 e_WriteLog(Format('Error loading texture %s', [RecName
]), MSG_WARNING
);
780 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
787 function CreateAnimTexture(RecName
: String; Map
: string; log
: Boolean): Integer;
790 TextureWAD
: PChar = nil;
791 TextData
: Pointer = nil;
792 TextureData
: Pointer = nil;
796 TextureResource
: String;
797 _width
, _height
, _framecount
, _speed
: Integer;
798 _backanimation
: Boolean;
800 ia
: TDynImageDataArray
= nil;
801 f
, c
, frdelay
, frloop
: Integer;
805 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
807 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
808 WADName
:= g_ExtractWadName(RecName
);
810 WAD
:= TWADFile
.Create();
812 if WADName
<> '' then
813 WADName
:= GameDir
+'/wads/'+WADName
817 WAD
.ReadFile(WADName
);
819 if not WAD
.GetResource(g_ExtractFilePathName(RecName
), TextureWAD
, ResLength
) then
823 e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
824 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
830 if WADName = Map then
832 //FreeMem(TextureWAD);
833 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
839 if ResLength
< 6 then
841 e_WriteLog(Format('Animated texture file "%s" too short', [RecName
]), MSG_WARNING
);
845 // ýòî ïòèöà? ýòî ñàìîë¸ò?
846 if (TextureWAD
[0] = 'D') and (TextureWAD
[1] = 'F') and
847 (TextureWAD
[2] = 'W') and (TextureWAD
[3] = 'A') and (TextureWAD
[4] = 'D') then
849 // íåò, ýòî ñóïåðìåí!
850 if not WAD
.ReadMemory(TextureWAD
, ResLength
) then
852 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName
]), MSG_WARNING
);
856 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
857 if not WAD
.GetResource('TEXT/ANIM', TextData
, ResLength
) then
859 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName
]), MSG_WARNING
);
863 cfg
:= TConfig
.CreateMem(TextData
, ResLength
);
865 TextureResource
:= cfg
.ReadStr('', 'resource', '');
866 if TextureResource
= '' then
868 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName
]), MSG_WARNING
);
872 _width
:= cfg
.ReadInt('', 'framewidth', 0);
873 _height
:= cfg
.ReadInt('', 'frameheight', 0);
874 _framecount
:= cfg
.ReadInt('', 'framecount', 0);
875 _speed
:= cfg
.ReadInt('', 'waitcount', 0);
876 _backanimation
:= cfg
.ReadBool('', 'backanimation', False);
881 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
882 if not WAD
.GetResource('TEXTURES/'+TextureResource
, TextureData
, ResLength
) then
884 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName
, 'TEXTURES/'+TextureResource
]), MSG_WARNING
);
891 SetLength(Textures
, Length(Textures
)+1);
892 with Textures
[High(Textures
)] do
894 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
895 if g_Frames_CreateMemory(@FramesID
, '', TextureData
, ResLength
, _width
, _height
, _framecount
, _backanimation
) then
897 TextureName
:= RecName
;
901 FramesCount
:= _framecount
;
903 result
:= High(Textures
);
907 if log
then e_WriteLog(Format('Error loading animation texture %s', [RecName
]), MSG_WARNING
);
913 // try animated image
915 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
916 if length(imgfmt) = 0 then
918 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
922 GlobalMetadata
.ClearMetaItems();
923 GlobalMetadata
.ClearMetaItemsForSaving();
924 if not LoadMultiImageFromMemory(TextureWAD
, ResLength
, ia
) then
926 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName
]), MSG_WARNING
);
929 if length(ia
) = 0 then
931 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName
]), MSG_WARNING
);
938 _width
:= ia
[0].width
;
939 _height
:= ia
[0].height
;
940 _framecount
:= length(ia
);
942 _backanimation
:= false;
945 if GlobalMetadata
.HasMetaItem(SMetaFrameDelay
) then
947 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
949 f
:= GlobalMetadata
.MetaItems
[SMetaFrameDelay
];
951 if f
< 0 then f
:= 0;
954 if c
< 13 then c
:= 0 else c
:= 1;
956 if f
< 1 then f
:= 1 else if f
> 255 then f
:= 255;
961 if GlobalMetadata
.HasMetaItem(SMetaAnimationLoops
) then
963 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
965 f
:= GlobalMetadata
.MetaItems
[SMetaAnimationLoops
];
967 if f
<> 0 then _backanimation
:= true; // non-infinite looping == forth-and-back
971 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
972 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
973 f
:= ord(_backanimation
);
974 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
);
976 SetLength(Textures
, Length(Textures
)+1);
977 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
978 if g_CreateFramesImg(ia
, @Textures
[High(Textures
)].FramesID
, '', _backanimation
) then
980 Textures
[High(Textures
)].TextureName
:= RecName
;
981 Textures
[High(Textures
)].Width
:= _width
;
982 Textures
[High(Textures
)].Height
:= _height
;
983 Textures
[High(Textures
)].Anim
:= True;
984 Textures
[High(Textures
)].FramesCount
:= length(ia
);
985 Textures
[High(Textures
)].Speed
:= _speed
;
986 result
:= High(Textures
);
987 //writeln(' CREATED!');
991 if log
then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName
]), MSG_WARNING
);
995 for f
:= 0 to High(ia
) do FreeImage(ia
[f
]);
998 if TextureWAD
<> nil then FreeMem(TextureWAD
);
999 if TextData
<> nil then FreeMem(TextData
);
1000 if TextureData
<> nil then FreeMem(TextureData
);
1004 procedure CreateItem(Item
: TItemRec_1
);
1006 if g_Game_IsClient
then Exit
;
1008 if (not (gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
])) and
1009 ByteBool(Item
.Options
and ITEM_OPTION_ONLYDM
) then
1012 g_Items_Create(Item
.X
, Item
.Y
, Item
.ItemType
, ByteBool(Item
.Options
and ITEM_OPTION_FALL
),
1013 gGameSettings
.GameMode
in [GM_DM
, GM_TDM
, GM_CTF
, GM_COOP
]);
1016 procedure CreateArea(Area
: TAreaRec_1
);
1021 case Area
.AreaType
of
1022 AREA_DMPOINT
, AREA_PLAYERPOINT1
, AREA_PLAYERPOINT2
,
1023 AREA_REDTEAMPOINT
, AREA_BLUETEAMPOINT
:
1025 SetLength(RespawnPoints
, Length(RespawnPoints
)+1);
1026 with RespawnPoints
[High(RespawnPoints
)] do
1030 Direction
:= TDirection(Area
.Direction
);
1032 case Area
.AreaType
of
1033 AREA_DMPOINT
: PointType
:= RESPAWNPOINT_DM
;
1034 AREA_PLAYERPOINT1
: PointType
:= RESPAWNPOINT_PLAYER1
;
1035 AREA_PLAYERPOINT2
: PointType
:= RESPAWNPOINT_PLAYER2
;
1036 AREA_REDTEAMPOINT
: PointType
:= RESPAWNPOINT_RED
;
1037 AREA_BLUETEAMPOINT
: PointType
:= RESPAWNPOINT_BLUE
;
1042 AREA_REDFLAG
, AREA_BLUEFLAG
:
1044 if Area
.AreaType
= AREA_REDFLAG
then a
:= FLAG_RED
else a
:= FLAG_BLUE
;
1046 if FlagPoints
[a
] <> nil then Exit
;
1050 with FlagPoints
[a
]^ do
1052 X
:= Area
.X
-FLAGRECT
.X
;
1053 Y
:= Area
.Y
-FLAGRECT
.Y
;
1054 Direction
:= TDirection(Area
.Direction
);
1060 FLAG_RED
: g_Frames_Get(id
, 'FRAMES_FLAG_RED');
1061 FLAG_BLUE
: g_Frames_Get(id
, 'FRAMES_FLAG_BLUE');
1064 Animation
:= TAnimation
.Create(id
, True, 8);
1065 Obj
.Rect
:= FLAGRECT
;
1073 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1074 with DOMFlagPoints[High(DOMFlagPoints)] do
1078 Direction := TDirection(Area.Direction);
1081 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1086 procedure CreateTrigger(Trigger
: TTriggerRec_1
; fTexturePanel1Type
, fTexturePanel2Type
: Word);
1090 if g_Game_IsClient
and not (Trigger
.TriggerType
in [TRIGGER_SOUND
, TRIGGER_MUSIC
]) then Exit
;
1096 Width
:= Trigger
.Width
;
1097 Height
:= Trigger
.Height
;
1098 Enabled
:= ByteBool(Trigger
.Enabled
);
1099 TexturePanel
:= Trigger
.TexturePanel
;
1100 TexturePanelType
:= fTexturePanel1Type
;
1101 ShotPanelType
:= fTexturePanel2Type
;
1102 TriggerType
:= Trigger
.TriggerType
;
1103 ActivateType
:= Trigger
.ActivateType
;
1104 Keys
:= Trigger
.Keys
;
1105 Data
.Default
:= Trigger
.DATA
;
1108 g_Triggers_Create(_trigger
);
1111 procedure CreateMonster(monster
: TMonsterRec_1
);
1116 if g_Game_IsClient
then Exit
;
1118 if (gGameSettings
.GameType
= GT_SINGLE
)
1119 or LongBool(gGameSettings
.Options
and GAME_OPTION_MONSTERS
) then
1121 mon
:= g_Monsters_Create(monster
.MonsterType
, monster
.X
, monster
.Y
, TDirection(monster
.Direction
));
1123 if gTriggers
<> nil then
1125 for a
:= 0 to High(gTriggers
) do
1127 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1129 if (gTriggers
[a
].Data
.MonsterID
-1) = mon
.StartID
then mon
.AddTrigger(a
);
1134 if monster
.MonsterType
<> MONSTER_BARREL
then Inc(gTotalMonsters
);
1138 procedure g_Map_ReAdd_DieTriggers();
1140 function monsDieTrig (mon
: TMonster
): Boolean;
1144 result
:= false; // don't stop
1145 mon
.ClearTriggers();
1146 for a
:= 0 to High(gTriggers
) do
1148 if gTriggers
[a
].TriggerType
in [TRIGGER_PRESS
, TRIGGER_ON
, TRIGGER_OFF
, TRIGGER_ONOFF
] then
1150 if (gTriggers
[a
].Data
.MonsterID
-1) = mon
.StartID
then mon
.AddTrigger(a
);
1156 if g_Game_IsClient
then Exit
;
1158 g_Mons_ForEach(monsDieTrig
);
1161 function extractWadName(resourceName
: string): string;
1165 posN
:= Pos(':', resourceName
);
1167 Result
:= Copy(resourceName
, 0, posN
-1)
1172 procedure addResToExternalResList(res
: string);
1174 res
:= extractWadName(res
);
1175 if (res
<> '') and (gExternalResources
.IndexOf(res
) = -1) then
1176 gExternalResources
.Add(res
);
1179 procedure generateExternalResourcesList(mapReader
: TMapReader_1
);
1181 textures
: TTexturesRec1Array
;
1182 mapHeader
: TMapHeaderRec_1
;
1184 resFile
: String = '';
1186 if gExternalResources
= nil then
1187 gExternalResources
:= TStringList
.Create
;
1189 gExternalResources
.Clear
;
1190 textures
:= mapReader
.GetTextures();
1191 for i
:= 0 to High(textures
) do
1193 addResToExternalResList(resFile
);
1198 mapHeader
:= mapReader
.GetMapHeader
;
1200 addResToExternalResList(mapHeader
.MusicName
);
1201 addResToExternalResList(mapHeader
.SkyName
);
1204 procedure mapCreateGrid ();
1206 mapX0
: Integer = $3fffffff;
1207 mapY0
: Integer = $3fffffff;
1208 mapX1
: Integer = -$3fffffff;
1209 mapY1
: Integer = -$3fffffff;
1211 procedure calcBoundingBox (constref panels
: TPanelArray
);
1216 for idx
:= 0 to High(panels
) do
1219 if not pan
.visvalid
then continue
;
1220 if (pan
.Width
< 1) or (pan
.Height
< 1) then continue
;
1221 if (mapX0
> pan
.x0
) then mapX0
:= pan
.x0
;
1222 if (mapY0
> pan
.y0
) then mapY0
:= pan
.y0
;
1223 if (mapX1
< pan
.x1
) then mapX1
:= pan
.x1
;
1224 if (mapY1
< pan
.y1
) then mapY1
:= pan
.y1
;
1228 procedure addPanelsToGrid (constref panels
: TPanelArray
; tag
: Integer);
1233 tag
:= panelTypeToTag(tag
);
1234 for idx
:= High(panels
) downto 0 do
1238 if not pan
.visvalid
then continue
;
1239 gMapGrid
.insertBody(pan
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
, tag
);
1240 //mapTree.insertObject(pan, tag, true); // as static object
1250 calcBoundingBox(gWalls
);
1251 calcBoundingBox(gRenderBackgrounds
);
1252 calcBoundingBox(gRenderForegrounds
);
1253 calcBoundingBox(gWater
);
1254 calcBoundingBox(gAcid1
);
1255 calcBoundingBox(gAcid2
);
1256 calcBoundingBox(gSteps
);
1257 calcBoundingBox(gLifts
);
1258 calcBoundingBox(gBlockMon
);
1260 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0
, mapY0
, mapX1
, mapY1
]), MSG_WARNING
);
1262 gMapGrid
:= TPanelGrid
.Create(mapX0
, mapY0
, mapX1
-mapX0
+1, mapY1
-mapY0
+1);
1263 //mapTree := TDynAABBTreeMap.Create();
1265 addPanelsToGrid(gWalls
, PANEL_WALL
);
1266 addPanelsToGrid(gWalls
, PANEL_CLOSEDOOR
);
1267 addPanelsToGrid(gWalls
, PANEL_OPENDOOR
);
1268 addPanelsToGrid(gRenderBackgrounds
, PANEL_BACK
);
1269 addPanelsToGrid(gRenderForegrounds
, PANEL_FORE
);
1270 addPanelsToGrid(gWater
, PANEL_WATER
);
1271 addPanelsToGrid(gAcid1
, PANEL_ACID1
);
1272 addPanelsToGrid(gAcid2
, PANEL_ACID2
);
1273 addPanelsToGrid(gSteps
, PANEL_STEP
);
1274 addPanelsToGrid(gLifts
, PANEL_LIFTUP
); // it doesn't matter which LIFT type is used here
1275 addPanelsToGrid(gBlockMon
, PANEL_BLOCKMON
);
1277 gMapGrid
.dumpStats();
1278 //e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1279 //mapTree.forEachLeaf(nil);
1282 function g_Map_Load(Res
: String): Boolean;
1284 DefaultMusRes
= 'Standart.wad:STDMUS\MUS1';
1285 DefaultSkyRes
= 'Standart.wad:STDSKY\SKY0';
1288 MapReader
: TMapReader_1
;
1289 Header
: TMapHeaderRec_1
;
1290 _textures
: TTexturesRec1Array
;
1291 _texnummap
: array of Integer; // `_textures` -> `Textures`
1292 panels
: TPanelsRec1Array
;
1293 items
: TItemsRec1Array
;
1294 monsters
: TMonsterRec1Array
;
1295 areas
: TAreasRec1Array
;
1296 triggers
: TTriggersRec1Array
;
1297 a
, b
, c
, k
: Integer;
1299 AddTextures
: TAddTextureArray
;
1300 texture
: TTextureRec_1
;
1301 TriggersTable
: Array of record
1302 TexturePanel
: Integer;
1307 FileName
, mapResName
, s
, TexName
: String;
1310 ok
, isAnim
, trigRef
: Boolean;
1311 CurTex
, ntn
: Integer;
1320 gMapInfo
.Map
:= Res
;
1321 TriggersTable
:= nil;
1322 FillChar(texture
, SizeOf(texture
), 0);
1324 sfsGCDisable(); // temporary disable removing of temporary volumes
1327 FileName
:= g_ExtractWadName(Res
);
1328 e_WriteLog('Loading map WAD: '+FileName
, MSG_NOTIFY
);
1329 g_Game_SetLoadingText(_lc
[I_LOAD_WAD_FILE
], 0, False);
1331 WAD
:= TWADFile
.Create();
1332 if not WAD
.ReadFile(FileName
) then
1334 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_WAD
], [FileName
]));
1338 //k8: why loader ignores path here?
1339 mapResName
:= g_ExtractFileName(Res
);
1340 if not WAD
.GetMapResource(mapResName
, Data
, Len
) then
1342 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_RES
], [mapResName
]));
1350 e_WriteLog('Loading map: '+mapResName
, MSG_NOTIFY
);
1351 g_Game_SetLoadingText(_lc
[I_LOAD_MAP
], 0, False);
1352 MapReader
:= TMapReader_1
.Create();
1354 if not MapReader
.LoadMap(Data
) then
1356 g_FatalError(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]));
1363 generateExternalResourcesList(MapReader
);
1364 // Çàãðóçêà òåêñòóð:
1365 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], 0, False);
1366 _textures
:= MapReader
.GetTextures();
1369 // Äîáàâëåíèå òåêñòóð â Textures[]:
1370 if _textures
<> nil then
1372 e_WriteLog(' Loading textures:', MSG_NOTIFY
);
1373 g_Game_SetLoadingText(_lc
[I_LOAD_TEXTURES
], High(_textures
), False);
1374 SetLength(_texnummap
, length(_textures
));
1376 for a
:= 0 to High(_textures
) do
1379 CopyMemory(@s
[1], @_textures
[a
].Resource
[0], 64);
1380 for b
:= 1 to Length(s
) do
1386 e_WriteLog(Format(' Loading texture #%d: %s', [a
, s
]), MSG_NOTIFY
);
1387 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1388 // Àíèìèðîâàííàÿ òåêñòóðà:
1389 if ByteBool(_textures
[a
].Anim
) then
1391 ntn
:= CreateAnimTexture(_textures
[a
].Resource
, FileName
, True);
1394 g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_ANIM
], [s
]));
1395 ntn
:= CreateNullTexture(_textures
[a
].Resource
);
1398 else // Îáû÷íàÿ òåêñòóðà:
1399 ntn
:= CreateTexture(_textures
[a
].Resource
, FileName
, True);
1402 g_SimpleError(Format(_lc
[I_GAME_ERROR_TEXTURE_SIMPLE
], [s
]));
1403 ntn
:= CreateNullTexture(_textures
[a
].Resource
);
1406 _texnummap
[a
] := ntn
; // fix texture number
1407 g_Game_StepLoading();
1411 // Çàãðóçêà òðèããåðîâ:
1412 gTriggerClientID
:= 0;
1413 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1414 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS
], 0, False);
1415 triggers
:= MapReader
.GetTriggers();
1417 // Çàãðóçêà ïàíåëåé:
1418 e_WriteLog(' Loading panels...', MSG_NOTIFY
);
1419 g_Game_SetLoadingText(_lc
[I_LOAD_PANELS
], 0, False);
1420 panels
:= MapReader
.GetPanels();
1422 // check texture numbers for panels
1423 for a
:= 0 to High(panels
) do
1425 if panels
[a
].TextureNum
> High(_textures
) then
1427 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR
);
1431 panels
[a
].TextureNum
:= _texnummap
[panels
[a
].TextureNum
];
1434 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1435 if triggers
<> nil then
1437 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY
);
1438 SetLength(TriggersTable
, Length(triggers
));
1439 g_Game_SetLoadingText(_lc
[I_LOAD_TRIGGERS_TABLE
], High(TriggersTable
), False);
1441 for a
:= 0 to High(TriggersTable
) do
1443 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1444 TriggersTable
[a
].TexturePanel
:= triggers
[a
].TexturePanel
;
1446 if triggers
[a
].TriggerType
in [TRIGGER_LIFTUP
, TRIGGER_LIFTDOWN
, TRIGGER_LIFT
] then
1447 TriggersTable
[a
].LiftPanel
:= TTriggerData(triggers
[a
].DATA
).PanelID
1449 TriggersTable
[a
].LiftPanel
:= -1;
1451 if triggers
[a
].TriggerType
in [TRIGGER_OPENDOOR
,
1452 TRIGGER_CLOSEDOOR
, TRIGGER_DOOR
, TRIGGER_DOOR5
,
1453 TRIGGER_CLOSETRAP
, TRIGGER_TRAP
] then
1454 TriggersTable
[a
].DoorPanel
:= TTriggerData(triggers
[a
].DATA
).PanelID
1456 TriggersTable
[a
].DoorPanel
:= -1;
1458 if triggers
[a
].TriggerType
= TRIGGER_SHOT
then
1459 TriggersTable
[a
].ShotPanel
:= TTriggerData(triggers
[a
].DATA
).ShotPanelID
1461 TriggersTable
[a
].ShotPanel
:= -1;
1463 g_Game_StepLoading();
1468 if panels
<> nil then
1470 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY
);
1471 g_Game_SetLoadingText(_lc
[I_LOAD_LINK_TRIGGERS
], High(panels
), False);
1473 for a
:= 0 to High(panels
) do
1475 SetLength(AddTextures
, 0);
1478 if _textures
<> nil then
1480 texture
:= _textures
[panels
[a
].TextureNum
];
1488 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1489 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1491 if (TriggersTable
<> nil) and (_textures
<> nil) then
1492 for b
:= 0 to High(TriggersTable
) do
1493 if (TriggersTable
[b
].TexturePanel
= a
)
1494 or (TriggersTable
[b
].ShotPanel
= a
) then
1503 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1505 CopyMemory(@s
[1], @texture
.Resource
[0], 64);
1508 for c
:= Len
downto 1 do
1516 // Ñïåö-òåêñòóðû çàïðåùåíû:
1517 if g_Map_IsSpecialTexture(s
) then
1520 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1521 ok
:= g_Texture_NumNameFindStart(s
);
1523 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1524 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1527 k
:= NNF_NAME_BEFORE
;
1528 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1529 while ok
or (k
= NNF_NAME_BEFORE
) or
1530 (k
= NNF_NAME_EQUALS
) do
1532 k
:= g_Texture_NumNameFindNext(TexName
);
1534 if (k
= NNF_NAME_BEFORE
) or
1535 (k
= NNF_NAME_AFTER
) then
1537 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1538 if ByteBool(texture
.Anim
) then
1539 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1541 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1543 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1545 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1549 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1551 ok
:= CreateTexture(TexName
, FileName
, False) >= 0;
1553 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1555 ok
:= CreateAnimTexture(TexName
, FileName
, False) >= 0;
1559 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1562 for c
:= 0 to High(Textures
) do
1563 if Textures
[c
].TextureName
= TexName
then
1565 SetLength(AddTextures
, Length(AddTextures
)+1);
1566 AddTextures
[High(AddTextures
)].Texture
:= c
;
1567 AddTextures
[High(AddTextures
)].Anim
:= isAnim
;
1573 if k
= NNF_NAME_EQUALS
then
1575 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1576 SetLength(AddTextures
, Length(AddTextures
)+1);
1577 AddTextures
[High(AddTextures
)].Texture
:= panels
[a
].TextureNum
;
1578 AddTextures
[High(AddTextures
)].Anim
:= ByteBool(texture
.Anim
);
1579 CurTex
:= High(AddTextures
);
1587 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1588 end; // if ok - ññûëàþòñÿ òðèããåðû
1592 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1593 SetLength(AddTextures
, 1);
1594 AddTextures
[0].Texture
:= panels
[a
].TextureNum
;
1595 AddTextures
[0].Anim
:= ByteBool(texture
.Anim
);
1599 //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);
1601 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1602 PanelID
:= CreatePanel(panels
[a
], AddTextures
, CurTex
, trigRef
);
1604 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1605 if TriggersTable
<> nil then
1606 for b
:= 0 to High(TriggersTable
) do
1608 // Òðèããåð äâåðè/ëèôòà:
1609 if (TriggersTable
[b
].LiftPanel
= a
) or
1610 (TriggersTable
[b
].DoorPanel
= a
) then
1611 TTriggerData(triggers
[b
].DATA
).PanelID
:= PanelID
;
1612 // Òðèããåð ñìåíû òåêñòóðû:
1613 if TriggersTable
[b
].TexturePanel
= a
then
1614 triggers
[b
].TexturePanel
:= PanelID
;
1615 // Òðèããåð "Òóðåëü":
1616 if TriggersTable
[b
].ShotPanel
= a
then
1617 TTriggerData(triggers
[b
].DATA
).ShotPanelID
:= PanelID
;
1620 g_Game_StepLoading();
1624 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1625 if (triggers
<> nil) and not gLoadGameMode
then
1627 e_WriteLog(' Creating triggers...', MSG_NOTIFY
);
1628 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_TRIGGERS
], 0, False);
1629 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1630 for a
:= 0 to High(triggers
) do
1632 if triggers
[a
].TexturePanel
<> -1 then
1633 b
:= panels
[TriggersTable
[a
].TexturePanel
].PanelType
1636 if (triggers
[a
].TriggerType
= TRIGGER_SHOT
) and
1637 (TTriggerData(triggers
[a
].DATA
).ShotPanelID
<> -1) then
1638 c
:= panels
[TriggersTable
[a
].ShotPanel
].PanelType
1641 CreateTrigger(triggers
[a
], b
, c
);
1645 // Çàãðóçêà ïðåäìåòîâ:
1646 e_WriteLog(' Loading triggers...', MSG_NOTIFY
);
1647 g_Game_SetLoadingText(_lc
[I_LOAD_ITEMS
], 0, False);
1648 items
:= MapReader
.GetItems();
1650 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1651 if (items
<> nil) and not gLoadGameMode
then
1653 e_WriteLog(' Spawning items...', MSG_NOTIFY
);
1654 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_ITEMS
], 0, False);
1655 for a
:= 0 to High(items
) do
1656 CreateItem(Items
[a
]);
1659 // Çàãðóçêà îáëàñòåé:
1660 e_WriteLog(' Loading areas...', MSG_NOTIFY
);
1661 g_Game_SetLoadingText(_lc
[I_LOAD_AREAS
], 0, False);
1662 areas
:= MapReader
.GetAreas();
1664 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1665 if areas
<> nil then
1667 e_WriteLog(' Creating areas...', MSG_NOTIFY
);
1668 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_AREAS
], 0, False);
1669 for a
:= 0 to High(areas
) do
1670 CreateArea(areas
[a
]);
1673 // Çàãðóçêà ìîíñòðîâ:
1674 e_WriteLog(' Loading monsters...', MSG_NOTIFY
);
1675 g_Game_SetLoadingText(_lc
[I_LOAD_MONSTERS
], 0, False);
1676 monsters
:= MapReader
.GetMonsters();
1678 gTotalMonsters
:= 0;
1680 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1681 if (monsters
<> nil) and not gLoadGameMode
then
1683 e_WriteLog(' Spawning monsters...', MSG_NOTIFY
);
1684 g_Game_SetLoadingText(_lc
[I_LOAD_CREATE_MONSTERS
], 0, False);
1685 for a
:= 0 to High(monsters
) do
1686 CreateMonster(monsters
[a
]);
1689 // Çàãðóçêà îïèñàíèÿ êàðòû:
1690 e_WriteLog(' Reading map info...', MSG_NOTIFY
);
1691 g_Game_SetLoadingText(_lc
[I_LOAD_MAP_HEADER
], 0, False);
1692 Header
:= MapReader
.GetMapHeader();
1698 Name
:= Header
.MapName
;
1699 Description
:= Header
.MapDescription
;
1700 Author
:= Header
.MapAuthor
;
1701 MusicName
:= Header
.MusicName
;
1702 SkyName
:= Header
.SkyName
;
1703 Height
:= Header
.Height
;
1704 Width
:= Header
.Width
;
1708 if gMapInfo
.SkyName
<> '' then
1710 e_WriteLog(' Loading sky: ' + gMapInfo
.SkyName
, MSG_NOTIFY
);
1711 g_Game_SetLoadingText(_lc
[I_LOAD_SKY
], 0, False);
1712 FileName
:= g_ExtractWadName(gMapInfo
.SkyName
);
1714 if FileName
<> '' then
1715 FileName
:= GameDir
+'/wads/'+FileName
1718 FileName
:= g_ExtractWadName(Res
);
1721 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.SkyName
);
1722 if g_Texture_CreateWAD(BackID
, s
) then
1724 g_Game_SetupScreenSize();
1727 g_FatalError(Format(_lc
[I_GAME_ERROR_SKY
], [s
]));
1732 if gMapInfo
.MusicName
<> '' then
1734 e_WriteLog(' Loading music: ' + gMapInfo
.MusicName
, MSG_NOTIFY
);
1735 g_Game_SetLoadingText(_lc
[I_LOAD_MUSIC
], 0, False);
1736 FileName
:= g_ExtractWadName(gMapInfo
.MusicName
);
1738 if FileName
<> '' then
1739 FileName
:= GameDir
+'/wads/'+FileName
1742 FileName
:= g_ExtractWadName(Res
);
1745 s
:= FileName
+':'+g_ExtractFilePathName(gMapInfo
.MusicName
);
1746 if g_Sound_CreateWADEx(gMapInfo
.MusicName
, s
, True) then
1749 g_FatalError(Format(_lc
[I_GAME_ERROR_MUSIC
], [s
]));
1752 // Îñòàëüíûå óñòàíâêè:
1760 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1761 if not gLoadGameMode
then
1764 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1770 TriggersTable
:= nil;
1773 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1774 if ok
and (not gLoadGameMode
) then
1776 gMusic
.SetByName(gMapInfo
.MusicName
);
1780 gMusic
.SetByName('');
1782 sfsGCEnable(); // enable releasing unused volumes
1785 e_WriteLog('Creating map grid', MSG_NOTIFY
);
1788 e_WriteLog('Done loading map.', MSG_NOTIFY
);
1792 function g_Map_GetMapInfo(Res
: String): TMapInfo
;
1795 MapReader
: TMapReader_1
;
1796 Header
: TMapHeaderRec_1
;
1801 FillChar(Result
, SizeOf(Result
), 0);
1802 FileName
:= g_ExtractWadName(Res
);
1804 WAD
:= TWADFile
.Create();
1805 if not WAD
.ReadFile(FileName
) then
1811 //k8: it ignores path again
1812 if not WAD
.GetMapResource(g_ExtractFileName(Res
), Data
, Len
) then
1820 MapReader
:= TMapReader_1
.Create();
1822 if not MapReader
.LoadMap(Data
) then
1824 g_Console_Add(Format(_lc
[I_GAME_ERROR_MAP_LOAD
], [Res
]), True);
1825 ZeroMemory(@Header
, SizeOf(Header
));
1826 Result
.Name
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
1827 Result
.Description
:= _lc
[I_GAME_ERROR_MAP_SELECT
];
1831 Header
:= MapReader
.GetMapHeader();
1832 Result
.Name
:= Header
.MapName
;
1833 Result
.Description
:= Header
.MapDescription
;
1840 Result
.Author
:= Header
.MapAuthor
;
1841 Result
.Height
:= Header
.Height
;
1842 Result
.Width
:= Header
.Width
;
1845 function g_Map_GetMapsList(WADName
: string): SArray
;
1852 WAD
:= TWADFile
.Create();
1853 if not WAD
.ReadFile(WADName
) then
1858 ResList
:= WAD
.GetMapResources();
1859 if ResList
<> nil then
1861 for a
:= 0 to High(ResList
) do
1863 SetLength(Result
, Length(Result
)+1);
1864 Result
[High(Result
)] := ResList
[a
];
1870 function g_Map_Exist(Res
: string): Boolean;
1873 FileName
, mnn
: string;
1879 FileName
:= addWadExtension(g_ExtractWadName(Res
));
1881 WAD
:= TWADFile
.Create
;
1882 if not WAD
.ReadFile(FileName
) then
1888 ResList
:= WAD
.GetMapResources();
1891 mnn
:= g_ExtractFileName(Res
);
1892 if ResList
<> nil then
1893 for a
:= 0 to High(ResList
) do if StrEquCI1251(ResList
[a
], mnn
) then
1900 procedure g_Map_Free();
1904 procedure FreePanelArray(var panels
: TPanelArray
);
1909 if panels
<> nil then
1911 for i
:= 0 to High(panels
) do
1924 RespawnPoints
:= nil;
1925 if FlagPoints
[FLAG_RED
] <> nil then
1927 Dispose(FlagPoints
[FLAG_RED
]);
1928 FlagPoints
[FLAG_RED
] := nil;
1930 if FlagPoints
[FLAG_BLUE
] <> nil then
1932 Dispose(FlagPoints
[FLAG_BLUE
]);
1933 FlagPoints
[FLAG_BLUE
] := nil;
1935 //DOMFlagPoints := nil;
1939 if Textures
<> nil then
1941 for a
:= 0 to High(Textures
) do
1942 if not g_Map_IsSpecialTexture(Textures
[a
].TextureName
) then
1943 if Textures
[a
].Anim
then
1944 g_Frames_DeleteByID(Textures
[a
].FramesID
)
1946 if Textures
[a
].TextureID
<> TEXTURE_NONE
then
1947 e_DeleteTexture(Textures
[a
].TextureID
);
1952 FreePanelArray(gWalls
);
1953 FreePanelArray(gRenderBackgrounds
);
1954 FreePanelArray(gRenderForegrounds
);
1955 FreePanelArray(gWater
);
1956 FreePanelArray(gAcid1
);
1957 FreePanelArray(gAcid2
);
1958 FreePanelArray(gSteps
);
1959 FreePanelArray(gLifts
);
1960 FreePanelArray(gBlockMon
);
1962 if BackID
<> DWORD(-1) then
1966 e_DeleteTexture(BackID
);
1967 BackID
:= DWORD(-1);
1970 g_Game_StopAllSounds(False);
1972 g_Sound_Delete(gMapInfo
.MusicName
);
1974 gMapInfo
.Name
:= '';
1975 gMapInfo
.Description
:= '';
1976 gMapInfo
.MusicName
:= '';
1977 gMapInfo
.Height
:= 0;
1978 gMapInfo
.Width
:= 0;
1986 procedure g_Map_Update();
1992 procedure UpdatePanelArray(var panels
: TPanelArray
);
1997 if panels
<> nil then
1998 for i
:= 0 to High(panels
) do
2003 UpdatePanelArray(gWalls
);
2004 UpdatePanelArray(gRenderBackgrounds
);
2005 UpdatePanelArray(gRenderForegrounds
);
2006 UpdatePanelArray(gWater
);
2007 UpdatePanelArray(gAcid1
);
2008 UpdatePanelArray(gAcid2
);
2009 UpdatePanelArray(gSteps
);
2011 if gGameSettings
.GameMode
= GM_CTF
then
2013 for a
:= FLAG_RED
to FLAG_BLUE
do
2014 if not (gFlags
[a
].State
in [FLAG_STATE_NONE
, FLAG_STATE_CAPTURED
]) then
2017 if gFlags
[a
].Animation
<> nil then
2018 gFlags
[a
].Animation
.Update();
2020 m
:= g_Obj_Move(@Obj
, True, True);
2022 if gTime
mod (GAME_TICK
*2) <> 0 then
2025 // Ñîïðîòèâëåíèå âîçäóõà:
2026 Obj
.Vel
.X
:= z_dec(Obj
.Vel
.X
, 1);
2028 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2029 if ((Count
= 0) or ByteBool(m
and MOVE_FALLOUT
)) and g_Game_IsServer
then
2032 gFlags
[a
].CaptureTime
:= 0;
2033 if a
= FLAG_RED
then
2034 s
:= _lc
[I_PLAYER_FLAG_RED
]
2036 s
:= _lc
[I_PLAYER_FLAG_BLUE
];
2037 g_Game_Message(Format(_lc
[I_MESSAGE_FLAG_RETURN
], [AnsiUpperCase(s
)]), 144);
2039 if g_Game_IsNet
then
2040 MH_SEND_FlagEvent(FLAG_STATE_RETURNED
, a
, 0);
2047 // Èãðîê áåðåò ôëàã:
2048 if gPlayers
<> nil then
2050 j
:= Random(Length(gPlayers
)) - 1;
2052 for d
:= 0 to High(gPlayers
) do
2055 if j
> High(gPlayers
) then
2058 if gPlayers
[j
] <> nil then
2059 if gPlayers
[j
].Live
and
2060 g_Obj_Collide(@Obj
, @gPlayers
[j
].Obj
) then
2062 if gPlayers
[j
].GetFlag(a
) then
2073 procedure g_Map_DrawPanels (PanelType
: Word);
2075 procedure DrawPanels (constref panels
: TPanelArray
; drawDoors
: Boolean=False);
2079 if (panels
<> nil) then
2081 // alas, no visible set
2082 for idx
:= 0 to High(panels
) do
2084 if not (drawDoors
xor panels
[idx
].Door
) then panels
[idx
].Draw();
2091 PANEL_WALL
: DrawPanels(gWalls
);
2092 PANEL_CLOSEDOOR
: DrawPanels(gWalls
, True);
2093 PANEL_BACK
: DrawPanels(gRenderBackgrounds
);
2094 PANEL_FORE
: DrawPanels(gRenderForegrounds
);
2095 PANEL_WATER
: DrawPanels(gWater
);
2096 PANEL_ACID1
: DrawPanels(gAcid1
);
2097 PANEL_ACID2
: DrawPanels(gAcid2
);
2098 PANEL_STEP
: DrawPanels(gSteps
);
2104 procedure g_Map_CollectDrawPanels (x0
, y0
, wdt
, hgt
: Integer);
2105 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2107 result
:= false; // don't stop, ever
2108 if ((tag
and GridTagDoor
) <> 0) <> pan
.Door
then exit
;
2109 gDrawPanelList
.insert(pan
);
2114 //tagmask := panelTypeToTag(PanelType);
2116 {if gdbg_map_use_tree_draw then
2118 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2122 gMapGrid
.forEachInAABB(x0
, y0
, wdt
, hgt
, checker
, (GridTagBack
or GridTagStep
or GridTagWall
or GridTagDoor
or GridTagAcid1
or GridTagAcid2
or GridTagWater
or GridTagFore
));
2124 // list will be rendered in `g_game.DrawPlayer()`
2128 procedure g_Map_DrawPanelShadowVolumes(lightX
: Integer; lightY
: Integer; radius
: Integer);
2129 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2131 result
:= false; // don't stop, ever
2132 pan
.DrawShadowVolume(lightX
, lightY
, radius
);
2136 {if gdbg_map_use_tree_draw then
2138 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2142 gMapGrid
.forEachInAABB(lightX
-radius
, lightY
-radius
, radius
*2, radius
*2, checker
, (GridTagWall
or GridTagDoor
));
2147 procedure g_Map_DrawBack(dx
, dy
: Integer);
2149 if gDrawBackGround
and (BackID
<> DWORD(-1)) then
2150 e_DrawSize(BackID
, dx
, dy
, 0, False, False, gBackSize
.X
, gBackSize
.Y
)
2152 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
2155 function g_Map_CollidePanelOld(X
, Y
: Integer; Width
, Height
: Word;
2156 PanelType
: Word; b1x3
: Boolean=false): Boolean;
2162 if WordBool(PanelType
and PANEL_WALL
) then
2163 if gWalls
<> nil then
2168 if gWalls
[a
].Enabled
and
2169 g_Collide(X
, Y
, Width
, Height
,
2170 gWalls
[a
].X
, gWalls
[a
].Y
,
2171 gWalls
[a
].Width
, gWalls
[a
].Height
) then
2178 if WordBool(PanelType
and PANEL_WATER
) then
2179 if gWater
<> nil then
2184 if g_Collide(X
, Y
, Width
, Height
,
2185 gWater
[a
].X
, gWater
[a
].Y
,
2186 gWater
[a
].Width
, gWater
[a
].Height
) then
2193 if WordBool(PanelType
and PANEL_ACID1
) then
2194 if gAcid1
<> nil then
2199 if g_Collide(X
, Y
, Width
, Height
,
2200 gAcid1
[a
].X
, gAcid1
[a
].Y
,
2201 gAcid1
[a
].Width
, gAcid1
[a
].Height
) then
2208 if WordBool(PanelType
and PANEL_ACID2
) then
2209 if gAcid2
<> nil then
2214 if g_Collide(X
, Y
, Width
, Height
,
2215 gAcid2
[a
].X
, gAcid2
[a
].Y
,
2216 gAcid2
[a
].Width
, gAcid2
[a
].Height
) then
2223 if WordBool(PanelType
and PANEL_STEP
) then
2224 if gSteps
<> nil then
2229 if g_Collide(X
, Y
, Width
, Height
,
2230 gSteps
[a
].X
, gSteps
[a
].Y
,
2231 gSteps
[a
].Width
, gSteps
[a
].Height
) then
2238 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then
2239 if gLifts
<> nil then
2244 if ((WordBool(PanelType
and (PANEL_LIFTUP
)) and (gLifts
[a
].LiftType
= 0)) or
2245 (WordBool(PanelType
and (PANEL_LIFTDOWN
)) and (gLifts
[a
].LiftType
= 1)) or
2246 (WordBool(PanelType
and (PANEL_LIFTLEFT
)) and (gLifts
[a
].LiftType
= 2)) or
2247 (WordBool(PanelType
and (PANEL_LIFTRIGHT
)) and (gLifts
[a
].LiftType
= 3))) and
2248 g_Collide(X
, Y
, Width
, Height
,
2249 gLifts
[a
].X
, gLifts
[a
].Y
,
2250 gLifts
[a
].Width
, gLifts
[a
].Height
) then
2257 if WordBool(PanelType
and PANEL_BLOCKMON
) then
2258 if gBlockMon
<> nil then
2260 h
:= High(gBlockMon
);
2264 ((gBlockMon
[a
].Width
+ gBlockMon
[a
].Height
) >= 64) ) and
2265 g_Collide(X
, Y
, Width
, Height
,
2266 gBlockMon
[a
].X
, gBlockMon
[a
].Y
,
2267 gBlockMon
[a
].Width
, gBlockMon
[a
].Height
) then
2275 function g_Map_CollideLiquid_TextureOld(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2279 function checkPanels (constref panels
: TPanelArray
): Boolean;
2284 if panels
= nil then exit
;
2285 for a
:= 0 to High(panels
) do
2287 if g_Collide(X
, Y
, Width
, Height
, panels
[a
].X
, panels
[a
].Y
, panels
[a
].Width
, panels
[a
].Height
) then
2290 texid
:= panels
[a
].GetTextureID();
2297 texid
:= TEXTURE_NONE
;
2299 if not checkPanels(gWater
) then
2300 if not checkPanels(gAcid1
) then
2301 if not checkPanels(gAcid2
) then exit
;
2306 function g_Map_CollidePanel(X
, Y
: Integer; Width
, Height
: Word; PanelType
: Word; b1x3
: Boolean): Boolean;
2307 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2309 result
:= false; // don't stop, ever
2311 if ((tag
and (GridTagWall
or GridTagDoor
)) <> 0) then
2313 if not pan
.Enabled
then exit
;
2316 if ((tag
and GridTagLift
) <> 0) then
2319 ((WordBool(PanelType
and PANEL_LIFTUP
) and (pan
.LiftType
= 0)) or
2320 (WordBool(PanelType
and PANEL_LIFTDOWN
) and (pan
.LiftType
= 1)) or
2321 (WordBool(PanelType
and PANEL_LIFTLEFT
) and (pan
.LiftType
= 2)) or
2322 (WordBool(PanelType
and PANEL_LIFTRIGHT
) and (pan
.LiftType
= 3))) and
2323 g_Collide(X
, Y
, Width
, Height
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
);
2327 if ((tag
and GridTagBlockMon
) <> 0) then
2329 result
:= ((not b1x3
) or (pan
.Width
+pan
.Height
>= 64)) and g_Collide(X
, Y
, Width
, Height
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
);
2334 result
:= g_Collide(X
, Y
, Width
, Height
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
);
2338 tagmask
: Integer = 0;
2340 if WordBool(PanelType
and (PANEL_WALL
or PANEL_CLOSEDOOR
or PANEL_OPENDOOR
)) then tagmask
:= tagmask
or (GridTagWall
or GridTagDoor
);
2341 if WordBool(PanelType
and PANEL_WATER
) then tagmask
:= tagmask
or GridTagWater
;
2342 if WordBool(PanelType
and PANEL_ACID1
) then tagmask
:= tagmask
or GridTagAcid1
;
2343 if WordBool(PanelType
and PANEL_ACID2
) then tagmask
:= tagmask
or GridTagAcid2
;
2344 if WordBool(PanelType
and PANEL_STEP
) then tagmask
:= tagmask
or GridTagStep
;
2345 if WordBool(PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
)) then tagmask
:= tagmask
or GridTagLift
;
2346 if WordBool(PanelType
and PANEL_BLOCKMON
) then tagmask
:= tagmask
or GridTagBlockMon
;
2348 if (tagmask
= 0) then begin result
:= false; exit
; end; // just in case
2350 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('*solids');
2351 if gdbg_map_use_accel_coldet
then
2353 {if gdbg_map_use_tree_coldet then
2355 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2356 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2357 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2359 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2360 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2365 result
:= gMapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, tagmask
);
2370 result
:= g_Map_CollidePanelOld(X
, Y
, Width
, Height
, PanelType
, b1x3
);
2372 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2376 function g_Map_CollideLiquid_Texture(X
, Y
: Integer; Width
, Height
: Word): DWORD
;
2378 cctype
: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2381 // slightly different from the old code, but meh...
2382 function checker (pan
: TPanel
; tag
: Integer): Boolean;
2384 result
:= false; // don't stop, ever
2385 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2388 0: if ((tag
and GridTagWater
) = 0) then exit
; // allowed: water
2389 1: if ((tag
and (GridTagWater
or GridTagAcid1
)) = 0) then exit
; // allowed: water, acid1
2390 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2393 if not g_Collide(X
, Y
, Width
, Height
, pan
.X
, pan
.Y
, pan
.Width
, pan
.Height
) then exit
;
2395 texid
:= pan
.GetTextureID();
2396 // water? water has the highest priority, so stop right here
2397 if ((tag
and GridTagWater
) <> 0) then begin cctype
:= 0; result
:= true; exit
; end;
2399 if ((tag
and GridTagAcid2
) <> 0) then cctype
:= 2;
2401 if ((tag
and GridTagAcid1
) <> 0) then cctype
:= 1;
2405 if (profMapCollision
<> nil) then profMapCollision
.sectionBeginAccum('liquids');
2406 if gdbg_map_use_accel_coldet
then
2408 texid
:= TEXTURE_NONE
;
2409 {if gdbg_map_use_tree_coldet then
2411 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2415 gMapGrid
.forEachInAABB(X
, Y
, Width
, Height
, checker
, (GridTagWater
or GridTagAcid1
or GridTagAcid2
));
2421 result
:= g_Map_CollideLiquid_TextureOld(X
, Y
, Width
, Height
);
2423 if (profMapCollision
<> nil) then profMapCollision
.sectionEnd();
2426 procedure g_Map_EnableWall(ID
: DWORD
);
2431 //g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2433 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2437 procedure g_Map_DisableWall(ID
: DWORD
);
2442 //g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2444 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2448 procedure g_Map_SwitchTexture(PanelType
: Word; ID
: DWORD
; AnimLoop
: Byte = 0);
2453 PANEL_WALL
, PANEL_OPENDOOR
, PANEL_CLOSEDOOR
:
2456 tp
:= gRenderForegrounds
[ID
];
2458 tp
:= gRenderBackgrounds
[ID
];
2471 tp
.NextTexture(AnimLoop
);
2472 if g_Game_IsServer
and g_Game_IsNet
then
2473 MH_SEND_PanelTexture(PanelType
, ID
, AnimLoop
);
2476 procedure g_Map_SetLift(ID
: DWORD
; t
: Integer);
2478 if gLifts
[ID
].LiftType
= t
then
2486 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2488 if LiftType = 0 then
2489 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2490 else if LiftType = 1 then
2491 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2492 else if LiftType = 2 then
2493 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2494 else if LiftType = 3 then
2495 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2498 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_PanelState(PanelType
, ID
);
2502 function g_Map_GetPoint(PointType
: Byte; var RespawnPoint
: TRespawnPoint
): Boolean;
2505 PointsArray
: Array of TRespawnPoint
;
2508 SetLength(PointsArray
, 0);
2510 if RespawnPoints
= nil then
2513 for a
:= 0 to High(RespawnPoints
) do
2514 if RespawnPoints
[a
].PointType
= PointType
then
2516 SetLength(PointsArray
, Length(PointsArray
)+1);
2517 PointsArray
[High(PointsArray
)] := RespawnPoints
[a
];
2520 if PointsArray
= nil then
2523 RespawnPoint
:= PointsArray
[Random(Length(PointsArray
))];
2527 function g_Map_GetPointCount(PointType
: Byte): Word;
2533 if RespawnPoints
= nil then
2536 for a
:= 0 to High(RespawnPoints
) do
2537 if RespawnPoints
[a
].PointType
= PointType
then
2538 Result
:= Result
+ 1;
2541 function g_Map_HaveFlagPoints(): Boolean;
2543 Result
:= (FlagPoints
[FLAG_RED
] <> nil) and (FlagPoints
[FLAG_BLUE
] <> nil);
2546 procedure g_Map_ResetFlag(Flag
: Byte);
2548 with gFlags
[Flag
] do
2554 Direction
:= D_LEFT
;
2555 State
:= FLAG_STATE_NONE
;
2556 if FlagPoints
[Flag
] <> nil then
2558 Obj
.X
:= FlagPoints
[Flag
]^.X
;
2559 Obj
.Y
:= FlagPoints
[Flag
]^.Y
;
2560 Direction
:= FlagPoints
[Flag
]^.Direction
;
2561 State
:= FLAG_STATE_NORMAL
;
2567 procedure g_Map_DrawFlags();
2570 Mirror
: TMirrorType
;
2572 if gGameSettings
.GameMode
<> GM_CTF
then
2575 for i
:= FLAG_RED
to FLAG_BLUE
do
2577 if State
<> FLAG_STATE_CAPTURED
then
2579 if State
= FLAG_STATE_NONE
then
2582 if Direction
= D_LEFT
then
2584 Mirror
:= M_HORIZONTAL
;
2593 Animation
.Draw(Obj
.X
+dx
, Obj
.Y
+1, Mirror
);
2595 if g_debug_Frames
then
2597 e_DrawQuad(Obj
.X
+Obj
.Rect
.X
,
2599 Obj
.X
+Obj
.Rect
.X
+Obj
.Rect
.Width
-1,
2600 Obj
.Y
+Obj
.Rect
.Y
+Obj
.Rect
.Height
-1,
2606 procedure g_Map_SaveState(Var Mem
: TBinMemoryWriter
);
2613 procedure SavePanelArray(var panels
: TPanelArray
);
2615 PAMem
: TBinMemoryWriter
;
2618 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2619 PAMem
:= TBinMemoryWriter
.Create((Length(panels
)+1) * 40);
2622 while i
< Length(panels
) do
2624 if panels
[i
].SaveIt
then
2628 // Ñîõðàíÿåì ïàíåëü:
2629 panels
[i
].SaveState(PAMem
);
2634 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2635 PAMem
.SaveToMemory(Mem
);
2639 procedure SaveFlag(flag
: PFlag
);
2642 dw
:= FLAG_SIGNATURE
; // 'FLAG'
2644 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2645 Mem
.WriteByte(flag
^.RespawnType
);
2647 Mem
.WriteByte(flag
^.State
);
2648 // Íàïðàâëåíèå ôëàãà:
2649 if flag
^.Direction
= D_LEFT
then
2655 Obj_SaveState(@flag
^.Obj
, Mem
);
2659 Mem
:= TBinMemoryWriter
.Create(1024 * 1024); // 1 MB
2661 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2662 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2663 SavePanelArray(gWalls
);
2664 // Ñîõðàíÿåì ïàíåëè ôîíà:
2665 SavePanelArray(gRenderBackgrounds
);
2666 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2667 SavePanelArray(gRenderForegrounds
);
2668 // Ñîõðàíÿåì ïàíåëè âîäû:
2669 SavePanelArray(gWater
);
2670 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2671 SavePanelArray(gAcid1
);
2672 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2673 SavePanelArray(gAcid2
);
2674 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2675 SavePanelArray(gSteps
);
2676 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2677 SavePanelArray(gLifts
);
2680 ///// Ñîõðàíÿåì ìóçûêó: /////
2681 // Ñèãíàòóðà ìóçûêè:
2682 dw
:= MUSIC_SIGNATURE
; // 'MUSI'
2685 Assert(gMusic
<> nil, 'g_Map_SaveState: gMusic = nil');
2686 if gMusic
.NoMusic
then
2690 Mem
.WriteString(str
, 64);
2691 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2692 dw
:= gMusic
.GetPosition();
2694 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2695 boo
:= gMusic
.SpecPause
;
2696 Mem
.WriteBoolean(boo
);
2699 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2700 Mem
.WriteInt(gTotalMonsters
);
2703 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2704 if gGameSettings
.GameMode
= GM_CTF
then
2706 // Ôëàã Êðàñíîé êîìàíäû:
2707 SaveFlag(@gFlags
[FLAG_RED
]);
2708 // Ôëàã Ñèíåé êîìàíäû:
2709 SaveFlag(@gFlags
[FLAG_BLUE
]);
2713 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2714 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
2716 // Î÷êè Êðàñíîé êîìàíäû:
2717 Mem
.WriteSmallInt(gTeamStat
[TEAM_RED
].Goals
);
2718 // Î÷êè Ñèíåé êîìàíäû:
2719 Mem
.WriteSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
2724 procedure g_Map_LoadState(Var Mem
: TBinMemoryReader
);
2731 procedure LoadPanelArray(var panels
: TPanelArray
);
2733 PAMem
: TBinMemoryReader
;
2736 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2737 PAMem
:= TBinMemoryReader
.Create();
2738 PAMem
.LoadFromMemory(Mem
);
2740 for i
:= 0 to Length(panels
)-1 do
2741 if panels
[i
].SaveIt
then
2747 raise EBinSizeError
.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2749 // Çàãðóæàåì ïàíåëü:
2750 panels
[i
].LoadState(PAMem
);
2753 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2757 procedure LoadFlag(flag
: PFlag
);
2761 if dw
<> FLAG_SIGNATURE
then // 'FLAG'
2763 raise EBinSizeError
.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2765 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2766 Mem
.ReadByte(flag
^.RespawnType
);
2768 Mem
.ReadByte(flag
^.State
);
2769 // Íàïðàâëåíèå ôëàãà:
2772 flag
^.Direction
:= D_LEFT
2774 flag
^.Direction
:= D_RIGHT
;
2776 Obj_LoadState(@flag
^.Obj
, Mem
);
2783 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2784 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2785 LoadPanelArray(gWalls
);
2786 // Çàãðóæàåì ïàíåëè ôîíà:
2787 LoadPanelArray(gRenderBackgrounds
);
2788 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2789 LoadPanelArray(gRenderForegrounds
);
2790 // Çàãðóæàåì ïàíåëè âîäû:
2791 LoadPanelArray(gWater
);
2792 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2793 LoadPanelArray(gAcid1
);
2794 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2795 LoadPanelArray(gAcid2
);
2796 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2797 LoadPanelArray(gSteps
);
2798 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2799 LoadPanelArray(gLifts
);
2802 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2806 ///// Çàãðóæàåì ìóçûêó: /////
2807 // Ñèãíàòóðà ìóçûêè:
2809 if dw
<> MUSIC_SIGNATURE
then // 'MUSI'
2811 raise EBinSizeError
.Create('g_Map_LoadState: Wrong Music Signature');
2814 Assert(gMusic
<> nil, 'g_Map_LoadState: gMusic = nil');
2815 Mem
.ReadString(str
);
2816 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2818 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2819 Mem
.ReadBoolean(boo
);
2820 // Çàïóñêàåì ýòó ìóçûêó:
2821 gMusic
.SetByName(str
);
2822 gMusic
.SpecPause
:= boo
;
2825 gMusic
.SetPosition(dw
);
2828 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2829 Mem
.ReadInt(gTotalMonsters
);
2832 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2833 if gGameSettings
.GameMode
= GM_CTF
then
2835 // Ôëàã Êðàñíîé êîìàíäû:
2836 LoadFlag(@gFlags
[FLAG_RED
]);
2837 // Ôëàã Ñèíåé êîìàíäû:
2838 LoadFlag(@gFlags
[FLAG_BLUE
]);
2842 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2843 if gGameSettings
.GameMode
in [GM_TDM
, GM_CTF
] then
2845 // Î÷êè Êðàñíîé êîìàíäû:
2846 Mem
.ReadSmallInt(gTeamStat
[TEAM_RED
].Goals
);
2847 // Î÷êè Ñèíåé êîìàíäû:
2848 Mem
.ReadSmallInt(gTeamStat
[TEAM_BLUE
].Goals
);
2853 function g_Map_PanelForPID(PanelID
: Integer; var PanelArrayID
: Integer): PPanel
;
2858 if (PanelID
< 0) or (PanelID
> High(PanelByID
)) then Exit
;
2859 Arr
:= PanelByID
[PanelID
].PWhere
^;
2860 PanelArrayID
:= PanelByID
[PanelID
].PArrID
;
2861 Result
:= Addr(Arr
[PanelByID
[PanelID
].PArrID
]);