DEADSOFTWARE

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