DEADSOFTWARE

new tracer seems to work
[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; log: Boolean=false): 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; log: Boolean=false): Boolean;
283 var
284 ex, ey: Integer;
285 begin
286 mapGrid.dbgShowTraceLog := log;
287 result := (mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor)) <> nil);
288 mapGrid.dbgShowTraceLog := false;
289 if result then
290 begin
291 if (hitx <> nil) then hitx^ := ex;
292 if (hity <> nil) then hity^ := ey;
293 end
294 else
295 begin
296 if (hitx <> nil) then hitx^ := x1;
297 if (hity <> nil) then hity^ := y1;
298 end;
299 end;
302 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
304 function checker (pan: TPanel; tag: Integer): Boolean;
305 begin
307 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
308 begin
309 result := pan.Enabled; // stop if wall is enabled
310 exit;
311 end;
314 if ((tag and GridTagLift) <> 0) then
315 begin
316 // stop if the lift of the right type
317 result :=
318 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
319 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
320 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
321 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
322 exit;
323 end;
325 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
326 end;
328 var
329 tagmask: Integer = 0;
330 begin
331 result := false;
333 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
334 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
335 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
336 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
337 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
338 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
339 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
341 if (tagmask = 0) then exit;// just in case
342 if ((tagmask and GridTagLift) <> 0) then
343 begin
344 // slow
345 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
346 end
347 else
348 begin
349 // fast
350 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
351 end;
352 end;
355 function g_Map_IsSpecialTexture(Texture: String): Boolean;
356 begin
357 Result := (Texture = TEXTURE_NAME_WATER) or
358 (Texture = TEXTURE_NAME_ACID1) or
359 (Texture = TEXTURE_NAME_ACID2);
360 end;
362 procedure CreateDoorMap();
363 var
364 PanelArray: Array of record
365 X, Y: Integer;
366 Width, Height: Word;
367 Active: Boolean;
368 PanelID: DWORD;
369 end;
370 a, b, c, m, i, len: Integer;
371 ok: Boolean;
372 begin
373 if gWalls = nil then
374 Exit;
376 i := 0;
377 len := 128;
378 SetLength(PanelArray, len);
380 for a := 0 to High(gWalls) do
381 if gWalls[a].Door then
382 begin
383 PanelArray[i].X := gWalls[a].X;
384 PanelArray[i].Y := gWalls[a].Y;
385 PanelArray[i].Width := gWalls[a].Width;
386 PanelArray[i].Height := gWalls[a].Height;
387 PanelArray[i].Active := True;
388 PanelArray[i].PanelID := a;
390 i := i + 1;
391 if i = len then
392 begin
393 len := len + 128;
394 SetLength(PanelArray, len);
395 end;
396 end;
398 // Íåò äâåðåé:
399 if i = 0 then
400 begin
401 PanelArray := nil;
402 Exit;
403 end;
405 SetLength(gDoorMap, 0);
407 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
409 for a := 0 to i-1 do
410 if PanelArray[a].Active then
411 begin
412 PanelArray[a].Active := False;
413 m := Length(gDoorMap);
414 SetLength(gDoorMap, m+1);
415 SetLength(gDoorMap[m], 1);
416 gDoorMap[m, 0] := PanelArray[a].PanelID;
417 ok := True;
419 while ok do
420 begin
421 ok := False;
423 for b := 0 to i-1 do
424 if PanelArray[b].Active then
425 for c := 0 to High(gDoorMap[m]) do
426 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
427 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
428 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
429 PanelArray[b].Width, PanelArray[b].Height,
430 gWalls[gDoorMap[m, c]].X,
431 gWalls[gDoorMap[m, c]].Y,
432 gWalls[gDoorMap[m, c]].Width,
433 gWalls[gDoorMap[m, c]].Height) then
434 begin
435 PanelArray[b].Active := False;
436 SetLength(gDoorMap[m],
437 Length(gDoorMap[m])+1);
438 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
439 ok := True;
440 Break;
441 end;
442 end;
444 g_Game_StepLoading();
445 end;
447 PanelArray := nil;
448 end;
450 procedure CreateLiftMap();
451 var
452 PanelArray: Array of record
453 X, Y: Integer;
454 Width, Height: Word;
455 Active: Boolean;
456 end;
457 a, b, c, len, i, j: Integer;
458 ok: Boolean;
459 begin
460 if gLifts = nil then
461 Exit;
463 len := Length(gLifts);
464 SetLength(PanelArray, len);
466 for a := 0 to len-1 do
467 begin
468 PanelArray[a].X := gLifts[a].X;
469 PanelArray[a].Y := gLifts[a].Y;
470 PanelArray[a].Width := gLifts[a].Width;
471 PanelArray[a].Height := gLifts[a].Height;
472 PanelArray[a].Active := True;
473 end;
475 SetLength(gLiftMap, len);
476 i := 0;
478 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
480 for a := 0 to len-1 do
481 if PanelArray[a].Active then
482 begin
483 PanelArray[a].Active := False;
484 SetLength(gLiftMap[i], 32);
485 j := 0;
486 gLiftMap[i, j] := a;
487 ok := True;
489 while ok do
490 begin
491 ok := False;
492 for b := 0 to len-1 do
493 if PanelArray[b].Active then
494 for c := 0 to j do
495 if g_CollideAround(PanelArray[b].X,
496 PanelArray[b].Y,
497 PanelArray[b].Width,
498 PanelArray[b].Height,
499 PanelArray[gLiftMap[i, c]].X,
500 PanelArray[gLiftMap[i, c]].Y,
501 PanelArray[gLiftMap[i, c]].Width,
502 PanelArray[gLiftMap[i, c]].Height) then
503 begin
504 PanelArray[b].Active := False;
505 j := j+1;
506 if j > High(gLiftMap[i]) then
507 SetLength(gLiftMap[i],
508 Length(gLiftMap[i])+32);
510 gLiftMap[i, j] := b;
511 ok := True;
513 Break;
514 end;
515 end;
517 SetLength(gLiftMap[i], j+1);
518 i := i+1;
520 g_Game_StepLoading();
521 end;
523 SetLength(gLiftMap, i);
525 PanelArray := nil;
526 end;
528 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
529 CurTex: Integer; sav: Boolean): Integer;
530 var
531 len: Integer;
532 panels: ^TPanelArray;
533 begin
534 Result := -1;
536 case PanelRec.PanelType of
537 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
538 panels := @gWalls;
539 PANEL_BACK:
540 panels := @gRenderBackgrounds;
541 PANEL_FORE:
542 panels := @gRenderForegrounds;
543 PANEL_WATER:
544 panels := @gWater;
545 PANEL_ACID1:
546 panels := @gAcid1;
547 PANEL_ACID2:
548 panels := @gAcid2;
549 PANEL_STEP:
550 panels := @gSteps;
551 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
552 panels := @gLifts;
553 PANEL_BLOCKMON:
554 panels := @gBlockMon;
555 else
556 Exit;
557 end;
559 len := Length(panels^);
560 SetLength(panels^, len + 1);
562 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
563 panels^[len].arrIdx := len;
564 panels^[len].proxyId := -1;
565 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
566 if sav then
567 panels^[len].SaveIt := True;
569 Result := len;
571 len := Length(PanelByID);
572 SetLength(PanelByID, len + 1);
573 PanelByID[len].PWhere := panels;
574 PanelByID[len].PArrID := Result;
575 end;
577 function CreateNullTexture(RecName: String): Integer;
578 begin
579 SetLength(Textures, Length(Textures)+1);
580 result := High(Textures);
582 with Textures[High(Textures)] do
583 begin
584 TextureName := RecName;
585 Width := 1;
586 Height := 1;
587 Anim := False;
588 TextureID := TEXTURE_NONE;
589 end;
590 end;
592 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
593 var
594 WAD: TWADFile;
595 TextureData: Pointer;
596 WADName, txname: String;
597 a, ResLength: Integer;
598 begin
599 Result := -1;
601 if Textures <> nil then
602 for a := 0 to High(Textures) do
603 if Textures[a].TextureName = RecName then
604 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
605 Result := a;
606 Exit;
607 end;
609 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
610 if (RecName = TEXTURE_NAME_WATER) or
611 (RecName = TEXTURE_NAME_ACID1) or
612 (RecName = TEXTURE_NAME_ACID2) then
613 begin
614 SetLength(Textures, Length(Textures)+1);
616 with Textures[High(Textures)] do
617 begin
618 TextureName := RecName;
620 if TextureName = TEXTURE_NAME_WATER then
621 TextureID := TEXTURE_SPECIAL_WATER
622 else
623 if TextureName = TEXTURE_NAME_ACID1 then
624 TextureID := TEXTURE_SPECIAL_ACID1
625 else
626 if TextureName = TEXTURE_NAME_ACID2 then
627 TextureID := TEXTURE_SPECIAL_ACID2;
629 Anim := False;
630 end;
632 result := High(Textures);
633 Exit;
634 end;
636 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
637 WADName := g_ExtractWadName(RecName);
639 WAD := TWADFile.Create();
641 if WADName <> '' then
642 WADName := GameDir+'/wads/'+WADName
643 else
644 WADName := Map;
646 WAD.ReadFile(WADName);
648 txname := RecName;
650 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
651 begin
652 FreeMem(TextureData);
653 RecName := 'COMMON\ALIEN';
654 end;
657 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
658 begin
659 SetLength(Textures, Length(Textures)+1);
660 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
661 Exit;
662 e_GetTextureSize(Textures[High(Textures)].TextureID,
663 @Textures[High(Textures)].Width,
664 @Textures[High(Textures)].Height);
665 FreeMem(TextureData);
666 Textures[High(Textures)].TextureName := {RecName}txname;
667 Textures[High(Textures)].Anim := False;
669 result := High(Textures);
670 end
671 else // Íåò òàêîãî ðåóñðñà â WAD'å
672 begin
673 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
674 if log then
675 begin
676 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
677 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
678 end;
679 end;
681 WAD.Free();
682 end;
684 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
685 var
686 WAD: TWADFile;
687 TextureWAD: PChar = nil;
688 TextData: Pointer = nil;
689 TextureData: Pointer = nil;
690 cfg: TConfig = nil;
691 WADName: String;
692 ResLength: Integer;
693 TextureResource: String;
694 _width, _height, _framecount, _speed: Integer;
695 _backanimation: Boolean;
696 //imgfmt: string;
697 ia: TDynImageDataArray = nil;
698 f, c, frdelay, frloop: Integer;
699 begin
700 result := -1;
702 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
704 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
705 WADName := g_ExtractWadName(RecName);
707 WAD := TWADFile.Create();
708 try
709 if WADName <> '' then
710 WADName := GameDir+'/wads/'+WADName
711 else
712 WADName := Map;
714 WAD.ReadFile(WADName);
716 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
717 begin
718 if log then
719 begin
720 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
721 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
722 end;
723 exit;
724 end;
726 {TEST
727 if WADName = Map then
728 begin
729 //FreeMem(TextureWAD);
730 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
731 end;
734 WAD.FreeWAD();
736 if ResLength < 6 then
737 begin
738 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
739 exit;
740 end;
742 // ýòî ïòèöà? ýòî ñàìîë¸ò?
743 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
744 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
745 begin
746 // íåò, ýòî ñóïåðìåí!
747 if not WAD.ReadMemory(TextureWAD, ResLength) then
748 begin
749 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
750 exit;
751 end;
753 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
754 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
755 begin
756 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
757 exit;
758 end;
760 cfg := TConfig.CreateMem(TextData, ResLength);
762 TextureResource := cfg.ReadStr('', 'resource', '');
763 if TextureResource = '' then
764 begin
765 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
766 exit;
767 end;
769 _width := cfg.ReadInt('', 'framewidth', 0);
770 _height := cfg.ReadInt('', 'frameheight', 0);
771 _framecount := cfg.ReadInt('', 'framecount', 0);
772 _speed := cfg.ReadInt('', 'waitcount', 0);
773 _backanimation := cfg.ReadBool('', 'backanimation', False);
775 cfg.Free();
776 cfg := nil;
778 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
779 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
780 begin
781 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
782 exit;
783 end;
785 WAD.Free();
786 WAD := nil;
788 SetLength(Textures, Length(Textures)+1);
789 with Textures[High(Textures)] do
790 begin
791 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
792 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
793 begin
794 TextureName := RecName;
795 Width := _width;
796 Height := _height;
797 Anim := True;
798 FramesCount := _framecount;
799 Speed := _speed;
800 result := High(Textures);
801 end
802 else
803 begin
804 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
805 end;
806 end;
807 end
808 else
809 begin
810 // try animated image
812 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
813 if length(imgfmt) = 0 then
814 begin
815 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
816 exit;
817 end;
819 GlobalMetadata.ClearMetaItems();
820 GlobalMetadata.ClearMetaItemsForSaving();
821 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
822 begin
823 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
824 exit;
825 end;
826 if length(ia) = 0 then
827 begin
828 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
829 exit;
830 end;
832 WAD.Free();
833 WAD := nil;
835 _width := ia[0].width;
836 _height := ia[0].height;
837 _framecount := length(ia);
838 _speed := 1;
839 _backanimation := false;
840 frdelay := -1;
841 frloop := -666;
842 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
843 begin
844 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
845 try
846 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
847 frdelay := f;
848 if f < 0 then f := 0;
849 // rounding ;-)
850 c := f mod 28;
851 if c < 13 then c := 0 else c := 1;
852 f := (f div 28)+c;
853 if f < 1 then f := 1 else if f > 255 then f := 255;
854 _speed := f;
855 except
856 end;
857 end;
858 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
859 begin
860 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
861 try
862 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
863 frloop := f;
864 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
865 except
866 end;
867 end;
868 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
869 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
870 f := ord(_backanimation);
871 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);
873 SetLength(Textures, Length(Textures)+1);
874 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
875 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
876 begin
877 Textures[High(Textures)].TextureName := RecName;
878 Textures[High(Textures)].Width := _width;
879 Textures[High(Textures)].Height := _height;
880 Textures[High(Textures)].Anim := True;
881 Textures[High(Textures)].FramesCount := length(ia);
882 Textures[High(Textures)].Speed := _speed;
883 result := High(Textures);
884 //writeln(' CREATED!');
885 end
886 else
887 begin
888 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
889 end;
890 end;
891 finally
892 for f := 0 to High(ia) do FreeImage(ia[f]);
893 WAD.Free();
894 cfg.Free();
895 if TextureWAD <> nil then FreeMem(TextureWAD);
896 if TextData <> nil then FreeMem(TextData);
897 if TextureData <> nil then FreeMem(TextureData);
898 end;
899 end;
901 procedure CreateItem(Item: TItemRec_1);
902 begin
903 if g_Game_IsClient then Exit;
905 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
906 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
907 Exit;
909 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
910 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
911 end;
913 procedure CreateArea(Area: TAreaRec_1);
914 var
915 a: Integer;
916 id: DWORD = 0;
917 begin
918 case Area.AreaType of
919 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
920 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
921 begin
922 SetLength(RespawnPoints, Length(RespawnPoints)+1);
923 with RespawnPoints[High(RespawnPoints)] do
924 begin
925 X := Area.X;
926 Y := Area.Y;
927 Direction := TDirection(Area.Direction);
929 case Area.AreaType of
930 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
931 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
932 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
933 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
934 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
935 end;
936 end;
937 end;
939 AREA_REDFLAG, AREA_BLUEFLAG:
940 begin
941 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
943 if FlagPoints[a] <> nil then Exit;
945 New(FlagPoints[a]);
947 with FlagPoints[a]^ do
948 begin
949 X := Area.X-FLAGRECT.X;
950 Y := Area.Y-FLAGRECT.Y;
951 Direction := TDirection(Area.Direction);
952 end;
954 with gFlags[a] do
955 begin
956 case a of
957 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
958 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
959 end;
961 Animation := TAnimation.Create(id, True, 8);
962 Obj.Rect := FLAGRECT;
964 g_Map_ResetFlag(a);
965 end;
966 end;
968 AREA_DOMFLAG:
969 begin
970 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
971 with DOMFlagPoints[High(DOMFlagPoints)] do
972 begin
973 X := Area.X;
974 Y := Area.Y;
975 Direction := TDirection(Area.Direction);
976 end;
978 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
979 end;
980 end;
981 end;
983 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
984 var
985 _trigger: TTrigger;
986 begin
987 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
989 with _trigger do
990 begin
991 X := Trigger.X;
992 Y := Trigger.Y;
993 Width := Trigger.Width;
994 Height := Trigger.Height;
995 Enabled := ByteBool(Trigger.Enabled);
996 TexturePanel := Trigger.TexturePanel;
997 TexturePanelType := fTexturePanel1Type;
998 ShotPanelType := fTexturePanel2Type;
999 TriggerType := Trigger.TriggerType;
1000 ActivateType := Trigger.ActivateType;
1001 Keys := Trigger.Keys;
1002 Data.Default := Trigger.DATA;
1003 end;
1005 g_Triggers_Create(_trigger);
1006 end;
1008 procedure CreateMonster(monster: TMonsterRec_1);
1009 var
1010 a: Integer;
1011 mon: TMonster;
1012 begin
1013 if g_Game_IsClient then Exit;
1015 if (gGameSettings.GameType = GT_SINGLE)
1016 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1017 begin
1018 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1020 if gTriggers <> nil then
1021 begin
1022 for a := 0 to High(gTriggers) do
1023 begin
1024 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1025 begin
1026 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1027 end;
1028 end;
1029 end;
1031 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1032 end;
1033 end;
1035 procedure g_Map_ReAdd_DieTriggers();
1037 function monsDieTrig (mon: TMonster): Boolean;
1038 var
1039 a: Integer;
1040 begin
1041 result := false; // don't stop
1042 mon.ClearTriggers();
1043 for a := 0 to High(gTriggers) do
1044 begin
1045 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1046 begin
1047 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1048 end;
1049 end;
1050 end;
1052 begin
1053 if g_Game_IsClient then Exit;
1055 g_Mons_ForEach(monsDieTrig);
1056 end;
1058 function extractWadName(resourceName: string): string;
1059 var
1060 posN: Integer;
1061 begin
1062 posN := Pos(':', resourceName);
1063 if posN > 0 then
1064 Result:= Copy(resourceName, 0, posN-1)
1065 else
1066 Result := '';
1067 end;
1069 procedure addResToExternalResList(res: string);
1070 begin
1071 res := extractWadName(res);
1072 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1073 gExternalResources.Add(res);
1074 end;
1076 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1077 var
1078 textures: TTexturesRec1Array;
1079 mapHeader: TMapHeaderRec_1;
1080 i: integer;
1081 resFile: String = '';
1082 begin
1083 if gExternalResources = nil then
1084 gExternalResources := TStringList.Create;
1086 gExternalResources.Clear;
1087 textures := mapReader.GetTextures();
1088 for i := 0 to High(textures) do
1089 begin
1090 addResToExternalResList(resFile);
1091 end;
1093 textures := nil;
1095 mapHeader := mapReader.GetMapHeader;
1097 addResToExternalResList(mapHeader.MusicName);
1098 addResToExternalResList(mapHeader.SkyName);
1099 end;
1102 procedure mapCreateGrid ();
1103 var
1104 mapX0: Integer = $3fffffff;
1105 mapY0: Integer = $3fffffff;
1106 mapX1: Integer = -$3fffffff;
1107 mapY1: Integer = -$3fffffff;
1109 procedure calcBoundingBox (constref panels: TPanelArray);
1110 var
1111 idx: Integer;
1112 pan: TPanel;
1113 begin
1114 for idx := 0 to High(panels) do
1115 begin
1116 pan := panels[idx];
1117 if not pan.visvalid then continue;
1118 if (pan.Width < 1) or (pan.Height < 1) then continue;
1119 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1120 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1121 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1122 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1123 end;
1124 end;
1126 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1127 var
1128 idx: Integer;
1129 pan: TPanel;
1130 begin
1131 tag := panelTypeToTag(tag);
1132 for idx := High(panels) downto 0 do
1133 begin
1134 pan := panels[idx];
1135 pan.tag := tag;
1136 if not pan.visvalid then continue;
1137 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1138 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1139 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1140 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1141 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1142 begin
1143 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1144 end;
1145 {$ENDIF}
1146 end;
1147 end;
1149 begin
1150 mapGrid.Free();
1151 mapGrid := nil;
1153 calcBoundingBox(gWalls);
1154 calcBoundingBox(gRenderBackgrounds);
1155 calcBoundingBox(gRenderForegrounds);
1156 calcBoundingBox(gWater);
1157 calcBoundingBox(gAcid1);
1158 calcBoundingBox(gAcid2);
1159 calcBoundingBox(gSteps);
1160 calcBoundingBox(gLifts);
1161 calcBoundingBox(gBlockMon);
1163 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1165 mapGrid := TPanelGrid.Create(mapX0-512, mapY0-512, mapX1-mapX0+1+512*2, mapY1-mapY0+1+512*2);
1167 addPanelsToGrid(gWalls, PANEL_WALL);
1168 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1169 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1170 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1171 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1172 addPanelsToGrid(gWater, PANEL_WATER);
1173 addPanelsToGrid(gAcid1, PANEL_ACID1);
1174 addPanelsToGrid(gAcid2, PANEL_ACID2);
1175 addPanelsToGrid(gSteps, PANEL_STEP);
1176 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1177 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1179 mapGrid.dumpStats();
1181 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1182 end;
1185 function g_Map_Load(Res: String): Boolean;
1186 const
1187 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1188 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1189 var
1190 WAD: TWADFile;
1191 MapReader: TMapReader_1;
1192 Header: TMapHeaderRec_1;
1193 _textures: TTexturesRec1Array;
1194 _texnummap: array of Integer; // `_textures` -> `Textures`
1195 panels: TPanelsRec1Array;
1196 items: TItemsRec1Array;
1197 monsters: TMonsterRec1Array;
1198 areas: TAreasRec1Array;
1199 triggers: TTriggersRec1Array;
1200 a, b, c, k: Integer;
1201 PanelID: DWORD;
1202 AddTextures: TAddTextureArray;
1203 texture: TTextureRec_1;
1204 TriggersTable: Array of record
1205 TexturePanel: Integer;
1206 LiftPanel: Integer;
1207 DoorPanel: Integer;
1208 ShotPanel: Integer;
1209 end;
1210 FileName, mapResName, s, TexName: String;
1211 Data: Pointer;
1212 Len: Integer;
1213 ok, isAnim, trigRef: Boolean;
1214 CurTex, ntn: Integer;
1216 begin
1217 mapGrid.Free();
1218 mapGrid := nil;
1219 //mapTree.Free();
1220 //mapTree := nil;
1222 Result := False;
1223 gMapInfo.Map := Res;
1224 TriggersTable := nil;
1225 FillChar(texture, SizeOf(texture), 0);
1227 sfsGCDisable(); // temporary disable removing of temporary volumes
1228 try
1229 // Çàãðóçêà WAD:
1230 FileName := g_ExtractWadName(Res);
1231 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1232 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1234 WAD := TWADFile.Create();
1235 if not WAD.ReadFile(FileName) then
1236 begin
1237 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1238 WAD.Free();
1239 Exit;
1240 end;
1241 //k8: why loader ignores path here?
1242 mapResName := g_ExtractFileName(Res);
1243 if not WAD.GetMapResource(mapResName, Data, Len) then
1244 begin
1245 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1246 WAD.Free();
1247 Exit;
1248 end;
1250 WAD.Free();
1252 // Çàãðóçêà êàðòû:
1253 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1254 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1255 MapReader := TMapReader_1.Create();
1257 if not MapReader.LoadMap(Data) then
1258 begin
1259 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1260 FreeMem(Data);
1261 MapReader.Free();
1262 Exit;
1263 end;
1265 FreeMem(Data);
1266 generateExternalResourcesList(MapReader);
1267 // Çàãðóçêà òåêñòóð:
1268 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1269 _textures := MapReader.GetTextures();
1270 _texnummap := nil;
1272 // Äîáàâëåíèå òåêñòóð â Textures[]:
1273 if _textures <> nil then
1274 begin
1275 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1276 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1277 SetLength(_texnummap, length(_textures));
1279 for a := 0 to High(_textures) do
1280 begin
1281 SetLength(s, 64);
1282 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1283 for b := 1 to Length(s) do
1284 if s[b] = #0 then
1285 begin
1286 SetLength(s, b-1);
1287 Break;
1288 end;
1289 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1290 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1291 // Àíèìèðîâàííàÿ òåêñòóðà:
1292 if ByteBool(_textures[a].Anim) then
1293 begin
1294 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1295 if ntn < 0 then
1296 begin
1297 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1298 ntn := CreateNullTexture(_textures[a].Resource);
1299 end;
1300 end
1301 else // Îáû÷íàÿ òåêñòóðà:
1302 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1303 if ntn < 0 then
1304 begin
1305 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1306 ntn := CreateNullTexture(_textures[a].Resource);
1307 end;
1309 _texnummap[a] := ntn; // fix texture number
1310 g_Game_StepLoading();
1311 end;
1312 end;
1314 // Çàãðóçêà òðèããåðîâ:
1315 gTriggerClientID := 0;
1316 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1317 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1318 triggers := MapReader.GetTriggers();
1320 // Çàãðóçêà ïàíåëåé:
1321 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1322 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1323 panels := MapReader.GetPanels();
1325 // check texture numbers for panels
1326 for a := 0 to High(panels) do
1327 begin
1328 if panels[a].TextureNum > High(_textures) then
1329 begin
1330 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1331 result := false;
1332 exit;
1333 end;
1334 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1335 end;
1337 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1338 if triggers <> nil then
1339 begin
1340 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1341 SetLength(TriggersTable, Length(triggers));
1342 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1344 for a := 0 to High(TriggersTable) do
1345 begin
1346 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1347 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1348 // Ëèôòû:
1349 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1350 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1351 else
1352 TriggersTable[a].LiftPanel := -1;
1353 // Äâåðè:
1354 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1355 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1356 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1357 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1358 else
1359 TriggersTable[a].DoorPanel := -1;
1360 // Òóðåëü:
1361 if triggers[a].TriggerType = TRIGGER_SHOT then
1362 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1363 else
1364 TriggersTable[a].ShotPanel := -1;
1366 g_Game_StepLoading();
1367 end;
1368 end;
1370 // Ñîçäàåì ïàíåëè:
1371 if panels <> nil then
1372 begin
1373 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1374 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1376 for a := 0 to High(panels) do
1377 begin
1378 SetLength(AddTextures, 0);
1379 trigRef := False;
1380 CurTex := -1;
1381 if _textures <> nil then
1382 begin
1383 texture := _textures[panels[a].TextureNum];
1384 ok := True;
1385 end
1386 else
1387 ok := False;
1389 if ok then
1390 begin
1391 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1392 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1393 ok := False;
1394 if (TriggersTable <> nil) and (_textures <> nil) then
1395 for b := 0 to High(TriggersTable) do
1396 if (TriggersTable[b].TexturePanel = a)
1397 or (TriggersTable[b].ShotPanel = a) then
1398 begin
1399 trigRef := True;
1400 ok := True;
1401 Break;
1402 end;
1403 end;
1405 if ok then
1406 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1407 SetLength(s, 64);
1408 CopyMemory(@s[1], @texture.Resource[0], 64);
1409 // Èçìåðÿåì äëèíó:
1410 Len := Length(s);
1411 for c := Len downto 1 do
1412 if s[c] <> #0 then
1413 begin
1414 Len := c;
1415 Break;
1416 end;
1417 SetLength(s, Len);
1419 // Ñïåö-òåêñòóðû çàïðåùåíû:
1420 if g_Map_IsSpecialTexture(s) then
1421 ok := False
1422 else
1423 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1424 ok := g_Texture_NumNameFindStart(s);
1426 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1427 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1428 if ok then
1429 begin
1430 k := NNF_NAME_BEFORE;
1431 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1432 while ok or (k = NNF_NAME_BEFORE) or
1433 (k = NNF_NAME_EQUALS) do
1434 begin
1435 k := g_Texture_NumNameFindNext(TexName);
1437 if (k = NNF_NAME_BEFORE) or
1438 (k = NNF_NAME_AFTER) then
1439 begin
1440 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1441 if ByteBool(texture.Anim) then
1442 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1443 isAnim := True;
1444 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1445 if not ok then
1446 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1447 isAnim := False;
1448 ok := CreateTexture(TexName, FileName, False) >= 0;
1449 end;
1450 end
1451 else
1452 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1453 isAnim := False;
1454 ok := CreateTexture(TexName, FileName, False) >= 0;
1455 if not ok then
1456 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1457 isAnim := True;
1458 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1459 end;
1460 end;
1462 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1463 if ok then
1464 begin
1465 for c := 0 to High(Textures) do
1466 if Textures[c].TextureName = TexName then
1467 begin
1468 SetLength(AddTextures, Length(AddTextures)+1);
1469 AddTextures[High(AddTextures)].Texture := c;
1470 AddTextures[High(AddTextures)].Anim := isAnim;
1471 Break;
1472 end;
1473 end;
1474 end
1475 else
1476 if k = NNF_NAME_EQUALS then
1477 begin
1478 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1479 SetLength(AddTextures, Length(AddTextures)+1);
1480 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1481 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1482 CurTex := High(AddTextures);
1483 ok := True;
1484 end
1485 else // NNF_NO_NAME
1486 ok := False;
1487 end; // while ok...
1489 ok := True;
1490 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1491 end; // if ok - ññûëàþòñÿ òðèããåðû
1493 if not ok then
1494 begin
1495 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1496 SetLength(AddTextures, 1);
1497 AddTextures[0].Texture := panels[a].TextureNum;
1498 AddTextures[0].Anim := ByteBool(texture.Anim);
1499 CurTex := 0;
1500 end;
1502 //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);
1504 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1505 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1507 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1508 if TriggersTable <> nil then
1509 for b := 0 to High(TriggersTable) do
1510 begin
1511 // Òðèããåð äâåðè/ëèôòà:
1512 if (TriggersTable[b].LiftPanel = a) or
1513 (TriggersTable[b].DoorPanel = a) then
1514 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1515 // Òðèããåð ñìåíû òåêñòóðû:
1516 if TriggersTable[b].TexturePanel = a then
1517 triggers[b].TexturePanel := PanelID;
1518 // Òðèããåð "Òóðåëü":
1519 if TriggersTable[b].ShotPanel = a then
1520 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1521 end;
1523 g_Game_StepLoading();
1524 end;
1525 end;
1527 // create map grid, init other grids (for monsters, for example)
1528 e_WriteLog('Creating map grid', MSG_NOTIFY);
1529 mapCreateGrid();
1532 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1533 if (triggers <> nil) and not gLoadGameMode then
1534 begin
1535 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1536 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1537 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1538 for a := 0 to High(triggers) do
1539 begin
1540 if triggers[a].TexturePanel <> -1 then
1541 b := panels[TriggersTable[a].TexturePanel].PanelType
1542 else
1543 b := 0;
1544 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1545 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1546 c := panels[TriggersTable[a].ShotPanel].PanelType
1547 else
1548 c := 0;
1549 CreateTrigger(triggers[a], b, c);
1550 end;
1551 end;
1553 // Çàãðóçêà ïðåäìåòîâ:
1554 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1555 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1556 items := MapReader.GetItems();
1558 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1559 if (items <> nil) and not gLoadGameMode then
1560 begin
1561 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1562 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1563 for a := 0 to High(items) do
1564 CreateItem(Items[a]);
1565 end;
1567 // Çàãðóçêà îáëàñòåé:
1568 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1569 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1570 areas := MapReader.GetAreas();
1572 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1573 if areas <> nil then
1574 begin
1575 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1576 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1577 for a := 0 to High(areas) do
1578 CreateArea(areas[a]);
1579 end;
1581 // Çàãðóçêà ìîíñòðîâ:
1582 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1583 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1584 monsters := MapReader.GetMonsters();
1586 gTotalMonsters := 0;
1588 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1589 if (monsters <> nil) and not gLoadGameMode then
1590 begin
1591 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1592 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1593 for a := 0 to High(monsters) do
1594 CreateMonster(monsters[a]);
1595 end;
1597 // Çàãðóçêà îïèñàíèÿ êàðòû:
1598 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1599 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1600 Header := MapReader.GetMapHeader();
1602 MapReader.Free();
1604 with gMapInfo do
1605 begin
1606 Name := Header.MapName;
1607 Description := Header.MapDescription;
1608 Author := Header.MapAuthor;
1609 MusicName := Header.MusicName;
1610 SkyName := Header.SkyName;
1611 Height := Header.Height;
1612 Width := Header.Width;
1613 end;
1615 // Çàãðóçêà íåáà:
1616 if gMapInfo.SkyName <> '' then
1617 begin
1618 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1619 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1620 FileName := g_ExtractWadName(gMapInfo.SkyName);
1622 if FileName <> '' then
1623 FileName := GameDir+'/wads/'+FileName
1624 else
1625 begin
1626 FileName := g_ExtractWadName(Res);
1627 end;
1629 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1630 if g_Texture_CreateWAD(BackID, s) then
1631 begin
1632 g_Game_SetupScreenSize();
1633 end
1634 else
1635 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1636 end;
1638 // Çàãðóçêà ìóçûêè:
1639 ok := False;
1640 if gMapInfo.MusicName <> '' then
1641 begin
1642 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1643 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1644 FileName := g_ExtractWadName(gMapInfo.MusicName);
1646 if FileName <> '' then
1647 FileName := GameDir+'/wads/'+FileName
1648 else
1649 begin
1650 FileName := g_ExtractWadName(Res);
1651 end;
1653 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1654 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1655 ok := True
1656 else
1657 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1658 end;
1660 // Îñòàëüíûå óñòàíâêè:
1661 CreateDoorMap();
1662 CreateLiftMap();
1664 g_Items_Init();
1665 g_Weapon_Init();
1666 g_Monsters_Init();
1668 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1669 if not gLoadGameMode then
1670 g_GFX_Init();
1672 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1673 _textures := nil;
1674 panels := nil;
1675 items := nil;
1676 areas := nil;
1677 triggers := nil;
1678 TriggersTable := nil;
1679 AddTextures := nil;
1681 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1682 if ok and (not gLoadGameMode) then
1683 begin
1684 gMusic.SetByName(gMapInfo.MusicName);
1685 gMusic.Play();
1686 end
1687 else
1688 gMusic.SetByName('');
1689 finally
1690 sfsGCEnable(); // enable releasing unused volumes
1691 end;
1693 e_WriteLog('Done loading map.', MSG_NOTIFY);
1694 Result := True;
1695 end;
1697 function g_Map_GetMapInfo(Res: String): TMapInfo;
1698 var
1699 WAD: TWADFile;
1700 MapReader: TMapReader_1;
1701 Header: TMapHeaderRec_1;
1702 FileName: String;
1703 Data: Pointer;
1704 Len: Integer;
1705 begin
1706 FillChar(Result, SizeOf(Result), 0);
1707 FileName := g_ExtractWadName(Res);
1709 WAD := TWADFile.Create();
1710 if not WAD.ReadFile(FileName) then
1711 begin
1712 WAD.Free();
1713 Exit;
1714 end;
1716 //k8: it ignores path again
1717 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1718 begin
1719 WAD.Free();
1720 Exit;
1721 end;
1723 WAD.Free();
1725 MapReader := TMapReader_1.Create();
1727 if not MapReader.LoadMap(Data) then
1728 begin
1729 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1730 ZeroMemory(@Header, SizeOf(Header));
1731 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1732 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1733 end
1734 else
1735 begin
1736 Header := MapReader.GetMapHeader();
1737 Result.Name := Header.MapName;
1738 Result.Description := Header.MapDescription;
1739 end;
1741 FreeMem(Data);
1742 MapReader.Free();
1744 Result.Map := Res;
1745 Result.Author := Header.MapAuthor;
1746 Result.Height := Header.Height;
1747 Result.Width := Header.Width;
1748 end;
1750 function g_Map_GetMapsList(WADName: string): SArray;
1751 var
1752 WAD: TWADFile;
1753 a: Integer;
1754 ResList: SArray;
1755 begin
1756 Result := nil;
1757 WAD := TWADFile.Create();
1758 if not WAD.ReadFile(WADName) then
1759 begin
1760 WAD.Free();
1761 Exit;
1762 end;
1763 ResList := WAD.GetMapResources();
1764 if ResList <> nil then
1765 begin
1766 for a := 0 to High(ResList) do
1767 begin
1768 SetLength(Result, Length(Result)+1);
1769 Result[High(Result)] := ResList[a];
1770 end;
1771 end;
1772 WAD.Free();
1773 end;
1775 function g_Map_Exist(Res: string): Boolean;
1776 var
1777 WAD: TWADFile;
1778 FileName, mnn: string;
1779 ResList: SArray;
1780 a: Integer;
1781 begin
1782 Result := False;
1784 FileName := addWadExtension(g_ExtractWadName(Res));
1786 WAD := TWADFile.Create;
1787 if not WAD.ReadFile(FileName) then
1788 begin
1789 WAD.Free();
1790 Exit;
1791 end;
1793 ResList := WAD.GetMapResources();
1794 WAD.Free();
1796 mnn := g_ExtractFileName(Res);
1797 if ResList <> nil then
1798 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1799 begin
1800 Result := True;
1801 Exit;
1802 end;
1803 end;
1805 procedure g_Map_Free();
1806 var
1807 a: Integer;
1809 procedure FreePanelArray(var panels: TPanelArray);
1810 var
1811 i: Integer;
1813 begin
1814 if panels <> nil then
1815 begin
1816 for i := 0 to High(panels) do
1817 panels[i].Free();
1818 panels := nil;
1819 end;
1820 end;
1822 begin
1823 g_GFX_Free();
1824 g_Weapon_Free();
1825 g_Items_Free();
1826 g_Triggers_Free();
1827 g_Monsters_Free();
1829 RespawnPoints := nil;
1830 if FlagPoints[FLAG_RED] <> nil then
1831 begin
1832 Dispose(FlagPoints[FLAG_RED]);
1833 FlagPoints[FLAG_RED] := nil;
1834 end;
1835 if FlagPoints[FLAG_BLUE] <> nil then
1836 begin
1837 Dispose(FlagPoints[FLAG_BLUE]);
1838 FlagPoints[FLAG_BLUE] := nil;
1839 end;
1840 //DOMFlagPoints := nil;
1842 //gDOMFlags := nil;
1844 if Textures <> nil then
1845 begin
1846 for a := 0 to High(Textures) do
1847 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1848 if Textures[a].Anim then
1849 g_Frames_DeleteByID(Textures[a].FramesID)
1850 else
1851 if Textures[a].TextureID <> TEXTURE_NONE then
1852 e_DeleteTexture(Textures[a].TextureID);
1854 Textures := nil;
1855 end;
1857 FreePanelArray(gWalls);
1858 FreePanelArray(gRenderBackgrounds);
1859 FreePanelArray(gRenderForegrounds);
1860 FreePanelArray(gWater);
1861 FreePanelArray(gAcid1);
1862 FreePanelArray(gAcid2);
1863 FreePanelArray(gSteps);
1864 FreePanelArray(gLifts);
1865 FreePanelArray(gBlockMon);
1867 if BackID <> DWORD(-1) then
1868 begin
1869 gBackSize.X := 0;
1870 gBackSize.Y := 0;
1871 e_DeleteTexture(BackID);
1872 BackID := DWORD(-1);
1873 end;
1875 g_Game_StopAllSounds(False);
1876 gMusic.FreeSound();
1877 g_Sound_Delete(gMapInfo.MusicName);
1879 gMapInfo.Name := '';
1880 gMapInfo.Description := '';
1881 gMapInfo.MusicName := '';
1882 gMapInfo.Height := 0;
1883 gMapInfo.Width := 0;
1885 gDoorMap := nil;
1886 gLiftMap := nil;
1888 PanelByID := nil;
1889 end;
1891 procedure g_Map_Update();
1892 var
1893 a, d, j: Integer;
1894 m: Word;
1895 s: String;
1897 procedure UpdatePanelArray(var panels: TPanelArray);
1898 var
1899 i: Integer;
1901 begin
1902 if panels <> nil then
1903 for i := 0 to High(panels) do
1904 panels[i].Update();
1905 end;
1907 begin
1908 UpdatePanelArray(gWalls);
1909 UpdatePanelArray(gRenderBackgrounds);
1910 UpdatePanelArray(gRenderForegrounds);
1911 UpdatePanelArray(gWater);
1912 UpdatePanelArray(gAcid1);
1913 UpdatePanelArray(gAcid2);
1914 UpdatePanelArray(gSteps);
1916 if gGameSettings.GameMode = GM_CTF then
1917 begin
1918 for a := FLAG_RED to FLAG_BLUE do
1919 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1920 with gFlags[a] do
1921 begin
1922 if gFlags[a].Animation <> nil then
1923 gFlags[a].Animation.Update();
1925 m := g_Obj_Move(@Obj, True, True);
1927 if gTime mod (GAME_TICK*2) <> 0 then
1928 Continue;
1930 // Ñîïðîòèâëåíèå âîçäóõà:
1931 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1933 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1934 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1935 begin
1936 g_Map_ResetFlag(a);
1937 gFlags[a].CaptureTime := 0;
1938 if a = FLAG_RED then
1939 s := _lc[I_PLAYER_FLAG_RED]
1940 else
1941 s := _lc[I_PLAYER_FLAG_BLUE];
1942 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1944 if g_Game_IsNet then
1945 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1946 Continue;
1947 end;
1949 if Count > 0 then
1950 Count := Count - 1;
1952 // Èãðîê áåðåò ôëàã:
1953 if gPlayers <> nil then
1954 begin
1955 j := Random(Length(gPlayers)) - 1;
1957 for d := 0 to High(gPlayers) do
1958 begin
1959 Inc(j);
1960 if j > High(gPlayers) then
1961 j := 0;
1963 if gPlayers[j] <> nil then
1964 if gPlayers[j].Live and
1965 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1966 begin
1967 if gPlayers[j].GetFlag(a) then
1968 Break;
1969 end;
1970 end;
1971 end;
1972 end;
1973 end;
1974 end;
1977 // old algo
1978 procedure g_Map_DrawPanels (PanelType: Word);
1980 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
1981 var
1982 idx: Integer;
1983 begin
1984 if (panels <> nil) then
1985 begin
1986 // alas, no visible set
1987 for idx := 0 to High(panels) do
1988 begin
1989 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1990 end;
1991 end;
1992 end;
1994 begin
1995 case PanelType of
1996 PANEL_WALL: DrawPanels(gWalls);
1997 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
1998 PANEL_BACK: DrawPanels(gRenderBackgrounds);
1999 PANEL_FORE: DrawPanels(gRenderForegrounds);
2000 PANEL_WATER: DrawPanels(gWater);
2001 PANEL_ACID1: DrawPanels(gAcid1);
2002 PANEL_ACID2: DrawPanels(gAcid2);
2003 PANEL_STEP: DrawPanels(gSteps);
2004 end;
2005 end;
2008 // new algo
2009 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2011 function checker (pan: TPanel; tag: Integer): Boolean;
2012 begin
2013 result := false; // don't stop, ever
2014 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2015 gDrawPanelList.insert(pan);
2016 end;
2018 begin
2019 dplClear();
2020 //tagmask := panelTypeToTag(PanelType);
2022 {if gdbg_map_use_tree_draw then
2023 begin
2024 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2025 end
2026 else}
2027 begin
2028 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask, true);
2029 end;
2030 // list will be rendered in `g_game.DrawPlayer()`
2031 end;
2034 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2036 function checker (pan: TPanel; tag: Integer): Boolean;
2037 begin
2038 result := false; // don't stop, ever
2039 pan.DrawShadowVolume(lightX, lightY, radius);
2040 end;
2042 begin
2043 {if gdbg_map_use_tree_draw then
2044 begin
2045 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2046 end
2047 else}
2048 begin
2049 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor), true);
2050 end;
2051 end;
2054 procedure g_Map_DrawBack(dx, dy: Integer);
2055 begin
2056 if gDrawBackGround and (BackID <> DWORD(-1)) then
2057 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2058 else
2059 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2060 end;
2062 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2063 PanelType: Word; b1x3: Boolean=false): Boolean;
2064 var
2065 a, h: Integer;
2066 begin
2067 Result := False;
2069 if WordBool(PanelType and PANEL_WALL) then
2070 if gWalls <> nil then
2071 begin
2072 h := High(gWalls);
2074 for a := 0 to h do
2075 if gWalls[a].Enabled and
2076 g_Collide(X, Y, Width, Height,
2077 gWalls[a].X, gWalls[a].Y,
2078 gWalls[a].Width, gWalls[a].Height) then
2079 begin
2080 Result := True;
2081 Exit;
2082 end;
2083 end;
2085 if WordBool(PanelType and PANEL_WATER) then
2086 if gWater <> nil then
2087 begin
2088 h := High(gWater);
2090 for a := 0 to h do
2091 if g_Collide(X, Y, Width, Height,
2092 gWater[a].X, gWater[a].Y,
2093 gWater[a].Width, gWater[a].Height) then
2094 begin
2095 Result := True;
2096 Exit;
2097 end;
2098 end;
2100 if WordBool(PanelType and PANEL_ACID1) then
2101 if gAcid1 <> nil then
2102 begin
2103 h := High(gAcid1);
2105 for a := 0 to h do
2106 if g_Collide(X, Y, Width, Height,
2107 gAcid1[a].X, gAcid1[a].Y,
2108 gAcid1[a].Width, gAcid1[a].Height) then
2109 begin
2110 Result := True;
2111 Exit;
2112 end;
2113 end;
2115 if WordBool(PanelType and PANEL_ACID2) then
2116 if gAcid2 <> nil then
2117 begin
2118 h := High(gAcid2);
2120 for a := 0 to h do
2121 if g_Collide(X, Y, Width, Height,
2122 gAcid2[a].X, gAcid2[a].Y,
2123 gAcid2[a].Width, gAcid2[a].Height) then
2124 begin
2125 Result := True;
2126 Exit;
2127 end;
2128 end;
2130 if WordBool(PanelType and PANEL_STEP) then
2131 if gSteps <> nil then
2132 begin
2133 h := High(gSteps);
2135 for a := 0 to h do
2136 if g_Collide(X, Y, Width, Height,
2137 gSteps[a].X, gSteps[a].Y,
2138 gSteps[a].Width, gSteps[a].Height) then
2139 begin
2140 Result := True;
2141 Exit;
2142 end;
2143 end;
2145 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2146 if gLifts <> nil then
2147 begin
2148 h := High(gLifts);
2150 for a := 0 to h do
2151 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2152 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2153 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2154 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2155 g_Collide(X, Y, Width, Height,
2156 gLifts[a].X, gLifts[a].Y,
2157 gLifts[a].Width, gLifts[a].Height) then
2158 begin
2159 Result := True;
2160 Exit;
2161 end;
2162 end;
2164 if WordBool(PanelType and PANEL_BLOCKMON) then
2165 if gBlockMon <> nil then
2166 begin
2167 h := High(gBlockMon);
2169 for a := 0 to h do
2170 if ( (not b1x3) or
2171 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2172 g_Collide(X, Y, Width, Height,
2173 gBlockMon[a].X, gBlockMon[a].Y,
2174 gBlockMon[a].Width, gBlockMon[a].Height) then
2175 begin
2176 Result := True;
2177 Exit;
2178 end;
2179 end;
2180 end;
2182 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2183 var
2184 texid: DWORD;
2186 function checkPanels (constref panels: TPanelArray): Boolean;
2187 var
2188 a: Integer;
2189 begin
2190 result := false;
2191 if panels = nil then exit;
2192 for a := 0 to High(panels) do
2193 begin
2194 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2195 begin
2196 result := true;
2197 texid := panels[a].GetTextureID();
2198 exit;
2199 end;
2200 end;
2201 end;
2203 begin
2204 texid := TEXTURE_NONE;
2205 result := texid;
2206 if not checkPanels(gWater) then
2207 if not checkPanels(gAcid1) then
2208 if not checkPanels(gAcid2) then exit;
2209 result := texid;
2210 end;
2213 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2214 const
2215 SlowMask = GridTagLift or GridTagBlockMon;
2216 function checker (pan: TPanel; tag: Integer): Boolean;
2217 begin
2219 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2220 begin
2221 result := pan.Enabled;
2222 exit;
2223 end;
2226 if ((tag and GridTagLift) <> 0) then
2227 begin
2228 result :=
2229 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2230 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2231 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2232 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2233 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2234 exit;
2235 end;
2237 if ((tag and GridTagBlockMon) <> 0) then
2238 begin
2239 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2240 exit;
2241 end;
2243 // other shit
2244 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2245 result := true; // i found her!
2246 end;
2248 var
2249 tagmask: Integer = 0;
2250 begin
2251 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2252 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2253 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2254 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2255 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2256 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2257 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2259 if (tagmask = 0) then begin result := false; exit; end; // just in case
2261 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2262 if gdbg_map_use_accel_coldet then
2263 begin
2264 {if gdbg_map_use_tree_coldet then
2265 begin
2266 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2267 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2268 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2269 begin
2270 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2271 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2272 end;
2273 end
2274 else}
2275 begin
2276 if (Width = 1) and (Height = 1) then
2277 begin
2278 if ((tagmask and SlowMask) <> 0) then
2279 begin
2280 // slow
2281 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2282 end
2283 else
2284 begin
2285 // fast
2286 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2287 end;
2288 end
2289 else
2290 begin
2291 if ((tagmask and SlowMask) <> 0) then
2292 begin
2293 // slow
2294 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2295 end
2296 else
2297 begin
2298 // fast
2299 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2300 end;
2301 end;
2302 end;
2303 end
2304 else
2305 begin
2306 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2307 end;
2308 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2309 end;
2312 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2313 var
2314 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2315 texid: DWORD;
2317 // slightly different from the old code, but meh...
2318 function checker (pan: TPanel; tag: Integer): Boolean;
2319 begin
2320 result := false; // don't stop, ever
2321 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2322 // check priorities
2323 case cctype of
2324 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2325 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2326 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2327 end;
2328 // collision?
2329 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2330 // yeah
2331 texid := pan.GetTextureID();
2332 // water? water has the highest priority, so stop right here
2333 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2334 // acid2?
2335 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2336 // acid1?
2337 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2338 end;
2340 begin
2341 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2342 if gdbg_map_use_accel_coldet then
2343 begin
2344 texid := TEXTURE_NONE;
2345 {if gdbg_map_use_tree_coldet then
2346 begin
2347 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2348 end
2349 else}
2350 begin
2351 if (Width = 1) and (Height = 1) then
2352 begin
2353 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2354 end
2355 else
2356 begin
2357 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2358 end;
2359 end;
2360 result := texid;
2361 end
2362 else
2363 begin
2364 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2365 end;
2366 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2367 end;
2370 procedure g_Map_EnableWall(ID: DWORD);
2371 var
2372 pan: TPanel;
2373 begin
2374 pan := gWalls[ID];
2375 pan.Enabled := True;
2376 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2378 mapGrid.proxyEnabled[pan.proxyId] := true;
2379 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2380 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2382 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2384 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2385 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);
2386 {$ENDIF}
2387 end;
2389 procedure g_Map_DisableWall(ID: DWORD);
2390 var
2391 pan: TPanel;
2392 begin
2393 pan := gWalls[ID];
2394 pan.Enabled := False;
2395 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2397 mapGrid.proxyEnabled[pan.proxyId] := false;
2398 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2400 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2402 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2403 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);
2404 {$ENDIF}
2405 end;
2407 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2408 var
2409 tp: TPanel;
2410 begin
2411 case PanelType of
2412 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2413 tp := gWalls[ID];
2414 PANEL_FORE:
2415 tp := gRenderForegrounds[ID];
2416 PANEL_BACK:
2417 tp := gRenderBackgrounds[ID];
2418 PANEL_WATER:
2419 tp := gWater[ID];
2420 PANEL_ACID1:
2421 tp := gAcid1[ID];
2422 PANEL_ACID2:
2423 tp := gAcid2[ID];
2424 PANEL_STEP:
2425 tp := gSteps[ID];
2426 else
2427 Exit;
2428 end;
2430 tp.NextTexture(AnimLoop);
2431 if g_Game_IsServer and g_Game_IsNet then
2432 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2433 end;
2435 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2436 begin
2437 if gLifts[ID].LiftType = t then
2438 Exit;
2440 with gLifts[ID] do
2441 begin
2442 LiftType := t;
2444 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2445 //TODO: make separate lift tags, and change tag here
2447 if LiftType = 0 then
2448 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2449 else if LiftType = 1 then
2450 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2451 else if LiftType = 2 then
2452 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2453 else if LiftType = 3 then
2454 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2456 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2457 end;
2458 end;
2460 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2461 var
2462 a: Integer;
2463 PointsArray: Array of TRespawnPoint;
2464 begin
2465 Result := False;
2466 SetLength(PointsArray, 0);
2468 if RespawnPoints = nil then
2469 Exit;
2471 for a := 0 to High(RespawnPoints) do
2472 if RespawnPoints[a].PointType = PointType then
2473 begin
2474 SetLength(PointsArray, Length(PointsArray)+1);
2475 PointsArray[High(PointsArray)] := RespawnPoints[a];
2476 end;
2478 if PointsArray = nil then
2479 Exit;
2481 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2482 Result := True;
2483 end;
2485 function g_Map_GetPointCount(PointType: Byte): Word;
2486 var
2487 a: Integer;
2488 begin
2489 Result := 0;
2491 if RespawnPoints = nil then
2492 Exit;
2494 for a := 0 to High(RespawnPoints) do
2495 if RespawnPoints[a].PointType = PointType then
2496 Result := Result + 1;
2497 end;
2499 function g_Map_HaveFlagPoints(): Boolean;
2500 begin
2501 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2502 end;
2504 procedure g_Map_ResetFlag(Flag: Byte);
2505 begin
2506 with gFlags[Flag] do
2507 begin
2508 Obj.X := -1000;
2509 Obj.Y := -1000;
2510 Obj.Vel.X := 0;
2511 Obj.Vel.Y := 0;
2512 Direction := D_LEFT;
2513 State := FLAG_STATE_NONE;
2514 if FlagPoints[Flag] <> nil then
2515 begin
2516 Obj.X := FlagPoints[Flag]^.X;
2517 Obj.Y := FlagPoints[Flag]^.Y;
2518 Direction := FlagPoints[Flag]^.Direction;
2519 State := FLAG_STATE_NORMAL;
2520 end;
2521 Count := -1;
2522 end;
2523 end;
2525 procedure g_Map_DrawFlags();
2526 var
2527 i, dx: Integer;
2528 Mirror: TMirrorType;
2529 begin
2530 if gGameSettings.GameMode <> GM_CTF then
2531 Exit;
2533 for i := FLAG_RED to FLAG_BLUE do
2534 with gFlags[i] do
2535 if State <> FLAG_STATE_CAPTURED then
2536 begin
2537 if State = FLAG_STATE_NONE then
2538 continue;
2540 if Direction = D_LEFT then
2541 begin
2542 Mirror := M_HORIZONTAL;
2543 dx := -1;
2544 end
2545 else
2546 begin
2547 Mirror := M_NONE;
2548 dx := 1;
2549 end;
2551 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2553 if g_debug_Frames then
2554 begin
2555 e_DrawQuad(Obj.X+Obj.Rect.X,
2556 Obj.Y+Obj.Rect.Y,
2557 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2558 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2559 0, 255, 0);
2560 end;
2561 end;
2562 end;
2564 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2565 var
2566 dw: DWORD;
2567 b: Byte;
2568 str: String;
2569 boo: Boolean;
2571 procedure SavePanelArray(var panels: TPanelArray);
2572 var
2573 PAMem: TBinMemoryWriter;
2574 i: Integer;
2575 begin
2576 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2577 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2579 i := 0;
2580 while i < Length(panels) do
2581 begin
2582 if panels[i].SaveIt then
2583 begin
2584 // ID ïàíåëè:
2585 PAMem.WriteInt(i);
2586 // Ñîõðàíÿåì ïàíåëü:
2587 panels[i].SaveState(PAMem);
2588 end;
2589 Inc(i);
2590 end;
2592 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2593 PAMem.SaveToMemory(Mem);
2594 PAMem.Free();
2595 end;
2597 procedure SaveFlag(flag: PFlag);
2598 begin
2599 // Ñèãíàòóðà ôëàãà:
2600 dw := FLAG_SIGNATURE; // 'FLAG'
2601 Mem.WriteDWORD(dw);
2602 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2603 Mem.WriteByte(flag^.RespawnType);
2604 // Ñîñòîÿíèå ôëàãà:
2605 Mem.WriteByte(flag^.State);
2606 // Íàïðàâëåíèå ôëàãà:
2607 if flag^.Direction = D_LEFT then
2608 b := 1
2609 else // D_RIGHT
2610 b := 2;
2611 Mem.WriteByte(b);
2612 // Îáúåêò ôëàãà:
2613 Obj_SaveState(@flag^.Obj, Mem);
2614 end;
2616 begin
2617 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2619 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2620 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2621 SavePanelArray(gWalls);
2622 // Ñîõðàíÿåì ïàíåëè ôîíà:
2623 SavePanelArray(gRenderBackgrounds);
2624 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2625 SavePanelArray(gRenderForegrounds);
2626 // Ñîõðàíÿåì ïàíåëè âîäû:
2627 SavePanelArray(gWater);
2628 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2629 SavePanelArray(gAcid1);
2630 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2631 SavePanelArray(gAcid2);
2632 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2633 SavePanelArray(gSteps);
2634 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2635 SavePanelArray(gLifts);
2636 ///// /////
2638 ///// Ñîõðàíÿåì ìóçûêó: /////
2639 // Ñèãíàòóðà ìóçûêè:
2640 dw := MUSIC_SIGNATURE; // 'MUSI'
2641 Mem.WriteDWORD(dw);
2642 // Íàçâàíèå ìóçûêè:
2643 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2644 if gMusic.NoMusic then
2645 str := ''
2646 else
2647 str := gMusic.Name;
2648 Mem.WriteString(str, 64);
2649 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2650 dw := gMusic.GetPosition();
2651 Mem.WriteDWORD(dw);
2652 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2653 boo := gMusic.SpecPause;
2654 Mem.WriteBoolean(boo);
2655 ///// /////
2657 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2658 Mem.WriteInt(gTotalMonsters);
2659 ///// /////
2661 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2662 if gGameSettings.GameMode = GM_CTF then
2663 begin
2664 // Ôëàã Êðàñíîé êîìàíäû:
2665 SaveFlag(@gFlags[FLAG_RED]);
2666 // Ôëàã Ñèíåé êîìàíäû:
2667 SaveFlag(@gFlags[FLAG_BLUE]);
2668 end;
2669 ///// /////
2671 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2672 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2673 begin
2674 // Î÷êè Êðàñíîé êîìàíäû:
2675 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2676 // Î÷êè Ñèíåé êîìàíäû:
2677 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2678 end;
2679 ///// /////
2680 end;
2682 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2683 var
2684 dw: DWORD;
2685 b: Byte;
2686 str: String;
2687 boo: Boolean;
2689 procedure LoadPanelArray(var panels: TPanelArray);
2690 var
2691 PAMem: TBinMemoryReader;
2692 i, id: Integer;
2693 begin
2694 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2695 PAMem := TBinMemoryReader.Create();
2696 PAMem.LoadFromMemory(Mem);
2698 for i := 0 to Length(panels)-1 do
2699 if panels[i].SaveIt then
2700 begin
2701 // ID ïàíåëè:
2702 PAMem.ReadInt(id);
2703 if id <> i then
2704 begin
2705 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2706 end;
2707 // Çàãðóæàåì ïàíåëü:
2708 panels[i].LoadState(PAMem);
2709 end;
2711 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2712 PAMem.Free();
2713 end;
2715 procedure LoadFlag(flag: PFlag);
2716 begin
2717 // Ñèãíàòóðà ôëàãà:
2718 Mem.ReadDWORD(dw);
2719 if dw <> FLAG_SIGNATURE then // 'FLAG'
2720 begin
2721 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2722 end;
2723 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2724 Mem.ReadByte(flag^.RespawnType);
2725 // Ñîñòîÿíèå ôëàãà:
2726 Mem.ReadByte(flag^.State);
2727 // Íàïðàâëåíèå ôëàãà:
2728 Mem.ReadByte(b);
2729 if b = 1 then
2730 flag^.Direction := D_LEFT
2731 else // b = 2
2732 flag^.Direction := D_RIGHT;
2733 // Îáúåêò ôëàãà:
2734 Obj_LoadState(@flag^.Obj, Mem);
2735 end;
2737 begin
2738 if Mem = nil then
2739 Exit;
2741 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2742 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2743 LoadPanelArray(gWalls);
2744 // Çàãðóæàåì ïàíåëè ôîíà:
2745 LoadPanelArray(gRenderBackgrounds);
2746 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2747 LoadPanelArray(gRenderForegrounds);
2748 // Çàãðóæàåì ïàíåëè âîäû:
2749 LoadPanelArray(gWater);
2750 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2751 LoadPanelArray(gAcid1);
2752 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2753 LoadPanelArray(gAcid2);
2754 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2755 LoadPanelArray(gSteps);
2756 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2757 LoadPanelArray(gLifts);
2758 ///// /////
2760 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2761 g_GFX_Init();
2762 mapCreateGrid();
2764 ///// Çàãðóæàåì ìóçûêó: /////
2765 // Ñèãíàòóðà ìóçûêè:
2766 Mem.ReadDWORD(dw);
2767 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2768 begin
2769 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2770 end;
2771 // Íàçâàíèå ìóçûêè:
2772 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2773 Mem.ReadString(str);
2774 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2775 Mem.ReadDWORD(dw);
2776 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2777 Mem.ReadBoolean(boo);
2778 // Çàïóñêàåì ýòó ìóçûêó:
2779 gMusic.SetByName(str);
2780 gMusic.SpecPause := boo;
2781 gMusic.Play();
2782 gMusic.Pause(True);
2783 gMusic.SetPosition(dw);
2784 ///// /////
2786 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2787 Mem.ReadInt(gTotalMonsters);
2788 ///// /////
2790 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2791 if gGameSettings.GameMode = GM_CTF then
2792 begin
2793 // Ôëàã Êðàñíîé êîìàíäû:
2794 LoadFlag(@gFlags[FLAG_RED]);
2795 // Ôëàã Ñèíåé êîìàíäû:
2796 LoadFlag(@gFlags[FLAG_BLUE]);
2797 end;
2798 ///// /////
2800 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2801 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2802 begin
2803 // Î÷êè Êðàñíîé êîìàíäû:
2804 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2805 // Î÷êè Ñèíåé êîìàíäû:
2806 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2807 end;
2808 ///// /////
2809 end;
2811 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2812 var
2813 Arr: TPanelArray;
2814 begin
2815 Result := nil;
2816 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2817 Arr := PanelByID[PanelID].PWhere^;
2818 PanelArrayID := PanelByID[PanelID].PArrID;
2819 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2820 end;
2823 // trace liquid, stepping by `dx` and `dy`
2824 // return last seen liquid coords, and `false` if we're started outside of the liquid
2825 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2826 const
2827 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2828 begin
2829 topx := x;
2830 topy := y;
2831 // started outside of the liquid?
2832 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2833 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2834 result := true;
2835 while true do
2836 begin
2837 Inc(x, dx);
2838 Inc(y, dy);
2839 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2840 topx := x;
2841 topy := y;
2842 end;
2843 end;
2846 end.