DEADSOFTWARE

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