DEADSOFTWARE

particle code uglyfying; more gitiks in holmes
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 e_graphics, g_basic, 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 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;
108 // trace liquid, stepping by `dx` and `dy`
109 // return last seen liquid coords, and `false` if we're started outside of the liquid
110 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
113 procedure g_Map_ProfilersBegin ();
114 procedure g_Map_ProfilersEnd ();
117 const
118 RESPAWNPOINT_PLAYER1 = 1;
119 RESPAWNPOINT_PLAYER2 = 2;
120 RESPAWNPOINT_DM = 3;
121 RESPAWNPOINT_RED = 4;
122 RESPAWNPOINT_BLUE = 5;
124 FLAG_NONE = 0;
125 FLAG_RED = 1;
126 FLAG_BLUE = 2;
127 FLAG_DOM = 3;
129 FLAG_STATE_NONE = 0;
130 FLAG_STATE_NORMAL = 1;
131 FLAG_STATE_DROPPED = 2;
132 FLAG_STATE_CAPTURED = 3;
133 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
134 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
136 FLAG_TIME = 720; // 20 seconds
138 SKY_STRETCH: Single = 1.5;
140 const
141 GridTagInvalid = 0;
143 (* draw order:
144 PANEL_BACK
145 PANEL_STEP
146 PANEL_WALL
147 PANEL_CLOSEDOOR
148 PANEL_ACID1
149 PANEL_ACID2
150 PANEL_WATER
151 PANEL_FORE
152 *)
153 // sorted by draw priority
154 GridTagBack = 1 shl 0;
155 GridTagStep = 1 shl 1;
156 GridTagWall = 1 shl 2;
157 GridTagDoor = 1 shl 3;
158 GridTagAcid1 = 1 shl 4;
159 GridTagAcid2 = 1 shl 5;
160 GridTagWater = 1 shl 6;
161 GridTagFore = 1 shl 7;
162 // the following are invisible
163 GridTagLift = 1 shl 8;
164 GridTagBlockMon = 1 shl 9;
166 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
169 var
170 gWalls: TPanelArray;
171 gRenderBackgrounds: TPanelArray;
172 gRenderForegrounds: TPanelArray;
173 gWater, gAcid1, gAcid2: TPanelArray;
174 gSteps: TPanelArray;
175 gLifts: TPanelArray;
176 gBlockMon: TPanelArray;
177 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
178 //gDOMFlags: array of TFlag;
179 gMapInfo: TMapInfo;
180 gBackSize: TPoint;
181 gDoorMap: array of array of DWORD;
182 gLiftMap: array of array of DWORD;
183 gWADHash: TMD5Digest;
184 BackID: DWORD = DWORD(-1);
185 gExternalResources: TStringList;
187 gdbg_map_use_accel_render: Boolean = true;
188 gdbg_map_use_accel_coldet: Boolean = true;
189 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
190 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
193 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
196 type
197 TPanelGrid = specialize TBodyGridBase<TPanel>;
199 var
200 mapGrid: TPanelGrid = nil;
203 implementation
205 uses
206 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
207 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
208 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
209 Math, g_monsters, g_saveload, g_language, g_netmsg,
210 utils, sfs,
211 ImagingTypes, Imaging, ImagingUtility,
212 ImagingGif, ImagingNetworkGraphics;
214 const
215 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
216 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
217 FLAG_SIGNATURE = $47414C46; // 'FLAG'
220 function panelTypeToTag (panelType: Word): Integer;
221 begin
222 case panelType of
223 PANEL_WALL: result := GridTagWall; // gWalls
224 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
225 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
226 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
227 PANEL_WATER: result := GridTagWater; // gWater
228 PANEL_ACID1: result := GridTagAcid1; // gAcid1
229 PANEL_ACID2: result := GridTagAcid2; // gAcid2
230 PANEL_STEP: result := GridTagStep; // gSteps
231 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
232 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
233 else result := GridTagInvalid;
234 end;
235 end;
238 function dplLess (a, b: TObject): Boolean;
239 var
240 pa, pb: TPanel;
241 begin
242 pa := TPanel(a);
243 pb := TPanel(b);
244 if (pa.tag < pb.tag) then begin result := true; exit; end;
245 if (pa.tag > pb.tag) then begin result := false; exit; end;
246 result := (pa.arrIdx < pb.arrIdx);
247 end;
249 procedure dplClear ();
250 begin
251 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
252 end;
255 type
256 TPanelID = record
257 PWhere: ^TPanelArray;
258 PArrID: Integer;
259 end;
261 var
262 PanelById: array of TPanelID;
263 Textures: TLevelTextureArray;
264 RespawnPoints: Array of TRespawnPoint;
265 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
266 //DOMFlagPoints: Array of TFlagPoint;
269 procedure g_Map_ProfilersBegin ();
270 begin
271 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
272 profMapCollision.mainBegin(g_profile_collision);
273 // create sections
274 if g_profile_collision then
275 begin
276 profMapCollision.sectionBegin('*solids');
277 profMapCollision.sectionEnd();
278 profMapCollision.sectionBegin('liquids');
279 profMapCollision.sectionEnd();
280 end;
281 end;
283 procedure g_Map_ProfilersEnd ();
284 begin
285 if (profMapCollision <> nil) then profMapCollision.mainEnd();
286 end;
289 // wall index in `gWalls` or -1
290 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
291 var
292 ex, ey: Integer;
293 begin
294 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
295 if (result <> nil) then
296 begin
297 if (hitx <> nil) then hitx^ := ex;
298 if (hity <> nil) then hity^ := ey;
299 end
300 else
301 begin
302 if (hitx <> nil) then hitx^ := x1;
303 if (hity <> nil) then hity^ := y1;
304 end;
305 end;
307 // returns panel or nil
308 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
309 var
310 ex, ey: Integer;
311 begin
312 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
313 if (result <> nil) then
314 begin
315 if (hitx <> nil) then hitx^ := ex;
316 if (hity <> nil) then hity^ := ey;
317 end
318 else
319 begin
320 if (hitx <> nil) then hitx^ := x1;
321 if (hity <> nil) then hity^ := y1;
322 end;
323 end;
326 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
328 function checker (pan: TPanel; tag: Integer): Boolean;
329 begin
331 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
332 begin
333 result := pan.Enabled; // stop if wall is enabled
334 exit;
335 end;
338 if ((tag and GridTagLift) <> 0) then
339 begin
340 // stop if the lift of the right type
341 result :=
342 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
343 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
344 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
345 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
346 exit;
347 end;
349 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
350 end;
352 var
353 tagmask: Integer = 0;
354 begin
355 result := false;
357 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
358 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
359 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
360 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
361 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
362 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
363 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
365 if (tagmask = 0) then exit;// just in case
366 if ((tagmask and GridTagLift) <> 0) then
367 begin
368 // slow
369 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
370 end
371 else
372 begin
373 // fast
374 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
375 end;
376 end;
379 function g_Map_IsSpecialTexture(Texture: String): Boolean;
380 begin
381 Result := (Texture = TEXTURE_NAME_WATER) or
382 (Texture = TEXTURE_NAME_ACID1) or
383 (Texture = TEXTURE_NAME_ACID2);
384 end;
386 procedure CreateDoorMap();
387 var
388 PanelArray: Array of record
389 X, Y: Integer;
390 Width, Height: Word;
391 Active: Boolean;
392 PanelID: DWORD;
393 end;
394 a, b, c, m, i, len: Integer;
395 ok: Boolean;
396 begin
397 if gWalls = nil then
398 Exit;
400 i := 0;
401 len := 128;
402 SetLength(PanelArray, len);
404 for a := 0 to High(gWalls) do
405 if gWalls[a].Door then
406 begin
407 PanelArray[i].X := gWalls[a].X;
408 PanelArray[i].Y := gWalls[a].Y;
409 PanelArray[i].Width := gWalls[a].Width;
410 PanelArray[i].Height := gWalls[a].Height;
411 PanelArray[i].Active := True;
412 PanelArray[i].PanelID := a;
414 i := i + 1;
415 if i = len then
416 begin
417 len := len + 128;
418 SetLength(PanelArray, len);
419 end;
420 end;
422 // Íåò äâåðåé:
423 if i = 0 then
424 begin
425 PanelArray := nil;
426 Exit;
427 end;
429 SetLength(gDoorMap, 0);
431 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
433 for a := 0 to i-1 do
434 if PanelArray[a].Active then
435 begin
436 PanelArray[a].Active := False;
437 m := Length(gDoorMap);
438 SetLength(gDoorMap, m+1);
439 SetLength(gDoorMap[m], 1);
440 gDoorMap[m, 0] := PanelArray[a].PanelID;
441 ok := True;
443 while ok do
444 begin
445 ok := False;
447 for b := 0 to i-1 do
448 if PanelArray[b].Active then
449 for c := 0 to High(gDoorMap[m]) do
450 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
451 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
452 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
453 PanelArray[b].Width, PanelArray[b].Height,
454 gWalls[gDoorMap[m, c]].X,
455 gWalls[gDoorMap[m, c]].Y,
456 gWalls[gDoorMap[m, c]].Width,
457 gWalls[gDoorMap[m, c]].Height) then
458 begin
459 PanelArray[b].Active := False;
460 SetLength(gDoorMap[m],
461 Length(gDoorMap[m])+1);
462 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
463 ok := True;
464 Break;
465 end;
466 end;
468 g_Game_StepLoading();
469 end;
471 PanelArray := nil;
472 end;
474 procedure CreateLiftMap();
475 var
476 PanelArray: Array of record
477 X, Y: Integer;
478 Width, Height: Word;
479 Active: Boolean;
480 end;
481 a, b, c, len, i, j: Integer;
482 ok: Boolean;
483 begin
484 if gLifts = nil then
485 Exit;
487 len := Length(gLifts);
488 SetLength(PanelArray, len);
490 for a := 0 to len-1 do
491 begin
492 PanelArray[a].X := gLifts[a].X;
493 PanelArray[a].Y := gLifts[a].Y;
494 PanelArray[a].Width := gLifts[a].Width;
495 PanelArray[a].Height := gLifts[a].Height;
496 PanelArray[a].Active := True;
497 end;
499 SetLength(gLiftMap, len);
500 i := 0;
502 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
504 for a := 0 to len-1 do
505 if PanelArray[a].Active then
506 begin
507 PanelArray[a].Active := False;
508 SetLength(gLiftMap[i], 32);
509 j := 0;
510 gLiftMap[i, j] := a;
511 ok := True;
513 while ok do
514 begin
515 ok := False;
516 for b := 0 to len-1 do
517 if PanelArray[b].Active then
518 for c := 0 to j do
519 if g_CollideAround(PanelArray[b].X,
520 PanelArray[b].Y,
521 PanelArray[b].Width,
522 PanelArray[b].Height,
523 PanelArray[gLiftMap[i, c]].X,
524 PanelArray[gLiftMap[i, c]].Y,
525 PanelArray[gLiftMap[i, c]].Width,
526 PanelArray[gLiftMap[i, c]].Height) then
527 begin
528 PanelArray[b].Active := False;
529 j := j+1;
530 if j > High(gLiftMap[i]) then
531 SetLength(gLiftMap[i],
532 Length(gLiftMap[i])+32);
534 gLiftMap[i, j] := b;
535 ok := True;
537 Break;
538 end;
539 end;
541 SetLength(gLiftMap[i], j+1);
542 i := i+1;
544 g_Game_StepLoading();
545 end;
547 SetLength(gLiftMap, i);
549 PanelArray := nil;
550 end;
552 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
553 CurTex: Integer; sav: Boolean): Integer;
554 var
555 len: Integer;
556 panels: ^TPanelArray;
557 begin
558 Result := -1;
560 case PanelRec.PanelType of
561 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
562 panels := @gWalls;
563 PANEL_BACK:
564 panels := @gRenderBackgrounds;
565 PANEL_FORE:
566 panels := @gRenderForegrounds;
567 PANEL_WATER:
568 panels := @gWater;
569 PANEL_ACID1:
570 panels := @gAcid1;
571 PANEL_ACID2:
572 panels := @gAcid2;
573 PANEL_STEP:
574 panels := @gSteps;
575 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
576 panels := @gLifts;
577 PANEL_BLOCKMON:
578 panels := @gBlockMon;
579 else
580 Exit;
581 end;
583 len := Length(panels^);
584 SetLength(panels^, len + 1);
586 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
587 panels^[len].arrIdx := len;
588 panels^[len].proxyId := -1;
589 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
590 if sav then
591 panels^[len].SaveIt := True;
593 Result := len;
595 len := Length(PanelByID);
596 SetLength(PanelByID, len + 1);
597 PanelByID[len].PWhere := panels;
598 PanelByID[len].PArrID := Result;
599 end;
601 function CreateNullTexture(RecName: String): Integer;
602 begin
603 SetLength(Textures, Length(Textures)+1);
604 result := High(Textures);
606 with Textures[High(Textures)] do
607 begin
608 TextureName := RecName;
609 Width := 1;
610 Height := 1;
611 Anim := False;
612 TextureID := TEXTURE_NONE;
613 end;
614 end;
616 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
617 var
618 WAD: TWADFile;
619 TextureData: Pointer;
620 WADName, txname: String;
621 a, ResLength: Integer;
622 begin
623 Result := -1;
625 if Textures <> nil then
626 for a := 0 to High(Textures) do
627 if Textures[a].TextureName = RecName then
628 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
629 Result := a;
630 Exit;
631 end;
633 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
634 if (RecName = TEXTURE_NAME_WATER) or
635 (RecName = TEXTURE_NAME_ACID1) or
636 (RecName = TEXTURE_NAME_ACID2) then
637 begin
638 SetLength(Textures, Length(Textures)+1);
640 with Textures[High(Textures)] do
641 begin
642 TextureName := RecName;
644 if TextureName = TEXTURE_NAME_WATER then
645 TextureID := TEXTURE_SPECIAL_WATER
646 else
647 if TextureName = TEXTURE_NAME_ACID1 then
648 TextureID := TEXTURE_SPECIAL_ACID1
649 else
650 if TextureName = TEXTURE_NAME_ACID2 then
651 TextureID := TEXTURE_SPECIAL_ACID2;
653 Anim := False;
654 end;
656 result := High(Textures);
657 Exit;
658 end;
660 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
661 WADName := g_ExtractWadName(RecName);
663 WAD := TWADFile.Create();
665 if WADName <> '' then
666 WADName := GameDir+'/wads/'+WADName
667 else
668 WADName := Map;
670 WAD.ReadFile(WADName);
672 txname := RecName;
674 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
675 begin
676 FreeMem(TextureData);
677 RecName := 'COMMON\ALIEN';
678 end;
681 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
682 begin
683 SetLength(Textures, Length(Textures)+1);
684 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
685 Exit;
686 e_GetTextureSize(Textures[High(Textures)].TextureID,
687 @Textures[High(Textures)].Width,
688 @Textures[High(Textures)].Height);
689 FreeMem(TextureData);
690 Textures[High(Textures)].TextureName := {RecName}txname;
691 Textures[High(Textures)].Anim := False;
693 result := High(Textures);
694 end
695 else // Íåò òàêîãî ðåóñðñà â WAD'å
696 begin
697 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
698 if log then
699 begin
700 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
701 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
702 end;
703 end;
705 WAD.Free();
706 end;
708 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
709 var
710 WAD: TWADFile;
711 TextureWAD: PChar = nil;
712 TextData: Pointer = nil;
713 TextureData: Pointer = nil;
714 cfg: TConfig = nil;
715 WADName: String;
716 ResLength: Integer;
717 TextureResource: String;
718 _width, _height, _framecount, _speed: Integer;
719 _backanimation: Boolean;
720 //imgfmt: string;
721 ia: TDynImageDataArray = nil;
722 f, c, frdelay, frloop: Integer;
723 begin
724 result := -1;
726 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
728 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
729 WADName := g_ExtractWadName(RecName);
731 WAD := TWADFile.Create();
732 try
733 if WADName <> '' then
734 WADName := GameDir+'/wads/'+WADName
735 else
736 WADName := Map;
738 WAD.ReadFile(WADName);
740 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
741 begin
742 if log then
743 begin
744 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
745 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
746 end;
747 exit;
748 end;
750 {TEST
751 if WADName = Map then
752 begin
753 //FreeMem(TextureWAD);
754 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
755 end;
758 WAD.FreeWAD();
760 if ResLength < 6 then
761 begin
762 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
763 exit;
764 end;
766 // ýòî ïòèöà? ýòî ñàìîë¸ò?
767 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
768 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
769 begin
770 // íåò, ýòî ñóïåðìåí!
771 if not WAD.ReadMemory(TextureWAD, ResLength) then
772 begin
773 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
774 exit;
775 end;
777 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
778 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
779 begin
780 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
781 exit;
782 end;
784 cfg := TConfig.CreateMem(TextData, ResLength);
786 TextureResource := cfg.ReadStr('', 'resource', '');
787 if TextureResource = '' then
788 begin
789 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
790 exit;
791 end;
793 _width := cfg.ReadInt('', 'framewidth', 0);
794 _height := cfg.ReadInt('', 'frameheight', 0);
795 _framecount := cfg.ReadInt('', 'framecount', 0);
796 _speed := cfg.ReadInt('', 'waitcount', 0);
797 _backanimation := cfg.ReadBool('', 'backanimation', False);
799 cfg.Free();
800 cfg := nil;
802 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
803 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
804 begin
805 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
806 exit;
807 end;
809 WAD.Free();
810 WAD := nil;
812 SetLength(Textures, Length(Textures)+1);
813 with Textures[High(Textures)] do
814 begin
815 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
816 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
817 begin
818 TextureName := RecName;
819 Width := _width;
820 Height := _height;
821 Anim := True;
822 FramesCount := _framecount;
823 Speed := _speed;
824 result := High(Textures);
825 end
826 else
827 begin
828 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
829 end;
830 end;
831 end
832 else
833 begin
834 // try animated image
836 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
837 if length(imgfmt) = 0 then
838 begin
839 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
840 exit;
841 end;
843 GlobalMetadata.ClearMetaItems();
844 GlobalMetadata.ClearMetaItemsForSaving();
845 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
846 begin
847 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
848 exit;
849 end;
850 if length(ia) = 0 then
851 begin
852 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
853 exit;
854 end;
856 WAD.Free();
857 WAD := nil;
859 _width := ia[0].width;
860 _height := ia[0].height;
861 _framecount := length(ia);
862 _speed := 1;
863 _backanimation := false;
864 frdelay := -1;
865 frloop := -666;
866 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
867 begin
868 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
869 try
870 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
871 frdelay := f;
872 if f < 0 then f := 0;
873 // rounding ;-)
874 c := f mod 28;
875 if c < 13 then c := 0 else c := 1;
876 f := (f div 28)+c;
877 if f < 1 then f := 1 else if f > 255 then f := 255;
878 _speed := f;
879 except
880 end;
881 end;
882 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
883 begin
884 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
885 try
886 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
887 frloop := f;
888 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
889 except
890 end;
891 end;
892 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
893 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
894 f := ord(_backanimation);
895 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);
897 SetLength(Textures, Length(Textures)+1);
898 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
899 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
900 begin
901 Textures[High(Textures)].TextureName := RecName;
902 Textures[High(Textures)].Width := _width;
903 Textures[High(Textures)].Height := _height;
904 Textures[High(Textures)].Anim := True;
905 Textures[High(Textures)].FramesCount := length(ia);
906 Textures[High(Textures)].Speed := _speed;
907 result := High(Textures);
908 //writeln(' CREATED!');
909 end
910 else
911 begin
912 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
913 end;
914 end;
915 finally
916 for f := 0 to High(ia) do FreeImage(ia[f]);
917 WAD.Free();
918 cfg.Free();
919 if TextureWAD <> nil then FreeMem(TextureWAD);
920 if TextData <> nil then FreeMem(TextData);
921 if TextureData <> nil then FreeMem(TextureData);
922 end;
923 end;
925 procedure CreateItem(Item: TItemRec_1);
926 begin
927 if g_Game_IsClient then Exit;
929 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
930 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
931 Exit;
933 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
934 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
935 end;
937 procedure CreateArea(Area: TAreaRec_1);
938 var
939 a: Integer;
940 id: DWORD = 0;
941 begin
942 case Area.AreaType of
943 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
944 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
945 begin
946 SetLength(RespawnPoints, Length(RespawnPoints)+1);
947 with RespawnPoints[High(RespawnPoints)] do
948 begin
949 X := Area.X;
950 Y := Area.Y;
951 Direction := TDirection(Area.Direction);
953 case Area.AreaType of
954 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
955 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
956 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
957 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
958 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
959 end;
960 end;
961 end;
963 AREA_REDFLAG, AREA_BLUEFLAG:
964 begin
965 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
967 if FlagPoints[a] <> nil then Exit;
969 New(FlagPoints[a]);
971 with FlagPoints[a]^ do
972 begin
973 X := Area.X-FLAGRECT.X;
974 Y := Area.Y-FLAGRECT.Y;
975 Direction := TDirection(Area.Direction);
976 end;
978 with gFlags[a] do
979 begin
980 case a of
981 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
982 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
983 end;
985 Animation := TAnimation.Create(id, True, 8);
986 Obj.Rect := FLAGRECT;
988 g_Map_ResetFlag(a);
989 end;
990 end;
992 AREA_DOMFLAG:
993 begin
994 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
995 with DOMFlagPoints[High(DOMFlagPoints)] do
996 begin
997 X := Area.X;
998 Y := Area.Y;
999 Direction := TDirection(Area.Direction);
1000 end;
1002 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1003 end;
1004 end;
1005 end;
1007 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
1008 var
1009 _trigger: TTrigger;
1010 begin
1011 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1013 with _trigger do
1014 begin
1015 X := Trigger.X;
1016 Y := Trigger.Y;
1017 Width := Trigger.Width;
1018 Height := Trigger.Height;
1019 Enabled := ByteBool(Trigger.Enabled);
1020 TexturePanel := Trigger.TexturePanel;
1021 TexturePanelType := fTexturePanel1Type;
1022 ShotPanelType := fTexturePanel2Type;
1023 TriggerType := Trigger.TriggerType;
1024 ActivateType := Trigger.ActivateType;
1025 Keys := Trigger.Keys;
1026 Data.Default := Trigger.DATA;
1027 end;
1029 g_Triggers_Create(_trigger);
1030 end;
1032 procedure CreateMonster(monster: TMonsterRec_1);
1033 var
1034 a: Integer;
1035 mon: TMonster;
1036 begin
1037 if g_Game_IsClient then Exit;
1039 if (gGameSettings.GameType = GT_SINGLE)
1040 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1041 begin
1042 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1044 if gTriggers <> nil then
1045 begin
1046 for a := 0 to High(gTriggers) do
1047 begin
1048 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1049 begin
1050 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1051 end;
1052 end;
1053 end;
1055 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1056 end;
1057 end;
1059 procedure g_Map_ReAdd_DieTriggers();
1061 function monsDieTrig (mon: TMonster): Boolean;
1062 var
1063 a: Integer;
1064 begin
1065 result := false; // don't stop
1066 mon.ClearTriggers();
1067 for a := 0 to High(gTriggers) do
1068 begin
1069 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1070 begin
1071 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1072 end;
1073 end;
1074 end;
1076 begin
1077 if g_Game_IsClient then Exit;
1079 g_Mons_ForEach(monsDieTrig);
1080 end;
1082 function extractWadName(resourceName: string): string;
1083 var
1084 posN: Integer;
1085 begin
1086 posN := Pos(':', resourceName);
1087 if posN > 0 then
1088 Result:= Copy(resourceName, 0, posN-1)
1089 else
1090 Result := '';
1091 end;
1093 procedure addResToExternalResList(res: string);
1094 begin
1095 res := extractWadName(res);
1096 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1097 gExternalResources.Add(res);
1098 end;
1100 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1101 var
1102 textures: TTexturesRec1Array;
1103 mapHeader: TMapHeaderRec_1;
1104 i: integer;
1105 resFile: String = '';
1106 begin
1107 if gExternalResources = nil then
1108 gExternalResources := TStringList.Create;
1110 gExternalResources.Clear;
1111 textures := mapReader.GetTextures();
1112 for i := 0 to High(textures) do
1113 begin
1114 addResToExternalResList(resFile);
1115 end;
1117 textures := nil;
1119 mapHeader := mapReader.GetMapHeader;
1121 addResToExternalResList(mapHeader.MusicName);
1122 addResToExternalResList(mapHeader.SkyName);
1123 end;
1126 procedure mapCreateGrid ();
1127 var
1128 mapX0: Integer = $3fffffff;
1129 mapY0: Integer = $3fffffff;
1130 mapX1: Integer = -$3fffffff;
1131 mapY1: Integer = -$3fffffff;
1133 procedure calcBoundingBox (constref panels: TPanelArray);
1134 var
1135 idx: Integer;
1136 pan: TPanel;
1137 begin
1138 for idx := 0 to High(panels) do
1139 begin
1140 pan := panels[idx];
1141 if not pan.visvalid then continue;
1142 if (pan.Width < 1) or (pan.Height < 1) then continue;
1143 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1144 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1145 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1146 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1147 end;
1148 end;
1150 procedure addPanelsToGrid (constref panels: TPanelArray);
1151 var
1152 idx: Integer;
1153 pan: TPanel;
1154 newtag: Integer;
1155 begin
1156 //tag := panelTypeToTag(tag);
1157 for idx := 0 to High(panels) do
1158 begin
1159 pan := panels[idx];
1160 if not pan.visvalid then continue;
1161 if (pan.proxyId <> -1) then
1162 begin
1163 {$IF DEFINED(D2F_DEBUG)}
1164 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);
1165 {$ENDIF}
1166 continue;
1167 end;
1168 case pan.PanelType of
1169 PANEL_WALL: newtag := GridTagWall;
1170 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1171 PANEL_BACK: newtag := GridTagBack;
1172 PANEL_FORE: newtag := GridTagFore;
1173 PANEL_WATER: newtag := GridTagWater;
1174 PANEL_ACID1: newtag := GridTagAcid1;
1175 PANEL_ACID2: newtag := GridTagAcid2;
1176 PANEL_STEP: newtag := GridTagStep;
1177 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1178 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1179 else continue; // oops
1180 end;
1181 pan.tag := newtag;
1183 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1184 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1185 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1186 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1188 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1189 begin
1190 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1191 end;
1193 {$ENDIF}
1194 end;
1195 end;
1197 begin
1198 mapGrid.Free();
1199 mapGrid := nil;
1201 calcBoundingBox(gWalls);
1202 calcBoundingBox(gRenderBackgrounds);
1203 calcBoundingBox(gRenderForegrounds);
1204 calcBoundingBox(gWater);
1205 calcBoundingBox(gAcid1);
1206 calcBoundingBox(gAcid2);
1207 calcBoundingBox(gSteps);
1208 calcBoundingBox(gLifts);
1209 calcBoundingBox(gBlockMon);
1211 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]), MSG_WARNING);
1213 if (mapX0 > 0) then mapX0 := 0;
1214 if (mapY0 > 0) then mapY0 := 0;
1216 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1217 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1219 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1221 addPanelsToGrid(gWalls);
1222 addPanelsToGrid(gRenderBackgrounds);
1223 addPanelsToGrid(gRenderForegrounds);
1224 addPanelsToGrid(gWater);
1225 addPanelsToGrid(gAcid1);
1226 addPanelsToGrid(gAcid2);
1227 addPanelsToGrid(gSteps);
1228 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1229 addPanelsToGrid(gBlockMon);
1231 mapGrid.dumpStats();
1233 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1234 end;
1237 function g_Map_Load(Res: String): Boolean;
1238 const
1239 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1240 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1241 var
1242 WAD: TWADFile;
1243 MapReader: TMapReader_1;
1244 Header: TMapHeaderRec_1;
1245 _textures: TTexturesRec1Array;
1246 _texnummap: array of Integer; // `_textures` -> `Textures`
1247 panels: TPanelsRec1Array;
1248 items: TItemsRec1Array;
1249 monsters: TMonsterRec1Array;
1250 areas: TAreasRec1Array;
1251 triggers: TTriggersRec1Array;
1252 a, b, c, k: Integer;
1253 PanelID: DWORD;
1254 AddTextures: TAddTextureArray;
1255 texture: TTextureRec_1;
1256 TriggersTable: Array of record
1257 TexturePanel: Integer;
1258 LiftPanel: Integer;
1259 DoorPanel: Integer;
1260 ShotPanel: Integer;
1261 end;
1262 FileName, mapResName, s, TexName: String;
1263 Data: Pointer;
1264 Len: Integer;
1265 ok, isAnim, trigRef: Boolean;
1266 CurTex, ntn: Integer;
1268 begin
1269 mapGrid.Free();
1270 mapGrid := nil;
1271 //mapTree.Free();
1272 //mapTree := nil;
1274 Result := False;
1275 gMapInfo.Map := Res;
1276 TriggersTable := nil;
1277 FillChar(texture, SizeOf(texture), 0);
1279 sfsGCDisable(); // temporary disable removing of temporary volumes
1280 try
1281 // Çàãðóçêà WAD:
1282 FileName := g_ExtractWadName(Res);
1283 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1284 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1286 WAD := TWADFile.Create();
1287 if not WAD.ReadFile(FileName) then
1288 begin
1289 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1290 WAD.Free();
1291 Exit;
1292 end;
1293 //k8: why loader ignores path here?
1294 mapResName := g_ExtractFileName(Res);
1295 if not WAD.GetMapResource(mapResName, Data, Len) then
1296 begin
1297 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1298 WAD.Free();
1299 Exit;
1300 end;
1302 WAD.Free();
1304 // Çàãðóçêà êàðòû:
1305 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1306 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1307 MapReader := TMapReader_1.Create();
1309 if not MapReader.LoadMap(Data) then
1310 begin
1311 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1312 FreeMem(Data);
1313 MapReader.Free();
1314 Exit;
1315 end;
1317 FreeMem(Data);
1318 generateExternalResourcesList(MapReader);
1319 // Çàãðóçêà òåêñòóð:
1320 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1321 _textures := MapReader.GetTextures();
1322 _texnummap := nil;
1324 // Çàãðóçêà îïèñàíèÿ êàðòû:
1325 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1326 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1327 Header := MapReader.GetMapHeader();
1329 with gMapInfo do
1330 begin
1331 Name := Header.MapName;
1332 Description := Header.MapDescription;
1333 Author := Header.MapAuthor;
1334 MusicName := Header.MusicName;
1335 SkyName := Header.SkyName;
1336 Height := Header.Height;
1337 Width := Header.Width;
1338 end;
1340 // Äîáàâëåíèå òåêñòóð â Textures[]:
1341 if _textures <> nil then
1342 begin
1343 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1344 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1345 SetLength(_texnummap, length(_textures));
1347 for a := 0 to High(_textures) do
1348 begin
1349 SetLength(s, 64);
1350 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1351 for b := 1 to Length(s) do
1352 if s[b] = #0 then
1353 begin
1354 SetLength(s, b-1);
1355 Break;
1356 end;
1357 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1358 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1359 // Àíèìèðîâàííàÿ òåêñòóðà:
1360 if ByteBool(_textures[a].Anim) then
1361 begin
1362 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1363 if ntn < 0 then
1364 begin
1365 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1366 ntn := CreateNullTexture(_textures[a].Resource);
1367 end;
1368 end
1369 else // Îáû÷íàÿ òåêñòóðà:
1370 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1371 if ntn < 0 then
1372 begin
1373 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1374 ntn := CreateNullTexture(_textures[a].Resource);
1375 end;
1377 _texnummap[a] := ntn; // fix texture number
1378 g_Game_StepLoading();
1379 end;
1380 end;
1382 // Çàãðóçêà òðèããåðîâ:
1383 gTriggerClientID := 0;
1384 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1385 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1386 triggers := MapReader.GetTriggers();
1388 // Çàãðóçêà ïàíåëåé:
1389 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1390 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1391 panels := MapReader.GetPanels();
1393 // check texture numbers for panels
1394 for a := 0 to High(panels) do
1395 begin
1396 if panels[a].TextureNum > High(_textures) then
1397 begin
1398 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1399 result := false;
1400 exit;
1401 end;
1402 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1403 end;
1405 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1406 if triggers <> nil then
1407 begin
1408 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1409 SetLength(TriggersTable, Length(triggers));
1410 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1412 for a := 0 to High(TriggersTable) do
1413 begin
1414 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1415 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1416 // Ëèôòû:
1417 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1418 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1419 else
1420 TriggersTable[a].LiftPanel := -1;
1421 // Äâåðè:
1422 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1423 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1424 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1425 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1426 else
1427 TriggersTable[a].DoorPanel := -1;
1428 // Òóðåëü:
1429 if triggers[a].TriggerType = TRIGGER_SHOT then
1430 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1431 else
1432 TriggersTable[a].ShotPanel := -1;
1434 g_Game_StepLoading();
1435 end;
1436 end;
1438 // Ñîçäàåì ïàíåëè:
1439 if panels <> nil then
1440 begin
1441 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1442 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1444 for a := 0 to High(panels) do
1445 begin
1446 SetLength(AddTextures, 0);
1447 trigRef := False;
1448 CurTex := -1;
1449 if _textures <> nil then
1450 begin
1451 texture := _textures[panels[a].TextureNum];
1452 ok := True;
1453 end
1454 else
1455 ok := False;
1457 if ok then
1458 begin
1459 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1460 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1461 ok := False;
1462 if (TriggersTable <> nil) and (_textures <> nil) then
1463 for b := 0 to High(TriggersTable) do
1464 if (TriggersTable[b].TexturePanel = a)
1465 or (TriggersTable[b].ShotPanel = a) then
1466 begin
1467 trigRef := True;
1468 ok := True;
1469 Break;
1470 end;
1471 end;
1473 if ok then
1474 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1475 SetLength(s, 64);
1476 CopyMemory(@s[1], @texture.Resource[0], 64);
1477 // Èçìåðÿåì äëèíó:
1478 Len := Length(s);
1479 for c := Len downto 1 do
1480 if s[c] <> #0 then
1481 begin
1482 Len := c;
1483 Break;
1484 end;
1485 SetLength(s, Len);
1487 // Ñïåö-òåêñòóðû çàïðåùåíû:
1488 if g_Map_IsSpecialTexture(s) then
1489 ok := False
1490 else
1491 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1492 ok := g_Texture_NumNameFindStart(s);
1494 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1495 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1496 if ok then
1497 begin
1498 k := NNF_NAME_BEFORE;
1499 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1500 while ok or (k = NNF_NAME_BEFORE) or
1501 (k = NNF_NAME_EQUALS) do
1502 begin
1503 k := g_Texture_NumNameFindNext(TexName);
1505 if (k = NNF_NAME_BEFORE) or
1506 (k = NNF_NAME_AFTER) then
1507 begin
1508 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1509 if ByteBool(texture.Anim) then
1510 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1511 isAnim := True;
1512 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1513 if not ok then
1514 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1515 isAnim := False;
1516 ok := CreateTexture(TexName, FileName, False) >= 0;
1517 end;
1518 end
1519 else
1520 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1521 isAnim := False;
1522 ok := CreateTexture(TexName, FileName, False) >= 0;
1523 if not ok then
1524 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1525 isAnim := True;
1526 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1527 end;
1528 end;
1530 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1531 if ok then
1532 begin
1533 for c := 0 to High(Textures) do
1534 if Textures[c].TextureName = TexName then
1535 begin
1536 SetLength(AddTextures, Length(AddTextures)+1);
1537 AddTextures[High(AddTextures)].Texture := c;
1538 AddTextures[High(AddTextures)].Anim := isAnim;
1539 Break;
1540 end;
1541 end;
1542 end
1543 else
1544 if k = NNF_NAME_EQUALS then
1545 begin
1546 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1547 SetLength(AddTextures, Length(AddTextures)+1);
1548 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1549 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1550 CurTex := High(AddTextures);
1551 ok := True;
1552 end
1553 else // NNF_NO_NAME
1554 ok := False;
1555 end; // while ok...
1557 ok := True;
1558 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1559 end; // if ok - ññûëàþòñÿ òðèããåðû
1561 if not ok then
1562 begin
1563 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1564 SetLength(AddTextures, 1);
1565 AddTextures[0].Texture := panels[a].TextureNum;
1566 AddTextures[0].Anim := ByteBool(texture.Anim);
1567 CurTex := 0;
1568 end;
1570 //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);
1572 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1573 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1575 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1576 if TriggersTable <> nil then
1577 for b := 0 to High(TriggersTable) do
1578 begin
1579 // Òðèããåð äâåðè/ëèôòà:
1580 if (TriggersTable[b].LiftPanel = a) or
1581 (TriggersTable[b].DoorPanel = a) then
1582 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1583 // Òðèããåð ñìåíû òåêñòóðû:
1584 if TriggersTable[b].TexturePanel = a then
1585 triggers[b].TexturePanel := PanelID;
1586 // Òðèããåð "Òóðåëü":
1587 if TriggersTable[b].ShotPanel = a then
1588 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1589 end;
1591 g_Game_StepLoading();
1592 end;
1593 end;
1595 // create map grid, init other grids (for monsters, for example)
1596 e_WriteLog('Creating map grid', MSG_NOTIFY);
1597 mapCreateGrid();
1599 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1600 if (triggers <> nil) and not gLoadGameMode then
1601 begin
1602 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1603 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1604 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1605 for a := 0 to High(triggers) do
1606 begin
1607 if triggers[a].TexturePanel <> -1 then
1608 b := panels[TriggersTable[a].TexturePanel].PanelType
1609 else
1610 b := 0;
1611 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1612 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1613 c := panels[TriggersTable[a].ShotPanel].PanelType
1614 else
1615 c := 0;
1616 CreateTrigger(triggers[a], b, c);
1617 end;
1618 end;
1620 // Çàãðóçêà ïðåäìåòîâ:
1621 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1622 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1623 items := MapReader.GetItems();
1625 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1626 if (items <> nil) and not gLoadGameMode then
1627 begin
1628 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1629 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1630 for a := 0 to High(items) do
1631 CreateItem(Items[a]);
1632 end;
1634 // Çàãðóçêà îáëàñòåé:
1635 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1636 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1637 areas := MapReader.GetAreas();
1639 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1640 if areas <> nil then
1641 begin
1642 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1643 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1644 for a := 0 to High(areas) do
1645 CreateArea(areas[a]);
1646 end;
1648 // Çàãðóçêà ìîíñòðîâ:
1649 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1650 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1651 monsters := MapReader.GetMonsters();
1653 gTotalMonsters := 0;
1655 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1656 if (monsters <> nil) and not gLoadGameMode then
1657 begin
1658 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1659 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1660 for a := 0 to High(monsters) do
1661 CreateMonster(monsters[a]);
1662 end;
1664 MapReader.Free();
1666 // Çàãðóçêà íåáà:
1667 if gMapInfo.SkyName <> '' then
1668 begin
1669 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1670 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1671 FileName := g_ExtractWadName(gMapInfo.SkyName);
1673 if FileName <> '' then
1674 FileName := GameDir+'/wads/'+FileName
1675 else
1676 begin
1677 FileName := g_ExtractWadName(Res);
1678 end;
1680 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1681 if g_Texture_CreateWAD(BackID, s) then
1682 begin
1683 g_Game_SetupScreenSize();
1684 end
1685 else
1686 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1687 end;
1689 // Çàãðóçêà ìóçûêè:
1690 ok := False;
1691 if gMapInfo.MusicName <> '' then
1692 begin
1693 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1694 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1695 FileName := g_ExtractWadName(gMapInfo.MusicName);
1697 if FileName <> '' then
1698 FileName := GameDir+'/wads/'+FileName
1699 else
1700 begin
1701 FileName := g_ExtractWadName(Res);
1702 end;
1704 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1705 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1706 ok := True
1707 else
1708 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1709 end;
1711 // Îñòàëüíûå óñòàíâêè:
1712 CreateDoorMap();
1713 CreateLiftMap();
1715 g_Items_Init();
1716 g_Weapon_Init();
1717 g_Monsters_Init();
1719 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1720 if not gLoadGameMode then
1721 g_GFX_Init();
1723 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1724 _textures := nil;
1725 panels := nil;
1726 items := nil;
1727 areas := nil;
1728 triggers := nil;
1729 TriggersTable := nil;
1730 AddTextures := nil;
1732 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1733 if ok and (not gLoadGameMode) then
1734 begin
1735 gMusic.SetByName(gMapInfo.MusicName);
1736 gMusic.Play();
1737 end
1738 else
1739 gMusic.SetByName('');
1740 finally
1741 sfsGCEnable(); // enable releasing unused volumes
1742 end;
1744 e_WriteLog('Done loading map.', MSG_NOTIFY);
1745 Result := True;
1746 end;
1748 function g_Map_GetMapInfo(Res: String): TMapInfo;
1749 var
1750 WAD: TWADFile;
1751 MapReader: TMapReader_1;
1752 Header: TMapHeaderRec_1;
1753 FileName: String;
1754 Data: Pointer;
1755 Len: Integer;
1756 begin
1757 FillChar(Result, SizeOf(Result), 0);
1758 FileName := g_ExtractWadName(Res);
1760 WAD := TWADFile.Create();
1761 if not WAD.ReadFile(FileName) then
1762 begin
1763 WAD.Free();
1764 Exit;
1765 end;
1767 //k8: it ignores path again
1768 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1769 begin
1770 WAD.Free();
1771 Exit;
1772 end;
1774 WAD.Free();
1776 MapReader := TMapReader_1.Create();
1778 if not MapReader.LoadMap(Data) then
1779 begin
1780 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1781 ZeroMemory(@Header, SizeOf(Header));
1782 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1783 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1784 end
1785 else
1786 begin
1787 Header := MapReader.GetMapHeader();
1788 Result.Name := Header.MapName;
1789 Result.Description := Header.MapDescription;
1790 end;
1792 FreeMem(Data);
1793 MapReader.Free();
1795 Result.Map := Res;
1796 Result.Author := Header.MapAuthor;
1797 Result.Height := Header.Height;
1798 Result.Width := Header.Width;
1799 end;
1801 function g_Map_GetMapsList(WADName: string): SArray;
1802 var
1803 WAD: TWADFile;
1804 a: Integer;
1805 ResList: SArray;
1806 begin
1807 Result := nil;
1808 WAD := TWADFile.Create();
1809 if not WAD.ReadFile(WADName) then
1810 begin
1811 WAD.Free();
1812 Exit;
1813 end;
1814 ResList := WAD.GetMapResources();
1815 if ResList <> nil then
1816 begin
1817 for a := 0 to High(ResList) do
1818 begin
1819 SetLength(Result, Length(Result)+1);
1820 Result[High(Result)] := ResList[a];
1821 end;
1822 end;
1823 WAD.Free();
1824 end;
1826 function g_Map_Exist(Res: string): Boolean;
1827 var
1828 WAD: TWADFile;
1829 FileName, mnn: string;
1830 ResList: SArray;
1831 a: Integer;
1832 begin
1833 Result := False;
1835 FileName := addWadExtension(g_ExtractWadName(Res));
1837 WAD := TWADFile.Create;
1838 if not WAD.ReadFile(FileName) then
1839 begin
1840 WAD.Free();
1841 Exit;
1842 end;
1844 ResList := WAD.GetMapResources();
1845 WAD.Free();
1847 mnn := g_ExtractFileName(Res);
1848 if ResList <> nil then
1849 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1850 begin
1851 Result := True;
1852 Exit;
1853 end;
1854 end;
1856 procedure g_Map_Free();
1857 var
1858 a: Integer;
1860 procedure FreePanelArray(var panels: TPanelArray);
1861 var
1862 i: Integer;
1864 begin
1865 if panels <> nil then
1866 begin
1867 for i := 0 to High(panels) do
1868 panels[i].Free();
1869 panels := nil;
1870 end;
1871 end;
1873 begin
1874 g_GFX_Free();
1875 g_Weapon_Free();
1876 g_Items_Free();
1877 g_Triggers_Free();
1878 g_Monsters_Free();
1880 RespawnPoints := nil;
1881 if FlagPoints[FLAG_RED] <> nil then
1882 begin
1883 Dispose(FlagPoints[FLAG_RED]);
1884 FlagPoints[FLAG_RED] := nil;
1885 end;
1886 if FlagPoints[FLAG_BLUE] <> nil then
1887 begin
1888 Dispose(FlagPoints[FLAG_BLUE]);
1889 FlagPoints[FLAG_BLUE] := nil;
1890 end;
1891 //DOMFlagPoints := nil;
1893 //gDOMFlags := nil;
1895 if Textures <> nil then
1896 begin
1897 for a := 0 to High(Textures) do
1898 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1899 if Textures[a].Anim then
1900 g_Frames_DeleteByID(Textures[a].FramesID)
1901 else
1902 if Textures[a].TextureID <> TEXTURE_NONE then
1903 e_DeleteTexture(Textures[a].TextureID);
1905 Textures := nil;
1906 end;
1908 FreePanelArray(gWalls);
1909 FreePanelArray(gRenderBackgrounds);
1910 FreePanelArray(gRenderForegrounds);
1911 FreePanelArray(gWater);
1912 FreePanelArray(gAcid1);
1913 FreePanelArray(gAcid2);
1914 FreePanelArray(gSteps);
1915 FreePanelArray(gLifts);
1916 FreePanelArray(gBlockMon);
1918 if BackID <> DWORD(-1) then
1919 begin
1920 gBackSize.X := 0;
1921 gBackSize.Y := 0;
1922 e_DeleteTexture(BackID);
1923 BackID := DWORD(-1);
1924 end;
1926 g_Game_StopAllSounds(False);
1927 gMusic.FreeSound();
1928 g_Sound_Delete(gMapInfo.MusicName);
1930 gMapInfo.Name := '';
1931 gMapInfo.Description := '';
1932 gMapInfo.MusicName := '';
1933 gMapInfo.Height := 0;
1934 gMapInfo.Width := 0;
1936 gDoorMap := nil;
1937 gLiftMap := nil;
1939 PanelByID := nil;
1940 end;
1942 procedure g_Map_Update();
1943 var
1944 a, d, j: Integer;
1945 m: Word;
1946 s: String;
1948 procedure UpdatePanelArray(var panels: TPanelArray);
1949 var
1950 i: Integer;
1952 begin
1953 if panels <> nil then
1954 for i := 0 to High(panels) do
1955 panels[i].Update();
1956 end;
1958 begin
1959 UpdatePanelArray(gWalls);
1960 UpdatePanelArray(gRenderBackgrounds);
1961 UpdatePanelArray(gRenderForegrounds);
1962 UpdatePanelArray(gWater);
1963 UpdatePanelArray(gAcid1);
1964 UpdatePanelArray(gAcid2);
1965 UpdatePanelArray(gSteps);
1967 if gGameSettings.GameMode = GM_CTF then
1968 begin
1969 for a := FLAG_RED to FLAG_BLUE do
1970 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1971 with gFlags[a] do
1972 begin
1973 if gFlags[a].Animation <> nil then
1974 gFlags[a].Animation.Update();
1976 m := g_Obj_Move(@Obj, True, True);
1978 if gTime mod (GAME_TICK*2) <> 0 then
1979 Continue;
1981 // Ñîïðîòèâëåíèå âîçäóõà:
1982 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1984 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1985 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1986 begin
1987 g_Map_ResetFlag(a);
1988 gFlags[a].CaptureTime := 0;
1989 if a = FLAG_RED then
1990 s := _lc[I_PLAYER_FLAG_RED]
1991 else
1992 s := _lc[I_PLAYER_FLAG_BLUE];
1993 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1995 if g_Game_IsNet then
1996 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1997 Continue;
1998 end;
2000 if Count > 0 then
2001 Count := Count - 1;
2003 // Èãðîê áåðåò ôëàã:
2004 if gPlayers <> nil then
2005 begin
2006 j := Random(Length(gPlayers)) - 1;
2008 for d := 0 to High(gPlayers) do
2009 begin
2010 Inc(j);
2011 if j > High(gPlayers) then
2012 j := 0;
2014 if gPlayers[j] <> nil then
2015 if gPlayers[j].Live and
2016 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2017 begin
2018 if gPlayers[j].GetFlag(a) then
2019 Break;
2020 end;
2021 end;
2022 end;
2023 end;
2024 end;
2025 end;
2028 // old algo
2029 procedure g_Map_DrawPanels (PanelType: Word);
2031 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2032 var
2033 idx: Integer;
2034 begin
2035 if (panels <> nil) then
2036 begin
2037 // alas, no visible set
2038 for idx := 0 to High(panels) do
2039 begin
2040 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2041 end;
2042 end;
2043 end;
2045 begin
2046 case PanelType of
2047 PANEL_WALL: DrawPanels(gWalls);
2048 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2049 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2050 PANEL_FORE: DrawPanels(gRenderForegrounds);
2051 PANEL_WATER: DrawPanels(gWater);
2052 PANEL_ACID1: DrawPanels(gAcid1);
2053 PANEL_ACID2: DrawPanels(gAcid2);
2054 PANEL_STEP: DrawPanels(gSteps);
2055 end;
2056 end;
2059 // new algo
2060 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2062 function checker (pan: TPanel; tag: Integer): Boolean;
2063 begin
2064 result := false; // don't stop, ever
2065 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2066 gDrawPanelList.insert(pan);
2067 end;
2069 begin
2070 dplClear();
2071 //tagmask := panelTypeToTag(PanelType);
2073 {if gdbg_map_use_tree_draw then
2074 begin
2075 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2076 end
2077 else}
2078 begin
2079 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2080 end;
2081 // list will be rendered in `g_game.DrawPlayer()`
2082 end;
2085 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2087 function checker (pan: TPanel; tag: Integer): Boolean;
2088 begin
2089 result := false; // don't stop, ever
2090 pan.DrawShadowVolume(lightX, lightY, radius);
2091 end;
2093 begin
2094 {if gdbg_map_use_tree_draw then
2095 begin
2096 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2097 end
2098 else}
2099 begin
2100 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2101 end;
2102 end;
2105 procedure g_Map_DrawBack(dx, dy: Integer);
2106 begin
2107 if gDrawBackGround and (BackID <> DWORD(-1)) then
2108 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2109 else
2110 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2111 end;
2113 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2114 PanelType: Word; b1x3: Boolean=false): Boolean;
2115 var
2116 a, h: Integer;
2117 begin
2118 Result := False;
2120 if WordBool(PanelType and PANEL_WALL) then
2121 if gWalls <> nil then
2122 begin
2123 h := High(gWalls);
2125 for a := 0 to h do
2126 if gWalls[a].Enabled and
2127 g_Collide(X, Y, Width, Height,
2128 gWalls[a].X, gWalls[a].Y,
2129 gWalls[a].Width, gWalls[a].Height) then
2130 begin
2131 Result := True;
2132 Exit;
2133 end;
2134 end;
2136 if WordBool(PanelType and PANEL_WATER) then
2137 if gWater <> nil then
2138 begin
2139 h := High(gWater);
2141 for a := 0 to h do
2142 if g_Collide(X, Y, Width, Height,
2143 gWater[a].X, gWater[a].Y,
2144 gWater[a].Width, gWater[a].Height) then
2145 begin
2146 Result := True;
2147 Exit;
2148 end;
2149 end;
2151 if WordBool(PanelType and PANEL_ACID1) then
2152 if gAcid1 <> nil then
2153 begin
2154 h := High(gAcid1);
2156 for a := 0 to h do
2157 if g_Collide(X, Y, Width, Height,
2158 gAcid1[a].X, gAcid1[a].Y,
2159 gAcid1[a].Width, gAcid1[a].Height) then
2160 begin
2161 Result := True;
2162 Exit;
2163 end;
2164 end;
2166 if WordBool(PanelType and PANEL_ACID2) then
2167 if gAcid2 <> nil then
2168 begin
2169 h := High(gAcid2);
2171 for a := 0 to h do
2172 if g_Collide(X, Y, Width, Height,
2173 gAcid2[a].X, gAcid2[a].Y,
2174 gAcid2[a].Width, gAcid2[a].Height) then
2175 begin
2176 Result := True;
2177 Exit;
2178 end;
2179 end;
2181 if WordBool(PanelType and PANEL_STEP) then
2182 if gSteps <> nil then
2183 begin
2184 h := High(gSteps);
2186 for a := 0 to h do
2187 if g_Collide(X, Y, Width, Height,
2188 gSteps[a].X, gSteps[a].Y,
2189 gSteps[a].Width, gSteps[a].Height) then
2190 begin
2191 Result := True;
2192 Exit;
2193 end;
2194 end;
2196 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2197 if gLifts <> nil then
2198 begin
2199 h := High(gLifts);
2201 for a := 0 to h do
2202 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2203 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2204 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2205 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2206 g_Collide(X, Y, Width, Height,
2207 gLifts[a].X, gLifts[a].Y,
2208 gLifts[a].Width, gLifts[a].Height) then
2209 begin
2210 Result := True;
2211 Exit;
2212 end;
2213 end;
2215 if WordBool(PanelType and PANEL_BLOCKMON) then
2216 if gBlockMon <> nil then
2217 begin
2218 h := High(gBlockMon);
2220 for a := 0 to h do
2221 if ( (not b1x3) or
2222 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2223 g_Collide(X, Y, Width, Height,
2224 gBlockMon[a].X, gBlockMon[a].Y,
2225 gBlockMon[a].Width, gBlockMon[a].Height) then
2226 begin
2227 Result := True;
2228 Exit;
2229 end;
2230 end;
2231 end;
2233 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2234 var
2235 texid: DWORD;
2237 function checkPanels (constref panels: TPanelArray): Boolean;
2238 var
2239 a: Integer;
2240 begin
2241 result := false;
2242 if panels = nil then exit;
2243 for a := 0 to High(panels) do
2244 begin
2245 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2246 begin
2247 result := true;
2248 texid := panels[a].GetTextureID();
2249 exit;
2250 end;
2251 end;
2252 end;
2254 begin
2255 texid := TEXTURE_NONE;
2256 result := texid;
2257 if not checkPanels(gWater) then
2258 if not checkPanels(gAcid1) then
2259 if not checkPanels(gAcid2) then exit;
2260 result := texid;
2261 end;
2264 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2265 const
2266 SlowMask = GridTagLift or GridTagBlockMon;
2267 function checker (pan: TPanel; tag: Integer): Boolean;
2268 begin
2270 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2271 begin
2272 result := pan.Enabled;
2273 exit;
2274 end;
2277 if ((tag and GridTagLift) <> 0) then
2278 begin
2279 result :=
2280 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2281 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2282 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2283 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2284 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2285 exit;
2286 end;
2288 if ((tag and GridTagBlockMon) <> 0) then
2289 begin
2290 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2291 exit;
2292 end;
2294 // other shit
2295 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2296 result := true; // i found her!
2297 end;
2299 var
2300 tagmask: Integer = 0;
2301 begin
2302 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2303 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2304 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2305 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2306 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2307 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2308 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2310 if (tagmask = 0) then begin result := false; exit; end; // just in case
2312 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2313 if gdbg_map_use_accel_coldet then
2314 begin
2315 {if gdbg_map_use_tree_coldet then
2316 begin
2317 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2318 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2319 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2320 begin
2321 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2322 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2323 end;
2324 end
2325 else}
2326 begin
2327 if (Width = 1) and (Height = 1) then
2328 begin
2329 if ((tagmask and SlowMask) <> 0) then
2330 begin
2331 // slow
2332 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2333 end
2334 else
2335 begin
2336 // fast
2337 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2338 end;
2339 end
2340 else
2341 begin
2342 if ((tagmask and SlowMask) <> 0) then
2343 begin
2344 // slow
2345 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2346 end
2347 else
2348 begin
2349 // fast
2350 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2351 end;
2352 end;
2353 end;
2354 end
2355 else
2356 begin
2357 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2358 end;
2359 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2360 end;
2363 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2364 var
2365 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2366 texid: DWORD;
2368 // slightly different from the old code, but meh...
2369 function checker (pan: TPanel; tag: Integer): Boolean;
2370 begin
2371 result := false; // don't stop, ever
2372 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2373 // check priorities
2374 case cctype of
2375 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2376 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2377 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2378 end;
2379 // collision?
2380 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2381 // yeah
2382 texid := pan.GetTextureID();
2383 // water? water has the highest priority, so stop right here
2384 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2385 // acid2?
2386 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2387 // acid1?
2388 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2389 end;
2391 begin
2392 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2393 if gdbg_map_use_accel_coldet then
2394 begin
2395 texid := TEXTURE_NONE;
2396 {if gdbg_map_use_tree_coldet then
2397 begin
2398 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2399 end
2400 else}
2401 begin
2402 if (Width = 1) and (Height = 1) then
2403 begin
2404 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2405 end
2406 else
2407 begin
2408 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2409 end;
2410 end;
2411 result := texid;
2412 end
2413 else
2414 begin
2415 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2416 end;
2417 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2418 end;
2421 procedure g_Map_EnableWall(ID: DWORD);
2422 var
2423 pan: TPanel;
2424 begin
2425 pan := gWalls[ID];
2426 pan.Enabled := True;
2427 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2429 mapGrid.proxyEnabled[pan.proxyId] := true;
2430 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2431 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2433 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2435 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2436 //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);
2437 {$ENDIF}
2438 end;
2440 procedure g_Map_DisableWall(ID: DWORD);
2441 var
2442 pan: TPanel;
2443 begin
2444 pan := gWalls[ID];
2445 pan.Enabled := False;
2446 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2448 mapGrid.proxyEnabled[pan.proxyId] := false;
2449 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2451 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2453 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2454 //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);
2455 {$ENDIF}
2456 end;
2458 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2459 var
2460 tp: TPanel;
2461 begin
2462 case PanelType of
2463 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2464 tp := gWalls[ID];
2465 PANEL_FORE:
2466 tp := gRenderForegrounds[ID];
2467 PANEL_BACK:
2468 tp := gRenderBackgrounds[ID];
2469 PANEL_WATER:
2470 tp := gWater[ID];
2471 PANEL_ACID1:
2472 tp := gAcid1[ID];
2473 PANEL_ACID2:
2474 tp := gAcid2[ID];
2475 PANEL_STEP:
2476 tp := gSteps[ID];
2477 else
2478 Exit;
2479 end;
2481 tp.NextTexture(AnimLoop);
2482 if g_Game_IsServer and g_Game_IsNet then
2483 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2484 end;
2486 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2487 begin
2488 if gLifts[ID].LiftType = t then
2489 Exit;
2491 with gLifts[ID] do
2492 begin
2493 LiftType := t;
2495 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2496 //TODO: make separate lift tags, and change tag here
2498 if LiftType = 0 then
2499 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2500 else if LiftType = 1 then
2501 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2502 else if LiftType = 2 then
2503 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2504 else if LiftType = 3 then
2505 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2507 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2508 end;
2509 end;
2511 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2512 var
2513 a: Integer;
2514 PointsArray: Array of TRespawnPoint;
2515 begin
2516 Result := False;
2517 SetLength(PointsArray, 0);
2519 if RespawnPoints = nil then
2520 Exit;
2522 for a := 0 to High(RespawnPoints) do
2523 if RespawnPoints[a].PointType = PointType then
2524 begin
2525 SetLength(PointsArray, Length(PointsArray)+1);
2526 PointsArray[High(PointsArray)] := RespawnPoints[a];
2527 end;
2529 if PointsArray = nil then
2530 Exit;
2532 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2533 Result := True;
2534 end;
2536 function g_Map_GetPointCount(PointType: Byte): Word;
2537 var
2538 a: Integer;
2539 begin
2540 Result := 0;
2542 if RespawnPoints = nil then
2543 Exit;
2545 for a := 0 to High(RespawnPoints) do
2546 if RespawnPoints[a].PointType = PointType then
2547 Result := Result + 1;
2548 end;
2550 function g_Map_HaveFlagPoints(): Boolean;
2551 begin
2552 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2553 end;
2555 procedure g_Map_ResetFlag(Flag: Byte);
2556 begin
2557 with gFlags[Flag] do
2558 begin
2559 Obj.X := -1000;
2560 Obj.Y := -1000;
2561 Obj.Vel.X := 0;
2562 Obj.Vel.Y := 0;
2563 Direction := D_LEFT;
2564 State := FLAG_STATE_NONE;
2565 if FlagPoints[Flag] <> nil then
2566 begin
2567 Obj.X := FlagPoints[Flag]^.X;
2568 Obj.Y := FlagPoints[Flag]^.Y;
2569 Direction := FlagPoints[Flag]^.Direction;
2570 State := FLAG_STATE_NORMAL;
2571 end;
2572 Count := -1;
2573 end;
2574 end;
2576 procedure g_Map_DrawFlags();
2577 var
2578 i, dx: Integer;
2579 Mirror: TMirrorType;
2580 begin
2581 if gGameSettings.GameMode <> GM_CTF then
2582 Exit;
2584 for i := FLAG_RED to FLAG_BLUE do
2585 with gFlags[i] do
2586 if State <> FLAG_STATE_CAPTURED then
2587 begin
2588 if State = FLAG_STATE_NONE then
2589 continue;
2591 if Direction = D_LEFT then
2592 begin
2593 Mirror := M_HORIZONTAL;
2594 dx := -1;
2595 end
2596 else
2597 begin
2598 Mirror := M_NONE;
2599 dx := 1;
2600 end;
2602 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2604 if g_debug_Frames then
2605 begin
2606 e_DrawQuad(Obj.X+Obj.Rect.X,
2607 Obj.Y+Obj.Rect.Y,
2608 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2609 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2610 0, 255, 0);
2611 end;
2612 end;
2613 end;
2615 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2616 var
2617 dw: DWORD;
2618 b: Byte;
2619 str: String;
2620 boo: Boolean;
2622 procedure SavePanelArray(var panels: TPanelArray);
2623 var
2624 PAMem: TBinMemoryWriter;
2625 i: Integer;
2626 begin
2627 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2628 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2630 i := 0;
2631 while i < Length(panels) do
2632 begin
2633 if panels[i].SaveIt then
2634 begin
2635 // ID ïàíåëè:
2636 PAMem.WriteInt(i);
2637 // Ñîõðàíÿåì ïàíåëü:
2638 panels[i].SaveState(PAMem);
2639 end;
2640 Inc(i);
2641 end;
2643 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2644 PAMem.SaveToMemory(Mem);
2645 PAMem.Free();
2646 end;
2648 procedure SaveFlag(flag: PFlag);
2649 begin
2650 // Ñèãíàòóðà ôëàãà:
2651 dw := FLAG_SIGNATURE; // 'FLAG'
2652 Mem.WriteDWORD(dw);
2653 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2654 Mem.WriteByte(flag^.RespawnType);
2655 // Ñîñòîÿíèå ôëàãà:
2656 Mem.WriteByte(flag^.State);
2657 // Íàïðàâëåíèå ôëàãà:
2658 if flag^.Direction = D_LEFT then
2659 b := 1
2660 else // D_RIGHT
2661 b := 2;
2662 Mem.WriteByte(b);
2663 // Îáúåêò ôëàãà:
2664 Obj_SaveState(@flag^.Obj, Mem);
2665 end;
2667 begin
2668 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2670 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2671 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2672 SavePanelArray(gWalls);
2673 // Ñîõðàíÿåì ïàíåëè ôîíà:
2674 SavePanelArray(gRenderBackgrounds);
2675 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2676 SavePanelArray(gRenderForegrounds);
2677 // Ñîõðàíÿåì ïàíåëè âîäû:
2678 SavePanelArray(gWater);
2679 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2680 SavePanelArray(gAcid1);
2681 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2682 SavePanelArray(gAcid2);
2683 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2684 SavePanelArray(gSteps);
2685 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2686 SavePanelArray(gLifts);
2687 ///// /////
2689 ///// Ñîõðàíÿåì ìóçûêó: /////
2690 // Ñèãíàòóðà ìóçûêè:
2691 dw := MUSIC_SIGNATURE; // 'MUSI'
2692 Mem.WriteDWORD(dw);
2693 // Íàçâàíèå ìóçûêè:
2694 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2695 if gMusic.NoMusic then
2696 str := ''
2697 else
2698 str := gMusic.Name;
2699 Mem.WriteString(str, 64);
2700 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2701 dw := gMusic.GetPosition();
2702 Mem.WriteDWORD(dw);
2703 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2704 boo := gMusic.SpecPause;
2705 Mem.WriteBoolean(boo);
2706 ///// /////
2708 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2709 Mem.WriteInt(gTotalMonsters);
2710 ///// /////
2712 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2713 if gGameSettings.GameMode = GM_CTF then
2714 begin
2715 // Ôëàã Êðàñíîé êîìàíäû:
2716 SaveFlag(@gFlags[FLAG_RED]);
2717 // Ôëàã Ñèíåé êîìàíäû:
2718 SaveFlag(@gFlags[FLAG_BLUE]);
2719 end;
2720 ///// /////
2722 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2723 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2724 begin
2725 // Î÷êè Êðàñíîé êîìàíäû:
2726 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2727 // Î÷êè Ñèíåé êîìàíäû:
2728 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2729 end;
2730 ///// /////
2731 end;
2733 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2734 var
2735 dw: DWORD;
2736 b: Byte;
2737 str: String;
2738 boo: Boolean;
2740 procedure LoadPanelArray(var panels: TPanelArray);
2741 var
2742 PAMem: TBinMemoryReader;
2743 i, id: Integer;
2744 begin
2745 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2746 PAMem := TBinMemoryReader.Create();
2747 PAMem.LoadFromMemory(Mem);
2749 for i := 0 to Length(panels)-1 do
2750 if panels[i].SaveIt then
2751 begin
2752 // ID ïàíåëè:
2753 PAMem.ReadInt(id);
2754 if id <> i then
2755 begin
2756 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2757 end;
2758 // Çàãðóæàåì ïàíåëü:
2759 panels[i].LoadState(PAMem);
2760 end;
2762 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2763 PAMem.Free();
2764 end;
2766 procedure LoadFlag(flag: PFlag);
2767 begin
2768 // Ñèãíàòóðà ôëàãà:
2769 Mem.ReadDWORD(dw);
2770 if dw <> FLAG_SIGNATURE then // 'FLAG'
2771 begin
2772 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2773 end;
2774 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2775 Mem.ReadByte(flag^.RespawnType);
2776 // Ñîñòîÿíèå ôëàãà:
2777 Mem.ReadByte(flag^.State);
2778 // Íàïðàâëåíèå ôëàãà:
2779 Mem.ReadByte(b);
2780 if b = 1 then
2781 flag^.Direction := D_LEFT
2782 else // b = 2
2783 flag^.Direction := D_RIGHT;
2784 // Îáúåêò ôëàãà:
2785 Obj_LoadState(@flag^.Obj, Mem);
2786 end;
2788 begin
2789 if Mem = nil then
2790 Exit;
2792 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2793 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2794 LoadPanelArray(gWalls);
2795 // Çàãðóæàåì ïàíåëè ôîíà:
2796 LoadPanelArray(gRenderBackgrounds);
2797 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2798 LoadPanelArray(gRenderForegrounds);
2799 // Çàãðóæàåì ïàíåëè âîäû:
2800 LoadPanelArray(gWater);
2801 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2802 LoadPanelArray(gAcid1);
2803 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2804 LoadPanelArray(gAcid2);
2805 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2806 LoadPanelArray(gSteps);
2807 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2808 LoadPanelArray(gLifts);
2809 ///// /////
2811 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2812 g_GFX_Init();
2813 mapCreateGrid();
2815 ///// Çàãðóæàåì ìóçûêó: /////
2816 // Ñèãíàòóðà ìóçûêè:
2817 Mem.ReadDWORD(dw);
2818 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2819 begin
2820 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2821 end;
2822 // Íàçâàíèå ìóçûêè:
2823 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2824 Mem.ReadString(str);
2825 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2826 Mem.ReadDWORD(dw);
2827 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2828 Mem.ReadBoolean(boo);
2829 // Çàïóñêàåì ýòó ìóçûêó:
2830 gMusic.SetByName(str);
2831 gMusic.SpecPause := boo;
2832 gMusic.Play();
2833 gMusic.Pause(True);
2834 gMusic.SetPosition(dw);
2835 ///// /////
2837 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2838 Mem.ReadInt(gTotalMonsters);
2839 ///// /////
2841 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2842 if gGameSettings.GameMode = GM_CTF then
2843 begin
2844 // Ôëàã Êðàñíîé êîìàíäû:
2845 LoadFlag(@gFlags[FLAG_RED]);
2846 // Ôëàã Ñèíåé êîìàíäû:
2847 LoadFlag(@gFlags[FLAG_BLUE]);
2848 end;
2849 ///// /////
2851 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2852 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2853 begin
2854 // Î÷êè Êðàñíîé êîìàíäû:
2855 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2856 // Î÷êè Ñèíåé êîìàíäû:
2857 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2858 end;
2859 ///// /////
2860 end;
2862 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2863 var
2864 Arr: TPanelArray;
2865 begin
2866 Result := nil;
2867 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2868 Arr := PanelByID[PanelID].PWhere^;
2869 PanelArrayID := PanelByID[PanelID].PArrID;
2870 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2871 end;
2874 // trace liquid, stepping by `dx` and `dy`
2875 // return last seen liquid coords, and `false` if we're started outside of the liquid
2876 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2877 const
2878 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2879 begin
2880 topx := x;
2881 topy := y;
2882 // started outside of the liquid?
2883 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2884 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2885 result := true;
2886 while true do
2887 begin
2888 Inc(x, dx);
2889 Inc(y, dy);
2890 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2891 topx := x;
2892 topy := y;
2893 end;
2894 end;
2897 end.