DEADSOFTWARE

did the same thing for binary heap
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 SysUtils, Classes,
24 e_graphics, g_basic, MAPDEF, g_textures,
25 g_phys, utils, g_panel, g_grid, md5, binheap, xprofiler, xparser, xdynrec;
27 type
28 TMapInfo = record
29 Map: String;
30 Name: String;
31 Description: String;
32 Author: String;
33 MusicName: String;
34 SkyName: String;
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 Animation: TAnimation;
57 Direction: TDirection;
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 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor); // unaccelerated
70 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
72 procedure g_Map_DrawBack(dx, dy: Integer);
73 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
74 PanelType: Word; b1x3: Boolean=false): Boolean;
75 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
77 procedure g_Map_EnableWallGUID (pguid: Integer);
78 procedure g_Map_DisableWallGUID (pguid: Integer);
79 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
81 // HACK!!!
82 procedure g_Map_EnableWall_XXX (ID: DWORD);
83 procedure g_Map_DisableWall_XXX (ID: DWORD);
84 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
86 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
88 procedure g_Map_ReAdd_DieTriggers();
89 function g_Map_IsSpecialTexture(Texture: String): Boolean;
91 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
92 function g_Map_GetPointCount(PointType: Byte): Word;
94 function g_Map_HaveFlagPoints(): Boolean;
96 procedure g_Map_ResetFlag(Flag: Byte);
97 procedure g_Map_DrawFlags();
99 procedure g_Map_SaveState (st: TStream);
100 procedure g_Map_LoadState (st: TStream);
102 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
104 // returns panel or nil
105 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
106 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
108 // returns panel or nil
109 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
110 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
112 type
113 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
115 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
116 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
118 // trace liquid, stepping by `dx` and `dy`
119 // return last seen liquid coords, and `false` if we're started outside of the liquid
120 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
123 // return `true` from `cb` to stop
124 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
126 procedure g_Map_NetSendInterestingPanels (); // yay!
129 procedure g_Map_ProfilersBegin ();
130 procedure g_Map_ProfilersEnd ();
133 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
136 function g_Map_MinX (): Integer; inline;
137 function g_Map_MinY (): Integer; inline;
138 function g_Map_MaxX (): Integer; inline;
139 function g_Map_MaxY (): Integer; inline;
141 const
142 NNF_NO_NAME = 0;
143 NNF_NAME_BEFORE = 1;
144 NNF_NAME_EQUALS = 2;
145 NNF_NAME_AFTER = 3;
147 function g_Texture_NumNameFindStart(name: String): Boolean;
148 function g_Texture_NumNameFindNext(var newName: String): Byte;
150 const
151 RESPAWNPOINT_PLAYER1 = 1;
152 RESPAWNPOINT_PLAYER2 = 2;
153 RESPAWNPOINT_DM = 3;
154 RESPAWNPOINT_RED = 4;
155 RESPAWNPOINT_BLUE = 5;
157 FLAG_NONE = 0;
158 FLAG_RED = 1;
159 FLAG_BLUE = 2;
160 FLAG_DOM = 3;
162 FLAG_STATE_NONE = 0;
163 FLAG_STATE_NORMAL = 1;
164 FLAG_STATE_DROPPED = 2;
165 FLAG_STATE_CAPTURED = 3;
166 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
167 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
169 FLAG_TIME = 720; // 20 seconds
171 SKY_STRETCH: Single = 1.5;
173 const
174 GridTagInvalid = 0;
176 (* draw order:
177 PANEL_BACK
178 PANEL_STEP
179 PANEL_WALL
180 PANEL_CLOSEDOOR
181 PANEL_ACID1
182 PANEL_ACID2
183 PANEL_WATER
184 PANEL_FORE
185 *)
186 // sorted by draw priority
187 GridTagBack = 1 shl 0; // gRenderBackgrounds
188 GridTagStep = 1 shl 1; // gSteps
189 GridTagWall = 1 shl 2; // gWalls
190 GridTagDoor = 1 shl 3; // gWalls
191 GridTagAcid1 = 1 shl 4; // gAcid1
192 GridTagAcid2 = 1 shl 5; // gAcid2
193 GridTagWater = 1 shl 6; // gWater
194 GridTagFore = 1 shl 7; // gRenderForegrounds
195 // the following are invisible
196 GridTagLift = 1 shl 8; // gLifts
197 GridTagBlockMon = 1 shl 9; // gBlockMon
199 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
200 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
202 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
205 type
206 TBinHeapPanelDrawCmp = class
207 public
208 class function less (const a, b: TPanel): Boolean; inline;
209 end;
211 TBinHeapPanelDraw = specialize TBinaryHeapBase<TPanel, TBinHeapPanelDrawCmp>;
213 var
214 gWalls: TPanelArray;
215 gRenderBackgrounds: TPanelArray;
216 gRenderForegrounds: TPanelArray;
217 gWater, gAcid1, gAcid2: TPanelArray;
218 gSteps: TPanelArray;
219 gLifts: TPanelArray;
220 gBlockMon: TPanelArray;
221 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
222 //gDOMFlags: array of TFlag;
223 gMapInfo: TMapInfo;
224 gBackSize: TDFPoint;
225 gDoorMap: array of array of DWORD;
226 gLiftMap: array of array of DWORD;
227 gWADHash: TMD5Digest;
228 BackID: DWORD = DWORD(-1);
229 gExternalResources: TStringList;
230 gMovingWallIds: array of Integer = nil;
232 gdbg_map_use_accel_render: Boolean = true;
233 gdbg_map_use_accel_coldet: Boolean = true;
234 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
235 gDrawPanelList: TBinHeapPanelDraw = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
237 gCurrentMap: TDynRecord = nil;
238 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
241 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
244 type
245 TPanelGrid = specialize TBodyGridBase<TPanel>;
247 var
248 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
251 implementation
253 uses
254 e_input, g_main, e_log, e_texture, g_items, g_gfx, g_console,
255 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
256 g_options, g_triggers, g_player,
257 Math, g_monsters, g_saveload, g_language, g_netmsg,
258 sfs, xstreams, hashtable, wadreader,
259 ImagingTypes, Imaging, ImagingUtility,
260 ImagingGif, ImagingNetworkGraphics;
262 const
263 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
264 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
265 FLAG_SIGNATURE = $47414C46; // 'FLAG'
268 // ////////////////////////////////////////////////////////////////////////// //
269 procedure mapWarningCB (const msg: AnsiString; line, col: Integer);
270 begin
271 if (line > 0) then
272 begin
273 e_LogWritefln('parse error at (%s,%s): %s', [line, col, msg], TMsgType.Warning);
274 end
275 else
276 begin
277 e_LogWritefln('parse error: %s', [msg], TMsgType.Warning);
278 end;
279 end;
282 // ////////////////////////////////////////////////////////////////////////// //
283 var
284 panByGUID: array of TPanel = nil;
287 // ////////////////////////////////////////////////////////////////////////// //
288 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
289 begin
290 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
291 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
292 end;
295 // return `true` from `cb` to stop
296 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
297 var
298 pan: TPanel;
299 begin
300 result := nil;
301 if not assigned(cb) then exit;
302 for pan in panByGUID do
303 begin
304 if cb(pan) then begin result := pan; exit; end;
305 end;
306 end;
309 procedure g_Map_NetSendInterestingPanels ();
310 var
311 pan: TPanel;
312 begin
313 if g_Game_IsServer and g_Game_IsNet then
314 begin
315 for pan in panByGUID do
316 begin
317 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
318 end;
319 end;
320 end;
323 // ////////////////////////////////////////////////////////////////////////// //
324 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
325 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
326 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
327 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
330 // ////////////////////////////////////////////////////////////////////////// //
331 var
332 dfmapdef: TDynMapDef = nil;
335 procedure loadMapDefinition ();
336 var
337 pr: TTextParser = nil;
338 st: TStream = nil;
339 WAD: TWADFile = nil;
340 begin
341 if (dfmapdef <> nil) then exit;
343 try
344 e_LogWritefln('parsing "mapdef.txt"...', []);
345 st := openDiskFileRO(DataDir+'mapdef.txt');
346 e_LogWritefln('found local "%smapdef.txt"', [DataDir]);
347 except
348 st := nil;
349 end;
351 if (st = nil) then
352 begin
353 WAD := TWADFile.Create();
354 if not WAD.ReadFile(GameWAD) then
355 begin
356 //raise Exception.Create('cannot load "game.wad"');
357 st := nil;
358 end
359 else
360 begin
361 st := WAD.openFileStream('mapdef.txt');
362 end;
363 end;
365 try
366 if (st = nil) then
367 begin
368 //raise Exception.Create('cannot open "mapdef.txt"');
369 e_LogWriteln('using default "mapdef.txt"...');
370 pr := TStrTextParser.Create(defaultMapDef);
371 end
372 else
373 begin
374 pr := TFileTextParser.Create(st);
375 end;
376 except on e: Exception do
377 begin
378 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
379 raise;
380 end;
381 end;
383 try
384 dfmapdef := TDynMapDef.Create(pr);
385 except
386 on e: TDynParseException do
387 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
388 on e: Exception do
389 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
390 end;
392 st.Free();
393 WAD.Free();
394 end;
397 // ////////////////////////////////////////////////////////////////////////// //
398 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
399 var
400 wst: TSFSMemoryChunkStream = nil;
401 begin
402 result := nil;
403 if (dataLen < 4) then exit;
405 if (dfmapdef = nil) then writeln('need to load mapdef');
406 loadMapDefinition();
407 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
409 wst := TSFSMemoryChunkStream.Create(data, dataLen);
410 try
411 result := dfmapdef.parseMap(wst);
412 except
413 on e: TDynParseException do
414 begin
415 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
416 wst.Free();
417 result := nil;
418 exit;
419 end;
420 on e: Exception do
421 begin
422 e_LogWritefln('ERROR: %s', [e.message]);
423 wst.Free();
424 result := nil;
425 exit;
426 end;
427 end;
429 //e_LogWriteln('map parsed.');
430 end;
433 // ////////////////////////////////////////////////////////////////////////// //
434 var
435 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
436 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
437 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
440 function g_Texture_NumNameFindStart(name: String): Boolean;
441 var
442 i: Integer;
444 begin
445 Result := False;
446 NNF_PureName := '';
447 NNF_FirstNum := -1;
448 NNF_CurrentNum := -1;
450 for i := Length(name) downto 1 do
451 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
452 begin
453 if i = Length(name) then
454 begin // Íåò öèôð â êîíöå ñòðîêè
455 Exit;
456 end
457 else
458 begin
459 NNF_PureName := Copy(name, 1, i);
460 Delete(name, 1, i);
461 Break;
462 end;
463 end;
465 // Íå ïåðåâåñòè â ÷èñëî:
466 if not TryStrToInt(name, NNF_FirstNum) then
467 Exit;
469 NNF_CurrentNum := 0;
471 Result := True;
472 end;
475 function g_Texture_NumNameFindNext(var newName: String): Byte;
476 begin
477 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
478 begin
479 newName := '';
480 Result := NNF_NO_NAME;
481 Exit;
482 end;
484 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
486 if NNF_CurrentNum < NNF_FirstNum then
487 Result := NNF_NAME_BEFORE
488 else
489 if NNF_CurrentNum > NNF_FirstNum then
490 Result := NNF_NAME_AFTER
491 else
492 Result := NNF_NAME_EQUALS;
494 Inc(NNF_CurrentNum);
495 end;
498 // ////////////////////////////////////////////////////////////////////////// //
499 function panelTypeToTag (panelType: Word): Integer;
500 begin
501 case panelType of
502 PANEL_WALL: result := GridTagWall; // gWalls
503 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
504 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
505 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
506 PANEL_WATER: result := GridTagWater; // gWater
507 PANEL_ACID1: result := GridTagAcid1; // gAcid1
508 PANEL_ACID2: result := GridTagAcid2; // gAcid2
509 PANEL_STEP: result := GridTagStep; // gSteps
510 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
511 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
512 else result := GridTagInvalid;
513 end;
514 end;
517 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
518 begin
519 if (a.tag < b.tag) then begin result := true; exit; end;
520 if (a.tag > b.tag) then begin result := false; exit; end;
521 result := (a.arrIdx < b.arrIdx);
522 end;
524 procedure dplClear ();
525 begin
526 if (gDrawPanelList = nil) then gDrawPanelList := TBinHeapPanelDraw.Create() else gDrawPanelList.clear();
527 end;
530 var
531 Textures: TLevelTextureArray = nil;
532 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
533 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
534 RespawnPoints: array of TRespawnPoint;
535 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
536 //DOMFlagPoints: Array of TFlagPoint;
539 procedure g_Map_ProfilersBegin ();
540 begin
541 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
542 profMapCollision.mainBegin(g_profile_collision);
543 // create sections
544 if g_profile_collision then
545 begin
546 profMapCollision.sectionBegin('*solids');
547 profMapCollision.sectionEnd();
548 profMapCollision.sectionBegin('liquids');
549 profMapCollision.sectionEnd();
550 end;
551 end;
553 procedure g_Map_ProfilersEnd ();
554 begin
555 if (profMapCollision <> nil) then profMapCollision.mainEnd();
556 end;
559 // wall index in `gWalls` or -1
560 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
561 var
562 ex, ey: Integer;
563 begin
564 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
565 if (result <> nil) then
566 begin
567 if (hitx <> nil) then hitx^ := ex;
568 if (hity <> nil) then hity^ := ey;
569 end
570 else
571 begin
572 if (hitx <> nil) then hitx^ := x1;
573 if (hity <> nil) then hity^ := y1;
574 end;
575 end;
577 // returns panel or nil
578 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
579 var
580 ex, ey: Integer;
581 begin
582 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
583 if (result <> nil) then
584 begin
585 if (hitx <> nil) then hitx^ := ex;
586 if (hity <> nil) then hity^ := ey;
587 end
588 else
589 begin
590 if (hitx <> nil) then hitx^ := x1;
591 if (hity <> nil) then hity^ := y1;
592 end;
593 end;
596 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
598 function checker (pan: TPanel; tag: Integer): Boolean;
599 begin
601 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
602 begin
603 result := pan.Enabled; // stop if wall is enabled
604 exit;
605 end;
608 if ((tag and GridTagLift) <> 0) then
609 begin
610 // stop if the lift of the right type
611 result :=
612 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
613 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
614 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
615 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
616 exit;
617 end;
619 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
620 end;
622 var
623 tagmask: Integer = 0;
624 begin
625 result := false;
627 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
628 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
629 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
630 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
631 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
632 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
633 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
635 if (tagmask = 0) then exit;// just in case
636 if ((tagmask and GridTagLift) <> 0) then
637 begin
638 // slow
639 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
640 end
641 else
642 begin
643 // fast
644 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
645 end;
646 end;
649 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
650 begin
651 result := nil;
652 if (tagmask = 0) then exit;
653 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
654 end;
657 function g_Map_IsSpecialTexture(Texture: String): Boolean;
658 begin
659 Result := (Texture = TEXTURE_NAME_WATER) or
660 (Texture = TEXTURE_NAME_ACID1) or
661 (Texture = TEXTURE_NAME_ACID2);
662 end;
664 procedure CreateDoorMap();
665 var
666 PanelArray: Array of record
667 X, Y: Integer;
668 Width, Height: Word;
669 Active: Boolean;
670 PanelID: DWORD;
671 end;
672 a, b, c, m, i, len: Integer;
673 ok: Boolean;
674 begin
675 if gWalls = nil then
676 Exit;
678 i := 0;
679 len := 128;
680 SetLength(PanelArray, len);
682 for a := 0 to High(gWalls) do
683 if gWalls[a].Door then
684 begin
685 PanelArray[i].X := gWalls[a].X;
686 PanelArray[i].Y := gWalls[a].Y;
687 PanelArray[i].Width := gWalls[a].Width;
688 PanelArray[i].Height := gWalls[a].Height;
689 PanelArray[i].Active := True;
690 PanelArray[i].PanelID := a;
692 i := i + 1;
693 if i = len then
694 begin
695 len := len + 128;
696 SetLength(PanelArray, len);
697 end;
698 end;
700 // Íåò äâåðåé:
701 if i = 0 then
702 begin
703 PanelArray := nil;
704 Exit;
705 end;
707 SetLength(gDoorMap, 0);
709 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
711 for a := 0 to i-1 do
712 if PanelArray[a].Active then
713 begin
714 PanelArray[a].Active := False;
715 m := Length(gDoorMap);
716 SetLength(gDoorMap, m+1);
717 SetLength(gDoorMap[m], 1);
718 gDoorMap[m, 0] := PanelArray[a].PanelID;
719 ok := True;
721 while ok do
722 begin
723 ok := False;
725 for b := 0 to i-1 do
726 if PanelArray[b].Active then
727 for c := 0 to High(gDoorMap[m]) do
728 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
729 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
730 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
731 PanelArray[b].Width, PanelArray[b].Height,
732 gWalls[gDoorMap[m, c]].X,
733 gWalls[gDoorMap[m, c]].Y,
734 gWalls[gDoorMap[m, c]].Width,
735 gWalls[gDoorMap[m, c]].Height) then
736 begin
737 PanelArray[b].Active := False;
738 SetLength(gDoorMap[m],
739 Length(gDoorMap[m])+1);
740 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
741 ok := True;
742 Break;
743 end;
744 end;
746 g_Game_StepLoading();
747 end;
749 PanelArray := nil;
750 end;
752 procedure CreateLiftMap();
753 var
754 PanelArray: Array of record
755 X, Y: Integer;
756 Width, Height: Word;
757 Active: Boolean;
758 end;
759 a, b, c, len, i, j: Integer;
760 ok: Boolean;
761 begin
762 if gLifts = nil then
763 Exit;
765 len := Length(gLifts);
766 SetLength(PanelArray, len);
768 for a := 0 to len-1 do
769 begin
770 PanelArray[a].X := gLifts[a].X;
771 PanelArray[a].Y := gLifts[a].Y;
772 PanelArray[a].Width := gLifts[a].Width;
773 PanelArray[a].Height := gLifts[a].Height;
774 PanelArray[a].Active := True;
775 end;
777 SetLength(gLiftMap, len);
778 i := 0;
780 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
782 for a := 0 to len-1 do
783 if PanelArray[a].Active then
784 begin
785 PanelArray[a].Active := False;
786 SetLength(gLiftMap[i], 32);
787 j := 0;
788 gLiftMap[i, j] := a;
789 ok := True;
791 while ok do
792 begin
793 ok := False;
794 for b := 0 to len-1 do
795 if PanelArray[b].Active then
796 for c := 0 to j do
797 if g_CollideAround(PanelArray[b].X,
798 PanelArray[b].Y,
799 PanelArray[b].Width,
800 PanelArray[b].Height,
801 PanelArray[gLiftMap[i, c]].X,
802 PanelArray[gLiftMap[i, c]].Y,
803 PanelArray[gLiftMap[i, c]].Width,
804 PanelArray[gLiftMap[i, c]].Height) then
805 begin
806 PanelArray[b].Active := False;
807 j := j+1;
808 if j > High(gLiftMap[i]) then
809 SetLength(gLiftMap[i],
810 Length(gLiftMap[i])+32);
812 gLiftMap[i, j] := b;
813 ok := True;
815 Break;
816 end;
817 end;
819 SetLength(gLiftMap[i], j+1);
820 i := i+1;
822 g_Game_StepLoading();
823 end;
825 SetLength(gLiftMap, i);
827 PanelArray := nil;
828 end;
830 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
831 var
832 len: Integer;
833 panels: ^TPanelArray;
834 pan: TPanel;
835 pguid: Integer;
836 begin
837 Result := -1;
839 case PanelRec.PanelType of
840 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
841 PANEL_BACK: panels := @gRenderBackgrounds;
842 PANEL_FORE: panels := @gRenderForegrounds;
843 PANEL_WATER: panels := @gWater;
844 PANEL_ACID1: panels := @gAcid1;
845 PANEL_ACID2: panels := @gAcid2;
846 PANEL_STEP: panels := @gSteps;
847 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
848 PANEL_BLOCKMON: panels := @gBlockMon;
849 else exit;
850 end;
852 len := Length(panels^);
853 SetLength(panels^, len+1);
855 pguid := Length(panByGUID);
856 SetLength(panByGUID, pguid+1); //FIXME!
857 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
858 assert(pguid >= 0);
859 assert(pguid < Length(panByGUID));
860 panByGUID[pguid] := pan;
861 panels^[len] := pan;
862 pan.arrIdx := len;
863 pan.proxyId := -1;
864 pan.tag := panelTypeToTag(PanelRec.PanelType);
866 PanelRec.user['panel_guid'] := pguid;
868 //result := len;
869 result := pguid;
870 end;
873 function CreateNullTexture(RecName: String): Integer;
874 begin
875 RecName := toLowerCase1251(RecName);
876 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
877 if TextNameHash.get(RecName, result) then exit; // i found her!
879 SetLength(Textures, Length(Textures)+1);
880 result := High(Textures);
882 with Textures[High(Textures)] do
883 begin
884 TextureName := RecName;
885 Width := 1;
886 Height := 1;
887 Anim := False;
888 TextureID := LongWord(TEXTURE_NONE);
889 end;
891 TextNameHash.put(RecName, result);
892 end;
895 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
896 var
897 WAD: TWADFile;
898 TextureData: Pointer;
899 WADName: String;
900 a, ResLength: Integer;
901 begin
902 RecName := toLowerCase1251(RecName);
903 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
904 if TextNameHash.get(RecName, result) then
905 begin
906 // i found her!
907 //e_LogWritefln('texture ''%s'' already loaded (%s)', [RecName, result]);
908 exit;
909 end;
911 Result := -1;
913 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
916 if Textures <> nil then
917 begin
918 for a := 0 to High(Textures) do
919 begin
920 if (Textures[a].TextureName = RecName) then
921 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
922 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
923 Result := a;
924 Exit;
925 end;
926 end;
927 end;
930 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
931 if (RecName = TEXTURE_NAME_WATER) or
932 (RecName = TEXTURE_NAME_ACID1) or
933 (RecName = TEXTURE_NAME_ACID2) then
934 begin
935 SetLength(Textures, Length(Textures)+1);
937 with Textures[High(Textures)] do
938 begin
939 TextureName := RecName;
940 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
941 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
942 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
944 Anim := False;
945 end;
947 result := High(Textures);
948 TextNameHash.put(RecName, result);
949 Exit;
950 end;
952 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
953 WADName := g_ExtractWadName(RecName);
955 WAD := TWADFile.Create();
957 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
959 WAD.ReadFile(WADName);
961 //txname := RecName;
963 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
964 begin
965 FreeMem(TextureData);
966 RecName := 'COMMON\ALIEN';
967 end;
970 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
971 begin
972 SetLength(Textures, Length(Textures)+1);
973 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
974 begin
975 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
976 SetLength(Textures, Length(Textures)-1);
977 result := -1;
978 Exit;
979 end;
980 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
981 FreeMem(TextureData);
982 Textures[High(Textures)].TextureName := RecName;
983 Textures[High(Textures)].Anim := False;
985 result := High(Textures);
986 TextNameHash.put(RecName, result);
987 end
988 else // Íåò òàêîãî ðåóñðñà â WAD'å
989 begin
990 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
991 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
992 if log and (not BadTextNameHash.get(RecName, a)) then
993 begin
994 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
995 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
996 end;
997 BadTextNameHash.put(RecName, -1);
998 end;
1000 WAD.Free();
1001 end;
1004 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
1005 var
1006 WAD: TWADFile;
1007 TextureWAD: PChar = nil;
1008 TextData: Pointer = nil;
1009 TextureData: Pointer = nil;
1010 cfg: TConfig = nil;
1011 WADName: String;
1012 ResLength: Integer;
1013 TextureResource: String;
1014 _width, _height, _framecount, _speed: Integer;
1015 _backanimation: Boolean;
1016 //imgfmt: string;
1017 ia: TDynImageDataArray = nil;
1018 f, c, frdelay, frloop: Integer;
1019 begin
1020 RecName := toLowerCase1251(RecName);
1021 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
1022 if TextNameHash.get(RecName, result) then
1023 begin
1024 // i found her!
1025 //e_LogWritefln('animated texture ''%s'' already loaded (%s)', [RecName, result]);
1026 exit;
1027 end;
1029 result := -1;
1031 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1033 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1034 if BadTextNameHash.get(RecName, f) then
1035 begin
1036 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1037 exit;
1038 end;
1040 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1041 WADName := g_ExtractWadName(RecName);
1043 WAD := TWADFile.Create();
1044 try
1045 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1047 WAD.ReadFile(WADName);
1049 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1050 begin
1051 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1052 if log and (not BadTextNameHash.get(RecName, f)) then
1053 begin
1054 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1055 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1056 end;
1057 BadTextNameHash.put(RecName, -1);
1058 exit;
1059 end;
1061 {TEST
1062 if WADName = Map then
1063 begin
1064 //FreeMem(TextureWAD);
1065 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1066 end;
1069 WAD.FreeWAD();
1071 if ResLength < 6 then
1072 begin
1073 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), TMsgType.Warning);
1074 BadTextNameHash.put(RecName, -1);
1075 exit;
1076 end;
1078 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1079 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1080 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1081 begin
1082 // íåò, ýòî ñóïåðìåí!
1083 if not WAD.ReadMemory(TextureWAD, ResLength) then
1084 begin
1085 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), TMsgType.Warning);
1086 BadTextNameHash.put(RecName, -1);
1087 exit;
1088 end;
1090 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1091 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1092 begin
1093 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), TMsgType.Warning);
1094 BadTextNameHash.put(RecName, -1);
1095 exit;
1096 end;
1098 cfg := TConfig.CreateMem(TextData, ResLength);
1100 TextureResource := cfg.ReadStr('', 'resource', '');
1101 if TextureResource = '' then
1102 begin
1103 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), TMsgType.Warning);
1104 BadTextNameHash.put(RecName, -1);
1105 exit;
1106 end;
1108 _width := cfg.ReadInt('', 'framewidth', 0);
1109 _height := cfg.ReadInt('', 'frameheight', 0);
1110 _framecount := cfg.ReadInt('', 'framecount', 0);
1111 _speed := cfg.ReadInt('', 'waitcount', 0);
1112 _backanimation := cfg.ReadBool('', 'backanimation', False);
1114 cfg.Free();
1115 cfg := nil;
1117 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1118 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1119 begin
1120 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), TMsgType.Warning);
1121 BadTextNameHash.put(RecName, -1);
1122 exit;
1123 end;
1125 WAD.Free();
1126 WAD := nil;
1128 SetLength(Textures, Length(Textures)+1);
1129 with Textures[High(Textures)] do
1130 begin
1131 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1132 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1133 begin
1134 TextureName := RecName;
1135 Width := _width;
1136 Height := _height;
1137 Anim := True;
1138 FramesCount := _framecount;
1139 Speed := _speed;
1140 result := High(Textures);
1141 TextNameHash.put(RecName, result);
1142 end
1143 else
1144 begin
1145 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1146 if log and (not BadTextNameHash.get(RecName, f)) then
1147 begin
1148 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1149 end;
1150 BadTextNameHash.put(RecName, -1);
1151 end;
1152 end;
1153 end
1154 else
1155 begin
1156 // try animated image
1158 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1159 if length(imgfmt) = 0 then
1160 begin
1161 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1162 exit;
1163 end;
1165 GlobalMetadata.ClearMetaItems();
1166 GlobalMetadata.ClearMetaItemsForSaving();
1167 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1168 begin
1169 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), TMsgType.Warning);
1170 BadTextNameHash.put(RecName, -1);
1171 exit;
1172 end;
1173 if length(ia) = 0 then
1174 begin
1175 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), TMsgType.Warning);
1176 BadTextNameHash.put(RecName, -1);
1177 exit;
1178 end;
1180 WAD.Free();
1181 WAD := nil;
1183 _width := ia[0].width;
1184 _height := ia[0].height;
1185 _framecount := length(ia);
1186 _speed := 1;
1187 _backanimation := false;
1188 frdelay := -1;
1189 frloop := -666;
1190 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1191 begin
1192 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1193 try
1194 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1195 frdelay := f;
1196 if f < 0 then f := 0;
1197 // rounding ;-)
1198 c := f mod 28;
1199 if c < 13 then c := 0 else c := 1;
1200 f := (f div 28)+c;
1201 if f < 1 then f := 1 else if f > 255 then f := 255;
1202 _speed := f;
1203 except
1204 end;
1205 end;
1206 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1207 begin
1208 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1209 try
1210 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1211 frloop := f;
1212 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1213 except
1214 end;
1215 end;
1216 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1217 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1218 f := ord(_backanimation);
1219 e_WriteLog(Format('Animated texture file "%s": %d frames (delay:%d; back:%d; frdelay:%d; frloop:%d), %dx%d', [RecName, length(ia), _speed, f, frdelay, frloop, _width, _height]), TMsgType.Notify);
1221 SetLength(Textures, Length(Textures)+1);
1222 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1223 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1224 begin
1225 Textures[High(Textures)].TextureName := RecName;
1226 Textures[High(Textures)].Width := _width;
1227 Textures[High(Textures)].Height := _height;
1228 Textures[High(Textures)].Anim := True;
1229 Textures[High(Textures)].FramesCount := length(ia);
1230 Textures[High(Textures)].Speed := _speed;
1231 result := High(Textures);
1232 TextNameHash.put(RecName, result);
1233 //writeln(' CREATED!');
1234 end
1235 else
1236 begin
1237 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1238 if log and (not BadTextNameHash.get(RecName, f)) then
1239 begin
1240 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), TMsgType.Warning);
1241 end;
1242 BadTextNameHash.put(RecName, -1);
1243 end;
1244 end;
1245 finally
1246 for f := 0 to High(ia) do FreeImage(ia[f]);
1247 WAD.Free();
1248 cfg.Free();
1249 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1250 if (TextData <> nil) then FreeMem(TextData);
1251 if (TextureData <> nil) then FreeMem(TextureData);
1252 end;
1253 end;
1255 procedure CreateItem(Item: TDynRecord);
1256 begin
1257 if g_Game_IsClient then Exit;
1259 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1260 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1261 Exit;
1263 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1264 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1265 end;
1267 procedure CreateArea(Area: TDynRecord);
1268 var
1269 a: Integer;
1270 id: DWORD = 0;
1271 begin
1272 case Area.AreaType of
1273 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1274 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1275 begin
1276 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1277 with RespawnPoints[High(RespawnPoints)] do
1278 begin
1279 X := Area.X;
1280 Y := Area.Y;
1281 Direction := TDirection(Area.Direction);
1283 case Area.AreaType of
1284 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1285 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1286 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1287 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1288 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1289 end;
1290 end;
1291 end;
1293 AREA_REDFLAG, AREA_BLUEFLAG:
1294 begin
1295 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1297 if FlagPoints[a] <> nil then Exit;
1299 New(FlagPoints[a]);
1301 with FlagPoints[a]^ do
1302 begin
1303 X := Area.X-FLAGRECT.X;
1304 Y := Area.Y-FLAGRECT.Y;
1305 Direction := TDirection(Area.Direction);
1306 end;
1308 with gFlags[a] do
1309 begin
1310 case a of
1311 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1312 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1313 end;
1315 Animation := TAnimation.Create(id, True, 8);
1316 Obj.Rect := FLAGRECT;
1318 g_Map_ResetFlag(a);
1319 end;
1320 end;
1322 AREA_DOMFLAG:
1323 begin
1324 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1325 with DOMFlagPoints[High(DOMFlagPoints)] do
1326 begin
1327 X := Area.X;
1328 Y := Area.Y;
1329 Direction := TDirection(Area.Direction);
1330 end;
1332 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1333 end;
1334 end;
1335 end;
1337 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1338 var
1339 _trigger: TTrigger;
1340 begin
1341 result := -1;
1342 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1344 with _trigger do
1345 begin
1346 mapId := Trigger.id;
1347 mapIndex := amapIdx;
1348 X := Trigger.X;
1349 Y := Trigger.Y;
1350 Width := Trigger.Width;
1351 Height := Trigger.Height;
1352 Enabled := Trigger.Enabled;
1353 TexturePanelGUID := atpanid;
1354 TriggerType := Trigger.TriggerType;
1355 ActivateType := Trigger.ActivateType;
1356 Keys := Trigger.Keys;
1357 trigPanelGUID := atrigpanid;
1358 end;
1360 result := Integer(g_Triggers_Create(_trigger, Trigger));
1361 end;
1363 procedure CreateMonster(monster: TDynRecord);
1364 var
1365 a: Integer;
1366 mon: TMonster;
1367 begin
1368 if g_Game_IsClient then Exit;
1370 if (gGameSettings.GameType = GT_SINGLE)
1371 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1372 begin
1373 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1375 if gTriggers <> nil then
1376 begin
1377 for a := 0 to High(gTriggers) do
1378 begin
1379 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1380 begin
1381 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1382 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1383 end;
1384 end;
1385 end;
1387 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1388 end;
1389 end;
1391 procedure g_Map_ReAdd_DieTriggers();
1393 function monsDieTrig (mon: TMonster): Boolean;
1394 var
1395 a: Integer;
1396 //tw: TStrTextWriter;
1397 begin
1398 result := false; // don't stop
1399 mon.ClearTriggers();
1400 for a := 0 to High(gTriggers) do
1401 begin
1402 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1403 begin
1404 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1406 tw := TStrTextWriter.Create();
1407 try
1408 gTriggers[a].trigData.writeTo(tw);
1409 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1410 finally
1411 tw.Free();
1412 end;
1414 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1415 end;
1416 end;
1417 end;
1419 begin
1420 if g_Game_IsClient then Exit;
1422 g_Mons_ForEach(monsDieTrig);
1423 end;
1425 function extractWadName(resourceName: string): string;
1426 var
1427 posN: Integer;
1428 begin
1429 posN := Pos(':', resourceName);
1430 if posN > 0 then
1431 Result:= Copy(resourceName, 0, posN-1)
1432 else
1433 Result := '';
1434 end;
1436 procedure addResToExternalResList(res: string);
1437 begin
1438 res := extractWadName(res);
1439 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1440 gExternalResources.Add(res);
1441 end;
1443 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1444 //var
1445 //textures: TTexturesRec1Array;
1446 //textures: TDynField;
1447 //trec: TDynRecord;
1448 //mapHeader: TMapHeaderRec_1;
1449 //i: integer;
1450 //resFile: String = '';
1451 begin
1452 if gExternalResources = nil then
1453 gExternalResources := TStringList.Create;
1455 gExternalResources.Clear;
1457 (*
1459 textures := GetTextures(map);
1460 for i := 0 to High(textures) do
1461 begin
1462 addResToExternalResList(resFile);
1463 end;
1466 textures := map['texture'];
1467 if (textures <> nil) then
1468 begin
1469 for trec in textures do
1470 begin
1471 addResToExternalResList(resFile);
1472 end;
1473 end;
1475 textures := nil;
1476 *)
1478 //mapHeader := GetMapHeader(map);
1480 addResToExternalResList(map.MusicName);
1481 addResToExternalResList(map.SkyName);
1482 end;
1485 procedure mapCreateGrid ();
1486 var
1487 mapX0: Integer = $3fffffff;
1488 mapY0: Integer = $3fffffff;
1489 mapX1: Integer = -$3fffffff;
1490 mapY1: Integer = -$3fffffff;
1492 procedure calcBoundingBox (constref panels: TPanelArray);
1493 var
1494 idx: Integer;
1495 pan: TPanel;
1496 begin
1497 for idx := 0 to High(panels) do
1498 begin
1499 pan := panels[idx];
1500 if not pan.visvalid then continue;
1501 if (pan.Width < 1) or (pan.Height < 1) then continue;
1502 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1503 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1504 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1505 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1506 end;
1507 end;
1509 procedure addPanelsToGrid (constref panels: TPanelArray);
1510 var
1511 idx: Integer;
1512 pan: TPanel;
1513 newtag: Integer;
1514 begin
1515 //tag := panelTypeToTag(tag);
1516 for idx := 0 to High(panels) do
1517 begin
1518 pan := panels[idx];
1519 if not pan.visvalid then continue;
1520 if (pan.proxyId <> -1) then
1521 begin
1522 {$IF DEFINED(D2F_DEBUG)}
1523 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);
1524 {$ENDIF}
1525 continue;
1526 end;
1527 case pan.PanelType of
1528 PANEL_WALL: newtag := GridTagWall;
1529 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1530 PANEL_BACK: newtag := GridTagBack;
1531 PANEL_FORE: newtag := GridTagFore;
1532 PANEL_WATER: newtag := GridTagWater;
1533 PANEL_ACID1: newtag := GridTagAcid1;
1534 PANEL_ACID2: newtag := GridTagAcid2;
1535 PANEL_STEP: newtag := GridTagStep;
1536 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1537 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1538 else continue; // oops
1539 end;
1540 pan.tag := newtag;
1542 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1543 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1544 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1545 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1547 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1548 begin
1549 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1550 end;
1552 {$ENDIF}
1553 end;
1554 end;
1556 begin
1557 mapGrid.Free();
1558 mapGrid := nil;
1560 calcBoundingBox(gWalls);
1561 calcBoundingBox(gRenderBackgrounds);
1562 calcBoundingBox(gRenderForegrounds);
1563 calcBoundingBox(gWater);
1564 calcBoundingBox(gAcid1);
1565 calcBoundingBox(gAcid2);
1566 calcBoundingBox(gSteps);
1567 calcBoundingBox(gLifts);
1568 calcBoundingBox(gBlockMon);
1570 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1572 if (mapX0 > 0) then mapX0 := 0;
1573 if (mapY0 > 0) then mapY0 := 0;
1575 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1576 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1578 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1579 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1581 addPanelsToGrid(gWalls);
1582 addPanelsToGrid(gRenderBackgrounds);
1583 addPanelsToGrid(gRenderForegrounds);
1584 addPanelsToGrid(gWater);
1585 addPanelsToGrid(gAcid1);
1586 addPanelsToGrid(gAcid2);
1587 addPanelsToGrid(gSteps);
1588 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1589 addPanelsToGrid(gBlockMon);
1591 mapGrid.dumpStats();
1593 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1594 end;
1597 function g_Map_Load(Res: String): Boolean;
1598 const
1599 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1600 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1601 type
1602 PTRec = ^TTRec;
1603 TTRec = record
1604 //TexturePanel: Integer;
1605 tnum: Integer;
1606 id: Integer;
1607 trigrec: TDynRecord;
1608 // texture pane;
1609 texPanelIdx: Integer;
1610 texPanel: TDynRecord;
1611 // "action" panel
1612 actPanelIdx: Integer;
1613 actPanel: TDynRecord;
1614 end;
1615 var
1616 WAD: TWADFile;
1617 //mapReader: TDynRecord = nil;
1618 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1619 panels: TDynField = nil; //TPanelsRec1Array;
1620 items: TDynField = nil; //TItemsRec1Array;
1621 monsters: TDynField = nil; //TMonsterRec1Array;
1622 areas: TDynField = nil; //TAreasRec1Array;
1623 triggers: TDynField = nil; //TTriggersRec1Array;
1624 b, c, k: Integer;
1625 PanelID: DWORD;
1626 AddTextures: TAddTextureArray;
1627 TriggersTable: array of TTRec;
1628 FileName, mapResName, TexName, s: AnsiString;
1629 Data: Pointer;
1630 Len: Integer;
1631 ok, isAnim: Boolean;
1632 CurTex, ntn: Integer;
1633 rec, texrec: TDynRecord;
1634 pttit: PTRec;
1635 pannum, trignum, cnt, tgpid: Integer;
1636 stt: UInt64;
1637 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1638 //moveActive: Boolean;
1639 pan: TPanel;
1640 mapOk: Boolean = false;
1641 usedTextures: THashStrInt = nil; // key: mapTextureList
1642 begin
1643 mapGrid.Free();
1644 mapGrid := nil;
1646 //gCurrentMap.Free();
1647 //gCurrentMap := nil;
1649 panByGUID := nil;
1651 Result := False;
1652 gMapInfo.Map := Res;
1653 TriggersTable := nil;
1654 //mapReader := nil;
1656 sfsGCDisable(); // temporary disable removing of temporary volumes
1657 try
1658 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1659 if (gCurrentMap = nil) then
1660 begin
1661 FileName := g_ExtractWadName(Res);
1662 e_WriteLog('Loading map WAD: '+FileName, TMsgType.Notify);
1663 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1665 WAD := TWADFile.Create();
1666 if not WAD.ReadFile(FileName) then
1667 begin
1668 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1669 WAD.Free();
1670 Exit;
1671 end;
1673 //k8: why loader ignores path here?
1674 mapResName := g_ExtractFileName(Res);
1675 if not WAD.GetMapResource(mapResName, Data, Len) then
1676 begin
1677 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1678 WAD.Free();
1679 Exit;
1680 end;
1682 WAD.Free();
1684 if (Len < 4) then
1685 begin
1686 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1687 FreeMem(Data);
1688 exit;
1689 end;
1691 // Çàãðóçêà êàðòû:
1692 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1693 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1695 stt := getTimeMicro();
1697 try
1698 gCurrentMap := g_Map_ParseMap(Data, Len);
1699 except
1700 gCurrentMap.Free();
1701 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1702 FreeMem(Data);
1703 gCurrentMapFileName := '';
1704 Exit;
1705 end;
1707 FreeMem(Data);
1709 if (gCurrentMap = nil) then
1710 begin
1711 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1712 gCurrentMapFileName := '';
1713 exit;
1714 end;
1715 end
1716 else
1717 begin
1718 stt := getTimeMicro();
1719 end;
1721 //gCurrentMap := mapReader;
1723 generateExternalResourcesList(gCurrentMap);
1724 mapTextureList := gCurrentMap['texture'];
1725 // get all other lists here too
1726 panels := gCurrentMap['panel'];
1727 triggers := gCurrentMap['trigger'];
1728 items := gCurrentMap['item'];
1729 areas := gCurrentMap['area'];
1730 monsters := gCurrentMap['monster'];
1732 // Çàãðóçêà îïèñàíèÿ êàðòû:
1733 e_WriteLog(' Reading map info...', TMsgType.Notify);
1734 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1736 with gMapInfo do
1737 begin
1738 Name := gCurrentMap.MapName;
1739 Description := gCurrentMap.MapDesc;
1740 Author := gCurrentMap.MapAuthor;
1741 MusicName := gCurrentMap.MusicName;
1742 SkyName := gCurrentMap.SkyName;
1743 Height := gCurrentMap.Height;
1744 Width := gCurrentMap.Width;
1745 end;
1747 // Çàãðóçêà òåêñòóð:
1748 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1749 // Äîáàâëåíèå òåêñòóð â Textures[]:
1750 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1751 begin
1752 e_WriteLog(' Loading textures:', TMsgType.Notify);
1753 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1755 // find used textures
1756 usedTextures := THashStrInt.Create();
1757 try
1758 if (panels <> nil) and (panels.count > 0) then
1759 begin
1760 for rec in panels do
1761 begin
1762 texrec := rec.TextureRec;
1763 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1764 end;
1765 end;
1767 cnt := -1;
1768 for rec in mapTextureList do
1769 begin
1770 Inc(cnt);
1771 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1772 begin
1773 rec.tagInt := -1; // just in case
1774 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1775 end
1776 else
1777 begin
1778 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1779 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1780 {$ENDIF}
1781 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1782 if rec.Anim then
1783 begin
1784 // Àíèìèðîâàííàÿ òåêñòóðà
1785 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1786 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]));
1787 end
1788 else
1789 begin
1790 // Îáû÷íàÿ òåêñòóðà
1791 ntn := CreateTexture(rec.Resource, FileName, True);
1792 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1793 end;
1794 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1796 rec.tagInt := ntn; // remember texture number
1797 end;
1798 g_Game_StepLoading();
1799 end;
1800 finally
1801 usedTextures.Free();
1802 end;
1804 // set panel tagInt to texture index
1805 if (panels <> nil) then
1806 begin
1807 for rec in panels do
1808 begin
1809 texrec := rec.TextureRec;
1810 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1811 end;
1812 end;
1813 end;
1816 // Çàãðóçêà òðèããåðîâ
1817 gTriggerClientID := 0;
1818 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1819 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1821 // Çàãðóçêà ïàíåëåé
1822 e_WriteLog(' Loading panels...', TMsgType.Notify);
1823 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1825 // check texture numbers for panels
1826 if (panels <> nil) and (panels.count > 0) then
1827 begin
1828 for rec in panels do
1829 begin
1830 if (rec.tagInt < 0) then
1831 begin
1832 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1833 result := false;
1834 gCurrentMap.Free();
1835 gCurrentMap := nil;
1836 gCurrentMapFileName := '';
1837 exit;
1838 end;
1839 end;
1840 end;
1842 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1843 if (triggers <> nil) and (triggers.count > 0) then
1844 begin
1845 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1846 //SetLength(TriggersTable, triggers.count);
1847 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1849 SetLength(TriggersTable, triggers.count);
1850 trignum := -1;
1851 for rec in triggers do
1852 begin
1853 Inc(trignum);
1854 pttit := @TriggersTable[trignum];
1855 pttit.trigrec := rec;
1856 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1857 pttit.texPanelIdx := -1; // will be fixed later
1858 pttit.texPanel := rec.TexturePanelRec;
1859 // action panel
1860 pttit.actPanelIdx := -1;
1861 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1862 // set flag
1863 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1864 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1865 // update progress
1866 g_Game_StepLoading();
1867 end;
1868 end;
1870 // Ñîçäàåì ïàíåëè
1871 if (panels <> nil) and (panels.count > 0) then
1872 begin
1873 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1874 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1876 pannum := -1;
1877 for rec in panels do
1878 begin
1879 Inc(pannum);
1880 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1881 texrec := nil;
1882 SetLength(AddTextures, 0);
1883 CurTex := -1;
1884 ok := false;
1886 if (mapTextureList <> nil) then
1887 begin
1888 texrec := rec.TextureRec;
1889 ok := (texrec <> nil);
1890 end;
1892 if ok then
1893 begin
1894 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1895 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1896 ok := false;
1897 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1898 begin
1899 if rec.userPanelTrigRef then
1900 begin
1901 // e_LogWritefln('trigref for panel %s', [pannum]);
1902 ok := True;
1903 end;
1904 end;
1905 end;
1907 if ok then
1908 begin
1909 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1910 s := texrec.Resource;
1912 // Ñïåö-òåêñòóðû çàïðåùåíû
1913 if g_Map_IsSpecialTexture(s) then
1914 begin
1915 ok := false
1916 end
1917 else
1918 begin
1919 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1920 ok := g_Texture_NumNameFindStart(s);
1921 end;
1923 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1924 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1925 if ok then
1926 begin
1927 k := NNF_NAME_BEFORE;
1928 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1929 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1930 begin
1931 k := g_Texture_NumNameFindNext(TexName);
1933 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1934 begin
1935 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1936 if texrec.Anim then
1937 begin
1938 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1939 isAnim := True;
1940 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1941 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1942 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1943 if not ok then
1944 begin
1945 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1946 isAnim := False;
1947 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1948 ok := CreateTexture(TexName, FileName, False) >= 0;
1949 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1950 end;
1951 end
1952 else
1953 begin
1954 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1955 isAnim := False;
1956 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1957 ok := CreateTexture(TexName, FileName, False) >= 0;
1958 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1959 if not ok then
1960 begin
1961 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1962 isAnim := True;
1963 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1964 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1965 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1966 end;
1967 end;
1969 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1970 if ok then
1971 begin
1973 for c := 0 to High(Textures) do
1974 begin
1975 if (Textures[c].TextureName = TexName) then
1976 begin
1977 SetLength(AddTextures, Length(AddTextures)+1);
1978 AddTextures[High(AddTextures)].Texture := c;
1979 AddTextures[High(AddTextures)].Anim := isAnim;
1980 break;
1981 end;
1982 end;
1984 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1985 begin
1986 SetLength(AddTextures, Length(AddTextures)+1);
1987 AddTextures[High(AddTextures)].Texture := c;
1988 AddTextures[High(AddTextures)].Anim := isAnim;
1989 end;
1990 end;
1991 end
1992 else
1993 begin
1994 if k = NNF_NAME_EQUALS then
1995 begin
1996 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1997 SetLength(AddTextures, Length(AddTextures)+1);
1998 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1999 AddTextures[High(AddTextures)].Anim := texrec.Anim;
2000 CurTex := High(AddTextures);
2001 ok := true;
2002 end
2003 else // NNF_NO_NAME
2004 begin
2005 ok := false;
2006 end;
2007 end;
2008 end; // while ok...
2010 ok := true;
2011 end; // if ok - åñòü ñìåæíûå òåêñòóðû
2012 end; // if ok - ññûëàþòñÿ òðèããåðû
2014 if not ok then
2015 begin
2016 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
2017 SetLength(AddTextures, 1);
2018 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
2019 AddTextures[0].Anim := false;
2020 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
2021 CurTex := 0;
2022 end;
2024 //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);
2026 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
2028 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
2029 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
2030 PanelID := CreatePanel(rec, AddTextures, CurTex);
2031 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
2032 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
2034 // setup lifts
2035 moveSpeed := rec.moveSpeed;
2036 //moveStart := rec.moveStart;
2037 //moveEnd := rec.moveEnd;
2038 //moveActive := rec['move_active'].value;
2039 if not moveSpeed.isZero then
2040 begin
2041 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
2042 gMovingWallIds[High(gMovingWallIds)] := PanelID;
2043 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
2044 end;
2046 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2048 g_Game_StepLoading();
2049 end;
2050 end;
2052 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2053 for b := 0 to High(TriggersTable) do
2054 begin
2055 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2056 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2057 end;
2059 // create map grid, init other grids (for monsters, for example)
2060 e_WriteLog('Creating map grid', TMsgType.Notify);
2061 mapCreateGrid();
2063 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2064 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2065 begin
2066 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2067 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2068 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2069 trignum := -1;
2070 for rec in triggers do
2071 begin
2072 Inc(trignum);
2073 tgpid := TriggersTable[trignum].actPanelIdx;
2074 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2075 TriggersTable[trignum].tnum := trignum;
2076 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
2077 end;
2078 end;
2080 //FIXME: use hashtable!
2081 for pan in panByGUID do
2082 begin
2083 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2084 begin
2085 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2086 end;
2087 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2088 begin
2089 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2090 end;
2091 end;
2093 // Çàãðóçêà ïðåäìåòîâ
2094 e_WriteLog(' Loading items...', TMsgType.Notify);
2095 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2097 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2098 if (items <> nil) and not gLoadGameMode then
2099 begin
2100 e_WriteLog(' Spawning items...', TMsgType.Notify);
2101 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2102 for rec in items do CreateItem(rec);
2103 end;
2105 // Çàãðóçêà îáëàñòåé
2106 e_WriteLog(' Loading areas...', TMsgType.Notify);
2107 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2109 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2110 if areas <> nil then
2111 begin
2112 e_WriteLog(' Creating areas...', TMsgType.Notify);
2113 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2114 for rec in areas do CreateArea(rec);
2115 end;
2117 // Çàãðóçêà ìîíñòðîâ
2118 e_WriteLog(' Loading monsters...', TMsgType.Notify);
2119 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2121 gTotalMonsters := 0;
2123 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2124 if (monsters <> nil) and not gLoadGameMode then
2125 begin
2126 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
2127 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2128 for rec in monsters do CreateMonster(rec);
2129 end;
2131 //gCurrentMap := mapReader; // this will be our current map now
2132 gCurrentMapFileName := Res;
2133 //mapReader := nil;
2135 // Çàãðóçêà íåáà
2136 if (gMapInfo.SkyName <> '') then
2137 begin
2138 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
2139 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2140 FileName := g_ExtractWadName(gMapInfo.SkyName);
2142 if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
2144 if gTextureFilter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
2145 try
2146 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2147 if g_Texture_CreateWAD(BackID, s) then
2148 begin
2149 g_Game_SetupScreenSize();
2150 end
2151 else
2152 begin
2153 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2154 end;
2155 finally
2156 TEXTUREFILTER := GL_NEAREST;
2157 end;
2158 end;
2160 // Çàãðóçêà ìóçûêè
2161 ok := False;
2162 if gMapInfo.MusicName <> '' then
2163 begin
2164 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
2165 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2166 FileName := g_ExtractWadName(gMapInfo.MusicName);
2168 if FileName <> '' then
2169 FileName := GameDir+'/wads/'+FileName
2170 else
2171 begin
2172 FileName := g_ExtractWadName(Res);
2173 end;
2175 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2176 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2177 ok := True
2178 else
2179 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2180 end;
2182 // Îñòàëüíûå óñòàíâêè
2183 CreateDoorMap();
2184 CreateLiftMap();
2186 g_Items_Init();
2187 g_Weapon_Init();
2188 g_Monsters_Init();
2190 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2191 if not gLoadGameMode then g_GFX_Init();
2193 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2194 mapTextureList := nil;
2195 panels := nil;
2196 items := nil;
2197 areas := nil;
2198 triggers := nil;
2199 TriggersTable := nil;
2200 AddTextures := nil;
2202 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2203 if ok and (not gLoadGameMode) then
2204 begin
2205 gMusic.SetByName(gMapInfo.MusicName);
2206 gMusic.Play();
2207 end
2208 else
2209 begin
2210 gMusic.SetByName('');
2211 end;
2213 stt := getTimeMicro()-stt;
2214 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2215 mapOk := true;
2216 finally
2217 sfsGCEnable(); // enable releasing unused volumes
2218 //mapReader.Free();
2219 e_ClearInputBuffer(); // why not?
2220 if not mapOk then
2221 begin
2222 gCurrentMap.Free();
2223 gCurrentMap := nil;
2224 gCurrentMapFileName := '';
2225 end;
2226 end;
2228 e_WriteLog('Done loading map.', TMsgType.Notify);
2229 Result := True;
2230 end;
2233 function g_Map_GetMapInfo(Res: String): TMapInfo;
2234 var
2235 WAD: TWADFile;
2236 mapReader: TDynRecord;
2237 //Header: TMapHeaderRec_1;
2238 FileName: String;
2239 Data: Pointer;
2240 Len: Integer;
2241 begin
2242 FillChar(Result, SizeOf(Result), 0);
2243 FileName := g_ExtractWadName(Res);
2245 WAD := TWADFile.Create();
2246 if not WAD.ReadFile(FileName) then
2247 begin
2248 WAD.Free();
2249 Exit;
2250 end;
2252 //k8: it ignores path again
2253 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2254 begin
2255 WAD.Free();
2256 Exit;
2257 end;
2259 WAD.Free();
2261 try
2262 mapReader := g_Map_ParseMap(Data, Len);
2263 except
2264 mapReader := nil;
2265 FreeMem(Data);
2266 exit;
2267 end;
2269 FreeMem(Data);
2271 if (mapReader = nil) then exit;
2273 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2274 begin
2275 Result.Name := mapReader.MapName;
2276 Result.Description := mapReader.MapDesc;
2277 Result.Map := Res;
2278 Result.Author := mapReader.MapAuthor;
2279 Result.Height := mapReader.Height;
2280 Result.Width := mapReader.Width;
2281 end
2282 else
2283 begin
2284 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2285 //ZeroMemory(@Header, SizeOf(Header));
2286 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2287 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2288 Result.Map := Res;
2289 Result.Author := '';
2290 Result.Height := 0;
2291 Result.Width := 0;
2292 end;
2294 mapReader.Free();
2295 end;
2297 function g_Map_GetMapsList(WADName: string): SSArray;
2298 var
2299 WAD: TWADFile;
2300 a: Integer;
2301 ResList: SSArray;
2302 begin
2303 Result := nil;
2304 WAD := TWADFile.Create();
2305 if not WAD.ReadFile(WADName) then
2306 begin
2307 WAD.Free();
2308 Exit;
2309 end;
2310 ResList := WAD.GetMapResources();
2311 if ResList <> nil then
2312 begin
2313 for a := 0 to High(ResList) do
2314 begin
2315 SetLength(Result, Length(Result)+1);
2316 Result[High(Result)] := ResList[a];
2317 end;
2318 end;
2319 WAD.Free();
2320 end;
2322 function g_Map_Exist(Res: string): Boolean;
2323 var
2324 WAD: TWADFile;
2325 FileName, mnn: string;
2326 ResList: SSArray;
2327 a: Integer;
2328 begin
2329 Result := False;
2331 FileName := addWadExtension(g_ExtractWadName(Res));
2333 WAD := TWADFile.Create;
2334 if not WAD.ReadFile(FileName) then
2335 begin
2336 WAD.Free();
2337 Exit;
2338 end;
2340 ResList := WAD.GetMapResources();
2341 WAD.Free();
2343 mnn := g_ExtractFileName(Res);
2344 if ResList <> nil then
2345 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2346 begin
2347 Result := True;
2348 Exit;
2349 end;
2350 end;
2352 procedure g_Map_Free(freeTextures: Boolean=true);
2353 var
2354 a: Integer;
2356 procedure FreePanelArray(var panels: TPanelArray);
2357 var
2358 i: Integer;
2360 begin
2361 if panels <> nil then
2362 begin
2363 for i := 0 to High(panels) do
2364 panels[i].Free();
2365 panels := nil;
2366 end;
2367 end;
2369 begin
2370 g_GFX_Free();
2371 g_Weapon_Free();
2372 g_Items_Free();
2373 g_Triggers_Free();
2374 g_Monsters_Free();
2376 RespawnPoints := nil;
2377 if FlagPoints[FLAG_RED] <> nil then
2378 begin
2379 Dispose(FlagPoints[FLAG_RED]);
2380 FlagPoints[FLAG_RED] := nil;
2381 end;
2382 if FlagPoints[FLAG_BLUE] <> nil then
2383 begin
2384 Dispose(FlagPoints[FLAG_BLUE]);
2385 FlagPoints[FLAG_BLUE] := nil;
2386 end;
2387 //DOMFlagPoints := nil;
2389 //gDOMFlags := nil;
2391 if (Length(gCurrentMapFileName) <> 0) then
2392 begin
2393 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2394 end
2395 else
2396 begin
2397 e_LogWritefln('g_Map_Free: no previous map.', []);
2398 end;
2400 if freeTextures then
2401 begin
2402 e_LogWritefln('g_Map_Free: clearing textures...', []);
2403 if (Textures <> nil) then
2404 begin
2405 for a := 0 to High(Textures) do
2406 begin
2407 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2408 begin
2409 if Textures[a].Anim then
2410 begin
2411 g_Frames_DeleteByID(Textures[a].FramesID)
2412 end
2413 else
2414 begin
2415 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2416 begin
2417 e_DeleteTexture(Textures[a].TextureID);
2418 end;
2419 end;
2420 end;
2421 end;
2422 Textures := nil;
2423 end;
2424 TextNameHash.Free();
2425 TextNameHash := nil;
2426 BadTextNameHash.Free();
2427 BadTextNameHash := nil;
2428 gCurrentMapFileName := '';
2429 gCurrentMap.Free();
2430 gCurrentMap := nil;
2431 end;
2433 panByGUID := nil;
2435 FreePanelArray(gWalls);
2436 FreePanelArray(gRenderBackgrounds);
2437 FreePanelArray(gRenderForegrounds);
2438 FreePanelArray(gWater);
2439 FreePanelArray(gAcid1);
2440 FreePanelArray(gAcid2);
2441 FreePanelArray(gSteps);
2442 FreePanelArray(gLifts);
2443 FreePanelArray(gBlockMon);
2444 gMovingWallIds := nil;
2446 if BackID <> DWORD(-1) then
2447 begin
2448 gBackSize.X := 0;
2449 gBackSize.Y := 0;
2450 e_DeleteTexture(BackID);
2451 BackID := DWORD(-1);
2452 end;
2454 g_Game_StopAllSounds(False);
2455 gMusic.FreeSound();
2456 g_Sound_Delete(gMapInfo.MusicName);
2458 gMapInfo.Name := '';
2459 gMapInfo.Description := '';
2460 gMapInfo.MusicName := '';
2461 gMapInfo.Height := 0;
2462 gMapInfo.Width := 0;
2464 gDoorMap := nil;
2465 gLiftMap := nil;
2466 end;
2468 procedure g_Map_Update();
2469 var
2470 a, d, j: Integer;
2471 m: Word;
2472 s: String;
2474 procedure UpdatePanelArray(var panels: TPanelArray);
2475 var
2476 i: Integer;
2478 begin
2479 for i := 0 to High(panels) do panels[i].Update();
2480 end;
2482 begin
2483 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2485 UpdatePanelArray(gWalls);
2486 UpdatePanelArray(gRenderBackgrounds);
2487 UpdatePanelArray(gRenderForegrounds);
2488 UpdatePanelArray(gWater);
2489 UpdatePanelArray(gAcid1);
2490 UpdatePanelArray(gAcid2);
2491 UpdatePanelArray(gSteps);
2493 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2495 if gGameSettings.GameMode = GM_CTF then
2496 begin
2497 for a := FLAG_RED to FLAG_BLUE do
2498 begin
2499 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2500 begin
2501 with gFlags[a] do
2502 begin
2503 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2505 m := g_Obj_Move(@Obj, True, True);
2507 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2509 // Ñîïðîòèâëåíèå âîçäóõà
2510 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2512 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2513 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2514 begin
2515 g_Map_ResetFlag(a);
2516 gFlags[a].CaptureTime := 0;
2517 if a = FLAG_RED then
2518 s := _lc[I_PLAYER_FLAG_RED]
2519 else
2520 s := _lc[I_PLAYER_FLAG_BLUE];
2521 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2523 if g_Game_IsNet then
2524 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2525 Continue;
2526 end;
2528 if Count > 0 then Count -= 1;
2530 // Èãðîê áåðåò ôëàã
2531 if gPlayers <> nil then
2532 begin
2533 j := Random(Length(gPlayers)) - 1;
2534 for d := 0 to High(gPlayers) do
2535 begin
2536 Inc(j);
2537 if j > High(gPlayers) then j := 0;
2538 if gPlayers[j] <> nil then
2539 begin
2540 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2541 begin
2542 if gPlayers[j].GetFlag(a) then Break;
2543 end;
2544 end;
2545 end;
2546 end;
2547 end;
2548 end;
2549 end;
2550 end;
2551 end;
2554 // old algo
2555 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2557 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2558 var
2559 idx: Integer;
2560 begin
2561 if (panels <> nil) then
2562 begin
2563 // alas, no visible set
2564 for idx := 0 to High(panels) do
2565 begin
2566 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2567 end;
2568 end;
2569 end;
2571 begin
2572 case PanelType of
2573 PANEL_WALL: DrawPanels(gWalls);
2574 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2575 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2576 PANEL_FORE: DrawPanels(gRenderForegrounds);
2577 PANEL_WATER: DrawPanels(gWater);
2578 PANEL_ACID1: DrawPanels(gAcid1);
2579 PANEL_ACID2: DrawPanels(gAcid2);
2580 PANEL_STEP: DrawPanels(gSteps);
2581 end;
2582 end;
2585 // new algo
2586 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2588 function checker (pan: TPanel; tag: Integer): Boolean;
2589 begin
2590 result := false; // don't stop, ever
2591 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2592 gDrawPanelList.insert(pan);
2593 end;
2595 begin
2596 dplClear();
2597 //tagmask := panelTypeToTag(PanelType);
2598 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2599 // list will be rendered in `g_game.DrawPlayer()`
2600 end;
2603 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2605 function checker (pan: TPanel; tag: Integer): Boolean;
2606 begin
2607 result := false; // don't stop, ever
2608 pan.DrawShadowVolume(lightX, lightY, radius);
2609 end;
2611 begin
2612 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2613 end;
2616 procedure g_Map_DrawBack(dx, dy: Integer);
2617 begin
2618 if gDrawBackGround and (BackID <> DWORD(-1)) then
2619 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2620 else
2621 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2622 end;
2624 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2625 PanelType: Word; b1x3: Boolean=false): Boolean;
2626 var
2627 a, h: Integer;
2628 begin
2629 Result := False;
2631 if WordBool(PanelType and PANEL_WALL) then
2632 if gWalls <> nil then
2633 begin
2634 h := High(gWalls);
2636 for a := 0 to h do
2637 if gWalls[a].Enabled and
2638 g_Collide(X, Y, Width, Height,
2639 gWalls[a].X, gWalls[a].Y,
2640 gWalls[a].Width, gWalls[a].Height) then
2641 begin
2642 Result := True;
2643 Exit;
2644 end;
2645 end;
2647 if WordBool(PanelType and PANEL_WATER) then
2648 if gWater <> nil then
2649 begin
2650 h := High(gWater);
2652 for a := 0 to h do
2653 if g_Collide(X, Y, Width, Height,
2654 gWater[a].X, gWater[a].Y,
2655 gWater[a].Width, gWater[a].Height) then
2656 begin
2657 Result := True;
2658 Exit;
2659 end;
2660 end;
2662 if WordBool(PanelType and PANEL_ACID1) then
2663 if gAcid1 <> nil then
2664 begin
2665 h := High(gAcid1);
2667 for a := 0 to h do
2668 if g_Collide(X, Y, Width, Height,
2669 gAcid1[a].X, gAcid1[a].Y,
2670 gAcid1[a].Width, gAcid1[a].Height) then
2671 begin
2672 Result := True;
2673 Exit;
2674 end;
2675 end;
2677 if WordBool(PanelType and PANEL_ACID2) then
2678 if gAcid2 <> nil then
2679 begin
2680 h := High(gAcid2);
2682 for a := 0 to h do
2683 if g_Collide(X, Y, Width, Height,
2684 gAcid2[a].X, gAcid2[a].Y,
2685 gAcid2[a].Width, gAcid2[a].Height) then
2686 begin
2687 Result := True;
2688 Exit;
2689 end;
2690 end;
2692 if WordBool(PanelType and PANEL_STEP) then
2693 if gSteps <> nil then
2694 begin
2695 h := High(gSteps);
2697 for a := 0 to h do
2698 if g_Collide(X, Y, Width, Height,
2699 gSteps[a].X, gSteps[a].Y,
2700 gSteps[a].Width, gSteps[a].Height) then
2701 begin
2702 Result := True;
2703 Exit;
2704 end;
2705 end;
2707 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2708 if gLifts <> nil then
2709 begin
2710 h := High(gLifts);
2712 for a := 0 to h do
2713 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2714 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2715 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2716 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2717 g_Collide(X, Y, Width, Height,
2718 gLifts[a].X, gLifts[a].Y,
2719 gLifts[a].Width, gLifts[a].Height) then
2720 begin
2721 Result := True;
2722 Exit;
2723 end;
2724 end;
2726 if WordBool(PanelType and PANEL_BLOCKMON) then
2727 if gBlockMon <> nil then
2728 begin
2729 h := High(gBlockMon);
2731 for a := 0 to h do
2732 if ( (not b1x3) or
2733 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2734 g_Collide(X, Y, Width, Height,
2735 gBlockMon[a].X, gBlockMon[a].Y,
2736 gBlockMon[a].Width, gBlockMon[a].Height) then
2737 begin
2738 Result := True;
2739 Exit;
2740 end;
2741 end;
2742 end;
2744 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2745 var
2746 texid: DWORD;
2748 function checkPanels (constref panels: TPanelArray): Boolean;
2749 var
2750 a: Integer;
2751 begin
2752 result := false;
2753 if panels = nil then exit;
2754 for a := 0 to High(panels) do
2755 begin
2756 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2757 begin
2758 result := true;
2759 texid := panels[a].GetTextureID();
2760 exit;
2761 end;
2762 end;
2763 end;
2765 begin
2766 texid := LongWord(TEXTURE_NONE);
2767 result := texid;
2768 if not checkPanels(gWater) then
2769 if not checkPanels(gAcid1) then
2770 if not checkPanels(gAcid2) then exit;
2771 result := texid;
2772 end;
2775 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2776 const
2777 SlowMask = GridTagLift or GridTagBlockMon;
2778 function checker (pan: TPanel; tag: Integer): Boolean;
2779 begin
2781 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2782 begin
2783 result := pan.Enabled;
2784 exit;
2785 end;
2788 if ((tag and GridTagLift) <> 0) then
2789 begin
2790 result :=
2791 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2792 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2793 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2794 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2795 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2796 exit;
2797 end;
2799 if ((tag and GridTagBlockMon) <> 0) then
2800 begin
2801 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2802 exit;
2803 end;
2805 // other shit
2806 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2807 result := true; // i found her!
2808 end;
2810 var
2811 tagmask: Integer = 0;
2812 begin
2813 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2814 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2815 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2816 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2817 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2818 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2819 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2821 if (tagmask = 0) then begin result := false; exit; end; // just in case
2823 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2824 if gdbg_map_use_accel_coldet then
2825 begin
2826 if (Width = 1) and (Height = 1) then
2827 begin
2828 if ((tagmask and SlowMask) <> 0) then
2829 begin
2830 // slow
2831 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2832 end
2833 else
2834 begin
2835 // fast
2836 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2837 end;
2838 end
2839 else
2840 begin
2841 if ((tagmask and SlowMask) <> 0) then
2842 begin
2843 // slow
2844 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2845 end
2846 else
2847 begin
2848 // fast
2849 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2850 end;
2851 end;
2852 end
2853 else
2854 begin
2855 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2856 end;
2857 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2858 end;
2861 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2862 var
2863 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2864 texid: DWORD;
2866 // slightly different from the old code, but meh...
2867 function checker (pan: TPanel; tag: Integer): Boolean;
2868 begin
2869 result := false; // don't stop, ever
2870 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2871 // check priorities
2872 case cctype of
2873 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2874 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2875 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2876 end;
2877 // collision?
2878 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2879 // yeah
2880 texid := pan.GetTextureID();
2881 // water? water has the highest priority, so stop right here
2882 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2883 // acid2?
2884 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2885 // acid1?
2886 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2887 end;
2889 begin
2890 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2891 if gdbg_map_use_accel_coldet then
2892 begin
2893 texid := LongWord(TEXTURE_NONE);
2894 if (Width = 1) and (Height = 1) then
2895 begin
2896 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2897 end
2898 else
2899 begin
2900 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2901 end;
2902 result := texid;
2903 end
2904 else
2905 begin
2906 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2907 end;
2908 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2909 end;
2912 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2913 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2914 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2917 procedure g_Map_EnableWallGUID (pguid: Integer);
2918 var
2919 pan: TPanel;
2920 begin
2921 //pan := gWalls[ID];
2922 pan := g_Map_PanelByGUID(pguid);
2923 if (pan = nil) then exit;
2924 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2926 pan.Enabled := True;
2927 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2929 mapGrid.proxyEnabled[pan.proxyId] := true;
2930 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2931 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2933 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2934 // mark platform as interesting
2935 pan.setDirty();
2937 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2938 //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);
2939 {$ENDIF}
2940 end;
2943 procedure g_Map_DisableWallGUID (pguid: Integer);
2944 var
2945 pan: TPanel;
2946 begin
2947 //pan := gWalls[ID];
2948 pan := g_Map_PanelByGUID(pguid);
2949 if (pan = nil) then exit;
2950 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2952 pan.Enabled := False;
2953 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2955 mapGrid.proxyEnabled[pan.proxyId] := false;
2956 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2958 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2959 // mark platform as interesting
2960 pan.setDirty();
2962 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2963 //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);
2964 {$ENDIF}
2965 end;
2968 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
2969 var
2970 tp: TPanel;
2971 begin
2972 tp := g_Map_PanelByGUID(pguid);
2973 if (tp = nil) then exit;
2974 tp.NextTexture(AnimLoop);
2975 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
2976 end;
2979 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2980 var
2981 pan: TPanel;
2982 begin
2983 //pan := gLifts[ID];
2984 pan := g_Map_PanelByGUID(pguid);
2985 if (pan = nil) then exit;
2986 if not pan.isGLift then exit;
2988 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2990 with {gLifts[ID]} pan do
2991 begin
2992 LiftType := t;
2994 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2995 //TODO: make separate lift tags, and change tag here
2997 case LiftType of
2998 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2999 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
3000 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
3001 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
3002 end;
3004 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3005 // mark platform as interesting
3006 pan.setDirty();
3007 end;
3008 end;
3011 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
3012 var
3013 a: Integer;
3014 PointsArray: Array of TRespawnPoint;
3015 begin
3016 Result := False;
3017 SetLength(PointsArray, 0);
3019 if RespawnPoints = nil then
3020 Exit;
3022 for a := 0 to High(RespawnPoints) do
3023 if RespawnPoints[a].PointType = PointType then
3024 begin
3025 SetLength(PointsArray, Length(PointsArray)+1);
3026 PointsArray[High(PointsArray)] := RespawnPoints[a];
3027 end;
3029 if PointsArray = nil then
3030 Exit;
3032 RespawnPoint := PointsArray[Random(Length(PointsArray))];
3033 Result := True;
3034 end;
3036 function g_Map_GetPointCount(PointType: Byte): Word;
3037 var
3038 a: Integer;
3039 begin
3040 Result := 0;
3042 if RespawnPoints = nil then
3043 Exit;
3045 for a := 0 to High(RespawnPoints) do
3046 if RespawnPoints[a].PointType = PointType then
3047 Result := Result + 1;
3048 end;
3050 function g_Map_HaveFlagPoints(): Boolean;
3051 begin
3052 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3053 end;
3055 procedure g_Map_ResetFlag(Flag: Byte);
3056 begin
3057 with gFlags[Flag] do
3058 begin
3059 Obj.X := -1000;
3060 Obj.Y := -1000;
3061 Obj.Vel.X := 0;
3062 Obj.Vel.Y := 0;
3063 Direction := TDirection.D_LEFT;
3064 State := FLAG_STATE_NONE;
3065 if FlagPoints[Flag] <> nil then
3066 begin
3067 Obj.X := FlagPoints[Flag]^.X;
3068 Obj.Y := FlagPoints[Flag]^.Y;
3069 Direction := FlagPoints[Flag]^.Direction;
3070 State := FLAG_STATE_NORMAL;
3071 end;
3072 Count := -1;
3073 end;
3074 end;
3076 procedure g_Map_DrawFlags();
3077 var
3078 i, dx: Integer;
3079 Mirror: TMirrorType;
3080 begin
3081 if gGameSettings.GameMode <> GM_CTF then
3082 Exit;
3084 for i := FLAG_RED to FLAG_BLUE do
3085 with gFlags[i] do
3086 if State <> FLAG_STATE_CAPTURED then
3087 begin
3088 if State = FLAG_STATE_NONE then
3089 continue;
3091 if Direction = TDirection.D_LEFT then
3092 begin
3093 Mirror := TMirrorType.Horizontal;
3094 dx := -1;
3095 end
3096 else
3097 begin
3098 Mirror := TMirrorType.None;
3099 dx := 1;
3100 end;
3102 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3104 if g_debug_Frames then
3105 begin
3106 e_DrawQuad(Obj.X+Obj.Rect.X,
3107 Obj.Y+Obj.Rect.Y,
3108 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3109 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3110 0, 255, 0);
3111 end;
3112 end;
3113 end;
3116 procedure g_Map_SaveState (st: TStream);
3117 var
3118 str: String;
3120 procedure savePanels ();
3121 var
3122 pan: TPanel;
3123 begin
3124 // Ñîõðàíÿåì ïàíåëè
3125 utils.writeInt(st, LongInt(Length(panByGUID)));
3126 for pan in panByGUID do pan.SaveState(st);
3127 end;
3129 procedure saveFlag (flag: PFlag);
3130 var
3131 b: Byte;
3132 begin
3133 utils.writeSign(st, 'FLAG');
3134 utils.writeInt(st, Byte(0)); // version
3135 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3136 utils.writeInt(st, Byte(flag^.RespawnType));
3137 // Ñîñòîÿíèå ôëàãà
3138 utils.writeInt(st, Byte(flag^.State));
3139 // Íàïðàâëåíèå ôëàãà
3140 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
3141 utils.writeInt(st, Byte(b));
3142 // Îáúåêò ôëàãà
3143 Obj_SaveState(st, @flag^.Obj);
3144 end;
3146 begin
3147 savePanels();
3149 // Ñîõðàíÿåì ìóçûêó
3150 utils.writeSign(st, 'MUSI');
3151 utils.writeInt(st, Byte(0));
3152 // Íàçâàíèå ìóçûêè
3153 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3154 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3155 utils.writeStr(st, str);
3156 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3157 utils.writeInt(st, LongWord(gMusic.GetPosition()));
3158 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3159 utils.writeBool(st, gMusic.SpecPause);
3161 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3162 utils.writeInt(st, LongInt(gTotalMonsters));
3163 ///// /////
3165 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3166 if (gGameSettings.GameMode = GM_CTF) then
3167 begin
3168 // Ôëàã Êðàñíîé êîìàíäû
3169 saveFlag(@gFlags[FLAG_RED]);
3170 // Ôëàã Ñèíåé êîìàíäû
3171 saveFlag(@gFlags[FLAG_BLUE]);
3172 end;
3173 ///// /////
3175 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3176 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3177 begin
3178 // Î÷êè Êðàñíîé êîìàíäû
3179 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
3180 // Î÷êè Ñèíåé êîìàíäû
3181 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
3182 end;
3183 ///// /////
3184 end;
3187 procedure g_Map_LoadState (st: TStream);
3188 var
3189 dw: DWORD;
3190 str: String;
3191 boo: Boolean;
3193 procedure loadPanels ();
3194 var
3195 pan: TPanel;
3196 begin
3197 // Çàãðóæàåì ïàíåëè
3198 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
3199 for pan in panByGUID do
3200 begin
3201 pan.LoadState(st);
3202 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3203 end;
3204 end;
3206 procedure loadFlag (flag: PFlag);
3207 var
3208 b: Byte;
3209 begin
3210 // Ñèãíàòóðà ôëàãà
3211 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
3212 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
3213 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3214 flag^.RespawnType := utils.readByte(st);
3215 // Ñîñòîÿíèå ôëàãà
3216 flag^.State := utils.readByte(st);
3217 // Íàïðàâëåíèå ôëàãà
3218 b := utils.readByte(st);
3219 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
3220 // Îáúåêò ôëàãà
3221 Obj_LoadState(@flag^.Obj, st);
3222 end;
3224 begin
3225 if (st = nil) then exit;
3227 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3228 loadPanels();
3229 ///// /////
3231 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3232 g_GFX_Init();
3233 //mapCreateGrid();
3235 ///// Çàãðóæàåì ìóçûêó: /////
3236 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
3237 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
3238 // Íàçâàíèå ìóçûêè
3239 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3240 str := utils.readStr(st);
3241 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3242 dw := utils.readLongWord(st);
3243 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3244 boo := utils.readBool(st);
3245 // Çàïóñêàåì ýòó ìóçûêó
3246 gMusic.SetByName(str);
3247 gMusic.SpecPause := boo;
3248 gMusic.Play();
3249 gMusic.Pause(true);
3250 gMusic.SetPosition(dw);
3251 ///// /////
3253 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3254 gTotalMonsters := utils.readLongInt(st);
3255 ///// /////
3257 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3258 if (gGameSettings.GameMode = GM_CTF) then
3259 begin
3260 // Ôëàã Êðàñíîé êîìàíäû
3261 loadFlag(@gFlags[FLAG_RED]);
3262 // Ôëàã Ñèíåé êîìàíäû
3263 loadFlag(@gFlags[FLAG_BLUE]);
3264 end;
3265 ///// /////
3267 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3268 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3269 begin
3270 // Î÷êè Êðàñíîé êîìàíäû
3271 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
3272 // Î÷êè Ñèíåé êîìàíäû
3273 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
3274 end;
3275 ///// /////
3276 end;
3279 // trace liquid, stepping by `dx` and `dy`
3280 // return last seen liquid coords, and `false` if we're started outside of the liquid
3281 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3282 const
3283 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3284 begin
3285 topx := x;
3286 topy := y;
3287 // started outside of the liquid?
3288 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3289 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3290 result := true;
3291 while true do
3292 begin
3293 Inc(x, dx);
3294 Inc(y, dy);
3295 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3296 topx := x;
3297 topy := y;
3298 end;
3299 end;
3302 begin
3303 DynWarningCB := mapWarningCB;
3304 end.