DEADSOFTWARE

the game is able to read text maps now (WARNING! the feature is still experimental!)
[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, xparser, xdynrec, xstreams,
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 {$IF DEFINED(D2D_NEW_MAP_READER)}
222 var
223 dfmapdef: TDynMapDef = nil;
226 procedure loadMapDefinition ();
227 var
228 pr: TTextParser = nil;
229 st: TStream = nil;
230 WAD: TWADFile = nil;
231 begin
232 if (dfmapdef <> nil) then exit;
233 try
234 e_LogWritefln('parsing "mapdef.txt"...', []);
235 st := openDiskFileRO(DataDir+'mapdef.txt');
236 except
237 st := nil;
238 e_LogWritefln('local "%smapdef.txt" not found', [DataDir]);
239 end;
240 if (st = nil) then
241 begin
242 WAD := TWADFile.Create();
243 if not WAD.ReadFile(GameWAD) then raise Exception.Create('cannot load "game.wad"');
244 st := WAD.openFileStream('mapdef.txt');
245 end;
247 if (st = nil) then raise Exception.Create('cannot open "mapdef.txt"');
248 pr := TFileTextParser.Create(st);
250 try
251 dfmapdef := TDynMapDef.Create(pr);
252 except on e: Exception do
253 raise Exception.Create(Format('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.line, pr.col, e.message]));
254 end;
256 st.Free();
257 WAD.Free();
258 end;
259 {$ENDIF}
262 function panelTypeToTag (panelType: Word): Integer;
263 begin
264 case panelType of
265 PANEL_WALL: result := GridTagWall; // gWalls
266 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
267 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
268 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
269 PANEL_WATER: result := GridTagWater; // gWater
270 PANEL_ACID1: result := GridTagAcid1; // gAcid1
271 PANEL_ACID2: result := GridTagAcid2; // gAcid2
272 PANEL_STEP: result := GridTagStep; // gSteps
273 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
274 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
275 else result := GridTagInvalid;
276 end;
277 end;
280 function dplLess (a, b: TObject): Boolean;
281 var
282 pa, pb: TPanel;
283 begin
284 pa := TPanel(a);
285 pb := TPanel(b);
286 if (pa.tag < pb.tag) then begin result := true; exit; end;
287 if (pa.tag > pb.tag) then begin result := false; exit; end;
288 result := (pa.arrIdx < pb.arrIdx);
289 end;
291 procedure dplClear ();
292 begin
293 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
294 end;
297 type
298 TPanelID = record
299 PWhere: ^TPanelArray;
300 PArrID: Integer;
301 end;
303 var
304 PanelById: array of TPanelID;
305 Textures: TLevelTextureArray;
306 RespawnPoints: Array of TRespawnPoint;
307 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
308 //DOMFlagPoints: Array of TFlagPoint;
311 procedure g_Map_ProfilersBegin ();
312 begin
313 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
314 profMapCollision.mainBegin(g_profile_collision);
315 // create sections
316 if g_profile_collision then
317 begin
318 profMapCollision.sectionBegin('*solids');
319 profMapCollision.sectionEnd();
320 profMapCollision.sectionBegin('liquids');
321 profMapCollision.sectionEnd();
322 end;
323 end;
325 procedure g_Map_ProfilersEnd ();
326 begin
327 if (profMapCollision <> nil) then profMapCollision.mainEnd();
328 end;
331 // wall index in `gWalls` or -1
332 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
333 var
334 ex, ey: Integer;
335 begin
336 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
337 if (result <> nil) then
338 begin
339 if (hitx <> nil) then hitx^ := ex;
340 if (hity <> nil) then hity^ := ey;
341 end
342 else
343 begin
344 if (hitx <> nil) then hitx^ := x1;
345 if (hity <> nil) then hity^ := y1;
346 end;
347 end;
349 // returns panel or nil
350 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
351 var
352 ex, ey: Integer;
353 begin
354 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
355 if (result <> nil) then
356 begin
357 if (hitx <> nil) then hitx^ := ex;
358 if (hity <> nil) then hity^ := ey;
359 end
360 else
361 begin
362 if (hitx <> nil) then hitx^ := x1;
363 if (hity <> nil) then hity^ := y1;
364 end;
365 end;
368 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
370 function checker (pan: TPanel; tag: Integer): Boolean;
371 begin
373 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
374 begin
375 result := pan.Enabled; // stop if wall is enabled
376 exit;
377 end;
380 if ((tag and GridTagLift) <> 0) then
381 begin
382 // stop if the lift of the right type
383 result :=
384 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
385 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
386 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
387 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
388 exit;
389 end;
391 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
392 end;
394 var
395 tagmask: Integer = 0;
396 begin
397 result := false;
399 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
400 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
401 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
402 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
403 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
404 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
405 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
407 if (tagmask = 0) then exit;// just in case
408 if ((tagmask and GridTagLift) <> 0) then
409 begin
410 // slow
411 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
412 end
413 else
414 begin
415 // fast
416 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
417 end;
418 end;
421 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
422 begin
423 result := nil;
424 if (tagmask = 0) then exit;
425 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
426 end;
429 function g_Map_IsSpecialTexture(Texture: String): Boolean;
430 begin
431 Result := (Texture = TEXTURE_NAME_WATER) or
432 (Texture = TEXTURE_NAME_ACID1) or
433 (Texture = TEXTURE_NAME_ACID2);
434 end;
436 procedure CreateDoorMap();
437 var
438 PanelArray: Array of record
439 X, Y: Integer;
440 Width, Height: Word;
441 Active: Boolean;
442 PanelID: DWORD;
443 end;
444 a, b, c, m, i, len: Integer;
445 ok: Boolean;
446 begin
447 if gWalls = nil then
448 Exit;
450 i := 0;
451 len := 128;
452 SetLength(PanelArray, len);
454 for a := 0 to High(gWalls) do
455 if gWalls[a].Door then
456 begin
457 PanelArray[i].X := gWalls[a].X;
458 PanelArray[i].Y := gWalls[a].Y;
459 PanelArray[i].Width := gWalls[a].Width;
460 PanelArray[i].Height := gWalls[a].Height;
461 PanelArray[i].Active := True;
462 PanelArray[i].PanelID := a;
464 i := i + 1;
465 if i = len then
466 begin
467 len := len + 128;
468 SetLength(PanelArray, len);
469 end;
470 end;
472 // Íåò äâåðåé:
473 if i = 0 then
474 begin
475 PanelArray := nil;
476 Exit;
477 end;
479 SetLength(gDoorMap, 0);
481 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
483 for a := 0 to i-1 do
484 if PanelArray[a].Active then
485 begin
486 PanelArray[a].Active := False;
487 m := Length(gDoorMap);
488 SetLength(gDoorMap, m+1);
489 SetLength(gDoorMap[m], 1);
490 gDoorMap[m, 0] := PanelArray[a].PanelID;
491 ok := True;
493 while ok do
494 begin
495 ok := False;
497 for b := 0 to i-1 do
498 if PanelArray[b].Active then
499 for c := 0 to High(gDoorMap[m]) do
500 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
501 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
502 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
503 PanelArray[b].Width, PanelArray[b].Height,
504 gWalls[gDoorMap[m, c]].X,
505 gWalls[gDoorMap[m, c]].Y,
506 gWalls[gDoorMap[m, c]].Width,
507 gWalls[gDoorMap[m, c]].Height) then
508 begin
509 PanelArray[b].Active := False;
510 SetLength(gDoorMap[m],
511 Length(gDoorMap[m])+1);
512 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
513 ok := True;
514 Break;
515 end;
516 end;
518 g_Game_StepLoading();
519 end;
521 PanelArray := nil;
522 end;
524 procedure CreateLiftMap();
525 var
526 PanelArray: Array of record
527 X, Y: Integer;
528 Width, Height: Word;
529 Active: Boolean;
530 end;
531 a, b, c, len, i, j: Integer;
532 ok: Boolean;
533 begin
534 if gLifts = nil then
535 Exit;
537 len := Length(gLifts);
538 SetLength(PanelArray, len);
540 for a := 0 to len-1 do
541 begin
542 PanelArray[a].X := gLifts[a].X;
543 PanelArray[a].Y := gLifts[a].Y;
544 PanelArray[a].Width := gLifts[a].Width;
545 PanelArray[a].Height := gLifts[a].Height;
546 PanelArray[a].Active := True;
547 end;
549 SetLength(gLiftMap, len);
550 i := 0;
552 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
554 for a := 0 to len-1 do
555 if PanelArray[a].Active then
556 begin
557 PanelArray[a].Active := False;
558 SetLength(gLiftMap[i], 32);
559 j := 0;
560 gLiftMap[i, j] := a;
561 ok := True;
563 while ok do
564 begin
565 ok := False;
566 for b := 0 to len-1 do
567 if PanelArray[b].Active then
568 for c := 0 to j do
569 if g_CollideAround(PanelArray[b].X,
570 PanelArray[b].Y,
571 PanelArray[b].Width,
572 PanelArray[b].Height,
573 PanelArray[gLiftMap[i, c]].X,
574 PanelArray[gLiftMap[i, c]].Y,
575 PanelArray[gLiftMap[i, c]].Width,
576 PanelArray[gLiftMap[i, c]].Height) then
577 begin
578 PanelArray[b].Active := False;
579 j := j+1;
580 if j > High(gLiftMap[i]) then
581 SetLength(gLiftMap[i],
582 Length(gLiftMap[i])+32);
584 gLiftMap[i, j] := b;
585 ok := True;
587 Break;
588 end;
589 end;
591 SetLength(gLiftMap[i], j+1);
592 i := i+1;
594 g_Game_StepLoading();
595 end;
597 SetLength(gLiftMap, i);
599 PanelArray := nil;
600 end;
602 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
603 CurTex: Integer; sav: Boolean): Integer;
604 var
605 len: Integer;
606 panels: ^TPanelArray;
607 begin
608 Result := -1;
610 case PanelRec.PanelType of
611 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
612 panels := @gWalls;
613 PANEL_BACK:
614 panels := @gRenderBackgrounds;
615 PANEL_FORE:
616 panels := @gRenderForegrounds;
617 PANEL_WATER:
618 panels := @gWater;
619 PANEL_ACID1:
620 panels := @gAcid1;
621 PANEL_ACID2:
622 panels := @gAcid2;
623 PANEL_STEP:
624 panels := @gSteps;
625 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
626 panels := @gLifts;
627 PANEL_BLOCKMON:
628 panels := @gBlockMon;
629 else
630 Exit;
631 end;
633 len := Length(panels^);
634 SetLength(panels^, len + 1);
636 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
637 panels^[len].arrIdx := len;
638 panels^[len].proxyId := -1;
639 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
640 if sav then
641 panels^[len].SaveIt := True;
643 Result := len;
645 len := Length(PanelByID);
646 SetLength(PanelByID, len + 1);
647 PanelByID[len].PWhere := panels;
648 PanelByID[len].PArrID := Result;
649 end;
651 function CreateNullTexture(RecName: String): Integer;
652 begin
653 SetLength(Textures, Length(Textures)+1);
654 result := High(Textures);
656 with Textures[High(Textures)] do
657 begin
658 TextureName := RecName;
659 Width := 1;
660 Height := 1;
661 Anim := False;
662 TextureID := TEXTURE_NONE;
663 end;
664 end;
666 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
667 var
668 WAD: TWADFile;
669 TextureData: Pointer;
670 WADName, txname: String;
671 a, ResLength: Integer;
672 begin
673 Result := -1;
675 if Textures <> nil then
676 for a := 0 to High(Textures) do
677 if Textures[a].TextureName = RecName then
678 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
679 Result := a;
680 Exit;
681 end;
683 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
684 if (RecName = TEXTURE_NAME_WATER) or
685 (RecName = TEXTURE_NAME_ACID1) or
686 (RecName = TEXTURE_NAME_ACID2) then
687 begin
688 SetLength(Textures, Length(Textures)+1);
690 with Textures[High(Textures)] do
691 begin
692 TextureName := RecName;
694 if TextureName = TEXTURE_NAME_WATER then
695 TextureID := TEXTURE_SPECIAL_WATER
696 else
697 if TextureName = TEXTURE_NAME_ACID1 then
698 TextureID := TEXTURE_SPECIAL_ACID1
699 else
700 if TextureName = TEXTURE_NAME_ACID2 then
701 TextureID := TEXTURE_SPECIAL_ACID2;
703 Anim := False;
704 end;
706 result := High(Textures);
707 Exit;
708 end;
710 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
711 WADName := g_ExtractWadName(RecName);
713 WAD := TWADFile.Create();
715 if WADName <> '' then
716 WADName := GameDir+'/wads/'+WADName
717 else
718 WADName := Map;
720 WAD.ReadFile(WADName);
722 txname := RecName;
724 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
725 begin
726 FreeMem(TextureData);
727 RecName := 'COMMON\ALIEN';
728 end;
731 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
732 begin
733 SetLength(Textures, Length(Textures)+1);
734 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
735 Exit;
736 e_GetTextureSize(Textures[High(Textures)].TextureID,
737 @Textures[High(Textures)].Width,
738 @Textures[High(Textures)].Height);
739 FreeMem(TextureData);
740 Textures[High(Textures)].TextureName := {RecName}txname;
741 Textures[High(Textures)].Anim := False;
743 result := High(Textures);
744 end
745 else // Íåò òàêîãî ðåóñðñà â WAD'å
746 begin
747 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
748 if log then
749 begin
750 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
751 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
752 end;
753 end;
755 WAD.Free();
756 end;
758 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
759 var
760 WAD: TWADFile;
761 TextureWAD: PChar = nil;
762 TextData: Pointer = nil;
763 TextureData: Pointer = nil;
764 cfg: TConfig = nil;
765 WADName: String;
766 ResLength: Integer;
767 TextureResource: String;
768 _width, _height, _framecount, _speed: Integer;
769 _backanimation: Boolean;
770 //imgfmt: string;
771 ia: TDynImageDataArray = nil;
772 f, c, frdelay, frloop: Integer;
773 begin
774 result := -1;
776 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
778 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
779 WADName := g_ExtractWadName(RecName);
781 WAD := TWADFile.Create();
782 try
783 if WADName <> '' then
784 WADName := GameDir+'/wads/'+WADName
785 else
786 WADName := Map;
788 WAD.ReadFile(WADName);
790 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
791 begin
792 if log then
793 begin
794 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
795 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
796 end;
797 exit;
798 end;
800 {TEST
801 if WADName = Map then
802 begin
803 //FreeMem(TextureWAD);
804 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
805 end;
808 WAD.FreeWAD();
810 if ResLength < 6 then
811 begin
812 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
813 exit;
814 end;
816 // ýòî ïòèöà? ýòî ñàìîë¸ò?
817 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
818 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
819 begin
820 // íåò, ýòî ñóïåðìåí!
821 if not WAD.ReadMemory(TextureWAD, ResLength) then
822 begin
823 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
824 exit;
825 end;
827 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
828 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
829 begin
830 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
831 exit;
832 end;
834 cfg := TConfig.CreateMem(TextData, ResLength);
836 TextureResource := cfg.ReadStr('', 'resource', '');
837 if TextureResource = '' then
838 begin
839 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
840 exit;
841 end;
843 _width := cfg.ReadInt('', 'framewidth', 0);
844 _height := cfg.ReadInt('', 'frameheight', 0);
845 _framecount := cfg.ReadInt('', 'framecount', 0);
846 _speed := cfg.ReadInt('', 'waitcount', 0);
847 _backanimation := cfg.ReadBool('', 'backanimation', False);
849 cfg.Free();
850 cfg := nil;
852 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
853 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
854 begin
855 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
856 exit;
857 end;
859 WAD.Free();
860 WAD := nil;
862 SetLength(Textures, Length(Textures)+1);
863 with Textures[High(Textures)] do
864 begin
865 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
866 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
867 begin
868 TextureName := RecName;
869 Width := _width;
870 Height := _height;
871 Anim := True;
872 FramesCount := _framecount;
873 Speed := _speed;
874 result := High(Textures);
875 end
876 else
877 begin
878 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
879 end;
880 end;
881 end
882 else
883 begin
884 // try animated image
886 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
887 if length(imgfmt) = 0 then
888 begin
889 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
890 exit;
891 end;
893 GlobalMetadata.ClearMetaItems();
894 GlobalMetadata.ClearMetaItemsForSaving();
895 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
896 begin
897 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
898 exit;
899 end;
900 if length(ia) = 0 then
901 begin
902 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
903 exit;
904 end;
906 WAD.Free();
907 WAD := nil;
909 _width := ia[0].width;
910 _height := ia[0].height;
911 _framecount := length(ia);
912 _speed := 1;
913 _backanimation := false;
914 frdelay := -1;
915 frloop := -666;
916 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
917 begin
918 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
919 try
920 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
921 frdelay := f;
922 if f < 0 then f := 0;
923 // rounding ;-)
924 c := f mod 28;
925 if c < 13 then c := 0 else c := 1;
926 f := (f div 28)+c;
927 if f < 1 then f := 1 else if f > 255 then f := 255;
928 _speed := f;
929 except
930 end;
931 end;
932 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
933 begin
934 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
935 try
936 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
937 frloop := f;
938 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
939 except
940 end;
941 end;
942 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
943 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
944 f := ord(_backanimation);
945 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);
947 SetLength(Textures, Length(Textures)+1);
948 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
949 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
950 begin
951 Textures[High(Textures)].TextureName := RecName;
952 Textures[High(Textures)].Width := _width;
953 Textures[High(Textures)].Height := _height;
954 Textures[High(Textures)].Anim := True;
955 Textures[High(Textures)].FramesCount := length(ia);
956 Textures[High(Textures)].Speed := _speed;
957 result := High(Textures);
958 //writeln(' CREATED!');
959 end
960 else
961 begin
962 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
963 end;
964 end;
965 finally
966 for f := 0 to High(ia) do FreeImage(ia[f]);
967 WAD.Free();
968 cfg.Free();
969 if TextureWAD <> nil then FreeMem(TextureWAD);
970 if TextData <> nil then FreeMem(TextData);
971 if TextureData <> nil then FreeMem(TextureData);
972 end;
973 end;
975 procedure CreateItem(Item: TItemRec_1);
976 begin
977 if g_Game_IsClient then Exit;
979 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
980 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
981 Exit;
983 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
984 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
985 end;
987 procedure CreateArea(Area: TAreaRec_1);
988 var
989 a: Integer;
990 id: DWORD = 0;
991 begin
992 case Area.AreaType of
993 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
994 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
995 begin
996 SetLength(RespawnPoints, Length(RespawnPoints)+1);
997 with RespawnPoints[High(RespawnPoints)] do
998 begin
999 X := Area.X;
1000 Y := Area.Y;
1001 Direction := TDirection(Area.Direction);
1003 case Area.AreaType of
1004 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1005 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1006 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1007 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1008 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1009 end;
1010 end;
1011 end;
1013 AREA_REDFLAG, AREA_BLUEFLAG:
1014 begin
1015 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1017 if FlagPoints[a] <> nil then Exit;
1019 New(FlagPoints[a]);
1021 with FlagPoints[a]^ do
1022 begin
1023 X := Area.X-FLAGRECT.X;
1024 Y := Area.Y-FLAGRECT.Y;
1025 Direction := TDirection(Area.Direction);
1026 end;
1028 with gFlags[a] do
1029 begin
1030 case a of
1031 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1032 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1033 end;
1035 Animation := TAnimation.Create(id, True, 8);
1036 Obj.Rect := FLAGRECT;
1038 g_Map_ResetFlag(a);
1039 end;
1040 end;
1042 AREA_DOMFLAG:
1043 begin
1044 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1045 with DOMFlagPoints[High(DOMFlagPoints)] do
1046 begin
1047 X := Area.X;
1048 Y := Area.Y;
1049 Direction := TDirection(Area.Direction);
1050 end;
1052 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1053 end;
1054 end;
1055 end;
1057 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
1058 var
1059 _trigger: TTrigger;
1060 begin
1061 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1063 with _trigger do
1064 begin
1065 X := Trigger.X;
1066 Y := Trigger.Y;
1067 Width := Trigger.Width;
1068 Height := Trigger.Height;
1069 Enabled := ByteBool(Trigger.Enabled);
1070 TexturePanel := Trigger.TexturePanel;
1071 TexturePanelType := fTexturePanel1Type;
1072 ShotPanelType := fTexturePanel2Type;
1073 TriggerType := Trigger.TriggerType;
1074 ActivateType := Trigger.ActivateType;
1075 Keys := Trigger.Keys;
1076 Data.Default := Trigger.DATA;
1077 end;
1079 g_Triggers_Create(_trigger);
1080 end;
1082 procedure CreateMonster(monster: TMonsterRec_1);
1083 var
1084 a: Integer;
1085 mon: TMonster;
1086 begin
1087 if g_Game_IsClient then Exit;
1089 if (gGameSettings.GameType = GT_SINGLE)
1090 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1091 begin
1092 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1094 if gTriggers <> nil then
1095 begin
1096 for a := 0 to High(gTriggers) do
1097 begin
1098 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1099 begin
1100 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1101 end;
1102 end;
1103 end;
1105 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1106 end;
1107 end;
1109 procedure g_Map_ReAdd_DieTriggers();
1111 function monsDieTrig (mon: TMonster): Boolean;
1112 var
1113 a: Integer;
1114 begin
1115 result := false; // don't stop
1116 mon.ClearTriggers();
1117 for a := 0 to High(gTriggers) do
1118 begin
1119 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1120 begin
1121 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1122 end;
1123 end;
1124 end;
1126 begin
1127 if g_Game_IsClient then Exit;
1129 g_Mons_ForEach(monsDieTrig);
1130 end;
1132 function extractWadName(resourceName: string): string;
1133 var
1134 posN: Integer;
1135 begin
1136 posN := Pos(':', resourceName);
1137 if posN > 0 then
1138 Result:= Copy(resourceName, 0, posN-1)
1139 else
1140 Result := '';
1141 end;
1143 procedure addResToExternalResList(res: string);
1144 begin
1145 res := extractWadName(res);
1146 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1147 gExternalResources.Add(res);
1148 end;
1150 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1151 var
1152 textures: TTexturesRec1Array;
1153 mapHeader: TMapHeaderRec_1;
1154 i: integer;
1155 resFile: String = '';
1156 begin
1157 if gExternalResources = nil then
1158 gExternalResources := TStringList.Create;
1160 gExternalResources.Clear;
1161 textures := mapReader.GetTextures();
1162 for i := 0 to High(textures) do
1163 begin
1164 addResToExternalResList(resFile);
1165 end;
1167 textures := nil;
1169 mapHeader := mapReader.GetMapHeader;
1171 addResToExternalResList(mapHeader.MusicName);
1172 addResToExternalResList(mapHeader.SkyName);
1173 end;
1176 procedure mapCreateGrid ();
1177 var
1178 mapX0: Integer = $3fffffff;
1179 mapY0: Integer = $3fffffff;
1180 mapX1: Integer = -$3fffffff;
1181 mapY1: Integer = -$3fffffff;
1183 procedure calcBoundingBox (constref panels: TPanelArray);
1184 var
1185 idx: Integer;
1186 pan: TPanel;
1187 begin
1188 for idx := 0 to High(panels) do
1189 begin
1190 pan := panels[idx];
1191 if not pan.visvalid then continue;
1192 if (pan.Width < 1) or (pan.Height < 1) then continue;
1193 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1194 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1195 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1196 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1197 end;
1198 end;
1200 procedure addPanelsToGrid (constref panels: TPanelArray);
1201 var
1202 idx: Integer;
1203 pan: TPanel;
1204 newtag: Integer;
1205 begin
1206 //tag := panelTypeToTag(tag);
1207 for idx := 0 to High(panels) do
1208 begin
1209 pan := panels[idx];
1210 if not pan.visvalid then continue;
1211 if (pan.proxyId <> -1) then
1212 begin
1213 {$IF DEFINED(D2F_DEBUG)}
1214 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);
1215 {$ENDIF}
1216 continue;
1217 end;
1218 case pan.PanelType of
1219 PANEL_WALL: newtag := GridTagWall;
1220 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1221 PANEL_BACK: newtag := GridTagBack;
1222 PANEL_FORE: newtag := GridTagFore;
1223 PANEL_WATER: newtag := GridTagWater;
1224 PANEL_ACID1: newtag := GridTagAcid1;
1225 PANEL_ACID2: newtag := GridTagAcid2;
1226 PANEL_STEP: newtag := GridTagStep;
1227 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1228 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1229 else continue; // oops
1230 end;
1231 pan.tag := newtag;
1233 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1234 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1235 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1236 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1238 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1239 begin
1240 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1241 end;
1243 {$ENDIF}
1244 end;
1245 end;
1247 begin
1248 mapGrid.Free();
1249 mapGrid := nil;
1251 calcBoundingBox(gWalls);
1252 calcBoundingBox(gRenderBackgrounds);
1253 calcBoundingBox(gRenderForegrounds);
1254 calcBoundingBox(gWater);
1255 calcBoundingBox(gAcid1);
1256 calcBoundingBox(gAcid2);
1257 calcBoundingBox(gSteps);
1258 calcBoundingBox(gLifts);
1259 calcBoundingBox(gBlockMon);
1261 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1263 if (mapX0 > 0) then mapX0 := 0;
1264 if (mapY0 > 0) then mapY0 := 0;
1266 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1267 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1269 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1270 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1272 addPanelsToGrid(gWalls);
1273 addPanelsToGrid(gRenderBackgrounds);
1274 addPanelsToGrid(gRenderForegrounds);
1275 addPanelsToGrid(gWater);
1276 addPanelsToGrid(gAcid1);
1277 addPanelsToGrid(gAcid2);
1278 addPanelsToGrid(gSteps);
1279 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1280 addPanelsToGrid(gBlockMon);
1282 mapGrid.dumpStats();
1284 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1285 end;
1288 function g_Map_Load(Res: String): Boolean;
1289 const
1290 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1291 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1292 var
1293 WAD: TWADFile;
1294 MapReader: TMapReader_1;
1295 Header: TMapHeaderRec_1;
1296 _textures: TTexturesRec1Array;
1297 _texnummap: array of Integer; // `_textures` -> `Textures`
1298 panels: TPanelsRec1Array;
1299 items: TItemsRec1Array;
1300 monsters: TMonsterRec1Array;
1301 areas: TAreasRec1Array;
1302 triggers: TTriggersRec1Array;
1303 a, b, c, k: Integer;
1304 PanelID: DWORD;
1305 AddTextures: TAddTextureArray;
1306 texture: TTextureRec_1;
1307 TriggersTable: Array of record
1308 TexturePanel: Integer;
1309 LiftPanel: Integer;
1310 DoorPanel: Integer;
1311 ShotPanel: Integer;
1312 end;
1313 FileName, mapResName, s, TexName: String;
1314 Data: Pointer;
1315 Len: Integer;
1316 ok, isAnim, trigRef: Boolean;
1317 CurTex, ntn: Integer;
1318 {$IF DEFINED(D2D_NEW_MAP_READER)}
1319 pr: TTextParser = nil;
1320 st: TStream = nil;
1321 wst: TSFSMemoryChunkStream = nil;
1322 rec: TDynRecord;
1323 {$ENDIF}
1324 begin
1325 mapGrid.Free();
1326 mapGrid := nil;
1328 Result := False;
1329 gMapInfo.Map := Res;
1330 TriggersTable := nil;
1331 FillChar(texture, SizeOf(texture), 0);
1333 sfsGCDisable(); // temporary disable removing of temporary volumes
1334 try
1335 // Çàãðóçêà WAD:
1336 FileName := g_ExtractWadName(Res);
1337 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1338 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1340 WAD := TWADFile.Create();
1341 if not WAD.ReadFile(FileName) then
1342 begin
1343 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1344 WAD.Free();
1345 Exit;
1346 end;
1348 //k8: why loader ignores path here?
1349 mapResName := g_ExtractFileName(Res);
1350 if not WAD.GetMapResource(mapResName, Data, Len) then
1351 begin
1352 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1353 WAD.Free();
1354 Exit;
1355 end;
1357 WAD.Free();
1359 if (Len < 4) then
1360 begin
1361 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1362 FreeMem(Data);
1363 exit;
1364 end;
1366 // Çàãðóçêà êàðòû:
1367 e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
1368 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1370 {$IF DEFINED(D2D_NEW_MAP_READER)}
1371 if (PChar(Data)[0] = 'M') and (PChar(Data)[1] = 'A') and (PChar(Data)[2] = 'P') and (PByte(Data)[3] = 1) then
1372 begin
1373 // nothing
1374 end
1375 else
1376 begin
1377 e_LogWritefln('Loading text map: %s', [mapResName]);
1378 loadMapDefinition();
1379 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
1380 //e_LogWritefln('***'#10'%s'#10'***', [dfmapdef.headerType.definition]);
1381 wst := TSFSMemoryChunkStream.Create(Data, Len);
1382 try
1383 pr := TFileTextParser.Create(wst);
1384 e_LogWritefln('parsing text map: %s', [mapResName]);
1385 rec := dfmapdef.parseMap(pr);
1386 except on e: Exception do
1387 begin
1388 if (pr <> nil) then e_LogWritefln('ERROR at (%s,%s): %s', [pr.line, pr.col, e.message])
1389 else e_LogWritefln('ERROR: %s', [e.message]);
1390 pr.Free();
1391 wst.Free();
1392 FreeMem(Data);
1393 exit;
1394 end;
1395 end;
1396 pr.Free();
1397 //wst.Free(); // pr will do it
1398 e_LogWritefln('writing text map to temporary bin storage...', []);
1399 st := TMemoryStream.Create();
1400 try
1401 rec.writeBinTo(st);
1402 Len := Integer(st.position);
1403 st.position := 0;
1404 FreeMem(Data);
1405 GetMem(Data, Len);
1406 st.ReadBuffer(Data^, Len);
1407 except on e: Exception do
1408 begin
1409 rec.Free();
1410 st.Free();
1411 e_LogWritefln('ERROR: %s', [e.message]);
1412 FreeMem(Data);
1413 exit;
1414 end;
1415 end;
1416 st.Free();
1417 end;
1418 {$ENDIF}
1420 MapReader := TMapReader_1.Create();
1422 if not MapReader.LoadMap(Data) then
1423 begin
1424 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1425 FreeMem(Data);
1426 MapReader.Free();
1427 Exit;
1428 end;
1430 FreeMem(Data);
1431 generateExternalResourcesList(MapReader);
1432 // Çàãðóçêà òåêñòóð:
1433 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1434 _textures := MapReader.GetTextures();
1435 _texnummap := nil;
1437 // Çàãðóçêà îïèñàíèÿ êàðòû:
1438 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1439 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1440 Header := MapReader.GetMapHeader();
1442 with gMapInfo do
1443 begin
1444 Name := Header.MapName;
1445 Description := Header.MapDescription;
1446 Author := Header.MapAuthor;
1447 MusicName := Header.MusicName;
1448 SkyName := Header.SkyName;
1449 Height := Header.Height;
1450 Width := Header.Width;
1451 end;
1453 // Äîáàâëåíèå òåêñòóð â Textures[]:
1454 if _textures <> nil then
1455 begin
1456 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1457 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1458 SetLength(_texnummap, length(_textures));
1460 for a := 0 to High(_textures) do
1461 begin
1462 SetLength(s, 64);
1463 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1464 for b := 1 to Length(s) do
1465 begin
1466 if s[b] = #0 then
1467 begin
1468 SetLength(s, b-1);
1469 Break;
1470 end;
1471 end;
1472 {$IF DEFINED(D2F_DEBUG)}
1473 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1474 {$ENDIF}
1475 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1476 // Àíèìèðîâàííàÿ òåêñòóðà:
1477 if ByteBool(_textures[a].Anim) then
1478 begin
1479 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1480 if ntn < 0 then
1481 begin
1482 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1483 ntn := CreateNullTexture(_textures[a].Resource);
1484 end;
1485 end
1486 else // Îáû÷íàÿ òåêñòóðà:
1487 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1488 if ntn < 0 then
1489 begin
1490 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1491 ntn := CreateNullTexture(_textures[a].Resource);
1492 end;
1494 _texnummap[a] := ntn; // fix texture number
1495 g_Game_StepLoading();
1496 end;
1497 end;
1499 // Çàãðóçêà òðèããåðîâ:
1500 gTriggerClientID := 0;
1501 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1502 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1503 triggers := MapReader.GetTriggers();
1505 // Çàãðóçêà ïàíåëåé:
1506 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1507 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1508 panels := MapReader.GetPanels();
1510 // check texture numbers for panels
1511 for a := 0 to High(panels) do
1512 begin
1513 if panels[a].TextureNum > High(_textures) then
1514 begin
1515 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1516 result := false;
1517 exit;
1518 end;
1519 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1520 end;
1522 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1523 if triggers <> nil then
1524 begin
1525 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1526 SetLength(TriggersTable, Length(triggers));
1527 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1529 for a := 0 to High(TriggersTable) do
1530 begin
1531 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1532 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1533 // Ëèôòû:
1534 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1535 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1536 else
1537 TriggersTable[a].LiftPanel := -1;
1538 // Äâåðè:
1539 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1540 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1541 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1542 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1543 else
1544 TriggersTable[a].DoorPanel := -1;
1545 // Òóðåëü:
1546 if triggers[a].TriggerType = TRIGGER_SHOT then
1547 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1548 else
1549 TriggersTable[a].ShotPanel := -1;
1551 g_Game_StepLoading();
1552 end;
1553 end;
1555 // Ñîçäàåì ïàíåëè:
1556 if panels <> nil then
1557 begin
1558 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1559 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1561 for a := 0 to High(panels) do
1562 begin
1563 SetLength(AddTextures, 0);
1564 trigRef := False;
1565 CurTex := -1;
1566 if _textures <> nil then
1567 begin
1568 texture := _textures[panels[a].TextureNum];
1569 ok := True;
1570 end
1571 else
1572 ok := False;
1574 if ok then
1575 begin
1576 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1577 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1578 ok := False;
1579 if (TriggersTable <> nil) and (_textures <> nil) then
1580 for b := 0 to High(TriggersTable) do
1581 if (TriggersTable[b].TexturePanel = a)
1582 or (TriggersTable[b].ShotPanel = a) then
1583 begin
1584 trigRef := True;
1585 ok := True;
1586 Break;
1587 end;
1588 end;
1590 if ok then
1591 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1592 SetLength(s, 64);
1593 CopyMemory(@s[1], @texture.Resource[0], 64);
1594 // Èçìåðÿåì äëèíó:
1595 Len := Length(s);
1596 for c := Len downto 1 do
1597 if s[c] <> #0 then
1598 begin
1599 Len := c;
1600 Break;
1601 end;
1602 SetLength(s, Len);
1604 // Ñïåö-òåêñòóðû çàïðåùåíû:
1605 if g_Map_IsSpecialTexture(s) then
1606 ok := False
1607 else
1608 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1609 ok := g_Texture_NumNameFindStart(s);
1611 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1612 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1613 if ok then
1614 begin
1615 k := NNF_NAME_BEFORE;
1616 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1617 while ok or (k = NNF_NAME_BEFORE) or
1618 (k = NNF_NAME_EQUALS) do
1619 begin
1620 k := g_Texture_NumNameFindNext(TexName);
1622 if (k = NNF_NAME_BEFORE) or
1623 (k = NNF_NAME_AFTER) then
1624 begin
1625 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1626 if ByteBool(texture.Anim) then
1627 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1628 isAnim := True;
1629 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1630 if not ok then
1631 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1632 isAnim := False;
1633 ok := CreateTexture(TexName, FileName, False) >= 0;
1634 end;
1635 end
1636 else
1637 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1638 isAnim := False;
1639 ok := CreateTexture(TexName, FileName, False) >= 0;
1640 if not ok then
1641 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1642 isAnim := True;
1643 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1644 end;
1645 end;
1647 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1648 if ok then
1649 begin
1650 for c := 0 to High(Textures) do
1651 if Textures[c].TextureName = TexName then
1652 begin
1653 SetLength(AddTextures, Length(AddTextures)+1);
1654 AddTextures[High(AddTextures)].Texture := c;
1655 AddTextures[High(AddTextures)].Anim := isAnim;
1656 Break;
1657 end;
1658 end;
1659 end
1660 else
1661 if k = NNF_NAME_EQUALS then
1662 begin
1663 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1664 SetLength(AddTextures, Length(AddTextures)+1);
1665 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1666 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1667 CurTex := High(AddTextures);
1668 ok := True;
1669 end
1670 else // NNF_NO_NAME
1671 ok := False;
1672 end; // while ok...
1674 ok := True;
1675 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1676 end; // if ok - ññûëàþòñÿ òðèããåðû
1678 if not ok then
1679 begin
1680 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1681 SetLength(AddTextures, 1);
1682 AddTextures[0].Texture := panels[a].TextureNum;
1683 AddTextures[0].Anim := ByteBool(texture.Anim);
1684 CurTex := 0;
1685 end;
1687 //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);
1689 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1690 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1692 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1693 if TriggersTable <> nil then
1694 for b := 0 to High(TriggersTable) do
1695 begin
1696 // Òðèããåð äâåðè/ëèôòà:
1697 if (TriggersTable[b].LiftPanel = a) or
1698 (TriggersTable[b].DoorPanel = a) then
1699 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1700 // Òðèããåð ñìåíû òåêñòóðû:
1701 if TriggersTable[b].TexturePanel = a then
1702 triggers[b].TexturePanel := PanelID;
1703 // Òðèããåð "Òóðåëü":
1704 if TriggersTable[b].ShotPanel = a then
1705 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1706 end;
1708 g_Game_StepLoading();
1709 end;
1710 end;
1712 // create map grid, init other grids (for monsters, for example)
1713 e_WriteLog('Creating map grid', MSG_NOTIFY);
1714 mapCreateGrid();
1716 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1717 if (triggers <> nil) and not gLoadGameMode then
1718 begin
1719 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1720 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1721 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1722 for a := 0 to High(triggers) do
1723 begin
1724 if triggers[a].TexturePanel <> -1 then
1725 b := panels[TriggersTable[a].TexturePanel].PanelType
1726 else
1727 b := 0;
1728 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1729 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1730 c := panels[TriggersTable[a].ShotPanel].PanelType
1731 else
1732 c := 0;
1733 CreateTrigger(triggers[a], b, c);
1734 end;
1735 end;
1737 // Çàãðóçêà ïðåäìåòîâ:
1738 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1739 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1740 items := MapReader.GetItems();
1742 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1743 if (items <> nil) and not gLoadGameMode then
1744 begin
1745 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1746 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1747 for a := 0 to High(items) do
1748 CreateItem(Items[a]);
1749 end;
1751 // Çàãðóçêà îáëàñòåé:
1752 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1753 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1754 areas := MapReader.GetAreas();
1756 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1757 if areas <> nil then
1758 begin
1759 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1760 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1761 for a := 0 to High(areas) do
1762 CreateArea(areas[a]);
1763 end;
1765 // Çàãðóçêà ìîíñòðîâ:
1766 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1767 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1768 monsters := MapReader.GetMonsters();
1770 gTotalMonsters := 0;
1772 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1773 if (monsters <> nil) and not gLoadGameMode then
1774 begin
1775 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1776 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1777 for a := 0 to High(monsters) do
1778 CreateMonster(monsters[a]);
1779 end;
1781 MapReader.Free();
1783 // Çàãðóçêà íåáà:
1784 if gMapInfo.SkyName <> '' then
1785 begin
1786 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1787 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1788 FileName := g_ExtractWadName(gMapInfo.SkyName);
1790 if FileName <> '' then
1791 FileName := GameDir+'/wads/'+FileName
1792 else
1793 begin
1794 FileName := g_ExtractWadName(Res);
1795 end;
1797 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1798 if g_Texture_CreateWAD(BackID, s) then
1799 begin
1800 g_Game_SetupScreenSize();
1801 end
1802 else
1803 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1804 end;
1806 // Çàãðóçêà ìóçûêè:
1807 ok := False;
1808 if gMapInfo.MusicName <> '' then
1809 begin
1810 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1811 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1812 FileName := g_ExtractWadName(gMapInfo.MusicName);
1814 if FileName <> '' then
1815 FileName := GameDir+'/wads/'+FileName
1816 else
1817 begin
1818 FileName := g_ExtractWadName(Res);
1819 end;
1821 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1822 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1823 ok := True
1824 else
1825 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1826 end;
1828 // Îñòàëüíûå óñòàíâêè:
1829 CreateDoorMap();
1830 CreateLiftMap();
1832 g_Items_Init();
1833 g_Weapon_Init();
1834 g_Monsters_Init();
1836 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1837 if not gLoadGameMode then
1838 g_GFX_Init();
1840 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1841 _textures := nil;
1842 panels := nil;
1843 items := nil;
1844 areas := nil;
1845 triggers := nil;
1846 TriggersTable := nil;
1847 AddTextures := nil;
1849 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1850 if ok and (not gLoadGameMode) then
1851 begin
1852 gMusic.SetByName(gMapInfo.MusicName);
1853 gMusic.Play();
1854 end
1855 else
1856 gMusic.SetByName('');
1857 finally
1858 sfsGCEnable(); // enable releasing unused volumes
1859 end;
1861 e_WriteLog('Done loading map.', MSG_NOTIFY);
1862 Result := True;
1863 end;
1865 function g_Map_GetMapInfo(Res: String): TMapInfo;
1866 var
1867 WAD: TWADFile;
1868 MapReader: TMapReader_1;
1869 Header: TMapHeaderRec_1;
1870 FileName: String;
1871 Data: Pointer;
1872 Len: Integer;
1873 begin
1874 FillChar(Result, SizeOf(Result), 0);
1875 FileName := g_ExtractWadName(Res);
1877 WAD := TWADFile.Create();
1878 if not WAD.ReadFile(FileName) then
1879 begin
1880 WAD.Free();
1881 Exit;
1882 end;
1884 //k8: it ignores path again
1885 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1886 begin
1887 WAD.Free();
1888 Exit;
1889 end;
1891 WAD.Free();
1893 MapReader := TMapReader_1.Create();
1895 if not MapReader.LoadMap(Data) then
1896 begin
1897 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1898 ZeroMemory(@Header, SizeOf(Header));
1899 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1900 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1901 end
1902 else
1903 begin
1904 Header := MapReader.GetMapHeader();
1905 Result.Name := Header.MapName;
1906 Result.Description := Header.MapDescription;
1907 end;
1909 FreeMem(Data);
1910 MapReader.Free();
1912 Result.Map := Res;
1913 Result.Author := Header.MapAuthor;
1914 Result.Height := Header.Height;
1915 Result.Width := Header.Width;
1916 end;
1918 function g_Map_GetMapsList(WADName: string): SArray;
1919 var
1920 WAD: TWADFile;
1921 a: Integer;
1922 ResList: SArray;
1923 begin
1924 Result := nil;
1925 WAD := TWADFile.Create();
1926 if not WAD.ReadFile(WADName) then
1927 begin
1928 WAD.Free();
1929 Exit;
1930 end;
1931 ResList := WAD.GetMapResources();
1932 if ResList <> nil then
1933 begin
1934 for a := 0 to High(ResList) do
1935 begin
1936 SetLength(Result, Length(Result)+1);
1937 Result[High(Result)] := ResList[a];
1938 end;
1939 end;
1940 WAD.Free();
1941 end;
1943 function g_Map_Exist(Res: string): Boolean;
1944 var
1945 WAD: TWADFile;
1946 FileName, mnn: string;
1947 ResList: SArray;
1948 a: Integer;
1949 begin
1950 Result := False;
1952 FileName := addWadExtension(g_ExtractWadName(Res));
1954 WAD := TWADFile.Create;
1955 if not WAD.ReadFile(FileName) then
1956 begin
1957 WAD.Free();
1958 Exit;
1959 end;
1961 ResList := WAD.GetMapResources();
1962 WAD.Free();
1964 mnn := g_ExtractFileName(Res);
1965 if ResList <> nil then
1966 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1967 begin
1968 Result := True;
1969 Exit;
1970 end;
1971 end;
1973 procedure g_Map_Free();
1974 var
1975 a: Integer;
1977 procedure FreePanelArray(var panels: TPanelArray);
1978 var
1979 i: Integer;
1981 begin
1982 if panels <> nil then
1983 begin
1984 for i := 0 to High(panels) do
1985 panels[i].Free();
1986 panels := nil;
1987 end;
1988 end;
1990 begin
1991 g_GFX_Free();
1992 g_Weapon_Free();
1993 g_Items_Free();
1994 g_Triggers_Free();
1995 g_Monsters_Free();
1997 RespawnPoints := nil;
1998 if FlagPoints[FLAG_RED] <> nil then
1999 begin
2000 Dispose(FlagPoints[FLAG_RED]);
2001 FlagPoints[FLAG_RED] := nil;
2002 end;
2003 if FlagPoints[FLAG_BLUE] <> nil then
2004 begin
2005 Dispose(FlagPoints[FLAG_BLUE]);
2006 FlagPoints[FLAG_BLUE] := nil;
2007 end;
2008 //DOMFlagPoints := nil;
2010 //gDOMFlags := nil;
2012 if Textures <> nil then
2013 begin
2014 for a := 0 to High(Textures) do
2015 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2016 if Textures[a].Anim then
2017 g_Frames_DeleteByID(Textures[a].FramesID)
2018 else
2019 if Textures[a].TextureID <> TEXTURE_NONE then
2020 e_DeleteTexture(Textures[a].TextureID);
2022 Textures := nil;
2023 end;
2025 FreePanelArray(gWalls);
2026 FreePanelArray(gRenderBackgrounds);
2027 FreePanelArray(gRenderForegrounds);
2028 FreePanelArray(gWater);
2029 FreePanelArray(gAcid1);
2030 FreePanelArray(gAcid2);
2031 FreePanelArray(gSteps);
2032 FreePanelArray(gLifts);
2033 FreePanelArray(gBlockMon);
2035 if BackID <> DWORD(-1) then
2036 begin
2037 gBackSize.X := 0;
2038 gBackSize.Y := 0;
2039 e_DeleteTexture(BackID);
2040 BackID := DWORD(-1);
2041 end;
2043 g_Game_StopAllSounds(False);
2044 gMusic.FreeSound();
2045 g_Sound_Delete(gMapInfo.MusicName);
2047 gMapInfo.Name := '';
2048 gMapInfo.Description := '';
2049 gMapInfo.MusicName := '';
2050 gMapInfo.Height := 0;
2051 gMapInfo.Width := 0;
2053 gDoorMap := nil;
2054 gLiftMap := nil;
2056 PanelByID := nil;
2057 end;
2059 procedure g_Map_Update();
2060 var
2061 a, d, j: Integer;
2062 m: Word;
2063 s: String;
2065 procedure UpdatePanelArray(var panels: TPanelArray);
2066 var
2067 i: Integer;
2069 begin
2070 if panels <> nil then
2071 for i := 0 to High(panels) do
2072 panels[i].Update();
2073 end;
2075 begin
2076 UpdatePanelArray(gWalls);
2077 UpdatePanelArray(gRenderBackgrounds);
2078 UpdatePanelArray(gRenderForegrounds);
2079 UpdatePanelArray(gWater);
2080 UpdatePanelArray(gAcid1);
2081 UpdatePanelArray(gAcid2);
2082 UpdatePanelArray(gSteps);
2084 if gGameSettings.GameMode = GM_CTF then
2085 begin
2086 for a := FLAG_RED to FLAG_BLUE do
2087 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2088 with gFlags[a] do
2089 begin
2090 if gFlags[a].Animation <> nil then
2091 gFlags[a].Animation.Update();
2093 m := g_Obj_Move(@Obj, True, True);
2095 if gTime mod (GAME_TICK*2) <> 0 then
2096 Continue;
2098 // Ñîïðîòèâëåíèå âîçäóõà:
2099 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2101 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2102 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2103 begin
2104 g_Map_ResetFlag(a);
2105 gFlags[a].CaptureTime := 0;
2106 if a = FLAG_RED then
2107 s := _lc[I_PLAYER_FLAG_RED]
2108 else
2109 s := _lc[I_PLAYER_FLAG_BLUE];
2110 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2112 if g_Game_IsNet then
2113 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2114 Continue;
2115 end;
2117 if Count > 0 then
2118 Count := Count - 1;
2120 // Èãðîê áåðåò ôëàã:
2121 if gPlayers <> nil then
2122 begin
2123 j := Random(Length(gPlayers)) - 1;
2125 for d := 0 to High(gPlayers) do
2126 begin
2127 Inc(j);
2128 if j > High(gPlayers) then
2129 j := 0;
2131 if gPlayers[j] <> nil then
2132 if gPlayers[j].Live and
2133 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2134 begin
2135 if gPlayers[j].GetFlag(a) then
2136 Break;
2137 end;
2138 end;
2139 end;
2140 end;
2141 end;
2142 end;
2145 // old algo
2146 procedure g_Map_DrawPanels (PanelType: Word);
2148 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2149 var
2150 idx: Integer;
2151 begin
2152 if (panels <> nil) then
2153 begin
2154 // alas, no visible set
2155 for idx := 0 to High(panels) do
2156 begin
2157 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2158 end;
2159 end;
2160 end;
2162 begin
2163 case PanelType of
2164 PANEL_WALL: DrawPanels(gWalls);
2165 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2166 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2167 PANEL_FORE: DrawPanels(gRenderForegrounds);
2168 PANEL_WATER: DrawPanels(gWater);
2169 PANEL_ACID1: DrawPanels(gAcid1);
2170 PANEL_ACID2: DrawPanels(gAcid2);
2171 PANEL_STEP: DrawPanels(gSteps);
2172 end;
2173 end;
2176 // new algo
2177 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2179 function checker (pan: TPanel; tag: Integer): Boolean;
2180 begin
2181 result := false; // don't stop, ever
2182 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2183 gDrawPanelList.insert(pan);
2184 end;
2186 begin
2187 dplClear();
2188 //tagmask := panelTypeToTag(PanelType);
2189 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2190 // list will be rendered in `g_game.DrawPlayer()`
2191 end;
2194 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2196 function checker (pan: TPanel; tag: Integer): Boolean;
2197 begin
2198 result := false; // don't stop, ever
2199 pan.DrawShadowVolume(lightX, lightY, radius);
2200 end;
2202 begin
2203 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2204 end;
2207 procedure g_Map_DrawBack(dx, dy: Integer);
2208 begin
2209 if gDrawBackGround and (BackID <> DWORD(-1)) then
2210 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2211 else
2212 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2213 end;
2215 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2216 PanelType: Word; b1x3: Boolean=false): Boolean;
2217 var
2218 a, h: Integer;
2219 begin
2220 Result := False;
2222 if WordBool(PanelType and PANEL_WALL) then
2223 if gWalls <> nil then
2224 begin
2225 h := High(gWalls);
2227 for a := 0 to h do
2228 if gWalls[a].Enabled and
2229 g_Collide(X, Y, Width, Height,
2230 gWalls[a].X, gWalls[a].Y,
2231 gWalls[a].Width, gWalls[a].Height) then
2232 begin
2233 Result := True;
2234 Exit;
2235 end;
2236 end;
2238 if WordBool(PanelType and PANEL_WATER) then
2239 if gWater <> nil then
2240 begin
2241 h := High(gWater);
2243 for a := 0 to h do
2244 if g_Collide(X, Y, Width, Height,
2245 gWater[a].X, gWater[a].Y,
2246 gWater[a].Width, gWater[a].Height) then
2247 begin
2248 Result := True;
2249 Exit;
2250 end;
2251 end;
2253 if WordBool(PanelType and PANEL_ACID1) then
2254 if gAcid1 <> nil then
2255 begin
2256 h := High(gAcid1);
2258 for a := 0 to h do
2259 if g_Collide(X, Y, Width, Height,
2260 gAcid1[a].X, gAcid1[a].Y,
2261 gAcid1[a].Width, gAcid1[a].Height) then
2262 begin
2263 Result := True;
2264 Exit;
2265 end;
2266 end;
2268 if WordBool(PanelType and PANEL_ACID2) then
2269 if gAcid2 <> nil then
2270 begin
2271 h := High(gAcid2);
2273 for a := 0 to h do
2274 if g_Collide(X, Y, Width, Height,
2275 gAcid2[a].X, gAcid2[a].Y,
2276 gAcid2[a].Width, gAcid2[a].Height) then
2277 begin
2278 Result := True;
2279 Exit;
2280 end;
2281 end;
2283 if WordBool(PanelType and PANEL_STEP) then
2284 if gSteps <> nil then
2285 begin
2286 h := High(gSteps);
2288 for a := 0 to h do
2289 if g_Collide(X, Y, Width, Height,
2290 gSteps[a].X, gSteps[a].Y,
2291 gSteps[a].Width, gSteps[a].Height) then
2292 begin
2293 Result := True;
2294 Exit;
2295 end;
2296 end;
2298 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2299 if gLifts <> nil then
2300 begin
2301 h := High(gLifts);
2303 for a := 0 to h do
2304 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2305 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2306 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2307 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2308 g_Collide(X, Y, Width, Height,
2309 gLifts[a].X, gLifts[a].Y,
2310 gLifts[a].Width, gLifts[a].Height) then
2311 begin
2312 Result := True;
2313 Exit;
2314 end;
2315 end;
2317 if WordBool(PanelType and PANEL_BLOCKMON) then
2318 if gBlockMon <> nil then
2319 begin
2320 h := High(gBlockMon);
2322 for a := 0 to h do
2323 if ( (not b1x3) or
2324 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2325 g_Collide(X, Y, Width, Height,
2326 gBlockMon[a].X, gBlockMon[a].Y,
2327 gBlockMon[a].Width, gBlockMon[a].Height) then
2328 begin
2329 Result := True;
2330 Exit;
2331 end;
2332 end;
2333 end;
2335 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2336 var
2337 texid: DWORD;
2339 function checkPanels (constref panels: TPanelArray): Boolean;
2340 var
2341 a: Integer;
2342 begin
2343 result := false;
2344 if panels = nil then exit;
2345 for a := 0 to High(panels) do
2346 begin
2347 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2348 begin
2349 result := true;
2350 texid := panels[a].GetTextureID();
2351 exit;
2352 end;
2353 end;
2354 end;
2356 begin
2357 texid := TEXTURE_NONE;
2358 result := texid;
2359 if not checkPanels(gWater) then
2360 if not checkPanels(gAcid1) then
2361 if not checkPanels(gAcid2) then exit;
2362 result := texid;
2363 end;
2366 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2367 const
2368 SlowMask = GridTagLift or GridTagBlockMon;
2369 function checker (pan: TPanel; tag: Integer): Boolean;
2370 begin
2372 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2373 begin
2374 result := pan.Enabled;
2375 exit;
2376 end;
2379 if ((tag and GridTagLift) <> 0) then
2380 begin
2381 result :=
2382 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2383 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2384 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2385 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2386 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2387 exit;
2388 end;
2390 if ((tag and GridTagBlockMon) <> 0) then
2391 begin
2392 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2393 exit;
2394 end;
2396 // other shit
2397 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2398 result := true; // i found her!
2399 end;
2401 var
2402 tagmask: Integer = 0;
2403 begin
2404 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2405 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2406 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2407 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2408 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2409 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2410 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2412 if (tagmask = 0) then begin result := false; exit; end; // just in case
2414 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2415 if gdbg_map_use_accel_coldet then
2416 begin
2417 if (Width = 1) and (Height = 1) then
2418 begin
2419 if ((tagmask and SlowMask) <> 0) then
2420 begin
2421 // slow
2422 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2423 end
2424 else
2425 begin
2426 // fast
2427 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2428 end;
2429 end
2430 else
2431 begin
2432 if ((tagmask and SlowMask) <> 0) then
2433 begin
2434 // slow
2435 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2436 end
2437 else
2438 begin
2439 // fast
2440 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2441 end;
2442 end;
2443 end
2444 else
2445 begin
2446 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2447 end;
2448 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2449 end;
2452 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2453 var
2454 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2455 texid: DWORD;
2457 // slightly different from the old code, but meh...
2458 function checker (pan: TPanel; tag: Integer): Boolean;
2459 begin
2460 result := false; // don't stop, ever
2461 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2462 // check priorities
2463 case cctype of
2464 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2465 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2466 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2467 end;
2468 // collision?
2469 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2470 // yeah
2471 texid := pan.GetTextureID();
2472 // water? water has the highest priority, so stop right here
2473 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2474 // acid2?
2475 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2476 // acid1?
2477 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2478 end;
2480 begin
2481 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2482 if gdbg_map_use_accel_coldet then
2483 begin
2484 texid := TEXTURE_NONE;
2485 if (Width = 1) and (Height = 1) then
2486 begin
2487 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2488 end
2489 else
2490 begin
2491 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2492 end;
2493 result := texid;
2494 end
2495 else
2496 begin
2497 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2498 end;
2499 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2500 end;
2503 procedure g_Map_EnableWall(ID: DWORD);
2504 var
2505 pan: TPanel;
2506 begin
2507 pan := gWalls[ID];
2508 pan.Enabled := True;
2509 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2511 mapGrid.proxyEnabled[pan.proxyId] := true;
2512 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2513 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2515 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2517 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2518 //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);
2519 {$ENDIF}
2520 end;
2522 procedure g_Map_DisableWall(ID: DWORD);
2523 var
2524 pan: TPanel;
2525 begin
2526 pan := gWalls[ID];
2527 pan.Enabled := False;
2528 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2530 mapGrid.proxyEnabled[pan.proxyId] := false;
2531 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2533 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2535 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2536 //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);
2537 {$ENDIF}
2538 end;
2540 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2541 var
2542 tp: TPanel;
2543 begin
2544 case PanelType of
2545 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2546 tp := gWalls[ID];
2547 PANEL_FORE:
2548 tp := gRenderForegrounds[ID];
2549 PANEL_BACK:
2550 tp := gRenderBackgrounds[ID];
2551 PANEL_WATER:
2552 tp := gWater[ID];
2553 PANEL_ACID1:
2554 tp := gAcid1[ID];
2555 PANEL_ACID2:
2556 tp := gAcid2[ID];
2557 PANEL_STEP:
2558 tp := gSteps[ID];
2559 else
2560 Exit;
2561 end;
2563 tp.NextTexture(AnimLoop);
2564 if g_Game_IsServer and g_Game_IsNet then
2565 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2566 end;
2568 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2569 begin
2570 if gLifts[ID].LiftType = t then
2571 Exit;
2573 with gLifts[ID] do
2574 begin
2575 LiftType := t;
2577 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2578 //TODO: make separate lift tags, and change tag here
2580 if LiftType = 0 then
2581 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2582 else if LiftType = 1 then
2583 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2584 else if LiftType = 2 then
2585 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2586 else if LiftType = 3 then
2587 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2589 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2590 end;
2591 end;
2593 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2594 var
2595 a: Integer;
2596 PointsArray: Array of TRespawnPoint;
2597 begin
2598 Result := False;
2599 SetLength(PointsArray, 0);
2601 if RespawnPoints = nil then
2602 Exit;
2604 for a := 0 to High(RespawnPoints) do
2605 if RespawnPoints[a].PointType = PointType then
2606 begin
2607 SetLength(PointsArray, Length(PointsArray)+1);
2608 PointsArray[High(PointsArray)] := RespawnPoints[a];
2609 end;
2611 if PointsArray = nil then
2612 Exit;
2614 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2615 Result := True;
2616 end;
2618 function g_Map_GetPointCount(PointType: Byte): Word;
2619 var
2620 a: Integer;
2621 begin
2622 Result := 0;
2624 if RespawnPoints = nil then
2625 Exit;
2627 for a := 0 to High(RespawnPoints) do
2628 if RespawnPoints[a].PointType = PointType then
2629 Result := Result + 1;
2630 end;
2632 function g_Map_HaveFlagPoints(): Boolean;
2633 begin
2634 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2635 end;
2637 procedure g_Map_ResetFlag(Flag: Byte);
2638 begin
2639 with gFlags[Flag] do
2640 begin
2641 Obj.X := -1000;
2642 Obj.Y := -1000;
2643 Obj.Vel.X := 0;
2644 Obj.Vel.Y := 0;
2645 Direction := D_LEFT;
2646 State := FLAG_STATE_NONE;
2647 if FlagPoints[Flag] <> nil then
2648 begin
2649 Obj.X := FlagPoints[Flag]^.X;
2650 Obj.Y := FlagPoints[Flag]^.Y;
2651 Direction := FlagPoints[Flag]^.Direction;
2652 State := FLAG_STATE_NORMAL;
2653 end;
2654 Count := -1;
2655 end;
2656 end;
2658 procedure g_Map_DrawFlags();
2659 var
2660 i, dx: Integer;
2661 Mirror: TMirrorType;
2662 begin
2663 if gGameSettings.GameMode <> GM_CTF then
2664 Exit;
2666 for i := FLAG_RED to FLAG_BLUE do
2667 with gFlags[i] do
2668 if State <> FLAG_STATE_CAPTURED then
2669 begin
2670 if State = FLAG_STATE_NONE then
2671 continue;
2673 if Direction = D_LEFT then
2674 begin
2675 Mirror := M_HORIZONTAL;
2676 dx := -1;
2677 end
2678 else
2679 begin
2680 Mirror := M_NONE;
2681 dx := 1;
2682 end;
2684 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2686 if g_debug_Frames then
2687 begin
2688 e_DrawQuad(Obj.X+Obj.Rect.X,
2689 Obj.Y+Obj.Rect.Y,
2690 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2691 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2692 0, 255, 0);
2693 end;
2694 end;
2695 end;
2697 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2698 var
2699 dw: DWORD;
2700 b: Byte;
2701 str: String;
2702 boo: Boolean;
2704 procedure SavePanelArray(var panels: TPanelArray);
2705 var
2706 PAMem: TBinMemoryWriter;
2707 i: Integer;
2708 begin
2709 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2710 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2712 i := 0;
2713 while i < Length(panels) do
2714 begin
2715 if panels[i].SaveIt then
2716 begin
2717 // ID ïàíåëè:
2718 PAMem.WriteInt(i);
2719 // Ñîõðàíÿåì ïàíåëü:
2720 panels[i].SaveState(PAMem);
2721 end;
2722 Inc(i);
2723 end;
2725 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2726 PAMem.SaveToMemory(Mem);
2727 PAMem.Free();
2728 end;
2730 procedure SaveFlag(flag: PFlag);
2731 begin
2732 // Ñèãíàòóðà ôëàãà:
2733 dw := FLAG_SIGNATURE; // 'FLAG'
2734 Mem.WriteDWORD(dw);
2735 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2736 Mem.WriteByte(flag^.RespawnType);
2737 // Ñîñòîÿíèå ôëàãà:
2738 Mem.WriteByte(flag^.State);
2739 // Íàïðàâëåíèå ôëàãà:
2740 if flag^.Direction = D_LEFT then
2741 b := 1
2742 else // D_RIGHT
2743 b := 2;
2744 Mem.WriteByte(b);
2745 // Îáúåêò ôëàãà:
2746 Obj_SaveState(@flag^.Obj, Mem);
2747 end;
2749 begin
2750 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2752 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2753 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2754 SavePanelArray(gWalls);
2755 // Ñîõðàíÿåì ïàíåëè ôîíà:
2756 SavePanelArray(gRenderBackgrounds);
2757 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2758 SavePanelArray(gRenderForegrounds);
2759 // Ñîõðàíÿåì ïàíåëè âîäû:
2760 SavePanelArray(gWater);
2761 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2762 SavePanelArray(gAcid1);
2763 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2764 SavePanelArray(gAcid2);
2765 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2766 SavePanelArray(gSteps);
2767 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2768 SavePanelArray(gLifts);
2769 ///// /////
2771 ///// Ñîõðàíÿåì ìóçûêó: /////
2772 // Ñèãíàòóðà ìóçûêè:
2773 dw := MUSIC_SIGNATURE; // 'MUSI'
2774 Mem.WriteDWORD(dw);
2775 // Íàçâàíèå ìóçûêè:
2776 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2777 if gMusic.NoMusic then
2778 str := ''
2779 else
2780 str := gMusic.Name;
2781 Mem.WriteString(str, 64);
2782 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2783 dw := gMusic.GetPosition();
2784 Mem.WriteDWORD(dw);
2785 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2786 boo := gMusic.SpecPause;
2787 Mem.WriteBoolean(boo);
2788 ///// /////
2790 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2791 Mem.WriteInt(gTotalMonsters);
2792 ///// /////
2794 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2795 if gGameSettings.GameMode = GM_CTF then
2796 begin
2797 // Ôëàã Êðàñíîé êîìàíäû:
2798 SaveFlag(@gFlags[FLAG_RED]);
2799 // Ôëàã Ñèíåé êîìàíäû:
2800 SaveFlag(@gFlags[FLAG_BLUE]);
2801 end;
2802 ///// /////
2804 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2805 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2806 begin
2807 // Î÷êè Êðàñíîé êîìàíäû:
2808 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2809 // Î÷êè Ñèíåé êîìàíäû:
2810 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2811 end;
2812 ///// /////
2813 end;
2815 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2816 var
2817 dw: DWORD;
2818 b: Byte;
2819 str: String;
2820 boo: Boolean;
2822 procedure LoadPanelArray(var panels: TPanelArray);
2823 var
2824 PAMem: TBinMemoryReader;
2825 i, id: Integer;
2826 begin
2827 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2828 PAMem := TBinMemoryReader.Create();
2829 PAMem.LoadFromMemory(Mem);
2831 for i := 0 to Length(panels)-1 do
2832 begin
2833 if panels[i].SaveIt then
2834 begin
2835 // ID ïàíåëè:
2836 PAMem.ReadInt(id);
2837 if id <> i then
2838 begin
2839 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2840 end;
2841 // Çàãðóæàåì ïàíåëü:
2842 panels[i].LoadState(PAMem);
2843 if (panels[i].arrIdx <> i) then raise Exception.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
2844 if (panels[i].proxyId >= 0) then mapGrid.proxyEnabled[panels[i].proxyId] := panels[i].Enabled;
2845 end;
2846 end;
2848 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2849 PAMem.Free();
2850 end;
2852 procedure LoadFlag(flag: PFlag);
2853 begin
2854 // Ñèãíàòóðà ôëàãà:
2855 Mem.ReadDWORD(dw);
2856 if dw <> FLAG_SIGNATURE then // 'FLAG'
2857 begin
2858 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2859 end;
2860 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2861 Mem.ReadByte(flag^.RespawnType);
2862 // Ñîñòîÿíèå ôëàãà:
2863 Mem.ReadByte(flag^.State);
2864 // Íàïðàâëåíèå ôëàãà:
2865 Mem.ReadByte(b);
2866 if b = 1 then
2867 flag^.Direction := D_LEFT
2868 else // b = 2
2869 flag^.Direction := D_RIGHT;
2870 // Îáúåêò ôëàãà:
2871 Obj_LoadState(@flag^.Obj, Mem);
2872 end;
2874 begin
2875 if Mem = nil then
2876 Exit;
2878 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2879 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2880 LoadPanelArray(gWalls);
2881 // Çàãðóæàåì ïàíåëè ôîíà:
2882 LoadPanelArray(gRenderBackgrounds);
2883 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2884 LoadPanelArray(gRenderForegrounds);
2885 // Çàãðóæàåì ïàíåëè âîäû:
2886 LoadPanelArray(gWater);
2887 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2888 LoadPanelArray(gAcid1);
2889 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2890 LoadPanelArray(gAcid2);
2891 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2892 LoadPanelArray(gSteps);
2893 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2894 LoadPanelArray(gLifts);
2895 ///// /////
2897 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2898 g_GFX_Init();
2899 //mapCreateGrid();
2901 ///// Çàãðóæàåì ìóçûêó: /////
2902 // Ñèãíàòóðà ìóçûêè:
2903 Mem.ReadDWORD(dw);
2904 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2905 begin
2906 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2907 end;
2908 // Íàçâàíèå ìóçûêè:
2909 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2910 Mem.ReadString(str);
2911 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2912 Mem.ReadDWORD(dw);
2913 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2914 Mem.ReadBoolean(boo);
2915 // Çàïóñêàåì ýòó ìóçûêó:
2916 gMusic.SetByName(str);
2917 gMusic.SpecPause := boo;
2918 gMusic.Play();
2919 gMusic.Pause(True);
2920 gMusic.SetPosition(dw);
2921 ///// /////
2923 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2924 Mem.ReadInt(gTotalMonsters);
2925 ///// /////
2927 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2928 if gGameSettings.GameMode = GM_CTF then
2929 begin
2930 // Ôëàã Êðàñíîé êîìàíäû:
2931 LoadFlag(@gFlags[FLAG_RED]);
2932 // Ôëàã Ñèíåé êîìàíäû:
2933 LoadFlag(@gFlags[FLAG_BLUE]);
2934 end;
2935 ///// /////
2937 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2938 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2939 begin
2940 // Î÷êè Êðàñíîé êîìàíäû:
2941 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2942 // Î÷êè Ñèíåé êîìàíäû:
2943 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2944 end;
2945 ///// /////
2946 end;
2948 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2949 var
2950 Arr: TPanelArray;
2951 begin
2952 Result := nil;
2953 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2954 Arr := PanelByID[PanelID].PWhere^;
2955 PanelArrayID := PanelByID[PanelID].PArrID;
2956 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2957 end;
2960 // trace liquid, stepping by `dx` and `dy`
2961 // return last seen liquid coords, and `false` if we're started outside of the liquid
2962 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2963 const
2964 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2965 begin
2966 topx := x;
2967 topy := y;
2968 // started outside of the liquid?
2969 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2970 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2971 result := true;
2972 while true do
2973 begin
2974 Inc(x, dx);
2975 Inc(y, dy);
2976 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2977 topx := x;
2978 topy := y;
2979 end;
2980 end;
2983 end.