DEADSOFTWARE

removed all mentions of dynaabb tree from the sources; WARNING! EVERYTHING IS BROKEN!
[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, MAPSTRUCT, g_textures, Classes,
24 g_phys, wadreader, BinEditor, g_panel, g_grid, md5, binheap, xprofiler;
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();
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 wall index in `gWalls` or -1
96 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
98 type
99 TForEachPanelCB = function (pan: TPanel): Boolean; // return `true` to stop
101 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
103 // trace liquid, stepping by `dx` and `dy`
104 // return last seen liquid coords, and `false` if we're started outside of the liquid
105 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
108 procedure g_Map_ProfilersBegin ();
109 procedure g_Map_ProfilersEnd ();
112 const
113 RESPAWNPOINT_PLAYER1 = 1;
114 RESPAWNPOINT_PLAYER2 = 2;
115 RESPAWNPOINT_DM = 3;
116 RESPAWNPOINT_RED = 4;
117 RESPAWNPOINT_BLUE = 5;
119 FLAG_NONE = 0;
120 FLAG_RED = 1;
121 FLAG_BLUE = 2;
122 FLAG_DOM = 3;
124 FLAG_STATE_NONE = 0;
125 FLAG_STATE_NORMAL = 1;
126 FLAG_STATE_DROPPED = 2;
127 FLAG_STATE_CAPTURED = 3;
128 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
129 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
131 FLAG_TIME = 720; // 20 seconds
133 SKY_STRETCH: Single = 1.5;
135 const
136 GridTagInvalid = 0;
138 (* draw order:
139 PANEL_BACK
140 PANEL_STEP
141 PANEL_WALL
142 PANEL_CLOSEDOOR
143 PANEL_ACID1
144 PANEL_ACID2
145 PANEL_WATER
146 PANEL_FORE
147 *)
148 // sorted by draw priority
149 GridTagBack = 1 shl 0;
150 GridTagStep = 1 shl 1;
151 GridTagWall = 1 shl 2;
152 GridTagDoor = 1 shl 3;
153 GridTagAcid1 = 1 shl 4;
154 GridTagAcid2 = 1 shl 5;
155 GridTagWater = 1 shl 6;
156 GridTagFore = 1 shl 7;
157 // the following are invisible
158 GridTagLift = 1 shl 8;
159 GridTagBlockMon = 1 shl 9;
161 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
164 var
165 gWalls: TPanelArray;
166 gRenderBackgrounds: TPanelArray;
167 gRenderForegrounds: TPanelArray;
168 gWater, gAcid1, gAcid2: TPanelArray;
169 gSteps: TPanelArray;
170 gLifts: TPanelArray;
171 gBlockMon: TPanelArray;
172 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
173 //gDOMFlags: array of TFlag;
174 gMapInfo: TMapInfo;
175 gBackSize: TPoint;
176 gDoorMap: array of array of DWORD;
177 gLiftMap: array of array of DWORD;
178 gWADHash: TMD5Digest;
179 BackID: DWORD = DWORD(-1);
180 gExternalResources: TStringList;
182 gdbg_map_use_accel_render: Boolean = true;
183 gdbg_map_use_accel_coldet: Boolean = true;
184 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
185 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
187 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
190 implementation
192 uses
193 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
194 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
195 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
196 Math, g_monsters, g_saveload, g_language, g_netmsg,
197 utils, sfs,
198 ImagingTypes, Imaging, ImagingUtility,
199 ImagingGif, ImagingNetworkGraphics;
201 const
202 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
203 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
204 FLAG_SIGNATURE = $47414C46; // 'FLAG'
207 type
208 TPanelGrid = specialize TBodyGridBase<TPanel>;
211 function panelTypeToTag (panelType: Word): Integer;
212 begin
213 case panelType of
214 PANEL_WALL: result := GridTagWall; // gWalls
215 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
216 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
217 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
218 PANEL_WATER: result := GridTagWater; // gWater
219 PANEL_ACID1: result := GridTagAcid1; // gAcid1
220 PANEL_ACID2: result := GridTagAcid2; // gAcid2
221 PANEL_STEP: result := GridTagStep; // gSteps
222 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
223 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
224 else result := GridTagInvalid;
225 end;
226 end;
229 function dplLess (a, b: TObject): Boolean;
230 var
231 pa, pb: TPanel;
232 begin
233 pa := TPanel(a);
234 pb := TPanel(b);
235 if (pa.tag < pb.tag) then begin result := true; exit; end;
236 if (pa.tag > pb.tag) then begin result := false; exit; end;
237 result := (pa.arrIdx < pb.arrIdx);
238 end;
240 procedure dplClear ();
241 begin
242 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
243 end;
246 type
247 TPanelID = record
248 PWhere: ^TPanelArray;
249 PArrID: Integer;
250 end;
252 var
253 PanelById: array of TPanelID;
254 Textures: TLevelTextureArray;
255 RespawnPoints: Array of TRespawnPoint;
256 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
257 //DOMFlagPoints: Array of TFlagPoint;
258 mapGrid: TPanelGrid = nil;
261 procedure g_Map_ProfilersBegin ();
262 begin
263 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
264 profMapCollision.mainBegin(g_profile_collision);
265 // create sections
266 if g_profile_collision then
267 begin
268 profMapCollision.sectionBegin('*solids');
269 profMapCollision.sectionEnd();
270 profMapCollision.sectionBegin('liquids');
271 profMapCollision.sectionEnd();
272 end;
273 end;
275 procedure g_Map_ProfilersEnd ();
276 begin
277 if (profMapCollision <> nil) then profMapCollision.mainEnd();
278 end;
281 // wall index in `gWalls` or -1
282 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
283 var
284 ex, ey: Integer;
285 begin
286 result := (mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor)) <> nil);
287 end;
290 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
292 function checker (pan: TPanel; tag: Integer): Boolean;
293 begin
295 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
296 begin
297 result := pan.Enabled; // stop if wall is enabled
298 exit;
299 end;
302 if ((tag and GridTagLift) <> 0) then
303 begin
304 // stop if the lift of the right type
305 result :=
306 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
307 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
308 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
309 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
310 exit;
311 end;
313 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
314 end;
316 var
317 tagmask: Integer = 0;
318 begin
319 result := false;
321 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
322 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
323 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
324 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
325 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
326 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
327 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
329 if (tagmask = 0) then exit;// just in case
330 if ((tagmask and GridTagLift) <> 0) then
331 begin
332 // slow
333 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
334 end
335 else
336 begin
337 // fast
338 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
339 end;
340 end;
343 function g_Map_IsSpecialTexture(Texture: String): Boolean;
344 begin
345 Result := (Texture = TEXTURE_NAME_WATER) or
346 (Texture = TEXTURE_NAME_ACID1) or
347 (Texture = TEXTURE_NAME_ACID2);
348 end;
350 procedure CreateDoorMap();
351 var
352 PanelArray: Array of record
353 X, Y: Integer;
354 Width, Height: Word;
355 Active: Boolean;
356 PanelID: DWORD;
357 end;
358 a, b, c, m, i, len: Integer;
359 ok: Boolean;
360 begin
361 if gWalls = nil then
362 Exit;
364 i := 0;
365 len := 128;
366 SetLength(PanelArray, len);
368 for a := 0 to High(gWalls) do
369 if gWalls[a].Door then
370 begin
371 PanelArray[i].X := gWalls[a].X;
372 PanelArray[i].Y := gWalls[a].Y;
373 PanelArray[i].Width := gWalls[a].Width;
374 PanelArray[i].Height := gWalls[a].Height;
375 PanelArray[i].Active := True;
376 PanelArray[i].PanelID := a;
378 i := i + 1;
379 if i = len then
380 begin
381 len := len + 128;
382 SetLength(PanelArray, len);
383 end;
384 end;
386 // Íåò äâåðåé:
387 if i = 0 then
388 begin
389 PanelArray := nil;
390 Exit;
391 end;
393 SetLength(gDoorMap, 0);
395 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
397 for a := 0 to i-1 do
398 if PanelArray[a].Active then
399 begin
400 PanelArray[a].Active := False;
401 m := Length(gDoorMap);
402 SetLength(gDoorMap, m+1);
403 SetLength(gDoorMap[m], 1);
404 gDoorMap[m, 0] := PanelArray[a].PanelID;
405 ok := True;
407 while ok do
408 begin
409 ok := False;
411 for b := 0 to i-1 do
412 if PanelArray[b].Active then
413 for c := 0 to High(gDoorMap[m]) do
414 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
415 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
416 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
417 PanelArray[b].Width, PanelArray[b].Height,
418 gWalls[gDoorMap[m, c]].X,
419 gWalls[gDoorMap[m, c]].Y,
420 gWalls[gDoorMap[m, c]].Width,
421 gWalls[gDoorMap[m, c]].Height) then
422 begin
423 PanelArray[b].Active := False;
424 SetLength(gDoorMap[m],
425 Length(gDoorMap[m])+1);
426 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
427 ok := True;
428 Break;
429 end;
430 end;
432 g_Game_StepLoading();
433 end;
435 PanelArray := nil;
436 end;
438 procedure CreateLiftMap();
439 var
440 PanelArray: Array of record
441 X, Y: Integer;
442 Width, Height: Word;
443 Active: Boolean;
444 end;
445 a, b, c, len, i, j: Integer;
446 ok: Boolean;
447 begin
448 if gLifts = nil then
449 Exit;
451 len := Length(gLifts);
452 SetLength(PanelArray, len);
454 for a := 0 to len-1 do
455 begin
456 PanelArray[a].X := gLifts[a].X;
457 PanelArray[a].Y := gLifts[a].Y;
458 PanelArray[a].Width := gLifts[a].Width;
459 PanelArray[a].Height := gLifts[a].Height;
460 PanelArray[a].Active := True;
461 end;
463 SetLength(gLiftMap, len);
464 i := 0;
466 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
468 for a := 0 to len-1 do
469 if PanelArray[a].Active then
470 begin
471 PanelArray[a].Active := False;
472 SetLength(gLiftMap[i], 32);
473 j := 0;
474 gLiftMap[i, j] := a;
475 ok := True;
477 while ok do
478 begin
479 ok := False;
480 for b := 0 to len-1 do
481 if PanelArray[b].Active then
482 for c := 0 to j do
483 if g_CollideAround(PanelArray[b].X,
484 PanelArray[b].Y,
485 PanelArray[b].Width,
486 PanelArray[b].Height,
487 PanelArray[gLiftMap[i, c]].X,
488 PanelArray[gLiftMap[i, c]].Y,
489 PanelArray[gLiftMap[i, c]].Width,
490 PanelArray[gLiftMap[i, c]].Height) then
491 begin
492 PanelArray[b].Active := False;
493 j := j+1;
494 if j > High(gLiftMap[i]) then
495 SetLength(gLiftMap[i],
496 Length(gLiftMap[i])+32);
498 gLiftMap[i, j] := b;
499 ok := True;
501 Break;
502 end;
503 end;
505 SetLength(gLiftMap[i], j+1);
506 i := i+1;
508 g_Game_StepLoading();
509 end;
511 SetLength(gLiftMap, i);
513 PanelArray := nil;
514 end;
516 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
517 CurTex: Integer; sav: Boolean): Integer;
518 var
519 len: Integer;
520 panels: ^TPanelArray;
521 begin
522 Result := -1;
524 case PanelRec.PanelType of
525 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
526 panels := @gWalls;
527 PANEL_BACK:
528 panels := @gRenderBackgrounds;
529 PANEL_FORE:
530 panels := @gRenderForegrounds;
531 PANEL_WATER:
532 panels := @gWater;
533 PANEL_ACID1:
534 panels := @gAcid1;
535 PANEL_ACID2:
536 panels := @gAcid2;
537 PANEL_STEP:
538 panels := @gSteps;
539 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
540 panels := @gLifts;
541 PANEL_BLOCKMON:
542 panels := @gBlockMon;
543 else
544 Exit;
545 end;
547 len := Length(panels^);
548 SetLength(panels^, len + 1);
550 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
551 panels^[len].arrIdx := len;
552 panels^[len].proxyId := -1;
553 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
554 if sav then
555 panels^[len].SaveIt := True;
557 Result := len;
559 len := Length(PanelByID);
560 SetLength(PanelByID, len + 1);
561 PanelByID[len].PWhere := panels;
562 PanelByID[len].PArrID := Result;
563 end;
565 function CreateNullTexture(RecName: String): Integer;
566 begin
567 SetLength(Textures, Length(Textures)+1);
568 result := High(Textures);
570 with Textures[High(Textures)] do
571 begin
572 TextureName := RecName;
573 Width := 1;
574 Height := 1;
575 Anim := False;
576 TextureID := TEXTURE_NONE;
577 end;
578 end;
580 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
581 var
582 WAD: TWADFile;
583 TextureData: Pointer;
584 WADName, txname: String;
585 a, ResLength: Integer;
586 begin
587 Result := -1;
589 if Textures <> nil then
590 for a := 0 to High(Textures) do
591 if Textures[a].TextureName = RecName then
592 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
593 Result := a;
594 Exit;
595 end;
597 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
598 if (RecName = TEXTURE_NAME_WATER) or
599 (RecName = TEXTURE_NAME_ACID1) or
600 (RecName = TEXTURE_NAME_ACID2) then
601 begin
602 SetLength(Textures, Length(Textures)+1);
604 with Textures[High(Textures)] do
605 begin
606 TextureName := RecName;
608 if TextureName = TEXTURE_NAME_WATER then
609 TextureID := TEXTURE_SPECIAL_WATER
610 else
611 if TextureName = TEXTURE_NAME_ACID1 then
612 TextureID := TEXTURE_SPECIAL_ACID1
613 else
614 if TextureName = TEXTURE_NAME_ACID2 then
615 TextureID := TEXTURE_SPECIAL_ACID2;
617 Anim := False;
618 end;
620 result := High(Textures);
621 Exit;
622 end;
624 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
625 WADName := g_ExtractWadName(RecName);
627 WAD := TWADFile.Create();
629 if WADName <> '' then
630 WADName := GameDir+'/wads/'+WADName
631 else
632 WADName := Map;
634 WAD.ReadFile(WADName);
636 txname := RecName;
638 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
639 begin
640 FreeMem(TextureData);
641 RecName := 'COMMON\ALIEN';
642 end;
645 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
646 begin
647 SetLength(Textures, Length(Textures)+1);
648 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
649 Exit;
650 e_GetTextureSize(Textures[High(Textures)].TextureID,
651 @Textures[High(Textures)].Width,
652 @Textures[High(Textures)].Height);
653 FreeMem(TextureData);
654 Textures[High(Textures)].TextureName := {RecName}txname;
655 Textures[High(Textures)].Anim := False;
657 result := High(Textures);
658 end
659 else // Íåò òàêîãî ðåóñðñà â WAD'å
660 begin
661 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
662 if log then
663 begin
664 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
665 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
666 end;
667 end;
669 WAD.Free();
670 end;
672 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
673 var
674 WAD: TWADFile;
675 TextureWAD: PChar = nil;
676 TextData: Pointer = nil;
677 TextureData: Pointer = nil;
678 cfg: TConfig = nil;
679 WADName: String;
680 ResLength: Integer;
681 TextureResource: String;
682 _width, _height, _framecount, _speed: Integer;
683 _backanimation: Boolean;
684 //imgfmt: string;
685 ia: TDynImageDataArray = nil;
686 f, c, frdelay, frloop: Integer;
687 begin
688 result := -1;
690 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
692 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
693 WADName := g_ExtractWadName(RecName);
695 WAD := TWADFile.Create();
696 try
697 if WADName <> '' then
698 WADName := GameDir+'/wads/'+WADName
699 else
700 WADName := Map;
702 WAD.ReadFile(WADName);
704 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
705 begin
706 if log then
707 begin
708 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
709 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
710 end;
711 exit;
712 end;
714 {TEST
715 if WADName = Map then
716 begin
717 //FreeMem(TextureWAD);
718 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
719 end;
722 WAD.FreeWAD();
724 if ResLength < 6 then
725 begin
726 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
727 exit;
728 end;
730 // ýòî ïòèöà? ýòî ñàìîë¸ò?
731 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
732 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
733 begin
734 // íåò, ýòî ñóïåðìåí!
735 if not WAD.ReadMemory(TextureWAD, ResLength) then
736 begin
737 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
738 exit;
739 end;
741 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
742 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
743 begin
744 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
745 exit;
746 end;
748 cfg := TConfig.CreateMem(TextData, ResLength);
750 TextureResource := cfg.ReadStr('', 'resource', '');
751 if TextureResource = '' then
752 begin
753 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
754 exit;
755 end;
757 _width := cfg.ReadInt('', 'framewidth', 0);
758 _height := cfg.ReadInt('', 'frameheight', 0);
759 _framecount := cfg.ReadInt('', 'framecount', 0);
760 _speed := cfg.ReadInt('', 'waitcount', 0);
761 _backanimation := cfg.ReadBool('', 'backanimation', False);
763 cfg.Free();
764 cfg := nil;
766 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
767 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
768 begin
769 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
770 exit;
771 end;
773 WAD.Free();
774 WAD := nil;
776 SetLength(Textures, Length(Textures)+1);
777 with Textures[High(Textures)] do
778 begin
779 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
780 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
781 begin
782 TextureName := RecName;
783 Width := _width;
784 Height := _height;
785 Anim := True;
786 FramesCount := _framecount;
787 Speed := _speed;
788 result := High(Textures);
789 end
790 else
791 begin
792 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
793 end;
794 end;
795 end
796 else
797 begin
798 // try animated image
800 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
801 if length(imgfmt) = 0 then
802 begin
803 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
804 exit;
805 end;
807 GlobalMetadata.ClearMetaItems();
808 GlobalMetadata.ClearMetaItemsForSaving();
809 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
810 begin
811 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
812 exit;
813 end;
814 if length(ia) = 0 then
815 begin
816 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
817 exit;
818 end;
820 WAD.Free();
821 WAD := nil;
823 _width := ia[0].width;
824 _height := ia[0].height;
825 _framecount := length(ia);
826 _speed := 1;
827 _backanimation := false;
828 frdelay := -1;
829 frloop := -666;
830 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
831 begin
832 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
833 try
834 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
835 frdelay := f;
836 if f < 0 then f := 0;
837 // rounding ;-)
838 c := f mod 28;
839 if c < 13 then c := 0 else c := 1;
840 f := (f div 28)+c;
841 if f < 1 then f := 1 else if f > 255 then f := 255;
842 _speed := f;
843 except
844 end;
845 end;
846 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
847 begin
848 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
849 try
850 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
851 frloop := f;
852 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
853 except
854 end;
855 end;
856 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
857 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
858 f := ord(_backanimation);
859 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);
861 SetLength(Textures, Length(Textures)+1);
862 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
863 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
864 begin
865 Textures[High(Textures)].TextureName := RecName;
866 Textures[High(Textures)].Width := _width;
867 Textures[High(Textures)].Height := _height;
868 Textures[High(Textures)].Anim := True;
869 Textures[High(Textures)].FramesCount := length(ia);
870 Textures[High(Textures)].Speed := _speed;
871 result := High(Textures);
872 //writeln(' CREATED!');
873 end
874 else
875 begin
876 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
877 end;
878 end;
879 finally
880 for f := 0 to High(ia) do FreeImage(ia[f]);
881 WAD.Free();
882 cfg.Free();
883 if TextureWAD <> nil then FreeMem(TextureWAD);
884 if TextData <> nil then FreeMem(TextData);
885 if TextureData <> nil then FreeMem(TextureData);
886 end;
887 end;
889 procedure CreateItem(Item: TItemRec_1);
890 begin
891 if g_Game_IsClient then Exit;
893 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
894 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
895 Exit;
897 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
898 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
899 end;
901 procedure CreateArea(Area: TAreaRec_1);
902 var
903 a: Integer;
904 id: DWORD = 0;
905 begin
906 case Area.AreaType of
907 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
908 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
909 begin
910 SetLength(RespawnPoints, Length(RespawnPoints)+1);
911 with RespawnPoints[High(RespawnPoints)] do
912 begin
913 X := Area.X;
914 Y := Area.Y;
915 Direction := TDirection(Area.Direction);
917 case Area.AreaType of
918 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
919 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
920 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
921 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
922 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
923 end;
924 end;
925 end;
927 AREA_REDFLAG, AREA_BLUEFLAG:
928 begin
929 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
931 if FlagPoints[a] <> nil then Exit;
933 New(FlagPoints[a]);
935 with FlagPoints[a]^ do
936 begin
937 X := Area.X-FLAGRECT.X;
938 Y := Area.Y-FLAGRECT.Y;
939 Direction := TDirection(Area.Direction);
940 end;
942 with gFlags[a] do
943 begin
944 case a of
945 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
946 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
947 end;
949 Animation := TAnimation.Create(id, True, 8);
950 Obj.Rect := FLAGRECT;
952 g_Map_ResetFlag(a);
953 end;
954 end;
956 AREA_DOMFLAG:
957 begin
958 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
959 with DOMFlagPoints[High(DOMFlagPoints)] do
960 begin
961 X := Area.X;
962 Y := Area.Y;
963 Direction := TDirection(Area.Direction);
964 end;
966 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
967 end;
968 end;
969 end;
971 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
972 var
973 _trigger: TTrigger;
974 begin
975 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
977 with _trigger do
978 begin
979 X := Trigger.X;
980 Y := Trigger.Y;
981 Width := Trigger.Width;
982 Height := Trigger.Height;
983 Enabled := ByteBool(Trigger.Enabled);
984 TexturePanel := Trigger.TexturePanel;
985 TexturePanelType := fTexturePanel1Type;
986 ShotPanelType := fTexturePanel2Type;
987 TriggerType := Trigger.TriggerType;
988 ActivateType := Trigger.ActivateType;
989 Keys := Trigger.Keys;
990 Data.Default := Trigger.DATA;
991 end;
993 g_Triggers_Create(_trigger);
994 end;
996 procedure CreateMonster(monster: TMonsterRec_1);
997 var
998 a: Integer;
999 mon: TMonster;
1000 begin
1001 if g_Game_IsClient then Exit;
1003 if (gGameSettings.GameType = GT_SINGLE)
1004 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1005 begin
1006 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1008 if gTriggers <> nil then
1009 begin
1010 for a := 0 to High(gTriggers) do
1011 begin
1012 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1013 begin
1014 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1015 end;
1016 end;
1017 end;
1019 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1020 end;
1021 end;
1023 procedure g_Map_ReAdd_DieTriggers();
1025 function monsDieTrig (mon: TMonster): Boolean;
1026 var
1027 a: Integer;
1028 begin
1029 result := false; // don't stop
1030 mon.ClearTriggers();
1031 for a := 0 to High(gTriggers) do
1032 begin
1033 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1034 begin
1035 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1036 end;
1037 end;
1038 end;
1040 begin
1041 if g_Game_IsClient then Exit;
1043 g_Mons_ForEach(monsDieTrig);
1044 end;
1046 function extractWadName(resourceName: string): string;
1047 var
1048 posN: Integer;
1049 begin
1050 posN := Pos(':', resourceName);
1051 if posN > 0 then
1052 Result:= Copy(resourceName, 0, posN-1)
1053 else
1054 Result := '';
1055 end;
1057 procedure addResToExternalResList(res: string);
1058 begin
1059 res := extractWadName(res);
1060 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1061 gExternalResources.Add(res);
1062 end;
1064 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1065 var
1066 textures: TTexturesRec1Array;
1067 mapHeader: TMapHeaderRec_1;
1068 i: integer;
1069 resFile: String = '';
1070 begin
1071 if gExternalResources = nil then
1072 gExternalResources := TStringList.Create;
1074 gExternalResources.Clear;
1075 textures := mapReader.GetTextures();
1076 for i := 0 to High(textures) do
1077 begin
1078 addResToExternalResList(resFile);
1079 end;
1081 textures := nil;
1083 mapHeader := mapReader.GetMapHeader;
1085 addResToExternalResList(mapHeader.MusicName);
1086 addResToExternalResList(mapHeader.SkyName);
1087 end;
1090 procedure mapCreateGrid ();
1091 var
1092 mapX0: Integer = $3fffffff;
1093 mapY0: Integer = $3fffffff;
1094 mapX1: Integer = -$3fffffff;
1095 mapY1: Integer = -$3fffffff;
1097 procedure calcBoundingBox (constref panels: TPanelArray);
1098 var
1099 idx: Integer;
1100 pan: TPanel;
1101 begin
1102 for idx := 0 to High(panels) do
1103 begin
1104 pan := panels[idx];
1105 if not pan.visvalid then continue;
1106 if (pan.Width < 1) or (pan.Height < 1) then continue;
1107 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1108 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1109 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1110 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1111 end;
1112 end;
1114 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1115 var
1116 idx: Integer;
1117 pan: TPanel;
1118 begin
1119 tag := panelTypeToTag(tag);
1120 for idx := High(panels) downto 0 do
1121 begin
1122 pan := panels[idx];
1123 pan.tag := tag;
1124 if not pan.visvalid then continue;
1125 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1126 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1127 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1128 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1129 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1130 begin
1131 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1132 end;
1133 {$ENDIF}
1134 end;
1135 end;
1137 begin
1138 mapGrid.Free();
1139 mapGrid := nil;
1141 calcBoundingBox(gWalls);
1142 calcBoundingBox(gRenderBackgrounds);
1143 calcBoundingBox(gRenderForegrounds);
1144 calcBoundingBox(gWater);
1145 calcBoundingBox(gAcid1);
1146 calcBoundingBox(gAcid2);
1147 calcBoundingBox(gSteps);
1148 calcBoundingBox(gLifts);
1149 calcBoundingBox(gBlockMon);
1151 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1153 mapGrid := TPanelGrid.Create(mapX0-512, mapY0-512, mapX1-mapX0+1+512*2, mapY1-mapY0+1+512*2);
1155 addPanelsToGrid(gWalls, PANEL_WALL);
1156 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1157 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1158 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1159 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1160 addPanelsToGrid(gWater, PANEL_WATER);
1161 addPanelsToGrid(gAcid1, PANEL_ACID1);
1162 addPanelsToGrid(gAcid2, PANEL_ACID2);
1163 addPanelsToGrid(gSteps, PANEL_STEP);
1164 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1165 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1167 mapGrid.dumpStats();
1169 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1170 end;
1173 function g_Map_Load(Res: String): Boolean;
1174 const
1175 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1176 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1177 var
1178 WAD: TWADFile;
1179 MapReader: TMapReader_1;
1180 Header: TMapHeaderRec_1;
1181 _textures: TTexturesRec1Array;
1182 _texnummap: array of Integer; // `_textures` -> `Textures`
1183 panels: TPanelsRec1Array;
1184 items: TItemsRec1Array;
1185 monsters: TMonsterRec1Array;
1186 areas: TAreasRec1Array;
1187 triggers: TTriggersRec1Array;
1188 a, b, c, k: Integer;
1189 PanelID: DWORD;
1190 AddTextures: TAddTextureArray;
1191 texture: TTextureRec_1;
1192 TriggersTable: Array of record
1193 TexturePanel: Integer;
1194 LiftPanel: Integer;
1195 DoorPanel: Integer;
1196 ShotPanel: Integer;
1197 end;
1198 FileName, mapResName, s, TexName: String;
1199 Data: Pointer;
1200 Len: Integer;
1201 ok, isAnim, trigRef: Boolean;
1202 CurTex, ntn: Integer;
1204 begin
1205 mapGrid.Free();
1206 mapGrid := nil;
1207 //mapTree.Free();
1208 //mapTree := nil;
1210 Result := False;
1211 gMapInfo.Map := Res;
1212 TriggersTable := nil;
1213 FillChar(texture, SizeOf(texture), 0);
1215 sfsGCDisable(); // temporary disable removing of temporary volumes
1216 try
1217 // Çàãðóçêà WAD:
1218 FileName := g_ExtractWadName(Res);
1219 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1220 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1222 WAD := TWADFile.Create();
1223 if not WAD.ReadFile(FileName) then
1224 begin
1225 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1226 WAD.Free();
1227 Exit;
1228 end;
1229 //k8: why loader ignores path here?
1230 mapResName := g_ExtractFileName(Res);
1231 if not WAD.GetMapResource(mapResName, Data, Len) then
1232 begin
1233 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1234 WAD.Free();
1235 Exit;
1236 end;
1238 WAD.Free();
1240 // Çàãðóçêà êàðòû:
1241 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1242 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1243 MapReader := TMapReader_1.Create();
1245 if not MapReader.LoadMap(Data) then
1246 begin
1247 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1248 FreeMem(Data);
1249 MapReader.Free();
1250 Exit;
1251 end;
1253 FreeMem(Data);
1254 generateExternalResourcesList(MapReader);
1255 // Çàãðóçêà òåêñòóð:
1256 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1257 _textures := MapReader.GetTextures();
1258 _texnummap := nil;
1260 // Äîáàâëåíèå òåêñòóð â Textures[]:
1261 if _textures <> nil then
1262 begin
1263 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1264 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1265 SetLength(_texnummap, length(_textures));
1267 for a := 0 to High(_textures) do
1268 begin
1269 SetLength(s, 64);
1270 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1271 for b := 1 to Length(s) do
1272 if s[b] = #0 then
1273 begin
1274 SetLength(s, b-1);
1275 Break;
1276 end;
1277 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1278 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1279 // Àíèìèðîâàííàÿ òåêñòóðà:
1280 if ByteBool(_textures[a].Anim) then
1281 begin
1282 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1283 if ntn < 0 then
1284 begin
1285 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1286 ntn := CreateNullTexture(_textures[a].Resource);
1287 end;
1288 end
1289 else // Îáû÷íàÿ òåêñòóðà:
1290 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1291 if ntn < 0 then
1292 begin
1293 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1294 ntn := CreateNullTexture(_textures[a].Resource);
1295 end;
1297 _texnummap[a] := ntn; // fix texture number
1298 g_Game_StepLoading();
1299 end;
1300 end;
1302 // Çàãðóçêà òðèããåðîâ:
1303 gTriggerClientID := 0;
1304 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1305 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1306 triggers := MapReader.GetTriggers();
1308 // Çàãðóçêà ïàíåëåé:
1309 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1310 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1311 panels := MapReader.GetPanels();
1313 // check texture numbers for panels
1314 for a := 0 to High(panels) do
1315 begin
1316 if panels[a].TextureNum > High(_textures) then
1317 begin
1318 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1319 result := false;
1320 exit;
1321 end;
1322 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1323 end;
1325 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1326 if triggers <> nil then
1327 begin
1328 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1329 SetLength(TriggersTable, Length(triggers));
1330 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1332 for a := 0 to High(TriggersTable) do
1333 begin
1334 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1335 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1336 // Ëèôòû:
1337 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1338 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1339 else
1340 TriggersTable[a].LiftPanel := -1;
1341 // Äâåðè:
1342 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1343 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1344 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1345 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1346 else
1347 TriggersTable[a].DoorPanel := -1;
1348 // Òóðåëü:
1349 if triggers[a].TriggerType = TRIGGER_SHOT then
1350 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1351 else
1352 TriggersTable[a].ShotPanel := -1;
1354 g_Game_StepLoading();
1355 end;
1356 end;
1358 // Ñîçäàåì ïàíåëè:
1359 if panels <> nil then
1360 begin
1361 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1362 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1364 for a := 0 to High(panels) do
1365 begin
1366 SetLength(AddTextures, 0);
1367 trigRef := False;
1368 CurTex := -1;
1369 if _textures <> nil then
1370 begin
1371 texture := _textures[panels[a].TextureNum];
1372 ok := True;
1373 end
1374 else
1375 ok := False;
1377 if ok then
1378 begin
1379 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1380 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1381 ok := False;
1382 if (TriggersTable <> nil) and (_textures <> nil) then
1383 for b := 0 to High(TriggersTable) do
1384 if (TriggersTable[b].TexturePanel = a)
1385 or (TriggersTable[b].ShotPanel = a) then
1386 begin
1387 trigRef := True;
1388 ok := True;
1389 Break;
1390 end;
1391 end;
1393 if ok then
1394 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1395 SetLength(s, 64);
1396 CopyMemory(@s[1], @texture.Resource[0], 64);
1397 // Èçìåðÿåì äëèíó:
1398 Len := Length(s);
1399 for c := Len downto 1 do
1400 if s[c] <> #0 then
1401 begin
1402 Len := c;
1403 Break;
1404 end;
1405 SetLength(s, Len);
1407 // Ñïåö-òåêñòóðû çàïðåùåíû:
1408 if g_Map_IsSpecialTexture(s) then
1409 ok := False
1410 else
1411 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1412 ok := g_Texture_NumNameFindStart(s);
1414 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1415 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1416 if ok then
1417 begin
1418 k := NNF_NAME_BEFORE;
1419 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1420 while ok or (k = NNF_NAME_BEFORE) or
1421 (k = NNF_NAME_EQUALS) do
1422 begin
1423 k := g_Texture_NumNameFindNext(TexName);
1425 if (k = NNF_NAME_BEFORE) or
1426 (k = NNF_NAME_AFTER) then
1427 begin
1428 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1429 if ByteBool(texture.Anim) then
1430 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1431 isAnim := True;
1432 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1433 if not ok then
1434 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1435 isAnim := False;
1436 ok := CreateTexture(TexName, FileName, False) >= 0;
1437 end;
1438 end
1439 else
1440 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1441 isAnim := False;
1442 ok := CreateTexture(TexName, FileName, False) >= 0;
1443 if not ok then
1444 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1445 isAnim := True;
1446 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1447 end;
1448 end;
1450 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1451 if ok then
1452 begin
1453 for c := 0 to High(Textures) do
1454 if Textures[c].TextureName = TexName then
1455 begin
1456 SetLength(AddTextures, Length(AddTextures)+1);
1457 AddTextures[High(AddTextures)].Texture := c;
1458 AddTextures[High(AddTextures)].Anim := isAnim;
1459 Break;
1460 end;
1461 end;
1462 end
1463 else
1464 if k = NNF_NAME_EQUALS then
1465 begin
1466 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1467 SetLength(AddTextures, Length(AddTextures)+1);
1468 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1469 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1470 CurTex := High(AddTextures);
1471 ok := True;
1472 end
1473 else // NNF_NO_NAME
1474 ok := False;
1475 end; // while ok...
1477 ok := True;
1478 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1479 end; // if ok - ññûëàþòñÿ òðèããåðû
1481 if not ok then
1482 begin
1483 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1484 SetLength(AddTextures, 1);
1485 AddTextures[0].Texture := panels[a].TextureNum;
1486 AddTextures[0].Anim := ByteBool(texture.Anim);
1487 CurTex := 0;
1488 end;
1490 //e_WriteLog(Format('panel #%d: TextureNum=%d; ht=%d; ht1=%d; atl=%d', [a, panels[a].TextureNum, High(_textures), High(Textures), High(AddTextures)]), MSG_NOTIFY);
1492 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1493 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1495 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1496 if TriggersTable <> nil then
1497 for b := 0 to High(TriggersTable) do
1498 begin
1499 // Òðèããåð äâåðè/ëèôòà:
1500 if (TriggersTable[b].LiftPanel = a) or
1501 (TriggersTable[b].DoorPanel = a) then
1502 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1503 // Òðèããåð ñìåíû òåêñòóðû:
1504 if TriggersTable[b].TexturePanel = a then
1505 triggers[b].TexturePanel := PanelID;
1506 // Òðèããåð "Òóðåëü":
1507 if TriggersTable[b].ShotPanel = a then
1508 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1509 end;
1511 g_Game_StepLoading();
1512 end;
1513 end;
1515 // create map grid, init other grids (for monsters, for example)
1516 e_WriteLog('Creating map grid', MSG_NOTIFY);
1517 mapCreateGrid();
1520 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1521 if (triggers <> nil) and not gLoadGameMode then
1522 begin
1523 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1524 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1525 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1526 for a := 0 to High(triggers) do
1527 begin
1528 if triggers[a].TexturePanel <> -1 then
1529 b := panels[TriggersTable[a].TexturePanel].PanelType
1530 else
1531 b := 0;
1532 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1533 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1534 c := panels[TriggersTable[a].ShotPanel].PanelType
1535 else
1536 c := 0;
1537 CreateTrigger(triggers[a], b, c);
1538 end;
1539 end;
1541 // Çàãðóçêà ïðåäìåòîâ:
1542 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1543 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1544 items := MapReader.GetItems();
1546 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1547 if (items <> nil) and not gLoadGameMode then
1548 begin
1549 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1550 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1551 for a := 0 to High(items) do
1552 CreateItem(Items[a]);
1553 end;
1555 // Çàãðóçêà îáëàñòåé:
1556 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1557 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1558 areas := MapReader.GetAreas();
1560 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1561 if areas <> nil then
1562 begin
1563 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1564 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1565 for a := 0 to High(areas) do
1566 CreateArea(areas[a]);
1567 end;
1569 // Çàãðóçêà ìîíñòðîâ:
1570 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1571 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1572 monsters := MapReader.GetMonsters();
1574 gTotalMonsters := 0;
1576 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1577 if (monsters <> nil) and not gLoadGameMode then
1578 begin
1579 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1580 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1581 for a := 0 to High(monsters) do
1582 CreateMonster(monsters[a]);
1583 end;
1585 // Çàãðóçêà îïèñàíèÿ êàðòû:
1586 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1587 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1588 Header := MapReader.GetMapHeader();
1590 MapReader.Free();
1592 with gMapInfo do
1593 begin
1594 Name := Header.MapName;
1595 Description := Header.MapDescription;
1596 Author := Header.MapAuthor;
1597 MusicName := Header.MusicName;
1598 SkyName := Header.SkyName;
1599 Height := Header.Height;
1600 Width := Header.Width;
1601 end;
1603 // Çàãðóçêà íåáà:
1604 if gMapInfo.SkyName <> '' then
1605 begin
1606 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1607 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1608 FileName := g_ExtractWadName(gMapInfo.SkyName);
1610 if FileName <> '' then
1611 FileName := GameDir+'/wads/'+FileName
1612 else
1613 begin
1614 FileName := g_ExtractWadName(Res);
1615 end;
1617 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1618 if g_Texture_CreateWAD(BackID, s) then
1619 begin
1620 g_Game_SetupScreenSize();
1621 end
1622 else
1623 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1624 end;
1626 // Çàãðóçêà ìóçûêè:
1627 ok := False;
1628 if gMapInfo.MusicName <> '' then
1629 begin
1630 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1631 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1632 FileName := g_ExtractWadName(gMapInfo.MusicName);
1634 if FileName <> '' then
1635 FileName := GameDir+'/wads/'+FileName
1636 else
1637 begin
1638 FileName := g_ExtractWadName(Res);
1639 end;
1641 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1642 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1643 ok := True
1644 else
1645 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1646 end;
1648 // Îñòàëüíûå óñòàíâêè:
1649 CreateDoorMap();
1650 CreateLiftMap();
1652 g_Items_Init();
1653 g_Weapon_Init();
1654 g_Monsters_Init();
1656 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1657 if not gLoadGameMode then
1658 g_GFX_Init();
1660 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1661 _textures := nil;
1662 panels := nil;
1663 items := nil;
1664 areas := nil;
1665 triggers := nil;
1666 TriggersTable := nil;
1667 AddTextures := nil;
1669 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1670 if ok and (not gLoadGameMode) then
1671 begin
1672 gMusic.SetByName(gMapInfo.MusicName);
1673 gMusic.Play();
1674 end
1675 else
1676 gMusic.SetByName('');
1677 finally
1678 sfsGCEnable(); // enable releasing unused volumes
1679 end;
1681 e_WriteLog('Done loading map.', MSG_NOTIFY);
1682 Result := True;
1683 end;
1685 function g_Map_GetMapInfo(Res: String): TMapInfo;
1686 var
1687 WAD: TWADFile;
1688 MapReader: TMapReader_1;
1689 Header: TMapHeaderRec_1;
1690 FileName: String;
1691 Data: Pointer;
1692 Len: Integer;
1693 begin
1694 FillChar(Result, SizeOf(Result), 0);
1695 FileName := g_ExtractWadName(Res);
1697 WAD := TWADFile.Create();
1698 if not WAD.ReadFile(FileName) then
1699 begin
1700 WAD.Free();
1701 Exit;
1702 end;
1704 //k8: it ignores path again
1705 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1706 begin
1707 WAD.Free();
1708 Exit;
1709 end;
1711 WAD.Free();
1713 MapReader := TMapReader_1.Create();
1715 if not MapReader.LoadMap(Data) then
1716 begin
1717 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1718 ZeroMemory(@Header, SizeOf(Header));
1719 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1720 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1721 end
1722 else
1723 begin
1724 Header := MapReader.GetMapHeader();
1725 Result.Name := Header.MapName;
1726 Result.Description := Header.MapDescription;
1727 end;
1729 FreeMem(Data);
1730 MapReader.Free();
1732 Result.Map := Res;
1733 Result.Author := Header.MapAuthor;
1734 Result.Height := Header.Height;
1735 Result.Width := Header.Width;
1736 end;
1738 function g_Map_GetMapsList(WADName: string): SArray;
1739 var
1740 WAD: TWADFile;
1741 a: Integer;
1742 ResList: SArray;
1743 begin
1744 Result := nil;
1745 WAD := TWADFile.Create();
1746 if not WAD.ReadFile(WADName) then
1747 begin
1748 WAD.Free();
1749 Exit;
1750 end;
1751 ResList := WAD.GetMapResources();
1752 if ResList <> nil then
1753 begin
1754 for a := 0 to High(ResList) do
1755 begin
1756 SetLength(Result, Length(Result)+1);
1757 Result[High(Result)] := ResList[a];
1758 end;
1759 end;
1760 WAD.Free();
1761 end;
1763 function g_Map_Exist(Res: string): Boolean;
1764 var
1765 WAD: TWADFile;
1766 FileName, mnn: string;
1767 ResList: SArray;
1768 a: Integer;
1769 begin
1770 Result := False;
1772 FileName := addWadExtension(g_ExtractWadName(Res));
1774 WAD := TWADFile.Create;
1775 if not WAD.ReadFile(FileName) then
1776 begin
1777 WAD.Free();
1778 Exit;
1779 end;
1781 ResList := WAD.GetMapResources();
1782 WAD.Free();
1784 mnn := g_ExtractFileName(Res);
1785 if ResList <> nil then
1786 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1787 begin
1788 Result := True;
1789 Exit;
1790 end;
1791 end;
1793 procedure g_Map_Free();
1794 var
1795 a: Integer;
1797 procedure FreePanelArray(var panels: TPanelArray);
1798 var
1799 i: Integer;
1801 begin
1802 if panels <> nil then
1803 begin
1804 for i := 0 to High(panels) do
1805 panels[i].Free();
1806 panels := nil;
1807 end;
1808 end;
1810 begin
1811 g_GFX_Free();
1812 g_Weapon_Free();
1813 g_Items_Free();
1814 g_Triggers_Free();
1815 g_Monsters_Free();
1817 RespawnPoints := nil;
1818 if FlagPoints[FLAG_RED] <> nil then
1819 begin
1820 Dispose(FlagPoints[FLAG_RED]);
1821 FlagPoints[FLAG_RED] := nil;
1822 end;
1823 if FlagPoints[FLAG_BLUE] <> nil then
1824 begin
1825 Dispose(FlagPoints[FLAG_BLUE]);
1826 FlagPoints[FLAG_BLUE] := nil;
1827 end;
1828 //DOMFlagPoints := nil;
1830 //gDOMFlags := nil;
1832 if Textures <> nil then
1833 begin
1834 for a := 0 to High(Textures) do
1835 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1836 if Textures[a].Anim then
1837 g_Frames_DeleteByID(Textures[a].FramesID)
1838 else
1839 if Textures[a].TextureID <> TEXTURE_NONE then
1840 e_DeleteTexture(Textures[a].TextureID);
1842 Textures := nil;
1843 end;
1845 FreePanelArray(gWalls);
1846 FreePanelArray(gRenderBackgrounds);
1847 FreePanelArray(gRenderForegrounds);
1848 FreePanelArray(gWater);
1849 FreePanelArray(gAcid1);
1850 FreePanelArray(gAcid2);
1851 FreePanelArray(gSteps);
1852 FreePanelArray(gLifts);
1853 FreePanelArray(gBlockMon);
1855 if BackID <> DWORD(-1) then
1856 begin
1857 gBackSize.X := 0;
1858 gBackSize.Y := 0;
1859 e_DeleteTexture(BackID);
1860 BackID := DWORD(-1);
1861 end;
1863 g_Game_StopAllSounds(False);
1864 gMusic.FreeSound();
1865 g_Sound_Delete(gMapInfo.MusicName);
1867 gMapInfo.Name := '';
1868 gMapInfo.Description := '';
1869 gMapInfo.MusicName := '';
1870 gMapInfo.Height := 0;
1871 gMapInfo.Width := 0;
1873 gDoorMap := nil;
1874 gLiftMap := nil;
1876 PanelByID := nil;
1877 end;
1879 procedure g_Map_Update();
1880 var
1881 a, d, j: Integer;
1882 m: Word;
1883 s: String;
1885 procedure UpdatePanelArray(var panels: TPanelArray);
1886 var
1887 i: Integer;
1889 begin
1890 if panels <> nil then
1891 for i := 0 to High(panels) do
1892 panels[i].Update();
1893 end;
1895 begin
1896 UpdatePanelArray(gWalls);
1897 UpdatePanelArray(gRenderBackgrounds);
1898 UpdatePanelArray(gRenderForegrounds);
1899 UpdatePanelArray(gWater);
1900 UpdatePanelArray(gAcid1);
1901 UpdatePanelArray(gAcid2);
1902 UpdatePanelArray(gSteps);
1904 if gGameSettings.GameMode = GM_CTF then
1905 begin
1906 for a := FLAG_RED to FLAG_BLUE do
1907 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1908 with gFlags[a] do
1909 begin
1910 if gFlags[a].Animation <> nil then
1911 gFlags[a].Animation.Update();
1913 m := g_Obj_Move(@Obj, True, True);
1915 if gTime mod (GAME_TICK*2) <> 0 then
1916 Continue;
1918 // Ñîïðîòèâëåíèå âîçäóõà:
1919 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1921 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1922 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1923 begin
1924 g_Map_ResetFlag(a);
1925 gFlags[a].CaptureTime := 0;
1926 if a = FLAG_RED then
1927 s := _lc[I_PLAYER_FLAG_RED]
1928 else
1929 s := _lc[I_PLAYER_FLAG_BLUE];
1930 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1932 if g_Game_IsNet then
1933 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1934 Continue;
1935 end;
1937 if Count > 0 then
1938 Count := Count - 1;
1940 // Èãðîê áåðåò ôëàã:
1941 if gPlayers <> nil then
1942 begin
1943 j := Random(Length(gPlayers)) - 1;
1945 for d := 0 to High(gPlayers) do
1946 begin
1947 Inc(j);
1948 if j > High(gPlayers) then
1949 j := 0;
1951 if gPlayers[j] <> nil then
1952 if gPlayers[j].Live and
1953 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1954 begin
1955 if gPlayers[j].GetFlag(a) then
1956 Break;
1957 end;
1958 end;
1959 end;
1960 end;
1961 end;
1962 end;
1965 // old algo
1966 procedure g_Map_DrawPanels (PanelType: Word);
1968 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
1969 var
1970 idx: Integer;
1971 begin
1972 if (panels <> nil) then
1973 begin
1974 // alas, no visible set
1975 for idx := 0 to High(panels) do
1976 begin
1977 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1978 end;
1979 end;
1980 end;
1982 begin
1983 case PanelType of
1984 PANEL_WALL: DrawPanels(gWalls);
1985 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
1986 PANEL_BACK: DrawPanels(gRenderBackgrounds);
1987 PANEL_FORE: DrawPanels(gRenderForegrounds);
1988 PANEL_WATER: DrawPanels(gWater);
1989 PANEL_ACID1: DrawPanels(gAcid1);
1990 PANEL_ACID2: DrawPanels(gAcid2);
1991 PANEL_STEP: DrawPanels(gSteps);
1992 end;
1993 end;
1996 // new algo
1997 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
1999 function checker (pan: TPanel; tag: Integer): Boolean;
2000 begin
2001 result := false; // don't stop, ever
2002 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2003 gDrawPanelList.insert(pan);
2004 end;
2006 begin
2007 dplClear();
2008 //tagmask := panelTypeToTag(PanelType);
2010 {if gdbg_map_use_tree_draw then
2011 begin
2012 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2013 end
2014 else}
2015 begin
2016 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask, true);
2017 end;
2018 // list will be rendered in `g_game.DrawPlayer()`
2019 end;
2022 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2024 function checker (pan: TPanel; tag: Integer): Boolean;
2025 begin
2026 result := false; // don't stop, ever
2027 pan.DrawShadowVolume(lightX, lightY, radius);
2028 end;
2030 begin
2031 {if gdbg_map_use_tree_draw then
2032 begin
2033 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2034 end
2035 else}
2036 begin
2037 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor), true);
2038 end;
2039 end;
2042 procedure g_Map_DrawBack(dx, dy: Integer);
2043 begin
2044 if gDrawBackGround and (BackID <> DWORD(-1)) then
2045 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2046 else
2047 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2048 end;
2050 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2051 PanelType: Word; b1x3: Boolean=false): Boolean;
2052 var
2053 a, h: Integer;
2054 begin
2055 Result := False;
2057 if WordBool(PanelType and PANEL_WALL) then
2058 if gWalls <> nil then
2059 begin
2060 h := High(gWalls);
2062 for a := 0 to h do
2063 if gWalls[a].Enabled and
2064 g_Collide(X, Y, Width, Height,
2065 gWalls[a].X, gWalls[a].Y,
2066 gWalls[a].Width, gWalls[a].Height) then
2067 begin
2068 Result := True;
2069 Exit;
2070 end;
2071 end;
2073 if WordBool(PanelType and PANEL_WATER) then
2074 if gWater <> nil then
2075 begin
2076 h := High(gWater);
2078 for a := 0 to h do
2079 if g_Collide(X, Y, Width, Height,
2080 gWater[a].X, gWater[a].Y,
2081 gWater[a].Width, gWater[a].Height) then
2082 begin
2083 Result := True;
2084 Exit;
2085 end;
2086 end;
2088 if WordBool(PanelType and PANEL_ACID1) then
2089 if gAcid1 <> nil then
2090 begin
2091 h := High(gAcid1);
2093 for a := 0 to h do
2094 if g_Collide(X, Y, Width, Height,
2095 gAcid1[a].X, gAcid1[a].Y,
2096 gAcid1[a].Width, gAcid1[a].Height) then
2097 begin
2098 Result := True;
2099 Exit;
2100 end;
2101 end;
2103 if WordBool(PanelType and PANEL_ACID2) then
2104 if gAcid2 <> nil then
2105 begin
2106 h := High(gAcid2);
2108 for a := 0 to h do
2109 if g_Collide(X, Y, Width, Height,
2110 gAcid2[a].X, gAcid2[a].Y,
2111 gAcid2[a].Width, gAcid2[a].Height) then
2112 begin
2113 Result := True;
2114 Exit;
2115 end;
2116 end;
2118 if WordBool(PanelType and PANEL_STEP) then
2119 if gSteps <> nil then
2120 begin
2121 h := High(gSteps);
2123 for a := 0 to h do
2124 if g_Collide(X, Y, Width, Height,
2125 gSteps[a].X, gSteps[a].Y,
2126 gSteps[a].Width, gSteps[a].Height) then
2127 begin
2128 Result := True;
2129 Exit;
2130 end;
2131 end;
2133 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2134 if gLifts <> nil then
2135 begin
2136 h := High(gLifts);
2138 for a := 0 to h do
2139 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2140 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2141 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2142 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2143 g_Collide(X, Y, Width, Height,
2144 gLifts[a].X, gLifts[a].Y,
2145 gLifts[a].Width, gLifts[a].Height) then
2146 begin
2147 Result := True;
2148 Exit;
2149 end;
2150 end;
2152 if WordBool(PanelType and PANEL_BLOCKMON) then
2153 if gBlockMon <> nil then
2154 begin
2155 h := High(gBlockMon);
2157 for a := 0 to h do
2158 if ( (not b1x3) or
2159 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2160 g_Collide(X, Y, Width, Height,
2161 gBlockMon[a].X, gBlockMon[a].Y,
2162 gBlockMon[a].Width, gBlockMon[a].Height) then
2163 begin
2164 Result := True;
2165 Exit;
2166 end;
2167 end;
2168 end;
2170 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2171 var
2172 texid: DWORD;
2174 function checkPanels (constref panels: TPanelArray): Boolean;
2175 var
2176 a: Integer;
2177 begin
2178 result := false;
2179 if panels = nil then exit;
2180 for a := 0 to High(panels) do
2181 begin
2182 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2183 begin
2184 result := true;
2185 texid := panels[a].GetTextureID();
2186 exit;
2187 end;
2188 end;
2189 end;
2191 begin
2192 texid := TEXTURE_NONE;
2193 result := texid;
2194 if not checkPanels(gWater) then
2195 if not checkPanels(gAcid1) then
2196 if not checkPanels(gAcid2) then exit;
2197 result := texid;
2198 end;
2201 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2202 const
2203 SlowMask = GridTagLift or GridTagBlockMon;
2204 function checker (pan: TPanel; tag: Integer): Boolean;
2205 begin
2207 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2208 begin
2209 result := pan.Enabled;
2210 exit;
2211 end;
2214 if ((tag and GridTagLift) <> 0) then
2215 begin
2216 result :=
2217 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2218 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2219 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2220 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2221 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2222 exit;
2223 end;
2225 if ((tag and GridTagBlockMon) <> 0) then
2226 begin
2227 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2228 exit;
2229 end;
2231 // other shit
2232 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2233 result := true; // i found her!
2234 end;
2236 var
2237 tagmask: Integer = 0;
2238 begin
2239 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2240 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2241 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2242 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2243 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2244 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2245 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2247 if (tagmask = 0) then begin result := false; exit; end; // just in case
2249 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2250 if gdbg_map_use_accel_coldet then
2251 begin
2252 {if gdbg_map_use_tree_coldet then
2253 begin
2254 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2255 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2256 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2257 begin
2258 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2259 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2260 end;
2261 end
2262 else}
2263 begin
2264 if (Width = 1) and (Height = 1) then
2265 begin
2266 if ((tagmask and SlowMask) <> 0) then
2267 begin
2268 // slow
2269 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2270 end
2271 else
2272 begin
2273 // fast
2274 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2275 end;
2276 end
2277 else
2278 begin
2279 if ((tagmask and SlowMask) <> 0) then
2280 begin
2281 // slow
2282 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2283 end
2284 else
2285 begin
2286 // fast
2287 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2288 end;
2289 end;
2290 end;
2291 end
2292 else
2293 begin
2294 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2295 end;
2296 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2297 end;
2300 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2301 var
2302 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2303 texid: DWORD;
2305 // slightly different from the old code, but meh...
2306 function checker (pan: TPanel; tag: Integer): Boolean;
2307 begin
2308 result := false; // don't stop, ever
2309 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2310 // check priorities
2311 case cctype of
2312 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2313 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2314 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2315 end;
2316 // collision?
2317 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2318 // yeah
2319 texid := pan.GetTextureID();
2320 // water? water has the highest priority, so stop right here
2321 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2322 // acid2?
2323 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2324 // acid1?
2325 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2326 end;
2328 begin
2329 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2330 if gdbg_map_use_accel_coldet then
2331 begin
2332 texid := TEXTURE_NONE;
2333 {if gdbg_map_use_tree_coldet then
2334 begin
2335 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2336 end
2337 else}
2338 begin
2339 if (Width = 1) and (Height = 1) then
2340 begin
2341 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2342 end
2343 else
2344 begin
2345 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2346 end;
2347 end;
2348 result := texid;
2349 end
2350 else
2351 begin
2352 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2353 end;
2354 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2355 end;
2358 procedure g_Map_EnableWall(ID: DWORD);
2359 var
2360 pan: TPanel;
2361 begin
2362 pan := gWalls[ID];
2363 pan.Enabled := True;
2364 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2366 mapGrid.proxyEnabled[pan.proxyId] := true;
2367 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2368 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2370 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2372 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2373 e_WriteLog(Format('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);
2374 {$ENDIF}
2375 end;
2377 procedure g_Map_DisableWall(ID: DWORD);
2378 var
2379 pan: TPanel;
2380 begin
2381 pan := gWalls[ID];
2382 pan.Enabled := False;
2383 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2385 mapGrid.proxyEnabled[pan.proxyId] := false;
2386 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2388 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2390 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2391 e_WriteLog(Format('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);
2392 {$ENDIF}
2393 end;
2395 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2396 var
2397 tp: TPanel;
2398 begin
2399 case PanelType of
2400 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2401 tp := gWalls[ID];
2402 PANEL_FORE:
2403 tp := gRenderForegrounds[ID];
2404 PANEL_BACK:
2405 tp := gRenderBackgrounds[ID];
2406 PANEL_WATER:
2407 tp := gWater[ID];
2408 PANEL_ACID1:
2409 tp := gAcid1[ID];
2410 PANEL_ACID2:
2411 tp := gAcid2[ID];
2412 PANEL_STEP:
2413 tp := gSteps[ID];
2414 else
2415 Exit;
2416 end;
2418 tp.NextTexture(AnimLoop);
2419 if g_Game_IsServer and g_Game_IsNet then
2420 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2421 end;
2423 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2424 begin
2425 if gLifts[ID].LiftType = t then
2426 Exit;
2428 with gLifts[ID] do
2429 begin
2430 LiftType := t;
2432 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2433 //TODO: make separate lift tags, and change tag here
2435 if LiftType = 0 then
2436 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2437 else if LiftType = 1 then
2438 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2439 else if LiftType = 2 then
2440 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2441 else if LiftType = 3 then
2442 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2444 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2445 end;
2446 end;
2448 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2449 var
2450 a: Integer;
2451 PointsArray: Array of TRespawnPoint;
2452 begin
2453 Result := False;
2454 SetLength(PointsArray, 0);
2456 if RespawnPoints = nil then
2457 Exit;
2459 for a := 0 to High(RespawnPoints) do
2460 if RespawnPoints[a].PointType = PointType then
2461 begin
2462 SetLength(PointsArray, Length(PointsArray)+1);
2463 PointsArray[High(PointsArray)] := RespawnPoints[a];
2464 end;
2466 if PointsArray = nil then
2467 Exit;
2469 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2470 Result := True;
2471 end;
2473 function g_Map_GetPointCount(PointType: Byte): Word;
2474 var
2475 a: Integer;
2476 begin
2477 Result := 0;
2479 if RespawnPoints = nil then
2480 Exit;
2482 for a := 0 to High(RespawnPoints) do
2483 if RespawnPoints[a].PointType = PointType then
2484 Result := Result + 1;
2485 end;
2487 function g_Map_HaveFlagPoints(): Boolean;
2488 begin
2489 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2490 end;
2492 procedure g_Map_ResetFlag(Flag: Byte);
2493 begin
2494 with gFlags[Flag] do
2495 begin
2496 Obj.X := -1000;
2497 Obj.Y := -1000;
2498 Obj.Vel.X := 0;
2499 Obj.Vel.Y := 0;
2500 Direction := D_LEFT;
2501 State := FLAG_STATE_NONE;
2502 if FlagPoints[Flag] <> nil then
2503 begin
2504 Obj.X := FlagPoints[Flag]^.X;
2505 Obj.Y := FlagPoints[Flag]^.Y;
2506 Direction := FlagPoints[Flag]^.Direction;
2507 State := FLAG_STATE_NORMAL;
2508 end;
2509 Count := -1;
2510 end;
2511 end;
2513 procedure g_Map_DrawFlags();
2514 var
2515 i, dx: Integer;
2516 Mirror: TMirrorType;
2517 begin
2518 if gGameSettings.GameMode <> GM_CTF then
2519 Exit;
2521 for i := FLAG_RED to FLAG_BLUE do
2522 with gFlags[i] do
2523 if State <> FLAG_STATE_CAPTURED then
2524 begin
2525 if State = FLAG_STATE_NONE then
2526 continue;
2528 if Direction = D_LEFT then
2529 begin
2530 Mirror := M_HORIZONTAL;
2531 dx := -1;
2532 end
2533 else
2534 begin
2535 Mirror := M_NONE;
2536 dx := 1;
2537 end;
2539 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2541 if g_debug_Frames then
2542 begin
2543 e_DrawQuad(Obj.X+Obj.Rect.X,
2544 Obj.Y+Obj.Rect.Y,
2545 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2546 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2547 0, 255, 0);
2548 end;
2549 end;
2550 end;
2552 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2553 var
2554 dw: DWORD;
2555 b: Byte;
2556 str: String;
2557 boo: Boolean;
2559 procedure SavePanelArray(var panels: TPanelArray);
2560 var
2561 PAMem: TBinMemoryWriter;
2562 i: Integer;
2563 begin
2564 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2565 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2567 i := 0;
2568 while i < Length(panels) do
2569 begin
2570 if panels[i].SaveIt then
2571 begin
2572 // ID ïàíåëè:
2573 PAMem.WriteInt(i);
2574 // Ñîõðàíÿåì ïàíåëü:
2575 panels[i].SaveState(PAMem);
2576 end;
2577 Inc(i);
2578 end;
2580 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2581 PAMem.SaveToMemory(Mem);
2582 PAMem.Free();
2583 end;
2585 procedure SaveFlag(flag: PFlag);
2586 begin
2587 // Ñèãíàòóðà ôëàãà:
2588 dw := FLAG_SIGNATURE; // 'FLAG'
2589 Mem.WriteDWORD(dw);
2590 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2591 Mem.WriteByte(flag^.RespawnType);
2592 // Ñîñòîÿíèå ôëàãà:
2593 Mem.WriteByte(flag^.State);
2594 // Íàïðàâëåíèå ôëàãà:
2595 if flag^.Direction = D_LEFT then
2596 b := 1
2597 else // D_RIGHT
2598 b := 2;
2599 Mem.WriteByte(b);
2600 // Îáúåêò ôëàãà:
2601 Obj_SaveState(@flag^.Obj, Mem);
2602 end;
2604 begin
2605 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2607 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2608 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2609 SavePanelArray(gWalls);
2610 // Ñîõðàíÿåì ïàíåëè ôîíà:
2611 SavePanelArray(gRenderBackgrounds);
2612 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2613 SavePanelArray(gRenderForegrounds);
2614 // Ñîõðàíÿåì ïàíåëè âîäû:
2615 SavePanelArray(gWater);
2616 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2617 SavePanelArray(gAcid1);
2618 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2619 SavePanelArray(gAcid2);
2620 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2621 SavePanelArray(gSteps);
2622 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2623 SavePanelArray(gLifts);
2624 ///// /////
2626 ///// Ñîõðàíÿåì ìóçûêó: /////
2627 // Ñèãíàòóðà ìóçûêè:
2628 dw := MUSIC_SIGNATURE; // 'MUSI'
2629 Mem.WriteDWORD(dw);
2630 // Íàçâàíèå ìóçûêè:
2631 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2632 if gMusic.NoMusic then
2633 str := ''
2634 else
2635 str := gMusic.Name;
2636 Mem.WriteString(str, 64);
2637 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2638 dw := gMusic.GetPosition();
2639 Mem.WriteDWORD(dw);
2640 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2641 boo := gMusic.SpecPause;
2642 Mem.WriteBoolean(boo);
2643 ///// /////
2645 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2646 Mem.WriteInt(gTotalMonsters);
2647 ///// /////
2649 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2650 if gGameSettings.GameMode = GM_CTF then
2651 begin
2652 // Ôëàã Êðàñíîé êîìàíäû:
2653 SaveFlag(@gFlags[FLAG_RED]);
2654 // Ôëàã Ñèíåé êîìàíäû:
2655 SaveFlag(@gFlags[FLAG_BLUE]);
2656 end;
2657 ///// /////
2659 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2660 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2661 begin
2662 // Î÷êè Êðàñíîé êîìàíäû:
2663 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2664 // Î÷êè Ñèíåé êîìàíäû:
2665 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2666 end;
2667 ///// /////
2668 end;
2670 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2671 var
2672 dw: DWORD;
2673 b: Byte;
2674 str: String;
2675 boo: Boolean;
2677 procedure LoadPanelArray(var panels: TPanelArray);
2678 var
2679 PAMem: TBinMemoryReader;
2680 i, id: Integer;
2681 begin
2682 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2683 PAMem := TBinMemoryReader.Create();
2684 PAMem.LoadFromMemory(Mem);
2686 for i := 0 to Length(panels)-1 do
2687 if panels[i].SaveIt then
2688 begin
2689 // ID ïàíåëè:
2690 PAMem.ReadInt(id);
2691 if id <> i then
2692 begin
2693 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2694 end;
2695 // Çàãðóæàåì ïàíåëü:
2696 panels[i].LoadState(PAMem);
2697 end;
2699 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2700 PAMem.Free();
2701 end;
2703 procedure LoadFlag(flag: PFlag);
2704 begin
2705 // Ñèãíàòóðà ôëàãà:
2706 Mem.ReadDWORD(dw);
2707 if dw <> FLAG_SIGNATURE then // 'FLAG'
2708 begin
2709 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2710 end;
2711 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2712 Mem.ReadByte(flag^.RespawnType);
2713 // Ñîñòîÿíèå ôëàãà:
2714 Mem.ReadByte(flag^.State);
2715 // Íàïðàâëåíèå ôëàãà:
2716 Mem.ReadByte(b);
2717 if b = 1 then
2718 flag^.Direction := D_LEFT
2719 else // b = 2
2720 flag^.Direction := D_RIGHT;
2721 // Îáúåêò ôëàãà:
2722 Obj_LoadState(@flag^.Obj, Mem);
2723 end;
2725 begin
2726 if Mem = nil then
2727 Exit;
2729 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2730 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2731 LoadPanelArray(gWalls);
2732 // Çàãðóæàåì ïàíåëè ôîíà:
2733 LoadPanelArray(gRenderBackgrounds);
2734 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2735 LoadPanelArray(gRenderForegrounds);
2736 // Çàãðóæàåì ïàíåëè âîäû:
2737 LoadPanelArray(gWater);
2738 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2739 LoadPanelArray(gAcid1);
2740 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2741 LoadPanelArray(gAcid2);
2742 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2743 LoadPanelArray(gSteps);
2744 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2745 LoadPanelArray(gLifts);
2746 ///// /////
2748 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2749 g_GFX_Init();
2750 mapCreateGrid();
2752 ///// Çàãðóæàåì ìóçûêó: /////
2753 // Ñèãíàòóðà ìóçûêè:
2754 Mem.ReadDWORD(dw);
2755 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2756 begin
2757 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2758 end;
2759 // Íàçâàíèå ìóçûêè:
2760 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2761 Mem.ReadString(str);
2762 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2763 Mem.ReadDWORD(dw);
2764 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2765 Mem.ReadBoolean(boo);
2766 // Çàïóñêàåì ýòó ìóçûêó:
2767 gMusic.SetByName(str);
2768 gMusic.SpecPause := boo;
2769 gMusic.Play();
2770 gMusic.Pause(True);
2771 gMusic.SetPosition(dw);
2772 ///// /////
2774 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2775 Mem.ReadInt(gTotalMonsters);
2776 ///// /////
2778 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2779 if gGameSettings.GameMode = GM_CTF then
2780 begin
2781 // Ôëàã Êðàñíîé êîìàíäû:
2782 LoadFlag(@gFlags[FLAG_RED]);
2783 // Ôëàã Ñèíåé êîìàíäû:
2784 LoadFlag(@gFlags[FLAG_BLUE]);
2785 end;
2786 ///// /////
2788 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2789 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2790 begin
2791 // Î÷êè Êðàñíîé êîìàíäû:
2792 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2793 // Î÷êè Ñèíåé êîìàíäû:
2794 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2795 end;
2796 ///// /////
2797 end;
2799 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2800 var
2801 Arr: TPanelArray;
2802 begin
2803 Result := nil;
2804 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2805 Arr := PanelByID[PanelID].PWhere^;
2806 PanelArrayID := PanelByID[PanelID].PArrID;
2807 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2808 end;
2811 // trace liquid, stepping by `dx` and `dy`
2812 // return last seen liquid coords, and `false` if we're started outside of the liquid
2813 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2814 const
2815 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2816 begin
2817 topx := x;
2818 topy := y;
2819 // started outside of the liquid?
2820 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2821 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2822 result := true;
2823 while true do
2824 begin
2825 Inc(x, dx);
2826 Inc(y, dy);
2827 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2828 topx := x;
2829 topy := y;
2830 end;
2831 end;
2834 end.