DEADSOFTWARE

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