DEADSOFTWARE

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