DEADSOFTWARE

save/load fixes
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 e_graphics, g_basic, MAPDEF, g_textures, Classes,
24 g_phys, wadreader, BinEditor, g_panel, g_grid, md5, binheap, xprofiler, xparser, xdynrec;
26 type
27 TMapInfo = record
28 Map: String;
29 Name: String;
30 Description: String;
31 Author: String;
32 MusicName: String;
33 SkyName: String;
34 Height: Word;
35 Width: Word;
36 end;
38 PRespawnPoint = ^TRespawnPoint;
39 TRespawnPoint = record
40 X, Y: Integer;
41 Direction: TDirection;
42 PointType: Byte;
43 end;
45 PFlagPoint = ^TFlagPoint;
46 TFlagPoint = TRespawnPoint;
48 PFlag = ^TFlag;
49 TFlag = record
50 Obj: TObj;
51 RespawnType: Byte;
52 State: Byte;
53 Count: Integer;
54 CaptureTime: LongWord;
55 Animation: TAnimation;
56 Direction: TDirection;
57 end;
59 function g_Map_Load(Res: String): Boolean;
60 function g_Map_GetMapInfo(Res: String): TMapInfo;
61 function g_Map_GetMapsList(WADName: String): SArray;
62 function g_Map_Exist(Res: String): Boolean;
63 procedure g_Map_Free(freeTextures: Boolean=true);
64 procedure g_Map_Update();
66 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
68 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor); // unaccelerated
69 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
71 procedure g_Map_DrawBack(dx, dy: Integer);
72 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
73 PanelType: Word; b1x3: Boolean=false): Boolean;
74 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
76 procedure g_Map_EnableWallGUID (pguid: Integer);
77 procedure g_Map_DisableWallGUID (pguid: Integer);
78 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
80 // HACK!!!
81 procedure g_Map_EnableWall_XXX (ID: DWORD);
82 procedure g_Map_DisableWall_XXX (ID: DWORD);
83 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
85 procedure g_Map_SwitchTextureGUID (PanelType: Word; pguid: Integer; AnimLoop: Byte = 0);
87 procedure g_Map_ReAdd_DieTriggers();
88 function g_Map_IsSpecialTexture(Texture: String): Boolean;
90 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
91 function g_Map_GetPointCount(PointType: Byte): Word;
93 function g_Map_HaveFlagPoints(): Boolean;
95 procedure g_Map_ResetFlag(Flag: Byte);
96 procedure g_Map_DrawFlags();
98 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
99 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
101 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
103 // returns panel or nil
104 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
105 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
107 // returns panel or nil
108 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
109 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
111 type
112 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
114 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
115 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
117 // trace liquid, stepping by `dx` and `dy`
118 // return last seen liquid coords, and `false` if we're started outside of the liquid
119 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
122 // return `true` from `cb` to stop
123 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
125 procedure g_Map_NetSendInterestingPanels (); // yay!
128 procedure g_Map_ProfilersBegin ();
129 procedure g_Map_ProfilersEnd ();
132 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
135 function g_Map_MinX (): Integer; inline;
136 function g_Map_MinY (): Integer; inline;
137 function g_Map_MaxX (): Integer; inline;
138 function g_Map_MaxY (): Integer; inline;
140 const
141 NNF_NO_NAME = 0;
142 NNF_NAME_BEFORE = 1;
143 NNF_NAME_EQUALS = 2;
144 NNF_NAME_AFTER = 3;
146 function g_Texture_NumNameFindStart(name: String): Boolean;
147 function g_Texture_NumNameFindNext(var newName: String): Byte;
149 const
150 RESPAWNPOINT_PLAYER1 = 1;
151 RESPAWNPOINT_PLAYER2 = 2;
152 RESPAWNPOINT_DM = 3;
153 RESPAWNPOINT_RED = 4;
154 RESPAWNPOINT_BLUE = 5;
156 FLAG_NONE = 0;
157 FLAG_RED = 1;
158 FLAG_BLUE = 2;
159 FLAG_DOM = 3;
161 FLAG_STATE_NONE = 0;
162 FLAG_STATE_NORMAL = 1;
163 FLAG_STATE_DROPPED = 2;
164 FLAG_STATE_CAPTURED = 3;
165 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
166 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
168 FLAG_TIME = 720; // 20 seconds
170 SKY_STRETCH: Single = 1.5;
172 const
173 GridTagInvalid = 0;
175 (* draw order:
176 PANEL_BACK
177 PANEL_STEP
178 PANEL_WALL
179 PANEL_CLOSEDOOR
180 PANEL_ACID1
181 PANEL_ACID2
182 PANEL_WATER
183 PANEL_FORE
184 *)
185 // sorted by draw priority
186 GridTagBack = 1 shl 0; // gRenderBackgrounds
187 GridTagStep = 1 shl 1; // gSteps
188 GridTagWall = 1 shl 2; // gWalls
189 GridTagDoor = 1 shl 3; // gWalls
190 GridTagAcid1 = 1 shl 4; // gAcid1
191 GridTagAcid2 = 1 shl 5; // gAcid2
192 GridTagWater = 1 shl 6; // gWater
193 GridTagFore = 1 shl 7; // gRenderForegrounds
194 // the following are invisible
195 GridTagLift = 1 shl 8; // gLifts
196 GridTagBlockMon = 1 shl 9; // gBlockMon
198 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
199 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
201 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
204 var
205 gWalls: TPanelArray;
206 gRenderBackgrounds: TPanelArray;
207 gRenderForegrounds: TPanelArray;
208 gWater, gAcid1, gAcid2: TPanelArray;
209 gSteps: TPanelArray;
210 gLifts: TPanelArray;
211 gBlockMon: TPanelArray;
212 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
213 //gDOMFlags: array of TFlag;
214 gMapInfo: TMapInfo;
215 gBackSize: TDFPoint;
216 gDoorMap: array of array of DWORD;
217 gLiftMap: array of array of DWORD;
218 gWADHash: TMD5Digest;
219 BackID: DWORD = DWORD(-1);
220 gExternalResources: TStringList;
221 gMovingWallIds: array of Integer = nil;
223 gdbg_map_use_accel_render: Boolean = true;
224 gdbg_map_use_accel_coldet: Boolean = true;
225 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
226 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
228 gCurrentMap: TDynRecord = nil;
229 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
232 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
235 type
236 TPanelGrid = specialize TBodyGridBase<TPanel>;
238 var
239 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
242 implementation
244 uses
245 e_input, g_main, e_log, SysUtils, g_items, g_gfx, g_console,
246 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
247 g_options, g_triggers, g_player,
248 Math, g_monsters, g_saveload, g_language, g_netmsg,
249 utils, sfs, xstreams, hashtable,
250 ImagingTypes, Imaging, ImagingUtility,
251 ImagingGif, ImagingNetworkGraphics;
253 const
254 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
255 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
256 FLAG_SIGNATURE = $47414C46; // 'FLAG'
259 var
260 panByGUID: array of TPanel = nil;
263 // ////////////////////////////////////////////////////////////////////////// //
264 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
265 begin
266 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
267 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
268 end;
271 // return `true` from `cb` to stop
272 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
273 var
274 pan: TPanel;
275 begin
276 result := nil;
277 if not assigned(cb) then exit;
278 for pan in panByGUID do
279 begin
280 if cb(pan) then begin result := pan; exit; end;
281 end;
282 end;
285 procedure g_Map_NetSendInterestingPanels ();
286 var
287 pan: TPanel;
288 begin
289 if g_Game_IsServer and g_Game_IsNet then
290 begin
291 for pan in panByGUID do
292 begin
293 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
294 end;
295 end;
296 end;
299 // ////////////////////////////////////////////////////////////////////////// //
300 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
301 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
302 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
303 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
306 // ////////////////////////////////////////////////////////////////////////// //
307 var
308 dfmapdef: TDynMapDef = nil;
311 procedure loadMapDefinition ();
312 var
313 pr: TTextParser = nil;
314 st: TStream = nil;
315 WAD: TWADFile = nil;
316 begin
317 if (dfmapdef <> nil) then exit;
319 try
320 e_LogWritefln('parsing "mapdef.txt"...', []);
321 st := openDiskFileRO(DataDir+'mapdef.txt');
322 e_LogWritefln('found local "%smapdef.txt"', [DataDir]);
323 except
324 st := nil;
325 end;
327 if (st = nil) then
328 begin
329 WAD := TWADFile.Create();
330 if not WAD.ReadFile(GameWAD) then
331 begin
332 //raise Exception.Create('cannot load "game.wad"');
333 st := nil;
334 end
335 else
336 begin
337 st := WAD.openFileStream('mapdef.txt');
338 end;
339 end;
341 try
342 if (st = nil) then
343 begin
344 //raise Exception.Create('cannot open "mapdef.txt"');
345 e_LogWriteln('using default "mapdef.txt"...');
346 pr := TStrTextParser.Create(defaultMapDef);
347 end
348 else
349 begin
350 pr := TFileTextParser.Create(st);
351 end;
352 except on e: Exception do
353 begin
354 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
355 raise;
356 end;
357 end;
359 try
360 dfmapdef := TDynMapDef.Create(pr);
361 except
362 on e: TDynParseException do
363 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
364 on e: Exception do
365 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
366 end;
368 st.Free();
369 WAD.Free();
370 end;
373 // ////////////////////////////////////////////////////////////////////////// //
374 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
375 var
376 wst: TSFSMemoryChunkStream = nil;
377 begin
378 result := nil;
379 if (dataLen < 4) then exit;
381 if (dfmapdef = nil) then writeln('need to load mapdef');
382 loadMapDefinition();
383 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
385 wst := TSFSMemoryChunkStream.Create(data, dataLen);
386 try
387 result := dfmapdef.parseMap(wst);
388 except
389 on e: TDynParseException do
390 begin
391 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
392 wst.Free();
393 result := nil;
394 exit;
395 end;
396 on e: Exception do
397 begin
398 e_LogWritefln('ERROR: %s', [e.message]);
399 wst.Free();
400 result := nil;
401 exit;
402 end;
403 end;
405 //e_LogWriteln('map parsed.');
406 end;
409 // ////////////////////////////////////////////////////////////////////////// //
410 var
411 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
412 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
413 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
416 function g_Texture_NumNameFindStart(name: String): Boolean;
417 var
418 i: Integer;
420 begin
421 Result := False;
422 NNF_PureName := '';
423 NNF_FirstNum := -1;
424 NNF_CurrentNum := -1;
426 for i := Length(name) downto 1 do
427 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
428 begin
429 if i = Length(name) then
430 begin // Íåò öèôð â êîíöå ñòðîêè
431 Exit;
432 end
433 else
434 begin
435 NNF_PureName := Copy(name, 1, i);
436 Delete(name, 1, i);
437 Break;
438 end;
439 end;
441 // Íå ïåðåâåñòè â ÷èñëî:
442 if not TryStrToInt(name, NNF_FirstNum) then
443 Exit;
445 NNF_CurrentNum := 0;
447 Result := True;
448 end;
451 function g_Texture_NumNameFindNext(var newName: String): Byte;
452 begin
453 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
454 begin
455 newName := '';
456 Result := NNF_NO_NAME;
457 Exit;
458 end;
460 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
462 if NNF_CurrentNum < NNF_FirstNum then
463 Result := NNF_NAME_BEFORE
464 else
465 if NNF_CurrentNum > NNF_FirstNum then
466 Result := NNF_NAME_AFTER
467 else
468 Result := NNF_NAME_EQUALS;
470 Inc(NNF_CurrentNum);
471 end;
474 // ////////////////////////////////////////////////////////////////////////// //
475 function panelTypeToTag (panelType: Word): Integer;
476 begin
477 case panelType of
478 PANEL_WALL: result := GridTagWall; // gWalls
479 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
480 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
481 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
482 PANEL_WATER: result := GridTagWater; // gWater
483 PANEL_ACID1: result := GridTagAcid1; // gAcid1
484 PANEL_ACID2: result := GridTagAcid2; // gAcid2
485 PANEL_STEP: result := GridTagStep; // gSteps
486 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
487 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
488 else result := GridTagInvalid;
489 end;
490 end;
493 function dplLess (a, b: TObject): Boolean;
494 var
495 pa, pb: TPanel;
496 begin
497 pa := TPanel(a);
498 pb := TPanel(b);
499 if (pa.tag < pb.tag) then begin result := true; exit; end;
500 if (pa.tag > pb.tag) then begin result := false; exit; end;
501 result := (pa.arrIdx < pb.arrIdx);
502 end;
504 procedure dplClear ();
505 begin
506 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
507 end;
510 var
511 Textures: TLevelTextureArray = nil;
512 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
513 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
514 RespawnPoints: array of TRespawnPoint;
515 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
516 //DOMFlagPoints: Array of TFlagPoint;
519 procedure g_Map_ProfilersBegin ();
520 begin
521 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
522 profMapCollision.mainBegin(g_profile_collision);
523 // create sections
524 if g_profile_collision then
525 begin
526 profMapCollision.sectionBegin('*solids');
527 profMapCollision.sectionEnd();
528 profMapCollision.sectionBegin('liquids');
529 profMapCollision.sectionEnd();
530 end;
531 end;
533 procedure g_Map_ProfilersEnd ();
534 begin
535 if (profMapCollision <> nil) then profMapCollision.mainEnd();
536 end;
539 // wall index in `gWalls` or -1
540 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
541 var
542 ex, ey: Integer;
543 begin
544 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
545 if (result <> nil) then
546 begin
547 if (hitx <> nil) then hitx^ := ex;
548 if (hity <> nil) then hity^ := ey;
549 end
550 else
551 begin
552 if (hitx <> nil) then hitx^ := x1;
553 if (hity <> nil) then hity^ := y1;
554 end;
555 end;
557 // returns panel or nil
558 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
559 var
560 ex, ey: Integer;
561 begin
562 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
563 if (result <> nil) then
564 begin
565 if (hitx <> nil) then hitx^ := ex;
566 if (hity <> nil) then hity^ := ey;
567 end
568 else
569 begin
570 if (hitx <> nil) then hitx^ := x1;
571 if (hity <> nil) then hity^ := y1;
572 end;
573 end;
576 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
578 function checker (pan: TPanel; tag: Integer): Boolean;
579 begin
581 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
582 begin
583 result := pan.Enabled; // stop if wall is enabled
584 exit;
585 end;
588 if ((tag and GridTagLift) <> 0) then
589 begin
590 // stop if the lift of the right type
591 result :=
592 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
593 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
594 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
595 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
596 exit;
597 end;
599 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
600 end;
602 var
603 tagmask: Integer = 0;
604 begin
605 result := false;
607 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
608 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
609 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
610 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
611 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
612 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
613 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
615 if (tagmask = 0) then exit;// just in case
616 if ((tagmask and GridTagLift) <> 0) then
617 begin
618 // slow
619 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
620 end
621 else
622 begin
623 // fast
624 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
625 end;
626 end;
629 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
630 begin
631 result := nil;
632 if (tagmask = 0) then exit;
633 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
634 end;
637 function g_Map_IsSpecialTexture(Texture: String): Boolean;
638 begin
639 Result := (Texture = TEXTURE_NAME_WATER) or
640 (Texture = TEXTURE_NAME_ACID1) or
641 (Texture = TEXTURE_NAME_ACID2);
642 end;
644 procedure CreateDoorMap();
645 var
646 PanelArray: Array of record
647 X, Y: Integer;
648 Width, Height: Word;
649 Active: Boolean;
650 PanelID: DWORD;
651 end;
652 a, b, c, m, i, len: Integer;
653 ok: Boolean;
654 begin
655 if gWalls = nil then
656 Exit;
658 i := 0;
659 len := 128;
660 SetLength(PanelArray, len);
662 for a := 0 to High(gWalls) do
663 if gWalls[a].Door then
664 begin
665 PanelArray[i].X := gWalls[a].X;
666 PanelArray[i].Y := gWalls[a].Y;
667 PanelArray[i].Width := gWalls[a].Width;
668 PanelArray[i].Height := gWalls[a].Height;
669 PanelArray[i].Active := True;
670 PanelArray[i].PanelID := a;
672 i := i + 1;
673 if i = len then
674 begin
675 len := len + 128;
676 SetLength(PanelArray, len);
677 end;
678 end;
680 // Íåò äâåðåé:
681 if i = 0 then
682 begin
683 PanelArray := nil;
684 Exit;
685 end;
687 SetLength(gDoorMap, 0);
689 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
691 for a := 0 to i-1 do
692 if PanelArray[a].Active then
693 begin
694 PanelArray[a].Active := False;
695 m := Length(gDoorMap);
696 SetLength(gDoorMap, m+1);
697 SetLength(gDoorMap[m], 1);
698 gDoorMap[m, 0] := PanelArray[a].PanelID;
699 ok := True;
701 while ok do
702 begin
703 ok := False;
705 for b := 0 to i-1 do
706 if PanelArray[b].Active then
707 for c := 0 to High(gDoorMap[m]) do
708 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
709 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
710 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
711 PanelArray[b].Width, PanelArray[b].Height,
712 gWalls[gDoorMap[m, c]].X,
713 gWalls[gDoorMap[m, c]].Y,
714 gWalls[gDoorMap[m, c]].Width,
715 gWalls[gDoorMap[m, c]].Height) then
716 begin
717 PanelArray[b].Active := False;
718 SetLength(gDoorMap[m],
719 Length(gDoorMap[m])+1);
720 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
721 ok := True;
722 Break;
723 end;
724 end;
726 g_Game_StepLoading();
727 end;
729 PanelArray := nil;
730 end;
732 procedure CreateLiftMap();
733 var
734 PanelArray: Array of record
735 X, Y: Integer;
736 Width, Height: Word;
737 Active: Boolean;
738 end;
739 a, b, c, len, i, j: Integer;
740 ok: Boolean;
741 begin
742 if gLifts = nil then
743 Exit;
745 len := Length(gLifts);
746 SetLength(PanelArray, len);
748 for a := 0 to len-1 do
749 begin
750 PanelArray[a].X := gLifts[a].X;
751 PanelArray[a].Y := gLifts[a].Y;
752 PanelArray[a].Width := gLifts[a].Width;
753 PanelArray[a].Height := gLifts[a].Height;
754 PanelArray[a].Active := True;
755 end;
757 SetLength(gLiftMap, len);
758 i := 0;
760 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
762 for a := 0 to len-1 do
763 if PanelArray[a].Active then
764 begin
765 PanelArray[a].Active := False;
766 SetLength(gLiftMap[i], 32);
767 j := 0;
768 gLiftMap[i, j] := a;
769 ok := True;
771 while ok do
772 begin
773 ok := False;
774 for b := 0 to len-1 do
775 if PanelArray[b].Active then
776 for c := 0 to j do
777 if g_CollideAround(PanelArray[b].X,
778 PanelArray[b].Y,
779 PanelArray[b].Width,
780 PanelArray[b].Height,
781 PanelArray[gLiftMap[i, c]].X,
782 PanelArray[gLiftMap[i, c]].Y,
783 PanelArray[gLiftMap[i, c]].Width,
784 PanelArray[gLiftMap[i, c]].Height) then
785 begin
786 PanelArray[b].Active := False;
787 j := j+1;
788 if j > High(gLiftMap[i]) then
789 SetLength(gLiftMap[i],
790 Length(gLiftMap[i])+32);
792 gLiftMap[i, j] := b;
793 ok := True;
795 Break;
796 end;
797 end;
799 SetLength(gLiftMap[i], j+1);
800 i := i+1;
802 g_Game_StepLoading();
803 end;
805 SetLength(gLiftMap, i);
807 PanelArray := nil;
808 end;
810 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
811 var
812 len: Integer;
813 panels: ^TPanelArray;
814 pan: TPanel;
815 pguid: Integer;
816 begin
817 Result := -1;
819 case PanelRec.PanelType of
820 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
821 PANEL_BACK: panels := @gRenderBackgrounds;
822 PANEL_FORE: panels := @gRenderForegrounds;
823 PANEL_WATER: panels := @gWater;
824 PANEL_ACID1: panels := @gAcid1;
825 PANEL_ACID2: panels := @gAcid2;
826 PANEL_STEP: panels := @gSteps;
827 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
828 PANEL_BLOCKMON: panels := @gBlockMon;
829 else exit;
830 end;
832 len := Length(panels^);
833 SetLength(panels^, len+1);
835 pguid := Length(panByGUID);
836 SetLength(panByGUID, pguid+1); //FIXME!
837 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
838 assert(pguid >= 0);
839 assert(pguid < Length(panByGUID));
840 panByGUID[pguid] := pan;
841 panels^[len] := pan;
842 pan.arrIdx := len;
843 pan.proxyId := -1;
844 pan.tag := panelTypeToTag(PanelRec.PanelType);
846 PanelRec.user['panel_guid'] := pguid;
848 //result := len;
849 result := pguid;
850 end;
853 function CreateNullTexture(RecName: String): Integer;
854 begin
855 RecName := toLowerCase1251(RecName);
856 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
857 if TextNameHash.get(RecName, result) then exit; // i found her!
859 SetLength(Textures, Length(Textures)+1);
860 result := High(Textures);
862 with Textures[High(Textures)] do
863 begin
864 TextureName := RecName;
865 Width := 1;
866 Height := 1;
867 Anim := False;
868 TextureID := LongWord(TEXTURE_NONE);
869 end;
871 TextNameHash.put(RecName, result);
872 end;
875 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
876 var
877 WAD: TWADFile;
878 TextureData: Pointer;
879 WADName: String;
880 a, ResLength: Integer;
881 begin
882 RecName := toLowerCase1251(RecName);
883 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
884 if TextNameHash.get(RecName, result) then
885 begin
886 // i found her!
887 //e_LogWritefln('texture ''%s'' already loaded', [RecName]);
888 exit;
889 end;
891 Result := -1;
893 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
896 if Textures <> nil then
897 begin
898 for a := 0 to High(Textures) do
899 begin
900 if (Textures[a].TextureName = RecName) then
901 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
902 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
903 Result := a;
904 Exit;
905 end;
906 end;
907 end;
910 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
911 if (RecName = TEXTURE_NAME_WATER) or
912 (RecName = TEXTURE_NAME_ACID1) or
913 (RecName = TEXTURE_NAME_ACID2) then
914 begin
915 SetLength(Textures, Length(Textures)+1);
917 with Textures[High(Textures)] do
918 begin
919 TextureName := RecName;
920 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
921 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
922 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
924 Anim := False;
925 end;
927 result := High(Textures);
928 TextNameHash.put(RecName, result);
929 Exit;
930 end;
932 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
933 WADName := g_ExtractWadName(RecName);
935 WAD := TWADFile.Create();
937 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
939 WAD.ReadFile(WADName);
941 //txname := RecName;
943 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
944 begin
945 FreeMem(TextureData);
946 RecName := 'COMMON\ALIEN';
947 end;
950 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
951 begin
952 SetLength(Textures, Length(Textures)+1);
953 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
954 begin
955 SetLength(Textures, Length(Textures)-1);
956 Exit;
957 end;
958 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
959 FreeMem(TextureData);
960 Textures[High(Textures)].TextureName := RecName;
961 Textures[High(Textures)].Anim := False;
963 result := High(Textures);
964 TextNameHash.put(RecName, result);
965 end
966 else // Íåò òàêîãî ðåóñðñà â WAD'å
967 begin
968 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
969 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
970 if log and (not BadTextNameHash.get(RecName, a)) then
971 begin
972 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
973 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
974 end;
975 BadTextNameHash.put(RecName, -1);
976 end;
978 WAD.Free();
979 end;
982 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
983 var
984 WAD: TWADFile;
985 TextureWAD: PChar = nil;
986 TextData: Pointer = nil;
987 TextureData: Pointer = nil;
988 cfg: TConfig = nil;
989 WADName: String;
990 ResLength: Integer;
991 TextureResource: String;
992 _width, _height, _framecount, _speed: Integer;
993 _backanimation: Boolean;
994 //imgfmt: string;
995 ia: TDynImageDataArray = nil;
996 f, c, frdelay, frloop: Integer;
997 begin
998 RecName := toLowerCase1251(RecName);
999 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
1000 if TextNameHash.get(RecName, result) then
1001 begin
1002 // i found her!
1003 //e_LogWritefln('animated texture ''%s'' already loaded', [RecName]);
1004 exit;
1005 end;
1007 result := -1;
1009 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1011 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1012 if BadTextNameHash.get(RecName, f) then
1013 begin
1014 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1015 exit;
1016 end;
1018 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1019 WADName := g_ExtractWadName(RecName);
1021 WAD := TWADFile.Create();
1022 try
1023 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1025 WAD.ReadFile(WADName);
1027 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1028 begin
1029 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1030 if log and (not BadTextNameHash.get(RecName, f)) then
1031 begin
1032 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1033 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1034 end;
1035 BadTextNameHash.put(RecName, -1);
1036 exit;
1037 end;
1039 {TEST
1040 if WADName = Map then
1041 begin
1042 //FreeMem(TextureWAD);
1043 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1044 end;
1047 WAD.FreeWAD();
1049 if ResLength < 6 then
1050 begin
1051 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
1052 BadTextNameHash.put(RecName, -1);
1053 exit;
1054 end;
1056 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1057 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1058 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1059 begin
1060 // íåò, ýòî ñóïåðìåí!
1061 if not WAD.ReadMemory(TextureWAD, ResLength) then
1062 begin
1063 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
1064 BadTextNameHash.put(RecName, -1);
1065 exit;
1066 end;
1068 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1069 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1070 begin
1071 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
1072 BadTextNameHash.put(RecName, -1);
1073 exit;
1074 end;
1076 cfg := TConfig.CreateMem(TextData, ResLength);
1078 TextureResource := cfg.ReadStr('', 'resource', '');
1079 if TextureResource = '' then
1080 begin
1081 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
1082 BadTextNameHash.put(RecName, -1);
1083 exit;
1084 end;
1086 _width := cfg.ReadInt('', 'framewidth', 0);
1087 _height := cfg.ReadInt('', 'frameheight', 0);
1088 _framecount := cfg.ReadInt('', 'framecount', 0);
1089 _speed := cfg.ReadInt('', 'waitcount', 0);
1090 _backanimation := cfg.ReadBool('', 'backanimation', False);
1092 cfg.Free();
1093 cfg := nil;
1095 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1096 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1097 begin
1098 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
1099 BadTextNameHash.put(RecName, -1);
1100 exit;
1101 end;
1103 WAD.Free();
1104 WAD := nil;
1106 SetLength(Textures, Length(Textures)+1);
1107 with Textures[High(Textures)] do
1108 begin
1109 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1110 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1111 begin
1112 TextureName := RecName;
1113 Width := _width;
1114 Height := _height;
1115 Anim := True;
1116 FramesCount := _framecount;
1117 Speed := _speed;
1118 result := High(Textures);
1119 TextNameHash.put(RecName, result);
1120 end
1121 else
1122 begin
1123 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1124 if log and (not BadTextNameHash.get(RecName, f)) then
1125 begin
1126 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1127 end;
1128 BadTextNameHash.put(RecName, -1);
1129 end;
1130 end;
1131 end
1132 else
1133 begin
1134 // try animated image
1136 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1137 if length(imgfmt) = 0 then
1138 begin
1139 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1140 exit;
1141 end;
1143 GlobalMetadata.ClearMetaItems();
1144 GlobalMetadata.ClearMetaItemsForSaving();
1145 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1146 begin
1147 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
1148 BadTextNameHash.put(RecName, -1);
1149 exit;
1150 end;
1151 if length(ia) = 0 then
1152 begin
1153 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
1154 BadTextNameHash.put(RecName, -1);
1155 exit;
1156 end;
1158 WAD.Free();
1159 WAD := nil;
1161 _width := ia[0].width;
1162 _height := ia[0].height;
1163 _framecount := length(ia);
1164 _speed := 1;
1165 _backanimation := false;
1166 frdelay := -1;
1167 frloop := -666;
1168 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1169 begin
1170 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1171 try
1172 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1173 frdelay := f;
1174 if f < 0 then f := 0;
1175 // rounding ;-)
1176 c := f mod 28;
1177 if c < 13 then c := 0 else c := 1;
1178 f := (f div 28)+c;
1179 if f < 1 then f := 1 else if f > 255 then f := 255;
1180 _speed := f;
1181 except
1182 end;
1183 end;
1184 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1185 begin
1186 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1187 try
1188 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1189 frloop := f;
1190 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1191 except
1192 end;
1193 end;
1194 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1195 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1196 f := ord(_backanimation);
1197 e_WriteLog(Format('Animated texture file "%s": %d frames (delay:%d; back:%d; frdelay:%d; frloop:%d), %dx%d', [RecName, length(ia), _speed, f, frdelay, frloop, _width, _height]), MSG_NOTIFY);
1199 SetLength(Textures, Length(Textures)+1);
1200 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1201 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1202 begin
1203 Textures[High(Textures)].TextureName := RecName;
1204 Textures[High(Textures)].Width := _width;
1205 Textures[High(Textures)].Height := _height;
1206 Textures[High(Textures)].Anim := True;
1207 Textures[High(Textures)].FramesCount := length(ia);
1208 Textures[High(Textures)].Speed := _speed;
1209 result := High(Textures);
1210 TextNameHash.put(RecName, result);
1211 //writeln(' CREATED!');
1212 end
1213 else
1214 begin
1215 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1216 if log and (not BadTextNameHash.get(RecName, f)) then
1217 begin
1218 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
1219 end;
1220 BadTextNameHash.put(RecName, -1);
1221 end;
1222 end;
1223 finally
1224 for f := 0 to High(ia) do FreeImage(ia[f]);
1225 WAD.Free();
1226 cfg.Free();
1227 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1228 if (TextData <> nil) then FreeMem(TextData);
1229 if (TextureData <> nil) then FreeMem(TextureData);
1230 end;
1231 end;
1233 procedure CreateItem(Item: TDynRecord);
1234 begin
1235 if g_Game_IsClient then Exit;
1237 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1238 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1239 Exit;
1241 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1242 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1243 end;
1245 procedure CreateArea(Area: TDynRecord);
1246 var
1247 a: Integer;
1248 id: DWORD = 0;
1249 begin
1250 case Area.AreaType of
1251 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1252 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1253 begin
1254 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1255 with RespawnPoints[High(RespawnPoints)] do
1256 begin
1257 X := Area.X;
1258 Y := Area.Y;
1259 Direction := TDirection(Area.Direction);
1261 case Area.AreaType of
1262 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1263 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1264 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1265 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1266 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1267 end;
1268 end;
1269 end;
1271 AREA_REDFLAG, AREA_BLUEFLAG:
1272 begin
1273 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1275 if FlagPoints[a] <> nil then Exit;
1277 New(FlagPoints[a]);
1279 with FlagPoints[a]^ do
1280 begin
1281 X := Area.X-FLAGRECT.X;
1282 Y := Area.Y-FLAGRECT.Y;
1283 Direction := TDirection(Area.Direction);
1284 end;
1286 with gFlags[a] do
1287 begin
1288 case a of
1289 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1290 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1291 end;
1293 Animation := TAnimation.Create(id, True, 8);
1294 Obj.Rect := FLAGRECT;
1296 g_Map_ResetFlag(a);
1297 end;
1298 end;
1300 AREA_DOMFLAG:
1301 begin
1302 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1303 with DOMFlagPoints[High(DOMFlagPoints)] do
1304 begin
1305 X := Area.X;
1306 Y := Area.Y;
1307 Direction := TDirection(Area.Direction);
1308 end;
1310 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1311 end;
1312 end;
1313 end;
1315 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer; fTexturePanel1Type, fTexturePanel2Type: Word): Integer;
1316 var
1317 _trigger: TTrigger;
1318 begin
1319 result := -1;
1320 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1322 with _trigger do
1323 begin
1324 mapId := Trigger.id;
1325 mapIndex := amapIdx;
1326 X := Trigger.X;
1327 Y := Trigger.Y;
1328 Width := Trigger.Width;
1329 Height := Trigger.Height;
1330 Enabled := Trigger.Enabled;
1331 //TexturePanel := Trigger.TexturePanel;
1332 TexturePanelGUID := atpanid;
1333 TexturePanelType := fTexturePanel1Type;
1334 ShotPanelType := fTexturePanel2Type;
1335 TriggerType := Trigger.TriggerType;
1336 ActivateType := Trigger.ActivateType;
1337 Keys := Trigger.Keys;
1338 trigPanelGUID := atrigpanid;
1339 //trigShotPanelId := ashotpanid;
1340 //Data.Default := Trigger.DATA;
1341 end;
1343 result := Integer(g_Triggers_Create(_trigger, Trigger));
1344 end;
1346 procedure CreateMonster(monster: TDynRecord);
1347 var
1348 a: Integer;
1349 mon: TMonster;
1350 begin
1351 if g_Game_IsClient then Exit;
1353 if (gGameSettings.GameType = GT_SINGLE)
1354 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1355 begin
1356 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1358 if gTriggers <> nil then
1359 begin
1360 for a := 0 to High(gTriggers) do
1361 begin
1362 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1363 begin
1364 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1365 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1366 end;
1367 end;
1368 end;
1370 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1371 end;
1372 end;
1374 procedure g_Map_ReAdd_DieTriggers();
1376 function monsDieTrig (mon: TMonster): Boolean;
1377 var
1378 a: Integer;
1379 //tw: TStrTextWriter;
1380 begin
1381 result := false; // don't stop
1382 mon.ClearTriggers();
1383 for a := 0 to High(gTriggers) do
1384 begin
1385 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1386 begin
1387 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1389 tw := TStrTextWriter.Create();
1390 try
1391 gTriggers[a].trigData.writeTo(tw);
1392 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1393 finally
1394 tw.Free();
1395 end;
1397 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1398 end;
1399 end;
1400 end;
1402 begin
1403 if g_Game_IsClient then Exit;
1405 g_Mons_ForEach(monsDieTrig);
1406 end;
1408 function extractWadName(resourceName: string): string;
1409 var
1410 posN: Integer;
1411 begin
1412 posN := Pos(':', resourceName);
1413 if posN > 0 then
1414 Result:= Copy(resourceName, 0, posN-1)
1415 else
1416 Result := '';
1417 end;
1419 procedure addResToExternalResList(res: string);
1420 begin
1421 res := extractWadName(res);
1422 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1423 gExternalResources.Add(res);
1424 end;
1426 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1427 //var
1428 //textures: TTexturesRec1Array;
1429 //textures: TDynField;
1430 //trec: TDynRecord;
1431 //mapHeader: TMapHeaderRec_1;
1432 //i: integer;
1433 //resFile: String = '';
1434 begin
1435 if gExternalResources = nil then
1436 gExternalResources := TStringList.Create;
1438 gExternalResources.Clear;
1440 (*
1442 textures := GetTextures(map);
1443 for i := 0 to High(textures) do
1444 begin
1445 addResToExternalResList(resFile);
1446 end;
1449 textures := map['texture'];
1450 if (textures <> nil) then
1451 begin
1452 for trec in textures do
1453 begin
1454 addResToExternalResList(resFile);
1455 end;
1456 end;
1458 textures := nil;
1459 *)
1461 //mapHeader := GetMapHeader(map);
1463 addResToExternalResList(map.MusicName);
1464 addResToExternalResList(map.SkyName);
1465 end;
1468 procedure mapCreateGrid ();
1469 var
1470 mapX0: Integer = $3fffffff;
1471 mapY0: Integer = $3fffffff;
1472 mapX1: Integer = -$3fffffff;
1473 mapY1: Integer = -$3fffffff;
1475 procedure calcBoundingBox (constref panels: TPanelArray);
1476 var
1477 idx: Integer;
1478 pan: TPanel;
1479 begin
1480 for idx := 0 to High(panels) do
1481 begin
1482 pan := panels[idx];
1483 if not pan.visvalid then continue;
1484 if (pan.Width < 1) or (pan.Height < 1) then continue;
1485 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1486 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1487 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1488 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1489 end;
1490 end;
1492 procedure addPanelsToGrid (constref panels: TPanelArray);
1493 var
1494 idx: Integer;
1495 pan: TPanel;
1496 newtag: Integer;
1497 begin
1498 //tag := panelTypeToTag(tag);
1499 for idx := 0 to High(panels) do
1500 begin
1501 pan := panels[idx];
1502 if not pan.visvalid then continue;
1503 if (pan.proxyId <> -1) then
1504 begin
1505 {$IF DEFINED(D2F_DEBUG)}
1506 e_WriteLog(Format('DUPLICATE wall #%d(%d) enabled (%d); type:%08x', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.PanelType]), MSG_NOTIFY);
1507 {$ENDIF}
1508 continue;
1509 end;
1510 case pan.PanelType of
1511 PANEL_WALL: newtag := GridTagWall;
1512 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1513 PANEL_BACK: newtag := GridTagBack;
1514 PANEL_FORE: newtag := GridTagFore;
1515 PANEL_WATER: newtag := GridTagWater;
1516 PANEL_ACID1: newtag := GridTagAcid1;
1517 PANEL_ACID2: newtag := GridTagAcid2;
1518 PANEL_STEP: newtag := GridTagStep;
1519 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1520 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1521 else continue; // oops
1522 end;
1523 pan.tag := newtag;
1525 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1526 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1527 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1528 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1530 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1531 begin
1532 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1533 end;
1535 {$ENDIF}
1536 end;
1537 end;
1539 begin
1540 mapGrid.Free();
1541 mapGrid := nil;
1543 calcBoundingBox(gWalls);
1544 calcBoundingBox(gRenderBackgrounds);
1545 calcBoundingBox(gRenderForegrounds);
1546 calcBoundingBox(gWater);
1547 calcBoundingBox(gAcid1);
1548 calcBoundingBox(gAcid2);
1549 calcBoundingBox(gSteps);
1550 calcBoundingBox(gLifts);
1551 calcBoundingBox(gBlockMon);
1553 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1555 if (mapX0 > 0) then mapX0 := 0;
1556 if (mapY0 > 0) then mapY0 := 0;
1558 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1559 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1561 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1562 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1564 addPanelsToGrid(gWalls);
1565 addPanelsToGrid(gRenderBackgrounds);
1566 addPanelsToGrid(gRenderForegrounds);
1567 addPanelsToGrid(gWater);
1568 addPanelsToGrid(gAcid1);
1569 addPanelsToGrid(gAcid2);
1570 addPanelsToGrid(gSteps);
1571 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1572 addPanelsToGrid(gBlockMon);
1574 mapGrid.dumpStats();
1576 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1577 end;
1580 function g_Map_Load(Res: String): Boolean;
1581 const
1582 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1583 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1584 type
1585 PTRec = ^TTRec;
1586 TTRec = record
1587 //TexturePanel: Integer;
1588 tnum: Integer;
1589 id: Integer;
1590 trigrec: TDynRecord;
1591 // texture pane;
1592 texPanelIdx: Integer;
1593 texPanel: TDynRecord;
1594 // "action" panel
1595 actPanelIdx: Integer;
1596 actPanel: TDynRecord;
1597 end;
1598 var
1599 WAD: TWADFile;
1600 mapReader: TDynRecord = nil;
1601 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1602 panels: TDynField = nil; //TPanelsRec1Array;
1603 items: TDynField = nil; //TItemsRec1Array;
1604 monsters: TDynField = nil; //TMonsterRec1Array;
1605 areas: TDynField = nil; //TAreasRec1Array;
1606 triggers: TDynField = nil; //TTriggersRec1Array;
1607 b, c, k: Integer;
1608 PanelID: DWORD;
1609 AddTextures: TAddTextureArray;
1610 TriggersTable: array of TTRec;
1611 FileName, mapResName, s, TexName: String;
1612 Data: Pointer;
1613 Len: Integer;
1614 ok, isAnim: Boolean;
1615 CurTex, ntn: Integer;
1616 rec, texrec: TDynRecord;
1617 pttit: PTRec;
1618 pannum, trignum, cnt, tgpid: Integer;
1619 stt: UInt64;
1620 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1621 //moveActive: Boolean;
1622 pan: TPanel;
1623 begin
1624 mapGrid.Free();
1625 mapGrid := nil;
1627 gCurrentMap.Free();
1628 gCurrentMap := nil;
1630 panByGUID := nil;
1632 Result := False;
1633 gMapInfo.Map := Res;
1634 TriggersTable := nil;
1635 mapReader := nil;
1637 sfsGCDisable(); // temporary disable removing of temporary volumes
1638 try
1639 // Çàãðóçêà WAD:
1640 FileName := g_ExtractWadName(Res);
1641 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1642 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1644 WAD := TWADFile.Create();
1645 if not WAD.ReadFile(FileName) then
1646 begin
1647 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1648 WAD.Free();
1649 Exit;
1650 end;
1652 //k8: why loader ignores path here?
1653 mapResName := g_ExtractFileName(Res);
1654 if not WAD.GetMapResource(mapResName, Data, Len) then
1655 begin
1656 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1657 WAD.Free();
1658 Exit;
1659 end;
1661 WAD.Free();
1663 if (Len < 4) then
1664 begin
1665 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1666 FreeMem(Data);
1667 exit;
1668 end;
1670 // Çàãðóçêà êàðòû:
1671 e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
1672 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1674 stt := curTimeMicro();
1676 try
1677 mapReader := g_Map_ParseMap(Data, Len);
1678 except
1679 mapReader.Free();
1680 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1681 FreeMem(Data);
1682 Exit;
1683 end;
1685 FreeMem(Data);
1687 if (mapReader = nil) then
1688 begin
1689 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1690 exit;
1691 end;
1693 generateExternalResourcesList(mapReader);
1694 mapTextureList := mapReader['texture'];
1695 // get all other lists here too
1696 panels := mapReader['panel'];
1697 triggers := mapReader['trigger'];
1698 items := mapReader['item'];
1699 areas := mapReader['area'];
1700 monsters := mapReader['monster'];
1702 // Çàãðóçêà îïèñàíèÿ êàðòû:
1703 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1704 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1706 with gMapInfo do
1707 begin
1708 Name := mapReader.MapName;
1709 Description := mapReader.MapDesc;
1710 Author := mapReader.MapAuthor;
1711 MusicName := mapReader.MusicName;
1712 SkyName := mapReader.SkyName;
1713 Height := mapReader.Height;
1714 Width := mapReader.Width;
1715 end;
1717 // Çàãðóçêà òåêñòóð:
1718 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1719 // Äîáàâëåíèå òåêñòóð â Textures[]:
1720 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1721 begin
1722 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1723 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1725 cnt := -1;
1726 for rec in mapTextureList do
1727 begin
1728 Inc(cnt);
1729 s := rec.Resource;
1730 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1731 e_WriteLog(Format(' Loading texture #%d: %s', [cnt, s]), MSG_NOTIFY);
1732 {$ENDIF}
1733 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1734 if rec.Anim then
1735 begin
1736 // Àíèìèðîâàííàÿ òåêñòóðà
1737 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1738 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1739 end
1740 else
1741 begin
1742 // Îáû÷íàÿ òåêñòóðà
1743 ntn := CreateTexture(rec.Resource, FileName, True);
1744 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1745 end;
1746 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1748 rec.tagInt := ntn; // remember texture number
1749 g_Game_StepLoading();
1750 end;
1752 // set panel tagInt to texture index
1753 if (panels <> nil) then
1754 begin
1755 for rec in panels do
1756 begin
1757 texrec := rec.TextureRec;
1758 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1759 end;
1760 end;
1761 end;
1763 // Çàãðóçêà òðèããåðîâ
1764 gTriggerClientID := 0;
1765 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1766 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1768 // Çàãðóçêà ïàíåëåé
1769 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1770 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1772 // check texture numbers for panels
1773 if (panels <> nil) and (panels.count > 0) then
1774 begin
1775 for rec in panels do
1776 begin
1777 if (rec.tagInt < 0) then
1778 begin
1779 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1780 result := false;
1781 exit;
1782 end;
1783 end;
1784 end;
1786 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1787 if (triggers <> nil) and (triggers.count > 0) then
1788 begin
1789 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1790 //SetLength(TriggersTable, triggers.count);
1791 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1793 SetLength(TriggersTable, triggers.count);
1794 trignum := -1;
1795 for rec in triggers do
1796 begin
1797 Inc(trignum);
1798 pttit := @TriggersTable[trignum];
1799 pttit.trigrec := rec;
1800 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1801 pttit.texPanelIdx := -1; // will be fixed later
1802 pttit.texPanel := rec.TexturePanelRec;
1803 // action panel
1804 pttit.actPanelIdx := -1;
1805 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1806 // set flag
1807 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1808 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1809 // update progress
1810 g_Game_StepLoading();
1811 end;
1812 end;
1814 // Ñîçäàåì ïàíåëè
1815 if (panels <> nil) and (panels.count > 0) then
1816 begin
1817 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1818 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1820 pannum := -1;
1821 for rec in panels do
1822 begin
1823 Inc(pannum);
1824 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1825 texrec := nil;
1826 SetLength(AddTextures, 0);
1827 CurTex := -1;
1828 ok := false;
1830 if (mapTextureList <> nil) then
1831 begin
1832 texrec := rec.TextureRec;
1833 ok := (texrec <> nil);
1834 end;
1836 if ok then
1837 begin
1838 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1839 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1840 ok := false;
1841 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1842 begin
1843 if rec.userPanelTrigRef then
1844 begin
1845 // e_LogWritefln('trigref for panel %s', [pannum]);
1846 ok := True;
1847 end;
1848 end;
1849 end;
1851 if ok then
1852 begin
1853 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1854 s := texrec.Resource;
1856 // Ñïåö-òåêñòóðû çàïðåùåíû
1857 if g_Map_IsSpecialTexture(s) then
1858 begin
1859 ok := false
1860 end
1861 else
1862 begin
1863 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1864 ok := g_Texture_NumNameFindStart(s);
1865 end;
1867 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1868 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1869 if ok then
1870 begin
1871 k := NNF_NAME_BEFORE;
1872 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1873 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1874 begin
1875 k := g_Texture_NumNameFindNext(TexName);
1877 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1878 begin
1879 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1880 if texrec.Anim then
1881 begin
1882 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1883 isAnim := True;
1884 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1885 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1886 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1887 if not ok then
1888 begin
1889 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1890 isAnim := False;
1891 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1892 ok := CreateTexture(TexName, FileName, False) >= 0;
1893 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1894 end;
1895 end
1896 else
1897 begin
1898 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1899 isAnim := False;
1900 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1901 ok := CreateTexture(TexName, FileName, False) >= 0;
1902 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1903 if not ok then
1904 begin
1905 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1906 isAnim := True;
1907 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1908 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1909 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1910 end;
1911 end;
1913 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1914 if ok then
1915 begin
1917 for c := 0 to High(Textures) do
1918 begin
1919 if (Textures[c].TextureName = TexName) then
1920 begin
1921 SetLength(AddTextures, Length(AddTextures)+1);
1922 AddTextures[High(AddTextures)].Texture := c;
1923 AddTextures[High(AddTextures)].Anim := isAnim;
1924 break;
1925 end;
1926 end;
1928 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1929 begin
1930 SetLength(AddTextures, Length(AddTextures)+1);
1931 AddTextures[High(AddTextures)].Texture := c;
1932 AddTextures[High(AddTextures)].Anim := isAnim;
1933 end;
1934 end;
1935 end
1936 else
1937 begin
1938 if k = NNF_NAME_EQUALS then
1939 begin
1940 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1941 SetLength(AddTextures, Length(AddTextures)+1);
1942 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1943 AddTextures[High(AddTextures)].Anim := texrec.Anim;
1944 CurTex := High(AddTextures);
1945 ok := true;
1946 end
1947 else // NNF_NO_NAME
1948 begin
1949 ok := false;
1950 end;
1951 end;
1952 end; // while ok...
1954 ok := true;
1955 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1956 end; // if ok - ññûëàþòñÿ òðèããåðû
1958 if not ok then
1959 begin
1960 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1961 SetLength(AddTextures, 1);
1962 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1963 AddTextures[0].Anim := false;
1964 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
1965 CurTex := 0;
1966 end;
1968 //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);
1970 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1972 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
1973 PanelID := CreatePanel(rec, AddTextures, CurTex);
1974 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
1975 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1977 // setup lifts
1978 moveSpeed := rec.moveSpeed;
1979 //moveStart := rec.moveStart;
1980 //moveEnd := rec.moveEnd;
1981 //moveActive := rec['move_active'].value;
1982 if not moveSpeed.isZero then
1983 begin
1984 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1985 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1986 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1987 end;
1989 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
1991 g_Game_StepLoading();
1992 end;
1993 end;
1995 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
1996 for b := 0 to High(TriggersTable) do
1997 begin
1998 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
1999 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2000 end;
2002 // create map grid, init other grids (for monsters, for example)
2003 e_WriteLog('Creating map grid', MSG_NOTIFY);
2004 mapCreateGrid();
2006 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2007 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2008 begin
2009 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2010 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2011 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2012 trignum := -1;
2013 for rec in triggers do
2014 begin
2015 Inc(trignum);
2016 if (TriggersTable[trignum].texPanel <> nil) then b := TriggersTable[trignum].texPanel.PanelType else b := 0;
2017 if (TriggersTable[trignum].actPanel <> nil) then c := TriggersTable[trignum].actPanel.PanelType else c := 0;
2018 // we can have only one of those
2019 tgpid := TriggersTable[trignum].actPanelIdx;
2020 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2021 TriggersTable[trignum].tnum := trignum;
2022 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid, Word(b), Word(c));
2023 end;
2024 end;
2026 //FIXME: use hashtable!
2027 for pan in panByGUID do
2028 begin
2029 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2030 begin
2031 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2032 end;
2033 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2034 begin
2035 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2036 end;
2037 end;
2039 // Çàãðóçêà ïðåäìåòîâ
2040 e_WriteLog(' Loading items...', MSG_NOTIFY);
2041 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2043 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2044 if (items <> nil) and not gLoadGameMode then
2045 begin
2046 e_WriteLog(' Spawning items...', MSG_NOTIFY);
2047 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2048 for rec in items do CreateItem(rec);
2049 end;
2051 // Çàãðóçêà îáëàñòåé
2052 e_WriteLog(' Loading areas...', MSG_NOTIFY);
2053 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2055 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2056 if areas <> nil then
2057 begin
2058 e_WriteLog(' Creating areas...', MSG_NOTIFY);
2059 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2060 for rec in areas do CreateArea(rec);
2061 end;
2063 // Çàãðóçêà ìîíñòðîâ
2064 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
2065 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2067 gTotalMonsters := 0;
2069 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2070 if (monsters <> nil) and not gLoadGameMode then
2071 begin
2072 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
2073 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2074 for rec in monsters do CreateMonster(rec);
2075 end;
2077 gCurrentMap := mapReader; // this will be our current map now
2078 gCurrentMapFileName := Res;
2079 mapReader := nil;
2081 // Çàãðóçêà íåáà
2082 if gMapInfo.SkyName <> '' then
2083 begin
2084 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
2085 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2086 FileName := g_ExtractWadName(gMapInfo.SkyName);
2088 if FileName <> '' then
2089 FileName := GameDir+'/wads/'+FileName
2090 else
2091 begin
2092 FileName := g_ExtractWadName(Res);
2093 end;
2095 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2096 if g_Texture_CreateWAD(BackID, s) then
2097 begin
2098 g_Game_SetupScreenSize();
2099 end
2100 else
2101 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2102 end;
2104 // Çàãðóçêà ìóçûêè
2105 ok := False;
2106 if gMapInfo.MusicName <> '' then
2107 begin
2108 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
2109 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2110 FileName := g_ExtractWadName(gMapInfo.MusicName);
2112 if FileName <> '' then
2113 FileName := GameDir+'/wads/'+FileName
2114 else
2115 begin
2116 FileName := g_ExtractWadName(Res);
2117 end;
2119 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2120 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2121 ok := True
2122 else
2123 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2124 end;
2126 // Îñòàëüíûå óñòàíâêè
2127 CreateDoorMap();
2128 CreateLiftMap();
2130 g_Items_Init();
2131 g_Weapon_Init();
2132 g_Monsters_Init();
2134 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2135 if not gLoadGameMode then g_GFX_Init();
2137 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2138 mapTextureList := nil;
2139 panels := nil;
2140 items := nil;
2141 areas := nil;
2142 triggers := nil;
2143 TriggersTable := nil;
2144 AddTextures := nil;
2146 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2147 if ok and (not gLoadGameMode) then
2148 begin
2149 gMusic.SetByName(gMapInfo.MusicName);
2150 gMusic.Play();
2151 end
2152 else
2153 begin
2154 gMusic.SetByName('');
2155 end;
2157 stt := curTimeMicro()-stt;
2158 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2159 finally
2160 sfsGCEnable(); // enable releasing unused volumes
2161 mapReader.Free();
2162 e_ClearInputBuffer(); // why not?
2163 end;
2165 e_WriteLog('Done loading map.', MSG_NOTIFY);
2166 Result := True;
2167 end;
2170 function g_Map_GetMapInfo(Res: String): TMapInfo;
2171 var
2172 WAD: TWADFile;
2173 mapReader: TDynRecord;
2174 //Header: TMapHeaderRec_1;
2175 FileName: String;
2176 Data: Pointer;
2177 Len: Integer;
2178 begin
2179 FillChar(Result, SizeOf(Result), 0);
2180 FileName := g_ExtractWadName(Res);
2182 WAD := TWADFile.Create();
2183 if not WAD.ReadFile(FileName) then
2184 begin
2185 WAD.Free();
2186 Exit;
2187 end;
2189 //k8: it ignores path again
2190 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2191 begin
2192 WAD.Free();
2193 Exit;
2194 end;
2196 WAD.Free();
2198 try
2199 mapReader := g_Map_ParseMap(Data, Len);
2200 except
2201 mapReader := nil;
2202 FreeMem(Data);
2203 exit;
2204 end;
2206 FreeMem(Data);
2208 if (mapReader = nil) then exit;
2210 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2211 begin
2212 Result.Name := mapReader.MapName;
2213 Result.Description := mapReader.MapDesc;
2214 Result.Map := Res;
2215 Result.Author := mapReader.MapAuthor;
2216 Result.Height := mapReader.Height;
2217 Result.Width := mapReader.Width;
2218 end
2219 else
2220 begin
2221 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2222 //ZeroMemory(@Header, SizeOf(Header));
2223 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2224 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2225 Result.Map := Res;
2226 Result.Author := '';
2227 Result.Height := 0;
2228 Result.Width := 0;
2229 end;
2231 mapReader.Free();
2232 end;
2234 function g_Map_GetMapsList(WADName: string): SArray;
2235 var
2236 WAD: TWADFile;
2237 a: Integer;
2238 ResList: SArray;
2239 begin
2240 Result := nil;
2241 WAD := TWADFile.Create();
2242 if not WAD.ReadFile(WADName) then
2243 begin
2244 WAD.Free();
2245 Exit;
2246 end;
2247 ResList := WAD.GetMapResources();
2248 if ResList <> nil then
2249 begin
2250 for a := 0 to High(ResList) do
2251 begin
2252 SetLength(Result, Length(Result)+1);
2253 Result[High(Result)] := ResList[a];
2254 end;
2255 end;
2256 WAD.Free();
2257 end;
2259 function g_Map_Exist(Res: string): Boolean;
2260 var
2261 WAD: TWADFile;
2262 FileName, mnn: string;
2263 ResList: SArray;
2264 a: Integer;
2265 begin
2266 Result := False;
2268 FileName := addWadExtension(g_ExtractWadName(Res));
2270 WAD := TWADFile.Create;
2271 if not WAD.ReadFile(FileName) then
2272 begin
2273 WAD.Free();
2274 Exit;
2275 end;
2277 ResList := WAD.GetMapResources();
2278 WAD.Free();
2280 mnn := g_ExtractFileName(Res);
2281 if ResList <> nil then
2282 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2283 begin
2284 Result := True;
2285 Exit;
2286 end;
2287 end;
2289 procedure g_Map_Free(freeTextures: Boolean=true);
2290 var
2291 a: Integer;
2293 procedure FreePanelArray(var panels: TPanelArray);
2294 var
2295 i: Integer;
2297 begin
2298 if panels <> nil then
2299 begin
2300 for i := 0 to High(panels) do
2301 panels[i].Free();
2302 panels := nil;
2303 end;
2304 end;
2306 begin
2307 g_GFX_Free();
2308 g_Weapon_Free();
2309 g_Items_Free();
2310 g_Triggers_Free();
2311 g_Monsters_Free();
2313 RespawnPoints := nil;
2314 if FlagPoints[FLAG_RED] <> nil then
2315 begin
2316 Dispose(FlagPoints[FLAG_RED]);
2317 FlagPoints[FLAG_RED] := nil;
2318 end;
2319 if FlagPoints[FLAG_BLUE] <> nil then
2320 begin
2321 Dispose(FlagPoints[FLAG_BLUE]);
2322 FlagPoints[FLAG_BLUE] := nil;
2323 end;
2324 //DOMFlagPoints := nil;
2326 //gDOMFlags := nil;
2328 if (Length(gCurrentMapFileName) <> 0) then
2329 begin
2330 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2331 end
2332 else
2333 begin
2334 e_LogWritefln('g_Map_Free: no previous map.', []);
2335 end;
2336 if freeTextures then
2337 begin
2338 e_LogWritefln('g_Map_Free: clearing textures...', []);
2339 if (Textures <> nil) then
2340 begin
2341 for a := 0 to High(Textures) do
2342 begin
2343 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2344 begin
2345 if Textures[a].Anim then
2346 begin
2347 g_Frames_DeleteByID(Textures[a].FramesID)
2348 end
2349 else
2350 begin
2351 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2352 begin
2353 e_DeleteTexture(Textures[a].TextureID);
2354 end;
2355 end;
2356 end;
2357 end;
2358 Textures := nil;
2359 end;
2360 TextNameHash.Free();
2361 TextNameHash := nil;
2362 BadTextNameHash.Free();
2363 BadTextNameHash := nil;
2364 end;
2366 panByGUID := nil;
2368 FreePanelArray(gWalls);
2369 FreePanelArray(gRenderBackgrounds);
2370 FreePanelArray(gRenderForegrounds);
2371 FreePanelArray(gWater);
2372 FreePanelArray(gAcid1);
2373 FreePanelArray(gAcid2);
2374 FreePanelArray(gSteps);
2375 FreePanelArray(gLifts);
2376 FreePanelArray(gBlockMon);
2377 gMovingWallIds := nil;
2379 if BackID <> DWORD(-1) then
2380 begin
2381 gBackSize.X := 0;
2382 gBackSize.Y := 0;
2383 e_DeleteTexture(BackID);
2384 BackID := DWORD(-1);
2385 end;
2387 g_Game_StopAllSounds(False);
2388 gMusic.FreeSound();
2389 g_Sound_Delete(gMapInfo.MusicName);
2391 gMapInfo.Name := '';
2392 gMapInfo.Description := '';
2393 gMapInfo.MusicName := '';
2394 gMapInfo.Height := 0;
2395 gMapInfo.Width := 0;
2397 gDoorMap := nil;
2398 gLiftMap := nil;
2399 end;
2401 procedure g_Map_Update();
2402 var
2403 a, d, j: Integer;
2404 m: Word;
2405 s: String;
2407 procedure UpdatePanelArray(var panels: TPanelArray);
2408 var
2409 i: Integer;
2411 begin
2412 for i := 0 to High(panels) do panels[i].Update();
2413 end;
2415 begin
2416 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2418 UpdatePanelArray(gWalls);
2419 UpdatePanelArray(gRenderBackgrounds);
2420 UpdatePanelArray(gRenderForegrounds);
2421 UpdatePanelArray(gWater);
2422 UpdatePanelArray(gAcid1);
2423 UpdatePanelArray(gAcid2);
2424 UpdatePanelArray(gSteps);
2426 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2428 if gGameSettings.GameMode = GM_CTF then
2429 begin
2430 for a := FLAG_RED to FLAG_BLUE do
2431 begin
2432 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2433 begin
2434 with gFlags[a] do
2435 begin
2436 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2438 m := g_Obj_Move(@Obj, True, True);
2440 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2442 // Ñîïðîòèâëåíèå âîçäóõà
2443 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2445 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2446 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2447 begin
2448 g_Map_ResetFlag(a);
2449 gFlags[a].CaptureTime := 0;
2450 if a = FLAG_RED then
2451 s := _lc[I_PLAYER_FLAG_RED]
2452 else
2453 s := _lc[I_PLAYER_FLAG_BLUE];
2454 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2456 if g_Game_IsNet then
2457 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2458 Continue;
2459 end;
2461 if Count > 0 then Count -= 1;
2463 // Èãðîê áåðåò ôëàã
2464 if gPlayers <> nil then
2465 begin
2466 j := Random(Length(gPlayers)) - 1;
2467 for d := 0 to High(gPlayers) do
2468 begin
2469 Inc(j);
2470 if j > High(gPlayers) then j := 0;
2471 if gPlayers[j] <> nil then
2472 begin
2473 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2474 begin
2475 if gPlayers[j].GetFlag(a) then Break;
2476 end;
2477 end;
2478 end;
2479 end;
2480 end;
2481 end;
2482 end;
2483 end;
2484 end;
2487 // old algo
2488 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2490 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2491 var
2492 idx: Integer;
2493 begin
2494 if (panels <> nil) then
2495 begin
2496 // alas, no visible set
2497 for idx := 0 to High(panels) do
2498 begin
2499 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2500 end;
2501 end;
2502 end;
2504 begin
2505 case PanelType of
2506 PANEL_WALL: DrawPanels(gWalls);
2507 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2508 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2509 PANEL_FORE: DrawPanels(gRenderForegrounds);
2510 PANEL_WATER: DrawPanels(gWater);
2511 PANEL_ACID1: DrawPanels(gAcid1);
2512 PANEL_ACID2: DrawPanels(gAcid2);
2513 PANEL_STEP: DrawPanels(gSteps);
2514 end;
2515 end;
2518 // new algo
2519 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2521 function checker (pan: TPanel; tag: Integer): Boolean;
2522 begin
2523 result := false; // don't stop, ever
2524 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2525 gDrawPanelList.insert(pan);
2526 end;
2528 begin
2529 dplClear();
2530 //tagmask := panelTypeToTag(PanelType);
2531 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2532 // list will be rendered in `g_game.DrawPlayer()`
2533 end;
2536 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2538 function checker (pan: TPanel; tag: Integer): Boolean;
2539 begin
2540 result := false; // don't stop, ever
2541 pan.DrawShadowVolume(lightX, lightY, radius);
2542 end;
2544 begin
2545 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2546 end;
2549 procedure g_Map_DrawBack(dx, dy: Integer);
2550 begin
2551 if gDrawBackGround and (BackID <> DWORD(-1)) then
2552 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2553 else
2554 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2555 end;
2557 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2558 PanelType: Word; b1x3: Boolean=false): Boolean;
2559 var
2560 a, h: Integer;
2561 begin
2562 Result := False;
2564 if WordBool(PanelType and PANEL_WALL) then
2565 if gWalls <> nil then
2566 begin
2567 h := High(gWalls);
2569 for a := 0 to h do
2570 if gWalls[a].Enabled and
2571 g_Collide(X, Y, Width, Height,
2572 gWalls[a].X, gWalls[a].Y,
2573 gWalls[a].Width, gWalls[a].Height) then
2574 begin
2575 Result := True;
2576 Exit;
2577 end;
2578 end;
2580 if WordBool(PanelType and PANEL_WATER) then
2581 if gWater <> nil then
2582 begin
2583 h := High(gWater);
2585 for a := 0 to h do
2586 if g_Collide(X, Y, Width, Height,
2587 gWater[a].X, gWater[a].Y,
2588 gWater[a].Width, gWater[a].Height) then
2589 begin
2590 Result := True;
2591 Exit;
2592 end;
2593 end;
2595 if WordBool(PanelType and PANEL_ACID1) then
2596 if gAcid1 <> nil then
2597 begin
2598 h := High(gAcid1);
2600 for a := 0 to h do
2601 if g_Collide(X, Y, Width, Height,
2602 gAcid1[a].X, gAcid1[a].Y,
2603 gAcid1[a].Width, gAcid1[a].Height) then
2604 begin
2605 Result := True;
2606 Exit;
2607 end;
2608 end;
2610 if WordBool(PanelType and PANEL_ACID2) then
2611 if gAcid2 <> nil then
2612 begin
2613 h := High(gAcid2);
2615 for a := 0 to h do
2616 if g_Collide(X, Y, Width, Height,
2617 gAcid2[a].X, gAcid2[a].Y,
2618 gAcid2[a].Width, gAcid2[a].Height) then
2619 begin
2620 Result := True;
2621 Exit;
2622 end;
2623 end;
2625 if WordBool(PanelType and PANEL_STEP) then
2626 if gSteps <> nil then
2627 begin
2628 h := High(gSteps);
2630 for a := 0 to h do
2631 if g_Collide(X, Y, Width, Height,
2632 gSteps[a].X, gSteps[a].Y,
2633 gSteps[a].Width, gSteps[a].Height) then
2634 begin
2635 Result := True;
2636 Exit;
2637 end;
2638 end;
2640 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2641 if gLifts <> nil then
2642 begin
2643 h := High(gLifts);
2645 for a := 0 to h do
2646 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2647 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2648 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2649 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2650 g_Collide(X, Y, Width, Height,
2651 gLifts[a].X, gLifts[a].Y,
2652 gLifts[a].Width, gLifts[a].Height) then
2653 begin
2654 Result := True;
2655 Exit;
2656 end;
2657 end;
2659 if WordBool(PanelType and PANEL_BLOCKMON) then
2660 if gBlockMon <> nil then
2661 begin
2662 h := High(gBlockMon);
2664 for a := 0 to h do
2665 if ( (not b1x3) or
2666 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2667 g_Collide(X, Y, Width, Height,
2668 gBlockMon[a].X, gBlockMon[a].Y,
2669 gBlockMon[a].Width, gBlockMon[a].Height) then
2670 begin
2671 Result := True;
2672 Exit;
2673 end;
2674 end;
2675 end;
2677 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2678 var
2679 texid: DWORD;
2681 function checkPanels (constref panels: TPanelArray): Boolean;
2682 var
2683 a: Integer;
2684 begin
2685 result := false;
2686 if panels = nil then exit;
2687 for a := 0 to High(panels) do
2688 begin
2689 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2690 begin
2691 result := true;
2692 texid := panels[a].GetTextureID();
2693 exit;
2694 end;
2695 end;
2696 end;
2698 begin
2699 texid := LongWord(TEXTURE_NONE);
2700 result := texid;
2701 if not checkPanels(gWater) then
2702 if not checkPanels(gAcid1) then
2703 if not checkPanels(gAcid2) then exit;
2704 result := texid;
2705 end;
2708 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2709 const
2710 SlowMask = GridTagLift or GridTagBlockMon;
2711 function checker (pan: TPanel; tag: Integer): Boolean;
2712 begin
2714 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2715 begin
2716 result := pan.Enabled;
2717 exit;
2718 end;
2721 if ((tag and GridTagLift) <> 0) then
2722 begin
2723 result :=
2724 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2725 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2726 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2727 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2728 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2729 exit;
2730 end;
2732 if ((tag and GridTagBlockMon) <> 0) then
2733 begin
2734 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2735 exit;
2736 end;
2738 // other shit
2739 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2740 result := true; // i found her!
2741 end;
2743 var
2744 tagmask: Integer = 0;
2745 begin
2746 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2747 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2748 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2749 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2750 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2751 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2752 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2754 if (tagmask = 0) then begin result := false; exit; end; // just in case
2756 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2757 if gdbg_map_use_accel_coldet then
2758 begin
2759 if (Width = 1) and (Height = 1) then
2760 begin
2761 if ((tagmask and SlowMask) <> 0) then
2762 begin
2763 // slow
2764 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2765 end
2766 else
2767 begin
2768 // fast
2769 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2770 end;
2771 end
2772 else
2773 begin
2774 if ((tagmask and SlowMask) <> 0) then
2775 begin
2776 // slow
2777 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2778 end
2779 else
2780 begin
2781 // fast
2782 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2783 end;
2784 end;
2785 end
2786 else
2787 begin
2788 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2789 end;
2790 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2791 end;
2794 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2795 var
2796 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2797 texid: DWORD;
2799 // slightly different from the old code, but meh...
2800 function checker (pan: TPanel; tag: Integer): Boolean;
2801 begin
2802 result := false; // don't stop, ever
2803 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2804 // check priorities
2805 case cctype of
2806 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2807 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2808 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2809 end;
2810 // collision?
2811 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2812 // yeah
2813 texid := pan.GetTextureID();
2814 // water? water has the highest priority, so stop right here
2815 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2816 // acid2?
2817 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2818 // acid1?
2819 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2820 end;
2822 begin
2823 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2824 if gdbg_map_use_accel_coldet then
2825 begin
2826 texid := LongWord(TEXTURE_NONE);
2827 if (Width = 1) and (Height = 1) then
2828 begin
2829 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2830 end
2831 else
2832 begin
2833 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2834 end;
2835 result := texid;
2836 end
2837 else
2838 begin
2839 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2840 end;
2841 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2842 end;
2845 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2846 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2847 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2850 procedure g_Map_EnableWallGUID (pguid: Integer);
2851 var
2852 pan: TPanel;
2853 begin
2854 //pan := gWalls[ID];
2855 pan := g_Map_PanelByGUID(pguid);
2856 if (pan = nil) then exit;
2857 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2859 pan.Enabled := True;
2860 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2862 mapGrid.proxyEnabled[pan.proxyId] := true;
2863 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2864 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2866 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2867 // mark platform as interesting
2868 pan.setDirty();
2870 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2871 //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);
2872 {$ENDIF}
2873 end;
2876 procedure g_Map_DisableWallGUID (pguid: Integer);
2877 var
2878 pan: TPanel;
2879 begin
2880 //pan := gWalls[ID];
2881 pan := g_Map_PanelByGUID(pguid);
2882 if (pan = nil) then exit;
2883 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2885 pan.Enabled := False;
2886 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2888 mapGrid.proxyEnabled[pan.proxyId] := false;
2889 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2891 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2892 // mark platform as interesting
2893 pan.setDirty();
2895 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2896 //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);
2897 {$ENDIF}
2898 end;
2901 procedure g_Map_SwitchTextureGUID (PanelType: Word; pguid: Integer; AnimLoop: Byte = 0);
2902 var
2903 tp: TPanel;
2904 begin
2905 tp := g_Map_PanelByGUID(pguid);
2906 if (tp = nil) then exit;
2908 case PanelType of
2909 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: tp := gWalls[ID];
2910 PANEL_FORE: tp := gRenderForegrounds[ID];
2911 PANEL_BACK: tp := gRenderBackgrounds[ID];
2912 PANEL_WATER: tp := gWater[ID];
2913 PANEL_ACID1: tp := gAcid1[ID];
2914 PANEL_ACID2: tp := gAcid2[ID];
2915 PANEL_STEP: tp := gSteps[ID];
2916 else exit;
2917 end;
2920 tp.NextTexture(AnimLoop);
2921 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
2922 end;
2925 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2926 var
2927 pan: TPanel;
2928 begin
2929 //pan := gLifts[ID];
2930 pan := g_Map_PanelByGUID(pguid);
2931 if (pan = nil) then exit;
2932 if not pan.isGLift then exit;
2934 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2936 with {gLifts[ID]} pan do
2937 begin
2938 LiftType := t;
2940 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2941 //TODO: make separate lift tags, and change tag here
2943 case LiftType of
2944 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2945 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2946 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2947 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2948 end;
2950 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2951 // mark platform as interesting
2952 pan.setDirty();
2953 end;
2954 end;
2957 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2958 var
2959 a: Integer;
2960 PointsArray: Array of TRespawnPoint;
2961 begin
2962 Result := False;
2963 SetLength(PointsArray, 0);
2965 if RespawnPoints = nil then
2966 Exit;
2968 for a := 0 to High(RespawnPoints) do
2969 if RespawnPoints[a].PointType = PointType then
2970 begin
2971 SetLength(PointsArray, Length(PointsArray)+1);
2972 PointsArray[High(PointsArray)] := RespawnPoints[a];
2973 end;
2975 if PointsArray = nil then
2976 Exit;
2978 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2979 Result := True;
2980 end;
2982 function g_Map_GetPointCount(PointType: Byte): Word;
2983 var
2984 a: Integer;
2985 begin
2986 Result := 0;
2988 if RespawnPoints = nil then
2989 Exit;
2991 for a := 0 to High(RespawnPoints) do
2992 if RespawnPoints[a].PointType = PointType then
2993 Result := Result + 1;
2994 end;
2996 function g_Map_HaveFlagPoints(): Boolean;
2997 begin
2998 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2999 end;
3001 procedure g_Map_ResetFlag(Flag: Byte);
3002 begin
3003 with gFlags[Flag] do
3004 begin
3005 Obj.X := -1000;
3006 Obj.Y := -1000;
3007 Obj.Vel.X := 0;
3008 Obj.Vel.Y := 0;
3009 Direction := D_LEFT;
3010 State := FLAG_STATE_NONE;
3011 if FlagPoints[Flag] <> nil then
3012 begin
3013 Obj.X := FlagPoints[Flag]^.X;
3014 Obj.Y := FlagPoints[Flag]^.Y;
3015 Direction := FlagPoints[Flag]^.Direction;
3016 State := FLAG_STATE_NORMAL;
3017 end;
3018 Count := -1;
3019 end;
3020 end;
3022 procedure g_Map_DrawFlags();
3023 var
3024 i, dx: Integer;
3025 Mirror: TMirrorType;
3026 begin
3027 if gGameSettings.GameMode <> GM_CTF then
3028 Exit;
3030 for i := FLAG_RED to FLAG_BLUE do
3031 with gFlags[i] do
3032 if State <> FLAG_STATE_CAPTURED then
3033 begin
3034 if State = FLAG_STATE_NONE then
3035 continue;
3037 if Direction = D_LEFT then
3038 begin
3039 Mirror := M_HORIZONTAL;
3040 dx := -1;
3041 end
3042 else
3043 begin
3044 Mirror := M_NONE;
3045 dx := 1;
3046 end;
3048 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3050 if g_debug_Frames then
3051 begin
3052 e_DrawQuad(Obj.X+Obj.Rect.X,
3053 Obj.Y+Obj.Rect.Y,
3054 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3055 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3056 0, 255, 0);
3057 end;
3058 end;
3059 end;
3062 procedure g_Map_SaveState (var Mem: TBinMemoryWriter);
3063 var
3064 dw: DWORD;
3065 b: Byte;
3066 str: String;
3067 boo: Boolean;
3069 procedure savePanels ();
3070 var
3071 PAMem: TBinMemoryWriter;
3072 pan: TPanel;
3073 begin
3074 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé
3075 PAMem := TBinMemoryWriter.Create((Length(panByGUID)+1) * 40);
3077 // Ñîõðàíÿåì ïàíåëè
3078 //Mem.WriteInt(Length(panByGUID));
3079 for pan in panByGUID do pan.SaveState(PAMem);
3081 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé
3082 PAMem.SaveToMemory(Mem);
3083 PAMem.Free();
3084 end;
3086 procedure SaveFlag (flag: PFlag);
3087 begin
3088 // Ñèãíàòóðà ôëàãà
3089 dw := FLAG_SIGNATURE; // 'FLAG'
3090 Mem.WriteDWORD(dw);
3091 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3092 Mem.WriteByte(flag^.RespawnType);
3093 // Ñîñòîÿíèå ôëàãà
3094 Mem.WriteByte(flag^.State);
3095 // Íàïðàâëåíèå ôëàãà
3096 if flag^.Direction = D_LEFT then b := 1 else b := 2; // D_RIGHT
3097 Mem.WriteByte(b);
3098 // Îáúåêò ôëàãà
3099 Obj_SaveState(@flag^.Obj, Mem);
3100 end;
3102 begin
3103 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
3105 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
3106 savePanels();
3107 ///// /////
3109 ///// Ñîõðàíÿåì ìóçûêó: /////
3110 // Ñèãíàòóðà ìóçûêè:
3111 dw := MUSIC_SIGNATURE; // 'MUSI'
3112 Mem.WriteDWORD(dw);
3113 // Íàçâàíèå ìóçûêè:
3114 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3115 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3116 Mem.WriteString(str, 64);
3117 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3118 dw := gMusic.GetPosition();
3119 Mem.WriteDWORD(dw);
3120 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3121 boo := gMusic.SpecPause;
3122 Mem.WriteBoolean(boo);
3123 ///// /////
3125 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3126 Mem.WriteInt(gTotalMonsters);
3127 ///// /////
3129 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3130 if gGameSettings.GameMode = GM_CTF then
3131 begin
3132 // Ôëàã Êðàñíîé êîìàíäû
3133 SaveFlag(@gFlags[FLAG_RED]);
3134 // Ôëàã Ñèíåé êîìàíäû
3135 SaveFlag(@gFlags[FLAG_BLUE]);
3136 end;
3137 ///// /////
3139 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3140 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3141 begin
3142 // Î÷êè Êðàñíîé êîìàíäû
3143 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
3144 // Î÷êè Ñèíåé êîìàíäû
3145 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
3146 end;
3147 ///// /////
3148 end;
3151 procedure g_Map_LoadState (var Mem: TBinMemoryReader);
3152 var
3153 dw: DWORD;
3154 b: Byte;
3155 str: String;
3156 boo: Boolean;
3158 procedure loadPanels ();
3159 var
3160 PAMem: TBinMemoryReader;
3161 pan: TPanel;
3162 //count: LongInt;
3163 begin
3164 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé
3165 PAMem := TBinMemoryReader.Create();
3166 PAMem.LoadFromMemory(Mem);
3168 // Çàãðóæàåì ïàíåëè
3169 //PAMem.ReadInt(count);
3170 //if (count <> Length(panByGUID)) then raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: invalid number of panels');
3171 //if (count <> Length(panByGUID)) then raise EBinSizeError.Create(Format('g_Map_LoadState: LoadPanelArray: invalid number of panels (%d : %d)', [count, Length(panByGUID)]));
3172 for pan in panByGUID do
3173 begin
3174 pan.LoadState(PAMem);
3175 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3176 end;
3178 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí
3179 PAMem.Free();
3180 end;
3182 procedure LoadFlag(flag: PFlag);
3183 begin
3184 // Ñèãíàòóðà ôëàãà
3185 Mem.ReadDWORD(dw);
3186 // 'FLAG'
3187 if dw <> FLAG_SIGNATURE then raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
3188 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3189 Mem.ReadByte(flag^.RespawnType);
3190 // Ñîñòîÿíèå ôëàãà
3191 Mem.ReadByte(flag^.State);
3192 // Íàïðàâëåíèå ôëàãà
3193 Mem.ReadByte(b);
3194 if b = 1 then flag^.Direction := D_LEFT else flag^.Direction := D_RIGHT; // b = 2
3195 // Îáúåêò ôëàãà
3196 Obj_LoadState(@flag^.Obj, Mem);
3197 end;
3199 begin
3200 if Mem = nil then Exit;
3202 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3203 loadPanels();
3204 ///// /////
3206 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3207 g_GFX_Init();
3208 //mapCreateGrid();
3210 ///// Çàãðóæàåì ìóçûêó: /////
3211 // Ñèãíàòóðà ìóçûêè
3212 Mem.ReadDWORD(dw);
3213 // 'MUSI'
3214 if dw <> MUSIC_SIGNATURE then raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
3215 // Íàçâàíèå ìóçûêè
3216 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3217 Mem.ReadString(str);
3218 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3219 Mem.ReadDWORD(dw);
3220 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3221 Mem.ReadBoolean(boo);
3222 // Çàïóñêàåì ýòó ìóçûêó
3223 gMusic.SetByName(str);
3224 gMusic.SpecPause := boo;
3225 gMusic.Play();
3226 gMusic.Pause(True);
3227 gMusic.SetPosition(dw);
3228 ///// /////
3230 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3231 Mem.ReadInt(gTotalMonsters);
3232 ///// /////
3234 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3235 if gGameSettings.GameMode = GM_CTF then
3236 begin
3237 // Ôëàã Êðàñíîé êîìàíäû
3238 LoadFlag(@gFlags[FLAG_RED]);
3239 // Ôëàã Ñèíåé êîìàíäû
3240 LoadFlag(@gFlags[FLAG_BLUE]);
3241 end;
3242 ///// /////
3244 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3245 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3246 begin
3247 // Î÷êè Êðàñíîé êîìàíäû
3248 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
3249 // Î÷êè Ñèíåé êîìàíäû
3250 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
3251 end;
3252 ///// /////
3253 end;
3256 // trace liquid, stepping by `dx` and `dy`
3257 // return last seen liquid coords, and `false` if we're started outside of the liquid
3258 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3259 const
3260 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3261 begin
3262 topx := x;
3263 topy := y;
3264 // started outside of the liquid?
3265 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3266 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3267 result := true;
3268 while true do
3269 begin
3270 Inc(x, dx);
3271 Inc(y, dy);
3272 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3273 topx := x;
3274 topy := y;
3275 end;
3276 end;
3279 end.