DEADSOFTWARE

game: remove unneded render imports
[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, g_textures,
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 end;
59 function g_Map_Load(Res: String): Boolean;
60 function g_Map_GetMapInfo(Res: String): TMapInfo;
61 function g_Map_GetMapsList(WADName: String): SSArray;
62 function g_Map_Exist(Res: String): Boolean;
63 procedure g_Map_Free(freeTextures: Boolean=true);
64 procedure g_Map_Update();
66 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
68 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
69 PanelType: Word; b1x3: Boolean=false): Boolean;
70 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
72 procedure g_Map_EnableWallGUID (pguid: Integer);
73 procedure g_Map_DisableWallGUID (pguid: Integer);
74 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
76 // HACK!!!
77 procedure g_Map_EnableWall_XXX (ID: DWORD);
78 procedure g_Map_DisableWall_XXX (ID: DWORD);
79 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
81 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
83 procedure g_Map_ReAdd_DieTriggers();
84 function g_Map_IsSpecialTexture(Texture: String): Boolean;
86 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
87 function g_Map_GetPointCount(PointType: Byte): Word;
88 function g_Map_GetRandomPointType(): Byte;
90 function g_Map_HaveFlagPoints(): Boolean;
92 procedure g_Map_ResetFlag(Flag: Byte);
94 procedure g_Map_SaveState (st: TStream);
95 procedure g_Map_LoadState (st: TStream);
97 // returns panel or nil
98 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
99 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
101 // returns panel or nil
102 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
103 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
105 type
106 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
108 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
109 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
111 // trace liquid, stepping by `dx` and `dy`
112 // return last seen liquid coords, and `false` if we're started outside of the liquid
113 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
116 // return `true` from `cb` to stop
117 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
119 procedure g_Map_NetSendInterestingPanels (); // yay!
122 procedure g_Map_ProfilersBegin ();
123 procedure g_Map_ProfilersEnd ();
126 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
129 function g_Map_MinX (): Integer; inline;
130 function g_Map_MinY (): Integer; inline;
131 function g_Map_MaxX (): Integer; inline;
132 function g_Map_MaxY (): Integer; inline;
134 const
135 NNF_NO_NAME = 0;
136 NNF_NAME_BEFORE = 1;
137 NNF_NAME_EQUALS = 2;
138 NNF_NAME_AFTER = 3;
140 function g_Texture_NumNameFindStart(name: String): Boolean;
141 function g_Texture_NumNameFindNext(var newName: String): Byte;
143 const
144 RESPAWNPOINT_PLAYER1 = 1;
145 RESPAWNPOINT_PLAYER2 = 2;
146 RESPAWNPOINT_DM = 3;
147 RESPAWNPOINT_RED = 4;
148 RESPAWNPOINT_BLUE = 5;
150 FLAG_NONE = 0;
151 FLAG_RED = 1;
152 FLAG_BLUE = 2;
153 FLAG_DOM = 3;
155 FLAG_STATE_NONE = 0;
156 FLAG_STATE_NORMAL = 1;
157 FLAG_STATE_DROPPED = 2;
158 FLAG_STATE_CAPTURED = 3;
159 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
160 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
162 FLAG_TIME = 720; // 20 seconds
164 SKY_STRETCH: Single = 1.5;
166 const
167 GridTagInvalid = 0;
169 (* draw order:
170 PANEL_BACK
171 PANEL_STEP
172 PANEL_WALL
173 PANEL_CLOSEDOOR
174 PANEL_ACID1
175 PANEL_ACID2
176 PANEL_WATER
177 PANEL_FORE
178 *)
179 // sorted by draw priority
180 GridTagBack = 1 shl 0; // gRenderBackgrounds
181 GridTagStep = 1 shl 1; // gSteps
182 GridTagWall = 1 shl 2; // gWalls
183 GridTagDoor = 1 shl 3; // gWalls
184 GridTagAcid1 = 1 shl 4; // gAcid1
185 GridTagAcid2 = 1 shl 5; // gAcid2
186 GridTagWater = 1 shl 6; // gWater
187 GridTagFore = 1 shl 7; // gRenderForegrounds
188 // the following are invisible
189 GridTagLift = 1 shl 8; // gLifts
190 GridTagBlockMon = 1 shl 9; // gBlockMon
192 GridTagSolid = (GridTagWall or GridTagDoor);
193 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
194 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
196 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
199 type
200 TBinHeapPanelDrawCmp = class
201 public
202 class function less (const a, b: TPanel): Boolean; inline;
203 end;
205 TBinHeapPanelDraw = specialize TBinaryHeapBase<TPanel, TBinHeapPanelDrawCmp>;
207 var
208 gWalls: TPanelArray;
209 gRenderBackgrounds: TPanelArray;
210 gRenderForegrounds: TPanelArray;
211 gWater, gAcid1, gAcid2: TPanelArray;
212 gSteps: TPanelArray;
213 gLifts: TPanelArray;
214 gBlockMon: TPanelArray;
215 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
216 //gDOMFlags: array of TFlag;
217 gMapInfo: TMapInfo;
218 gDoorMap: array of array of DWORD;
219 gLiftMap: array of array of DWORD;
220 gWADHash: TMD5Digest;
221 gExternalResources: array of TDiskFileInfo = nil;
222 gMovingWallIds: array of Integer = nil;
224 gdbg_map_use_accel_render: Boolean = true;
225 gdbg_map_use_accel_coldet: Boolean = true;
226 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
227 gDrawPanelList: TBinHeapPanelDraw = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
229 gCurrentMap: TDynRecord = nil;
230 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
231 gTestMap: String = '';
234 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
237 type
238 TPanelGrid = specialize TBodyGridBase<TPanel>;
240 var
241 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
243 var (* private state *)
244 Textures: TLevelTextureArray = nil;
246 implementation
248 uses
249 e_input, e_log, e_res, g_items, g_gfx, g_console,
250 g_weapons, g_game, g_sound, e_sound, CONFIG,
251 g_options, g_triggers, g_player,
252 Math, g_monsters, g_saveload, g_language, g_netmsg,
253 sfs, xstreams, hashtable, wadreader,
254 g_res_downloader;
256 const
257 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
258 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
259 FLAG_SIGNATURE = $47414C46; // 'FLAG'
262 // ////////////////////////////////////////////////////////////////////////// //
263 procedure mapWarningCB (const msg: AnsiString; line, col: Integer);
264 begin
265 if (line > 0) then
266 begin
267 e_LogWritefln('parse error at (%s,%s): %s', [line, col, msg], TMsgType.Warning);
268 end
269 else
270 begin
271 e_LogWritefln('parse error: %s', [msg], TMsgType.Warning);
272 end;
273 end;
276 // ////////////////////////////////////////////////////////////////////////// //
277 var
278 panByGUID: array of TPanel = nil;
281 // ////////////////////////////////////////////////////////////////////////// //
282 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
283 begin
284 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
285 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
286 end;
289 // return `true` from `cb` to stop
290 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
291 var
292 pan: TPanel;
293 begin
294 result := nil;
295 if not assigned(cb) then exit;
296 for pan in panByGUID do
297 begin
298 if cb(pan) then begin result := pan; exit; end;
299 end;
300 end;
303 procedure g_Map_NetSendInterestingPanels ();
304 var
305 pan: TPanel;
306 begin
307 if g_Game_IsServer and g_Game_IsNet then
308 begin
309 for pan in panByGUID do
310 begin
311 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
312 end;
313 end;
314 end;
317 // ////////////////////////////////////////////////////////////////////////// //
318 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
319 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
320 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
321 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
324 // ////////////////////////////////////////////////////////////////////////// //
325 var
326 dfmapdef: TDynMapDef = nil;
329 procedure loadMapDefinition ();
330 var
331 pr: TTextParser = nil;
332 st: TStream = nil;
333 WAD: TWADFile = nil;
334 begin
335 if (dfmapdef <> nil) then exit;
337 try
338 e_LogWritefln('parsing "mapdef.txt"...', []);
339 st := e_OpenResourceRO(DataDirs, 'mapdef.txt');
340 e_LogWritefln('found local "mapdef.txt"', []);
341 except
342 st := nil;
343 end;
345 if (st = nil) then
346 begin
347 WAD := TWADFile.Create();
348 if not WAD.ReadFile(GameWAD) then
349 begin
350 //raise Exception.Create('cannot load "game.wad"');
351 st := nil;
352 end
353 else
354 begin
355 st := WAD.openFileStream('mapdef.txt');
356 end;
357 end;
359 try
360 if (st = nil) then
361 begin
362 //raise Exception.Create('cannot open "mapdef.txt"');
363 e_LogWriteln('using default "mapdef.txt"...');
364 pr := TStrTextParser.Create(defaultMapDef);
365 end
366 else
367 begin
368 pr := TFileTextParser.Create(st);
369 end;
370 except on e: Exception do
371 begin
372 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
373 raise;
374 end;
375 end;
377 try
378 dfmapdef := TDynMapDef.Create(pr);
379 except
380 on e: TDynParseException do
381 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
382 on e: Exception do
383 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
384 end;
386 st.Free();
387 WAD.Free();
388 end;
391 // ////////////////////////////////////////////////////////////////////////// //
392 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
393 var
394 wst: TSFSMemoryChunkStream = nil;
395 begin
396 result := nil;
397 if (dataLen < 4) then exit;
399 if (dfmapdef = nil) then writeln('need to load mapdef');
400 loadMapDefinition();
401 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
403 wst := TSFSMemoryChunkStream.Create(data, dataLen);
404 try
405 result := dfmapdef.parseMap(wst);
406 except
407 on e: TDynParseException do
408 begin
409 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
410 wst.Free();
411 result := nil;
412 exit;
413 end;
414 on e: Exception do
415 begin
416 e_LogWritefln('ERROR: %s', [e.message]);
417 wst.Free();
418 result := nil;
419 exit;
420 end;
421 end;
423 //e_LogWriteln('map parsed.');
424 end;
427 // ////////////////////////////////////////////////////////////////////////// //
428 var
429 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
430 NNF_PureExt: String; // extension postfix
431 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
432 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
435 function g_Texture_NumNameFindStart(name: String): Boolean;
436 var
437 i, j: Integer;
439 begin
440 Result := False;
441 NNF_PureName := '';
442 NNF_PureExt := '';
443 NNF_FirstNum := -1;
444 NNF_CurrentNum := -1;
446 for i := Length(name) downto 1 do
447 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
448 begin
449 if i = Length(name) then
450 begin // Íåò öèôð â êîíöå ñòðîêè
451 Exit;
452 end
453 else
454 begin
455 j := i + 1;
456 while (j <= Length(name)) and (name[j] <> '.') do inc(j);
457 NNF_PureName := Copy(name, 1, i);
458 NNF_PureExt := Copy(name, j);
459 name := Copy(name, i + 1, j - i - 1);
460 Break;
461 end;
462 end;
464 // Íå ïåðåâåñòè â ÷èñëî:
465 if not TryStrToInt(name, NNF_FirstNum) then
466 Exit;
468 NNF_CurrentNum := 0;
470 Result := True;
471 end;
474 function g_Texture_NumNameFindNext(var newName: String): Byte;
475 begin
476 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
477 begin
478 newName := '';
479 Result := NNF_NO_NAME;
480 Exit;
481 end;
483 newName := NNF_PureName + IntToStr(NNF_CurrentNum) + NNF_PureExt;
485 if NNF_CurrentNum < NNF_FirstNum then
486 Result := NNF_NAME_BEFORE
487 else
488 if NNF_CurrentNum > NNF_FirstNum then
489 Result := NNF_NAME_AFTER
490 else
491 Result := NNF_NAME_EQUALS;
493 Inc(NNF_CurrentNum);
494 end;
497 // ////////////////////////////////////////////////////////////////////////// //
498 function panelTypeToTag (panelType: Word): Integer;
499 begin
500 case panelType of
501 PANEL_WALL: result := GridTagWall; // gWalls
502 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
503 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
504 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
505 PANEL_WATER: result := GridTagWater; // gWater
506 PANEL_ACID1: result := GridTagAcid1; // gAcid1
507 PANEL_ACID2: result := GridTagAcid2; // gAcid2
508 PANEL_STEP: result := GridTagStep; // gSteps
509 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
510 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
511 else result := GridTagInvalid;
512 end;
513 end;
516 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
517 begin
518 if (a.tag < b.tag) then begin result := true; exit; end;
519 if (a.tag > b.tag) then begin result := false; exit; end;
520 result := (a.arrIdx < b.arrIdx);
521 end;
523 var
524 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
525 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
526 RespawnPoints: array of TRespawnPoint;
527 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
528 //DOMFlagPoints: Array of TFlagPoint;
531 procedure g_Map_ProfilersBegin ();
532 begin
533 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
534 if (profMapCollision <> nil) then profMapCollision.mainBegin(g_profile_collision);
535 // create sections
536 if g_profile_collision and (profMapCollision <> nil) then
537 begin
538 profMapCollision.sectionBegin('*solids');
539 profMapCollision.sectionEnd();
540 profMapCollision.sectionBegin('liquids');
541 profMapCollision.sectionEnd();
542 end;
543 end;
545 procedure g_Map_ProfilersEnd ();
546 begin
547 if (profMapCollision <> nil) then profMapCollision.mainEnd();
548 end;
551 // wall index in `gWalls` or -1
552 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
553 var
554 ex, ey: Integer;
555 begin
556 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, (GridTagWall or GridTagDoor));
557 if (result <> nil) then
558 begin
559 if (hitx <> nil) then hitx^ := ex;
560 if (hity <> nil) then hity^ := ey;
561 end
562 else
563 begin
564 if (hitx <> nil) then hitx^ := x1;
565 if (hity <> nil) then hity^ := y1;
566 end;
567 end;
569 // returns panel or nil
570 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
571 var
572 ex, ey: Integer;
573 begin
574 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, tag);
575 if (result <> nil) then
576 begin
577 if (hitx <> nil) then hitx^ := ex;
578 if (hity <> nil) then hity^ := ey;
579 end
580 else
581 begin
582 if (hitx <> nil) then hitx^ := x1;
583 if (hity <> nil) then hity^ := y1;
584 end;
585 end;
588 function xxPanAtPointChecker (pan: TPanel; panelType: Word): Boolean; inline;
589 begin
590 if ((pan.tag and GridTagLift) <> 0) then
591 begin
592 // stop if the lift of the right type
593 result :=
594 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
595 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
596 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
597 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT)));
598 exit;
599 end;
600 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
601 end;
603 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
604 var
605 tagmask: Integer = 0;
606 mwit: PPanel;
607 it: TPanelGrid.Iter;
608 begin
609 result := false;
611 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
612 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
613 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
614 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
615 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
616 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
617 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
619 if (tagmask = 0) then exit;// just in case
621 if ((tagmask and GridTagLift) <> 0) then
622 begin
623 // slow
624 it := mapGrid.forEachAtPoint(x, y, tagmask);
625 for mwit in it do if (xxPanAtPointChecker(mwit^, PanelType)) then begin result := true; break; end;
626 end
627 else
628 begin
629 // fast
630 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true);
631 result := (it.length <> 0); // firsthit
632 end;
633 it.release();
634 end;
637 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
638 var
639 it: TPanelGrid.Iter;
640 begin
641 result := nil;
642 if (tagmask = 0) then exit;
643 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true); // firsthit
644 if (it.length <> 0) then result := it.first^;
645 it.release();
646 end;
649 function g_Map_IsSpecialTexture(Texture: String): Boolean;
650 begin
651 Result := (Texture = TEXTURE_NAME_WATER) or
652 (Texture = TEXTURE_NAME_ACID1) or
653 (Texture = TEXTURE_NAME_ACID2);
654 end;
656 procedure CreateDoorMap();
657 var
658 PanelArray: Array of record
659 X, Y: Integer;
660 Width, Height: Word;
661 Active: Boolean;
662 PanelID: DWORD;
663 end;
664 a, b, c, m, i, len: Integer;
665 ok: Boolean;
666 begin
667 if gWalls = nil then
668 Exit;
670 i := 0;
671 len := 128;
672 SetLength(PanelArray, len);
674 for a := 0 to High(gWalls) do
675 if gWalls[a].Door then
676 begin
677 PanelArray[i].X := gWalls[a].X;
678 PanelArray[i].Y := gWalls[a].Y;
679 PanelArray[i].Width := gWalls[a].Width;
680 PanelArray[i].Height := gWalls[a].Height;
681 PanelArray[i].Active := True;
682 PanelArray[i].PanelID := a;
684 i := i + 1;
685 if i = len then
686 begin
687 len := len + 128;
688 SetLength(PanelArray, len);
689 end;
690 end;
692 // Íåò äâåðåé:
693 if i = 0 then
694 begin
695 PanelArray := nil;
696 Exit;
697 end;
699 SetLength(gDoorMap, 0);
701 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
703 for a := 0 to i-1 do
704 if PanelArray[a].Active then
705 begin
706 PanelArray[a].Active := False;
707 m := Length(gDoorMap);
708 SetLength(gDoorMap, m+1);
709 SetLength(gDoorMap[m], 1);
710 gDoorMap[m, 0] := PanelArray[a].PanelID;
711 ok := True;
713 while ok do
714 begin
715 ok := False;
717 for b := 0 to i-1 do
718 if PanelArray[b].Active then
719 for c := 0 to High(gDoorMap[m]) do
720 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
721 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
722 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
723 PanelArray[b].Width, PanelArray[b].Height,
724 gWalls[gDoorMap[m, c]].X,
725 gWalls[gDoorMap[m, c]].Y,
726 gWalls[gDoorMap[m, c]].Width,
727 gWalls[gDoorMap[m, c]].Height) then
728 begin
729 PanelArray[b].Active := False;
730 SetLength(gDoorMap[m],
731 Length(gDoorMap[m])+1);
732 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
733 ok := True;
734 Break;
735 end;
736 end;
738 g_Game_StepLoading();
739 end;
741 PanelArray := nil;
742 end;
744 procedure CreateLiftMap();
745 var
746 PanelArray: Array of record
747 X, Y: Integer;
748 Width, Height: Word;
749 Active: Boolean;
750 end;
751 a, b, c, len, i, j: Integer;
752 ok: Boolean;
753 begin
754 if gLifts = nil then
755 Exit;
757 len := Length(gLifts);
758 SetLength(PanelArray, len);
760 for a := 0 to len-1 do
761 begin
762 PanelArray[a].X := gLifts[a].X;
763 PanelArray[a].Y := gLifts[a].Y;
764 PanelArray[a].Width := gLifts[a].Width;
765 PanelArray[a].Height := gLifts[a].Height;
766 PanelArray[a].Active := True;
767 end;
769 SetLength(gLiftMap, len);
770 i := 0;
772 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
774 for a := 0 to len-1 do
775 if PanelArray[a].Active then
776 begin
777 PanelArray[a].Active := False;
778 SetLength(gLiftMap[i], 32);
779 j := 0;
780 gLiftMap[i, j] := a;
781 ok := True;
783 while ok do
784 begin
785 ok := False;
786 for b := 0 to len-1 do
787 if PanelArray[b].Active then
788 for c := 0 to j do
789 if g_CollideAround(PanelArray[b].X,
790 PanelArray[b].Y,
791 PanelArray[b].Width,
792 PanelArray[b].Height,
793 PanelArray[gLiftMap[i, c]].X,
794 PanelArray[gLiftMap[i, c]].Y,
795 PanelArray[gLiftMap[i, c]].Width,
796 PanelArray[gLiftMap[i, c]].Height) then
797 begin
798 PanelArray[b].Active := False;
799 j := j+1;
800 if j > High(gLiftMap[i]) then
801 SetLength(gLiftMap[i],
802 Length(gLiftMap[i])+32);
804 gLiftMap[i, j] := b;
805 ok := True;
807 Break;
808 end;
809 end;
811 SetLength(gLiftMap[i], j+1);
812 i := i+1;
814 g_Game_StepLoading();
815 end;
817 SetLength(gLiftMap, i);
819 PanelArray := nil;
820 end;
822 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
823 var
824 len: Integer;
825 panels: ^TPanelArray;
826 pan: TPanel;
827 pguid: Integer;
828 begin
829 Result := -1;
831 case PanelRec.PanelType of
832 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
833 PANEL_BACK: panels := @gRenderBackgrounds;
834 PANEL_FORE: panels := @gRenderForegrounds;
835 PANEL_WATER: panels := @gWater;
836 PANEL_ACID1: panels := @gAcid1;
837 PANEL_ACID2: panels := @gAcid2;
838 PANEL_STEP: panels := @gSteps;
839 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
840 PANEL_BLOCKMON: panels := @gBlockMon;
841 else exit;
842 end;
844 len := Length(panels^);
845 SetLength(panels^, len+1);
847 pguid := Length(panByGUID);
848 SetLength(panByGUID, pguid+1); //FIXME!
849 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
850 assert(pguid >= 0);
851 assert(pguid < Length(panByGUID));
852 panByGUID[pguid] := pan;
853 panels^[len] := pan;
854 pan.arrIdx := len;
855 pan.proxyId := -1;
856 pan.tag := panelTypeToTag(PanelRec.PanelType);
858 PanelRec.user['panel_guid'] := pguid;
860 //result := len;
861 result := pguid;
862 end;
865 function CreateNullTexture(RecName: String): Integer;
866 begin
867 RecName := toLowerCase1251(RecName);
868 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
869 if TextNameHash.get(RecName, result) then exit; // i found her!
871 SetLength(Textures, Length(Textures) + 1);
872 Textures[High(Textures)].TextureName := RecName;
873 Textures[High(Textures)].FullName := '';
874 Result := High(Textures);
875 TextNameHash.put(RecName, result);
876 end;
879 function extractWadName (resourceName: string): string;
880 var
881 posN: Integer;
882 begin
883 posN := Pos(':', resourceName);
884 if posN > 0 then
885 Result:= Copy(resourceName, 0, posN-1)
886 else
887 Result := '';
888 end;
891 procedure addResToExternalResList (res: AnsiString);
892 var
893 uname: AnsiString;
894 f: Integer;
895 fi: TDiskFileInfo;
896 begin
897 if g_Game_IsClient or not g_Game_IsNet then exit;
898 if (length(res) = 0) then exit; // map wad
899 //res := extractWadName(res);
900 //if (length(res) = 0) then exit; // map wad
901 uname := toLowerCase1251(res);
902 // do not add duplicates
903 for f := 0 to High(gExternalResources) do
904 begin
905 if (gExternalResources[f].userName = uname) then exit;
906 end;
907 //writeln('***(000) addResToExternalResList: res=[', res, ']');
908 // add new resource
909 fi.userName := uname;
910 if not findFileCI(res) then exit;
911 //writeln('***(001) addResToExternalResList: res=[', res, ']');
912 fi.diskName := res;
913 if (not GetDiskFileInfo(res, fi)) then
914 begin
915 fi.tag := -1;
916 end
917 else
918 begin
919 //writeln('***(002) addResToExternalResList: res=[', res, ']');
920 fi.tag := 0; // non-zero means "cannot caclucate hash"
921 try
922 fi.hash := MD5File(fi.diskName);
923 except
924 fi.tag := -1;
925 end;
926 end;
927 //e_LogWritefln('addext: res=[%s]; uname=[%s]; diskName=[%s]', [res, fi.userName, fi.diskName]);
928 SetLength(gExternalResources, length(gExternalResources)+1);
929 gExternalResources[High(gExternalResources)] := fi;
930 end;
933 procedure compactExtResList ();
934 var
935 src, dest: Integer;
936 begin
937 src := 0;
938 dest := 0;
939 for src := 0 to High(gExternalResources) do
940 begin
941 if (gExternalResources[src].tag = 0) then
942 begin
943 // copy it
944 if (dest <> src) then gExternalResources[dest] := gExternalResources[src];
945 Inc(dest);
946 end;
947 end;
948 if (dest <> length(gExternalResources)) then SetLength(gExternalResources, dest);
949 end;
952 function GetReplacementWad (WadName: AnsiString): AnsiString;
953 begin
954 result := '';
955 if WadName <> '' then
956 begin
957 result := WadName;
958 if g_Game_IsClient then result := g_Res_FindReplacementWad(WadName);
959 if (result = WadName) then result := e_FindWad(WadDirs, result)
960 end;
961 end;
964 procedure generateExternalResourcesList (map: TDynRecord);
965 begin
966 SetLength(gExternalResources, 0);
967 addResToExternalResList(GetReplacementWad(g_ExtractWadName(map.MusicName)));
968 addResToExternalResList(GetReplacementWad(g_ExtractWadName(map.SkyName)));
969 end;
971 function CreateTexture (RecName: AnsiString; Map: String; log: Boolean): Integer;
972 var
973 HName: AnsiString;
974 WAD, WADz: TWADFile;
975 WADName, ResName: String;
976 ResData, ReszData: Pointer;
977 ResLen, ReszLen: Integer;
978 cfg: TConfig;
979 id: Integer;
980 begin
981 Result := -1;
982 HName := toLowerCase1251(RecName);
983 if (TextNameHash = nil) then
984 TextNameHash := THashStrInt.Create();
985 if TextNameHash.get(HName, Result) then
986 begin
987 // e_LogWritefln('CreateTexture: found loaded %s', [Result]);
988 end
989 else
990 begin
991 Result := -1;
992 if (BadTextNameHash = nil) or not BadTextNameHash.has(HName) then
993 begin
994 case RecName of
995 TEXTURE_NAME_WATER, TEXTURE_NAME_ACID1, TEXTURE_NAME_ACID2:
996 begin
997 SetLength(Textures, Length(Textures) + 1);
998 Textures[High(Textures)].FullName := RecName;
999 Textures[High(Textures)].TextureName := RecName;
1000 Result := High(Textures);
1001 TextNameHash.put(RecName, result);
1002 end
1003 else
1004 WADName := GetReplacementWad(g_ExtractWadName(RecName));
1005 if (WADName <> '') then
1006 addResToExternalResList(WADName);
1007 if WADName = '' then
1008 WADName := Map;
1009 ResName := g_ExtractFilePathName(RecName);
1010 WAD := TWADFile.Create();
1011 if WAD.ReadFile(WadName) then
1012 begin
1013 if WAD.GetResource(ResName, ResData, ResLen, log) then
1014 begin
1015 if IsWadData(ResData, ResLen) then
1016 begin
1017 WADz := TWADFile.Create();
1018 if WADz.ReadMemory(ResData, ResLen) then
1019 begin
1020 if WADz.GetResource('TEXT/ANIM', ReszData, ReszLen) then
1021 begin
1022 cfg := TConfig.CreateMem(ReszData, ReszLen);
1023 if cfg <> nil then
1024 begin
1025 SetLength(Textures, Length(Textures) + 1);
1026 Textures[High(Textures)].TextureName := RecName;
1027 Textures[High(Textures)].FullName := WadName + ':' + ResName;
1028 Textures[High(Textures)].FramesCount := cfg.ReadInt('', 'framecount', 0);
1029 Textures[High(Textures)].Speed := cfg.ReadInt('', 'waitcount', 0);
1030 Result := High(Textures);
1031 TextNameHash.put(HName, result);
1032 cfg.Free;
1033 end;
1034 FreeMem(ReszData);
1035 end
1036 end;
1037 WADz.Free;
1038 end
1039 else
1040 begin
1041 SetLength(Textures, Length(Textures) + 1);
1042 Textures[High(Textures)].FullName := WADName + ':' + ResName;
1043 Textures[High(Textures)].TextureName := RecName;
1044 Result := High(Textures);
1045 TextNameHash.put(HName, result);
1046 end;
1047 FreeMem(ResData);
1048 end
1049 end;
1050 WAD.Free;
1051 end
1052 end;
1053 end;
1054 if Result < 0 then
1055 begin
1056 if (BadTextNameHash = nil) then
1057 BadTextNameHash := THashStrInt.Create();
1058 if log and (not BadTextNameHash.get(HName, id)) then
1059 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1060 BadTextNameHash.put(HName, -1);
1061 end
1062 end;
1064 procedure CreateItem(Item: TDynRecord);
1065 begin
1066 if g_Game_IsClient then Exit;
1068 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1069 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1070 Exit;
1072 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1073 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1074 end;
1076 procedure CreateArea(Area: TDynRecord);
1077 var
1078 a: Integer;
1079 begin
1080 case Area.AreaType of
1081 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1082 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1083 begin
1084 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1085 with RespawnPoints[High(RespawnPoints)] do
1086 begin
1087 X := Area.X;
1088 Y := Area.Y;
1089 Direction := TDirection(Area.Direction);
1091 case Area.AreaType of
1092 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1093 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1094 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1095 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1096 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1097 end;
1098 end;
1099 end;
1101 AREA_REDFLAG, AREA_BLUEFLAG:
1102 begin
1103 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1105 if FlagPoints[a] <> nil then Exit;
1107 New(FlagPoints[a]);
1109 with FlagPoints[a]^ do
1110 begin
1111 X := Area.X-FLAGRECT.X;
1112 Y := Area.Y-FLAGRECT.Y;
1113 Direction := TDirection(Area.Direction);
1114 end;
1116 with gFlags[a] do
1117 begin
1118 Obj.Rect := FLAGRECT;
1119 g_Map_ResetFlag(a);
1120 end;
1121 end;
1123 AREA_DOMFLAG:
1124 begin
1125 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1126 with DOMFlagPoints[High(DOMFlagPoints)] do
1127 begin
1128 X := Area.X;
1129 Y := Area.Y;
1130 Direction := TDirection(Area.Direction);
1131 end;
1133 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1134 end;
1135 end;
1136 end;
1138 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1139 var
1140 _trigger: TTrigger;
1141 tp: TPanel;
1142 begin
1143 result := -1;
1144 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1146 with _trigger do
1147 begin
1148 mapId := Trigger.id;
1149 mapIndex := amapIdx;
1150 X := Trigger.X;
1151 Y := Trigger.Y;
1152 Width := Trigger.Width;
1153 Height := Trigger.Height;
1154 Enabled := Trigger.Enabled;
1155 TexturePanelGUID := atpanid;
1156 TriggerType := Trigger.TriggerType;
1157 ActivateType := Trigger.ActivateType;
1158 Keys := Trigger.Keys;
1159 trigPanelGUID := atrigpanid;
1160 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1161 if TexturePanelGUID <> -1 then
1162 begin
1163 tp := g_Map_PanelByGUID(TexturePanelGUID);
1164 if (tp <> nil) then tp.hasTexTrigger := True;
1165 end;
1166 end;
1168 result := Integer(g_Triggers_Create(_trigger, Trigger));
1169 end;
1171 procedure CreateMonster(monster: TDynRecord);
1172 var
1173 a: Integer;
1174 mon: TMonster;
1175 begin
1176 if g_Game_IsClient then Exit;
1178 if (gGameSettings.GameType = GT_SINGLE)
1179 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1180 begin
1181 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1183 if gTriggers <> nil then
1184 begin
1185 for a := 0 to High(gTriggers) do
1186 begin
1187 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1188 begin
1189 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1190 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1191 end;
1192 end;
1193 end;
1195 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1196 end;
1197 end;
1199 procedure g_Map_ReAdd_DieTriggers();
1201 function monsDieTrig (mon: TMonster): Boolean;
1202 var
1203 a: Integer;
1204 //tw: TStrTextWriter;
1205 begin
1206 result := false; // don't stop
1207 mon.ClearTriggers();
1208 for a := 0 to High(gTriggers) do
1209 begin
1210 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1211 begin
1212 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1214 tw := TStrTextWriter.Create();
1215 try
1216 gTriggers[a].trigData.writeTo(tw);
1217 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1218 finally
1219 tw.Free();
1220 end;
1222 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1223 end;
1224 end;
1225 end;
1227 begin
1228 if g_Game_IsClient then Exit;
1230 g_Mons_ForEach(monsDieTrig);
1231 end;
1233 procedure mapCreateGrid ();
1234 var
1235 mapX0: Integer = $3fffffff;
1236 mapY0: Integer = $3fffffff;
1237 mapX1: Integer = -$3fffffff;
1238 mapY1: Integer = -$3fffffff;
1240 procedure calcBoundingBox (constref panels: TPanelArray);
1241 var
1242 idx: Integer;
1243 pan: TPanel;
1244 begin
1245 for idx := 0 to High(panels) do
1246 begin
1247 pan := panels[idx];
1248 if not pan.visvalid then continue;
1249 if (pan.Width < 1) or (pan.Height < 1) then continue;
1250 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1251 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1252 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1253 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1254 end;
1255 end;
1257 procedure addPanelsToGrid (constref panels: TPanelArray);
1258 var
1259 idx: Integer;
1260 pan: TPanel;
1261 newtag: Integer;
1262 begin
1263 //tag := panelTypeToTag(tag);
1264 for idx := 0 to High(panels) do
1265 begin
1266 pan := panels[idx];
1267 if not pan.visvalid then continue;
1268 if (pan.proxyId <> -1) then
1269 begin
1270 {$IF DEFINED(D2F_DEBUG)}
1271 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);
1272 {$ENDIF}
1273 continue;
1274 end;
1275 case pan.PanelType of
1276 PANEL_WALL: newtag := GridTagWall;
1277 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1278 PANEL_BACK: newtag := GridTagBack;
1279 PANEL_FORE: newtag := GridTagFore;
1280 PANEL_WATER: newtag := GridTagWater;
1281 PANEL_ACID1: newtag := GridTagAcid1;
1282 PANEL_ACID2: newtag := GridTagAcid2;
1283 PANEL_STEP: newtag := GridTagStep;
1284 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1285 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1286 else continue; // oops
1287 end;
1288 pan.tag := newtag;
1290 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1291 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1292 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1293 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1295 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1296 begin
1297 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1298 end;
1300 {$ENDIF}
1301 end;
1302 end;
1304 begin
1305 mapGrid.Free();
1306 mapGrid := nil;
1308 calcBoundingBox(gWalls);
1309 calcBoundingBox(gRenderBackgrounds);
1310 calcBoundingBox(gRenderForegrounds);
1311 calcBoundingBox(gWater);
1312 calcBoundingBox(gAcid1);
1313 calcBoundingBox(gAcid2);
1314 calcBoundingBox(gSteps);
1315 calcBoundingBox(gLifts);
1316 calcBoundingBox(gBlockMon);
1318 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1320 if (mapX0 > 0) then mapX0 := 0;
1321 if (mapY0 > 0) then mapY0 := 0;
1323 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1324 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1326 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1327 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1329 addPanelsToGrid(gWalls);
1330 addPanelsToGrid(gRenderBackgrounds);
1331 addPanelsToGrid(gRenderForegrounds);
1332 addPanelsToGrid(gWater);
1333 addPanelsToGrid(gAcid1);
1334 addPanelsToGrid(gAcid2);
1335 addPanelsToGrid(gSteps);
1336 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1337 addPanelsToGrid(gBlockMon);
1339 mapGrid.dumpStats();
1341 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1342 end;
1345 function g_Map_Load(Res: String): Boolean;
1346 const
1347 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1348 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1349 type
1350 PTRec = ^TTRec;
1351 TTRec = record
1352 //TexturePanel: Integer;
1353 tnum: Integer;
1354 id: Integer;
1355 trigrec: TDynRecord;
1356 // texture pane;
1357 texPanelIdx: Integer;
1358 texPanel: TDynRecord;
1359 // "action" panel
1360 actPanelIdx: Integer;
1361 actPanel: TDynRecord;
1362 end;
1363 var
1364 WAD, TestWAD: TWADFile;
1365 //mapReader: TDynRecord = nil;
1366 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1367 panels: TDynField = nil; //TPanelsRec1Array;
1368 items: TDynField = nil; //TItemsRec1Array;
1369 monsters: TDynField = nil; //TMonsterRec1Array;
1370 areas: TDynField = nil; //TAreasRec1Array;
1371 triggers: TDynField = nil; //TTriggersRec1Array;
1372 b, c, k: Integer;
1373 PanelID: DWORD;
1374 AddTextures: TAddTextureArray;
1375 TriggersTable: array of TTRec;
1376 FileName, mapResName, TexName, s: AnsiString;
1377 Data: Pointer;
1378 Len: Integer;
1379 ok: Boolean;
1380 CurTex, ntn: Integer;
1381 rec, texrec: TDynRecord;
1382 pttit: PTRec;
1383 pannum, trignum, cnt, tgpid: Integer;
1384 stt: UInt64;
1385 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1386 //moveActive: Boolean;
1387 pan: TPanel;
1388 mapOk: Boolean = false;
1389 usedTextures: THashStrInt = nil; // key: mapTextureList
1390 begin
1391 mapGrid.Free();
1392 mapGrid := nil;
1393 TestWAD := nil;
1394 Data := nil;
1396 //gCurrentMap.Free();
1397 //gCurrentMap := nil;
1399 panByGUID := nil;
1401 Result := False;
1402 gMapInfo.Map := Res;
1403 TriggersTable := nil;
1404 //mapReader := nil;
1406 sfsGCDisable(); // temporary disable removing of temporary volumes
1407 try
1408 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1409 if (gCurrentMap = nil) then
1410 begin
1411 FileName := g_ExtractWadName(Res);
1412 e_LogWritefln('Loading map WAD [%s] (res=[%s])', [FileName, Res], TMsgType.Notify);
1413 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1415 WAD := TWADFile.Create();
1416 if not WAD.ReadFile(FileName) then
1417 begin
1418 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1419 WAD.Free();
1420 Exit;
1421 end;
1423 if gTestMap <> '' then
1424 begin
1425 s := g_ExtractWadName(gTestMap);
1426 TestWAD := TWADFile.Create();
1427 if not TestWAD.ReadFile(s) then
1428 begin
1429 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1430 TestWAD.Free();
1431 TestWAD := nil;
1432 end;
1433 end;
1435 if TestWAD <> nil then
1436 begin
1437 mapResName := g_ExtractFileName(gTestMap);
1438 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1439 begin
1440 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1441 Data := nil;
1442 end else
1443 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1444 TestWAD.Free();
1445 TestWAD := nil;
1446 end;
1448 if Data = nil then
1449 begin
1450 //k8: why loader ignores path here?
1451 mapResName := g_ExtractFileName(Res);
1452 if not WAD.GetMapResource(mapResName, Data, Len) then
1453 begin
1454 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1455 WAD.Free();
1456 Exit;
1457 end;
1458 end;
1460 WAD.Free();
1462 if (Len < 4) then
1463 begin
1464 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1465 FreeMem(Data);
1466 exit;
1467 end;
1469 // Çàãðóçêà êàðòû:
1470 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1471 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1473 stt := getTimeMicro();
1475 try
1476 gCurrentMap := g_Map_ParseMap(Data, Len);
1477 except
1478 gCurrentMap.Free();
1479 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1480 FreeMem(Data);
1481 gCurrentMapFileName := '';
1482 Exit;
1483 end;
1485 FreeMem(Data);
1487 if (gCurrentMap = nil) then
1488 begin
1489 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1490 gCurrentMapFileName := '';
1491 exit;
1492 end;
1493 end
1494 else
1495 begin
1496 stt := getTimeMicro();
1497 end;
1499 //gCurrentMap := mapReader;
1501 generateExternalResourcesList(gCurrentMap);
1502 mapTextureList := gCurrentMap['texture'];
1503 // get all other lists here too
1504 panels := gCurrentMap['panel'];
1505 triggers := gCurrentMap['trigger'];
1506 items := gCurrentMap['item'];
1507 areas := gCurrentMap['area'];
1508 monsters := gCurrentMap['monster'];
1510 // Çàãðóçêà îïèñàíèÿ êàðòû:
1511 e_WriteLog(' Reading map info...', TMsgType.Notify);
1512 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1514 with gMapInfo do
1515 begin
1516 Name := gCurrentMap.MapName;
1517 Description := gCurrentMap.MapDesc;
1518 Author := gCurrentMap.MapAuthor;
1519 MusicName := gCurrentMap.MusicName;
1520 SkyName := gCurrentMap.SkyName;
1521 Height := gCurrentMap.Height;
1522 Width := gCurrentMap.Width;
1523 end;
1525 // Çàãðóçêà òåêñòóð:
1526 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1527 // Äîáàâëåíèå òåêñòóð â Textures[]:
1528 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1529 begin
1530 e_WriteLog(' Loading textures:', TMsgType.Notify);
1531 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1533 // find used textures
1534 usedTextures := THashStrInt.Create();
1535 try
1536 if (panels <> nil) and (panels.count > 0) then
1537 begin
1538 for rec in panels do
1539 begin
1540 texrec := rec.TextureRec;
1541 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1542 end;
1543 end;
1545 cnt := -1;
1546 for rec in mapTextureList do
1547 begin
1548 Inc(cnt);
1549 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1550 begin
1551 rec.tagInt := -1; // just in case
1552 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1553 end
1554 else
1555 begin
1556 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1557 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1558 {$ENDIF}
1559 ntn := CreateTexture(rec.Resource, FileName, True);
1560 if ntn < 0 then
1561 begin
1562 if rec.Anim then
1563 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]))
1564 else
1565 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1566 ntn := CreateNullTexture(rec.Resource)
1567 end;
1568 rec.tagInt := ntn; // remember texture number
1569 end;
1570 g_Game_StepLoading();
1571 end;
1572 finally
1573 usedTextures.Free();
1574 end;
1576 // set panel tagInt to texture index
1577 if (panels <> nil) then
1578 begin
1579 for rec in panels do
1580 begin
1581 texrec := rec.TextureRec;
1582 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1583 end;
1584 end;
1585 end;
1588 // Çàãðóçêà òðèããåðîâ
1589 gTriggerClientID := 0;
1590 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1591 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1593 // Çàãðóçêà ïàíåëåé
1594 e_WriteLog(' Loading panels...', TMsgType.Notify);
1595 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1597 // check texture numbers for panels
1598 if (panels <> nil) and (panels.count > 0) then
1599 begin
1600 for rec in panels do
1601 begin
1602 if (rec.tagInt < 0) then
1603 begin
1604 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1605 result := false;
1606 gCurrentMap.Free();
1607 gCurrentMap := nil;
1608 gCurrentMapFileName := '';
1609 exit;
1610 end;
1611 end;
1612 end;
1614 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1615 if (triggers <> nil) and (triggers.count > 0) then
1616 begin
1617 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1618 //SetLength(TriggersTable, triggers.count);
1619 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1621 SetLength(TriggersTable, triggers.count);
1622 trignum := -1;
1623 for rec in triggers do
1624 begin
1625 Inc(trignum);
1626 pttit := @TriggersTable[trignum];
1627 pttit.trigrec := rec;
1628 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1629 pttit.texPanelIdx := -1; // will be fixed later
1630 pttit.texPanel := rec.TexturePanelRec;
1631 // action panel
1632 pttit.actPanelIdx := -1;
1633 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1634 // set flag
1635 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1636 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1637 // update progress
1638 g_Game_StepLoading();
1639 end;
1640 end;
1642 // Ñîçäàåì ïàíåëè
1643 if (panels <> nil) and (panels.count > 0) then
1644 begin
1645 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1646 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1648 pannum := -1;
1649 for rec in panels do
1650 begin
1651 Inc(pannum);
1652 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1653 texrec := nil;
1654 SetLength(AddTextures, 0);
1655 CurTex := -1;
1656 ok := false;
1658 if (mapTextureList <> nil) then
1659 begin
1660 texrec := rec.TextureRec;
1661 ok := (texrec <> nil);
1662 end;
1664 if ok then
1665 begin
1666 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1667 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1668 ok := false;
1669 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1670 begin
1671 if rec.userPanelTrigRef then
1672 begin
1673 // e_LogWritefln('trigref for panel %s', [pannum]);
1674 ok := True;
1675 end;
1676 end;
1677 end;
1679 if ok then
1680 begin
1681 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1682 s := texrec.Resource;
1684 // Ñïåö-òåêñòóðû çàïðåùåíû
1685 if g_Map_IsSpecialTexture(s) then
1686 begin
1687 ok := false
1688 end
1689 else
1690 begin
1691 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1692 ok := g_Texture_NumNameFindStart(s);
1693 end;
1695 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1696 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1697 if ok then
1698 begin
1699 k := NNF_NAME_BEFORE;
1700 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1701 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1702 begin
1703 k := g_Texture_NumNameFindNext(TexName);
1705 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1706 begin
1707 ok := CreateTexture(TexName, FileName, False) >= 0;
1709 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1710 if ok then
1711 begin
1713 for c := 0 to High(Textures) do
1714 begin
1715 if (Textures[c].TextureName = TexName) then
1716 begin
1717 SetLength(AddTextures, Length(AddTextures)+1);
1718 AddTextures[High(AddTextures)].Texture := c;
1719 break;
1720 end;
1721 end;
1723 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1724 begin
1725 SetLength(AddTextures, Length(AddTextures)+1);
1726 AddTextures[High(AddTextures)].Texture := c;
1727 end;
1728 end;
1729 end
1730 else
1731 begin
1732 if k = NNF_NAME_EQUALS then
1733 begin
1734 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1735 SetLength(AddTextures, Length(AddTextures)+1);
1736 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1737 CurTex := High(AddTextures);
1738 ok := true;
1739 end
1740 else // NNF_NO_NAME
1741 begin
1742 ok := false;
1743 end;
1744 end;
1745 end; // while ok...
1747 ok := true;
1748 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1749 end; // if ok - ññûëàþòñÿ òðèããåðû
1751 if not ok then
1752 begin
1753 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1754 SetLength(AddTextures, 1);
1755 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1756 CurTex := 0;
1757 end;
1759 //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);
1761 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1763 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
1764 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
1765 PanelID := CreatePanel(rec, AddTextures, CurTex);
1766 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
1767 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1769 // setup lifts
1770 moveSpeed := rec.moveSpeed;
1771 //moveStart := rec.moveStart;
1772 //moveEnd := rec.moveEnd;
1773 //moveActive := rec['move_active'].value;
1774 if not moveSpeed.isZero then
1775 begin
1776 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1777 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1778 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1779 end;
1781 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
1783 g_Game_StepLoading();
1784 end;
1785 end;
1787 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
1788 for b := 0 to High(TriggersTable) do
1789 begin
1790 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
1791 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
1792 end;
1794 // create map grid, init other grids (for monsters, for example)
1795 e_WriteLog('Creating map grid', TMsgType.Notify);
1796 mapCreateGrid();
1798 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
1799 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
1800 begin
1801 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
1802 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1803 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
1804 trignum := -1;
1805 for rec in triggers do
1806 begin
1807 Inc(trignum);
1808 tgpid := TriggersTable[trignum].actPanelIdx;
1809 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
1810 TriggersTable[trignum].tnum := trignum;
1811 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
1812 end;
1813 end;
1815 //FIXME: use hashtable!
1816 for pan in panByGUID do
1817 begin
1818 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
1819 begin
1820 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
1821 end;
1822 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
1823 begin
1824 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
1825 end;
1826 end;
1828 // Çàãðóçêà ïðåäìåòîâ
1829 e_WriteLog(' Loading items...', TMsgType.Notify);
1830 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1832 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
1833 if (items <> nil) and not gLoadGameMode then
1834 begin
1835 e_WriteLog(' Spawning items...', TMsgType.Notify);
1836 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1837 for rec in items do CreateItem(rec);
1838 end;
1840 // Çàãðóçêà îáëàñòåé
1841 e_WriteLog(' Loading areas...', TMsgType.Notify);
1842 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1844 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
1845 if areas <> nil then
1846 begin
1847 e_WriteLog(' Creating areas...', TMsgType.Notify);
1848 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1849 for rec in areas do CreateArea(rec);
1850 end;
1852 // Çàãðóçêà ìîíñòðîâ
1853 e_WriteLog(' Loading monsters...', TMsgType.Notify);
1854 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1856 gTotalMonsters := 0;
1858 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
1859 if (monsters <> nil) and not gLoadGameMode then
1860 begin
1861 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
1862 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1863 for rec in monsters do CreateMonster(rec);
1864 end;
1866 //gCurrentMap := mapReader; // this will be our current map now
1867 gCurrentMapFileName := Res;
1868 //mapReader := nil;
1870 // Çàãðóçêà íåáà
1871 gMapInfo.SkyFullName := '';
1872 if (gMapInfo.SkyName <> '') then
1873 begin
1874 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
1875 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1876 gMapInfo.SkyFullName := e_GetResourcePath(WadDirs, gMapInfo.SkyName, g_ExtractWadName(Res));
1877 end;
1879 // Çàãðóçêà ìóçûêè
1880 ok := False;
1881 if gMapInfo.MusicName <> '' then
1882 begin
1883 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
1884 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1886 s := e_GetResourcePath(WadDirs, gMapInfo.MusicName, g_ExtractWadName(Res));
1887 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1888 ok := True
1889 else
1890 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1891 end;
1893 // Îñòàëüíûå óñòàíâêè
1894 CreateDoorMap();
1895 CreateLiftMap();
1897 g_Items_Init();
1898 g_Weapon_Init();
1899 g_Monsters_Init();
1901 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1902 if not gLoadGameMode then g_GFX_Init();
1904 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1905 mapTextureList := nil;
1906 panels := nil;
1907 items := nil;
1908 areas := nil;
1909 triggers := nil;
1910 TriggersTable := nil;
1911 AddTextures := nil;
1913 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1914 if ok and (not gLoadGameMode) then
1915 begin
1916 gMusic.SetByName(gMapInfo.MusicName);
1917 gMusic.Play();
1918 end
1919 else
1920 begin
1921 gMusic.SetByName('');
1922 end;
1924 stt := getTimeMicro()-stt;
1925 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
1926 mapOk := true;
1927 finally
1928 sfsGCEnable(); // enable releasing unused volumes
1929 //mapReader.Free();
1930 e_UnpressAllKeys; // why not?
1931 if not mapOk then
1932 begin
1933 gCurrentMap.Free();
1934 gCurrentMap := nil;
1935 gCurrentMapFileName := '';
1936 end;
1937 end;
1939 compactExtResList();
1940 e_WriteLog('Done loading map.', TMsgType.Notify);
1941 Result := True;
1942 end;
1945 function g_Map_GetMapInfo(Res: String): TMapInfo;
1946 var
1947 WAD: TWADFile;
1948 mapReader: TDynRecord;
1949 //Header: TMapHeaderRec_1;
1950 FileName: String;
1951 Data: Pointer;
1952 Len: Integer;
1953 begin
1954 FillChar(Result, SizeOf(Result), 0);
1955 FileName := g_ExtractWadName(Res);
1957 WAD := TWADFile.Create();
1958 if not WAD.ReadFile(FileName) then
1959 begin
1960 WAD.Free();
1961 Exit;
1962 end;
1964 //k8: it ignores path again
1965 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1966 begin
1967 WAD.Free();
1968 Exit;
1969 end;
1971 WAD.Free();
1973 try
1974 mapReader := g_Map_ParseMap(Data, Len);
1975 except
1976 mapReader := nil;
1977 FreeMem(Data);
1978 exit;
1979 end;
1981 FreeMem(Data);
1983 if (mapReader = nil) then exit;
1985 if (mapReader.Width > 0) and (mapReader.Height > 0) then
1986 begin
1987 Result.Name := mapReader.MapName;
1988 Result.Description := mapReader.MapDesc;
1989 Result.Map := Res;
1990 Result.Author := mapReader.MapAuthor;
1991 Result.Height := mapReader.Height;
1992 Result.Width := mapReader.Width;
1993 end
1994 else
1995 begin
1996 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1997 //ZeroMemory(@Header, SizeOf(Header));
1998 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1999 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2000 Result.Map := Res;
2001 Result.Author := '';
2002 Result.Height := 0;
2003 Result.Width := 0;
2004 end;
2006 mapReader.Free();
2007 end;
2009 function g_Map_GetMapsList(WADName: string): SSArray;
2010 var
2011 WAD: TWADFile;
2012 a: Integer;
2013 ResList: SSArray;
2014 begin
2015 Result := nil;
2016 WAD := TWADFile.Create();
2017 if not WAD.ReadFile(WADName) then
2018 begin
2019 WAD.Free();
2020 Exit;
2021 end;
2022 ResList := WAD.GetMapResources();
2023 if ResList <> nil then
2024 begin
2025 for a := 0 to High(ResList) do
2026 begin
2027 SetLength(Result, Length(Result)+1);
2028 Result[High(Result)] := ResList[a];
2029 end;
2030 end;
2031 WAD.Free();
2032 end;
2034 function g_Map_Exist(Res: string): Boolean;
2035 var
2036 WAD: TWADFile;
2037 FileName, mnn: string;
2038 ResList: SSArray;
2039 a: Integer;
2040 begin
2041 Result := False;
2043 FileName := addWadExtension(g_ExtractWadName(Res));
2045 WAD := TWADFile.Create;
2046 if not WAD.ReadFile(FileName) then
2047 begin
2048 WAD.Free();
2049 Exit;
2050 end;
2052 ResList := WAD.GetMapResources();
2053 WAD.Free();
2055 mnn := g_ExtractFileName(Res);
2056 if ResList <> nil then
2057 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2058 begin
2059 Result := True;
2060 Exit;
2061 end;
2062 end;
2064 procedure g_Map_Free(freeTextures: Boolean=true);
2066 procedure FreePanelArray(var panels: TPanelArray);
2067 var
2068 i: Integer;
2070 begin
2071 if panels <> nil then
2072 begin
2073 for i := 0 to High(panels) do
2074 panels[i].Free();
2075 panels := nil;
2076 end;
2077 end;
2079 begin
2080 g_GFX_Free();
2081 g_Weapon_Free();
2082 g_Items_Free();
2083 g_Triggers_Free();
2084 g_Monsters_Free();
2086 RespawnPoints := nil;
2087 if FlagPoints[FLAG_RED] <> nil then
2088 begin
2089 Dispose(FlagPoints[FLAG_RED]);
2090 FlagPoints[FLAG_RED] := nil;
2091 end;
2092 if FlagPoints[FLAG_BLUE] <> nil then
2093 begin
2094 Dispose(FlagPoints[FLAG_BLUE]);
2095 FlagPoints[FLAG_BLUE] := nil;
2096 end;
2097 //DOMFlagPoints := nil;
2099 //gDOMFlags := nil;
2101 if (Length(gCurrentMapFileName) <> 0) then
2102 begin
2103 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2104 end
2105 else
2106 begin
2107 e_LogWritefln('g_Map_Free: no previous map.', []);
2108 end;
2110 if freeTextures then
2111 begin
2112 e_LogWritefln('g_Map_Free: clearing textures...', []);
2113 Textures := nil;
2114 TextNameHash.Free();
2115 TextNameHash := nil;
2116 BadTextNameHash.Free();
2117 BadTextNameHash := nil;
2118 gCurrentMapFileName := '';
2119 gCurrentMap.Free();
2120 gCurrentMap := nil;
2121 end;
2123 panByGUID := nil;
2125 FreePanelArray(gWalls);
2126 FreePanelArray(gRenderBackgrounds);
2127 FreePanelArray(gRenderForegrounds);
2128 FreePanelArray(gWater);
2129 FreePanelArray(gAcid1);
2130 FreePanelArray(gAcid2);
2131 FreePanelArray(gSteps);
2132 FreePanelArray(gLifts);
2133 FreePanelArray(gBlockMon);
2134 gMovingWallIds := nil;
2136 g_Game_StopAllSounds(False);
2137 gMusic.FreeSound();
2138 g_Sound_Delete(gMapInfo.MusicName);
2140 gMapInfo.Name := '';
2141 gMapInfo.Description := '';
2142 gMapInfo.MusicName := '';
2143 gMapInfo.Height := 0;
2144 gMapInfo.Width := 0;
2146 gDoorMap := nil;
2147 gLiftMap := nil;
2148 end;
2150 procedure g_Map_Update();
2151 var
2152 a, d, j: Integer;
2153 m: Word;
2154 s: String;
2155 b: Byte;
2157 procedure UpdatePanelArray(var panels: TPanelArray);
2158 var
2159 i: Integer;
2161 begin
2162 for i := 0 to High(panels) do panels[i].Update();
2163 end;
2165 begin
2166 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2168 UpdatePanelArray(gWalls);
2169 UpdatePanelArray(gRenderBackgrounds);
2170 UpdatePanelArray(gRenderForegrounds);
2171 UpdatePanelArray(gWater);
2172 UpdatePanelArray(gAcid1);
2173 UpdatePanelArray(gAcid2);
2174 UpdatePanelArray(gSteps);
2176 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2178 if gGameSettings.GameMode = GM_CTF then
2179 begin
2180 for a := FLAG_RED to FLAG_BLUE do
2181 begin
2182 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2183 begin
2184 with gFlags[a] do
2185 begin
2186 m := g_Obj_Move(@Obj, True, True);
2188 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2190 // Ñîïðîòèâëåíèå âîçäóõà
2191 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2193 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2194 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2195 begin
2196 g_Map_ResetFlag(a);
2197 gFlags[a].CaptureTime := 0;
2198 if a = FLAG_RED then
2199 s := _lc[I_PLAYER_FLAG_RED]
2200 else
2201 s := _lc[I_PLAYER_FLAG_BLUE];
2202 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2204 if (((gPlayer1 <> nil) and (((gPlayer1.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer1.Team = TEAM_BLUE) and (a = FLAG_BLUE))))
2205 or ((gPlayer2 <> nil) and (((gPlayer2.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer2.Team = TEAM_BLUE) and (a = FLAG_BLUE))))) then
2206 b := 0
2207 else
2208 b := 1;
2210 if not sound_ret_flag[b].IsPlaying() then
2211 sound_ret_flag[b].Play();
2213 if g_Game_IsNet then
2214 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2215 Continue;
2216 end;
2218 if Count > 0 then Count -= 1;
2220 // Èãðîê áåðåò ôëàã
2221 if gPlayers <> nil then
2222 begin
2223 j := Random(Length(gPlayers)) - 1;
2224 for d := 0 to High(gPlayers) do
2225 begin
2226 Inc(j);
2227 if j > High(gPlayers) then j := 0;
2228 if gPlayers[j] <> nil then
2229 begin
2230 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2231 begin
2232 if gPlayers[j].GetFlag(a) then Break;
2233 end;
2234 end;
2235 end;
2236 end;
2237 end;
2238 end;
2239 end;
2240 end;
2241 end;
2243 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2244 PanelType: Word; b1x3: Boolean=false): Boolean;
2245 var
2246 a, h: Integer;
2247 begin
2248 Result := False;
2250 if WordBool(PanelType and PANEL_WALL) then
2251 if gWalls <> nil then
2252 begin
2253 h := High(gWalls);
2255 for a := 0 to h do
2256 if gWalls[a].Enabled and
2257 g_Collide(X, Y, Width, Height,
2258 gWalls[a].X, gWalls[a].Y,
2259 gWalls[a].Width, gWalls[a].Height) then
2260 begin
2261 Result := True;
2262 Exit;
2263 end;
2264 end;
2266 if WordBool(PanelType and PANEL_WATER) then
2267 if gWater <> nil then
2268 begin
2269 h := High(gWater);
2271 for a := 0 to h do
2272 if g_Collide(X, Y, Width, Height,
2273 gWater[a].X, gWater[a].Y,
2274 gWater[a].Width, gWater[a].Height) then
2275 begin
2276 Result := True;
2277 Exit;
2278 end;
2279 end;
2281 if WordBool(PanelType and PANEL_ACID1) then
2282 if gAcid1 <> nil then
2283 begin
2284 h := High(gAcid1);
2286 for a := 0 to h do
2287 if g_Collide(X, Y, Width, Height,
2288 gAcid1[a].X, gAcid1[a].Y,
2289 gAcid1[a].Width, gAcid1[a].Height) then
2290 begin
2291 Result := True;
2292 Exit;
2293 end;
2294 end;
2296 if WordBool(PanelType and PANEL_ACID2) then
2297 if gAcid2 <> nil then
2298 begin
2299 h := High(gAcid2);
2301 for a := 0 to h do
2302 if g_Collide(X, Y, Width, Height,
2303 gAcid2[a].X, gAcid2[a].Y,
2304 gAcid2[a].Width, gAcid2[a].Height) then
2305 begin
2306 Result := True;
2307 Exit;
2308 end;
2309 end;
2311 if WordBool(PanelType and PANEL_STEP) then
2312 if gSteps <> nil then
2313 begin
2314 h := High(gSteps);
2316 for a := 0 to h do
2317 if g_Collide(X, Y, Width, Height,
2318 gSteps[a].X, gSteps[a].Y,
2319 gSteps[a].Width, gSteps[a].Height) then
2320 begin
2321 Result := True;
2322 Exit;
2323 end;
2324 end;
2326 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2327 if gLifts <> nil then
2328 begin
2329 h := High(gLifts);
2331 for a := 0 to h do
2332 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = LIFTTYPE_UP)) or
2333 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = LIFTTYPE_DOWN)) or
2334 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = LIFTTYPE_LEFT)) or
2335 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = LIFTTYPE_RIGHT))) and
2336 g_Collide(X, Y, Width, Height,
2337 gLifts[a].X, gLifts[a].Y,
2338 gLifts[a].Width, gLifts[a].Height) then
2339 begin
2340 Result := True;
2341 Exit;
2342 end;
2343 end;
2345 if WordBool(PanelType and PANEL_BLOCKMON) then
2346 if gBlockMon <> nil then
2347 begin
2348 h := High(gBlockMon);
2350 for a := 0 to h do
2351 if ( (not b1x3) or
2352 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2353 g_Collide(X, Y, Width, Height,
2354 gBlockMon[a].X, gBlockMon[a].Y,
2355 gBlockMon[a].Width, gBlockMon[a].Height) then
2356 begin
2357 Result := True;
2358 Exit;
2359 end;
2360 end;
2361 end;
2363 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2364 var
2365 texid: DWORD;
2367 function checkPanels (constref panels: TPanelArray): Boolean;
2368 var
2369 a: Integer;
2370 begin
2371 result := false;
2372 if panels = nil then exit;
2373 for a := 0 to High(panels) do
2374 begin
2375 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2376 begin
2377 result := true;
2378 texid := panels[a].GetTextureID();
2379 exit;
2380 end;
2381 end;
2382 end;
2384 begin
2385 texid := LongWord(TEXTURE_NONE);
2386 result := texid;
2387 if not checkPanels(gWater) then
2388 if not checkPanels(gAcid1) then
2389 if not checkPanels(gAcid2) then exit;
2390 result := texid;
2391 end;
2394 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2395 const
2396 SlowMask = GridTagLift or GridTagBlockMon;
2398 function checker (pan: TPanel; tag: Integer): Boolean;
2399 begin
2401 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2402 begin
2403 result := pan.Enabled;
2404 exit;
2405 end;
2408 if ((tag and GridTagLift) <> 0) then
2409 begin
2410 result :=
2411 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2412 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2413 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2414 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2415 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2416 exit;
2417 end;
2419 if ((tag and GridTagBlockMon) <> 0) then
2420 begin
2421 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2422 exit;
2423 end;
2425 // other shit
2426 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2427 result := true; // i found her!
2428 end;
2430 var
2431 tagmask: Integer = 0;
2432 mwit: PPanel;
2433 it: TPanelGrid.Iter;
2434 pan: TPanel;
2435 begin
2436 result := false;
2437 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2438 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2439 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2440 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2441 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2442 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2443 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2445 if (tagmask = 0) then exit; // just in case
2447 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2448 if gdbg_map_use_accel_coldet then
2449 begin
2450 if ((tagmask and SlowMask) <> 0) then
2451 begin
2452 // slow
2453 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2454 for mwit in it do
2455 begin
2456 pan := mwit^;
2457 if ((pan.tag and GridTagLift) <> 0) then
2458 begin
2459 result :=
2460 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2461 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2462 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2463 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2464 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2465 end
2466 else if ((pan.tag and GridTagBlockMon) <> 0) then
2467 begin
2468 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2469 end
2470 else
2471 begin
2472 // other shit
2473 result := true; // i found her!
2474 end;
2475 if (result) then break;
2476 end;
2477 end
2478 else
2479 begin
2480 // fast
2481 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2482 result := (it.length > 0);
2483 end;
2484 it.release();
2485 end
2486 else
2487 begin
2488 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2489 end;
2490 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2491 end;
2494 // returns `true` if we need to stop
2495 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2496 begin
2497 result := false;
2498 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2499 // check priorities
2500 case cctype of
2501 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2502 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2503 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2504 end;
2505 // collision?
2506 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2507 // yeah
2508 texid := pan.GetTextureID();
2509 // water? water has the highest priority, so stop right here
2510 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2511 // acid2?
2512 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2513 // acid1?
2514 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2515 end;
2517 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2518 var
2519 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2520 mwit: PPanel;
2521 it: TPanelGrid.Iter;
2522 begin
2523 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2524 if gdbg_map_use_accel_coldet then
2525 begin
2526 result := LongWord(TEXTURE_NONE);
2527 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2528 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2529 it.release();
2530 end
2531 else
2532 begin
2533 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2534 end;
2535 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2536 end;
2539 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2540 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2541 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2544 procedure g_Map_EnableWallGUID (pguid: Integer);
2545 var
2546 pan: TPanel;
2547 begin
2548 //pan := gWalls[ID];
2549 pan := g_Map_PanelByGUID(pguid);
2550 if (pan = nil) then exit;
2551 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2553 pan.Enabled := True;
2554 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
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 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2582 mapGrid.proxyEnabled[pan.proxyId] := false;
2583 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2585 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2586 // mark platform as interesting
2587 pan.setDirty();
2589 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2590 //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);
2591 {$ENDIF}
2592 end;
2595 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
2596 var
2597 tp: TPanel;
2598 begin
2599 tp := g_Map_PanelByGUID(pguid);
2600 if (tp = nil) then exit;
2601 tp.NextTexture(AnimLoop);
2602 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
2603 end;
2606 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2607 var
2608 pan: TPanel;
2609 begin
2610 //pan := gLifts[ID];
2611 pan := g_Map_PanelByGUID(pguid);
2612 if (pan = nil) then exit;
2613 if not pan.isGLift then exit;
2615 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2617 with {gLifts[ID]} pan do
2618 begin
2619 LiftType := t;
2621 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2622 //TODO: make separate lift tags, and change tag here
2624 case LiftType of
2625 LIFTTYPE_UP: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2626 LIFTTYPE_DOWN: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2627 LIFTTYPE_LEFT: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2628 LIFTTYPE_RIGHT: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2629 end;
2631 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2632 // mark platform as interesting
2633 pan.setDirty();
2634 end;
2635 end;
2638 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2639 var
2640 a: Integer;
2641 PointsArray: Array of TRespawnPoint;
2642 begin
2643 Result := False;
2644 SetLength(PointsArray, 0);
2646 if RespawnPoints = nil then
2647 Exit;
2649 for a := 0 to High(RespawnPoints) do
2650 if RespawnPoints[a].PointType = PointType then
2651 begin
2652 SetLength(PointsArray, Length(PointsArray)+1);
2653 PointsArray[High(PointsArray)] := RespawnPoints[a];
2654 end;
2656 if PointsArray = nil then
2657 Exit;
2659 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2660 Result := True;
2661 end;
2663 function g_Map_GetPointCount(PointType: Byte): Word;
2664 var
2665 a: Integer;
2666 begin
2667 Result := 0;
2669 if RespawnPoints = nil then
2670 Exit;
2672 for a := 0 to High(RespawnPoints) do
2673 if RespawnPoints[a].PointType = PointType then
2674 Result := Result + 1;
2675 end;
2677 function g_Map_GetRandomPointType(): Byte;
2678 begin
2679 if RespawnPoints = nil then
2680 Result := 255
2681 else
2682 Result := RespawnPoints[Random(Length(RespawnPoints))].PointType;
2683 end;
2685 function g_Map_HaveFlagPoints(): Boolean;
2686 begin
2687 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2688 end;
2690 procedure g_Map_ResetFlag(Flag: Byte);
2691 begin
2692 with gFlags[Flag] do
2693 begin
2694 Obj.X := -1000;
2695 Obj.Y := -1000;
2696 Obj.Vel.X := 0;
2697 Obj.Vel.Y := 0;
2698 Direction := TDirection.D_LEFT;
2699 State := FLAG_STATE_NONE;
2700 if FlagPoints[Flag] <> nil then
2701 begin
2702 Obj.X := FlagPoints[Flag]^.X;
2703 Obj.Y := FlagPoints[Flag]^.Y;
2704 Direction := FlagPoints[Flag]^.Direction;
2705 State := FLAG_STATE_NORMAL;
2706 end;
2707 Count := -1;
2708 end;
2709 end;
2711 procedure g_Map_SaveState (st: TStream);
2712 var
2713 str: String;
2715 procedure savePanels ();
2716 var
2717 pan: TPanel;
2718 begin
2719 // Ñîõðàíÿåì ïàíåëè
2720 utils.writeInt(st, LongInt(Length(panByGUID)));
2721 for pan in panByGUID do pan.SaveState(st);
2722 end;
2724 procedure saveFlag (flag: PFlag);
2725 var
2726 b: Byte;
2727 begin
2728 utils.writeSign(st, 'FLAG');
2729 utils.writeInt(st, Byte(0)); // version
2730 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
2731 utils.writeInt(st, Byte(flag^.RespawnType));
2732 // Ñîñòîÿíèå ôëàãà
2733 utils.writeInt(st, Byte(flag^.State));
2734 // Íàïðàâëåíèå ôëàãà
2735 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
2736 utils.writeInt(st, Byte(b));
2737 // Îáúåêò ôëàãà
2738 Obj_SaveState(st, @flag^.Obj);
2739 end;
2741 begin
2742 savePanels();
2744 // Ñîõðàíÿåì ìóçûêó
2745 utils.writeSign(st, 'MUSI');
2746 utils.writeInt(st, Byte(0));
2747 // Íàçâàíèå ìóçûêè
2748 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2749 if gMusic.NoMusic then str := '' else str := gMusic.Name;
2750 utils.writeStr(st, str);
2751 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
2752 utils.writeInt(st, LongWord(gMusic.GetPosition()));
2753 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
2754 utils.writeBool(st, gMusic.SpecPause);
2756 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2757 utils.writeInt(st, LongInt(gTotalMonsters));
2758 ///// /////
2760 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2761 if (gGameSettings.GameMode = GM_CTF) then
2762 begin
2763 // Ôëàã Êðàñíîé êîìàíäû
2764 saveFlag(@gFlags[FLAG_RED]);
2765 // Ôëàã Ñèíåé êîìàíäû
2766 saveFlag(@gFlags[FLAG_BLUE]);
2767 end;
2768 ///// /////
2770 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2771 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2772 begin
2773 // Î÷êè Êðàñíîé êîìàíäû
2774 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
2775 // Î÷êè Ñèíåé êîìàíäû
2776 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
2777 end;
2778 ///// /////
2779 end;
2782 procedure g_Map_LoadState (st: TStream);
2783 var
2784 dw: DWORD;
2785 str: String;
2786 boo: Boolean;
2788 procedure loadPanels ();
2789 var
2790 pan: TPanel;
2791 begin
2792 // Çàãðóæàåì ïàíåëè
2793 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
2794 for pan in panByGUID do
2795 begin
2796 pan.LoadState(st);
2797 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
2798 end;
2799 end;
2801 procedure loadFlag (flag: PFlag);
2802 var
2803 b: Byte;
2804 begin
2805 // Ñèãíàòóðà ôëàãà
2806 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
2807 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
2808 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
2809 flag^.RespawnType := utils.readByte(st);
2810 // Ñîñòîÿíèå ôëàãà
2811 flag^.State := utils.readByte(st);
2812 // Íàïðàâëåíèå ôëàãà
2813 b := utils.readByte(st);
2814 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
2815 // Îáúåêò ôëàãà
2816 Obj_LoadState(@flag^.Obj, st);
2817 end;
2819 begin
2820 if (st = nil) then exit;
2822 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2823 loadPanels();
2824 ///// /////
2826 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
2827 g_GFX_Init();
2828 //mapCreateGrid();
2830 ///// Çàãðóæàåì ìóçûêó: /////
2831 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
2832 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
2833 // Íàçâàíèå ìóçûêè
2834 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2835 str := utils.readStr(st);
2836 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
2837 dw := utils.readLongWord(st);
2838 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
2839 boo := utils.readBool(st);
2840 // Çàïóñêàåì ýòó ìóçûêó
2841 gMusic.SetByName(str);
2842 gMusic.SpecPause := boo;
2843 gMusic.Play();
2844 gMusic.Pause(true);
2845 gMusic.SetPosition(dw);
2846 ///// /////
2848 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2849 gTotalMonsters := utils.readLongInt(st);
2850 ///// /////
2852 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2853 if (gGameSettings.GameMode = GM_CTF) then
2854 begin
2855 // Ôëàã Êðàñíîé êîìàíäû
2856 loadFlag(@gFlags[FLAG_RED]);
2857 // Ôëàã Ñèíåé êîìàíäû
2858 loadFlag(@gFlags[FLAG_BLUE]);
2859 end;
2860 ///// /////
2862 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2863 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2864 begin
2865 // Î÷êè Êðàñíîé êîìàíäû
2866 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
2867 // Î÷êè Ñèíåé êîìàíäû
2868 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
2869 end;
2870 ///// /////
2871 end;
2874 // trace liquid, stepping by `dx` and `dy`
2875 // return last seen liquid coords, and `false` if we're started outside of the liquid
2876 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2877 const
2878 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2879 begin
2880 topx := x;
2881 topy := y;
2882 // started outside of the liquid?
2883 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2884 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
2885 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2886 result := true;
2887 while true do
2888 begin
2889 Inc(x, dx);
2890 Inc(y, dy);
2891 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2892 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
2893 topx := x;
2894 topy := y;
2895 end;
2896 end;
2899 begin
2900 DynWarningCB := mapWarningCB;
2901 end.