DEADSOFTWARE

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