DEADSOFTWARE

experimental grid with buckets
[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_accel_render: Boolean = true;
138 gdbg_map_use_accel_coldet: Boolean = true;
139 gdbg_map_use_tree_draw: Boolean = false;
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 result := false;
205 if (flesh = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
206 pan := (flesh as TPanel);
207 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width, pan.Y+pan.Height);
208 if (pan.Width < 1) or (pan.Height < 1) then exit;
209 //if (pan.Width = 1) then aabb.maxX += 1;
210 //if (pan.Height = 1) then aabb.maxY += 1;
211 //if (pan.Width < 3) or (pan.Height < 3) then exit;
212 //aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-2, pan.Y+pan.Height-2);
213 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
214 result := aabb.valid;
215 end;
217 var
218 PanelById: array of TPanelID;
219 Textures: TLevelTextureArray;
220 RespawnPoints: Array of TRespawnPoint;
221 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
222 //DOMFlagPoints: Array of TFlagPoint;
223 gMapGrid: TBodyGrid = nil;
224 mapTree: TDynAABBTree = nil;
227 procedure g_Map_ProfilersBegin ();
228 begin
229 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('MAP COLLISION', g_profile_history_size);
230 profMapCollision.mainBegin(g_profile_collision);
231 // create sections
232 if g_profile_collision then
233 begin
234 profMapCollision.sectionBegin('wall coldet');
235 profMapCollision.sectionEnd();
236 profMapCollision.sectionBegin('liquid coldet');
237 profMapCollision.sectionEnd();
238 end;
239 end;
241 procedure g_Map_ProfilersEnd ();
242 begin
243 if (profMapCollision <> nil) then profMapCollision.mainEnd();
244 end;
247 function g_Map_IsSpecialTexture(Texture: String): Boolean;
248 begin
249 Result := (Texture = TEXTURE_NAME_WATER) or
250 (Texture = TEXTURE_NAME_ACID1) or
251 (Texture = TEXTURE_NAME_ACID2);
252 end;
254 procedure CreateDoorMap();
255 var
256 PanelArray: Array of record
257 X, Y: Integer;
258 Width, Height: Word;
259 Active: Boolean;
260 PanelID: DWORD;
261 end;
262 a, b, c, m, i, len: Integer;
263 ok: Boolean;
264 begin
265 if gWalls = nil then
266 Exit;
268 i := 0;
269 len := 128;
270 SetLength(PanelArray, len);
272 for a := 0 to High(gWalls) do
273 if gWalls[a].Door then
274 begin
275 PanelArray[i].X := gWalls[a].X;
276 PanelArray[i].Y := gWalls[a].Y;
277 PanelArray[i].Width := gWalls[a].Width;
278 PanelArray[i].Height := gWalls[a].Height;
279 PanelArray[i].Active := True;
280 PanelArray[i].PanelID := a;
282 i := i + 1;
283 if i = len then
284 begin
285 len := len + 128;
286 SetLength(PanelArray, len);
287 end;
288 end;
290 // Íåò äâåðåé:
291 if i = 0 then
292 begin
293 PanelArray := nil;
294 Exit;
295 end;
297 SetLength(gDoorMap, 0);
299 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
301 for a := 0 to i-1 do
302 if PanelArray[a].Active then
303 begin
304 PanelArray[a].Active := False;
305 m := Length(gDoorMap);
306 SetLength(gDoorMap, m+1);
307 SetLength(gDoorMap[m], 1);
308 gDoorMap[m, 0] := PanelArray[a].PanelID;
309 ok := True;
311 while ok do
312 begin
313 ok := False;
315 for b := 0 to i-1 do
316 if PanelArray[b].Active then
317 for c := 0 to High(gDoorMap[m]) do
318 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
319 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
320 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
321 PanelArray[b].Width, PanelArray[b].Height,
322 gWalls[gDoorMap[m, c]].X,
323 gWalls[gDoorMap[m, c]].Y,
324 gWalls[gDoorMap[m, c]].Width,
325 gWalls[gDoorMap[m, c]].Height) then
326 begin
327 PanelArray[b].Active := False;
328 SetLength(gDoorMap[m],
329 Length(gDoorMap[m])+1);
330 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
331 ok := True;
332 Break;
333 end;
334 end;
336 g_Game_StepLoading();
337 end;
339 PanelArray := nil;
340 end;
342 procedure CreateLiftMap();
343 var
344 PanelArray: Array of record
345 X, Y: Integer;
346 Width, Height: Word;
347 Active: Boolean;
348 end;
349 a, b, c, len, i, j: Integer;
350 ok: Boolean;
351 begin
352 if gLifts = nil then
353 Exit;
355 len := Length(gLifts);
356 SetLength(PanelArray, len);
358 for a := 0 to len-1 do
359 begin
360 PanelArray[a].X := gLifts[a].X;
361 PanelArray[a].Y := gLifts[a].Y;
362 PanelArray[a].Width := gLifts[a].Width;
363 PanelArray[a].Height := gLifts[a].Height;
364 PanelArray[a].Active := True;
365 end;
367 SetLength(gLiftMap, len);
368 i := 0;
370 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
372 for a := 0 to len-1 do
373 if PanelArray[a].Active then
374 begin
375 PanelArray[a].Active := False;
376 SetLength(gLiftMap[i], 32);
377 j := 0;
378 gLiftMap[i, j] := a;
379 ok := True;
381 while ok do
382 begin
383 ok := False;
384 for b := 0 to len-1 do
385 if PanelArray[b].Active then
386 for c := 0 to j do
387 if g_CollideAround(PanelArray[b].X,
388 PanelArray[b].Y,
389 PanelArray[b].Width,
390 PanelArray[b].Height,
391 PanelArray[gLiftMap[i, c]].X,
392 PanelArray[gLiftMap[i, c]].Y,
393 PanelArray[gLiftMap[i, c]].Width,
394 PanelArray[gLiftMap[i, c]].Height) then
395 begin
396 PanelArray[b].Active := False;
397 j := j+1;
398 if j > High(gLiftMap[i]) then
399 SetLength(gLiftMap[i],
400 Length(gLiftMap[i])+32);
402 gLiftMap[i, j] := b;
403 ok := True;
405 Break;
406 end;
407 end;
409 SetLength(gLiftMap[i], j+1);
410 i := i+1;
412 g_Game_StepLoading();
413 end;
415 SetLength(gLiftMap, i);
417 PanelArray := nil;
418 end;
420 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
421 CurTex: Integer; sav: Boolean): Integer;
422 var
423 len: Integer;
424 panels: ^TPanelArray;
425 begin
426 Result := -1;
428 case PanelRec.PanelType of
429 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
430 panels := @gWalls;
431 PANEL_BACK:
432 panels := @gRenderBackgrounds;
433 PANEL_FORE:
434 panels := @gRenderForegrounds;
435 PANEL_WATER:
436 panels := @gWater;
437 PANEL_ACID1:
438 panels := @gAcid1;
439 PANEL_ACID2:
440 panels := @gAcid2;
441 PANEL_STEP:
442 panels := @gSteps;
443 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
444 panels := @gLifts;
445 PANEL_BLOCKMON:
446 panels := @gBlockMon;
447 else
448 Exit;
449 end;
451 len := Length(panels^);
452 SetLength(panels^, len + 1);
454 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
455 panels^[len].ArrIdx := len;
456 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
457 if sav then
458 panels^[len].SaveIt := True;
460 Result := len;
462 len := Length(PanelByID);
463 SetLength(PanelByID, len + 1);
464 PanelByID[len].PWhere := panels;
465 PanelByID[len].PArrID := Result;
466 end;
468 function CreateNullTexture(RecName: String): Integer;
469 begin
470 SetLength(Textures, Length(Textures)+1);
471 result := High(Textures);
473 with Textures[High(Textures)] do
474 begin
475 TextureName := RecName;
476 Width := 1;
477 Height := 1;
478 Anim := False;
479 TextureID := TEXTURE_NONE;
480 end;
481 end;
483 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
484 var
485 WAD: TWADFile;
486 TextureData: Pointer;
487 WADName, txname: String;
488 a, ResLength: Integer;
489 begin
490 Result := -1;
492 if Textures <> nil then
493 for a := 0 to High(Textures) do
494 if Textures[a].TextureName = RecName then
495 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
496 Result := a;
497 Exit;
498 end;
500 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
501 if (RecName = TEXTURE_NAME_WATER) or
502 (RecName = TEXTURE_NAME_ACID1) or
503 (RecName = TEXTURE_NAME_ACID2) then
504 begin
505 SetLength(Textures, Length(Textures)+1);
507 with Textures[High(Textures)] do
508 begin
509 TextureName := RecName;
511 if TextureName = TEXTURE_NAME_WATER then
512 TextureID := TEXTURE_SPECIAL_WATER
513 else
514 if TextureName = TEXTURE_NAME_ACID1 then
515 TextureID := TEXTURE_SPECIAL_ACID1
516 else
517 if TextureName = TEXTURE_NAME_ACID2 then
518 TextureID := TEXTURE_SPECIAL_ACID2;
520 Anim := False;
521 end;
523 result := High(Textures);
524 Exit;
525 end;
527 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
528 WADName := g_ExtractWadName(RecName);
530 WAD := TWADFile.Create();
532 if WADName <> '' then
533 WADName := GameDir+'/wads/'+WADName
534 else
535 WADName := Map;
537 WAD.ReadFile(WADName);
539 txname := RecName;
541 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
542 begin
543 FreeMem(TextureData);
544 RecName := 'COMMON\ALIEN';
545 end;
548 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
549 begin
550 SetLength(Textures, Length(Textures)+1);
551 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
552 Exit;
553 e_GetTextureSize(Textures[High(Textures)].TextureID,
554 @Textures[High(Textures)].Width,
555 @Textures[High(Textures)].Height);
556 FreeMem(TextureData);
557 Textures[High(Textures)].TextureName := {RecName}txname;
558 Textures[High(Textures)].Anim := False;
560 result := High(Textures);
561 end
562 else // Íåò òàêîãî ðåóñðñà â WAD'å
563 begin
564 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
565 if log then
566 begin
567 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
568 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
569 end;
570 end;
572 WAD.Free();
573 end;
575 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
576 var
577 WAD: TWADFile;
578 TextureWAD: PChar = nil;
579 TextData: Pointer = nil;
580 TextureData: Pointer = nil;
581 cfg: TConfig = nil;
582 WADName: String;
583 ResLength: Integer;
584 TextureResource: String;
585 _width, _height, _framecount, _speed: Integer;
586 _backanimation: Boolean;
587 //imgfmt: string;
588 ia: TDynImageDataArray = nil;
589 f, c, frdelay, frloop: Integer;
590 begin
591 result := -1;
593 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
595 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
596 WADName := g_ExtractWadName(RecName);
598 WAD := TWADFile.Create();
599 try
600 if WADName <> '' then
601 WADName := GameDir+'/wads/'+WADName
602 else
603 WADName := Map;
605 WAD.ReadFile(WADName);
607 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
608 begin
609 if log then
610 begin
611 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
612 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
613 end;
614 exit;
615 end;
617 {TEST
618 if WADName = Map then
619 begin
620 //FreeMem(TextureWAD);
621 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
622 end;
625 WAD.FreeWAD();
627 if ResLength < 6 then
628 begin
629 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
630 exit;
631 end;
633 // ýòî ïòèöà? ýòî ñàìîë¸ò?
634 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
635 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
636 begin
637 // íåò, ýòî ñóïåðìåí!
638 if not WAD.ReadMemory(TextureWAD, ResLength) then
639 begin
640 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
641 exit;
642 end;
644 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
645 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
646 begin
647 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
648 exit;
649 end;
651 cfg := TConfig.CreateMem(TextData, ResLength);
653 TextureResource := cfg.ReadStr('', 'resource', '');
654 if TextureResource = '' then
655 begin
656 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
657 exit;
658 end;
660 _width := cfg.ReadInt('', 'framewidth', 0);
661 _height := cfg.ReadInt('', 'frameheight', 0);
662 _framecount := cfg.ReadInt('', 'framecount', 0);
663 _speed := cfg.ReadInt('', 'waitcount', 0);
664 _backanimation := cfg.ReadBool('', 'backanimation', False);
666 cfg.Free();
667 cfg := nil;
669 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
670 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
671 begin
672 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
673 exit;
674 end;
676 WAD.Free();
677 WAD := nil;
679 SetLength(Textures, Length(Textures)+1);
680 with Textures[High(Textures)] do
681 begin
682 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
683 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
684 begin
685 TextureName := RecName;
686 Width := _width;
687 Height := _height;
688 Anim := True;
689 FramesCount := _framecount;
690 Speed := _speed;
691 result := High(Textures);
692 end
693 else
694 begin
695 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
696 end;
697 end;
698 end
699 else
700 begin
701 // try animated image
703 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
704 if length(imgfmt) = 0 then
705 begin
706 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
707 exit;
708 end;
710 GlobalMetadata.ClearMetaItems();
711 GlobalMetadata.ClearMetaItemsForSaving();
712 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
713 begin
714 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
715 exit;
716 end;
717 if length(ia) = 0 then
718 begin
719 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
720 exit;
721 end;
723 WAD.Free();
724 WAD := nil;
726 _width := ia[0].width;
727 _height := ia[0].height;
728 _framecount := length(ia);
729 _speed := 1;
730 _backanimation := false;
731 frdelay := -1;
732 frloop := -666;
733 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
734 begin
735 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
736 try
737 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
738 frdelay := f;
739 if f < 0 then f := 0;
740 // rounding ;-)
741 c := f mod 28;
742 if c < 13 then c := 0 else c := 1;
743 f := (f div 28)+c;
744 if f < 1 then f := 1 else if f > 255 then f := 255;
745 _speed := f;
746 except
747 end;
748 end;
749 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
750 begin
751 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
752 try
753 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
754 frloop := f;
755 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
756 except
757 end;
758 end;
759 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
760 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
761 f := ord(_backanimation);
762 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);
764 SetLength(Textures, Length(Textures)+1);
765 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
766 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
767 begin
768 Textures[High(Textures)].TextureName := RecName;
769 Textures[High(Textures)].Width := _width;
770 Textures[High(Textures)].Height := _height;
771 Textures[High(Textures)].Anim := True;
772 Textures[High(Textures)].FramesCount := length(ia);
773 Textures[High(Textures)].Speed := _speed;
774 result := High(Textures);
775 //writeln(' CREATED!');
776 end
777 else
778 begin
779 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
780 end;
781 end;
782 finally
783 for f := 0 to High(ia) do FreeImage(ia[f]);
784 WAD.Free();
785 cfg.Free();
786 if TextureWAD <> nil then FreeMem(TextureWAD);
787 if TextData <> nil then FreeMem(TextData);
788 if TextureData <> nil then FreeMem(TextureData);
789 end;
790 end;
792 procedure CreateItem(Item: TItemRec_1);
793 begin
794 if g_Game_IsClient then Exit;
796 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
797 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
798 Exit;
800 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
801 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
802 end;
804 procedure CreateArea(Area: TAreaRec_1);
805 var
806 a: Integer;
807 id: DWORD;
808 begin
809 case Area.AreaType of
810 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
811 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
812 begin
813 SetLength(RespawnPoints, Length(RespawnPoints)+1);
814 with RespawnPoints[High(RespawnPoints)] do
815 begin
816 X := Area.X;
817 Y := Area.Y;
818 Direction := TDirection(Area.Direction);
820 case Area.AreaType of
821 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
822 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
823 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
824 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
825 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
826 end;
827 end;
828 end;
830 AREA_REDFLAG, AREA_BLUEFLAG:
831 begin
832 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
834 if FlagPoints[a] <> nil then Exit;
836 New(FlagPoints[a]);
838 with FlagPoints[a]^ do
839 begin
840 X := Area.X-FLAGRECT.X;
841 Y := Area.Y-FLAGRECT.Y;
842 Direction := TDirection(Area.Direction);
843 end;
845 with gFlags[a] do
846 begin
847 case a of
848 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
849 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
850 end;
852 Animation := TAnimation.Create(id, True, 8);
853 Obj.Rect := FLAGRECT;
855 g_Map_ResetFlag(a);
856 end;
857 end;
859 AREA_DOMFLAG:
860 begin
861 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
862 with DOMFlagPoints[High(DOMFlagPoints)] do
863 begin
864 X := Area.X;
865 Y := Area.Y;
866 Direction := TDirection(Area.Direction);
867 end;
869 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
870 end;
871 end;
872 end;
874 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
875 var
876 _trigger: TTrigger;
877 begin
878 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
880 with _trigger do
881 begin
882 X := Trigger.X;
883 Y := Trigger.Y;
884 Width := Trigger.Width;
885 Height := Trigger.Height;
886 Enabled := ByteBool(Trigger.Enabled);
887 TexturePanel := Trigger.TexturePanel;
888 TexturePanelType := fTexturePanel1Type;
889 ShotPanelType := fTexturePanel2Type;
890 TriggerType := Trigger.TriggerType;
891 ActivateType := Trigger.ActivateType;
892 Keys := Trigger.Keys;
893 Data.Default := Trigger.DATA;
894 end;
896 g_Triggers_Create(_trigger);
897 end;
899 procedure CreateMonster(monster: TMonsterRec_1);
900 var
901 a, i: Integer;
902 begin
903 if g_Game_IsClient then Exit;
905 if (gGameSettings.GameType = GT_SINGLE)
906 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
907 begin
908 i := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y,
909 TDirection(monster.Direction));
911 if gTriggers <> nil then
912 for a := 0 to High(gTriggers) do
913 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
914 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
915 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
916 gMonsters[i].AddTrigger(a);
918 if monster.MonsterType <> MONSTER_BARREL then
919 Inc(gTotalMonsters);
920 end;
921 end;
923 procedure g_Map_ReAdd_DieTriggers();
924 var
925 i, a: Integer;
926 begin
927 if g_Game_IsClient then Exit;
929 for i := 0 to High(gMonsters) do
930 if gMonsters[i] <> nil then
931 begin
932 gMonsters[i].ClearTriggers();
934 for a := 0 to High(gTriggers) do
935 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
936 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
937 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
938 gMonsters[i].AddTrigger(a);
939 end;
940 end;
942 function extractWadName(resourceName: string): string;
943 var
944 posN: Integer;
945 begin
946 posN := Pos(':', resourceName);
947 if posN > 0 then
948 Result:= Copy(resourceName, 0, posN-1)
949 else
950 Result := '';
951 end;
953 procedure addResToExternalResList(res: string);
954 begin
955 res := extractWadName(res);
956 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
957 gExternalResources.Add(res);
958 end;
960 procedure generateExternalResourcesList(mapReader: TMapReader_1);
961 var
962 textures: TTexturesRec1Array;
963 mapHeader: TMapHeaderRec_1;
964 i: integer;
965 resFile: String = '';
966 begin
967 if gExternalResources = nil then
968 gExternalResources := TStringList.Create;
970 gExternalResources.Clear;
971 textures := mapReader.GetTextures();
972 for i := 0 to High(textures) do
973 begin
974 addResToExternalResList(resFile);
975 end;
977 textures := nil;
979 mapHeader := mapReader.GetMapHeader;
981 addResToExternalResList(mapHeader.MusicName);
982 addResToExternalResList(mapHeader.SkyName);
983 end;
985 procedure mapCreateGrid ();
986 var
987 mapX0: Integer = $3fffffff;
988 mapY0: Integer = $3fffffff;
989 mapX1: Integer = -$3fffffff;
990 mapY1: Integer = -$3fffffff;
992 procedure fixMinMax (var panels: TPanelArray);
993 var
994 idx: Integer;
995 begin
996 for idx := 0 to High(panels) do
997 begin
998 if (panels[idx].Width < 1) or (panels[idx].Height < 1) then continue;
999 if mapX0 > panels[idx].X then mapX0 := panels[idx].X;
1000 if mapY0 > panels[idx].Y then mapY0 := panels[idx].Y;
1001 if mapX1 < panels[idx].X+panels[idx].Width-1 then mapX1 := panels[idx].X+panels[idx].Width-1;
1002 if mapY1 < panels[idx].Y+panels[idx].Height-1 then mapY1 := panels[idx].Y+panels[idx].Height-1;
1003 end;
1004 end;
1006 procedure addPanelsToGrid (var panels: TPanelArray; tag: Integer);
1007 var
1008 idx: Integer;
1009 begin
1010 tag := panelTypeToTag(tag);
1011 for idx := High(panels) downto 0 do
1012 begin
1013 panels[idx].tag := tag;
1014 gMapGrid.insertBody(panels[idx], panels[idx].X, panels[idx].Y, panels[idx].Width, panels[idx].Height, tag);
1015 mapTree.insertObject(panels[idx], tag, true); // as static object
1016 end;
1017 end;
1019 begin
1020 gMapGrid.Free();
1021 gMapGrid := nil;
1022 mapTree.Free();
1023 mapTree := nil;
1025 fixMinMax(gWalls);
1026 fixMinMax(gRenderBackgrounds);
1027 fixMinMax(gRenderForegrounds);
1028 fixMinMax(gWater);
1029 fixMinMax(gAcid1);
1030 fixMinMax(gAcid2);
1031 fixMinMax(gSteps);
1032 fixMinMax(gLifts);
1033 fixMinMax(gBlockMon);
1035 if (mapX0 < 0) or (mapY0 < 0) then
1036 begin
1037 e_WriteLog(Format('funny map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1038 //raise Exception.Create('we are fucked');
1039 end;
1041 gMapGrid := TBodyGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1042 mapTree := TDynAABBTreeMap.Create();
1044 addPanelsToGrid(gWalls, PANEL_WALL); // and PANEL_CLOSEDOOR
1045 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1046 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1047 addPanelsToGrid(gWater, PANEL_WATER);
1048 addPanelsToGrid(gAcid1, PANEL_ACID1);
1049 addPanelsToGrid(gAcid2, PANEL_ACID2);
1050 addPanelsToGrid(gSteps, PANEL_STEP);
1051 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1052 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1054 gMapGrid.dumpStats();
1055 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1056 mapTree.forEachLeaf(nil);
1057 end;
1059 function g_Map_Load(Res: String): Boolean;
1060 const
1061 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1062 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1063 var
1064 WAD: TWADFile;
1065 MapReader: TMapReader_1;
1066 Header: TMapHeaderRec_1;
1067 _textures: TTexturesRec1Array;
1068 _texnummap: array of Integer; // `_textures` -> `Textures`
1069 panels: TPanelsRec1Array;
1070 items: TItemsRec1Array;
1071 monsters: TMonsterRec1Array;
1072 areas: TAreasRec1Array;
1073 triggers: TTriggersRec1Array;
1074 a, b, c, k: Integer;
1075 PanelID: DWORD;
1076 AddTextures: TAddTextureArray;
1077 texture: TTextureRec_1;
1078 TriggersTable: Array of record
1079 TexturePanel: Integer;
1080 LiftPanel: Integer;
1081 DoorPanel: Integer;
1082 ShotPanel: Integer;
1083 end;
1084 FileName, mapResName, s, TexName: String;
1085 Data: Pointer;
1086 Len: Integer;
1087 ok, isAnim, trigRef: Boolean;
1088 CurTex, ntn: Integer;
1090 begin
1091 gMapGrid.Free();
1092 gMapGrid := nil;
1093 mapTree.Free();
1094 mapTree := nil;
1096 Result := False;
1097 gMapInfo.Map := Res;
1098 TriggersTable := nil;
1099 FillChar(texture, SizeOf(texture), 0);
1101 sfsGCDisable(); // temporary disable removing of temporary volumes
1102 try
1103 // Çàãðóçêà WAD:
1104 FileName := g_ExtractWadName(Res);
1105 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1106 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1108 WAD := TWADFile.Create();
1109 if not WAD.ReadFile(FileName) then
1110 begin
1111 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1112 WAD.Free();
1113 Exit;
1114 end;
1115 //k8: why loader ignores path here?
1116 mapResName := g_ExtractFileName(Res);
1117 if not WAD.GetMapResource(mapResName, Data, Len) then
1118 begin
1119 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1120 WAD.Free();
1121 Exit;
1122 end;
1124 WAD.Free();
1126 // Çàãðóçêà êàðòû:
1127 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1128 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1129 MapReader := TMapReader_1.Create();
1131 if not MapReader.LoadMap(Data) then
1132 begin
1133 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1134 FreeMem(Data);
1135 MapReader.Free();
1136 Exit;
1137 end;
1139 FreeMem(Data);
1140 generateExternalResourcesList(MapReader);
1141 // Çàãðóçêà òåêñòóð:
1142 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1143 _textures := MapReader.GetTextures();
1144 _texnummap := nil;
1146 // Äîáàâëåíèå òåêñòóð â Textures[]:
1147 if _textures <> nil then
1148 begin
1149 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1150 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1151 SetLength(_texnummap, length(_textures));
1153 for a := 0 to High(_textures) do
1154 begin
1155 SetLength(s, 64);
1156 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1157 for b := 1 to Length(s) do
1158 if s[b] = #0 then
1159 begin
1160 SetLength(s, b-1);
1161 Break;
1162 end;
1163 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1164 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1165 // Àíèìèðîâàííàÿ òåêñòóðà:
1166 if ByteBool(_textures[a].Anim) then
1167 begin
1168 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1169 if ntn < 0 then
1170 begin
1171 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1172 ntn := CreateNullTexture(_textures[a].Resource);
1173 end;
1174 end
1175 else // Îáû÷íàÿ òåêñòóðà:
1176 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1177 if ntn < 0 then
1178 begin
1179 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1180 ntn := CreateNullTexture(_textures[a].Resource);
1181 end;
1183 _texnummap[a] := ntn; // fix texture number
1184 g_Game_StepLoading();
1185 end;
1186 end;
1188 // Çàãðóçêà òðèããåðîâ:
1189 gTriggerClientID := 0;
1190 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1191 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1192 triggers := MapReader.GetTriggers();
1194 // Çàãðóçêà ïàíåëåé:
1195 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1196 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1197 panels := MapReader.GetPanels();
1199 // check texture numbers for panels
1200 for a := 0 to High(panels) do
1201 begin
1202 if panels[a].TextureNum > High(_textures) then
1203 begin
1204 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1205 result := false;
1206 exit;
1207 end;
1208 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1209 end;
1211 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1212 if triggers <> nil then
1213 begin
1214 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1215 SetLength(TriggersTable, Length(triggers));
1216 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1218 for a := 0 to High(TriggersTable) do
1219 begin
1220 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1221 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1222 // Ëèôòû:
1223 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1224 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1225 else
1226 TriggersTable[a].LiftPanel := -1;
1227 // Äâåðè:
1228 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1229 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1230 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1231 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1232 else
1233 TriggersTable[a].DoorPanel := -1;
1234 // Òóðåëü:
1235 if triggers[a].TriggerType = TRIGGER_SHOT then
1236 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1237 else
1238 TriggersTable[a].ShotPanel := -1;
1240 g_Game_StepLoading();
1241 end;
1242 end;
1244 // Ñîçäàåì ïàíåëè:
1245 if panels <> nil then
1246 begin
1247 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1248 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1250 for a := 0 to High(panels) do
1251 begin
1252 SetLength(AddTextures, 0);
1253 trigRef := False;
1254 CurTex := -1;
1255 if _textures <> nil then
1256 begin
1257 texture := _textures[panels[a].TextureNum];
1258 ok := True;
1259 end
1260 else
1261 ok := False;
1263 if ok then
1264 begin
1265 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1266 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1267 ok := False;
1268 if (TriggersTable <> nil) and (_textures <> nil) then
1269 for b := 0 to High(TriggersTable) do
1270 if (TriggersTable[b].TexturePanel = a)
1271 or (TriggersTable[b].ShotPanel = a) then
1272 begin
1273 trigRef := True;
1274 ok := True;
1275 Break;
1276 end;
1277 end;
1279 if ok then
1280 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1281 SetLength(s, 64);
1282 CopyMemory(@s[1], @texture.Resource[0], 64);
1283 // Èçìåðÿåì äëèíó:
1284 Len := Length(s);
1285 for c := Len downto 1 do
1286 if s[c] <> #0 then
1287 begin
1288 Len := c;
1289 Break;
1290 end;
1291 SetLength(s, Len);
1293 // Ñïåö-òåêñòóðû çàïðåùåíû:
1294 if g_Map_IsSpecialTexture(s) then
1295 ok := False
1296 else
1297 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1298 ok := g_Texture_NumNameFindStart(s);
1300 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1301 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1302 if ok then
1303 begin
1304 k := NNF_NAME_BEFORE;
1305 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1306 while ok or (k = NNF_NAME_BEFORE) or
1307 (k = NNF_NAME_EQUALS) do
1308 begin
1309 k := g_Texture_NumNameFindNext(TexName);
1311 if (k = NNF_NAME_BEFORE) or
1312 (k = NNF_NAME_AFTER) then
1313 begin
1314 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1315 if ByteBool(texture.Anim) then
1316 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1317 isAnim := True;
1318 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1319 if not ok then
1320 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1321 isAnim := False;
1322 ok := CreateTexture(TexName, FileName, False) >= 0;
1323 end;
1324 end
1325 else
1326 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1327 isAnim := False;
1328 ok := CreateTexture(TexName, FileName, False) >= 0;
1329 if not ok then
1330 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1331 isAnim := True;
1332 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1333 end;
1334 end;
1336 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1337 if ok then
1338 begin
1339 for c := 0 to High(Textures) do
1340 if Textures[c].TextureName = TexName then
1341 begin
1342 SetLength(AddTextures, Length(AddTextures)+1);
1343 AddTextures[High(AddTextures)].Texture := c;
1344 AddTextures[High(AddTextures)].Anim := isAnim;
1345 Break;
1346 end;
1347 end;
1348 end
1349 else
1350 if k = NNF_NAME_EQUALS then
1351 begin
1352 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1353 SetLength(AddTextures, Length(AddTextures)+1);
1354 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1355 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1356 CurTex := High(AddTextures);
1357 ok := True;
1358 end
1359 else // NNF_NO_NAME
1360 ok := False;
1361 end; // while ok...
1363 ok := True;
1364 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1365 end; // if ok - ññûëàþòñÿ òðèããåðû
1367 if not ok then
1368 begin
1369 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1370 SetLength(AddTextures, 1);
1371 AddTextures[0].Texture := panels[a].TextureNum;
1372 AddTextures[0].Anim := ByteBool(texture.Anim);
1373 CurTex := 0;
1374 end;
1376 //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);
1378 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1379 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1381 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1382 if TriggersTable <> nil then
1383 for b := 0 to High(TriggersTable) do
1384 begin
1385 // Òðèããåð äâåðè/ëèôòà:
1386 if (TriggersTable[b].LiftPanel = a) or
1387 (TriggersTable[b].DoorPanel = a) then
1388 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1389 // Òðèããåð ñìåíû òåêñòóðû:
1390 if TriggersTable[b].TexturePanel = a then
1391 triggers[b].TexturePanel := PanelID;
1392 // Òðèããåð "Òóðåëü":
1393 if TriggersTable[b].ShotPanel = a then
1394 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1395 end;
1397 g_Game_StepLoading();
1398 end;
1399 end;
1401 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1402 if (triggers <> nil) and not gLoadGameMode then
1403 begin
1404 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1405 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1406 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1407 for a := 0 to High(triggers) do
1408 begin
1409 if triggers[a].TexturePanel <> -1 then
1410 b := panels[TriggersTable[a].TexturePanel].PanelType
1411 else
1412 b := 0;
1413 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1414 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1415 c := panels[TriggersTable[a].ShotPanel].PanelType
1416 else
1417 c := 0;
1418 CreateTrigger(triggers[a], b, c);
1419 end;
1420 end;
1422 // Çàãðóçêà ïðåäìåòîâ:
1423 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1424 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1425 items := MapReader.GetItems();
1427 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1428 if (items <> nil) and not gLoadGameMode then
1429 begin
1430 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1431 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1432 for a := 0 to High(items) do
1433 CreateItem(Items[a]);
1434 end;
1436 // Çàãðóçêà îáëàñòåé:
1437 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1438 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1439 areas := MapReader.GetAreas();
1441 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1442 if areas <> nil then
1443 begin
1444 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1445 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1446 for a := 0 to High(areas) do
1447 CreateArea(areas[a]);
1448 end;
1450 // Çàãðóçêà ìîíñòðîâ:
1451 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1452 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1453 monsters := MapReader.GetMonsters();
1455 gTotalMonsters := 0;
1457 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1458 if (monsters <> nil) and not gLoadGameMode then
1459 begin
1460 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1461 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1462 for a := 0 to High(monsters) do
1463 CreateMonster(monsters[a]);
1464 end;
1466 // Çàãðóçêà îïèñàíèÿ êàðòû:
1467 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1468 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1469 Header := MapReader.GetMapHeader();
1471 MapReader.Free();
1473 with gMapInfo do
1474 begin
1475 Name := Header.MapName;
1476 Description := Header.MapDescription;
1477 Author := Header.MapAuthor;
1478 MusicName := Header.MusicName;
1479 SkyName := Header.SkyName;
1480 Height := Header.Height;
1481 Width := Header.Width;
1482 end;
1484 // Çàãðóçêà íåáà:
1485 if gMapInfo.SkyName <> '' then
1486 begin
1487 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1488 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1489 FileName := g_ExtractWadName(gMapInfo.SkyName);
1491 if FileName <> '' then
1492 FileName := GameDir+'/wads/'+FileName
1493 else
1494 begin
1495 FileName := g_ExtractWadName(Res);
1496 end;
1498 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1499 if g_Texture_CreateWAD(BackID, s) then
1500 begin
1501 g_Game_SetupScreenSize();
1502 end
1503 else
1504 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1505 end;
1507 // Çàãðóçêà ìóçûêè:
1508 ok := False;
1509 if gMapInfo.MusicName <> '' then
1510 begin
1511 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1512 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1513 FileName := g_ExtractWadName(gMapInfo.MusicName);
1515 if FileName <> '' then
1516 FileName := GameDir+'/wads/'+FileName
1517 else
1518 begin
1519 FileName := g_ExtractWadName(Res);
1520 end;
1522 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1523 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1524 ok := True
1525 else
1526 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1527 end;
1529 // Îñòàëüíûå óñòàíâêè:
1530 CreateDoorMap();
1531 CreateLiftMap();
1533 g_Items_Init();
1534 g_Weapon_Init();
1535 g_Monsters_Init();
1537 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1538 if not gLoadGameMode then
1539 g_GFX_Init();
1541 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1542 _textures := nil;
1543 panels := nil;
1544 items := nil;
1545 areas := nil;
1546 triggers := nil;
1547 TriggersTable := nil;
1548 AddTextures := nil;
1550 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1551 if ok and (not gLoadGameMode) then
1552 begin
1553 gMusic.SetByName(gMapInfo.MusicName);
1554 gMusic.Play();
1555 end
1556 else
1557 gMusic.SetByName('');
1558 finally
1559 sfsGCEnable(); // enable releasing unused volumes
1560 end;
1562 e_WriteLog('Creating map grid', MSG_NOTIFY);
1563 mapCreateGrid();
1565 e_WriteLog('Done loading map.', MSG_NOTIFY);
1566 Result := True;
1567 end;
1569 function g_Map_GetMapInfo(Res: String): TMapInfo;
1570 var
1571 WAD: TWADFile;
1572 MapReader: TMapReader_1;
1573 Header: TMapHeaderRec_1;
1574 FileName: String;
1575 Data: Pointer;
1576 Len: Integer;
1577 begin
1578 FillChar(Result, SizeOf(Result), 0);
1579 FileName := g_ExtractWadName(Res);
1581 WAD := TWADFile.Create();
1582 if not WAD.ReadFile(FileName) then
1583 begin
1584 WAD.Free();
1585 Exit;
1586 end;
1588 //k8: it ignores path again
1589 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1590 begin
1591 WAD.Free();
1592 Exit;
1593 end;
1595 WAD.Free();
1597 MapReader := TMapReader_1.Create();
1599 if not MapReader.LoadMap(Data) then
1600 begin
1601 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1602 ZeroMemory(@Header, SizeOf(Header));
1603 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1604 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1605 end
1606 else
1607 begin
1608 Header := MapReader.GetMapHeader();
1609 Result.Name := Header.MapName;
1610 Result.Description := Header.MapDescription;
1611 end;
1613 FreeMem(Data);
1614 MapReader.Free();
1616 Result.Map := Res;
1617 Result.Author := Header.MapAuthor;
1618 Result.Height := Header.Height;
1619 Result.Width := Header.Width;
1620 end;
1622 function g_Map_GetMapsList(WADName: string): SArray;
1623 var
1624 WAD: TWADFile;
1625 a: Integer;
1626 ResList: SArray;
1627 begin
1628 Result := nil;
1629 WAD := TWADFile.Create();
1630 if not WAD.ReadFile(WADName) then
1631 begin
1632 WAD.Free();
1633 Exit;
1634 end;
1635 ResList := WAD.GetMapResources();
1636 if ResList <> nil then
1637 begin
1638 for a := 0 to High(ResList) do
1639 begin
1640 SetLength(Result, Length(Result)+1);
1641 Result[High(Result)] := ResList[a];
1642 end;
1643 end;
1644 WAD.Free();
1645 end;
1647 function g_Map_Exist(Res: string): Boolean;
1648 var
1649 WAD: TWADFile;
1650 FileName, mnn: string;
1651 ResList: SArray;
1652 a: Integer;
1653 begin
1654 Result := False;
1656 FileName := addWadExtension(g_ExtractWadName(Res));
1658 WAD := TWADFile.Create;
1659 if not WAD.ReadFile(FileName) then
1660 begin
1661 WAD.Free();
1662 Exit;
1663 end;
1665 ResList := WAD.GetMapResources();
1666 WAD.Free();
1668 mnn := g_ExtractFileName(Res);
1669 if ResList <> nil then
1670 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1671 begin
1672 Result := True;
1673 Exit;
1674 end;
1675 end;
1677 procedure g_Map_Free();
1678 var
1679 a: Integer;
1681 procedure FreePanelArray(var panels: TPanelArray);
1682 var
1683 i: Integer;
1685 begin
1686 if panels <> nil then
1687 begin
1688 for i := 0 to High(panels) do
1689 panels[i].Free();
1690 panels := nil;
1691 end;
1692 end;
1694 begin
1695 g_GFX_Free();
1696 g_Weapon_Free();
1697 g_Items_Free();
1698 g_Triggers_Free();
1699 g_Monsters_Free();
1701 RespawnPoints := nil;
1702 if FlagPoints[FLAG_RED] <> nil then
1703 begin
1704 Dispose(FlagPoints[FLAG_RED]);
1705 FlagPoints[FLAG_RED] := nil;
1706 end;
1707 if FlagPoints[FLAG_BLUE] <> nil then
1708 begin
1709 Dispose(FlagPoints[FLAG_BLUE]);
1710 FlagPoints[FLAG_BLUE] := nil;
1711 end;
1712 //DOMFlagPoints := nil;
1714 //gDOMFlags := nil;
1716 if Textures <> nil then
1717 begin
1718 for a := 0 to High(Textures) do
1719 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1720 if Textures[a].Anim then
1721 g_Frames_DeleteByID(Textures[a].FramesID)
1722 else
1723 if Textures[a].TextureID <> TEXTURE_NONE then
1724 e_DeleteTexture(Textures[a].TextureID);
1726 Textures := nil;
1727 end;
1729 FreePanelArray(gWalls);
1730 FreePanelArray(gRenderBackgrounds);
1731 FreePanelArray(gRenderForegrounds);
1732 FreePanelArray(gWater);
1733 FreePanelArray(gAcid1);
1734 FreePanelArray(gAcid2);
1735 FreePanelArray(gSteps);
1736 FreePanelArray(gLifts);
1737 FreePanelArray(gBlockMon);
1739 if BackID <> DWORD(-1) then
1740 begin
1741 gBackSize.X := 0;
1742 gBackSize.Y := 0;
1743 e_DeleteTexture(BackID);
1744 BackID := DWORD(-1);
1745 end;
1747 g_Game_StopAllSounds(False);
1748 gMusic.FreeSound();
1749 g_Sound_Delete(gMapInfo.MusicName);
1751 gMapInfo.Name := '';
1752 gMapInfo.Description := '';
1753 gMapInfo.MusicName := '';
1754 gMapInfo.Height := 0;
1755 gMapInfo.Width := 0;
1757 gDoorMap := nil;
1758 gLiftMap := nil;
1760 PanelByID := nil;
1761 end;
1763 procedure g_Map_Update();
1764 var
1765 a, d, j: Integer;
1766 m: Word;
1767 s: String;
1769 procedure UpdatePanelArray(var panels: TPanelArray);
1770 var
1771 i: Integer;
1773 begin
1774 if panels <> nil then
1775 for i := 0 to High(panels) do
1776 panels[i].Update();
1777 end;
1779 begin
1780 UpdatePanelArray(gWalls);
1781 UpdatePanelArray(gRenderBackgrounds);
1782 UpdatePanelArray(gRenderForegrounds);
1783 UpdatePanelArray(gWater);
1784 UpdatePanelArray(gAcid1);
1785 UpdatePanelArray(gAcid2);
1786 UpdatePanelArray(gSteps);
1788 if gGameSettings.GameMode = GM_CTF then
1789 begin
1790 for a := FLAG_RED to FLAG_BLUE do
1791 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1792 with gFlags[a] do
1793 begin
1794 if gFlags[a].Animation <> nil then
1795 gFlags[a].Animation.Update();
1797 m := g_Obj_Move(@Obj, True, True);
1799 if gTime mod (GAME_TICK*2) <> 0 then
1800 Continue;
1802 // Ñîïðîòèâëåíèå âîçäóõà:
1803 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1805 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1806 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1807 begin
1808 g_Map_ResetFlag(a);
1809 gFlags[a].CaptureTime := 0;
1810 if a = FLAG_RED then
1811 s := _lc[I_PLAYER_FLAG_RED]
1812 else
1813 s := _lc[I_PLAYER_FLAG_BLUE];
1814 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1816 if g_Game_IsNet then
1817 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1818 Continue;
1819 end;
1821 if Count > 0 then
1822 Count := Count - 1;
1824 // Èãðîê áåðåò ôëàã:
1825 if gPlayers <> nil then
1826 begin
1827 j := Random(Length(gPlayers)) - 1;
1829 for d := 0 to High(gPlayers) do
1830 begin
1831 Inc(j);
1832 if j > High(gPlayers) then
1833 j := 0;
1835 if gPlayers[j] <> nil then
1836 if gPlayers[j].Live and
1837 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1838 begin
1839 if gPlayers[j].GetFlag(a) then
1840 Break;
1841 end;
1842 end;
1843 end;
1844 end;
1845 end;
1846 end;
1849 procedure g_Map_DrawPanelsOld(PanelType: Word);
1851 procedure DrawPanels (stp: Integer; var panels: TPanelArray; drawDoors: Boolean=False);
1852 var
1853 idx: Integer;
1854 begin
1855 if (panels <> nil) and (stp >= 0) and (stp <= 6) then
1856 begin
1857 // alas, no visible set
1858 for idx := 0 to High(panels) do
1859 begin
1860 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1861 end;
1862 end;
1863 end;
1865 begin
1866 case PanelType of
1867 PANEL_WALL: DrawPanels(0, gWalls);
1868 PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True);
1869 PANEL_BACK: DrawPanels(1, gRenderBackgrounds);
1870 PANEL_FORE: DrawPanels(2, gRenderForegrounds);
1871 PANEL_WATER: DrawPanels(3, gWater);
1872 PANEL_ACID1: DrawPanels(4, gAcid1);
1873 PANEL_ACID2: DrawPanels(5, gAcid2);
1874 PANEL_STEP: DrawPanels(6, gSteps);
1875 end;
1876 end;
1879 var
1880 gDrawPanelList: TBinaryHeapObj = nil;
1882 function dplLess (a, b: TObject): Boolean;
1883 begin
1884 result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
1885 end;
1887 procedure dplClear ();
1888 begin
1889 if gDrawPanelList = nil then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
1890 end;
1892 procedure dplAddPanel (pan: TPanel);
1893 begin
1894 if pan = nil then exit;
1895 gDrawPanelList.insert(pan);
1896 end;
1899 procedure g_Map_DrawPanels(x0, y0, wdt, hgt: Integer; PanelType: Word);
1900 var
1901 ptag: Integer;
1903 function checker (obj: TObject; tag: Integer): Boolean;
1904 var
1905 pan: TPanel;
1906 begin
1907 result := false; // don't stop, ever
1908 //e_WriteLog(Format(' *body: tag:%d; ptag:%d; pantype=%d', [tag, ptag, PanelType]), MSG_NOTIFY);
1909 if (tag <> ptag) then exit;
1911 if obj = nil then begin e_WriteLog(Format(' !bodyFUUUUU0: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY); exit; end;
1912 if not (obj is TPanel) then begin e_WriteLog(Format(' !bodyFUUUUU1: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY); exit; end;
1913 //pan := (obj as TPanel);
1914 //e_WriteLog(Format(' !body: (%d,%d)-(%dx%d) tag:%d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, tag, PanelType]), MSG_NOTIFY);
1916 pan := (obj as TPanel);
1917 if (PanelType = PANEL_CLOSEDOOR) then begin if not pan.Door then exit; end else begin if pan.Door then exit; end;
1918 //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);
1919 dplAddPanel(pan);
1920 end;
1922 procedure DrawPanels (stp: Integer; var panels: TPanelArray; drawDoors: Boolean=False);
1923 var
1924 idx: Integer;
1925 pan: TPanel;
1926 begin
1927 if (panels <> nil) and (stp >= 0) and (stp <= 6) then
1928 begin
1929 // alas, no visible set
1930 for idx := 0 to High(panels) do
1931 begin
1932 if not (drawDoors xor panels[idx].Door) then
1933 begin
1934 pan := panels[idx];
1936 if (pan.Width < 1) or (pan.Height < 1) then continue;
1937 if (pan.X+pan.Width <= x0) or (pan.Y+pan.Height <= y0) then continue;
1938 if (pan.X >= x0+wdt) or (pan.Y >= y0+hgt) then continue;
1940 pan.Draw();
1941 //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);
1942 end;
1943 end;
1944 end;
1945 end;
1947 begin
1948 //g_Map_DrawPanelsOld(PanelType); exit;
1949 //e_WriteLog('==================', MSG_NOTIFY);
1950 //e_WriteLog(Format('***QQQ: qtag:%d', [PanelType]), MSG_NOTIFY);
1951 dplClear();
1952 ptag := panelTypeToTag(PanelType);
1954 if gdbg_map_use_accel_render then
1955 begin
1956 if gdbg_map_use_tree_draw then
1957 begin
1958 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, ptag);
1959 end
1960 else
1961 begin
1962 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, ptag);
1963 end;
1964 // sort and draw the list (we need to sort it, or rendering is fucked)
1965 while gDrawPanelList.count > 0 do
1966 begin
1967 (gDrawPanelList.front() as TPanel).Draw();
1968 gDrawPanelList.popFront();
1969 end;
1970 end
1971 else
1972 begin
1973 //e_WriteLog(Format('+++QQQ: qtag:%d', [PanelType]), MSG_NOTIFY);
1974 case PanelType of
1975 PANEL_WALL: DrawPanels(0, gWalls);
1976 PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True);
1977 PANEL_BACK: DrawPanels(1, gRenderBackgrounds);
1978 PANEL_FORE: DrawPanels(2, gRenderForegrounds);
1979 PANEL_WATER: DrawPanels(3, gWater);
1980 PANEL_ACID1: DrawPanels(4, gAcid1);
1981 PANEL_ACID2: DrawPanels(5, gAcid2);
1982 PANEL_STEP: DrawPanels(6, gSteps);
1983 end;
1984 end;
1986 //e_WriteLog('==================', MSG_NOTIFY);
1987 end;
1990 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
1991 function checker (obj: TObject; tag: Integer): Boolean;
1992 var
1993 pan: TPanel;
1994 begin
1995 result := false; // don't stop, ever
1996 if (tag <> GridTagWallDoor) then exit; // only walls
1997 pan := (obj as TPanel);
1998 pan.DrawShadowVolume(lightX, lightY, radius);
1999 end;
2001 begin
2002 if gdbg_map_use_tree_draw then
2003 begin
2004 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, GridTagWallDoor);
2005 end
2006 else
2007 begin
2008 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, GridTagWallDoor);
2009 end;
2010 end;
2013 procedure g_Map_DrawBack(dx, dy: Integer);
2014 begin
2015 if gDrawBackGround and (BackID <> DWORD(-1)) then
2016 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2017 else
2018 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2019 end;
2021 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2022 PanelType: Word; b1x3: Boolean): Boolean;
2023 var
2024 a, h: Integer;
2025 begin
2026 Result := False;
2028 if WordBool(PanelType and PANEL_WALL) then
2029 if gWalls <> nil then
2030 begin
2031 h := High(gWalls);
2033 for a := 0 to h do
2034 if gWalls[a].Enabled and
2035 g_Collide(X, Y, Width, Height,
2036 gWalls[a].X, gWalls[a].Y,
2037 gWalls[a].Width, gWalls[a].Height) then
2038 begin
2039 Result := True;
2040 Exit;
2041 end;
2042 end;
2044 if WordBool(PanelType and PANEL_WATER) then
2045 if gWater <> nil then
2046 begin
2047 h := High(gWater);
2049 for a := 0 to h do
2050 if g_Collide(X, Y, Width, Height,
2051 gWater[a].X, gWater[a].Y,
2052 gWater[a].Width, gWater[a].Height) then
2053 begin
2054 Result := True;
2055 Exit;
2056 end;
2057 end;
2059 if WordBool(PanelType and PANEL_ACID1) then
2060 if gAcid1 <> nil then
2061 begin
2062 h := High(gAcid1);
2064 for a := 0 to h do
2065 if g_Collide(X, Y, Width, Height,
2066 gAcid1[a].X, gAcid1[a].Y,
2067 gAcid1[a].Width, gAcid1[a].Height) then
2068 begin
2069 Result := True;
2070 Exit;
2071 end;
2072 end;
2074 if WordBool(PanelType and PANEL_ACID2) then
2075 if gAcid2 <> nil then
2076 begin
2077 h := High(gAcid2);
2079 for a := 0 to h do
2080 if g_Collide(X, Y, Width, Height,
2081 gAcid2[a].X, gAcid2[a].Y,
2082 gAcid2[a].Width, gAcid2[a].Height) then
2083 begin
2084 Result := True;
2085 Exit;
2086 end;
2087 end;
2089 if WordBool(PanelType and PANEL_STEP) then
2090 if gSteps <> nil then
2091 begin
2092 h := High(gSteps);
2094 for a := 0 to h do
2095 if g_Collide(X, Y, Width, Height,
2096 gSteps[a].X, gSteps[a].Y,
2097 gSteps[a].Width, gSteps[a].Height) then
2098 begin
2099 Result := True;
2100 Exit;
2101 end;
2102 end;
2104 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2105 if gLifts <> nil then
2106 begin
2107 h := High(gLifts);
2109 for a := 0 to h do
2110 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2111 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2112 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2113 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2114 g_Collide(X, Y, Width, Height,
2115 gLifts[a].X, gLifts[a].Y,
2116 gLifts[a].Width, gLifts[a].Height) then
2117 begin
2118 Result := True;
2119 Exit;
2120 end;
2121 end;
2123 if WordBool(PanelType and PANEL_BLOCKMON) then
2124 if gBlockMon <> nil then
2125 begin
2126 h := High(gBlockMon);
2128 for a := 0 to h do
2129 if ( (not b1x3) or
2130 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2131 g_Collide(X, Y, Width, Height,
2132 gBlockMon[a].X, gBlockMon[a].Y,
2133 gBlockMon[a].Width, gBlockMon[a].Height) then
2134 begin
2135 Result := True;
2136 Exit;
2137 end;
2138 end;
2139 end;
2141 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2142 var
2143 texid: DWORD;
2145 function checkPanels (var panels: TPanelArray): Boolean;
2146 var
2147 a: Integer;
2148 begin
2149 result := false;
2150 if panels = nil then exit;
2151 for a := 0 to High(panels) do
2152 begin
2153 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2154 begin
2155 result := true;
2156 texid := panels[a].GetTextureID();
2157 exit;
2158 end;
2159 end;
2160 end;
2162 begin
2163 texid := TEXTURE_NONE;
2164 result := texid;
2165 if not checkPanels(gWater) then
2166 if not checkPanels(gAcid1) then
2167 if not checkPanels(gAcid2) then exit;
2168 result := texid;
2169 end;
2172 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2174 function checker (obj: TObject; tag: Integer): Boolean;
2175 var
2176 pan: TPanel;
2177 a: Integer;
2178 begin
2179 result := false; // don't stop, ever
2181 //e_WriteLog(Format(' *body: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
2183 if obj = nil then
2184 begin
2185 e_WriteLog(Format(' !bodyFUUUUU0: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
2186 end;
2187 if not (obj is TPanel) then
2188 begin
2189 e_WriteLog(Format(' !bodyFUUUUU1: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
2190 exit;
2191 end;
2193 pan := (obj as TPanel);
2194 a := pan.ArrIdx;
2196 if WordBool(PanelType and PANEL_WALL) and (tag = GridTagWallDoor) then
2197 begin
2198 if gWalls[a].Enabled and g_Collide(X, Y, Width, Height, gWalls[a].X, gWalls[a].Y, gWalls[a].Width, gWalls[a].Height) then
2199 begin
2200 result := true;
2201 exit;
2202 end;
2203 end;
2205 if WordBool(PanelType and PANEL_WATER) and (tag = GridTagWater) then
2206 begin
2207 if g_Collide(X, Y, Width, Height, gWater[a].X, gWater[a].Y, gWater[a].Width, gWater[a].Height) then
2208 begin
2209 result := True;
2210 exit;
2211 end;
2212 end;
2214 if WordBool(PanelType and PANEL_ACID1) and (tag = GridTagAcid1) then
2215 begin
2216 if g_Collide(X, Y, Width, Height, gAcid1[a].X, gAcid1[a].Y, gAcid1[a].Width, gAcid1[a].Height) then
2217 begin
2218 result := True;
2219 exit;
2220 end;
2221 end;
2223 if WordBool(PanelType and PANEL_ACID2) and (tag = GridTagAcid2) then
2224 begin
2225 if g_Collide(X, Y, Width, Height, gAcid2[a].X, gAcid2[a].Y, gAcid2[a].Width, gAcid2[a].Height) then
2226 begin
2227 result := True;
2228 exit;
2229 end;
2230 end;
2232 if WordBool(PanelType and PANEL_STEP) and (tag = GridTagStep) then
2233 begin
2234 if g_Collide(X, Y, Width, Height, gSteps[a].X, gSteps[a].Y, gSteps[a].Width, gSteps[a].Height) then
2235 begin
2236 result := True;
2237 exit;
2238 end;
2239 end;
2241 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) and (tag = GridTagLift) then
2242 begin
2243 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2244 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2245 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2246 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2247 g_Collide(X, Y, Width, Height, gLifts[a].X, gLifts[a].Y, gLifts[a].Width, gLifts[a].Height) then
2248 begin
2249 result := true;
2250 exit;
2251 end;
2252 end;
2254 if WordBool(PanelType and PANEL_BLOCKMON) and (tag = GridTagBlockMon) then
2255 begin
2256 if ((not b1x3) or ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64)) and
2257 g_Collide(X, Y, Width, Height, gBlockMon[a].X, gBlockMon[a].Y, gBlockMon[a].Width, gBlockMon[a].Height) then
2258 begin
2259 result := True;
2260 exit;
2261 end;
2262 end;
2263 end;
2265 begin
2266 //TODO: detailed profile
2267 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('wall coldet');
2268 if gdbg_map_use_accel_coldet then
2269 begin
2270 if gdbg_map_use_tree_coldet then
2271 begin
2272 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2273 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWallDoor or GridTagWater or GridTagAcid1 or GridTagAcid2 or GridTagStep or GridTagLift or GridTagBlockMon)) <> nil);
2274 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2275 begin
2276 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2277 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2278 end;
2279 end
2280 else
2281 begin
2282 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWallDoor or GridTagWater or GridTagAcid1 or GridTagAcid2 or GridTagStep or GridTagLift or GridTagBlockMon));
2283 end;
2284 end
2285 else
2286 begin
2287 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2288 end;
2289 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2290 end;
2293 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2294 var
2295 cctype: Integer = 3; // priority: 0: water, 1: acid1, 2: acid2; 3: others (nothing)
2296 texid: DWORD;
2298 // slightly different from the old code, but meh...
2299 function checker (obj: TObject; tag: Integer): Boolean;
2300 var
2301 pan: TPanel;
2302 a: Integer;
2303 begin
2304 result := false; // don't stop, ever
2305 if (tag <> GridTagWater) and (tag <> GridTagAcid1) and (tag <> GridTagAcid2) then exit;
2306 pan := (obj as TPanel);
2307 a := pan.ArrIdx;
2308 // water
2309 if (tag = GridTagWater) then
2310 begin
2311 if g_Collide(X, Y, Width, Height, gWater[a].X, gWater[a].Y, gWater[a].Width, gWater[a].Height) then
2312 begin
2313 result := true; // water has highest priority, so stop right here
2314 texid := gWater[a].GetTextureID();
2315 exit;
2316 end;
2317 end;
2318 // acid1
2319 if (cctype > 1) and (tag = GridTagAcid1) then
2320 begin
2321 if g_Collide(X, Y, Width, Height, gAcid1[a].X, gAcid1[a].Y, gAcid1[a].Width, gAcid1[a].Height) then
2322 begin
2323 cctype := 1;
2324 texid := gAcid1[a].GetTextureID();
2325 exit;
2326 end;
2327 end;
2328 // acid2
2329 if (cctype > 2) and (tag = GridTagAcid2) then
2330 begin
2331 if g_Collide(X, Y, Width, Height, gAcid2[a].X, gAcid2[a].Y, gAcid2[a].Width, gAcid2[a].Height) then
2332 begin
2333 cctype := 2;
2334 texid := gAcid2[a].GetTextureID();
2335 exit;
2336 end;
2337 end;
2338 end;
2340 begin
2341 //TODO: detailed profile?
2342 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquid coldet');
2343 if gdbg_map_use_accel_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, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2349 end
2350 else
2351 begin
2352 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2353 end;
2354 result := texid;
2355 end
2356 else
2357 begin
2358 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2359 end;
2360 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2361 end;
2363 procedure g_Map_EnableWall(ID: DWORD);
2364 begin
2365 with gWalls[ID] do
2366 begin
2367 Enabled := True;
2368 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2370 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2371 end;
2372 end;
2374 procedure g_Map_DisableWall(ID: DWORD);
2375 begin
2376 with gWalls[ID] do
2377 begin
2378 Enabled := False;
2379 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2381 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2382 end;
2383 end;
2385 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2386 var
2387 tp: TPanel;
2388 begin
2389 case PanelType of
2390 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2391 tp := gWalls[ID];
2392 PANEL_FORE:
2393 tp := gRenderForegrounds[ID];
2394 PANEL_BACK:
2395 tp := gRenderBackgrounds[ID];
2396 PANEL_WATER:
2397 tp := gWater[ID];
2398 PANEL_ACID1:
2399 tp := gAcid1[ID];
2400 PANEL_ACID2:
2401 tp := gAcid2[ID];
2402 PANEL_STEP:
2403 tp := gSteps[ID];
2404 else
2405 Exit;
2406 end;
2408 tp.NextTexture(AnimLoop);
2409 if g_Game_IsServer and g_Game_IsNet then
2410 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2411 end;
2413 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2414 begin
2415 if gLifts[ID].LiftType = t then
2416 Exit;
2418 with gLifts[ID] do
2419 begin
2420 LiftType := t;
2422 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2424 if LiftType = 0 then
2425 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2426 else if LiftType = 1 then
2427 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2428 else if LiftType = 2 then
2429 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2430 else if LiftType = 3 then
2431 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2433 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2434 end;
2435 end;
2437 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2438 var
2439 a: Integer;
2440 PointsArray: Array of TRespawnPoint;
2441 begin
2442 Result := False;
2443 SetLength(PointsArray, 0);
2445 if RespawnPoints = nil then
2446 Exit;
2448 for a := 0 to High(RespawnPoints) do
2449 if RespawnPoints[a].PointType = PointType then
2450 begin
2451 SetLength(PointsArray, Length(PointsArray)+1);
2452 PointsArray[High(PointsArray)] := RespawnPoints[a];
2453 end;
2455 if PointsArray = nil then
2456 Exit;
2458 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2459 Result := True;
2460 end;
2462 function g_Map_GetPointCount(PointType: Byte): Word;
2463 var
2464 a: Integer;
2465 begin
2466 Result := 0;
2468 if RespawnPoints = nil then
2469 Exit;
2471 for a := 0 to High(RespawnPoints) do
2472 if RespawnPoints[a].PointType = PointType then
2473 Result := Result + 1;
2474 end;
2476 function g_Map_HaveFlagPoints(): Boolean;
2477 begin
2478 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2479 end;
2481 procedure g_Map_ResetFlag(Flag: Byte);
2482 begin
2483 with gFlags[Flag] do
2484 begin
2485 Obj.X := -1000;
2486 Obj.Y := -1000;
2487 Obj.Vel.X := 0;
2488 Obj.Vel.Y := 0;
2489 Direction := D_LEFT;
2490 State := FLAG_STATE_NONE;
2491 if FlagPoints[Flag] <> nil then
2492 begin
2493 Obj.X := FlagPoints[Flag]^.X;
2494 Obj.Y := FlagPoints[Flag]^.Y;
2495 Direction := FlagPoints[Flag]^.Direction;
2496 State := FLAG_STATE_NORMAL;
2497 end;
2498 Count := -1;
2499 end;
2500 end;
2502 procedure g_Map_DrawFlags();
2503 var
2504 i, dx: Integer;
2505 Mirror: TMirrorType;
2506 begin
2507 if gGameSettings.GameMode <> GM_CTF then
2508 Exit;
2510 for i := FLAG_RED to FLAG_BLUE do
2511 with gFlags[i] do
2512 if State <> FLAG_STATE_CAPTURED then
2513 begin
2514 if State = FLAG_STATE_NONE then
2515 continue;
2517 if Direction = D_LEFT then
2518 begin
2519 Mirror := M_HORIZONTAL;
2520 dx := -1;
2521 end
2522 else
2523 begin
2524 Mirror := M_NONE;
2525 dx := 1;
2526 end;
2528 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2530 if g_debug_Frames then
2531 begin
2532 e_DrawQuad(Obj.X+Obj.Rect.X,
2533 Obj.Y+Obj.Rect.Y,
2534 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2535 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2536 0, 255, 0);
2537 end;
2538 end;
2539 end;
2541 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2542 var
2543 dw: DWORD;
2544 b: Byte;
2545 str: String;
2546 boo: Boolean;
2548 procedure SavePanelArray(var panels: TPanelArray);
2549 var
2550 PAMem: TBinMemoryWriter;
2551 i: Integer;
2552 begin
2553 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2554 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2556 i := 0;
2557 while i < Length(panels) do
2558 begin
2559 if panels[i].SaveIt then
2560 begin
2561 // ID ïàíåëè:
2562 PAMem.WriteInt(i);
2563 // Ñîõðàíÿåì ïàíåëü:
2564 panels[i].SaveState(PAMem);
2565 end;
2566 Inc(i);
2567 end;
2569 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2570 PAMem.SaveToMemory(Mem);
2571 PAMem.Free();
2572 end;
2574 procedure SaveFlag(flag: PFlag);
2575 begin
2576 // Ñèãíàòóðà ôëàãà:
2577 dw := FLAG_SIGNATURE; // 'FLAG'
2578 Mem.WriteDWORD(dw);
2579 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2580 Mem.WriteByte(flag^.RespawnType);
2581 // Ñîñòîÿíèå ôëàãà:
2582 Mem.WriteByte(flag^.State);
2583 // Íàïðàâëåíèå ôëàãà:
2584 if flag^.Direction = D_LEFT then
2585 b := 1
2586 else // D_RIGHT
2587 b := 2;
2588 Mem.WriteByte(b);
2589 // Îáúåêò ôëàãà:
2590 Obj_SaveState(@flag^.Obj, Mem);
2591 end;
2593 begin
2594 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2596 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2597 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2598 SavePanelArray(gWalls);
2599 // Ñîõðàíÿåì ïàíåëè ôîíà:
2600 SavePanelArray(gRenderBackgrounds);
2601 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2602 SavePanelArray(gRenderForegrounds);
2603 // Ñîõðàíÿåì ïàíåëè âîäû:
2604 SavePanelArray(gWater);
2605 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2606 SavePanelArray(gAcid1);
2607 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2608 SavePanelArray(gAcid2);
2609 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2610 SavePanelArray(gSteps);
2611 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2612 SavePanelArray(gLifts);
2613 ///// /////
2615 ///// Ñîõðàíÿåì ìóçûêó: /////
2616 // Ñèãíàòóðà ìóçûêè:
2617 dw := MUSIC_SIGNATURE; // 'MUSI'
2618 Mem.WriteDWORD(dw);
2619 // Íàçâàíèå ìóçûêè:
2620 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2621 if gMusic.NoMusic then
2622 str := ''
2623 else
2624 str := gMusic.Name;
2625 Mem.WriteString(str, 64);
2626 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2627 dw := gMusic.GetPosition();
2628 Mem.WriteDWORD(dw);
2629 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2630 boo := gMusic.SpecPause;
2631 Mem.WriteBoolean(boo);
2632 ///// /////
2634 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2635 Mem.WriteInt(gTotalMonsters);
2636 ///// /////
2638 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2639 if gGameSettings.GameMode = GM_CTF then
2640 begin
2641 // Ôëàã Êðàñíîé êîìàíäû:
2642 SaveFlag(@gFlags[FLAG_RED]);
2643 // Ôëàã Ñèíåé êîìàíäû:
2644 SaveFlag(@gFlags[FLAG_BLUE]);
2645 end;
2646 ///// /////
2648 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2649 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2650 begin
2651 // Î÷êè Êðàñíîé êîìàíäû:
2652 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2653 // Î÷êè Ñèíåé êîìàíäû:
2654 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2655 end;
2656 ///// /////
2657 end;
2659 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2660 var
2661 dw: DWORD;
2662 b: Byte;
2663 str: String;
2664 boo: Boolean;
2666 procedure LoadPanelArray(var panels: TPanelArray);
2667 var
2668 PAMem: TBinMemoryReader;
2669 i, id: Integer;
2670 begin
2671 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2672 PAMem := TBinMemoryReader.Create();
2673 PAMem.LoadFromMemory(Mem);
2675 for i := 0 to Length(panels)-1 do
2676 if panels[i].SaveIt then
2677 begin
2678 // ID ïàíåëè:
2679 PAMem.ReadInt(id);
2680 if id <> i then
2681 begin
2682 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2683 end;
2684 // Çàãðóæàåì ïàíåëü:
2685 panels[i].LoadState(PAMem);
2686 end;
2688 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2689 PAMem.Free();
2690 end;
2692 procedure LoadFlag(flag: PFlag);
2693 begin
2694 // Ñèãíàòóðà ôëàãà:
2695 Mem.ReadDWORD(dw);
2696 if dw <> FLAG_SIGNATURE then // 'FLAG'
2697 begin
2698 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2699 end;
2700 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2701 Mem.ReadByte(flag^.RespawnType);
2702 // Ñîñòîÿíèå ôëàãà:
2703 Mem.ReadByte(flag^.State);
2704 // Íàïðàâëåíèå ôëàãà:
2705 Mem.ReadByte(b);
2706 if b = 1 then
2707 flag^.Direction := D_LEFT
2708 else // b = 2
2709 flag^.Direction := D_RIGHT;
2710 // Îáúåêò ôëàãà:
2711 Obj_LoadState(@flag^.Obj, Mem);
2712 end;
2714 begin
2715 if Mem = nil then
2716 Exit;
2718 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2719 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2720 LoadPanelArray(gWalls);
2721 // Çàãðóæàåì ïàíåëè ôîíà:
2722 LoadPanelArray(gRenderBackgrounds);
2723 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2724 LoadPanelArray(gRenderForegrounds);
2725 // Çàãðóæàåì ïàíåëè âîäû:
2726 LoadPanelArray(gWater);
2727 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2728 LoadPanelArray(gAcid1);
2729 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2730 LoadPanelArray(gAcid2);
2731 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2732 LoadPanelArray(gSteps);
2733 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2734 LoadPanelArray(gLifts);
2735 ///// /////
2737 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2738 g_GFX_Init();
2739 mapCreateGrid();
2741 ///// Çàãðóæàåì ìóçûêó: /////
2742 // Ñèãíàòóðà ìóçûêè:
2743 Mem.ReadDWORD(dw);
2744 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2745 begin
2746 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2747 end;
2748 // Íàçâàíèå ìóçûêè:
2749 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2750 Mem.ReadString(str);
2751 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2752 Mem.ReadDWORD(dw);
2753 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2754 Mem.ReadBoolean(boo);
2755 // Çàïóñêàåì ýòó ìóçûêó:
2756 gMusic.SetByName(str);
2757 gMusic.SpecPause := boo;
2758 gMusic.Play();
2759 gMusic.Pause(True);
2760 gMusic.SetPosition(dw);
2761 ///// /////
2763 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2764 Mem.ReadInt(gTotalMonsters);
2765 ///// /////
2767 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2768 if gGameSettings.GameMode = GM_CTF then
2769 begin
2770 // Ôëàã Êðàñíîé êîìàíäû:
2771 LoadFlag(@gFlags[FLAG_RED]);
2772 // Ôëàã Ñèíåé êîìàíäû:
2773 LoadFlag(@gFlags[FLAG_BLUE]);
2774 end;
2775 ///// /////
2777 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2778 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2779 begin
2780 // Î÷êè Êðàñíîé êîìàíäû:
2781 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2782 // Î÷êè Ñèíåé êîìàíäû:
2783 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2784 end;
2785 ///// /////
2786 end;
2788 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2789 var
2790 Arr: TPanelArray;
2791 begin
2792 Result := nil;
2793 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2794 Arr := PanelByID[PanelID].PWhere^;
2795 PanelArrayID := PanelByID[PanelID].PArrID;
2796 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2797 end;
2799 end.