DEADSOFTWARE

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