DEADSOFTWARE

4e347ef549b1aceaec2e9e27d9025e1956c8a20a
[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 {$IFDEF USE_NANOGL}
256 nanoGL,
257 {$ELSE}
258 GL, GLExt,
259 {$ENDIF}
260 e_input, g_main, e_log, e_texture, g_items, g_gfx, g_console,
261 g_weapons, g_game, g_sound, e_sound, CONFIG,
262 g_options, g_triggers, g_player,
263 Math, g_monsters, g_saveload, g_language, g_netmsg,
264 sfs, xstreams, hashtable, wadreader,
265 ImagingTypes, Imaging, ImagingUtility,
266 ImagingGif, ImagingNetworkGraphics;
268 const
269 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
270 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
271 FLAG_SIGNATURE = $47414C46; // 'FLAG'
274 // ////////////////////////////////////////////////////////////////////////// //
275 procedure mapWarningCB (const msg: AnsiString; line, col: Integer);
276 begin
277 if (line > 0) then
278 begin
279 e_LogWritefln('parse error at (%s,%s): %s', [line, col, msg], TMsgType.Warning);
280 end
281 else
282 begin
283 e_LogWritefln('parse error: %s', [msg], TMsgType.Warning);
284 end;
285 end;
288 // ////////////////////////////////////////////////////////////////////////// //
289 var
290 panByGUID: array of TPanel = nil;
293 // ////////////////////////////////////////////////////////////////////////// //
294 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
295 begin
296 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
297 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
298 end;
301 // return `true` from `cb` to stop
302 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
303 var
304 pan: TPanel;
305 begin
306 result := nil;
307 if not assigned(cb) then exit;
308 for pan in panByGUID do
309 begin
310 if cb(pan) then begin result := pan; exit; end;
311 end;
312 end;
315 procedure g_Map_NetSendInterestingPanels ();
316 var
317 pan: TPanel;
318 begin
319 if g_Game_IsServer and g_Game_IsNet then
320 begin
321 for pan in panByGUID do
322 begin
323 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
324 end;
325 end;
326 end;
329 // ////////////////////////////////////////////////////////////////////////// //
330 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
331 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
332 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
333 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
336 // ////////////////////////////////////////////////////////////////////////// //
337 var
338 dfmapdef: TDynMapDef = nil;
341 procedure loadMapDefinition ();
342 var
343 pr: TTextParser = nil;
344 st: TStream = nil;
345 WAD: TWADFile = nil;
346 begin
347 if (dfmapdef <> nil) then exit;
349 try
350 e_LogWritefln('parsing "mapdef.txt"...', []);
351 st := openDiskFileRO(DataDir+'mapdef.txt');
352 e_LogWritefln('found local "%smapdef.txt"', [DataDir]);
353 except
354 st := nil;
355 end;
357 if (st = nil) then
358 begin
359 WAD := TWADFile.Create();
360 if not WAD.ReadFile(GameWAD) then
361 begin
362 //raise Exception.Create('cannot load "game.wad"');
363 st := nil;
364 end
365 else
366 begin
367 st := WAD.openFileStream('mapdef.txt');
368 end;
369 end;
371 try
372 if (st = nil) then
373 begin
374 //raise Exception.Create('cannot open "mapdef.txt"');
375 e_LogWriteln('using default "mapdef.txt"...');
376 pr := TStrTextParser.Create(defaultMapDef);
377 end
378 else
379 begin
380 pr := TFileTextParser.Create(st);
381 end;
382 except on e: Exception do
383 begin
384 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
385 raise;
386 end;
387 end;
389 try
390 dfmapdef := TDynMapDef.Create(pr);
391 except
392 on e: TDynParseException do
393 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
394 on e: Exception do
395 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
396 end;
398 st.Free();
399 WAD.Free();
400 end;
403 // ////////////////////////////////////////////////////////////////////////// //
404 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
405 var
406 wst: TSFSMemoryChunkStream = nil;
407 begin
408 result := nil;
409 if (dataLen < 4) then exit;
411 if (dfmapdef = nil) then writeln('need to load mapdef');
412 loadMapDefinition();
413 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
415 wst := TSFSMemoryChunkStream.Create(data, dataLen);
416 try
417 result := dfmapdef.parseMap(wst);
418 except
419 on e: TDynParseException do
420 begin
421 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
422 wst.Free();
423 result := nil;
424 exit;
425 end;
426 on e: Exception do
427 begin
428 e_LogWritefln('ERROR: %s', [e.message]);
429 wst.Free();
430 result := nil;
431 exit;
432 end;
433 end;
435 //e_LogWriteln('map parsed.');
436 end;
439 // ////////////////////////////////////////////////////////////////////////// //
440 var
441 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
442 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
443 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
446 function g_Texture_NumNameFindStart(name: String): Boolean;
447 var
448 i: Integer;
450 begin
451 Result := False;
452 NNF_PureName := '';
453 NNF_FirstNum := -1;
454 NNF_CurrentNum := -1;
456 for i := Length(name) downto 1 do
457 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
458 begin
459 if i = Length(name) then
460 begin // Íåò öèôð â êîíöå ñòðîêè
461 Exit;
462 end
463 else
464 begin
465 NNF_PureName := Copy(name, 1, i);
466 Delete(name, 1, i);
467 Break;
468 end;
469 end;
471 // Íå ïåðåâåñòè â ÷èñëî:
472 if not TryStrToInt(name, NNF_FirstNum) then
473 Exit;
475 NNF_CurrentNum := 0;
477 Result := True;
478 end;
481 function g_Texture_NumNameFindNext(var newName: String): Byte;
482 begin
483 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
484 begin
485 newName := '';
486 Result := NNF_NO_NAME;
487 Exit;
488 end;
490 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
492 if NNF_CurrentNum < NNF_FirstNum then
493 Result := NNF_NAME_BEFORE
494 else
495 if NNF_CurrentNum > NNF_FirstNum then
496 Result := NNF_NAME_AFTER
497 else
498 Result := NNF_NAME_EQUALS;
500 Inc(NNF_CurrentNum);
501 end;
504 // ////////////////////////////////////////////////////////////////////////// //
505 function panelTypeToTag (panelType: Word): Integer;
506 begin
507 case panelType of
508 PANEL_WALL: result := GridTagWall; // gWalls
509 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
510 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
511 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
512 PANEL_WATER: result := GridTagWater; // gWater
513 PANEL_ACID1: result := GridTagAcid1; // gAcid1
514 PANEL_ACID2: result := GridTagAcid2; // gAcid2
515 PANEL_STEP: result := GridTagStep; // gSteps
516 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
517 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
518 else result := GridTagInvalid;
519 end;
520 end;
523 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
524 begin
525 if (a.tag < b.tag) then begin result := true; exit; end;
526 if (a.tag > b.tag) then begin result := false; exit; end;
527 result := (a.arrIdx < b.arrIdx);
528 end;
530 procedure dplClear ();
531 begin
532 if (gDrawPanelList = nil) then gDrawPanelList := TBinHeapPanelDraw.Create() else gDrawPanelList.clear();
533 end;
536 var
537 Textures: TLevelTextureArray = nil;
538 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
539 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
540 RespawnPoints: array of TRespawnPoint;
541 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
542 //DOMFlagPoints: Array of TFlagPoint;
545 procedure g_Map_ProfilersBegin ();
546 begin
547 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
548 if (profMapCollision <> nil) then profMapCollision.mainBegin(g_profile_collision);
549 // create sections
550 if g_profile_collision and (profMapCollision <> nil) then
551 begin
552 profMapCollision.sectionBegin('*solids');
553 profMapCollision.sectionEnd();
554 profMapCollision.sectionBegin('liquids');
555 profMapCollision.sectionEnd();
556 end;
557 end;
559 procedure g_Map_ProfilersEnd ();
560 begin
561 if (profMapCollision <> nil) then profMapCollision.mainEnd();
562 end;
565 // wall index in `gWalls` or -1
566 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
567 var
568 ex, ey: Integer;
569 begin
570 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, (GridTagWall or GridTagDoor));
571 if (result <> nil) then
572 begin
573 if (hitx <> nil) then hitx^ := ex;
574 if (hity <> nil) then hity^ := ey;
575 end
576 else
577 begin
578 if (hitx <> nil) then hitx^ := x1;
579 if (hity <> nil) then hity^ := y1;
580 end;
581 end;
583 // returns panel or nil
584 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
585 var
586 ex, ey: Integer;
587 begin
588 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, tag);
589 if (result <> nil) then
590 begin
591 if (hitx <> nil) then hitx^ := ex;
592 if (hity <> nil) then hity^ := ey;
593 end
594 else
595 begin
596 if (hitx <> nil) then hitx^ := x1;
597 if (hity <> nil) then hity^ := y1;
598 end;
599 end;
602 function xxPanAtPointChecker (pan: TPanel; panelType: Word): Boolean; inline;
603 begin
604 if ((pan.tag and GridTagLift) <> 0) then
605 begin
606 // stop if the lift of the right type
607 result :=
608 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
609 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
610 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
611 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
612 exit;
613 end;
614 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
615 end;
617 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
618 var
619 tagmask: Integer = 0;
620 mwit: PPanel;
621 it: TPanelGrid.Iter;
622 begin
623 result := false;
625 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
626 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
627 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
628 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
629 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
630 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
631 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
633 if (tagmask = 0) then exit;// just in case
635 if ((tagmask and GridTagLift) <> 0) then
636 begin
637 // slow
638 it := mapGrid.forEachAtPoint(x, y, tagmask);
639 for mwit in it do if (xxPanAtPointChecker(mwit^, PanelType)) then begin result := true; break; end;
640 end
641 else
642 begin
643 // fast
644 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true);
645 result := (it.length <> 0); // firsthit
646 end;
647 it.release();
648 end;
651 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
652 var
653 it: TPanelGrid.Iter;
654 begin
655 result := nil;
656 if (tagmask = 0) then exit;
657 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true); // firsthit
658 if (it.length <> 0) then result := it.first^;
659 it.release();
660 end;
663 function g_Map_IsSpecialTexture(Texture: String): Boolean;
664 begin
665 Result := (Texture = TEXTURE_NAME_WATER) or
666 (Texture = TEXTURE_NAME_ACID1) or
667 (Texture = TEXTURE_NAME_ACID2);
668 end;
670 procedure CreateDoorMap();
671 var
672 PanelArray: Array of record
673 X, Y: Integer;
674 Width, Height: Word;
675 Active: Boolean;
676 PanelID: DWORD;
677 end;
678 a, b, c, m, i, len: Integer;
679 ok: Boolean;
680 begin
681 if gWalls = nil then
682 Exit;
684 i := 0;
685 len := 128;
686 SetLength(PanelArray, len);
688 for a := 0 to High(gWalls) do
689 if gWalls[a].Door then
690 begin
691 PanelArray[i].X := gWalls[a].X;
692 PanelArray[i].Y := gWalls[a].Y;
693 PanelArray[i].Width := gWalls[a].Width;
694 PanelArray[i].Height := gWalls[a].Height;
695 PanelArray[i].Active := True;
696 PanelArray[i].PanelID := a;
698 i := i + 1;
699 if i = len then
700 begin
701 len := len + 128;
702 SetLength(PanelArray, len);
703 end;
704 end;
706 // Íåò äâåðåé:
707 if i = 0 then
708 begin
709 PanelArray := nil;
710 Exit;
711 end;
713 SetLength(gDoorMap, 0);
715 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
717 for a := 0 to i-1 do
718 if PanelArray[a].Active then
719 begin
720 PanelArray[a].Active := False;
721 m := Length(gDoorMap);
722 SetLength(gDoorMap, m+1);
723 SetLength(gDoorMap[m], 1);
724 gDoorMap[m, 0] := PanelArray[a].PanelID;
725 ok := True;
727 while ok do
728 begin
729 ok := False;
731 for b := 0 to i-1 do
732 if PanelArray[b].Active then
733 for c := 0 to High(gDoorMap[m]) do
734 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
735 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
736 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
737 PanelArray[b].Width, PanelArray[b].Height,
738 gWalls[gDoorMap[m, c]].X,
739 gWalls[gDoorMap[m, c]].Y,
740 gWalls[gDoorMap[m, c]].Width,
741 gWalls[gDoorMap[m, c]].Height) then
742 begin
743 PanelArray[b].Active := False;
744 SetLength(gDoorMap[m],
745 Length(gDoorMap[m])+1);
746 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
747 ok := True;
748 Break;
749 end;
750 end;
752 g_Game_StepLoading();
753 end;
755 PanelArray := nil;
756 end;
758 procedure CreateLiftMap();
759 var
760 PanelArray: Array of record
761 X, Y: Integer;
762 Width, Height: Word;
763 Active: Boolean;
764 end;
765 a, b, c, len, i, j: Integer;
766 ok: Boolean;
767 begin
768 if gLifts = nil then
769 Exit;
771 len := Length(gLifts);
772 SetLength(PanelArray, len);
774 for a := 0 to len-1 do
775 begin
776 PanelArray[a].X := gLifts[a].X;
777 PanelArray[a].Y := gLifts[a].Y;
778 PanelArray[a].Width := gLifts[a].Width;
779 PanelArray[a].Height := gLifts[a].Height;
780 PanelArray[a].Active := True;
781 end;
783 SetLength(gLiftMap, len);
784 i := 0;
786 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
788 for a := 0 to len-1 do
789 if PanelArray[a].Active then
790 begin
791 PanelArray[a].Active := False;
792 SetLength(gLiftMap[i], 32);
793 j := 0;
794 gLiftMap[i, j] := a;
795 ok := True;
797 while ok do
798 begin
799 ok := False;
800 for b := 0 to len-1 do
801 if PanelArray[b].Active then
802 for c := 0 to j do
803 if g_CollideAround(PanelArray[b].X,
804 PanelArray[b].Y,
805 PanelArray[b].Width,
806 PanelArray[b].Height,
807 PanelArray[gLiftMap[i, c]].X,
808 PanelArray[gLiftMap[i, c]].Y,
809 PanelArray[gLiftMap[i, c]].Width,
810 PanelArray[gLiftMap[i, c]].Height) then
811 begin
812 PanelArray[b].Active := False;
813 j := j+1;
814 if j > High(gLiftMap[i]) then
815 SetLength(gLiftMap[i],
816 Length(gLiftMap[i])+32);
818 gLiftMap[i, j] := b;
819 ok := True;
821 Break;
822 end;
823 end;
825 SetLength(gLiftMap[i], j+1);
826 i := i+1;
828 g_Game_StepLoading();
829 end;
831 SetLength(gLiftMap, i);
833 PanelArray := nil;
834 end;
836 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
837 var
838 len: Integer;
839 panels: ^TPanelArray;
840 pan: TPanel;
841 pguid: Integer;
842 begin
843 Result := -1;
845 case PanelRec.PanelType of
846 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
847 PANEL_BACK: panels := @gRenderBackgrounds;
848 PANEL_FORE: panels := @gRenderForegrounds;
849 PANEL_WATER: panels := @gWater;
850 PANEL_ACID1: panels := @gAcid1;
851 PANEL_ACID2: panels := @gAcid2;
852 PANEL_STEP: panels := @gSteps;
853 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
854 PANEL_BLOCKMON: panels := @gBlockMon;
855 else exit;
856 end;
858 len := Length(panels^);
859 SetLength(panels^, len+1);
861 pguid := Length(panByGUID);
862 SetLength(panByGUID, pguid+1); //FIXME!
863 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
864 assert(pguid >= 0);
865 assert(pguid < Length(panByGUID));
866 panByGUID[pguid] := pan;
867 panels^[len] := pan;
868 pan.arrIdx := len;
869 pan.proxyId := -1;
870 pan.tag := panelTypeToTag(PanelRec.PanelType);
872 PanelRec.user['panel_guid'] := pguid;
874 //result := len;
875 result := pguid;
876 end;
879 function CreateNullTexture(RecName: String): Integer;
880 begin
881 RecName := toLowerCase1251(RecName);
882 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
883 if TextNameHash.get(RecName, result) then exit; // i found her!
885 SetLength(Textures, Length(Textures)+1);
886 result := High(Textures);
888 with Textures[High(Textures)] do
889 begin
890 TextureName := RecName;
891 Width := 1;
892 Height := 1;
893 Anim := False;
894 TextureID := LongWord(TEXTURE_NONE);
895 end;
897 TextNameHash.put(RecName, result);
898 end;
901 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
902 var
903 WAD: TWADFile;
904 TextureData: Pointer;
905 WADName: String;
906 a, ResLength: Integer;
907 begin
908 RecName := toLowerCase1251(RecName);
909 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
910 if TextNameHash.get(RecName, result) then
911 begin
912 // i found her!
913 //e_LogWritefln('texture ''%s'' already loaded (%s)', [RecName, result]);
914 exit;
915 end;
917 Result := -1;
919 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
922 if Textures <> nil then
923 begin
924 for a := 0 to High(Textures) do
925 begin
926 if (Textures[a].TextureName = RecName) then
927 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
928 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
929 Result := a;
930 Exit;
931 end;
932 end;
933 end;
936 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
937 if (RecName = TEXTURE_NAME_WATER) or
938 (RecName = TEXTURE_NAME_ACID1) or
939 (RecName = TEXTURE_NAME_ACID2) then
940 begin
941 SetLength(Textures, Length(Textures)+1);
943 with Textures[High(Textures)] do
944 begin
945 TextureName := RecName;
946 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
947 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
948 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
950 Anim := False;
951 end;
953 result := High(Textures);
954 TextNameHash.put(RecName, result);
955 Exit;
956 end;
958 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
959 WADName := g_ExtractWadName(RecName);
961 WAD := TWADFile.Create();
963 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
965 WAD.ReadFile(WADName);
967 //txname := RecName;
969 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
970 begin
971 FreeMem(TextureData);
972 RecName := 'COMMON\ALIEN';
973 end;
976 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
977 begin
978 SetLength(Textures, Length(Textures)+1);
979 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
980 begin
981 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
982 SetLength(Textures, Length(Textures)-1);
983 result := -1;
984 Exit;
985 end;
986 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
987 FreeMem(TextureData);
988 Textures[High(Textures)].TextureName := RecName;
989 Textures[High(Textures)].Anim := False;
991 result := High(Textures);
992 TextNameHash.put(RecName, result);
993 end
994 else // Íåò òàêîãî ðåóñðñà â WAD'å
995 begin
996 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
997 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
998 if log and (not BadTextNameHash.get(RecName, a)) then
999 begin
1000 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1001 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1002 end;
1003 BadTextNameHash.put(RecName, -1);
1004 end;
1006 WAD.Free();
1007 end;
1010 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
1011 var
1012 WAD: TWADFile;
1013 TextureWAD: PChar = nil;
1014 TextData: Pointer = nil;
1015 TextureData: Pointer = nil;
1016 cfg: TConfig = nil;
1017 WADName: String;
1018 ResLength: Integer;
1019 TextureResource: String;
1020 _width, _height, _framecount, _speed: Integer;
1021 _backanimation: Boolean;
1022 //imgfmt: string;
1023 ia: TDynImageDataArray = nil;
1024 f, c, frdelay, frloop: Integer;
1025 begin
1026 RecName := toLowerCase1251(RecName);
1027 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
1028 if TextNameHash.get(RecName, result) then
1029 begin
1030 // i found her!
1031 //e_LogWritefln('animated texture ''%s'' already loaded (%s)', [RecName, result]);
1032 exit;
1033 end;
1035 result := -1;
1037 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1039 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1040 if BadTextNameHash.get(RecName, f) then
1041 begin
1042 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1043 exit;
1044 end;
1046 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1047 WADName := g_ExtractWadName(RecName);
1049 WAD := TWADFile.Create();
1050 try
1051 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1053 WAD.ReadFile(WADName);
1055 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1056 begin
1057 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1058 if log and (not BadTextNameHash.get(RecName, f)) then
1059 begin
1060 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1061 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1062 end;
1063 BadTextNameHash.put(RecName, -1);
1064 exit;
1065 end;
1067 {TEST
1068 if WADName = Map then
1069 begin
1070 //FreeMem(TextureWAD);
1071 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1072 end;
1075 WAD.FreeWAD();
1077 if ResLength < 6 then
1078 begin
1079 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), TMsgType.Warning);
1080 BadTextNameHash.put(RecName, -1);
1081 exit;
1082 end;
1084 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1085 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1086 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1087 begin
1088 // íåò, ýòî ñóïåðìåí!
1089 if not WAD.ReadMemory(TextureWAD, ResLength) then
1090 begin
1091 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), TMsgType.Warning);
1092 BadTextNameHash.put(RecName, -1);
1093 exit;
1094 end;
1096 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1097 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1098 begin
1099 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), TMsgType.Warning);
1100 BadTextNameHash.put(RecName, -1);
1101 exit;
1102 end;
1104 cfg := TConfig.CreateMem(TextData, ResLength);
1106 TextureResource := cfg.ReadStr('', 'resource', '');
1107 if TextureResource = '' then
1108 begin
1109 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), TMsgType.Warning);
1110 BadTextNameHash.put(RecName, -1);
1111 exit;
1112 end;
1114 _width := cfg.ReadInt('', 'framewidth', 0);
1115 _height := cfg.ReadInt('', 'frameheight', 0);
1116 _framecount := cfg.ReadInt('', 'framecount', 0);
1117 _speed := cfg.ReadInt('', 'waitcount', 0);
1118 _backanimation := cfg.ReadBool('', 'backanimation', False);
1120 cfg.Free();
1121 cfg := nil;
1123 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1124 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1125 begin
1126 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), TMsgType.Warning);
1127 BadTextNameHash.put(RecName, -1);
1128 exit;
1129 end;
1131 WAD.Free();
1132 WAD := nil;
1134 SetLength(Textures, Length(Textures)+1);
1135 with Textures[High(Textures)] do
1136 begin
1137 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1138 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1139 begin
1140 TextureName := RecName;
1141 Width := _width;
1142 Height := _height;
1143 Anim := True;
1144 FramesCount := _framecount;
1145 Speed := _speed;
1146 result := High(Textures);
1147 TextNameHash.put(RecName, result);
1148 end
1149 else
1150 begin
1151 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1152 if log and (not BadTextNameHash.get(RecName, f)) then
1153 begin
1154 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1155 end;
1156 BadTextNameHash.put(RecName, -1);
1157 end;
1158 end;
1159 end
1160 else
1161 begin
1162 // try animated image
1164 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1165 if length(imgfmt) = 0 then
1166 begin
1167 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1168 exit;
1169 end;
1171 GlobalMetadata.ClearMetaItems();
1172 GlobalMetadata.ClearMetaItemsForSaving();
1173 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1174 begin
1175 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), TMsgType.Warning);
1176 BadTextNameHash.put(RecName, -1);
1177 exit;
1178 end;
1179 if length(ia) = 0 then
1180 begin
1181 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), TMsgType.Warning);
1182 BadTextNameHash.put(RecName, -1);
1183 exit;
1184 end;
1186 WAD.Free();
1187 WAD := nil;
1189 _width := ia[0].width;
1190 _height := ia[0].height;
1191 _framecount := length(ia);
1192 _speed := 1;
1193 _backanimation := false;
1194 frdelay := -1;
1195 frloop := -666;
1196 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1197 begin
1198 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1199 try
1200 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1201 frdelay := f;
1202 if f < 0 then f := 0;
1203 // rounding ;-)
1204 c := f mod 28;
1205 if c < 13 then c := 0 else c := 1;
1206 f := (f div 28)+c;
1207 if f < 1 then f := 1 else if f > 255 then f := 255;
1208 _speed := f;
1209 except
1210 end;
1211 end;
1212 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1213 begin
1214 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1215 try
1216 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1217 frloop := f;
1218 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1219 except
1220 end;
1221 end;
1222 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1223 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1224 f := ord(_backanimation);
1225 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);
1227 SetLength(Textures, Length(Textures)+1);
1228 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1229 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1230 begin
1231 Textures[High(Textures)].TextureName := RecName;
1232 Textures[High(Textures)].Width := _width;
1233 Textures[High(Textures)].Height := _height;
1234 Textures[High(Textures)].Anim := True;
1235 Textures[High(Textures)].FramesCount := length(ia);
1236 Textures[High(Textures)].Speed := _speed;
1237 result := High(Textures);
1238 TextNameHash.put(RecName, result);
1239 //writeln(' CREATED!');
1240 end
1241 else
1242 begin
1243 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1244 if log and (not BadTextNameHash.get(RecName, f)) then
1245 begin
1246 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), TMsgType.Warning);
1247 end;
1248 BadTextNameHash.put(RecName, -1);
1249 end;
1250 end;
1251 finally
1252 for f := 0 to High(ia) do FreeImage(ia[f]);
1253 WAD.Free();
1254 cfg.Free();
1255 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1256 if (TextData <> nil) then FreeMem(TextData);
1257 if (TextureData <> nil) then FreeMem(TextureData);
1258 end;
1259 end;
1261 procedure CreateItem(Item: TDynRecord);
1262 begin
1263 if g_Game_IsClient then Exit;
1265 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1266 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1267 Exit;
1269 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1270 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1271 end;
1273 procedure CreateArea(Area: TDynRecord);
1274 var
1275 a: Integer;
1276 id: DWORD = 0;
1277 begin
1278 case Area.AreaType of
1279 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1280 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1281 begin
1282 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1283 with RespawnPoints[High(RespawnPoints)] do
1284 begin
1285 X := Area.X;
1286 Y := Area.Y;
1287 Direction := TDirection(Area.Direction);
1289 case Area.AreaType of
1290 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1291 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1292 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1293 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1294 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1295 end;
1296 end;
1297 end;
1299 AREA_REDFLAG, AREA_BLUEFLAG:
1300 begin
1301 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1303 if FlagPoints[a] <> nil then Exit;
1305 New(FlagPoints[a]);
1307 with FlagPoints[a]^ do
1308 begin
1309 X := Area.X-FLAGRECT.X;
1310 Y := Area.Y-FLAGRECT.Y;
1311 Direction := TDirection(Area.Direction);
1312 end;
1314 with gFlags[a] do
1315 begin
1316 case a of
1317 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1318 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1319 end;
1321 Animation := TAnimation.Create(id, True, 8);
1322 Obj.Rect := FLAGRECT;
1324 g_Map_ResetFlag(a);
1325 end;
1326 end;
1328 AREA_DOMFLAG:
1329 begin
1330 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1331 with DOMFlagPoints[High(DOMFlagPoints)] do
1332 begin
1333 X := Area.X;
1334 Y := Area.Y;
1335 Direction := TDirection(Area.Direction);
1336 end;
1338 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1339 end;
1340 end;
1341 end;
1343 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1344 var
1345 _trigger: TTrigger;
1346 tp: TPanel;
1347 begin
1348 result := -1;
1349 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1351 with _trigger do
1352 begin
1353 mapId := Trigger.id;
1354 mapIndex := amapIdx;
1355 X := Trigger.X;
1356 Y := Trigger.Y;
1357 Width := Trigger.Width;
1358 Height := Trigger.Height;
1359 Enabled := Trigger.Enabled;
1360 TexturePanelGUID := atpanid;
1361 TriggerType := Trigger.TriggerType;
1362 ActivateType := Trigger.ActivateType;
1363 Keys := Trigger.Keys;
1364 trigPanelGUID := atrigpanid;
1365 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1366 if TexturePanelGUID <> -1 then
1367 begin
1368 tp := g_Map_PanelByGUID(TexturePanelGUID);
1369 if (tp <> nil) then tp.hasTexTrigger := True;
1370 end;
1371 end;
1373 result := Integer(g_Triggers_Create(_trigger, Trigger));
1374 end;
1376 procedure CreateMonster(monster: TDynRecord);
1377 var
1378 a: Integer;
1379 mon: TMonster;
1380 begin
1381 if g_Game_IsClient then Exit;
1383 if (gGameSettings.GameType = GT_SINGLE)
1384 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1385 begin
1386 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1388 if gTriggers <> nil then
1389 begin
1390 for a := 0 to High(gTriggers) do
1391 begin
1392 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1393 begin
1394 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1395 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1396 end;
1397 end;
1398 end;
1400 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1401 end;
1402 end;
1404 procedure g_Map_ReAdd_DieTriggers();
1406 function monsDieTrig (mon: TMonster): Boolean;
1407 var
1408 a: Integer;
1409 //tw: TStrTextWriter;
1410 begin
1411 result := false; // don't stop
1412 mon.ClearTriggers();
1413 for a := 0 to High(gTriggers) do
1414 begin
1415 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1416 begin
1417 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1419 tw := TStrTextWriter.Create();
1420 try
1421 gTriggers[a].trigData.writeTo(tw);
1422 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1423 finally
1424 tw.Free();
1425 end;
1427 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1428 end;
1429 end;
1430 end;
1432 begin
1433 if g_Game_IsClient then Exit;
1435 g_Mons_ForEach(monsDieTrig);
1436 end;
1438 function extractWadName(resourceName: string): string;
1439 var
1440 posN: Integer;
1441 begin
1442 posN := Pos(':', resourceName);
1443 if posN > 0 then
1444 Result:= Copy(resourceName, 0, posN-1)
1445 else
1446 Result := '';
1447 end;
1449 procedure addResToExternalResList(res: string);
1450 begin
1451 res := extractWadName(res);
1452 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1453 gExternalResources.Add(res);
1454 end;
1456 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1457 //var
1458 //textures: TTexturesRec1Array;
1459 //textures: TDynField;
1460 //trec: TDynRecord;
1461 //mapHeader: TMapHeaderRec_1;
1462 //i: integer;
1463 //resFile: String = '';
1464 begin
1465 if gExternalResources = nil then
1466 gExternalResources := TStringList.Create;
1468 gExternalResources.Clear;
1470 (*
1472 textures := GetTextures(map);
1473 for i := 0 to High(textures) do
1474 begin
1475 addResToExternalResList(resFile);
1476 end;
1479 textures := map['texture'];
1480 if (textures <> nil) then
1481 begin
1482 for trec in textures do
1483 begin
1484 addResToExternalResList(resFile);
1485 end;
1486 end;
1488 textures := nil;
1489 *)
1491 //mapHeader := GetMapHeader(map);
1493 addResToExternalResList(map.MusicName);
1494 addResToExternalResList(map.SkyName);
1495 end;
1498 procedure mapCreateGrid ();
1499 var
1500 mapX0: Integer = $3fffffff;
1501 mapY0: Integer = $3fffffff;
1502 mapX1: Integer = -$3fffffff;
1503 mapY1: Integer = -$3fffffff;
1505 procedure calcBoundingBox (constref panels: TPanelArray);
1506 var
1507 idx: Integer;
1508 pan: TPanel;
1509 begin
1510 for idx := 0 to High(panels) do
1511 begin
1512 pan := panels[idx];
1513 if not pan.visvalid then continue;
1514 if (pan.Width < 1) or (pan.Height < 1) then continue;
1515 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1516 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1517 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1518 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1519 end;
1520 end;
1522 procedure addPanelsToGrid (constref panels: TPanelArray);
1523 var
1524 idx: Integer;
1525 pan: TPanel;
1526 newtag: Integer;
1527 begin
1528 //tag := panelTypeToTag(tag);
1529 for idx := 0 to High(panels) do
1530 begin
1531 pan := panels[idx];
1532 if not pan.visvalid then continue;
1533 if (pan.proxyId <> -1) then
1534 begin
1535 {$IF DEFINED(D2F_DEBUG)}
1536 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);
1537 {$ENDIF}
1538 continue;
1539 end;
1540 case pan.PanelType of
1541 PANEL_WALL: newtag := GridTagWall;
1542 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1543 PANEL_BACK: newtag := GridTagBack;
1544 PANEL_FORE: newtag := GridTagFore;
1545 PANEL_WATER: newtag := GridTagWater;
1546 PANEL_ACID1: newtag := GridTagAcid1;
1547 PANEL_ACID2: newtag := GridTagAcid2;
1548 PANEL_STEP: newtag := GridTagStep;
1549 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1550 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1551 else continue; // oops
1552 end;
1553 pan.tag := newtag;
1555 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1556 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1557 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1558 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1560 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1561 begin
1562 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1563 end;
1565 {$ENDIF}
1566 end;
1567 end;
1569 begin
1570 mapGrid.Free();
1571 mapGrid := nil;
1573 calcBoundingBox(gWalls);
1574 calcBoundingBox(gRenderBackgrounds);
1575 calcBoundingBox(gRenderForegrounds);
1576 calcBoundingBox(gWater);
1577 calcBoundingBox(gAcid1);
1578 calcBoundingBox(gAcid2);
1579 calcBoundingBox(gSteps);
1580 calcBoundingBox(gLifts);
1581 calcBoundingBox(gBlockMon);
1583 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1585 if (mapX0 > 0) then mapX0 := 0;
1586 if (mapY0 > 0) then mapY0 := 0;
1588 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1589 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1591 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1592 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1594 addPanelsToGrid(gWalls);
1595 addPanelsToGrid(gRenderBackgrounds);
1596 addPanelsToGrid(gRenderForegrounds);
1597 addPanelsToGrid(gWater);
1598 addPanelsToGrid(gAcid1);
1599 addPanelsToGrid(gAcid2);
1600 addPanelsToGrid(gSteps);
1601 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1602 addPanelsToGrid(gBlockMon);
1604 mapGrid.dumpStats();
1606 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1607 end;
1610 function g_Map_Load(Res: String): Boolean;
1611 const
1612 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1613 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1614 type
1615 PTRec = ^TTRec;
1616 TTRec = record
1617 //TexturePanel: Integer;
1618 tnum: Integer;
1619 id: Integer;
1620 trigrec: TDynRecord;
1621 // texture pane;
1622 texPanelIdx: Integer;
1623 texPanel: TDynRecord;
1624 // "action" panel
1625 actPanelIdx: Integer;
1626 actPanel: TDynRecord;
1627 end;
1628 var
1629 WAD, TestWAD: TWADFile;
1630 //mapReader: TDynRecord = nil;
1631 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1632 panels: TDynField = nil; //TPanelsRec1Array;
1633 items: TDynField = nil; //TItemsRec1Array;
1634 monsters: TDynField = nil; //TMonsterRec1Array;
1635 areas: TDynField = nil; //TAreasRec1Array;
1636 triggers: TDynField = nil; //TTriggersRec1Array;
1637 b, c, k: Integer;
1638 PanelID: DWORD;
1639 AddTextures: TAddTextureArray;
1640 TriggersTable: array of TTRec;
1641 FileName, mapResName, TexName, s: AnsiString;
1642 Data: Pointer;
1643 Len: Integer;
1644 ok, isAnim: Boolean;
1645 CurTex, ntn: Integer;
1646 rec, texrec: TDynRecord;
1647 pttit: PTRec;
1648 pannum, trignum, cnt, tgpid: Integer;
1649 stt: UInt64;
1650 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1651 //moveActive: Boolean;
1652 pan: TPanel;
1653 mapOk: Boolean = false;
1654 usedTextures: THashStrInt = nil; // key: mapTextureList
1655 begin
1656 mapGrid.Free();
1657 mapGrid := nil;
1658 TestWAD := nil;
1659 Data := nil;
1661 //gCurrentMap.Free();
1662 //gCurrentMap := nil;
1664 panByGUID := nil;
1666 Result := False;
1667 gMapInfo.Map := Res;
1668 TriggersTable := nil;
1669 //mapReader := nil;
1671 sfsGCDisable(); // temporary disable removing of temporary volumes
1672 try
1673 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1674 if (gCurrentMap = nil) then
1675 begin
1676 FileName := g_ExtractWadName(Res);
1677 e_WriteLog('Loading map WAD: '+FileName, TMsgType.Notify);
1678 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1680 WAD := TWADFile.Create();
1681 if not WAD.ReadFile(FileName) then
1682 begin
1683 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1684 WAD.Free();
1685 Exit;
1686 end;
1688 if gTestMap <> '' then
1689 begin
1690 s := g_ExtractWadName(gTestMap);
1691 TestWAD := TWADFile.Create();
1692 if not TestWAD.ReadFile(s) then
1693 begin
1694 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1695 TestWAD.Free();
1696 TestWAD := nil;
1697 end;
1698 end;
1700 if TestWAD <> nil then
1701 begin
1702 mapResName := g_ExtractFileName(gTestMap);
1703 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1704 begin
1705 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1706 Data := nil;
1707 end else
1708 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1709 TestWAD.Free();
1710 TestWAD := nil;
1711 end;
1713 if Data = nil then
1714 begin
1715 //k8: why loader ignores path here?
1716 mapResName := g_ExtractFileName(Res);
1717 if not WAD.GetMapResource(mapResName, Data, Len) then
1718 begin
1719 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1720 WAD.Free();
1721 Exit;
1722 end;
1723 end;
1725 WAD.Free();
1727 if (Len < 4) then
1728 begin
1729 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1730 FreeMem(Data);
1731 exit;
1732 end;
1734 // Çàãðóçêà êàðòû:
1735 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1736 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1738 stt := getTimeMicro();
1740 try
1741 gCurrentMap := g_Map_ParseMap(Data, Len);
1742 except
1743 gCurrentMap.Free();
1744 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1745 FreeMem(Data);
1746 gCurrentMapFileName := '';
1747 Exit;
1748 end;
1750 FreeMem(Data);
1752 if (gCurrentMap = nil) then
1753 begin
1754 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1755 gCurrentMapFileName := '';
1756 exit;
1757 end;
1758 end
1759 else
1760 begin
1761 stt := getTimeMicro();
1762 end;
1764 //gCurrentMap := mapReader;
1766 generateExternalResourcesList(gCurrentMap);
1767 mapTextureList := gCurrentMap['texture'];
1768 // get all other lists here too
1769 panels := gCurrentMap['panel'];
1770 triggers := gCurrentMap['trigger'];
1771 items := gCurrentMap['item'];
1772 areas := gCurrentMap['area'];
1773 monsters := gCurrentMap['monster'];
1775 // Çàãðóçêà îïèñàíèÿ êàðòû:
1776 e_WriteLog(' Reading map info...', TMsgType.Notify);
1777 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1779 with gMapInfo do
1780 begin
1781 Name := gCurrentMap.MapName;
1782 Description := gCurrentMap.MapDesc;
1783 Author := gCurrentMap.MapAuthor;
1784 MusicName := gCurrentMap.MusicName;
1785 SkyName := gCurrentMap.SkyName;
1786 Height := gCurrentMap.Height;
1787 Width := gCurrentMap.Width;
1788 end;
1790 // Çàãðóçêà òåêñòóð:
1791 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1792 // Äîáàâëåíèå òåêñòóð â Textures[]:
1793 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1794 begin
1795 e_WriteLog(' Loading textures:', TMsgType.Notify);
1796 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1798 // find used textures
1799 usedTextures := THashStrInt.Create();
1800 try
1801 if (panels <> nil) and (panels.count > 0) then
1802 begin
1803 for rec in panels do
1804 begin
1805 texrec := rec.TextureRec;
1806 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1807 end;
1808 end;
1810 cnt := -1;
1811 for rec in mapTextureList do
1812 begin
1813 Inc(cnt);
1814 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1815 begin
1816 rec.tagInt := -1; // just in case
1817 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1818 end
1819 else
1820 begin
1821 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1822 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1823 {$ENDIF}
1824 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1825 if rec.Anim then
1826 begin
1827 // Àíèìèðîâàííàÿ òåêñòóðà
1828 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1829 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]));
1830 end
1831 else
1832 begin
1833 // Îáû÷íàÿ òåêñòóðà
1834 ntn := CreateTexture(rec.Resource, FileName, True);
1835 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1836 end;
1837 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1839 rec.tagInt := ntn; // remember texture number
1840 end;
1841 g_Game_StepLoading();
1842 end;
1843 finally
1844 usedTextures.Free();
1845 end;
1847 // set panel tagInt to texture index
1848 if (panels <> nil) then
1849 begin
1850 for rec in panels do
1851 begin
1852 texrec := rec.TextureRec;
1853 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1854 end;
1855 end;
1856 end;
1859 // Çàãðóçêà òðèããåðîâ
1860 gTriggerClientID := 0;
1861 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1862 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1864 // Çàãðóçêà ïàíåëåé
1865 e_WriteLog(' Loading panels...', TMsgType.Notify);
1866 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1868 // check texture numbers for panels
1869 if (panels <> nil) and (panels.count > 0) then
1870 begin
1871 for rec in panels do
1872 begin
1873 if (rec.tagInt < 0) then
1874 begin
1875 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1876 result := false;
1877 gCurrentMap.Free();
1878 gCurrentMap := nil;
1879 gCurrentMapFileName := '';
1880 exit;
1881 end;
1882 end;
1883 end;
1885 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1886 if (triggers <> nil) and (triggers.count > 0) then
1887 begin
1888 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1889 //SetLength(TriggersTable, triggers.count);
1890 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1892 SetLength(TriggersTable, triggers.count);
1893 trignum := -1;
1894 for rec in triggers do
1895 begin
1896 Inc(trignum);
1897 pttit := @TriggersTable[trignum];
1898 pttit.trigrec := rec;
1899 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1900 pttit.texPanelIdx := -1; // will be fixed later
1901 pttit.texPanel := rec.TexturePanelRec;
1902 // action panel
1903 pttit.actPanelIdx := -1;
1904 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1905 // set flag
1906 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1907 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1908 // update progress
1909 g_Game_StepLoading();
1910 end;
1911 end;
1913 // Ñîçäàåì ïàíåëè
1914 if (panels <> nil) and (panels.count > 0) then
1915 begin
1916 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1917 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1919 pannum := -1;
1920 for rec in panels do
1921 begin
1922 Inc(pannum);
1923 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1924 texrec := nil;
1925 SetLength(AddTextures, 0);
1926 CurTex := -1;
1927 ok := false;
1929 if (mapTextureList <> nil) then
1930 begin
1931 texrec := rec.TextureRec;
1932 ok := (texrec <> nil);
1933 end;
1935 if ok then
1936 begin
1937 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1938 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1939 ok := false;
1940 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1941 begin
1942 if rec.userPanelTrigRef then
1943 begin
1944 // e_LogWritefln('trigref for panel %s', [pannum]);
1945 ok := True;
1946 end;
1947 end;
1948 end;
1950 if ok then
1951 begin
1952 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1953 s := texrec.Resource;
1955 // Ñïåö-òåêñòóðû çàïðåùåíû
1956 if g_Map_IsSpecialTexture(s) then
1957 begin
1958 ok := false
1959 end
1960 else
1961 begin
1962 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1963 ok := g_Texture_NumNameFindStart(s);
1964 end;
1966 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1967 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1968 if ok then
1969 begin
1970 k := NNF_NAME_BEFORE;
1971 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1972 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1973 begin
1974 k := g_Texture_NumNameFindNext(TexName);
1976 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1977 begin
1978 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1979 if texrec.Anim then
1980 begin
1981 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1982 isAnim := True;
1983 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1984 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1985 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1986 if not ok then
1987 begin
1988 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1989 isAnim := False;
1990 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1991 ok := CreateTexture(TexName, FileName, False) >= 0;
1992 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1993 end;
1994 end
1995 else
1996 begin
1997 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1998 isAnim := False;
1999 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2000 ok := CreateTexture(TexName, FileName, False) >= 0;
2001 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2002 if not ok then
2003 begin
2004 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
2005 isAnim := True;
2006 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2007 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2008 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2009 end;
2010 end;
2012 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
2013 if ok then
2014 begin
2016 for c := 0 to High(Textures) do
2017 begin
2018 if (Textures[c].TextureName = TexName) then
2019 begin
2020 SetLength(AddTextures, Length(AddTextures)+1);
2021 AddTextures[High(AddTextures)].Texture := c;
2022 AddTextures[High(AddTextures)].Anim := isAnim;
2023 break;
2024 end;
2025 end;
2027 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
2028 begin
2029 SetLength(AddTextures, Length(AddTextures)+1);
2030 AddTextures[High(AddTextures)].Texture := c;
2031 AddTextures[High(AddTextures)].Anim := isAnim;
2032 end;
2033 end;
2034 end
2035 else
2036 begin
2037 if k = NNF_NAME_EQUALS then
2038 begin
2039 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
2040 SetLength(AddTextures, Length(AddTextures)+1);
2041 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
2042 AddTextures[High(AddTextures)].Anim := texrec.Anim;
2043 CurTex := High(AddTextures);
2044 ok := true;
2045 end
2046 else // NNF_NO_NAME
2047 begin
2048 ok := false;
2049 end;
2050 end;
2051 end; // while ok...
2053 ok := true;
2054 end; // if ok - åñòü ñìåæíûå òåêñòóðû
2055 end; // if ok - ññûëàþòñÿ òðèããåðû
2057 if not ok then
2058 begin
2059 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
2060 SetLength(AddTextures, 1);
2061 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
2062 AddTextures[0].Anim := false;
2063 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
2064 CurTex := 0;
2065 end;
2067 //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);
2069 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
2071 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
2072 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
2073 PanelID := CreatePanel(rec, AddTextures, CurTex);
2074 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
2075 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
2077 // setup lifts
2078 moveSpeed := rec.moveSpeed;
2079 //moveStart := rec.moveStart;
2080 //moveEnd := rec.moveEnd;
2081 //moveActive := rec['move_active'].value;
2082 if not moveSpeed.isZero then
2083 begin
2084 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
2085 gMovingWallIds[High(gMovingWallIds)] := PanelID;
2086 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
2087 end;
2089 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2091 g_Game_StepLoading();
2092 end;
2093 end;
2095 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2096 for b := 0 to High(TriggersTable) do
2097 begin
2098 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2099 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2100 end;
2102 // create map grid, init other grids (for monsters, for example)
2103 e_WriteLog('Creating map grid', TMsgType.Notify);
2104 mapCreateGrid();
2106 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2107 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2108 begin
2109 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2110 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2111 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2112 trignum := -1;
2113 for rec in triggers do
2114 begin
2115 Inc(trignum);
2116 tgpid := TriggersTable[trignum].actPanelIdx;
2117 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2118 TriggersTable[trignum].tnum := trignum;
2119 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
2120 end;
2121 end;
2123 //FIXME: use hashtable!
2124 for pan in panByGUID do
2125 begin
2126 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2127 begin
2128 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2129 end;
2130 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2131 begin
2132 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2133 end;
2134 end;
2136 // Çàãðóçêà ïðåäìåòîâ
2137 e_WriteLog(' Loading items...', TMsgType.Notify);
2138 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2140 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2141 if (items <> nil) and not gLoadGameMode then
2142 begin
2143 e_WriteLog(' Spawning items...', TMsgType.Notify);
2144 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2145 for rec in items do CreateItem(rec);
2146 end;
2148 // Çàãðóçêà îáëàñòåé
2149 e_WriteLog(' Loading areas...', TMsgType.Notify);
2150 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2152 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2153 if areas <> nil then
2154 begin
2155 e_WriteLog(' Creating areas...', TMsgType.Notify);
2156 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2157 for rec in areas do CreateArea(rec);
2158 end;
2160 // Çàãðóçêà ìîíñòðîâ
2161 e_WriteLog(' Loading monsters...', TMsgType.Notify);
2162 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2164 gTotalMonsters := 0;
2166 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2167 if (monsters <> nil) and not gLoadGameMode then
2168 begin
2169 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
2170 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2171 for rec in monsters do CreateMonster(rec);
2172 end;
2174 //gCurrentMap := mapReader; // this will be our current map now
2175 gCurrentMapFileName := Res;
2176 //mapReader := nil;
2178 // Çàãðóçêà íåáà
2179 if (gMapInfo.SkyName <> '') then
2180 begin
2181 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
2182 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2183 FileName := g_ExtractWadName(gMapInfo.SkyName);
2185 if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
2187 if gTextureFilter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
2188 try
2189 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2190 if g_Texture_CreateWAD(BackID, s) then
2191 begin
2192 g_Game_SetupScreenSize();
2193 end
2194 else
2195 begin
2196 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2197 end;
2198 finally
2199 TEXTUREFILTER := GL_NEAREST;
2200 end;
2201 end;
2203 // Çàãðóçêà ìóçûêè
2204 ok := False;
2205 if gMapInfo.MusicName <> '' then
2206 begin
2207 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
2208 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2209 FileName := g_ExtractWadName(gMapInfo.MusicName);
2211 if FileName <> '' then
2212 FileName := GameDir+'/wads/'+FileName
2213 else
2214 begin
2215 FileName := g_ExtractWadName(Res);
2216 end;
2218 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2219 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2220 ok := True
2221 else
2222 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2223 end;
2225 // Îñòàëüíûå óñòàíâêè
2226 CreateDoorMap();
2227 CreateLiftMap();
2229 g_Items_Init();
2230 g_Weapon_Init();
2231 g_Monsters_Init();
2233 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2234 if not gLoadGameMode then g_GFX_Init();
2236 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2237 mapTextureList := nil;
2238 panels := nil;
2239 items := nil;
2240 areas := nil;
2241 triggers := nil;
2242 TriggersTable := nil;
2243 AddTextures := nil;
2245 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2246 if ok and (not gLoadGameMode) then
2247 begin
2248 gMusic.SetByName(gMapInfo.MusicName);
2249 gMusic.Play();
2250 end
2251 else
2252 begin
2253 gMusic.SetByName('');
2254 end;
2256 stt := getTimeMicro()-stt;
2257 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2258 mapOk := true;
2259 finally
2260 sfsGCEnable(); // enable releasing unused volumes
2261 //mapReader.Free();
2262 e_ClearInputBuffer(); // why not?
2263 if not mapOk then
2264 begin
2265 gCurrentMap.Free();
2266 gCurrentMap := nil;
2267 gCurrentMapFileName := '';
2268 end;
2269 end;
2271 e_WriteLog('Done loading map.', TMsgType.Notify);
2272 Result := True;
2273 end;
2276 function g_Map_GetMapInfo(Res: String): TMapInfo;
2277 var
2278 WAD: TWADFile;
2279 mapReader: TDynRecord;
2280 //Header: TMapHeaderRec_1;
2281 FileName: String;
2282 Data: Pointer;
2283 Len: Integer;
2284 begin
2285 FillChar(Result, SizeOf(Result), 0);
2286 FileName := g_ExtractWadName(Res);
2288 WAD := TWADFile.Create();
2289 if not WAD.ReadFile(FileName) then
2290 begin
2291 WAD.Free();
2292 Exit;
2293 end;
2295 //k8: it ignores path again
2296 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2297 begin
2298 WAD.Free();
2299 Exit;
2300 end;
2302 WAD.Free();
2304 try
2305 mapReader := g_Map_ParseMap(Data, Len);
2306 except
2307 mapReader := nil;
2308 FreeMem(Data);
2309 exit;
2310 end;
2312 FreeMem(Data);
2314 if (mapReader = nil) then exit;
2316 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2317 begin
2318 Result.Name := mapReader.MapName;
2319 Result.Description := mapReader.MapDesc;
2320 Result.Map := Res;
2321 Result.Author := mapReader.MapAuthor;
2322 Result.Height := mapReader.Height;
2323 Result.Width := mapReader.Width;
2324 end
2325 else
2326 begin
2327 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2328 //ZeroMemory(@Header, SizeOf(Header));
2329 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2330 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2331 Result.Map := Res;
2332 Result.Author := '';
2333 Result.Height := 0;
2334 Result.Width := 0;
2335 end;
2337 mapReader.Free();
2338 end;
2340 function g_Map_GetMapsList(WADName: string): SSArray;
2341 var
2342 WAD: TWADFile;
2343 a: Integer;
2344 ResList: SSArray;
2345 begin
2346 Result := nil;
2347 WAD := TWADFile.Create();
2348 if not WAD.ReadFile(WADName) then
2349 begin
2350 WAD.Free();
2351 Exit;
2352 end;
2353 ResList := WAD.GetMapResources();
2354 if ResList <> nil then
2355 begin
2356 for a := 0 to High(ResList) do
2357 begin
2358 SetLength(Result, Length(Result)+1);
2359 Result[High(Result)] := ResList[a];
2360 end;
2361 end;
2362 WAD.Free();
2363 end;
2365 function g_Map_Exist(Res: string): Boolean;
2366 var
2367 WAD: TWADFile;
2368 FileName, mnn: string;
2369 ResList: SSArray;
2370 a: Integer;
2371 begin
2372 Result := False;
2374 FileName := addWadExtension(g_ExtractWadName(Res));
2376 WAD := TWADFile.Create;
2377 if not WAD.ReadFile(FileName) then
2378 begin
2379 WAD.Free();
2380 Exit;
2381 end;
2383 ResList := WAD.GetMapResources();
2384 WAD.Free();
2386 mnn := g_ExtractFileName(Res);
2387 if ResList <> nil then
2388 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2389 begin
2390 Result := True;
2391 Exit;
2392 end;
2393 end;
2395 procedure g_Map_Free(freeTextures: Boolean=true);
2396 var
2397 a: Integer;
2399 procedure FreePanelArray(var panels: TPanelArray);
2400 var
2401 i: Integer;
2403 begin
2404 if panels <> nil then
2405 begin
2406 for i := 0 to High(panels) do
2407 panels[i].Free();
2408 panels := nil;
2409 end;
2410 end;
2412 begin
2413 g_GFX_Free();
2414 g_Weapon_Free();
2415 g_Items_Free();
2416 g_Triggers_Free();
2417 g_Monsters_Free();
2419 RespawnPoints := nil;
2420 if FlagPoints[FLAG_RED] <> nil then
2421 begin
2422 Dispose(FlagPoints[FLAG_RED]);
2423 FlagPoints[FLAG_RED] := nil;
2424 end;
2425 if FlagPoints[FLAG_BLUE] <> nil then
2426 begin
2427 Dispose(FlagPoints[FLAG_BLUE]);
2428 FlagPoints[FLAG_BLUE] := nil;
2429 end;
2430 //DOMFlagPoints := nil;
2432 //gDOMFlags := nil;
2434 if (Length(gCurrentMapFileName) <> 0) then
2435 begin
2436 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2437 end
2438 else
2439 begin
2440 e_LogWritefln('g_Map_Free: no previous map.', []);
2441 end;
2443 if freeTextures then
2444 begin
2445 e_LogWritefln('g_Map_Free: clearing textures...', []);
2446 if (Textures <> nil) then
2447 begin
2448 for a := 0 to High(Textures) do
2449 begin
2450 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2451 begin
2452 if Textures[a].Anim then
2453 begin
2454 g_Frames_DeleteByID(Textures[a].FramesID)
2455 end
2456 else
2457 begin
2458 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2459 begin
2460 e_DeleteTexture(Textures[a].TextureID);
2461 end;
2462 end;
2463 end;
2464 end;
2465 Textures := nil;
2466 end;
2467 TextNameHash.Free();
2468 TextNameHash := nil;
2469 BadTextNameHash.Free();
2470 BadTextNameHash := nil;
2471 gCurrentMapFileName := '';
2472 gCurrentMap.Free();
2473 gCurrentMap := nil;
2474 end;
2476 panByGUID := nil;
2478 FreePanelArray(gWalls);
2479 FreePanelArray(gRenderBackgrounds);
2480 FreePanelArray(gRenderForegrounds);
2481 FreePanelArray(gWater);
2482 FreePanelArray(gAcid1);
2483 FreePanelArray(gAcid2);
2484 FreePanelArray(gSteps);
2485 FreePanelArray(gLifts);
2486 FreePanelArray(gBlockMon);
2487 gMovingWallIds := nil;
2489 if BackID <> DWORD(-1) then
2490 begin
2491 gBackSize.X := 0;
2492 gBackSize.Y := 0;
2493 e_DeleteTexture(BackID);
2494 BackID := DWORD(-1);
2495 end;
2497 g_Game_StopAllSounds(False);
2498 gMusic.FreeSound();
2499 g_Sound_Delete(gMapInfo.MusicName);
2501 gMapInfo.Name := '';
2502 gMapInfo.Description := '';
2503 gMapInfo.MusicName := '';
2504 gMapInfo.Height := 0;
2505 gMapInfo.Width := 0;
2507 gDoorMap := nil;
2508 gLiftMap := nil;
2509 end;
2511 procedure g_Map_Update();
2512 var
2513 a, d, j: Integer;
2514 m: Word;
2515 s: String;
2517 procedure UpdatePanelArray(var panels: TPanelArray);
2518 var
2519 i: Integer;
2521 begin
2522 for i := 0 to High(panels) do panels[i].Update();
2523 end;
2525 begin
2526 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2528 UpdatePanelArray(gWalls);
2529 UpdatePanelArray(gRenderBackgrounds);
2530 UpdatePanelArray(gRenderForegrounds);
2531 UpdatePanelArray(gWater);
2532 UpdatePanelArray(gAcid1);
2533 UpdatePanelArray(gAcid2);
2534 UpdatePanelArray(gSteps);
2536 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2538 if gGameSettings.GameMode = GM_CTF then
2539 begin
2540 for a := FLAG_RED to FLAG_BLUE do
2541 begin
2542 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2543 begin
2544 with gFlags[a] do
2545 begin
2546 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2548 m := g_Obj_Move(@Obj, True, True);
2550 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2552 // Ñîïðîòèâëåíèå âîçäóõà
2553 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2555 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2556 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2557 begin
2558 g_Map_ResetFlag(a);
2559 gFlags[a].CaptureTime := 0;
2560 if a = FLAG_RED then
2561 s := _lc[I_PLAYER_FLAG_RED]
2562 else
2563 s := _lc[I_PLAYER_FLAG_BLUE];
2564 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2566 if g_Game_IsNet then
2567 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2568 Continue;
2569 end;
2571 if Count > 0 then Count -= 1;
2573 // Èãðîê áåðåò ôëàã
2574 if gPlayers <> nil then
2575 begin
2576 j := Random(Length(gPlayers)) - 1;
2577 for d := 0 to High(gPlayers) do
2578 begin
2579 Inc(j);
2580 if j > High(gPlayers) then j := 0;
2581 if gPlayers[j] <> nil then
2582 begin
2583 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2584 begin
2585 if gPlayers[j].GetFlag(a) then Break;
2586 end;
2587 end;
2588 end;
2589 end;
2590 end;
2591 end;
2592 end;
2593 end;
2594 end;
2597 // old algo
2598 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2600 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2601 var
2602 idx: Integer;
2603 begin
2604 if (panels <> nil) then
2605 begin
2606 // alas, no visible set
2607 for idx := 0 to High(panels) do
2608 begin
2609 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2610 end;
2611 end;
2612 end;
2614 begin
2615 case PanelType of
2616 PANEL_WALL: DrawPanels(gWalls);
2617 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2618 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2619 PANEL_FORE: DrawPanels(gRenderForegrounds);
2620 PANEL_WATER: DrawPanels(gWater);
2621 PANEL_ACID1: DrawPanels(gAcid1);
2622 PANEL_ACID2: DrawPanels(gAcid2);
2623 PANEL_STEP: DrawPanels(gSteps);
2624 end;
2625 end;
2628 // new algo
2629 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2630 var
2631 mwit: PPanel;
2632 it: TPanelGrid.Iter;
2633 begin
2634 dplClear();
2635 it := mapGrid.forEachInAABB(x0, y0, wdt, hgt, GridDrawableMask);
2636 for mwit in it do if (((mwit^.tag and GridTagDoor) <> 0) = mwit^.Door) then gDrawPanelList.insert(mwit^);
2637 it.release();
2638 // list will be rendered in `g_game.DrawPlayer()`
2639 end;
2642 procedure g_Map_DrawPanelShadowVolumes (lightX: Integer; lightY: Integer; radius: Integer);
2643 var
2644 mwit: PPanel;
2645 it: TPanelGrid.Iter;
2646 begin
2647 it := mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, (GridTagWall or GridTagDoor));
2648 for mwit in it do mwit^.DrawShadowVolume(lightX, lightY, radius);
2649 it.release();
2650 end;
2653 procedure g_Map_DrawBack(dx, dy: Integer);
2654 begin
2655 if gDrawBackGround and (BackID <> DWORD(-1)) then
2656 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2657 else
2658 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2659 end;
2661 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2662 PanelType: Word; b1x3: Boolean=false): Boolean;
2663 var
2664 a, h: Integer;
2665 begin
2666 Result := False;
2668 if WordBool(PanelType and PANEL_WALL) then
2669 if gWalls <> nil then
2670 begin
2671 h := High(gWalls);
2673 for a := 0 to h do
2674 if gWalls[a].Enabled and
2675 g_Collide(X, Y, Width, Height,
2676 gWalls[a].X, gWalls[a].Y,
2677 gWalls[a].Width, gWalls[a].Height) then
2678 begin
2679 Result := True;
2680 Exit;
2681 end;
2682 end;
2684 if WordBool(PanelType and PANEL_WATER) then
2685 if gWater <> nil then
2686 begin
2687 h := High(gWater);
2689 for a := 0 to h do
2690 if g_Collide(X, Y, Width, Height,
2691 gWater[a].X, gWater[a].Y,
2692 gWater[a].Width, gWater[a].Height) then
2693 begin
2694 Result := True;
2695 Exit;
2696 end;
2697 end;
2699 if WordBool(PanelType and PANEL_ACID1) then
2700 if gAcid1 <> nil then
2701 begin
2702 h := High(gAcid1);
2704 for a := 0 to h do
2705 if g_Collide(X, Y, Width, Height,
2706 gAcid1[a].X, gAcid1[a].Y,
2707 gAcid1[a].Width, gAcid1[a].Height) then
2708 begin
2709 Result := True;
2710 Exit;
2711 end;
2712 end;
2714 if WordBool(PanelType and PANEL_ACID2) then
2715 if gAcid2 <> nil then
2716 begin
2717 h := High(gAcid2);
2719 for a := 0 to h do
2720 if g_Collide(X, Y, Width, Height,
2721 gAcid2[a].X, gAcid2[a].Y,
2722 gAcid2[a].Width, gAcid2[a].Height) then
2723 begin
2724 Result := True;
2725 Exit;
2726 end;
2727 end;
2729 if WordBool(PanelType and PANEL_STEP) then
2730 if gSteps <> nil then
2731 begin
2732 h := High(gSteps);
2734 for a := 0 to h do
2735 if g_Collide(X, Y, Width, Height,
2736 gSteps[a].X, gSteps[a].Y,
2737 gSteps[a].Width, gSteps[a].Height) then
2738 begin
2739 Result := True;
2740 Exit;
2741 end;
2742 end;
2744 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2745 if gLifts <> nil then
2746 begin
2747 h := High(gLifts);
2749 for a := 0 to h do
2750 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2751 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2752 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2753 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2754 g_Collide(X, Y, Width, Height,
2755 gLifts[a].X, gLifts[a].Y,
2756 gLifts[a].Width, gLifts[a].Height) then
2757 begin
2758 Result := True;
2759 Exit;
2760 end;
2761 end;
2763 if WordBool(PanelType and PANEL_BLOCKMON) then
2764 if gBlockMon <> nil then
2765 begin
2766 h := High(gBlockMon);
2768 for a := 0 to h do
2769 if ( (not b1x3) or
2770 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2771 g_Collide(X, Y, Width, Height,
2772 gBlockMon[a].X, gBlockMon[a].Y,
2773 gBlockMon[a].Width, gBlockMon[a].Height) then
2774 begin
2775 Result := True;
2776 Exit;
2777 end;
2778 end;
2779 end;
2781 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2782 var
2783 texid: DWORD;
2785 function checkPanels (constref panels: TPanelArray): Boolean;
2786 var
2787 a: Integer;
2788 begin
2789 result := false;
2790 if panels = nil then exit;
2791 for a := 0 to High(panels) do
2792 begin
2793 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2794 begin
2795 result := true;
2796 texid := panels[a].GetTextureID();
2797 exit;
2798 end;
2799 end;
2800 end;
2802 begin
2803 texid := LongWord(TEXTURE_NONE);
2804 result := texid;
2805 if not checkPanels(gWater) then
2806 if not checkPanels(gAcid1) then
2807 if not checkPanels(gAcid2) then exit;
2808 result := texid;
2809 end;
2812 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2813 const
2814 SlowMask = GridTagLift or GridTagBlockMon;
2816 function checker (pan: TPanel; tag: Integer): Boolean;
2817 begin
2819 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2820 begin
2821 result := pan.Enabled;
2822 exit;
2823 end;
2826 if ((tag and GridTagLift) <> 0) then
2827 begin
2828 result :=
2829 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2830 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2831 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2832 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2833 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2834 exit;
2835 end;
2837 if ((tag and GridTagBlockMon) <> 0) then
2838 begin
2839 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2840 exit;
2841 end;
2843 // other shit
2844 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2845 result := true; // i found her!
2846 end;
2848 var
2849 tagmask: Integer = 0;
2850 mwit: PPanel;
2851 it: TPanelGrid.Iter;
2852 pan: TPanel;
2853 begin
2854 result := false;
2855 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2856 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2857 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2858 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2859 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2860 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2861 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2863 if (tagmask = 0) then exit; // just in case
2865 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2866 if gdbg_map_use_accel_coldet then
2867 begin
2868 if ((tagmask and SlowMask) <> 0) then
2869 begin
2870 // slow
2871 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2872 for mwit in it do
2873 begin
2874 pan := mwit^;
2875 if ((pan.tag and GridTagLift) <> 0) then
2876 begin
2877 result :=
2878 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2879 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2880 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2881 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2882 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2883 end
2884 else if ((pan.tag and GridTagBlockMon) <> 0) then
2885 begin
2886 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2887 end
2888 else
2889 begin
2890 // other shit
2891 result := true; // i found her!
2892 end;
2893 if (result) then break;
2894 end;
2895 end
2896 else
2897 begin
2898 // fast
2899 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2900 result := (it.length > 0);
2901 end;
2902 it.release();
2903 end
2904 else
2905 begin
2906 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2907 end;
2908 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2909 end;
2912 // returns `true` if we need to stop
2913 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2914 begin
2915 result := false;
2916 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2917 // check priorities
2918 case cctype of
2919 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2920 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2921 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2922 end;
2923 // collision?
2924 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2925 // yeah
2926 texid := pan.GetTextureID();
2927 // water? water has the highest priority, so stop right here
2928 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2929 // acid2?
2930 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2931 // acid1?
2932 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2933 end;
2935 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2936 var
2937 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2938 mwit: PPanel;
2939 it: TPanelGrid.Iter;
2940 begin
2941 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2942 if gdbg_map_use_accel_coldet then
2943 begin
2944 result := LongWord(TEXTURE_NONE);
2945 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2946 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2947 it.release();
2948 end
2949 else
2950 begin
2951 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2952 end;
2953 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2954 end;
2957 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2958 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2959 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2962 procedure g_Map_EnableWallGUID (pguid: Integer);
2963 var
2964 pan: TPanel;
2965 begin
2966 //pan := gWalls[ID];
2967 pan := g_Map_PanelByGUID(pguid);
2968 if (pan = nil) then exit;
2969 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2971 pan.Enabled := True;
2972 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2974 mapGrid.proxyEnabled[pan.proxyId] := true;
2975 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2976 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2978 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2979 // mark platform as interesting
2980 pan.setDirty();
2982 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2983 //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);
2984 {$ENDIF}
2985 end;
2988 procedure g_Map_DisableWallGUID (pguid: Integer);
2989 var
2990 pan: TPanel;
2991 begin
2992 //pan := gWalls[ID];
2993 pan := g_Map_PanelByGUID(pguid);
2994 if (pan = nil) then exit;
2995 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2997 pan.Enabled := False;
2998 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
3000 mapGrid.proxyEnabled[pan.proxyId] := false;
3001 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
3003 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3004 // mark platform as interesting
3005 pan.setDirty();
3007 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
3008 //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);
3009 {$ENDIF}
3010 end;
3013 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
3014 var
3015 tp: TPanel;
3016 begin
3017 tp := g_Map_PanelByGUID(pguid);
3018 if (tp = nil) then exit;
3019 tp.NextTexture(AnimLoop);
3020 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
3021 end;
3024 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
3025 var
3026 pan: TPanel;
3027 begin
3028 //pan := gLifts[ID];
3029 pan := g_Map_PanelByGUID(pguid);
3030 if (pan = nil) then exit;
3031 if not pan.isGLift then exit;
3033 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
3035 with {gLifts[ID]} pan do
3036 begin
3037 LiftType := t;
3039 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
3040 //TODO: make separate lift tags, and change tag here
3042 case LiftType of
3043 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
3044 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
3045 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
3046 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
3047 end;
3049 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3050 // mark platform as interesting
3051 pan.setDirty();
3052 end;
3053 end;
3056 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
3057 var
3058 a: Integer;
3059 PointsArray: Array of TRespawnPoint;
3060 begin
3061 Result := False;
3062 SetLength(PointsArray, 0);
3064 if RespawnPoints = nil then
3065 Exit;
3067 for a := 0 to High(RespawnPoints) do
3068 if RespawnPoints[a].PointType = PointType then
3069 begin
3070 SetLength(PointsArray, Length(PointsArray)+1);
3071 PointsArray[High(PointsArray)] := RespawnPoints[a];
3072 end;
3074 if PointsArray = nil then
3075 Exit;
3077 RespawnPoint := PointsArray[Random(Length(PointsArray))];
3078 Result := True;
3079 end;
3081 function g_Map_GetPointCount(PointType: Byte): Word;
3082 var
3083 a: Integer;
3084 begin
3085 Result := 0;
3087 if RespawnPoints = nil then
3088 Exit;
3090 for a := 0 to High(RespawnPoints) do
3091 if RespawnPoints[a].PointType = PointType then
3092 Result := Result + 1;
3093 end;
3095 function g_Map_HaveFlagPoints(): Boolean;
3096 begin
3097 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3098 end;
3100 procedure g_Map_ResetFlag(Flag: Byte);
3101 begin
3102 with gFlags[Flag] do
3103 begin
3104 Obj.X := -1000;
3105 Obj.Y := -1000;
3106 Obj.Vel.X := 0;
3107 Obj.Vel.Y := 0;
3108 Direction := TDirection.D_LEFT;
3109 State := FLAG_STATE_NONE;
3110 if FlagPoints[Flag] <> nil then
3111 begin
3112 Obj.X := FlagPoints[Flag]^.X;
3113 Obj.Y := FlagPoints[Flag]^.Y;
3114 Direction := FlagPoints[Flag]^.Direction;
3115 State := FLAG_STATE_NORMAL;
3116 end;
3117 Count := -1;
3118 end;
3119 end;
3121 procedure g_Map_DrawFlags();
3122 var
3123 i, dx: Integer;
3124 Mirror: TMirrorType;
3125 begin
3126 if gGameSettings.GameMode <> GM_CTF then
3127 Exit;
3129 for i := FLAG_RED to FLAG_BLUE do
3130 with gFlags[i] do
3131 if State <> FLAG_STATE_CAPTURED then
3132 begin
3133 if State = FLAG_STATE_NONE then
3134 continue;
3136 if Direction = TDirection.D_LEFT then
3137 begin
3138 Mirror := TMirrorType.Horizontal;
3139 dx := -1;
3140 end
3141 else
3142 begin
3143 Mirror := TMirrorType.None;
3144 dx := 1;
3145 end;
3147 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3149 if g_debug_Frames then
3150 begin
3151 e_DrawQuad(Obj.X+Obj.Rect.X,
3152 Obj.Y+Obj.Rect.Y,
3153 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3154 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3155 0, 255, 0);
3156 end;
3157 end;
3158 end;
3161 procedure g_Map_SaveState (st: TStream);
3162 var
3163 str: String;
3165 procedure savePanels ();
3166 var
3167 pan: TPanel;
3168 begin
3169 // Ñîõðàíÿåì ïàíåëè
3170 utils.writeInt(st, LongInt(Length(panByGUID)));
3171 for pan in panByGUID do pan.SaveState(st);
3172 end;
3174 procedure saveFlag (flag: PFlag);
3175 var
3176 b: Byte;
3177 begin
3178 utils.writeSign(st, 'FLAG');
3179 utils.writeInt(st, Byte(0)); // version
3180 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3181 utils.writeInt(st, Byte(flag^.RespawnType));
3182 // Ñîñòîÿíèå ôëàãà
3183 utils.writeInt(st, Byte(flag^.State));
3184 // Íàïðàâëåíèå ôëàãà
3185 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
3186 utils.writeInt(st, Byte(b));
3187 // Îáúåêò ôëàãà
3188 Obj_SaveState(st, @flag^.Obj);
3189 end;
3191 begin
3192 savePanels();
3194 // Ñîõðàíÿåì ìóçûêó
3195 utils.writeSign(st, 'MUSI');
3196 utils.writeInt(st, Byte(0));
3197 // Íàçâàíèå ìóçûêè
3198 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3199 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3200 utils.writeStr(st, str);
3201 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3202 utils.writeInt(st, LongWord(gMusic.GetPosition()));
3203 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3204 utils.writeBool(st, gMusic.SpecPause);
3206 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3207 utils.writeInt(st, LongInt(gTotalMonsters));
3208 ///// /////
3210 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3211 if (gGameSettings.GameMode = GM_CTF) then
3212 begin
3213 // Ôëàã Êðàñíîé êîìàíäû
3214 saveFlag(@gFlags[FLAG_RED]);
3215 // Ôëàã Ñèíåé êîìàíäû
3216 saveFlag(@gFlags[FLAG_BLUE]);
3217 end;
3218 ///// /////
3220 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3221 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3222 begin
3223 // Î÷êè Êðàñíîé êîìàíäû
3224 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
3225 // Î÷êè Ñèíåé êîìàíäû
3226 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
3227 end;
3228 ///// /////
3229 end;
3232 procedure g_Map_LoadState (st: TStream);
3233 var
3234 dw: DWORD;
3235 str: String;
3236 boo: Boolean;
3238 procedure loadPanels ();
3239 var
3240 pan: TPanel;
3241 begin
3242 // Çàãðóæàåì ïàíåëè
3243 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
3244 for pan in panByGUID do
3245 begin
3246 pan.LoadState(st);
3247 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3248 end;
3249 end;
3251 procedure loadFlag (flag: PFlag);
3252 var
3253 b: Byte;
3254 begin
3255 // Ñèãíàòóðà ôëàãà
3256 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
3257 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
3258 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3259 flag^.RespawnType := utils.readByte(st);
3260 // Ñîñòîÿíèå ôëàãà
3261 flag^.State := utils.readByte(st);
3262 // Íàïðàâëåíèå ôëàãà
3263 b := utils.readByte(st);
3264 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
3265 // Îáúåêò ôëàãà
3266 Obj_LoadState(@flag^.Obj, st);
3267 end;
3269 begin
3270 if (st = nil) then exit;
3272 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3273 loadPanels();
3274 ///// /////
3276 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3277 g_GFX_Init();
3278 //mapCreateGrid();
3280 ///// Çàãðóæàåì ìóçûêó: /////
3281 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
3282 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
3283 // Íàçâàíèå ìóçûêè
3284 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3285 str := utils.readStr(st);
3286 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3287 dw := utils.readLongWord(st);
3288 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3289 boo := utils.readBool(st);
3290 // Çàïóñêàåì ýòó ìóçûêó
3291 gMusic.SetByName(str);
3292 gMusic.SpecPause := boo;
3293 gMusic.Play();
3294 gMusic.Pause(true);
3295 gMusic.SetPosition(dw);
3296 ///// /////
3298 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3299 gTotalMonsters := utils.readLongInt(st);
3300 ///// /////
3302 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3303 if (gGameSettings.GameMode = GM_CTF) then
3304 begin
3305 // Ôëàã Êðàñíîé êîìàíäû
3306 loadFlag(@gFlags[FLAG_RED]);
3307 // Ôëàã Ñèíåé êîìàíäû
3308 loadFlag(@gFlags[FLAG_BLUE]);
3309 end;
3310 ///// /////
3312 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3313 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3314 begin
3315 // Î÷êè Êðàñíîé êîìàíäû
3316 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
3317 // Î÷êè Ñèíåé êîìàíäû
3318 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
3319 end;
3320 ///// /////
3321 end;
3324 // trace liquid, stepping by `dx` and `dy`
3325 // return last seen liquid coords, and `false` if we're started outside of the liquid
3326 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3327 const
3328 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3329 begin
3330 topx := x;
3331 topy := y;
3332 // started outside of the liquid?
3333 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3334 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
3335 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3336 result := true;
3337 while true do
3338 begin
3339 Inc(x, dx);
3340 Inc(y, dy);
3341 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3342 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
3343 topx := x;
3344 topy := y;
3345 end;
3346 end;
3349 begin
3350 DynWarningCB := mapWarningCB;
3351 end.