DEADSOFTWARE

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