DEADSOFTWARE

migrating from PanelIDs to panel GUIDs; part one
[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; // 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 procedure g_Map_ProfilersBegin ();
123 procedure g_Map_ProfilersEnd ();
126 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
128 const
129 NNF_NO_NAME = 0;
130 NNF_NAME_BEFORE = 1;
131 NNF_NAME_EQUALS = 2;
132 NNF_NAME_AFTER = 3;
134 function g_Texture_NumNameFindStart(name: String): Boolean;
135 function g_Texture_NumNameFindNext(var newName: String): Byte;
137 const
138 RESPAWNPOINT_PLAYER1 = 1;
139 RESPAWNPOINT_PLAYER2 = 2;
140 RESPAWNPOINT_DM = 3;
141 RESPAWNPOINT_RED = 4;
142 RESPAWNPOINT_BLUE = 5;
144 FLAG_NONE = 0;
145 FLAG_RED = 1;
146 FLAG_BLUE = 2;
147 FLAG_DOM = 3;
149 FLAG_STATE_NONE = 0;
150 FLAG_STATE_NORMAL = 1;
151 FLAG_STATE_DROPPED = 2;
152 FLAG_STATE_CAPTURED = 3;
153 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
154 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
156 FLAG_TIME = 720; // 20 seconds
158 SKY_STRETCH: Single = 1.5;
160 const
161 GridTagInvalid = 0;
163 (* draw order:
164 PANEL_BACK
165 PANEL_STEP
166 PANEL_WALL
167 PANEL_CLOSEDOOR
168 PANEL_ACID1
169 PANEL_ACID2
170 PANEL_WATER
171 PANEL_FORE
172 *)
173 // sorted by draw priority
174 GridTagBack = 1 shl 0; // gRenderBackgrounds
175 GridTagStep = 1 shl 1; // gSteps
176 GridTagWall = 1 shl 2; // gWalls
177 GridTagDoor = 1 shl 3; // gWalls
178 GridTagAcid1 = 1 shl 4; // gAcid1
179 GridTagAcid2 = 1 shl 5; // gAcid2
180 GridTagWater = 1 shl 6; // gWater
181 GridTagFore = 1 shl 7; // gRenderForegrounds
182 // the following are invisible
183 GridTagLift = 1 shl 8; // gLifts
184 GridTagBlockMon = 1 shl 9; // gBlockMon
186 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
189 var
190 gWalls: TPanelArray;
191 gRenderBackgrounds: TPanelArray;
192 gRenderForegrounds: TPanelArray;
193 gWater, gAcid1, gAcid2: TPanelArray;
194 gSteps: TPanelArray;
195 gLifts: TPanelArray;
196 gBlockMon: TPanelArray;
197 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
198 //gDOMFlags: array of TFlag;
199 gMapInfo: TMapInfo;
200 gBackSize: TDFPoint;
201 gDoorMap: array of array of DWORD;
202 gLiftMap: array of array of DWORD;
203 gWADHash: TMD5Digest;
204 BackID: DWORD = DWORD(-1);
205 gExternalResources: TStringList;
206 gMovingWallIds: array of Integer = nil;
208 gdbg_map_use_accel_render: Boolean = true;
209 gdbg_map_use_accel_coldet: Boolean = true;
210 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
211 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
213 gCurrentMap: TDynRecord = nil;
214 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
217 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
220 type
221 TPanelGrid = specialize TBodyGridBase<TPanel>;
223 var
224 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
227 implementation
229 uses
230 e_input, g_main, e_log, SysUtils, g_items, g_gfx, g_console,
231 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
232 g_options, g_triggers, g_player,
233 Math, g_monsters, g_saveload, g_language, g_netmsg,
234 utils, sfs, xstreams, hashtable,
235 ImagingTypes, Imaging, ImagingUtility,
236 ImagingGif, ImagingNetworkGraphics;
238 const
239 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
240 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
241 FLAG_SIGNATURE = $47414C46; // 'FLAG'
245 type
246 THashIntPanel = specialize THashBase<Integer, TPanel>;
248 function hashNewIntPanel (): THashIntPanel;
249 begin
250 result := THashIntPanel.Create(hiihash, hiiequ);
251 end;
255 var
256 //panByGUID: THashIntPanel = nil;
257 panByGUID: array of TPanel = nil;
258 //panLastGUID: Integer = 0;
261 // ////////////////////////////////////////////////////////////////////////// //
262 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
263 begin
264 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
265 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
266 end;
269 // ////////////////////////////////////////////////////////////////////////// //
270 var
271 dfmapdef: TDynMapDef = nil;
273 procedure loadMapDefinition ();
274 var
275 pr: TTextParser = nil;
276 st: TStream = nil;
277 WAD: TWADFile = nil;
278 begin
279 if (dfmapdef <> nil) then exit;
280 try
281 e_LogWritefln('parsing "mapdef.txt"...', []);
282 st := openDiskFileRO(DataDir+'mapdef.txt');
283 except
284 st := nil;
285 e_LogWritefln('local "%smapdef.txt" not found', [DataDir]);
286 end;
287 if (st = nil) then
288 begin
289 WAD := TWADFile.Create();
290 if not WAD.ReadFile(GameWAD) then
291 begin
292 //raise Exception.Create('cannot load "game.wad"');
293 st := nil;
294 end
295 else
296 begin
297 st := WAD.openFileStream('mapdef.txt');
298 end;
299 end;
301 if (st = nil) then
302 begin
303 //raise Exception.Create('cannot open "mapdef.txt"');
304 e_LogWritefln('using default "mapdef.txt"...', [], MSG_WARNING);
305 pr := TStrTextParser.Create(defaultMapDef);
306 end
307 else
308 begin
309 pr := TFileTextParser.Create(st);
310 end;
312 try
313 dfmapdef := TDynMapDef.Create(pr);
314 except on e: Exception do
315 raise Exception.Create(Format('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.line, pr.col, e.message]));
316 end;
318 st.Free();
319 WAD.Free();
320 end;
323 // ////////////////////////////////////////////////////////////////////////// //
324 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
325 var
326 wst: TSFSMemoryChunkStream = nil;
327 pr: TTextParser = nil;
328 begin
329 result := nil;
330 if (dataLen < 4) then exit;
331 loadMapDefinition();
332 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
334 wst := TSFSMemoryChunkStream.Create(data, dataLen);
336 if (PAnsiChar(data)[0] = 'M') and (PAnsiChar(data)[1] = 'A') and (PAnsiChar(data)[2] = 'P') and (PByte(data)[3] = 1) then
337 begin
338 // binary map
339 try
340 result := dfmapdef.parseBinMap(wst);
341 except on e: Exception do
342 begin
343 e_LogWritefln('ERROR: %s', [e.message]);
344 wst.Free();
345 result := nil;
346 exit;
347 end;
348 end;
349 wst.Free();
350 end
351 else
352 begin
353 // text map
354 pr := TFileTextParser.Create(wst);
355 try
356 result := dfmapdef.parseMap(pr);
357 except on e: Exception do
358 begin
359 if (pr <> nil) then e_LogWritefln('ERROR at (%s,%s): %s', [pr.line, pr.col, e.message])
360 else e_LogWritefln('ERROR: %s', [e.message]);
361 pr.Free(); // will free `wst`
362 result := nil;
363 exit;
364 end;
365 end;
366 pr.Free(); // will free `wst`
367 end;
368 end;
371 // ////////////////////////////////////////////////////////////////////////// //
372 var
373 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
374 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
375 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
378 function g_Texture_NumNameFindStart(name: String): Boolean;
379 var
380 i: Integer;
382 begin
383 Result := False;
384 NNF_PureName := '';
385 NNF_FirstNum := -1;
386 NNF_CurrentNum := -1;
388 for i := Length(name) downto 1 do
389 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
390 begin
391 if i = Length(name) then
392 begin // Íåò öèôð â êîíöå ñòðîêè
393 Exit;
394 end
395 else
396 begin
397 NNF_PureName := Copy(name, 1, i);
398 Delete(name, 1, i);
399 Break;
400 end;
401 end;
403 // Íå ïåðåâåñòè â ÷èñëî:
404 if not TryStrToInt(name, NNF_FirstNum) then
405 Exit;
407 NNF_CurrentNum := 0;
409 Result := True;
410 end;
413 function g_Texture_NumNameFindNext(var newName: String): Byte;
414 begin
415 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
416 begin
417 newName := '';
418 Result := NNF_NO_NAME;
419 Exit;
420 end;
422 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
424 if NNF_CurrentNum < NNF_FirstNum then
425 Result := NNF_NAME_BEFORE
426 else
427 if NNF_CurrentNum > NNF_FirstNum then
428 Result := NNF_NAME_AFTER
429 else
430 Result := NNF_NAME_EQUALS;
432 Inc(NNF_CurrentNum);
433 end;
436 // ////////////////////////////////////////////////////////////////////////// //
437 function panelTypeToTag (panelType: Word): Integer;
438 begin
439 case panelType of
440 PANEL_WALL: result := GridTagWall; // gWalls
441 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
442 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
443 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
444 PANEL_WATER: result := GridTagWater; // gWater
445 PANEL_ACID1: result := GridTagAcid1; // gAcid1
446 PANEL_ACID2: result := GridTagAcid2; // gAcid2
447 PANEL_STEP: result := GridTagStep; // gSteps
448 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
449 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
450 else result := GridTagInvalid;
451 end;
452 end;
455 function dplLess (a, b: TObject): Boolean;
456 var
457 pa, pb: TPanel;
458 begin
459 pa := TPanel(a);
460 pb := TPanel(b);
461 if (pa.tag < pb.tag) then begin result := true; exit; end;
462 if (pa.tag > pb.tag) then begin result := false; exit; end;
463 result := (pa.arrIdx < pb.arrIdx);
464 end;
466 procedure dplClear ();
467 begin
468 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
469 end;
472 var
473 Textures: TLevelTextureArray = nil;
474 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
475 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
476 RespawnPoints: array of TRespawnPoint;
477 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
478 //DOMFlagPoints: Array of TFlagPoint;
481 procedure g_Map_ProfilersBegin ();
482 begin
483 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
484 profMapCollision.mainBegin(g_profile_collision);
485 // create sections
486 if g_profile_collision then
487 begin
488 profMapCollision.sectionBegin('*solids');
489 profMapCollision.sectionEnd();
490 profMapCollision.sectionBegin('liquids');
491 profMapCollision.sectionEnd();
492 end;
493 end;
495 procedure g_Map_ProfilersEnd ();
496 begin
497 if (profMapCollision <> nil) then profMapCollision.mainEnd();
498 end;
501 // wall index in `gWalls` or -1
502 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
503 var
504 ex, ey: Integer;
505 begin
506 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
507 if (result <> nil) then
508 begin
509 if (hitx <> nil) then hitx^ := ex;
510 if (hity <> nil) then hity^ := ey;
511 end
512 else
513 begin
514 if (hitx <> nil) then hitx^ := x1;
515 if (hity <> nil) then hity^ := y1;
516 end;
517 end;
519 // returns panel or nil
520 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
521 var
522 ex, ey: Integer;
523 begin
524 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
525 if (result <> nil) then
526 begin
527 if (hitx <> nil) then hitx^ := ex;
528 if (hity <> nil) then hity^ := ey;
529 end
530 else
531 begin
532 if (hitx <> nil) then hitx^ := x1;
533 if (hity <> nil) then hity^ := y1;
534 end;
535 end;
538 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
540 function checker (pan: TPanel; tag: Integer): Boolean;
541 begin
543 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
544 begin
545 result := pan.Enabled; // stop if wall is enabled
546 exit;
547 end;
550 if ((tag and GridTagLift) <> 0) then
551 begin
552 // stop if the lift of the right type
553 result :=
554 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
555 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
556 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
557 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
558 exit;
559 end;
561 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
562 end;
564 var
565 tagmask: Integer = 0;
566 begin
567 result := false;
569 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
570 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
571 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
572 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
573 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
574 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
575 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
577 if (tagmask = 0) then exit;// just in case
578 if ((tagmask and GridTagLift) <> 0) then
579 begin
580 // slow
581 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
582 end
583 else
584 begin
585 // fast
586 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
587 end;
588 end;
591 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
592 begin
593 result := nil;
594 if (tagmask = 0) then exit;
595 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
596 end;
599 function g_Map_IsSpecialTexture(Texture: String): Boolean;
600 begin
601 Result := (Texture = TEXTURE_NAME_WATER) or
602 (Texture = TEXTURE_NAME_ACID1) or
603 (Texture = TEXTURE_NAME_ACID2);
604 end;
606 procedure CreateDoorMap();
607 var
608 PanelArray: Array of record
609 X, Y: Integer;
610 Width, Height: Word;
611 Active: Boolean;
612 PanelID: DWORD;
613 end;
614 a, b, c, m, i, len: Integer;
615 ok: Boolean;
616 begin
617 if gWalls = nil then
618 Exit;
620 i := 0;
621 len := 128;
622 SetLength(PanelArray, len);
624 for a := 0 to High(gWalls) do
625 if gWalls[a].Door then
626 begin
627 PanelArray[i].X := gWalls[a].X;
628 PanelArray[i].Y := gWalls[a].Y;
629 PanelArray[i].Width := gWalls[a].Width;
630 PanelArray[i].Height := gWalls[a].Height;
631 PanelArray[i].Active := True;
632 PanelArray[i].PanelID := a;
634 i := i + 1;
635 if i = len then
636 begin
637 len := len + 128;
638 SetLength(PanelArray, len);
639 end;
640 end;
642 // Íåò äâåðåé:
643 if i = 0 then
644 begin
645 PanelArray := nil;
646 Exit;
647 end;
649 SetLength(gDoorMap, 0);
651 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
653 for a := 0 to i-1 do
654 if PanelArray[a].Active then
655 begin
656 PanelArray[a].Active := False;
657 m := Length(gDoorMap);
658 SetLength(gDoorMap, m+1);
659 SetLength(gDoorMap[m], 1);
660 gDoorMap[m, 0] := PanelArray[a].PanelID;
661 ok := True;
663 while ok do
664 begin
665 ok := False;
667 for b := 0 to i-1 do
668 if PanelArray[b].Active then
669 for c := 0 to High(gDoorMap[m]) do
670 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
671 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
672 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
673 PanelArray[b].Width, PanelArray[b].Height,
674 gWalls[gDoorMap[m, c]].X,
675 gWalls[gDoorMap[m, c]].Y,
676 gWalls[gDoorMap[m, c]].Width,
677 gWalls[gDoorMap[m, c]].Height) then
678 begin
679 PanelArray[b].Active := False;
680 SetLength(gDoorMap[m],
681 Length(gDoorMap[m])+1);
682 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
683 ok := True;
684 Break;
685 end;
686 end;
688 g_Game_StepLoading();
689 end;
691 PanelArray := nil;
692 end;
694 procedure CreateLiftMap();
695 var
696 PanelArray: Array of record
697 X, Y: Integer;
698 Width, Height: Word;
699 Active: Boolean;
700 end;
701 a, b, c, len, i, j: Integer;
702 ok: Boolean;
703 begin
704 if gLifts = nil then
705 Exit;
707 len := Length(gLifts);
708 SetLength(PanelArray, len);
710 for a := 0 to len-1 do
711 begin
712 PanelArray[a].X := gLifts[a].X;
713 PanelArray[a].Y := gLifts[a].Y;
714 PanelArray[a].Width := gLifts[a].Width;
715 PanelArray[a].Height := gLifts[a].Height;
716 PanelArray[a].Active := True;
717 end;
719 SetLength(gLiftMap, len);
720 i := 0;
722 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
724 for a := 0 to len-1 do
725 if PanelArray[a].Active then
726 begin
727 PanelArray[a].Active := False;
728 SetLength(gLiftMap[i], 32);
729 j := 0;
730 gLiftMap[i, j] := a;
731 ok := True;
733 while ok do
734 begin
735 ok := False;
736 for b := 0 to len-1 do
737 if PanelArray[b].Active then
738 for c := 0 to j do
739 if g_CollideAround(PanelArray[b].X,
740 PanelArray[b].Y,
741 PanelArray[b].Width,
742 PanelArray[b].Height,
743 PanelArray[gLiftMap[i, c]].X,
744 PanelArray[gLiftMap[i, c]].Y,
745 PanelArray[gLiftMap[i, c]].Width,
746 PanelArray[gLiftMap[i, c]].Height) then
747 begin
748 PanelArray[b].Active := False;
749 j := j+1;
750 if j > High(gLiftMap[i]) then
751 SetLength(gLiftMap[i],
752 Length(gLiftMap[i])+32);
754 gLiftMap[i, j] := b;
755 ok := True;
757 Break;
758 end;
759 end;
761 SetLength(gLiftMap[i], j+1);
762 i := i+1;
764 g_Game_StepLoading();
765 end;
767 SetLength(gLiftMap, i);
769 PanelArray := nil;
770 end;
772 function CreatePanel(PanelRec: TDynRecord; AddTextures: TAddTextureArray;
773 CurTex: Integer; sav: Boolean): Integer;
774 var
775 len: Integer;
776 panels: ^TPanelArray;
777 pan: TPanel;
778 pguid: Integer;
779 begin
780 Result := -1;
782 case PanelRec.PanelType of
783 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
784 PANEL_BACK: panels := @gRenderBackgrounds;
785 PANEL_FORE: panels := @gRenderForegrounds;
786 PANEL_WATER: panels := @gWater;
787 PANEL_ACID1: panels := @gAcid1;
788 PANEL_ACID2: panels := @gAcid2;
789 PANEL_STEP: panels := @gSteps;
790 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
791 PANEL_BLOCKMON: panels := @gBlockMon;
792 else exit;
793 end;
795 len := Length(panels^);
796 SetLength(panels^, len+1);
798 //Inc(panLastGUID);
799 pguid := Length(panByGUID);
800 SetLength(panByGUID, pguid+1); //FIXME!
801 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
802 panels^[len] := pan;
803 pan.arrIdx := len;
804 pan.proxyId := -1;
805 pan.tag := panelTypeToTag(PanelRec.PanelType);
806 if sav then pan.SaveIt := True;
808 PanelRec.user['panel_guid'] := pan.guid;
809 //panByGUID.put(pan.guid, pan);
811 result := len;
812 end;
815 function CreateNullTexture(RecName: String): Integer;
816 begin
817 RecName := toLowerCase1251(RecName);
818 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
819 if TextNameHash.get(RecName, result) then exit; // i found her!
821 SetLength(Textures, Length(Textures)+1);
822 result := High(Textures);
824 with Textures[High(Textures)] do
825 begin
826 TextureName := RecName;
827 Width := 1;
828 Height := 1;
829 Anim := False;
830 TextureID := LongWord(TEXTURE_NONE);
831 end;
833 TextNameHash.put(RecName, result);
834 end;
837 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
838 var
839 WAD: TWADFile;
840 TextureData: Pointer;
841 WADName: String;
842 a, ResLength: Integer;
843 begin
844 RecName := toLowerCase1251(RecName);
845 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
846 if TextNameHash.get(RecName, result) then
847 begin
848 // i found her!
849 //e_LogWritefln('texture ''%s'' already loaded', [RecName]);
850 exit;
851 end;
853 Result := -1;
855 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
858 if Textures <> nil then
859 begin
860 for a := 0 to High(Textures) do
861 begin
862 if (Textures[a].TextureName = RecName) then
863 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
864 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
865 Result := a;
866 Exit;
867 end;
868 end;
869 end;
872 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
873 if (RecName = TEXTURE_NAME_WATER) or
874 (RecName = TEXTURE_NAME_ACID1) or
875 (RecName = TEXTURE_NAME_ACID2) then
876 begin
877 SetLength(Textures, Length(Textures)+1);
879 with Textures[High(Textures)] do
880 begin
881 TextureName := RecName;
882 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
883 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
884 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
886 Anim := False;
887 end;
889 result := High(Textures);
890 TextNameHash.put(RecName, result);
891 Exit;
892 end;
894 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
895 WADName := g_ExtractWadName(RecName);
897 WAD := TWADFile.Create();
899 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
901 WAD.ReadFile(WADName);
903 //txname := RecName;
905 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
906 begin
907 FreeMem(TextureData);
908 RecName := 'COMMON\ALIEN';
909 end;
912 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
913 begin
914 SetLength(Textures, Length(Textures)+1);
915 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
916 begin
917 SetLength(Textures, Length(Textures)-1);
918 Exit;
919 end;
920 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
921 FreeMem(TextureData);
922 Textures[High(Textures)].TextureName := RecName;
923 Textures[High(Textures)].Anim := False;
925 result := High(Textures);
926 TextNameHash.put(RecName, result);
927 end
928 else // Íåò òàêîãî ðåóñðñà â WAD'å
929 begin
930 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
931 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
932 if log and (not BadTextNameHash.get(RecName, a)) then
933 begin
934 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
935 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
936 end;
937 BadTextNameHash.put(RecName, -1);
938 end;
940 WAD.Free();
941 end;
944 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
945 var
946 WAD: TWADFile;
947 TextureWAD: PChar = nil;
948 TextData: Pointer = nil;
949 TextureData: Pointer = nil;
950 cfg: TConfig = nil;
951 WADName: String;
952 ResLength: Integer;
953 TextureResource: String;
954 _width, _height, _framecount, _speed: Integer;
955 _backanimation: Boolean;
956 //imgfmt: string;
957 ia: TDynImageDataArray = nil;
958 f, c, frdelay, frloop: Integer;
959 begin
960 RecName := toLowerCase1251(RecName);
961 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
962 if TextNameHash.get(RecName, result) then
963 begin
964 // i found her!
965 //e_LogWritefln('animated texture ''%s'' already loaded', [RecName]);
966 exit;
967 end;
969 result := -1;
971 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
973 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
974 if BadTextNameHash.get(RecName, f) then
975 begin
976 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
977 exit;
978 end;
980 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
981 WADName := g_ExtractWadName(RecName);
983 WAD := TWADFile.Create();
984 try
985 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
987 WAD.ReadFile(WADName);
989 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
990 begin
991 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
992 if log and (not BadTextNameHash.get(RecName, f)) then
993 begin
994 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
995 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
996 end;
997 BadTextNameHash.put(RecName, -1);
998 exit;
999 end;
1001 {TEST
1002 if WADName = Map then
1003 begin
1004 //FreeMem(TextureWAD);
1005 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1006 end;
1009 WAD.FreeWAD();
1011 if ResLength < 6 then
1012 begin
1013 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
1014 BadTextNameHash.put(RecName, -1);
1015 exit;
1016 end;
1018 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1019 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1020 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1021 begin
1022 // íåò, ýòî ñóïåðìåí!
1023 if not WAD.ReadMemory(TextureWAD, ResLength) then
1024 begin
1025 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
1026 BadTextNameHash.put(RecName, -1);
1027 exit;
1028 end;
1030 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1031 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1032 begin
1033 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
1034 BadTextNameHash.put(RecName, -1);
1035 exit;
1036 end;
1038 cfg := TConfig.CreateMem(TextData, ResLength);
1040 TextureResource := cfg.ReadStr('', 'resource', '');
1041 if TextureResource = '' then
1042 begin
1043 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
1044 BadTextNameHash.put(RecName, -1);
1045 exit;
1046 end;
1048 _width := cfg.ReadInt('', 'framewidth', 0);
1049 _height := cfg.ReadInt('', 'frameheight', 0);
1050 _framecount := cfg.ReadInt('', 'framecount', 0);
1051 _speed := cfg.ReadInt('', 'waitcount', 0);
1052 _backanimation := cfg.ReadBool('', 'backanimation', False);
1054 cfg.Free();
1055 cfg := nil;
1057 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1058 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1059 begin
1060 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
1061 BadTextNameHash.put(RecName, -1);
1062 exit;
1063 end;
1065 WAD.Free();
1066 WAD := nil;
1068 SetLength(Textures, Length(Textures)+1);
1069 with Textures[High(Textures)] do
1070 begin
1071 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1072 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1073 begin
1074 TextureName := RecName;
1075 Width := _width;
1076 Height := _height;
1077 Anim := True;
1078 FramesCount := _framecount;
1079 Speed := _speed;
1080 result := High(Textures);
1081 TextNameHash.put(RecName, result);
1082 end
1083 else
1084 begin
1085 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1086 if log and (not BadTextNameHash.get(RecName, f)) then
1087 begin
1088 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1089 end;
1090 BadTextNameHash.put(RecName, -1);
1091 end;
1092 end;
1093 end
1094 else
1095 begin
1096 // try animated image
1098 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1099 if length(imgfmt) = 0 then
1100 begin
1101 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1102 exit;
1103 end;
1105 GlobalMetadata.ClearMetaItems();
1106 GlobalMetadata.ClearMetaItemsForSaving();
1107 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1108 begin
1109 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
1110 BadTextNameHash.put(RecName, -1);
1111 exit;
1112 end;
1113 if length(ia) = 0 then
1114 begin
1115 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
1116 BadTextNameHash.put(RecName, -1);
1117 exit;
1118 end;
1120 WAD.Free();
1121 WAD := nil;
1123 _width := ia[0].width;
1124 _height := ia[0].height;
1125 _framecount := length(ia);
1126 _speed := 1;
1127 _backanimation := false;
1128 frdelay := -1;
1129 frloop := -666;
1130 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1131 begin
1132 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1133 try
1134 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1135 frdelay := f;
1136 if f < 0 then f := 0;
1137 // rounding ;-)
1138 c := f mod 28;
1139 if c < 13 then c := 0 else c := 1;
1140 f := (f div 28)+c;
1141 if f < 1 then f := 1 else if f > 255 then f := 255;
1142 _speed := f;
1143 except
1144 end;
1145 end;
1146 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1147 begin
1148 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1149 try
1150 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1151 frloop := f;
1152 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1153 except
1154 end;
1155 end;
1156 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1157 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1158 f := ord(_backanimation);
1159 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);
1161 SetLength(Textures, Length(Textures)+1);
1162 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1163 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1164 begin
1165 Textures[High(Textures)].TextureName := RecName;
1166 Textures[High(Textures)].Width := _width;
1167 Textures[High(Textures)].Height := _height;
1168 Textures[High(Textures)].Anim := True;
1169 Textures[High(Textures)].FramesCount := length(ia);
1170 Textures[High(Textures)].Speed := _speed;
1171 result := High(Textures);
1172 TextNameHash.put(RecName, result);
1173 //writeln(' CREATED!');
1174 end
1175 else
1176 begin
1177 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1178 if log and (not BadTextNameHash.get(RecName, f)) then
1179 begin
1180 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
1181 end;
1182 BadTextNameHash.put(RecName, -1);
1183 end;
1184 end;
1185 finally
1186 for f := 0 to High(ia) do FreeImage(ia[f]);
1187 WAD.Free();
1188 cfg.Free();
1189 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1190 if (TextData <> nil) then FreeMem(TextData);
1191 if (TextureData <> nil) then FreeMem(TextureData);
1192 end;
1193 end;
1195 procedure CreateItem(Item: TDynRecord);
1196 begin
1197 if g_Game_IsClient then Exit;
1199 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1200 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1201 Exit;
1203 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1204 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1205 end;
1207 procedure CreateArea(Area: TDynRecord);
1208 var
1209 a: Integer;
1210 id: DWORD = 0;
1211 begin
1212 case Area.AreaType of
1213 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1214 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1215 begin
1216 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1217 with RespawnPoints[High(RespawnPoints)] do
1218 begin
1219 X := Area.X;
1220 Y := Area.Y;
1221 Direction := TDirection(Area.Direction);
1223 case Area.AreaType of
1224 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1225 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1226 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1227 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1228 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1229 end;
1230 end;
1231 end;
1233 AREA_REDFLAG, AREA_BLUEFLAG:
1234 begin
1235 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1237 if FlagPoints[a] <> nil then Exit;
1239 New(FlagPoints[a]);
1241 with FlagPoints[a]^ do
1242 begin
1243 X := Area.X-FLAGRECT.X;
1244 Y := Area.Y-FLAGRECT.Y;
1245 Direction := TDirection(Area.Direction);
1246 end;
1248 with gFlags[a] do
1249 begin
1250 case a of
1251 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1252 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1253 end;
1255 Animation := TAnimation.Create(id, True, 8);
1256 Obj.Rect := FLAGRECT;
1258 g_Map_ResetFlag(a);
1259 end;
1260 end;
1262 AREA_DOMFLAG:
1263 begin
1264 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1265 with DOMFlagPoints[High(DOMFlagPoints)] do
1266 begin
1267 X := Area.X;
1268 Y := Area.Y;
1269 Direction := TDirection(Area.Direction);
1270 end;
1272 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1273 end;
1274 end;
1275 end;
1277 procedure CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer; fTexturePanel1Type, fTexturePanel2Type: Word);
1278 var
1279 _trigger: TTrigger;
1280 begin
1281 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1283 with _trigger do
1284 begin
1285 mapId := Trigger.id;
1286 mapIndex := amapIdx;
1287 X := Trigger.X;
1288 Y := Trigger.Y;
1289 Width := Trigger.Width;
1290 Height := Trigger.Height;
1291 Enabled := Trigger.Enabled;
1292 //TexturePanel := Trigger.TexturePanel;
1293 TexturePanelGUID := atpanid;
1294 TexturePanelType := fTexturePanel1Type;
1295 ShotPanelType := fTexturePanel2Type;
1296 TriggerType := Trigger.TriggerType;
1297 ActivateType := Trigger.ActivateType;
1298 Keys := Trigger.Keys;
1299 trigPanelGUID := atrigpanid;
1300 //trigShotPanelId := ashotpanid;
1301 //Data.Default := Trigger.DATA;
1302 if (Trigger.trigRec = nil) then
1303 begin
1304 trigData := nil;
1305 if (TriggerType <> TRIGGER_SECRET) then
1306 begin
1307 e_LogWritefln('trigger of type %s has no triggerdata; wtf?!', [TriggerType], MSG_WARNING);
1308 end;
1309 end
1310 else
1311 begin
1312 trigData := Trigger.trigRec.clone();
1313 end;
1314 end;
1316 g_Triggers_Create(_trigger);
1317 end;
1319 procedure CreateMonster(monster: TDynRecord);
1320 var
1321 a: Integer;
1322 mon: TMonster;
1323 begin
1324 if g_Game_IsClient then Exit;
1326 if (gGameSettings.GameType = GT_SINGLE)
1327 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1328 begin
1329 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1331 if gTriggers <> nil then
1332 begin
1333 for a := 0 to High(gTriggers) do
1334 begin
1335 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1336 begin
1337 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1338 if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1339 end;
1340 end;
1341 end;
1343 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1344 end;
1345 end;
1347 procedure g_Map_ReAdd_DieTriggers();
1349 function monsDieTrig (mon: TMonster): Boolean;
1350 var
1351 a: Integer;
1352 //tw: TStrTextWriter;
1353 begin
1354 result := false; // don't stop
1355 mon.ClearTriggers();
1356 for a := 0 to High(gTriggers) do
1357 begin
1358 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1359 begin
1360 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1362 tw := TStrTextWriter.Create();
1363 try
1364 gTriggers[a].trigData.writeTo(tw);
1365 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1366 finally
1367 tw.Free();
1368 end;
1370 if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1371 end;
1372 end;
1373 end;
1375 begin
1376 if g_Game_IsClient then Exit;
1378 g_Mons_ForEach(monsDieTrig);
1379 end;
1381 function extractWadName(resourceName: string): string;
1382 var
1383 posN: Integer;
1384 begin
1385 posN := Pos(':', resourceName);
1386 if posN > 0 then
1387 Result:= Copy(resourceName, 0, posN-1)
1388 else
1389 Result := '';
1390 end;
1392 procedure addResToExternalResList(res: string);
1393 begin
1394 res := extractWadName(res);
1395 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1396 gExternalResources.Add(res);
1397 end;
1399 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1400 //var
1401 //textures: TTexturesRec1Array;
1402 //textures: TDynField;
1403 //trec: TDynRecord;
1404 //mapHeader: TMapHeaderRec_1;
1405 //i: integer;
1406 //resFile: String = '';
1407 begin
1408 if gExternalResources = nil then
1409 gExternalResources := TStringList.Create;
1411 gExternalResources.Clear;
1413 (*
1415 textures := GetTextures(map);
1416 for i := 0 to High(textures) do
1417 begin
1418 addResToExternalResList(resFile);
1419 end;
1422 textures := map['texture'];
1423 if (textures <> nil) then
1424 begin
1425 for trec in textures do
1426 begin
1427 addResToExternalResList(resFile);
1428 end;
1429 end;
1431 textures := nil;
1432 *)
1434 //mapHeader := GetMapHeader(map);
1436 addResToExternalResList(map.MusicName);
1437 addResToExternalResList(map.SkyName);
1438 end;
1441 procedure mapCreateGrid ();
1442 var
1443 mapX0: Integer = $3fffffff;
1444 mapY0: Integer = $3fffffff;
1445 mapX1: Integer = -$3fffffff;
1446 mapY1: Integer = -$3fffffff;
1448 procedure calcBoundingBox (constref panels: TPanelArray);
1449 var
1450 idx: Integer;
1451 pan: TPanel;
1452 begin
1453 for idx := 0 to High(panels) do
1454 begin
1455 pan := panels[idx];
1456 if not pan.visvalid then continue;
1457 if (pan.Width < 1) or (pan.Height < 1) then continue;
1458 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1459 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1460 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1461 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1462 end;
1463 end;
1465 procedure addPanelsToGrid (constref panels: TPanelArray);
1466 var
1467 idx: Integer;
1468 pan: TPanel;
1469 newtag: Integer;
1470 begin
1471 //tag := panelTypeToTag(tag);
1472 for idx := 0 to High(panels) do
1473 begin
1474 pan := panels[idx];
1475 if not pan.visvalid then continue;
1476 if (pan.proxyId <> -1) then
1477 begin
1478 {$IF DEFINED(D2F_DEBUG)}
1479 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);
1480 {$ENDIF}
1481 continue;
1482 end;
1483 case pan.PanelType of
1484 PANEL_WALL: newtag := GridTagWall;
1485 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1486 PANEL_BACK: newtag := GridTagBack;
1487 PANEL_FORE: newtag := GridTagFore;
1488 PANEL_WATER: newtag := GridTagWater;
1489 PANEL_ACID1: newtag := GridTagAcid1;
1490 PANEL_ACID2: newtag := GridTagAcid2;
1491 PANEL_STEP: newtag := GridTagStep;
1492 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1493 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1494 else continue; // oops
1495 end;
1496 pan.tag := newtag;
1498 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1499 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1500 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1501 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1503 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1504 begin
1505 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1506 end;
1508 {$ENDIF}
1509 end;
1510 end;
1512 begin
1513 mapGrid.Free();
1514 mapGrid := nil;
1516 calcBoundingBox(gWalls);
1517 calcBoundingBox(gRenderBackgrounds);
1518 calcBoundingBox(gRenderForegrounds);
1519 calcBoundingBox(gWater);
1520 calcBoundingBox(gAcid1);
1521 calcBoundingBox(gAcid2);
1522 calcBoundingBox(gSteps);
1523 calcBoundingBox(gLifts);
1524 calcBoundingBox(gBlockMon);
1526 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1528 if (mapX0 > 0) then mapX0 := 0;
1529 if (mapY0 > 0) then mapY0 := 0;
1531 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1532 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1534 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1535 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1537 addPanelsToGrid(gWalls);
1538 addPanelsToGrid(gRenderBackgrounds);
1539 addPanelsToGrid(gRenderForegrounds);
1540 addPanelsToGrid(gWater);
1541 addPanelsToGrid(gAcid1);
1542 addPanelsToGrid(gAcid2);
1543 addPanelsToGrid(gSteps);
1544 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1545 addPanelsToGrid(gBlockMon);
1547 mapGrid.dumpStats();
1549 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1550 end;
1553 function g_Map_Load(Res: String): Boolean;
1554 const
1555 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1556 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1557 type
1558 PTRec = ^TTRec;
1559 TTRec = record
1560 //TexturePanel: Integer;
1561 texPanIdx: Integer;
1562 LiftPanelIdx: Integer;
1563 DoorPanelIdx: Integer;
1564 ShotPanelIdx: Integer;
1565 MPlatPanelIdx: Integer;
1566 trigrec: TDynRecord;
1567 texPan: TDynRecord;
1568 liftPan: TDynRecord;
1569 doorPan: TDynRecord;
1570 shotPan: TDynRecord;
1571 mplatPan: TDynRecord;
1572 end;
1573 var
1574 WAD: TWADFile;
1575 mapReader: TDynRecord = nil;
1576 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1577 panels: TDynField = nil; //TPanelsRec1Array;
1578 items: TDynField = nil; //TItemsRec1Array;
1579 monsters: TDynField = nil; //TMonsterRec1Array;
1580 areas: TDynField = nil; //TAreasRec1Array;
1581 triggers: TDynField = nil; //TTriggersRec1Array;
1582 b, c, k: Integer;
1583 PanelID: DWORD;
1584 AddTextures: TAddTextureArray;
1585 TriggersTable: array of TTRec;
1586 FileName, mapResName, s, TexName: String;
1587 Data: Pointer;
1588 Len: Integer;
1589 ok, isAnim, trigRef: Boolean;
1590 CurTex, ntn: Integer;
1591 rec, texrec: TDynRecord;
1592 pttit: PTRec;
1593 pannum, trignum, cnt, tgpid: Integer;
1594 stt: UInt64;
1595 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1596 //moveActive: Boolean;
1597 begin
1598 mapGrid.Free();
1599 mapGrid := nil;
1601 gCurrentMap.Free();
1602 gCurrentMap := nil;
1604 //panByGUID.Free();
1605 //panByGUID := hashNewIntPanel();
1606 //panLastGUID := 0;
1607 panByGUID := nil;
1609 Result := False;
1610 gMapInfo.Map := Res;
1611 TriggersTable := nil;
1612 mapReader := nil;
1614 sfsGCDisable(); // temporary disable removing of temporary volumes
1615 try
1616 // Çàãðóçêà WAD:
1617 FileName := g_ExtractWadName(Res);
1618 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1619 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1621 WAD := TWADFile.Create();
1622 if not WAD.ReadFile(FileName) then
1623 begin
1624 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1625 WAD.Free();
1626 Exit;
1627 end;
1629 //k8: why loader ignores path here?
1630 mapResName := g_ExtractFileName(Res);
1631 if not WAD.GetMapResource(mapResName, Data, Len) then
1632 begin
1633 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1634 WAD.Free();
1635 Exit;
1636 end;
1638 WAD.Free();
1640 if (Len < 4) then
1641 begin
1642 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1643 FreeMem(Data);
1644 exit;
1645 end;
1647 // Çàãðóçêà êàðòû:
1648 e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
1649 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1651 stt := curTimeMicro();
1653 try
1654 mapReader := g_Map_ParseMap(Data, Len);
1655 except
1656 mapReader.Free();
1657 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1658 FreeMem(Data);
1659 Exit;
1660 end;
1662 FreeMem(Data);
1664 generateExternalResourcesList(mapReader);
1665 mapTextureList := mapReader['texture'];
1666 // get all other lists here too
1667 panels := mapReader['panel'];
1668 triggers := mapReader['trigger'];
1669 items := mapReader['item'];
1670 areas := mapReader['area'];
1671 monsters := mapReader['monster'];
1673 // Çàãðóçêà îïèñàíèÿ êàðòû:
1674 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1675 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1677 with gMapInfo do
1678 begin
1679 Name := mapReader.MapName;
1680 Description := mapReader.MapDesc;
1681 Author := mapReader.MapAuthor;
1682 MusicName := mapReader.MusicName;
1683 SkyName := mapReader.SkyName;
1684 Height := mapReader.Height;
1685 Width := mapReader.Width;
1686 end;
1688 // Çàãðóçêà òåêñòóð:
1689 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1690 // Äîáàâëåíèå òåêñòóð â Textures[]:
1691 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1692 begin
1693 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1694 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1696 cnt := -1;
1697 for rec in mapTextureList do
1698 begin
1699 Inc(cnt);
1700 s := rec.Resource;
1701 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1702 e_WriteLog(Format(' Loading texture #%d: %s', [cnt, s]), MSG_NOTIFY);
1703 {$ENDIF}
1704 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1705 if rec.Anim then
1706 begin
1707 // Àíèìèðîâàííàÿ òåêñòóðà
1708 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1709 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1710 end
1711 else
1712 begin
1713 // Îáû÷íàÿ òåêñòóðà
1714 ntn := CreateTexture(rec.Resource, FileName, True);
1715 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1716 end;
1717 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1719 rec.tagInt := ntn; // remember texture number
1720 g_Game_StepLoading();
1721 end;
1723 // set panel tagInt to texture index
1724 if (panels <> nil) then
1725 begin
1726 for rec in panels do
1727 begin
1728 texrec := rec.TextureRec;
1729 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1730 end;
1731 end;
1732 end;
1734 // Çàãðóçêà òðèããåðîâ
1735 gTriggerClientID := 0;
1736 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1737 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1739 // Çàãðóçêà ïàíåëåé
1740 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1741 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1743 // check texture numbers for panels
1744 if (panels <> nil) and (panels.count > 0) then
1745 begin
1746 for rec in panels do
1747 begin
1748 if (rec.tagInt < 0) then
1749 begin
1750 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1751 result := false;
1752 exit;
1753 end;
1754 end;
1755 end;
1757 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1758 if (triggers <> nil) and (triggers.count > 0) then
1759 begin
1760 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1761 //SetLength(TriggersTable, triggers.count);
1762 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1764 for rec in triggers do
1765 begin
1766 SetLength(TriggersTable, Length(TriggersTable)+1);
1767 pttit := @TriggersTable[High(TriggersTable)];
1768 pttit.trigrec := rec;
1769 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1770 pttit.texPan := mapReader.panel[rec.TexturePanel];
1771 pttit.liftPan := nil;
1772 pttit.doorPan := nil;
1773 pttit.shotPan := nil;
1774 pttit.mplatPan := nil;
1775 pttit.texPanIdx := -1;
1776 pttit.LiftPanelIdx := -1;
1777 pttit.DoorPanelIdx := -1;
1778 pttit.ShotPanelIdx := -1;
1779 pttit.MPlatPanelIdx := -1;
1780 // Ëèôòû
1781 if rec.TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1782 begin
1783 pttit.liftPan := mapReader.panel[rec.trigRec.tgPanelID];
1784 end;
1785 // Äâåðè
1786 if rec.TriggerType in [TRIGGER_OPENDOOR, TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5, TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1787 begin
1788 pttit.doorPan := mapReader.panel[rec.trigRec.tgPanelID];
1789 end;
1790 // Òóðåëü
1791 if (rec.TriggerType = TRIGGER_SHOT) then
1792 begin
1793 pttit.shotPan := mapReader.panel[rec.trigRec.tgShotPanelID];
1794 end;
1795 //
1796 if rec.TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1797 begin
1798 pttit.mplatPan := mapReader.panel[rec.trigRec.tgPanelID];
1799 end;
1801 if (pttit.texPan <> nil) then pttit.texPan.userPanelTrigRef := true;
1802 if (pttit.liftPan <> nil) then pttit.liftPan.userPanelTrigRef := true;
1803 if (pttit.doorPan <> nil) then pttit.doorPan.userPanelTrigRef := true;
1804 if (pttit.shotPan <> nil) then pttit.shotPan.userPanelTrigRef := true;
1805 if (pttit.mplatPan <> nil) then pttit.mplatPan.userPanelTrigRef := true;
1807 g_Game_StepLoading();
1808 end;
1809 end;
1811 // Ñîçäàåì ïàíåëè
1812 if (panels <> nil) and (panels.count > 0) then
1813 begin
1814 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1815 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1817 pannum := -1;
1818 for rec in panels do
1819 begin
1820 Inc(pannum);
1821 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1822 texrec := nil;
1823 SetLength(AddTextures, 0);
1824 trigRef := False;
1825 CurTex := -1;
1826 ok := false;
1828 if (mapTextureList <> nil) then
1829 begin
1830 texrec := rec.TextureRec;
1831 ok := (texrec <> nil);
1832 end;
1834 if ok then
1835 begin
1836 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1837 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1838 ok := false;
1839 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1840 begin
1842 for b := 0 to High(TriggersTable) do
1843 begin
1844 if (TriggersTable[b].texPan = rec) or (TriggersTable[b].shotPan = rec) then
1845 begin
1846 trigRef := True;
1847 ok := True;
1848 break;
1849 end;
1850 end;
1852 if rec.userPanelTrigRef then
1853 begin
1854 // e_LogWritefln('trigref for panel %s', [pannum]);
1855 trigRef := True;
1856 ok := True;
1857 end;
1858 end;
1859 end;
1861 if ok then
1862 begin
1863 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1864 s := texrec.Resource;
1866 // Ñïåö-òåêñòóðû çàïðåùåíû
1867 if g_Map_IsSpecialTexture(s) then
1868 begin
1869 ok := false
1870 end
1871 else
1872 begin
1873 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1874 ok := g_Texture_NumNameFindStart(s);
1875 end;
1877 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1878 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1879 if ok then
1880 begin
1881 k := NNF_NAME_BEFORE;
1882 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1883 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1884 begin
1885 k := g_Texture_NumNameFindNext(TexName);
1887 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1888 begin
1889 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1890 if texrec.Anim then
1891 begin
1892 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1893 isAnim := True;
1894 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1895 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1896 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1897 if not ok then
1898 begin
1899 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1900 isAnim := False;
1901 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1902 ok := CreateTexture(TexName, FileName, False) >= 0;
1903 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1904 end;
1905 end
1906 else
1907 begin
1908 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1909 isAnim := False;
1910 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1911 ok := CreateTexture(TexName, FileName, False) >= 0;
1912 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1913 if not ok then
1914 begin
1915 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1916 isAnim := True;
1917 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1918 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1919 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1920 end;
1921 end;
1923 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1924 if ok then
1925 begin
1927 for c := 0 to High(Textures) do
1928 begin
1929 if (Textures[c].TextureName = TexName) then
1930 begin
1931 SetLength(AddTextures, Length(AddTextures)+1);
1932 AddTextures[High(AddTextures)].Texture := c;
1933 AddTextures[High(AddTextures)].Anim := isAnim;
1934 break;
1935 end;
1936 end;
1938 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1939 begin
1940 SetLength(AddTextures, Length(AddTextures)+1);
1941 AddTextures[High(AddTextures)].Texture := c;
1942 AddTextures[High(AddTextures)].Anim := isAnim;
1943 end;
1944 end;
1945 end
1946 else
1947 begin
1948 if k = NNF_NAME_EQUALS then
1949 begin
1950 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1951 SetLength(AddTextures, Length(AddTextures)+1);
1952 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1953 AddTextures[High(AddTextures)].Anim := texrec.Anim;
1954 CurTex := High(AddTextures);
1955 ok := true;
1956 end
1957 else // NNF_NO_NAME
1958 begin
1959 ok := false;
1960 end;
1961 end;
1962 end; // while ok...
1964 ok := true;
1965 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1966 end; // if ok - ññûëàþòñÿ òðèããåðû
1968 if not ok then
1969 begin
1970 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1971 SetLength(AddTextures, 1);
1972 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1973 AddTextures[0].Anim := false;
1974 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
1975 CurTex := 0;
1976 end;
1978 //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);
1980 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1982 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð
1983 PanelID := CreatePanel(rec, AddTextures, CurTex, trigRef);
1984 //e_LogWritefln('panel #%s of type %s got id #%s', [pannum, rec.PanelType, PanelID]);
1985 // set 'gamePanelId' field to panel id
1986 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1988 // setup lifts
1989 moveSpeed := rec.moveSpeed;
1990 //moveStart := rec.moveStart;
1991 //moveEnd := rec.moveEnd;
1992 //moveActive := rec['move_active'].varvalue;
1993 if not moveSpeed.isZero then
1994 begin
1995 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1996 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1997 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1998 end;
2000 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2002 g_Game_StepLoading();
2003 end;
2004 end;
2006 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2007 for b := 0 to High(TriggersTable) do
2008 begin
2009 if (TriggersTable[b].texPan <> nil) then TriggersTable[b].texPanIdx := TriggersTable[b].texPan.userPanelId;
2010 if (TriggersTable[b].liftPan <> nil) then TriggersTable[b].LiftPanelIdx := TriggersTable[b].liftPan.userPanelId;
2011 if (TriggersTable[b].doorPan <> nil) then TriggersTable[b].DoorPanelIdx := TriggersTable[b].doorPan.userPanelId;
2012 if (TriggersTable[b].shotPan <> nil) then TriggersTable[b].ShotPanelIdx := TriggersTable[b].shotPan.userPanelId;
2013 if (TriggersTable[b].mplatPan <> nil) then TriggersTable[b].MPlatPanelIdx := TriggersTable[b].mplatPan.userPanelId;
2014 end;
2016 // create map grid, init other grids (for monsters, for example)
2017 e_WriteLog('Creating map grid', MSG_NOTIFY);
2018 mapCreateGrid();
2020 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2021 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2022 begin
2023 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2024 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2025 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2026 trignum := -1;
2027 for rec in triggers do
2028 begin
2029 Inc(trignum);
2030 if (TriggersTable[trignum].texPan <> nil) then b := TriggersTable[trignum].texPan.PanelType else b := 0;
2031 if (TriggersTable[trignum].shotPan <> nil) then c := TriggersTable[trignum].shotPan.PanelType else c := 0;
2032 // we can have only one of those
2033 if (TriggersTable[trignum].LiftPanelIdx <> -1) then tgpid := TriggersTable[trignum].LiftPanelIdx
2034 else if (TriggersTable[trignum].DoorPanelIdx <> -1) then tgpid := TriggersTable[trignum].DoorPanelIdx
2035 else if (TriggersTable[trignum].ShotPanelIdx <> -1) then tgpid := TriggersTable[trignum].ShotPanelIdx
2036 else if (TriggersTable[trignum].MPlatPanelIdx <> -1) then tgpid := TriggersTable[trignum].MPlatPanelIdx
2037 else tgpid := -1;
2038 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2039 CreateTrigger(trignum, rec, TriggersTable[trignum].texPanIdx, tgpid, Word(b), Word(c));
2040 end;
2041 end;
2043 // Çàãðóçêà ïðåäìåòîâ
2044 e_WriteLog(' Loading items...', MSG_NOTIFY);
2045 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2047 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2048 if (items <> nil) and not gLoadGameMode then
2049 begin
2050 e_WriteLog(' Spawning items...', MSG_NOTIFY);
2051 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2052 for rec in items do CreateItem(rec);
2053 end;
2055 // Çàãðóçêà îáëàñòåé
2056 e_WriteLog(' Loading areas...', MSG_NOTIFY);
2057 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2059 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2060 if areas <> nil then
2061 begin
2062 e_WriteLog(' Creating areas...', MSG_NOTIFY);
2063 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2064 for rec in areas do CreateArea(rec);
2065 end;
2067 // Çàãðóçêà ìîíñòðîâ
2068 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
2069 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2071 gTotalMonsters := 0;
2073 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2074 if (monsters <> nil) and not gLoadGameMode then
2075 begin
2076 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
2077 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2078 for rec in monsters do CreateMonster(rec);
2079 end;
2081 gCurrentMap := mapReader; // this will be our current map now
2082 gCurrentMapFileName := Res;
2083 mapReader := nil;
2085 // Çàãðóçêà íåáà
2086 if gMapInfo.SkyName <> '' then
2087 begin
2088 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
2089 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2090 FileName := g_ExtractWadName(gMapInfo.SkyName);
2092 if FileName <> '' then
2093 FileName := GameDir+'/wads/'+FileName
2094 else
2095 begin
2096 FileName := g_ExtractWadName(Res);
2097 end;
2099 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2100 if g_Texture_CreateWAD(BackID, s) then
2101 begin
2102 g_Game_SetupScreenSize();
2103 end
2104 else
2105 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2106 end;
2108 // Çàãðóçêà ìóçûêè
2109 ok := False;
2110 if gMapInfo.MusicName <> '' then
2111 begin
2112 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
2113 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2114 FileName := g_ExtractWadName(gMapInfo.MusicName);
2116 if FileName <> '' then
2117 FileName := GameDir+'/wads/'+FileName
2118 else
2119 begin
2120 FileName := g_ExtractWadName(Res);
2121 end;
2123 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2124 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2125 ok := True
2126 else
2127 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2128 end;
2130 // Îñòàëüíûå óñòàíâêè
2131 CreateDoorMap();
2132 CreateLiftMap();
2134 g_Items_Init();
2135 g_Weapon_Init();
2136 g_Monsters_Init();
2138 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2139 if not gLoadGameMode then g_GFX_Init();
2141 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2142 mapTextureList := nil;
2143 panels := nil;
2144 items := nil;
2145 areas := nil;
2146 triggers := nil;
2147 TriggersTable := nil;
2148 AddTextures := nil;
2150 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2151 if ok and (not gLoadGameMode) then
2152 begin
2153 gMusic.SetByName(gMapInfo.MusicName);
2154 gMusic.Play();
2155 end
2156 else
2157 begin
2158 gMusic.SetByName('');
2159 end;
2161 stt := curTimeMicro()-stt;
2162 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2163 finally
2164 sfsGCEnable(); // enable releasing unused volumes
2165 mapReader.Free();
2166 e_ClearInputBuffer(); // why not?
2167 end;
2169 e_WriteLog('Done loading map.', MSG_NOTIFY);
2170 Result := True;
2171 end;
2174 function g_Map_GetMapInfo(Res: String): TMapInfo;
2175 var
2176 WAD: TWADFile;
2177 mapReader: TDynRecord;
2178 //Header: TMapHeaderRec_1;
2179 FileName: String;
2180 Data: Pointer;
2181 Len: Integer;
2182 begin
2183 FillChar(Result, SizeOf(Result), 0);
2184 FileName := g_ExtractWadName(Res);
2186 WAD := TWADFile.Create();
2187 if not WAD.ReadFile(FileName) then
2188 begin
2189 WAD.Free();
2190 Exit;
2191 end;
2193 //k8: it ignores path again
2194 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2195 begin
2196 WAD.Free();
2197 Exit;
2198 end;
2200 WAD.Free();
2202 try
2203 mapReader := g_Map_ParseMap(Data, Len);
2204 except
2205 mapReader := nil;
2206 end;
2208 FreeMem(Data);
2210 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2211 begin
2212 Result.Name := mapReader.MapName;
2213 Result.Description := mapReader.MapDesc;
2214 Result.Map := Res;
2215 Result.Author := mapReader.MapAuthor;
2216 Result.Height := mapReader.Height;
2217 Result.Width := mapReader.Width;
2218 end
2219 else
2220 begin
2221 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2222 //ZeroMemory(@Header, SizeOf(Header));
2223 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2224 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2225 Result.Map := Res;
2226 Result.Author := '';
2227 Result.Height := 0;
2228 Result.Width := 0;
2229 end;
2231 mapReader.Free();
2232 end;
2234 function g_Map_GetMapsList(WADName: string): SArray;
2235 var
2236 WAD: TWADFile;
2237 a: Integer;
2238 ResList: SArray;
2239 begin
2240 Result := nil;
2241 WAD := TWADFile.Create();
2242 if not WAD.ReadFile(WADName) then
2243 begin
2244 WAD.Free();
2245 Exit;
2246 end;
2247 ResList := WAD.GetMapResources();
2248 if ResList <> nil then
2249 begin
2250 for a := 0 to High(ResList) do
2251 begin
2252 SetLength(Result, Length(Result)+1);
2253 Result[High(Result)] := ResList[a];
2254 end;
2255 end;
2256 WAD.Free();
2257 end;
2259 function g_Map_Exist(Res: string): Boolean;
2260 var
2261 WAD: TWADFile;
2262 FileName, mnn: string;
2263 ResList: SArray;
2264 a: Integer;
2265 begin
2266 Result := False;
2268 FileName := addWadExtension(g_ExtractWadName(Res));
2270 WAD := TWADFile.Create;
2271 if not WAD.ReadFile(FileName) then
2272 begin
2273 WAD.Free();
2274 Exit;
2275 end;
2277 ResList := WAD.GetMapResources();
2278 WAD.Free();
2280 mnn := g_ExtractFileName(Res);
2281 if ResList <> nil then
2282 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2283 begin
2284 Result := True;
2285 Exit;
2286 end;
2287 end;
2289 procedure g_Map_Free(freeTextures: Boolean=true);
2290 var
2291 a: Integer;
2293 procedure FreePanelArray(var panels: TPanelArray);
2294 var
2295 i: Integer;
2297 begin
2298 if panels <> nil then
2299 begin
2300 for i := 0 to High(panels) do
2301 panels[i].Free();
2302 panels := nil;
2303 end;
2304 end;
2306 begin
2307 g_GFX_Free();
2308 g_Weapon_Free();
2309 g_Items_Free();
2310 g_Triggers_Free();
2311 g_Monsters_Free();
2313 RespawnPoints := nil;
2314 if FlagPoints[FLAG_RED] <> nil then
2315 begin
2316 Dispose(FlagPoints[FLAG_RED]);
2317 FlagPoints[FLAG_RED] := nil;
2318 end;
2319 if FlagPoints[FLAG_BLUE] <> nil then
2320 begin
2321 Dispose(FlagPoints[FLAG_BLUE]);
2322 FlagPoints[FLAG_BLUE] := nil;
2323 end;
2324 //DOMFlagPoints := nil;
2326 //gDOMFlags := nil;
2328 if (Length(gCurrentMapFileName) <> 0) then
2329 begin
2330 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2331 end
2332 else
2333 begin
2334 e_LogWritefln('g_Map_Free: no previous map.', []);
2335 end;
2336 if freeTextures then
2337 begin
2338 e_LogWritefln('g_Map_Free: clearing textures...', []);
2339 if (Textures <> nil) then
2340 begin
2341 for a := 0 to High(Textures) do
2342 begin
2343 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2344 begin
2345 if Textures[a].Anim then
2346 begin
2347 g_Frames_DeleteByID(Textures[a].FramesID)
2348 end
2349 else
2350 begin
2351 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2352 begin
2353 e_DeleteTexture(Textures[a].TextureID);
2354 end;
2355 end;
2356 end;
2357 end;
2358 Textures := nil;
2359 end;
2360 TextNameHash.Free();
2361 TextNameHash := nil;
2362 BadTextNameHash.Free();
2363 BadTextNameHash := nil;
2364 end;
2366 //panByGUID.Free();
2367 //panByGUID := nil;
2368 //panLastGUID := 0;
2369 panByGUID := nil;
2371 FreePanelArray(gWalls);
2372 FreePanelArray(gRenderBackgrounds);
2373 FreePanelArray(gRenderForegrounds);
2374 FreePanelArray(gWater);
2375 FreePanelArray(gAcid1);
2376 FreePanelArray(gAcid2);
2377 FreePanelArray(gSteps);
2378 FreePanelArray(gLifts);
2379 FreePanelArray(gBlockMon);
2380 gMovingWallIds := nil;
2382 if BackID <> DWORD(-1) then
2383 begin
2384 gBackSize.X := 0;
2385 gBackSize.Y := 0;
2386 e_DeleteTexture(BackID);
2387 BackID := DWORD(-1);
2388 end;
2390 g_Game_StopAllSounds(False);
2391 gMusic.FreeSound();
2392 g_Sound_Delete(gMapInfo.MusicName);
2394 gMapInfo.Name := '';
2395 gMapInfo.Description := '';
2396 gMapInfo.MusicName := '';
2397 gMapInfo.Height := 0;
2398 gMapInfo.Width := 0;
2400 gDoorMap := nil;
2401 gLiftMap := nil;
2402 end;
2404 procedure g_Map_Update();
2405 var
2406 a, d, j: Integer;
2407 m: Word;
2408 s: String;
2410 procedure UpdatePanelArray(var panels: TPanelArray);
2411 var
2412 i: Integer;
2414 begin
2415 for i := 0 to High(panels) do panels[i].Update();
2416 end;
2418 begin
2419 UpdatePanelArray(gWalls);
2420 UpdatePanelArray(gRenderBackgrounds);
2421 UpdatePanelArray(gRenderForegrounds);
2422 UpdatePanelArray(gWater);
2423 UpdatePanelArray(gAcid1);
2424 UpdatePanelArray(gAcid2);
2425 UpdatePanelArray(gSteps);
2427 if gGameSettings.GameMode = GM_CTF then
2428 begin
2429 for a := FLAG_RED to FLAG_BLUE do
2430 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2431 with gFlags[a] do
2432 begin
2433 if gFlags[a].Animation <> nil then
2434 gFlags[a].Animation.Update();
2436 m := g_Obj_Move(@Obj, True, True);
2438 if gTime mod (GAME_TICK*2) <> 0 then
2439 Continue;
2441 // Ñîïðîòèâëåíèå âîçäóõà:
2442 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2444 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2445 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2446 begin
2447 g_Map_ResetFlag(a);
2448 gFlags[a].CaptureTime := 0;
2449 if a = FLAG_RED then
2450 s := _lc[I_PLAYER_FLAG_RED]
2451 else
2452 s := _lc[I_PLAYER_FLAG_BLUE];
2453 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2455 if g_Game_IsNet then
2456 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2457 Continue;
2458 end;
2460 if Count > 0 then
2461 Count := Count - 1;
2463 // Èãðîê áåðåò ôëàã:
2464 if gPlayers <> nil then
2465 begin
2466 j := Random(Length(gPlayers)) - 1;
2468 for d := 0 to High(gPlayers) do
2469 begin
2470 Inc(j);
2471 if j > High(gPlayers) then
2472 j := 0;
2474 if gPlayers[j] <> nil then
2475 if gPlayers[j].Live and
2476 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2477 begin
2478 if gPlayers[j].GetFlag(a) then
2479 Break;
2480 end;
2481 end;
2482 end;
2483 end;
2484 end;
2485 end;
2488 // old algo
2489 procedure g_Map_DrawPanels (PanelType: Word);
2491 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2492 var
2493 idx: Integer;
2494 begin
2495 if (panels <> nil) then
2496 begin
2497 // alas, no visible set
2498 for idx := 0 to High(panels) do
2499 begin
2500 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2501 end;
2502 end;
2503 end;
2505 begin
2506 case PanelType of
2507 PANEL_WALL: DrawPanels(gWalls);
2508 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2509 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2510 PANEL_FORE: DrawPanels(gRenderForegrounds);
2511 PANEL_WATER: DrawPanels(gWater);
2512 PANEL_ACID1: DrawPanels(gAcid1);
2513 PANEL_ACID2: DrawPanels(gAcid2);
2514 PANEL_STEP: DrawPanels(gSteps);
2515 end;
2516 end;
2519 // new algo
2520 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2522 function checker (pan: TPanel; tag: Integer): Boolean;
2523 begin
2524 result := false; // don't stop, ever
2525 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2526 gDrawPanelList.insert(pan);
2527 end;
2529 begin
2530 dplClear();
2531 //tagmask := panelTypeToTag(PanelType);
2532 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2533 // list will be rendered in `g_game.DrawPlayer()`
2534 end;
2537 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2539 function checker (pan: TPanel; tag: Integer): Boolean;
2540 begin
2541 result := false; // don't stop, ever
2542 pan.DrawShadowVolume(lightX, lightY, radius);
2543 end;
2545 begin
2546 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2547 end;
2550 procedure g_Map_DrawBack(dx, dy: Integer);
2551 begin
2552 if gDrawBackGround and (BackID <> DWORD(-1)) then
2553 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2554 else
2555 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2556 end;
2558 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2559 PanelType: Word; b1x3: Boolean=false): Boolean;
2560 var
2561 a, h: Integer;
2562 begin
2563 Result := False;
2565 if WordBool(PanelType and PANEL_WALL) then
2566 if gWalls <> nil then
2567 begin
2568 h := High(gWalls);
2570 for a := 0 to h do
2571 if gWalls[a].Enabled and
2572 g_Collide(X, Y, Width, Height,
2573 gWalls[a].X, gWalls[a].Y,
2574 gWalls[a].Width, gWalls[a].Height) then
2575 begin
2576 Result := True;
2577 Exit;
2578 end;
2579 end;
2581 if WordBool(PanelType and PANEL_WATER) then
2582 if gWater <> nil then
2583 begin
2584 h := High(gWater);
2586 for a := 0 to h do
2587 if g_Collide(X, Y, Width, Height,
2588 gWater[a].X, gWater[a].Y,
2589 gWater[a].Width, gWater[a].Height) then
2590 begin
2591 Result := True;
2592 Exit;
2593 end;
2594 end;
2596 if WordBool(PanelType and PANEL_ACID1) then
2597 if gAcid1 <> nil then
2598 begin
2599 h := High(gAcid1);
2601 for a := 0 to h do
2602 if g_Collide(X, Y, Width, Height,
2603 gAcid1[a].X, gAcid1[a].Y,
2604 gAcid1[a].Width, gAcid1[a].Height) then
2605 begin
2606 Result := True;
2607 Exit;
2608 end;
2609 end;
2611 if WordBool(PanelType and PANEL_ACID2) then
2612 if gAcid2 <> nil then
2613 begin
2614 h := High(gAcid2);
2616 for a := 0 to h do
2617 if g_Collide(X, Y, Width, Height,
2618 gAcid2[a].X, gAcid2[a].Y,
2619 gAcid2[a].Width, gAcid2[a].Height) then
2620 begin
2621 Result := True;
2622 Exit;
2623 end;
2624 end;
2626 if WordBool(PanelType and PANEL_STEP) then
2627 if gSteps <> nil then
2628 begin
2629 h := High(gSteps);
2631 for a := 0 to h do
2632 if g_Collide(X, Y, Width, Height,
2633 gSteps[a].X, gSteps[a].Y,
2634 gSteps[a].Width, gSteps[a].Height) then
2635 begin
2636 Result := True;
2637 Exit;
2638 end;
2639 end;
2641 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2642 if gLifts <> nil then
2643 begin
2644 h := High(gLifts);
2646 for a := 0 to h do
2647 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2648 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2649 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2650 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2651 g_Collide(X, Y, Width, Height,
2652 gLifts[a].X, gLifts[a].Y,
2653 gLifts[a].Width, gLifts[a].Height) then
2654 begin
2655 Result := True;
2656 Exit;
2657 end;
2658 end;
2660 if WordBool(PanelType and PANEL_BLOCKMON) then
2661 if gBlockMon <> nil then
2662 begin
2663 h := High(gBlockMon);
2665 for a := 0 to h do
2666 if ( (not b1x3) or
2667 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2668 g_Collide(X, Y, Width, Height,
2669 gBlockMon[a].X, gBlockMon[a].Y,
2670 gBlockMon[a].Width, gBlockMon[a].Height) then
2671 begin
2672 Result := True;
2673 Exit;
2674 end;
2675 end;
2676 end;
2678 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2679 var
2680 texid: DWORD;
2682 function checkPanels (constref panels: TPanelArray): Boolean;
2683 var
2684 a: Integer;
2685 begin
2686 result := false;
2687 if panels = nil then exit;
2688 for a := 0 to High(panels) do
2689 begin
2690 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2691 begin
2692 result := true;
2693 texid := panels[a].GetTextureID();
2694 exit;
2695 end;
2696 end;
2697 end;
2699 begin
2700 texid := LongWord(TEXTURE_NONE);
2701 result := texid;
2702 if not checkPanels(gWater) then
2703 if not checkPanels(gAcid1) then
2704 if not checkPanels(gAcid2) then exit;
2705 result := texid;
2706 end;
2709 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2710 const
2711 SlowMask = GridTagLift or GridTagBlockMon;
2712 function checker (pan: TPanel; tag: Integer): Boolean;
2713 begin
2715 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2716 begin
2717 result := pan.Enabled;
2718 exit;
2719 end;
2722 if ((tag and GridTagLift) <> 0) then
2723 begin
2724 result :=
2725 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2726 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2727 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2728 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2729 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2730 exit;
2731 end;
2733 if ((tag and GridTagBlockMon) <> 0) then
2734 begin
2735 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2736 exit;
2737 end;
2739 // other shit
2740 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2741 result := true; // i found her!
2742 end;
2744 var
2745 tagmask: Integer = 0;
2746 begin
2747 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2748 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2749 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2750 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2751 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2752 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2753 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2755 if (tagmask = 0) then begin result := false; exit; end; // just in case
2757 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2758 if gdbg_map_use_accel_coldet then
2759 begin
2760 if (Width = 1) and (Height = 1) then
2761 begin
2762 if ((tagmask and SlowMask) <> 0) then
2763 begin
2764 // slow
2765 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2766 end
2767 else
2768 begin
2769 // fast
2770 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2771 end;
2772 end
2773 else
2774 begin
2775 if ((tagmask and SlowMask) <> 0) then
2776 begin
2777 // slow
2778 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2779 end
2780 else
2781 begin
2782 // fast
2783 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2784 end;
2785 end;
2786 end
2787 else
2788 begin
2789 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2790 end;
2791 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2792 end;
2795 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2796 var
2797 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2798 texid: DWORD;
2800 // slightly different from the old code, but meh...
2801 function checker (pan: TPanel; tag: Integer): Boolean;
2802 begin
2803 result := false; // don't stop, ever
2804 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2805 // check priorities
2806 case cctype of
2807 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2808 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2809 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2810 end;
2811 // collision?
2812 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2813 // yeah
2814 texid := pan.GetTextureID();
2815 // water? water has the highest priority, so stop right here
2816 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2817 // acid2?
2818 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2819 // acid1?
2820 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2821 end;
2823 begin
2824 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2825 if gdbg_map_use_accel_coldet then
2826 begin
2827 texid := LongWord(TEXTURE_NONE);
2828 if (Width = 1) and (Height = 1) then
2829 begin
2830 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2831 end
2832 else
2833 begin
2834 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2835 end;
2836 result := texid;
2837 end
2838 else
2839 begin
2840 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2841 end;
2842 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2843 end;
2846 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2847 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2848 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2851 procedure g_Map_EnableWallGUID (pguid: Integer);
2852 var
2853 pan: TPanel;
2854 begin
2855 //pan := gWalls[ID];
2856 pan := g_Map_PanelByGUID(pguid);
2857 if (pan = nil) then exit;
2858 pan.Enabled := True;
2859 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2861 mapGrid.proxyEnabled[pan.proxyId] := true;
2862 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2863 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2865 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState({gWalls[ID]}pan.PanelType, pguid);
2867 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2868 //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);
2869 {$ENDIF}
2870 end;
2873 procedure g_Map_DisableWallGUID (pguid: Integer);
2874 var
2875 pan: TPanel;
2876 begin
2877 //pan := gWalls[ID];
2878 pan := g_Map_PanelByGUID(pguid);
2879 if (pan = nil) then exit;
2880 pan.Enabled := False;
2881 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2883 mapGrid.proxyEnabled[pan.proxyId] := false;
2884 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2886 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, pguid);
2888 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2889 //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);
2890 {$ENDIF}
2891 end;
2894 procedure g_Map_SwitchTextureGUID (PanelType: Word; pguid: Integer; AnimLoop: Byte = 0);
2895 var
2896 tp: TPanel;
2897 begin
2898 tp := g_Map_PanelByGUID(pguid);
2899 if (tp = nil) then exit;
2901 case PanelType of
2902 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: tp := gWalls[ID];
2903 PANEL_FORE: tp := gRenderForegrounds[ID];
2904 PANEL_BACK: tp := gRenderBackgrounds[ID];
2905 PANEL_WATER: tp := gWater[ID];
2906 PANEL_ACID1: tp := gAcid1[ID];
2907 PANEL_ACID2: tp := gAcid2[ID];
2908 PANEL_STEP: tp := gSteps[ID];
2909 else exit;
2910 end;
2913 tp.NextTexture(AnimLoop);
2914 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(PanelType, pguid, AnimLoop);
2915 end;
2918 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2919 var
2920 pan: TPanel;
2921 begin
2922 //pan := gLifts[ID];
2923 pan := g_Map_PanelByGUID(pguid);
2924 if (pan = nil) then exit;
2925 if not pan.isGLift then exit;
2927 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2929 with {gLifts[ID]} pan do
2930 begin
2931 LiftType := t;
2933 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2934 //TODO: make separate lift tags, and change tag here
2936 case LiftType of
2937 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2938 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2939 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2940 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2941 end;
2943 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, pguid);
2944 end;
2945 end;
2948 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2949 var
2950 a: Integer;
2951 PointsArray: Array of TRespawnPoint;
2952 begin
2953 Result := False;
2954 SetLength(PointsArray, 0);
2956 if RespawnPoints = nil then
2957 Exit;
2959 for a := 0 to High(RespawnPoints) do
2960 if RespawnPoints[a].PointType = PointType then
2961 begin
2962 SetLength(PointsArray, Length(PointsArray)+1);
2963 PointsArray[High(PointsArray)] := RespawnPoints[a];
2964 end;
2966 if PointsArray = nil then
2967 Exit;
2969 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2970 Result := True;
2971 end;
2973 function g_Map_GetPointCount(PointType: Byte): Word;
2974 var
2975 a: Integer;
2976 begin
2977 Result := 0;
2979 if RespawnPoints = nil then
2980 Exit;
2982 for a := 0 to High(RespawnPoints) do
2983 if RespawnPoints[a].PointType = PointType then
2984 Result := Result + 1;
2985 end;
2987 function g_Map_HaveFlagPoints(): Boolean;
2988 begin
2989 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2990 end;
2992 procedure g_Map_ResetFlag(Flag: Byte);
2993 begin
2994 with gFlags[Flag] do
2995 begin
2996 Obj.X := -1000;
2997 Obj.Y := -1000;
2998 Obj.Vel.X := 0;
2999 Obj.Vel.Y := 0;
3000 Direction := D_LEFT;
3001 State := FLAG_STATE_NONE;
3002 if FlagPoints[Flag] <> nil then
3003 begin
3004 Obj.X := FlagPoints[Flag]^.X;
3005 Obj.Y := FlagPoints[Flag]^.Y;
3006 Direction := FlagPoints[Flag]^.Direction;
3007 State := FLAG_STATE_NORMAL;
3008 end;
3009 Count := -1;
3010 end;
3011 end;
3013 procedure g_Map_DrawFlags();
3014 var
3015 i, dx: Integer;
3016 Mirror: TMirrorType;
3017 begin
3018 if gGameSettings.GameMode <> GM_CTF then
3019 Exit;
3021 for i := FLAG_RED to FLAG_BLUE do
3022 with gFlags[i] do
3023 if State <> FLAG_STATE_CAPTURED then
3024 begin
3025 if State = FLAG_STATE_NONE then
3026 continue;
3028 if Direction = D_LEFT then
3029 begin
3030 Mirror := M_HORIZONTAL;
3031 dx := -1;
3032 end
3033 else
3034 begin
3035 Mirror := M_NONE;
3036 dx := 1;
3037 end;
3039 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3041 if g_debug_Frames then
3042 begin
3043 e_DrawQuad(Obj.X+Obj.Rect.X,
3044 Obj.Y+Obj.Rect.Y,
3045 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3046 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3047 0, 255, 0);
3048 end;
3049 end;
3050 end;
3052 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
3053 var
3054 dw: DWORD;
3055 b: Byte;
3056 str: String;
3057 boo: Boolean;
3059 procedure SavePanelArray(var panels: TPanelArray);
3060 var
3061 PAMem: TBinMemoryWriter;
3062 i: Integer;
3063 begin
3064 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
3065 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
3067 i := 0;
3068 while i < Length(panels) do
3069 begin
3070 if true{panels[i].SaveIt} then
3071 begin
3072 // ID ïàíåëè:
3073 PAMem.WriteInt(i);
3074 // Ñîõðàíÿåì ïàíåëü:
3075 panels[i].SaveState(PAMem);
3076 end;
3077 Inc(i);
3078 end;
3080 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
3081 PAMem.SaveToMemory(Mem);
3082 PAMem.Free();
3083 end;
3085 procedure SaveFlag(flag: PFlag);
3086 begin
3087 // Ñèãíàòóðà ôëàãà:
3088 dw := FLAG_SIGNATURE; // 'FLAG'
3089 Mem.WriteDWORD(dw);
3090 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
3091 Mem.WriteByte(flag^.RespawnType);
3092 // Ñîñòîÿíèå ôëàãà:
3093 Mem.WriteByte(flag^.State);
3094 // Íàïðàâëåíèå ôëàãà:
3095 if flag^.Direction = D_LEFT then
3096 b := 1
3097 else // D_RIGHT
3098 b := 2;
3099 Mem.WriteByte(b);
3100 // Îáúåêò ôëàãà:
3101 Obj_SaveState(@flag^.Obj, Mem);
3102 end;
3104 begin
3105 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
3107 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
3108 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
3109 SavePanelArray(gWalls);
3110 // Ñîõðàíÿåì ïàíåëè ôîíà:
3111 SavePanelArray(gRenderBackgrounds);
3112 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
3113 SavePanelArray(gRenderForegrounds);
3114 // Ñîõðàíÿåì ïàíåëè âîäû:
3115 SavePanelArray(gWater);
3116 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
3117 SavePanelArray(gAcid1);
3118 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
3119 SavePanelArray(gAcid2);
3120 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
3121 SavePanelArray(gSteps);
3122 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
3123 SavePanelArray(gLifts);
3124 ///// /////
3126 ///// Ñîõðàíÿåì ìóçûêó: /////
3127 // Ñèãíàòóðà ìóçûêè:
3128 dw := MUSIC_SIGNATURE; // 'MUSI'
3129 Mem.WriteDWORD(dw);
3130 // Íàçâàíèå ìóçûêè:
3131 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3132 if gMusic.NoMusic then
3133 str := ''
3134 else
3135 str := gMusic.Name;
3136 Mem.WriteString(str, 64);
3137 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
3138 dw := gMusic.GetPosition();
3139 Mem.WriteDWORD(dw);
3140 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
3141 boo := gMusic.SpecPause;
3142 Mem.WriteBoolean(boo);
3143 ///// /////
3145 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3146 Mem.WriteInt(gTotalMonsters);
3147 ///// /////
3149 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3150 if gGameSettings.GameMode = GM_CTF then
3151 begin
3152 // Ôëàã Êðàñíîé êîìàíäû:
3153 SaveFlag(@gFlags[FLAG_RED]);
3154 // Ôëàã Ñèíåé êîìàíäû:
3155 SaveFlag(@gFlags[FLAG_BLUE]);
3156 end;
3157 ///// /////
3159 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3160 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3161 begin
3162 // Î÷êè Êðàñíîé êîìàíäû:
3163 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
3164 // Î÷êè Ñèíåé êîìàíäû:
3165 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
3166 end;
3167 ///// /////
3168 end;
3170 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
3171 var
3172 dw: DWORD;
3173 b: Byte;
3174 str: String;
3175 boo: Boolean;
3177 procedure LoadPanelArray(var panels: TPanelArray);
3178 var
3179 PAMem: TBinMemoryReader;
3180 i, id: Integer;
3181 begin
3182 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
3183 PAMem := TBinMemoryReader.Create();
3184 PAMem.LoadFromMemory(Mem);
3186 for i := 0 to Length(panels)-1 do
3187 begin
3188 if true{panels[i].SaveIt} then
3189 begin
3190 // ID ïàíåëè:
3191 PAMem.ReadInt(id);
3192 if id <> i then
3193 begin
3194 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
3195 end;
3196 // Çàãðóæàåì ïàíåëü:
3197 panels[i].LoadState(PAMem);
3198 if (panels[i].arrIdx <> i) then raise Exception.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
3199 if (panels[i].proxyId >= 0) then mapGrid.proxyEnabled[panels[i].proxyId] := panels[i].Enabled;
3200 end;
3201 end;
3203 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
3204 PAMem.Free();
3205 end;
3207 procedure LoadFlag(flag: PFlag);
3208 begin
3209 // Ñèãíàòóðà ôëàãà:
3210 Mem.ReadDWORD(dw);
3211 if dw <> FLAG_SIGNATURE then // 'FLAG'
3212 begin
3213 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
3214 end;
3215 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
3216 Mem.ReadByte(flag^.RespawnType);
3217 // Ñîñòîÿíèå ôëàãà:
3218 Mem.ReadByte(flag^.State);
3219 // Íàïðàâëåíèå ôëàãà:
3220 Mem.ReadByte(b);
3221 if b = 1 then
3222 flag^.Direction := D_LEFT
3223 else // b = 2
3224 flag^.Direction := D_RIGHT;
3225 // Îáúåêò ôëàãà:
3226 Obj_LoadState(@flag^.Obj, Mem);
3227 end;
3229 begin
3230 if Mem = nil then
3231 Exit;
3233 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3234 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
3235 LoadPanelArray(gWalls);
3236 // Çàãðóæàåì ïàíåëè ôîíà:
3237 LoadPanelArray(gRenderBackgrounds);
3238 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
3239 LoadPanelArray(gRenderForegrounds);
3240 // Çàãðóæàåì ïàíåëè âîäû:
3241 LoadPanelArray(gWater);
3242 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
3243 LoadPanelArray(gAcid1);
3244 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
3245 LoadPanelArray(gAcid2);
3246 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
3247 LoadPanelArray(gSteps);
3248 // Çàãðóæàåì ïàíåëè ëèôòîâ:
3249 LoadPanelArray(gLifts);
3250 ///// /////
3252 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
3253 g_GFX_Init();
3254 //mapCreateGrid();
3256 ///// Çàãðóæàåì ìóçûêó: /////
3257 // Ñèãíàòóðà ìóçûêè:
3258 Mem.ReadDWORD(dw);
3259 if dw <> MUSIC_SIGNATURE then // 'MUSI'
3260 begin
3261 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
3262 end;
3263 // Íàçâàíèå ìóçûêè:
3264 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3265 Mem.ReadString(str);
3266 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
3267 Mem.ReadDWORD(dw);
3268 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
3269 Mem.ReadBoolean(boo);
3270 // Çàïóñêàåì ýòó ìóçûêó:
3271 gMusic.SetByName(str);
3272 gMusic.SpecPause := boo;
3273 gMusic.Play();
3274 gMusic.Pause(True);
3275 gMusic.SetPosition(dw);
3276 ///// /////
3278 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3279 Mem.ReadInt(gTotalMonsters);
3280 ///// /////
3282 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3283 if gGameSettings.GameMode = GM_CTF then
3284 begin
3285 // Ôëàã Êðàñíîé êîìàíäû:
3286 LoadFlag(@gFlags[FLAG_RED]);
3287 // Ôëàã Ñèíåé êîìàíäû:
3288 LoadFlag(@gFlags[FLAG_BLUE]);
3289 end;
3290 ///// /////
3292 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3293 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3294 begin
3295 // Î÷êè Êðàñíîé êîìàíäû:
3296 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
3297 // Î÷êè Ñèíåé êîìàíäû:
3298 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
3299 end;
3300 ///// /////
3301 end;
3304 // trace liquid, stepping by `dx` and `dy`
3305 // return last seen liquid coords, and `false` if we're started outside of the liquid
3306 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3307 const
3308 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3309 begin
3310 topx := x;
3311 topy := y;
3312 // started outside of the liquid?
3313 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3314 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3315 result := true;
3316 while true do
3317 begin
3318 Inc(x, dx);
3319 Inc(y, dy);
3320 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3321 topx := x;
3322 topy := y;
3323 end;
3324 end;
3327 end.