DEADSOFTWARE

71f50d1c66c93f4af8352dfe44e400b2ec506005
[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, g_window,
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: TWADFile;
963 WADName, ResName: String;
964 ResData: Pointer;
965 ResLen: Integer;
966 id: Integer;
967 begin
968 Result := -1;
969 HName := toLowerCase1251(RecName);
970 if (TextNameHash = nil) then
971 TextNameHash := THashStrInt.Create();
972 if TextNameHash.get(HName, Result) then
973 begin
974 // e_LogWritefln('CreateTexture: found loaded %s', [Result]);
975 end
976 else
977 begin
978 Result := -1;
979 if (BadTextNameHash = nil) or not BadTextNameHash.has(HName) then
980 begin
981 case RecName of
982 TEXTURE_NAME_WATER, TEXTURE_NAME_ACID1, TEXTURE_NAME_ACID2:
983 begin
984 SetLength(Textures, Length(Textures) + 1);
985 Textures[High(Textures)].FullName := RecName;
986 Textures[High(Textures)].TextureName := RecName;
987 Result := High(Textures);
988 TextNameHash.put(RecName, result);
989 end
990 else
991 WADName := GetReplacementWad(g_ExtractWadName(RecName));
992 if (WADName <> '') then
993 addResToExternalResList(WADName);
994 if WADName = '' then
995 WADName := Map;
996 ResName := g_ExtractFilePathName(RecName);
997 WAD := TWADFile.Create();
998 if WAD.ReadFile(WadName) then
999 begin
1000 if WAD.GetResource(ResName, ResData, ResLen, log) then
1001 begin
1002 SetLength(Textures, Length(Textures) + 1);
1003 Textures[High(Textures)].FullName := WADName + ':' + ResName;
1004 Textures[High(Textures)].TextureName := RecName;
1005 Result := High(Textures);
1006 TextNameHash.put(HName, result);
1007 FreeMem(ResData);
1008 end;
1009 end;
1010 WAD.Free;
1011 end
1012 end;
1013 end;
1014 if Result < 0 then
1015 begin
1016 if (BadTextNameHash = nil) then
1017 BadTextNameHash := THashStrInt.Create();
1018 if log and (not BadTextNameHash.get(HName, id)) then
1019 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1020 BadTextNameHash.put(HName, -1);
1021 end
1022 end;
1024 procedure CreateItem(Item: TDynRecord);
1025 begin
1026 if g_Game_IsClient then Exit;
1028 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1029 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1030 Exit;
1032 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1033 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1034 end;
1036 procedure CreateArea(Area: TDynRecord);
1037 var
1038 a: Integer;
1039 begin
1040 case Area.AreaType of
1041 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1042 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1043 begin
1044 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1045 with RespawnPoints[High(RespawnPoints)] do
1046 begin
1047 X := Area.X;
1048 Y := Area.Y;
1049 Direction := TDirection(Area.Direction);
1051 case Area.AreaType of
1052 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1053 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1054 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1055 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1056 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1057 end;
1058 end;
1059 end;
1061 AREA_REDFLAG, AREA_BLUEFLAG:
1062 begin
1063 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1065 if FlagPoints[a] <> nil then Exit;
1067 New(FlagPoints[a]);
1069 with FlagPoints[a]^ do
1070 begin
1071 X := Area.X-FLAGRECT.X;
1072 Y := Area.Y-FLAGRECT.Y;
1073 Direction := TDirection(Area.Direction);
1074 end;
1076 with gFlags[a] do
1077 begin
1078 Obj.Rect := FLAGRECT;
1079 g_Map_ResetFlag(a);
1080 end;
1081 end;
1083 AREA_DOMFLAG:
1084 begin
1085 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1086 with DOMFlagPoints[High(DOMFlagPoints)] do
1087 begin
1088 X := Area.X;
1089 Y := Area.Y;
1090 Direction := TDirection(Area.Direction);
1091 end;
1093 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1094 end;
1095 end;
1096 end;
1098 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1099 var
1100 _trigger: TTrigger;
1101 tp: TPanel;
1102 begin
1103 result := -1;
1104 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1106 with _trigger do
1107 begin
1108 mapId := Trigger.id;
1109 mapIndex := amapIdx;
1110 X := Trigger.X;
1111 Y := Trigger.Y;
1112 Width := Trigger.Width;
1113 Height := Trigger.Height;
1114 Enabled := Trigger.Enabled;
1115 TexturePanelGUID := atpanid;
1116 TriggerType := Trigger.TriggerType;
1117 ActivateType := Trigger.ActivateType;
1118 Keys := Trigger.Keys;
1119 trigPanelGUID := atrigpanid;
1120 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1121 if TexturePanelGUID <> -1 then
1122 begin
1123 tp := g_Map_PanelByGUID(TexturePanelGUID);
1124 if (tp <> nil) then tp.hasTexTrigger := True;
1125 end;
1126 end;
1128 result := Integer(g_Triggers_Create(_trigger, Trigger));
1129 end;
1131 procedure CreateMonster(monster: TDynRecord);
1132 var
1133 a: Integer;
1134 mon: TMonster;
1135 begin
1136 if g_Game_IsClient then Exit;
1138 if (gGameSettings.GameType = GT_SINGLE)
1139 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1140 begin
1141 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1143 if gTriggers <> nil then
1144 begin
1145 for a := 0 to High(gTriggers) do
1146 begin
1147 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1148 begin
1149 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1150 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1151 end;
1152 end;
1153 end;
1155 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1156 end;
1157 end;
1159 procedure g_Map_ReAdd_DieTriggers();
1161 function monsDieTrig (mon: TMonster): Boolean;
1162 var
1163 a: Integer;
1164 //tw: TStrTextWriter;
1165 begin
1166 result := false; // don't stop
1167 mon.ClearTriggers();
1168 for a := 0 to High(gTriggers) do
1169 begin
1170 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1171 begin
1172 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1174 tw := TStrTextWriter.Create();
1175 try
1176 gTriggers[a].trigData.writeTo(tw);
1177 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1178 finally
1179 tw.Free();
1180 end;
1182 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1183 end;
1184 end;
1185 end;
1187 begin
1188 if g_Game_IsClient then Exit;
1190 g_Mons_ForEach(monsDieTrig);
1191 end;
1193 procedure mapCreateGrid ();
1194 var
1195 mapX0: Integer = $3fffffff;
1196 mapY0: Integer = $3fffffff;
1197 mapX1: Integer = -$3fffffff;
1198 mapY1: Integer = -$3fffffff;
1200 procedure calcBoundingBox (constref panels: TPanelArray);
1201 var
1202 idx: Integer;
1203 pan: TPanel;
1204 begin
1205 for idx := 0 to High(panels) do
1206 begin
1207 pan := panels[idx];
1208 if not pan.visvalid then continue;
1209 if (pan.Width < 1) or (pan.Height < 1) then continue;
1210 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1211 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1212 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1213 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1214 end;
1215 end;
1217 procedure addPanelsToGrid (constref panels: TPanelArray);
1218 var
1219 idx: Integer;
1220 pan: TPanel;
1221 newtag: Integer;
1222 begin
1223 //tag := panelTypeToTag(tag);
1224 for idx := 0 to High(panels) do
1225 begin
1226 pan := panels[idx];
1227 if not pan.visvalid then continue;
1228 if (pan.proxyId <> -1) then
1229 begin
1230 {$IF DEFINED(D2F_DEBUG)}
1231 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);
1232 {$ENDIF}
1233 continue;
1234 end;
1235 case pan.PanelType of
1236 PANEL_WALL: newtag := GridTagWall;
1237 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1238 PANEL_BACK: newtag := GridTagBack;
1239 PANEL_FORE: newtag := GridTagFore;
1240 PANEL_WATER: newtag := GridTagWater;
1241 PANEL_ACID1: newtag := GridTagAcid1;
1242 PANEL_ACID2: newtag := GridTagAcid2;
1243 PANEL_STEP: newtag := GridTagStep;
1244 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1245 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1246 else continue; // oops
1247 end;
1248 pan.tag := newtag;
1250 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1251 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1252 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1253 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1255 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1256 begin
1257 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1258 end;
1260 {$ENDIF}
1261 end;
1262 end;
1264 begin
1265 mapGrid.Free();
1266 mapGrid := nil;
1268 calcBoundingBox(gWalls);
1269 calcBoundingBox(gRenderBackgrounds);
1270 calcBoundingBox(gRenderForegrounds);
1271 calcBoundingBox(gWater);
1272 calcBoundingBox(gAcid1);
1273 calcBoundingBox(gAcid2);
1274 calcBoundingBox(gSteps);
1275 calcBoundingBox(gLifts);
1276 calcBoundingBox(gBlockMon);
1278 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1280 if (mapX0 > 0) then mapX0 := 0;
1281 if (mapY0 > 0) then mapY0 := 0;
1283 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1284 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1286 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1287 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1289 addPanelsToGrid(gWalls);
1290 addPanelsToGrid(gRenderBackgrounds);
1291 addPanelsToGrid(gRenderForegrounds);
1292 addPanelsToGrid(gWater);
1293 addPanelsToGrid(gAcid1);
1294 addPanelsToGrid(gAcid2);
1295 addPanelsToGrid(gSteps);
1296 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1297 addPanelsToGrid(gBlockMon);
1299 mapGrid.dumpStats();
1301 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1302 end;
1305 function g_Map_Load(Res: String): Boolean;
1306 const
1307 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1308 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1309 type
1310 PTRec = ^TTRec;
1311 TTRec = record
1312 //TexturePanel: Integer;
1313 tnum: Integer;
1314 id: Integer;
1315 trigrec: TDynRecord;
1316 // texture pane;
1317 texPanelIdx: Integer;
1318 texPanel: TDynRecord;
1319 // "action" panel
1320 actPanelIdx: Integer;
1321 actPanel: TDynRecord;
1322 end;
1323 var
1324 WAD, TestWAD: TWADFile;
1325 //mapReader: TDynRecord = nil;
1326 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1327 panels: TDynField = nil; //TPanelsRec1Array;
1328 items: TDynField = nil; //TItemsRec1Array;
1329 monsters: TDynField = nil; //TMonsterRec1Array;
1330 areas: TDynField = nil; //TAreasRec1Array;
1331 triggers: TDynField = nil; //TTriggersRec1Array;
1332 b, c, k: Integer;
1333 PanelID: DWORD;
1334 AddTextures: TAddTextureArray;
1335 TriggersTable: array of TTRec;
1336 FileName, mapResName, TexName, s: AnsiString;
1337 Data: Pointer;
1338 Len: Integer;
1339 ok: Boolean;
1340 CurTex, ntn: Integer;
1341 rec, texrec: TDynRecord;
1342 pttit: PTRec;
1343 pannum, trignum, cnt, tgpid: Integer;
1344 stt: UInt64;
1345 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1346 //moveActive: Boolean;
1347 pan: TPanel;
1348 mapOk: Boolean = false;
1349 usedTextures: THashStrInt = nil; // key: mapTextureList
1350 begin
1351 mapGrid.Free();
1352 mapGrid := nil;
1353 TestWAD := nil;
1354 Data := nil;
1356 //gCurrentMap.Free();
1357 //gCurrentMap := nil;
1359 panByGUID := nil;
1361 Result := False;
1362 gMapInfo.Map := Res;
1363 TriggersTable := nil;
1364 //mapReader := nil;
1366 sfsGCDisable(); // temporary disable removing of temporary volumes
1367 try
1368 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1369 if (gCurrentMap = nil) then
1370 begin
1371 FileName := g_ExtractWadName(Res);
1372 e_LogWritefln('Loading map WAD [%s] (res=[%s])', [FileName, Res], TMsgType.Notify);
1373 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1375 WAD := TWADFile.Create();
1376 if not WAD.ReadFile(FileName) then
1377 begin
1378 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1379 WAD.Free();
1380 Exit;
1381 end;
1383 if gTestMap <> '' then
1384 begin
1385 s := g_ExtractWadName(gTestMap);
1386 TestWAD := TWADFile.Create();
1387 if not TestWAD.ReadFile(s) then
1388 begin
1389 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1390 TestWAD.Free();
1391 TestWAD := nil;
1392 end;
1393 end;
1395 if TestWAD <> nil then
1396 begin
1397 mapResName := g_ExtractFileName(gTestMap);
1398 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1399 begin
1400 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1401 Data := nil;
1402 end else
1403 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1404 TestWAD.Free();
1405 TestWAD := nil;
1406 end;
1408 if Data = nil then
1409 begin
1410 //k8: why loader ignores path here?
1411 mapResName := g_ExtractFileName(Res);
1412 if not WAD.GetMapResource(mapResName, Data, Len) then
1413 begin
1414 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1415 WAD.Free();
1416 Exit;
1417 end;
1418 end;
1420 WAD.Free();
1422 if (Len < 4) then
1423 begin
1424 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1425 FreeMem(Data);
1426 exit;
1427 end;
1429 // Çàãðóçêà êàðòû:
1430 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1431 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1433 stt := getTimeMicro();
1435 try
1436 gCurrentMap := g_Map_ParseMap(Data, Len);
1437 except
1438 gCurrentMap.Free();
1439 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1440 FreeMem(Data);
1441 gCurrentMapFileName := '';
1442 Exit;
1443 end;
1445 FreeMem(Data);
1447 if (gCurrentMap = nil) then
1448 begin
1449 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1450 gCurrentMapFileName := '';
1451 exit;
1452 end;
1453 end
1454 else
1455 begin
1456 stt := getTimeMicro();
1457 end;
1459 //gCurrentMap := mapReader;
1461 generateExternalResourcesList(gCurrentMap);
1462 mapTextureList := gCurrentMap['texture'];
1463 // get all other lists here too
1464 panels := gCurrentMap['panel'];
1465 triggers := gCurrentMap['trigger'];
1466 items := gCurrentMap['item'];
1467 areas := gCurrentMap['area'];
1468 monsters := gCurrentMap['monster'];
1470 // Çàãðóçêà îïèñàíèÿ êàðòû:
1471 e_WriteLog(' Reading map info...', TMsgType.Notify);
1472 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1474 with gMapInfo do
1475 begin
1476 Name := gCurrentMap.MapName;
1477 Description := gCurrentMap.MapDesc;
1478 Author := gCurrentMap.MapAuthor;
1479 MusicName := gCurrentMap.MusicName;
1480 SkyName := gCurrentMap.SkyName;
1481 Height := gCurrentMap.Height;
1482 Width := gCurrentMap.Width;
1483 end;
1485 // Çàãðóçêà òåêñòóð:
1486 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1487 // Äîáàâëåíèå òåêñòóð â Textures[]:
1488 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1489 begin
1490 e_WriteLog(' Loading textures:', TMsgType.Notify);
1491 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1493 // find used textures
1494 usedTextures := THashStrInt.Create();
1495 try
1496 if (panels <> nil) and (panels.count > 0) then
1497 begin
1498 for rec in panels do
1499 begin
1500 texrec := rec.TextureRec;
1501 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1502 end;
1503 end;
1505 cnt := -1;
1506 for rec in mapTextureList do
1507 begin
1508 Inc(cnt);
1509 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1510 begin
1511 rec.tagInt := -1; // just in case
1512 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1513 end
1514 else
1515 begin
1516 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1517 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1518 {$ENDIF}
1519 ntn := CreateTexture(rec.Resource, FileName, True);
1520 if ntn < 0 then
1521 begin
1522 if rec.Anim then
1523 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]))
1524 else
1525 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1526 ntn := CreateNullTexture(rec.Resource)
1527 end;
1528 rec.tagInt := ntn; // remember texture number
1529 end;
1530 g_Game_StepLoading();
1531 end;
1532 finally
1533 usedTextures.Free();
1534 end;
1536 // set panel tagInt to texture index
1537 if (panels <> nil) then
1538 begin
1539 for rec in panels do
1540 begin
1541 texrec := rec.TextureRec;
1542 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1543 end;
1544 end;
1545 end;
1548 // Çàãðóçêà òðèããåðîâ
1549 gTriggerClientID := 0;
1550 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1551 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1553 // Çàãðóçêà ïàíåëåé
1554 e_WriteLog(' Loading panels...', TMsgType.Notify);
1555 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1557 // check texture numbers for panels
1558 if (panels <> nil) and (panels.count > 0) then
1559 begin
1560 for rec in panels do
1561 begin
1562 if (rec.tagInt < 0) then
1563 begin
1564 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1565 result := false;
1566 gCurrentMap.Free();
1567 gCurrentMap := nil;
1568 gCurrentMapFileName := '';
1569 exit;
1570 end;
1571 end;
1572 end;
1574 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1575 if (triggers <> nil) and (triggers.count > 0) then
1576 begin
1577 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1578 //SetLength(TriggersTable, triggers.count);
1579 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1581 SetLength(TriggersTable, triggers.count);
1582 trignum := -1;
1583 for rec in triggers do
1584 begin
1585 Inc(trignum);
1586 pttit := @TriggersTable[trignum];
1587 pttit.trigrec := rec;
1588 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1589 pttit.texPanelIdx := -1; // will be fixed later
1590 pttit.texPanel := rec.TexturePanelRec;
1591 // action panel
1592 pttit.actPanelIdx := -1;
1593 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1594 // set flag
1595 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1596 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1597 // update progress
1598 g_Game_StepLoading();
1599 end;
1600 end;
1602 // Ñîçäàåì ïàíåëè
1603 if (panels <> nil) and (panels.count > 0) then
1604 begin
1605 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1606 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1608 pannum := -1;
1609 for rec in panels do
1610 begin
1611 Inc(pannum);
1612 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1613 texrec := nil;
1614 SetLength(AddTextures, 0);
1615 CurTex := -1;
1616 ok := false;
1618 if (mapTextureList <> nil) then
1619 begin
1620 texrec := rec.TextureRec;
1621 ok := (texrec <> nil);
1622 end;
1624 if ok then
1625 begin
1626 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1627 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1628 ok := false;
1629 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1630 begin
1631 if rec.userPanelTrigRef then
1632 begin
1633 // e_LogWritefln('trigref for panel %s', [pannum]);
1634 ok := True;
1635 end;
1636 end;
1637 end;
1639 if ok then
1640 begin
1641 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1642 s := texrec.Resource;
1644 // Ñïåö-òåêñòóðû çàïðåùåíû
1645 if g_Map_IsSpecialTexture(s) then
1646 begin
1647 ok := false
1648 end
1649 else
1650 begin
1651 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1652 ok := g_Texture_NumNameFindStart(s);
1653 end;
1655 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1656 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1657 if ok then
1658 begin
1659 k := NNF_NAME_BEFORE;
1660 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1661 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1662 begin
1663 k := g_Texture_NumNameFindNext(TexName);
1665 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1666 begin
1667 ok := CreateTexture(TexName, FileName, False) >= 0;
1669 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1670 if ok then
1671 begin
1673 for c := 0 to High(Textures) do
1674 begin
1675 if (Textures[c].TextureName = TexName) then
1676 begin
1677 SetLength(AddTextures, Length(AddTextures)+1);
1678 AddTextures[High(AddTextures)].Texture := c;
1679 break;
1680 end;
1681 end;
1683 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1684 begin
1685 SetLength(AddTextures, Length(AddTextures)+1);
1686 AddTextures[High(AddTextures)].Texture := c;
1687 end;
1688 end;
1689 end
1690 else
1691 begin
1692 if k = NNF_NAME_EQUALS then
1693 begin
1694 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1695 SetLength(AddTextures, Length(AddTextures)+1);
1696 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1697 CurTex := High(AddTextures);
1698 ok := true;
1699 end
1700 else // NNF_NO_NAME
1701 begin
1702 ok := false;
1703 end;
1704 end;
1705 end; // while ok...
1707 ok := true;
1708 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1709 end; // if ok - ññûëàþòñÿ òðèããåðû
1711 if not ok then
1712 begin
1713 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1714 SetLength(AddTextures, 1);
1715 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1716 CurTex := 0;
1717 end;
1719 //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);
1721 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1723 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
1724 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
1725 PanelID := CreatePanel(rec, AddTextures, CurTex);
1726 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
1727 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1729 // setup lifts
1730 moveSpeed := rec.moveSpeed;
1731 //moveStart := rec.moveStart;
1732 //moveEnd := rec.moveEnd;
1733 //moveActive := rec['move_active'].value;
1734 if not moveSpeed.isZero then
1735 begin
1736 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1737 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1738 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1739 end;
1741 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
1743 g_Game_StepLoading();
1744 end;
1745 end;
1747 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
1748 for b := 0 to High(TriggersTable) do
1749 begin
1750 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
1751 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
1752 end;
1754 // create map grid, init other grids (for monsters, for example)
1755 e_WriteLog('Creating map grid', TMsgType.Notify);
1756 mapCreateGrid();
1758 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
1759 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
1760 begin
1761 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
1762 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1763 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
1764 trignum := -1;
1765 for rec in triggers do
1766 begin
1767 Inc(trignum);
1768 tgpid := TriggersTable[trignum].actPanelIdx;
1769 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
1770 TriggersTable[trignum].tnum := trignum;
1771 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
1772 end;
1773 end;
1775 //FIXME: use hashtable!
1776 for pan in panByGUID do
1777 begin
1778 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
1779 begin
1780 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
1781 end;
1782 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
1783 begin
1784 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
1785 end;
1786 end;
1788 // Çàãðóçêà ïðåäìåòîâ
1789 e_WriteLog(' Loading items...', TMsgType.Notify);
1790 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1792 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
1793 if (items <> nil) and not gLoadGameMode then
1794 begin
1795 e_WriteLog(' Spawning items...', TMsgType.Notify);
1796 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1797 for rec in items do CreateItem(rec);
1798 end;
1800 // Çàãðóçêà îáëàñòåé
1801 e_WriteLog(' Loading areas...', TMsgType.Notify);
1802 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1804 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
1805 if areas <> nil then
1806 begin
1807 e_WriteLog(' Creating areas...', TMsgType.Notify);
1808 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1809 for rec in areas do CreateArea(rec);
1810 end;
1812 // Çàãðóçêà ìîíñòðîâ
1813 e_WriteLog(' Loading monsters...', TMsgType.Notify);
1814 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1816 gTotalMonsters := 0;
1818 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
1819 if (monsters <> nil) and not gLoadGameMode then
1820 begin
1821 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
1822 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1823 for rec in monsters do CreateMonster(rec);
1824 end;
1826 //gCurrentMap := mapReader; // this will be our current map now
1827 gCurrentMapFileName := Res;
1828 //mapReader := nil;
1830 // Çàãðóçêà íåáà
1831 gMapInfo.SkyFullName := '';
1832 if (gMapInfo.SkyName <> '') then
1833 begin
1834 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
1835 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1836 gMapInfo.SkyFullName := e_GetResourcePath(WadDirs, gMapInfo.SkyName, g_ExtractWadName(Res));
1837 end;
1839 // Çàãðóçêà ìóçûêè
1840 ok := False;
1841 if gMapInfo.MusicName <> '' then
1842 begin
1843 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
1844 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1846 s := e_GetResourcePath(WadDirs, gMapInfo.MusicName, g_ExtractWadName(Res));
1847 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1848 ok := True
1849 else
1850 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1851 end;
1853 // Îñòàëüíûå óñòàíâêè
1854 CreateDoorMap();
1855 CreateLiftMap();
1857 g_Items_Init();
1858 g_Weapon_Init();
1859 g_Monsters_Init();
1861 {$IFDEF ENABLE_GFX}
1862 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1863 if not gLoadGameMode then g_GFX_Init();
1864 {$ENDIF}
1866 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1867 mapTextureList := nil;
1868 panels := nil;
1869 items := nil;
1870 areas := nil;
1871 triggers := nil;
1872 TriggersTable := nil;
1873 AddTextures := nil;
1875 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1876 if ok and (not gLoadGameMode) then
1877 begin
1878 gMusic.SetByName(gMapInfo.MusicName);
1879 gMusic.Play();
1880 end
1881 else
1882 begin
1883 gMusic.SetByName('');
1884 end;
1886 stt := getTimeMicro()-stt;
1887 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
1888 mapOk := true;
1889 finally
1890 sfsGCEnable(); // enable releasing unused volumes
1891 //mapReader.Free();
1892 e_UnpressAllKeys; // why not?
1893 if not mapOk then
1894 begin
1895 gCurrentMap.Free();
1896 gCurrentMap := nil;
1897 gCurrentMapFileName := '';
1898 end;
1899 end;
1901 compactExtResList();
1902 e_WriteLog('Done loading map.', TMsgType.Notify);
1903 Result := True;
1904 end;
1907 function g_Map_GetMapInfo(Res: String): TMapInfo;
1908 var
1909 WAD: TWADFile;
1910 mapReader: TDynRecord;
1911 //Header: TMapHeaderRec_1;
1912 FileName: String;
1913 Data: Pointer;
1914 Len: Integer;
1915 begin
1916 FillChar(Result, SizeOf(Result), 0);
1917 FileName := g_ExtractWadName(Res);
1919 WAD := TWADFile.Create();
1920 if not WAD.ReadFile(FileName) then
1921 begin
1922 WAD.Free();
1923 Exit;
1924 end;
1926 //k8: it ignores path again
1927 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1928 begin
1929 WAD.Free();
1930 Exit;
1931 end;
1933 WAD.Free();
1935 try
1936 mapReader := g_Map_ParseMap(Data, Len);
1937 except
1938 mapReader := nil;
1939 FreeMem(Data);
1940 exit;
1941 end;
1943 FreeMem(Data);
1945 if (mapReader = nil) then exit;
1947 if (mapReader.Width > 0) and (mapReader.Height > 0) then
1948 begin
1949 Result.Name := mapReader.MapName;
1950 Result.Description := mapReader.MapDesc;
1951 Result.Map := Res;
1952 Result.Author := mapReader.MapAuthor;
1953 Result.Height := mapReader.Height;
1954 Result.Width := mapReader.Width;
1955 end
1956 else
1957 begin
1958 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1959 //ZeroMemory(@Header, SizeOf(Header));
1960 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1961 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1962 Result.Map := Res;
1963 Result.Author := '';
1964 Result.Height := 0;
1965 Result.Width := 0;
1966 end;
1968 mapReader.Free();
1969 end;
1971 function g_Map_GetMapsList(WADName: string): SSArray;
1972 var
1973 WAD: TWADFile;
1974 a: Integer;
1975 ResList: SSArray;
1976 begin
1977 Result := nil;
1978 WAD := TWADFile.Create();
1979 if not WAD.ReadFile(WADName) then
1980 begin
1981 WAD.Free();
1982 Exit;
1983 end;
1984 ResList := WAD.GetMapResources();
1985 if ResList <> nil then
1986 begin
1987 for a := 0 to High(ResList) do
1988 begin
1989 SetLength(Result, Length(Result)+1);
1990 Result[High(Result)] := ResList[a];
1991 end;
1992 end;
1993 WAD.Free();
1994 end;
1996 function g_Map_Exist(Res: string): Boolean;
1997 var
1998 WAD: TWADFile;
1999 FileName, mnn: string;
2000 ResList: SSArray;
2001 a: Integer;
2002 begin
2003 Result := False;
2005 FileName := addWadExtension(g_ExtractWadName(Res));
2007 WAD := TWADFile.Create;
2008 if not WAD.ReadFile(FileName) then
2009 begin
2010 WAD.Free();
2011 Exit;
2012 end;
2014 ResList := WAD.GetMapResources();
2015 WAD.Free();
2017 mnn := g_ExtractFileName(Res);
2018 if ResList <> nil then
2019 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2020 begin
2021 Result := True;
2022 Exit;
2023 end;
2024 end;
2026 procedure g_Map_Free(freeTextures: Boolean=true);
2028 procedure FreePanelArray(var panels: TPanelArray);
2029 var
2030 i: Integer;
2032 begin
2033 if panels <> nil then
2034 begin
2035 for i := 0 to High(panels) do
2036 panels[i].Free();
2037 panels := nil;
2038 end;
2039 end;
2041 begin
2042 {$IFDEF ENABLE_GFX}
2043 g_GFX_Free;
2044 {$ENDIF}
2045 g_Weapon_Free();
2046 g_Items_Free();
2047 g_Triggers_Free();
2048 g_Monsters_Free();
2050 RespawnPoints := nil;
2051 if FlagPoints[FLAG_RED] <> nil then
2052 begin
2053 Dispose(FlagPoints[FLAG_RED]);
2054 FlagPoints[FLAG_RED] := nil;
2055 end;
2056 if FlagPoints[FLAG_BLUE] <> nil then
2057 begin
2058 Dispose(FlagPoints[FLAG_BLUE]);
2059 FlagPoints[FLAG_BLUE] := nil;
2060 end;
2061 //DOMFlagPoints := nil;
2063 //gDOMFlags := nil;
2065 if (Length(gCurrentMapFileName) <> 0) then
2066 begin
2067 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2068 end
2069 else
2070 begin
2071 e_LogWritefln('g_Map_Free: no previous map.', []);
2072 end;
2074 if freeTextures then
2075 begin
2076 e_LogWritefln('g_Map_Free: clearing textures...', []);
2077 Textures := nil;
2078 TextNameHash.Free();
2079 TextNameHash := nil;
2080 BadTextNameHash.Free();
2081 BadTextNameHash := nil;
2082 gCurrentMapFileName := '';
2083 gCurrentMap.Free();
2084 gCurrentMap := nil;
2085 end;
2087 panByGUID := nil;
2089 FreePanelArray(gWalls);
2090 FreePanelArray(gRenderBackgrounds);
2091 FreePanelArray(gRenderForegrounds);
2092 FreePanelArray(gWater);
2093 FreePanelArray(gAcid1);
2094 FreePanelArray(gAcid2);
2095 FreePanelArray(gSteps);
2096 FreePanelArray(gLifts);
2097 FreePanelArray(gBlockMon);
2098 gMovingWallIds := nil;
2100 g_Game_StopAllSounds(False);
2101 gMusic.FreeSound();
2102 g_Sound_Delete(gMapInfo.MusicName);
2104 gMapInfo.Name := '';
2105 gMapInfo.Description := '';
2106 gMapInfo.MusicName := '';
2107 gMapInfo.Height := 0;
2108 gMapInfo.Width := 0;
2110 gDoorMap := nil;
2111 gLiftMap := nil;
2112 end;
2114 procedure g_Map_Update();
2115 var
2116 a, d, j: Integer;
2117 m: Word;
2118 s: String;
2119 b: Byte;
2121 procedure UpdatePanelArray(var panels: TPanelArray);
2122 var
2123 i: Integer;
2125 begin
2126 for i := 0 to High(panels) do panels[i].Update();
2127 end;
2129 begin
2130 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2132 UpdatePanelArray(gWalls);
2133 UpdatePanelArray(gRenderBackgrounds);
2134 UpdatePanelArray(gRenderForegrounds);
2135 UpdatePanelArray(gWater);
2136 UpdatePanelArray(gAcid1);
2137 UpdatePanelArray(gAcid2);
2138 UpdatePanelArray(gSteps);
2140 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2142 if gGameSettings.GameMode = GM_CTF then
2143 begin
2144 for a := FLAG_RED to FLAG_BLUE do
2145 begin
2146 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2147 begin
2148 with gFlags[a] do
2149 begin
2150 Obj.oldX := Obj.X;
2151 Obj.oldY := Obj.Y;
2153 m := g_Obj_Move(@Obj, True, True);
2155 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
2157 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2159 // Ñîïðîòèâëåíèå âîçäóõà
2160 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2162 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2163 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2164 begin
2165 g_Map_ResetFlag(a);
2166 gFlags[a].CaptureTime := 0;
2167 if a = FLAG_RED then
2168 s := _lc[I_PLAYER_FLAG_RED]
2169 else
2170 s := _lc[I_PLAYER_FLAG_BLUE];
2171 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2173 if (((gPlayer1 <> nil) and (((gPlayer1.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer1.Team = TEAM_BLUE) and (a = FLAG_BLUE))))
2174 or ((gPlayer2 <> nil) and (((gPlayer2.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer2.Team = TEAM_BLUE) and (a = FLAG_BLUE))))) then
2175 b := 0
2176 else
2177 b := 1;
2179 if not sound_ret_flag[b].IsPlaying() then
2180 sound_ret_flag[b].Play();
2182 if g_Game_IsNet then
2183 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2184 Continue;
2185 end;
2187 if Count > 0 then Count -= 1;
2189 // Èãðîê áåðåò ôëàã
2190 if gPlayers <> nil then
2191 begin
2192 j := Random(Length(gPlayers)) - 1;
2193 for d := 0 to High(gPlayers) do
2194 begin
2195 Inc(j);
2196 if j > High(gPlayers) then j := 0;
2197 if gPlayers[j] <> nil then
2198 begin
2199 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2200 begin
2201 if gPlayers[j].GetFlag(a) then Break;
2202 end;
2203 end;
2204 end;
2205 end;
2206 end;
2207 end;
2208 end;
2209 end;
2210 end;
2212 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2213 PanelType: Word; b1x3: Boolean=false): Boolean;
2214 var
2215 a, h: Integer;
2216 begin
2217 Result := False;
2219 if WordBool(PanelType and PANEL_WALL) then
2220 if gWalls <> nil then
2221 begin
2222 h := High(gWalls);
2224 for a := 0 to h do
2225 if gWalls[a].Enabled and
2226 g_Collide(X, Y, Width, Height,
2227 gWalls[a].X, gWalls[a].Y,
2228 gWalls[a].Width, gWalls[a].Height) then
2229 begin
2230 Result := True;
2231 Exit;
2232 end;
2233 end;
2235 if WordBool(PanelType and PANEL_WATER) then
2236 if gWater <> nil then
2237 begin
2238 h := High(gWater);
2240 for a := 0 to h do
2241 if g_Collide(X, Y, Width, Height,
2242 gWater[a].X, gWater[a].Y,
2243 gWater[a].Width, gWater[a].Height) then
2244 begin
2245 Result := True;
2246 Exit;
2247 end;
2248 end;
2250 if WordBool(PanelType and PANEL_ACID1) then
2251 if gAcid1 <> nil then
2252 begin
2253 h := High(gAcid1);
2255 for a := 0 to h do
2256 if g_Collide(X, Y, Width, Height,
2257 gAcid1[a].X, gAcid1[a].Y,
2258 gAcid1[a].Width, gAcid1[a].Height) then
2259 begin
2260 Result := True;
2261 Exit;
2262 end;
2263 end;
2265 if WordBool(PanelType and PANEL_ACID2) then
2266 if gAcid2 <> nil then
2267 begin
2268 h := High(gAcid2);
2270 for a := 0 to h do
2271 if g_Collide(X, Y, Width, Height,
2272 gAcid2[a].X, gAcid2[a].Y,
2273 gAcid2[a].Width, gAcid2[a].Height) then
2274 begin
2275 Result := True;
2276 Exit;
2277 end;
2278 end;
2280 if WordBool(PanelType and PANEL_STEP) then
2281 if gSteps <> nil then
2282 begin
2283 h := High(gSteps);
2285 for a := 0 to h do
2286 if g_Collide(X, Y, Width, Height,
2287 gSteps[a].X, gSteps[a].Y,
2288 gSteps[a].Width, gSteps[a].Height) then
2289 begin
2290 Result := True;
2291 Exit;
2292 end;
2293 end;
2295 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2296 if gLifts <> nil then
2297 begin
2298 h := High(gLifts);
2300 for a := 0 to h do
2301 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = LIFTTYPE_UP)) or
2302 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = LIFTTYPE_DOWN)) or
2303 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = LIFTTYPE_LEFT)) or
2304 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = LIFTTYPE_RIGHT))) and
2305 g_Collide(X, Y, Width, Height,
2306 gLifts[a].X, gLifts[a].Y,
2307 gLifts[a].Width, gLifts[a].Height) then
2308 begin
2309 Result := True;
2310 Exit;
2311 end;
2312 end;
2314 if WordBool(PanelType and PANEL_BLOCKMON) then
2315 if gBlockMon <> nil then
2316 begin
2317 h := High(gBlockMon);
2319 for a := 0 to h do
2320 if ( (not b1x3) or
2321 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2322 g_Collide(X, Y, Width, Height,
2323 gBlockMon[a].X, gBlockMon[a].Y,
2324 gBlockMon[a].Width, gBlockMon[a].Height) then
2325 begin
2326 Result := True;
2327 Exit;
2328 end;
2329 end;
2330 end;
2332 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2333 var
2334 texid: DWORD;
2336 function checkPanels (constref panels: TPanelArray): Boolean;
2337 var
2338 a: Integer;
2339 begin
2340 result := false;
2341 if panels = nil then exit;
2342 for a := 0 to High(panels) do
2343 begin
2344 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2345 begin
2346 result := true;
2347 texid := panels[a].GetTextureID();
2348 exit;
2349 end;
2350 end;
2351 end;
2353 begin
2354 texid := LongWord(TEXTURE_NONE);
2355 result := texid;
2356 if not checkPanels(gWater) then
2357 if not checkPanels(gAcid1) then
2358 if not checkPanels(gAcid2) then exit;
2359 result := texid;
2360 end;
2363 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2364 const
2365 SlowMask = GridTagLift or GridTagBlockMon;
2367 function checker (pan: TPanel; tag: Integer): Boolean;
2368 begin
2370 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2371 begin
2372 result := pan.Enabled;
2373 exit;
2374 end;
2377 if ((tag and GridTagLift) <> 0) then
2378 begin
2379 result :=
2380 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2381 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2382 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2383 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2384 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2385 exit;
2386 end;
2388 if ((tag and GridTagBlockMon) <> 0) then
2389 begin
2390 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2391 exit;
2392 end;
2394 // other shit
2395 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2396 result := true; // i found her!
2397 end;
2399 var
2400 tagmask: Integer = 0;
2401 mwit: PPanel;
2402 it: TPanelGrid.Iter;
2403 pan: TPanel;
2404 begin
2405 result := false;
2406 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2407 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2408 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2409 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2410 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2411 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2412 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2414 if (tagmask = 0) then exit; // just in case
2416 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2417 if gdbg_map_use_accel_coldet then
2418 begin
2419 if ((tagmask and SlowMask) <> 0) then
2420 begin
2421 // slow
2422 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2423 for mwit in it do
2424 begin
2425 pan := mwit^;
2426 if ((pan.tag and GridTagLift) <> 0) then
2427 begin
2428 result :=
2429 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2430 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2431 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2432 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2433 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2434 end
2435 else if ((pan.tag and GridTagBlockMon) <> 0) then
2436 begin
2437 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2438 end
2439 else
2440 begin
2441 // other shit
2442 result := true; // i found her!
2443 end;
2444 if (result) then break;
2445 end;
2446 end
2447 else
2448 begin
2449 // fast
2450 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2451 result := (it.length > 0);
2452 end;
2453 it.release();
2454 end
2455 else
2456 begin
2457 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2458 end;
2459 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2460 end;
2463 // returns `true` if we need to stop
2464 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2465 begin
2466 result := false;
2467 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2468 // check priorities
2469 case cctype of
2470 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2471 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2472 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2473 end;
2474 // collision?
2475 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2476 // yeah
2477 texid := pan.GetTextureID();
2478 // water? water has the highest priority, so stop right here
2479 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2480 // acid2?
2481 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2482 // acid1?
2483 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2484 end;
2486 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2487 var
2488 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2489 mwit: PPanel;
2490 it: TPanelGrid.Iter;
2491 begin
2492 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2493 if gdbg_map_use_accel_coldet then
2494 begin
2495 result := LongWord(TEXTURE_NONE);
2496 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2497 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2498 it.release();
2499 end
2500 else
2501 begin
2502 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2503 end;
2504 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2505 end;
2508 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2509 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2510 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2513 procedure g_Map_EnableWallGUID (pguid: Integer);
2514 var
2515 pan: TPanel;
2516 begin
2517 //pan := gWalls[ID];
2518 pan := g_Map_PanelByGUID(pguid);
2519 if (pan = nil) then exit;
2520 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2522 pan.Enabled := True;
2524 {$IFDEF ENABLE_GFX}
2525 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2526 {$ENDIF}
2528 mapGrid.proxyEnabled[pan.proxyId] := true;
2529 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2530 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2532 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2533 // mark platform as interesting
2534 pan.setDirty();
2536 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2537 //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);
2538 {$ENDIF}
2539 end;
2542 procedure g_Map_DisableWallGUID (pguid: Integer);
2543 var
2544 pan: TPanel;
2545 begin
2546 //pan := gWalls[ID];
2547 pan := g_Map_PanelByGUID(pguid);
2548 if (pan = nil) then exit;
2549 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2551 pan.Enabled := False;
2552 {$IFDEF ENABLE_GFX}
2553 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2554 {$ENDIF}
2556 mapGrid.proxyEnabled[pan.proxyId] := false;
2557 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2559 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2560 // mark platform as interesting
2561 pan.setDirty();
2563 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2564 //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);
2565 {$ENDIF}
2566 end;
2569 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
2570 var
2571 tp: TPanel;
2572 begin
2573 tp := g_Map_PanelByGUID(pguid);
2574 if (tp = nil) then exit;
2575 tp.NextTexture(AnimLoop);
2576 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid);
2577 end;
2580 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2581 var
2582 pan: TPanel;
2583 begin
2584 //pan := gLifts[ID];
2585 pan := g_Map_PanelByGUID(pguid);
2586 if (pan = nil) then exit;
2587 if not pan.isGLift then exit;
2589 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2591 with {gLifts[ID]} pan do
2592 begin
2593 LiftType := t;
2595 {$IFDEF ENABLE_GFX}
2596 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2597 //TODO: make separate lift tags, and change tag here
2598 case LiftType of
2599 LIFTTYPE_UP: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2600 LIFTTYPE_DOWN: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2601 LIFTTYPE_LEFT: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2602 LIFTTYPE_RIGHT: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2603 end;
2604 {$ENDIF}
2606 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2607 // mark platform as interesting
2608 pan.setDirty();
2609 end;
2610 end;
2613 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2614 var
2615 a: Integer;
2616 PointsArray: Array of TRespawnPoint;
2617 begin
2618 Result := False;
2619 SetLength(PointsArray, 0);
2621 if RespawnPoints = nil then
2622 Exit;
2624 for a := 0 to High(RespawnPoints) do
2625 if RespawnPoints[a].PointType = PointType then
2626 begin
2627 SetLength(PointsArray, Length(PointsArray)+1);
2628 PointsArray[High(PointsArray)] := RespawnPoints[a];
2629 end;
2631 if PointsArray = nil then
2632 Exit;
2634 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2635 Result := True;
2636 end;
2638 function g_Map_GetPointCount(PointType: Byte): Word;
2639 var
2640 a: Integer;
2641 begin
2642 Result := 0;
2644 if RespawnPoints = nil then
2645 Exit;
2647 for a := 0 to High(RespawnPoints) do
2648 if RespawnPoints[a].PointType = PointType then
2649 Result := Result + 1;
2650 end;
2652 function g_Map_GetRandomPointType(): Byte;
2653 begin
2654 if RespawnPoints = nil then
2655 Result := 255
2656 else
2657 Result := RespawnPoints[Random(Length(RespawnPoints))].PointType;
2658 end;
2660 function g_Map_HaveFlagPoints(): Boolean;
2661 begin
2662 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2663 end;
2665 procedure g_Map_ResetFlag(Flag: Byte);
2666 begin
2667 with gFlags[Flag] do
2668 begin
2669 Obj.X := -1000;
2670 Obj.Y := -1000;
2671 Obj.Vel.X := 0;
2672 Obj.Vel.Y := 0;
2673 Direction := TDirection.D_LEFT;
2674 State := FLAG_STATE_NONE;
2675 if FlagPoints[Flag] <> nil then
2676 begin
2677 Obj.X := FlagPoints[Flag]^.X;
2678 Obj.Y := FlagPoints[Flag]^.Y;
2679 Direction := FlagPoints[Flag]^.Direction;
2680 State := FLAG_STATE_NORMAL;
2681 end;
2682 Obj.oldX := Obj.X;
2683 Obj.oldY := Obj.Y;
2684 NeedSend := False; // the event will take care of this
2685 Count := -1;
2686 end;
2687 end;
2689 procedure g_Map_SaveState (st: TStream);
2690 var
2691 str: String;
2693 procedure savePanels ();
2694 var
2695 pan: TPanel;
2696 begin
2697 // Ñîõðàíÿåì ïàíåëè
2698 utils.writeInt(st, LongInt(Length(panByGUID)));
2699 for pan in panByGUID do pan.SaveState(st);
2700 end;
2702 procedure saveFlag (flag: PFlag);
2703 var
2704 b: Byte;
2705 begin
2706 utils.writeSign(st, 'FLAG');
2707 utils.writeInt(st, Byte(0)); // version
2708 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
2709 utils.writeInt(st, Byte(flag^.RespawnType));
2710 // Ñîñòîÿíèå ôëàãà
2711 utils.writeInt(st, Byte(flag^.State));
2712 // Íàïðàâëåíèå ôëàãà
2713 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
2714 utils.writeInt(st, Byte(b));
2715 // Îáúåêò ôëàãà
2716 Obj_SaveState(st, @flag^.Obj);
2717 end;
2719 begin
2720 savePanels();
2722 // Ñîõðàíÿåì ìóçûêó
2723 utils.writeSign(st, 'MUSI');
2724 utils.writeInt(st, Byte(0));
2725 // Íàçâàíèå ìóçûêè
2726 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2727 if gMusic.NoMusic then str := '' else str := gMusic.Name;
2728 utils.writeStr(st, str);
2729 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
2730 utils.writeInt(st, LongWord(gMusic.GetPosition()));
2731 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
2732 utils.writeBool(st, gMusic.SpecPause);
2734 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2735 utils.writeInt(st, LongInt(gTotalMonsters));
2736 ///// /////
2738 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2739 if (gGameSettings.GameMode = GM_CTF) then
2740 begin
2741 // Ôëàã Êðàñíîé êîìàíäû
2742 saveFlag(@gFlags[FLAG_RED]);
2743 // Ôëàã Ñèíåé êîìàíäû
2744 saveFlag(@gFlags[FLAG_BLUE]);
2745 end;
2746 ///// /////
2748 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2749 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2750 begin
2751 // Î÷êè Êðàñíîé êîìàíäû
2752 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Score));
2753 // Î÷êè Ñèíåé êîìàíäû
2754 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Score));
2755 end;
2756 ///// /////
2757 end;
2760 procedure g_Map_LoadState (st: TStream);
2761 var
2762 dw: DWORD;
2763 str: String;
2764 boo: Boolean;
2766 procedure loadPanels ();
2767 var
2768 pan: TPanel;
2769 begin
2770 // Çàãðóæàåì ïàíåëè
2771 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
2772 for pan in panByGUID do
2773 begin
2774 pan.LoadState(st);
2775 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
2776 end;
2777 end;
2779 procedure loadFlag (flag: PFlag);
2780 var
2781 b: Byte;
2782 begin
2783 // Ñèãíàòóðà ôëàãà
2784 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
2785 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
2786 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
2787 flag^.RespawnType := utils.readByte(st);
2788 // Ñîñòîÿíèå ôëàãà
2789 flag^.State := utils.readByte(st);
2790 // Íàïðàâëåíèå ôëàãà
2791 b := utils.readByte(st);
2792 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
2793 // Îáúåêò ôëàãà
2794 Obj_LoadState(@flag^.Obj, st);
2795 end;
2797 begin
2798 if (st = nil) then exit;
2800 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2801 loadPanels();
2802 ///// /////
2804 {$IFDEF ENABLE_GFX}
2805 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
2806 g_GFX_Init();
2807 {$ENDIF}
2809 //mapCreateGrid();
2811 ///// Çàãðóæàåì ìóçûêó: /////
2812 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
2813 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
2814 // Íàçâàíèå ìóçûêè
2815 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2816 str := utils.readStr(st);
2817 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
2818 dw := utils.readLongWord(st);
2819 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
2820 boo := utils.readBool(st);
2821 // Çàïóñêàåì ýòó ìóçûêó
2822 gMusic.SetByName(str);
2823 gMusic.SpecPause := boo;
2824 gMusic.Play();
2825 gMusic.Pause(true);
2826 gMusic.SetPosition(dw);
2827 ///// /////
2829 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2830 gTotalMonsters := utils.readLongInt(st);
2831 ///// /////
2833 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2834 if (gGameSettings.GameMode = GM_CTF) then
2835 begin
2836 // Ôëàã Êðàñíîé êîìàíäû
2837 loadFlag(@gFlags[FLAG_RED]);
2838 // Ôëàã Ñèíåé êîìàíäû
2839 loadFlag(@gFlags[FLAG_BLUE]);
2840 end;
2841 ///// /////
2843 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2844 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2845 begin
2846 // Î÷êè Êðàñíîé êîìàíäû
2847 gTeamStat[TEAM_RED].Score := utils.readSmallInt(st);
2848 // Î÷êè Ñèíåé êîìàíäû
2849 gTeamStat[TEAM_BLUE].Score := utils.readSmallInt(st);
2850 end;
2851 ///// /////
2852 end;
2855 // trace liquid, stepping by `dx` and `dy`
2856 // return last seen liquid coords, and `false` if we're started outside of the liquid
2857 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2858 const
2859 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2860 begin
2861 topx := x;
2862 topy := y;
2863 // started outside of the liquid?
2864 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2865 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
2866 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2867 result := true;
2868 while true do
2869 begin
2870 Inc(x, dx);
2871 Inc(y, dy);
2872 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2873 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
2874 topx := x;
2875 topy := y;
2876 end;
2877 end;
2880 begin
2881 DynWarningCB := mapWarningCB;
2882 end.