DEADSOFTWARE

some fixes for recursive grid queries (grid doesn't support recursive queries, but...
[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 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
24 g_phys, wadreader, BinEditor, g_panel, g_grid, md5, binheap, xprofiler;
26 type
27 TMapInfo = record
28 Map: String;
29 Name: String;
30 Description: String;
31 Author: String;
32 MusicName: String;
33 SkyName: String;
34 Height: Word;
35 Width: Word;
36 end;
38 PRespawnPoint = ^TRespawnPoint;
39 TRespawnPoint = record
40 X, Y: Integer;
41 Direction: TDirection;
42 PointType: Byte;
43 end;
45 PFlagPoint = ^TFlagPoint;
46 TFlagPoint = TRespawnPoint;
48 PFlag = ^TFlag;
49 TFlag = record
50 Obj: TObj;
51 RespawnType: Byte;
52 State: Byte;
53 Count: Integer;
54 CaptureTime: LongWord;
55 Animation: TAnimation;
56 Direction: TDirection;
57 end;
59 function g_Map_Load(Res: String): Boolean;
60 function g_Map_GetMapInfo(Res: String): TMapInfo;
61 function g_Map_GetMapsList(WADName: String): SArray;
62 function g_Map_Exist(Res: String): Boolean;
63 procedure g_Map_Free();
64 procedure g_Map_Update();
66 procedure g_Map_DrawPanels (PanelType: Word); // unaccelerated
67 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
69 procedure g_Map_DrawBack(dx, dy: Integer);
70 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
71 PanelType: Word; b1x3: Boolean=false): Boolean;
72 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
73 procedure g_Map_EnableWall(ID: DWORD);
74 procedure g_Map_DisableWall(ID: DWORD);
75 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
76 procedure g_Map_SetLift(ID: DWORD; t: Integer);
77 procedure g_Map_ReAdd_DieTriggers();
78 function g_Map_IsSpecialTexture(Texture: String): Boolean;
80 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
81 function g_Map_GetPointCount(PointType: Byte): Word;
83 function g_Map_HaveFlagPoints(): Boolean;
85 procedure g_Map_ResetFlag(Flag: Byte);
86 procedure g_Map_DrawFlags();
88 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
90 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
91 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
93 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
95 // returns panel or nil
96 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
97 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
99 // returns panel or nil
100 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
101 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
103 type
104 TForEachPanelCB = function (pan: TPanel): Boolean; // return `true` to stop
106 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
107 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
109 // trace liquid, stepping by `dx` and `dy`
110 // return last seen liquid coords, and `false` if we're started outside of the liquid
111 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
114 procedure g_Map_ProfilersBegin ();
115 procedure g_Map_ProfilersEnd ();
118 const
119 RESPAWNPOINT_PLAYER1 = 1;
120 RESPAWNPOINT_PLAYER2 = 2;
121 RESPAWNPOINT_DM = 3;
122 RESPAWNPOINT_RED = 4;
123 RESPAWNPOINT_BLUE = 5;
125 FLAG_NONE = 0;
126 FLAG_RED = 1;
127 FLAG_BLUE = 2;
128 FLAG_DOM = 3;
130 FLAG_STATE_NONE = 0;
131 FLAG_STATE_NORMAL = 1;
132 FLAG_STATE_DROPPED = 2;
133 FLAG_STATE_CAPTURED = 3;
134 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
135 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
137 FLAG_TIME = 720; // 20 seconds
139 SKY_STRETCH: Single = 1.5;
141 const
142 GridTagInvalid = 0;
144 (* draw order:
145 PANEL_BACK
146 PANEL_STEP
147 PANEL_WALL
148 PANEL_CLOSEDOOR
149 PANEL_ACID1
150 PANEL_ACID2
151 PANEL_WATER
152 PANEL_FORE
153 *)
154 // sorted by draw priority
155 GridTagBack = 1 shl 0;
156 GridTagStep = 1 shl 1;
157 GridTagWall = 1 shl 2;
158 GridTagDoor = 1 shl 3;
159 GridTagAcid1 = 1 shl 4;
160 GridTagAcid2 = 1 shl 5;
161 GridTagWater = 1 shl 6;
162 GridTagFore = 1 shl 7;
163 // the following are invisible
164 GridTagLift = 1 shl 8;
165 GridTagBlockMon = 1 shl 9;
167 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
170 var
171 gWalls: TPanelArray;
172 gRenderBackgrounds: TPanelArray;
173 gRenderForegrounds: TPanelArray;
174 gWater, gAcid1, gAcid2: TPanelArray;
175 gSteps: TPanelArray;
176 gLifts: TPanelArray;
177 gBlockMon: TPanelArray;
178 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
179 //gDOMFlags: array of TFlag;
180 gMapInfo: TMapInfo;
181 gBackSize: TPoint;
182 gDoorMap: array of array of DWORD;
183 gLiftMap: array of array of DWORD;
184 gWADHash: TMD5Digest;
185 BackID: DWORD = DWORD(-1);
186 gExternalResources: TStringList;
188 gdbg_map_use_accel_render: Boolean = true;
189 gdbg_map_use_accel_coldet: Boolean = true;
190 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
191 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
194 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
197 type
198 TPanelGrid = specialize TBodyGridBase<TPanel>;
200 var
201 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
204 implementation
206 uses
207 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
208 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
209 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
210 Math, g_monsters, g_saveload, g_language, g_netmsg,
211 utils, sfs,
212 ImagingTypes, Imaging, ImagingUtility,
213 ImagingGif, ImagingNetworkGraphics;
215 const
216 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
217 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
218 FLAG_SIGNATURE = $47414C46; // 'FLAG'
221 function panelTypeToTag (panelType: Word): Integer;
222 begin
223 case panelType of
224 PANEL_WALL: result := GridTagWall; // gWalls
225 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
226 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
227 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
228 PANEL_WATER: result := GridTagWater; // gWater
229 PANEL_ACID1: result := GridTagAcid1; // gAcid1
230 PANEL_ACID2: result := GridTagAcid2; // gAcid2
231 PANEL_STEP: result := GridTagStep; // gSteps
232 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
233 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
234 else result := GridTagInvalid;
235 end;
236 end;
239 function dplLess (a, b: TObject): Boolean;
240 var
241 pa, pb: TPanel;
242 begin
243 pa := TPanel(a);
244 pb := TPanel(b);
245 if (pa.tag < pb.tag) then begin result := true; exit; end;
246 if (pa.tag > pb.tag) then begin result := false; exit; end;
247 result := (pa.arrIdx < pb.arrIdx);
248 end;
250 procedure dplClear ();
251 begin
252 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
253 end;
256 type
257 TPanelID = record
258 PWhere: ^TPanelArray;
259 PArrID: Integer;
260 end;
262 var
263 PanelById: array of TPanelID;
264 Textures: TLevelTextureArray;
265 RespawnPoints: Array of TRespawnPoint;
266 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
267 //DOMFlagPoints: Array of TFlagPoint;
270 procedure g_Map_ProfilersBegin ();
271 begin
272 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
273 profMapCollision.mainBegin(g_profile_collision);
274 // create sections
275 if g_profile_collision then
276 begin
277 profMapCollision.sectionBegin('*solids');
278 profMapCollision.sectionEnd();
279 profMapCollision.sectionBegin('liquids');
280 profMapCollision.sectionEnd();
281 end;
282 end;
284 procedure g_Map_ProfilersEnd ();
285 begin
286 if (profMapCollision <> nil) then profMapCollision.mainEnd();
287 end;
290 // wall index in `gWalls` or -1
291 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
292 var
293 ex, ey: Integer;
294 begin
295 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
296 if (result <> nil) then
297 begin
298 if (hitx <> nil) then hitx^ := ex;
299 if (hity <> nil) then hity^ := ey;
300 end
301 else
302 begin
303 if (hitx <> nil) then hitx^ := x1;
304 if (hity <> nil) then hity^ := y1;
305 end;
306 end;
308 // returns panel or nil
309 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
310 var
311 ex, ey: Integer;
312 begin
313 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
314 if (result <> nil) then
315 begin
316 if (hitx <> nil) then hitx^ := ex;
317 if (hity <> nil) then hity^ := ey;
318 end
319 else
320 begin
321 if (hitx <> nil) then hitx^ := x1;
322 if (hity <> nil) then hity^ := y1;
323 end;
324 end;
327 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
329 function checker (pan: TPanel; tag: Integer): Boolean;
330 begin
332 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
333 begin
334 result := pan.Enabled; // stop if wall is enabled
335 exit;
336 end;
339 if ((tag and GridTagLift) <> 0) then
340 begin
341 // stop if the lift of the right type
342 result :=
343 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
344 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
345 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
346 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
347 exit;
348 end;
350 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
351 end;
353 var
354 tagmask: Integer = 0;
355 begin
356 result := false;
358 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
359 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
360 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
361 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
362 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
363 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
364 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
366 if (tagmask = 0) then exit;// just in case
367 if ((tagmask and GridTagLift) <> 0) then
368 begin
369 // slow
370 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
371 end
372 else
373 begin
374 // fast
375 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
376 end;
377 end;
380 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
381 begin
382 result := nil;
383 if (tagmask = 0) then exit;
384 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
385 end;
388 function g_Map_IsSpecialTexture(Texture: String): Boolean;
389 begin
390 Result := (Texture = TEXTURE_NAME_WATER) or
391 (Texture = TEXTURE_NAME_ACID1) or
392 (Texture = TEXTURE_NAME_ACID2);
393 end;
395 procedure CreateDoorMap();
396 var
397 PanelArray: Array of record
398 X, Y: Integer;
399 Width, Height: Word;
400 Active: Boolean;
401 PanelID: DWORD;
402 end;
403 a, b, c, m, i, len: Integer;
404 ok: Boolean;
405 begin
406 if gWalls = nil then
407 Exit;
409 i := 0;
410 len := 128;
411 SetLength(PanelArray, len);
413 for a := 0 to High(gWalls) do
414 if gWalls[a].Door then
415 begin
416 PanelArray[i].X := gWalls[a].X;
417 PanelArray[i].Y := gWalls[a].Y;
418 PanelArray[i].Width := gWalls[a].Width;
419 PanelArray[i].Height := gWalls[a].Height;
420 PanelArray[i].Active := True;
421 PanelArray[i].PanelID := a;
423 i := i + 1;
424 if i = len then
425 begin
426 len := len + 128;
427 SetLength(PanelArray, len);
428 end;
429 end;
431 // Íåò äâåðåé:
432 if i = 0 then
433 begin
434 PanelArray := nil;
435 Exit;
436 end;
438 SetLength(gDoorMap, 0);
440 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
442 for a := 0 to i-1 do
443 if PanelArray[a].Active then
444 begin
445 PanelArray[a].Active := False;
446 m := Length(gDoorMap);
447 SetLength(gDoorMap, m+1);
448 SetLength(gDoorMap[m], 1);
449 gDoorMap[m, 0] := PanelArray[a].PanelID;
450 ok := True;
452 while ok do
453 begin
454 ok := False;
456 for b := 0 to i-1 do
457 if PanelArray[b].Active then
458 for c := 0 to High(gDoorMap[m]) do
459 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
460 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
461 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
462 PanelArray[b].Width, PanelArray[b].Height,
463 gWalls[gDoorMap[m, c]].X,
464 gWalls[gDoorMap[m, c]].Y,
465 gWalls[gDoorMap[m, c]].Width,
466 gWalls[gDoorMap[m, c]].Height) then
467 begin
468 PanelArray[b].Active := False;
469 SetLength(gDoorMap[m],
470 Length(gDoorMap[m])+1);
471 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
472 ok := True;
473 Break;
474 end;
475 end;
477 g_Game_StepLoading();
478 end;
480 PanelArray := nil;
481 end;
483 procedure CreateLiftMap();
484 var
485 PanelArray: Array of record
486 X, Y: Integer;
487 Width, Height: Word;
488 Active: Boolean;
489 end;
490 a, b, c, len, i, j: Integer;
491 ok: Boolean;
492 begin
493 if gLifts = nil then
494 Exit;
496 len := Length(gLifts);
497 SetLength(PanelArray, len);
499 for a := 0 to len-1 do
500 begin
501 PanelArray[a].X := gLifts[a].X;
502 PanelArray[a].Y := gLifts[a].Y;
503 PanelArray[a].Width := gLifts[a].Width;
504 PanelArray[a].Height := gLifts[a].Height;
505 PanelArray[a].Active := True;
506 end;
508 SetLength(gLiftMap, len);
509 i := 0;
511 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
513 for a := 0 to len-1 do
514 if PanelArray[a].Active then
515 begin
516 PanelArray[a].Active := False;
517 SetLength(gLiftMap[i], 32);
518 j := 0;
519 gLiftMap[i, j] := a;
520 ok := True;
522 while ok do
523 begin
524 ok := False;
525 for b := 0 to len-1 do
526 if PanelArray[b].Active then
527 for c := 0 to j do
528 if g_CollideAround(PanelArray[b].X,
529 PanelArray[b].Y,
530 PanelArray[b].Width,
531 PanelArray[b].Height,
532 PanelArray[gLiftMap[i, c]].X,
533 PanelArray[gLiftMap[i, c]].Y,
534 PanelArray[gLiftMap[i, c]].Width,
535 PanelArray[gLiftMap[i, c]].Height) then
536 begin
537 PanelArray[b].Active := False;
538 j := j+1;
539 if j > High(gLiftMap[i]) then
540 SetLength(gLiftMap[i],
541 Length(gLiftMap[i])+32);
543 gLiftMap[i, j] := b;
544 ok := True;
546 Break;
547 end;
548 end;
550 SetLength(gLiftMap[i], j+1);
551 i := i+1;
553 g_Game_StepLoading();
554 end;
556 SetLength(gLiftMap, i);
558 PanelArray := nil;
559 end;
561 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
562 CurTex: Integer; sav: Boolean): Integer;
563 var
564 len: Integer;
565 panels: ^TPanelArray;
566 begin
567 Result := -1;
569 case PanelRec.PanelType of
570 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
571 panels := @gWalls;
572 PANEL_BACK:
573 panels := @gRenderBackgrounds;
574 PANEL_FORE:
575 panels := @gRenderForegrounds;
576 PANEL_WATER:
577 panels := @gWater;
578 PANEL_ACID1:
579 panels := @gAcid1;
580 PANEL_ACID2:
581 panels := @gAcid2;
582 PANEL_STEP:
583 panels := @gSteps;
584 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
585 panels := @gLifts;
586 PANEL_BLOCKMON:
587 panels := @gBlockMon;
588 else
589 Exit;
590 end;
592 len := Length(panels^);
593 SetLength(panels^, len + 1);
595 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
596 panels^[len].arrIdx := len;
597 panels^[len].proxyId := -1;
598 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
599 if sav then
600 panels^[len].SaveIt := True;
602 Result := len;
604 len := Length(PanelByID);
605 SetLength(PanelByID, len + 1);
606 PanelByID[len].PWhere := panels;
607 PanelByID[len].PArrID := Result;
608 end;
610 function CreateNullTexture(RecName: String): Integer;
611 begin
612 SetLength(Textures, Length(Textures)+1);
613 result := High(Textures);
615 with Textures[High(Textures)] do
616 begin
617 TextureName := RecName;
618 Width := 1;
619 Height := 1;
620 Anim := False;
621 TextureID := TEXTURE_NONE;
622 end;
623 end;
625 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
626 var
627 WAD: TWADFile;
628 TextureData: Pointer;
629 WADName, txname: String;
630 a, ResLength: Integer;
631 begin
632 Result := -1;
634 if Textures <> nil then
635 for a := 0 to High(Textures) do
636 if Textures[a].TextureName = RecName then
637 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
638 Result := a;
639 Exit;
640 end;
642 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
643 if (RecName = TEXTURE_NAME_WATER) or
644 (RecName = TEXTURE_NAME_ACID1) or
645 (RecName = TEXTURE_NAME_ACID2) then
646 begin
647 SetLength(Textures, Length(Textures)+1);
649 with Textures[High(Textures)] do
650 begin
651 TextureName := RecName;
653 if TextureName = TEXTURE_NAME_WATER then
654 TextureID := TEXTURE_SPECIAL_WATER
655 else
656 if TextureName = TEXTURE_NAME_ACID1 then
657 TextureID := TEXTURE_SPECIAL_ACID1
658 else
659 if TextureName = TEXTURE_NAME_ACID2 then
660 TextureID := TEXTURE_SPECIAL_ACID2;
662 Anim := False;
663 end;
665 result := High(Textures);
666 Exit;
667 end;
669 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
670 WADName := g_ExtractWadName(RecName);
672 WAD := TWADFile.Create();
674 if WADName <> '' then
675 WADName := GameDir+'/wads/'+WADName
676 else
677 WADName := Map;
679 WAD.ReadFile(WADName);
681 txname := RecName;
683 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
684 begin
685 FreeMem(TextureData);
686 RecName := 'COMMON\ALIEN';
687 end;
690 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
691 begin
692 SetLength(Textures, Length(Textures)+1);
693 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
694 Exit;
695 e_GetTextureSize(Textures[High(Textures)].TextureID,
696 @Textures[High(Textures)].Width,
697 @Textures[High(Textures)].Height);
698 FreeMem(TextureData);
699 Textures[High(Textures)].TextureName := {RecName}txname;
700 Textures[High(Textures)].Anim := False;
702 result := High(Textures);
703 end
704 else // Íåò òàêîãî ðåóñðñà â WAD'å
705 begin
706 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
707 if log then
708 begin
709 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
710 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
711 end;
712 end;
714 WAD.Free();
715 end;
717 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
718 var
719 WAD: TWADFile;
720 TextureWAD: PChar = nil;
721 TextData: Pointer = nil;
722 TextureData: Pointer = nil;
723 cfg: TConfig = nil;
724 WADName: String;
725 ResLength: Integer;
726 TextureResource: String;
727 _width, _height, _framecount, _speed: Integer;
728 _backanimation: Boolean;
729 //imgfmt: string;
730 ia: TDynImageDataArray = nil;
731 f, c, frdelay, frloop: Integer;
732 begin
733 result := -1;
735 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
737 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
738 WADName := g_ExtractWadName(RecName);
740 WAD := TWADFile.Create();
741 try
742 if WADName <> '' then
743 WADName := GameDir+'/wads/'+WADName
744 else
745 WADName := Map;
747 WAD.ReadFile(WADName);
749 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
750 begin
751 if log then
752 begin
753 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
754 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
755 end;
756 exit;
757 end;
759 {TEST
760 if WADName = Map then
761 begin
762 //FreeMem(TextureWAD);
763 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
764 end;
767 WAD.FreeWAD();
769 if ResLength < 6 then
770 begin
771 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
772 exit;
773 end;
775 // ýòî ïòèöà? ýòî ñàìîë¸ò?
776 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
777 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
778 begin
779 // íåò, ýòî ñóïåðìåí!
780 if not WAD.ReadMemory(TextureWAD, ResLength) then
781 begin
782 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
783 exit;
784 end;
786 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
787 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
788 begin
789 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
790 exit;
791 end;
793 cfg := TConfig.CreateMem(TextData, ResLength);
795 TextureResource := cfg.ReadStr('', 'resource', '');
796 if TextureResource = '' then
797 begin
798 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
799 exit;
800 end;
802 _width := cfg.ReadInt('', 'framewidth', 0);
803 _height := cfg.ReadInt('', 'frameheight', 0);
804 _framecount := cfg.ReadInt('', 'framecount', 0);
805 _speed := cfg.ReadInt('', 'waitcount', 0);
806 _backanimation := cfg.ReadBool('', 'backanimation', False);
808 cfg.Free();
809 cfg := nil;
811 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
812 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
813 begin
814 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
815 exit;
816 end;
818 WAD.Free();
819 WAD := nil;
821 SetLength(Textures, Length(Textures)+1);
822 with Textures[High(Textures)] do
823 begin
824 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
825 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
826 begin
827 TextureName := RecName;
828 Width := _width;
829 Height := _height;
830 Anim := True;
831 FramesCount := _framecount;
832 Speed := _speed;
833 result := High(Textures);
834 end
835 else
836 begin
837 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
838 end;
839 end;
840 end
841 else
842 begin
843 // try animated image
845 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
846 if length(imgfmt) = 0 then
847 begin
848 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
849 exit;
850 end;
852 GlobalMetadata.ClearMetaItems();
853 GlobalMetadata.ClearMetaItemsForSaving();
854 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
855 begin
856 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
857 exit;
858 end;
859 if length(ia) = 0 then
860 begin
861 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
862 exit;
863 end;
865 WAD.Free();
866 WAD := nil;
868 _width := ia[0].width;
869 _height := ia[0].height;
870 _framecount := length(ia);
871 _speed := 1;
872 _backanimation := false;
873 frdelay := -1;
874 frloop := -666;
875 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
876 begin
877 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
878 try
879 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
880 frdelay := f;
881 if f < 0 then f := 0;
882 // rounding ;-)
883 c := f mod 28;
884 if c < 13 then c := 0 else c := 1;
885 f := (f div 28)+c;
886 if f < 1 then f := 1 else if f > 255 then f := 255;
887 _speed := f;
888 except
889 end;
890 end;
891 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
892 begin
893 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
894 try
895 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
896 frloop := f;
897 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
898 except
899 end;
900 end;
901 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
902 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
903 f := ord(_backanimation);
904 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);
906 SetLength(Textures, Length(Textures)+1);
907 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
908 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
909 begin
910 Textures[High(Textures)].TextureName := RecName;
911 Textures[High(Textures)].Width := _width;
912 Textures[High(Textures)].Height := _height;
913 Textures[High(Textures)].Anim := True;
914 Textures[High(Textures)].FramesCount := length(ia);
915 Textures[High(Textures)].Speed := _speed;
916 result := High(Textures);
917 //writeln(' CREATED!');
918 end
919 else
920 begin
921 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
922 end;
923 end;
924 finally
925 for f := 0 to High(ia) do FreeImage(ia[f]);
926 WAD.Free();
927 cfg.Free();
928 if TextureWAD <> nil then FreeMem(TextureWAD);
929 if TextData <> nil then FreeMem(TextData);
930 if TextureData <> nil then FreeMem(TextureData);
931 end;
932 end;
934 procedure CreateItem(Item: TItemRec_1);
935 begin
936 if g_Game_IsClient then Exit;
938 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
939 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
940 Exit;
942 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
943 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
944 end;
946 procedure CreateArea(Area: TAreaRec_1);
947 var
948 a: Integer;
949 id: DWORD = 0;
950 begin
951 case Area.AreaType of
952 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
953 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
954 begin
955 SetLength(RespawnPoints, Length(RespawnPoints)+1);
956 with RespawnPoints[High(RespawnPoints)] do
957 begin
958 X := Area.X;
959 Y := Area.Y;
960 Direction := TDirection(Area.Direction);
962 case Area.AreaType of
963 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
964 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
965 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
966 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
967 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
968 end;
969 end;
970 end;
972 AREA_REDFLAG, AREA_BLUEFLAG:
973 begin
974 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
976 if FlagPoints[a] <> nil then Exit;
978 New(FlagPoints[a]);
980 with FlagPoints[a]^ do
981 begin
982 X := Area.X-FLAGRECT.X;
983 Y := Area.Y-FLAGRECT.Y;
984 Direction := TDirection(Area.Direction);
985 end;
987 with gFlags[a] do
988 begin
989 case a of
990 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
991 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
992 end;
994 Animation := TAnimation.Create(id, True, 8);
995 Obj.Rect := FLAGRECT;
997 g_Map_ResetFlag(a);
998 end;
999 end;
1001 AREA_DOMFLAG:
1002 begin
1003 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1004 with DOMFlagPoints[High(DOMFlagPoints)] do
1005 begin
1006 X := Area.X;
1007 Y := Area.Y;
1008 Direction := TDirection(Area.Direction);
1009 end;
1011 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1012 end;
1013 end;
1014 end;
1016 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
1017 var
1018 _trigger: TTrigger;
1019 begin
1020 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1022 with _trigger do
1023 begin
1024 X := Trigger.X;
1025 Y := Trigger.Y;
1026 Width := Trigger.Width;
1027 Height := Trigger.Height;
1028 Enabled := ByteBool(Trigger.Enabled);
1029 TexturePanel := Trigger.TexturePanel;
1030 TexturePanelType := fTexturePanel1Type;
1031 ShotPanelType := fTexturePanel2Type;
1032 TriggerType := Trigger.TriggerType;
1033 ActivateType := Trigger.ActivateType;
1034 Keys := Trigger.Keys;
1035 Data.Default := Trigger.DATA;
1036 end;
1038 g_Triggers_Create(_trigger);
1039 end;
1041 procedure CreateMonster(monster: TMonsterRec_1);
1042 var
1043 a: Integer;
1044 mon: TMonster;
1045 begin
1046 if g_Game_IsClient then Exit;
1048 if (gGameSettings.GameType = GT_SINGLE)
1049 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1050 begin
1051 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1053 if gTriggers <> nil then
1054 begin
1055 for a := 0 to High(gTriggers) do
1056 begin
1057 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1058 begin
1059 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1060 end;
1061 end;
1062 end;
1064 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1065 end;
1066 end;
1068 procedure g_Map_ReAdd_DieTriggers();
1070 function monsDieTrig (mon: TMonster): Boolean;
1071 var
1072 a: Integer;
1073 begin
1074 result := false; // don't stop
1075 mon.ClearTriggers();
1076 for a := 0 to High(gTriggers) do
1077 begin
1078 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1079 begin
1080 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1081 end;
1082 end;
1083 end;
1085 begin
1086 if g_Game_IsClient then Exit;
1088 g_Mons_ForEach(monsDieTrig);
1089 end;
1091 function extractWadName(resourceName: string): string;
1092 var
1093 posN: Integer;
1094 begin
1095 posN := Pos(':', resourceName);
1096 if posN > 0 then
1097 Result:= Copy(resourceName, 0, posN-1)
1098 else
1099 Result := '';
1100 end;
1102 procedure addResToExternalResList(res: string);
1103 begin
1104 res := extractWadName(res);
1105 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1106 gExternalResources.Add(res);
1107 end;
1109 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1110 var
1111 textures: TTexturesRec1Array;
1112 mapHeader: TMapHeaderRec_1;
1113 i: integer;
1114 resFile: String = '';
1115 begin
1116 if gExternalResources = nil then
1117 gExternalResources := TStringList.Create;
1119 gExternalResources.Clear;
1120 textures := mapReader.GetTextures();
1121 for i := 0 to High(textures) do
1122 begin
1123 addResToExternalResList(resFile);
1124 end;
1126 textures := nil;
1128 mapHeader := mapReader.GetMapHeader;
1130 addResToExternalResList(mapHeader.MusicName);
1131 addResToExternalResList(mapHeader.SkyName);
1132 end;
1135 procedure mapCreateGrid ();
1136 var
1137 mapX0: Integer = $3fffffff;
1138 mapY0: Integer = $3fffffff;
1139 mapX1: Integer = -$3fffffff;
1140 mapY1: Integer = -$3fffffff;
1142 procedure calcBoundingBox (constref panels: TPanelArray);
1143 var
1144 idx: Integer;
1145 pan: TPanel;
1146 begin
1147 for idx := 0 to High(panels) do
1148 begin
1149 pan := panels[idx];
1150 if not pan.visvalid then continue;
1151 if (pan.Width < 1) or (pan.Height < 1) then continue;
1152 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1153 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1154 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1155 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1156 end;
1157 end;
1159 procedure addPanelsToGrid (constref panels: TPanelArray);
1160 var
1161 idx: Integer;
1162 pan: TPanel;
1163 newtag: Integer;
1164 begin
1165 //tag := panelTypeToTag(tag);
1166 for idx := 0 to High(panels) do
1167 begin
1168 pan := panels[idx];
1169 if not pan.visvalid then continue;
1170 if (pan.proxyId <> -1) then
1171 begin
1172 {$IF DEFINED(D2F_DEBUG)}
1173 e_WriteLog(Format('DUPLICATE wall #%d(%d) enabled (%d); type:%08x', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.PanelType]), MSG_NOTIFY);
1174 {$ENDIF}
1175 continue;
1176 end;
1177 case pan.PanelType of
1178 PANEL_WALL: newtag := GridTagWall;
1179 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1180 PANEL_BACK: newtag := GridTagBack;
1181 PANEL_FORE: newtag := GridTagFore;
1182 PANEL_WATER: newtag := GridTagWater;
1183 PANEL_ACID1: newtag := GridTagAcid1;
1184 PANEL_ACID2: newtag := GridTagAcid2;
1185 PANEL_STEP: newtag := GridTagStep;
1186 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1187 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1188 else continue; // oops
1189 end;
1190 pan.tag := newtag;
1192 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1193 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1194 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1195 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1197 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1198 begin
1199 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1200 end;
1202 {$ENDIF}
1203 end;
1204 end;
1206 begin
1207 mapGrid.Free();
1208 mapGrid := nil;
1210 calcBoundingBox(gWalls);
1211 calcBoundingBox(gRenderBackgrounds);
1212 calcBoundingBox(gRenderForegrounds);
1213 calcBoundingBox(gWater);
1214 calcBoundingBox(gAcid1);
1215 calcBoundingBox(gAcid2);
1216 calcBoundingBox(gSteps);
1217 calcBoundingBox(gLifts);
1218 calcBoundingBox(gBlockMon);
1220 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1222 if (mapX0 > 0) then mapX0 := 0;
1223 if (mapY0 > 0) then mapY0 := 0;
1225 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1226 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1228 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1229 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1231 addPanelsToGrid(gWalls);
1232 addPanelsToGrid(gRenderBackgrounds);
1233 addPanelsToGrid(gRenderForegrounds);
1234 addPanelsToGrid(gWater);
1235 addPanelsToGrid(gAcid1);
1236 addPanelsToGrid(gAcid2);
1237 addPanelsToGrid(gSteps);
1238 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1239 addPanelsToGrid(gBlockMon);
1241 mapGrid.dumpStats();
1243 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1244 end;
1247 function g_Map_Load(Res: String): Boolean;
1248 const
1249 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1250 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1251 var
1252 WAD: TWADFile;
1253 MapReader: TMapReader_1;
1254 Header: TMapHeaderRec_1;
1255 _textures: TTexturesRec1Array;
1256 _texnummap: array of Integer; // `_textures` -> `Textures`
1257 panels: TPanelsRec1Array;
1258 items: TItemsRec1Array;
1259 monsters: TMonsterRec1Array;
1260 areas: TAreasRec1Array;
1261 triggers: TTriggersRec1Array;
1262 a, b, c, k: Integer;
1263 PanelID: DWORD;
1264 AddTextures: TAddTextureArray;
1265 texture: TTextureRec_1;
1266 TriggersTable: Array of record
1267 TexturePanel: Integer;
1268 LiftPanel: Integer;
1269 DoorPanel: Integer;
1270 ShotPanel: Integer;
1271 end;
1272 FileName, mapResName, s, TexName: String;
1273 Data: Pointer;
1274 Len: Integer;
1275 ok, isAnim, trigRef: Boolean;
1276 CurTex, ntn: Integer;
1278 begin
1279 mapGrid.Free();
1280 mapGrid := nil;
1281 //mapTree.Free();
1282 //mapTree := nil;
1284 Result := False;
1285 gMapInfo.Map := Res;
1286 TriggersTable := nil;
1287 FillChar(texture, SizeOf(texture), 0);
1289 sfsGCDisable(); // temporary disable removing of temporary volumes
1290 try
1291 // Çàãðóçêà WAD:
1292 FileName := g_ExtractWadName(Res);
1293 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1294 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1296 WAD := TWADFile.Create();
1297 if not WAD.ReadFile(FileName) then
1298 begin
1299 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1300 WAD.Free();
1301 Exit;
1302 end;
1303 //k8: why loader ignores path here?
1304 mapResName := g_ExtractFileName(Res);
1305 if not WAD.GetMapResource(mapResName, Data, Len) then
1306 begin
1307 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1308 WAD.Free();
1309 Exit;
1310 end;
1312 WAD.Free();
1314 // Çàãðóçêà êàðòû:
1315 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1316 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1317 MapReader := TMapReader_1.Create();
1319 if not MapReader.LoadMap(Data) then
1320 begin
1321 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1322 FreeMem(Data);
1323 MapReader.Free();
1324 Exit;
1325 end;
1327 FreeMem(Data);
1328 generateExternalResourcesList(MapReader);
1329 // Çàãðóçêà òåêñòóð:
1330 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1331 _textures := MapReader.GetTextures();
1332 _texnummap := nil;
1334 // Çàãðóçêà îïèñàíèÿ êàðòû:
1335 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1336 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1337 Header := MapReader.GetMapHeader();
1339 with gMapInfo do
1340 begin
1341 Name := Header.MapName;
1342 Description := Header.MapDescription;
1343 Author := Header.MapAuthor;
1344 MusicName := Header.MusicName;
1345 SkyName := Header.SkyName;
1346 Height := Header.Height;
1347 Width := Header.Width;
1348 end;
1350 // Äîáàâëåíèå òåêñòóð â Textures[]:
1351 if _textures <> nil then
1352 begin
1353 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1354 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1355 SetLength(_texnummap, length(_textures));
1357 for a := 0 to High(_textures) do
1358 begin
1359 SetLength(s, 64);
1360 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1361 for b := 1 to Length(s) do
1362 begin
1363 if s[b] = #0 then
1364 begin
1365 SetLength(s, b-1);
1366 Break;
1367 end;
1368 end;
1369 {$IF DEFINED(D2F_DEBUG)}
1370 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1371 {$ENDIF}
1372 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1373 // Àíèìèðîâàííàÿ òåêñòóðà:
1374 if ByteBool(_textures[a].Anim) then
1375 begin
1376 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1377 if ntn < 0 then
1378 begin
1379 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1380 ntn := CreateNullTexture(_textures[a].Resource);
1381 end;
1382 end
1383 else // Îáû÷íàÿ òåêñòóðà:
1384 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1385 if ntn < 0 then
1386 begin
1387 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1388 ntn := CreateNullTexture(_textures[a].Resource);
1389 end;
1391 _texnummap[a] := ntn; // fix texture number
1392 g_Game_StepLoading();
1393 end;
1394 end;
1396 // Çàãðóçêà òðèããåðîâ:
1397 gTriggerClientID := 0;
1398 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1399 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1400 triggers := MapReader.GetTriggers();
1402 // Çàãðóçêà ïàíåëåé:
1403 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1404 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1405 panels := MapReader.GetPanels();
1407 // check texture numbers for panels
1408 for a := 0 to High(panels) do
1409 begin
1410 if panels[a].TextureNum > High(_textures) then
1411 begin
1412 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1413 result := false;
1414 exit;
1415 end;
1416 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1417 end;
1419 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1420 if triggers <> nil then
1421 begin
1422 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1423 SetLength(TriggersTable, Length(triggers));
1424 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1426 for a := 0 to High(TriggersTable) do
1427 begin
1428 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1429 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1430 // Ëèôòû:
1431 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1432 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1433 else
1434 TriggersTable[a].LiftPanel := -1;
1435 // Äâåðè:
1436 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1437 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1438 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1439 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1440 else
1441 TriggersTable[a].DoorPanel := -1;
1442 // Òóðåëü:
1443 if triggers[a].TriggerType = TRIGGER_SHOT then
1444 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1445 else
1446 TriggersTable[a].ShotPanel := -1;
1448 g_Game_StepLoading();
1449 end;
1450 end;
1452 // Ñîçäàåì ïàíåëè:
1453 if panels <> nil then
1454 begin
1455 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1456 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1458 for a := 0 to High(panels) do
1459 begin
1460 SetLength(AddTextures, 0);
1461 trigRef := False;
1462 CurTex := -1;
1463 if _textures <> nil then
1464 begin
1465 texture := _textures[panels[a].TextureNum];
1466 ok := True;
1467 end
1468 else
1469 ok := False;
1471 if ok then
1472 begin
1473 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1474 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1475 ok := False;
1476 if (TriggersTable <> nil) and (_textures <> nil) then
1477 for b := 0 to High(TriggersTable) do
1478 if (TriggersTable[b].TexturePanel = a)
1479 or (TriggersTable[b].ShotPanel = a) then
1480 begin
1481 trigRef := True;
1482 ok := True;
1483 Break;
1484 end;
1485 end;
1487 if ok then
1488 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1489 SetLength(s, 64);
1490 CopyMemory(@s[1], @texture.Resource[0], 64);
1491 // Èçìåðÿåì äëèíó:
1492 Len := Length(s);
1493 for c := Len downto 1 do
1494 if s[c] <> #0 then
1495 begin
1496 Len := c;
1497 Break;
1498 end;
1499 SetLength(s, Len);
1501 // Ñïåö-òåêñòóðû çàïðåùåíû:
1502 if g_Map_IsSpecialTexture(s) then
1503 ok := False
1504 else
1505 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1506 ok := g_Texture_NumNameFindStart(s);
1508 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1509 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1510 if ok then
1511 begin
1512 k := NNF_NAME_BEFORE;
1513 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1514 while ok or (k = NNF_NAME_BEFORE) or
1515 (k = NNF_NAME_EQUALS) do
1516 begin
1517 k := g_Texture_NumNameFindNext(TexName);
1519 if (k = NNF_NAME_BEFORE) or
1520 (k = NNF_NAME_AFTER) then
1521 begin
1522 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1523 if ByteBool(texture.Anim) then
1524 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1525 isAnim := True;
1526 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1527 if not ok then
1528 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1529 isAnim := False;
1530 ok := CreateTexture(TexName, FileName, False) >= 0;
1531 end;
1532 end
1533 else
1534 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1535 isAnim := False;
1536 ok := CreateTexture(TexName, FileName, False) >= 0;
1537 if not ok then
1538 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1539 isAnim := True;
1540 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1541 end;
1542 end;
1544 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1545 if ok then
1546 begin
1547 for c := 0 to High(Textures) do
1548 if Textures[c].TextureName = TexName then
1549 begin
1550 SetLength(AddTextures, Length(AddTextures)+1);
1551 AddTextures[High(AddTextures)].Texture := c;
1552 AddTextures[High(AddTextures)].Anim := isAnim;
1553 Break;
1554 end;
1555 end;
1556 end
1557 else
1558 if k = NNF_NAME_EQUALS then
1559 begin
1560 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1561 SetLength(AddTextures, Length(AddTextures)+1);
1562 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1563 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1564 CurTex := High(AddTextures);
1565 ok := True;
1566 end
1567 else // NNF_NO_NAME
1568 ok := False;
1569 end; // while ok...
1571 ok := True;
1572 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1573 end; // if ok - ññûëàþòñÿ òðèããåðû
1575 if not ok then
1576 begin
1577 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1578 SetLength(AddTextures, 1);
1579 AddTextures[0].Texture := panels[a].TextureNum;
1580 AddTextures[0].Anim := ByteBool(texture.Anim);
1581 CurTex := 0;
1582 end;
1584 //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);
1586 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1587 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1589 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1590 if TriggersTable <> nil then
1591 for b := 0 to High(TriggersTable) do
1592 begin
1593 // Òðèããåð äâåðè/ëèôòà:
1594 if (TriggersTable[b].LiftPanel = a) or
1595 (TriggersTable[b].DoorPanel = a) then
1596 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1597 // Òðèããåð ñìåíû òåêñòóðû:
1598 if TriggersTable[b].TexturePanel = a then
1599 triggers[b].TexturePanel := PanelID;
1600 // Òðèããåð "Òóðåëü":
1601 if TriggersTable[b].ShotPanel = a then
1602 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1603 end;
1605 g_Game_StepLoading();
1606 end;
1607 end;
1609 // create map grid, init other grids (for monsters, for example)
1610 e_WriteLog('Creating map grid', MSG_NOTIFY);
1611 mapCreateGrid();
1613 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1614 if (triggers <> nil) and not gLoadGameMode then
1615 begin
1616 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1617 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1618 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1619 for a := 0 to High(triggers) do
1620 begin
1621 if triggers[a].TexturePanel <> -1 then
1622 b := panels[TriggersTable[a].TexturePanel].PanelType
1623 else
1624 b := 0;
1625 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1626 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1627 c := panels[TriggersTable[a].ShotPanel].PanelType
1628 else
1629 c := 0;
1630 CreateTrigger(triggers[a], b, c);
1631 end;
1632 end;
1634 // Çàãðóçêà ïðåäìåòîâ:
1635 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1636 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1637 items := MapReader.GetItems();
1639 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1640 if (items <> nil) and not gLoadGameMode then
1641 begin
1642 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1643 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1644 for a := 0 to High(items) do
1645 CreateItem(Items[a]);
1646 end;
1648 // Çàãðóçêà îáëàñòåé:
1649 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1650 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1651 areas := MapReader.GetAreas();
1653 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1654 if areas <> nil then
1655 begin
1656 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1657 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1658 for a := 0 to High(areas) do
1659 CreateArea(areas[a]);
1660 end;
1662 // Çàãðóçêà ìîíñòðîâ:
1663 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1664 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1665 monsters := MapReader.GetMonsters();
1667 gTotalMonsters := 0;
1669 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1670 if (monsters <> nil) and not gLoadGameMode then
1671 begin
1672 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1673 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1674 for a := 0 to High(monsters) do
1675 CreateMonster(monsters[a]);
1676 end;
1678 MapReader.Free();
1680 // Çàãðóçêà íåáà:
1681 if gMapInfo.SkyName <> '' then
1682 begin
1683 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1684 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1685 FileName := g_ExtractWadName(gMapInfo.SkyName);
1687 if FileName <> '' then
1688 FileName := GameDir+'/wads/'+FileName
1689 else
1690 begin
1691 FileName := g_ExtractWadName(Res);
1692 end;
1694 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1695 if g_Texture_CreateWAD(BackID, s) then
1696 begin
1697 g_Game_SetupScreenSize();
1698 end
1699 else
1700 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1701 end;
1703 // Çàãðóçêà ìóçûêè:
1704 ok := False;
1705 if gMapInfo.MusicName <> '' then
1706 begin
1707 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1708 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1709 FileName := g_ExtractWadName(gMapInfo.MusicName);
1711 if FileName <> '' then
1712 FileName := GameDir+'/wads/'+FileName
1713 else
1714 begin
1715 FileName := g_ExtractWadName(Res);
1716 end;
1718 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1719 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1720 ok := True
1721 else
1722 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1723 end;
1725 // Îñòàëüíûå óñòàíâêè:
1726 CreateDoorMap();
1727 CreateLiftMap();
1729 g_Items_Init();
1730 g_Weapon_Init();
1731 g_Monsters_Init();
1733 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1734 if not gLoadGameMode then
1735 g_GFX_Init();
1737 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1738 _textures := nil;
1739 panels := nil;
1740 items := nil;
1741 areas := nil;
1742 triggers := nil;
1743 TriggersTable := nil;
1744 AddTextures := nil;
1746 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1747 if ok and (not gLoadGameMode) then
1748 begin
1749 gMusic.SetByName(gMapInfo.MusicName);
1750 gMusic.Play();
1751 end
1752 else
1753 gMusic.SetByName('');
1754 finally
1755 sfsGCEnable(); // enable releasing unused volumes
1756 end;
1758 e_WriteLog('Done loading map.', MSG_NOTIFY);
1759 Result := True;
1760 end;
1762 function g_Map_GetMapInfo(Res: String): TMapInfo;
1763 var
1764 WAD: TWADFile;
1765 MapReader: TMapReader_1;
1766 Header: TMapHeaderRec_1;
1767 FileName: String;
1768 Data: Pointer;
1769 Len: Integer;
1770 begin
1771 FillChar(Result, SizeOf(Result), 0);
1772 FileName := g_ExtractWadName(Res);
1774 WAD := TWADFile.Create();
1775 if not WAD.ReadFile(FileName) then
1776 begin
1777 WAD.Free();
1778 Exit;
1779 end;
1781 //k8: it ignores path again
1782 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1783 begin
1784 WAD.Free();
1785 Exit;
1786 end;
1788 WAD.Free();
1790 MapReader := TMapReader_1.Create();
1792 if not MapReader.LoadMap(Data) then
1793 begin
1794 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1795 ZeroMemory(@Header, SizeOf(Header));
1796 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1797 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1798 end
1799 else
1800 begin
1801 Header := MapReader.GetMapHeader();
1802 Result.Name := Header.MapName;
1803 Result.Description := Header.MapDescription;
1804 end;
1806 FreeMem(Data);
1807 MapReader.Free();
1809 Result.Map := Res;
1810 Result.Author := Header.MapAuthor;
1811 Result.Height := Header.Height;
1812 Result.Width := Header.Width;
1813 end;
1815 function g_Map_GetMapsList(WADName: string): SArray;
1816 var
1817 WAD: TWADFile;
1818 a: Integer;
1819 ResList: SArray;
1820 begin
1821 Result := nil;
1822 WAD := TWADFile.Create();
1823 if not WAD.ReadFile(WADName) then
1824 begin
1825 WAD.Free();
1826 Exit;
1827 end;
1828 ResList := WAD.GetMapResources();
1829 if ResList <> nil then
1830 begin
1831 for a := 0 to High(ResList) do
1832 begin
1833 SetLength(Result, Length(Result)+1);
1834 Result[High(Result)] := ResList[a];
1835 end;
1836 end;
1837 WAD.Free();
1838 end;
1840 function g_Map_Exist(Res: string): Boolean;
1841 var
1842 WAD: TWADFile;
1843 FileName, mnn: string;
1844 ResList: SArray;
1845 a: Integer;
1846 begin
1847 Result := False;
1849 FileName := addWadExtension(g_ExtractWadName(Res));
1851 WAD := TWADFile.Create;
1852 if not WAD.ReadFile(FileName) then
1853 begin
1854 WAD.Free();
1855 Exit;
1856 end;
1858 ResList := WAD.GetMapResources();
1859 WAD.Free();
1861 mnn := g_ExtractFileName(Res);
1862 if ResList <> nil then
1863 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1864 begin
1865 Result := True;
1866 Exit;
1867 end;
1868 end;
1870 procedure g_Map_Free();
1871 var
1872 a: Integer;
1874 procedure FreePanelArray(var panels: TPanelArray);
1875 var
1876 i: Integer;
1878 begin
1879 if panels <> nil then
1880 begin
1881 for i := 0 to High(panels) do
1882 panels[i].Free();
1883 panels := nil;
1884 end;
1885 end;
1887 begin
1888 g_GFX_Free();
1889 g_Weapon_Free();
1890 g_Items_Free();
1891 g_Triggers_Free();
1892 g_Monsters_Free();
1894 RespawnPoints := nil;
1895 if FlagPoints[FLAG_RED] <> nil then
1896 begin
1897 Dispose(FlagPoints[FLAG_RED]);
1898 FlagPoints[FLAG_RED] := nil;
1899 end;
1900 if FlagPoints[FLAG_BLUE] <> nil then
1901 begin
1902 Dispose(FlagPoints[FLAG_BLUE]);
1903 FlagPoints[FLAG_BLUE] := nil;
1904 end;
1905 //DOMFlagPoints := nil;
1907 //gDOMFlags := nil;
1909 if Textures <> nil then
1910 begin
1911 for a := 0 to High(Textures) do
1912 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1913 if Textures[a].Anim then
1914 g_Frames_DeleteByID(Textures[a].FramesID)
1915 else
1916 if Textures[a].TextureID <> TEXTURE_NONE then
1917 e_DeleteTexture(Textures[a].TextureID);
1919 Textures := nil;
1920 end;
1922 FreePanelArray(gWalls);
1923 FreePanelArray(gRenderBackgrounds);
1924 FreePanelArray(gRenderForegrounds);
1925 FreePanelArray(gWater);
1926 FreePanelArray(gAcid1);
1927 FreePanelArray(gAcid2);
1928 FreePanelArray(gSteps);
1929 FreePanelArray(gLifts);
1930 FreePanelArray(gBlockMon);
1932 if BackID <> DWORD(-1) then
1933 begin
1934 gBackSize.X := 0;
1935 gBackSize.Y := 0;
1936 e_DeleteTexture(BackID);
1937 BackID := DWORD(-1);
1938 end;
1940 g_Game_StopAllSounds(False);
1941 gMusic.FreeSound();
1942 g_Sound_Delete(gMapInfo.MusicName);
1944 gMapInfo.Name := '';
1945 gMapInfo.Description := '';
1946 gMapInfo.MusicName := '';
1947 gMapInfo.Height := 0;
1948 gMapInfo.Width := 0;
1950 gDoorMap := nil;
1951 gLiftMap := nil;
1953 PanelByID := nil;
1954 end;
1956 procedure g_Map_Update();
1957 var
1958 a, d, j: Integer;
1959 m: Word;
1960 s: String;
1962 procedure UpdatePanelArray(var panels: TPanelArray);
1963 var
1964 i: Integer;
1966 begin
1967 if panels <> nil then
1968 for i := 0 to High(panels) do
1969 panels[i].Update();
1970 end;
1972 begin
1973 UpdatePanelArray(gWalls);
1974 UpdatePanelArray(gRenderBackgrounds);
1975 UpdatePanelArray(gRenderForegrounds);
1976 UpdatePanelArray(gWater);
1977 UpdatePanelArray(gAcid1);
1978 UpdatePanelArray(gAcid2);
1979 UpdatePanelArray(gSteps);
1981 if gGameSettings.GameMode = GM_CTF then
1982 begin
1983 for a := FLAG_RED to FLAG_BLUE do
1984 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1985 with gFlags[a] do
1986 begin
1987 if gFlags[a].Animation <> nil then
1988 gFlags[a].Animation.Update();
1990 m := g_Obj_Move(@Obj, True, True);
1992 if gTime mod (GAME_TICK*2) <> 0 then
1993 Continue;
1995 // Ñîïðîòèâëåíèå âîçäóõà:
1996 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1998 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1999 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2000 begin
2001 g_Map_ResetFlag(a);
2002 gFlags[a].CaptureTime := 0;
2003 if a = FLAG_RED then
2004 s := _lc[I_PLAYER_FLAG_RED]
2005 else
2006 s := _lc[I_PLAYER_FLAG_BLUE];
2007 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2009 if g_Game_IsNet then
2010 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2011 Continue;
2012 end;
2014 if Count > 0 then
2015 Count := Count - 1;
2017 // Èãðîê áåðåò ôëàã:
2018 if gPlayers <> nil then
2019 begin
2020 j := Random(Length(gPlayers)) - 1;
2022 for d := 0 to High(gPlayers) do
2023 begin
2024 Inc(j);
2025 if j > High(gPlayers) then
2026 j := 0;
2028 if gPlayers[j] <> nil then
2029 if gPlayers[j].Live and
2030 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2031 begin
2032 if gPlayers[j].GetFlag(a) then
2033 Break;
2034 end;
2035 end;
2036 end;
2037 end;
2038 end;
2039 end;
2042 // old algo
2043 procedure g_Map_DrawPanels (PanelType: Word);
2045 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2046 var
2047 idx: Integer;
2048 begin
2049 if (panels <> nil) then
2050 begin
2051 // alas, no visible set
2052 for idx := 0 to High(panels) do
2053 begin
2054 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2055 end;
2056 end;
2057 end;
2059 begin
2060 case PanelType of
2061 PANEL_WALL: DrawPanels(gWalls);
2062 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2063 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2064 PANEL_FORE: DrawPanels(gRenderForegrounds);
2065 PANEL_WATER: DrawPanels(gWater);
2066 PANEL_ACID1: DrawPanels(gAcid1);
2067 PANEL_ACID2: DrawPanels(gAcid2);
2068 PANEL_STEP: DrawPanels(gSteps);
2069 end;
2070 end;
2073 // new algo
2074 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2076 function checker (pan: TPanel; tag: Integer): Boolean;
2077 begin
2078 result := false; // don't stop, ever
2079 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2080 gDrawPanelList.insert(pan);
2081 end;
2083 begin
2084 dplClear();
2085 //tagmask := panelTypeToTag(PanelType);
2087 {if gdbg_map_use_tree_draw then
2088 begin
2089 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2090 end
2091 else}
2092 begin
2093 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2094 end;
2095 // list will be rendered in `g_game.DrawPlayer()`
2096 end;
2099 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2101 function checker (pan: TPanel; tag: Integer): Boolean;
2102 begin
2103 result := false; // don't stop, ever
2104 pan.DrawShadowVolume(lightX, lightY, radius);
2105 end;
2107 begin
2108 {if gdbg_map_use_tree_draw then
2109 begin
2110 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2111 end
2112 else}
2113 begin
2114 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2115 end;
2116 end;
2119 procedure g_Map_DrawBack(dx, dy: Integer);
2120 begin
2121 if gDrawBackGround and (BackID <> DWORD(-1)) then
2122 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2123 else
2124 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2125 end;
2127 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2128 PanelType: Word; b1x3: Boolean=false): Boolean;
2129 var
2130 a, h: Integer;
2131 begin
2132 Result := False;
2134 if WordBool(PanelType and PANEL_WALL) then
2135 if gWalls <> nil then
2136 begin
2137 h := High(gWalls);
2139 for a := 0 to h do
2140 if gWalls[a].Enabled and
2141 g_Collide(X, Y, Width, Height,
2142 gWalls[a].X, gWalls[a].Y,
2143 gWalls[a].Width, gWalls[a].Height) then
2144 begin
2145 Result := True;
2146 Exit;
2147 end;
2148 end;
2150 if WordBool(PanelType and PANEL_WATER) then
2151 if gWater <> nil then
2152 begin
2153 h := High(gWater);
2155 for a := 0 to h do
2156 if g_Collide(X, Y, Width, Height,
2157 gWater[a].X, gWater[a].Y,
2158 gWater[a].Width, gWater[a].Height) then
2159 begin
2160 Result := True;
2161 Exit;
2162 end;
2163 end;
2165 if WordBool(PanelType and PANEL_ACID1) then
2166 if gAcid1 <> nil then
2167 begin
2168 h := High(gAcid1);
2170 for a := 0 to h do
2171 if g_Collide(X, Y, Width, Height,
2172 gAcid1[a].X, gAcid1[a].Y,
2173 gAcid1[a].Width, gAcid1[a].Height) then
2174 begin
2175 Result := True;
2176 Exit;
2177 end;
2178 end;
2180 if WordBool(PanelType and PANEL_ACID2) then
2181 if gAcid2 <> nil then
2182 begin
2183 h := High(gAcid2);
2185 for a := 0 to h do
2186 if g_Collide(X, Y, Width, Height,
2187 gAcid2[a].X, gAcid2[a].Y,
2188 gAcid2[a].Width, gAcid2[a].Height) then
2189 begin
2190 Result := True;
2191 Exit;
2192 end;
2193 end;
2195 if WordBool(PanelType and PANEL_STEP) then
2196 if gSteps <> nil then
2197 begin
2198 h := High(gSteps);
2200 for a := 0 to h do
2201 if g_Collide(X, Y, Width, Height,
2202 gSteps[a].X, gSteps[a].Y,
2203 gSteps[a].Width, gSteps[a].Height) then
2204 begin
2205 Result := True;
2206 Exit;
2207 end;
2208 end;
2210 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2211 if gLifts <> nil then
2212 begin
2213 h := High(gLifts);
2215 for a := 0 to h do
2216 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2217 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2218 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2219 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2220 g_Collide(X, Y, Width, Height,
2221 gLifts[a].X, gLifts[a].Y,
2222 gLifts[a].Width, gLifts[a].Height) then
2223 begin
2224 Result := True;
2225 Exit;
2226 end;
2227 end;
2229 if WordBool(PanelType and PANEL_BLOCKMON) then
2230 if gBlockMon <> nil then
2231 begin
2232 h := High(gBlockMon);
2234 for a := 0 to h do
2235 if ( (not b1x3) or
2236 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2237 g_Collide(X, Y, Width, Height,
2238 gBlockMon[a].X, gBlockMon[a].Y,
2239 gBlockMon[a].Width, gBlockMon[a].Height) then
2240 begin
2241 Result := True;
2242 Exit;
2243 end;
2244 end;
2245 end;
2247 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2248 var
2249 texid: DWORD;
2251 function checkPanels (constref panels: TPanelArray): Boolean;
2252 var
2253 a: Integer;
2254 begin
2255 result := false;
2256 if panels = nil then exit;
2257 for a := 0 to High(panels) do
2258 begin
2259 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2260 begin
2261 result := true;
2262 texid := panels[a].GetTextureID();
2263 exit;
2264 end;
2265 end;
2266 end;
2268 begin
2269 texid := TEXTURE_NONE;
2270 result := texid;
2271 if not checkPanels(gWater) then
2272 if not checkPanels(gAcid1) then
2273 if not checkPanels(gAcid2) then exit;
2274 result := texid;
2275 end;
2278 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2279 const
2280 SlowMask = GridTagLift or GridTagBlockMon;
2281 function checker (pan: TPanel; tag: Integer): Boolean;
2282 begin
2284 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2285 begin
2286 result := pan.Enabled;
2287 exit;
2288 end;
2291 if ((tag and GridTagLift) <> 0) then
2292 begin
2293 result :=
2294 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2295 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2296 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2297 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2298 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2299 exit;
2300 end;
2302 if ((tag and GridTagBlockMon) <> 0) then
2303 begin
2304 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2305 exit;
2306 end;
2308 // other shit
2309 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2310 result := true; // i found her!
2311 end;
2313 var
2314 tagmask: Integer = 0;
2315 begin
2316 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2317 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2318 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2319 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2320 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2321 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2322 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2324 if (tagmask = 0) then begin result := false; exit; end; // just in case
2326 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2327 if gdbg_map_use_accel_coldet then
2328 begin
2329 {if gdbg_map_use_tree_coldet then
2330 begin
2331 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2332 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2333 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2334 begin
2335 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2336 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2337 end;
2338 end
2339 else}
2340 begin
2341 if (Width = 1) and (Height = 1) then
2342 begin
2343 if ((tagmask and SlowMask) <> 0) then
2344 begin
2345 // slow
2346 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2347 end
2348 else
2349 begin
2350 // fast
2351 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2352 end;
2353 end
2354 else
2355 begin
2356 if ((tagmask and SlowMask) <> 0) then
2357 begin
2358 // slow
2359 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2360 end
2361 else
2362 begin
2363 // fast
2364 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2365 end;
2366 end;
2367 end;
2368 end
2369 else
2370 begin
2371 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2372 end;
2373 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2374 end;
2377 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2378 var
2379 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2380 texid: DWORD;
2382 // slightly different from the old code, but meh...
2383 function checker (pan: TPanel; tag: Integer): Boolean;
2384 begin
2385 result := false; // don't stop, ever
2386 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2387 // check priorities
2388 case cctype of
2389 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2390 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2391 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2392 end;
2393 // collision?
2394 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2395 // yeah
2396 texid := pan.GetTextureID();
2397 // water? water has the highest priority, so stop right here
2398 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2399 // acid2?
2400 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2401 // acid1?
2402 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2403 end;
2405 begin
2406 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2407 if gdbg_map_use_accel_coldet then
2408 begin
2409 texid := TEXTURE_NONE;
2410 {if gdbg_map_use_tree_coldet then
2411 begin
2412 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2413 end
2414 else}
2415 begin
2416 if (Width = 1) and (Height = 1) then
2417 begin
2418 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2419 end
2420 else
2421 begin
2422 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2423 end;
2424 end;
2425 result := texid;
2426 end
2427 else
2428 begin
2429 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2430 end;
2431 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2432 end;
2435 procedure g_Map_EnableWall(ID: DWORD);
2436 var
2437 pan: TPanel;
2438 begin
2439 pan := gWalls[ID];
2440 pan.Enabled := True;
2441 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2443 mapGrid.proxyEnabled[pan.proxyId] := true;
2444 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2445 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2447 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2449 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2450 //e_WriteLog(Format('ENABLE: wall #%d(%d) enabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY);
2451 {$ENDIF}
2452 end;
2454 procedure g_Map_DisableWall(ID: DWORD);
2455 var
2456 pan: TPanel;
2457 begin
2458 pan := gWalls[ID];
2459 pan.Enabled := False;
2460 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2462 mapGrid.proxyEnabled[pan.proxyId] := false;
2463 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2465 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2467 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2468 //e_WriteLog(Format('DISABLE: wall #%d(%d) disabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY);
2469 {$ENDIF}
2470 end;
2472 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2473 var
2474 tp: TPanel;
2475 begin
2476 case PanelType of
2477 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2478 tp := gWalls[ID];
2479 PANEL_FORE:
2480 tp := gRenderForegrounds[ID];
2481 PANEL_BACK:
2482 tp := gRenderBackgrounds[ID];
2483 PANEL_WATER:
2484 tp := gWater[ID];
2485 PANEL_ACID1:
2486 tp := gAcid1[ID];
2487 PANEL_ACID2:
2488 tp := gAcid2[ID];
2489 PANEL_STEP:
2490 tp := gSteps[ID];
2491 else
2492 Exit;
2493 end;
2495 tp.NextTexture(AnimLoop);
2496 if g_Game_IsServer and g_Game_IsNet then
2497 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2498 end;
2500 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2501 begin
2502 if gLifts[ID].LiftType = t then
2503 Exit;
2505 with gLifts[ID] do
2506 begin
2507 LiftType := t;
2509 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2510 //TODO: make separate lift tags, and change tag here
2512 if LiftType = 0 then
2513 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2514 else if LiftType = 1 then
2515 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2516 else if LiftType = 2 then
2517 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2518 else if LiftType = 3 then
2519 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2521 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2522 end;
2523 end;
2525 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2526 var
2527 a: Integer;
2528 PointsArray: Array of TRespawnPoint;
2529 begin
2530 Result := False;
2531 SetLength(PointsArray, 0);
2533 if RespawnPoints = nil then
2534 Exit;
2536 for a := 0 to High(RespawnPoints) do
2537 if RespawnPoints[a].PointType = PointType then
2538 begin
2539 SetLength(PointsArray, Length(PointsArray)+1);
2540 PointsArray[High(PointsArray)] := RespawnPoints[a];
2541 end;
2543 if PointsArray = nil then
2544 Exit;
2546 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2547 Result := True;
2548 end;
2550 function g_Map_GetPointCount(PointType: Byte): Word;
2551 var
2552 a: Integer;
2553 begin
2554 Result := 0;
2556 if RespawnPoints = nil then
2557 Exit;
2559 for a := 0 to High(RespawnPoints) do
2560 if RespawnPoints[a].PointType = PointType then
2561 Result := Result + 1;
2562 end;
2564 function g_Map_HaveFlagPoints(): Boolean;
2565 begin
2566 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2567 end;
2569 procedure g_Map_ResetFlag(Flag: Byte);
2570 begin
2571 with gFlags[Flag] do
2572 begin
2573 Obj.X := -1000;
2574 Obj.Y := -1000;
2575 Obj.Vel.X := 0;
2576 Obj.Vel.Y := 0;
2577 Direction := D_LEFT;
2578 State := FLAG_STATE_NONE;
2579 if FlagPoints[Flag] <> nil then
2580 begin
2581 Obj.X := FlagPoints[Flag]^.X;
2582 Obj.Y := FlagPoints[Flag]^.Y;
2583 Direction := FlagPoints[Flag]^.Direction;
2584 State := FLAG_STATE_NORMAL;
2585 end;
2586 Count := -1;
2587 end;
2588 end;
2590 procedure g_Map_DrawFlags();
2591 var
2592 i, dx: Integer;
2593 Mirror: TMirrorType;
2594 begin
2595 if gGameSettings.GameMode <> GM_CTF then
2596 Exit;
2598 for i := FLAG_RED to FLAG_BLUE do
2599 with gFlags[i] do
2600 if State <> FLAG_STATE_CAPTURED then
2601 begin
2602 if State = FLAG_STATE_NONE then
2603 continue;
2605 if Direction = D_LEFT then
2606 begin
2607 Mirror := M_HORIZONTAL;
2608 dx := -1;
2609 end
2610 else
2611 begin
2612 Mirror := M_NONE;
2613 dx := 1;
2614 end;
2616 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2618 if g_debug_Frames then
2619 begin
2620 e_DrawQuad(Obj.X+Obj.Rect.X,
2621 Obj.Y+Obj.Rect.Y,
2622 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2623 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2624 0, 255, 0);
2625 end;
2626 end;
2627 end;
2629 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2630 var
2631 dw: DWORD;
2632 b: Byte;
2633 str: String;
2634 boo: Boolean;
2636 procedure SavePanelArray(var panels: TPanelArray);
2637 var
2638 PAMem: TBinMemoryWriter;
2639 i: Integer;
2640 begin
2641 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2642 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2644 i := 0;
2645 while i < Length(panels) do
2646 begin
2647 if panels[i].SaveIt then
2648 begin
2649 // ID ïàíåëè:
2650 PAMem.WriteInt(i);
2651 // Ñîõðàíÿåì ïàíåëü:
2652 panels[i].SaveState(PAMem);
2653 end;
2654 Inc(i);
2655 end;
2657 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2658 PAMem.SaveToMemory(Mem);
2659 PAMem.Free();
2660 end;
2662 procedure SaveFlag(flag: PFlag);
2663 begin
2664 // Ñèãíàòóðà ôëàãà:
2665 dw := FLAG_SIGNATURE; // 'FLAG'
2666 Mem.WriteDWORD(dw);
2667 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2668 Mem.WriteByte(flag^.RespawnType);
2669 // Ñîñòîÿíèå ôëàãà:
2670 Mem.WriteByte(flag^.State);
2671 // Íàïðàâëåíèå ôëàãà:
2672 if flag^.Direction = D_LEFT then
2673 b := 1
2674 else // D_RIGHT
2675 b := 2;
2676 Mem.WriteByte(b);
2677 // Îáúåêò ôëàãà:
2678 Obj_SaveState(@flag^.Obj, Mem);
2679 end;
2681 begin
2682 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2684 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2685 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2686 SavePanelArray(gWalls);
2687 // Ñîõðàíÿåì ïàíåëè ôîíà:
2688 SavePanelArray(gRenderBackgrounds);
2689 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2690 SavePanelArray(gRenderForegrounds);
2691 // Ñîõðàíÿåì ïàíåëè âîäû:
2692 SavePanelArray(gWater);
2693 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2694 SavePanelArray(gAcid1);
2695 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2696 SavePanelArray(gAcid2);
2697 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2698 SavePanelArray(gSteps);
2699 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2700 SavePanelArray(gLifts);
2701 ///// /////
2703 ///// Ñîõðàíÿåì ìóçûêó: /////
2704 // Ñèãíàòóðà ìóçûêè:
2705 dw := MUSIC_SIGNATURE; // 'MUSI'
2706 Mem.WriteDWORD(dw);
2707 // Íàçâàíèå ìóçûêè:
2708 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2709 if gMusic.NoMusic then
2710 str := ''
2711 else
2712 str := gMusic.Name;
2713 Mem.WriteString(str, 64);
2714 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2715 dw := gMusic.GetPosition();
2716 Mem.WriteDWORD(dw);
2717 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2718 boo := gMusic.SpecPause;
2719 Mem.WriteBoolean(boo);
2720 ///// /////
2722 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2723 Mem.WriteInt(gTotalMonsters);
2724 ///// /////
2726 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2727 if gGameSettings.GameMode = GM_CTF then
2728 begin
2729 // Ôëàã Êðàñíîé êîìàíäû:
2730 SaveFlag(@gFlags[FLAG_RED]);
2731 // Ôëàã Ñèíåé êîìàíäû:
2732 SaveFlag(@gFlags[FLAG_BLUE]);
2733 end;
2734 ///// /////
2736 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2737 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2738 begin
2739 // Î÷êè Êðàñíîé êîìàíäû:
2740 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2741 // Î÷êè Ñèíåé êîìàíäû:
2742 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2743 end;
2744 ///// /////
2745 end;
2747 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2748 var
2749 dw: DWORD;
2750 b: Byte;
2751 str: String;
2752 boo: Boolean;
2754 procedure LoadPanelArray(var panels: TPanelArray);
2755 var
2756 PAMem: TBinMemoryReader;
2757 i, id: Integer;
2758 begin
2759 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2760 PAMem := TBinMemoryReader.Create();
2761 PAMem.LoadFromMemory(Mem);
2763 for i := 0 to Length(panels)-1 do
2764 begin
2765 if panels[i].SaveIt then
2766 begin
2767 // ID ïàíåëè:
2768 PAMem.ReadInt(id);
2769 if id <> i then
2770 begin
2771 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2772 end;
2773 // Çàãðóæàåì ïàíåëü:
2774 panels[i].LoadState(PAMem);
2775 if (panels[i].arrIdx <> i) then raise Exception.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
2776 if (panels[i].proxyId >= 0) then mapGrid.proxyEnabled[panels[i].proxyId] := panels[i].Enabled;
2777 end;
2778 end;
2780 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2781 PAMem.Free();
2782 end;
2784 procedure LoadFlag(flag: PFlag);
2785 begin
2786 // Ñèãíàòóðà ôëàãà:
2787 Mem.ReadDWORD(dw);
2788 if dw <> FLAG_SIGNATURE then // 'FLAG'
2789 begin
2790 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2791 end;
2792 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2793 Mem.ReadByte(flag^.RespawnType);
2794 // Ñîñòîÿíèå ôëàãà:
2795 Mem.ReadByte(flag^.State);
2796 // Íàïðàâëåíèå ôëàãà:
2797 Mem.ReadByte(b);
2798 if b = 1 then
2799 flag^.Direction := D_LEFT
2800 else // b = 2
2801 flag^.Direction := D_RIGHT;
2802 // Îáúåêò ôëàãà:
2803 Obj_LoadState(@flag^.Obj, Mem);
2804 end;
2806 begin
2807 if Mem = nil then
2808 Exit;
2810 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2811 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2812 LoadPanelArray(gWalls);
2813 // Çàãðóæàåì ïàíåëè ôîíà:
2814 LoadPanelArray(gRenderBackgrounds);
2815 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2816 LoadPanelArray(gRenderForegrounds);
2817 // Çàãðóæàåì ïàíåëè âîäû:
2818 LoadPanelArray(gWater);
2819 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2820 LoadPanelArray(gAcid1);
2821 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2822 LoadPanelArray(gAcid2);
2823 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2824 LoadPanelArray(gSteps);
2825 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2826 LoadPanelArray(gLifts);
2827 ///// /////
2829 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2830 g_GFX_Init();
2831 //mapCreateGrid();
2833 ///// Çàãðóæàåì ìóçûêó: /////
2834 // Ñèãíàòóðà ìóçûêè:
2835 Mem.ReadDWORD(dw);
2836 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2837 begin
2838 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2839 end;
2840 // Íàçâàíèå ìóçûêè:
2841 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2842 Mem.ReadString(str);
2843 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2844 Mem.ReadDWORD(dw);
2845 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2846 Mem.ReadBoolean(boo);
2847 // Çàïóñêàåì ýòó ìóçûêó:
2848 gMusic.SetByName(str);
2849 gMusic.SpecPause := boo;
2850 gMusic.Play();
2851 gMusic.Pause(True);
2852 gMusic.SetPosition(dw);
2853 ///// /////
2855 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2856 Mem.ReadInt(gTotalMonsters);
2857 ///// /////
2859 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2860 if gGameSettings.GameMode = GM_CTF then
2861 begin
2862 // Ôëàã Êðàñíîé êîìàíäû:
2863 LoadFlag(@gFlags[FLAG_RED]);
2864 // Ôëàã Ñèíåé êîìàíäû:
2865 LoadFlag(@gFlags[FLAG_BLUE]);
2866 end;
2867 ///// /////
2869 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2870 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2871 begin
2872 // Î÷êè Êðàñíîé êîìàíäû:
2873 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2874 // Î÷êè Ñèíåé êîìàíäû:
2875 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2876 end;
2877 ///// /////
2878 end;
2880 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2881 var
2882 Arr: TPanelArray;
2883 begin
2884 Result := nil;
2885 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2886 Arr := PanelByID[PanelID].PWhere^;
2887 PanelArrayID := PanelByID[PanelID].PArrID;
2888 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2889 end;
2892 // trace liquid, stepping by `dx` and `dy`
2893 // return last seen liquid coords, and `false` if we're started outside of the liquid
2894 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2895 const
2896 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2897 begin
2898 topx := x;
2899 topy := y;
2900 // started outside of the liquid?
2901 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2902 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2903 result := true;
2904 while true do
2905 begin
2906 Inc(x, dx);
2907 Inc(y, dy);
2908 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2909 topx := x;
2910 topy := y;
2911 end;
2912 end;
2915 end.