DEADSOFTWARE

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