DEADSOFTWARE

"t_dump_node_queries" (temp debug, DO NOT TOUCH)
[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, 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(x0, y0, wdt, hgt: Integer; PanelType: Word);
67 procedure g_Map_DrawBack(dx, dy: Integer);
68 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
69 PanelType: Word; b1x3: Boolean): Boolean;
70 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
71 procedure g_Map_EnableWall(ID: DWORD);
72 procedure g_Map_DisableWall(ID: DWORD);
73 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
74 procedure g_Map_SetLift(ID: DWORD; t: Integer);
75 procedure g_Map_ReAdd_DieTriggers();
76 function g_Map_IsSpecialTexture(Texture: String): Boolean;
78 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
79 function g_Map_GetPointCount(PointType: Byte): Word;
81 function g_Map_HaveFlagPoints(): Boolean;
83 procedure g_Map_ResetFlag(Flag: Byte);
84 procedure g_Map_DrawFlags();
86 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
88 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
89 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
91 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
93 procedure g_Map_ProfilersBegin ();
94 procedure g_Map_ProfilersEnd ();
96 const
97 RESPAWNPOINT_PLAYER1 = 1;
98 RESPAWNPOINT_PLAYER2 = 2;
99 RESPAWNPOINT_DM = 3;
100 RESPAWNPOINT_RED = 4;
101 RESPAWNPOINT_BLUE = 5;
103 FLAG_NONE = 0;
104 FLAG_RED = 1;
105 FLAG_BLUE = 2;
106 FLAG_DOM = 3;
108 FLAG_STATE_NONE = 0;
109 FLAG_STATE_NORMAL = 1;
110 FLAG_STATE_DROPPED = 2;
111 FLAG_STATE_CAPTURED = 3;
112 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
113 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
115 FLAG_TIME = 720; // 20 seconds
117 SKY_STRETCH: Single = 1.5;
119 var
120 gWalls: TPanelArray;
121 gRenderBackgrounds: TPanelArray;
122 gRenderForegrounds: TPanelArray;
123 gWater, gAcid1, gAcid2: TPanelArray;
124 gSteps: TPanelArray;
125 gLifts: TPanelArray;
126 gBlockMon: TPanelArray;
127 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
128 //gDOMFlags: array of TFlag;
129 gMapInfo: TMapInfo;
130 gBackSize: TPoint;
131 gDoorMap: array of array of DWORD;
132 gLiftMap: array of array of DWORD;
133 gWADHash: TMD5Digest;
134 BackID: DWORD = DWORD(-1);
135 gExternalResources: TStringList;
137 gdbg_map_use_grid_render: Boolean = true;
138 gdbg_map_use_grid_coldet: Boolean = true;
139 gdbg_map_use_tree_draw: Boolean = true;
140 gdbg_map_use_tree_coldet: Boolean = false;
141 gdbg_map_dump_coldet_tree_queries: Boolean = false;
142 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
144 implementation
146 uses
147 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
148 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
149 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
150 Math, g_monsters, g_saveload, g_language, g_netmsg,
151 utils, sfs, binheap,
152 ImagingTypes, Imaging, ImagingUtility,
153 ImagingGif, ImagingNetworkGraphics;
155 const
156 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
157 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
158 FLAG_SIGNATURE = $47414C46; // 'FLAG'
160 GridTagInvalid = 0;
161 GridTagWallDoor = $0001;
162 GridTagBack = $0002;
163 GridTagFore = $0004;
164 GridTagWater = $0008;
165 GridTagAcid1 = $0010;
166 GridTagAcid2 = $0020;
167 GridTagStep = $0040;
168 GridTagLift = $0080;
169 GridTagBlockMon = $0100;
172 function panelTypeToTag (panelType: Word): Integer;
173 begin
174 case panelType of
175 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagWallDoor; // gWalls
176 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
177 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
178 PANEL_WATER: result := GridTagWater; // gWater
179 PANEL_ACID1: result := GridTagAcid1; // gAcid1
180 PANEL_ACID2: result := GridTagAcid2; // gAcid2
181 PANEL_STEP: result := GridTagStep; // gSteps
182 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
183 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
184 else result := GridTagInvalid;
185 end;
186 end;
189 type
190 TPanelID = record
191 PWhere: ^TPanelArray;
192 PArrID: Integer;
193 end;
195 type
196 TDynAABBTreeMap = class(TDynAABBTree)
197 function getFleshAABB (var aabb: AABB2D; flesh: TTreeFlesh): Boolean; override;
198 end;
200 function TDynAABBTreeMap.getFleshAABB (var aabb: AABB2D; flesh: TTreeFlesh): Boolean;
201 var
202 pan: TPanel;
203 begin
204 if (flesh = nil) then begin result := false; exit; end;
205 pan := (flesh as TPanel);
206 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width, pan.Y+pan.Height);
207 //e_WriteLog(Format('getFleshAABB(%d;%d) AABB:(%f,%f)-(%f,%f); valid=%d; volume=%f; x=%d; y=%d; w=%d; h=%d', [pan.tag, pan.ArrIdx, aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, Integer(aabb.valid), aabb.volume, pan.X, pan.Y, pan.Width, pan.Height]), MSG_NOTIFY);
208 result := aabb.valid;
209 end;
211 var
212 PanelById: array of TPanelID;
213 Textures: TLevelTextureArray;
214 RespawnPoints: Array of TRespawnPoint;
215 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
216 //DOMFlagPoints: Array of TFlagPoint;
217 gMapGrid: TBodyGrid = nil;
218 mapTree: TDynAABBTree = nil;
221 procedure g_Map_ProfilersBegin ();
222 begin
223 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('MAP COLLISION', g_profile_history_size);
224 profMapCollision.mainBegin(g_profile_collision);
225 // create sections
226 if g_profile_collision then
227 begin
228 profMapCollision.sectionBegin('wall coldet');
229 profMapCollision.sectionEnd();
230 profMapCollision.sectionBegin('liquid coldet');
231 profMapCollision.sectionEnd();
232 end;
233 end;
235 procedure g_Map_ProfilersEnd ();
236 begin
237 if (profMapCollision <> nil) then profMapCollision.mainEnd();
238 end;
241 function g_Map_IsSpecialTexture(Texture: String): Boolean;
242 begin
243 Result := (Texture = TEXTURE_NAME_WATER) or
244 (Texture = TEXTURE_NAME_ACID1) or
245 (Texture = TEXTURE_NAME_ACID2);
246 end;
248 procedure CreateDoorMap();
249 var
250 PanelArray: Array of record
251 X, Y: Integer;
252 Width, Height: Word;
253 Active: Boolean;
254 PanelID: DWORD;
255 end;
256 a, b, c, m, i, len: Integer;
257 ok: Boolean;
258 begin
259 if gWalls = nil then
260 Exit;
262 i := 0;
263 len := 128;
264 SetLength(PanelArray, len);
266 for a := 0 to High(gWalls) do
267 if gWalls[a].Door then
268 begin
269 PanelArray[i].X := gWalls[a].X;
270 PanelArray[i].Y := gWalls[a].Y;
271 PanelArray[i].Width := gWalls[a].Width;
272 PanelArray[i].Height := gWalls[a].Height;
273 PanelArray[i].Active := True;
274 PanelArray[i].PanelID := a;
276 i := i + 1;
277 if i = len then
278 begin
279 len := len + 128;
280 SetLength(PanelArray, len);
281 end;
282 end;
284 // Íåò äâåðåé:
285 if i = 0 then
286 begin
287 PanelArray := nil;
288 Exit;
289 end;
291 SetLength(gDoorMap, 0);
293 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
295 for a := 0 to i-1 do
296 if PanelArray[a].Active then
297 begin
298 PanelArray[a].Active := False;
299 m := Length(gDoorMap);
300 SetLength(gDoorMap, m+1);
301 SetLength(gDoorMap[m], 1);
302 gDoorMap[m, 0] := PanelArray[a].PanelID;
303 ok := True;
305 while ok do
306 begin
307 ok := False;
309 for b := 0 to i-1 do
310 if PanelArray[b].Active then
311 for c := 0 to High(gDoorMap[m]) do
312 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
313 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
314 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
315 PanelArray[b].Width, PanelArray[b].Height,
316 gWalls[gDoorMap[m, c]].X,
317 gWalls[gDoorMap[m, c]].Y,
318 gWalls[gDoorMap[m, c]].Width,
319 gWalls[gDoorMap[m, c]].Height) then
320 begin
321 PanelArray[b].Active := False;
322 SetLength(gDoorMap[m],
323 Length(gDoorMap[m])+1);
324 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
325 ok := True;
326 Break;
327 end;
328 end;
330 g_Game_StepLoading();
331 end;
333 PanelArray := nil;
334 end;
336 procedure CreateLiftMap();
337 var
338 PanelArray: Array of record
339 X, Y: Integer;
340 Width, Height: Word;
341 Active: Boolean;
342 end;
343 a, b, c, len, i, j: Integer;
344 ok: Boolean;
345 begin
346 if gLifts = nil then
347 Exit;
349 len := Length(gLifts);
350 SetLength(PanelArray, len);
352 for a := 0 to len-1 do
353 begin
354 PanelArray[a].X := gLifts[a].X;
355 PanelArray[a].Y := gLifts[a].Y;
356 PanelArray[a].Width := gLifts[a].Width;
357 PanelArray[a].Height := gLifts[a].Height;
358 PanelArray[a].Active := True;
359 end;
361 SetLength(gLiftMap, len);
362 i := 0;
364 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
366 for a := 0 to len-1 do
367 if PanelArray[a].Active then
368 begin
369 PanelArray[a].Active := False;
370 SetLength(gLiftMap[i], 32);
371 j := 0;
372 gLiftMap[i, j] := a;
373 ok := True;
375 while ok do
376 begin
377 ok := False;
378 for b := 0 to len-1 do
379 if PanelArray[b].Active then
380 for c := 0 to j do
381 if g_CollideAround(PanelArray[b].X,
382 PanelArray[b].Y,
383 PanelArray[b].Width,
384 PanelArray[b].Height,
385 PanelArray[gLiftMap[i, c]].X,
386 PanelArray[gLiftMap[i, c]].Y,
387 PanelArray[gLiftMap[i, c]].Width,
388 PanelArray[gLiftMap[i, c]].Height) then
389 begin
390 PanelArray[b].Active := False;
391 j := j+1;
392 if j > High(gLiftMap[i]) then
393 SetLength(gLiftMap[i],
394 Length(gLiftMap[i])+32);
396 gLiftMap[i, j] := b;
397 ok := True;
399 Break;
400 end;
401 end;
403 SetLength(gLiftMap[i], j+1);
404 i := i+1;
406 g_Game_StepLoading();
407 end;
409 SetLength(gLiftMap, i);
411 PanelArray := nil;
412 end;
414 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
415 CurTex: Integer; sav: Boolean): Integer;
416 var
417 len: Integer;
418 panels: ^TPanelArray;
419 begin
420 Result := -1;
422 case PanelRec.PanelType of
423 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
424 panels := @gWalls;
425 PANEL_BACK:
426 panels := @gRenderBackgrounds;
427 PANEL_FORE:
428 panels := @gRenderForegrounds;
429 PANEL_WATER:
430 panels := @gWater;
431 PANEL_ACID1:
432 panels := @gAcid1;
433 PANEL_ACID2:
434 panels := @gAcid2;
435 PANEL_STEP:
436 panels := @gSteps;
437 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
438 panels := @gLifts;
439 PANEL_BLOCKMON:
440 panels := @gBlockMon;
441 else
442 Exit;
443 end;
445 len := Length(panels^);
446 SetLength(panels^, len + 1);
448 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
449 panels^[len].ArrIdx := len;
450 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
451 if sav then
452 panels^[len].SaveIt := True;
454 Result := len;
456 len := Length(PanelByID);
457 SetLength(PanelByID, len + 1);
458 PanelByID[len].PWhere := panels;
459 PanelByID[len].PArrID := Result;
460 end;
462 function CreateNullTexture(RecName: String): Integer;
463 begin
464 SetLength(Textures, Length(Textures)+1);
465 result := High(Textures);
467 with Textures[High(Textures)] do
468 begin
469 TextureName := RecName;
470 Width := 1;
471 Height := 1;
472 Anim := False;
473 TextureID := TEXTURE_NONE;
474 end;
475 end;
477 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
478 var
479 WAD: TWADFile;
480 TextureData: Pointer;
481 WADName, txname: String;
482 a, ResLength: Integer;
483 begin
484 Result := -1;
486 if Textures <> nil then
487 for a := 0 to High(Textures) do
488 if Textures[a].TextureName = RecName then
489 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
490 Result := a;
491 Exit;
492 end;
494 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
495 if (RecName = TEXTURE_NAME_WATER) or
496 (RecName = TEXTURE_NAME_ACID1) or
497 (RecName = TEXTURE_NAME_ACID2) then
498 begin
499 SetLength(Textures, Length(Textures)+1);
501 with Textures[High(Textures)] do
502 begin
503 TextureName := RecName;
505 if TextureName = TEXTURE_NAME_WATER then
506 TextureID := TEXTURE_SPECIAL_WATER
507 else
508 if TextureName = TEXTURE_NAME_ACID1 then
509 TextureID := TEXTURE_SPECIAL_ACID1
510 else
511 if TextureName = TEXTURE_NAME_ACID2 then
512 TextureID := TEXTURE_SPECIAL_ACID2;
514 Anim := False;
515 end;
517 result := High(Textures);
518 Exit;
519 end;
521 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
522 WADName := g_ExtractWadName(RecName);
524 WAD := TWADFile.Create();
526 if WADName <> '' then
527 WADName := GameDir+'/wads/'+WADName
528 else
529 WADName := Map;
531 WAD.ReadFile(WADName);
533 txname := RecName;
535 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
536 begin
537 FreeMem(TextureData);
538 RecName := 'COMMON\ALIEN';
539 end;
542 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
543 begin
544 SetLength(Textures, Length(Textures)+1);
545 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
546 Exit;
547 e_GetTextureSize(Textures[High(Textures)].TextureID,
548 @Textures[High(Textures)].Width,
549 @Textures[High(Textures)].Height);
550 FreeMem(TextureData);
551 Textures[High(Textures)].TextureName := {RecName}txname;
552 Textures[High(Textures)].Anim := False;
554 result := High(Textures);
555 end
556 else // Íåò òàêîãî ðåóñðñà â WAD'å
557 begin
558 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
559 if log then
560 begin
561 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
562 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
563 end;
564 end;
566 WAD.Free();
567 end;
569 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
570 var
571 WAD: TWADFile;
572 TextureWAD: PChar = nil;
573 TextData: Pointer = nil;
574 TextureData: Pointer = nil;
575 cfg: TConfig = nil;
576 WADName: String;
577 ResLength: Integer;
578 TextureResource: String;
579 _width, _height, _framecount, _speed: Integer;
580 _backanimation: Boolean;
581 //imgfmt: string;
582 ia: TDynImageDataArray = nil;
583 f, c, frdelay, frloop: Integer;
584 begin
585 result := -1;
587 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
589 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
590 WADName := g_ExtractWadName(RecName);
592 WAD := TWADFile.Create();
593 try
594 if WADName <> '' then
595 WADName := GameDir+'/wads/'+WADName
596 else
597 WADName := Map;
599 WAD.ReadFile(WADName);
601 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
602 begin
603 if log then
604 begin
605 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
606 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
607 end;
608 exit;
609 end;
611 {TEST
612 if WADName = Map then
613 begin
614 //FreeMem(TextureWAD);
615 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
616 end;
619 WAD.FreeWAD();
621 if ResLength < 6 then
622 begin
623 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
624 exit;
625 end;
627 // ýòî ïòèöà? ýòî ñàìîë¸ò?
628 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
629 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
630 begin
631 // íåò, ýòî ñóïåðìåí!
632 if not WAD.ReadMemory(TextureWAD, ResLength) then
633 begin
634 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
635 exit;
636 end;
638 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
639 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
640 begin
641 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
642 exit;
643 end;
645 cfg := TConfig.CreateMem(TextData, ResLength);
647 TextureResource := cfg.ReadStr('', 'resource', '');
648 if TextureResource = '' then
649 begin
650 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
651 exit;
652 end;
654 _width := cfg.ReadInt('', 'framewidth', 0);
655 _height := cfg.ReadInt('', 'frameheight', 0);
656 _framecount := cfg.ReadInt('', 'framecount', 0);
657 _speed := cfg.ReadInt('', 'waitcount', 0);
658 _backanimation := cfg.ReadBool('', 'backanimation', False);
660 cfg.Free();
661 cfg := nil;
663 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
664 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
665 begin
666 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
667 exit;
668 end;
670 WAD.Free();
671 WAD := nil;
673 SetLength(Textures, Length(Textures)+1);
674 with Textures[High(Textures)] do
675 begin
676 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
677 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
678 begin
679 TextureName := RecName;
680 Width := _width;
681 Height := _height;
682 Anim := True;
683 FramesCount := _framecount;
684 Speed := _speed;
685 result := High(Textures);
686 end
687 else
688 begin
689 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
690 end;
691 end;
692 end
693 else
694 begin
695 // try animated image
697 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
698 if length(imgfmt) = 0 then
699 begin
700 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
701 exit;
702 end;
704 GlobalMetadata.ClearMetaItems();
705 GlobalMetadata.ClearMetaItemsForSaving();
706 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
707 begin
708 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
709 exit;
710 end;
711 if length(ia) = 0 then
712 begin
713 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
714 exit;
715 end;
717 WAD.Free();
718 WAD := nil;
720 _width := ia[0].width;
721 _height := ia[0].height;
722 _framecount := length(ia);
723 _speed := 1;
724 _backanimation := false;
725 frdelay := -1;
726 frloop := -666;
727 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
728 begin
729 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
730 try
731 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
732 frdelay := f;
733 if f < 0 then f := 0;
734 // rounding ;-)
735 c := f mod 28;
736 if c < 13 then c := 0 else c := 1;
737 f := (f div 28)+c;
738 if f < 1 then f := 1 else if f > 255 then f := 255;
739 _speed := f;
740 except
741 end;
742 end;
743 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
744 begin
745 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
746 try
747 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
748 frloop := f;
749 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
750 except
751 end;
752 end;
753 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
754 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
755 f := ord(_backanimation);
756 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);
758 SetLength(Textures, Length(Textures)+1);
759 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
760 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
761 begin
762 Textures[High(Textures)].TextureName := RecName;
763 Textures[High(Textures)].Width := _width;
764 Textures[High(Textures)].Height := _height;
765 Textures[High(Textures)].Anim := True;
766 Textures[High(Textures)].FramesCount := length(ia);
767 Textures[High(Textures)].Speed := _speed;
768 result := High(Textures);
769 //writeln(' CREATED!');
770 end
771 else
772 begin
773 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
774 end;
775 end;
776 finally
777 for f := 0 to High(ia) do FreeImage(ia[f]);
778 WAD.Free();
779 cfg.Free();
780 if TextureWAD <> nil then FreeMem(TextureWAD);
781 if TextData <> nil then FreeMem(TextData);
782 if TextureData <> nil then FreeMem(TextureData);
783 end;
784 end;
786 procedure CreateItem(Item: TItemRec_1);
787 begin
788 if g_Game_IsClient then Exit;
790 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
791 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
792 Exit;
794 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
795 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
796 end;
798 procedure CreateArea(Area: TAreaRec_1);
799 var
800 a: Integer;
801 id: DWORD;
802 begin
803 case Area.AreaType of
804 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
805 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
806 begin
807 SetLength(RespawnPoints, Length(RespawnPoints)+1);
808 with RespawnPoints[High(RespawnPoints)] do
809 begin
810 X := Area.X;
811 Y := Area.Y;
812 Direction := TDirection(Area.Direction);
814 case Area.AreaType of
815 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
816 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
817 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
818 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
819 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
820 end;
821 end;
822 end;
824 AREA_REDFLAG, AREA_BLUEFLAG:
825 begin
826 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
828 if FlagPoints[a] <> nil then Exit;
830 New(FlagPoints[a]);
832 with FlagPoints[a]^ do
833 begin
834 X := Area.X-FLAGRECT.X;
835 Y := Area.Y-FLAGRECT.Y;
836 Direction := TDirection(Area.Direction);
837 end;
839 with gFlags[a] do
840 begin
841 case a of
842 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
843 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
844 end;
846 Animation := TAnimation.Create(id, True, 8);
847 Obj.Rect := FLAGRECT;
849 g_Map_ResetFlag(a);
850 end;
851 end;
853 AREA_DOMFLAG:
854 begin
855 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
856 with DOMFlagPoints[High(DOMFlagPoints)] do
857 begin
858 X := Area.X;
859 Y := Area.Y;
860 Direction := TDirection(Area.Direction);
861 end;
863 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
864 end;
865 end;
866 end;
868 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
869 var
870 _trigger: TTrigger;
871 begin
872 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
874 with _trigger do
875 begin
876 X := Trigger.X;
877 Y := Trigger.Y;
878 Width := Trigger.Width;
879 Height := Trigger.Height;
880 Enabled := ByteBool(Trigger.Enabled);
881 TexturePanel := Trigger.TexturePanel;
882 TexturePanelType := fTexturePanel1Type;
883 ShotPanelType := fTexturePanel2Type;
884 TriggerType := Trigger.TriggerType;
885 ActivateType := Trigger.ActivateType;
886 Keys := Trigger.Keys;
887 Data.Default := Trigger.DATA;
888 end;
890 g_Triggers_Create(_trigger);
891 end;
893 procedure CreateMonster(monster: TMonsterRec_1);
894 var
895 a, i: Integer;
896 begin
897 if g_Game_IsClient then Exit;
899 if (gGameSettings.GameType = GT_SINGLE)
900 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
901 begin
902 i := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y,
903 TDirection(monster.Direction));
905 if gTriggers <> nil then
906 for a := 0 to High(gTriggers) do
907 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
908 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
909 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
910 gMonsters[i].AddTrigger(a);
912 if monster.MonsterType <> MONSTER_BARREL then
913 Inc(gTotalMonsters);
914 end;
915 end;
917 procedure g_Map_ReAdd_DieTriggers();
918 var
919 i, a: Integer;
920 begin
921 if g_Game_IsClient then Exit;
923 for i := 0 to High(gMonsters) do
924 if gMonsters[i] <> nil then
925 begin
926 gMonsters[i].ClearTriggers();
928 for a := 0 to High(gTriggers) do
929 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
930 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
931 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
932 gMonsters[i].AddTrigger(a);
933 end;
934 end;
936 function extractWadName(resourceName: string): string;
937 var
938 posN: Integer;
939 begin
940 posN := Pos(':', resourceName);
941 if posN > 0 then
942 Result:= Copy(resourceName, 0, posN-1)
943 else
944 Result := '';
945 end;
947 procedure addResToExternalResList(res: string);
948 begin
949 res := extractWadName(res);
950 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
951 gExternalResources.Add(res);
952 end;
954 procedure generateExternalResourcesList(mapReader: TMapReader_1);
955 var
956 textures: TTexturesRec1Array;
957 mapHeader: TMapHeaderRec_1;
958 i: integer;
959 resFile: String = '';
960 begin
961 if gExternalResources = nil then
962 gExternalResources := TStringList.Create;
964 gExternalResources.Clear;
965 textures := mapReader.GetTextures();
966 for i := 0 to High(textures) do
967 begin
968 addResToExternalResList(resFile);
969 end;
971 textures := nil;
973 mapHeader := mapReader.GetMapHeader;
975 addResToExternalResList(mapHeader.MusicName);
976 addResToExternalResList(mapHeader.SkyName);
977 end;
979 procedure mapCreateGrid ();
980 var
981 mapX0: Integer = $3fffffff;
982 mapY0: Integer = $3fffffff;
983 mapX1: Integer = -$3fffffff;
984 mapY1: Integer = -$3fffffff;
986 procedure fixMinMax (var panels: TPanelArray);
987 var
988 idx: Integer;
989 begin
990 for idx := 0 to High(panels) do
991 begin
992 if (panels[idx].Width < 1) or (panels[idx].Height < 1) then continue;
993 if mapX0 > panels[idx].X then mapX0 := panels[idx].X;
994 if mapY0 > panels[idx].Y then mapY0 := panels[idx].Y;
995 if mapX1 < panels[idx].X+panels[idx].Width-1 then mapX1 := panels[idx].X+panels[idx].Width-1;
996 if mapY1 < panels[idx].Y+panels[idx].Height-1 then mapY1 := panels[idx].Y+panels[idx].Height-1;
997 end;
998 end;
1000 procedure addPanelsToGrid (var panels: TPanelArray; tag: Integer);
1001 var
1002 idx: Integer;
1003 begin
1004 tag := panelTypeToTag(tag);
1005 for idx := High(panels) downto 0 do
1006 begin
1007 panels[idx].tag := tag;
1008 gMapGrid.insertBody(panels[idx], panels[idx].X, panels[idx].Y, panels[idx].Width, panels[idx].Height, tag);
1009 mapTree.insertObject(panels[idx], tag, true); // as static object
1010 end;
1011 end;
1013 begin
1014 gMapGrid.Free();
1015 gMapGrid := nil;
1016 mapTree.Free();
1017 mapTree := nil;
1019 fixMinMax(gWalls);
1020 fixMinMax(gRenderBackgrounds);
1021 fixMinMax(gRenderForegrounds);
1022 fixMinMax(gWater);
1023 fixMinMax(gAcid1);
1024 fixMinMax(gAcid2);
1025 fixMinMax(gSteps);
1026 fixMinMax(gLifts);
1027 fixMinMax(gBlockMon);
1029 if (mapX0 < 0) or (mapY0 < 0) then
1030 begin
1031 e_WriteLog(Format('funny map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1032 //raise Exception.Create('we are fucked');
1033 end;
1035 gMapGrid := TBodyGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1036 mapTree := TDynAABBTreeMap.Create();
1038 addPanelsToGrid(gWalls, PANEL_WALL); // and PANEL_CLOSEDOOR
1039 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1040 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1041 addPanelsToGrid(gWater, PANEL_WATER);
1042 addPanelsToGrid(gAcid1, PANEL_ACID1);
1043 addPanelsToGrid(gAcid2, PANEL_ACID2);
1044 addPanelsToGrid(gSteps, PANEL_STEP);
1045 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1046 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1048 gMapGrid.dumpStats();
1049 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1050 mapTree.forEachLeaf(nil);
1051 end;
1053 function g_Map_Load(Res: String): Boolean;
1054 const
1055 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1056 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1057 var
1058 WAD: TWADFile;
1059 MapReader: TMapReader_1;
1060 Header: TMapHeaderRec_1;
1061 _textures: TTexturesRec1Array;
1062 _texnummap: array of Integer; // `_textures` -> `Textures`
1063 panels: TPanelsRec1Array;
1064 items: TItemsRec1Array;
1065 monsters: TMonsterRec1Array;
1066 areas: TAreasRec1Array;
1067 triggers: TTriggersRec1Array;
1068 a, b, c, k: Integer;
1069 PanelID: DWORD;
1070 AddTextures: TAddTextureArray;
1071 texture: TTextureRec_1;
1072 TriggersTable: Array of record
1073 TexturePanel: Integer;
1074 LiftPanel: Integer;
1075 DoorPanel: Integer;
1076 ShotPanel: Integer;
1077 end;
1078 FileName, mapResName, s, TexName: String;
1079 Data: Pointer;
1080 Len: Integer;
1081 ok, isAnim, trigRef: Boolean;
1082 CurTex, ntn: Integer;
1084 begin
1085 gMapGrid.Free();
1086 gMapGrid := nil;
1087 mapTree.Free();
1088 mapTree := nil;
1090 Result := False;
1091 gMapInfo.Map := Res;
1092 TriggersTable := nil;
1093 FillChar(texture, SizeOf(texture), 0);
1095 sfsGCDisable(); // temporary disable removing of temporary volumes
1096 try
1097 // Çàãðóçêà WAD:
1098 FileName := g_ExtractWadName(Res);
1099 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1100 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1102 WAD := TWADFile.Create();
1103 if not WAD.ReadFile(FileName) then
1104 begin
1105 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1106 WAD.Free();
1107 Exit;
1108 end;
1109 //k8: why loader ignores path here?
1110 mapResName := g_ExtractFileName(Res);
1111 if not WAD.GetMapResource(mapResName, Data, Len) then
1112 begin
1113 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1114 WAD.Free();
1115 Exit;
1116 end;
1118 WAD.Free();
1120 // Çàãðóçêà êàðòû:
1121 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1122 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1123 MapReader := TMapReader_1.Create();
1125 if not MapReader.LoadMap(Data) then
1126 begin
1127 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1128 FreeMem(Data);
1129 MapReader.Free();
1130 Exit;
1131 end;
1133 FreeMem(Data);
1134 generateExternalResourcesList(MapReader);
1135 // Çàãðóçêà òåêñòóð:
1136 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1137 _textures := MapReader.GetTextures();
1138 _texnummap := nil;
1140 // Äîáàâëåíèå òåêñòóð â Textures[]:
1141 if _textures <> nil then
1142 begin
1143 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1144 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1145 SetLength(_texnummap, length(_textures));
1147 for a := 0 to High(_textures) do
1148 begin
1149 SetLength(s, 64);
1150 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1151 for b := 1 to Length(s) do
1152 if s[b] = #0 then
1153 begin
1154 SetLength(s, b-1);
1155 Break;
1156 end;
1157 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1158 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1159 // Àíèìèðîâàííàÿ òåêñòóðà:
1160 if ByteBool(_textures[a].Anim) then
1161 begin
1162 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1163 if ntn < 0 then
1164 begin
1165 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1166 ntn := CreateNullTexture(_textures[a].Resource);
1167 end;
1168 end
1169 else // Îáû÷íàÿ òåêñòóðà:
1170 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1171 if ntn < 0 then
1172 begin
1173 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1174 ntn := CreateNullTexture(_textures[a].Resource);
1175 end;
1177 _texnummap[a] := ntn; // fix texture number
1178 g_Game_StepLoading();
1179 end;
1180 end;
1182 // Çàãðóçêà òðèããåðîâ:
1183 gTriggerClientID := 0;
1184 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1185 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1186 triggers := MapReader.GetTriggers();
1188 // Çàãðóçêà ïàíåëåé:
1189 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1190 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1191 panels := MapReader.GetPanels();
1193 // check texture numbers for panels
1194 for a := 0 to High(panels) do
1195 begin
1196 if panels[a].TextureNum > High(_textures) then
1197 begin
1198 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1199 result := false;
1200 exit;
1201 end;
1202 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1203 end;
1205 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1206 if triggers <> nil then
1207 begin
1208 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1209 SetLength(TriggersTable, Length(triggers));
1210 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1212 for a := 0 to High(TriggersTable) do
1213 begin
1214 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1215 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1216 // Ëèôòû:
1217 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1218 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1219 else
1220 TriggersTable[a].LiftPanel := -1;
1221 // Äâåðè:
1222 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1223 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1224 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1225 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1226 else
1227 TriggersTable[a].DoorPanel := -1;
1228 // Òóðåëü:
1229 if triggers[a].TriggerType = TRIGGER_SHOT then
1230 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1231 else
1232 TriggersTable[a].ShotPanel := -1;
1234 g_Game_StepLoading();
1235 end;
1236 end;
1238 // Ñîçäàåì ïàíåëè:
1239 if panels <> nil then
1240 begin
1241 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1242 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1244 for a := 0 to High(panels) do
1245 begin
1246 SetLength(AddTextures, 0);
1247 trigRef := False;
1248 CurTex := -1;
1249 if _textures <> nil then
1250 begin
1251 texture := _textures[panels[a].TextureNum];
1252 ok := True;
1253 end
1254 else
1255 ok := False;
1257 if ok then
1258 begin
1259 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1260 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1261 ok := False;
1262 if (TriggersTable <> nil) and (_textures <> nil) then
1263 for b := 0 to High(TriggersTable) do
1264 if (TriggersTable[b].TexturePanel = a)
1265 or (TriggersTable[b].ShotPanel = a) then
1266 begin
1267 trigRef := True;
1268 ok := True;
1269 Break;
1270 end;
1271 end;
1273 if ok then
1274 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1275 SetLength(s, 64);
1276 CopyMemory(@s[1], @texture.Resource[0], 64);
1277 // Èçìåðÿåì äëèíó:
1278 Len := Length(s);
1279 for c := Len downto 1 do
1280 if s[c] <> #0 then
1281 begin
1282 Len := c;
1283 Break;
1284 end;
1285 SetLength(s, Len);
1287 // Ñïåö-òåêñòóðû çàïðåùåíû:
1288 if g_Map_IsSpecialTexture(s) then
1289 ok := False
1290 else
1291 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1292 ok := g_Texture_NumNameFindStart(s);
1294 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1295 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1296 if ok then
1297 begin
1298 k := NNF_NAME_BEFORE;
1299 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1300 while ok or (k = NNF_NAME_BEFORE) or
1301 (k = NNF_NAME_EQUALS) do
1302 begin
1303 k := g_Texture_NumNameFindNext(TexName);
1305 if (k = NNF_NAME_BEFORE) or
1306 (k = NNF_NAME_AFTER) then
1307 begin
1308 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1309 if ByteBool(texture.Anim) then
1310 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1311 isAnim := True;
1312 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1313 if not ok then
1314 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1315 isAnim := False;
1316 ok := CreateTexture(TexName, FileName, False) >= 0;
1317 end;
1318 end
1319 else
1320 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1321 isAnim := False;
1322 ok := CreateTexture(TexName, FileName, False) >= 0;
1323 if not ok then
1324 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1325 isAnim := True;
1326 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1327 end;
1328 end;
1330 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1331 if ok then
1332 begin
1333 for c := 0 to High(Textures) do
1334 if Textures[c].TextureName = TexName then
1335 begin
1336 SetLength(AddTextures, Length(AddTextures)+1);
1337 AddTextures[High(AddTextures)].Texture := c;
1338 AddTextures[High(AddTextures)].Anim := isAnim;
1339 Break;
1340 end;
1341 end;
1342 end
1343 else
1344 if k = NNF_NAME_EQUALS then
1345 begin
1346 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1347 SetLength(AddTextures, Length(AddTextures)+1);
1348 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1349 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1350 CurTex := High(AddTextures);
1351 ok := True;
1352 end
1353 else // NNF_NO_NAME
1354 ok := False;
1355 end; // while ok...
1357 ok := True;
1358 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1359 end; // if ok - ññûëàþòñÿ òðèããåðû
1361 if not ok then
1362 begin
1363 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1364 SetLength(AddTextures, 1);
1365 AddTextures[0].Texture := panels[a].TextureNum;
1366 AddTextures[0].Anim := ByteBool(texture.Anim);
1367 CurTex := 0;
1368 end;
1370 //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);
1372 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1373 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1375 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1376 if TriggersTable <> nil then
1377 for b := 0 to High(TriggersTable) do
1378 begin
1379 // Òðèããåð äâåðè/ëèôòà:
1380 if (TriggersTable[b].LiftPanel = a) or
1381 (TriggersTable[b].DoorPanel = a) then
1382 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1383 // Òðèããåð ñìåíû òåêñòóðû:
1384 if TriggersTable[b].TexturePanel = a then
1385 triggers[b].TexturePanel := PanelID;
1386 // Òðèããåð "Òóðåëü":
1387 if TriggersTable[b].ShotPanel = a then
1388 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1389 end;
1391 g_Game_StepLoading();
1392 end;
1393 end;
1395 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1396 if (triggers <> nil) and not gLoadGameMode then
1397 begin
1398 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1399 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1400 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1401 for a := 0 to High(triggers) do
1402 begin
1403 if triggers[a].TexturePanel <> -1 then
1404 b := panels[TriggersTable[a].TexturePanel].PanelType
1405 else
1406 b := 0;
1407 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1408 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1409 c := panels[TriggersTable[a].ShotPanel].PanelType
1410 else
1411 c := 0;
1412 CreateTrigger(triggers[a], b, c);
1413 end;
1414 end;
1416 // Çàãðóçêà ïðåäìåòîâ:
1417 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1418 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1419 items := MapReader.GetItems();
1421 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1422 if (items <> nil) and not gLoadGameMode then
1423 begin
1424 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1425 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1426 for a := 0 to High(items) do
1427 CreateItem(Items[a]);
1428 end;
1430 // Çàãðóçêà îáëàñòåé:
1431 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1432 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1433 areas := MapReader.GetAreas();
1435 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1436 if areas <> nil then
1437 begin
1438 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1439 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1440 for a := 0 to High(areas) do
1441 CreateArea(areas[a]);
1442 end;
1444 // Çàãðóçêà ìîíñòðîâ:
1445 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1446 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1447 monsters := MapReader.GetMonsters();
1449 gTotalMonsters := 0;
1451 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1452 if (monsters <> nil) and not gLoadGameMode then
1453 begin
1454 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1455 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1456 for a := 0 to High(monsters) do
1457 CreateMonster(monsters[a]);
1458 end;
1460 // Çàãðóçêà îïèñàíèÿ êàðòû:
1461 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1462 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1463 Header := MapReader.GetMapHeader();
1465 MapReader.Free();
1467 with gMapInfo do
1468 begin
1469 Name := Header.MapName;
1470 Description := Header.MapDescription;
1471 Author := Header.MapAuthor;
1472 MusicName := Header.MusicName;
1473 SkyName := Header.SkyName;
1474 Height := Header.Height;
1475 Width := Header.Width;
1476 end;
1478 // Çàãðóçêà íåáà:
1479 if gMapInfo.SkyName <> '' then
1480 begin
1481 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1482 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1483 FileName := g_ExtractWadName(gMapInfo.SkyName);
1485 if FileName <> '' then
1486 FileName := GameDir+'/wads/'+FileName
1487 else
1488 begin
1489 FileName := g_ExtractWadName(Res);
1490 end;
1492 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1493 if g_Texture_CreateWAD(BackID, s) then
1494 begin
1495 g_Game_SetupScreenSize();
1496 end
1497 else
1498 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1499 end;
1501 // Çàãðóçêà ìóçûêè:
1502 ok := False;
1503 if gMapInfo.MusicName <> '' then
1504 begin
1505 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1506 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1507 FileName := g_ExtractWadName(gMapInfo.MusicName);
1509 if FileName <> '' then
1510 FileName := GameDir+'/wads/'+FileName
1511 else
1512 begin
1513 FileName := g_ExtractWadName(Res);
1514 end;
1516 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1517 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1518 ok := True
1519 else
1520 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1521 end;
1523 // Îñòàëüíûå óñòàíâêè:
1524 CreateDoorMap();
1525 CreateLiftMap();
1527 g_Items_Init();
1528 g_Weapon_Init();
1529 g_Monsters_Init();
1531 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1532 if not gLoadGameMode then
1533 g_GFX_Init();
1535 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1536 _textures := nil;
1537 panels := nil;
1538 items := nil;
1539 areas := nil;
1540 triggers := nil;
1541 TriggersTable := nil;
1542 AddTextures := nil;
1544 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1545 if ok and (not gLoadGameMode) then
1546 begin
1547 gMusic.SetByName(gMapInfo.MusicName);
1548 gMusic.Play();
1549 end
1550 else
1551 gMusic.SetByName('');
1552 finally
1553 sfsGCEnable(); // enable releasing unused volumes
1554 end;
1556 e_WriteLog('Creating map grid', MSG_NOTIFY);
1557 mapCreateGrid();
1559 e_WriteLog('Done loading map.', MSG_NOTIFY);
1560 Result := True;
1561 end;
1563 function g_Map_GetMapInfo(Res: String): TMapInfo;
1564 var
1565 WAD: TWADFile;
1566 MapReader: TMapReader_1;
1567 Header: TMapHeaderRec_1;
1568 FileName: String;
1569 Data: Pointer;
1570 Len: Integer;
1571 begin
1572 FillChar(Result, SizeOf(Result), 0);
1573 FileName := g_ExtractWadName(Res);
1575 WAD := TWADFile.Create();
1576 if not WAD.ReadFile(FileName) then
1577 begin
1578 WAD.Free();
1579 Exit;
1580 end;
1582 //k8: it ignores path again
1583 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1584 begin
1585 WAD.Free();
1586 Exit;
1587 end;
1589 WAD.Free();
1591 MapReader := TMapReader_1.Create();
1593 if not MapReader.LoadMap(Data) then
1594 begin
1595 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1596 ZeroMemory(@Header, SizeOf(Header));
1597 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1598 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1599 end
1600 else
1601 begin
1602 Header := MapReader.GetMapHeader();
1603 Result.Name := Header.MapName;
1604 Result.Description := Header.MapDescription;
1605 end;
1607 FreeMem(Data);
1608 MapReader.Free();
1610 Result.Map := Res;
1611 Result.Author := Header.MapAuthor;
1612 Result.Height := Header.Height;
1613 Result.Width := Header.Width;
1614 end;
1616 function g_Map_GetMapsList(WADName: string): SArray;
1617 var
1618 WAD: TWADFile;
1619 a: Integer;
1620 ResList: SArray;
1621 begin
1622 Result := nil;
1623 WAD := TWADFile.Create();
1624 if not WAD.ReadFile(WADName) then
1625 begin
1626 WAD.Free();
1627 Exit;
1628 end;
1629 ResList := WAD.GetMapResources();
1630 if ResList <> nil then
1631 begin
1632 for a := 0 to High(ResList) do
1633 begin
1634 SetLength(Result, Length(Result)+1);
1635 Result[High(Result)] := ResList[a];
1636 end;
1637 end;
1638 WAD.Free();
1639 end;
1641 function g_Map_Exist(Res: string): Boolean;
1642 var
1643 WAD: TWADFile;
1644 FileName, mnn: string;
1645 ResList: SArray;
1646 a: Integer;
1647 begin
1648 Result := False;
1650 FileName := addWadExtension(g_ExtractWadName(Res));
1652 WAD := TWADFile.Create;
1653 if not WAD.ReadFile(FileName) then
1654 begin
1655 WAD.Free();
1656 Exit;
1657 end;
1659 ResList := WAD.GetMapResources();
1660 WAD.Free();
1662 mnn := g_ExtractFileName(Res);
1663 if ResList <> nil then
1664 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1665 begin
1666 Result := True;
1667 Exit;
1668 end;
1669 end;
1671 procedure g_Map_Free();
1672 var
1673 a: Integer;
1675 procedure FreePanelArray(var panels: TPanelArray);
1676 var
1677 i: Integer;
1679 begin
1680 if panels <> nil then
1681 begin
1682 for i := 0 to High(panels) do
1683 panels[i].Free();
1684 panels := nil;
1685 end;
1686 end;
1688 begin
1689 g_GFX_Free();
1690 g_Weapon_Free();
1691 g_Items_Free();
1692 g_Triggers_Free();
1693 g_Monsters_Free();
1695 RespawnPoints := nil;
1696 if FlagPoints[FLAG_RED] <> nil then
1697 begin
1698 Dispose(FlagPoints[FLAG_RED]);
1699 FlagPoints[FLAG_RED] := nil;
1700 end;
1701 if FlagPoints[FLAG_BLUE] <> nil then
1702 begin
1703 Dispose(FlagPoints[FLAG_BLUE]);
1704 FlagPoints[FLAG_BLUE] := nil;
1705 end;
1706 //DOMFlagPoints := nil;
1708 //gDOMFlags := nil;
1710 if Textures <> nil then
1711 begin
1712 for a := 0 to High(Textures) do
1713 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1714 if Textures[a].Anim then
1715 g_Frames_DeleteByID(Textures[a].FramesID)
1716 else
1717 if Textures[a].TextureID <> TEXTURE_NONE then
1718 e_DeleteTexture(Textures[a].TextureID);
1720 Textures := nil;
1721 end;
1723 FreePanelArray(gWalls);
1724 FreePanelArray(gRenderBackgrounds);
1725 FreePanelArray(gRenderForegrounds);
1726 FreePanelArray(gWater);
1727 FreePanelArray(gAcid1);
1728 FreePanelArray(gAcid2);
1729 FreePanelArray(gSteps);
1730 FreePanelArray(gLifts);
1731 FreePanelArray(gBlockMon);
1733 if BackID <> DWORD(-1) then
1734 begin
1735 gBackSize.X := 0;
1736 gBackSize.Y := 0;
1737 e_DeleteTexture(BackID);
1738 BackID := DWORD(-1);
1739 end;
1741 g_Game_StopAllSounds(False);
1742 gMusic.FreeSound();
1743 g_Sound_Delete(gMapInfo.MusicName);
1745 gMapInfo.Name := '';
1746 gMapInfo.Description := '';
1747 gMapInfo.MusicName := '';
1748 gMapInfo.Height := 0;
1749 gMapInfo.Width := 0;
1751 gDoorMap := nil;
1752 gLiftMap := nil;
1754 PanelByID := nil;
1755 end;
1757 procedure g_Map_Update();
1758 var
1759 a, d, j: Integer;
1760 m: Word;
1761 s: String;
1763 procedure UpdatePanelArray(var panels: TPanelArray);
1764 var
1765 i: Integer;
1767 begin
1768 if panels <> nil then
1769 for i := 0 to High(panels) do
1770 panels[i].Update();
1771 end;
1773 begin
1774 UpdatePanelArray(gWalls);
1775 UpdatePanelArray(gRenderBackgrounds);
1776 UpdatePanelArray(gRenderForegrounds);
1777 UpdatePanelArray(gWater);
1778 UpdatePanelArray(gAcid1);
1779 UpdatePanelArray(gAcid2);
1780 UpdatePanelArray(gSteps);
1782 if gGameSettings.GameMode = GM_CTF then
1783 begin
1784 for a := FLAG_RED to FLAG_BLUE do
1785 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1786 with gFlags[a] do
1787 begin
1788 if gFlags[a].Animation <> nil then
1789 gFlags[a].Animation.Update();
1791 m := g_Obj_Move(@Obj, True, True);
1793 if gTime mod (GAME_TICK*2) <> 0 then
1794 Continue;
1796 // Ñîïðîòèâëåíèå âîçäóõà:
1797 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1799 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1800 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1801 begin
1802 g_Map_ResetFlag(a);
1803 gFlags[a].CaptureTime := 0;
1804 if a = FLAG_RED then
1805 s := _lc[I_PLAYER_FLAG_RED]
1806 else
1807 s := _lc[I_PLAYER_FLAG_BLUE];
1808 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1810 if g_Game_IsNet then
1811 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1812 Continue;
1813 end;
1815 if Count > 0 then
1816 Count := Count - 1;
1818 // Èãðîê áåðåò ôëàã:
1819 if gPlayers <> nil then
1820 begin
1821 j := Random(Length(gPlayers)) - 1;
1823 for d := 0 to High(gPlayers) do
1824 begin
1825 Inc(j);
1826 if j > High(gPlayers) then
1827 j := 0;
1829 if gPlayers[j] <> nil then
1830 if gPlayers[j].Live and
1831 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1832 begin
1833 if gPlayers[j].GetFlag(a) then
1834 Break;
1835 end;
1836 end;
1837 end;
1838 end;
1839 end;
1840 end;
1843 procedure g_Map_DrawPanelsOld(PanelType: Word);
1845 procedure DrawPanels (stp: Integer; var panels: TPanelArray; drawDoors: Boolean=False);
1846 var
1847 idx: Integer;
1848 begin
1849 if (panels <> nil) and (stp >= 0) and (stp <= 6) then
1850 begin
1851 // alas, no visible set
1852 for idx := 0 to High(panels) do
1853 begin
1854 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1855 end;
1856 end;
1857 end;
1859 begin
1860 case PanelType of
1861 PANEL_WALL: DrawPanels(0, gWalls);
1862 PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True);
1863 PANEL_BACK: DrawPanels(1, gRenderBackgrounds);
1864 PANEL_FORE: DrawPanels(2, gRenderForegrounds);
1865 PANEL_WATER: DrawPanels(3, gWater);
1866 PANEL_ACID1: DrawPanels(4, gAcid1);
1867 PANEL_ACID2: DrawPanels(5, gAcid2);
1868 PANEL_STEP: DrawPanels(6, gSteps);
1869 end;
1870 end;
1873 var
1874 gDrawPanelList: TBinaryHeapObj = nil;
1876 function dplLess (a, b: TObject): Boolean;
1877 begin
1878 result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
1879 end;
1881 procedure dplClear ();
1882 begin
1883 if gDrawPanelList = nil then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
1884 end;
1886 procedure dplAddPanel (pan: TPanel);
1887 begin
1888 if pan = nil then exit;
1889 gDrawPanelList.insert(pan);
1890 end;
1893 procedure g_Map_DrawPanels(x0, y0, wdt, hgt: Integer; PanelType: Word);
1894 var
1895 ptag: Integer;
1897 function checker (obj: TObject; tag: Integer): Boolean;
1898 var
1899 pan: TPanel;
1900 begin
1901 result := false; // don't stop, ever
1902 //e_WriteLog(Format(' *body: tag:%d; ptag:%d; pantype=%d', [tag, ptag, PanelType]), MSG_NOTIFY);
1903 if (tag <> ptag) then exit;
1905 if obj = nil then begin e_WriteLog(Format(' !bodyFUUUUU0: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY); exit; end;
1906 if not (obj is TPanel) then begin e_WriteLog(Format(' !bodyFUUUUU1: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY); exit; end;
1907 //pan := (obj as TPanel);
1908 //e_WriteLog(Format(' !body: (%d,%d)-(%dx%d) tag:%d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, tag, PanelType]), MSG_NOTIFY);
1910 pan := (obj as TPanel);
1911 if (PanelType = PANEL_CLOSEDOOR) then begin if not pan.Door then exit; end else begin if pan.Door then exit; end;
1912 //e_WriteLog(Format(' body hit: (%d,%d)-(%dx%d) tag: %d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, tag, PanelType]), MSG_NOTIFY);
1913 dplAddPanel(pan);
1914 end;
1916 procedure DrawPanels (stp: Integer; var panels: TPanelArray; drawDoors: Boolean=False);
1917 var
1918 idx: Integer;
1919 pan: TPanel;
1920 begin
1921 if (panels <> nil) and (stp >= 0) and (stp <= 6) then
1922 begin
1923 // alas, no visible set
1924 for idx := 0 to High(panels) do
1925 begin
1926 if not (drawDoors xor panels[idx].Door) then
1927 begin
1928 pan := panels[idx];
1930 if (pan.Width < 1) or (pan.Height < 1) then continue;
1931 if (pan.X+pan.Width <= x0) or (pan.Y+pan.Height <= y0) then continue;
1932 if (pan.X >= x0+wdt) or (pan.Y >= y0+hgt) then continue;
1934 pan.Draw();
1935 //e_WriteLog(Format(' *body hit: (%d,%d)-(%dx%d) tag: %d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, PanelType, PanelType]), MSG_NOTIFY);
1936 end;
1937 end;
1938 end;
1939 end;
1941 begin
1942 //g_Map_DrawPanelsOld(PanelType); exit;
1943 //e_WriteLog('==================', MSG_NOTIFY);
1944 //e_WriteLog(Format('***QQQ: qtag:%d', [PanelType]), MSG_NOTIFY);
1945 dplClear();
1946 ptag := panelTypeToTag(PanelType);
1948 if gdbg_map_use_grid_render then
1949 begin
1950 if gdbg_map_use_tree_draw then
1951 begin
1952 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, ptag);
1953 end
1954 else
1955 begin
1956 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker);
1957 end;
1958 // sort and draw the list (we need to sort it, or rendering is fucked)
1959 while gDrawPanelList.count > 0 do
1960 begin
1961 (gDrawPanelList.front() as TPanel).Draw();
1962 gDrawPanelList.popFront();
1963 end;
1964 end
1965 else
1966 begin
1967 //e_WriteLog(Format('+++QQQ: qtag:%d', [PanelType]), MSG_NOTIFY);
1968 case PanelType of
1969 PANEL_WALL: DrawPanels(0, gWalls);
1970 PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True);
1971 PANEL_BACK: DrawPanels(1, gRenderBackgrounds);
1972 PANEL_FORE: DrawPanels(2, gRenderForegrounds);
1973 PANEL_WATER: DrawPanels(3, gWater);
1974 PANEL_ACID1: DrawPanels(4, gAcid1);
1975 PANEL_ACID2: DrawPanels(5, gAcid2);
1976 PANEL_STEP: DrawPanels(6, gSteps);
1977 end;
1978 end;
1980 //e_WriteLog('==================', MSG_NOTIFY);
1981 end;
1984 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
1985 function checker (obj: TObject; tag: Integer): Boolean;
1986 var
1987 pan: TPanel;
1988 begin
1989 result := false; // don't stop, ever
1990 if (tag <> GridTagWallDoor) then exit; // only walls
1991 pan := (obj as TPanel);
1992 pan.DrawShadowVolume(lightX, lightY, radius);
1993 end;
1995 begin
1996 if gdbg_map_use_tree_draw then
1997 begin
1998 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, GridTagWallDoor);
1999 end
2000 else
2001 begin
2002 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker);
2003 end;
2004 end;
2007 procedure g_Map_DrawBack(dx, dy: Integer);
2008 begin
2009 if gDrawBackGround and (BackID <> DWORD(-1)) then
2010 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2011 else
2012 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2013 end;
2015 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2016 PanelType: Word; b1x3: Boolean): Boolean;
2017 var
2018 a, h: Integer;
2019 begin
2020 Result := False;
2022 if WordBool(PanelType and PANEL_WALL) then
2023 if gWalls <> nil then
2024 begin
2025 h := High(gWalls);
2027 for a := 0 to h do
2028 if gWalls[a].Enabled and
2029 g_Collide(X, Y, Width, Height,
2030 gWalls[a].X, gWalls[a].Y,
2031 gWalls[a].Width, gWalls[a].Height) then
2032 begin
2033 Result := True;
2034 Exit;
2035 end;
2036 end;
2038 if WordBool(PanelType and PANEL_WATER) then
2039 if gWater <> nil then
2040 begin
2041 h := High(gWater);
2043 for a := 0 to h do
2044 if g_Collide(X, Y, Width, Height,
2045 gWater[a].X, gWater[a].Y,
2046 gWater[a].Width, gWater[a].Height) then
2047 begin
2048 Result := True;
2049 Exit;
2050 end;
2051 end;
2053 if WordBool(PanelType and PANEL_ACID1) then
2054 if gAcid1 <> nil then
2055 begin
2056 h := High(gAcid1);
2058 for a := 0 to h do
2059 if g_Collide(X, Y, Width, Height,
2060 gAcid1[a].X, gAcid1[a].Y,
2061 gAcid1[a].Width, gAcid1[a].Height) then
2062 begin
2063 Result := True;
2064 Exit;
2065 end;
2066 end;
2068 if WordBool(PanelType and PANEL_ACID2) then
2069 if gAcid2 <> nil then
2070 begin
2071 h := High(gAcid2);
2073 for a := 0 to h do
2074 if g_Collide(X, Y, Width, Height,
2075 gAcid2[a].X, gAcid2[a].Y,
2076 gAcid2[a].Width, gAcid2[a].Height) then
2077 begin
2078 Result := True;
2079 Exit;
2080 end;
2081 end;
2083 if WordBool(PanelType and PANEL_STEP) then
2084 if gSteps <> nil then
2085 begin
2086 h := High(gSteps);
2088 for a := 0 to h do
2089 if g_Collide(X, Y, Width, Height,
2090 gSteps[a].X, gSteps[a].Y,
2091 gSteps[a].Width, gSteps[a].Height) then
2092 begin
2093 Result := True;
2094 Exit;
2095 end;
2096 end;
2098 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2099 if gLifts <> nil then
2100 begin
2101 h := High(gLifts);
2103 for a := 0 to h do
2104 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2105 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2106 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2107 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2108 g_Collide(X, Y, Width, Height,
2109 gLifts[a].X, gLifts[a].Y,
2110 gLifts[a].Width, gLifts[a].Height) then
2111 begin
2112 Result := True;
2113 Exit;
2114 end;
2115 end;
2117 if WordBool(PanelType and PANEL_BLOCKMON) then
2118 if gBlockMon <> nil then
2119 begin
2120 h := High(gBlockMon);
2122 for a := 0 to h do
2123 if ( (not b1x3) or
2124 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2125 g_Collide(X, Y, Width, Height,
2126 gBlockMon[a].X, gBlockMon[a].Y,
2127 gBlockMon[a].Width, gBlockMon[a].Height) then
2128 begin
2129 Result := True;
2130 Exit;
2131 end;
2132 end;
2133 end;
2135 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2136 var
2137 texid: DWORD;
2139 function checkPanels (var panels: TPanelArray): Boolean;
2140 var
2141 a: Integer;
2142 begin
2143 result := false;
2144 if panels = nil then exit;
2145 for a := 0 to High(panels) do
2146 begin
2147 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2148 begin
2149 result := true;
2150 texid := panels[a].GetTextureID();
2151 exit;
2152 end;
2153 end;
2154 end;
2156 begin
2157 texid := TEXTURE_NONE;
2158 result := texid;
2159 if not checkPanels(gWater) then
2160 if not checkPanels(gAcid1) then
2161 if not checkPanels(gAcid2) then exit;
2162 result := texid;
2163 end;
2166 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2168 function checker (obj: TObject; tag: Integer): Boolean;
2169 var
2170 pan: TPanel;
2171 a: Integer;
2172 begin
2173 result := false; // don't stop, ever
2175 //e_WriteLog(Format(' *body: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
2177 if obj = nil then
2178 begin
2179 e_WriteLog(Format(' !bodyFUUUUU0: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
2180 end;
2181 if not (obj is TPanel) then
2182 begin
2183 e_WriteLog(Format(' !bodyFUUUUU1: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
2184 exit;
2185 end;
2187 pan := (obj as TPanel);
2188 a := pan.ArrIdx;
2190 if WordBool(PanelType and PANEL_WALL) and (tag = GridTagWallDoor) then
2191 begin
2192 if gWalls[a].Enabled and g_Collide(X, Y, Width, Height, gWalls[a].X, gWalls[a].Y, gWalls[a].Width, gWalls[a].Height) then
2193 begin
2194 result := true;
2195 exit;
2196 end;
2197 end;
2199 if WordBool(PanelType and PANEL_WATER) and (tag = GridTagWater) then
2200 begin
2201 if g_Collide(X, Y, Width, Height, gWater[a].X, gWater[a].Y, gWater[a].Width, gWater[a].Height) then
2202 begin
2203 result := True;
2204 exit;
2205 end;
2206 end;
2208 if WordBool(PanelType and PANEL_ACID1) and (tag = GridTagAcid1) then
2209 begin
2210 if g_Collide(X, Y, Width, Height, gAcid1[a].X, gAcid1[a].Y, gAcid1[a].Width, gAcid1[a].Height) then
2211 begin
2212 result := True;
2213 exit;
2214 end;
2215 end;
2217 if WordBool(PanelType and PANEL_ACID2) and (tag = GridTagAcid2) then
2218 begin
2219 if g_Collide(X, Y, Width, Height, gAcid2[a].X, gAcid2[a].Y, gAcid2[a].Width, gAcid2[a].Height) then
2220 begin
2221 result := True;
2222 exit;
2223 end;
2224 end;
2226 if WordBool(PanelType and PANEL_STEP) and (tag = GridTagStep) then
2227 begin
2228 if g_Collide(X, Y, Width, Height, gSteps[a].X, gSteps[a].Y, gSteps[a].Width, gSteps[a].Height) then
2229 begin
2230 result := True;
2231 exit;
2232 end;
2233 end;
2235 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) and (tag = GridTagLift) then
2236 begin
2237 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2238 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2239 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2240 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2241 g_Collide(X, Y, Width, Height, gLifts[a].X, gLifts[a].Y, gLifts[a].Width, gLifts[a].Height) then
2242 begin
2243 result := true;
2244 exit;
2245 end;
2246 end;
2248 if WordBool(PanelType and PANEL_BLOCKMON) and (tag = GridTagBlockMon) then
2249 begin
2250 if ((not b1x3) or ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64)) and
2251 g_Collide(X, Y, Width, Height, gBlockMon[a].X, gBlockMon[a].Y, gBlockMon[a].Width, gBlockMon[a].Height) then
2252 begin
2253 result := True;
2254 exit;
2255 end;
2256 end;
2257 end;
2259 begin
2260 //TODO: detailed profile
2261 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('wall coldet');
2262 try
2263 if gdbg_map_use_grid_coldet then
2264 begin
2265 if gdbg_map_use_tree_coldet then
2266 begin
2267 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWallDoor or GridTagWater or GridTagAcid1 or GridTagAcid2 or GridTagStep or GridTagLift or GridTagBlockMon)) <> nil);
2268 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2269 begin
2270 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2271 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2272 end;
2273 end
2274 else
2275 begin
2276 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
2277 end;
2278 end
2279 else
2280 begin
2281 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2282 end;
2283 finally
2284 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2285 end;
2286 end;
2289 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2290 var
2291 cctype: Integer = 3; // priority: 0: water, 1: acid1, 2: acid2; 3: others (nothing)
2292 texid: DWORD;
2294 // slightly different from the old code, but meh...
2295 function checker (obj: TObject; tag: Integer): Boolean;
2296 var
2297 pan: TPanel;
2298 a: Integer;
2299 begin
2300 result := false; // don't stop, ever
2301 if (tag <> GridTagWater) and (tag <> GridTagAcid1) and (tag <> GridTagAcid2) then exit;
2302 pan := (obj as TPanel);
2303 a := pan.ArrIdx;
2304 // water
2305 if (tag = GridTagWater) then
2306 begin
2307 if g_Collide(X, Y, Width, Height, gWater[a].X, gWater[a].Y, gWater[a].Width, gWater[a].Height) then
2308 begin
2309 result := true; // water has highest priority, so stop right here
2310 texid := gWater[a].GetTextureID();
2311 exit;
2312 end;
2313 end;
2314 // acid1
2315 if (cctype > 1) and (tag = GridTagAcid1) then
2316 begin
2317 if g_Collide(X, Y, Width, Height, gAcid1[a].X, gAcid1[a].Y, gAcid1[a].Width, gAcid1[a].Height) then
2318 begin
2319 cctype := 1;
2320 texid := gAcid1[a].GetTextureID();
2321 exit;
2322 end;
2323 end;
2324 // acid2
2325 if (cctype > 2) and (tag = GridTagAcid2) then
2326 begin
2327 if g_Collide(X, Y, Width, Height, gAcid2[a].X, gAcid2[a].Y, gAcid2[a].Width, gAcid2[a].Height) then
2328 begin
2329 cctype := 2;
2330 texid := gAcid2[a].GetTextureID();
2331 exit;
2332 end;
2333 end;
2334 end;
2336 {var
2337 cctype1: Integer = 3; // priority: 0: water, 1: acid1, 2: acid2; 3: others (nothing)
2338 texid1: DWORD;}
2339 begin
2340 //TODO: detailed profile?
2341 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquid coldet');
2342 try
2343 if gdbg_map_use_grid_coldet then
2344 begin
2345 texid := TEXTURE_NONE;
2346 if gdbg_map_use_tree_coldet then
2347 begin
2348 mapTree.aabbQuery(X, Y, Width, Height, checker);
2350 cctype1 := cctype;
2351 texid1 := texid;
2352 cctype := 3;
2353 texid := TEXTURE_NONE;
2354 gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
2355 if (cctype1 <> cctype) or (texid1 <> texid) then
2356 begin
2357 e_WriteLog(Format('g_Map_CollideLiquid_Texture(%d, %d, %u, %u): tree(cctype:%d;texid:%u); grid(cctype:%d;texid:%u)', [X, Y, Width, Height, cctype1, texid1, cctype, texid]), MSG_WARNING);
2358 end;}
2359 end
2360 else
2361 begin
2362 gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
2363 end;
2364 result := texid;
2365 end
2366 else
2367 begin
2368 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2369 end;
2370 finally
2371 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2372 end;
2373 end;
2375 procedure g_Map_EnableWall(ID: DWORD);
2376 begin
2377 with gWalls[ID] do
2378 begin
2379 Enabled := True;
2380 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2382 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2383 end;
2384 end;
2386 procedure g_Map_DisableWall(ID: DWORD);
2387 begin
2388 with gWalls[ID] do
2389 begin
2390 Enabled := False;
2391 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2393 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2394 end;
2395 end;
2397 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2398 var
2399 tp: TPanel;
2400 begin
2401 case PanelType of
2402 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2403 tp := gWalls[ID];
2404 PANEL_FORE:
2405 tp := gRenderForegrounds[ID];
2406 PANEL_BACK:
2407 tp := gRenderBackgrounds[ID];
2408 PANEL_WATER:
2409 tp := gWater[ID];
2410 PANEL_ACID1:
2411 tp := gAcid1[ID];
2412 PANEL_ACID2:
2413 tp := gAcid2[ID];
2414 PANEL_STEP:
2415 tp := gSteps[ID];
2416 else
2417 Exit;
2418 end;
2420 tp.NextTexture(AnimLoop);
2421 if g_Game_IsServer and g_Game_IsNet then
2422 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2423 end;
2425 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2426 begin
2427 if gLifts[ID].LiftType = t then
2428 Exit;
2430 with gLifts[ID] do
2431 begin
2432 LiftType := t;
2434 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2436 if LiftType = 0 then
2437 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2438 else if LiftType = 1 then
2439 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2440 else if LiftType = 2 then
2441 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2442 else if LiftType = 3 then
2443 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2445 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2446 end;
2447 end;
2449 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2450 var
2451 a: Integer;
2452 PointsArray: Array of TRespawnPoint;
2453 begin
2454 Result := False;
2455 SetLength(PointsArray, 0);
2457 if RespawnPoints = nil then
2458 Exit;
2460 for a := 0 to High(RespawnPoints) do
2461 if RespawnPoints[a].PointType = PointType then
2462 begin
2463 SetLength(PointsArray, Length(PointsArray)+1);
2464 PointsArray[High(PointsArray)] := RespawnPoints[a];
2465 end;
2467 if PointsArray = nil then
2468 Exit;
2470 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2471 Result := True;
2472 end;
2474 function g_Map_GetPointCount(PointType: Byte): Word;
2475 var
2476 a: Integer;
2477 begin
2478 Result := 0;
2480 if RespawnPoints = nil then
2481 Exit;
2483 for a := 0 to High(RespawnPoints) do
2484 if RespawnPoints[a].PointType = PointType then
2485 Result := Result + 1;
2486 end;
2488 function g_Map_HaveFlagPoints(): Boolean;
2489 begin
2490 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2491 end;
2493 procedure g_Map_ResetFlag(Flag: Byte);
2494 begin
2495 with gFlags[Flag] do
2496 begin
2497 Obj.X := -1000;
2498 Obj.Y := -1000;
2499 Obj.Vel.X := 0;
2500 Obj.Vel.Y := 0;
2501 Direction := D_LEFT;
2502 State := FLAG_STATE_NONE;
2503 if FlagPoints[Flag] <> nil then
2504 begin
2505 Obj.X := FlagPoints[Flag]^.X;
2506 Obj.Y := FlagPoints[Flag]^.Y;
2507 Direction := FlagPoints[Flag]^.Direction;
2508 State := FLAG_STATE_NORMAL;
2509 end;
2510 Count := -1;
2511 end;
2512 end;
2514 procedure g_Map_DrawFlags();
2515 var
2516 i, dx: Integer;
2517 Mirror: TMirrorType;
2518 begin
2519 if gGameSettings.GameMode <> GM_CTF then
2520 Exit;
2522 for i := FLAG_RED to FLAG_BLUE do
2523 with gFlags[i] do
2524 if State <> FLAG_STATE_CAPTURED then
2525 begin
2526 if State = FLAG_STATE_NONE then
2527 continue;
2529 if Direction = D_LEFT then
2530 begin
2531 Mirror := M_HORIZONTAL;
2532 dx := -1;
2533 end
2534 else
2535 begin
2536 Mirror := M_NONE;
2537 dx := 1;
2538 end;
2540 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2542 if g_debug_Frames then
2543 begin
2544 e_DrawQuad(Obj.X+Obj.Rect.X,
2545 Obj.Y+Obj.Rect.Y,
2546 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2547 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2548 0, 255, 0);
2549 end;
2550 end;
2551 end;
2553 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2554 var
2555 dw: DWORD;
2556 b: Byte;
2557 str: String;
2558 boo: Boolean;
2560 procedure SavePanelArray(var panels: TPanelArray);
2561 var
2562 PAMem: TBinMemoryWriter;
2563 i: Integer;
2564 begin
2565 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2566 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2568 i := 0;
2569 while i < Length(panels) do
2570 begin
2571 if panels[i].SaveIt then
2572 begin
2573 // ID ïàíåëè:
2574 PAMem.WriteInt(i);
2575 // Ñîõðàíÿåì ïàíåëü:
2576 panels[i].SaveState(PAMem);
2577 end;
2578 Inc(i);
2579 end;
2581 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2582 PAMem.SaveToMemory(Mem);
2583 PAMem.Free();
2584 end;
2586 procedure SaveFlag(flag: PFlag);
2587 begin
2588 // Ñèãíàòóðà ôëàãà:
2589 dw := FLAG_SIGNATURE; // 'FLAG'
2590 Mem.WriteDWORD(dw);
2591 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2592 Mem.WriteByte(flag^.RespawnType);
2593 // Ñîñòîÿíèå ôëàãà:
2594 Mem.WriteByte(flag^.State);
2595 // Íàïðàâëåíèå ôëàãà:
2596 if flag^.Direction = D_LEFT then
2597 b := 1
2598 else // D_RIGHT
2599 b := 2;
2600 Mem.WriteByte(b);
2601 // Îáúåêò ôëàãà:
2602 Obj_SaveState(@flag^.Obj, Mem);
2603 end;
2605 begin
2606 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2608 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2609 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2610 SavePanelArray(gWalls);
2611 // Ñîõðàíÿåì ïàíåëè ôîíà:
2612 SavePanelArray(gRenderBackgrounds);
2613 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2614 SavePanelArray(gRenderForegrounds);
2615 // Ñîõðàíÿåì ïàíåëè âîäû:
2616 SavePanelArray(gWater);
2617 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2618 SavePanelArray(gAcid1);
2619 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2620 SavePanelArray(gAcid2);
2621 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2622 SavePanelArray(gSteps);
2623 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2624 SavePanelArray(gLifts);
2625 ///// /////
2627 ///// Ñîõðàíÿåì ìóçûêó: /////
2628 // Ñèãíàòóðà ìóçûêè:
2629 dw := MUSIC_SIGNATURE; // 'MUSI'
2630 Mem.WriteDWORD(dw);
2631 // Íàçâàíèå ìóçûêè:
2632 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2633 if gMusic.NoMusic then
2634 str := ''
2635 else
2636 str := gMusic.Name;
2637 Mem.WriteString(str, 64);
2638 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2639 dw := gMusic.GetPosition();
2640 Mem.WriteDWORD(dw);
2641 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2642 boo := gMusic.SpecPause;
2643 Mem.WriteBoolean(boo);
2644 ///// /////
2646 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2647 Mem.WriteInt(gTotalMonsters);
2648 ///// /////
2650 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2651 if gGameSettings.GameMode = GM_CTF then
2652 begin
2653 // Ôëàã Êðàñíîé êîìàíäû:
2654 SaveFlag(@gFlags[FLAG_RED]);
2655 // Ôëàã Ñèíåé êîìàíäû:
2656 SaveFlag(@gFlags[FLAG_BLUE]);
2657 end;
2658 ///// /////
2660 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2661 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2662 begin
2663 // Î÷êè Êðàñíîé êîìàíäû:
2664 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2665 // Î÷êè Ñèíåé êîìàíäû:
2666 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2667 end;
2668 ///// /////
2669 end;
2671 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2672 var
2673 dw: DWORD;
2674 b: Byte;
2675 str: String;
2676 boo: Boolean;
2678 procedure LoadPanelArray(var panels: TPanelArray);
2679 var
2680 PAMem: TBinMemoryReader;
2681 i, id: Integer;
2682 begin
2683 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2684 PAMem := TBinMemoryReader.Create();
2685 PAMem.LoadFromMemory(Mem);
2687 for i := 0 to Length(panels)-1 do
2688 if panels[i].SaveIt then
2689 begin
2690 // ID ïàíåëè:
2691 PAMem.ReadInt(id);
2692 if id <> i then
2693 begin
2694 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2695 end;
2696 // Çàãðóæàåì ïàíåëü:
2697 panels[i].LoadState(PAMem);
2698 end;
2700 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2701 PAMem.Free();
2702 end;
2704 procedure LoadFlag(flag: PFlag);
2705 begin
2706 // Ñèãíàòóðà ôëàãà:
2707 Mem.ReadDWORD(dw);
2708 if dw <> FLAG_SIGNATURE then // 'FLAG'
2709 begin
2710 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2711 end;
2712 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2713 Mem.ReadByte(flag^.RespawnType);
2714 // Ñîñòîÿíèå ôëàãà:
2715 Mem.ReadByte(flag^.State);
2716 // Íàïðàâëåíèå ôëàãà:
2717 Mem.ReadByte(b);
2718 if b = 1 then
2719 flag^.Direction := D_LEFT
2720 else // b = 2
2721 flag^.Direction := D_RIGHT;
2722 // Îáúåêò ôëàãà:
2723 Obj_LoadState(@flag^.Obj, Mem);
2724 end;
2726 begin
2727 if Mem = nil then
2728 Exit;
2730 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2731 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2732 LoadPanelArray(gWalls);
2733 // Çàãðóæàåì ïàíåëè ôîíà:
2734 LoadPanelArray(gRenderBackgrounds);
2735 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2736 LoadPanelArray(gRenderForegrounds);
2737 // Çàãðóæàåì ïàíåëè âîäû:
2738 LoadPanelArray(gWater);
2739 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2740 LoadPanelArray(gAcid1);
2741 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2742 LoadPanelArray(gAcid2);
2743 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2744 LoadPanelArray(gSteps);
2745 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2746 LoadPanelArray(gLifts);
2747 ///// /////
2749 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2750 g_GFX_Init();
2751 mapCreateGrid();
2753 ///// Çàãðóæàåì ìóçûêó: /////
2754 // Ñèãíàòóðà ìóçûêè:
2755 Mem.ReadDWORD(dw);
2756 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2757 begin
2758 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2759 end;
2760 // Íàçâàíèå ìóçûêè:
2761 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2762 Mem.ReadString(str);
2763 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2764 Mem.ReadDWORD(dw);
2765 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2766 Mem.ReadBoolean(boo);
2767 // Çàïóñêàåì ýòó ìóçûêó:
2768 gMusic.SetByName(str);
2769 gMusic.SpecPause := boo;
2770 gMusic.Play();
2771 gMusic.Pause(True);
2772 gMusic.SetPosition(dw);
2773 ///// /////
2775 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2776 Mem.ReadInt(gTotalMonsters);
2777 ///// /////
2779 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2780 if gGameSettings.GameMode = GM_CTF then
2781 begin
2782 // Ôëàã Êðàñíîé êîìàíäû:
2783 LoadFlag(@gFlags[FLAG_RED]);
2784 // Ôëàã Ñèíåé êîìàíäû:
2785 LoadFlag(@gFlags[FLAG_BLUE]);
2786 end;
2787 ///// /////
2789 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2790 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2791 begin
2792 // Î÷êè Êðàñíîé êîìàíäû:
2793 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2794 // Î÷êè Ñèíåé êîìàíäû:
2795 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2796 end;
2797 ///// /////
2798 end;
2800 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2801 var
2802 Arr: TPanelArray;
2803 begin
2804 Result := nil;
2805 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2806 Arr := PanelByID[PanelID].PWhere^;
2807 PanelArrayID := PanelByID[PanelID].PArrID;
2808 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2809 end;
2811 end.