DEADSOFTWARE

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