DEADSOFTWARE

no more global `gItems[]` array; created DynTree for items (not used yet); also,...
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
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.
7 *
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.
12 *
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/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_map;
19 interface
21 uses
22 e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
23 g_phys, wadreader, BinEditor, g_panel, g_grid, z_aabbtree, md5, binheap, xprofiler;
25 type
26 TMapInfo = record
27 Map: String;
28 Name: String;
29 Description: String;
30 Author: String;
31 MusicName: String;
32 SkyName: String;
33 Height: Word;
34 Width: Word;
35 end;
37 PRespawnPoint = ^TRespawnPoint;
38 TRespawnPoint = record
39 X, Y: Integer;
40 Direction: TDirection;
41 PointType: Byte;
42 end;
44 PFlagPoint = ^TFlagPoint;
45 TFlagPoint = TRespawnPoint;
47 PFlag = ^TFlag;
48 TFlag = record
49 Obj: TObj;
50 RespawnType: Byte;
51 State: Byte;
52 Count: Integer;
53 CaptureTime: LongWord;
54 Animation: TAnimation;
55 Direction: TDirection;
56 end;
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): 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 procedure g_Map_ProfilersBegin ();
95 procedure g_Map_ProfilersEnd ();
97 const
98 RESPAWNPOINT_PLAYER1 = 1;
99 RESPAWNPOINT_PLAYER2 = 2;
100 RESPAWNPOINT_DM = 3;
101 RESPAWNPOINT_RED = 4;
102 RESPAWNPOINT_BLUE = 5;
104 FLAG_NONE = 0;
105 FLAG_RED = 1;
106 FLAG_BLUE = 2;
107 FLAG_DOM = 3;
109 FLAG_STATE_NONE = 0;
110 FLAG_STATE_NORMAL = 1;
111 FLAG_STATE_DROPPED = 2;
112 FLAG_STATE_CAPTURED = 3;
113 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
114 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
116 FLAG_TIME = 720; // 20 seconds
118 SKY_STRETCH: Single = 1.5;
120 const
121 GridTagInvalid = 0;
123 (* draw order:
124 PANEL_BACK
125 PANEL_STEP
126 PANEL_WALL
127 PANEL_CLOSEDOOR
128 PANEL_ACID1
129 PANEL_ACID2
130 PANEL_WATER
131 PANEL_FORE
132 *)
133 // sorted by draw priority
134 GridTagBack = 1 shl 0;
135 GridTagStep = 1 shl 1;
136 GridTagWall = 1 shl 2;
137 GridTagDoor = 1 shl 3;
138 GridTagAcid1 = 1 shl 4;
139 GridTagAcid2 = 1 shl 5;
140 GridTagWater = 1 shl 6;
141 GridTagFore = 1 shl 7;
142 // the following are invisible
143 GridTagLift = 1 shl 8;
144 GridTagBlockMon = 1 shl 9;
147 var
148 gWalls: TPanelArray;
149 gRenderBackgrounds: TPanelArray;
150 gRenderForegrounds: TPanelArray;
151 gWater, gAcid1, gAcid2: TPanelArray;
152 gSteps: TPanelArray;
153 gLifts: TPanelArray;
154 gBlockMon: TPanelArray;
155 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
156 //gDOMFlags: array of TFlag;
157 gMapInfo: TMapInfo;
158 gBackSize: TPoint;
159 gDoorMap: array of array of DWORD;
160 gLiftMap: array of array of DWORD;
161 gWADHash: TMD5Digest;
162 BackID: DWORD = DWORD(-1);
163 gExternalResources: TStringList;
165 gdbg_map_use_accel_render: Boolean = true;
166 gdbg_map_use_accel_coldet: Boolean = true;
167 gdbg_map_use_tree_draw: Boolean = false;
168 gdbg_map_use_tree_coldet: Boolean = false;
169 gdbg_map_dump_coldet_tree_queries: Boolean = false;
170 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
171 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
173 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
175 implementation
177 uses
178 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
179 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
180 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
181 Math, g_monsters, g_saveload, g_language, g_netmsg,
182 utils, sfs,
183 ImagingTypes, Imaging, ImagingUtility,
184 ImagingGif, ImagingNetworkGraphics;
186 const
187 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
188 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
189 FLAG_SIGNATURE = $47414C46; // 'FLAG'
192 function panelTypeToTag (panelType: Word): Integer;
193 begin
194 case panelType of
195 PANEL_WALL: result := GridTagWall; // gWalls
196 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
197 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
198 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
199 PANEL_WATER: result := GridTagWater; // gWater
200 PANEL_ACID1: result := GridTagAcid1; // gAcid1
201 PANEL_ACID2: result := GridTagAcid2; // gAcid2
202 PANEL_STEP: result := GridTagStep; // gSteps
203 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
204 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
205 else result := GridTagInvalid;
206 end;
207 end;
210 function dplLess (a, b: TObject): Boolean;
211 var
212 pa, pb: TPanel;
213 begin
214 //result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
215 pa := TPanel(a);
216 pb := TPanel(b);
217 if (pa.tag < pb.tag) then begin result := true; exit; end;
218 if (pa.tag > pb.tag) then begin result := false; exit; end;
219 result := (pa.ArrIdx < pb.ArrIdx);
220 end;
222 procedure dplClear ();
223 begin
224 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
225 end;
228 type
229 TPanelID = record
230 PWhere: ^TPanelArray;
231 PArrID: Integer;
232 end;
234 type
235 TDynAABBTreeMap = class(TDynAABBTree)
236 function getFleshAABB (var aabb: AABB2D; flesh: TTreeFlesh; tag: Integer): Boolean; override;
237 end;
239 function TDynAABBTreeMap.getFleshAABB (var aabb: AABB2D; flesh: TTreeFlesh; tag: Integer): Boolean;
240 var
241 pan: TPanel;
242 begin
243 result := false;
244 if (flesh = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
245 //pan := (flesh as TPanel);
246 pan := TPanel(flesh);
247 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width, pan.Y+pan.Height);
248 if (pan.Width < 1) or (pan.Height < 1) then exit;
249 //if (pan.Width = 1) then aabb.maxX += 1;
250 //if (pan.Height = 1) then aabb.maxY += 1;
251 //if (pan.Width < 3) or (pan.Height < 3) then exit;
252 //aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-2, pan.Y+pan.Height-2);
253 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
254 result := aabb.valid;
255 end;
257 var
258 PanelById: array of TPanelID;
259 Textures: TLevelTextureArray;
260 RespawnPoints: Array of TRespawnPoint;
261 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
262 //DOMFlagPoints: Array of TFlagPoint;
263 gMapGrid: TBodyGrid = nil;
264 mapTree: TDynAABBTree = nil;
267 procedure g_Map_ProfilersBegin ();
268 begin
269 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
270 profMapCollision.mainBegin(g_profile_collision);
271 // create sections
272 if g_profile_collision then
273 begin
274 profMapCollision.sectionBegin('*solids');
275 profMapCollision.sectionEnd();
276 profMapCollision.sectionBegin('liquids');
277 profMapCollision.sectionEnd();
278 end;
279 end;
281 procedure g_Map_ProfilersEnd ();
282 begin
283 if (profMapCollision <> nil) then profMapCollision.mainEnd();
284 end;
287 function g_Map_IsSpecialTexture(Texture: String): Boolean;
288 begin
289 Result := (Texture = TEXTURE_NAME_WATER) or
290 (Texture = TEXTURE_NAME_ACID1) or
291 (Texture = TEXTURE_NAME_ACID2);
292 end;
294 procedure CreateDoorMap();
295 var
296 PanelArray: Array of record
297 X, Y: Integer;
298 Width, Height: Word;
299 Active: Boolean;
300 PanelID: DWORD;
301 end;
302 a, b, c, m, i, len: Integer;
303 ok: Boolean;
304 begin
305 if gWalls = nil then
306 Exit;
308 i := 0;
309 len := 128;
310 SetLength(PanelArray, len);
312 for a := 0 to High(gWalls) do
313 if gWalls[a].Door then
314 begin
315 PanelArray[i].X := gWalls[a].X;
316 PanelArray[i].Y := gWalls[a].Y;
317 PanelArray[i].Width := gWalls[a].Width;
318 PanelArray[i].Height := gWalls[a].Height;
319 PanelArray[i].Active := True;
320 PanelArray[i].PanelID := a;
322 i := i + 1;
323 if i = len then
324 begin
325 len := len + 128;
326 SetLength(PanelArray, len);
327 end;
328 end;
330 // Íåò äâåðåé:
331 if i = 0 then
332 begin
333 PanelArray := nil;
334 Exit;
335 end;
337 SetLength(gDoorMap, 0);
339 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
341 for a := 0 to i-1 do
342 if PanelArray[a].Active then
343 begin
344 PanelArray[a].Active := False;
345 m := Length(gDoorMap);
346 SetLength(gDoorMap, m+1);
347 SetLength(gDoorMap[m], 1);
348 gDoorMap[m, 0] := PanelArray[a].PanelID;
349 ok := True;
351 while ok do
352 begin
353 ok := False;
355 for b := 0 to i-1 do
356 if PanelArray[b].Active then
357 for c := 0 to High(gDoorMap[m]) do
358 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
359 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
360 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
361 PanelArray[b].Width, PanelArray[b].Height,
362 gWalls[gDoorMap[m, c]].X,
363 gWalls[gDoorMap[m, c]].Y,
364 gWalls[gDoorMap[m, c]].Width,
365 gWalls[gDoorMap[m, c]].Height) then
366 begin
367 PanelArray[b].Active := False;
368 SetLength(gDoorMap[m],
369 Length(gDoorMap[m])+1);
370 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
371 ok := True;
372 Break;
373 end;
374 end;
376 g_Game_StepLoading();
377 end;
379 PanelArray := nil;
380 end;
382 procedure CreateLiftMap();
383 var
384 PanelArray: Array of record
385 X, Y: Integer;
386 Width, Height: Word;
387 Active: Boolean;
388 end;
389 a, b, c, len, i, j: Integer;
390 ok: Boolean;
391 begin
392 if gLifts = nil then
393 Exit;
395 len := Length(gLifts);
396 SetLength(PanelArray, len);
398 for a := 0 to len-1 do
399 begin
400 PanelArray[a].X := gLifts[a].X;
401 PanelArray[a].Y := gLifts[a].Y;
402 PanelArray[a].Width := gLifts[a].Width;
403 PanelArray[a].Height := gLifts[a].Height;
404 PanelArray[a].Active := True;
405 end;
407 SetLength(gLiftMap, len);
408 i := 0;
410 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
412 for a := 0 to len-1 do
413 if PanelArray[a].Active then
414 begin
415 PanelArray[a].Active := False;
416 SetLength(gLiftMap[i], 32);
417 j := 0;
418 gLiftMap[i, j] := a;
419 ok := True;
421 while ok do
422 begin
423 ok := False;
424 for b := 0 to len-1 do
425 if PanelArray[b].Active then
426 for c := 0 to j do
427 if g_CollideAround(PanelArray[b].X,
428 PanelArray[b].Y,
429 PanelArray[b].Width,
430 PanelArray[b].Height,
431 PanelArray[gLiftMap[i, c]].X,
432 PanelArray[gLiftMap[i, c]].Y,
433 PanelArray[gLiftMap[i, c]].Width,
434 PanelArray[gLiftMap[i, c]].Height) then
435 begin
436 PanelArray[b].Active := False;
437 j := j+1;
438 if j > High(gLiftMap[i]) then
439 SetLength(gLiftMap[i],
440 Length(gLiftMap[i])+32);
442 gLiftMap[i, j] := b;
443 ok := True;
445 Break;
446 end;
447 end;
449 SetLength(gLiftMap[i], j+1);
450 i := i+1;
452 g_Game_StepLoading();
453 end;
455 SetLength(gLiftMap, i);
457 PanelArray := nil;
458 end;
460 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
461 CurTex: Integer; sav: Boolean): Integer;
462 var
463 len: Integer;
464 panels: ^TPanelArray;
465 begin
466 Result := -1;
468 case PanelRec.PanelType of
469 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
470 panels := @gWalls;
471 PANEL_BACK:
472 panels := @gRenderBackgrounds;
473 PANEL_FORE:
474 panels := @gRenderForegrounds;
475 PANEL_WATER:
476 panels := @gWater;
477 PANEL_ACID1:
478 panels := @gAcid1;
479 PANEL_ACID2:
480 panels := @gAcid2;
481 PANEL_STEP:
482 panels := @gSteps;
483 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
484 panels := @gLifts;
485 PANEL_BLOCKMON:
486 panels := @gBlockMon;
487 else
488 Exit;
489 end;
491 len := Length(panels^);
492 SetLength(panels^, len + 1);
494 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
495 panels^[len].ArrIdx := len;
496 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
497 if sav then
498 panels^[len].SaveIt := True;
500 Result := len;
502 len := Length(PanelByID);
503 SetLength(PanelByID, len + 1);
504 PanelByID[len].PWhere := panels;
505 PanelByID[len].PArrID := Result;
506 end;
508 function CreateNullTexture(RecName: String): Integer;
509 begin
510 SetLength(Textures, Length(Textures)+1);
511 result := High(Textures);
513 with Textures[High(Textures)] do
514 begin
515 TextureName := RecName;
516 Width := 1;
517 Height := 1;
518 Anim := False;
519 TextureID := TEXTURE_NONE;
520 end;
521 end;
523 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
524 var
525 WAD: TWADFile;
526 TextureData: Pointer;
527 WADName, txname: String;
528 a, ResLength: Integer;
529 begin
530 Result := -1;
532 if Textures <> nil then
533 for a := 0 to High(Textures) do
534 if Textures[a].TextureName = RecName then
535 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
536 Result := a;
537 Exit;
538 end;
540 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
541 if (RecName = TEXTURE_NAME_WATER) or
542 (RecName = TEXTURE_NAME_ACID1) or
543 (RecName = TEXTURE_NAME_ACID2) then
544 begin
545 SetLength(Textures, Length(Textures)+1);
547 with Textures[High(Textures)] do
548 begin
549 TextureName := RecName;
551 if TextureName = TEXTURE_NAME_WATER then
552 TextureID := TEXTURE_SPECIAL_WATER
553 else
554 if TextureName = TEXTURE_NAME_ACID1 then
555 TextureID := TEXTURE_SPECIAL_ACID1
556 else
557 if TextureName = TEXTURE_NAME_ACID2 then
558 TextureID := TEXTURE_SPECIAL_ACID2;
560 Anim := False;
561 end;
563 result := High(Textures);
564 Exit;
565 end;
567 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
568 WADName := g_ExtractWadName(RecName);
570 WAD := TWADFile.Create();
572 if WADName <> '' then
573 WADName := GameDir+'/wads/'+WADName
574 else
575 WADName := Map;
577 WAD.ReadFile(WADName);
579 txname := RecName;
581 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
582 begin
583 FreeMem(TextureData);
584 RecName := 'COMMON\ALIEN';
585 end;
588 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
589 begin
590 SetLength(Textures, Length(Textures)+1);
591 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
592 Exit;
593 e_GetTextureSize(Textures[High(Textures)].TextureID,
594 @Textures[High(Textures)].Width,
595 @Textures[High(Textures)].Height);
596 FreeMem(TextureData);
597 Textures[High(Textures)].TextureName := {RecName}txname;
598 Textures[High(Textures)].Anim := False;
600 result := High(Textures);
601 end
602 else // Íåò òàêîãî ðåóñðñà â WAD'å
603 begin
604 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
605 if log then
606 begin
607 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
608 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
609 end;
610 end;
612 WAD.Free();
613 end;
615 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
616 var
617 WAD: TWADFile;
618 TextureWAD: PChar = nil;
619 TextData: Pointer = nil;
620 TextureData: Pointer = nil;
621 cfg: TConfig = nil;
622 WADName: String;
623 ResLength: Integer;
624 TextureResource: String;
625 _width, _height, _framecount, _speed: Integer;
626 _backanimation: Boolean;
627 //imgfmt: string;
628 ia: TDynImageDataArray = nil;
629 f, c, frdelay, frloop: Integer;
630 begin
631 result := -1;
633 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
635 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
636 WADName := g_ExtractWadName(RecName);
638 WAD := TWADFile.Create();
639 try
640 if WADName <> '' then
641 WADName := GameDir+'/wads/'+WADName
642 else
643 WADName := Map;
645 WAD.ReadFile(WADName);
647 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
648 begin
649 if log then
650 begin
651 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
652 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
653 end;
654 exit;
655 end;
657 {TEST
658 if WADName = Map then
659 begin
660 //FreeMem(TextureWAD);
661 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
662 end;
665 WAD.FreeWAD();
667 if ResLength < 6 then
668 begin
669 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
670 exit;
671 end;
673 // ýòî ïòèöà? ýòî ñàìîë¸ò?
674 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
675 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
676 begin
677 // íåò, ýòî ñóïåðìåí!
678 if not WAD.ReadMemory(TextureWAD, ResLength) then
679 begin
680 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
681 exit;
682 end;
684 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
685 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
686 begin
687 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
688 exit;
689 end;
691 cfg := TConfig.CreateMem(TextData, ResLength);
693 TextureResource := cfg.ReadStr('', 'resource', '');
694 if TextureResource = '' then
695 begin
696 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
697 exit;
698 end;
700 _width := cfg.ReadInt('', 'framewidth', 0);
701 _height := cfg.ReadInt('', 'frameheight', 0);
702 _framecount := cfg.ReadInt('', 'framecount', 0);
703 _speed := cfg.ReadInt('', 'waitcount', 0);
704 _backanimation := cfg.ReadBool('', 'backanimation', False);
706 cfg.Free();
707 cfg := nil;
709 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
710 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
711 begin
712 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
713 exit;
714 end;
716 WAD.Free();
717 WAD := nil;
719 SetLength(Textures, Length(Textures)+1);
720 with Textures[High(Textures)] do
721 begin
722 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
723 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
724 begin
725 TextureName := RecName;
726 Width := _width;
727 Height := _height;
728 Anim := True;
729 FramesCount := _framecount;
730 Speed := _speed;
731 result := High(Textures);
732 end
733 else
734 begin
735 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
736 end;
737 end;
738 end
739 else
740 begin
741 // try animated image
743 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
744 if length(imgfmt) = 0 then
745 begin
746 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
747 exit;
748 end;
750 GlobalMetadata.ClearMetaItems();
751 GlobalMetadata.ClearMetaItemsForSaving();
752 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
753 begin
754 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
755 exit;
756 end;
757 if length(ia) = 0 then
758 begin
759 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
760 exit;
761 end;
763 WAD.Free();
764 WAD := nil;
766 _width := ia[0].width;
767 _height := ia[0].height;
768 _framecount := length(ia);
769 _speed := 1;
770 _backanimation := false;
771 frdelay := -1;
772 frloop := -666;
773 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
774 begin
775 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
776 try
777 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
778 frdelay := f;
779 if f < 0 then f := 0;
780 // rounding ;-)
781 c := f mod 28;
782 if c < 13 then c := 0 else c := 1;
783 f := (f div 28)+c;
784 if f < 1 then f := 1 else if f > 255 then f := 255;
785 _speed := f;
786 except
787 end;
788 end;
789 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
790 begin
791 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
792 try
793 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
794 frloop := f;
795 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
796 except
797 end;
798 end;
799 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
800 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
801 f := ord(_backanimation);
802 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);
804 SetLength(Textures, Length(Textures)+1);
805 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
806 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
807 begin
808 Textures[High(Textures)].TextureName := RecName;
809 Textures[High(Textures)].Width := _width;
810 Textures[High(Textures)].Height := _height;
811 Textures[High(Textures)].Anim := True;
812 Textures[High(Textures)].FramesCount := length(ia);
813 Textures[High(Textures)].Speed := _speed;
814 result := High(Textures);
815 //writeln(' CREATED!');
816 end
817 else
818 begin
819 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
820 end;
821 end;
822 finally
823 for f := 0 to High(ia) do FreeImage(ia[f]);
824 WAD.Free();
825 cfg.Free();
826 if TextureWAD <> nil then FreeMem(TextureWAD);
827 if TextData <> nil then FreeMem(TextData);
828 if TextureData <> nil then FreeMem(TextureData);
829 end;
830 end;
832 procedure CreateItem(Item: TItemRec_1);
833 begin
834 if g_Game_IsClient then Exit;
836 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
837 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
838 Exit;
840 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
841 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
842 end;
844 procedure CreateArea(Area: TAreaRec_1);
845 var
846 a: Integer;
847 id: DWORD;
848 begin
849 case Area.AreaType of
850 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
851 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
852 begin
853 SetLength(RespawnPoints, Length(RespawnPoints)+1);
854 with RespawnPoints[High(RespawnPoints)] do
855 begin
856 X := Area.X;
857 Y := Area.Y;
858 Direction := TDirection(Area.Direction);
860 case Area.AreaType of
861 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
862 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
863 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
864 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
865 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
866 end;
867 end;
868 end;
870 AREA_REDFLAG, AREA_BLUEFLAG:
871 begin
872 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
874 if FlagPoints[a] <> nil then Exit;
876 New(FlagPoints[a]);
878 with FlagPoints[a]^ do
879 begin
880 X := Area.X-FLAGRECT.X;
881 Y := Area.Y-FLAGRECT.Y;
882 Direction := TDirection(Area.Direction);
883 end;
885 with gFlags[a] do
886 begin
887 case a of
888 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
889 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
890 end;
892 Animation := TAnimation.Create(id, True, 8);
893 Obj.Rect := FLAGRECT;
895 g_Map_ResetFlag(a);
896 end;
897 end;
899 AREA_DOMFLAG:
900 begin
901 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
902 with DOMFlagPoints[High(DOMFlagPoints)] do
903 begin
904 X := Area.X;
905 Y := Area.Y;
906 Direction := TDirection(Area.Direction);
907 end;
909 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
910 end;
911 end;
912 end;
914 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
915 var
916 _trigger: TTrigger;
917 begin
918 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
920 with _trigger do
921 begin
922 X := Trigger.X;
923 Y := Trigger.Y;
924 Width := Trigger.Width;
925 Height := Trigger.Height;
926 Enabled := ByteBool(Trigger.Enabled);
927 TexturePanel := Trigger.TexturePanel;
928 TexturePanelType := fTexturePanel1Type;
929 ShotPanelType := fTexturePanel2Type;
930 TriggerType := Trigger.TriggerType;
931 ActivateType := Trigger.ActivateType;
932 Keys := Trigger.Keys;
933 Data.Default := Trigger.DATA;
934 end;
936 g_Triggers_Create(_trigger);
937 end;
939 procedure CreateMonster(monster: TMonsterRec_1);
940 var
941 a, i: Integer;
942 begin
943 if g_Game_IsClient then Exit;
945 if (gGameSettings.GameType = GT_SINGLE)
946 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
947 begin
948 i := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y,
949 TDirection(monster.Direction));
951 if gTriggers <> nil then
952 for a := 0 to High(gTriggers) do
953 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
954 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
955 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
956 gMonsters[i].AddTrigger(a);
958 if monster.MonsterType <> MONSTER_BARREL then
959 Inc(gTotalMonsters);
960 end;
961 end;
963 procedure g_Map_ReAdd_DieTriggers();
964 var
965 i, a: Integer;
966 begin
967 if g_Game_IsClient then Exit;
969 for i := 0 to High(gMonsters) do
970 if gMonsters[i] <> nil then
971 begin
972 gMonsters[i].ClearTriggers();
974 for a := 0 to High(gTriggers) do
975 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
976 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
977 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
978 gMonsters[i].AddTrigger(a);
979 end;
980 end;
982 function extractWadName(resourceName: string): string;
983 var
984 posN: Integer;
985 begin
986 posN := Pos(':', resourceName);
987 if posN > 0 then
988 Result:= Copy(resourceName, 0, posN-1)
989 else
990 Result := '';
991 end;
993 procedure addResToExternalResList(res: string);
994 begin
995 res := extractWadName(res);
996 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
997 gExternalResources.Add(res);
998 end;
1000 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1001 var
1002 textures: TTexturesRec1Array;
1003 mapHeader: TMapHeaderRec_1;
1004 i: integer;
1005 resFile: String = '';
1006 begin
1007 if gExternalResources = nil then
1008 gExternalResources := TStringList.Create;
1010 gExternalResources.Clear;
1011 textures := mapReader.GetTextures();
1012 for i := 0 to High(textures) do
1013 begin
1014 addResToExternalResList(resFile);
1015 end;
1017 textures := nil;
1019 mapHeader := mapReader.GetMapHeader;
1021 addResToExternalResList(mapHeader.MusicName);
1022 addResToExternalResList(mapHeader.SkyName);
1023 end;
1025 procedure mapCreateGrid ();
1026 var
1027 mapX0: Integer = $3fffffff;
1028 mapY0: Integer = $3fffffff;
1029 mapX1: Integer = -$3fffffff;
1030 mapY1: Integer = -$3fffffff;
1032 procedure calcBoundingBox (var panels: TPanelArray);
1033 var
1034 idx: Integer;
1035 pan: TPanel;
1036 begin
1037 for idx := 0 to High(panels) do
1038 begin
1039 pan := panels[idx];
1040 if not pan.visvalid then continue;
1041 if (pan.Width < 1) or (pan.Height < 1) then continue;
1042 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1043 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1044 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1045 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1046 end;
1047 end;
1049 procedure addPanelsToGrid (var panels: TPanelArray; tag: Integer);
1050 var
1051 idx: Integer;
1052 pan: TPanel;
1053 begin
1054 tag := panelTypeToTag(tag);
1055 for idx := High(panels) downto 0 do
1056 begin
1057 pan := panels[idx];
1058 pan.tag := tag;
1059 if not pan.visvalid then continue;
1060 gMapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1061 mapTree.insertObject(pan, tag, true); // as static object
1062 end;
1063 end;
1065 begin
1066 gMapGrid.Free();
1067 gMapGrid := nil;
1068 mapTree.Free();
1069 mapTree := nil;
1071 calcBoundingBox(gWalls);
1072 calcBoundingBox(gRenderBackgrounds);
1073 calcBoundingBox(gRenderForegrounds);
1074 calcBoundingBox(gWater);
1075 calcBoundingBox(gAcid1);
1076 calcBoundingBox(gAcid2);
1077 calcBoundingBox(gSteps);
1078 calcBoundingBox(gLifts);
1079 calcBoundingBox(gBlockMon);
1081 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1083 gMapGrid := TBodyGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1084 mapTree := TDynAABBTreeMap.Create();
1086 addPanelsToGrid(gWalls, PANEL_WALL);
1087 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1088 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1089 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1090 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1091 addPanelsToGrid(gWater, PANEL_WATER);
1092 addPanelsToGrid(gAcid1, PANEL_ACID1);
1093 addPanelsToGrid(gAcid2, PANEL_ACID2);
1094 addPanelsToGrid(gSteps, PANEL_STEP);
1095 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1096 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1098 gMapGrid.dumpStats();
1099 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1100 mapTree.forEachLeaf(nil);
1101 end;
1103 function g_Map_Load(Res: String): Boolean;
1104 const
1105 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1106 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1107 var
1108 WAD: TWADFile;
1109 MapReader: TMapReader_1;
1110 Header: TMapHeaderRec_1;
1111 _textures: TTexturesRec1Array;
1112 _texnummap: array of Integer; // `_textures` -> `Textures`
1113 panels: TPanelsRec1Array;
1114 items: TItemsRec1Array;
1115 monsters: TMonsterRec1Array;
1116 areas: TAreasRec1Array;
1117 triggers: TTriggersRec1Array;
1118 a, b, c, k: Integer;
1119 PanelID: DWORD;
1120 AddTextures: TAddTextureArray;
1121 texture: TTextureRec_1;
1122 TriggersTable: Array of record
1123 TexturePanel: Integer;
1124 LiftPanel: Integer;
1125 DoorPanel: Integer;
1126 ShotPanel: Integer;
1127 end;
1128 FileName, mapResName, s, TexName: String;
1129 Data: Pointer;
1130 Len: Integer;
1131 ok, isAnim, trigRef: Boolean;
1132 CurTex, ntn: Integer;
1134 begin
1135 gMapGrid.Free();
1136 gMapGrid := nil;
1137 mapTree.Free();
1138 mapTree := nil;
1140 Result := False;
1141 gMapInfo.Map := Res;
1142 TriggersTable := nil;
1143 FillChar(texture, SizeOf(texture), 0);
1145 sfsGCDisable(); // temporary disable removing of temporary volumes
1146 try
1147 // Çàãðóçêà WAD:
1148 FileName := g_ExtractWadName(Res);
1149 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1150 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1152 WAD := TWADFile.Create();
1153 if not WAD.ReadFile(FileName) then
1154 begin
1155 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1156 WAD.Free();
1157 Exit;
1158 end;
1159 //k8: why loader ignores path here?
1160 mapResName := g_ExtractFileName(Res);
1161 if not WAD.GetMapResource(mapResName, Data, Len) then
1162 begin
1163 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1164 WAD.Free();
1165 Exit;
1166 end;
1168 WAD.Free();
1170 // Çàãðóçêà êàðòû:
1171 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1172 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1173 MapReader := TMapReader_1.Create();
1175 if not MapReader.LoadMap(Data) then
1176 begin
1177 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1178 FreeMem(Data);
1179 MapReader.Free();
1180 Exit;
1181 end;
1183 FreeMem(Data);
1184 generateExternalResourcesList(MapReader);
1185 // Çàãðóçêà òåêñòóð:
1186 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1187 _textures := MapReader.GetTextures();
1188 _texnummap := nil;
1190 // Äîáàâëåíèå òåêñòóð â Textures[]:
1191 if _textures <> nil then
1192 begin
1193 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1194 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1195 SetLength(_texnummap, length(_textures));
1197 for a := 0 to High(_textures) do
1198 begin
1199 SetLength(s, 64);
1200 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1201 for b := 1 to Length(s) do
1202 if s[b] = #0 then
1203 begin
1204 SetLength(s, b-1);
1205 Break;
1206 end;
1207 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1208 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1209 // Àíèìèðîâàííàÿ òåêñòóðà:
1210 if ByteBool(_textures[a].Anim) then
1211 begin
1212 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1213 if ntn < 0 then
1214 begin
1215 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1216 ntn := CreateNullTexture(_textures[a].Resource);
1217 end;
1218 end
1219 else // Îáû÷íàÿ òåêñòóðà:
1220 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1221 if ntn < 0 then
1222 begin
1223 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1224 ntn := CreateNullTexture(_textures[a].Resource);
1225 end;
1227 _texnummap[a] := ntn; // fix texture number
1228 g_Game_StepLoading();
1229 end;
1230 end;
1232 // Çàãðóçêà òðèããåðîâ:
1233 gTriggerClientID := 0;
1234 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1235 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1236 triggers := MapReader.GetTriggers();
1238 // Çàãðóçêà ïàíåëåé:
1239 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1240 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1241 panels := MapReader.GetPanels();
1243 // check texture numbers for panels
1244 for a := 0 to High(panels) do
1245 begin
1246 if panels[a].TextureNum > High(_textures) then
1247 begin
1248 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1249 result := false;
1250 exit;
1251 end;
1252 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1253 end;
1255 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1256 if triggers <> nil then
1257 begin
1258 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1259 SetLength(TriggersTable, Length(triggers));
1260 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1262 for a := 0 to High(TriggersTable) do
1263 begin
1264 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1265 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1266 // Ëèôòû:
1267 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1268 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1269 else
1270 TriggersTable[a].LiftPanel := -1;
1271 // Äâåðè:
1272 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1273 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1274 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1275 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1276 else
1277 TriggersTable[a].DoorPanel := -1;
1278 // Òóðåëü:
1279 if triggers[a].TriggerType = TRIGGER_SHOT then
1280 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1281 else
1282 TriggersTable[a].ShotPanel := -1;
1284 g_Game_StepLoading();
1285 end;
1286 end;
1288 // Ñîçäàåì ïàíåëè:
1289 if panels <> nil then
1290 begin
1291 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1292 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1294 for a := 0 to High(panels) do
1295 begin
1296 SetLength(AddTextures, 0);
1297 trigRef := False;
1298 CurTex := -1;
1299 if _textures <> nil then
1300 begin
1301 texture := _textures[panels[a].TextureNum];
1302 ok := True;
1303 end
1304 else
1305 ok := False;
1307 if ok then
1308 begin
1309 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1310 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1311 ok := False;
1312 if (TriggersTable <> nil) and (_textures <> nil) then
1313 for b := 0 to High(TriggersTable) do
1314 if (TriggersTable[b].TexturePanel = a)
1315 or (TriggersTable[b].ShotPanel = a) then
1316 begin
1317 trigRef := True;
1318 ok := True;
1319 Break;
1320 end;
1321 end;
1323 if ok then
1324 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1325 SetLength(s, 64);
1326 CopyMemory(@s[1], @texture.Resource[0], 64);
1327 // Èçìåðÿåì äëèíó:
1328 Len := Length(s);
1329 for c := Len downto 1 do
1330 if s[c] <> #0 then
1331 begin
1332 Len := c;
1333 Break;
1334 end;
1335 SetLength(s, Len);
1337 // Ñïåö-òåêñòóðû çàïðåùåíû:
1338 if g_Map_IsSpecialTexture(s) then
1339 ok := False
1340 else
1341 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1342 ok := g_Texture_NumNameFindStart(s);
1344 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1345 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1346 if ok then
1347 begin
1348 k := NNF_NAME_BEFORE;
1349 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1350 while ok or (k = NNF_NAME_BEFORE) or
1351 (k = NNF_NAME_EQUALS) do
1352 begin
1353 k := g_Texture_NumNameFindNext(TexName);
1355 if (k = NNF_NAME_BEFORE) or
1356 (k = NNF_NAME_AFTER) then
1357 begin
1358 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1359 if ByteBool(texture.Anim) then
1360 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1361 isAnim := True;
1362 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1363 if not ok then
1364 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1365 isAnim := False;
1366 ok := CreateTexture(TexName, FileName, False) >= 0;
1367 end;
1368 end
1369 else
1370 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1371 isAnim := False;
1372 ok := CreateTexture(TexName, FileName, False) >= 0;
1373 if not ok then
1374 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1375 isAnim := True;
1376 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1377 end;
1378 end;
1380 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1381 if ok then
1382 begin
1383 for c := 0 to High(Textures) do
1384 if Textures[c].TextureName = TexName then
1385 begin
1386 SetLength(AddTextures, Length(AddTextures)+1);
1387 AddTextures[High(AddTextures)].Texture := c;
1388 AddTextures[High(AddTextures)].Anim := isAnim;
1389 Break;
1390 end;
1391 end;
1392 end
1393 else
1394 if k = NNF_NAME_EQUALS then
1395 begin
1396 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1397 SetLength(AddTextures, Length(AddTextures)+1);
1398 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1399 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1400 CurTex := High(AddTextures);
1401 ok := True;
1402 end
1403 else // NNF_NO_NAME
1404 ok := False;
1405 end; // while ok...
1407 ok := True;
1408 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1409 end; // if ok - ññûëàþòñÿ òðèããåðû
1411 if not ok then
1412 begin
1413 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1414 SetLength(AddTextures, 1);
1415 AddTextures[0].Texture := panels[a].TextureNum;
1416 AddTextures[0].Anim := ByteBool(texture.Anim);
1417 CurTex := 0;
1418 end;
1420 //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);
1422 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1423 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1425 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1426 if TriggersTable <> nil then
1427 for b := 0 to High(TriggersTable) do
1428 begin
1429 // Òðèããåð äâåðè/ëèôòà:
1430 if (TriggersTable[b].LiftPanel = a) or
1431 (TriggersTable[b].DoorPanel = a) then
1432 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1433 // Òðèããåð ñìåíû òåêñòóðû:
1434 if TriggersTable[b].TexturePanel = a then
1435 triggers[b].TexturePanel := PanelID;
1436 // Òðèããåð "Òóðåëü":
1437 if TriggersTable[b].ShotPanel = a then
1438 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1439 end;
1441 g_Game_StepLoading();
1442 end;
1443 end;
1445 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1446 if (triggers <> nil) and not gLoadGameMode then
1447 begin
1448 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1449 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1450 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1451 for a := 0 to High(triggers) do
1452 begin
1453 if triggers[a].TexturePanel <> -1 then
1454 b := panels[TriggersTable[a].TexturePanel].PanelType
1455 else
1456 b := 0;
1457 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1458 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1459 c := panels[TriggersTable[a].ShotPanel].PanelType
1460 else
1461 c := 0;
1462 CreateTrigger(triggers[a], b, c);
1463 end;
1464 end;
1466 // Çàãðóçêà ïðåäìåòîâ:
1467 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1468 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1469 items := MapReader.GetItems();
1471 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1472 if (items <> nil) and not gLoadGameMode then
1473 begin
1474 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1475 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1476 for a := 0 to High(items) do
1477 CreateItem(Items[a]);
1478 end;
1480 // Çàãðóçêà îáëàñòåé:
1481 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1482 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1483 areas := MapReader.GetAreas();
1485 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1486 if areas <> nil then
1487 begin
1488 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1489 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1490 for a := 0 to High(areas) do
1491 CreateArea(areas[a]);
1492 end;
1494 // Çàãðóçêà ìîíñòðîâ:
1495 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1496 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1497 monsters := MapReader.GetMonsters();
1499 gTotalMonsters := 0;
1501 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1502 if (monsters <> nil) and not gLoadGameMode then
1503 begin
1504 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1505 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1506 for a := 0 to High(monsters) do
1507 CreateMonster(monsters[a]);
1508 end;
1510 // Çàãðóçêà îïèñàíèÿ êàðòû:
1511 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1512 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1513 Header := MapReader.GetMapHeader();
1515 MapReader.Free();
1517 with gMapInfo do
1518 begin
1519 Name := Header.MapName;
1520 Description := Header.MapDescription;
1521 Author := Header.MapAuthor;
1522 MusicName := Header.MusicName;
1523 SkyName := Header.SkyName;
1524 Height := Header.Height;
1525 Width := Header.Width;
1526 end;
1528 // Çàãðóçêà íåáà:
1529 if gMapInfo.SkyName <> '' then
1530 begin
1531 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1532 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1533 FileName := g_ExtractWadName(gMapInfo.SkyName);
1535 if FileName <> '' then
1536 FileName := GameDir+'/wads/'+FileName
1537 else
1538 begin
1539 FileName := g_ExtractWadName(Res);
1540 end;
1542 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1543 if g_Texture_CreateWAD(BackID, s) then
1544 begin
1545 g_Game_SetupScreenSize();
1546 end
1547 else
1548 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1549 end;
1551 // Çàãðóçêà ìóçûêè:
1552 ok := False;
1553 if gMapInfo.MusicName <> '' then
1554 begin
1555 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1556 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1557 FileName := g_ExtractWadName(gMapInfo.MusicName);
1559 if FileName <> '' then
1560 FileName := GameDir+'/wads/'+FileName
1561 else
1562 begin
1563 FileName := g_ExtractWadName(Res);
1564 end;
1566 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1567 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1568 ok := True
1569 else
1570 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1571 end;
1573 // Îñòàëüíûå óñòàíâêè:
1574 CreateDoorMap();
1575 CreateLiftMap();
1577 g_Items_Init();
1578 g_Weapon_Init();
1579 g_Monsters_Init();
1581 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1582 if not gLoadGameMode then
1583 g_GFX_Init();
1585 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1586 _textures := nil;
1587 panels := nil;
1588 items := nil;
1589 areas := nil;
1590 triggers := nil;
1591 TriggersTable := nil;
1592 AddTextures := nil;
1594 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1595 if ok and (not gLoadGameMode) then
1596 begin
1597 gMusic.SetByName(gMapInfo.MusicName);
1598 gMusic.Play();
1599 end
1600 else
1601 gMusic.SetByName('');
1602 finally
1603 sfsGCEnable(); // enable releasing unused volumes
1604 end;
1606 e_WriteLog('Creating map grid', MSG_NOTIFY);
1607 mapCreateGrid();
1609 e_WriteLog('Done loading map.', MSG_NOTIFY);
1610 Result := True;
1611 end;
1613 function g_Map_GetMapInfo(Res: String): TMapInfo;
1614 var
1615 WAD: TWADFile;
1616 MapReader: TMapReader_1;
1617 Header: TMapHeaderRec_1;
1618 FileName: String;
1619 Data: Pointer;
1620 Len: Integer;
1621 begin
1622 FillChar(Result, SizeOf(Result), 0);
1623 FileName := g_ExtractWadName(Res);
1625 WAD := TWADFile.Create();
1626 if not WAD.ReadFile(FileName) then
1627 begin
1628 WAD.Free();
1629 Exit;
1630 end;
1632 //k8: it ignores path again
1633 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1634 begin
1635 WAD.Free();
1636 Exit;
1637 end;
1639 WAD.Free();
1641 MapReader := TMapReader_1.Create();
1643 if not MapReader.LoadMap(Data) then
1644 begin
1645 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1646 ZeroMemory(@Header, SizeOf(Header));
1647 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1648 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1649 end
1650 else
1651 begin
1652 Header := MapReader.GetMapHeader();
1653 Result.Name := Header.MapName;
1654 Result.Description := Header.MapDescription;
1655 end;
1657 FreeMem(Data);
1658 MapReader.Free();
1660 Result.Map := Res;
1661 Result.Author := Header.MapAuthor;
1662 Result.Height := Header.Height;
1663 Result.Width := Header.Width;
1664 end;
1666 function g_Map_GetMapsList(WADName: string): SArray;
1667 var
1668 WAD: TWADFile;
1669 a: Integer;
1670 ResList: SArray;
1671 begin
1672 Result := nil;
1673 WAD := TWADFile.Create();
1674 if not WAD.ReadFile(WADName) then
1675 begin
1676 WAD.Free();
1677 Exit;
1678 end;
1679 ResList := WAD.GetMapResources();
1680 if ResList <> nil then
1681 begin
1682 for a := 0 to High(ResList) do
1683 begin
1684 SetLength(Result, Length(Result)+1);
1685 Result[High(Result)] := ResList[a];
1686 end;
1687 end;
1688 WAD.Free();
1689 end;
1691 function g_Map_Exist(Res: string): Boolean;
1692 var
1693 WAD: TWADFile;
1694 FileName, mnn: string;
1695 ResList: SArray;
1696 a: Integer;
1697 begin
1698 Result := False;
1700 FileName := addWadExtension(g_ExtractWadName(Res));
1702 WAD := TWADFile.Create;
1703 if not WAD.ReadFile(FileName) then
1704 begin
1705 WAD.Free();
1706 Exit;
1707 end;
1709 ResList := WAD.GetMapResources();
1710 WAD.Free();
1712 mnn := g_ExtractFileName(Res);
1713 if ResList <> nil then
1714 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1715 begin
1716 Result := True;
1717 Exit;
1718 end;
1719 end;
1721 procedure g_Map_Free();
1722 var
1723 a: Integer;
1725 procedure FreePanelArray(var panels: TPanelArray);
1726 var
1727 i: Integer;
1729 begin
1730 if panels <> nil then
1731 begin
1732 for i := 0 to High(panels) do
1733 panels[i].Free();
1734 panels := nil;
1735 end;
1736 end;
1738 begin
1739 g_GFX_Free();
1740 g_Weapon_Free();
1741 g_Items_Free();
1742 g_Triggers_Free();
1743 g_Monsters_Free();
1745 RespawnPoints := nil;
1746 if FlagPoints[FLAG_RED] <> nil then
1747 begin
1748 Dispose(FlagPoints[FLAG_RED]);
1749 FlagPoints[FLAG_RED] := nil;
1750 end;
1751 if FlagPoints[FLAG_BLUE] <> nil then
1752 begin
1753 Dispose(FlagPoints[FLAG_BLUE]);
1754 FlagPoints[FLAG_BLUE] := nil;
1755 end;
1756 //DOMFlagPoints := nil;
1758 //gDOMFlags := nil;
1760 if Textures <> nil then
1761 begin
1762 for a := 0 to High(Textures) do
1763 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1764 if Textures[a].Anim then
1765 g_Frames_DeleteByID(Textures[a].FramesID)
1766 else
1767 if Textures[a].TextureID <> TEXTURE_NONE then
1768 e_DeleteTexture(Textures[a].TextureID);
1770 Textures := nil;
1771 end;
1773 FreePanelArray(gWalls);
1774 FreePanelArray(gRenderBackgrounds);
1775 FreePanelArray(gRenderForegrounds);
1776 FreePanelArray(gWater);
1777 FreePanelArray(gAcid1);
1778 FreePanelArray(gAcid2);
1779 FreePanelArray(gSteps);
1780 FreePanelArray(gLifts);
1781 FreePanelArray(gBlockMon);
1783 if BackID <> DWORD(-1) then
1784 begin
1785 gBackSize.X := 0;
1786 gBackSize.Y := 0;
1787 e_DeleteTexture(BackID);
1788 BackID := DWORD(-1);
1789 end;
1791 g_Game_StopAllSounds(False);
1792 gMusic.FreeSound();
1793 g_Sound_Delete(gMapInfo.MusicName);
1795 gMapInfo.Name := '';
1796 gMapInfo.Description := '';
1797 gMapInfo.MusicName := '';
1798 gMapInfo.Height := 0;
1799 gMapInfo.Width := 0;
1801 gDoorMap := nil;
1802 gLiftMap := nil;
1804 PanelByID := nil;
1805 end;
1807 procedure g_Map_Update();
1808 var
1809 a, d, j: Integer;
1810 m: Word;
1811 s: String;
1813 procedure UpdatePanelArray(var panels: TPanelArray);
1814 var
1815 i: Integer;
1817 begin
1818 if panels <> nil then
1819 for i := 0 to High(panels) do
1820 panels[i].Update();
1821 end;
1823 begin
1824 UpdatePanelArray(gWalls);
1825 UpdatePanelArray(gRenderBackgrounds);
1826 UpdatePanelArray(gRenderForegrounds);
1827 UpdatePanelArray(gWater);
1828 UpdatePanelArray(gAcid1);
1829 UpdatePanelArray(gAcid2);
1830 UpdatePanelArray(gSteps);
1832 if gGameSettings.GameMode = GM_CTF then
1833 begin
1834 for a := FLAG_RED to FLAG_BLUE do
1835 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1836 with gFlags[a] do
1837 begin
1838 if gFlags[a].Animation <> nil then
1839 gFlags[a].Animation.Update();
1841 m := g_Obj_Move(@Obj, True, True);
1843 if gTime mod (GAME_TICK*2) <> 0 then
1844 Continue;
1846 // Ñîïðîòèâëåíèå âîçäóõà:
1847 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1849 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1850 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1851 begin
1852 g_Map_ResetFlag(a);
1853 gFlags[a].CaptureTime := 0;
1854 if a = FLAG_RED then
1855 s := _lc[I_PLAYER_FLAG_RED]
1856 else
1857 s := _lc[I_PLAYER_FLAG_BLUE];
1858 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1860 if g_Game_IsNet then
1861 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1862 Continue;
1863 end;
1865 if Count > 0 then
1866 Count := Count - 1;
1868 // Èãðîê áåðåò ôëàã:
1869 if gPlayers <> nil then
1870 begin
1871 j := Random(Length(gPlayers)) - 1;
1873 for d := 0 to High(gPlayers) do
1874 begin
1875 Inc(j);
1876 if j > High(gPlayers) then
1877 j := 0;
1879 if gPlayers[j] <> nil then
1880 if gPlayers[j].Live and
1881 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1882 begin
1883 if gPlayers[j].GetFlag(a) then
1884 Break;
1885 end;
1886 end;
1887 end;
1888 end;
1889 end;
1890 end;
1893 // old algo
1894 procedure g_Map_DrawPanels (PanelType: Word);
1896 procedure DrawPanels (var panels: TPanelArray; drawDoors: Boolean=False);
1897 var
1898 idx: Integer;
1899 begin
1900 if (panels <> nil) then
1901 begin
1902 // alas, no visible set
1903 for idx := 0 to High(panels) do
1904 begin
1905 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1906 end;
1907 end;
1908 end;
1910 begin
1911 case PanelType of
1912 PANEL_WALL: DrawPanels(gWalls);
1913 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
1914 PANEL_BACK: DrawPanels(gRenderBackgrounds);
1915 PANEL_FORE: DrawPanels(gRenderForegrounds);
1916 PANEL_WATER: DrawPanels(gWater);
1917 PANEL_ACID1: DrawPanels(gAcid1);
1918 PANEL_ACID2: DrawPanels(gAcid2);
1919 PANEL_STEP: DrawPanels(gSteps);
1920 end;
1921 end;
1924 // new algo
1925 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
1926 function checker (obj: TObject; tag: Integer): Boolean;
1927 var
1928 pan: TPanel;
1929 begin
1930 result := false; // don't stop, ever
1931 //pan := (obj as TPanel);
1932 pan := TPanel(obj);
1933 //if (PanelType = PANEL_CLOSEDOOR) then begin if not pan.Door then exit; end else begin if pan.Door then exit; end;
1934 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
1935 //dplAddPanel(pan);
1936 gDrawPanelList.insert(pan);
1937 end;
1939 begin
1940 dplClear();
1941 //tagmask := panelTypeToTag(PanelType);
1943 if gdbg_map_use_tree_draw then
1944 begin
1945 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
1946 end
1947 else
1948 begin
1949 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
1950 end;
1951 // list will be rendered in `g_game.DrawPlayer()`
1952 (*
1953 while (gDrawPanelList.count > 0) do
1954 begin
1955 //(gDrawPanelList.front() as TPanel).Draw();
1956 TPanel(gDrawPanelList.front()).Draw();
1957 gDrawPanelList.popFront();
1958 end;
1959 *)
1960 end;
1963 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
1964 function checker (obj: TObject; tag: Integer): Boolean;
1965 var
1966 pan: TPanel;
1967 begin
1968 result := false; // don't stop, ever
1969 //if (tag <> GridTagWall) and (tag <> GridTagDoor) then exit; // only walls
1970 //pan := (obj as TPanel);
1971 pan := TPanel(obj);
1972 pan.DrawShadowVolume(lightX, lightY, radius);
1973 end;
1975 begin
1976 if gdbg_map_use_tree_draw then
1977 begin
1978 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
1979 end
1980 else
1981 begin
1982 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
1983 end;
1984 end;
1987 procedure g_Map_DrawBack(dx, dy: Integer);
1988 begin
1989 if gDrawBackGround and (BackID <> DWORD(-1)) then
1990 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
1991 else
1992 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
1993 end;
1995 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
1996 PanelType: Word; b1x3: Boolean): Boolean;
1997 var
1998 a, h: Integer;
1999 begin
2000 Result := False;
2002 if WordBool(PanelType and PANEL_WALL) then
2003 if gWalls <> nil then
2004 begin
2005 h := High(gWalls);
2007 for a := 0 to h do
2008 if gWalls[a].Enabled and
2009 g_Collide(X, Y, Width, Height,
2010 gWalls[a].X, gWalls[a].Y,
2011 gWalls[a].Width, gWalls[a].Height) then
2012 begin
2013 Result := True;
2014 Exit;
2015 end;
2016 end;
2018 if WordBool(PanelType and PANEL_WATER) then
2019 if gWater <> nil then
2020 begin
2021 h := High(gWater);
2023 for a := 0 to h do
2024 if g_Collide(X, Y, Width, Height,
2025 gWater[a].X, gWater[a].Y,
2026 gWater[a].Width, gWater[a].Height) then
2027 begin
2028 Result := True;
2029 Exit;
2030 end;
2031 end;
2033 if WordBool(PanelType and PANEL_ACID1) then
2034 if gAcid1 <> nil then
2035 begin
2036 h := High(gAcid1);
2038 for a := 0 to h do
2039 if g_Collide(X, Y, Width, Height,
2040 gAcid1[a].X, gAcid1[a].Y,
2041 gAcid1[a].Width, gAcid1[a].Height) then
2042 begin
2043 Result := True;
2044 Exit;
2045 end;
2046 end;
2048 if WordBool(PanelType and PANEL_ACID2) then
2049 if gAcid2 <> nil then
2050 begin
2051 h := High(gAcid2);
2053 for a := 0 to h do
2054 if g_Collide(X, Y, Width, Height,
2055 gAcid2[a].X, gAcid2[a].Y,
2056 gAcid2[a].Width, gAcid2[a].Height) then
2057 begin
2058 Result := True;
2059 Exit;
2060 end;
2061 end;
2063 if WordBool(PanelType and PANEL_STEP) then
2064 if gSteps <> nil then
2065 begin
2066 h := High(gSteps);
2068 for a := 0 to h do
2069 if g_Collide(X, Y, Width, Height,
2070 gSteps[a].X, gSteps[a].Y,
2071 gSteps[a].Width, gSteps[a].Height) then
2072 begin
2073 Result := True;
2074 Exit;
2075 end;
2076 end;
2078 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2079 if gLifts <> nil then
2080 begin
2081 h := High(gLifts);
2083 for a := 0 to h do
2084 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2085 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2086 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2087 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2088 g_Collide(X, Y, Width, Height,
2089 gLifts[a].X, gLifts[a].Y,
2090 gLifts[a].Width, gLifts[a].Height) then
2091 begin
2092 Result := True;
2093 Exit;
2094 end;
2095 end;
2097 if WordBool(PanelType and PANEL_BLOCKMON) then
2098 if gBlockMon <> nil then
2099 begin
2100 h := High(gBlockMon);
2102 for a := 0 to h do
2103 if ( (not b1x3) or
2104 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2105 g_Collide(X, Y, Width, Height,
2106 gBlockMon[a].X, gBlockMon[a].Y,
2107 gBlockMon[a].Width, gBlockMon[a].Height) then
2108 begin
2109 Result := True;
2110 Exit;
2111 end;
2112 end;
2113 end;
2115 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2116 var
2117 texid: DWORD;
2119 function checkPanels (var panels: TPanelArray): Boolean;
2120 var
2121 a: Integer;
2122 begin
2123 result := false;
2124 if panels = nil then exit;
2125 for a := 0 to High(panels) do
2126 begin
2127 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2128 begin
2129 result := true;
2130 texid := panels[a].GetTextureID();
2131 exit;
2132 end;
2133 end;
2134 end;
2136 begin
2137 texid := TEXTURE_NONE;
2138 result := texid;
2139 if not checkPanels(gWater) then
2140 if not checkPanels(gAcid1) then
2141 if not checkPanels(gAcid2) then exit;
2142 result := texid;
2143 end;
2146 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2147 function checker (obj: TObject; tag: Integer): Boolean;
2148 var
2149 pan: TPanel;
2150 begin
2151 result := false; // don't stop, ever
2152 pan := TPanel(obj);
2154 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2155 begin
2156 if not pan.Enabled then exit;
2157 end;
2159 if ((tag and GridTagLift) <> 0) then
2160 begin
2161 result :=
2162 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2163 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2164 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2165 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) and
2166 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2167 exit;
2168 end;
2170 if ((tag and GridTagBlockMon) <> 0) then
2171 begin
2172 result := ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2173 exit;
2174 end;
2176 // other shit
2177 result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2178 end;
2180 var
2181 tagmask: Integer = 0;
2182 begin
2183 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2184 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2185 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2186 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2187 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2188 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2189 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2191 if (tagmask = 0) then begin result := false; exit; end; // just in case
2193 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2194 if gdbg_map_use_accel_coldet then
2195 begin
2196 if gdbg_map_use_tree_coldet then
2197 begin
2198 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2199 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2200 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2201 begin
2202 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2203 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2204 end;
2205 end
2206 else
2207 begin
2208 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2209 end;
2210 end
2211 else
2212 begin
2213 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2214 end;
2215 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2216 end;
2219 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2220 var
2221 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2222 texid: DWORD;
2224 // slightly different from the old code, but meh...
2225 function checker (obj: TObject; tag: Integer): Boolean;
2226 var
2227 pan: TPanel;
2228 begin
2229 result := false; // don't stop, ever
2230 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2231 // check priorities
2232 case cctype of
2233 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2234 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2235 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2236 end;
2237 // collision?
2238 pan := (obj as TPanel);
2239 if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2240 // yeah
2241 texid := TPanel(obj).GetTextureID();
2242 // water? water has the highest priority, so stop right here
2243 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2244 // acid2?
2245 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2246 // acid1?
2247 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2248 end;
2250 begin
2251 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2252 if gdbg_map_use_accel_coldet then
2253 begin
2254 texid := TEXTURE_NONE;
2255 if gdbg_map_use_tree_coldet then
2256 begin
2257 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2258 end
2259 else
2260 begin
2261 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2262 end;
2263 result := texid;
2264 end
2265 else
2266 begin
2267 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2268 end;
2269 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2270 end;
2272 procedure g_Map_EnableWall(ID: DWORD);
2273 begin
2274 with gWalls[ID] do
2275 begin
2276 Enabled := True;
2277 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2279 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2280 end;
2281 end;
2283 procedure g_Map_DisableWall(ID: DWORD);
2284 begin
2285 with gWalls[ID] do
2286 begin
2287 Enabled := False;
2288 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2290 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2291 end;
2292 end;
2294 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2295 var
2296 tp: TPanel;
2297 begin
2298 case PanelType of
2299 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2300 tp := gWalls[ID];
2301 PANEL_FORE:
2302 tp := gRenderForegrounds[ID];
2303 PANEL_BACK:
2304 tp := gRenderBackgrounds[ID];
2305 PANEL_WATER:
2306 tp := gWater[ID];
2307 PANEL_ACID1:
2308 tp := gAcid1[ID];
2309 PANEL_ACID2:
2310 tp := gAcid2[ID];
2311 PANEL_STEP:
2312 tp := gSteps[ID];
2313 else
2314 Exit;
2315 end;
2317 tp.NextTexture(AnimLoop);
2318 if g_Game_IsServer and g_Game_IsNet then
2319 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2320 end;
2322 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2323 begin
2324 if gLifts[ID].LiftType = t then
2325 Exit;
2327 with gLifts[ID] do
2328 begin
2329 LiftType := t;
2331 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2333 if LiftType = 0 then
2334 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2335 else if LiftType = 1 then
2336 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2337 else if LiftType = 2 then
2338 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2339 else if LiftType = 3 then
2340 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2342 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2343 end;
2344 end;
2346 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2347 var
2348 a: Integer;
2349 PointsArray: Array of TRespawnPoint;
2350 begin
2351 Result := False;
2352 SetLength(PointsArray, 0);
2354 if RespawnPoints = nil then
2355 Exit;
2357 for a := 0 to High(RespawnPoints) do
2358 if RespawnPoints[a].PointType = PointType then
2359 begin
2360 SetLength(PointsArray, Length(PointsArray)+1);
2361 PointsArray[High(PointsArray)] := RespawnPoints[a];
2362 end;
2364 if PointsArray = nil then
2365 Exit;
2367 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2368 Result := True;
2369 end;
2371 function g_Map_GetPointCount(PointType: Byte): Word;
2372 var
2373 a: Integer;
2374 begin
2375 Result := 0;
2377 if RespawnPoints = nil then
2378 Exit;
2380 for a := 0 to High(RespawnPoints) do
2381 if RespawnPoints[a].PointType = PointType then
2382 Result := Result + 1;
2383 end;
2385 function g_Map_HaveFlagPoints(): Boolean;
2386 begin
2387 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2388 end;
2390 procedure g_Map_ResetFlag(Flag: Byte);
2391 begin
2392 with gFlags[Flag] do
2393 begin
2394 Obj.X := -1000;
2395 Obj.Y := -1000;
2396 Obj.Vel.X := 0;
2397 Obj.Vel.Y := 0;
2398 Direction := D_LEFT;
2399 State := FLAG_STATE_NONE;
2400 if FlagPoints[Flag] <> nil then
2401 begin
2402 Obj.X := FlagPoints[Flag]^.X;
2403 Obj.Y := FlagPoints[Flag]^.Y;
2404 Direction := FlagPoints[Flag]^.Direction;
2405 State := FLAG_STATE_NORMAL;
2406 end;
2407 Count := -1;
2408 end;
2409 end;
2411 procedure g_Map_DrawFlags();
2412 var
2413 i, dx: Integer;
2414 Mirror: TMirrorType;
2415 begin
2416 if gGameSettings.GameMode <> GM_CTF then
2417 Exit;
2419 for i := FLAG_RED to FLAG_BLUE do
2420 with gFlags[i] do
2421 if State <> FLAG_STATE_CAPTURED then
2422 begin
2423 if State = FLAG_STATE_NONE then
2424 continue;
2426 if Direction = D_LEFT then
2427 begin
2428 Mirror := M_HORIZONTAL;
2429 dx := -1;
2430 end
2431 else
2432 begin
2433 Mirror := M_NONE;
2434 dx := 1;
2435 end;
2437 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2439 if g_debug_Frames then
2440 begin
2441 e_DrawQuad(Obj.X+Obj.Rect.X,
2442 Obj.Y+Obj.Rect.Y,
2443 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2444 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2445 0, 255, 0);
2446 end;
2447 end;
2448 end;
2450 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2451 var
2452 dw: DWORD;
2453 b: Byte;
2454 str: String;
2455 boo: Boolean;
2457 procedure SavePanelArray(var panels: TPanelArray);
2458 var
2459 PAMem: TBinMemoryWriter;
2460 i: Integer;
2461 begin
2462 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2463 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2465 i := 0;
2466 while i < Length(panels) do
2467 begin
2468 if panels[i].SaveIt then
2469 begin
2470 // ID ïàíåëè:
2471 PAMem.WriteInt(i);
2472 // Ñîõðàíÿåì ïàíåëü:
2473 panels[i].SaveState(PAMem);
2474 end;
2475 Inc(i);
2476 end;
2478 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2479 PAMem.SaveToMemory(Mem);
2480 PAMem.Free();
2481 end;
2483 procedure SaveFlag(flag: PFlag);
2484 begin
2485 // Ñèãíàòóðà ôëàãà:
2486 dw := FLAG_SIGNATURE; // 'FLAG'
2487 Mem.WriteDWORD(dw);
2488 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2489 Mem.WriteByte(flag^.RespawnType);
2490 // Ñîñòîÿíèå ôëàãà:
2491 Mem.WriteByte(flag^.State);
2492 // Íàïðàâëåíèå ôëàãà:
2493 if flag^.Direction = D_LEFT then
2494 b := 1
2495 else // D_RIGHT
2496 b := 2;
2497 Mem.WriteByte(b);
2498 // Îáúåêò ôëàãà:
2499 Obj_SaveState(@flag^.Obj, Mem);
2500 end;
2502 begin
2503 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2505 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2506 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2507 SavePanelArray(gWalls);
2508 // Ñîõðàíÿåì ïàíåëè ôîíà:
2509 SavePanelArray(gRenderBackgrounds);
2510 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2511 SavePanelArray(gRenderForegrounds);
2512 // Ñîõðàíÿåì ïàíåëè âîäû:
2513 SavePanelArray(gWater);
2514 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2515 SavePanelArray(gAcid1);
2516 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2517 SavePanelArray(gAcid2);
2518 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2519 SavePanelArray(gSteps);
2520 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2521 SavePanelArray(gLifts);
2522 ///// /////
2524 ///// Ñîõðàíÿåì ìóçûêó: /////
2525 // Ñèãíàòóðà ìóçûêè:
2526 dw := MUSIC_SIGNATURE; // 'MUSI'
2527 Mem.WriteDWORD(dw);
2528 // Íàçâàíèå ìóçûêè:
2529 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2530 if gMusic.NoMusic then
2531 str := ''
2532 else
2533 str := gMusic.Name;
2534 Mem.WriteString(str, 64);
2535 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2536 dw := gMusic.GetPosition();
2537 Mem.WriteDWORD(dw);
2538 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2539 boo := gMusic.SpecPause;
2540 Mem.WriteBoolean(boo);
2541 ///// /////
2543 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2544 Mem.WriteInt(gTotalMonsters);
2545 ///// /////
2547 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2548 if gGameSettings.GameMode = GM_CTF then
2549 begin
2550 // Ôëàã Êðàñíîé êîìàíäû:
2551 SaveFlag(@gFlags[FLAG_RED]);
2552 // Ôëàã Ñèíåé êîìàíäû:
2553 SaveFlag(@gFlags[FLAG_BLUE]);
2554 end;
2555 ///// /////
2557 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2558 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2559 begin
2560 // Î÷êè Êðàñíîé êîìàíäû:
2561 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2562 // Î÷êè Ñèíåé êîìàíäû:
2563 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2564 end;
2565 ///// /////
2566 end;
2568 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2569 var
2570 dw: DWORD;
2571 b: Byte;
2572 str: String;
2573 boo: Boolean;
2575 procedure LoadPanelArray(var panels: TPanelArray);
2576 var
2577 PAMem: TBinMemoryReader;
2578 i, id: Integer;
2579 begin
2580 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2581 PAMem := TBinMemoryReader.Create();
2582 PAMem.LoadFromMemory(Mem);
2584 for i := 0 to Length(panels)-1 do
2585 if panels[i].SaveIt then
2586 begin
2587 // ID ïàíåëè:
2588 PAMem.ReadInt(id);
2589 if id <> i then
2590 begin
2591 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2592 end;
2593 // Çàãðóæàåì ïàíåëü:
2594 panels[i].LoadState(PAMem);
2595 end;
2597 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2598 PAMem.Free();
2599 end;
2601 procedure LoadFlag(flag: PFlag);
2602 begin
2603 // Ñèãíàòóðà ôëàãà:
2604 Mem.ReadDWORD(dw);
2605 if dw <> FLAG_SIGNATURE then // 'FLAG'
2606 begin
2607 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2608 end;
2609 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2610 Mem.ReadByte(flag^.RespawnType);
2611 // Ñîñòîÿíèå ôëàãà:
2612 Mem.ReadByte(flag^.State);
2613 // Íàïðàâëåíèå ôëàãà:
2614 Mem.ReadByte(b);
2615 if b = 1 then
2616 flag^.Direction := D_LEFT
2617 else // b = 2
2618 flag^.Direction := D_RIGHT;
2619 // Îáúåêò ôëàãà:
2620 Obj_LoadState(@flag^.Obj, Mem);
2621 end;
2623 begin
2624 if Mem = nil then
2625 Exit;
2627 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2628 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2629 LoadPanelArray(gWalls);
2630 // Çàãðóæàåì ïàíåëè ôîíà:
2631 LoadPanelArray(gRenderBackgrounds);
2632 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2633 LoadPanelArray(gRenderForegrounds);
2634 // Çàãðóæàåì ïàíåëè âîäû:
2635 LoadPanelArray(gWater);
2636 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2637 LoadPanelArray(gAcid1);
2638 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2639 LoadPanelArray(gAcid2);
2640 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2641 LoadPanelArray(gSteps);
2642 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2643 LoadPanelArray(gLifts);
2644 ///// /////
2646 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2647 g_GFX_Init();
2648 mapCreateGrid();
2650 ///// Çàãðóæàåì ìóçûêó: /////
2651 // Ñèãíàòóðà ìóçûêè:
2652 Mem.ReadDWORD(dw);
2653 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2654 begin
2655 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2656 end;
2657 // Íàçâàíèå ìóçûêè:
2658 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2659 Mem.ReadString(str);
2660 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2661 Mem.ReadDWORD(dw);
2662 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2663 Mem.ReadBoolean(boo);
2664 // Çàïóñêàåì ýòó ìóçûêó:
2665 gMusic.SetByName(str);
2666 gMusic.SpecPause := boo;
2667 gMusic.Play();
2668 gMusic.Pause(True);
2669 gMusic.SetPosition(dw);
2670 ///// /////
2672 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2673 Mem.ReadInt(gTotalMonsters);
2674 ///// /////
2676 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2677 if gGameSettings.GameMode = GM_CTF then
2678 begin
2679 // Ôëàã Êðàñíîé êîìàíäû:
2680 LoadFlag(@gFlags[FLAG_RED]);
2681 // Ôëàã Ñèíåé êîìàíäû:
2682 LoadFlag(@gFlags[FLAG_BLUE]);
2683 end;
2684 ///// /////
2686 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2687 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2688 begin
2689 // Î÷êè Êðàñíîé êîìàíäû:
2690 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2691 // Î÷êè Ñèíåé êîìàíäû:
2692 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2693 end;
2694 ///// /////
2695 end;
2697 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2698 var
2699 Arr: TPanelArray;
2700 begin
2701 Result := nil;
2702 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2703 Arr := PanelByID[PanelID].PWhere^;
2704 PanelArrayID := PanelByID[PanelID].PArrID;
2705 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2706 end;
2708 end.