DEADSOFTWARE

fixed passing thru disabled walls
[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);
1128 var
1129 idx: Integer;
1130 pan: TPanel;
1131 newtag: Integer;
1132 begin
1133 //tag := panelTypeToTag(tag);
1134 for idx := 0 to High(panels) do
1135 begin
1136 pan := panels[idx];
1137 if not pan.visvalid then continue;
1138 if (pan.proxyId <> -1) then
1139 begin
1140 {$IF DEFINED(D2F_DEBUG)}
1141 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);
1142 {$ENDIF}
1143 continue;
1144 end;
1145 case pan.PanelType of
1146 PANEL_WALL: newtag := GridTagWall;
1147 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1148 PANEL_BACK: newtag := GridTagBack;
1149 PANEL_FORE: newtag := GridTagFore;
1150 PANEL_WATER: newtag := GridTagWater;
1151 PANEL_ACID1: newtag := GridTagAcid1;
1152 PANEL_ACID2: newtag := GridTagAcid2;
1153 PANEL_STEP: newtag := GridTagStep;
1154 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1155 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1156 else continue; // oops
1157 end;
1158 pan.tag := newtag;
1160 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1161 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1162 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1163 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1165 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1166 begin
1167 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1168 end;
1170 {$ENDIF}
1171 end;
1172 end;
1174 begin
1175 mapGrid.Free();
1176 mapGrid := nil;
1178 calcBoundingBox(gWalls);
1179 calcBoundingBox(gRenderBackgrounds);
1180 calcBoundingBox(gRenderForegrounds);
1181 calcBoundingBox(gWater);
1182 calcBoundingBox(gAcid1);
1183 calcBoundingBox(gAcid2);
1184 calcBoundingBox(gSteps);
1185 calcBoundingBox(gLifts);
1186 calcBoundingBox(gBlockMon);
1188 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);
1190 if (mapX0 > 0) then mapX0 := 0;
1191 if (mapY0 > 0) then mapY0 := 0;
1193 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1194 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1196 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1198 addPanelsToGrid(gWalls);
1199 addPanelsToGrid(gRenderBackgrounds);
1200 addPanelsToGrid(gRenderForegrounds);
1201 addPanelsToGrid(gWater);
1202 addPanelsToGrid(gAcid1);
1203 addPanelsToGrid(gAcid2);
1204 addPanelsToGrid(gSteps);
1205 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1206 addPanelsToGrid(gBlockMon);
1208 mapGrid.dumpStats();
1210 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1211 end;
1214 function g_Map_Load(Res: String): Boolean;
1215 const
1216 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1217 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1218 var
1219 WAD: TWADFile;
1220 MapReader: TMapReader_1;
1221 Header: TMapHeaderRec_1;
1222 _textures: TTexturesRec1Array;
1223 _texnummap: array of Integer; // `_textures` -> `Textures`
1224 panels: TPanelsRec1Array;
1225 items: TItemsRec1Array;
1226 monsters: TMonsterRec1Array;
1227 areas: TAreasRec1Array;
1228 triggers: TTriggersRec1Array;
1229 a, b, c, k: Integer;
1230 PanelID: DWORD;
1231 AddTextures: TAddTextureArray;
1232 texture: TTextureRec_1;
1233 TriggersTable: Array of record
1234 TexturePanel: Integer;
1235 LiftPanel: Integer;
1236 DoorPanel: Integer;
1237 ShotPanel: Integer;
1238 end;
1239 FileName, mapResName, s, TexName: String;
1240 Data: Pointer;
1241 Len: Integer;
1242 ok, isAnim, trigRef: Boolean;
1243 CurTex, ntn: Integer;
1245 begin
1246 mapGrid.Free();
1247 mapGrid := nil;
1248 //mapTree.Free();
1249 //mapTree := nil;
1251 Result := False;
1252 gMapInfo.Map := Res;
1253 TriggersTable := nil;
1254 FillChar(texture, SizeOf(texture), 0);
1256 sfsGCDisable(); // temporary disable removing of temporary volumes
1257 try
1258 // Çàãðóçêà WAD:
1259 FileName := g_ExtractWadName(Res);
1260 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1261 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1263 WAD := TWADFile.Create();
1264 if not WAD.ReadFile(FileName) then
1265 begin
1266 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1267 WAD.Free();
1268 Exit;
1269 end;
1270 //k8: why loader ignores path here?
1271 mapResName := g_ExtractFileName(Res);
1272 if not WAD.GetMapResource(mapResName, Data, Len) then
1273 begin
1274 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1275 WAD.Free();
1276 Exit;
1277 end;
1279 WAD.Free();
1281 // Çàãðóçêà êàðòû:
1282 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1283 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1284 MapReader := TMapReader_1.Create();
1286 if not MapReader.LoadMap(Data) then
1287 begin
1288 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1289 FreeMem(Data);
1290 MapReader.Free();
1291 Exit;
1292 end;
1294 FreeMem(Data);
1295 generateExternalResourcesList(MapReader);
1296 // Çàãðóçêà òåêñòóð:
1297 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1298 _textures := MapReader.GetTextures();
1299 _texnummap := nil;
1301 // Çàãðóçêà îïèñàíèÿ êàðòû:
1302 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1303 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1304 Header := MapReader.GetMapHeader();
1306 with gMapInfo do
1307 begin
1308 Name := Header.MapName;
1309 Description := Header.MapDescription;
1310 Author := Header.MapAuthor;
1311 MusicName := Header.MusicName;
1312 SkyName := Header.SkyName;
1313 Height := Header.Height;
1314 Width := Header.Width;
1315 end;
1317 // Äîáàâëåíèå òåêñòóð â Textures[]:
1318 if _textures <> nil then
1319 begin
1320 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1321 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1322 SetLength(_texnummap, length(_textures));
1324 for a := 0 to High(_textures) do
1325 begin
1326 SetLength(s, 64);
1327 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1328 for b := 1 to Length(s) do
1329 if s[b] = #0 then
1330 begin
1331 SetLength(s, b-1);
1332 Break;
1333 end;
1334 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1335 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1336 // Àíèìèðîâàííàÿ òåêñòóðà:
1337 if ByteBool(_textures[a].Anim) then
1338 begin
1339 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1340 if ntn < 0 then
1341 begin
1342 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1343 ntn := CreateNullTexture(_textures[a].Resource);
1344 end;
1345 end
1346 else // Îáû÷íàÿ òåêñòóðà:
1347 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1348 if ntn < 0 then
1349 begin
1350 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1351 ntn := CreateNullTexture(_textures[a].Resource);
1352 end;
1354 _texnummap[a] := ntn; // fix texture number
1355 g_Game_StepLoading();
1356 end;
1357 end;
1359 // Çàãðóçêà òðèããåðîâ:
1360 gTriggerClientID := 0;
1361 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1362 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1363 triggers := MapReader.GetTriggers();
1365 // Çàãðóçêà ïàíåëåé:
1366 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1367 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1368 panels := MapReader.GetPanels();
1370 // check texture numbers for panels
1371 for a := 0 to High(panels) do
1372 begin
1373 if panels[a].TextureNum > High(_textures) then
1374 begin
1375 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1376 result := false;
1377 exit;
1378 end;
1379 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1380 end;
1382 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1383 if triggers <> nil then
1384 begin
1385 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1386 SetLength(TriggersTable, Length(triggers));
1387 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1389 for a := 0 to High(TriggersTable) do
1390 begin
1391 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1392 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1393 // Ëèôòû:
1394 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1395 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1396 else
1397 TriggersTable[a].LiftPanel := -1;
1398 // Äâåðè:
1399 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1400 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1401 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1402 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1403 else
1404 TriggersTable[a].DoorPanel := -1;
1405 // Òóðåëü:
1406 if triggers[a].TriggerType = TRIGGER_SHOT then
1407 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1408 else
1409 TriggersTable[a].ShotPanel := -1;
1411 g_Game_StepLoading();
1412 end;
1413 end;
1415 // Ñîçäàåì ïàíåëè:
1416 if panels <> nil then
1417 begin
1418 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1419 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1421 for a := 0 to High(panels) do
1422 begin
1423 SetLength(AddTextures, 0);
1424 trigRef := False;
1425 CurTex := -1;
1426 if _textures <> nil then
1427 begin
1428 texture := _textures[panels[a].TextureNum];
1429 ok := True;
1430 end
1431 else
1432 ok := False;
1434 if ok then
1435 begin
1436 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1437 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1438 ok := False;
1439 if (TriggersTable <> nil) and (_textures <> nil) then
1440 for b := 0 to High(TriggersTable) do
1441 if (TriggersTable[b].TexturePanel = a)
1442 or (TriggersTable[b].ShotPanel = a) then
1443 begin
1444 trigRef := True;
1445 ok := True;
1446 Break;
1447 end;
1448 end;
1450 if ok then
1451 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1452 SetLength(s, 64);
1453 CopyMemory(@s[1], @texture.Resource[0], 64);
1454 // Èçìåðÿåì äëèíó:
1455 Len := Length(s);
1456 for c := Len downto 1 do
1457 if s[c] <> #0 then
1458 begin
1459 Len := c;
1460 Break;
1461 end;
1462 SetLength(s, Len);
1464 // Ñïåö-òåêñòóðû çàïðåùåíû:
1465 if g_Map_IsSpecialTexture(s) then
1466 ok := False
1467 else
1468 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1469 ok := g_Texture_NumNameFindStart(s);
1471 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1472 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1473 if ok then
1474 begin
1475 k := NNF_NAME_BEFORE;
1476 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1477 while ok or (k = NNF_NAME_BEFORE) or
1478 (k = NNF_NAME_EQUALS) do
1479 begin
1480 k := g_Texture_NumNameFindNext(TexName);
1482 if (k = NNF_NAME_BEFORE) or
1483 (k = NNF_NAME_AFTER) then
1484 begin
1485 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1486 if ByteBool(texture.Anim) then
1487 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1488 isAnim := True;
1489 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1490 if not ok then
1491 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1492 isAnim := False;
1493 ok := CreateTexture(TexName, FileName, False) >= 0;
1494 end;
1495 end
1496 else
1497 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1498 isAnim := False;
1499 ok := CreateTexture(TexName, FileName, False) >= 0;
1500 if not ok then
1501 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1502 isAnim := True;
1503 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1504 end;
1505 end;
1507 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1508 if ok then
1509 begin
1510 for c := 0 to High(Textures) do
1511 if Textures[c].TextureName = TexName then
1512 begin
1513 SetLength(AddTextures, Length(AddTextures)+1);
1514 AddTextures[High(AddTextures)].Texture := c;
1515 AddTextures[High(AddTextures)].Anim := isAnim;
1516 Break;
1517 end;
1518 end;
1519 end
1520 else
1521 if k = NNF_NAME_EQUALS then
1522 begin
1523 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1524 SetLength(AddTextures, Length(AddTextures)+1);
1525 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1526 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1527 CurTex := High(AddTextures);
1528 ok := True;
1529 end
1530 else // NNF_NO_NAME
1531 ok := False;
1532 end; // while ok...
1534 ok := True;
1535 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1536 end; // if ok - ññûëàþòñÿ òðèããåðû
1538 if not ok then
1539 begin
1540 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1541 SetLength(AddTextures, 1);
1542 AddTextures[0].Texture := panels[a].TextureNum;
1543 AddTextures[0].Anim := ByteBool(texture.Anim);
1544 CurTex := 0;
1545 end;
1547 //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);
1549 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1550 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1552 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1553 if TriggersTable <> nil then
1554 for b := 0 to High(TriggersTable) do
1555 begin
1556 // Òðèããåð äâåðè/ëèôòà:
1557 if (TriggersTable[b].LiftPanel = a) or
1558 (TriggersTable[b].DoorPanel = a) then
1559 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1560 // Òðèããåð ñìåíû òåêñòóðû:
1561 if TriggersTable[b].TexturePanel = a then
1562 triggers[b].TexturePanel := PanelID;
1563 // Òðèããåð "Òóðåëü":
1564 if TriggersTable[b].ShotPanel = a then
1565 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1566 end;
1568 g_Game_StepLoading();
1569 end;
1570 end;
1572 // create map grid, init other grids (for monsters, for example)
1573 e_WriteLog('Creating map grid', MSG_NOTIFY);
1574 mapCreateGrid();
1576 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1577 if (triggers <> nil) and not gLoadGameMode then
1578 begin
1579 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1580 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1581 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1582 for a := 0 to High(triggers) do
1583 begin
1584 if triggers[a].TexturePanel <> -1 then
1585 b := panels[TriggersTable[a].TexturePanel].PanelType
1586 else
1587 b := 0;
1588 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1589 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1590 c := panels[TriggersTable[a].ShotPanel].PanelType
1591 else
1592 c := 0;
1593 CreateTrigger(triggers[a], b, c);
1594 end;
1595 end;
1597 // Çàãðóçêà ïðåäìåòîâ:
1598 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1599 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1600 items := MapReader.GetItems();
1602 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1603 if (items <> nil) and not gLoadGameMode then
1604 begin
1605 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1606 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1607 for a := 0 to High(items) do
1608 CreateItem(Items[a]);
1609 end;
1611 // Çàãðóçêà îáëàñòåé:
1612 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1613 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1614 areas := MapReader.GetAreas();
1616 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1617 if areas <> nil then
1618 begin
1619 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1620 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1621 for a := 0 to High(areas) do
1622 CreateArea(areas[a]);
1623 end;
1625 // Çàãðóçêà ìîíñòðîâ:
1626 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1627 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1628 monsters := MapReader.GetMonsters();
1630 gTotalMonsters := 0;
1632 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1633 if (monsters <> nil) and not gLoadGameMode then
1634 begin
1635 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1636 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1637 for a := 0 to High(monsters) do
1638 CreateMonster(monsters[a]);
1639 end;
1641 MapReader.Free();
1643 // Çàãðóçêà íåáà:
1644 if gMapInfo.SkyName <> '' then
1645 begin
1646 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1647 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1648 FileName := g_ExtractWadName(gMapInfo.SkyName);
1650 if FileName <> '' then
1651 FileName := GameDir+'/wads/'+FileName
1652 else
1653 begin
1654 FileName := g_ExtractWadName(Res);
1655 end;
1657 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1658 if g_Texture_CreateWAD(BackID, s) then
1659 begin
1660 g_Game_SetupScreenSize();
1661 end
1662 else
1663 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1664 end;
1666 // Çàãðóçêà ìóçûêè:
1667 ok := False;
1668 if gMapInfo.MusicName <> '' then
1669 begin
1670 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1671 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1672 FileName := g_ExtractWadName(gMapInfo.MusicName);
1674 if FileName <> '' then
1675 FileName := GameDir+'/wads/'+FileName
1676 else
1677 begin
1678 FileName := g_ExtractWadName(Res);
1679 end;
1681 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1682 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1683 ok := True
1684 else
1685 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1686 end;
1688 // Îñòàëüíûå óñòàíâêè:
1689 CreateDoorMap();
1690 CreateLiftMap();
1692 g_Items_Init();
1693 g_Weapon_Init();
1694 g_Monsters_Init();
1696 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1697 if not gLoadGameMode then
1698 g_GFX_Init();
1700 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1701 _textures := nil;
1702 panels := nil;
1703 items := nil;
1704 areas := nil;
1705 triggers := nil;
1706 TriggersTable := nil;
1707 AddTextures := nil;
1709 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1710 if ok and (not gLoadGameMode) then
1711 begin
1712 gMusic.SetByName(gMapInfo.MusicName);
1713 gMusic.Play();
1714 end
1715 else
1716 gMusic.SetByName('');
1717 finally
1718 sfsGCEnable(); // enable releasing unused volumes
1719 end;
1721 e_WriteLog('Done loading map.', MSG_NOTIFY);
1722 Result := True;
1723 end;
1725 function g_Map_GetMapInfo(Res: String): TMapInfo;
1726 var
1727 WAD: TWADFile;
1728 MapReader: TMapReader_1;
1729 Header: TMapHeaderRec_1;
1730 FileName: String;
1731 Data: Pointer;
1732 Len: Integer;
1733 begin
1734 FillChar(Result, SizeOf(Result), 0);
1735 FileName := g_ExtractWadName(Res);
1737 WAD := TWADFile.Create();
1738 if not WAD.ReadFile(FileName) then
1739 begin
1740 WAD.Free();
1741 Exit;
1742 end;
1744 //k8: it ignores path again
1745 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1746 begin
1747 WAD.Free();
1748 Exit;
1749 end;
1751 WAD.Free();
1753 MapReader := TMapReader_1.Create();
1755 if not MapReader.LoadMap(Data) then
1756 begin
1757 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1758 ZeroMemory(@Header, SizeOf(Header));
1759 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1760 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1761 end
1762 else
1763 begin
1764 Header := MapReader.GetMapHeader();
1765 Result.Name := Header.MapName;
1766 Result.Description := Header.MapDescription;
1767 end;
1769 FreeMem(Data);
1770 MapReader.Free();
1772 Result.Map := Res;
1773 Result.Author := Header.MapAuthor;
1774 Result.Height := Header.Height;
1775 Result.Width := Header.Width;
1776 end;
1778 function g_Map_GetMapsList(WADName: string): SArray;
1779 var
1780 WAD: TWADFile;
1781 a: Integer;
1782 ResList: SArray;
1783 begin
1784 Result := nil;
1785 WAD := TWADFile.Create();
1786 if not WAD.ReadFile(WADName) then
1787 begin
1788 WAD.Free();
1789 Exit;
1790 end;
1791 ResList := WAD.GetMapResources();
1792 if ResList <> nil then
1793 begin
1794 for a := 0 to High(ResList) do
1795 begin
1796 SetLength(Result, Length(Result)+1);
1797 Result[High(Result)] := ResList[a];
1798 end;
1799 end;
1800 WAD.Free();
1801 end;
1803 function g_Map_Exist(Res: string): Boolean;
1804 var
1805 WAD: TWADFile;
1806 FileName, mnn: string;
1807 ResList: SArray;
1808 a: Integer;
1809 begin
1810 Result := False;
1812 FileName := addWadExtension(g_ExtractWadName(Res));
1814 WAD := TWADFile.Create;
1815 if not WAD.ReadFile(FileName) then
1816 begin
1817 WAD.Free();
1818 Exit;
1819 end;
1821 ResList := WAD.GetMapResources();
1822 WAD.Free();
1824 mnn := g_ExtractFileName(Res);
1825 if ResList <> nil then
1826 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1827 begin
1828 Result := True;
1829 Exit;
1830 end;
1831 end;
1833 procedure g_Map_Free();
1834 var
1835 a: Integer;
1837 procedure FreePanelArray(var panels: TPanelArray);
1838 var
1839 i: Integer;
1841 begin
1842 if panels <> nil then
1843 begin
1844 for i := 0 to High(panels) do
1845 panels[i].Free();
1846 panels := nil;
1847 end;
1848 end;
1850 begin
1851 g_GFX_Free();
1852 g_Weapon_Free();
1853 g_Items_Free();
1854 g_Triggers_Free();
1855 g_Monsters_Free();
1857 RespawnPoints := nil;
1858 if FlagPoints[FLAG_RED] <> nil then
1859 begin
1860 Dispose(FlagPoints[FLAG_RED]);
1861 FlagPoints[FLAG_RED] := nil;
1862 end;
1863 if FlagPoints[FLAG_BLUE] <> nil then
1864 begin
1865 Dispose(FlagPoints[FLAG_BLUE]);
1866 FlagPoints[FLAG_BLUE] := nil;
1867 end;
1868 //DOMFlagPoints := nil;
1870 //gDOMFlags := nil;
1872 if Textures <> nil then
1873 begin
1874 for a := 0 to High(Textures) do
1875 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1876 if Textures[a].Anim then
1877 g_Frames_DeleteByID(Textures[a].FramesID)
1878 else
1879 if Textures[a].TextureID <> TEXTURE_NONE then
1880 e_DeleteTexture(Textures[a].TextureID);
1882 Textures := nil;
1883 end;
1885 FreePanelArray(gWalls);
1886 FreePanelArray(gRenderBackgrounds);
1887 FreePanelArray(gRenderForegrounds);
1888 FreePanelArray(gWater);
1889 FreePanelArray(gAcid1);
1890 FreePanelArray(gAcid2);
1891 FreePanelArray(gSteps);
1892 FreePanelArray(gLifts);
1893 FreePanelArray(gBlockMon);
1895 if BackID <> DWORD(-1) then
1896 begin
1897 gBackSize.X := 0;
1898 gBackSize.Y := 0;
1899 e_DeleteTexture(BackID);
1900 BackID := DWORD(-1);
1901 end;
1903 g_Game_StopAllSounds(False);
1904 gMusic.FreeSound();
1905 g_Sound_Delete(gMapInfo.MusicName);
1907 gMapInfo.Name := '';
1908 gMapInfo.Description := '';
1909 gMapInfo.MusicName := '';
1910 gMapInfo.Height := 0;
1911 gMapInfo.Width := 0;
1913 gDoorMap := nil;
1914 gLiftMap := nil;
1916 PanelByID := nil;
1917 end;
1919 procedure g_Map_Update();
1920 var
1921 a, d, j: Integer;
1922 m: Word;
1923 s: String;
1925 procedure UpdatePanelArray(var panels: TPanelArray);
1926 var
1927 i: Integer;
1929 begin
1930 if panels <> nil then
1931 for i := 0 to High(panels) do
1932 panels[i].Update();
1933 end;
1935 begin
1936 UpdatePanelArray(gWalls);
1937 UpdatePanelArray(gRenderBackgrounds);
1938 UpdatePanelArray(gRenderForegrounds);
1939 UpdatePanelArray(gWater);
1940 UpdatePanelArray(gAcid1);
1941 UpdatePanelArray(gAcid2);
1942 UpdatePanelArray(gSteps);
1944 if gGameSettings.GameMode = GM_CTF then
1945 begin
1946 for a := FLAG_RED to FLAG_BLUE do
1947 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1948 with gFlags[a] do
1949 begin
1950 if gFlags[a].Animation <> nil then
1951 gFlags[a].Animation.Update();
1953 m := g_Obj_Move(@Obj, True, True);
1955 if gTime mod (GAME_TICK*2) <> 0 then
1956 Continue;
1958 // Ñîïðîòèâëåíèå âîçäóõà:
1959 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1961 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1962 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1963 begin
1964 g_Map_ResetFlag(a);
1965 gFlags[a].CaptureTime := 0;
1966 if a = FLAG_RED then
1967 s := _lc[I_PLAYER_FLAG_RED]
1968 else
1969 s := _lc[I_PLAYER_FLAG_BLUE];
1970 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1972 if g_Game_IsNet then
1973 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1974 Continue;
1975 end;
1977 if Count > 0 then
1978 Count := Count - 1;
1980 // Èãðîê áåðåò ôëàã:
1981 if gPlayers <> nil then
1982 begin
1983 j := Random(Length(gPlayers)) - 1;
1985 for d := 0 to High(gPlayers) do
1986 begin
1987 Inc(j);
1988 if j > High(gPlayers) then
1989 j := 0;
1991 if gPlayers[j] <> nil then
1992 if gPlayers[j].Live and
1993 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1994 begin
1995 if gPlayers[j].GetFlag(a) then
1996 Break;
1997 end;
1998 end;
1999 end;
2000 end;
2001 end;
2002 end;
2005 // old algo
2006 procedure g_Map_DrawPanels (PanelType: Word);
2008 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2009 var
2010 idx: Integer;
2011 begin
2012 if (panels <> nil) then
2013 begin
2014 // alas, no visible set
2015 for idx := 0 to High(panels) do
2016 begin
2017 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2018 end;
2019 end;
2020 end;
2022 begin
2023 case PanelType of
2024 PANEL_WALL: DrawPanels(gWalls);
2025 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2026 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2027 PANEL_FORE: DrawPanels(gRenderForegrounds);
2028 PANEL_WATER: DrawPanels(gWater);
2029 PANEL_ACID1: DrawPanels(gAcid1);
2030 PANEL_ACID2: DrawPanels(gAcid2);
2031 PANEL_STEP: DrawPanels(gSteps);
2032 end;
2033 end;
2036 // new algo
2037 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2039 function checker (pan: TPanel; tag: Integer): Boolean;
2040 begin
2041 result := false; // don't stop, ever
2042 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2043 gDrawPanelList.insert(pan);
2044 end;
2046 begin
2047 dplClear();
2048 //tagmask := panelTypeToTag(PanelType);
2050 {if gdbg_map_use_tree_draw then
2051 begin
2052 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2053 end
2054 else}
2055 begin
2056 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2057 end;
2058 // list will be rendered in `g_game.DrawPlayer()`
2059 end;
2062 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2064 function checker (pan: TPanel; tag: Integer): Boolean;
2065 begin
2066 result := false; // don't stop, ever
2067 pan.DrawShadowVolume(lightX, lightY, radius);
2068 end;
2070 begin
2071 {if gdbg_map_use_tree_draw then
2072 begin
2073 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2074 end
2075 else}
2076 begin
2077 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2078 end;
2079 end;
2082 procedure g_Map_DrawBack(dx, dy: Integer);
2083 begin
2084 if gDrawBackGround and (BackID <> DWORD(-1)) then
2085 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2086 else
2087 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2088 end;
2090 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2091 PanelType: Word; b1x3: Boolean=false): Boolean;
2092 var
2093 a, h: Integer;
2094 begin
2095 Result := False;
2097 if WordBool(PanelType and PANEL_WALL) then
2098 if gWalls <> nil then
2099 begin
2100 h := High(gWalls);
2102 for a := 0 to h do
2103 if gWalls[a].Enabled and
2104 g_Collide(X, Y, Width, Height,
2105 gWalls[a].X, gWalls[a].Y,
2106 gWalls[a].Width, gWalls[a].Height) then
2107 begin
2108 Result := True;
2109 Exit;
2110 end;
2111 end;
2113 if WordBool(PanelType and PANEL_WATER) then
2114 if gWater <> nil then
2115 begin
2116 h := High(gWater);
2118 for a := 0 to h do
2119 if g_Collide(X, Y, Width, Height,
2120 gWater[a].X, gWater[a].Y,
2121 gWater[a].Width, gWater[a].Height) then
2122 begin
2123 Result := True;
2124 Exit;
2125 end;
2126 end;
2128 if WordBool(PanelType and PANEL_ACID1) then
2129 if gAcid1 <> nil then
2130 begin
2131 h := High(gAcid1);
2133 for a := 0 to h do
2134 if g_Collide(X, Y, Width, Height,
2135 gAcid1[a].X, gAcid1[a].Y,
2136 gAcid1[a].Width, gAcid1[a].Height) then
2137 begin
2138 Result := True;
2139 Exit;
2140 end;
2141 end;
2143 if WordBool(PanelType and PANEL_ACID2) then
2144 if gAcid2 <> nil then
2145 begin
2146 h := High(gAcid2);
2148 for a := 0 to h do
2149 if g_Collide(X, Y, Width, Height,
2150 gAcid2[a].X, gAcid2[a].Y,
2151 gAcid2[a].Width, gAcid2[a].Height) then
2152 begin
2153 Result := True;
2154 Exit;
2155 end;
2156 end;
2158 if WordBool(PanelType and PANEL_STEP) then
2159 if gSteps <> nil then
2160 begin
2161 h := High(gSteps);
2163 for a := 0 to h do
2164 if g_Collide(X, Y, Width, Height,
2165 gSteps[a].X, gSteps[a].Y,
2166 gSteps[a].Width, gSteps[a].Height) then
2167 begin
2168 Result := True;
2169 Exit;
2170 end;
2171 end;
2173 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2174 if gLifts <> nil then
2175 begin
2176 h := High(gLifts);
2178 for a := 0 to h do
2179 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2180 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2181 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2182 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2183 g_Collide(X, Y, Width, Height,
2184 gLifts[a].X, gLifts[a].Y,
2185 gLifts[a].Width, gLifts[a].Height) then
2186 begin
2187 Result := True;
2188 Exit;
2189 end;
2190 end;
2192 if WordBool(PanelType and PANEL_BLOCKMON) then
2193 if gBlockMon <> nil then
2194 begin
2195 h := High(gBlockMon);
2197 for a := 0 to h do
2198 if ( (not b1x3) or
2199 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2200 g_Collide(X, Y, Width, Height,
2201 gBlockMon[a].X, gBlockMon[a].Y,
2202 gBlockMon[a].Width, gBlockMon[a].Height) then
2203 begin
2204 Result := True;
2205 Exit;
2206 end;
2207 end;
2208 end;
2210 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2211 var
2212 texid: DWORD;
2214 function checkPanels (constref panels: TPanelArray): Boolean;
2215 var
2216 a: Integer;
2217 begin
2218 result := false;
2219 if panels = nil then exit;
2220 for a := 0 to High(panels) do
2221 begin
2222 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2223 begin
2224 result := true;
2225 texid := panels[a].GetTextureID();
2226 exit;
2227 end;
2228 end;
2229 end;
2231 begin
2232 texid := TEXTURE_NONE;
2233 result := texid;
2234 if not checkPanels(gWater) then
2235 if not checkPanels(gAcid1) then
2236 if not checkPanels(gAcid2) then exit;
2237 result := texid;
2238 end;
2241 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2242 const
2243 SlowMask = GridTagLift or GridTagBlockMon;
2244 function checker (pan: TPanel; tag: Integer): Boolean;
2245 begin
2247 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2248 begin
2249 result := pan.Enabled;
2250 exit;
2251 end;
2254 if ((tag and GridTagLift) <> 0) then
2255 begin
2256 result :=
2257 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2258 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2259 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2260 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2261 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2262 exit;
2263 end;
2265 if ((tag and GridTagBlockMon) <> 0) then
2266 begin
2267 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2268 exit;
2269 end;
2271 // other shit
2272 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2273 result := true; // i found her!
2274 end;
2276 var
2277 tagmask: Integer = 0;
2278 begin
2279 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2280 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2281 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2282 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2283 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2284 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2285 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2287 if (tagmask = 0) then begin result := false; exit; end; // just in case
2289 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2290 if gdbg_map_use_accel_coldet then
2291 begin
2292 {if gdbg_map_use_tree_coldet then
2293 begin
2294 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2295 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2296 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2297 begin
2298 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2299 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2300 end;
2301 end
2302 else}
2303 begin
2304 if (Width = 1) and (Height = 1) then
2305 begin
2306 if ((tagmask and SlowMask) <> 0) then
2307 begin
2308 // slow
2309 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2310 end
2311 else
2312 begin
2313 // fast
2314 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2315 end;
2316 end
2317 else
2318 begin
2319 if ((tagmask and SlowMask) <> 0) then
2320 begin
2321 // slow
2322 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2323 end
2324 else
2325 begin
2326 // fast
2327 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2328 end;
2329 end;
2330 end;
2331 end
2332 else
2333 begin
2334 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2335 end;
2336 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2337 end;
2340 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2341 var
2342 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2343 texid: DWORD;
2345 // slightly different from the old code, but meh...
2346 function checker (pan: TPanel; tag: Integer): Boolean;
2347 begin
2348 result := false; // don't stop, ever
2349 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2350 // check priorities
2351 case cctype of
2352 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2353 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2354 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2355 end;
2356 // collision?
2357 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2358 // yeah
2359 texid := pan.GetTextureID();
2360 // water? water has the highest priority, so stop right here
2361 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2362 // acid2?
2363 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2364 // acid1?
2365 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2366 end;
2368 begin
2369 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2370 if gdbg_map_use_accel_coldet then
2371 begin
2372 texid := TEXTURE_NONE;
2373 {if gdbg_map_use_tree_coldet then
2374 begin
2375 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2376 end
2377 else}
2378 begin
2379 if (Width = 1) and (Height = 1) then
2380 begin
2381 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2382 end
2383 else
2384 begin
2385 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2386 end;
2387 end;
2388 result := texid;
2389 end
2390 else
2391 begin
2392 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2393 end;
2394 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2395 end;
2398 procedure g_Map_EnableWall(ID: DWORD);
2399 var
2400 pan: TPanel;
2401 begin
2402 pan := gWalls[ID];
2403 pan.Enabled := True;
2404 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2406 mapGrid.proxyEnabled[pan.proxyId] := true;
2407 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2408 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2410 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2412 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2413 //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);
2414 {$ENDIF}
2415 end;
2417 procedure g_Map_DisableWall(ID: DWORD);
2418 var
2419 pan: TPanel;
2420 begin
2421 pan := gWalls[ID];
2422 pan.Enabled := False;
2423 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2425 mapGrid.proxyEnabled[pan.proxyId] := false;
2426 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2428 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2430 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2431 //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);
2432 {$ENDIF}
2433 end;
2435 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2436 var
2437 tp: TPanel;
2438 begin
2439 case PanelType of
2440 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2441 tp := gWalls[ID];
2442 PANEL_FORE:
2443 tp := gRenderForegrounds[ID];
2444 PANEL_BACK:
2445 tp := gRenderBackgrounds[ID];
2446 PANEL_WATER:
2447 tp := gWater[ID];
2448 PANEL_ACID1:
2449 tp := gAcid1[ID];
2450 PANEL_ACID2:
2451 tp := gAcid2[ID];
2452 PANEL_STEP:
2453 tp := gSteps[ID];
2454 else
2455 Exit;
2456 end;
2458 tp.NextTexture(AnimLoop);
2459 if g_Game_IsServer and g_Game_IsNet then
2460 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2461 end;
2463 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2464 begin
2465 if gLifts[ID].LiftType = t then
2466 Exit;
2468 with gLifts[ID] do
2469 begin
2470 LiftType := t;
2472 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2473 //TODO: make separate lift tags, and change tag here
2475 if LiftType = 0 then
2476 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2477 else if LiftType = 1 then
2478 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2479 else if LiftType = 2 then
2480 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2481 else if LiftType = 3 then
2482 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2484 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2485 end;
2486 end;
2488 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2489 var
2490 a: Integer;
2491 PointsArray: Array of TRespawnPoint;
2492 begin
2493 Result := False;
2494 SetLength(PointsArray, 0);
2496 if RespawnPoints = nil then
2497 Exit;
2499 for a := 0 to High(RespawnPoints) do
2500 if RespawnPoints[a].PointType = PointType then
2501 begin
2502 SetLength(PointsArray, Length(PointsArray)+1);
2503 PointsArray[High(PointsArray)] := RespawnPoints[a];
2504 end;
2506 if PointsArray = nil then
2507 Exit;
2509 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2510 Result := True;
2511 end;
2513 function g_Map_GetPointCount(PointType: Byte): Word;
2514 var
2515 a: Integer;
2516 begin
2517 Result := 0;
2519 if RespawnPoints = nil then
2520 Exit;
2522 for a := 0 to High(RespawnPoints) do
2523 if RespawnPoints[a].PointType = PointType then
2524 Result := Result + 1;
2525 end;
2527 function g_Map_HaveFlagPoints(): Boolean;
2528 begin
2529 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2530 end;
2532 procedure g_Map_ResetFlag(Flag: Byte);
2533 begin
2534 with gFlags[Flag] do
2535 begin
2536 Obj.X := -1000;
2537 Obj.Y := -1000;
2538 Obj.Vel.X := 0;
2539 Obj.Vel.Y := 0;
2540 Direction := D_LEFT;
2541 State := FLAG_STATE_NONE;
2542 if FlagPoints[Flag] <> nil then
2543 begin
2544 Obj.X := FlagPoints[Flag]^.X;
2545 Obj.Y := FlagPoints[Flag]^.Y;
2546 Direction := FlagPoints[Flag]^.Direction;
2547 State := FLAG_STATE_NORMAL;
2548 end;
2549 Count := -1;
2550 end;
2551 end;
2553 procedure g_Map_DrawFlags();
2554 var
2555 i, dx: Integer;
2556 Mirror: TMirrorType;
2557 begin
2558 if gGameSettings.GameMode <> GM_CTF then
2559 Exit;
2561 for i := FLAG_RED to FLAG_BLUE do
2562 with gFlags[i] do
2563 if State <> FLAG_STATE_CAPTURED then
2564 begin
2565 if State = FLAG_STATE_NONE then
2566 continue;
2568 if Direction = D_LEFT then
2569 begin
2570 Mirror := M_HORIZONTAL;
2571 dx := -1;
2572 end
2573 else
2574 begin
2575 Mirror := M_NONE;
2576 dx := 1;
2577 end;
2579 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2581 if g_debug_Frames then
2582 begin
2583 e_DrawQuad(Obj.X+Obj.Rect.X,
2584 Obj.Y+Obj.Rect.Y,
2585 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2586 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2587 0, 255, 0);
2588 end;
2589 end;
2590 end;
2592 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2593 var
2594 dw: DWORD;
2595 b: Byte;
2596 str: String;
2597 boo: Boolean;
2599 procedure SavePanelArray(var panels: TPanelArray);
2600 var
2601 PAMem: TBinMemoryWriter;
2602 i: Integer;
2603 begin
2604 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2605 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2607 i := 0;
2608 while i < Length(panels) do
2609 begin
2610 if panels[i].SaveIt then
2611 begin
2612 // ID ïàíåëè:
2613 PAMem.WriteInt(i);
2614 // Ñîõðàíÿåì ïàíåëü:
2615 panels[i].SaveState(PAMem);
2616 end;
2617 Inc(i);
2618 end;
2620 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2621 PAMem.SaveToMemory(Mem);
2622 PAMem.Free();
2623 end;
2625 procedure SaveFlag(flag: PFlag);
2626 begin
2627 // Ñèãíàòóðà ôëàãà:
2628 dw := FLAG_SIGNATURE; // 'FLAG'
2629 Mem.WriteDWORD(dw);
2630 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2631 Mem.WriteByte(flag^.RespawnType);
2632 // Ñîñòîÿíèå ôëàãà:
2633 Mem.WriteByte(flag^.State);
2634 // Íàïðàâëåíèå ôëàãà:
2635 if flag^.Direction = D_LEFT then
2636 b := 1
2637 else // D_RIGHT
2638 b := 2;
2639 Mem.WriteByte(b);
2640 // Îáúåêò ôëàãà:
2641 Obj_SaveState(@flag^.Obj, Mem);
2642 end;
2644 begin
2645 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2647 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2648 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2649 SavePanelArray(gWalls);
2650 // Ñîõðàíÿåì ïàíåëè ôîíà:
2651 SavePanelArray(gRenderBackgrounds);
2652 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2653 SavePanelArray(gRenderForegrounds);
2654 // Ñîõðàíÿåì ïàíåëè âîäû:
2655 SavePanelArray(gWater);
2656 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2657 SavePanelArray(gAcid1);
2658 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2659 SavePanelArray(gAcid2);
2660 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2661 SavePanelArray(gSteps);
2662 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2663 SavePanelArray(gLifts);
2664 ///// /////
2666 ///// Ñîõðàíÿåì ìóçûêó: /////
2667 // Ñèãíàòóðà ìóçûêè:
2668 dw := MUSIC_SIGNATURE; // 'MUSI'
2669 Mem.WriteDWORD(dw);
2670 // Íàçâàíèå ìóçûêè:
2671 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2672 if gMusic.NoMusic then
2673 str := ''
2674 else
2675 str := gMusic.Name;
2676 Mem.WriteString(str, 64);
2677 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2678 dw := gMusic.GetPosition();
2679 Mem.WriteDWORD(dw);
2680 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2681 boo := gMusic.SpecPause;
2682 Mem.WriteBoolean(boo);
2683 ///// /////
2685 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2686 Mem.WriteInt(gTotalMonsters);
2687 ///// /////
2689 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2690 if gGameSettings.GameMode = GM_CTF then
2691 begin
2692 // Ôëàã Êðàñíîé êîìàíäû:
2693 SaveFlag(@gFlags[FLAG_RED]);
2694 // Ôëàã Ñèíåé êîìàíäû:
2695 SaveFlag(@gFlags[FLAG_BLUE]);
2696 end;
2697 ///// /////
2699 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2700 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2701 begin
2702 // Î÷êè Êðàñíîé êîìàíäû:
2703 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2704 // Î÷êè Ñèíåé êîìàíäû:
2705 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2706 end;
2707 ///// /////
2708 end;
2710 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2711 var
2712 dw: DWORD;
2713 b: Byte;
2714 str: String;
2715 boo: Boolean;
2717 procedure LoadPanelArray(var panels: TPanelArray);
2718 var
2719 PAMem: TBinMemoryReader;
2720 i, id: Integer;
2721 begin
2722 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2723 PAMem := TBinMemoryReader.Create();
2724 PAMem.LoadFromMemory(Mem);
2726 for i := 0 to Length(panels)-1 do
2727 if panels[i].SaveIt then
2728 begin
2729 // ID ïàíåëè:
2730 PAMem.ReadInt(id);
2731 if id <> i then
2732 begin
2733 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2734 end;
2735 // Çàãðóæàåì ïàíåëü:
2736 panels[i].LoadState(PAMem);
2737 end;
2739 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2740 PAMem.Free();
2741 end;
2743 procedure LoadFlag(flag: PFlag);
2744 begin
2745 // Ñèãíàòóðà ôëàãà:
2746 Mem.ReadDWORD(dw);
2747 if dw <> FLAG_SIGNATURE then // 'FLAG'
2748 begin
2749 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2750 end;
2751 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2752 Mem.ReadByte(flag^.RespawnType);
2753 // Ñîñòîÿíèå ôëàãà:
2754 Mem.ReadByte(flag^.State);
2755 // Íàïðàâëåíèå ôëàãà:
2756 Mem.ReadByte(b);
2757 if b = 1 then
2758 flag^.Direction := D_LEFT
2759 else // b = 2
2760 flag^.Direction := D_RIGHT;
2761 // Îáúåêò ôëàãà:
2762 Obj_LoadState(@flag^.Obj, Mem);
2763 end;
2765 begin
2766 if Mem = nil then
2767 Exit;
2769 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2770 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2771 LoadPanelArray(gWalls);
2772 // Çàãðóæàåì ïàíåëè ôîíà:
2773 LoadPanelArray(gRenderBackgrounds);
2774 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2775 LoadPanelArray(gRenderForegrounds);
2776 // Çàãðóæàåì ïàíåëè âîäû:
2777 LoadPanelArray(gWater);
2778 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2779 LoadPanelArray(gAcid1);
2780 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2781 LoadPanelArray(gAcid2);
2782 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2783 LoadPanelArray(gSteps);
2784 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2785 LoadPanelArray(gLifts);
2786 ///// /////
2788 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2789 g_GFX_Init();
2790 mapCreateGrid();
2792 ///// Çàãðóæàåì ìóçûêó: /////
2793 // Ñèãíàòóðà ìóçûêè:
2794 Mem.ReadDWORD(dw);
2795 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2796 begin
2797 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2798 end;
2799 // Íàçâàíèå ìóçûêè:
2800 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2801 Mem.ReadString(str);
2802 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2803 Mem.ReadDWORD(dw);
2804 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2805 Mem.ReadBoolean(boo);
2806 // Çàïóñêàåì ýòó ìóçûêó:
2807 gMusic.SetByName(str);
2808 gMusic.SpecPause := boo;
2809 gMusic.Play();
2810 gMusic.Pause(True);
2811 gMusic.SetPosition(dw);
2812 ///// /////
2814 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2815 Mem.ReadInt(gTotalMonsters);
2816 ///// /////
2818 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2819 if gGameSettings.GameMode = GM_CTF then
2820 begin
2821 // Ôëàã Êðàñíîé êîìàíäû:
2822 LoadFlag(@gFlags[FLAG_RED]);
2823 // Ôëàã Ñèíåé êîìàíäû:
2824 LoadFlag(@gFlags[FLAG_BLUE]);
2825 end;
2826 ///// /////
2828 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2829 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2830 begin
2831 // Î÷êè Êðàñíîé êîìàíäû:
2832 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2833 // Î÷êè Ñèíåé êîìàíäû:
2834 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2835 end;
2836 ///// /////
2837 end;
2839 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2840 var
2841 Arr: TPanelArray;
2842 begin
2843 Result := nil;
2844 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2845 Arr := PanelByID[PanelID].PWhere^;
2846 PanelArrayID := PanelByID[PanelID].PArrID;
2847 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2848 end;
2851 // trace liquid, stepping by `dx` and `dy`
2852 // return last seen liquid coords, and `false` if we're started outside of the liquid
2853 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2854 const
2855 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2856 begin
2857 topx := x;
2858 topy := y;
2859 // started outside of the liquid?
2860 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2861 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2862 result := true;
2863 while true do
2864 begin
2865 Inc(x, dx);
2866 Inc(y, dy);
2867 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2868 topx := x;
2869 topy := y;
2870 end;
2871 end;
2874 end.