DEADSOFTWARE

03c029f6404dad7c884a4647d9752216b9ab6482
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
17 unit g_map;
19 interface
21 uses
22 SysUtils, Classes, mempool,
23 g_base, g_basic, MAPDEF,
24 g_phys, utils, g_panel, g_grid, md5, binheap, xprofiler, xparser, xdynrec;
26 type
27 TMapInfo = record
28 Map: String;
29 Name: String;
30 Description: String;
31 Author: String;
32 MusicName: String;
33 SkyName: String;
34 SkyFullName: String; // used by render
35 Height: Word;
36 Width: Word;
37 end;
39 PRespawnPoint = ^TRespawnPoint;
40 TRespawnPoint = record
41 X, Y: Integer;
42 Direction: TDirection;
43 PointType: Byte;
44 end;
46 PFlagPoint = ^TFlagPoint;
47 TFlagPoint = TRespawnPoint;
49 PFlag = ^TFlag;
50 TFlag = record
51 Obj: TObj;
52 RespawnType: Byte;
53 State: Byte;
54 Count: Integer;
55 CaptureTime: LongWord;
56 Direction: TDirection;
57 NeedSend: Boolean;
58 end;
60 function g_Map_Load(Res: String): Boolean;
61 function g_Map_GetMapInfo(Res: String): TMapInfo;
62 function g_Map_GetMapsList(WADName: String): SSArray;
63 function g_Map_Exist(Res: String): Boolean;
64 procedure g_Map_Free(freeTextures: Boolean=true);
65 procedure g_Map_Update();
67 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
69 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
70 PanelType: Word; b1x3: Boolean=false): Boolean;
71 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
73 procedure g_Map_EnableWallGUID (pguid: Integer);
74 procedure g_Map_DisableWallGUID (pguid: Integer);
75 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
77 // HACK!!!
78 procedure g_Map_EnableWall_XXX (ID: DWORD);
79 procedure g_Map_DisableWall_XXX (ID: DWORD);
80 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
82 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
84 procedure g_Map_ReAdd_DieTriggers();
85 function g_Map_IsSpecialTexture(Texture: String): Boolean;
87 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
88 function g_Map_GetPointCount(PointType: Byte): Word;
89 function g_Map_GetRandomPointType(): Byte;
91 function g_Map_HaveFlagPoints(): Boolean;
93 procedure g_Map_ResetFlag(Flag: Byte);
95 procedure g_Map_SaveState (st: TStream);
96 procedure g_Map_LoadState (st: TStream);
98 // returns panel or nil
99 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
100 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
102 // returns panel or nil
103 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
104 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
106 type
107 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
109 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
110 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
112 // trace liquid, stepping by `dx` and `dy`
113 // return last seen liquid coords, and `false` if we're started outside of the liquid
114 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
117 // return `true` from `cb` to stop
118 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
120 procedure g_Map_NetSendInterestingPanels (); // yay!
123 procedure g_Map_ProfilersBegin ();
124 procedure g_Map_ProfilersEnd ();
127 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
130 function g_Map_MinX (): Integer; inline;
131 function g_Map_MinY (): Integer; inline;
132 function g_Map_MaxX (): Integer; inline;
133 function g_Map_MaxY (): Integer; inline;
135 const
136 NNF_NO_NAME = 0;
137 NNF_NAME_BEFORE = 1;
138 NNF_NAME_EQUALS = 2;
139 NNF_NAME_AFTER = 3;
141 function g_Texture_NumNameFindStart(name: String): Boolean;
142 function g_Texture_NumNameFindNext(var newName: String): Byte;
144 const
145 RESPAWNPOINT_PLAYER1 = 1;
146 RESPAWNPOINT_PLAYER2 = 2;
147 RESPAWNPOINT_DM = 3;
148 RESPAWNPOINT_RED = 4;
149 RESPAWNPOINT_BLUE = 5;
151 FLAG_NONE = 0;
152 FLAG_RED = 1;
153 FLAG_BLUE = 2;
154 FLAG_DOM = 3;
155 FLAG_LAST = FLAG_DOM;
157 FLAG_STATE_NONE = 0;
158 FLAG_STATE_NORMAL = 1;
159 FLAG_STATE_DROPPED = 2;
160 FLAG_STATE_CAPTURED = 3;
161 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
162 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
164 FLAG_TIME = 720; // 20 seconds
166 SKY_STRETCH: Single = 1.5;
168 const
169 GridTagInvalid = 0;
171 (* draw order:
172 PANEL_BACK
173 PANEL_STEP
174 PANEL_WALL
175 PANEL_CLOSEDOOR
176 PANEL_ACID1
177 PANEL_ACID2
178 PANEL_WATER
179 PANEL_FORE
180 *)
181 // sorted by draw priority
182 GridTagBack = 1 shl 0; // gRenderBackgrounds
183 GridTagStep = 1 shl 1; // gSteps
184 GridTagWall = 1 shl 2; // gWalls
185 GridTagDoor = 1 shl 3; // gWalls
186 GridTagAcid1 = 1 shl 4; // gAcid1
187 GridTagAcid2 = 1 shl 5; // gAcid2
188 GridTagWater = 1 shl 6; // gWater
189 GridTagFore = 1 shl 7; // gRenderForegrounds
190 // the following are invisible
191 GridTagLift = 1 shl 8; // gLifts
192 GridTagBlockMon = 1 shl 9; // gBlockMon
194 GridTagSolid = (GridTagWall or GridTagDoor);
195 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
196 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
198 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
200 var
201 gWalls: TPanelArray;
202 gRenderBackgrounds: TPanelArray;
203 gRenderForegrounds: TPanelArray;
204 gWater, gAcid1, gAcid2: TPanelArray;
205 gSteps: TPanelArray;
206 gLifts: TPanelArray;
207 gBlockMon: TPanelArray;
208 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
209 //gDOMFlags: array of TFlag;
210 gMapInfo: TMapInfo;
211 gDoorMap: array of array of DWORD;
212 gLiftMap: array of array of DWORD;
213 gWADHash: TMD5Digest;
214 gExternalResources: array of TDiskFileInfo = nil;
215 gMovingWallIds: array of Integer = nil;
217 gdbg_map_use_accel_render: Boolean = true;
218 gdbg_map_use_accel_coldet: Boolean = true;
219 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
221 gCurrentMap: TDynRecord = nil;
222 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
223 gTestMap: String = '';
226 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
229 type
230 TPanelGrid = specialize TBodyGridBase<TPanel>;
232 var
233 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
235 var (* private state *)
236 Textures: TLevelTextureArray = nil;
238 implementation
240 uses
241 {$IFDEF ENABLE_GFX}
242 g_gfx,
243 {$ENDIF}
244 e_input, e_log, e_res, g_items, g_console,
245 g_weapons, g_game, g_sound, e_sound, CONFIG,
246 g_options, g_triggers, g_player,
247 Math, g_monsters, g_saveload, g_language, g_netmsg,
248 sfs, xstreams, hashtable, wadreader,
249 g_res_downloader
252 const
253 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
254 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
255 FLAG_SIGNATURE = $47414C46; // 'FLAG'
258 // ////////////////////////////////////////////////////////////////////////// //
259 procedure mapWarningCB (const msg: AnsiString; line, col: Integer);
260 begin
261 if (line > 0) then
262 begin
263 e_LogWritefln('parse error at (%s,%s): %s', [line, col, msg], TMsgType.Warning);
264 end
265 else
266 begin
267 e_LogWritefln('parse error: %s', [msg], TMsgType.Warning);
268 end;
269 end;
272 // ////////////////////////////////////////////////////////////////////////// //
273 var
274 panByGUID: array of TPanel = nil;
277 // ////////////////////////////////////////////////////////////////////////// //
278 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
279 begin
280 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
281 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
282 end;
285 // return `true` from `cb` to stop
286 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
287 var
288 pan: TPanel;
289 begin
290 result := nil;
291 if not assigned(cb) then exit;
292 for pan in panByGUID do
293 begin
294 if cb(pan) then begin result := pan; exit; end;
295 end;
296 end;
299 procedure g_Map_NetSendInterestingPanels ();
300 var
301 pan: TPanel;
302 begin
303 if g_Game_IsServer and g_Game_IsNet then
304 begin
305 for pan in panByGUID do
306 begin
307 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
308 end;
309 end;
310 end;
313 // ////////////////////////////////////////////////////////////////////////// //
314 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
315 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
316 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
317 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
320 // ////////////////////////////////////////////////////////////////////////// //
321 var
322 dfmapdef: TDynMapDef = nil;
325 procedure loadMapDefinition ();
326 var
327 pr: TTextParser = nil;
328 st: TStream = nil;
329 WAD: TWADFile = nil;
330 begin
331 if (dfmapdef <> nil) then exit;
333 try
334 e_LogWritefln('parsing "mapdef.txt"...', []);
335 st := e_OpenResourceRO(DataDirs, 'mapdef.txt');
336 e_LogWritefln('found local "mapdef.txt"', []);
337 except
338 st := nil;
339 end;
341 if (st = nil) then
342 begin
343 WAD := TWADFile.Create();
344 if not WAD.ReadFile(GameWAD) then
345 begin
346 //raise Exception.Create('cannot load "game.wad"');
347 st := nil;
348 end
349 else
350 begin
351 st := WAD.openFileStream('mapdef.txt');
352 end;
353 end;
355 try
356 if (st = nil) then
357 begin
358 //raise Exception.Create('cannot open "mapdef.txt"');
359 e_LogWriteln('using default "mapdef.txt"...');
360 pr := TStrTextParser.Create(defaultMapDef);
361 end
362 else
363 begin
364 pr := TFileTextParser.Create(st);
365 end;
366 except on e: Exception do
367 begin
368 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
369 raise;
370 end;
371 end;
373 try
374 dfmapdef := TDynMapDef.Create(pr);
375 except
376 on e: TDynParseException do
377 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
378 on e: Exception do
379 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
380 end;
382 st.Free();
383 WAD.Free();
384 end;
387 // ////////////////////////////////////////////////////////////////////////// //
388 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
389 var
390 wst: TSFSMemoryChunkStream = nil;
391 begin
392 result := nil;
393 if (dataLen < 4) then exit;
395 if (dfmapdef = nil) then writeln('need to load mapdef');
396 loadMapDefinition();
397 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
399 wst := TSFSMemoryChunkStream.Create(data, dataLen);
400 try
401 result := dfmapdef.parseMap(wst);
402 except
403 on e: TDynParseException do
404 begin
405 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
406 wst.Free();
407 result := nil;
408 exit;
409 end;
410 on e: Exception do
411 begin
412 e_LogWritefln('ERROR: %s', [e.message]);
413 wst.Free();
414 result := nil;
415 exit;
416 end;
417 end;
419 //e_LogWriteln('map parsed.');
420 end;
423 // ////////////////////////////////////////////////////////////////////////// //
424 var
425 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
426 NNF_PureExt: String; // extension postfix
427 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
428 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
431 function g_Texture_NumNameFindStart(name: String): Boolean;
432 var
433 i, j: Integer;
435 begin
436 Result := False;
437 NNF_PureName := '';
438 NNF_PureExt := '';
439 NNF_FirstNum := -1;
440 NNF_CurrentNum := -1;
442 for i := Length(name) downto 1 do
443 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
444 begin
445 if i = Length(name) then
446 begin // Íåò öèôð â êîíöå ñòðîêè
447 Exit;
448 end
449 else
450 begin
451 j := i + 1;
452 while (j <= Length(name)) and (name[j] <> '.') do inc(j);
453 NNF_PureName := Copy(name, 1, i);
454 NNF_PureExt := Copy(name, j);
455 name := Copy(name, i + 1, j - i - 1);
456 Break;
457 end;
458 end;
460 // Íå ïåðåâåñòè â ÷èñëî:
461 if not TryStrToInt(name, NNF_FirstNum) then
462 Exit;
464 NNF_CurrentNum := 0;
466 Result := True;
467 end;
470 function g_Texture_NumNameFindNext(var newName: String): Byte;
471 begin
472 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
473 begin
474 newName := '';
475 Result := NNF_NO_NAME;
476 Exit;
477 end;
479 newName := NNF_PureName + IntToStr(NNF_CurrentNum) + NNF_PureExt;
481 if NNF_CurrentNum < NNF_FirstNum then
482 Result := NNF_NAME_BEFORE
483 else
484 if NNF_CurrentNum > NNF_FirstNum then
485 Result := NNF_NAME_AFTER
486 else
487 Result := NNF_NAME_EQUALS;
489 Inc(NNF_CurrentNum);
490 end;
493 // ////////////////////////////////////////////////////////////////////////// //
494 function panelTypeToTag (panelType: Word): Integer;
495 begin
496 case panelType of
497 PANEL_WALL: result := GridTagWall; // gWalls
498 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
499 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
500 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
501 PANEL_WATER: result := GridTagWater; // gWater
502 PANEL_ACID1: result := GridTagAcid1; // gAcid1
503 PANEL_ACID2: result := GridTagAcid2; // gAcid2
504 PANEL_STEP: result := GridTagStep; // gSteps
505 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
506 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
507 else result := GridTagInvalid;
508 end;
509 end;
511 var
512 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
513 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
514 RespawnPoints: array of TRespawnPoint;
515 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
516 //DOMFlagPoints: Array of TFlagPoint;
519 procedure g_Map_ProfilersBegin ();
520 begin
521 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
522 if (profMapCollision <> nil) then profMapCollision.mainBegin(g_profile_collision);
523 // create sections
524 if g_profile_collision and (profMapCollision <> nil) then
525 begin
526 profMapCollision.sectionBegin('*solids');
527 profMapCollision.sectionEnd();
528 profMapCollision.sectionBegin('liquids');
529 profMapCollision.sectionEnd();
530 end;
531 end;
533 procedure g_Map_ProfilersEnd ();
534 begin
535 if (profMapCollision <> nil) then profMapCollision.mainEnd();
536 end;
539 // wall index in `gWalls` or -1
540 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
541 var
542 ex, ey: Integer;
543 begin
544 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, (GridTagWall or GridTagDoor));
545 if (result <> nil) then
546 begin
547 if (hitx <> nil) then hitx^ := ex;
548 if (hity <> nil) then hity^ := ey;
549 end
550 else
551 begin
552 if (hitx <> nil) then hitx^ := x1;
553 if (hity <> nil) then hity^ := y1;
554 end;
555 end;
557 // returns panel or nil
558 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
559 var
560 ex, ey: Integer;
561 begin
562 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, tag);
563 if (result <> nil) then
564 begin
565 if (hitx <> nil) then hitx^ := ex;
566 if (hity <> nil) then hity^ := ey;
567 end
568 else
569 begin
570 if (hitx <> nil) then hitx^ := x1;
571 if (hity <> nil) then hity^ := y1;
572 end;
573 end;
576 function xxPanAtPointChecker (pan: TPanel; panelType: Word): Boolean; inline;
577 begin
578 if ((pan.tag and GridTagLift) <> 0) then
579 begin
580 // stop if the lift of the right type
581 result :=
582 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
583 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
584 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
585 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT)));
586 exit;
587 end;
588 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
589 end;
591 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
592 var
593 tagmask: Integer = 0;
594 mwit: PPanel;
595 it: TPanelGrid.Iter;
596 begin
597 result := false;
599 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
600 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
601 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
602 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
603 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
604 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
605 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
607 if (tagmask = 0) then exit;// just in case
609 if ((tagmask and GridTagLift) <> 0) then
610 begin
611 // slow
612 it := mapGrid.forEachAtPoint(x, y, tagmask);
613 for mwit in it do if (xxPanAtPointChecker(mwit^, PanelType)) then begin result := true; break; end;
614 end
615 else
616 begin
617 // fast
618 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true);
619 result := (it.length <> 0); // firsthit
620 end;
621 it.release();
622 end;
625 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
626 var
627 it: TPanelGrid.Iter;
628 begin
629 result := nil;
630 if (tagmask = 0) then exit;
631 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true); // firsthit
632 if (it.length <> 0) then result := it.first^;
633 it.release();
634 end;
637 function g_Map_IsSpecialTexture(Texture: String): Boolean;
638 begin
639 Result := (Texture = TEXTURE_NAME_WATER) or
640 (Texture = TEXTURE_NAME_ACID1) or
641 (Texture = TEXTURE_NAME_ACID2);
642 end;
644 procedure CreateDoorMap();
645 var
646 PanelArray: Array of record
647 X, Y: Integer;
648 Width, Height: Word;
649 Active: Boolean;
650 PanelID: DWORD;
651 end;
652 a, b, c, m, i, len: Integer;
653 ok: Boolean;
654 begin
655 if gWalls = nil then
656 Exit;
658 i := 0;
659 len := 128;
660 SetLength(PanelArray, len);
662 for a := 0 to High(gWalls) do
663 if gWalls[a].Door then
664 begin
665 PanelArray[i].X := gWalls[a].X;
666 PanelArray[i].Y := gWalls[a].Y;
667 PanelArray[i].Width := gWalls[a].Width;
668 PanelArray[i].Height := gWalls[a].Height;
669 PanelArray[i].Active := True;
670 PanelArray[i].PanelID := a;
672 i := i + 1;
673 if i = len then
674 begin
675 len := len + 128;
676 SetLength(PanelArray, len);
677 end;
678 end;
680 // Íåò äâåðåé:
681 if i = 0 then
682 begin
683 PanelArray := nil;
684 Exit;
685 end;
687 SetLength(gDoorMap, 0);
689 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
691 for a := 0 to i-1 do
692 if PanelArray[a].Active then
693 begin
694 PanelArray[a].Active := False;
695 m := Length(gDoorMap);
696 SetLength(gDoorMap, m+1);
697 SetLength(gDoorMap[m], 1);
698 gDoorMap[m, 0] := PanelArray[a].PanelID;
699 ok := True;
701 while ok do
702 begin
703 ok := False;
705 for b := 0 to i-1 do
706 if PanelArray[b].Active then
707 for c := 0 to High(gDoorMap[m]) do
708 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
709 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
710 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
711 PanelArray[b].Width, PanelArray[b].Height,
712 gWalls[gDoorMap[m, c]].X,
713 gWalls[gDoorMap[m, c]].Y,
714 gWalls[gDoorMap[m, c]].Width,
715 gWalls[gDoorMap[m, c]].Height) then
716 begin
717 PanelArray[b].Active := False;
718 SetLength(gDoorMap[m],
719 Length(gDoorMap[m])+1);
720 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
721 ok := True;
722 Break;
723 end;
724 end;
726 g_Game_StepLoading();
727 end;
729 PanelArray := nil;
730 end;
732 procedure CreateLiftMap();
733 var
734 PanelArray: Array of record
735 X, Y: Integer;
736 Width, Height: Word;
737 Active: Boolean;
738 end;
739 a, b, c, len, i, j: Integer;
740 ok: Boolean;
741 begin
742 if gLifts = nil then
743 Exit;
745 len := Length(gLifts);
746 SetLength(PanelArray, len);
748 for a := 0 to len-1 do
749 begin
750 PanelArray[a].X := gLifts[a].X;
751 PanelArray[a].Y := gLifts[a].Y;
752 PanelArray[a].Width := gLifts[a].Width;
753 PanelArray[a].Height := gLifts[a].Height;
754 PanelArray[a].Active := True;
755 end;
757 SetLength(gLiftMap, len);
758 i := 0;
760 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
762 for a := 0 to len-1 do
763 if PanelArray[a].Active then
764 begin
765 PanelArray[a].Active := False;
766 SetLength(gLiftMap[i], 32);
767 j := 0;
768 gLiftMap[i, j] := a;
769 ok := True;
771 while ok do
772 begin
773 ok := False;
774 for b := 0 to len-1 do
775 if PanelArray[b].Active then
776 for c := 0 to j do
777 if g_CollideAround(PanelArray[b].X,
778 PanelArray[b].Y,
779 PanelArray[b].Width,
780 PanelArray[b].Height,
781 PanelArray[gLiftMap[i, c]].X,
782 PanelArray[gLiftMap[i, c]].Y,
783 PanelArray[gLiftMap[i, c]].Width,
784 PanelArray[gLiftMap[i, c]].Height) then
785 begin
786 PanelArray[b].Active := False;
787 j := j+1;
788 if j > High(gLiftMap[i]) then
789 SetLength(gLiftMap[i],
790 Length(gLiftMap[i])+32);
792 gLiftMap[i, j] := b;
793 ok := True;
795 Break;
796 end;
797 end;
799 SetLength(gLiftMap[i], j+1);
800 i := i+1;
802 g_Game_StepLoading();
803 end;
805 SetLength(gLiftMap, i);
807 PanelArray := nil;
808 end;
810 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
811 var
812 len: Integer;
813 panels: ^TPanelArray;
814 pan: TPanel;
815 pguid: Integer;
816 begin
817 Result := -1;
819 case PanelRec.PanelType of
820 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
821 PANEL_BACK: panels := @gRenderBackgrounds;
822 PANEL_FORE: panels := @gRenderForegrounds;
823 PANEL_WATER: panels := @gWater;
824 PANEL_ACID1: panels := @gAcid1;
825 PANEL_ACID2: panels := @gAcid2;
826 PANEL_STEP: panels := @gSteps;
827 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
828 PANEL_BLOCKMON: panels := @gBlockMon;
829 else exit;
830 end;
832 len := Length(panels^);
833 SetLength(panels^, len+1);
835 pguid := Length(panByGUID);
836 SetLength(panByGUID, pguid+1); //FIXME!
837 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
838 assert(pguid >= 0);
839 assert(pguid < Length(panByGUID));
840 panByGUID[pguid] := pan;
841 panels^[len] := pan;
842 pan.arrIdx := len;
843 pan.proxyId := -1;
844 pan.tag := panelTypeToTag(PanelRec.PanelType);
846 PanelRec.user['panel_guid'] := pguid;
848 //result := len;
849 result := pguid;
850 end;
853 function CreateNullTexture(RecName: String): Integer;
854 begin
855 RecName := toLowerCase1251(RecName);
856 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
857 if TextNameHash.get(RecName, result) then exit; // i found her!
859 SetLength(Textures, Length(Textures) + 1);
860 Textures[High(Textures)].TextureName := RecName;
861 Textures[High(Textures)].FullName := '';
862 Result := High(Textures);
863 TextNameHash.put(RecName, result);
864 end;
867 function extractWadName (resourceName: string): string;
868 var
869 posN: Integer;
870 begin
871 posN := Pos(':', resourceName);
872 if posN > 0 then
873 Result:= Copy(resourceName, 0, posN-1)
874 else
875 Result := '';
876 end;
879 procedure addResToExternalResList (res: AnsiString);
880 var
881 uname: AnsiString;
882 f: Integer;
883 fi: TDiskFileInfo;
884 begin
885 if g_Game_IsClient or not g_Game_IsNet then exit;
886 if (length(res) = 0) then exit; // map wad
887 //res := extractWadName(res);
888 //if (length(res) = 0) then exit; // map wad
889 uname := toLowerCase1251(res);
890 // do not add duplicates
891 for f := 0 to High(gExternalResources) do
892 begin
893 if (gExternalResources[f].userName = uname) then exit;
894 end;
895 //writeln('***(000) addResToExternalResList: res=[', res, ']');
896 // add new resource
897 fi.userName := uname;
898 if not findFileCI(res) then exit;
899 //writeln('***(001) addResToExternalResList: res=[', res, ']');
900 fi.diskName := res;
901 if (not GetDiskFileInfo(res, fi)) then
902 begin
903 fi.tag := -1;
904 end
905 else
906 begin
907 //writeln('***(002) addResToExternalResList: res=[', res, ']');
908 fi.tag := 0; // non-zero means "cannot caclucate hash"
909 try
910 fi.hash := MD5File(fi.diskName);
911 except
912 fi.tag := -1;
913 end;
914 end;
915 //e_LogWritefln('addext: res=[%s]; uname=[%s]; diskName=[%s]', [res, fi.userName, fi.diskName]);
916 SetLength(gExternalResources, length(gExternalResources)+1);
917 gExternalResources[High(gExternalResources)] := fi;
918 end;
921 procedure compactExtResList ();
922 var
923 src, dest: Integer;
924 begin
925 src := 0;
926 dest := 0;
927 for src := 0 to High(gExternalResources) do
928 begin
929 if (gExternalResources[src].tag = 0) then
930 begin
931 // copy it
932 if (dest <> src) then gExternalResources[dest] := gExternalResources[src];
933 Inc(dest);
934 end;
935 end;
936 if (dest <> length(gExternalResources)) then SetLength(gExternalResources, dest);
937 end;
940 function GetReplacementWad (WadName: AnsiString): AnsiString;
941 begin
942 result := '';
943 if WadName <> '' then
944 begin
945 result := WadName;
946 if g_Game_IsClient then result := g_Res_FindReplacementWad(WadName);
947 if (result = WadName) then result := e_FindWad(WadDirs, result)
948 end;
949 end;
952 procedure generateExternalResourcesList (map: TDynRecord);
953 begin
954 SetLength(gExternalResources, 0);
955 addResToExternalResList(GetReplacementWad(g_ExtractWadName(map.MusicName)));
956 addResToExternalResList(GetReplacementWad(g_ExtractWadName(map.SkyName)));
957 end;
959 function CreateTexture (RecName: AnsiString; Map: String; log: Boolean): Integer;
960 var
961 HName: AnsiString;
962 WAD, WADz: TWADFile;
963 WADName, ResName: String;
964 ResData, ReszData: Pointer;
965 ResLen, ReszLen: Integer;
966 cfg: TConfig;
967 id: Integer;
968 begin
969 Result := -1;
970 HName := toLowerCase1251(RecName);
971 if (TextNameHash = nil) then
972 TextNameHash := THashStrInt.Create();
973 if TextNameHash.get(HName, Result) then
974 begin
975 // e_LogWritefln('CreateTexture: found loaded %s', [Result]);
976 end
977 else
978 begin
979 Result := -1;
980 if (BadTextNameHash = nil) or not BadTextNameHash.has(HName) then
981 begin
982 case RecName of
983 TEXTURE_NAME_WATER, TEXTURE_NAME_ACID1, TEXTURE_NAME_ACID2:
984 begin
985 SetLength(Textures, Length(Textures) + 1);
986 Textures[High(Textures)].FullName := RecName;
987 Textures[High(Textures)].TextureName := RecName;
988 Result := High(Textures);
989 TextNameHash.put(RecName, result);
990 end
991 else
992 WADName := GetReplacementWad(g_ExtractWadName(RecName));
993 if (WADName <> '') then
994 addResToExternalResList(WADName);
995 if WADName = '' then
996 WADName := Map;
997 ResName := g_ExtractFilePathName(RecName);
998 WAD := TWADFile.Create();
999 if WAD.ReadFile(WadName) then
1000 begin
1001 if WAD.GetResource(ResName, ResData, ResLen, log) then
1002 begin
1003 if IsWadData(ResData, ResLen) then
1004 begin
1005 WADz := TWADFile.Create();
1006 if WADz.ReadMemory(ResData, ResLen) then
1007 begin
1008 if WADz.GetResource('TEXT/ANIM', ReszData, ReszLen) then
1009 begin
1010 cfg := TConfig.CreateMem(ReszData, ReszLen);
1011 if cfg <> nil then
1012 begin
1013 SetLength(Textures, Length(Textures) + 1);
1014 Textures[High(Textures)].TextureName := RecName;
1015 Textures[High(Textures)].FullName := WadName + ':' + ResName;
1016 Textures[High(Textures)].FramesCount := cfg.ReadInt('', 'framecount', 0);
1017 Textures[High(Textures)].Speed := cfg.ReadInt('', 'waitcount', 0);
1018 Result := High(Textures);
1019 TextNameHash.put(HName, result);
1020 cfg.Free;
1021 end;
1022 FreeMem(ReszData);
1023 end
1024 end;
1025 WADz.Free;
1026 end
1027 else
1028 begin
1029 SetLength(Textures, Length(Textures) + 1);
1030 Textures[High(Textures)].FullName := WADName + ':' + ResName;
1031 Textures[High(Textures)].TextureName := RecName;
1032 Result := High(Textures);
1033 TextNameHash.put(HName, result);
1034 end;
1035 FreeMem(ResData);
1036 end
1037 end;
1038 WAD.Free;
1039 end
1040 end;
1041 end;
1042 if Result < 0 then
1043 begin
1044 if (BadTextNameHash = nil) then
1045 BadTextNameHash := THashStrInt.Create();
1046 if log and (not BadTextNameHash.get(HName, id)) then
1047 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1048 BadTextNameHash.put(HName, -1);
1049 end
1050 end;
1052 procedure CreateItem(Item: TDynRecord);
1053 begin
1054 if g_Game_IsClient then Exit;
1056 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1057 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1058 Exit;
1060 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1061 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1062 end;
1064 procedure CreateArea(Area: TDynRecord);
1065 var
1066 a: Integer;
1067 begin
1068 case Area.AreaType of
1069 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1070 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1071 begin
1072 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1073 with RespawnPoints[High(RespawnPoints)] do
1074 begin
1075 X := Area.X;
1076 Y := Area.Y;
1077 Direction := TDirection(Area.Direction);
1079 case Area.AreaType of
1080 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1081 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1082 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1083 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1084 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1085 end;
1086 end;
1087 end;
1089 AREA_REDFLAG, AREA_BLUEFLAG:
1090 begin
1091 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1093 if FlagPoints[a] <> nil then Exit;
1095 New(FlagPoints[a]);
1097 with FlagPoints[a]^ do
1098 begin
1099 X := Area.X-FLAGRECT.X;
1100 Y := Area.Y-FLAGRECT.Y;
1101 Direction := TDirection(Area.Direction);
1102 end;
1104 with gFlags[a] do
1105 begin
1106 Obj.Rect := FLAGRECT;
1107 g_Map_ResetFlag(a);
1108 end;
1109 end;
1111 AREA_DOMFLAG:
1112 begin
1113 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1114 with DOMFlagPoints[High(DOMFlagPoints)] do
1115 begin
1116 X := Area.X;
1117 Y := Area.Y;
1118 Direction := TDirection(Area.Direction);
1119 end;
1121 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1122 end;
1123 end;
1124 end;
1126 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1127 var
1128 _trigger: TTrigger;
1129 tp: TPanel;
1130 begin
1131 result := -1;
1132 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1134 with _trigger do
1135 begin
1136 mapId := Trigger.id;
1137 mapIndex := amapIdx;
1138 X := Trigger.X;
1139 Y := Trigger.Y;
1140 Width := Trigger.Width;
1141 Height := Trigger.Height;
1142 Enabled := Trigger.Enabled;
1143 TexturePanelGUID := atpanid;
1144 TriggerType := Trigger.TriggerType;
1145 ActivateType := Trigger.ActivateType;
1146 Keys := Trigger.Keys;
1147 trigPanelGUID := atrigpanid;
1148 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1149 if TexturePanelGUID <> -1 then
1150 begin
1151 tp := g_Map_PanelByGUID(TexturePanelGUID);
1152 if (tp <> nil) then tp.hasTexTrigger := True;
1153 end;
1154 end;
1156 result := Integer(g_Triggers_Create(_trigger, Trigger));
1157 end;
1159 procedure CreateMonster(monster: TDynRecord);
1160 var
1161 a: Integer;
1162 mon: TMonster;
1163 begin
1164 if g_Game_IsClient then Exit;
1166 if (gGameSettings.GameType = GT_SINGLE)
1167 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1168 begin
1169 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1171 if gTriggers <> nil then
1172 begin
1173 for a := 0 to High(gTriggers) do
1174 begin
1175 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1176 begin
1177 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1178 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1179 end;
1180 end;
1181 end;
1183 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1184 end;
1185 end;
1187 procedure g_Map_ReAdd_DieTriggers();
1189 function monsDieTrig (mon: TMonster): Boolean;
1190 var
1191 a: Integer;
1192 //tw: TStrTextWriter;
1193 begin
1194 result := false; // don't stop
1195 mon.ClearTriggers();
1196 for a := 0 to High(gTriggers) do
1197 begin
1198 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1199 begin
1200 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1202 tw := TStrTextWriter.Create();
1203 try
1204 gTriggers[a].trigData.writeTo(tw);
1205 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1206 finally
1207 tw.Free();
1208 end;
1210 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1211 end;
1212 end;
1213 end;
1215 begin
1216 if g_Game_IsClient then Exit;
1218 g_Mons_ForEach(monsDieTrig);
1219 end;
1221 procedure mapCreateGrid ();
1222 var
1223 mapX0: Integer = $3fffffff;
1224 mapY0: Integer = $3fffffff;
1225 mapX1: Integer = -$3fffffff;
1226 mapY1: Integer = -$3fffffff;
1228 procedure calcBoundingBox (constref panels: TPanelArray);
1229 var
1230 idx: Integer;
1231 pan: TPanel;
1232 begin
1233 for idx := 0 to High(panels) do
1234 begin
1235 pan := panels[idx];
1236 if not pan.visvalid then continue;
1237 if (pan.Width < 1) or (pan.Height < 1) then continue;
1238 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1239 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1240 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1241 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1242 end;
1243 end;
1245 procedure addPanelsToGrid (constref panels: TPanelArray);
1246 var
1247 idx: Integer;
1248 pan: TPanel;
1249 newtag: Integer;
1250 begin
1251 //tag := panelTypeToTag(tag);
1252 for idx := 0 to High(panels) do
1253 begin
1254 pan := panels[idx];
1255 if not pan.visvalid then continue;
1256 if (pan.proxyId <> -1) then
1257 begin
1258 {$IF DEFINED(D2F_DEBUG)}
1259 e_WriteLog(Format('DUPLICATE wall #%d(%d) enabled (%d); type:%08x', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.PanelType]), TMsgType.Notify);
1260 {$ENDIF}
1261 continue;
1262 end;
1263 case pan.PanelType of
1264 PANEL_WALL: newtag := GridTagWall;
1265 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1266 PANEL_BACK: newtag := GridTagBack;
1267 PANEL_FORE: newtag := GridTagFore;
1268 PANEL_WATER: newtag := GridTagWater;
1269 PANEL_ACID1: newtag := GridTagAcid1;
1270 PANEL_ACID2: newtag := GridTagAcid2;
1271 PANEL_STEP: newtag := GridTagStep;
1272 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1273 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1274 else continue; // oops
1275 end;
1276 pan.tag := newtag;
1278 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1279 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1280 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1281 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1283 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1284 begin
1285 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1286 end;
1288 {$ENDIF}
1289 end;
1290 end;
1292 begin
1293 mapGrid.Free();
1294 mapGrid := nil;
1296 calcBoundingBox(gWalls);
1297 calcBoundingBox(gRenderBackgrounds);
1298 calcBoundingBox(gRenderForegrounds);
1299 calcBoundingBox(gWater);
1300 calcBoundingBox(gAcid1);
1301 calcBoundingBox(gAcid2);
1302 calcBoundingBox(gSteps);
1303 calcBoundingBox(gLifts);
1304 calcBoundingBox(gBlockMon);
1306 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1308 if (mapX0 > 0) then mapX0 := 0;
1309 if (mapY0 > 0) then mapY0 := 0;
1311 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1312 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1314 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1315 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1317 addPanelsToGrid(gWalls);
1318 addPanelsToGrid(gRenderBackgrounds);
1319 addPanelsToGrid(gRenderForegrounds);
1320 addPanelsToGrid(gWater);
1321 addPanelsToGrid(gAcid1);
1322 addPanelsToGrid(gAcid2);
1323 addPanelsToGrid(gSteps);
1324 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1325 addPanelsToGrid(gBlockMon);
1327 mapGrid.dumpStats();
1329 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1330 end;
1333 function g_Map_Load(Res: String): Boolean;
1334 const
1335 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1336 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1337 type
1338 PTRec = ^TTRec;
1339 TTRec = record
1340 //TexturePanel: Integer;
1341 tnum: Integer;
1342 id: Integer;
1343 trigrec: TDynRecord;
1344 // texture pane;
1345 texPanelIdx: Integer;
1346 texPanel: TDynRecord;
1347 // "action" panel
1348 actPanelIdx: Integer;
1349 actPanel: TDynRecord;
1350 end;
1351 var
1352 WAD, TestWAD: TWADFile;
1353 //mapReader: TDynRecord = nil;
1354 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1355 panels: TDynField = nil; //TPanelsRec1Array;
1356 items: TDynField = nil; //TItemsRec1Array;
1357 monsters: TDynField = nil; //TMonsterRec1Array;
1358 areas: TDynField = nil; //TAreasRec1Array;
1359 triggers: TDynField = nil; //TTriggersRec1Array;
1360 b, c, k: Integer;
1361 PanelID: DWORD;
1362 AddTextures: TAddTextureArray;
1363 TriggersTable: array of TTRec;
1364 FileName, mapResName, TexName, s: AnsiString;
1365 Data: Pointer;
1366 Len: Integer;
1367 ok: Boolean;
1368 CurTex, ntn: Integer;
1369 rec, texrec: TDynRecord;
1370 pttit: PTRec;
1371 pannum, trignum, cnt, tgpid: Integer;
1372 stt: UInt64;
1373 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1374 //moveActive: Boolean;
1375 pan: TPanel;
1376 mapOk: Boolean = false;
1377 usedTextures: THashStrInt = nil; // key: mapTextureList
1378 begin
1379 mapGrid.Free();
1380 mapGrid := nil;
1381 TestWAD := nil;
1382 Data := nil;
1384 //gCurrentMap.Free();
1385 //gCurrentMap := nil;
1387 panByGUID := nil;
1389 Result := False;
1390 gMapInfo.Map := Res;
1391 TriggersTable := nil;
1392 //mapReader := nil;
1394 sfsGCDisable(); // temporary disable removing of temporary volumes
1395 try
1396 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1397 if (gCurrentMap = nil) then
1398 begin
1399 FileName := g_ExtractWadName(Res);
1400 e_LogWritefln('Loading map WAD [%s] (res=[%s])', [FileName, Res], TMsgType.Notify);
1401 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1403 WAD := TWADFile.Create();
1404 if not WAD.ReadFile(FileName) then
1405 begin
1406 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1407 WAD.Free();
1408 Exit;
1409 end;
1411 if gTestMap <> '' then
1412 begin
1413 s := g_ExtractWadName(gTestMap);
1414 TestWAD := TWADFile.Create();
1415 if not TestWAD.ReadFile(s) then
1416 begin
1417 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1418 TestWAD.Free();
1419 TestWAD := nil;
1420 end;
1421 end;
1423 if TestWAD <> nil then
1424 begin
1425 mapResName := g_ExtractFileName(gTestMap);
1426 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1427 begin
1428 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1429 Data := nil;
1430 end else
1431 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1432 TestWAD.Free();
1433 TestWAD := nil;
1434 end;
1436 if Data = nil then
1437 begin
1438 //k8: why loader ignores path here?
1439 mapResName := g_ExtractFileName(Res);
1440 if not WAD.GetMapResource(mapResName, Data, Len) then
1441 begin
1442 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1443 WAD.Free();
1444 Exit;
1445 end;
1446 end;
1448 WAD.Free();
1450 if (Len < 4) then
1451 begin
1452 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1453 FreeMem(Data);
1454 exit;
1455 end;
1457 // Çàãðóçêà êàðòû:
1458 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1459 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1461 stt := getTimeMicro();
1463 try
1464 gCurrentMap := g_Map_ParseMap(Data, Len);
1465 except
1466 gCurrentMap.Free();
1467 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1468 FreeMem(Data);
1469 gCurrentMapFileName := '';
1470 Exit;
1471 end;
1473 FreeMem(Data);
1475 if (gCurrentMap = nil) then
1476 begin
1477 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1478 gCurrentMapFileName := '';
1479 exit;
1480 end;
1481 end
1482 else
1483 begin
1484 stt := getTimeMicro();
1485 end;
1487 //gCurrentMap := mapReader;
1489 generateExternalResourcesList(gCurrentMap);
1490 mapTextureList := gCurrentMap['texture'];
1491 // get all other lists here too
1492 panels := gCurrentMap['panel'];
1493 triggers := gCurrentMap['trigger'];
1494 items := gCurrentMap['item'];
1495 areas := gCurrentMap['area'];
1496 monsters := gCurrentMap['monster'];
1498 // Çàãðóçêà îïèñàíèÿ êàðòû:
1499 e_WriteLog(' Reading map info...', TMsgType.Notify);
1500 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1502 with gMapInfo do
1503 begin
1504 Name := gCurrentMap.MapName;
1505 Description := gCurrentMap.MapDesc;
1506 Author := gCurrentMap.MapAuthor;
1507 MusicName := gCurrentMap.MusicName;
1508 SkyName := gCurrentMap.SkyName;
1509 Height := gCurrentMap.Height;
1510 Width := gCurrentMap.Width;
1511 end;
1513 // Çàãðóçêà òåêñòóð:
1514 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1515 // Äîáàâëåíèå òåêñòóð â Textures[]:
1516 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1517 begin
1518 e_WriteLog(' Loading textures:', TMsgType.Notify);
1519 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1521 // find used textures
1522 usedTextures := THashStrInt.Create();
1523 try
1524 if (panels <> nil) and (panels.count > 0) then
1525 begin
1526 for rec in panels do
1527 begin
1528 texrec := rec.TextureRec;
1529 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1530 end;
1531 end;
1533 cnt := -1;
1534 for rec in mapTextureList do
1535 begin
1536 Inc(cnt);
1537 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1538 begin
1539 rec.tagInt := -1; // just in case
1540 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1541 end
1542 else
1543 begin
1544 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1545 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1546 {$ENDIF}
1547 ntn := CreateTexture(rec.Resource, FileName, True);
1548 if ntn < 0 then
1549 begin
1550 if rec.Anim then
1551 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]))
1552 else
1553 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1554 ntn := CreateNullTexture(rec.Resource)
1555 end;
1556 rec.tagInt := ntn; // remember texture number
1557 end;
1558 g_Game_StepLoading();
1559 end;
1560 finally
1561 usedTextures.Free();
1562 end;
1564 // set panel tagInt to texture index
1565 if (panels <> nil) then
1566 begin
1567 for rec in panels do
1568 begin
1569 texrec := rec.TextureRec;
1570 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1571 end;
1572 end;
1573 end;
1576 // Çàãðóçêà òðèããåðîâ
1577 gTriggerClientID := 0;
1578 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1579 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1581 // Çàãðóçêà ïàíåëåé
1582 e_WriteLog(' Loading panels...', TMsgType.Notify);
1583 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1585 // check texture numbers for panels
1586 if (panels <> nil) and (panels.count > 0) then
1587 begin
1588 for rec in panels do
1589 begin
1590 if (rec.tagInt < 0) then
1591 begin
1592 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1593 result := false;
1594 gCurrentMap.Free();
1595 gCurrentMap := nil;
1596 gCurrentMapFileName := '';
1597 exit;
1598 end;
1599 end;
1600 end;
1602 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1603 if (triggers <> nil) and (triggers.count > 0) then
1604 begin
1605 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1606 //SetLength(TriggersTable, triggers.count);
1607 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1609 SetLength(TriggersTable, triggers.count);
1610 trignum := -1;
1611 for rec in triggers do
1612 begin
1613 Inc(trignum);
1614 pttit := @TriggersTable[trignum];
1615 pttit.trigrec := rec;
1616 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1617 pttit.texPanelIdx := -1; // will be fixed later
1618 pttit.texPanel := rec.TexturePanelRec;
1619 // action panel
1620 pttit.actPanelIdx := -1;
1621 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1622 // set flag
1623 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1624 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1625 // update progress
1626 g_Game_StepLoading();
1627 end;
1628 end;
1630 // Ñîçäàåì ïàíåëè
1631 if (panels <> nil) and (panels.count > 0) then
1632 begin
1633 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1634 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1636 pannum := -1;
1637 for rec in panels do
1638 begin
1639 Inc(pannum);
1640 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1641 texrec := nil;
1642 SetLength(AddTextures, 0);
1643 CurTex := -1;
1644 ok := false;
1646 if (mapTextureList <> nil) then
1647 begin
1648 texrec := rec.TextureRec;
1649 ok := (texrec <> nil);
1650 end;
1652 if ok then
1653 begin
1654 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1655 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1656 ok := false;
1657 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1658 begin
1659 if rec.userPanelTrigRef then
1660 begin
1661 // e_LogWritefln('trigref for panel %s', [pannum]);
1662 ok := True;
1663 end;
1664 end;
1665 end;
1667 if ok then
1668 begin
1669 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1670 s := texrec.Resource;
1672 // Ñïåö-òåêñòóðû çàïðåùåíû
1673 if g_Map_IsSpecialTexture(s) then
1674 begin
1675 ok := false
1676 end
1677 else
1678 begin
1679 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1680 ok := g_Texture_NumNameFindStart(s);
1681 end;
1683 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1684 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1685 if ok then
1686 begin
1687 k := NNF_NAME_BEFORE;
1688 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1689 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1690 begin
1691 k := g_Texture_NumNameFindNext(TexName);
1693 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1694 begin
1695 ok := CreateTexture(TexName, FileName, False) >= 0;
1697 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1698 if ok then
1699 begin
1701 for c := 0 to High(Textures) do
1702 begin
1703 if (Textures[c].TextureName = TexName) then
1704 begin
1705 SetLength(AddTextures, Length(AddTextures)+1);
1706 AddTextures[High(AddTextures)].Texture := c;
1707 break;
1708 end;
1709 end;
1711 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1712 begin
1713 SetLength(AddTextures, Length(AddTextures)+1);
1714 AddTextures[High(AddTextures)].Texture := c;
1715 end;
1716 end;
1717 end
1718 else
1719 begin
1720 if k = NNF_NAME_EQUALS then
1721 begin
1722 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1723 SetLength(AddTextures, Length(AddTextures)+1);
1724 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1725 CurTex := High(AddTextures);
1726 ok := true;
1727 end
1728 else // NNF_NO_NAME
1729 begin
1730 ok := false;
1731 end;
1732 end;
1733 end; // while ok...
1735 ok := true;
1736 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1737 end; // if ok - ññûëàþòñÿ òðèããåðû
1739 if not ok then
1740 begin
1741 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1742 SetLength(AddTextures, 1);
1743 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1744 CurTex := 0;
1745 end;
1747 //e_WriteLog(Format('panel #%d: TextureNum=%d; ht=%d; ht1=%d; atl=%d', [a, panels[a].TextureNum, High(mapTextureList), High(Textures), High(AddTextures)]), MSG_NOTIFY);
1749 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1751 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
1752 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
1753 PanelID := CreatePanel(rec, AddTextures, CurTex);
1754 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
1755 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1757 // setup lifts
1758 moveSpeed := rec.moveSpeed;
1759 //moveStart := rec.moveStart;
1760 //moveEnd := rec.moveEnd;
1761 //moveActive := rec['move_active'].value;
1762 if not moveSpeed.isZero then
1763 begin
1764 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1765 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1766 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1767 end;
1769 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
1771 g_Game_StepLoading();
1772 end;
1773 end;
1775 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
1776 for b := 0 to High(TriggersTable) do
1777 begin
1778 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
1779 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
1780 end;
1782 // create map grid, init other grids (for monsters, for example)
1783 e_WriteLog('Creating map grid', TMsgType.Notify);
1784 mapCreateGrid();
1786 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
1787 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
1788 begin
1789 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
1790 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1791 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
1792 trignum := -1;
1793 for rec in triggers do
1794 begin
1795 Inc(trignum);
1796 tgpid := TriggersTable[trignum].actPanelIdx;
1797 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
1798 TriggersTable[trignum].tnum := trignum;
1799 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
1800 end;
1801 end;
1803 //FIXME: use hashtable!
1804 for pan in panByGUID do
1805 begin
1806 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
1807 begin
1808 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
1809 end;
1810 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
1811 begin
1812 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
1813 end;
1814 end;
1816 // Çàãðóçêà ïðåäìåòîâ
1817 e_WriteLog(' Loading items...', TMsgType.Notify);
1818 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1820 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
1821 if (items <> nil) and not gLoadGameMode then
1822 begin
1823 e_WriteLog(' Spawning items...', TMsgType.Notify);
1824 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1825 for rec in items do CreateItem(rec);
1826 end;
1828 // Çàãðóçêà îáëàñòåé
1829 e_WriteLog(' Loading areas...', TMsgType.Notify);
1830 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1832 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
1833 if areas <> nil then
1834 begin
1835 e_WriteLog(' Creating areas...', TMsgType.Notify);
1836 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1837 for rec in areas do CreateArea(rec);
1838 end;
1840 // Çàãðóçêà ìîíñòðîâ
1841 e_WriteLog(' Loading monsters...', TMsgType.Notify);
1842 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1844 gTotalMonsters := 0;
1846 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
1847 if (monsters <> nil) and not gLoadGameMode then
1848 begin
1849 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
1850 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1851 for rec in monsters do CreateMonster(rec);
1852 end;
1854 //gCurrentMap := mapReader; // this will be our current map now
1855 gCurrentMapFileName := Res;
1856 //mapReader := nil;
1858 // Çàãðóçêà íåáà
1859 gMapInfo.SkyFullName := '';
1860 if (gMapInfo.SkyName <> '') then
1861 begin
1862 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
1863 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1864 gMapInfo.SkyFullName := e_GetResourcePath(WadDirs, gMapInfo.SkyName, g_ExtractWadName(Res));
1865 end;
1867 // Çàãðóçêà ìóçûêè
1868 ok := False;
1869 if gMapInfo.MusicName <> '' then
1870 begin
1871 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
1872 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1874 s := e_GetResourcePath(WadDirs, gMapInfo.MusicName, g_ExtractWadName(Res));
1875 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1876 ok := True
1877 else
1878 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1879 end;
1881 // Îñòàëüíûå óñòàíâêè
1882 CreateDoorMap();
1883 CreateLiftMap();
1885 g_Items_Init();
1886 g_Weapon_Init();
1887 g_Monsters_Init();
1889 {$IFDEF ENABLE_GFX}
1890 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1891 if not gLoadGameMode then g_GFX_Init();
1892 {$ENDIF}
1894 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1895 mapTextureList := nil;
1896 panels := nil;
1897 items := nil;
1898 areas := nil;
1899 triggers := nil;
1900 TriggersTable := nil;
1901 AddTextures := nil;
1903 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1904 if ok and (not gLoadGameMode) then
1905 begin
1906 gMusic.SetByName(gMapInfo.MusicName);
1907 gMusic.Play();
1908 end
1909 else
1910 begin
1911 gMusic.SetByName('');
1912 end;
1914 stt := getTimeMicro()-stt;
1915 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
1916 mapOk := true;
1917 finally
1918 sfsGCEnable(); // enable releasing unused volumes
1919 //mapReader.Free();
1920 e_UnpressAllKeys; // why not?
1921 if not mapOk then
1922 begin
1923 gCurrentMap.Free();
1924 gCurrentMap := nil;
1925 gCurrentMapFileName := '';
1926 end;
1927 end;
1929 compactExtResList();
1930 e_WriteLog('Done loading map.', TMsgType.Notify);
1931 Result := True;
1932 end;
1935 function g_Map_GetMapInfo(Res: String): TMapInfo;
1936 var
1937 WAD: TWADFile;
1938 mapReader: TDynRecord;
1939 //Header: TMapHeaderRec_1;
1940 FileName: String;
1941 Data: Pointer;
1942 Len: Integer;
1943 begin
1944 FillChar(Result, SizeOf(Result), 0);
1945 FileName := g_ExtractWadName(Res);
1947 WAD := TWADFile.Create();
1948 if not WAD.ReadFile(FileName) then
1949 begin
1950 WAD.Free();
1951 Exit;
1952 end;
1954 //k8: it ignores path again
1955 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1956 begin
1957 WAD.Free();
1958 Exit;
1959 end;
1961 WAD.Free();
1963 try
1964 mapReader := g_Map_ParseMap(Data, Len);
1965 except
1966 mapReader := nil;
1967 FreeMem(Data);
1968 exit;
1969 end;
1971 FreeMem(Data);
1973 if (mapReader = nil) then exit;
1975 if (mapReader.Width > 0) and (mapReader.Height > 0) then
1976 begin
1977 Result.Name := mapReader.MapName;
1978 Result.Description := mapReader.MapDesc;
1979 Result.Map := Res;
1980 Result.Author := mapReader.MapAuthor;
1981 Result.Height := mapReader.Height;
1982 Result.Width := mapReader.Width;
1983 end
1984 else
1985 begin
1986 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1987 //ZeroMemory(@Header, SizeOf(Header));
1988 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1989 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1990 Result.Map := Res;
1991 Result.Author := '';
1992 Result.Height := 0;
1993 Result.Width := 0;
1994 end;
1996 mapReader.Free();
1997 end;
1999 function g_Map_GetMapsList(WADName: string): SSArray;
2000 var
2001 WAD: TWADFile;
2002 a: Integer;
2003 ResList: SSArray;
2004 begin
2005 Result := nil;
2006 WAD := TWADFile.Create();
2007 if not WAD.ReadFile(WADName) then
2008 begin
2009 WAD.Free();
2010 Exit;
2011 end;
2012 ResList := WAD.GetMapResources();
2013 if ResList <> nil then
2014 begin
2015 for a := 0 to High(ResList) do
2016 begin
2017 SetLength(Result, Length(Result)+1);
2018 Result[High(Result)] := ResList[a];
2019 end;
2020 end;
2021 WAD.Free();
2022 end;
2024 function g_Map_Exist(Res: string): Boolean;
2025 var
2026 WAD: TWADFile;
2027 FileName, mnn: string;
2028 ResList: SSArray;
2029 a: Integer;
2030 begin
2031 Result := False;
2033 FileName := addWadExtension(g_ExtractWadName(Res));
2035 WAD := TWADFile.Create;
2036 if not WAD.ReadFile(FileName) then
2037 begin
2038 WAD.Free();
2039 Exit;
2040 end;
2042 ResList := WAD.GetMapResources();
2043 WAD.Free();
2045 mnn := g_ExtractFileName(Res);
2046 if ResList <> nil then
2047 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2048 begin
2049 Result := True;
2050 Exit;
2051 end;
2052 end;
2054 procedure g_Map_Free(freeTextures: Boolean=true);
2056 procedure FreePanelArray(var panels: TPanelArray);
2057 var
2058 i: Integer;
2060 begin
2061 if panels <> nil then
2062 begin
2063 for i := 0 to High(panels) do
2064 panels[i].Free();
2065 panels := nil;
2066 end;
2067 end;
2069 begin
2070 {$IFDEF ENABLE_GFX}
2071 g_GFX_Free;
2072 {$ENDIF}
2073 g_Weapon_Free();
2074 g_Items_Free();
2075 g_Triggers_Free();
2076 g_Monsters_Free();
2078 RespawnPoints := nil;
2079 if FlagPoints[FLAG_RED] <> nil then
2080 begin
2081 Dispose(FlagPoints[FLAG_RED]);
2082 FlagPoints[FLAG_RED] := nil;
2083 end;
2084 if FlagPoints[FLAG_BLUE] <> nil then
2085 begin
2086 Dispose(FlagPoints[FLAG_BLUE]);
2087 FlagPoints[FLAG_BLUE] := nil;
2088 end;
2089 //DOMFlagPoints := nil;
2091 //gDOMFlags := nil;
2093 if (Length(gCurrentMapFileName) <> 0) then
2094 begin
2095 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2096 end
2097 else
2098 begin
2099 e_LogWritefln('g_Map_Free: no previous map.', []);
2100 end;
2102 if freeTextures then
2103 begin
2104 e_LogWritefln('g_Map_Free: clearing textures...', []);
2105 Textures := nil;
2106 TextNameHash.Free();
2107 TextNameHash := nil;
2108 BadTextNameHash.Free();
2109 BadTextNameHash := nil;
2110 gCurrentMapFileName := '';
2111 gCurrentMap.Free();
2112 gCurrentMap := nil;
2113 end;
2115 panByGUID := nil;
2117 FreePanelArray(gWalls);
2118 FreePanelArray(gRenderBackgrounds);
2119 FreePanelArray(gRenderForegrounds);
2120 FreePanelArray(gWater);
2121 FreePanelArray(gAcid1);
2122 FreePanelArray(gAcid2);
2123 FreePanelArray(gSteps);
2124 FreePanelArray(gLifts);
2125 FreePanelArray(gBlockMon);
2126 gMovingWallIds := nil;
2128 g_Game_StopAllSounds(False);
2129 gMusic.FreeSound();
2130 g_Sound_Delete(gMapInfo.MusicName);
2132 gMapInfo.Name := '';
2133 gMapInfo.Description := '';
2134 gMapInfo.MusicName := '';
2135 gMapInfo.Height := 0;
2136 gMapInfo.Width := 0;
2138 gDoorMap := nil;
2139 gLiftMap := nil;
2140 end;
2142 procedure g_Map_Update();
2143 var
2144 a, d, j: Integer;
2145 m: Word;
2146 s: String;
2147 b: Byte;
2149 procedure UpdatePanelArray(var panels: TPanelArray);
2150 var
2151 i: Integer;
2153 begin
2154 for i := 0 to High(panels) do panels[i].Update();
2155 end;
2157 begin
2158 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2160 UpdatePanelArray(gWalls);
2161 UpdatePanelArray(gRenderBackgrounds);
2162 UpdatePanelArray(gRenderForegrounds);
2163 UpdatePanelArray(gWater);
2164 UpdatePanelArray(gAcid1);
2165 UpdatePanelArray(gAcid2);
2166 UpdatePanelArray(gSteps);
2168 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2170 if gGameSettings.GameMode = GM_CTF then
2171 begin
2172 for a := FLAG_RED to FLAG_BLUE do
2173 begin
2174 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2175 begin
2176 with gFlags[a] do
2177 begin
2178 Obj.oldX := Obj.X;
2179 Obj.oldY := Obj.Y;
2181 m := g_Obj_Move(@Obj, True, True);
2183 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
2185 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2187 // Ñîïðîòèâëåíèå âîçäóõà
2188 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2190 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2191 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2192 begin
2193 g_Map_ResetFlag(a);
2194 gFlags[a].CaptureTime := 0;
2195 if a = FLAG_RED then
2196 s := _lc[I_PLAYER_FLAG_RED]
2197 else
2198 s := _lc[I_PLAYER_FLAG_BLUE];
2199 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2201 if (((gPlayer1 <> nil) and (((gPlayer1.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer1.Team = TEAM_BLUE) and (a = FLAG_BLUE))))
2202 or ((gPlayer2 <> nil) and (((gPlayer2.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer2.Team = TEAM_BLUE) and (a = FLAG_BLUE))))) then
2203 b := 0
2204 else
2205 b := 1;
2207 if not sound_ret_flag[b].IsPlaying() then
2208 sound_ret_flag[b].Play();
2210 if g_Game_IsNet then
2211 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2212 Continue;
2213 end;
2215 if Count > 0 then Count -= 1;
2217 // Èãðîê áåðåò ôëàã
2218 if gPlayers <> nil then
2219 begin
2220 j := Random(Length(gPlayers)) - 1;
2221 for d := 0 to High(gPlayers) do
2222 begin
2223 Inc(j);
2224 if j > High(gPlayers) then j := 0;
2225 if gPlayers[j] <> nil then
2226 begin
2227 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2228 begin
2229 if gPlayers[j].GetFlag(a) then Break;
2230 end;
2231 end;
2232 end;
2233 end;
2234 end;
2235 end;
2236 end;
2237 end;
2238 end;
2240 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2241 PanelType: Word; b1x3: Boolean=false): Boolean;
2242 var
2243 a, h: Integer;
2244 begin
2245 Result := False;
2247 if WordBool(PanelType and PANEL_WALL) then
2248 if gWalls <> nil then
2249 begin
2250 h := High(gWalls);
2252 for a := 0 to h do
2253 if gWalls[a].Enabled and
2254 g_Collide(X, Y, Width, Height,
2255 gWalls[a].X, gWalls[a].Y,
2256 gWalls[a].Width, gWalls[a].Height) then
2257 begin
2258 Result := True;
2259 Exit;
2260 end;
2261 end;
2263 if WordBool(PanelType and PANEL_WATER) then
2264 if gWater <> nil then
2265 begin
2266 h := High(gWater);
2268 for a := 0 to h do
2269 if g_Collide(X, Y, Width, Height,
2270 gWater[a].X, gWater[a].Y,
2271 gWater[a].Width, gWater[a].Height) then
2272 begin
2273 Result := True;
2274 Exit;
2275 end;
2276 end;
2278 if WordBool(PanelType and PANEL_ACID1) then
2279 if gAcid1 <> nil then
2280 begin
2281 h := High(gAcid1);
2283 for a := 0 to h do
2284 if g_Collide(X, Y, Width, Height,
2285 gAcid1[a].X, gAcid1[a].Y,
2286 gAcid1[a].Width, gAcid1[a].Height) then
2287 begin
2288 Result := True;
2289 Exit;
2290 end;
2291 end;
2293 if WordBool(PanelType and PANEL_ACID2) then
2294 if gAcid2 <> nil then
2295 begin
2296 h := High(gAcid2);
2298 for a := 0 to h do
2299 if g_Collide(X, Y, Width, Height,
2300 gAcid2[a].X, gAcid2[a].Y,
2301 gAcid2[a].Width, gAcid2[a].Height) then
2302 begin
2303 Result := True;
2304 Exit;
2305 end;
2306 end;
2308 if WordBool(PanelType and PANEL_STEP) then
2309 if gSteps <> nil then
2310 begin
2311 h := High(gSteps);
2313 for a := 0 to h do
2314 if g_Collide(X, Y, Width, Height,
2315 gSteps[a].X, gSteps[a].Y,
2316 gSteps[a].Width, gSteps[a].Height) then
2317 begin
2318 Result := True;
2319 Exit;
2320 end;
2321 end;
2323 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2324 if gLifts <> nil then
2325 begin
2326 h := High(gLifts);
2328 for a := 0 to h do
2329 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = LIFTTYPE_UP)) or
2330 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = LIFTTYPE_DOWN)) or
2331 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = LIFTTYPE_LEFT)) or
2332 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = LIFTTYPE_RIGHT))) and
2333 g_Collide(X, Y, Width, Height,
2334 gLifts[a].X, gLifts[a].Y,
2335 gLifts[a].Width, gLifts[a].Height) then
2336 begin
2337 Result := True;
2338 Exit;
2339 end;
2340 end;
2342 if WordBool(PanelType and PANEL_BLOCKMON) then
2343 if gBlockMon <> nil then
2344 begin
2345 h := High(gBlockMon);
2347 for a := 0 to h do
2348 if ( (not b1x3) or
2349 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2350 g_Collide(X, Y, Width, Height,
2351 gBlockMon[a].X, gBlockMon[a].Y,
2352 gBlockMon[a].Width, gBlockMon[a].Height) then
2353 begin
2354 Result := True;
2355 Exit;
2356 end;
2357 end;
2358 end;
2360 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2361 var
2362 texid: DWORD;
2364 function checkPanels (constref panels: TPanelArray): Boolean;
2365 var
2366 a: Integer;
2367 begin
2368 result := false;
2369 if panels = nil then exit;
2370 for a := 0 to High(panels) do
2371 begin
2372 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2373 begin
2374 result := true;
2375 texid := panels[a].GetTextureID();
2376 exit;
2377 end;
2378 end;
2379 end;
2381 begin
2382 texid := LongWord(TEXTURE_NONE);
2383 result := texid;
2384 if not checkPanels(gWater) then
2385 if not checkPanels(gAcid1) then
2386 if not checkPanels(gAcid2) then exit;
2387 result := texid;
2388 end;
2391 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2392 const
2393 SlowMask = GridTagLift or GridTagBlockMon;
2395 function checker (pan: TPanel; tag: Integer): Boolean;
2396 begin
2398 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2399 begin
2400 result := pan.Enabled;
2401 exit;
2402 end;
2405 if ((tag and GridTagLift) <> 0) then
2406 begin
2407 result :=
2408 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2409 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2410 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2411 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2412 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2413 exit;
2414 end;
2416 if ((tag and GridTagBlockMon) <> 0) then
2417 begin
2418 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2419 exit;
2420 end;
2422 // other shit
2423 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2424 result := true; // i found her!
2425 end;
2427 var
2428 tagmask: Integer = 0;
2429 mwit: PPanel;
2430 it: TPanelGrid.Iter;
2431 pan: TPanel;
2432 begin
2433 result := false;
2434 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2435 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2436 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2437 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2438 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2439 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2440 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2442 if (tagmask = 0) then exit; // just in case
2444 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2445 if gdbg_map_use_accel_coldet then
2446 begin
2447 if ((tagmask and SlowMask) <> 0) then
2448 begin
2449 // slow
2450 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2451 for mwit in it do
2452 begin
2453 pan := mwit^;
2454 if ((pan.tag and GridTagLift) <> 0) then
2455 begin
2456 result :=
2457 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2458 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2459 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2460 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2461 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2462 end
2463 else if ((pan.tag and GridTagBlockMon) <> 0) then
2464 begin
2465 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2466 end
2467 else
2468 begin
2469 // other shit
2470 result := true; // i found her!
2471 end;
2472 if (result) then break;
2473 end;
2474 end
2475 else
2476 begin
2477 // fast
2478 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2479 result := (it.length > 0);
2480 end;
2481 it.release();
2482 end
2483 else
2484 begin
2485 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2486 end;
2487 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2488 end;
2491 // returns `true` if we need to stop
2492 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2493 begin
2494 result := false;
2495 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2496 // check priorities
2497 case cctype of
2498 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2499 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2500 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2501 end;
2502 // collision?
2503 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2504 // yeah
2505 texid := pan.GetTextureID();
2506 // water? water has the highest priority, so stop right here
2507 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2508 // acid2?
2509 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2510 // acid1?
2511 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2512 end;
2514 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2515 var
2516 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2517 mwit: PPanel;
2518 it: TPanelGrid.Iter;
2519 begin
2520 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2521 if gdbg_map_use_accel_coldet then
2522 begin
2523 result := LongWord(TEXTURE_NONE);
2524 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2525 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2526 it.release();
2527 end
2528 else
2529 begin
2530 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2531 end;
2532 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2533 end;
2536 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2537 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2538 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2541 procedure g_Map_EnableWallGUID (pguid: Integer);
2542 var
2543 pan: TPanel;
2544 begin
2545 //pan := gWalls[ID];
2546 pan := g_Map_PanelByGUID(pguid);
2547 if (pan = nil) then exit;
2548 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2550 pan.Enabled := True;
2552 {$IFDEF ENABLE_GFX}
2553 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2554 {$ENDIF}
2556 mapGrid.proxyEnabled[pan.proxyId] := true;
2557 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2558 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2560 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2561 // mark platform as interesting
2562 pan.setDirty();
2564 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2565 //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);
2566 {$ENDIF}
2567 end;
2570 procedure g_Map_DisableWallGUID (pguid: Integer);
2571 var
2572 pan: TPanel;
2573 begin
2574 //pan := gWalls[ID];
2575 pan := g_Map_PanelByGUID(pguid);
2576 if (pan = nil) then exit;
2577 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2579 pan.Enabled := False;
2580 {$IFDEF ENABLE_GFX}
2581 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2582 {$ENDIF}
2584 mapGrid.proxyEnabled[pan.proxyId] := false;
2585 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2587 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2588 // mark platform as interesting
2589 pan.setDirty();
2591 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2592 //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);
2593 {$ENDIF}
2594 end;
2597 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
2598 var
2599 tp: TPanel;
2600 begin
2601 tp := g_Map_PanelByGUID(pguid);
2602 if (tp = nil) then exit;
2603 tp.NextTexture(AnimLoop);
2604 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
2605 end;
2608 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2609 var
2610 pan: TPanel;
2611 begin
2612 //pan := gLifts[ID];
2613 pan := g_Map_PanelByGUID(pguid);
2614 if (pan = nil) then exit;
2615 if not pan.isGLift then exit;
2617 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2619 with {gLifts[ID]} pan do
2620 begin
2621 LiftType := t;
2623 {$IFDEF ENABLE_GFX}
2624 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2625 //TODO: make separate lift tags, and change tag here
2626 case LiftType of
2627 LIFTTYPE_UP: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2628 LIFTTYPE_DOWN: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2629 LIFTTYPE_LEFT: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2630 LIFTTYPE_RIGHT: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2631 end;
2632 {$ENDIF}
2634 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2635 // mark platform as interesting
2636 pan.setDirty();
2637 end;
2638 end;
2641 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2642 var
2643 a: Integer;
2644 PointsArray: Array of TRespawnPoint;
2645 begin
2646 Result := False;
2647 SetLength(PointsArray, 0);
2649 if RespawnPoints = nil then
2650 Exit;
2652 for a := 0 to High(RespawnPoints) do
2653 if RespawnPoints[a].PointType = PointType then
2654 begin
2655 SetLength(PointsArray, Length(PointsArray)+1);
2656 PointsArray[High(PointsArray)] := RespawnPoints[a];
2657 end;
2659 if PointsArray = nil then
2660 Exit;
2662 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2663 Result := True;
2664 end;
2666 function g_Map_GetPointCount(PointType: Byte): Word;
2667 var
2668 a: Integer;
2669 begin
2670 Result := 0;
2672 if RespawnPoints = nil then
2673 Exit;
2675 for a := 0 to High(RespawnPoints) do
2676 if RespawnPoints[a].PointType = PointType then
2677 Result := Result + 1;
2678 end;
2680 function g_Map_GetRandomPointType(): Byte;
2681 begin
2682 if RespawnPoints = nil then
2683 Result := 255
2684 else
2685 Result := RespawnPoints[Random(Length(RespawnPoints))].PointType;
2686 end;
2688 function g_Map_HaveFlagPoints(): Boolean;
2689 begin
2690 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2691 end;
2693 procedure g_Map_ResetFlag(Flag: Byte);
2694 begin
2695 with gFlags[Flag] do
2696 begin
2697 Obj.X := -1000;
2698 Obj.Y := -1000;
2699 Obj.Vel.X := 0;
2700 Obj.Vel.Y := 0;
2701 Direction := TDirection.D_LEFT;
2702 State := FLAG_STATE_NONE;
2703 if FlagPoints[Flag] <> nil then
2704 begin
2705 Obj.X := FlagPoints[Flag]^.X;
2706 Obj.Y := FlagPoints[Flag]^.Y;
2707 Direction := FlagPoints[Flag]^.Direction;
2708 State := FLAG_STATE_NORMAL;
2709 end;
2710 Obj.oldX := Obj.X;
2711 Obj.oldY := Obj.Y;
2712 NeedSend := False; // the event will take care of this
2713 Count := -1;
2714 end;
2715 end;
2717 procedure g_Map_SaveState (st: TStream);
2718 var
2719 str: String;
2721 procedure savePanels ();
2722 var
2723 pan: TPanel;
2724 begin
2725 // Ñîõðàíÿåì ïàíåëè
2726 utils.writeInt(st, LongInt(Length(panByGUID)));
2727 for pan in panByGUID do pan.SaveState(st);
2728 end;
2730 procedure saveFlag (flag: PFlag);
2731 var
2732 b: Byte;
2733 begin
2734 utils.writeSign(st, 'FLAG');
2735 utils.writeInt(st, Byte(0)); // version
2736 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
2737 utils.writeInt(st, Byte(flag^.RespawnType));
2738 // Ñîñòîÿíèå ôëàãà
2739 utils.writeInt(st, Byte(flag^.State));
2740 // Íàïðàâëåíèå ôëàãà
2741 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
2742 utils.writeInt(st, Byte(b));
2743 // Îáúåêò ôëàãà
2744 Obj_SaveState(st, @flag^.Obj);
2745 end;
2747 begin
2748 savePanels();
2750 // Ñîõðàíÿåì ìóçûêó
2751 utils.writeSign(st, 'MUSI');
2752 utils.writeInt(st, Byte(0));
2753 // Íàçâàíèå ìóçûêè
2754 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2755 if gMusic.NoMusic then str := '' else str := gMusic.Name;
2756 utils.writeStr(st, str);
2757 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
2758 utils.writeInt(st, LongWord(gMusic.GetPosition()));
2759 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
2760 utils.writeBool(st, gMusic.SpecPause);
2762 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2763 utils.writeInt(st, LongInt(gTotalMonsters));
2764 ///// /////
2766 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2767 if (gGameSettings.GameMode = GM_CTF) then
2768 begin
2769 // Ôëàã Êðàñíîé êîìàíäû
2770 saveFlag(@gFlags[FLAG_RED]);
2771 // Ôëàã Ñèíåé êîìàíäû
2772 saveFlag(@gFlags[FLAG_BLUE]);
2773 end;
2774 ///// /////
2776 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2777 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2778 begin
2779 // Î÷êè Êðàñíîé êîìàíäû
2780 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Score));
2781 // Î÷êè Ñèíåé êîìàíäû
2782 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Score));
2783 end;
2784 ///// /////
2785 end;
2788 procedure g_Map_LoadState (st: TStream);
2789 var
2790 dw: DWORD;
2791 str: String;
2792 boo: Boolean;
2794 procedure loadPanels ();
2795 var
2796 pan: TPanel;
2797 begin
2798 // Çàãðóæàåì ïàíåëè
2799 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
2800 for pan in panByGUID do
2801 begin
2802 pan.LoadState(st);
2803 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
2804 end;
2805 end;
2807 procedure loadFlag (flag: PFlag);
2808 var
2809 b: Byte;
2810 begin
2811 // Ñèãíàòóðà ôëàãà
2812 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
2813 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
2814 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
2815 flag^.RespawnType := utils.readByte(st);
2816 // Ñîñòîÿíèå ôëàãà
2817 flag^.State := utils.readByte(st);
2818 // Íàïðàâëåíèå ôëàãà
2819 b := utils.readByte(st);
2820 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
2821 // Îáúåêò ôëàãà
2822 Obj_LoadState(@flag^.Obj, st);
2823 end;
2825 begin
2826 if (st = nil) then exit;
2828 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2829 loadPanels();
2830 ///// /////
2832 {$IFDEF ENABLE_GFX}
2833 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
2834 g_GFX_Init();
2835 {$ENDIF}
2837 //mapCreateGrid();
2839 ///// Çàãðóæàåì ìóçûêó: /////
2840 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
2841 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
2842 // Íàçâàíèå ìóçûêè
2843 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2844 str := utils.readStr(st);
2845 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
2846 dw := utils.readLongWord(st);
2847 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
2848 boo := utils.readBool(st);
2849 // Çàïóñêàåì ýòó ìóçûêó
2850 gMusic.SetByName(str);
2851 gMusic.SpecPause := boo;
2852 gMusic.Play();
2853 gMusic.Pause(true);
2854 gMusic.SetPosition(dw);
2855 ///// /////
2857 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2858 gTotalMonsters := utils.readLongInt(st);
2859 ///// /////
2861 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2862 if (gGameSettings.GameMode = GM_CTF) then
2863 begin
2864 // Ôëàã Êðàñíîé êîìàíäû
2865 loadFlag(@gFlags[FLAG_RED]);
2866 // Ôëàã Ñèíåé êîìàíäû
2867 loadFlag(@gFlags[FLAG_BLUE]);
2868 end;
2869 ///// /////
2871 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2872 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2873 begin
2874 // Î÷êè Êðàñíîé êîìàíäû
2875 gTeamStat[TEAM_RED].Score := utils.readSmallInt(st);
2876 // Î÷êè Ñèíåé êîìàíäû
2877 gTeamStat[TEAM_BLUE].Score := utils.readSmallInt(st);
2878 end;
2879 ///// /////
2880 end;
2883 // trace liquid, stepping by `dx` and `dy`
2884 // return last seen liquid coords, and `false` if we're started outside of the liquid
2885 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2886 const
2887 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2888 begin
2889 topx := x;
2890 topy := y;
2891 // started outside of the liquid?
2892 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2893 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
2894 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2895 result := true;
2896 while true do
2897 begin
2898 Inc(x, dx);
2899 Inc(y, dy);
2900 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2901 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
2902 topx := x;
2903 topy := y;
2904 end;
2905 end;
2908 begin
2909 DynWarningCB := mapWarningCB;
2910 end.