DEADSOFTWARE

removed "monidx" argument from monster spatial query callback (each monster has UIDs...
[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 type
193 TPanelGrid = specialize TBodyGridBase<TPanel>;
195 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
197 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
198 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
199 end;
201 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
202 begin
203 result := false;
204 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
205 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
206 if (pan.Width < 1) or (pan.Height < 1) then exit;
207 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
208 result := true;
209 end;
212 function panelTypeToTag (panelType: Word): Integer;
213 begin
214 case panelType of
215 PANEL_WALL: result := GridTagWall; // gWalls
216 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
217 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
218 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
219 PANEL_WATER: result := GridTagWater; // gWater
220 PANEL_ACID1: result := GridTagAcid1; // gAcid1
221 PANEL_ACID2: result := GridTagAcid2; // gAcid2
222 PANEL_STEP: result := GridTagStep; // gSteps
223 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
224 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
225 else result := GridTagInvalid;
226 end;
227 end;
230 function dplLess (a, b: TObject): Boolean;
231 var
232 pa, pb: TPanel;
233 begin
234 //result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
235 pa := TPanel(a);
236 pb := TPanel(b);
237 if (pa.tag < pb.tag) then begin result := true; exit; end;
238 if (pa.tag > pb.tag) then begin result := false; exit; end;
239 result := (pa.ArrIdx < pb.ArrIdx);
240 end;
242 procedure dplClear ();
243 begin
244 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
245 end;
248 type
249 TPanelID = record
250 PWhere: ^TPanelArray;
251 PArrID: Integer;
252 end;
254 var
255 PanelById: array of TPanelID;
256 Textures: TLevelTextureArray;
257 RespawnPoints: Array of TRespawnPoint;
258 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
259 //DOMFlagPoints: Array of TFlagPoint;
260 gMapGrid: TPanelGrid = nil;
261 mapTree: TDynAABBTreeMap = nil;
264 procedure g_Map_ProfilersBegin ();
265 begin
266 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
267 profMapCollision.mainBegin(g_profile_collision);
268 // create sections
269 if g_profile_collision then
270 begin
271 profMapCollision.sectionBegin('*solids');
272 profMapCollision.sectionEnd();
273 profMapCollision.sectionBegin('liquids');
274 profMapCollision.sectionEnd();
275 end;
276 end;
278 procedure g_Map_ProfilersEnd ();
279 begin
280 if (profMapCollision <> nil) then profMapCollision.mainEnd();
281 end;
284 function g_Map_IsSpecialTexture(Texture: String): Boolean;
285 begin
286 Result := (Texture = TEXTURE_NAME_WATER) or
287 (Texture = TEXTURE_NAME_ACID1) or
288 (Texture = TEXTURE_NAME_ACID2);
289 end;
291 procedure CreateDoorMap();
292 var
293 PanelArray: Array of record
294 X, Y: Integer;
295 Width, Height: Word;
296 Active: Boolean;
297 PanelID: DWORD;
298 end;
299 a, b, c, m, i, len: Integer;
300 ok: Boolean;
301 begin
302 if gWalls = nil then
303 Exit;
305 i := 0;
306 len := 128;
307 SetLength(PanelArray, len);
309 for a := 0 to High(gWalls) do
310 if gWalls[a].Door then
311 begin
312 PanelArray[i].X := gWalls[a].X;
313 PanelArray[i].Y := gWalls[a].Y;
314 PanelArray[i].Width := gWalls[a].Width;
315 PanelArray[i].Height := gWalls[a].Height;
316 PanelArray[i].Active := True;
317 PanelArray[i].PanelID := a;
319 i := i + 1;
320 if i = len then
321 begin
322 len := len + 128;
323 SetLength(PanelArray, len);
324 end;
325 end;
327 // Íåò äâåðåé:
328 if i = 0 then
329 begin
330 PanelArray := nil;
331 Exit;
332 end;
334 SetLength(gDoorMap, 0);
336 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
338 for a := 0 to i-1 do
339 if PanelArray[a].Active then
340 begin
341 PanelArray[a].Active := False;
342 m := Length(gDoorMap);
343 SetLength(gDoorMap, m+1);
344 SetLength(gDoorMap[m], 1);
345 gDoorMap[m, 0] := PanelArray[a].PanelID;
346 ok := True;
348 while ok do
349 begin
350 ok := False;
352 for b := 0 to i-1 do
353 if PanelArray[b].Active then
354 for c := 0 to High(gDoorMap[m]) do
355 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
356 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
357 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
358 PanelArray[b].Width, PanelArray[b].Height,
359 gWalls[gDoorMap[m, c]].X,
360 gWalls[gDoorMap[m, c]].Y,
361 gWalls[gDoorMap[m, c]].Width,
362 gWalls[gDoorMap[m, c]].Height) then
363 begin
364 PanelArray[b].Active := False;
365 SetLength(gDoorMap[m],
366 Length(gDoorMap[m])+1);
367 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
368 ok := True;
369 Break;
370 end;
371 end;
373 g_Game_StepLoading();
374 end;
376 PanelArray := nil;
377 end;
379 procedure CreateLiftMap();
380 var
381 PanelArray: Array of record
382 X, Y: Integer;
383 Width, Height: Word;
384 Active: Boolean;
385 end;
386 a, b, c, len, i, j: Integer;
387 ok: Boolean;
388 begin
389 if gLifts = nil then
390 Exit;
392 len := Length(gLifts);
393 SetLength(PanelArray, len);
395 for a := 0 to len-1 do
396 begin
397 PanelArray[a].X := gLifts[a].X;
398 PanelArray[a].Y := gLifts[a].Y;
399 PanelArray[a].Width := gLifts[a].Width;
400 PanelArray[a].Height := gLifts[a].Height;
401 PanelArray[a].Active := True;
402 end;
404 SetLength(gLiftMap, len);
405 i := 0;
407 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
409 for a := 0 to len-1 do
410 if PanelArray[a].Active then
411 begin
412 PanelArray[a].Active := False;
413 SetLength(gLiftMap[i], 32);
414 j := 0;
415 gLiftMap[i, j] := a;
416 ok := True;
418 while ok do
419 begin
420 ok := False;
421 for b := 0 to len-1 do
422 if PanelArray[b].Active then
423 for c := 0 to j do
424 if g_CollideAround(PanelArray[b].X,
425 PanelArray[b].Y,
426 PanelArray[b].Width,
427 PanelArray[b].Height,
428 PanelArray[gLiftMap[i, c]].X,
429 PanelArray[gLiftMap[i, c]].Y,
430 PanelArray[gLiftMap[i, c]].Width,
431 PanelArray[gLiftMap[i, c]].Height) then
432 begin
433 PanelArray[b].Active := False;
434 j := j+1;
435 if j > High(gLiftMap[i]) then
436 SetLength(gLiftMap[i],
437 Length(gLiftMap[i])+32);
439 gLiftMap[i, j] := b;
440 ok := True;
442 Break;
443 end;
444 end;
446 SetLength(gLiftMap[i], j+1);
447 i := i+1;
449 g_Game_StepLoading();
450 end;
452 SetLength(gLiftMap, i);
454 PanelArray := nil;
455 end;
457 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
458 CurTex: Integer; sav: Boolean): Integer;
459 var
460 len: Integer;
461 panels: ^TPanelArray;
462 begin
463 Result := -1;
465 case PanelRec.PanelType of
466 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
467 panels := @gWalls;
468 PANEL_BACK:
469 panels := @gRenderBackgrounds;
470 PANEL_FORE:
471 panels := @gRenderForegrounds;
472 PANEL_WATER:
473 panels := @gWater;
474 PANEL_ACID1:
475 panels := @gAcid1;
476 PANEL_ACID2:
477 panels := @gAcid2;
478 PANEL_STEP:
479 panels := @gSteps;
480 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
481 panels := @gLifts;
482 PANEL_BLOCKMON:
483 panels := @gBlockMon;
484 else
485 Exit;
486 end;
488 len := Length(panels^);
489 SetLength(panels^, len + 1);
491 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
492 panels^[len].ArrIdx := len;
493 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
494 if sav then
495 panels^[len].SaveIt := True;
497 Result := len;
499 len := Length(PanelByID);
500 SetLength(PanelByID, len + 1);
501 PanelByID[len].PWhere := panels;
502 PanelByID[len].PArrID := Result;
503 end;
505 function CreateNullTexture(RecName: String): Integer;
506 begin
507 SetLength(Textures, Length(Textures)+1);
508 result := High(Textures);
510 with Textures[High(Textures)] do
511 begin
512 TextureName := RecName;
513 Width := 1;
514 Height := 1;
515 Anim := False;
516 TextureID := TEXTURE_NONE;
517 end;
518 end;
520 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
521 var
522 WAD: TWADFile;
523 TextureData: Pointer;
524 WADName, txname: String;
525 a, ResLength: Integer;
526 begin
527 Result := -1;
529 if Textures <> nil then
530 for a := 0 to High(Textures) do
531 if Textures[a].TextureName = RecName then
532 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
533 Result := a;
534 Exit;
535 end;
537 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
538 if (RecName = TEXTURE_NAME_WATER) or
539 (RecName = TEXTURE_NAME_ACID1) or
540 (RecName = TEXTURE_NAME_ACID2) then
541 begin
542 SetLength(Textures, Length(Textures)+1);
544 with Textures[High(Textures)] do
545 begin
546 TextureName := RecName;
548 if TextureName = TEXTURE_NAME_WATER then
549 TextureID := TEXTURE_SPECIAL_WATER
550 else
551 if TextureName = TEXTURE_NAME_ACID1 then
552 TextureID := TEXTURE_SPECIAL_ACID1
553 else
554 if TextureName = TEXTURE_NAME_ACID2 then
555 TextureID := TEXTURE_SPECIAL_ACID2;
557 Anim := False;
558 end;
560 result := High(Textures);
561 Exit;
562 end;
564 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
565 WADName := g_ExtractWadName(RecName);
567 WAD := TWADFile.Create();
569 if WADName <> '' then
570 WADName := GameDir+'/wads/'+WADName
571 else
572 WADName := Map;
574 WAD.ReadFile(WADName);
576 txname := RecName;
578 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
579 begin
580 FreeMem(TextureData);
581 RecName := 'COMMON\ALIEN';
582 end;
585 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
586 begin
587 SetLength(Textures, Length(Textures)+1);
588 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
589 Exit;
590 e_GetTextureSize(Textures[High(Textures)].TextureID,
591 @Textures[High(Textures)].Width,
592 @Textures[High(Textures)].Height);
593 FreeMem(TextureData);
594 Textures[High(Textures)].TextureName := {RecName}txname;
595 Textures[High(Textures)].Anim := False;
597 result := High(Textures);
598 end
599 else // Íåò òàêîãî ðåóñðñà â WAD'å
600 begin
601 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
602 if log then
603 begin
604 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
605 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
606 end;
607 end;
609 WAD.Free();
610 end;
612 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
613 var
614 WAD: TWADFile;
615 TextureWAD: PChar = nil;
616 TextData: Pointer = nil;
617 TextureData: Pointer = nil;
618 cfg: TConfig = nil;
619 WADName: String;
620 ResLength: Integer;
621 TextureResource: String;
622 _width, _height, _framecount, _speed: Integer;
623 _backanimation: Boolean;
624 //imgfmt: string;
625 ia: TDynImageDataArray = nil;
626 f, c, frdelay, frloop: Integer;
627 begin
628 result := -1;
630 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
632 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
633 WADName := g_ExtractWadName(RecName);
635 WAD := TWADFile.Create();
636 try
637 if WADName <> '' then
638 WADName := GameDir+'/wads/'+WADName
639 else
640 WADName := Map;
642 WAD.ReadFile(WADName);
644 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
645 begin
646 if log then
647 begin
648 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
649 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
650 end;
651 exit;
652 end;
654 {TEST
655 if WADName = Map then
656 begin
657 //FreeMem(TextureWAD);
658 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
659 end;
662 WAD.FreeWAD();
664 if ResLength < 6 then
665 begin
666 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
667 exit;
668 end;
670 // ýòî ïòèöà? ýòî ñàìîë¸ò?
671 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
672 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
673 begin
674 // íåò, ýòî ñóïåðìåí!
675 if not WAD.ReadMemory(TextureWAD, ResLength) then
676 begin
677 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
678 exit;
679 end;
681 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
682 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
683 begin
684 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
685 exit;
686 end;
688 cfg := TConfig.CreateMem(TextData, ResLength);
690 TextureResource := cfg.ReadStr('', 'resource', '');
691 if TextureResource = '' then
692 begin
693 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
694 exit;
695 end;
697 _width := cfg.ReadInt('', 'framewidth', 0);
698 _height := cfg.ReadInt('', 'frameheight', 0);
699 _framecount := cfg.ReadInt('', 'framecount', 0);
700 _speed := cfg.ReadInt('', 'waitcount', 0);
701 _backanimation := cfg.ReadBool('', 'backanimation', False);
703 cfg.Free();
704 cfg := nil;
706 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
707 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
708 begin
709 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
710 exit;
711 end;
713 WAD.Free();
714 WAD := nil;
716 SetLength(Textures, Length(Textures)+1);
717 with Textures[High(Textures)] do
718 begin
719 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
720 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
721 begin
722 TextureName := RecName;
723 Width := _width;
724 Height := _height;
725 Anim := True;
726 FramesCount := _framecount;
727 Speed := _speed;
728 result := High(Textures);
729 end
730 else
731 begin
732 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
733 end;
734 end;
735 end
736 else
737 begin
738 // try animated image
740 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
741 if length(imgfmt) = 0 then
742 begin
743 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
744 exit;
745 end;
747 GlobalMetadata.ClearMetaItems();
748 GlobalMetadata.ClearMetaItemsForSaving();
749 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
750 begin
751 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
752 exit;
753 end;
754 if length(ia) = 0 then
755 begin
756 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
757 exit;
758 end;
760 WAD.Free();
761 WAD := nil;
763 _width := ia[0].width;
764 _height := ia[0].height;
765 _framecount := length(ia);
766 _speed := 1;
767 _backanimation := false;
768 frdelay := -1;
769 frloop := -666;
770 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
771 begin
772 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
773 try
774 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
775 frdelay := f;
776 if f < 0 then f := 0;
777 // rounding ;-)
778 c := f mod 28;
779 if c < 13 then c := 0 else c := 1;
780 f := (f div 28)+c;
781 if f < 1 then f := 1 else if f > 255 then f := 255;
782 _speed := f;
783 except
784 end;
785 end;
786 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
787 begin
788 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
789 try
790 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
791 frloop := f;
792 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
793 except
794 end;
795 end;
796 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
797 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
798 f := ord(_backanimation);
799 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);
801 SetLength(Textures, Length(Textures)+1);
802 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
803 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
804 begin
805 Textures[High(Textures)].TextureName := RecName;
806 Textures[High(Textures)].Width := _width;
807 Textures[High(Textures)].Height := _height;
808 Textures[High(Textures)].Anim := True;
809 Textures[High(Textures)].FramesCount := length(ia);
810 Textures[High(Textures)].Speed := _speed;
811 result := High(Textures);
812 //writeln(' CREATED!');
813 end
814 else
815 begin
816 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
817 end;
818 end;
819 finally
820 for f := 0 to High(ia) do FreeImage(ia[f]);
821 WAD.Free();
822 cfg.Free();
823 if TextureWAD <> nil then FreeMem(TextureWAD);
824 if TextData <> nil then FreeMem(TextData);
825 if TextureData <> nil then FreeMem(TextureData);
826 end;
827 end;
829 procedure CreateItem(Item: TItemRec_1);
830 begin
831 if g_Game_IsClient then Exit;
833 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
834 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
835 Exit;
837 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
838 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
839 end;
841 procedure CreateArea(Area: TAreaRec_1);
842 var
843 a: Integer;
844 id: DWORD;
845 begin
846 case Area.AreaType of
847 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
848 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
849 begin
850 SetLength(RespawnPoints, Length(RespawnPoints)+1);
851 with RespawnPoints[High(RespawnPoints)] do
852 begin
853 X := Area.X;
854 Y := Area.Y;
855 Direction := TDirection(Area.Direction);
857 case Area.AreaType of
858 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
859 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
860 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
861 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
862 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
863 end;
864 end;
865 end;
867 AREA_REDFLAG, AREA_BLUEFLAG:
868 begin
869 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
871 if FlagPoints[a] <> nil then Exit;
873 New(FlagPoints[a]);
875 with FlagPoints[a]^ do
876 begin
877 X := Area.X-FLAGRECT.X;
878 Y := Area.Y-FLAGRECT.Y;
879 Direction := TDirection(Area.Direction);
880 end;
882 with gFlags[a] do
883 begin
884 case a of
885 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
886 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
887 end;
889 Animation := TAnimation.Create(id, True, 8);
890 Obj.Rect := FLAGRECT;
892 g_Map_ResetFlag(a);
893 end;
894 end;
896 AREA_DOMFLAG:
897 begin
898 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
899 with DOMFlagPoints[High(DOMFlagPoints)] do
900 begin
901 X := Area.X;
902 Y := Area.Y;
903 Direction := TDirection(Area.Direction);
904 end;
906 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
907 end;
908 end;
909 end;
911 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
912 var
913 _trigger: TTrigger;
914 begin
915 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
917 with _trigger do
918 begin
919 X := Trigger.X;
920 Y := Trigger.Y;
921 Width := Trigger.Width;
922 Height := Trigger.Height;
923 Enabled := ByteBool(Trigger.Enabled);
924 TexturePanel := Trigger.TexturePanel;
925 TexturePanelType := fTexturePanel1Type;
926 ShotPanelType := fTexturePanel2Type;
927 TriggerType := Trigger.TriggerType;
928 ActivateType := Trigger.ActivateType;
929 Keys := Trigger.Keys;
930 Data.Default := Trigger.DATA;
931 end;
933 g_Triggers_Create(_trigger);
934 end;
936 procedure CreateMonster(monster: TMonsterRec_1);
937 var
938 a: Integer;
939 mon: TMonster;
940 begin
941 if g_Game_IsClient then Exit;
943 if (gGameSettings.GameType = GT_SINGLE)
944 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
945 begin
946 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
948 if gTriggers <> nil then
949 begin
950 for a := 0 to High(gTriggers) do
951 begin
952 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
953 begin
954 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
955 end;
956 end;
957 end;
959 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
960 end;
961 end;
963 procedure g_Map_ReAdd_DieTriggers();
965 function monsDieTrig (mon: TMonster): Boolean;
966 var
967 a: Integer;
968 begin
969 result := false; // don't stop
970 mon.ClearTriggers();
971 for a := 0 to High(gTriggers) do
972 begin
973 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
974 begin
975 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
976 end;
977 end;
978 end;
980 begin
981 if g_Game_IsClient then Exit;
983 g_Mons_ForEach(monsDieTrig);
984 end;
986 function extractWadName(resourceName: string): string;
987 var
988 posN: Integer;
989 begin
990 posN := Pos(':', resourceName);
991 if posN > 0 then
992 Result:= Copy(resourceName, 0, posN-1)
993 else
994 Result := '';
995 end;
997 procedure addResToExternalResList(res: string);
998 begin
999 res := extractWadName(res);
1000 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1001 gExternalResources.Add(res);
1002 end;
1004 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1005 var
1006 textures: TTexturesRec1Array;
1007 mapHeader: TMapHeaderRec_1;
1008 i: integer;
1009 resFile: String = '';
1010 begin
1011 if gExternalResources = nil then
1012 gExternalResources := TStringList.Create;
1014 gExternalResources.Clear;
1015 textures := mapReader.GetTextures();
1016 for i := 0 to High(textures) do
1017 begin
1018 addResToExternalResList(resFile);
1019 end;
1021 textures := nil;
1023 mapHeader := mapReader.GetMapHeader;
1025 addResToExternalResList(mapHeader.MusicName);
1026 addResToExternalResList(mapHeader.SkyName);
1027 end;
1029 procedure mapCreateGrid ();
1030 var
1031 mapX0: Integer = $3fffffff;
1032 mapY0: Integer = $3fffffff;
1033 mapX1: Integer = -$3fffffff;
1034 mapY1: Integer = -$3fffffff;
1036 procedure calcBoundingBox (constref panels: TPanelArray);
1037 var
1038 idx: Integer;
1039 pan: TPanel;
1040 begin
1041 for idx := 0 to High(panels) do
1042 begin
1043 pan := panels[idx];
1044 if not pan.visvalid then continue;
1045 if (pan.Width < 1) or (pan.Height < 1) then continue;
1046 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1047 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1048 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1049 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1050 end;
1051 end;
1053 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1054 var
1055 idx: Integer;
1056 pan: TPanel;
1057 begin
1058 tag := panelTypeToTag(tag);
1059 for idx := High(panels) downto 0 do
1060 begin
1061 pan := panels[idx];
1062 pan.tag := tag;
1063 if not pan.visvalid then continue;
1064 gMapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1065 mapTree.insertObject(pan, tag, true); // as static object
1066 end;
1067 end;
1069 begin
1070 gMapGrid.Free();
1071 gMapGrid := nil;
1072 mapTree.Free();
1073 mapTree := nil;
1075 calcBoundingBox(gWalls);
1076 calcBoundingBox(gRenderBackgrounds);
1077 calcBoundingBox(gRenderForegrounds);
1078 calcBoundingBox(gWater);
1079 calcBoundingBox(gAcid1);
1080 calcBoundingBox(gAcid2);
1081 calcBoundingBox(gSteps);
1082 calcBoundingBox(gLifts);
1083 calcBoundingBox(gBlockMon);
1085 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1087 gMapGrid := TPanelGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1088 mapTree := TDynAABBTreeMap.Create();
1090 addPanelsToGrid(gWalls, PANEL_WALL);
1091 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1092 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1093 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1094 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1095 addPanelsToGrid(gWater, PANEL_WATER);
1096 addPanelsToGrid(gAcid1, PANEL_ACID1);
1097 addPanelsToGrid(gAcid2, PANEL_ACID2);
1098 addPanelsToGrid(gSteps, PANEL_STEP);
1099 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1100 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1102 gMapGrid.dumpStats();
1103 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1104 mapTree.forEachLeaf(nil);
1105 end;
1107 function g_Map_Load(Res: String): Boolean;
1108 const
1109 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1110 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1111 var
1112 WAD: TWADFile;
1113 MapReader: TMapReader_1;
1114 Header: TMapHeaderRec_1;
1115 _textures: TTexturesRec1Array;
1116 _texnummap: array of Integer; // `_textures` -> `Textures`
1117 panels: TPanelsRec1Array;
1118 items: TItemsRec1Array;
1119 monsters: TMonsterRec1Array;
1120 areas: TAreasRec1Array;
1121 triggers: TTriggersRec1Array;
1122 a, b, c, k: Integer;
1123 PanelID: DWORD;
1124 AddTextures: TAddTextureArray;
1125 texture: TTextureRec_1;
1126 TriggersTable: Array of record
1127 TexturePanel: Integer;
1128 LiftPanel: Integer;
1129 DoorPanel: Integer;
1130 ShotPanel: Integer;
1131 end;
1132 FileName, mapResName, s, TexName: String;
1133 Data: Pointer;
1134 Len: Integer;
1135 ok, isAnim, trigRef: Boolean;
1136 CurTex, ntn: Integer;
1138 begin
1139 gMapGrid.Free();
1140 gMapGrid := nil;
1141 mapTree.Free();
1142 mapTree := nil;
1144 Result := False;
1145 gMapInfo.Map := Res;
1146 TriggersTable := nil;
1147 FillChar(texture, SizeOf(texture), 0);
1149 sfsGCDisable(); // temporary disable removing of temporary volumes
1150 try
1151 // Çàãðóçêà WAD:
1152 FileName := g_ExtractWadName(Res);
1153 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1154 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1156 WAD := TWADFile.Create();
1157 if not WAD.ReadFile(FileName) then
1158 begin
1159 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1160 WAD.Free();
1161 Exit;
1162 end;
1163 //k8: why loader ignores path here?
1164 mapResName := g_ExtractFileName(Res);
1165 if not WAD.GetMapResource(mapResName, Data, Len) then
1166 begin
1167 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1168 WAD.Free();
1169 Exit;
1170 end;
1172 WAD.Free();
1174 // Çàãðóçêà êàðòû:
1175 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1176 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1177 MapReader := TMapReader_1.Create();
1179 if not MapReader.LoadMap(Data) then
1180 begin
1181 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1182 FreeMem(Data);
1183 MapReader.Free();
1184 Exit;
1185 end;
1187 FreeMem(Data);
1188 generateExternalResourcesList(MapReader);
1189 // Çàãðóçêà òåêñòóð:
1190 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1191 _textures := MapReader.GetTextures();
1192 _texnummap := nil;
1194 // Äîáàâëåíèå òåêñòóð â Textures[]:
1195 if _textures <> nil then
1196 begin
1197 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1198 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1199 SetLength(_texnummap, length(_textures));
1201 for a := 0 to High(_textures) do
1202 begin
1203 SetLength(s, 64);
1204 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1205 for b := 1 to Length(s) do
1206 if s[b] = #0 then
1207 begin
1208 SetLength(s, b-1);
1209 Break;
1210 end;
1211 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1212 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1213 // Àíèìèðîâàííàÿ òåêñòóðà:
1214 if ByteBool(_textures[a].Anim) then
1215 begin
1216 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1217 if ntn < 0 then
1218 begin
1219 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1220 ntn := CreateNullTexture(_textures[a].Resource);
1221 end;
1222 end
1223 else // Îáû÷íàÿ òåêñòóðà:
1224 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1225 if ntn < 0 then
1226 begin
1227 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1228 ntn := CreateNullTexture(_textures[a].Resource);
1229 end;
1231 _texnummap[a] := ntn; // fix texture number
1232 g_Game_StepLoading();
1233 end;
1234 end;
1236 // Çàãðóçêà òðèããåðîâ:
1237 gTriggerClientID := 0;
1238 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1239 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1240 triggers := MapReader.GetTriggers();
1242 // Çàãðóçêà ïàíåëåé:
1243 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1244 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1245 panels := MapReader.GetPanels();
1247 // check texture numbers for panels
1248 for a := 0 to High(panels) do
1249 begin
1250 if panels[a].TextureNum > High(_textures) then
1251 begin
1252 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1253 result := false;
1254 exit;
1255 end;
1256 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1257 end;
1259 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1260 if triggers <> nil then
1261 begin
1262 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1263 SetLength(TriggersTable, Length(triggers));
1264 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1266 for a := 0 to High(TriggersTable) do
1267 begin
1268 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1269 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1270 // Ëèôòû:
1271 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1272 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1273 else
1274 TriggersTable[a].LiftPanel := -1;
1275 // Äâåðè:
1276 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1277 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1278 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1279 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1280 else
1281 TriggersTable[a].DoorPanel := -1;
1282 // Òóðåëü:
1283 if triggers[a].TriggerType = TRIGGER_SHOT then
1284 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1285 else
1286 TriggersTable[a].ShotPanel := -1;
1288 g_Game_StepLoading();
1289 end;
1290 end;
1292 // Ñîçäàåì ïàíåëè:
1293 if panels <> nil then
1294 begin
1295 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1296 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1298 for a := 0 to High(panels) do
1299 begin
1300 SetLength(AddTextures, 0);
1301 trigRef := False;
1302 CurTex := -1;
1303 if _textures <> nil then
1304 begin
1305 texture := _textures[panels[a].TextureNum];
1306 ok := True;
1307 end
1308 else
1309 ok := False;
1311 if ok then
1312 begin
1313 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1314 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1315 ok := False;
1316 if (TriggersTable <> nil) and (_textures <> nil) then
1317 for b := 0 to High(TriggersTable) do
1318 if (TriggersTable[b].TexturePanel = a)
1319 or (TriggersTable[b].ShotPanel = a) then
1320 begin
1321 trigRef := True;
1322 ok := True;
1323 Break;
1324 end;
1325 end;
1327 if ok then
1328 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1329 SetLength(s, 64);
1330 CopyMemory(@s[1], @texture.Resource[0], 64);
1331 // Èçìåðÿåì äëèíó:
1332 Len := Length(s);
1333 for c := Len downto 1 do
1334 if s[c] <> #0 then
1335 begin
1336 Len := c;
1337 Break;
1338 end;
1339 SetLength(s, Len);
1341 // Ñïåö-òåêñòóðû çàïðåùåíû:
1342 if g_Map_IsSpecialTexture(s) then
1343 ok := False
1344 else
1345 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1346 ok := g_Texture_NumNameFindStart(s);
1348 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1349 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1350 if ok then
1351 begin
1352 k := NNF_NAME_BEFORE;
1353 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1354 while ok or (k = NNF_NAME_BEFORE) or
1355 (k = NNF_NAME_EQUALS) do
1356 begin
1357 k := g_Texture_NumNameFindNext(TexName);
1359 if (k = NNF_NAME_BEFORE) or
1360 (k = NNF_NAME_AFTER) then
1361 begin
1362 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1363 if ByteBool(texture.Anim) then
1364 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1365 isAnim := True;
1366 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1367 if not ok then
1368 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1369 isAnim := False;
1370 ok := CreateTexture(TexName, FileName, False) >= 0;
1371 end;
1372 end
1373 else
1374 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1375 isAnim := False;
1376 ok := CreateTexture(TexName, FileName, False) >= 0;
1377 if not ok then
1378 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1379 isAnim := True;
1380 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1381 end;
1382 end;
1384 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1385 if ok then
1386 begin
1387 for c := 0 to High(Textures) do
1388 if Textures[c].TextureName = TexName then
1389 begin
1390 SetLength(AddTextures, Length(AddTextures)+1);
1391 AddTextures[High(AddTextures)].Texture := c;
1392 AddTextures[High(AddTextures)].Anim := isAnim;
1393 Break;
1394 end;
1395 end;
1396 end
1397 else
1398 if k = NNF_NAME_EQUALS then
1399 begin
1400 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1401 SetLength(AddTextures, Length(AddTextures)+1);
1402 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1403 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1404 CurTex := High(AddTextures);
1405 ok := True;
1406 end
1407 else // NNF_NO_NAME
1408 ok := False;
1409 end; // while ok...
1411 ok := True;
1412 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1413 end; // if ok - ññûëàþòñÿ òðèããåðû
1415 if not ok then
1416 begin
1417 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1418 SetLength(AddTextures, 1);
1419 AddTextures[0].Texture := panels[a].TextureNum;
1420 AddTextures[0].Anim := ByteBool(texture.Anim);
1421 CurTex := 0;
1422 end;
1424 //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);
1426 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1427 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1429 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1430 if TriggersTable <> nil then
1431 for b := 0 to High(TriggersTable) do
1432 begin
1433 // Òðèããåð äâåðè/ëèôòà:
1434 if (TriggersTable[b].LiftPanel = a) or
1435 (TriggersTable[b].DoorPanel = a) then
1436 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1437 // Òðèããåð ñìåíû òåêñòóðû:
1438 if TriggersTable[b].TexturePanel = a then
1439 triggers[b].TexturePanel := PanelID;
1440 // Òðèããåð "Òóðåëü":
1441 if TriggersTable[b].ShotPanel = a then
1442 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1443 end;
1445 g_Game_StepLoading();
1446 end;
1447 end;
1449 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1450 if (triggers <> nil) and not gLoadGameMode then
1451 begin
1452 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1453 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1454 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1455 for a := 0 to High(triggers) do
1456 begin
1457 if triggers[a].TexturePanel <> -1 then
1458 b := panels[TriggersTable[a].TexturePanel].PanelType
1459 else
1460 b := 0;
1461 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1462 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1463 c := panels[TriggersTable[a].ShotPanel].PanelType
1464 else
1465 c := 0;
1466 CreateTrigger(triggers[a], b, c);
1467 end;
1468 end;
1470 // Çàãðóçêà ïðåäìåòîâ:
1471 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1472 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1473 items := MapReader.GetItems();
1475 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1476 if (items <> nil) and not gLoadGameMode then
1477 begin
1478 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1479 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1480 for a := 0 to High(items) do
1481 CreateItem(Items[a]);
1482 end;
1484 // Çàãðóçêà îáëàñòåé:
1485 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1486 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1487 areas := MapReader.GetAreas();
1489 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1490 if areas <> nil then
1491 begin
1492 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1493 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1494 for a := 0 to High(areas) do
1495 CreateArea(areas[a]);
1496 end;
1498 // Çàãðóçêà ìîíñòðîâ:
1499 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1500 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1501 monsters := MapReader.GetMonsters();
1503 gTotalMonsters := 0;
1505 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1506 if (monsters <> nil) and not gLoadGameMode then
1507 begin
1508 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1509 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1510 for a := 0 to High(monsters) do
1511 CreateMonster(monsters[a]);
1512 end;
1514 // Çàãðóçêà îïèñàíèÿ êàðòû:
1515 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1516 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1517 Header := MapReader.GetMapHeader();
1519 MapReader.Free();
1521 with gMapInfo do
1522 begin
1523 Name := Header.MapName;
1524 Description := Header.MapDescription;
1525 Author := Header.MapAuthor;
1526 MusicName := Header.MusicName;
1527 SkyName := Header.SkyName;
1528 Height := Header.Height;
1529 Width := Header.Width;
1530 end;
1532 // Çàãðóçêà íåáà:
1533 if gMapInfo.SkyName <> '' then
1534 begin
1535 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1536 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1537 FileName := g_ExtractWadName(gMapInfo.SkyName);
1539 if FileName <> '' then
1540 FileName := GameDir+'/wads/'+FileName
1541 else
1542 begin
1543 FileName := g_ExtractWadName(Res);
1544 end;
1546 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1547 if g_Texture_CreateWAD(BackID, s) then
1548 begin
1549 g_Game_SetupScreenSize();
1550 end
1551 else
1552 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1553 end;
1555 // Çàãðóçêà ìóçûêè:
1556 ok := False;
1557 if gMapInfo.MusicName <> '' then
1558 begin
1559 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1560 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1561 FileName := g_ExtractWadName(gMapInfo.MusicName);
1563 if FileName <> '' then
1564 FileName := GameDir+'/wads/'+FileName
1565 else
1566 begin
1567 FileName := g_ExtractWadName(Res);
1568 end;
1570 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1571 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1572 ok := True
1573 else
1574 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1575 end;
1577 // Îñòàëüíûå óñòàíâêè:
1578 CreateDoorMap();
1579 CreateLiftMap();
1581 g_Items_Init();
1582 g_Weapon_Init();
1583 g_Monsters_Init();
1585 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1586 if not gLoadGameMode then
1587 g_GFX_Init();
1589 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1590 _textures := nil;
1591 panels := nil;
1592 items := nil;
1593 areas := nil;
1594 triggers := nil;
1595 TriggersTable := nil;
1596 AddTextures := nil;
1598 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1599 if ok and (not gLoadGameMode) then
1600 begin
1601 gMusic.SetByName(gMapInfo.MusicName);
1602 gMusic.Play();
1603 end
1604 else
1605 gMusic.SetByName('');
1606 finally
1607 sfsGCEnable(); // enable releasing unused volumes
1608 end;
1610 e_WriteLog('Creating map grid', MSG_NOTIFY);
1611 mapCreateGrid();
1613 e_WriteLog('Done loading map.', MSG_NOTIFY);
1614 Result := True;
1615 end;
1617 function g_Map_GetMapInfo(Res: String): TMapInfo;
1618 var
1619 WAD: TWADFile;
1620 MapReader: TMapReader_1;
1621 Header: TMapHeaderRec_1;
1622 FileName: String;
1623 Data: Pointer;
1624 Len: Integer;
1625 begin
1626 FillChar(Result, SizeOf(Result), 0);
1627 FileName := g_ExtractWadName(Res);
1629 WAD := TWADFile.Create();
1630 if not WAD.ReadFile(FileName) then
1631 begin
1632 WAD.Free();
1633 Exit;
1634 end;
1636 //k8: it ignores path again
1637 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1638 begin
1639 WAD.Free();
1640 Exit;
1641 end;
1643 WAD.Free();
1645 MapReader := TMapReader_1.Create();
1647 if not MapReader.LoadMap(Data) then
1648 begin
1649 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1650 ZeroMemory(@Header, SizeOf(Header));
1651 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1652 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1653 end
1654 else
1655 begin
1656 Header := MapReader.GetMapHeader();
1657 Result.Name := Header.MapName;
1658 Result.Description := Header.MapDescription;
1659 end;
1661 FreeMem(Data);
1662 MapReader.Free();
1664 Result.Map := Res;
1665 Result.Author := Header.MapAuthor;
1666 Result.Height := Header.Height;
1667 Result.Width := Header.Width;
1668 end;
1670 function g_Map_GetMapsList(WADName: string): SArray;
1671 var
1672 WAD: TWADFile;
1673 a: Integer;
1674 ResList: SArray;
1675 begin
1676 Result := nil;
1677 WAD := TWADFile.Create();
1678 if not WAD.ReadFile(WADName) then
1679 begin
1680 WAD.Free();
1681 Exit;
1682 end;
1683 ResList := WAD.GetMapResources();
1684 if ResList <> nil then
1685 begin
1686 for a := 0 to High(ResList) do
1687 begin
1688 SetLength(Result, Length(Result)+1);
1689 Result[High(Result)] := ResList[a];
1690 end;
1691 end;
1692 WAD.Free();
1693 end;
1695 function g_Map_Exist(Res: string): Boolean;
1696 var
1697 WAD: TWADFile;
1698 FileName, mnn: string;
1699 ResList: SArray;
1700 a: Integer;
1701 begin
1702 Result := False;
1704 FileName := addWadExtension(g_ExtractWadName(Res));
1706 WAD := TWADFile.Create;
1707 if not WAD.ReadFile(FileName) then
1708 begin
1709 WAD.Free();
1710 Exit;
1711 end;
1713 ResList := WAD.GetMapResources();
1714 WAD.Free();
1716 mnn := g_ExtractFileName(Res);
1717 if ResList <> nil then
1718 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1719 begin
1720 Result := True;
1721 Exit;
1722 end;
1723 end;
1725 procedure g_Map_Free();
1726 var
1727 a: Integer;
1729 procedure FreePanelArray(var panels: TPanelArray);
1730 var
1731 i: Integer;
1733 begin
1734 if panels <> nil then
1735 begin
1736 for i := 0 to High(panels) do
1737 panels[i].Free();
1738 panels := nil;
1739 end;
1740 end;
1742 begin
1743 g_GFX_Free();
1744 g_Weapon_Free();
1745 g_Items_Free();
1746 g_Triggers_Free();
1747 g_Monsters_Free();
1749 RespawnPoints := nil;
1750 if FlagPoints[FLAG_RED] <> nil then
1751 begin
1752 Dispose(FlagPoints[FLAG_RED]);
1753 FlagPoints[FLAG_RED] := nil;
1754 end;
1755 if FlagPoints[FLAG_BLUE] <> nil then
1756 begin
1757 Dispose(FlagPoints[FLAG_BLUE]);
1758 FlagPoints[FLAG_BLUE] := nil;
1759 end;
1760 //DOMFlagPoints := nil;
1762 //gDOMFlags := nil;
1764 if Textures <> nil then
1765 begin
1766 for a := 0 to High(Textures) do
1767 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1768 if Textures[a].Anim then
1769 g_Frames_DeleteByID(Textures[a].FramesID)
1770 else
1771 if Textures[a].TextureID <> TEXTURE_NONE then
1772 e_DeleteTexture(Textures[a].TextureID);
1774 Textures := nil;
1775 end;
1777 FreePanelArray(gWalls);
1778 FreePanelArray(gRenderBackgrounds);
1779 FreePanelArray(gRenderForegrounds);
1780 FreePanelArray(gWater);
1781 FreePanelArray(gAcid1);
1782 FreePanelArray(gAcid2);
1783 FreePanelArray(gSteps);
1784 FreePanelArray(gLifts);
1785 FreePanelArray(gBlockMon);
1787 if BackID <> DWORD(-1) then
1788 begin
1789 gBackSize.X := 0;
1790 gBackSize.Y := 0;
1791 e_DeleteTexture(BackID);
1792 BackID := DWORD(-1);
1793 end;
1795 g_Game_StopAllSounds(False);
1796 gMusic.FreeSound();
1797 g_Sound_Delete(gMapInfo.MusicName);
1799 gMapInfo.Name := '';
1800 gMapInfo.Description := '';
1801 gMapInfo.MusicName := '';
1802 gMapInfo.Height := 0;
1803 gMapInfo.Width := 0;
1805 gDoorMap := nil;
1806 gLiftMap := nil;
1808 PanelByID := nil;
1809 end;
1811 procedure g_Map_Update();
1812 var
1813 a, d, j: Integer;
1814 m: Word;
1815 s: String;
1817 procedure UpdatePanelArray(var panels: TPanelArray);
1818 var
1819 i: Integer;
1821 begin
1822 if panels <> nil then
1823 for i := 0 to High(panels) do
1824 panels[i].Update();
1825 end;
1827 begin
1828 UpdatePanelArray(gWalls);
1829 UpdatePanelArray(gRenderBackgrounds);
1830 UpdatePanelArray(gRenderForegrounds);
1831 UpdatePanelArray(gWater);
1832 UpdatePanelArray(gAcid1);
1833 UpdatePanelArray(gAcid2);
1834 UpdatePanelArray(gSteps);
1836 if gGameSettings.GameMode = GM_CTF then
1837 begin
1838 for a := FLAG_RED to FLAG_BLUE do
1839 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1840 with gFlags[a] do
1841 begin
1842 if gFlags[a].Animation <> nil then
1843 gFlags[a].Animation.Update();
1845 m := g_Obj_Move(@Obj, True, True);
1847 if gTime mod (GAME_TICK*2) <> 0 then
1848 Continue;
1850 // Ñîïðîòèâëåíèå âîçäóõà:
1851 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1853 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1854 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1855 begin
1856 g_Map_ResetFlag(a);
1857 gFlags[a].CaptureTime := 0;
1858 if a = FLAG_RED then
1859 s := _lc[I_PLAYER_FLAG_RED]
1860 else
1861 s := _lc[I_PLAYER_FLAG_BLUE];
1862 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1864 if g_Game_IsNet then
1865 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1866 Continue;
1867 end;
1869 if Count > 0 then
1870 Count := Count - 1;
1872 // Èãðîê áåðåò ôëàã:
1873 if gPlayers <> nil then
1874 begin
1875 j := Random(Length(gPlayers)) - 1;
1877 for d := 0 to High(gPlayers) do
1878 begin
1879 Inc(j);
1880 if j > High(gPlayers) then
1881 j := 0;
1883 if gPlayers[j] <> nil then
1884 if gPlayers[j].Live and
1885 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1886 begin
1887 if gPlayers[j].GetFlag(a) then
1888 Break;
1889 end;
1890 end;
1891 end;
1892 end;
1893 end;
1894 end;
1897 // old algo
1898 procedure g_Map_DrawPanels (PanelType: Word);
1900 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
1901 var
1902 idx: Integer;
1903 begin
1904 if (panels <> nil) then
1905 begin
1906 // alas, no visible set
1907 for idx := 0 to High(panels) do
1908 begin
1909 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1910 end;
1911 end;
1912 end;
1914 begin
1915 case PanelType of
1916 PANEL_WALL: DrawPanels(gWalls);
1917 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
1918 PANEL_BACK: DrawPanels(gRenderBackgrounds);
1919 PANEL_FORE: DrawPanels(gRenderForegrounds);
1920 PANEL_WATER: DrawPanels(gWater);
1921 PANEL_ACID1: DrawPanels(gAcid1);
1922 PANEL_ACID2: DrawPanels(gAcid2);
1923 PANEL_STEP: DrawPanels(gSteps);
1924 end;
1925 end;
1928 // new algo
1929 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
1930 function checker (pan: TPanel; tag: Integer): Boolean;
1931 begin
1932 result := false; // don't stop, ever
1933 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
1934 gDrawPanelList.insert(pan);
1935 end;
1937 begin
1938 dplClear();
1939 //tagmask := panelTypeToTag(PanelType);
1941 if gdbg_map_use_tree_draw then
1942 begin
1943 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
1944 end
1945 else
1946 begin
1947 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
1948 end;
1949 // list will be rendered in `g_game.DrawPlayer()`
1950 end;
1953 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
1954 function checker (pan: TPanel; tag: Integer): Boolean;
1955 begin
1956 result := false; // don't stop, ever
1957 pan.DrawShadowVolume(lightX, lightY, radius);
1958 end;
1960 begin
1961 if gdbg_map_use_tree_draw then
1962 begin
1963 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
1964 end
1965 else
1966 begin
1967 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
1968 end;
1969 end;
1972 procedure g_Map_DrawBack(dx, dy: Integer);
1973 begin
1974 if gDrawBackGround and (BackID <> DWORD(-1)) then
1975 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
1976 else
1977 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
1978 end;
1980 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
1981 PanelType: Word; b1x3: Boolean): Boolean;
1982 var
1983 a, h: Integer;
1984 begin
1985 Result := False;
1987 if WordBool(PanelType and PANEL_WALL) then
1988 if gWalls <> nil then
1989 begin
1990 h := High(gWalls);
1992 for a := 0 to h do
1993 if gWalls[a].Enabled and
1994 g_Collide(X, Y, Width, Height,
1995 gWalls[a].X, gWalls[a].Y,
1996 gWalls[a].Width, gWalls[a].Height) then
1997 begin
1998 Result := True;
1999 Exit;
2000 end;
2001 end;
2003 if WordBool(PanelType and PANEL_WATER) then
2004 if gWater <> nil then
2005 begin
2006 h := High(gWater);
2008 for a := 0 to h do
2009 if g_Collide(X, Y, Width, Height,
2010 gWater[a].X, gWater[a].Y,
2011 gWater[a].Width, gWater[a].Height) then
2012 begin
2013 Result := True;
2014 Exit;
2015 end;
2016 end;
2018 if WordBool(PanelType and PANEL_ACID1) then
2019 if gAcid1 <> nil then
2020 begin
2021 h := High(gAcid1);
2023 for a := 0 to h do
2024 if g_Collide(X, Y, Width, Height,
2025 gAcid1[a].X, gAcid1[a].Y,
2026 gAcid1[a].Width, gAcid1[a].Height) then
2027 begin
2028 Result := True;
2029 Exit;
2030 end;
2031 end;
2033 if WordBool(PanelType and PANEL_ACID2) then
2034 if gAcid2 <> nil then
2035 begin
2036 h := High(gAcid2);
2038 for a := 0 to h do
2039 if g_Collide(X, Y, Width, Height,
2040 gAcid2[a].X, gAcid2[a].Y,
2041 gAcid2[a].Width, gAcid2[a].Height) then
2042 begin
2043 Result := True;
2044 Exit;
2045 end;
2046 end;
2048 if WordBool(PanelType and PANEL_STEP) then
2049 if gSteps <> nil then
2050 begin
2051 h := High(gSteps);
2053 for a := 0 to h do
2054 if g_Collide(X, Y, Width, Height,
2055 gSteps[a].X, gSteps[a].Y,
2056 gSteps[a].Width, gSteps[a].Height) then
2057 begin
2058 Result := True;
2059 Exit;
2060 end;
2061 end;
2063 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2064 if gLifts <> nil then
2065 begin
2066 h := High(gLifts);
2068 for a := 0 to h do
2069 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2070 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2071 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2072 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2073 g_Collide(X, Y, Width, Height,
2074 gLifts[a].X, gLifts[a].Y,
2075 gLifts[a].Width, gLifts[a].Height) then
2076 begin
2077 Result := True;
2078 Exit;
2079 end;
2080 end;
2082 if WordBool(PanelType and PANEL_BLOCKMON) then
2083 if gBlockMon <> nil then
2084 begin
2085 h := High(gBlockMon);
2087 for a := 0 to h do
2088 if ( (not b1x3) or
2089 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2090 g_Collide(X, Y, Width, Height,
2091 gBlockMon[a].X, gBlockMon[a].Y,
2092 gBlockMon[a].Width, gBlockMon[a].Height) then
2093 begin
2094 Result := True;
2095 Exit;
2096 end;
2097 end;
2098 end;
2100 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2101 var
2102 texid: DWORD;
2104 function checkPanels (constref panels: TPanelArray): Boolean;
2105 var
2106 a: Integer;
2107 begin
2108 result := false;
2109 if panels = nil then exit;
2110 for a := 0 to High(panels) do
2111 begin
2112 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2113 begin
2114 result := true;
2115 texid := panels[a].GetTextureID();
2116 exit;
2117 end;
2118 end;
2119 end;
2121 begin
2122 texid := TEXTURE_NONE;
2123 result := texid;
2124 if not checkPanels(gWater) then
2125 if not checkPanels(gAcid1) then
2126 if not checkPanels(gAcid2) then exit;
2127 result := texid;
2128 end;
2131 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2132 function checker (pan: TPanel; tag: Integer): Boolean;
2133 begin
2134 result := false; // don't stop, ever
2136 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2137 begin
2138 if not pan.Enabled then exit;
2139 end;
2141 if ((tag and GridTagLift) <> 0) then
2142 begin
2143 result :=
2144 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2145 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2146 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2147 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) and
2148 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2149 exit;
2150 end;
2152 if ((tag and GridTagBlockMon) <> 0) then
2153 begin
2154 result := ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2155 exit;
2156 end;
2158 // other shit
2159 result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2160 end;
2162 var
2163 tagmask: Integer = 0;
2164 begin
2165 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2166 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2167 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2168 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2169 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2170 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2171 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2173 if (tagmask = 0) then begin result := false; exit; end; // just in case
2175 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2176 if gdbg_map_use_accel_coldet then
2177 begin
2178 if gdbg_map_use_tree_coldet then
2179 begin
2180 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2181 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2182 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2183 begin
2184 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2185 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2186 end;
2187 end
2188 else
2189 begin
2190 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2191 end;
2192 end
2193 else
2194 begin
2195 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2196 end;
2197 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2198 end;
2201 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2202 var
2203 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2204 texid: DWORD;
2206 // slightly different from the old code, but meh...
2207 function checker (pan: TPanel; tag: Integer): Boolean;
2208 begin
2209 result := false; // don't stop, ever
2210 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2211 // check priorities
2212 case cctype of
2213 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2214 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2215 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2216 end;
2217 // collision?
2218 if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2219 // yeah
2220 texid := pan.GetTextureID();
2221 // water? water has the highest priority, so stop right here
2222 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2223 // acid2?
2224 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2225 // acid1?
2226 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2227 end;
2229 begin
2230 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2231 if gdbg_map_use_accel_coldet then
2232 begin
2233 texid := TEXTURE_NONE;
2234 if gdbg_map_use_tree_coldet then
2235 begin
2236 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2237 end
2238 else
2239 begin
2240 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2241 end;
2242 result := texid;
2243 end
2244 else
2245 begin
2246 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2247 end;
2248 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2249 end;
2251 procedure g_Map_EnableWall(ID: DWORD);
2252 begin
2253 with gWalls[ID] do
2254 begin
2255 Enabled := True;
2256 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2258 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2259 end;
2260 end;
2262 procedure g_Map_DisableWall(ID: DWORD);
2263 begin
2264 with gWalls[ID] do
2265 begin
2266 Enabled := False;
2267 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2269 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2270 end;
2271 end;
2273 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2274 var
2275 tp: TPanel;
2276 begin
2277 case PanelType of
2278 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2279 tp := gWalls[ID];
2280 PANEL_FORE:
2281 tp := gRenderForegrounds[ID];
2282 PANEL_BACK:
2283 tp := gRenderBackgrounds[ID];
2284 PANEL_WATER:
2285 tp := gWater[ID];
2286 PANEL_ACID1:
2287 tp := gAcid1[ID];
2288 PANEL_ACID2:
2289 tp := gAcid2[ID];
2290 PANEL_STEP:
2291 tp := gSteps[ID];
2292 else
2293 Exit;
2294 end;
2296 tp.NextTexture(AnimLoop);
2297 if g_Game_IsServer and g_Game_IsNet then
2298 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2299 end;
2301 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2302 begin
2303 if gLifts[ID].LiftType = t then
2304 Exit;
2306 with gLifts[ID] do
2307 begin
2308 LiftType := t;
2310 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2312 if LiftType = 0 then
2313 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2314 else if LiftType = 1 then
2315 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2316 else if LiftType = 2 then
2317 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2318 else if LiftType = 3 then
2319 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2321 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2322 end;
2323 end;
2325 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2326 var
2327 a: Integer;
2328 PointsArray: Array of TRespawnPoint;
2329 begin
2330 Result := False;
2331 SetLength(PointsArray, 0);
2333 if RespawnPoints = nil then
2334 Exit;
2336 for a := 0 to High(RespawnPoints) do
2337 if RespawnPoints[a].PointType = PointType then
2338 begin
2339 SetLength(PointsArray, Length(PointsArray)+1);
2340 PointsArray[High(PointsArray)] := RespawnPoints[a];
2341 end;
2343 if PointsArray = nil then
2344 Exit;
2346 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2347 Result := True;
2348 end;
2350 function g_Map_GetPointCount(PointType: Byte): Word;
2351 var
2352 a: Integer;
2353 begin
2354 Result := 0;
2356 if RespawnPoints = nil then
2357 Exit;
2359 for a := 0 to High(RespawnPoints) do
2360 if RespawnPoints[a].PointType = PointType then
2361 Result := Result + 1;
2362 end;
2364 function g_Map_HaveFlagPoints(): Boolean;
2365 begin
2366 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2367 end;
2369 procedure g_Map_ResetFlag(Flag: Byte);
2370 begin
2371 with gFlags[Flag] do
2372 begin
2373 Obj.X := -1000;
2374 Obj.Y := -1000;
2375 Obj.Vel.X := 0;
2376 Obj.Vel.Y := 0;
2377 Direction := D_LEFT;
2378 State := FLAG_STATE_NONE;
2379 if FlagPoints[Flag] <> nil then
2380 begin
2381 Obj.X := FlagPoints[Flag]^.X;
2382 Obj.Y := FlagPoints[Flag]^.Y;
2383 Direction := FlagPoints[Flag]^.Direction;
2384 State := FLAG_STATE_NORMAL;
2385 end;
2386 Count := -1;
2387 end;
2388 end;
2390 procedure g_Map_DrawFlags();
2391 var
2392 i, dx: Integer;
2393 Mirror: TMirrorType;
2394 begin
2395 if gGameSettings.GameMode <> GM_CTF then
2396 Exit;
2398 for i := FLAG_RED to FLAG_BLUE do
2399 with gFlags[i] do
2400 if State <> FLAG_STATE_CAPTURED then
2401 begin
2402 if State = FLAG_STATE_NONE then
2403 continue;
2405 if Direction = D_LEFT then
2406 begin
2407 Mirror := M_HORIZONTAL;
2408 dx := -1;
2409 end
2410 else
2411 begin
2412 Mirror := M_NONE;
2413 dx := 1;
2414 end;
2416 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2418 if g_debug_Frames then
2419 begin
2420 e_DrawQuad(Obj.X+Obj.Rect.X,
2421 Obj.Y+Obj.Rect.Y,
2422 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2423 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2424 0, 255, 0);
2425 end;
2426 end;
2427 end;
2429 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2430 var
2431 dw: DWORD;
2432 b: Byte;
2433 str: String;
2434 boo: Boolean;
2436 procedure SavePanelArray(var panels: TPanelArray);
2437 var
2438 PAMem: TBinMemoryWriter;
2439 i: Integer;
2440 begin
2441 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2442 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2444 i := 0;
2445 while i < Length(panels) do
2446 begin
2447 if panels[i].SaveIt then
2448 begin
2449 // ID ïàíåëè:
2450 PAMem.WriteInt(i);
2451 // Ñîõðàíÿåì ïàíåëü:
2452 panels[i].SaveState(PAMem);
2453 end;
2454 Inc(i);
2455 end;
2457 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2458 PAMem.SaveToMemory(Mem);
2459 PAMem.Free();
2460 end;
2462 procedure SaveFlag(flag: PFlag);
2463 begin
2464 // Ñèãíàòóðà ôëàãà:
2465 dw := FLAG_SIGNATURE; // 'FLAG'
2466 Mem.WriteDWORD(dw);
2467 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2468 Mem.WriteByte(flag^.RespawnType);
2469 // Ñîñòîÿíèå ôëàãà:
2470 Mem.WriteByte(flag^.State);
2471 // Íàïðàâëåíèå ôëàãà:
2472 if flag^.Direction = D_LEFT then
2473 b := 1
2474 else // D_RIGHT
2475 b := 2;
2476 Mem.WriteByte(b);
2477 // Îáúåêò ôëàãà:
2478 Obj_SaveState(@flag^.Obj, Mem);
2479 end;
2481 begin
2482 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2484 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2485 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2486 SavePanelArray(gWalls);
2487 // Ñîõðàíÿåì ïàíåëè ôîíà:
2488 SavePanelArray(gRenderBackgrounds);
2489 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2490 SavePanelArray(gRenderForegrounds);
2491 // Ñîõðàíÿåì ïàíåëè âîäû:
2492 SavePanelArray(gWater);
2493 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2494 SavePanelArray(gAcid1);
2495 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2496 SavePanelArray(gAcid2);
2497 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2498 SavePanelArray(gSteps);
2499 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2500 SavePanelArray(gLifts);
2501 ///// /////
2503 ///// Ñîõðàíÿåì ìóçûêó: /////
2504 // Ñèãíàòóðà ìóçûêè:
2505 dw := MUSIC_SIGNATURE; // 'MUSI'
2506 Mem.WriteDWORD(dw);
2507 // Íàçâàíèå ìóçûêè:
2508 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2509 if gMusic.NoMusic then
2510 str := ''
2511 else
2512 str := gMusic.Name;
2513 Mem.WriteString(str, 64);
2514 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2515 dw := gMusic.GetPosition();
2516 Mem.WriteDWORD(dw);
2517 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2518 boo := gMusic.SpecPause;
2519 Mem.WriteBoolean(boo);
2520 ///// /////
2522 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2523 Mem.WriteInt(gTotalMonsters);
2524 ///// /////
2526 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2527 if gGameSettings.GameMode = GM_CTF then
2528 begin
2529 // Ôëàã Êðàñíîé êîìàíäû:
2530 SaveFlag(@gFlags[FLAG_RED]);
2531 // Ôëàã Ñèíåé êîìàíäû:
2532 SaveFlag(@gFlags[FLAG_BLUE]);
2533 end;
2534 ///// /////
2536 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2537 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2538 begin
2539 // Î÷êè Êðàñíîé êîìàíäû:
2540 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2541 // Î÷êè Ñèíåé êîìàíäû:
2542 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2543 end;
2544 ///// /////
2545 end;
2547 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2548 var
2549 dw: DWORD;
2550 b: Byte;
2551 str: String;
2552 boo: Boolean;
2554 procedure LoadPanelArray(var panels: TPanelArray);
2555 var
2556 PAMem: TBinMemoryReader;
2557 i, id: Integer;
2558 begin
2559 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2560 PAMem := TBinMemoryReader.Create();
2561 PAMem.LoadFromMemory(Mem);
2563 for i := 0 to Length(panels)-1 do
2564 if panels[i].SaveIt then
2565 begin
2566 // ID ïàíåëè:
2567 PAMem.ReadInt(id);
2568 if id <> i then
2569 begin
2570 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2571 end;
2572 // Çàãðóæàåì ïàíåëü:
2573 panels[i].LoadState(PAMem);
2574 end;
2576 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2577 PAMem.Free();
2578 end;
2580 procedure LoadFlag(flag: PFlag);
2581 begin
2582 // Ñèãíàòóðà ôëàãà:
2583 Mem.ReadDWORD(dw);
2584 if dw <> FLAG_SIGNATURE then // 'FLAG'
2585 begin
2586 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2587 end;
2588 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2589 Mem.ReadByte(flag^.RespawnType);
2590 // Ñîñòîÿíèå ôëàãà:
2591 Mem.ReadByte(flag^.State);
2592 // Íàïðàâëåíèå ôëàãà:
2593 Mem.ReadByte(b);
2594 if b = 1 then
2595 flag^.Direction := D_LEFT
2596 else // b = 2
2597 flag^.Direction := D_RIGHT;
2598 // Îáúåêò ôëàãà:
2599 Obj_LoadState(@flag^.Obj, Mem);
2600 end;
2602 begin
2603 if Mem = nil then
2604 Exit;
2606 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2607 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2608 LoadPanelArray(gWalls);
2609 // Çàãðóæàåì ïàíåëè ôîíà:
2610 LoadPanelArray(gRenderBackgrounds);
2611 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2612 LoadPanelArray(gRenderForegrounds);
2613 // Çàãðóæàåì ïàíåëè âîäû:
2614 LoadPanelArray(gWater);
2615 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2616 LoadPanelArray(gAcid1);
2617 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2618 LoadPanelArray(gAcid2);
2619 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2620 LoadPanelArray(gSteps);
2621 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2622 LoadPanelArray(gLifts);
2623 ///// /////
2625 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2626 g_GFX_Init();
2627 mapCreateGrid();
2629 ///// Çàãðóæàåì ìóçûêó: /////
2630 // Ñèãíàòóðà ìóçûêè:
2631 Mem.ReadDWORD(dw);
2632 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2633 begin
2634 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2635 end;
2636 // Íàçâàíèå ìóçûêè:
2637 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2638 Mem.ReadString(str);
2639 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2640 Mem.ReadDWORD(dw);
2641 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2642 Mem.ReadBoolean(boo);
2643 // Çàïóñêàåì ýòó ìóçûêó:
2644 gMusic.SetByName(str);
2645 gMusic.SpecPause := boo;
2646 gMusic.Play();
2647 gMusic.Pause(True);
2648 gMusic.SetPosition(dw);
2649 ///// /////
2651 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2652 Mem.ReadInt(gTotalMonsters);
2653 ///// /////
2655 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2656 if gGameSettings.GameMode = GM_CTF then
2657 begin
2658 // Ôëàã Êðàñíîé êîìàíäû:
2659 LoadFlag(@gFlags[FLAG_RED]);
2660 // Ôëàã Ñèíåé êîìàíäû:
2661 LoadFlag(@gFlags[FLAG_BLUE]);
2662 end;
2663 ///// /////
2665 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2666 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2667 begin
2668 // Î÷êè Êðàñíîé êîìàíäû:
2669 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2670 // Î÷êè Ñèíåé êîìàíäû:
2671 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2672 end;
2673 ///// /////
2674 end;
2676 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2677 var
2678 Arr: TPanelArray;
2679 begin
2680 Result := nil;
2681 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2682 Arr := PanelByID[PanelID].PWhere^;
2683 PanelArrayID := PanelByID[PanelID].PArrID;
2684 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2685 end;
2687 end.