DEADSOFTWARE

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