DEADSOFTWARE

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