DEADSOFTWARE

skip disabled panels in map rendering collector
[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): TPanel;
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()`
188 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
191 type
192 TPanelGrid = specialize TBodyGridBase<TPanel>;
194 var
195 mapGrid: TPanelGrid = nil;
198 implementation
200 uses
201 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
202 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
203 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
204 Math, g_monsters, g_saveload, g_language, g_netmsg,
205 utils, sfs,
206 ImagingTypes, Imaging, ImagingUtility,
207 ImagingGif, ImagingNetworkGraphics;
209 const
210 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
211 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
212 FLAG_SIGNATURE = $47414C46; // 'FLAG'
215 function panelTypeToTag (panelType: Word): Integer;
216 begin
217 case panelType of
218 PANEL_WALL: result := GridTagWall; // gWalls
219 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
220 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
221 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
222 PANEL_WATER: result := GridTagWater; // gWater
223 PANEL_ACID1: result := GridTagAcid1; // gAcid1
224 PANEL_ACID2: result := GridTagAcid2; // gAcid2
225 PANEL_STEP: result := GridTagStep; // gSteps
226 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
227 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
228 else result := GridTagInvalid;
229 end;
230 end;
233 function dplLess (a, b: TObject): Boolean;
234 var
235 pa, pb: TPanel;
236 begin
237 pa := TPanel(a);
238 pb := TPanel(b);
239 if (pa.tag < pb.tag) then begin result := true; exit; end;
240 if (pa.tag > pb.tag) then begin result := false; exit; end;
241 result := (pa.arrIdx < pb.arrIdx);
242 end;
244 procedure dplClear ();
245 begin
246 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
247 end;
250 type
251 TPanelID = record
252 PWhere: ^TPanelArray;
253 PArrID: Integer;
254 end;
256 var
257 PanelById: array of TPanelID;
258 Textures: TLevelTextureArray;
259 RespawnPoints: Array of TRespawnPoint;
260 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
261 //DOMFlagPoints: Array of TFlagPoint;
264 procedure g_Map_ProfilersBegin ();
265 begin
266 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
267 profMapCollision.mainBegin(g_profile_collision);
268 // create sections
269 if g_profile_collision then
270 begin
271 profMapCollision.sectionBegin('*solids');
272 profMapCollision.sectionEnd();
273 profMapCollision.sectionBegin('liquids');
274 profMapCollision.sectionEnd();
275 end;
276 end;
278 procedure g_Map_ProfilersEnd ();
279 begin
280 if (profMapCollision <> nil) then profMapCollision.mainEnd();
281 end;
284 // wall index in `gWalls` or -1
285 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
286 var
287 ex, ey: Integer;
288 begin
289 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
290 if (result <> nil) then
291 begin
292 if (hitx <> nil) then hitx^ := ex;
293 if (hity <> nil) then hity^ := ey;
294 end
295 else
296 begin
297 if (hitx <> nil) then hitx^ := x1;
298 if (hity <> nil) then hity^ := y1;
299 end;
300 end;
303 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
305 function checker (pan: TPanel; tag: Integer): Boolean;
306 begin
308 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
309 begin
310 result := pan.Enabled; // stop if wall is enabled
311 exit;
312 end;
315 if ((tag and GridTagLift) <> 0) then
316 begin
317 // stop if the lift of the right type
318 result :=
319 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
320 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
321 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
322 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
323 exit;
324 end;
326 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
327 end;
329 var
330 tagmask: Integer = 0;
331 begin
332 result := false;
334 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
335 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
336 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
337 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
338 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
339 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
340 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
342 if (tagmask = 0) then exit;// just in case
343 if ((tagmask and GridTagLift) <> 0) then
344 begin
345 // slow
346 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
347 end
348 else
349 begin
350 // fast
351 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
352 end;
353 end;
356 function g_Map_IsSpecialTexture(Texture: String): Boolean;
357 begin
358 Result := (Texture = TEXTURE_NAME_WATER) or
359 (Texture = TEXTURE_NAME_ACID1) or
360 (Texture = TEXTURE_NAME_ACID2);
361 end;
363 procedure CreateDoorMap();
364 var
365 PanelArray: Array of record
366 X, Y: Integer;
367 Width, Height: Word;
368 Active: Boolean;
369 PanelID: DWORD;
370 end;
371 a, b, c, m, i, len: Integer;
372 ok: Boolean;
373 begin
374 if gWalls = nil then
375 Exit;
377 i := 0;
378 len := 128;
379 SetLength(PanelArray, len);
381 for a := 0 to High(gWalls) do
382 if gWalls[a].Door then
383 begin
384 PanelArray[i].X := gWalls[a].X;
385 PanelArray[i].Y := gWalls[a].Y;
386 PanelArray[i].Width := gWalls[a].Width;
387 PanelArray[i].Height := gWalls[a].Height;
388 PanelArray[i].Active := True;
389 PanelArray[i].PanelID := a;
391 i := i + 1;
392 if i = len then
393 begin
394 len := len + 128;
395 SetLength(PanelArray, len);
396 end;
397 end;
399 // Íåò äâåðåé:
400 if i = 0 then
401 begin
402 PanelArray := nil;
403 Exit;
404 end;
406 SetLength(gDoorMap, 0);
408 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
410 for a := 0 to i-1 do
411 if PanelArray[a].Active then
412 begin
413 PanelArray[a].Active := False;
414 m := Length(gDoorMap);
415 SetLength(gDoorMap, m+1);
416 SetLength(gDoorMap[m], 1);
417 gDoorMap[m, 0] := PanelArray[a].PanelID;
418 ok := True;
420 while ok do
421 begin
422 ok := False;
424 for b := 0 to i-1 do
425 if PanelArray[b].Active then
426 for c := 0 to High(gDoorMap[m]) do
427 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
428 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
429 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
430 PanelArray[b].Width, PanelArray[b].Height,
431 gWalls[gDoorMap[m, c]].X,
432 gWalls[gDoorMap[m, c]].Y,
433 gWalls[gDoorMap[m, c]].Width,
434 gWalls[gDoorMap[m, c]].Height) then
435 begin
436 PanelArray[b].Active := False;
437 SetLength(gDoorMap[m],
438 Length(gDoorMap[m])+1);
439 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
440 ok := True;
441 Break;
442 end;
443 end;
445 g_Game_StepLoading();
446 end;
448 PanelArray := nil;
449 end;
451 procedure CreateLiftMap();
452 var
453 PanelArray: Array of record
454 X, Y: Integer;
455 Width, Height: Word;
456 Active: Boolean;
457 end;
458 a, b, c, len, i, j: Integer;
459 ok: Boolean;
460 begin
461 if gLifts = nil then
462 Exit;
464 len := Length(gLifts);
465 SetLength(PanelArray, len);
467 for a := 0 to len-1 do
468 begin
469 PanelArray[a].X := gLifts[a].X;
470 PanelArray[a].Y := gLifts[a].Y;
471 PanelArray[a].Width := gLifts[a].Width;
472 PanelArray[a].Height := gLifts[a].Height;
473 PanelArray[a].Active := True;
474 end;
476 SetLength(gLiftMap, len);
477 i := 0;
479 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
481 for a := 0 to len-1 do
482 if PanelArray[a].Active then
483 begin
484 PanelArray[a].Active := False;
485 SetLength(gLiftMap[i], 32);
486 j := 0;
487 gLiftMap[i, j] := a;
488 ok := True;
490 while ok do
491 begin
492 ok := False;
493 for b := 0 to len-1 do
494 if PanelArray[b].Active then
495 for c := 0 to j do
496 if g_CollideAround(PanelArray[b].X,
497 PanelArray[b].Y,
498 PanelArray[b].Width,
499 PanelArray[b].Height,
500 PanelArray[gLiftMap[i, c]].X,
501 PanelArray[gLiftMap[i, c]].Y,
502 PanelArray[gLiftMap[i, c]].Width,
503 PanelArray[gLiftMap[i, c]].Height) then
504 begin
505 PanelArray[b].Active := False;
506 j := j+1;
507 if j > High(gLiftMap[i]) then
508 SetLength(gLiftMap[i],
509 Length(gLiftMap[i])+32);
511 gLiftMap[i, j] := b;
512 ok := True;
514 Break;
515 end;
516 end;
518 SetLength(gLiftMap[i], j+1);
519 i := i+1;
521 g_Game_StepLoading();
522 end;
524 SetLength(gLiftMap, i);
526 PanelArray := nil;
527 end;
529 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
530 CurTex: Integer; sav: Boolean): Integer;
531 var
532 len: Integer;
533 panels: ^TPanelArray;
534 begin
535 Result := -1;
537 case PanelRec.PanelType of
538 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
539 panels := @gWalls;
540 PANEL_BACK:
541 panels := @gRenderBackgrounds;
542 PANEL_FORE:
543 panels := @gRenderForegrounds;
544 PANEL_WATER:
545 panels := @gWater;
546 PANEL_ACID1:
547 panels := @gAcid1;
548 PANEL_ACID2:
549 panels := @gAcid2;
550 PANEL_STEP:
551 panels := @gSteps;
552 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
553 panels := @gLifts;
554 PANEL_BLOCKMON:
555 panels := @gBlockMon;
556 else
557 Exit;
558 end;
560 len := Length(panels^);
561 SetLength(panels^, len + 1);
563 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
564 panels^[len].arrIdx := len;
565 panels^[len].proxyId := -1;
566 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
567 if sav then
568 panels^[len].SaveIt := True;
570 Result := len;
572 len := Length(PanelByID);
573 SetLength(PanelByID, len + 1);
574 PanelByID[len].PWhere := panels;
575 PanelByID[len].PArrID := Result;
576 end;
578 function CreateNullTexture(RecName: String): Integer;
579 begin
580 SetLength(Textures, Length(Textures)+1);
581 result := High(Textures);
583 with Textures[High(Textures)] do
584 begin
585 TextureName := RecName;
586 Width := 1;
587 Height := 1;
588 Anim := False;
589 TextureID := TEXTURE_NONE;
590 end;
591 end;
593 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
594 var
595 WAD: TWADFile;
596 TextureData: Pointer;
597 WADName, txname: String;
598 a, ResLength: Integer;
599 begin
600 Result := -1;
602 if Textures <> nil then
603 for a := 0 to High(Textures) do
604 if Textures[a].TextureName = RecName then
605 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
606 Result := a;
607 Exit;
608 end;
610 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
611 if (RecName = TEXTURE_NAME_WATER) or
612 (RecName = TEXTURE_NAME_ACID1) or
613 (RecName = TEXTURE_NAME_ACID2) then
614 begin
615 SetLength(Textures, Length(Textures)+1);
617 with Textures[High(Textures)] do
618 begin
619 TextureName := RecName;
621 if TextureName = TEXTURE_NAME_WATER then
622 TextureID := TEXTURE_SPECIAL_WATER
623 else
624 if TextureName = TEXTURE_NAME_ACID1 then
625 TextureID := TEXTURE_SPECIAL_ACID1
626 else
627 if TextureName = TEXTURE_NAME_ACID2 then
628 TextureID := TEXTURE_SPECIAL_ACID2;
630 Anim := False;
631 end;
633 result := High(Textures);
634 Exit;
635 end;
637 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
638 WADName := g_ExtractWadName(RecName);
640 WAD := TWADFile.Create();
642 if WADName <> '' then
643 WADName := GameDir+'/wads/'+WADName
644 else
645 WADName := Map;
647 WAD.ReadFile(WADName);
649 txname := RecName;
651 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
652 begin
653 FreeMem(TextureData);
654 RecName := 'COMMON\ALIEN';
655 end;
658 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
659 begin
660 SetLength(Textures, Length(Textures)+1);
661 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
662 Exit;
663 e_GetTextureSize(Textures[High(Textures)].TextureID,
664 @Textures[High(Textures)].Width,
665 @Textures[High(Textures)].Height);
666 FreeMem(TextureData);
667 Textures[High(Textures)].TextureName := {RecName}txname;
668 Textures[High(Textures)].Anim := False;
670 result := High(Textures);
671 end
672 else // Íåò òàêîãî ðåóñðñà â WAD'å
673 begin
674 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
675 if log then
676 begin
677 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
678 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
679 end;
680 end;
682 WAD.Free();
683 end;
685 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
686 var
687 WAD: TWADFile;
688 TextureWAD: PChar = nil;
689 TextData: Pointer = nil;
690 TextureData: Pointer = nil;
691 cfg: TConfig = nil;
692 WADName: String;
693 ResLength: Integer;
694 TextureResource: String;
695 _width, _height, _framecount, _speed: Integer;
696 _backanimation: Boolean;
697 //imgfmt: string;
698 ia: TDynImageDataArray = nil;
699 f, c, frdelay, frloop: Integer;
700 begin
701 result := -1;
703 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
705 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
706 WADName := g_ExtractWadName(RecName);
708 WAD := TWADFile.Create();
709 try
710 if WADName <> '' then
711 WADName := GameDir+'/wads/'+WADName
712 else
713 WADName := Map;
715 WAD.ReadFile(WADName);
717 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
718 begin
719 if log then
720 begin
721 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
722 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
723 end;
724 exit;
725 end;
727 {TEST
728 if WADName = Map then
729 begin
730 //FreeMem(TextureWAD);
731 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
732 end;
735 WAD.FreeWAD();
737 if ResLength < 6 then
738 begin
739 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
740 exit;
741 end;
743 // ýòî ïòèöà? ýòî ñàìîë¸ò?
744 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
745 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
746 begin
747 // íåò, ýòî ñóïåðìåí!
748 if not WAD.ReadMemory(TextureWAD, ResLength) then
749 begin
750 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
751 exit;
752 end;
754 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
755 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
756 begin
757 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
758 exit;
759 end;
761 cfg := TConfig.CreateMem(TextData, ResLength);
763 TextureResource := cfg.ReadStr('', 'resource', '');
764 if TextureResource = '' then
765 begin
766 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
767 exit;
768 end;
770 _width := cfg.ReadInt('', 'framewidth', 0);
771 _height := cfg.ReadInt('', 'frameheight', 0);
772 _framecount := cfg.ReadInt('', 'framecount', 0);
773 _speed := cfg.ReadInt('', 'waitcount', 0);
774 _backanimation := cfg.ReadBool('', 'backanimation', False);
776 cfg.Free();
777 cfg := nil;
779 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
780 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
781 begin
782 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
783 exit;
784 end;
786 WAD.Free();
787 WAD := nil;
789 SetLength(Textures, Length(Textures)+1);
790 with Textures[High(Textures)] do
791 begin
792 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
793 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
794 begin
795 TextureName := RecName;
796 Width := _width;
797 Height := _height;
798 Anim := True;
799 FramesCount := _framecount;
800 Speed := _speed;
801 result := High(Textures);
802 end
803 else
804 begin
805 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
806 end;
807 end;
808 end
809 else
810 begin
811 // try animated image
813 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
814 if length(imgfmt) = 0 then
815 begin
816 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
817 exit;
818 end;
820 GlobalMetadata.ClearMetaItems();
821 GlobalMetadata.ClearMetaItemsForSaving();
822 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
823 begin
824 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
825 exit;
826 end;
827 if length(ia) = 0 then
828 begin
829 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
830 exit;
831 end;
833 WAD.Free();
834 WAD := nil;
836 _width := ia[0].width;
837 _height := ia[0].height;
838 _framecount := length(ia);
839 _speed := 1;
840 _backanimation := false;
841 frdelay := -1;
842 frloop := -666;
843 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
844 begin
845 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
846 try
847 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
848 frdelay := f;
849 if f < 0 then f := 0;
850 // rounding ;-)
851 c := f mod 28;
852 if c < 13 then c := 0 else c := 1;
853 f := (f div 28)+c;
854 if f < 1 then f := 1 else if f > 255 then f := 255;
855 _speed := f;
856 except
857 end;
858 end;
859 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
860 begin
861 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
862 try
863 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
864 frloop := f;
865 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
866 except
867 end;
868 end;
869 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
870 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
871 f := ord(_backanimation);
872 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);
874 SetLength(Textures, Length(Textures)+1);
875 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
876 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
877 begin
878 Textures[High(Textures)].TextureName := RecName;
879 Textures[High(Textures)].Width := _width;
880 Textures[High(Textures)].Height := _height;
881 Textures[High(Textures)].Anim := True;
882 Textures[High(Textures)].FramesCount := length(ia);
883 Textures[High(Textures)].Speed := _speed;
884 result := High(Textures);
885 //writeln(' CREATED!');
886 end
887 else
888 begin
889 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
890 end;
891 end;
892 finally
893 for f := 0 to High(ia) do FreeImage(ia[f]);
894 WAD.Free();
895 cfg.Free();
896 if TextureWAD <> nil then FreeMem(TextureWAD);
897 if TextData <> nil then FreeMem(TextData);
898 if TextureData <> nil then FreeMem(TextureData);
899 end;
900 end;
902 procedure CreateItem(Item: TItemRec_1);
903 begin
904 if g_Game_IsClient then Exit;
906 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
907 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
908 Exit;
910 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
911 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
912 end;
914 procedure CreateArea(Area: TAreaRec_1);
915 var
916 a: Integer;
917 id: DWORD = 0;
918 begin
919 case Area.AreaType of
920 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
921 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
922 begin
923 SetLength(RespawnPoints, Length(RespawnPoints)+1);
924 with RespawnPoints[High(RespawnPoints)] do
925 begin
926 X := Area.X;
927 Y := Area.Y;
928 Direction := TDirection(Area.Direction);
930 case Area.AreaType of
931 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
932 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
933 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
934 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
935 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
936 end;
937 end;
938 end;
940 AREA_REDFLAG, AREA_BLUEFLAG:
941 begin
942 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
944 if FlagPoints[a] <> nil then Exit;
946 New(FlagPoints[a]);
948 with FlagPoints[a]^ do
949 begin
950 X := Area.X-FLAGRECT.X;
951 Y := Area.Y-FLAGRECT.Y;
952 Direction := TDirection(Area.Direction);
953 end;
955 with gFlags[a] do
956 begin
957 case a of
958 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
959 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
960 end;
962 Animation := TAnimation.Create(id, True, 8);
963 Obj.Rect := FLAGRECT;
965 g_Map_ResetFlag(a);
966 end;
967 end;
969 AREA_DOMFLAG:
970 begin
971 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
972 with DOMFlagPoints[High(DOMFlagPoints)] do
973 begin
974 X := Area.X;
975 Y := Area.Y;
976 Direction := TDirection(Area.Direction);
977 end;
979 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
980 end;
981 end;
982 end;
984 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
985 var
986 _trigger: TTrigger;
987 begin
988 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
990 with _trigger do
991 begin
992 X := Trigger.X;
993 Y := Trigger.Y;
994 Width := Trigger.Width;
995 Height := Trigger.Height;
996 Enabled := ByteBool(Trigger.Enabled);
997 TexturePanel := Trigger.TexturePanel;
998 TexturePanelType := fTexturePanel1Type;
999 ShotPanelType := fTexturePanel2Type;
1000 TriggerType := Trigger.TriggerType;
1001 ActivateType := Trigger.ActivateType;
1002 Keys := Trigger.Keys;
1003 Data.Default := Trigger.DATA;
1004 end;
1006 g_Triggers_Create(_trigger);
1007 end;
1009 procedure CreateMonster(monster: TMonsterRec_1);
1010 var
1011 a: Integer;
1012 mon: TMonster;
1013 begin
1014 if g_Game_IsClient then Exit;
1016 if (gGameSettings.GameType = GT_SINGLE)
1017 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1018 begin
1019 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1021 if gTriggers <> nil then
1022 begin
1023 for a := 0 to High(gTriggers) do
1024 begin
1025 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1026 begin
1027 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1028 end;
1029 end;
1030 end;
1032 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1033 end;
1034 end;
1036 procedure g_Map_ReAdd_DieTriggers();
1038 function monsDieTrig (mon: TMonster): Boolean;
1039 var
1040 a: Integer;
1041 begin
1042 result := false; // don't stop
1043 mon.ClearTriggers();
1044 for a := 0 to High(gTriggers) do
1045 begin
1046 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1047 begin
1048 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1049 end;
1050 end;
1051 end;
1053 begin
1054 if g_Game_IsClient then Exit;
1056 g_Mons_ForEach(monsDieTrig);
1057 end;
1059 function extractWadName(resourceName: string): string;
1060 var
1061 posN: Integer;
1062 begin
1063 posN := Pos(':', resourceName);
1064 if posN > 0 then
1065 Result:= Copy(resourceName, 0, posN-1)
1066 else
1067 Result := '';
1068 end;
1070 procedure addResToExternalResList(res: string);
1071 begin
1072 res := extractWadName(res);
1073 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1074 gExternalResources.Add(res);
1075 end;
1077 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1078 var
1079 textures: TTexturesRec1Array;
1080 mapHeader: TMapHeaderRec_1;
1081 i: integer;
1082 resFile: String = '';
1083 begin
1084 if gExternalResources = nil then
1085 gExternalResources := TStringList.Create;
1087 gExternalResources.Clear;
1088 textures := mapReader.GetTextures();
1089 for i := 0 to High(textures) do
1090 begin
1091 addResToExternalResList(resFile);
1092 end;
1094 textures := nil;
1096 mapHeader := mapReader.GetMapHeader;
1098 addResToExternalResList(mapHeader.MusicName);
1099 addResToExternalResList(mapHeader.SkyName);
1100 end;
1103 procedure mapCreateGrid ();
1104 var
1105 mapX0: Integer = $3fffffff;
1106 mapY0: Integer = $3fffffff;
1107 mapX1: Integer = -$3fffffff;
1108 mapY1: Integer = -$3fffffff;
1110 procedure calcBoundingBox (constref panels: TPanelArray);
1111 var
1112 idx: Integer;
1113 pan: TPanel;
1114 begin
1115 for idx := 0 to High(panels) do
1116 begin
1117 pan := panels[idx];
1118 if not pan.visvalid then continue;
1119 if (pan.Width < 1) or (pan.Height < 1) then continue;
1120 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1121 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1122 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1123 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1124 end;
1125 end;
1127 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1128 var
1129 idx: Integer;
1130 pan: TPanel;
1131 begin
1132 tag := panelTypeToTag(tag);
1133 for idx := 0 to High(panels) do
1134 begin
1135 pan := panels[idx];
1136 pan.tag := tag;
1137 if not pan.visvalid then continue;
1138 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1139 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1140 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1141 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1143 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1144 begin
1145 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1146 end;
1148 {$ENDIF}
1149 end;
1150 end;
1152 begin
1153 mapGrid.Free();
1154 mapGrid := nil;
1156 calcBoundingBox(gWalls);
1157 calcBoundingBox(gRenderBackgrounds);
1158 calcBoundingBox(gRenderForegrounds);
1159 calcBoundingBox(gWater);
1160 calcBoundingBox(gAcid1);
1161 calcBoundingBox(gAcid2);
1162 calcBoundingBox(gSteps);
1163 calcBoundingBox(gLifts);
1164 calcBoundingBox(gBlockMon);
1166 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);
1168 if (mapX0 > 0) then mapX0 := 0;
1169 if (mapY0 > 0) then mapY0 := 0;
1171 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1172 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1174 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1176 addPanelsToGrid(gWalls, PANEL_WALL);
1177 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1178 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1179 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1180 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1181 addPanelsToGrid(gWater, PANEL_WATER);
1182 addPanelsToGrid(gAcid1, PANEL_ACID1);
1183 addPanelsToGrid(gAcid2, PANEL_ACID2);
1184 addPanelsToGrid(gSteps, PANEL_STEP);
1185 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1186 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1188 mapGrid.dumpStats();
1190 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1191 end;
1194 function g_Map_Load(Res: String): Boolean;
1195 const
1196 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1197 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1198 var
1199 WAD: TWADFile;
1200 MapReader: TMapReader_1;
1201 Header: TMapHeaderRec_1;
1202 _textures: TTexturesRec1Array;
1203 _texnummap: array of Integer; // `_textures` -> `Textures`
1204 panels: TPanelsRec1Array;
1205 items: TItemsRec1Array;
1206 monsters: TMonsterRec1Array;
1207 areas: TAreasRec1Array;
1208 triggers: TTriggersRec1Array;
1209 a, b, c, k: Integer;
1210 PanelID: DWORD;
1211 AddTextures: TAddTextureArray;
1212 texture: TTextureRec_1;
1213 TriggersTable: Array of record
1214 TexturePanel: Integer;
1215 LiftPanel: Integer;
1216 DoorPanel: Integer;
1217 ShotPanel: Integer;
1218 end;
1219 FileName, mapResName, s, TexName: String;
1220 Data: Pointer;
1221 Len: Integer;
1222 ok, isAnim, trigRef: Boolean;
1223 CurTex, ntn: Integer;
1225 begin
1226 mapGrid.Free();
1227 mapGrid := nil;
1228 //mapTree.Free();
1229 //mapTree := nil;
1231 Result := False;
1232 gMapInfo.Map := Res;
1233 TriggersTable := nil;
1234 FillChar(texture, SizeOf(texture), 0);
1236 sfsGCDisable(); // temporary disable removing of temporary volumes
1237 try
1238 // Çàãðóçêà WAD:
1239 FileName := g_ExtractWadName(Res);
1240 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1241 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1243 WAD := TWADFile.Create();
1244 if not WAD.ReadFile(FileName) then
1245 begin
1246 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1247 WAD.Free();
1248 Exit;
1249 end;
1250 //k8: why loader ignores path here?
1251 mapResName := g_ExtractFileName(Res);
1252 if not WAD.GetMapResource(mapResName, Data, Len) then
1253 begin
1254 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1255 WAD.Free();
1256 Exit;
1257 end;
1259 WAD.Free();
1261 // Çàãðóçêà êàðòû:
1262 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1263 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1264 MapReader := TMapReader_1.Create();
1266 if not MapReader.LoadMap(Data) then
1267 begin
1268 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1269 FreeMem(Data);
1270 MapReader.Free();
1271 Exit;
1272 end;
1274 FreeMem(Data);
1275 generateExternalResourcesList(MapReader);
1276 // Çàãðóçêà òåêñòóð:
1277 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1278 _textures := MapReader.GetTextures();
1279 _texnummap := nil;
1281 // Çàãðóçêà îïèñàíèÿ êàðòû:
1282 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1283 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1284 Header := MapReader.GetMapHeader();
1286 with gMapInfo do
1287 begin
1288 Name := Header.MapName;
1289 Description := Header.MapDescription;
1290 Author := Header.MapAuthor;
1291 MusicName := Header.MusicName;
1292 SkyName := Header.SkyName;
1293 Height := Header.Height;
1294 Width := Header.Width;
1295 end;
1297 // Äîáàâëåíèå òåêñòóð â Textures[]:
1298 if _textures <> nil then
1299 begin
1300 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1301 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1302 SetLength(_texnummap, length(_textures));
1304 for a := 0 to High(_textures) do
1305 begin
1306 SetLength(s, 64);
1307 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1308 for b := 1 to Length(s) do
1309 if s[b] = #0 then
1310 begin
1311 SetLength(s, b-1);
1312 Break;
1313 end;
1314 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1315 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1316 // Àíèìèðîâàííàÿ òåêñòóðà:
1317 if ByteBool(_textures[a].Anim) then
1318 begin
1319 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1320 if ntn < 0 then
1321 begin
1322 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1323 ntn := CreateNullTexture(_textures[a].Resource);
1324 end;
1325 end
1326 else // Îáû÷íàÿ òåêñòóðà:
1327 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1328 if ntn < 0 then
1329 begin
1330 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1331 ntn := CreateNullTexture(_textures[a].Resource);
1332 end;
1334 _texnummap[a] := ntn; // fix texture number
1335 g_Game_StepLoading();
1336 end;
1337 end;
1339 // Çàãðóçêà òðèããåðîâ:
1340 gTriggerClientID := 0;
1341 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1342 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1343 triggers := MapReader.GetTriggers();
1345 // Çàãðóçêà ïàíåëåé:
1346 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1347 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1348 panels := MapReader.GetPanels();
1350 // check texture numbers for panels
1351 for a := 0 to High(panels) do
1352 begin
1353 if panels[a].TextureNum > High(_textures) then
1354 begin
1355 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1356 result := false;
1357 exit;
1358 end;
1359 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1360 end;
1362 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1363 if triggers <> nil then
1364 begin
1365 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1366 SetLength(TriggersTable, Length(triggers));
1367 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1369 for a := 0 to High(TriggersTable) do
1370 begin
1371 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1372 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1373 // Ëèôòû:
1374 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1375 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1376 else
1377 TriggersTable[a].LiftPanel := -1;
1378 // Äâåðè:
1379 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1380 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1381 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1382 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1383 else
1384 TriggersTable[a].DoorPanel := -1;
1385 // Òóðåëü:
1386 if triggers[a].TriggerType = TRIGGER_SHOT then
1387 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1388 else
1389 TriggersTable[a].ShotPanel := -1;
1391 g_Game_StepLoading();
1392 end;
1393 end;
1395 // Ñîçäàåì ïàíåëè:
1396 if panels <> nil then
1397 begin
1398 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1399 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1401 for a := 0 to High(panels) do
1402 begin
1403 SetLength(AddTextures, 0);
1404 trigRef := False;
1405 CurTex := -1;
1406 if _textures <> nil then
1407 begin
1408 texture := _textures[panels[a].TextureNum];
1409 ok := True;
1410 end
1411 else
1412 ok := False;
1414 if ok then
1415 begin
1416 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1417 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1418 ok := False;
1419 if (TriggersTable <> nil) and (_textures <> nil) then
1420 for b := 0 to High(TriggersTable) do
1421 if (TriggersTable[b].TexturePanel = a)
1422 or (TriggersTable[b].ShotPanel = a) then
1423 begin
1424 trigRef := True;
1425 ok := True;
1426 Break;
1427 end;
1428 end;
1430 if ok then
1431 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1432 SetLength(s, 64);
1433 CopyMemory(@s[1], @texture.Resource[0], 64);
1434 // Èçìåðÿåì äëèíó:
1435 Len := Length(s);
1436 for c := Len downto 1 do
1437 if s[c] <> #0 then
1438 begin
1439 Len := c;
1440 Break;
1441 end;
1442 SetLength(s, Len);
1444 // Ñïåö-òåêñòóðû çàïðåùåíû:
1445 if g_Map_IsSpecialTexture(s) then
1446 ok := False
1447 else
1448 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1449 ok := g_Texture_NumNameFindStart(s);
1451 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1452 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1453 if ok then
1454 begin
1455 k := NNF_NAME_BEFORE;
1456 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1457 while ok or (k = NNF_NAME_BEFORE) or
1458 (k = NNF_NAME_EQUALS) do
1459 begin
1460 k := g_Texture_NumNameFindNext(TexName);
1462 if (k = NNF_NAME_BEFORE) or
1463 (k = NNF_NAME_AFTER) then
1464 begin
1465 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1466 if ByteBool(texture.Anim) then
1467 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1468 isAnim := True;
1469 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1470 if not ok then
1471 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1472 isAnim := False;
1473 ok := CreateTexture(TexName, FileName, False) >= 0;
1474 end;
1475 end
1476 else
1477 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1478 isAnim := False;
1479 ok := CreateTexture(TexName, FileName, False) >= 0;
1480 if not ok then
1481 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1482 isAnim := True;
1483 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1484 end;
1485 end;
1487 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1488 if ok then
1489 begin
1490 for c := 0 to High(Textures) do
1491 if Textures[c].TextureName = TexName then
1492 begin
1493 SetLength(AddTextures, Length(AddTextures)+1);
1494 AddTextures[High(AddTextures)].Texture := c;
1495 AddTextures[High(AddTextures)].Anim := isAnim;
1496 Break;
1497 end;
1498 end;
1499 end
1500 else
1501 if k = NNF_NAME_EQUALS then
1502 begin
1503 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1504 SetLength(AddTextures, Length(AddTextures)+1);
1505 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1506 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1507 CurTex := High(AddTextures);
1508 ok := True;
1509 end
1510 else // NNF_NO_NAME
1511 ok := False;
1512 end; // while ok...
1514 ok := True;
1515 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1516 end; // if ok - ññûëàþòñÿ òðèããåðû
1518 if not ok then
1519 begin
1520 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1521 SetLength(AddTextures, 1);
1522 AddTextures[0].Texture := panels[a].TextureNum;
1523 AddTextures[0].Anim := ByteBool(texture.Anim);
1524 CurTex := 0;
1525 end;
1527 //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);
1529 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1530 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1532 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1533 if TriggersTable <> nil then
1534 for b := 0 to High(TriggersTable) do
1535 begin
1536 // Òðèããåð äâåðè/ëèôòà:
1537 if (TriggersTable[b].LiftPanel = a) or
1538 (TriggersTable[b].DoorPanel = a) then
1539 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1540 // Òðèããåð ñìåíû òåêñòóðû:
1541 if TriggersTable[b].TexturePanel = a then
1542 triggers[b].TexturePanel := PanelID;
1543 // Òðèããåð "Òóðåëü":
1544 if TriggersTable[b].ShotPanel = a then
1545 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1546 end;
1548 g_Game_StepLoading();
1549 end;
1550 end;
1552 // create map grid, init other grids (for monsters, for example)
1553 e_WriteLog('Creating map grid', MSG_NOTIFY);
1554 mapCreateGrid();
1556 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1557 if (triggers <> nil) and not gLoadGameMode then
1558 begin
1559 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1560 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1561 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1562 for a := 0 to High(triggers) do
1563 begin
1564 if triggers[a].TexturePanel <> -1 then
1565 b := panels[TriggersTable[a].TexturePanel].PanelType
1566 else
1567 b := 0;
1568 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1569 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1570 c := panels[TriggersTable[a].ShotPanel].PanelType
1571 else
1572 c := 0;
1573 CreateTrigger(triggers[a], b, c);
1574 end;
1575 end;
1577 // Çàãðóçêà ïðåäìåòîâ:
1578 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1579 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1580 items := MapReader.GetItems();
1582 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1583 if (items <> nil) and not gLoadGameMode then
1584 begin
1585 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1586 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1587 for a := 0 to High(items) do
1588 CreateItem(Items[a]);
1589 end;
1591 // Çàãðóçêà îáëàñòåé:
1592 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1593 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1594 areas := MapReader.GetAreas();
1596 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1597 if areas <> nil then
1598 begin
1599 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1600 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1601 for a := 0 to High(areas) do
1602 CreateArea(areas[a]);
1603 end;
1605 // Çàãðóçêà ìîíñòðîâ:
1606 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1607 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1608 monsters := MapReader.GetMonsters();
1610 gTotalMonsters := 0;
1612 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1613 if (monsters <> nil) and not gLoadGameMode then
1614 begin
1615 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1616 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1617 for a := 0 to High(monsters) do
1618 CreateMonster(monsters[a]);
1619 end;
1621 MapReader.Free();
1623 // Çàãðóçêà íåáà:
1624 if gMapInfo.SkyName <> '' then
1625 begin
1626 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1627 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1628 FileName := g_ExtractWadName(gMapInfo.SkyName);
1630 if FileName <> '' then
1631 FileName := GameDir+'/wads/'+FileName
1632 else
1633 begin
1634 FileName := g_ExtractWadName(Res);
1635 end;
1637 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1638 if g_Texture_CreateWAD(BackID, s) then
1639 begin
1640 g_Game_SetupScreenSize();
1641 end
1642 else
1643 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1644 end;
1646 // Çàãðóçêà ìóçûêè:
1647 ok := False;
1648 if gMapInfo.MusicName <> '' then
1649 begin
1650 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1651 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1652 FileName := g_ExtractWadName(gMapInfo.MusicName);
1654 if FileName <> '' then
1655 FileName := GameDir+'/wads/'+FileName
1656 else
1657 begin
1658 FileName := g_ExtractWadName(Res);
1659 end;
1661 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1662 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1663 ok := True
1664 else
1665 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1666 end;
1668 // Îñòàëüíûå óñòàíâêè:
1669 CreateDoorMap();
1670 CreateLiftMap();
1672 g_Items_Init();
1673 g_Weapon_Init();
1674 g_Monsters_Init();
1676 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1677 if not gLoadGameMode then
1678 g_GFX_Init();
1680 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1681 _textures := nil;
1682 panels := nil;
1683 items := nil;
1684 areas := nil;
1685 triggers := nil;
1686 TriggersTable := nil;
1687 AddTextures := nil;
1689 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1690 if ok and (not gLoadGameMode) then
1691 begin
1692 gMusic.SetByName(gMapInfo.MusicName);
1693 gMusic.Play();
1694 end
1695 else
1696 gMusic.SetByName('');
1697 finally
1698 sfsGCEnable(); // enable releasing unused volumes
1699 end;
1701 e_WriteLog('Done loading map.', MSG_NOTIFY);
1702 Result := True;
1703 end;
1705 function g_Map_GetMapInfo(Res: String): TMapInfo;
1706 var
1707 WAD: TWADFile;
1708 MapReader: TMapReader_1;
1709 Header: TMapHeaderRec_1;
1710 FileName: String;
1711 Data: Pointer;
1712 Len: Integer;
1713 begin
1714 FillChar(Result, SizeOf(Result), 0);
1715 FileName := g_ExtractWadName(Res);
1717 WAD := TWADFile.Create();
1718 if not WAD.ReadFile(FileName) then
1719 begin
1720 WAD.Free();
1721 Exit;
1722 end;
1724 //k8: it ignores path again
1725 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1726 begin
1727 WAD.Free();
1728 Exit;
1729 end;
1731 WAD.Free();
1733 MapReader := TMapReader_1.Create();
1735 if not MapReader.LoadMap(Data) then
1736 begin
1737 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1738 ZeroMemory(@Header, SizeOf(Header));
1739 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1740 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1741 end
1742 else
1743 begin
1744 Header := MapReader.GetMapHeader();
1745 Result.Name := Header.MapName;
1746 Result.Description := Header.MapDescription;
1747 end;
1749 FreeMem(Data);
1750 MapReader.Free();
1752 Result.Map := Res;
1753 Result.Author := Header.MapAuthor;
1754 Result.Height := Header.Height;
1755 Result.Width := Header.Width;
1756 end;
1758 function g_Map_GetMapsList(WADName: string): SArray;
1759 var
1760 WAD: TWADFile;
1761 a: Integer;
1762 ResList: SArray;
1763 begin
1764 Result := nil;
1765 WAD := TWADFile.Create();
1766 if not WAD.ReadFile(WADName) then
1767 begin
1768 WAD.Free();
1769 Exit;
1770 end;
1771 ResList := WAD.GetMapResources();
1772 if ResList <> nil then
1773 begin
1774 for a := 0 to High(ResList) do
1775 begin
1776 SetLength(Result, Length(Result)+1);
1777 Result[High(Result)] := ResList[a];
1778 end;
1779 end;
1780 WAD.Free();
1781 end;
1783 function g_Map_Exist(Res: string): Boolean;
1784 var
1785 WAD: TWADFile;
1786 FileName, mnn: string;
1787 ResList: SArray;
1788 a: Integer;
1789 begin
1790 Result := False;
1792 FileName := addWadExtension(g_ExtractWadName(Res));
1794 WAD := TWADFile.Create;
1795 if not WAD.ReadFile(FileName) then
1796 begin
1797 WAD.Free();
1798 Exit;
1799 end;
1801 ResList := WAD.GetMapResources();
1802 WAD.Free();
1804 mnn := g_ExtractFileName(Res);
1805 if ResList <> nil then
1806 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1807 begin
1808 Result := True;
1809 Exit;
1810 end;
1811 end;
1813 procedure g_Map_Free();
1814 var
1815 a: Integer;
1817 procedure FreePanelArray(var panels: TPanelArray);
1818 var
1819 i: Integer;
1821 begin
1822 if panels <> nil then
1823 begin
1824 for i := 0 to High(panels) do
1825 panels[i].Free();
1826 panels := nil;
1827 end;
1828 end;
1830 begin
1831 g_GFX_Free();
1832 g_Weapon_Free();
1833 g_Items_Free();
1834 g_Triggers_Free();
1835 g_Monsters_Free();
1837 RespawnPoints := nil;
1838 if FlagPoints[FLAG_RED] <> nil then
1839 begin
1840 Dispose(FlagPoints[FLAG_RED]);
1841 FlagPoints[FLAG_RED] := nil;
1842 end;
1843 if FlagPoints[FLAG_BLUE] <> nil then
1844 begin
1845 Dispose(FlagPoints[FLAG_BLUE]);
1846 FlagPoints[FLAG_BLUE] := nil;
1847 end;
1848 //DOMFlagPoints := nil;
1850 //gDOMFlags := nil;
1852 if Textures <> nil then
1853 begin
1854 for a := 0 to High(Textures) do
1855 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1856 if Textures[a].Anim then
1857 g_Frames_DeleteByID(Textures[a].FramesID)
1858 else
1859 if Textures[a].TextureID <> TEXTURE_NONE then
1860 e_DeleteTexture(Textures[a].TextureID);
1862 Textures := nil;
1863 end;
1865 FreePanelArray(gWalls);
1866 FreePanelArray(gRenderBackgrounds);
1867 FreePanelArray(gRenderForegrounds);
1868 FreePanelArray(gWater);
1869 FreePanelArray(gAcid1);
1870 FreePanelArray(gAcid2);
1871 FreePanelArray(gSteps);
1872 FreePanelArray(gLifts);
1873 FreePanelArray(gBlockMon);
1875 if BackID <> DWORD(-1) then
1876 begin
1877 gBackSize.X := 0;
1878 gBackSize.Y := 0;
1879 e_DeleteTexture(BackID);
1880 BackID := DWORD(-1);
1881 end;
1883 g_Game_StopAllSounds(False);
1884 gMusic.FreeSound();
1885 g_Sound_Delete(gMapInfo.MusicName);
1887 gMapInfo.Name := '';
1888 gMapInfo.Description := '';
1889 gMapInfo.MusicName := '';
1890 gMapInfo.Height := 0;
1891 gMapInfo.Width := 0;
1893 gDoorMap := nil;
1894 gLiftMap := nil;
1896 PanelByID := nil;
1897 end;
1899 procedure g_Map_Update();
1900 var
1901 a, d, j: Integer;
1902 m: Word;
1903 s: String;
1905 procedure UpdatePanelArray(var panels: TPanelArray);
1906 var
1907 i: Integer;
1909 begin
1910 if panels <> nil then
1911 for i := 0 to High(panels) do
1912 panels[i].Update();
1913 end;
1915 begin
1916 UpdatePanelArray(gWalls);
1917 UpdatePanelArray(gRenderBackgrounds);
1918 UpdatePanelArray(gRenderForegrounds);
1919 UpdatePanelArray(gWater);
1920 UpdatePanelArray(gAcid1);
1921 UpdatePanelArray(gAcid2);
1922 UpdatePanelArray(gSteps);
1924 if gGameSettings.GameMode = GM_CTF then
1925 begin
1926 for a := FLAG_RED to FLAG_BLUE do
1927 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1928 with gFlags[a] do
1929 begin
1930 if gFlags[a].Animation <> nil then
1931 gFlags[a].Animation.Update();
1933 m := g_Obj_Move(@Obj, True, True);
1935 if gTime mod (GAME_TICK*2) <> 0 then
1936 Continue;
1938 // Ñîïðîòèâëåíèå âîçäóõà:
1939 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1941 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1942 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1943 begin
1944 g_Map_ResetFlag(a);
1945 gFlags[a].CaptureTime := 0;
1946 if a = FLAG_RED then
1947 s := _lc[I_PLAYER_FLAG_RED]
1948 else
1949 s := _lc[I_PLAYER_FLAG_BLUE];
1950 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1952 if g_Game_IsNet then
1953 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1954 Continue;
1955 end;
1957 if Count > 0 then
1958 Count := Count - 1;
1960 // Èãðîê áåðåò ôëàã:
1961 if gPlayers <> nil then
1962 begin
1963 j := Random(Length(gPlayers)) - 1;
1965 for d := 0 to High(gPlayers) do
1966 begin
1967 Inc(j);
1968 if j > High(gPlayers) then
1969 j := 0;
1971 if gPlayers[j] <> nil then
1972 if gPlayers[j].Live and
1973 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1974 begin
1975 if gPlayers[j].GetFlag(a) then
1976 Break;
1977 end;
1978 end;
1979 end;
1980 end;
1981 end;
1982 end;
1985 // old algo
1986 procedure g_Map_DrawPanels (PanelType: Word);
1988 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
1989 var
1990 idx: Integer;
1991 begin
1992 if (panels <> nil) then
1993 begin
1994 // alas, no visible set
1995 for idx := 0 to High(panels) do
1996 begin
1997 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1998 end;
1999 end;
2000 end;
2002 begin
2003 case PanelType of
2004 PANEL_WALL: DrawPanels(gWalls);
2005 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2006 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2007 PANEL_FORE: DrawPanels(gRenderForegrounds);
2008 PANEL_WATER: DrawPanels(gWater);
2009 PANEL_ACID1: DrawPanels(gAcid1);
2010 PANEL_ACID2: DrawPanels(gAcid2);
2011 PANEL_STEP: DrawPanels(gSteps);
2012 end;
2013 end;
2016 // new algo
2017 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2019 function checker (pan: TPanel; tag: Integer): Boolean;
2020 begin
2021 result := false; // don't stop, ever
2022 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2023 gDrawPanelList.insert(pan);
2024 end;
2026 begin
2027 dplClear();
2028 //tagmask := panelTypeToTag(PanelType);
2030 {if gdbg_map_use_tree_draw then
2031 begin
2032 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2033 end
2034 else}
2035 begin
2036 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2037 end;
2038 // list will be rendered in `g_game.DrawPlayer()`
2039 end;
2042 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2044 function checker (pan: TPanel; tag: Integer): Boolean;
2045 begin
2046 result := false; // don't stop, ever
2047 pan.DrawShadowVolume(lightX, lightY, radius);
2048 end;
2050 begin
2051 {if gdbg_map_use_tree_draw then
2052 begin
2053 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2054 end
2055 else}
2056 begin
2057 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2058 end;
2059 end;
2062 procedure g_Map_DrawBack(dx, dy: Integer);
2063 begin
2064 if gDrawBackGround and (BackID <> DWORD(-1)) then
2065 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2066 else
2067 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2068 end;
2070 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2071 PanelType: Word; b1x3: Boolean=false): Boolean;
2072 var
2073 a, h: Integer;
2074 begin
2075 Result := False;
2077 if WordBool(PanelType and PANEL_WALL) then
2078 if gWalls <> nil then
2079 begin
2080 h := High(gWalls);
2082 for a := 0 to h do
2083 if gWalls[a].Enabled and
2084 g_Collide(X, Y, Width, Height,
2085 gWalls[a].X, gWalls[a].Y,
2086 gWalls[a].Width, gWalls[a].Height) then
2087 begin
2088 Result := True;
2089 Exit;
2090 end;
2091 end;
2093 if WordBool(PanelType and PANEL_WATER) then
2094 if gWater <> nil then
2095 begin
2096 h := High(gWater);
2098 for a := 0 to h do
2099 if g_Collide(X, Y, Width, Height,
2100 gWater[a].X, gWater[a].Y,
2101 gWater[a].Width, gWater[a].Height) then
2102 begin
2103 Result := True;
2104 Exit;
2105 end;
2106 end;
2108 if WordBool(PanelType and PANEL_ACID1) then
2109 if gAcid1 <> nil then
2110 begin
2111 h := High(gAcid1);
2113 for a := 0 to h do
2114 if g_Collide(X, Y, Width, Height,
2115 gAcid1[a].X, gAcid1[a].Y,
2116 gAcid1[a].Width, gAcid1[a].Height) then
2117 begin
2118 Result := True;
2119 Exit;
2120 end;
2121 end;
2123 if WordBool(PanelType and PANEL_ACID2) then
2124 if gAcid2 <> nil then
2125 begin
2126 h := High(gAcid2);
2128 for a := 0 to h do
2129 if g_Collide(X, Y, Width, Height,
2130 gAcid2[a].X, gAcid2[a].Y,
2131 gAcid2[a].Width, gAcid2[a].Height) then
2132 begin
2133 Result := True;
2134 Exit;
2135 end;
2136 end;
2138 if WordBool(PanelType and PANEL_STEP) then
2139 if gSteps <> nil then
2140 begin
2141 h := High(gSteps);
2143 for a := 0 to h do
2144 if g_Collide(X, Y, Width, Height,
2145 gSteps[a].X, gSteps[a].Y,
2146 gSteps[a].Width, gSteps[a].Height) then
2147 begin
2148 Result := True;
2149 Exit;
2150 end;
2151 end;
2153 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2154 if gLifts <> nil then
2155 begin
2156 h := High(gLifts);
2158 for a := 0 to h do
2159 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2160 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2161 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2162 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2163 g_Collide(X, Y, Width, Height,
2164 gLifts[a].X, gLifts[a].Y,
2165 gLifts[a].Width, gLifts[a].Height) then
2166 begin
2167 Result := True;
2168 Exit;
2169 end;
2170 end;
2172 if WordBool(PanelType and PANEL_BLOCKMON) then
2173 if gBlockMon <> nil then
2174 begin
2175 h := High(gBlockMon);
2177 for a := 0 to h do
2178 if ( (not b1x3) or
2179 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2180 g_Collide(X, Y, Width, Height,
2181 gBlockMon[a].X, gBlockMon[a].Y,
2182 gBlockMon[a].Width, gBlockMon[a].Height) then
2183 begin
2184 Result := True;
2185 Exit;
2186 end;
2187 end;
2188 end;
2190 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2191 var
2192 texid: DWORD;
2194 function checkPanels (constref panels: TPanelArray): Boolean;
2195 var
2196 a: Integer;
2197 begin
2198 result := false;
2199 if panels = nil then exit;
2200 for a := 0 to High(panels) do
2201 begin
2202 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2203 begin
2204 result := true;
2205 texid := panels[a].GetTextureID();
2206 exit;
2207 end;
2208 end;
2209 end;
2211 begin
2212 texid := TEXTURE_NONE;
2213 result := texid;
2214 if not checkPanels(gWater) then
2215 if not checkPanels(gAcid1) then
2216 if not checkPanels(gAcid2) then exit;
2217 result := texid;
2218 end;
2221 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2222 const
2223 SlowMask = GridTagLift or GridTagBlockMon;
2224 function checker (pan: TPanel; tag: Integer): Boolean;
2225 begin
2227 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2228 begin
2229 result := pan.Enabled;
2230 exit;
2231 end;
2234 if ((tag and GridTagLift) <> 0) then
2235 begin
2236 result :=
2237 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2238 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2239 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2240 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2241 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2242 exit;
2243 end;
2245 if ((tag and GridTagBlockMon) <> 0) then
2246 begin
2247 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2248 exit;
2249 end;
2251 // other shit
2252 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2253 result := true; // i found her!
2254 end;
2256 var
2257 tagmask: Integer = 0;
2258 begin
2259 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2260 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2261 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2262 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2263 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2264 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2265 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2267 if (tagmask = 0) then begin result := false; exit; end; // just in case
2269 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2270 if gdbg_map_use_accel_coldet then
2271 begin
2272 {if gdbg_map_use_tree_coldet then
2273 begin
2274 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2275 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2276 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2277 begin
2278 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2279 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2280 end;
2281 end
2282 else}
2283 begin
2284 if (Width = 1) and (Height = 1) then
2285 begin
2286 if ((tagmask and SlowMask) <> 0) then
2287 begin
2288 // slow
2289 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2290 end
2291 else
2292 begin
2293 // fast
2294 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2295 end;
2296 end
2297 else
2298 begin
2299 if ((tagmask and SlowMask) <> 0) then
2300 begin
2301 // slow
2302 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2303 end
2304 else
2305 begin
2306 // fast
2307 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2308 end;
2309 end;
2310 end;
2311 end
2312 else
2313 begin
2314 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2315 end;
2316 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2317 end;
2320 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2321 var
2322 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2323 texid: DWORD;
2325 // slightly different from the old code, but meh...
2326 function checker (pan: TPanel; tag: Integer): Boolean;
2327 begin
2328 result := false; // don't stop, ever
2329 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2330 // check priorities
2331 case cctype of
2332 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2333 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2334 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2335 end;
2336 // collision?
2337 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2338 // yeah
2339 texid := pan.GetTextureID();
2340 // water? water has the highest priority, so stop right here
2341 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2342 // acid2?
2343 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2344 // acid1?
2345 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2346 end;
2348 begin
2349 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2350 if gdbg_map_use_accel_coldet then
2351 begin
2352 texid := TEXTURE_NONE;
2353 {if gdbg_map_use_tree_coldet then
2354 begin
2355 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2356 end
2357 else}
2358 begin
2359 if (Width = 1) and (Height = 1) then
2360 begin
2361 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2362 end
2363 else
2364 begin
2365 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2366 end;
2367 end;
2368 result := texid;
2369 end
2370 else
2371 begin
2372 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2373 end;
2374 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2375 end;
2378 procedure g_Map_EnableWall(ID: DWORD);
2379 var
2380 pan: TPanel;
2381 begin
2382 pan := gWalls[ID];
2383 pan.Enabled := True;
2384 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2386 mapGrid.proxyEnabled[pan.proxyId] := true;
2387 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2388 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2390 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2392 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2393 //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);
2394 {$ENDIF}
2395 end;
2397 procedure g_Map_DisableWall(ID: DWORD);
2398 var
2399 pan: TPanel;
2400 begin
2401 pan := gWalls[ID];
2402 pan.Enabled := False;
2403 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2405 mapGrid.proxyEnabled[pan.proxyId] := false;
2406 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2408 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2410 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2411 //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);
2412 {$ENDIF}
2413 end;
2415 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2416 var
2417 tp: TPanel;
2418 begin
2419 case PanelType of
2420 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2421 tp := gWalls[ID];
2422 PANEL_FORE:
2423 tp := gRenderForegrounds[ID];
2424 PANEL_BACK:
2425 tp := gRenderBackgrounds[ID];
2426 PANEL_WATER:
2427 tp := gWater[ID];
2428 PANEL_ACID1:
2429 tp := gAcid1[ID];
2430 PANEL_ACID2:
2431 tp := gAcid2[ID];
2432 PANEL_STEP:
2433 tp := gSteps[ID];
2434 else
2435 Exit;
2436 end;
2438 tp.NextTexture(AnimLoop);
2439 if g_Game_IsServer and g_Game_IsNet then
2440 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2441 end;
2443 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2444 begin
2445 if gLifts[ID].LiftType = t then
2446 Exit;
2448 with gLifts[ID] do
2449 begin
2450 LiftType := t;
2452 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2453 //TODO: make separate lift tags, and change tag here
2455 if LiftType = 0 then
2456 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2457 else if LiftType = 1 then
2458 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2459 else if LiftType = 2 then
2460 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2461 else if LiftType = 3 then
2462 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2464 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2465 end;
2466 end;
2468 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2469 var
2470 a: Integer;
2471 PointsArray: Array of TRespawnPoint;
2472 begin
2473 Result := False;
2474 SetLength(PointsArray, 0);
2476 if RespawnPoints = nil then
2477 Exit;
2479 for a := 0 to High(RespawnPoints) do
2480 if RespawnPoints[a].PointType = PointType then
2481 begin
2482 SetLength(PointsArray, Length(PointsArray)+1);
2483 PointsArray[High(PointsArray)] := RespawnPoints[a];
2484 end;
2486 if PointsArray = nil then
2487 Exit;
2489 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2490 Result := True;
2491 end;
2493 function g_Map_GetPointCount(PointType: Byte): Word;
2494 var
2495 a: Integer;
2496 begin
2497 Result := 0;
2499 if RespawnPoints = nil then
2500 Exit;
2502 for a := 0 to High(RespawnPoints) do
2503 if RespawnPoints[a].PointType = PointType then
2504 Result := Result + 1;
2505 end;
2507 function g_Map_HaveFlagPoints(): Boolean;
2508 begin
2509 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2510 end;
2512 procedure g_Map_ResetFlag(Flag: Byte);
2513 begin
2514 with gFlags[Flag] do
2515 begin
2516 Obj.X := -1000;
2517 Obj.Y := -1000;
2518 Obj.Vel.X := 0;
2519 Obj.Vel.Y := 0;
2520 Direction := D_LEFT;
2521 State := FLAG_STATE_NONE;
2522 if FlagPoints[Flag] <> nil then
2523 begin
2524 Obj.X := FlagPoints[Flag]^.X;
2525 Obj.Y := FlagPoints[Flag]^.Y;
2526 Direction := FlagPoints[Flag]^.Direction;
2527 State := FLAG_STATE_NORMAL;
2528 end;
2529 Count := -1;
2530 end;
2531 end;
2533 procedure g_Map_DrawFlags();
2534 var
2535 i, dx: Integer;
2536 Mirror: TMirrorType;
2537 begin
2538 if gGameSettings.GameMode <> GM_CTF then
2539 Exit;
2541 for i := FLAG_RED to FLAG_BLUE do
2542 with gFlags[i] do
2543 if State <> FLAG_STATE_CAPTURED then
2544 begin
2545 if State = FLAG_STATE_NONE then
2546 continue;
2548 if Direction = D_LEFT then
2549 begin
2550 Mirror := M_HORIZONTAL;
2551 dx := -1;
2552 end
2553 else
2554 begin
2555 Mirror := M_NONE;
2556 dx := 1;
2557 end;
2559 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2561 if g_debug_Frames then
2562 begin
2563 e_DrawQuad(Obj.X+Obj.Rect.X,
2564 Obj.Y+Obj.Rect.Y,
2565 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2566 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2567 0, 255, 0);
2568 end;
2569 end;
2570 end;
2572 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2573 var
2574 dw: DWORD;
2575 b: Byte;
2576 str: String;
2577 boo: Boolean;
2579 procedure SavePanelArray(var panels: TPanelArray);
2580 var
2581 PAMem: TBinMemoryWriter;
2582 i: Integer;
2583 begin
2584 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2585 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2587 i := 0;
2588 while i < Length(panels) do
2589 begin
2590 if panels[i].SaveIt then
2591 begin
2592 // ID ïàíåëè:
2593 PAMem.WriteInt(i);
2594 // Ñîõðàíÿåì ïàíåëü:
2595 panels[i].SaveState(PAMem);
2596 end;
2597 Inc(i);
2598 end;
2600 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2601 PAMem.SaveToMemory(Mem);
2602 PAMem.Free();
2603 end;
2605 procedure SaveFlag(flag: PFlag);
2606 begin
2607 // Ñèãíàòóðà ôëàãà:
2608 dw := FLAG_SIGNATURE; // 'FLAG'
2609 Mem.WriteDWORD(dw);
2610 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2611 Mem.WriteByte(flag^.RespawnType);
2612 // Ñîñòîÿíèå ôëàãà:
2613 Mem.WriteByte(flag^.State);
2614 // Íàïðàâëåíèå ôëàãà:
2615 if flag^.Direction = D_LEFT then
2616 b := 1
2617 else // D_RIGHT
2618 b := 2;
2619 Mem.WriteByte(b);
2620 // Îáúåêò ôëàãà:
2621 Obj_SaveState(@flag^.Obj, Mem);
2622 end;
2624 begin
2625 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2627 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2628 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2629 SavePanelArray(gWalls);
2630 // Ñîõðàíÿåì ïàíåëè ôîíà:
2631 SavePanelArray(gRenderBackgrounds);
2632 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2633 SavePanelArray(gRenderForegrounds);
2634 // Ñîõðàíÿåì ïàíåëè âîäû:
2635 SavePanelArray(gWater);
2636 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2637 SavePanelArray(gAcid1);
2638 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2639 SavePanelArray(gAcid2);
2640 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2641 SavePanelArray(gSteps);
2642 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2643 SavePanelArray(gLifts);
2644 ///// /////
2646 ///// Ñîõðàíÿåì ìóçûêó: /////
2647 // Ñèãíàòóðà ìóçûêè:
2648 dw := MUSIC_SIGNATURE; // 'MUSI'
2649 Mem.WriteDWORD(dw);
2650 // Íàçâàíèå ìóçûêè:
2651 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2652 if gMusic.NoMusic then
2653 str := ''
2654 else
2655 str := gMusic.Name;
2656 Mem.WriteString(str, 64);
2657 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2658 dw := gMusic.GetPosition();
2659 Mem.WriteDWORD(dw);
2660 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2661 boo := gMusic.SpecPause;
2662 Mem.WriteBoolean(boo);
2663 ///// /////
2665 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2666 Mem.WriteInt(gTotalMonsters);
2667 ///// /////
2669 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2670 if gGameSettings.GameMode = GM_CTF then
2671 begin
2672 // Ôëàã Êðàñíîé êîìàíäû:
2673 SaveFlag(@gFlags[FLAG_RED]);
2674 // Ôëàã Ñèíåé êîìàíäû:
2675 SaveFlag(@gFlags[FLAG_BLUE]);
2676 end;
2677 ///// /////
2679 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2680 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2681 begin
2682 // Î÷êè Êðàñíîé êîìàíäû:
2683 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2684 // Î÷êè Ñèíåé êîìàíäû:
2685 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2686 end;
2687 ///// /////
2688 end;
2690 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2691 var
2692 dw: DWORD;
2693 b: Byte;
2694 str: String;
2695 boo: Boolean;
2697 procedure LoadPanelArray(var panels: TPanelArray);
2698 var
2699 PAMem: TBinMemoryReader;
2700 i, id: Integer;
2701 begin
2702 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2703 PAMem := TBinMemoryReader.Create();
2704 PAMem.LoadFromMemory(Mem);
2706 for i := 0 to Length(panels)-1 do
2707 if panels[i].SaveIt then
2708 begin
2709 // ID ïàíåëè:
2710 PAMem.ReadInt(id);
2711 if id <> i then
2712 begin
2713 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2714 end;
2715 // Çàãðóæàåì ïàíåëü:
2716 panels[i].LoadState(PAMem);
2717 end;
2719 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2720 PAMem.Free();
2721 end;
2723 procedure LoadFlag(flag: PFlag);
2724 begin
2725 // Ñèãíàòóðà ôëàãà:
2726 Mem.ReadDWORD(dw);
2727 if dw <> FLAG_SIGNATURE then // 'FLAG'
2728 begin
2729 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2730 end;
2731 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2732 Mem.ReadByte(flag^.RespawnType);
2733 // Ñîñòîÿíèå ôëàãà:
2734 Mem.ReadByte(flag^.State);
2735 // Íàïðàâëåíèå ôëàãà:
2736 Mem.ReadByte(b);
2737 if b = 1 then
2738 flag^.Direction := D_LEFT
2739 else // b = 2
2740 flag^.Direction := D_RIGHT;
2741 // Îáúåêò ôëàãà:
2742 Obj_LoadState(@flag^.Obj, Mem);
2743 end;
2745 begin
2746 if Mem = nil then
2747 Exit;
2749 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2750 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2751 LoadPanelArray(gWalls);
2752 // Çàãðóæàåì ïàíåëè ôîíà:
2753 LoadPanelArray(gRenderBackgrounds);
2754 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2755 LoadPanelArray(gRenderForegrounds);
2756 // Çàãðóæàåì ïàíåëè âîäû:
2757 LoadPanelArray(gWater);
2758 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2759 LoadPanelArray(gAcid1);
2760 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2761 LoadPanelArray(gAcid2);
2762 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2763 LoadPanelArray(gSteps);
2764 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2765 LoadPanelArray(gLifts);
2766 ///// /////
2768 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2769 g_GFX_Init();
2770 mapCreateGrid();
2772 ///// Çàãðóæàåì ìóçûêó: /////
2773 // Ñèãíàòóðà ìóçûêè:
2774 Mem.ReadDWORD(dw);
2775 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2776 begin
2777 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2778 end;
2779 // Íàçâàíèå ìóçûêè:
2780 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2781 Mem.ReadString(str);
2782 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2783 Mem.ReadDWORD(dw);
2784 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2785 Mem.ReadBoolean(boo);
2786 // Çàïóñêàåì ýòó ìóçûêó:
2787 gMusic.SetByName(str);
2788 gMusic.SpecPause := boo;
2789 gMusic.Play();
2790 gMusic.Pause(True);
2791 gMusic.SetPosition(dw);
2792 ///// /////
2794 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2795 Mem.ReadInt(gTotalMonsters);
2796 ///// /////
2798 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2799 if gGameSettings.GameMode = GM_CTF then
2800 begin
2801 // Ôëàã Êðàñíîé êîìàíäû:
2802 LoadFlag(@gFlags[FLAG_RED]);
2803 // Ôëàã Ñèíåé êîìàíäû:
2804 LoadFlag(@gFlags[FLAG_BLUE]);
2805 end;
2806 ///// /////
2808 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2809 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2810 begin
2811 // Î÷êè Êðàñíîé êîìàíäû:
2812 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2813 // Î÷êè Ñèíåé êîìàíäû:
2814 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2815 end;
2816 ///// /////
2817 end;
2819 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2820 var
2821 Arr: TPanelArray;
2822 begin
2823 Result := nil;
2824 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2825 Arr := PanelByID[PanelID].PWhere^;
2826 PanelArrayID := PanelByID[PanelID].PArrID;
2827 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2828 end;
2831 // trace liquid, stepping by `dx` and `dy`
2832 // return last seen liquid coords, and `false` if we're started outside of the liquid
2833 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2834 const
2835 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2836 begin
2837 topx := x;
2838 topy := y;
2839 // started outside of the liquid?
2840 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2841 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2842 result := true;
2843 while true do
2844 begin
2845 Inc(x, dx);
2846 Inc(y, dy);
2847 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2848 topx := x;
2849 topy := y;
2850 end;
2851 end;
2854 end.