DEADSOFTWARE

`Grid.forEachAtPoint()` converted to no-callback
[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, nil, (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, nil, 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;
614 function checker (pan: TPanel; tag: Integer): Boolean;
615 begin
617 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
618 begin
619 result := pan.Enabled; // stop if wall is enabled
620 exit;
621 end;
624 if ((tag and GridTagLift) <> 0) then
625 begin
626 // stop if the lift of the right type
627 result :=
628 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
629 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
630 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
631 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
632 exit;
633 end;
635 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
636 end;
638 var
639 tagmask: Integer = 0;
640 pmark: PoolMark;
641 hitcount: Integer;
642 ppan: PPanel;
643 begin
644 result := false;
646 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
647 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
648 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
649 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
650 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
651 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
652 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
654 if (tagmask = 0) then exit;// just in case
656 pmark := framePool.mark();
657 if ((tagmask and GridTagLift) <> 0) then
658 begin
659 // slow
660 hitcount := mapGrid.forEachAtPoint(x, y, tagmask);
661 ppan := PPanel(framePool.getPtr(pmark));
662 while (hitcount > 0) do
663 begin
664 if (xxPanAtPointChecker(ppan^, PanelType)) then begin result := true; break; end;
665 Inc(ppan);
666 Dec(hitcount);
667 end;
668 end
669 else
670 begin
671 // fast
672 result := (mapGrid.forEachAtPoint(x, y, tagmask, false, true) <> 0); // firsthit
673 end;
674 framePool.release(pmark);
675 end;
678 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
679 var
680 pmark: PoolMark;
681 hitcount: Integer;
682 begin
683 result := nil;
684 if (tagmask = 0) then exit;
685 //result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
686 pmark := framePool.mark();
687 hitcount := mapGrid.forEachAtPoint(x, y, tagmask, false, true); // firsthit
688 if (hitcount <> 0) then result := PPanel(framePool.getPtr(pmark))^;
689 framePool.release(pmark);
690 end;
693 function g_Map_IsSpecialTexture(Texture: String): Boolean;
694 begin
695 Result := (Texture = TEXTURE_NAME_WATER) or
696 (Texture = TEXTURE_NAME_ACID1) or
697 (Texture = TEXTURE_NAME_ACID2);
698 end;
700 procedure CreateDoorMap();
701 var
702 PanelArray: Array of record
703 X, Y: Integer;
704 Width, Height: Word;
705 Active: Boolean;
706 PanelID: DWORD;
707 end;
708 a, b, c, m, i, len: Integer;
709 ok: Boolean;
710 begin
711 if gWalls = nil then
712 Exit;
714 i := 0;
715 len := 128;
716 SetLength(PanelArray, len);
718 for a := 0 to High(gWalls) do
719 if gWalls[a].Door then
720 begin
721 PanelArray[i].X := gWalls[a].X;
722 PanelArray[i].Y := gWalls[a].Y;
723 PanelArray[i].Width := gWalls[a].Width;
724 PanelArray[i].Height := gWalls[a].Height;
725 PanelArray[i].Active := True;
726 PanelArray[i].PanelID := a;
728 i := i + 1;
729 if i = len then
730 begin
731 len := len + 128;
732 SetLength(PanelArray, len);
733 end;
734 end;
736 // Íåò äâåðåé:
737 if i = 0 then
738 begin
739 PanelArray := nil;
740 Exit;
741 end;
743 SetLength(gDoorMap, 0);
745 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
747 for a := 0 to i-1 do
748 if PanelArray[a].Active then
749 begin
750 PanelArray[a].Active := False;
751 m := Length(gDoorMap);
752 SetLength(gDoorMap, m+1);
753 SetLength(gDoorMap[m], 1);
754 gDoorMap[m, 0] := PanelArray[a].PanelID;
755 ok := True;
757 while ok do
758 begin
759 ok := False;
761 for b := 0 to i-1 do
762 if PanelArray[b].Active then
763 for c := 0 to High(gDoorMap[m]) do
764 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
765 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
766 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
767 PanelArray[b].Width, PanelArray[b].Height,
768 gWalls[gDoorMap[m, c]].X,
769 gWalls[gDoorMap[m, c]].Y,
770 gWalls[gDoorMap[m, c]].Width,
771 gWalls[gDoorMap[m, c]].Height) then
772 begin
773 PanelArray[b].Active := False;
774 SetLength(gDoorMap[m],
775 Length(gDoorMap[m])+1);
776 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
777 ok := True;
778 Break;
779 end;
780 end;
782 g_Game_StepLoading();
783 end;
785 PanelArray := nil;
786 end;
788 procedure CreateLiftMap();
789 var
790 PanelArray: Array of record
791 X, Y: Integer;
792 Width, Height: Word;
793 Active: Boolean;
794 end;
795 a, b, c, len, i, j: Integer;
796 ok: Boolean;
797 begin
798 if gLifts = nil then
799 Exit;
801 len := Length(gLifts);
802 SetLength(PanelArray, len);
804 for a := 0 to len-1 do
805 begin
806 PanelArray[a].X := gLifts[a].X;
807 PanelArray[a].Y := gLifts[a].Y;
808 PanelArray[a].Width := gLifts[a].Width;
809 PanelArray[a].Height := gLifts[a].Height;
810 PanelArray[a].Active := True;
811 end;
813 SetLength(gLiftMap, len);
814 i := 0;
816 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
818 for a := 0 to len-1 do
819 if PanelArray[a].Active then
820 begin
821 PanelArray[a].Active := False;
822 SetLength(gLiftMap[i], 32);
823 j := 0;
824 gLiftMap[i, j] := a;
825 ok := True;
827 while ok do
828 begin
829 ok := False;
830 for b := 0 to len-1 do
831 if PanelArray[b].Active then
832 for c := 0 to j do
833 if g_CollideAround(PanelArray[b].X,
834 PanelArray[b].Y,
835 PanelArray[b].Width,
836 PanelArray[b].Height,
837 PanelArray[gLiftMap[i, c]].X,
838 PanelArray[gLiftMap[i, c]].Y,
839 PanelArray[gLiftMap[i, c]].Width,
840 PanelArray[gLiftMap[i, c]].Height) then
841 begin
842 PanelArray[b].Active := False;
843 j := j+1;
844 if j > High(gLiftMap[i]) then
845 SetLength(gLiftMap[i],
846 Length(gLiftMap[i])+32);
848 gLiftMap[i, j] := b;
849 ok := True;
851 Break;
852 end;
853 end;
855 SetLength(gLiftMap[i], j+1);
856 i := i+1;
858 g_Game_StepLoading();
859 end;
861 SetLength(gLiftMap, i);
863 PanelArray := nil;
864 end;
866 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
867 var
868 len: Integer;
869 panels: ^TPanelArray;
870 pan: TPanel;
871 pguid: Integer;
872 begin
873 Result := -1;
875 case PanelRec.PanelType of
876 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
877 PANEL_BACK: panels := @gRenderBackgrounds;
878 PANEL_FORE: panels := @gRenderForegrounds;
879 PANEL_WATER: panels := @gWater;
880 PANEL_ACID1: panels := @gAcid1;
881 PANEL_ACID2: panels := @gAcid2;
882 PANEL_STEP: panels := @gSteps;
883 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
884 PANEL_BLOCKMON: panels := @gBlockMon;
885 else exit;
886 end;
888 len := Length(panels^);
889 SetLength(panels^, len+1);
891 pguid := Length(panByGUID);
892 SetLength(panByGUID, pguid+1); //FIXME!
893 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
894 assert(pguid >= 0);
895 assert(pguid < Length(panByGUID));
896 panByGUID[pguid] := pan;
897 panels^[len] := pan;
898 pan.arrIdx := len;
899 pan.proxyId := -1;
900 pan.tag := panelTypeToTag(PanelRec.PanelType);
902 PanelRec.user['panel_guid'] := pguid;
904 //result := len;
905 result := pguid;
906 end;
909 function CreateNullTexture(RecName: String): Integer;
910 begin
911 RecName := toLowerCase1251(RecName);
912 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
913 if TextNameHash.get(RecName, result) then exit; // i found her!
915 SetLength(Textures, Length(Textures)+1);
916 result := High(Textures);
918 with Textures[High(Textures)] do
919 begin
920 TextureName := RecName;
921 Width := 1;
922 Height := 1;
923 Anim := False;
924 TextureID := LongWord(TEXTURE_NONE);
925 end;
927 TextNameHash.put(RecName, result);
928 end;
931 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
932 var
933 WAD: TWADFile;
934 TextureData: Pointer;
935 WADName: String;
936 a, ResLength: Integer;
937 begin
938 RecName := toLowerCase1251(RecName);
939 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
940 if TextNameHash.get(RecName, result) then
941 begin
942 // i found her!
943 //e_LogWritefln('texture ''%s'' already loaded (%s)', [RecName, result]);
944 exit;
945 end;
947 Result := -1;
949 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
952 if Textures <> nil then
953 begin
954 for a := 0 to High(Textures) do
955 begin
956 if (Textures[a].TextureName = RecName) then
957 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
958 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
959 Result := a;
960 Exit;
961 end;
962 end;
963 end;
966 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
967 if (RecName = TEXTURE_NAME_WATER) or
968 (RecName = TEXTURE_NAME_ACID1) or
969 (RecName = TEXTURE_NAME_ACID2) then
970 begin
971 SetLength(Textures, Length(Textures)+1);
973 with Textures[High(Textures)] do
974 begin
975 TextureName := RecName;
976 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
977 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
978 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
980 Anim := False;
981 end;
983 result := High(Textures);
984 TextNameHash.put(RecName, result);
985 Exit;
986 end;
988 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
989 WADName := g_ExtractWadName(RecName);
991 WAD := TWADFile.Create();
993 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
995 WAD.ReadFile(WADName);
997 //txname := RecName;
999 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
1000 begin
1001 FreeMem(TextureData);
1002 RecName := 'COMMON\ALIEN';
1003 end;
1006 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
1007 begin
1008 SetLength(Textures, Length(Textures)+1);
1009 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
1010 begin
1011 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1012 SetLength(Textures, Length(Textures)-1);
1013 result := -1;
1014 Exit;
1015 end;
1016 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
1017 FreeMem(TextureData);
1018 Textures[High(Textures)].TextureName := RecName;
1019 Textures[High(Textures)].Anim := False;
1021 result := High(Textures);
1022 TextNameHash.put(RecName, result);
1023 end
1024 else // Íåò òàêîãî ðåóñðñà â WAD'å
1025 begin
1026 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
1027 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1028 if log and (not BadTextNameHash.get(RecName, a)) then
1029 begin
1030 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1031 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1032 end;
1033 BadTextNameHash.put(RecName, -1);
1034 end;
1036 WAD.Free();
1037 end;
1040 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
1041 var
1042 WAD: TWADFile;
1043 TextureWAD: PChar = nil;
1044 TextData: Pointer = nil;
1045 TextureData: Pointer = nil;
1046 cfg: TConfig = nil;
1047 WADName: String;
1048 ResLength: Integer;
1049 TextureResource: String;
1050 _width, _height, _framecount, _speed: Integer;
1051 _backanimation: Boolean;
1052 //imgfmt: string;
1053 ia: TDynImageDataArray = nil;
1054 f, c, frdelay, frloop: Integer;
1055 begin
1056 RecName := toLowerCase1251(RecName);
1057 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
1058 if TextNameHash.get(RecName, result) then
1059 begin
1060 // i found her!
1061 //e_LogWritefln('animated texture ''%s'' already loaded (%s)', [RecName, result]);
1062 exit;
1063 end;
1065 result := -1;
1067 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1069 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1070 if BadTextNameHash.get(RecName, f) then
1071 begin
1072 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1073 exit;
1074 end;
1076 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1077 WADName := g_ExtractWadName(RecName);
1079 WAD := TWADFile.Create();
1080 try
1081 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1083 WAD.ReadFile(WADName);
1085 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1086 begin
1087 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1088 if log and (not BadTextNameHash.get(RecName, f)) then
1089 begin
1090 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1091 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1092 end;
1093 BadTextNameHash.put(RecName, -1);
1094 exit;
1095 end;
1097 {TEST
1098 if WADName = Map then
1099 begin
1100 //FreeMem(TextureWAD);
1101 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1102 end;
1105 WAD.FreeWAD();
1107 if ResLength < 6 then
1108 begin
1109 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), TMsgType.Warning);
1110 BadTextNameHash.put(RecName, -1);
1111 exit;
1112 end;
1114 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1115 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1116 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1117 begin
1118 // íåò, ýòî ñóïåðìåí!
1119 if not WAD.ReadMemory(TextureWAD, ResLength) then
1120 begin
1121 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), TMsgType.Warning);
1122 BadTextNameHash.put(RecName, -1);
1123 exit;
1124 end;
1126 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1127 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1128 begin
1129 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), TMsgType.Warning);
1130 BadTextNameHash.put(RecName, -1);
1131 exit;
1132 end;
1134 cfg := TConfig.CreateMem(TextData, ResLength);
1136 TextureResource := cfg.ReadStr('', 'resource', '');
1137 if TextureResource = '' then
1138 begin
1139 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), TMsgType.Warning);
1140 BadTextNameHash.put(RecName, -1);
1141 exit;
1142 end;
1144 _width := cfg.ReadInt('', 'framewidth', 0);
1145 _height := cfg.ReadInt('', 'frameheight', 0);
1146 _framecount := cfg.ReadInt('', 'framecount', 0);
1147 _speed := cfg.ReadInt('', 'waitcount', 0);
1148 _backanimation := cfg.ReadBool('', 'backanimation', False);
1150 cfg.Free();
1151 cfg := nil;
1153 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1154 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1155 begin
1156 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), TMsgType.Warning);
1157 BadTextNameHash.put(RecName, -1);
1158 exit;
1159 end;
1161 WAD.Free();
1162 WAD := nil;
1164 SetLength(Textures, Length(Textures)+1);
1165 with Textures[High(Textures)] do
1166 begin
1167 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1168 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1169 begin
1170 TextureName := RecName;
1171 Width := _width;
1172 Height := _height;
1173 Anim := True;
1174 FramesCount := _framecount;
1175 Speed := _speed;
1176 result := High(Textures);
1177 TextNameHash.put(RecName, result);
1178 end
1179 else
1180 begin
1181 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1182 if log and (not BadTextNameHash.get(RecName, f)) then
1183 begin
1184 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1185 end;
1186 BadTextNameHash.put(RecName, -1);
1187 end;
1188 end;
1189 end
1190 else
1191 begin
1192 // try animated image
1194 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1195 if length(imgfmt) = 0 then
1196 begin
1197 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1198 exit;
1199 end;
1201 GlobalMetadata.ClearMetaItems();
1202 GlobalMetadata.ClearMetaItemsForSaving();
1203 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1204 begin
1205 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), TMsgType.Warning);
1206 BadTextNameHash.put(RecName, -1);
1207 exit;
1208 end;
1209 if length(ia) = 0 then
1210 begin
1211 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), TMsgType.Warning);
1212 BadTextNameHash.put(RecName, -1);
1213 exit;
1214 end;
1216 WAD.Free();
1217 WAD := nil;
1219 _width := ia[0].width;
1220 _height := ia[0].height;
1221 _framecount := length(ia);
1222 _speed := 1;
1223 _backanimation := false;
1224 frdelay := -1;
1225 frloop := -666;
1226 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1227 begin
1228 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1229 try
1230 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1231 frdelay := f;
1232 if f < 0 then f := 0;
1233 // rounding ;-)
1234 c := f mod 28;
1235 if c < 13 then c := 0 else c := 1;
1236 f := (f div 28)+c;
1237 if f < 1 then f := 1 else if f > 255 then f := 255;
1238 _speed := f;
1239 except
1240 end;
1241 end;
1242 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1243 begin
1244 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1245 try
1246 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1247 frloop := f;
1248 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1249 except
1250 end;
1251 end;
1252 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1253 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1254 f := ord(_backanimation);
1255 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);
1257 SetLength(Textures, Length(Textures)+1);
1258 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1259 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1260 begin
1261 Textures[High(Textures)].TextureName := RecName;
1262 Textures[High(Textures)].Width := _width;
1263 Textures[High(Textures)].Height := _height;
1264 Textures[High(Textures)].Anim := True;
1265 Textures[High(Textures)].FramesCount := length(ia);
1266 Textures[High(Textures)].Speed := _speed;
1267 result := High(Textures);
1268 TextNameHash.put(RecName, result);
1269 //writeln(' CREATED!');
1270 end
1271 else
1272 begin
1273 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1274 if log and (not BadTextNameHash.get(RecName, f)) then
1275 begin
1276 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), TMsgType.Warning);
1277 end;
1278 BadTextNameHash.put(RecName, -1);
1279 end;
1280 end;
1281 finally
1282 for f := 0 to High(ia) do FreeImage(ia[f]);
1283 WAD.Free();
1284 cfg.Free();
1285 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1286 if (TextData <> nil) then FreeMem(TextData);
1287 if (TextureData <> nil) then FreeMem(TextureData);
1288 end;
1289 end;
1291 procedure CreateItem(Item: TDynRecord);
1292 begin
1293 if g_Game_IsClient then Exit;
1295 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1296 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1297 Exit;
1299 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1300 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1301 end;
1303 procedure CreateArea(Area: TDynRecord);
1304 var
1305 a: Integer;
1306 id: DWORD = 0;
1307 begin
1308 case Area.AreaType of
1309 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1310 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1311 begin
1312 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1313 with RespawnPoints[High(RespawnPoints)] do
1314 begin
1315 X := Area.X;
1316 Y := Area.Y;
1317 Direction := TDirection(Area.Direction);
1319 case Area.AreaType of
1320 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1321 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1322 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1323 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1324 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1325 end;
1326 end;
1327 end;
1329 AREA_REDFLAG, AREA_BLUEFLAG:
1330 begin
1331 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1333 if FlagPoints[a] <> nil then Exit;
1335 New(FlagPoints[a]);
1337 with FlagPoints[a]^ do
1338 begin
1339 X := Area.X-FLAGRECT.X;
1340 Y := Area.Y-FLAGRECT.Y;
1341 Direction := TDirection(Area.Direction);
1342 end;
1344 with gFlags[a] do
1345 begin
1346 case a of
1347 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1348 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1349 end;
1351 Animation := TAnimation.Create(id, True, 8);
1352 Obj.Rect := FLAGRECT;
1354 g_Map_ResetFlag(a);
1355 end;
1356 end;
1358 AREA_DOMFLAG:
1359 begin
1360 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1361 with DOMFlagPoints[High(DOMFlagPoints)] do
1362 begin
1363 X := Area.X;
1364 Y := Area.Y;
1365 Direction := TDirection(Area.Direction);
1366 end;
1368 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1369 end;
1370 end;
1371 end;
1373 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1374 var
1375 _trigger: TTrigger;
1376 begin
1377 result := -1;
1378 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1380 with _trigger do
1381 begin
1382 mapId := Trigger.id;
1383 mapIndex := amapIdx;
1384 X := Trigger.X;
1385 Y := Trigger.Y;
1386 Width := Trigger.Width;
1387 Height := Trigger.Height;
1388 Enabled := Trigger.Enabled;
1389 TexturePanelGUID := atpanid;
1390 TriggerType := Trigger.TriggerType;
1391 ActivateType := Trigger.ActivateType;
1392 Keys := Trigger.Keys;
1393 trigPanelGUID := atrigpanid;
1394 end;
1396 result := Integer(g_Triggers_Create(_trigger, Trigger));
1397 end;
1399 procedure CreateMonster(monster: TDynRecord);
1400 var
1401 a: Integer;
1402 mon: TMonster;
1403 begin
1404 if g_Game_IsClient then Exit;
1406 if (gGameSettings.GameType = GT_SINGLE)
1407 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1408 begin
1409 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1411 if gTriggers <> nil then
1412 begin
1413 for a := 0 to High(gTriggers) do
1414 begin
1415 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1416 begin
1417 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1418 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1419 end;
1420 end;
1421 end;
1423 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1424 end;
1425 end;
1427 procedure g_Map_ReAdd_DieTriggers();
1429 function monsDieTrig (mon: TMonster): Boolean;
1430 var
1431 a: Integer;
1432 //tw: TStrTextWriter;
1433 begin
1434 result := false; // don't stop
1435 mon.ClearTriggers();
1436 for a := 0 to High(gTriggers) do
1437 begin
1438 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1439 begin
1440 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1442 tw := TStrTextWriter.Create();
1443 try
1444 gTriggers[a].trigData.writeTo(tw);
1445 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1446 finally
1447 tw.Free();
1448 end;
1450 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1451 end;
1452 end;
1453 end;
1455 begin
1456 if g_Game_IsClient then Exit;
1458 g_Mons_ForEach(monsDieTrig);
1459 end;
1461 function extractWadName(resourceName: string): string;
1462 var
1463 posN: Integer;
1464 begin
1465 posN := Pos(':', resourceName);
1466 if posN > 0 then
1467 Result:= Copy(resourceName, 0, posN-1)
1468 else
1469 Result := '';
1470 end;
1472 procedure addResToExternalResList(res: string);
1473 begin
1474 res := extractWadName(res);
1475 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1476 gExternalResources.Add(res);
1477 end;
1479 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1480 //var
1481 //textures: TTexturesRec1Array;
1482 //textures: TDynField;
1483 //trec: TDynRecord;
1484 //mapHeader: TMapHeaderRec_1;
1485 //i: integer;
1486 //resFile: String = '';
1487 begin
1488 if gExternalResources = nil then
1489 gExternalResources := TStringList.Create;
1491 gExternalResources.Clear;
1493 (*
1495 textures := GetTextures(map);
1496 for i := 0 to High(textures) do
1497 begin
1498 addResToExternalResList(resFile);
1499 end;
1502 textures := map['texture'];
1503 if (textures <> nil) then
1504 begin
1505 for trec in textures do
1506 begin
1507 addResToExternalResList(resFile);
1508 end;
1509 end;
1511 textures := nil;
1512 *)
1514 //mapHeader := GetMapHeader(map);
1516 addResToExternalResList(map.MusicName);
1517 addResToExternalResList(map.SkyName);
1518 end;
1521 procedure mapCreateGrid ();
1522 var
1523 mapX0: Integer = $3fffffff;
1524 mapY0: Integer = $3fffffff;
1525 mapX1: Integer = -$3fffffff;
1526 mapY1: Integer = -$3fffffff;
1528 procedure calcBoundingBox (constref panels: TPanelArray);
1529 var
1530 idx: Integer;
1531 pan: TPanel;
1532 begin
1533 for idx := 0 to High(panels) do
1534 begin
1535 pan := panels[idx];
1536 if not pan.visvalid then continue;
1537 if (pan.Width < 1) or (pan.Height < 1) then continue;
1538 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1539 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1540 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1541 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1542 end;
1543 end;
1545 procedure addPanelsToGrid (constref panels: TPanelArray);
1546 var
1547 idx: Integer;
1548 pan: TPanel;
1549 newtag: Integer;
1550 begin
1551 //tag := panelTypeToTag(tag);
1552 for idx := 0 to High(panels) do
1553 begin
1554 pan := panels[idx];
1555 if not pan.visvalid then continue;
1556 if (pan.proxyId <> -1) then
1557 begin
1558 {$IF DEFINED(D2F_DEBUG)}
1559 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);
1560 {$ENDIF}
1561 continue;
1562 end;
1563 case pan.PanelType of
1564 PANEL_WALL: newtag := GridTagWall;
1565 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1566 PANEL_BACK: newtag := GridTagBack;
1567 PANEL_FORE: newtag := GridTagFore;
1568 PANEL_WATER: newtag := GridTagWater;
1569 PANEL_ACID1: newtag := GridTagAcid1;
1570 PANEL_ACID2: newtag := GridTagAcid2;
1571 PANEL_STEP: newtag := GridTagStep;
1572 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1573 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1574 else continue; // oops
1575 end;
1576 pan.tag := newtag;
1578 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1579 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1580 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1581 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1583 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1584 begin
1585 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1586 end;
1588 {$ENDIF}
1589 end;
1590 end;
1592 begin
1593 mapGrid.Free();
1594 mapGrid := nil;
1596 calcBoundingBox(gWalls);
1597 calcBoundingBox(gRenderBackgrounds);
1598 calcBoundingBox(gRenderForegrounds);
1599 calcBoundingBox(gWater);
1600 calcBoundingBox(gAcid1);
1601 calcBoundingBox(gAcid2);
1602 calcBoundingBox(gSteps);
1603 calcBoundingBox(gLifts);
1604 calcBoundingBox(gBlockMon);
1606 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1608 if (mapX0 > 0) then mapX0 := 0;
1609 if (mapY0 > 0) then mapY0 := 0;
1611 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1612 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1614 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1615 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1617 addPanelsToGrid(gWalls);
1618 addPanelsToGrid(gRenderBackgrounds);
1619 addPanelsToGrid(gRenderForegrounds);
1620 addPanelsToGrid(gWater);
1621 addPanelsToGrid(gAcid1);
1622 addPanelsToGrid(gAcid2);
1623 addPanelsToGrid(gSteps);
1624 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1625 addPanelsToGrid(gBlockMon);
1627 mapGrid.dumpStats();
1629 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1630 end;
1633 function g_Map_Load(Res: String): Boolean;
1634 const
1635 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1636 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1637 type
1638 PTRec = ^TTRec;
1639 TTRec = record
1640 //TexturePanel: Integer;
1641 tnum: Integer;
1642 id: Integer;
1643 trigrec: TDynRecord;
1644 // texture pane;
1645 texPanelIdx: Integer;
1646 texPanel: TDynRecord;
1647 // "action" panel
1648 actPanelIdx: Integer;
1649 actPanel: TDynRecord;
1650 end;
1651 var
1652 WAD, TestWAD: TWADFile;
1653 //mapReader: TDynRecord = nil;
1654 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1655 panels: TDynField = nil; //TPanelsRec1Array;
1656 items: TDynField = nil; //TItemsRec1Array;
1657 monsters: TDynField = nil; //TMonsterRec1Array;
1658 areas: TDynField = nil; //TAreasRec1Array;
1659 triggers: TDynField = nil; //TTriggersRec1Array;
1660 b, c, k: Integer;
1661 PanelID: DWORD;
1662 AddTextures: TAddTextureArray;
1663 TriggersTable: array of TTRec;
1664 FileName, mapResName, TexName, s: AnsiString;
1665 Data: Pointer;
1666 Len: Integer;
1667 ok, isAnim: Boolean;
1668 CurTex, ntn: Integer;
1669 rec, texrec: TDynRecord;
1670 pttit: PTRec;
1671 pannum, trignum, cnt, tgpid: Integer;
1672 stt: UInt64;
1673 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1674 //moveActive: Boolean;
1675 pan: TPanel;
1676 mapOk: Boolean = false;
1677 usedTextures: THashStrInt = nil; // key: mapTextureList
1678 begin
1679 mapGrid.Free();
1680 mapGrid := nil;
1681 TestWAD := nil;
1682 Data := nil;
1684 //gCurrentMap.Free();
1685 //gCurrentMap := nil;
1687 panByGUID := nil;
1689 Result := False;
1690 gMapInfo.Map := Res;
1691 TriggersTable := nil;
1692 //mapReader := nil;
1694 sfsGCDisable(); // temporary disable removing of temporary volumes
1695 try
1696 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1697 if (gCurrentMap = nil) then
1698 begin
1699 FileName := g_ExtractWadName(Res);
1700 e_WriteLog('Loading map WAD: '+FileName, TMsgType.Notify);
1701 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1703 WAD := TWADFile.Create();
1704 if not WAD.ReadFile(FileName) then
1705 begin
1706 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1707 WAD.Free();
1708 Exit;
1709 end;
1711 if gTestMap <> '' then
1712 begin
1713 s := g_ExtractWadName(gTestMap);
1714 TestWAD := TWADFile.Create();
1715 if not TestWAD.ReadFile(s) then
1716 begin
1717 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1718 TestWAD.Free();
1719 TestWAD := nil;
1720 end;
1721 end;
1723 if TestWAD <> nil then
1724 begin
1725 mapResName := g_ExtractFileName(gTestMap);
1726 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1727 begin
1728 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1729 Data := nil;
1730 end else
1731 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1732 TestWAD.Free();
1733 TestWAD := nil;
1734 end;
1736 if Data = nil then
1737 begin
1738 //k8: why loader ignores path here?
1739 mapResName := g_ExtractFileName(Res);
1740 if not WAD.GetMapResource(mapResName, Data, Len) then
1741 begin
1742 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1743 WAD.Free();
1744 Exit;
1745 end;
1746 end;
1748 WAD.Free();
1750 if (Len < 4) then
1751 begin
1752 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1753 FreeMem(Data);
1754 exit;
1755 end;
1757 // Çàãðóçêà êàðòû:
1758 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1759 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1761 stt := getTimeMicro();
1763 try
1764 gCurrentMap := g_Map_ParseMap(Data, Len);
1765 except
1766 gCurrentMap.Free();
1767 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1768 FreeMem(Data);
1769 gCurrentMapFileName := '';
1770 Exit;
1771 end;
1773 FreeMem(Data);
1775 if (gCurrentMap = nil) then
1776 begin
1777 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1778 gCurrentMapFileName := '';
1779 exit;
1780 end;
1781 end
1782 else
1783 begin
1784 stt := getTimeMicro();
1785 end;
1787 //gCurrentMap := mapReader;
1789 generateExternalResourcesList(gCurrentMap);
1790 mapTextureList := gCurrentMap['texture'];
1791 // get all other lists here too
1792 panels := gCurrentMap['panel'];
1793 triggers := gCurrentMap['trigger'];
1794 items := gCurrentMap['item'];
1795 areas := gCurrentMap['area'];
1796 monsters := gCurrentMap['monster'];
1798 // Çàãðóçêà îïèñàíèÿ êàðòû:
1799 e_WriteLog(' Reading map info...', TMsgType.Notify);
1800 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1802 with gMapInfo do
1803 begin
1804 Name := gCurrentMap.MapName;
1805 Description := gCurrentMap.MapDesc;
1806 Author := gCurrentMap.MapAuthor;
1807 MusicName := gCurrentMap.MusicName;
1808 SkyName := gCurrentMap.SkyName;
1809 Height := gCurrentMap.Height;
1810 Width := gCurrentMap.Width;
1811 end;
1813 // Çàãðóçêà òåêñòóð:
1814 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1815 // Äîáàâëåíèå òåêñòóð â Textures[]:
1816 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1817 begin
1818 e_WriteLog(' Loading textures:', TMsgType.Notify);
1819 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1821 // find used textures
1822 usedTextures := THashStrInt.Create();
1823 try
1824 if (panels <> nil) and (panels.count > 0) then
1825 begin
1826 for rec in panels do
1827 begin
1828 texrec := rec.TextureRec;
1829 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1830 end;
1831 end;
1833 cnt := -1;
1834 for rec in mapTextureList do
1835 begin
1836 Inc(cnt);
1837 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1838 begin
1839 rec.tagInt := -1; // just in case
1840 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1841 end
1842 else
1843 begin
1844 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1845 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1846 {$ENDIF}
1847 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1848 if rec.Anim then
1849 begin
1850 // Àíèìèðîâàííàÿ òåêñòóðà
1851 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1852 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]));
1853 end
1854 else
1855 begin
1856 // Îáû÷íàÿ òåêñòóðà
1857 ntn := CreateTexture(rec.Resource, FileName, True);
1858 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1859 end;
1860 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1862 rec.tagInt := ntn; // remember texture number
1863 end;
1864 g_Game_StepLoading();
1865 end;
1866 finally
1867 usedTextures.Free();
1868 end;
1870 // set panel tagInt to texture index
1871 if (panels <> nil) then
1872 begin
1873 for rec in panels do
1874 begin
1875 texrec := rec.TextureRec;
1876 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1877 end;
1878 end;
1879 end;
1882 // Çàãðóçêà òðèããåðîâ
1883 gTriggerClientID := 0;
1884 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1885 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1887 // Çàãðóçêà ïàíåëåé
1888 e_WriteLog(' Loading panels...', TMsgType.Notify);
1889 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1891 // check texture numbers for panels
1892 if (panels <> nil) and (panels.count > 0) then
1893 begin
1894 for rec in panels do
1895 begin
1896 if (rec.tagInt < 0) then
1897 begin
1898 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1899 result := false;
1900 gCurrentMap.Free();
1901 gCurrentMap := nil;
1902 gCurrentMapFileName := '';
1903 exit;
1904 end;
1905 end;
1906 end;
1908 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1909 if (triggers <> nil) and (triggers.count > 0) then
1910 begin
1911 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1912 //SetLength(TriggersTable, triggers.count);
1913 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1915 SetLength(TriggersTable, triggers.count);
1916 trignum := -1;
1917 for rec in triggers do
1918 begin
1919 Inc(trignum);
1920 pttit := @TriggersTable[trignum];
1921 pttit.trigrec := rec;
1922 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1923 pttit.texPanelIdx := -1; // will be fixed later
1924 pttit.texPanel := rec.TexturePanelRec;
1925 // action panel
1926 pttit.actPanelIdx := -1;
1927 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1928 // set flag
1929 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1930 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1931 // update progress
1932 g_Game_StepLoading();
1933 end;
1934 end;
1936 // Ñîçäàåì ïàíåëè
1937 if (panels <> nil) and (panels.count > 0) then
1938 begin
1939 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1940 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1942 pannum := -1;
1943 for rec in panels do
1944 begin
1945 Inc(pannum);
1946 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1947 texrec := nil;
1948 SetLength(AddTextures, 0);
1949 CurTex := -1;
1950 ok := false;
1952 if (mapTextureList <> nil) then
1953 begin
1954 texrec := rec.TextureRec;
1955 ok := (texrec <> nil);
1956 end;
1958 if ok then
1959 begin
1960 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1961 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1962 ok := false;
1963 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1964 begin
1965 if rec.userPanelTrigRef then
1966 begin
1967 // e_LogWritefln('trigref for panel %s', [pannum]);
1968 ok := True;
1969 end;
1970 end;
1971 end;
1973 if ok then
1974 begin
1975 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1976 s := texrec.Resource;
1978 // Ñïåö-òåêñòóðû çàïðåùåíû
1979 if g_Map_IsSpecialTexture(s) then
1980 begin
1981 ok := false
1982 end
1983 else
1984 begin
1985 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1986 ok := g_Texture_NumNameFindStart(s);
1987 end;
1989 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1990 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1991 if ok then
1992 begin
1993 k := NNF_NAME_BEFORE;
1994 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1995 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1996 begin
1997 k := g_Texture_NumNameFindNext(TexName);
1999 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
2000 begin
2001 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
2002 if texrec.Anim then
2003 begin
2004 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
2005 isAnim := True;
2006 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2007 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2008 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2009 if not ok then
2010 begin
2011 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
2012 isAnim := False;
2013 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2014 ok := CreateTexture(TexName, FileName, False) >= 0;
2015 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2016 end;
2017 end
2018 else
2019 begin
2020 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
2021 isAnim := False;
2022 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2023 ok := CreateTexture(TexName, FileName, False) >= 0;
2024 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2025 if not ok then
2026 begin
2027 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
2028 isAnim := True;
2029 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2030 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2031 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2032 end;
2033 end;
2035 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
2036 if ok then
2037 begin
2039 for c := 0 to High(Textures) do
2040 begin
2041 if (Textures[c].TextureName = TexName) then
2042 begin
2043 SetLength(AddTextures, Length(AddTextures)+1);
2044 AddTextures[High(AddTextures)].Texture := c;
2045 AddTextures[High(AddTextures)].Anim := isAnim;
2046 break;
2047 end;
2048 end;
2050 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
2051 begin
2052 SetLength(AddTextures, Length(AddTextures)+1);
2053 AddTextures[High(AddTextures)].Texture := c;
2054 AddTextures[High(AddTextures)].Anim := isAnim;
2055 end;
2056 end;
2057 end
2058 else
2059 begin
2060 if k = NNF_NAME_EQUALS then
2061 begin
2062 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
2063 SetLength(AddTextures, Length(AddTextures)+1);
2064 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
2065 AddTextures[High(AddTextures)].Anim := texrec.Anim;
2066 CurTex := High(AddTextures);
2067 ok := true;
2068 end
2069 else // NNF_NO_NAME
2070 begin
2071 ok := false;
2072 end;
2073 end;
2074 end; // while ok...
2076 ok := true;
2077 end; // if ok - åñòü ñìåæíûå òåêñòóðû
2078 end; // if ok - ññûëàþòñÿ òðèããåðû
2080 if not ok then
2081 begin
2082 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
2083 SetLength(AddTextures, 1);
2084 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
2085 AddTextures[0].Anim := false;
2086 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
2087 CurTex := 0;
2088 end;
2090 //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);
2092 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
2094 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
2095 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
2096 PanelID := CreatePanel(rec, AddTextures, CurTex);
2097 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
2098 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
2100 // setup lifts
2101 moveSpeed := rec.moveSpeed;
2102 //moveStart := rec.moveStart;
2103 //moveEnd := rec.moveEnd;
2104 //moveActive := rec['move_active'].value;
2105 if not moveSpeed.isZero then
2106 begin
2107 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
2108 gMovingWallIds[High(gMovingWallIds)] := PanelID;
2109 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
2110 end;
2112 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2114 g_Game_StepLoading();
2115 end;
2116 end;
2118 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2119 for b := 0 to High(TriggersTable) do
2120 begin
2121 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2122 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2123 end;
2125 // create map grid, init other grids (for monsters, for example)
2126 e_WriteLog('Creating map grid', TMsgType.Notify);
2127 mapCreateGrid();
2129 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2130 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2131 begin
2132 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2133 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2134 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2135 trignum := -1;
2136 for rec in triggers do
2137 begin
2138 Inc(trignum);
2139 tgpid := TriggersTable[trignum].actPanelIdx;
2140 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2141 TriggersTable[trignum].tnum := trignum;
2142 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
2143 end;
2144 end;
2146 //FIXME: use hashtable!
2147 for pan in panByGUID do
2148 begin
2149 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2150 begin
2151 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2152 end;
2153 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2154 begin
2155 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2156 end;
2157 end;
2159 // Çàãðóçêà ïðåäìåòîâ
2160 e_WriteLog(' Loading items...', TMsgType.Notify);
2161 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2163 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2164 if (items <> nil) and not gLoadGameMode then
2165 begin
2166 e_WriteLog(' Spawning items...', TMsgType.Notify);
2167 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2168 for rec in items do CreateItem(rec);
2169 end;
2171 // Çàãðóçêà îáëàñòåé
2172 e_WriteLog(' Loading areas...', TMsgType.Notify);
2173 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2175 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2176 if areas <> nil then
2177 begin
2178 e_WriteLog(' Creating areas...', TMsgType.Notify);
2179 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2180 for rec in areas do CreateArea(rec);
2181 end;
2183 // Çàãðóçêà ìîíñòðîâ
2184 e_WriteLog(' Loading monsters...', TMsgType.Notify);
2185 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2187 gTotalMonsters := 0;
2189 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2190 if (monsters <> nil) and not gLoadGameMode then
2191 begin
2192 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
2193 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2194 for rec in monsters do CreateMonster(rec);
2195 end;
2197 //gCurrentMap := mapReader; // this will be our current map now
2198 gCurrentMapFileName := Res;
2199 //mapReader := nil;
2201 // Çàãðóçêà íåáà
2202 if (gMapInfo.SkyName <> '') then
2203 begin
2204 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
2205 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2206 FileName := g_ExtractWadName(gMapInfo.SkyName);
2208 if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
2210 if gTextureFilter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
2211 try
2212 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2213 if g_Texture_CreateWAD(BackID, s) then
2214 begin
2215 g_Game_SetupScreenSize();
2216 end
2217 else
2218 begin
2219 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2220 end;
2221 finally
2222 TEXTUREFILTER := GL_NEAREST;
2223 end;
2224 end;
2226 // Çàãðóçêà ìóçûêè
2227 ok := False;
2228 if gMapInfo.MusicName <> '' then
2229 begin
2230 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
2231 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2232 FileName := g_ExtractWadName(gMapInfo.MusicName);
2234 if FileName <> '' then
2235 FileName := GameDir+'/wads/'+FileName
2236 else
2237 begin
2238 FileName := g_ExtractWadName(Res);
2239 end;
2241 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2242 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2243 ok := True
2244 else
2245 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2246 end;
2248 // Îñòàëüíûå óñòàíâêè
2249 CreateDoorMap();
2250 CreateLiftMap();
2252 g_Items_Init();
2253 g_Weapon_Init();
2254 g_Monsters_Init();
2256 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2257 if not gLoadGameMode then g_GFX_Init();
2259 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2260 mapTextureList := nil;
2261 panels := nil;
2262 items := nil;
2263 areas := nil;
2264 triggers := nil;
2265 TriggersTable := nil;
2266 AddTextures := nil;
2268 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2269 if ok and (not gLoadGameMode) then
2270 begin
2271 gMusic.SetByName(gMapInfo.MusicName);
2272 gMusic.Play();
2273 end
2274 else
2275 begin
2276 gMusic.SetByName('');
2277 end;
2279 stt := getTimeMicro()-stt;
2280 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2281 mapOk := true;
2282 finally
2283 sfsGCEnable(); // enable releasing unused volumes
2284 //mapReader.Free();
2285 e_ClearInputBuffer(); // why not?
2286 if not mapOk then
2287 begin
2288 gCurrentMap.Free();
2289 gCurrentMap := nil;
2290 gCurrentMapFileName := '';
2291 end;
2292 end;
2294 e_WriteLog('Done loading map.', TMsgType.Notify);
2295 Result := True;
2296 end;
2299 function g_Map_GetMapInfo(Res: String): TMapInfo;
2300 var
2301 WAD: TWADFile;
2302 mapReader: TDynRecord;
2303 //Header: TMapHeaderRec_1;
2304 FileName: String;
2305 Data: Pointer;
2306 Len: Integer;
2307 begin
2308 FillChar(Result, SizeOf(Result), 0);
2309 FileName := g_ExtractWadName(Res);
2311 WAD := TWADFile.Create();
2312 if not WAD.ReadFile(FileName) then
2313 begin
2314 WAD.Free();
2315 Exit;
2316 end;
2318 //k8: it ignores path again
2319 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2320 begin
2321 WAD.Free();
2322 Exit;
2323 end;
2325 WAD.Free();
2327 try
2328 mapReader := g_Map_ParseMap(Data, Len);
2329 except
2330 mapReader := nil;
2331 FreeMem(Data);
2332 exit;
2333 end;
2335 FreeMem(Data);
2337 if (mapReader = nil) then exit;
2339 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2340 begin
2341 Result.Name := mapReader.MapName;
2342 Result.Description := mapReader.MapDesc;
2343 Result.Map := Res;
2344 Result.Author := mapReader.MapAuthor;
2345 Result.Height := mapReader.Height;
2346 Result.Width := mapReader.Width;
2347 end
2348 else
2349 begin
2350 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2351 //ZeroMemory(@Header, SizeOf(Header));
2352 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2353 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2354 Result.Map := Res;
2355 Result.Author := '';
2356 Result.Height := 0;
2357 Result.Width := 0;
2358 end;
2360 mapReader.Free();
2361 end;
2363 function g_Map_GetMapsList(WADName: string): SSArray;
2364 var
2365 WAD: TWADFile;
2366 a: Integer;
2367 ResList: SSArray;
2368 begin
2369 Result := nil;
2370 WAD := TWADFile.Create();
2371 if not WAD.ReadFile(WADName) then
2372 begin
2373 WAD.Free();
2374 Exit;
2375 end;
2376 ResList := WAD.GetMapResources();
2377 if ResList <> nil then
2378 begin
2379 for a := 0 to High(ResList) do
2380 begin
2381 SetLength(Result, Length(Result)+1);
2382 Result[High(Result)] := ResList[a];
2383 end;
2384 end;
2385 WAD.Free();
2386 end;
2388 function g_Map_Exist(Res: string): Boolean;
2389 var
2390 WAD: TWADFile;
2391 FileName, mnn: string;
2392 ResList: SSArray;
2393 a: Integer;
2394 begin
2395 Result := False;
2397 FileName := addWadExtension(g_ExtractWadName(Res));
2399 WAD := TWADFile.Create;
2400 if not WAD.ReadFile(FileName) then
2401 begin
2402 WAD.Free();
2403 Exit;
2404 end;
2406 ResList := WAD.GetMapResources();
2407 WAD.Free();
2409 mnn := g_ExtractFileName(Res);
2410 if ResList <> nil then
2411 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2412 begin
2413 Result := True;
2414 Exit;
2415 end;
2416 end;
2418 procedure g_Map_Free(freeTextures: Boolean=true);
2419 var
2420 a: Integer;
2422 procedure FreePanelArray(var panels: TPanelArray);
2423 var
2424 i: Integer;
2426 begin
2427 if panels <> nil then
2428 begin
2429 for i := 0 to High(panels) do
2430 panels[i].Free();
2431 panels := nil;
2432 end;
2433 end;
2435 begin
2436 g_GFX_Free();
2437 g_Weapon_Free();
2438 g_Items_Free();
2439 g_Triggers_Free();
2440 g_Monsters_Free();
2442 RespawnPoints := nil;
2443 if FlagPoints[FLAG_RED] <> nil then
2444 begin
2445 Dispose(FlagPoints[FLAG_RED]);
2446 FlagPoints[FLAG_RED] := nil;
2447 end;
2448 if FlagPoints[FLAG_BLUE] <> nil then
2449 begin
2450 Dispose(FlagPoints[FLAG_BLUE]);
2451 FlagPoints[FLAG_BLUE] := nil;
2452 end;
2453 //DOMFlagPoints := nil;
2455 //gDOMFlags := nil;
2457 if (Length(gCurrentMapFileName) <> 0) then
2458 begin
2459 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2460 end
2461 else
2462 begin
2463 e_LogWritefln('g_Map_Free: no previous map.', []);
2464 end;
2466 if freeTextures then
2467 begin
2468 e_LogWritefln('g_Map_Free: clearing textures...', []);
2469 if (Textures <> nil) then
2470 begin
2471 for a := 0 to High(Textures) do
2472 begin
2473 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2474 begin
2475 if Textures[a].Anim then
2476 begin
2477 g_Frames_DeleteByID(Textures[a].FramesID)
2478 end
2479 else
2480 begin
2481 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2482 begin
2483 e_DeleteTexture(Textures[a].TextureID);
2484 end;
2485 end;
2486 end;
2487 end;
2488 Textures := nil;
2489 end;
2490 TextNameHash.Free();
2491 TextNameHash := nil;
2492 BadTextNameHash.Free();
2493 BadTextNameHash := nil;
2494 gCurrentMapFileName := '';
2495 gCurrentMap.Free();
2496 gCurrentMap := nil;
2497 end;
2499 panByGUID := nil;
2501 FreePanelArray(gWalls);
2502 FreePanelArray(gRenderBackgrounds);
2503 FreePanelArray(gRenderForegrounds);
2504 FreePanelArray(gWater);
2505 FreePanelArray(gAcid1);
2506 FreePanelArray(gAcid2);
2507 FreePanelArray(gSteps);
2508 FreePanelArray(gLifts);
2509 FreePanelArray(gBlockMon);
2510 gMovingWallIds := nil;
2512 if BackID <> DWORD(-1) then
2513 begin
2514 gBackSize.X := 0;
2515 gBackSize.Y := 0;
2516 e_DeleteTexture(BackID);
2517 BackID := DWORD(-1);
2518 end;
2520 g_Game_StopAllSounds(False);
2521 gMusic.FreeSound();
2522 g_Sound_Delete(gMapInfo.MusicName);
2524 gMapInfo.Name := '';
2525 gMapInfo.Description := '';
2526 gMapInfo.MusicName := '';
2527 gMapInfo.Height := 0;
2528 gMapInfo.Width := 0;
2530 gDoorMap := nil;
2531 gLiftMap := nil;
2532 end;
2534 procedure g_Map_Update();
2535 var
2536 a, d, j: Integer;
2537 m: Word;
2538 s: String;
2540 procedure UpdatePanelArray(var panels: TPanelArray);
2541 var
2542 i: Integer;
2544 begin
2545 for i := 0 to High(panels) do panels[i].Update();
2546 end;
2548 begin
2549 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2551 UpdatePanelArray(gWalls);
2552 UpdatePanelArray(gRenderBackgrounds);
2553 UpdatePanelArray(gRenderForegrounds);
2554 UpdatePanelArray(gWater);
2555 UpdatePanelArray(gAcid1);
2556 UpdatePanelArray(gAcid2);
2557 UpdatePanelArray(gSteps);
2559 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2561 if gGameSettings.GameMode = GM_CTF then
2562 begin
2563 for a := FLAG_RED to FLAG_BLUE do
2564 begin
2565 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2566 begin
2567 with gFlags[a] do
2568 begin
2569 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2571 m := g_Obj_Move(@Obj, True, True);
2573 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2575 // Ñîïðîòèâëåíèå âîçäóõà
2576 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2578 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2579 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2580 begin
2581 g_Map_ResetFlag(a);
2582 gFlags[a].CaptureTime := 0;
2583 if a = FLAG_RED then
2584 s := _lc[I_PLAYER_FLAG_RED]
2585 else
2586 s := _lc[I_PLAYER_FLAG_BLUE];
2587 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2589 if g_Game_IsNet then
2590 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2591 Continue;
2592 end;
2594 if Count > 0 then Count -= 1;
2596 // Èãðîê áåðåò ôëàã
2597 if gPlayers <> nil then
2598 begin
2599 j := Random(Length(gPlayers)) - 1;
2600 for d := 0 to High(gPlayers) do
2601 begin
2602 Inc(j);
2603 if j > High(gPlayers) then j := 0;
2604 if gPlayers[j] <> nil then
2605 begin
2606 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2607 begin
2608 if gPlayers[j].GetFlag(a) then Break;
2609 end;
2610 end;
2611 end;
2612 end;
2613 end;
2614 end;
2615 end;
2616 end;
2617 end;
2620 // old algo
2621 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2623 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2624 var
2625 idx: Integer;
2626 begin
2627 if (panels <> nil) then
2628 begin
2629 // alas, no visible set
2630 for idx := 0 to High(panels) do
2631 begin
2632 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2633 end;
2634 end;
2635 end;
2637 begin
2638 case PanelType of
2639 PANEL_WALL: DrawPanels(gWalls);
2640 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2641 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2642 PANEL_FORE: DrawPanels(gRenderForegrounds);
2643 PANEL_WATER: DrawPanels(gWater);
2644 PANEL_ACID1: DrawPanels(gAcid1);
2645 PANEL_ACID2: DrawPanels(gAcid2);
2646 PANEL_STEP: DrawPanels(gSteps);
2647 end;
2648 end;
2651 // new algo
2652 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2653 (*
2654 function checker (pan: TPanel; tag: Integer): Boolean;
2655 begin
2656 result := false; // don't stop, ever
2657 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2658 gDrawPanelList.insert(pan);
2659 end;
2660 *)
2661 var
2662 pmark: PoolMark;
2663 phit: PPanel;
2664 hitcount: Integer;
2665 begin
2666 dplClear();
2667 //tagmask := panelTypeToTag(PanelType);
2668 //mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2669 pmark := framePool.mark();
2670 hitcount := mapGrid.forEachInAABB(x0, y0, wdt, hgt, GridDrawableMask);
2671 if (hitcount = 0) then exit;
2672 phit := PPanel(framePool.getPtr(pmark));
2673 while (hitcount > 0) do
2674 begin
2675 if ((phit^.tag and GridTagDoor) <> 0) <> phit^.Door then
2676 begin
2677 end
2678 else
2679 begin
2680 gDrawPanelList.insert(phit^);
2681 end;
2682 Inc(phit);
2683 Dec(hitcount);
2684 end;
2685 framePool.release(pmark);
2686 // list will be rendered in `g_game.DrawPlayer()`
2687 end;
2690 procedure g_Map_DrawPanelShadowVolumes (lightX: Integer; lightY: Integer; radius: Integer);
2691 (*
2692 function checker (pan: TPanel; tag: Integer): Boolean;
2693 begin
2694 result := false; // don't stop, ever
2695 pan.DrawShadowVolume(lightX, lightY, radius);
2696 end;
2697 *)
2698 var
2699 pmark: PoolMark;
2700 phit: PPanel;
2701 hitcount: Integer;
2702 begin
2703 //mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2704 pmark := framePool.mark();
2705 hitcount := mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, (GridTagWall or GridTagDoor));
2706 if (hitcount = 0) then exit;
2707 phit := PPanel(framePool.getPtr(pmark));
2708 while (hitcount > 0) do
2709 begin
2710 phit^.DrawShadowVolume(lightX, lightY, radius);
2711 Inc(phit);
2712 Dec(hitcount);
2713 end;
2714 framePool.release(pmark);
2715 end;
2718 procedure g_Map_DrawBack(dx, dy: Integer);
2719 begin
2720 if gDrawBackGround and (BackID <> DWORD(-1)) then
2721 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2722 else
2723 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2724 end;
2726 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2727 PanelType: Word; b1x3: Boolean=false): Boolean;
2728 var
2729 a, h: Integer;
2730 begin
2731 Result := False;
2733 if WordBool(PanelType and PANEL_WALL) then
2734 if gWalls <> nil then
2735 begin
2736 h := High(gWalls);
2738 for a := 0 to h do
2739 if gWalls[a].Enabled and
2740 g_Collide(X, Y, Width, Height,
2741 gWalls[a].X, gWalls[a].Y,
2742 gWalls[a].Width, gWalls[a].Height) then
2743 begin
2744 Result := True;
2745 Exit;
2746 end;
2747 end;
2749 if WordBool(PanelType and PANEL_WATER) then
2750 if gWater <> nil then
2751 begin
2752 h := High(gWater);
2754 for a := 0 to h do
2755 if g_Collide(X, Y, Width, Height,
2756 gWater[a].X, gWater[a].Y,
2757 gWater[a].Width, gWater[a].Height) then
2758 begin
2759 Result := True;
2760 Exit;
2761 end;
2762 end;
2764 if WordBool(PanelType and PANEL_ACID1) then
2765 if gAcid1 <> nil then
2766 begin
2767 h := High(gAcid1);
2769 for a := 0 to h do
2770 if g_Collide(X, Y, Width, Height,
2771 gAcid1[a].X, gAcid1[a].Y,
2772 gAcid1[a].Width, gAcid1[a].Height) then
2773 begin
2774 Result := True;
2775 Exit;
2776 end;
2777 end;
2779 if WordBool(PanelType and PANEL_ACID2) then
2780 if gAcid2 <> nil then
2781 begin
2782 h := High(gAcid2);
2784 for a := 0 to h do
2785 if g_Collide(X, Y, Width, Height,
2786 gAcid2[a].X, gAcid2[a].Y,
2787 gAcid2[a].Width, gAcid2[a].Height) then
2788 begin
2789 Result := True;
2790 Exit;
2791 end;
2792 end;
2794 if WordBool(PanelType and PANEL_STEP) then
2795 if gSteps <> nil then
2796 begin
2797 h := High(gSteps);
2799 for a := 0 to h do
2800 if g_Collide(X, Y, Width, Height,
2801 gSteps[a].X, gSteps[a].Y,
2802 gSteps[a].Width, gSteps[a].Height) then
2803 begin
2804 Result := True;
2805 Exit;
2806 end;
2807 end;
2809 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2810 if gLifts <> nil then
2811 begin
2812 h := High(gLifts);
2814 for a := 0 to h do
2815 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2816 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2817 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2818 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2819 g_Collide(X, Y, Width, Height,
2820 gLifts[a].X, gLifts[a].Y,
2821 gLifts[a].Width, gLifts[a].Height) then
2822 begin
2823 Result := True;
2824 Exit;
2825 end;
2826 end;
2828 if WordBool(PanelType and PANEL_BLOCKMON) then
2829 if gBlockMon <> nil then
2830 begin
2831 h := High(gBlockMon);
2833 for a := 0 to h do
2834 if ( (not b1x3) or
2835 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2836 g_Collide(X, Y, Width, Height,
2837 gBlockMon[a].X, gBlockMon[a].Y,
2838 gBlockMon[a].Width, gBlockMon[a].Height) then
2839 begin
2840 Result := True;
2841 Exit;
2842 end;
2843 end;
2844 end;
2846 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2847 var
2848 texid: DWORD;
2850 function checkPanels (constref panels: TPanelArray): Boolean;
2851 var
2852 a: Integer;
2853 begin
2854 result := false;
2855 if panels = nil then exit;
2856 for a := 0 to High(panels) do
2857 begin
2858 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2859 begin
2860 result := true;
2861 texid := panels[a].GetTextureID();
2862 exit;
2863 end;
2864 end;
2865 end;
2867 begin
2868 texid := LongWord(TEXTURE_NONE);
2869 result := texid;
2870 if not checkPanels(gWater) then
2871 if not checkPanels(gAcid1) then
2872 if not checkPanels(gAcid2) then exit;
2873 result := texid;
2874 end;
2877 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2878 const
2879 SlowMask = GridTagLift or GridTagBlockMon;
2881 function checker (pan: TPanel; tag: Integer): Boolean;
2882 begin
2884 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2885 begin
2886 result := pan.Enabled;
2887 exit;
2888 end;
2891 if ((tag and GridTagLift) <> 0) then
2892 begin
2893 result :=
2894 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2895 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2896 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2897 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2898 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2899 exit;
2900 end;
2902 if ((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 exit;
2906 end;
2908 // other shit
2909 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2910 result := true; // i found her!
2911 end;
2913 var
2914 tagmask: Integer = 0;
2915 pmark: PoolMark;
2916 phit: PPanel;
2917 hitcount: Integer;
2918 pan: TPanel;
2919 begin
2920 result := false;
2921 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2922 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2923 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2924 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2925 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2926 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2927 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2929 if (tagmask = 0) then exit; // just in case
2931 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2932 if gdbg_map_use_accel_coldet then
2933 begin
2934 {if (Width = 1) and (Height = 1) then
2935 begin
2936 if ((tagmask and SlowMask) <> 0) then
2937 begin
2938 // slow
2939 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2940 end
2941 else
2942 begin
2943 // fast
2944 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2945 end;
2946 end
2947 else}
2948 begin
2949 pmark := framePool.mark();
2950 if ((tagmask and SlowMask) <> 0) then
2951 begin
2952 // slow
2953 //result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2954 hitcount := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2955 //if (hitcount = 0) then exit;
2956 phit := PPanel(framePool.getPtr(pmark));
2957 while (hitcount > 0) do
2958 begin
2959 pan := phit^;
2960 if ((pan.tag and GridTagLift) <> 0) then
2961 begin
2962 result :=
2963 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2964 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2965 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2966 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2967 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2968 end
2969 else if ((pan.tag and GridTagBlockMon) <> 0) then
2970 begin
2971 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2972 end
2973 else
2974 begin
2975 // other shit
2976 result := true; // i found her!
2977 end;
2978 if (result) then break;
2979 Inc(phit);
2980 Dec(hitcount);
2981 end;
2982 end
2983 else
2984 begin
2985 // fast
2986 //result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2987 hitcount := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2988 result := (hitcount > 0);
2989 end;
2990 framePool.release(pmark);
2991 end;
2992 end
2993 else
2994 begin
2995 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2996 end;
2997 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2998 end;
3001 // returns `true` if we need to stop
3002 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
3003 begin
3004 result := false;
3005 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
3006 // check priorities
3007 case cctype of
3008 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
3009 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
3010 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
3011 end;
3012 // collision?
3013 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
3014 // yeah
3015 texid := pan.GetTextureID();
3016 // water? water has the highest priority, so stop right here
3017 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
3018 // acid2?
3019 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
3020 // acid1?
3021 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
3022 end;
3024 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
3025 var
3026 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
3027 //texid: DWORD;
3029 (*
3030 // slightly different from the old code, but meh...
3031 function checker (pan: TPanel; tag: Integer): Boolean;
3032 begin
3033 result := false; // don't stop, ever
3034 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
3035 // check priorities
3036 case cctype of
3037 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
3038 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
3039 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
3040 end;
3041 // collision?
3042 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
3043 // yeah
3044 texid := pan.GetTextureID();
3045 // water? water has the highest priority, so stop right here
3046 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
3047 // acid2?
3048 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
3049 // acid1?
3050 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
3051 end;
3052 *)
3053 var
3054 pmark: PoolMark;
3055 phit: PPanel;
3056 hitcount: Integer;
3057 pan: TPanel;
3058 begin
3059 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
3060 if gdbg_map_use_accel_coldet then
3061 begin
3062 {texid}result := LongWord(TEXTURE_NONE);
3064 if (Width = 1) and (Height = 1) then
3065 begin
3066 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
3067 end
3068 else
3069 begin
3070 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
3071 end;
3073 pmark := framePool.mark();
3074 hitcount := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
3075 if (hitcount = 0) then exit;
3076 phit := PPanel(framePool.getPtr(pmark));
3077 while (hitcount > 0) do
3078 begin
3079 pan := phit^;
3080 Inc(phit);
3081 Dec(hitcount);
3082 if (liquidChecker(pan, result, cctype)) then break;
3083 end;
3084 framePool.release(pmark);
3085 //result := texid;
3086 end
3087 else
3088 begin
3089 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
3090 end;
3091 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
3092 end;
3095 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
3096 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
3097 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
3100 procedure g_Map_EnableWallGUID (pguid: Integer);
3101 var
3102 pan: TPanel;
3103 begin
3104 //pan := gWalls[ID];
3105 pan := g_Map_PanelByGUID(pguid);
3106 if (pan = nil) then exit;
3107 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
3109 pan.Enabled := True;
3110 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
3112 mapGrid.proxyEnabled[pan.proxyId] := true;
3113 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
3114 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
3116 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3117 // mark platform as interesting
3118 pan.setDirty();
3120 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
3121 //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);
3122 {$ENDIF}
3123 end;
3126 procedure g_Map_DisableWallGUID (pguid: Integer);
3127 var
3128 pan: TPanel;
3129 begin
3130 //pan := gWalls[ID];
3131 pan := g_Map_PanelByGUID(pguid);
3132 if (pan = nil) then exit;
3133 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
3135 pan.Enabled := False;
3136 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
3138 mapGrid.proxyEnabled[pan.proxyId] := false;
3139 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
3141 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3142 // mark platform as interesting
3143 pan.setDirty();
3145 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
3146 //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);
3147 {$ENDIF}
3148 end;
3151 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
3152 var
3153 tp: TPanel;
3154 begin
3155 tp := g_Map_PanelByGUID(pguid);
3156 if (tp = nil) then exit;
3157 tp.NextTexture(AnimLoop);
3158 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
3159 end;
3162 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
3163 var
3164 pan: TPanel;
3165 begin
3166 //pan := gLifts[ID];
3167 pan := g_Map_PanelByGUID(pguid);
3168 if (pan = nil) then exit;
3169 if not pan.isGLift then exit;
3171 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
3173 with {gLifts[ID]} pan do
3174 begin
3175 LiftType := t;
3177 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
3178 //TODO: make separate lift tags, and change tag here
3180 case LiftType of
3181 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
3182 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
3183 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
3184 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
3185 end;
3187 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3188 // mark platform as interesting
3189 pan.setDirty();
3190 end;
3191 end;
3194 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
3195 var
3196 a: Integer;
3197 PointsArray: Array of TRespawnPoint;
3198 begin
3199 Result := False;
3200 SetLength(PointsArray, 0);
3202 if RespawnPoints = nil then
3203 Exit;
3205 for a := 0 to High(RespawnPoints) do
3206 if RespawnPoints[a].PointType = PointType then
3207 begin
3208 SetLength(PointsArray, Length(PointsArray)+1);
3209 PointsArray[High(PointsArray)] := RespawnPoints[a];
3210 end;
3212 if PointsArray = nil then
3213 Exit;
3215 RespawnPoint := PointsArray[Random(Length(PointsArray))];
3216 Result := True;
3217 end;
3219 function g_Map_GetPointCount(PointType: Byte): Word;
3220 var
3221 a: Integer;
3222 begin
3223 Result := 0;
3225 if RespawnPoints = nil then
3226 Exit;
3228 for a := 0 to High(RespawnPoints) do
3229 if RespawnPoints[a].PointType = PointType then
3230 Result := Result + 1;
3231 end;
3233 function g_Map_HaveFlagPoints(): Boolean;
3234 begin
3235 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3236 end;
3238 procedure g_Map_ResetFlag(Flag: Byte);
3239 begin
3240 with gFlags[Flag] do
3241 begin
3242 Obj.X := -1000;
3243 Obj.Y := -1000;
3244 Obj.Vel.X := 0;
3245 Obj.Vel.Y := 0;
3246 Direction := TDirection.D_LEFT;
3247 State := FLAG_STATE_NONE;
3248 if FlagPoints[Flag] <> nil then
3249 begin
3250 Obj.X := FlagPoints[Flag]^.X;
3251 Obj.Y := FlagPoints[Flag]^.Y;
3252 Direction := FlagPoints[Flag]^.Direction;
3253 State := FLAG_STATE_NORMAL;
3254 end;
3255 Count := -1;
3256 end;
3257 end;
3259 procedure g_Map_DrawFlags();
3260 var
3261 i, dx: Integer;
3262 Mirror: TMirrorType;
3263 begin
3264 if gGameSettings.GameMode <> GM_CTF then
3265 Exit;
3267 for i := FLAG_RED to FLAG_BLUE do
3268 with gFlags[i] do
3269 if State <> FLAG_STATE_CAPTURED then
3270 begin
3271 if State = FLAG_STATE_NONE then
3272 continue;
3274 if Direction = TDirection.D_LEFT then
3275 begin
3276 Mirror := TMirrorType.Horizontal;
3277 dx := -1;
3278 end
3279 else
3280 begin
3281 Mirror := TMirrorType.None;
3282 dx := 1;
3283 end;
3285 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3287 if g_debug_Frames then
3288 begin
3289 e_DrawQuad(Obj.X+Obj.Rect.X,
3290 Obj.Y+Obj.Rect.Y,
3291 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3292 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3293 0, 255, 0);
3294 end;
3295 end;
3296 end;
3299 procedure g_Map_SaveState (st: TStream);
3300 var
3301 str: String;
3303 procedure savePanels ();
3304 var
3305 pan: TPanel;
3306 begin
3307 // Ñîõðàíÿåì ïàíåëè
3308 utils.writeInt(st, LongInt(Length(panByGUID)));
3309 for pan in panByGUID do pan.SaveState(st);
3310 end;
3312 procedure saveFlag (flag: PFlag);
3313 var
3314 b: Byte;
3315 begin
3316 utils.writeSign(st, 'FLAG');
3317 utils.writeInt(st, Byte(0)); // version
3318 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3319 utils.writeInt(st, Byte(flag^.RespawnType));
3320 // Ñîñòîÿíèå ôëàãà
3321 utils.writeInt(st, Byte(flag^.State));
3322 // Íàïðàâëåíèå ôëàãà
3323 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
3324 utils.writeInt(st, Byte(b));
3325 // Îáúåêò ôëàãà
3326 Obj_SaveState(st, @flag^.Obj);
3327 end;
3329 begin
3330 savePanels();
3332 // Ñîõðàíÿåì ìóçûêó
3333 utils.writeSign(st, 'MUSI');
3334 utils.writeInt(st, Byte(0));
3335 // Íàçâàíèå ìóçûêè
3336 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3337 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3338 utils.writeStr(st, str);
3339 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3340 utils.writeInt(st, LongWord(gMusic.GetPosition()));
3341 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3342 utils.writeBool(st, gMusic.SpecPause);
3344 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3345 utils.writeInt(st, LongInt(gTotalMonsters));
3346 ///// /////
3348 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3349 if (gGameSettings.GameMode = GM_CTF) then
3350 begin
3351 // Ôëàã Êðàñíîé êîìàíäû
3352 saveFlag(@gFlags[FLAG_RED]);
3353 // Ôëàã Ñèíåé êîìàíäû
3354 saveFlag(@gFlags[FLAG_BLUE]);
3355 end;
3356 ///// /////
3358 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3359 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3360 begin
3361 // Î÷êè Êðàñíîé êîìàíäû
3362 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
3363 // Î÷êè Ñèíåé êîìàíäû
3364 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
3365 end;
3366 ///// /////
3367 end;
3370 procedure g_Map_LoadState (st: TStream);
3371 var
3372 dw: DWORD;
3373 str: String;
3374 boo: Boolean;
3376 procedure loadPanels ();
3377 var
3378 pan: TPanel;
3379 begin
3380 // Çàãðóæàåì ïàíåëè
3381 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
3382 for pan in panByGUID do
3383 begin
3384 pan.LoadState(st);
3385 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3386 end;
3387 end;
3389 procedure loadFlag (flag: PFlag);
3390 var
3391 b: Byte;
3392 begin
3393 // Ñèãíàòóðà ôëàãà
3394 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
3395 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
3396 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3397 flag^.RespawnType := utils.readByte(st);
3398 // Ñîñòîÿíèå ôëàãà
3399 flag^.State := utils.readByte(st);
3400 // Íàïðàâëåíèå ôëàãà
3401 b := utils.readByte(st);
3402 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
3403 // Îáúåêò ôëàãà
3404 Obj_LoadState(@flag^.Obj, st);
3405 end;
3407 begin
3408 if (st = nil) then exit;
3410 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3411 loadPanels();
3412 ///// /////
3414 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3415 g_GFX_Init();
3416 //mapCreateGrid();
3418 ///// Çàãðóæàåì ìóçûêó: /////
3419 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
3420 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
3421 // Íàçâàíèå ìóçûêè
3422 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3423 str := utils.readStr(st);
3424 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3425 dw := utils.readLongWord(st);
3426 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3427 boo := utils.readBool(st);
3428 // Çàïóñêàåì ýòó ìóçûêó
3429 gMusic.SetByName(str);
3430 gMusic.SpecPause := boo;
3431 gMusic.Play();
3432 gMusic.Pause(true);
3433 gMusic.SetPosition(dw);
3434 ///// /////
3436 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3437 gTotalMonsters := utils.readLongInt(st);
3438 ///// /////
3440 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3441 if (gGameSettings.GameMode = GM_CTF) then
3442 begin
3443 // Ôëàã Êðàñíîé êîìàíäû
3444 loadFlag(@gFlags[FLAG_RED]);
3445 // Ôëàã Ñèíåé êîìàíäû
3446 loadFlag(@gFlags[FLAG_BLUE]);
3447 end;
3448 ///// /////
3450 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3451 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3452 begin
3453 // Î÷êè Êðàñíîé êîìàíäû
3454 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
3455 // Î÷êè Ñèíåé êîìàíäû
3456 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
3457 end;
3458 ///// /////
3459 end;
3462 // trace liquid, stepping by `dx` and `dy`
3463 // return last seen liquid coords, and `false` if we're started outside of the liquid
3464 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3465 const
3466 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3467 begin
3468 topx := x;
3469 topy := y;
3470 // started outside of the liquid?
3471 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3472 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
3473 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3474 result := true;
3475 while true do
3476 begin
3477 Inc(x, dx);
3478 Inc(y, dy);
3479 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3480 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
3481 topx := x;
3482 topy := y;
3483 end;
3484 end;
3487 begin
3488 DynWarningCB := mapWarningCB;
3489 end.