DEADSOFTWARE

made some class' properties published for Holmes
[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;
197 gdbg_map_use_accel_render: Boolean = true;
198 gdbg_map_use_accel_coldet: Boolean = true;
199 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
200 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
202 gCurrentMap: TDynRecord = nil;
203 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
206 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
209 type
210 TPanelGrid = specialize TBodyGridBase<TPanel>;
212 var
213 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
216 implementation
218 uses
219 e_input, g_main, e_log, SysUtils, g_items, g_gfx, g_console,
220 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
221 g_options, g_triggers, g_player,
222 Math, g_monsters, g_saveload, g_language, g_netmsg,
223 utils, sfs, xstreams, hashtable,
224 ImagingTypes, Imaging, ImagingUtility,
225 ImagingGif, ImagingNetworkGraphics;
227 const
228 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
229 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
230 FLAG_SIGNATURE = $47414C46; // 'FLAG'
233 var
234 dfmapdef: TDynMapDef = nil;
236 procedure loadMapDefinition ();
237 var
238 pr: TTextParser = nil;
239 st: TStream = nil;
240 WAD: TWADFile = nil;
241 begin
242 if (dfmapdef <> nil) then exit;
243 try
244 e_LogWritefln('parsing "mapdef.txt"...', []);
245 st := openDiskFileRO(DataDir+'mapdef.txt');
246 except
247 st := nil;
248 e_LogWritefln('local "%smapdef.txt" not found', [DataDir]);
249 end;
250 if (st = nil) then
251 begin
252 WAD := TWADFile.Create();
253 if not WAD.ReadFile(GameWAD) then
254 begin
255 //raise Exception.Create('cannot load "game.wad"');
256 st := nil;
257 end
258 else
259 begin
260 st := WAD.openFileStream('mapdef.txt');
261 end;
262 end;
264 if (st = nil) then
265 begin
266 //raise Exception.Create('cannot open "mapdef.txt"');
267 e_LogWritefln('using default "mapdef.txt"...', [], MSG_WARNING);
268 pr := TStrTextParser.Create(defaultMapDef);
269 end
270 else
271 begin
272 pr := TFileTextParser.Create(st);
273 end;
275 try
276 dfmapdef := TDynMapDef.Create(pr);
277 except on e: Exception do
278 raise Exception.Create(Format('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.line, pr.col, e.message]));
279 end;
281 st.Free();
282 WAD.Free();
283 end;
286 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
287 var
288 wst: TSFSMemoryChunkStream = nil;
289 pr: TTextParser = nil;
290 begin
291 result := nil;
292 if (dataLen < 4) then exit;
293 loadMapDefinition();
294 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
296 wst := TSFSMemoryChunkStream.Create(data, dataLen);
298 if (PAnsiChar(data)[0] = 'M') and (PAnsiChar(data)[1] = 'A') and (PAnsiChar(data)[2] = 'P') and (PByte(data)[3] = 1) then
299 begin
300 // binary map
301 try
302 result := dfmapdef.parseBinMap(wst);
303 except on e: Exception do
304 begin
305 e_LogWritefln('ERROR: %s', [e.message]);
306 wst.Free();
307 result := nil;
308 exit;
309 end;
310 end;
311 wst.Free();
312 end
313 else
314 begin
315 // text map
316 pr := TFileTextParser.Create(wst);
317 try
318 result := dfmapdef.parseMap(pr);
319 except on e: Exception do
320 begin
321 if (pr <> nil) then e_LogWritefln('ERROR at (%s,%s): %s', [pr.line, pr.col, e.message])
322 else e_LogWritefln('ERROR: %s', [e.message]);
323 pr.Free(); // will free `wst`
324 result := nil;
325 exit;
326 end;
327 end;
328 pr.Free(); // will free `wst`
329 end;
330 end;
333 var
334 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
335 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
336 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
339 function g_Texture_NumNameFindStart(name: String): Boolean;
340 var
341 i: Integer;
343 begin
344 Result := False;
345 NNF_PureName := '';
346 NNF_FirstNum := -1;
347 NNF_CurrentNum := -1;
349 for i := Length(name) downto 1 do
350 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
351 begin
352 if i = Length(name) then
353 begin // Íåò öèôð â êîíöå ñòðîêè
354 Exit;
355 end
356 else
357 begin
358 NNF_PureName := Copy(name, 1, i);
359 Delete(name, 1, i);
360 Break;
361 end;
362 end;
364 // Íå ïåðåâåñòè â ÷èñëî:
365 if not TryStrToInt(name, NNF_FirstNum) then
366 Exit;
368 NNF_CurrentNum := 0;
370 Result := True;
371 end;
374 function g_Texture_NumNameFindNext(var newName: String): Byte;
375 begin
376 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
377 begin
378 newName := '';
379 Result := NNF_NO_NAME;
380 Exit;
381 end;
383 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
385 if NNF_CurrentNum < NNF_FirstNum then
386 Result := NNF_NAME_BEFORE
387 else
388 if NNF_CurrentNum > NNF_FirstNum then
389 Result := NNF_NAME_AFTER
390 else
391 Result := NNF_NAME_EQUALS;
393 Inc(NNF_CurrentNum);
394 end;
397 function panelTypeToTag (panelType: Word): Integer;
398 begin
399 case panelType of
400 PANEL_WALL: result := GridTagWall; // gWalls
401 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
402 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
403 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
404 PANEL_WATER: result := GridTagWater; // gWater
405 PANEL_ACID1: result := GridTagAcid1; // gAcid1
406 PANEL_ACID2: result := GridTagAcid2; // gAcid2
407 PANEL_STEP: result := GridTagStep; // gSteps
408 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
409 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
410 else result := GridTagInvalid;
411 end;
412 end;
415 function dplLess (a, b: TObject): Boolean;
416 var
417 pa, pb: TPanel;
418 begin
419 pa := TPanel(a);
420 pb := TPanel(b);
421 if (pa.tag < pb.tag) then begin result := true; exit; end;
422 if (pa.tag > pb.tag) then begin result := false; exit; end;
423 result := (pa.arrIdx < pb.arrIdx);
424 end;
426 procedure dplClear ();
427 begin
428 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
429 end;
432 type
433 TPanelID = record
434 PWhere: ^TPanelArray;
435 PArrID: Integer;
436 end;
438 var
439 PanelById: array of TPanelID;
440 Textures: TLevelTextureArray = nil;
441 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
442 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
443 RespawnPoints: Array of TRespawnPoint;
444 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
445 //DOMFlagPoints: Array of TFlagPoint;
448 procedure g_Map_ProfilersBegin ();
449 begin
450 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
451 profMapCollision.mainBegin(g_profile_collision);
452 // create sections
453 if g_profile_collision then
454 begin
455 profMapCollision.sectionBegin('*solids');
456 profMapCollision.sectionEnd();
457 profMapCollision.sectionBegin('liquids');
458 profMapCollision.sectionEnd();
459 end;
460 end;
462 procedure g_Map_ProfilersEnd ();
463 begin
464 if (profMapCollision <> nil) then profMapCollision.mainEnd();
465 end;
468 // wall index in `gWalls` or -1
469 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
470 var
471 ex, ey: Integer;
472 begin
473 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
474 if (result <> nil) then
475 begin
476 if (hitx <> nil) then hitx^ := ex;
477 if (hity <> nil) then hity^ := ey;
478 end
479 else
480 begin
481 if (hitx <> nil) then hitx^ := x1;
482 if (hity <> nil) then hity^ := y1;
483 end;
484 end;
486 // returns panel or nil
487 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
488 var
489 ex, ey: Integer;
490 begin
491 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
492 if (result <> nil) then
493 begin
494 if (hitx <> nil) then hitx^ := ex;
495 if (hity <> nil) then hity^ := ey;
496 end
497 else
498 begin
499 if (hitx <> nil) then hitx^ := x1;
500 if (hity <> nil) then hity^ := y1;
501 end;
502 end;
505 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
507 function checker (pan: TPanel; tag: Integer): Boolean;
508 begin
510 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
511 begin
512 result := pan.Enabled; // stop if wall is enabled
513 exit;
514 end;
517 if ((tag and GridTagLift) <> 0) then
518 begin
519 // stop if the lift of the right type
520 result :=
521 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
522 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
523 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
524 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
525 exit;
526 end;
528 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
529 end;
531 var
532 tagmask: Integer = 0;
533 begin
534 result := false;
536 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
537 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
538 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
539 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
540 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
541 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
542 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
544 if (tagmask = 0) then exit;// just in case
545 if ((tagmask and GridTagLift) <> 0) then
546 begin
547 // slow
548 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
549 end
550 else
551 begin
552 // fast
553 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
554 end;
555 end;
558 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
559 begin
560 result := nil;
561 if (tagmask = 0) then exit;
562 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
563 end;
566 function g_Map_IsSpecialTexture(Texture: String): Boolean;
567 begin
568 Result := (Texture = TEXTURE_NAME_WATER) or
569 (Texture = TEXTURE_NAME_ACID1) or
570 (Texture = TEXTURE_NAME_ACID2);
571 end;
573 procedure CreateDoorMap();
574 var
575 PanelArray: Array of record
576 X, Y: Integer;
577 Width, Height: Word;
578 Active: Boolean;
579 PanelID: DWORD;
580 end;
581 a, b, c, m, i, len: Integer;
582 ok: Boolean;
583 begin
584 if gWalls = nil then
585 Exit;
587 i := 0;
588 len := 128;
589 SetLength(PanelArray, len);
591 for a := 0 to High(gWalls) do
592 if gWalls[a].Door then
593 begin
594 PanelArray[i].X := gWalls[a].X;
595 PanelArray[i].Y := gWalls[a].Y;
596 PanelArray[i].Width := gWalls[a].Width;
597 PanelArray[i].Height := gWalls[a].Height;
598 PanelArray[i].Active := True;
599 PanelArray[i].PanelID := a;
601 i := i + 1;
602 if i = len then
603 begin
604 len := len + 128;
605 SetLength(PanelArray, len);
606 end;
607 end;
609 // Íåò äâåðåé:
610 if i = 0 then
611 begin
612 PanelArray := nil;
613 Exit;
614 end;
616 SetLength(gDoorMap, 0);
618 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
620 for a := 0 to i-1 do
621 if PanelArray[a].Active then
622 begin
623 PanelArray[a].Active := False;
624 m := Length(gDoorMap);
625 SetLength(gDoorMap, m+1);
626 SetLength(gDoorMap[m], 1);
627 gDoorMap[m, 0] := PanelArray[a].PanelID;
628 ok := True;
630 while ok do
631 begin
632 ok := False;
634 for b := 0 to i-1 do
635 if PanelArray[b].Active then
636 for c := 0 to High(gDoorMap[m]) do
637 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
638 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
639 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
640 PanelArray[b].Width, PanelArray[b].Height,
641 gWalls[gDoorMap[m, c]].X,
642 gWalls[gDoorMap[m, c]].Y,
643 gWalls[gDoorMap[m, c]].Width,
644 gWalls[gDoorMap[m, c]].Height) then
645 begin
646 PanelArray[b].Active := False;
647 SetLength(gDoorMap[m],
648 Length(gDoorMap[m])+1);
649 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
650 ok := True;
651 Break;
652 end;
653 end;
655 g_Game_StepLoading();
656 end;
658 PanelArray := nil;
659 end;
661 procedure CreateLiftMap();
662 var
663 PanelArray: Array of record
664 X, Y: Integer;
665 Width, Height: Word;
666 Active: Boolean;
667 end;
668 a, b, c, len, i, j: Integer;
669 ok: Boolean;
670 begin
671 if gLifts = nil then
672 Exit;
674 len := Length(gLifts);
675 SetLength(PanelArray, len);
677 for a := 0 to len-1 do
678 begin
679 PanelArray[a].X := gLifts[a].X;
680 PanelArray[a].Y := gLifts[a].Y;
681 PanelArray[a].Width := gLifts[a].Width;
682 PanelArray[a].Height := gLifts[a].Height;
683 PanelArray[a].Active := True;
684 end;
686 SetLength(gLiftMap, len);
687 i := 0;
689 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
691 for a := 0 to len-1 do
692 if PanelArray[a].Active then
693 begin
694 PanelArray[a].Active := False;
695 SetLength(gLiftMap[i], 32);
696 j := 0;
697 gLiftMap[i, j] := a;
698 ok := True;
700 while ok do
701 begin
702 ok := False;
703 for b := 0 to len-1 do
704 if PanelArray[b].Active then
705 for c := 0 to j do
706 if g_CollideAround(PanelArray[b].X,
707 PanelArray[b].Y,
708 PanelArray[b].Width,
709 PanelArray[b].Height,
710 PanelArray[gLiftMap[i, c]].X,
711 PanelArray[gLiftMap[i, c]].Y,
712 PanelArray[gLiftMap[i, c]].Width,
713 PanelArray[gLiftMap[i, c]].Height) then
714 begin
715 PanelArray[b].Active := False;
716 j := j+1;
717 if j > High(gLiftMap[i]) then
718 SetLength(gLiftMap[i],
719 Length(gLiftMap[i])+32);
721 gLiftMap[i, j] := b;
722 ok := True;
724 Break;
725 end;
726 end;
728 SetLength(gLiftMap[i], j+1);
729 i := i+1;
731 g_Game_StepLoading();
732 end;
734 SetLength(gLiftMap, i);
736 PanelArray := nil;
737 end;
739 function CreatePanel(PanelRec: TDynRecord; AddTextures: TAddTextureArray;
740 CurTex: Integer; sav: Boolean): Integer;
741 var
742 len: Integer;
743 panels: ^TPanelArray;
744 begin
745 Result := -1;
747 case PanelRec.PanelType of
748 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
749 panels := @gWalls;
750 PANEL_BACK:
751 panels := @gRenderBackgrounds;
752 PANEL_FORE:
753 panels := @gRenderForegrounds;
754 PANEL_WATER:
755 panels := @gWater;
756 PANEL_ACID1:
757 panels := @gAcid1;
758 PANEL_ACID2:
759 panels := @gAcid2;
760 PANEL_STEP:
761 panels := @gSteps;
762 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
763 panels := @gLifts;
764 PANEL_BLOCKMON:
765 panels := @gBlockMon;
766 else
767 Exit;
768 end;
770 len := Length(panels^);
771 SetLength(panels^, len + 1);
773 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
774 panels^[len].arrIdx := len;
775 panels^[len].proxyId := -1;
776 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
777 if sav then
778 panels^[len].SaveIt := True;
780 Result := len;
782 len := Length(PanelByID);
783 SetLength(PanelByID, len + 1);
784 PanelByID[len].PWhere := panels;
785 PanelByID[len].PArrID := Result;
786 end;
789 function CreateNullTexture(RecName: String): Integer;
790 begin
791 RecName := toLowerCase1251(RecName);
792 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
793 if TextNameHash.get(RecName, result) then exit; // i found her!
795 SetLength(Textures, Length(Textures)+1);
796 result := High(Textures);
798 with Textures[High(Textures)] do
799 begin
800 TextureName := RecName;
801 Width := 1;
802 Height := 1;
803 Anim := False;
804 TextureID := LongWord(TEXTURE_NONE);
805 end;
807 TextNameHash.put(RecName, result);
808 end;
811 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
812 var
813 WAD: TWADFile;
814 TextureData: Pointer;
815 WADName: String;
816 a, ResLength: Integer;
817 begin
818 RecName := toLowerCase1251(RecName);
819 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
820 if TextNameHash.get(RecName, result) then
821 begin
822 // i found her!
823 //e_LogWritefln('texture ''%s'' already loaded', [RecName]);
824 exit;
825 end;
827 Result := -1;
829 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
832 if Textures <> nil then
833 begin
834 for a := 0 to High(Textures) do
835 begin
836 if (Textures[a].TextureName = RecName) then
837 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
838 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
839 Result := a;
840 Exit;
841 end;
842 end;
843 end;
846 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
847 if (RecName = TEXTURE_NAME_WATER) or
848 (RecName = TEXTURE_NAME_ACID1) or
849 (RecName = TEXTURE_NAME_ACID2) then
850 begin
851 SetLength(Textures, Length(Textures)+1);
853 with Textures[High(Textures)] do
854 begin
855 TextureName := RecName;
856 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
857 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
858 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
860 Anim := False;
861 end;
863 result := High(Textures);
864 TextNameHash.put(RecName, result);
865 Exit;
866 end;
868 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
869 WADName := g_ExtractWadName(RecName);
871 WAD := TWADFile.Create();
873 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
875 WAD.ReadFile(WADName);
877 //txname := RecName;
879 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
880 begin
881 FreeMem(TextureData);
882 RecName := 'COMMON\ALIEN';
883 end;
886 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
887 begin
888 SetLength(Textures, Length(Textures)+1);
889 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
890 begin
891 SetLength(Textures, Length(Textures)-1);
892 Exit;
893 end;
894 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
895 FreeMem(TextureData);
896 Textures[High(Textures)].TextureName := RecName;
897 Textures[High(Textures)].Anim := False;
899 result := High(Textures);
900 TextNameHash.put(RecName, result);
901 end
902 else // Íåò òàêîãî ðåóñðñà â WAD'å
903 begin
904 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
905 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
906 if log and (not BadTextNameHash.get(RecName, a)) then
907 begin
908 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
909 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
910 end;
911 BadTextNameHash.put(RecName, -1);
912 end;
914 WAD.Free();
915 end;
918 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
919 var
920 WAD: TWADFile;
921 TextureWAD: PChar = nil;
922 TextData: Pointer = nil;
923 TextureData: Pointer = nil;
924 cfg: TConfig = nil;
925 WADName: String;
926 ResLength: Integer;
927 TextureResource: String;
928 _width, _height, _framecount, _speed: Integer;
929 _backanimation: Boolean;
930 //imgfmt: string;
931 ia: TDynImageDataArray = nil;
932 f, c, frdelay, frloop: Integer;
933 begin
934 RecName := toLowerCase1251(RecName);
935 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
936 if TextNameHash.get(RecName, result) then
937 begin
938 // i found her!
939 //e_LogWritefln('animated texture ''%s'' already loaded', [RecName]);
940 exit;
941 end;
943 result := -1;
945 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
947 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
948 if BadTextNameHash.get(RecName, f) then
949 begin
950 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
951 exit;
952 end;
954 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
955 WADName := g_ExtractWadName(RecName);
957 WAD := TWADFile.Create();
958 try
959 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
961 WAD.ReadFile(WADName);
963 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
964 begin
965 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
966 if log and (not BadTextNameHash.get(RecName, f)) then
967 begin
968 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
969 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
970 end;
971 BadTextNameHash.put(RecName, -1);
972 exit;
973 end;
975 {TEST
976 if WADName = Map then
977 begin
978 //FreeMem(TextureWAD);
979 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
980 end;
983 WAD.FreeWAD();
985 if ResLength < 6 then
986 begin
987 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
988 BadTextNameHash.put(RecName, -1);
989 exit;
990 end;
992 // ýòî ïòèöà? ýòî ñàìîë¸ò?
993 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
994 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
995 begin
996 // íåò, ýòî ñóïåðìåí!
997 if not WAD.ReadMemory(TextureWAD, ResLength) then
998 begin
999 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
1000 BadTextNameHash.put(RecName, -1);
1001 exit;
1002 end;
1004 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1005 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1006 begin
1007 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
1008 BadTextNameHash.put(RecName, -1);
1009 exit;
1010 end;
1012 cfg := TConfig.CreateMem(TextData, ResLength);
1014 TextureResource := cfg.ReadStr('', 'resource', '');
1015 if TextureResource = '' then
1016 begin
1017 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
1018 BadTextNameHash.put(RecName, -1);
1019 exit;
1020 end;
1022 _width := cfg.ReadInt('', 'framewidth', 0);
1023 _height := cfg.ReadInt('', 'frameheight', 0);
1024 _framecount := cfg.ReadInt('', 'framecount', 0);
1025 _speed := cfg.ReadInt('', 'waitcount', 0);
1026 _backanimation := cfg.ReadBool('', 'backanimation', False);
1028 cfg.Free();
1029 cfg := nil;
1031 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1032 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1033 begin
1034 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
1035 BadTextNameHash.put(RecName, -1);
1036 exit;
1037 end;
1039 WAD.Free();
1040 WAD := nil;
1042 SetLength(Textures, Length(Textures)+1);
1043 with Textures[High(Textures)] do
1044 begin
1045 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1046 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1047 begin
1048 TextureName := RecName;
1049 Width := _width;
1050 Height := _height;
1051 Anim := True;
1052 FramesCount := _framecount;
1053 Speed := _speed;
1054 result := High(Textures);
1055 TextNameHash.put(RecName, result);
1056 end
1057 else
1058 begin
1059 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1060 if log and (not BadTextNameHash.get(RecName, f)) then
1061 begin
1062 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1063 end;
1064 BadTextNameHash.put(RecName, -1);
1065 end;
1066 end;
1067 end
1068 else
1069 begin
1070 // try animated image
1072 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1073 if length(imgfmt) = 0 then
1074 begin
1075 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1076 exit;
1077 end;
1079 GlobalMetadata.ClearMetaItems();
1080 GlobalMetadata.ClearMetaItemsForSaving();
1081 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1082 begin
1083 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
1084 BadTextNameHash.put(RecName, -1);
1085 exit;
1086 end;
1087 if length(ia) = 0 then
1088 begin
1089 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
1090 BadTextNameHash.put(RecName, -1);
1091 exit;
1092 end;
1094 WAD.Free();
1095 WAD := nil;
1097 _width := ia[0].width;
1098 _height := ia[0].height;
1099 _framecount := length(ia);
1100 _speed := 1;
1101 _backanimation := false;
1102 frdelay := -1;
1103 frloop := -666;
1104 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1105 begin
1106 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1107 try
1108 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1109 frdelay := f;
1110 if f < 0 then f := 0;
1111 // rounding ;-)
1112 c := f mod 28;
1113 if c < 13 then c := 0 else c := 1;
1114 f := (f div 28)+c;
1115 if f < 1 then f := 1 else if f > 255 then f := 255;
1116 _speed := f;
1117 except
1118 end;
1119 end;
1120 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1121 begin
1122 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1123 try
1124 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1125 frloop := f;
1126 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1127 except
1128 end;
1129 end;
1130 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1131 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1132 f := ord(_backanimation);
1133 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);
1135 SetLength(Textures, Length(Textures)+1);
1136 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1137 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1138 begin
1139 Textures[High(Textures)].TextureName := RecName;
1140 Textures[High(Textures)].Width := _width;
1141 Textures[High(Textures)].Height := _height;
1142 Textures[High(Textures)].Anim := True;
1143 Textures[High(Textures)].FramesCount := length(ia);
1144 Textures[High(Textures)].Speed := _speed;
1145 result := High(Textures);
1146 TextNameHash.put(RecName, result);
1147 //writeln(' CREATED!');
1148 end
1149 else
1150 begin
1151 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1152 if log and (not BadTextNameHash.get(RecName, f)) then
1153 begin
1154 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
1155 end;
1156 BadTextNameHash.put(RecName, -1);
1157 end;
1158 end;
1159 finally
1160 for f := 0 to High(ia) do FreeImage(ia[f]);
1161 WAD.Free();
1162 cfg.Free();
1163 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1164 if (TextData <> nil) then FreeMem(TextData);
1165 if (TextureData <> nil) then FreeMem(TextureData);
1166 end;
1167 end;
1169 procedure CreateItem(Item: TDynRecord);
1170 begin
1171 if g_Game_IsClient then Exit;
1173 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1174 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1175 Exit;
1177 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1178 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1179 end;
1181 procedure CreateArea(Area: TDynRecord);
1182 var
1183 a: Integer;
1184 id: DWORD = 0;
1185 begin
1186 case Area.AreaType of
1187 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1188 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1189 begin
1190 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1191 with RespawnPoints[High(RespawnPoints)] do
1192 begin
1193 X := Area.X;
1194 Y := Area.Y;
1195 Direction := TDirection(Area.Direction);
1197 case Area.AreaType of
1198 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1199 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1200 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1201 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1202 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1203 end;
1204 end;
1205 end;
1207 AREA_REDFLAG, AREA_BLUEFLAG:
1208 begin
1209 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1211 if FlagPoints[a] <> nil then Exit;
1213 New(FlagPoints[a]);
1215 with FlagPoints[a]^ do
1216 begin
1217 X := Area.X-FLAGRECT.X;
1218 Y := Area.Y-FLAGRECT.Y;
1219 Direction := TDirection(Area.Direction);
1220 end;
1222 with gFlags[a] do
1223 begin
1224 case a of
1225 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1226 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1227 end;
1229 Animation := TAnimation.Create(id, True, 8);
1230 Obj.Rect := FLAGRECT;
1232 g_Map_ResetFlag(a);
1233 end;
1234 end;
1236 AREA_DOMFLAG:
1237 begin
1238 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1239 with DOMFlagPoints[High(DOMFlagPoints)] do
1240 begin
1241 X := Area.X;
1242 Y := Area.Y;
1243 Direction := TDirection(Area.Direction);
1244 end;
1246 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1247 end;
1248 end;
1249 end;
1251 procedure CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer; fTexturePanel1Type, fTexturePanel2Type: Word);
1252 var
1253 _trigger: TTrigger;
1254 begin
1255 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1257 with _trigger do
1258 begin
1259 mapId := Trigger.id;
1260 mapIndex := amapIdx;
1261 X := Trigger.X;
1262 Y := Trigger.Y;
1263 Width := Trigger.Width;
1264 Height := Trigger.Height;
1265 Enabled := Trigger.Enabled;
1266 //TexturePanel := Trigger.TexturePanel;
1267 TexturePanel := atpanid;
1268 TexturePanelType := fTexturePanel1Type;
1269 ShotPanelType := fTexturePanel2Type;
1270 TriggerType := Trigger.TriggerType;
1271 ActivateType := Trigger.ActivateType;
1272 Keys := Trigger.Keys;
1273 trigPanelId := atrigpanid;
1274 //trigShotPanelId := ashotpanid;
1275 //Data.Default := Trigger.DATA;
1276 if (Trigger.trigRec = nil) then
1277 begin
1278 trigData := nil;
1279 if (TriggerType <> TRIGGER_SECRET) then
1280 begin
1281 e_LogWritefln('trigger of type %s has no triggerdata; wtf?!', [TriggerType], MSG_WARNING);
1282 end;
1283 end
1284 else
1285 begin
1286 trigData := Trigger.trigRec.clone();
1287 end;
1288 end;
1290 g_Triggers_Create(_trigger);
1291 end;
1293 procedure CreateMonster(monster: TDynRecord);
1294 var
1295 a: Integer;
1296 mon: TMonster;
1297 begin
1298 if g_Game_IsClient then Exit;
1300 if (gGameSettings.GameType = GT_SINGLE)
1301 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1302 begin
1303 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1305 if gTriggers <> nil then
1306 begin
1307 for a := 0 to High(gTriggers) do
1308 begin
1309 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1310 begin
1311 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1312 if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1313 end;
1314 end;
1315 end;
1317 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1318 end;
1319 end;
1321 procedure g_Map_ReAdd_DieTriggers();
1323 function monsDieTrig (mon: TMonster): Boolean;
1324 var
1325 a: Integer;
1326 //tw: TStrTextWriter;
1327 begin
1328 result := false; // don't stop
1329 mon.ClearTriggers();
1330 for a := 0 to High(gTriggers) do
1331 begin
1332 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1333 begin
1334 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1336 tw := TStrTextWriter.Create();
1337 try
1338 gTriggers[a].trigData.writeTo(tw);
1339 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1340 finally
1341 tw.Free();
1342 end;
1344 if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1345 end;
1346 end;
1347 end;
1349 begin
1350 if g_Game_IsClient then Exit;
1352 g_Mons_ForEach(monsDieTrig);
1353 end;
1355 function extractWadName(resourceName: string): string;
1356 var
1357 posN: Integer;
1358 begin
1359 posN := Pos(':', resourceName);
1360 if posN > 0 then
1361 Result:= Copy(resourceName, 0, posN-1)
1362 else
1363 Result := '';
1364 end;
1366 procedure addResToExternalResList(res: string);
1367 begin
1368 res := extractWadName(res);
1369 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1370 gExternalResources.Add(res);
1371 end;
1373 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1374 //var
1375 //textures: TTexturesRec1Array;
1376 //textures: TDynField;
1377 //trec: TDynRecord;
1378 //mapHeader: TMapHeaderRec_1;
1379 //i: integer;
1380 //resFile: String = '';
1381 begin
1382 if gExternalResources = nil then
1383 gExternalResources := TStringList.Create;
1385 gExternalResources.Clear;
1387 (*
1389 textures := GetTextures(map);
1390 for i := 0 to High(textures) do
1391 begin
1392 addResToExternalResList(resFile);
1393 end;
1396 textures := map['texture'];
1397 if (textures <> nil) then
1398 begin
1399 for trec in textures do
1400 begin
1401 addResToExternalResList(resFile);
1402 end;
1403 end;
1405 textures := nil;
1406 *)
1408 //mapHeader := GetMapHeader(map);
1410 addResToExternalResList(map.MusicName);
1411 addResToExternalResList(map.SkyName);
1412 end;
1415 procedure mapCreateGrid ();
1416 var
1417 mapX0: Integer = $3fffffff;
1418 mapY0: Integer = $3fffffff;
1419 mapX1: Integer = -$3fffffff;
1420 mapY1: Integer = -$3fffffff;
1422 procedure calcBoundingBox (constref panels: TPanelArray);
1423 var
1424 idx: Integer;
1425 pan: TPanel;
1426 begin
1427 for idx := 0 to High(panels) do
1428 begin
1429 pan := panels[idx];
1430 if not pan.visvalid then continue;
1431 if (pan.Width < 1) or (pan.Height < 1) then continue;
1432 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1433 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1434 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1435 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1436 end;
1437 end;
1439 procedure addPanelsToGrid (constref panels: TPanelArray);
1440 var
1441 idx: Integer;
1442 pan: TPanel;
1443 newtag: Integer;
1444 begin
1445 //tag := panelTypeToTag(tag);
1446 for idx := 0 to High(panels) do
1447 begin
1448 pan := panels[idx];
1449 if not pan.visvalid then continue;
1450 if (pan.proxyId <> -1) then
1451 begin
1452 {$IF DEFINED(D2F_DEBUG)}
1453 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);
1454 {$ENDIF}
1455 continue;
1456 end;
1457 case pan.PanelType of
1458 PANEL_WALL: newtag := GridTagWall;
1459 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1460 PANEL_BACK: newtag := GridTagBack;
1461 PANEL_FORE: newtag := GridTagFore;
1462 PANEL_WATER: newtag := GridTagWater;
1463 PANEL_ACID1: newtag := GridTagAcid1;
1464 PANEL_ACID2: newtag := GridTagAcid2;
1465 PANEL_STEP: newtag := GridTagStep;
1466 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1467 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1468 else continue; // oops
1469 end;
1470 pan.tag := newtag;
1472 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1473 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1474 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1475 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1477 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1478 begin
1479 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1480 end;
1482 {$ENDIF}
1483 end;
1484 end;
1486 begin
1487 mapGrid.Free();
1488 mapGrid := nil;
1490 calcBoundingBox(gWalls);
1491 calcBoundingBox(gRenderBackgrounds);
1492 calcBoundingBox(gRenderForegrounds);
1493 calcBoundingBox(gWater);
1494 calcBoundingBox(gAcid1);
1495 calcBoundingBox(gAcid2);
1496 calcBoundingBox(gSteps);
1497 calcBoundingBox(gLifts);
1498 calcBoundingBox(gBlockMon);
1500 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1502 if (mapX0 > 0) then mapX0 := 0;
1503 if (mapY0 > 0) then mapY0 := 0;
1505 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1506 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1508 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1509 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1511 addPanelsToGrid(gWalls);
1512 addPanelsToGrid(gRenderBackgrounds);
1513 addPanelsToGrid(gRenderForegrounds);
1514 addPanelsToGrid(gWater);
1515 addPanelsToGrid(gAcid1);
1516 addPanelsToGrid(gAcid2);
1517 addPanelsToGrid(gSteps);
1518 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1519 addPanelsToGrid(gBlockMon);
1521 mapGrid.dumpStats();
1523 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1524 end;
1527 function g_Map_Load(Res: String): Boolean;
1528 const
1529 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1530 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1531 type
1532 PTRec = ^TTRec;
1533 TTRec = record
1534 //TexturePanel: Integer;
1535 texPanIdx: Integer;
1536 LiftPanelIdx: Integer;
1537 DoorPanelIdx: Integer;
1538 ShotPanelIdx: Integer;
1539 trigrec: TDynRecord;
1540 texPan: TDynRecord;
1541 liftPan: TDynRecord;
1542 doorPan: TDynRecord;
1543 shotPan: TDynRecord;
1544 end;
1545 var
1546 WAD: TWADFile;
1547 mapReader: TDynRecord = nil;
1548 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1549 panels: TDynField = nil; //TPanelsRec1Array;
1550 items: TDynField = nil; //TItemsRec1Array;
1551 monsters: TDynField = nil; //TMonsterRec1Array;
1552 areas: TDynField = nil; //TAreasRec1Array;
1553 triggers: TDynField = nil; //TTriggersRec1Array;
1554 b, c, k: Integer;
1555 PanelID: DWORD;
1556 AddTextures: TAddTextureArray;
1557 TriggersTable: array of TTRec;
1558 FileName, mapResName, s, TexName: String;
1559 Data: Pointer;
1560 Len: Integer;
1561 ok, isAnim, trigRef: Boolean;
1562 CurTex, ntn: Integer;
1563 rec, texrec: TDynRecord;
1564 pttit: PTRec;
1565 pannum, trignum, cnt, tgpid: Integer;
1566 stt: UInt64;
1567 begin
1568 mapGrid.Free();
1569 mapGrid := nil;
1571 gCurrentMap.Free();
1572 gCurrentMap := nil;
1574 Result := False;
1575 gMapInfo.Map := Res;
1576 TriggersTable := nil;
1577 mapReader := nil;
1579 sfsGCDisable(); // temporary disable removing of temporary volumes
1580 try
1581 // Çàãðóçêà WAD:
1582 FileName := g_ExtractWadName(Res);
1583 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1584 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1586 WAD := TWADFile.Create();
1587 if not WAD.ReadFile(FileName) then
1588 begin
1589 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1590 WAD.Free();
1591 Exit;
1592 end;
1594 //k8: why loader ignores path here?
1595 mapResName := g_ExtractFileName(Res);
1596 if not WAD.GetMapResource(mapResName, Data, Len) then
1597 begin
1598 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1599 WAD.Free();
1600 Exit;
1601 end;
1603 WAD.Free();
1605 if (Len < 4) then
1606 begin
1607 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1608 FreeMem(Data);
1609 exit;
1610 end;
1612 // Çàãðóçêà êàðòû:
1613 e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
1614 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1616 stt := curTimeMicro();
1618 try
1619 mapReader := g_Map_ParseMap(Data, Len);
1620 except
1621 mapReader.Free();
1622 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1623 FreeMem(Data);
1624 Exit;
1625 end;
1627 FreeMem(Data);
1629 generateExternalResourcesList(mapReader);
1630 mapTextureList := mapReader['texture'];
1631 // get all other lists here too
1632 panels := mapReader['panel'];
1633 triggers := mapReader['trigger'];
1634 items := mapReader['item'];
1635 areas := mapReader['area'];
1636 monsters := mapReader['monster'];
1638 // Çàãðóçêà îïèñàíèÿ êàðòû:
1639 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1640 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1642 with gMapInfo do
1643 begin
1644 Name := mapReader.MapName;
1645 Description := mapReader.MapDesc;
1646 Author := mapReader.MapAuthor;
1647 MusicName := mapReader.MusicName;
1648 SkyName := mapReader.SkyName;
1649 Height := mapReader.Height;
1650 Width := mapReader.Width;
1651 end;
1653 // Çàãðóçêà òåêñòóð:
1654 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1655 // Äîáàâëåíèå òåêñòóð â Textures[]:
1656 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1657 begin
1658 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1659 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1661 cnt := -1;
1662 for rec in mapTextureList do
1663 begin
1664 Inc(cnt);
1665 s := rec.Resource;
1666 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1667 e_WriteLog(Format(' Loading texture #%d: %s', [cnt, s]), MSG_NOTIFY);
1668 {$ENDIF}
1669 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1670 if rec.Anim then
1671 begin
1672 // Àíèìèðîâàííàÿ òåêñòóðà
1673 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1674 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1675 end
1676 else
1677 begin
1678 // Îáû÷íàÿ òåêñòóðà
1679 ntn := CreateTexture(rec.Resource, FileName, True);
1680 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1681 end;
1682 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1684 rec.tagInt := ntn; // remember texture number
1685 g_Game_StepLoading();
1686 end;
1688 // set panel tagInt to texture index
1689 if (panels <> nil) then
1690 begin
1691 for rec in panels do
1692 begin
1693 texrec := rec.TextureRec;
1694 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1695 end;
1696 end;
1697 end;
1699 // Çàãðóçêà òðèããåðîâ
1700 gTriggerClientID := 0;
1701 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1702 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1704 // Çàãðóçêà ïàíåëåé
1705 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1706 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1708 // check texture numbers for panels
1709 if (panels <> nil) and (panels.count > 0) then
1710 begin
1711 for rec in panels do
1712 begin
1713 if (rec.tagInt < 0) then
1714 begin
1715 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1716 result := false;
1717 exit;
1718 end;
1719 end;
1720 end;
1722 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1723 if (triggers <> nil) and (triggers.count > 0) then
1724 begin
1725 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1726 //SetLength(TriggersTable, triggers.count);
1727 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1729 for rec in triggers do
1730 begin
1731 SetLength(TriggersTable, Length(TriggersTable)+1);
1732 pttit := @TriggersTable[High(TriggersTable)];
1733 pttit.trigrec := rec;
1734 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1735 pttit.texPan := mapReader.panel[rec.TexturePanel];
1736 pttit.liftPan := nil;
1737 pttit.doorPan := nil;
1738 pttit.shotPan := nil;
1739 pttit.texPanIdx := -1;
1740 pttit.LiftPanelIdx := -1;
1741 pttit.DoorPanelIdx := -1;
1742 pttit.ShotPanelIdx := -1;
1743 // Ëèôòû
1744 if rec.TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1745 begin
1746 pttit.liftPan := mapReader.panel[rec.trigRec.tgPanelID];
1747 end;
1748 // Äâåðè
1749 if rec.TriggerType in [TRIGGER_OPENDOOR, TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5, TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1750 begin
1751 pttit.doorPan := mapReader.panel[rec.trigRec.tgPanelID];
1752 end;
1753 // Òóðåëü
1754 if (rec.TriggerType = TRIGGER_SHOT) then
1755 begin
1756 pttit.shotPan := mapReader.panel[rec.trigRec.tgShotPanelID];
1757 end;
1759 if (pttit.texPan <> nil) then pttit.texPan.userPanelTrigRef := true;
1760 if (pttit.liftPan <> nil) then pttit.liftPan.userPanelTrigRef := true;
1761 if (pttit.doorPan <> nil) then pttit.doorPan.userPanelTrigRef := true;
1762 if (pttit.shotPan <> nil) then pttit.shotPan.userPanelTrigRef := true;
1764 g_Game_StepLoading();
1765 end;
1766 end;
1768 // Ñîçäàåì ïàíåëè
1769 if (panels <> nil) and (panels.count > 0) then
1770 begin
1771 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1772 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1774 pannum := -1;
1775 for rec in panels do
1776 begin
1777 Inc(pannum);
1778 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1779 texrec := nil;
1780 SetLength(AddTextures, 0);
1781 trigRef := False;
1782 CurTex := -1;
1783 ok := false;
1785 if (mapTextureList <> nil) then
1786 begin
1787 texrec := rec.TextureRec;
1788 ok := (texrec <> nil);
1789 end;
1791 if ok then
1792 begin
1793 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1794 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1795 ok := false;
1796 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1797 begin
1799 for b := 0 to High(TriggersTable) do
1800 begin
1801 if (TriggersTable[b].texPan = rec) or (TriggersTable[b].shotPan = rec) then
1802 begin
1803 trigRef := True;
1804 ok := True;
1805 break;
1806 end;
1807 end;
1809 if rec.userPanelTrigRef then
1810 begin
1811 // e_LogWritefln('trigref for panel %s', [pannum]);
1812 trigRef := True;
1813 ok := True;
1814 end;
1815 end;
1816 end;
1818 if ok then
1819 begin
1820 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1821 s := texrec.Resource;
1823 // Ñïåö-òåêñòóðû çàïðåùåíû
1824 if g_Map_IsSpecialTexture(s) then
1825 begin
1826 ok := false
1827 end
1828 else
1829 begin
1830 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1831 ok := g_Texture_NumNameFindStart(s);
1832 end;
1834 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1835 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1836 if ok then
1837 begin
1838 k := NNF_NAME_BEFORE;
1839 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1840 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1841 begin
1842 k := g_Texture_NumNameFindNext(TexName);
1844 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1845 begin
1846 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1847 if texrec.Anim then
1848 begin
1849 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1850 isAnim := True;
1851 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1852 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1853 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1854 if not ok then
1855 begin
1856 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1857 isAnim := False;
1858 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1859 ok := CreateTexture(TexName, FileName, False) >= 0;
1860 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1861 end;
1862 end
1863 else
1864 begin
1865 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1866 isAnim := False;
1867 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1868 ok := CreateTexture(TexName, FileName, False) >= 0;
1869 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1870 if not ok then
1871 begin
1872 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1873 isAnim := True;
1874 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1875 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1876 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1877 end;
1878 end;
1880 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1881 if ok then
1882 begin
1884 for c := 0 to High(Textures) do
1885 begin
1886 if (Textures[c].TextureName = TexName) then
1887 begin
1888 SetLength(AddTextures, Length(AddTextures)+1);
1889 AddTextures[High(AddTextures)].Texture := c;
1890 AddTextures[High(AddTextures)].Anim := isAnim;
1891 break;
1892 end;
1893 end;
1895 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1896 begin
1897 SetLength(AddTextures, Length(AddTextures)+1);
1898 AddTextures[High(AddTextures)].Texture := c;
1899 AddTextures[High(AddTextures)].Anim := isAnim;
1900 end;
1901 end;
1902 end
1903 else
1904 begin
1905 if k = NNF_NAME_EQUALS then
1906 begin
1907 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1908 SetLength(AddTextures, Length(AddTextures)+1);
1909 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1910 AddTextures[High(AddTextures)].Anim := texrec.Anim;
1911 CurTex := High(AddTextures);
1912 ok := true;
1913 end
1914 else // NNF_NO_NAME
1915 begin
1916 ok := false;
1917 end;
1918 end;
1919 end; // while ok...
1921 ok := true;
1922 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1923 end; // if ok - ññûëàþòñÿ òðèããåðû
1925 if not ok then
1926 begin
1927 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1928 SetLength(AddTextures, 1);
1929 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1930 AddTextures[0].Anim := false;
1931 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
1932 CurTex := 0;
1933 end;
1935 //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);
1937 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1939 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð
1940 PanelID := CreatePanel(rec, AddTextures, CurTex, trigRef);
1941 //e_LogWritefln('panel #%s of type %s got id #%s', [pannum, rec.PanelType, PanelID]);
1942 // set 'gamePanelId' field to panel id
1943 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1945 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
1947 g_Game_StepLoading();
1948 end;
1949 end;
1951 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
1952 for b := 0 to High(TriggersTable) do
1953 begin
1954 if (TriggersTable[b].texPan <> nil) then TriggersTable[b].texPanIdx := TriggersTable[b].texPan.userPanelId;
1955 if (TriggersTable[b].liftPan <> nil) then TriggersTable[b].LiftPanelIdx := TriggersTable[b].liftPan.userPanelId;
1956 if (TriggersTable[b].doorPan <> nil) then TriggersTable[b].DoorPanelIdx := TriggersTable[b].doorPan.userPanelId;
1957 if (TriggersTable[b].shotPan <> nil) then TriggersTable[b].ShotPanelIdx := TriggersTable[b].shotPan.userPanelId;
1958 end;
1960 // create map grid, init other grids (for monsters, for example)
1961 e_WriteLog('Creating map grid', MSG_NOTIFY);
1962 mapCreateGrid();
1964 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
1965 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
1966 begin
1967 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
1968 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1969 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
1970 trignum := -1;
1971 for rec in triggers do
1972 begin
1973 Inc(trignum);
1974 if (TriggersTable[trignum].texPan <> nil) then b := TriggersTable[trignum].texPan.PanelType else b := 0;
1975 if (TriggersTable[trignum].shotPan <> nil) then c := TriggersTable[trignum].shotPan.PanelType else c := 0;
1976 // we can have only one of those
1977 if (TriggersTable[trignum].LiftPanelIdx <> -1) then tgpid := TriggersTable[trignum].LiftPanelIdx
1978 else if (TriggersTable[trignum].DoorPanelIdx <> -1) then tgpid := TriggersTable[trignum].DoorPanelIdx
1979 else if (TriggersTable[trignum].ShotPanelIdx <> -1) then tgpid := TriggersTable[trignum].ShotPanelIdx
1980 else tgpid := -1;
1981 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
1982 CreateTrigger(trignum, rec, TriggersTable[trignum].texPanIdx, tgpid, Word(b), Word(c));
1983 end;
1984 end;
1986 // Çàãðóçêà ïðåäìåòîâ
1987 e_WriteLog(' Loading items...', MSG_NOTIFY);
1988 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1990 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
1991 if (items <> nil) and not gLoadGameMode then
1992 begin
1993 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1994 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1995 for rec in items do CreateItem(rec);
1996 end;
1998 // Çàãðóçêà îáëàñòåé
1999 e_WriteLog(' Loading areas...', MSG_NOTIFY);
2000 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2002 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2003 if areas <> nil then
2004 begin
2005 e_WriteLog(' Creating areas...', MSG_NOTIFY);
2006 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2007 for rec in areas do CreateArea(rec);
2008 end;
2010 // Çàãðóçêà ìîíñòðîâ
2011 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
2012 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2014 gTotalMonsters := 0;
2016 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2017 if (monsters <> nil) and not gLoadGameMode then
2018 begin
2019 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
2020 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2021 for rec in monsters do CreateMonster(rec);
2022 end;
2024 gCurrentMap := mapReader; // this will be our current map now
2025 gCurrentMapFileName := Res;
2026 mapReader := nil;
2028 // Çàãðóçêà íåáà
2029 if gMapInfo.SkyName <> '' then
2030 begin
2031 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
2032 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2033 FileName := g_ExtractWadName(gMapInfo.SkyName);
2035 if FileName <> '' then
2036 FileName := GameDir+'/wads/'+FileName
2037 else
2038 begin
2039 FileName := g_ExtractWadName(Res);
2040 end;
2042 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2043 if g_Texture_CreateWAD(BackID, s) then
2044 begin
2045 g_Game_SetupScreenSize();
2046 end
2047 else
2048 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2049 end;
2051 // Çàãðóçêà ìóçûêè
2052 ok := False;
2053 if gMapInfo.MusicName <> '' then
2054 begin
2055 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
2056 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2057 FileName := g_ExtractWadName(gMapInfo.MusicName);
2059 if FileName <> '' then
2060 FileName := GameDir+'/wads/'+FileName
2061 else
2062 begin
2063 FileName := g_ExtractWadName(Res);
2064 end;
2066 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2067 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2068 ok := True
2069 else
2070 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2071 end;
2073 // Îñòàëüíûå óñòàíâêè
2074 CreateDoorMap();
2075 CreateLiftMap();
2077 g_Items_Init();
2078 g_Weapon_Init();
2079 g_Monsters_Init();
2081 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2082 if not gLoadGameMode then g_GFX_Init();
2084 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2085 mapTextureList := nil;
2086 panels := nil;
2087 items := nil;
2088 areas := nil;
2089 triggers := nil;
2090 TriggersTable := nil;
2091 AddTextures := nil;
2093 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2094 if ok and (not gLoadGameMode) then
2095 begin
2096 gMusic.SetByName(gMapInfo.MusicName);
2097 gMusic.Play();
2098 end
2099 else
2100 begin
2101 gMusic.SetByName('');
2102 end;
2104 stt := curTimeMicro()-stt;
2105 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2106 finally
2107 sfsGCEnable(); // enable releasing unused volumes
2108 mapReader.Free();
2109 e_ClearInputBuffer(); // why not?
2110 end;
2112 e_WriteLog('Done loading map.', MSG_NOTIFY);
2113 Result := True;
2114 end;
2117 function g_Map_GetMapInfo(Res: String): TMapInfo;
2118 var
2119 WAD: TWADFile;
2120 mapReader: TDynRecord;
2121 //Header: TMapHeaderRec_1;
2122 FileName: String;
2123 Data: Pointer;
2124 Len: Integer;
2125 begin
2126 FillChar(Result, SizeOf(Result), 0);
2127 FileName := g_ExtractWadName(Res);
2129 WAD := TWADFile.Create();
2130 if not WAD.ReadFile(FileName) then
2131 begin
2132 WAD.Free();
2133 Exit;
2134 end;
2136 //k8: it ignores path again
2137 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2138 begin
2139 WAD.Free();
2140 Exit;
2141 end;
2143 WAD.Free();
2145 try
2146 mapReader := g_Map_ParseMap(Data, Len);
2147 except
2148 mapReader := nil;
2149 end;
2151 FreeMem(Data);
2153 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2154 begin
2155 Result.Name := mapReader.MapName;
2156 Result.Description := mapReader.MapDesc;
2157 Result.Map := Res;
2158 Result.Author := mapReader.MapAuthor;
2159 Result.Height := mapReader.Height;
2160 Result.Width := mapReader.Width;
2161 end
2162 else
2163 begin
2164 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2165 //ZeroMemory(@Header, SizeOf(Header));
2166 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2167 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2168 Result.Map := Res;
2169 Result.Author := '';
2170 Result.Height := 0;
2171 Result.Width := 0;
2172 end;
2174 mapReader.Free();
2175 end;
2177 function g_Map_GetMapsList(WADName: string): SArray;
2178 var
2179 WAD: TWADFile;
2180 a: Integer;
2181 ResList: SArray;
2182 begin
2183 Result := nil;
2184 WAD := TWADFile.Create();
2185 if not WAD.ReadFile(WADName) then
2186 begin
2187 WAD.Free();
2188 Exit;
2189 end;
2190 ResList := WAD.GetMapResources();
2191 if ResList <> nil then
2192 begin
2193 for a := 0 to High(ResList) do
2194 begin
2195 SetLength(Result, Length(Result)+1);
2196 Result[High(Result)] := ResList[a];
2197 end;
2198 end;
2199 WAD.Free();
2200 end;
2202 function g_Map_Exist(Res: string): Boolean;
2203 var
2204 WAD: TWADFile;
2205 FileName, mnn: string;
2206 ResList: SArray;
2207 a: Integer;
2208 begin
2209 Result := False;
2211 FileName := addWadExtension(g_ExtractWadName(Res));
2213 WAD := TWADFile.Create;
2214 if not WAD.ReadFile(FileName) then
2215 begin
2216 WAD.Free();
2217 Exit;
2218 end;
2220 ResList := WAD.GetMapResources();
2221 WAD.Free();
2223 mnn := g_ExtractFileName(Res);
2224 if ResList <> nil then
2225 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2226 begin
2227 Result := True;
2228 Exit;
2229 end;
2230 end;
2232 procedure g_Map_Free(freeTextures: Boolean=true);
2233 var
2234 a: Integer;
2236 procedure FreePanelArray(var panels: TPanelArray);
2237 var
2238 i: Integer;
2240 begin
2241 if panels <> nil then
2242 begin
2243 for i := 0 to High(panels) do
2244 panels[i].Free();
2245 panels := nil;
2246 end;
2247 end;
2249 begin
2250 g_GFX_Free();
2251 g_Weapon_Free();
2252 g_Items_Free();
2253 g_Triggers_Free();
2254 g_Monsters_Free();
2256 RespawnPoints := nil;
2257 if FlagPoints[FLAG_RED] <> nil then
2258 begin
2259 Dispose(FlagPoints[FLAG_RED]);
2260 FlagPoints[FLAG_RED] := nil;
2261 end;
2262 if FlagPoints[FLAG_BLUE] <> nil then
2263 begin
2264 Dispose(FlagPoints[FLAG_BLUE]);
2265 FlagPoints[FLAG_BLUE] := nil;
2266 end;
2267 //DOMFlagPoints := nil;
2269 //gDOMFlags := nil;
2271 if (Length(gCurrentMapFileName) <> 0) then
2272 begin
2273 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2274 end
2275 else
2276 begin
2277 e_LogWritefln('g_Map_Free: no previous map.', []);
2278 end;
2279 if freeTextures then
2280 begin
2281 e_LogWritefln('g_Map_Free: clearing textures...', []);
2282 if (Textures <> nil) then
2283 begin
2284 for a := 0 to High(Textures) do
2285 begin
2286 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2287 begin
2288 if Textures[a].Anim then
2289 begin
2290 g_Frames_DeleteByID(Textures[a].FramesID)
2291 end
2292 else
2293 begin
2294 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2295 begin
2296 e_DeleteTexture(Textures[a].TextureID);
2297 end;
2298 end;
2299 end;
2300 end;
2301 Textures := nil;
2302 end;
2303 TextNameHash.Free();
2304 TextNameHash := nil;
2305 BadTextNameHash.Free();
2306 BadTextNameHash := nil;
2307 end;
2309 FreePanelArray(gWalls);
2310 FreePanelArray(gRenderBackgrounds);
2311 FreePanelArray(gRenderForegrounds);
2312 FreePanelArray(gWater);
2313 FreePanelArray(gAcid1);
2314 FreePanelArray(gAcid2);
2315 FreePanelArray(gSteps);
2316 FreePanelArray(gLifts);
2317 FreePanelArray(gBlockMon);
2319 if BackID <> DWORD(-1) then
2320 begin
2321 gBackSize.X := 0;
2322 gBackSize.Y := 0;
2323 e_DeleteTexture(BackID);
2324 BackID := DWORD(-1);
2325 end;
2327 g_Game_StopAllSounds(False);
2328 gMusic.FreeSound();
2329 g_Sound_Delete(gMapInfo.MusicName);
2331 gMapInfo.Name := '';
2332 gMapInfo.Description := '';
2333 gMapInfo.MusicName := '';
2334 gMapInfo.Height := 0;
2335 gMapInfo.Width := 0;
2337 gDoorMap := nil;
2338 gLiftMap := nil;
2340 PanelByID := nil;
2341 end;
2343 procedure g_Map_Update();
2344 var
2345 a, d, j: Integer;
2346 m: Word;
2347 s: String;
2349 procedure UpdatePanelArray(var panels: TPanelArray);
2350 var
2351 i: Integer;
2353 begin
2354 if panels <> nil then
2355 for i := 0 to High(panels) do
2356 panels[i].Update();
2357 end;
2359 begin
2360 UpdatePanelArray(gWalls);
2361 UpdatePanelArray(gRenderBackgrounds);
2362 UpdatePanelArray(gRenderForegrounds);
2363 UpdatePanelArray(gWater);
2364 UpdatePanelArray(gAcid1);
2365 UpdatePanelArray(gAcid2);
2366 UpdatePanelArray(gSteps);
2368 if gGameSettings.GameMode = GM_CTF then
2369 begin
2370 for a := FLAG_RED to FLAG_BLUE do
2371 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2372 with gFlags[a] do
2373 begin
2374 if gFlags[a].Animation <> nil then
2375 gFlags[a].Animation.Update();
2377 m := g_Obj_Move(@Obj, True, True);
2379 if gTime mod (GAME_TICK*2) <> 0 then
2380 Continue;
2382 // Ñîïðîòèâëåíèå âîçäóõà:
2383 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2385 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2386 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2387 begin
2388 g_Map_ResetFlag(a);
2389 gFlags[a].CaptureTime := 0;
2390 if a = FLAG_RED then
2391 s := _lc[I_PLAYER_FLAG_RED]
2392 else
2393 s := _lc[I_PLAYER_FLAG_BLUE];
2394 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2396 if g_Game_IsNet then
2397 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2398 Continue;
2399 end;
2401 if Count > 0 then
2402 Count := Count - 1;
2404 // Èãðîê áåðåò ôëàã:
2405 if gPlayers <> nil then
2406 begin
2407 j := Random(Length(gPlayers)) - 1;
2409 for d := 0 to High(gPlayers) do
2410 begin
2411 Inc(j);
2412 if j > High(gPlayers) then
2413 j := 0;
2415 if gPlayers[j] <> nil then
2416 if gPlayers[j].Live and
2417 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2418 begin
2419 if gPlayers[j].GetFlag(a) then
2420 Break;
2421 end;
2422 end;
2423 end;
2424 end;
2425 end;
2426 end;
2429 // old algo
2430 procedure g_Map_DrawPanels (PanelType: Word);
2432 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2433 var
2434 idx: Integer;
2435 begin
2436 if (panels <> nil) then
2437 begin
2438 // alas, no visible set
2439 for idx := 0 to High(panels) do
2440 begin
2441 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2442 end;
2443 end;
2444 end;
2446 begin
2447 case PanelType of
2448 PANEL_WALL: DrawPanels(gWalls);
2449 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2450 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2451 PANEL_FORE: DrawPanels(gRenderForegrounds);
2452 PANEL_WATER: DrawPanels(gWater);
2453 PANEL_ACID1: DrawPanels(gAcid1);
2454 PANEL_ACID2: DrawPanels(gAcid2);
2455 PANEL_STEP: DrawPanels(gSteps);
2456 end;
2457 end;
2460 // new algo
2461 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2463 function checker (pan: TPanel; tag: Integer): Boolean;
2464 begin
2465 result := false; // don't stop, ever
2466 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2467 gDrawPanelList.insert(pan);
2468 end;
2470 begin
2471 dplClear();
2472 //tagmask := panelTypeToTag(PanelType);
2473 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2474 // list will be rendered in `g_game.DrawPlayer()`
2475 end;
2478 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2480 function checker (pan: TPanel; tag: Integer): Boolean;
2481 begin
2482 result := false; // don't stop, ever
2483 pan.DrawShadowVolume(lightX, lightY, radius);
2484 end;
2486 begin
2487 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2488 end;
2491 procedure g_Map_DrawBack(dx, dy: Integer);
2492 begin
2493 if gDrawBackGround and (BackID <> DWORD(-1)) then
2494 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2495 else
2496 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2497 end;
2499 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2500 PanelType: Word; b1x3: Boolean=false): Boolean;
2501 var
2502 a, h: Integer;
2503 begin
2504 Result := False;
2506 if WordBool(PanelType and PANEL_WALL) then
2507 if gWalls <> nil then
2508 begin
2509 h := High(gWalls);
2511 for a := 0 to h do
2512 if gWalls[a].Enabled and
2513 g_Collide(X, Y, Width, Height,
2514 gWalls[a].X, gWalls[a].Y,
2515 gWalls[a].Width, gWalls[a].Height) then
2516 begin
2517 Result := True;
2518 Exit;
2519 end;
2520 end;
2522 if WordBool(PanelType and PANEL_WATER) then
2523 if gWater <> nil then
2524 begin
2525 h := High(gWater);
2527 for a := 0 to h do
2528 if g_Collide(X, Y, Width, Height,
2529 gWater[a].X, gWater[a].Y,
2530 gWater[a].Width, gWater[a].Height) then
2531 begin
2532 Result := True;
2533 Exit;
2534 end;
2535 end;
2537 if WordBool(PanelType and PANEL_ACID1) then
2538 if gAcid1 <> nil then
2539 begin
2540 h := High(gAcid1);
2542 for a := 0 to h do
2543 if g_Collide(X, Y, Width, Height,
2544 gAcid1[a].X, gAcid1[a].Y,
2545 gAcid1[a].Width, gAcid1[a].Height) then
2546 begin
2547 Result := True;
2548 Exit;
2549 end;
2550 end;
2552 if WordBool(PanelType and PANEL_ACID2) then
2553 if gAcid2 <> nil then
2554 begin
2555 h := High(gAcid2);
2557 for a := 0 to h do
2558 if g_Collide(X, Y, Width, Height,
2559 gAcid2[a].X, gAcid2[a].Y,
2560 gAcid2[a].Width, gAcid2[a].Height) then
2561 begin
2562 Result := True;
2563 Exit;
2564 end;
2565 end;
2567 if WordBool(PanelType and PANEL_STEP) then
2568 if gSteps <> nil then
2569 begin
2570 h := High(gSteps);
2572 for a := 0 to h do
2573 if g_Collide(X, Y, Width, Height,
2574 gSteps[a].X, gSteps[a].Y,
2575 gSteps[a].Width, gSteps[a].Height) then
2576 begin
2577 Result := True;
2578 Exit;
2579 end;
2580 end;
2582 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2583 if gLifts <> nil then
2584 begin
2585 h := High(gLifts);
2587 for a := 0 to h do
2588 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2589 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2590 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2591 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2592 g_Collide(X, Y, Width, Height,
2593 gLifts[a].X, gLifts[a].Y,
2594 gLifts[a].Width, gLifts[a].Height) then
2595 begin
2596 Result := True;
2597 Exit;
2598 end;
2599 end;
2601 if WordBool(PanelType and PANEL_BLOCKMON) then
2602 if gBlockMon <> nil then
2603 begin
2604 h := High(gBlockMon);
2606 for a := 0 to h do
2607 if ( (not b1x3) or
2608 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2609 g_Collide(X, Y, Width, Height,
2610 gBlockMon[a].X, gBlockMon[a].Y,
2611 gBlockMon[a].Width, gBlockMon[a].Height) then
2612 begin
2613 Result := True;
2614 Exit;
2615 end;
2616 end;
2617 end;
2619 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2620 var
2621 texid: DWORD;
2623 function checkPanels (constref panels: TPanelArray): Boolean;
2624 var
2625 a: Integer;
2626 begin
2627 result := false;
2628 if panels = nil then exit;
2629 for a := 0 to High(panels) do
2630 begin
2631 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2632 begin
2633 result := true;
2634 texid := panels[a].GetTextureID();
2635 exit;
2636 end;
2637 end;
2638 end;
2640 begin
2641 texid := LongWord(TEXTURE_NONE);
2642 result := texid;
2643 if not checkPanels(gWater) then
2644 if not checkPanels(gAcid1) then
2645 if not checkPanels(gAcid2) then exit;
2646 result := texid;
2647 end;
2650 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2651 const
2652 SlowMask = GridTagLift or GridTagBlockMon;
2653 function checker (pan: TPanel; tag: Integer): Boolean;
2654 begin
2656 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2657 begin
2658 result := pan.Enabled;
2659 exit;
2660 end;
2663 if ((tag and GridTagLift) <> 0) then
2664 begin
2665 result :=
2666 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2667 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2668 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2669 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2670 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2671 exit;
2672 end;
2674 if ((tag and GridTagBlockMon) <> 0) then
2675 begin
2676 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2677 exit;
2678 end;
2680 // other shit
2681 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2682 result := true; // i found her!
2683 end;
2685 var
2686 tagmask: Integer = 0;
2687 begin
2688 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2689 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2690 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2691 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2692 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2693 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2694 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2696 if (tagmask = 0) then begin result := false; exit; end; // just in case
2698 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2699 if gdbg_map_use_accel_coldet then
2700 begin
2701 if (Width = 1) and (Height = 1) then
2702 begin
2703 if ((tagmask and SlowMask) <> 0) then
2704 begin
2705 // slow
2706 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2707 end
2708 else
2709 begin
2710 // fast
2711 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2712 end;
2713 end
2714 else
2715 begin
2716 if ((tagmask and SlowMask) <> 0) then
2717 begin
2718 // slow
2719 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2720 end
2721 else
2722 begin
2723 // fast
2724 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2725 end;
2726 end;
2727 end
2728 else
2729 begin
2730 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2731 end;
2732 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2733 end;
2736 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2737 var
2738 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2739 texid: DWORD;
2741 // slightly different from the old code, but meh...
2742 function checker (pan: TPanel; tag: Integer): Boolean;
2743 begin
2744 result := false; // don't stop, ever
2745 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2746 // check priorities
2747 case cctype of
2748 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2749 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2750 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2751 end;
2752 // collision?
2753 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2754 // yeah
2755 texid := pan.GetTextureID();
2756 // water? water has the highest priority, so stop right here
2757 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2758 // acid2?
2759 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2760 // acid1?
2761 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2762 end;
2764 begin
2765 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2766 if gdbg_map_use_accel_coldet then
2767 begin
2768 texid := LongWord(TEXTURE_NONE);
2769 if (Width = 1) and (Height = 1) then
2770 begin
2771 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2772 end
2773 else
2774 begin
2775 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2776 end;
2777 result := texid;
2778 end
2779 else
2780 begin
2781 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2782 end;
2783 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2784 end;
2787 procedure g_Map_EnableWall(ID: DWORD);
2788 var
2789 pan: TPanel;
2790 begin
2791 pan := gWalls[ID];
2792 pan.Enabled := True;
2793 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2795 mapGrid.proxyEnabled[pan.proxyId] := true;
2796 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2797 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2799 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2801 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2802 //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);
2803 {$ENDIF}
2804 end;
2806 procedure g_Map_DisableWall(ID: DWORD);
2807 var
2808 pan: TPanel;
2809 begin
2810 pan := gWalls[ID];
2811 pan.Enabled := False;
2812 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2814 mapGrid.proxyEnabled[pan.proxyId] := false;
2815 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2817 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2819 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2820 //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);
2821 {$ENDIF}
2822 end;
2824 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2825 var
2826 tp: TPanel;
2827 begin
2828 case PanelType of
2829 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2830 tp := gWalls[ID];
2831 PANEL_FORE:
2832 tp := gRenderForegrounds[ID];
2833 PANEL_BACK:
2834 tp := gRenderBackgrounds[ID];
2835 PANEL_WATER:
2836 tp := gWater[ID];
2837 PANEL_ACID1:
2838 tp := gAcid1[ID];
2839 PANEL_ACID2:
2840 tp := gAcid2[ID];
2841 PANEL_STEP:
2842 tp := gSteps[ID];
2843 else
2844 Exit;
2845 end;
2847 tp.NextTexture(AnimLoop);
2848 if g_Game_IsServer and g_Game_IsNet then
2849 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2850 end;
2852 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2853 begin
2854 if gLifts[ID].LiftType = t then
2855 Exit;
2857 with gLifts[ID] do
2858 begin
2859 LiftType := t;
2861 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2862 //TODO: make separate lift tags, and change tag here
2864 if LiftType = 0 then
2865 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2866 else if LiftType = 1 then
2867 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2868 else if LiftType = 2 then
2869 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2870 else if LiftType = 3 then
2871 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2873 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2874 end;
2875 end;
2877 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2878 var
2879 a: Integer;
2880 PointsArray: Array of TRespawnPoint;
2881 begin
2882 Result := False;
2883 SetLength(PointsArray, 0);
2885 if RespawnPoints = nil then
2886 Exit;
2888 for a := 0 to High(RespawnPoints) do
2889 if RespawnPoints[a].PointType = PointType then
2890 begin
2891 SetLength(PointsArray, Length(PointsArray)+1);
2892 PointsArray[High(PointsArray)] := RespawnPoints[a];
2893 end;
2895 if PointsArray = nil then
2896 Exit;
2898 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2899 Result := True;
2900 end;
2902 function g_Map_GetPointCount(PointType: Byte): Word;
2903 var
2904 a: Integer;
2905 begin
2906 Result := 0;
2908 if RespawnPoints = nil then
2909 Exit;
2911 for a := 0 to High(RespawnPoints) do
2912 if RespawnPoints[a].PointType = PointType then
2913 Result := Result + 1;
2914 end;
2916 function g_Map_HaveFlagPoints(): Boolean;
2917 begin
2918 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2919 end;
2921 procedure g_Map_ResetFlag(Flag: Byte);
2922 begin
2923 with gFlags[Flag] do
2924 begin
2925 Obj.X := -1000;
2926 Obj.Y := -1000;
2927 Obj.Vel.X := 0;
2928 Obj.Vel.Y := 0;
2929 Direction := D_LEFT;
2930 State := FLAG_STATE_NONE;
2931 if FlagPoints[Flag] <> nil then
2932 begin
2933 Obj.X := FlagPoints[Flag]^.X;
2934 Obj.Y := FlagPoints[Flag]^.Y;
2935 Direction := FlagPoints[Flag]^.Direction;
2936 State := FLAG_STATE_NORMAL;
2937 end;
2938 Count := -1;
2939 end;
2940 end;
2942 procedure g_Map_DrawFlags();
2943 var
2944 i, dx: Integer;
2945 Mirror: TMirrorType;
2946 begin
2947 if gGameSettings.GameMode <> GM_CTF then
2948 Exit;
2950 for i := FLAG_RED to FLAG_BLUE do
2951 with gFlags[i] do
2952 if State <> FLAG_STATE_CAPTURED then
2953 begin
2954 if State = FLAG_STATE_NONE then
2955 continue;
2957 if Direction = D_LEFT then
2958 begin
2959 Mirror := M_HORIZONTAL;
2960 dx := -1;
2961 end
2962 else
2963 begin
2964 Mirror := M_NONE;
2965 dx := 1;
2966 end;
2968 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2970 if g_debug_Frames then
2971 begin
2972 e_DrawQuad(Obj.X+Obj.Rect.X,
2973 Obj.Y+Obj.Rect.Y,
2974 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2975 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2976 0, 255, 0);
2977 end;
2978 end;
2979 end;
2981 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2982 var
2983 dw: DWORD;
2984 b: Byte;
2985 str: String;
2986 boo: Boolean;
2988 procedure SavePanelArray(var panels: TPanelArray);
2989 var
2990 PAMem: TBinMemoryWriter;
2991 i: Integer;
2992 begin
2993 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2994 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2996 i := 0;
2997 while i < Length(panels) do
2998 begin
2999 if panels[i].SaveIt then
3000 begin
3001 // ID ïàíåëè:
3002 PAMem.WriteInt(i);
3003 // Ñîõðàíÿåì ïàíåëü:
3004 panels[i].SaveState(PAMem);
3005 end;
3006 Inc(i);
3007 end;
3009 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
3010 PAMem.SaveToMemory(Mem);
3011 PAMem.Free();
3012 end;
3014 procedure SaveFlag(flag: PFlag);
3015 begin
3016 // Ñèãíàòóðà ôëàãà:
3017 dw := FLAG_SIGNATURE; // 'FLAG'
3018 Mem.WriteDWORD(dw);
3019 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
3020 Mem.WriteByte(flag^.RespawnType);
3021 // Ñîñòîÿíèå ôëàãà:
3022 Mem.WriteByte(flag^.State);
3023 // Íàïðàâëåíèå ôëàãà:
3024 if flag^.Direction = D_LEFT then
3025 b := 1
3026 else // D_RIGHT
3027 b := 2;
3028 Mem.WriteByte(b);
3029 // Îáúåêò ôëàãà:
3030 Obj_SaveState(@flag^.Obj, Mem);
3031 end;
3033 begin
3034 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
3036 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
3037 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
3038 SavePanelArray(gWalls);
3039 // Ñîõðàíÿåì ïàíåëè ôîíà:
3040 SavePanelArray(gRenderBackgrounds);
3041 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
3042 SavePanelArray(gRenderForegrounds);
3043 // Ñîõðàíÿåì ïàíåëè âîäû:
3044 SavePanelArray(gWater);
3045 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
3046 SavePanelArray(gAcid1);
3047 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
3048 SavePanelArray(gAcid2);
3049 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
3050 SavePanelArray(gSteps);
3051 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
3052 SavePanelArray(gLifts);
3053 ///// /////
3055 ///// Ñîõðàíÿåì ìóçûêó: /////
3056 // Ñèãíàòóðà ìóçûêè:
3057 dw := MUSIC_SIGNATURE; // 'MUSI'
3058 Mem.WriteDWORD(dw);
3059 // Íàçâàíèå ìóçûêè:
3060 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3061 if gMusic.NoMusic then
3062 str := ''
3063 else
3064 str := gMusic.Name;
3065 Mem.WriteString(str, 64);
3066 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
3067 dw := gMusic.GetPosition();
3068 Mem.WriteDWORD(dw);
3069 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
3070 boo := gMusic.SpecPause;
3071 Mem.WriteBoolean(boo);
3072 ///// /////
3074 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3075 Mem.WriteInt(gTotalMonsters);
3076 ///// /////
3078 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3079 if gGameSettings.GameMode = GM_CTF then
3080 begin
3081 // Ôëàã Êðàñíîé êîìàíäû:
3082 SaveFlag(@gFlags[FLAG_RED]);
3083 // Ôëàã Ñèíåé êîìàíäû:
3084 SaveFlag(@gFlags[FLAG_BLUE]);
3085 end;
3086 ///// /////
3088 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3089 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3090 begin
3091 // Î÷êè Êðàñíîé êîìàíäû:
3092 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
3093 // Î÷êè Ñèíåé êîìàíäû:
3094 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
3095 end;
3096 ///// /////
3097 end;
3099 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
3100 var
3101 dw: DWORD;
3102 b: Byte;
3103 str: String;
3104 boo: Boolean;
3106 procedure LoadPanelArray(var panels: TPanelArray);
3107 var
3108 PAMem: TBinMemoryReader;
3109 i, id: Integer;
3110 begin
3111 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
3112 PAMem := TBinMemoryReader.Create();
3113 PAMem.LoadFromMemory(Mem);
3115 for i := 0 to Length(panels)-1 do
3116 begin
3117 if panels[i].SaveIt then
3118 begin
3119 // ID ïàíåëè:
3120 PAMem.ReadInt(id);
3121 if id <> i then
3122 begin
3123 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
3124 end;
3125 // Çàãðóæàåì ïàíåëü:
3126 panels[i].LoadState(PAMem);
3127 if (panels[i].arrIdx <> i) then raise Exception.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
3128 if (panels[i].proxyId >= 0) then mapGrid.proxyEnabled[panels[i].proxyId] := panels[i].Enabled;
3129 end;
3130 end;
3132 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
3133 PAMem.Free();
3134 end;
3136 procedure LoadFlag(flag: PFlag);
3137 begin
3138 // Ñèãíàòóðà ôëàãà:
3139 Mem.ReadDWORD(dw);
3140 if dw <> FLAG_SIGNATURE then // 'FLAG'
3141 begin
3142 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
3143 end;
3144 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
3145 Mem.ReadByte(flag^.RespawnType);
3146 // Ñîñòîÿíèå ôëàãà:
3147 Mem.ReadByte(flag^.State);
3148 // Íàïðàâëåíèå ôëàãà:
3149 Mem.ReadByte(b);
3150 if b = 1 then
3151 flag^.Direction := D_LEFT
3152 else // b = 2
3153 flag^.Direction := D_RIGHT;
3154 // Îáúåêò ôëàãà:
3155 Obj_LoadState(@flag^.Obj, Mem);
3156 end;
3158 begin
3159 if Mem = nil then
3160 Exit;
3162 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3163 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
3164 LoadPanelArray(gWalls);
3165 // Çàãðóæàåì ïàíåëè ôîíà:
3166 LoadPanelArray(gRenderBackgrounds);
3167 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
3168 LoadPanelArray(gRenderForegrounds);
3169 // Çàãðóæàåì ïàíåëè âîäû:
3170 LoadPanelArray(gWater);
3171 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
3172 LoadPanelArray(gAcid1);
3173 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
3174 LoadPanelArray(gAcid2);
3175 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
3176 LoadPanelArray(gSteps);
3177 // Çàãðóæàåì ïàíåëè ëèôòîâ:
3178 LoadPanelArray(gLifts);
3179 ///// /////
3181 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
3182 g_GFX_Init();
3183 //mapCreateGrid();
3185 ///// Çàãðóæàåì ìóçûêó: /////
3186 // Ñèãíàòóðà ìóçûêè:
3187 Mem.ReadDWORD(dw);
3188 if dw <> MUSIC_SIGNATURE then // 'MUSI'
3189 begin
3190 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
3191 end;
3192 // Íàçâàíèå ìóçûêè:
3193 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3194 Mem.ReadString(str);
3195 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
3196 Mem.ReadDWORD(dw);
3197 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
3198 Mem.ReadBoolean(boo);
3199 // Çàïóñêàåì ýòó ìóçûêó:
3200 gMusic.SetByName(str);
3201 gMusic.SpecPause := boo;
3202 gMusic.Play();
3203 gMusic.Pause(True);
3204 gMusic.SetPosition(dw);
3205 ///// /////
3207 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3208 Mem.ReadInt(gTotalMonsters);
3209 ///// /////
3211 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3212 if gGameSettings.GameMode = GM_CTF then
3213 begin
3214 // Ôëàã Êðàñíîé êîìàíäû:
3215 LoadFlag(@gFlags[FLAG_RED]);
3216 // Ôëàã Ñèíåé êîìàíäû:
3217 LoadFlag(@gFlags[FLAG_BLUE]);
3218 end;
3219 ///// /////
3221 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3222 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3223 begin
3224 // Î÷êè Êðàñíîé êîìàíäû:
3225 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
3226 // Î÷êè Ñèíåé êîìàíäû:
3227 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
3228 end;
3229 ///// /////
3230 end;
3233 // trace liquid, stepping by `dx` and `dy`
3234 // return last seen liquid coords, and `false` if we're started outside of the liquid
3235 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3236 const
3237 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3238 begin
3239 topx := x;
3240 topy := y;
3241 // started outside of the liquid?
3242 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3243 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3244 result := true;
3245 while true do
3246 begin
3247 Inc(x, dx);
3248 Inc(y, dy);
3249 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3250 topx := x;
3251 topy := y;
3252 end;
3253 end;
3256 end.