DEADSOFTWARE

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