DEADSOFTWARE

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