DEADSOFTWARE

better hitscan tracer; no more level trace bitmap (but no more particles too, alas)
[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 unit g_map;
19 interface
21 uses
22 e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
23 g_phys, wadreader, BinEditor, g_panel, g_grid, z_aabbtree, md5, binheap, xprofiler;
25 type
26 TMapInfo = record
27 Map: String;
28 Name: String;
29 Description: String;
30 Author: String;
31 MusicName: String;
32 SkyName: String;
33 Height: Word;
34 Width: Word;
35 end;
37 PRespawnPoint = ^TRespawnPoint;
38 TRespawnPoint = record
39 X, Y: Integer;
40 Direction: TDirection;
41 PointType: Byte;
42 end;
44 PFlagPoint = ^TFlagPoint;
45 TFlagPoint = TRespawnPoint;
47 PFlag = ^TFlag;
48 TFlag = record
49 Obj: TObj;
50 RespawnType: Byte;
51 State: Byte;
52 Count: Integer;
53 CaptureTime: LongWord;
54 Animation: TAnimation;
55 Direction: TDirection;
56 end;
58 function g_Map_Load(Res: String): Boolean;
59 function g_Map_GetMapInfo(Res: String): TMapInfo;
60 function g_Map_GetMapsList(WADName: String): SArray;
61 function g_Map_Exist(Res: String): Boolean;
62 procedure g_Map_Free();
63 procedure g_Map_Update();
65 procedure g_Map_DrawPanels (PanelType: Word); // unaccelerated
66 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
68 procedure g_Map_DrawBack(dx, dy: Integer);
69 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
70 PanelType: Word; b1x3: Boolean): Boolean;
71 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
72 procedure g_Map_EnableWall(ID: DWORD);
73 procedure g_Map_DisableWall(ID: DWORD);
74 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
75 procedure g_Map_SetLift(ID: DWORD; t: Integer);
76 procedure g_Map_ReAdd_DieTriggers();
77 function g_Map_IsSpecialTexture(Texture: String): Boolean;
79 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
80 function g_Map_GetPointCount(PointType: Byte): Word;
82 function g_Map_HaveFlagPoints(): Boolean;
84 procedure g_Map_ResetFlag(Flag: Byte);
85 procedure g_Map_DrawFlags();
87 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
89 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
90 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
92 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
94 procedure g_Map_ProfilersBegin ();
95 procedure g_Map_ProfilersEnd ();
97 const
98 RESPAWNPOINT_PLAYER1 = 1;
99 RESPAWNPOINT_PLAYER2 = 2;
100 RESPAWNPOINT_DM = 3;
101 RESPAWNPOINT_RED = 4;
102 RESPAWNPOINT_BLUE = 5;
104 FLAG_NONE = 0;
105 FLAG_RED = 1;
106 FLAG_BLUE = 2;
107 FLAG_DOM = 3;
109 FLAG_STATE_NONE = 0;
110 FLAG_STATE_NORMAL = 1;
111 FLAG_STATE_DROPPED = 2;
112 FLAG_STATE_CAPTURED = 3;
113 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
114 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
116 FLAG_TIME = 720; // 20 seconds
118 SKY_STRETCH: Single = 1.5;
120 const
121 GridTagInvalid = 0;
123 (* draw order:
124 PANEL_BACK
125 PANEL_STEP
126 PANEL_WALL
127 PANEL_CLOSEDOOR
128 PANEL_ACID1
129 PANEL_ACID2
130 PANEL_WATER
131 PANEL_FORE
132 *)
133 // sorted by draw priority
134 GridTagBack = 1 shl 0;
135 GridTagStep = 1 shl 1;
136 GridTagWall = 1 shl 2;
137 GridTagDoor = 1 shl 3;
138 GridTagAcid1 = 1 shl 4;
139 GridTagAcid2 = 1 shl 5;
140 GridTagWater = 1 shl 6;
141 GridTagFore = 1 shl 7;
142 // the following are invisible
143 GridTagLift = 1 shl 8;
144 GridTagBlockMon = 1 shl 9;
147 var
148 gWalls: TPanelArray;
149 gRenderBackgrounds: TPanelArray;
150 gRenderForegrounds: TPanelArray;
151 gWater, gAcid1, gAcid2: TPanelArray;
152 gSteps: TPanelArray;
153 gLifts: TPanelArray;
154 gBlockMon: TPanelArray;
155 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
156 //gDOMFlags: array of TFlag;
157 gMapInfo: TMapInfo;
158 gBackSize: TPoint;
159 gDoorMap: array of array of DWORD;
160 gLiftMap: array of array of DWORD;
161 gWADHash: TMD5Digest;
162 BackID: DWORD = DWORD(-1);
163 gExternalResources: TStringList;
165 gdbg_map_use_accel_render: Boolean = true;
166 gdbg_map_use_accel_coldet: Boolean = true;
167 gdbg_map_use_tree_draw: Boolean = false;
168 gdbg_map_use_tree_coldet: Boolean = false;
169 gdbg_map_dump_coldet_tree_queries: Boolean = false;
170 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
171 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
173 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
175 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
178 implementation
180 uses
181 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
182 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
183 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
184 Math, g_monsters, g_saveload, g_language, g_netmsg,
185 utils, sfs,
186 ImagingTypes, Imaging, ImagingUtility,
187 ImagingGif, ImagingNetworkGraphics;
189 const
190 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
191 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
192 FLAG_SIGNATURE = $47414C46; // 'FLAG'
195 type
196 TPanelGrid = specialize TBodyGridBase<TPanel>;
198 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
200 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
201 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
202 end;
204 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
205 begin
206 result := false;
207 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
208 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
209 if (pan.Width < 1) or (pan.Height < 1) then exit;
210 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
211 result := true;
212 end;
215 function panelTypeToTag (panelType: Word): Integer;
216 begin
217 case panelType of
218 PANEL_WALL: result := GridTagWall; // gWalls
219 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
220 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
221 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
222 PANEL_WATER: result := GridTagWater; // gWater
223 PANEL_ACID1: result := GridTagAcid1; // gAcid1
224 PANEL_ACID2: result := GridTagAcid2; // gAcid2
225 PANEL_STEP: result := GridTagStep; // gSteps
226 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
227 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
228 else result := GridTagInvalid;
229 end;
230 end;
233 function dplLess (a, b: TObject): Boolean;
234 var
235 pa, pb: TPanel;
236 begin
237 //result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
238 pa := TPanel(a);
239 pb := TPanel(b);
240 if (pa.tag < pb.tag) then begin result := true; exit; end;
241 if (pa.tag > pb.tag) then begin result := false; exit; end;
242 result := (pa.ArrIdx < pb.ArrIdx);
243 end;
245 procedure dplClear ();
246 begin
247 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
248 end;
251 type
252 TPanelID = record
253 PWhere: ^TPanelArray;
254 PArrID: Integer;
255 end;
257 var
258 PanelById: array of TPanelID;
259 Textures: TLevelTextureArray;
260 RespawnPoints: Array of TRespawnPoint;
261 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
262 //DOMFlagPoints: Array of TFlagPoint;
263 gMapGrid: TPanelGrid = nil;
264 mapTree: TDynAABBTreeMap = nil;
267 procedure g_Map_ProfilersBegin ();
268 begin
269 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
270 profMapCollision.mainBegin(g_profile_collision);
271 // create sections
272 if g_profile_collision then
273 begin
274 profMapCollision.sectionBegin('*solids');
275 profMapCollision.sectionEnd();
276 profMapCollision.sectionBegin('liquids');
277 profMapCollision.sectionEnd();
278 end;
279 end;
281 procedure g_Map_ProfilersEnd ();
282 begin
283 if (profMapCollision <> nil) then profMapCollision.mainEnd();
284 end;
287 // wall index in `gWalls` or -1
288 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
289 var
290 maxDistSq: Single;
292 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
293 var
294 aabb: AABB2D;
295 tmin: Single;
296 begin
297 result := -666.0; // invalid
298 if not pan.Enabled then exit;
299 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
300 if not aabb.valid then exit;
301 if aabb.intersects(ray, @tmin) then
302 begin
303 //if (tmin*tmin > maxDistSq) then exit;
304 if (tmin >= 0.0) then
305 begin
306 //e_WriteLog(Format('sqchecker(%d,%d,%d,%d): panel #%d (%d,%d)-(%d,%d); tmin=%f', [x0, y0, x1, y1, pan.arrIdx, pan.X, pan.Y, pan.Width, pan.Height, tmin]), MSG_NOTIFY);
307 //if (tmin < 0.0) then tmin := 0.0;
308 result := tmin;
309 end;
310 end;
311 end;
313 var
314 qr: TDynAABBTreeMap.TSegmentQueryResult;
315 ray: Ray2D;
316 hxf, hyf: Single;
317 hx, hy: Integer;
318 begin
319 result := -1;
320 if (mapTree = nil) then exit;
321 maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
322 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
323 begin
324 if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
325 begin
326 result := qr.flesh.arrIdx;
327 if (hitx <> nil) or (hity <> nil) then
328 begin
329 ray := Ray2D.Create(x0, y0, x1, y1);
330 hxf := ray.origX+ray.dirX*qr.time;
331 hyf := ray.origY+ray.dirY*qr.time;
332 while true do
333 begin
334 hx := trunc(hxf);
335 hy := trunc(hyf);
336 if (hx >= qr.flesh.X) and (hy >= qr.flesh.Y) and (hx < qr.flesh.X+qr.flesh.Width) and (hy < qr.flesh.Y+qr.flesh.Height) then
337 begin
338 // go back a little
339 hxf -= ray.dirX;
340 hyf -= ray.dirY;
341 end
342 else
343 begin
344 break;
345 end;
346 end;
347 if (hitx <> nil) then hitx^ := hx;
348 if (hity <> nil) then hity^ := hy;
349 end;
350 end;
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].tag := panelTypeToTag(PanelRec.PanelType);
565 if sav then
566 panels^[len].SaveIt := True;
568 Result := len;
570 len := Length(PanelByID);
571 SetLength(PanelByID, len + 1);
572 PanelByID[len].PWhere := panels;
573 PanelByID[len].PArrID := Result;
574 end;
576 function CreateNullTexture(RecName: String): Integer;
577 begin
578 SetLength(Textures, Length(Textures)+1);
579 result := High(Textures);
581 with Textures[High(Textures)] do
582 begin
583 TextureName := RecName;
584 Width := 1;
585 Height := 1;
586 Anim := False;
587 TextureID := TEXTURE_NONE;
588 end;
589 end;
591 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
592 var
593 WAD: TWADFile;
594 TextureData: Pointer;
595 WADName, txname: String;
596 a, ResLength: Integer;
597 begin
598 Result := -1;
600 if Textures <> nil then
601 for a := 0 to High(Textures) do
602 if Textures[a].TextureName = RecName then
603 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
604 Result := a;
605 Exit;
606 end;
608 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
609 if (RecName = TEXTURE_NAME_WATER) or
610 (RecName = TEXTURE_NAME_ACID1) or
611 (RecName = TEXTURE_NAME_ACID2) then
612 begin
613 SetLength(Textures, Length(Textures)+1);
615 with Textures[High(Textures)] do
616 begin
617 TextureName := RecName;
619 if TextureName = TEXTURE_NAME_WATER then
620 TextureID := TEXTURE_SPECIAL_WATER
621 else
622 if TextureName = TEXTURE_NAME_ACID1 then
623 TextureID := TEXTURE_SPECIAL_ACID1
624 else
625 if TextureName = TEXTURE_NAME_ACID2 then
626 TextureID := TEXTURE_SPECIAL_ACID2;
628 Anim := False;
629 end;
631 result := High(Textures);
632 Exit;
633 end;
635 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
636 WADName := g_ExtractWadName(RecName);
638 WAD := TWADFile.Create();
640 if WADName <> '' then
641 WADName := GameDir+'/wads/'+WADName
642 else
643 WADName := Map;
645 WAD.ReadFile(WADName);
647 txname := RecName;
649 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
650 begin
651 FreeMem(TextureData);
652 RecName := 'COMMON\ALIEN';
653 end;
656 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
657 begin
658 SetLength(Textures, Length(Textures)+1);
659 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
660 Exit;
661 e_GetTextureSize(Textures[High(Textures)].TextureID,
662 @Textures[High(Textures)].Width,
663 @Textures[High(Textures)].Height);
664 FreeMem(TextureData);
665 Textures[High(Textures)].TextureName := {RecName}txname;
666 Textures[High(Textures)].Anim := False;
668 result := High(Textures);
669 end
670 else // Íåò òàêîãî ðåóñðñà â WAD'å
671 begin
672 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
673 if log then
674 begin
675 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
676 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
677 end;
678 end;
680 WAD.Free();
681 end;
683 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
684 var
685 WAD: TWADFile;
686 TextureWAD: PChar = nil;
687 TextData: Pointer = nil;
688 TextureData: Pointer = nil;
689 cfg: TConfig = nil;
690 WADName: String;
691 ResLength: Integer;
692 TextureResource: String;
693 _width, _height, _framecount, _speed: Integer;
694 _backanimation: Boolean;
695 //imgfmt: string;
696 ia: TDynImageDataArray = nil;
697 f, c, frdelay, frloop: Integer;
698 begin
699 result := -1;
701 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
703 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
704 WADName := g_ExtractWadName(RecName);
706 WAD := TWADFile.Create();
707 try
708 if WADName <> '' then
709 WADName := GameDir+'/wads/'+WADName
710 else
711 WADName := Map;
713 WAD.ReadFile(WADName);
715 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
716 begin
717 if log then
718 begin
719 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
720 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
721 end;
722 exit;
723 end;
725 {TEST
726 if WADName = Map then
727 begin
728 //FreeMem(TextureWAD);
729 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
730 end;
733 WAD.FreeWAD();
735 if ResLength < 6 then
736 begin
737 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
738 exit;
739 end;
741 // ýòî ïòèöà? ýòî ñàìîë¸ò?
742 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
743 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
744 begin
745 // íåò, ýòî ñóïåðìåí!
746 if not WAD.ReadMemory(TextureWAD, ResLength) then
747 begin
748 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
749 exit;
750 end;
752 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
753 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
754 begin
755 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
756 exit;
757 end;
759 cfg := TConfig.CreateMem(TextData, ResLength);
761 TextureResource := cfg.ReadStr('', 'resource', '');
762 if TextureResource = '' then
763 begin
764 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
765 exit;
766 end;
768 _width := cfg.ReadInt('', 'framewidth', 0);
769 _height := cfg.ReadInt('', 'frameheight', 0);
770 _framecount := cfg.ReadInt('', 'framecount', 0);
771 _speed := cfg.ReadInt('', 'waitcount', 0);
772 _backanimation := cfg.ReadBool('', 'backanimation', False);
774 cfg.Free();
775 cfg := nil;
777 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
778 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
779 begin
780 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
781 exit;
782 end;
784 WAD.Free();
785 WAD := nil;
787 SetLength(Textures, Length(Textures)+1);
788 with Textures[High(Textures)] do
789 begin
790 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
791 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
792 begin
793 TextureName := RecName;
794 Width := _width;
795 Height := _height;
796 Anim := True;
797 FramesCount := _framecount;
798 Speed := _speed;
799 result := High(Textures);
800 end
801 else
802 begin
803 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
804 end;
805 end;
806 end
807 else
808 begin
809 // try animated image
811 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
812 if length(imgfmt) = 0 then
813 begin
814 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
815 exit;
816 end;
818 GlobalMetadata.ClearMetaItems();
819 GlobalMetadata.ClearMetaItemsForSaving();
820 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
821 begin
822 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
823 exit;
824 end;
825 if length(ia) = 0 then
826 begin
827 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
828 exit;
829 end;
831 WAD.Free();
832 WAD := nil;
834 _width := ia[0].width;
835 _height := ia[0].height;
836 _framecount := length(ia);
837 _speed := 1;
838 _backanimation := false;
839 frdelay := -1;
840 frloop := -666;
841 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
842 begin
843 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
844 try
845 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
846 frdelay := f;
847 if f < 0 then f := 0;
848 // rounding ;-)
849 c := f mod 28;
850 if c < 13 then c := 0 else c := 1;
851 f := (f div 28)+c;
852 if f < 1 then f := 1 else if f > 255 then f := 255;
853 _speed := f;
854 except
855 end;
856 end;
857 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
858 begin
859 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
860 try
861 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
862 frloop := f;
863 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
864 except
865 end;
866 end;
867 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
868 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
869 f := ord(_backanimation);
870 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);
872 SetLength(Textures, Length(Textures)+1);
873 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
874 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
875 begin
876 Textures[High(Textures)].TextureName := RecName;
877 Textures[High(Textures)].Width := _width;
878 Textures[High(Textures)].Height := _height;
879 Textures[High(Textures)].Anim := True;
880 Textures[High(Textures)].FramesCount := length(ia);
881 Textures[High(Textures)].Speed := _speed;
882 result := High(Textures);
883 //writeln(' CREATED!');
884 end
885 else
886 begin
887 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
888 end;
889 end;
890 finally
891 for f := 0 to High(ia) do FreeImage(ia[f]);
892 WAD.Free();
893 cfg.Free();
894 if TextureWAD <> nil then FreeMem(TextureWAD);
895 if TextData <> nil then FreeMem(TextData);
896 if TextureData <> nil then FreeMem(TextureData);
897 end;
898 end;
900 procedure CreateItem(Item: TItemRec_1);
901 begin
902 if g_Game_IsClient then Exit;
904 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
905 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
906 Exit;
908 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
909 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
910 end;
912 procedure CreateArea(Area: TAreaRec_1);
913 var
914 a: Integer;
915 id: DWORD;
916 begin
917 case Area.AreaType of
918 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
919 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
920 begin
921 SetLength(RespawnPoints, Length(RespawnPoints)+1);
922 with RespawnPoints[High(RespawnPoints)] do
923 begin
924 X := Area.X;
925 Y := Area.Y;
926 Direction := TDirection(Area.Direction);
928 case Area.AreaType of
929 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
930 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
931 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
932 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
933 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
934 end;
935 end;
936 end;
938 AREA_REDFLAG, AREA_BLUEFLAG:
939 begin
940 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
942 if FlagPoints[a] <> nil then Exit;
944 New(FlagPoints[a]);
946 with FlagPoints[a]^ do
947 begin
948 X := Area.X-FLAGRECT.X;
949 Y := Area.Y-FLAGRECT.Y;
950 Direction := TDirection(Area.Direction);
951 end;
953 with gFlags[a] do
954 begin
955 case a of
956 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
957 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
958 end;
960 Animation := TAnimation.Create(id, True, 8);
961 Obj.Rect := FLAGRECT;
963 g_Map_ResetFlag(a);
964 end;
965 end;
967 AREA_DOMFLAG:
968 begin
969 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
970 with DOMFlagPoints[High(DOMFlagPoints)] do
971 begin
972 X := Area.X;
973 Y := Area.Y;
974 Direction := TDirection(Area.Direction);
975 end;
977 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
978 end;
979 end;
980 end;
982 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
983 var
984 _trigger: TTrigger;
985 begin
986 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
988 with _trigger do
989 begin
990 X := Trigger.X;
991 Y := Trigger.Y;
992 Width := Trigger.Width;
993 Height := Trigger.Height;
994 Enabled := ByteBool(Trigger.Enabled);
995 TexturePanel := Trigger.TexturePanel;
996 TexturePanelType := fTexturePanel1Type;
997 ShotPanelType := fTexturePanel2Type;
998 TriggerType := Trigger.TriggerType;
999 ActivateType := Trigger.ActivateType;
1000 Keys := Trigger.Keys;
1001 Data.Default := Trigger.DATA;
1002 end;
1004 g_Triggers_Create(_trigger);
1005 end;
1007 procedure CreateMonster(monster: TMonsterRec_1);
1008 var
1009 a: Integer;
1010 mon: TMonster;
1011 begin
1012 if g_Game_IsClient then Exit;
1014 if (gGameSettings.GameType = GT_SINGLE)
1015 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1016 begin
1017 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1019 if gTriggers <> nil then
1020 begin
1021 for a := 0 to High(gTriggers) do
1022 begin
1023 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1024 begin
1025 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1026 end;
1027 end;
1028 end;
1030 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1031 end;
1032 end;
1034 procedure g_Map_ReAdd_DieTriggers();
1036 function monsDieTrig (mon: TMonster): Boolean;
1037 var
1038 a: Integer;
1039 begin
1040 result := false; // don't stop
1041 mon.ClearTriggers();
1042 for a := 0 to High(gTriggers) do
1043 begin
1044 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1045 begin
1046 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1047 end;
1048 end;
1049 end;
1051 begin
1052 if g_Game_IsClient then Exit;
1054 g_Mons_ForEach(monsDieTrig);
1055 end;
1057 function extractWadName(resourceName: string): string;
1058 var
1059 posN: Integer;
1060 begin
1061 posN := Pos(':', resourceName);
1062 if posN > 0 then
1063 Result:= Copy(resourceName, 0, posN-1)
1064 else
1065 Result := '';
1066 end;
1068 procedure addResToExternalResList(res: string);
1069 begin
1070 res := extractWadName(res);
1071 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1072 gExternalResources.Add(res);
1073 end;
1075 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1076 var
1077 textures: TTexturesRec1Array;
1078 mapHeader: TMapHeaderRec_1;
1079 i: integer;
1080 resFile: String = '';
1081 begin
1082 if gExternalResources = nil then
1083 gExternalResources := TStringList.Create;
1085 gExternalResources.Clear;
1086 textures := mapReader.GetTextures();
1087 for i := 0 to High(textures) do
1088 begin
1089 addResToExternalResList(resFile);
1090 end;
1092 textures := nil;
1094 mapHeader := mapReader.GetMapHeader;
1096 addResToExternalResList(mapHeader.MusicName);
1097 addResToExternalResList(mapHeader.SkyName);
1098 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 gMapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1136 mapTree.insertObject(pan, tag, true); // as static object
1137 end;
1138 end;
1140 begin
1141 gMapGrid.Free();
1142 gMapGrid := nil;
1143 mapTree.Free();
1144 mapTree := nil;
1146 calcBoundingBox(gWalls);
1147 calcBoundingBox(gRenderBackgrounds);
1148 calcBoundingBox(gRenderForegrounds);
1149 calcBoundingBox(gWater);
1150 calcBoundingBox(gAcid1);
1151 calcBoundingBox(gAcid2);
1152 calcBoundingBox(gSteps);
1153 calcBoundingBox(gLifts);
1154 calcBoundingBox(gBlockMon);
1156 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1158 gMapGrid := TPanelGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1159 mapTree := TDynAABBTreeMap.Create();
1161 addPanelsToGrid(gWalls, PANEL_WALL);
1162 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1163 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1164 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1165 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1166 addPanelsToGrid(gWater, PANEL_WATER);
1167 addPanelsToGrid(gAcid1, PANEL_ACID1);
1168 addPanelsToGrid(gAcid2, PANEL_ACID2);
1169 addPanelsToGrid(gSteps, PANEL_STEP);
1170 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1171 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1173 gMapGrid.dumpStats();
1174 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1175 mapTree.forEachLeaf(nil);
1176 end;
1178 function g_Map_Load(Res: String): Boolean;
1179 const
1180 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1181 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1182 var
1183 WAD: TWADFile;
1184 MapReader: TMapReader_1;
1185 Header: TMapHeaderRec_1;
1186 _textures: TTexturesRec1Array;
1187 _texnummap: array of Integer; // `_textures` -> `Textures`
1188 panels: TPanelsRec1Array;
1189 items: TItemsRec1Array;
1190 monsters: TMonsterRec1Array;
1191 areas: TAreasRec1Array;
1192 triggers: TTriggersRec1Array;
1193 a, b, c, k: Integer;
1194 PanelID: DWORD;
1195 AddTextures: TAddTextureArray;
1196 texture: TTextureRec_1;
1197 TriggersTable: Array of record
1198 TexturePanel: Integer;
1199 LiftPanel: Integer;
1200 DoorPanel: Integer;
1201 ShotPanel: Integer;
1202 end;
1203 FileName, mapResName, s, TexName: String;
1204 Data: Pointer;
1205 Len: Integer;
1206 ok, isAnim, trigRef: Boolean;
1207 CurTex, ntn: Integer;
1209 begin
1210 gMapGrid.Free();
1211 gMapGrid := nil;
1212 mapTree.Free();
1213 mapTree := nil;
1215 Result := False;
1216 gMapInfo.Map := Res;
1217 TriggersTable := nil;
1218 FillChar(texture, SizeOf(texture), 0);
1220 sfsGCDisable(); // temporary disable removing of temporary volumes
1221 try
1222 // Çàãðóçêà WAD:
1223 FileName := g_ExtractWadName(Res);
1224 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1225 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1227 WAD := TWADFile.Create();
1228 if not WAD.ReadFile(FileName) then
1229 begin
1230 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1231 WAD.Free();
1232 Exit;
1233 end;
1234 //k8: why loader ignores path here?
1235 mapResName := g_ExtractFileName(Res);
1236 if not WAD.GetMapResource(mapResName, Data, Len) then
1237 begin
1238 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1239 WAD.Free();
1240 Exit;
1241 end;
1243 WAD.Free();
1245 // Çàãðóçêà êàðòû:
1246 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1247 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1248 MapReader := TMapReader_1.Create();
1250 if not MapReader.LoadMap(Data) then
1251 begin
1252 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1253 FreeMem(Data);
1254 MapReader.Free();
1255 Exit;
1256 end;
1258 FreeMem(Data);
1259 generateExternalResourcesList(MapReader);
1260 // Çàãðóçêà òåêñòóð:
1261 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1262 _textures := MapReader.GetTextures();
1263 _texnummap := nil;
1265 // Äîáàâëåíèå òåêñòóð â Textures[]:
1266 if _textures <> nil then
1267 begin
1268 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1269 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1270 SetLength(_texnummap, length(_textures));
1272 for a := 0 to High(_textures) do
1273 begin
1274 SetLength(s, 64);
1275 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1276 for b := 1 to Length(s) do
1277 if s[b] = #0 then
1278 begin
1279 SetLength(s, b-1);
1280 Break;
1281 end;
1282 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1283 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1284 // Àíèìèðîâàííàÿ òåêñòóðà:
1285 if ByteBool(_textures[a].Anim) then
1286 begin
1287 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1288 if ntn < 0 then
1289 begin
1290 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1291 ntn := CreateNullTexture(_textures[a].Resource);
1292 end;
1293 end
1294 else // Îáû÷íàÿ òåêñòóðà:
1295 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1296 if ntn < 0 then
1297 begin
1298 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1299 ntn := CreateNullTexture(_textures[a].Resource);
1300 end;
1302 _texnummap[a] := ntn; // fix texture number
1303 g_Game_StepLoading();
1304 end;
1305 end;
1307 // Çàãðóçêà òðèããåðîâ:
1308 gTriggerClientID := 0;
1309 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1310 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1311 triggers := MapReader.GetTriggers();
1313 // Çàãðóçêà ïàíåëåé:
1314 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1315 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1316 panels := MapReader.GetPanels();
1318 // check texture numbers for panels
1319 for a := 0 to High(panels) do
1320 begin
1321 if panels[a].TextureNum > High(_textures) then
1322 begin
1323 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1324 result := false;
1325 exit;
1326 end;
1327 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1328 end;
1330 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1331 if triggers <> nil then
1332 begin
1333 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1334 SetLength(TriggersTable, Length(triggers));
1335 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1337 for a := 0 to High(TriggersTable) do
1338 begin
1339 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1340 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1341 // Ëèôòû:
1342 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1343 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1344 else
1345 TriggersTable[a].LiftPanel := -1;
1346 // Äâåðè:
1347 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1348 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1349 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1350 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1351 else
1352 TriggersTable[a].DoorPanel := -1;
1353 // Òóðåëü:
1354 if triggers[a].TriggerType = TRIGGER_SHOT then
1355 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1356 else
1357 TriggersTable[a].ShotPanel := -1;
1359 g_Game_StepLoading();
1360 end;
1361 end;
1363 // Ñîçäàåì ïàíåëè:
1364 if panels <> nil then
1365 begin
1366 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1367 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1369 for a := 0 to High(panels) do
1370 begin
1371 SetLength(AddTextures, 0);
1372 trigRef := False;
1373 CurTex := -1;
1374 if _textures <> nil then
1375 begin
1376 texture := _textures[panels[a].TextureNum];
1377 ok := True;
1378 end
1379 else
1380 ok := False;
1382 if ok then
1383 begin
1384 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1385 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1386 ok := False;
1387 if (TriggersTable <> nil) and (_textures <> nil) then
1388 for b := 0 to High(TriggersTable) do
1389 if (TriggersTable[b].TexturePanel = a)
1390 or (TriggersTable[b].ShotPanel = a) then
1391 begin
1392 trigRef := True;
1393 ok := True;
1394 Break;
1395 end;
1396 end;
1398 if ok then
1399 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1400 SetLength(s, 64);
1401 CopyMemory(@s[1], @texture.Resource[0], 64);
1402 // Èçìåðÿåì äëèíó:
1403 Len := Length(s);
1404 for c := Len downto 1 do
1405 if s[c] <> #0 then
1406 begin
1407 Len := c;
1408 Break;
1409 end;
1410 SetLength(s, Len);
1412 // Ñïåö-òåêñòóðû çàïðåùåíû:
1413 if g_Map_IsSpecialTexture(s) then
1414 ok := False
1415 else
1416 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1417 ok := g_Texture_NumNameFindStart(s);
1419 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1420 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1421 if ok then
1422 begin
1423 k := NNF_NAME_BEFORE;
1424 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1425 while ok or (k = NNF_NAME_BEFORE) or
1426 (k = NNF_NAME_EQUALS) do
1427 begin
1428 k := g_Texture_NumNameFindNext(TexName);
1430 if (k = NNF_NAME_BEFORE) or
1431 (k = NNF_NAME_AFTER) then
1432 begin
1433 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1434 if ByteBool(texture.Anim) then
1435 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1436 isAnim := True;
1437 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1438 if not ok then
1439 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1440 isAnim := False;
1441 ok := CreateTexture(TexName, FileName, False) >= 0;
1442 end;
1443 end
1444 else
1445 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1446 isAnim := False;
1447 ok := CreateTexture(TexName, FileName, False) >= 0;
1448 if not ok then
1449 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1450 isAnim := True;
1451 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1452 end;
1453 end;
1455 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1456 if ok then
1457 begin
1458 for c := 0 to High(Textures) do
1459 if Textures[c].TextureName = TexName then
1460 begin
1461 SetLength(AddTextures, Length(AddTextures)+1);
1462 AddTextures[High(AddTextures)].Texture := c;
1463 AddTextures[High(AddTextures)].Anim := isAnim;
1464 Break;
1465 end;
1466 end;
1467 end
1468 else
1469 if k = NNF_NAME_EQUALS then
1470 begin
1471 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1472 SetLength(AddTextures, Length(AddTextures)+1);
1473 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1474 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1475 CurTex := High(AddTextures);
1476 ok := True;
1477 end
1478 else // NNF_NO_NAME
1479 ok := False;
1480 end; // while ok...
1482 ok := True;
1483 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1484 end; // if ok - ññûëàþòñÿ òðèããåðû
1486 if not ok then
1487 begin
1488 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1489 SetLength(AddTextures, 1);
1490 AddTextures[0].Texture := panels[a].TextureNum;
1491 AddTextures[0].Anim := ByteBool(texture.Anim);
1492 CurTex := 0;
1493 end;
1495 //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);
1497 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1498 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1500 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1501 if TriggersTable <> nil then
1502 for b := 0 to High(TriggersTable) do
1503 begin
1504 // Òðèããåð äâåðè/ëèôòà:
1505 if (TriggersTable[b].LiftPanel = a) or
1506 (TriggersTable[b].DoorPanel = a) then
1507 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1508 // Òðèããåð ñìåíû òåêñòóðû:
1509 if TriggersTable[b].TexturePanel = a then
1510 triggers[b].TexturePanel := PanelID;
1511 // Òðèããåð "Òóðåëü":
1512 if TriggersTable[b].ShotPanel = a then
1513 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1514 end;
1516 g_Game_StepLoading();
1517 end;
1518 end;
1520 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1521 if (triggers <> nil) and not gLoadGameMode then
1522 begin
1523 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1524 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1525 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1526 for a := 0 to High(triggers) do
1527 begin
1528 if triggers[a].TexturePanel <> -1 then
1529 b := panels[TriggersTable[a].TexturePanel].PanelType
1530 else
1531 b := 0;
1532 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1533 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1534 c := panels[TriggersTable[a].ShotPanel].PanelType
1535 else
1536 c := 0;
1537 CreateTrigger(triggers[a], b, c);
1538 end;
1539 end;
1541 // Çàãðóçêà ïðåäìåòîâ:
1542 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1543 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1544 items := MapReader.GetItems();
1546 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1547 if (items <> nil) and not gLoadGameMode then
1548 begin
1549 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1550 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1551 for a := 0 to High(items) do
1552 CreateItem(Items[a]);
1553 end;
1555 // Çàãðóçêà îáëàñòåé:
1556 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1557 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1558 areas := MapReader.GetAreas();
1560 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1561 if areas <> nil then
1562 begin
1563 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1564 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1565 for a := 0 to High(areas) do
1566 CreateArea(areas[a]);
1567 end;
1569 // Çàãðóçêà ìîíñòðîâ:
1570 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1571 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1572 monsters := MapReader.GetMonsters();
1574 gTotalMonsters := 0;
1576 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1577 if (monsters <> nil) and not gLoadGameMode then
1578 begin
1579 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1580 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1581 for a := 0 to High(monsters) do
1582 CreateMonster(monsters[a]);
1583 end;
1585 // Çàãðóçêà îïèñàíèÿ êàðòû:
1586 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1587 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1588 Header := MapReader.GetMapHeader();
1590 MapReader.Free();
1592 with gMapInfo do
1593 begin
1594 Name := Header.MapName;
1595 Description := Header.MapDescription;
1596 Author := Header.MapAuthor;
1597 MusicName := Header.MusicName;
1598 SkyName := Header.SkyName;
1599 Height := Header.Height;
1600 Width := Header.Width;
1601 end;
1603 // Çàãðóçêà íåáà:
1604 if gMapInfo.SkyName <> '' then
1605 begin
1606 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1607 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1608 FileName := g_ExtractWadName(gMapInfo.SkyName);
1610 if FileName <> '' then
1611 FileName := GameDir+'/wads/'+FileName
1612 else
1613 begin
1614 FileName := g_ExtractWadName(Res);
1615 end;
1617 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1618 if g_Texture_CreateWAD(BackID, s) then
1619 begin
1620 g_Game_SetupScreenSize();
1621 end
1622 else
1623 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1624 end;
1626 // Çàãðóçêà ìóçûêè:
1627 ok := False;
1628 if gMapInfo.MusicName <> '' then
1629 begin
1630 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1631 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1632 FileName := g_ExtractWadName(gMapInfo.MusicName);
1634 if FileName <> '' then
1635 FileName := GameDir+'/wads/'+FileName
1636 else
1637 begin
1638 FileName := g_ExtractWadName(Res);
1639 end;
1641 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1642 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1643 ok := True
1644 else
1645 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1646 end;
1648 // Îñòàëüíûå óñòàíâêè:
1649 CreateDoorMap();
1650 CreateLiftMap();
1652 g_Items_Init();
1653 g_Weapon_Init();
1654 g_Monsters_Init();
1656 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1657 if not gLoadGameMode then
1658 g_GFX_Init();
1660 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1661 _textures := nil;
1662 panels := nil;
1663 items := nil;
1664 areas := nil;
1665 triggers := nil;
1666 TriggersTable := nil;
1667 AddTextures := nil;
1669 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1670 if ok and (not gLoadGameMode) then
1671 begin
1672 gMusic.SetByName(gMapInfo.MusicName);
1673 gMusic.Play();
1674 end
1675 else
1676 gMusic.SetByName('');
1677 finally
1678 sfsGCEnable(); // enable releasing unused volumes
1679 end;
1681 e_WriteLog('Creating map grid', MSG_NOTIFY);
1682 mapCreateGrid();
1684 e_WriteLog('Done loading map.', MSG_NOTIFY);
1685 Result := True;
1686 end;
1688 function g_Map_GetMapInfo(Res: String): TMapInfo;
1689 var
1690 WAD: TWADFile;
1691 MapReader: TMapReader_1;
1692 Header: TMapHeaderRec_1;
1693 FileName: String;
1694 Data: Pointer;
1695 Len: Integer;
1696 begin
1697 FillChar(Result, SizeOf(Result), 0);
1698 FileName := g_ExtractWadName(Res);
1700 WAD := TWADFile.Create();
1701 if not WAD.ReadFile(FileName) then
1702 begin
1703 WAD.Free();
1704 Exit;
1705 end;
1707 //k8: it ignores path again
1708 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1709 begin
1710 WAD.Free();
1711 Exit;
1712 end;
1714 WAD.Free();
1716 MapReader := TMapReader_1.Create();
1718 if not MapReader.LoadMap(Data) then
1719 begin
1720 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1721 ZeroMemory(@Header, SizeOf(Header));
1722 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1723 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1724 end
1725 else
1726 begin
1727 Header := MapReader.GetMapHeader();
1728 Result.Name := Header.MapName;
1729 Result.Description := Header.MapDescription;
1730 end;
1732 FreeMem(Data);
1733 MapReader.Free();
1735 Result.Map := Res;
1736 Result.Author := Header.MapAuthor;
1737 Result.Height := Header.Height;
1738 Result.Width := Header.Width;
1739 end;
1741 function g_Map_GetMapsList(WADName: string): SArray;
1742 var
1743 WAD: TWADFile;
1744 a: Integer;
1745 ResList: SArray;
1746 begin
1747 Result := nil;
1748 WAD := TWADFile.Create();
1749 if not WAD.ReadFile(WADName) then
1750 begin
1751 WAD.Free();
1752 Exit;
1753 end;
1754 ResList := WAD.GetMapResources();
1755 if ResList <> nil then
1756 begin
1757 for a := 0 to High(ResList) do
1758 begin
1759 SetLength(Result, Length(Result)+1);
1760 Result[High(Result)] := ResList[a];
1761 end;
1762 end;
1763 WAD.Free();
1764 end;
1766 function g_Map_Exist(Res: string): Boolean;
1767 var
1768 WAD: TWADFile;
1769 FileName, mnn: string;
1770 ResList: SArray;
1771 a: Integer;
1772 begin
1773 Result := False;
1775 FileName := addWadExtension(g_ExtractWadName(Res));
1777 WAD := TWADFile.Create;
1778 if not WAD.ReadFile(FileName) then
1779 begin
1780 WAD.Free();
1781 Exit;
1782 end;
1784 ResList := WAD.GetMapResources();
1785 WAD.Free();
1787 mnn := g_ExtractFileName(Res);
1788 if ResList <> nil then
1789 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1790 begin
1791 Result := True;
1792 Exit;
1793 end;
1794 end;
1796 procedure g_Map_Free();
1797 var
1798 a: Integer;
1800 procedure FreePanelArray(var panels: TPanelArray);
1801 var
1802 i: Integer;
1804 begin
1805 if panels <> nil then
1806 begin
1807 for i := 0 to High(panels) do
1808 panels[i].Free();
1809 panels := nil;
1810 end;
1811 end;
1813 begin
1814 g_GFX_Free();
1815 g_Weapon_Free();
1816 g_Items_Free();
1817 g_Triggers_Free();
1818 g_Monsters_Free();
1820 RespawnPoints := nil;
1821 if FlagPoints[FLAG_RED] <> nil then
1822 begin
1823 Dispose(FlagPoints[FLAG_RED]);
1824 FlagPoints[FLAG_RED] := nil;
1825 end;
1826 if FlagPoints[FLAG_BLUE] <> nil then
1827 begin
1828 Dispose(FlagPoints[FLAG_BLUE]);
1829 FlagPoints[FLAG_BLUE] := nil;
1830 end;
1831 //DOMFlagPoints := nil;
1833 //gDOMFlags := nil;
1835 if Textures <> nil then
1836 begin
1837 for a := 0 to High(Textures) do
1838 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1839 if Textures[a].Anim then
1840 g_Frames_DeleteByID(Textures[a].FramesID)
1841 else
1842 if Textures[a].TextureID <> TEXTURE_NONE then
1843 e_DeleteTexture(Textures[a].TextureID);
1845 Textures := nil;
1846 end;
1848 FreePanelArray(gWalls);
1849 FreePanelArray(gRenderBackgrounds);
1850 FreePanelArray(gRenderForegrounds);
1851 FreePanelArray(gWater);
1852 FreePanelArray(gAcid1);
1853 FreePanelArray(gAcid2);
1854 FreePanelArray(gSteps);
1855 FreePanelArray(gLifts);
1856 FreePanelArray(gBlockMon);
1858 if BackID <> DWORD(-1) then
1859 begin
1860 gBackSize.X := 0;
1861 gBackSize.Y := 0;
1862 e_DeleteTexture(BackID);
1863 BackID := DWORD(-1);
1864 end;
1866 g_Game_StopAllSounds(False);
1867 gMusic.FreeSound();
1868 g_Sound_Delete(gMapInfo.MusicName);
1870 gMapInfo.Name := '';
1871 gMapInfo.Description := '';
1872 gMapInfo.MusicName := '';
1873 gMapInfo.Height := 0;
1874 gMapInfo.Width := 0;
1876 gDoorMap := nil;
1877 gLiftMap := nil;
1879 PanelByID := nil;
1880 end;
1882 procedure g_Map_Update();
1883 var
1884 a, d, j: Integer;
1885 m: Word;
1886 s: String;
1888 procedure UpdatePanelArray(var panels: TPanelArray);
1889 var
1890 i: Integer;
1892 begin
1893 if panels <> nil then
1894 for i := 0 to High(panels) do
1895 panels[i].Update();
1896 end;
1898 begin
1899 UpdatePanelArray(gWalls);
1900 UpdatePanelArray(gRenderBackgrounds);
1901 UpdatePanelArray(gRenderForegrounds);
1902 UpdatePanelArray(gWater);
1903 UpdatePanelArray(gAcid1);
1904 UpdatePanelArray(gAcid2);
1905 UpdatePanelArray(gSteps);
1907 if gGameSettings.GameMode = GM_CTF then
1908 begin
1909 for a := FLAG_RED to FLAG_BLUE do
1910 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1911 with gFlags[a] do
1912 begin
1913 if gFlags[a].Animation <> nil then
1914 gFlags[a].Animation.Update();
1916 m := g_Obj_Move(@Obj, True, True);
1918 if gTime mod (GAME_TICK*2) <> 0 then
1919 Continue;
1921 // Ñîïðîòèâëåíèå âîçäóõà:
1922 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1924 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1925 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1926 begin
1927 g_Map_ResetFlag(a);
1928 gFlags[a].CaptureTime := 0;
1929 if a = FLAG_RED then
1930 s := _lc[I_PLAYER_FLAG_RED]
1931 else
1932 s := _lc[I_PLAYER_FLAG_BLUE];
1933 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1935 if g_Game_IsNet then
1936 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1937 Continue;
1938 end;
1940 if Count > 0 then
1941 Count := Count - 1;
1943 // Èãðîê áåðåò ôëàã:
1944 if gPlayers <> nil then
1945 begin
1946 j := Random(Length(gPlayers)) - 1;
1948 for d := 0 to High(gPlayers) do
1949 begin
1950 Inc(j);
1951 if j > High(gPlayers) then
1952 j := 0;
1954 if gPlayers[j] <> nil then
1955 if gPlayers[j].Live and
1956 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1957 begin
1958 if gPlayers[j].GetFlag(a) then
1959 Break;
1960 end;
1961 end;
1962 end;
1963 end;
1964 end;
1965 end;
1968 // old algo
1969 procedure g_Map_DrawPanels (PanelType: Word);
1971 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
1972 var
1973 idx: Integer;
1974 begin
1975 if (panels <> nil) then
1976 begin
1977 // alas, no visible set
1978 for idx := 0 to High(panels) do
1979 begin
1980 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1981 end;
1982 end;
1983 end;
1985 begin
1986 case PanelType of
1987 PANEL_WALL: DrawPanels(gWalls);
1988 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
1989 PANEL_BACK: DrawPanels(gRenderBackgrounds);
1990 PANEL_FORE: DrawPanels(gRenderForegrounds);
1991 PANEL_WATER: DrawPanels(gWater);
1992 PANEL_ACID1: DrawPanels(gAcid1);
1993 PANEL_ACID2: DrawPanels(gAcid2);
1994 PANEL_STEP: DrawPanels(gSteps);
1995 end;
1996 end;
1999 // new algo
2000 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2001 function checker (pan: TPanel; tag: Integer): Boolean;
2002 begin
2003 result := false; // don't stop, ever
2004 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2005 gDrawPanelList.insert(pan);
2006 end;
2008 begin
2009 dplClear();
2010 //tagmask := panelTypeToTag(PanelType);
2012 if gdbg_map_use_tree_draw then
2013 begin
2014 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2015 end
2016 else
2017 begin
2018 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2019 end;
2020 // list will be rendered in `g_game.DrawPlayer()`
2021 end;
2024 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2025 function checker (pan: TPanel; tag: Integer): Boolean;
2026 begin
2027 result := false; // don't stop, ever
2028 pan.DrawShadowVolume(lightX, lightY, radius);
2029 end;
2031 begin
2032 if gdbg_map_use_tree_draw then
2033 begin
2034 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2035 end
2036 else
2037 begin
2038 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2039 end;
2040 end;
2043 procedure g_Map_DrawBack(dx, dy: Integer);
2044 begin
2045 if gDrawBackGround and (BackID <> DWORD(-1)) then
2046 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2047 else
2048 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2049 end;
2051 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2052 PanelType: Word; b1x3: Boolean): Boolean;
2053 var
2054 a, h: Integer;
2055 begin
2056 Result := False;
2058 if WordBool(PanelType and PANEL_WALL) then
2059 if gWalls <> nil then
2060 begin
2061 h := High(gWalls);
2063 for a := 0 to h do
2064 if gWalls[a].Enabled and
2065 g_Collide(X, Y, Width, Height,
2066 gWalls[a].X, gWalls[a].Y,
2067 gWalls[a].Width, gWalls[a].Height) then
2068 begin
2069 Result := True;
2070 Exit;
2071 end;
2072 end;
2074 if WordBool(PanelType and PANEL_WATER) then
2075 if gWater <> nil then
2076 begin
2077 h := High(gWater);
2079 for a := 0 to h do
2080 if g_Collide(X, Y, Width, Height,
2081 gWater[a].X, gWater[a].Y,
2082 gWater[a].Width, gWater[a].Height) then
2083 begin
2084 Result := True;
2085 Exit;
2086 end;
2087 end;
2089 if WordBool(PanelType and PANEL_ACID1) then
2090 if gAcid1 <> nil then
2091 begin
2092 h := High(gAcid1);
2094 for a := 0 to h do
2095 if g_Collide(X, Y, Width, Height,
2096 gAcid1[a].X, gAcid1[a].Y,
2097 gAcid1[a].Width, gAcid1[a].Height) then
2098 begin
2099 Result := True;
2100 Exit;
2101 end;
2102 end;
2104 if WordBool(PanelType and PANEL_ACID2) then
2105 if gAcid2 <> nil then
2106 begin
2107 h := High(gAcid2);
2109 for a := 0 to h do
2110 if g_Collide(X, Y, Width, Height,
2111 gAcid2[a].X, gAcid2[a].Y,
2112 gAcid2[a].Width, gAcid2[a].Height) then
2113 begin
2114 Result := True;
2115 Exit;
2116 end;
2117 end;
2119 if WordBool(PanelType and PANEL_STEP) then
2120 if gSteps <> nil then
2121 begin
2122 h := High(gSteps);
2124 for a := 0 to h do
2125 if g_Collide(X, Y, Width, Height,
2126 gSteps[a].X, gSteps[a].Y,
2127 gSteps[a].Width, gSteps[a].Height) then
2128 begin
2129 Result := True;
2130 Exit;
2131 end;
2132 end;
2134 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2135 if gLifts <> nil then
2136 begin
2137 h := High(gLifts);
2139 for a := 0 to h do
2140 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2141 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2142 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2143 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2144 g_Collide(X, Y, Width, Height,
2145 gLifts[a].X, gLifts[a].Y,
2146 gLifts[a].Width, gLifts[a].Height) then
2147 begin
2148 Result := True;
2149 Exit;
2150 end;
2151 end;
2153 if WordBool(PanelType and PANEL_BLOCKMON) then
2154 if gBlockMon <> nil then
2155 begin
2156 h := High(gBlockMon);
2158 for a := 0 to h do
2159 if ( (not b1x3) or
2160 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2161 g_Collide(X, Y, Width, Height,
2162 gBlockMon[a].X, gBlockMon[a].Y,
2163 gBlockMon[a].Width, gBlockMon[a].Height) then
2164 begin
2165 Result := True;
2166 Exit;
2167 end;
2168 end;
2169 end;
2171 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2172 var
2173 texid: DWORD;
2175 function checkPanels (constref panels: TPanelArray): Boolean;
2176 var
2177 a: Integer;
2178 begin
2179 result := false;
2180 if panels = nil then exit;
2181 for a := 0 to High(panels) do
2182 begin
2183 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2184 begin
2185 result := true;
2186 texid := panels[a].GetTextureID();
2187 exit;
2188 end;
2189 end;
2190 end;
2192 begin
2193 texid := TEXTURE_NONE;
2194 result := texid;
2195 if not checkPanels(gWater) then
2196 if not checkPanels(gAcid1) then
2197 if not checkPanels(gAcid2) then exit;
2198 result := texid;
2199 end;
2202 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2203 function checker (pan: TPanel; tag: Integer): Boolean;
2204 begin
2205 result := false; // don't stop, ever
2207 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2208 begin
2209 if not pan.Enabled then exit;
2210 end;
2212 if ((tag and GridTagLift) <> 0) then
2213 begin
2214 result :=
2215 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2216 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2217 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2218 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) and
2219 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2220 exit;
2221 end;
2223 if ((tag and GridTagBlockMon) <> 0) then
2224 begin
2225 result := ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2226 exit;
2227 end;
2229 // other shit
2230 result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2231 end;
2233 var
2234 tagmask: Integer = 0;
2235 begin
2236 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2237 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2238 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2239 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2240 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2241 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2242 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2244 if (tagmask = 0) then begin result := false; exit; end; // just in case
2246 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2247 if gdbg_map_use_accel_coldet then
2248 begin
2249 if gdbg_map_use_tree_coldet then
2250 begin
2251 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2252 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2253 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2254 begin
2255 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2256 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2257 end;
2258 end
2259 else
2260 begin
2261 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2262 end;
2263 end
2264 else
2265 begin
2266 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2267 end;
2268 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2269 end;
2272 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2273 var
2274 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2275 texid: DWORD;
2277 // slightly different from the old code, but meh...
2278 function checker (pan: TPanel; tag: Integer): Boolean;
2279 begin
2280 result := false; // don't stop, ever
2281 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2282 // check priorities
2283 case cctype of
2284 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2285 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2286 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2287 end;
2288 // collision?
2289 if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2290 // yeah
2291 texid := pan.GetTextureID();
2292 // water? water has the highest priority, so stop right here
2293 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2294 // acid2?
2295 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2296 // acid1?
2297 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2298 end;
2300 begin
2301 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2302 if gdbg_map_use_accel_coldet then
2303 begin
2304 texid := TEXTURE_NONE;
2305 if gdbg_map_use_tree_coldet then
2306 begin
2307 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2308 end
2309 else
2310 begin
2311 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2312 end;
2313 result := texid;
2314 end
2315 else
2316 begin
2317 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2318 end;
2319 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2320 end;
2322 procedure g_Map_EnableWall(ID: DWORD);
2323 begin
2324 with gWalls[ID] do
2325 begin
2326 Enabled := True;
2327 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2329 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2330 end;
2331 end;
2333 procedure g_Map_DisableWall(ID: DWORD);
2334 begin
2335 with gWalls[ID] do
2336 begin
2337 Enabled := False;
2338 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2340 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2341 end;
2342 end;
2344 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2345 var
2346 tp: TPanel;
2347 begin
2348 case PanelType of
2349 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2350 tp := gWalls[ID];
2351 PANEL_FORE:
2352 tp := gRenderForegrounds[ID];
2353 PANEL_BACK:
2354 tp := gRenderBackgrounds[ID];
2355 PANEL_WATER:
2356 tp := gWater[ID];
2357 PANEL_ACID1:
2358 tp := gAcid1[ID];
2359 PANEL_ACID2:
2360 tp := gAcid2[ID];
2361 PANEL_STEP:
2362 tp := gSteps[ID];
2363 else
2364 Exit;
2365 end;
2367 tp.NextTexture(AnimLoop);
2368 if g_Game_IsServer and g_Game_IsNet then
2369 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2370 end;
2372 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2373 begin
2374 if gLifts[ID].LiftType = t then
2375 Exit;
2377 with gLifts[ID] do
2378 begin
2379 LiftType := t;
2381 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2383 if LiftType = 0 then
2384 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2385 else if LiftType = 1 then
2386 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2387 else if LiftType = 2 then
2388 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2389 else if LiftType = 3 then
2390 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2392 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2393 end;
2394 end;
2396 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2397 var
2398 a: Integer;
2399 PointsArray: Array of TRespawnPoint;
2400 begin
2401 Result := False;
2402 SetLength(PointsArray, 0);
2404 if RespawnPoints = nil then
2405 Exit;
2407 for a := 0 to High(RespawnPoints) do
2408 if RespawnPoints[a].PointType = PointType then
2409 begin
2410 SetLength(PointsArray, Length(PointsArray)+1);
2411 PointsArray[High(PointsArray)] := RespawnPoints[a];
2412 end;
2414 if PointsArray = nil then
2415 Exit;
2417 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2418 Result := True;
2419 end;
2421 function g_Map_GetPointCount(PointType: Byte): Word;
2422 var
2423 a: Integer;
2424 begin
2425 Result := 0;
2427 if RespawnPoints = nil then
2428 Exit;
2430 for a := 0 to High(RespawnPoints) do
2431 if RespawnPoints[a].PointType = PointType then
2432 Result := Result + 1;
2433 end;
2435 function g_Map_HaveFlagPoints(): Boolean;
2436 begin
2437 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2438 end;
2440 procedure g_Map_ResetFlag(Flag: Byte);
2441 begin
2442 with gFlags[Flag] do
2443 begin
2444 Obj.X := -1000;
2445 Obj.Y := -1000;
2446 Obj.Vel.X := 0;
2447 Obj.Vel.Y := 0;
2448 Direction := D_LEFT;
2449 State := FLAG_STATE_NONE;
2450 if FlagPoints[Flag] <> nil then
2451 begin
2452 Obj.X := FlagPoints[Flag]^.X;
2453 Obj.Y := FlagPoints[Flag]^.Y;
2454 Direction := FlagPoints[Flag]^.Direction;
2455 State := FLAG_STATE_NORMAL;
2456 end;
2457 Count := -1;
2458 end;
2459 end;
2461 procedure g_Map_DrawFlags();
2462 var
2463 i, dx: Integer;
2464 Mirror: TMirrorType;
2465 begin
2466 if gGameSettings.GameMode <> GM_CTF then
2467 Exit;
2469 for i := FLAG_RED to FLAG_BLUE do
2470 with gFlags[i] do
2471 if State <> FLAG_STATE_CAPTURED then
2472 begin
2473 if State = FLAG_STATE_NONE then
2474 continue;
2476 if Direction = D_LEFT then
2477 begin
2478 Mirror := M_HORIZONTAL;
2479 dx := -1;
2480 end
2481 else
2482 begin
2483 Mirror := M_NONE;
2484 dx := 1;
2485 end;
2487 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2489 if g_debug_Frames then
2490 begin
2491 e_DrawQuad(Obj.X+Obj.Rect.X,
2492 Obj.Y+Obj.Rect.Y,
2493 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2494 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2495 0, 255, 0);
2496 end;
2497 end;
2498 end;
2500 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2501 var
2502 dw: DWORD;
2503 b: Byte;
2504 str: String;
2505 boo: Boolean;
2507 procedure SavePanelArray(var panels: TPanelArray);
2508 var
2509 PAMem: TBinMemoryWriter;
2510 i: Integer;
2511 begin
2512 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2513 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2515 i := 0;
2516 while i < Length(panels) do
2517 begin
2518 if panels[i].SaveIt then
2519 begin
2520 // ID ïàíåëè:
2521 PAMem.WriteInt(i);
2522 // Ñîõðàíÿåì ïàíåëü:
2523 panels[i].SaveState(PAMem);
2524 end;
2525 Inc(i);
2526 end;
2528 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2529 PAMem.SaveToMemory(Mem);
2530 PAMem.Free();
2531 end;
2533 procedure SaveFlag(flag: PFlag);
2534 begin
2535 // Ñèãíàòóðà ôëàãà:
2536 dw := FLAG_SIGNATURE; // 'FLAG'
2537 Mem.WriteDWORD(dw);
2538 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2539 Mem.WriteByte(flag^.RespawnType);
2540 // Ñîñòîÿíèå ôëàãà:
2541 Mem.WriteByte(flag^.State);
2542 // Íàïðàâëåíèå ôëàãà:
2543 if flag^.Direction = D_LEFT then
2544 b := 1
2545 else // D_RIGHT
2546 b := 2;
2547 Mem.WriteByte(b);
2548 // Îáúåêò ôëàãà:
2549 Obj_SaveState(@flag^.Obj, Mem);
2550 end;
2552 begin
2553 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2555 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2556 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2557 SavePanelArray(gWalls);
2558 // Ñîõðàíÿåì ïàíåëè ôîíà:
2559 SavePanelArray(gRenderBackgrounds);
2560 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2561 SavePanelArray(gRenderForegrounds);
2562 // Ñîõðàíÿåì ïàíåëè âîäû:
2563 SavePanelArray(gWater);
2564 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2565 SavePanelArray(gAcid1);
2566 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2567 SavePanelArray(gAcid2);
2568 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2569 SavePanelArray(gSteps);
2570 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2571 SavePanelArray(gLifts);
2572 ///// /////
2574 ///// Ñîõðàíÿåì ìóçûêó: /////
2575 // Ñèãíàòóðà ìóçûêè:
2576 dw := MUSIC_SIGNATURE; // 'MUSI'
2577 Mem.WriteDWORD(dw);
2578 // Íàçâàíèå ìóçûêè:
2579 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2580 if gMusic.NoMusic then
2581 str := ''
2582 else
2583 str := gMusic.Name;
2584 Mem.WriteString(str, 64);
2585 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2586 dw := gMusic.GetPosition();
2587 Mem.WriteDWORD(dw);
2588 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2589 boo := gMusic.SpecPause;
2590 Mem.WriteBoolean(boo);
2591 ///// /////
2593 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2594 Mem.WriteInt(gTotalMonsters);
2595 ///// /////
2597 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2598 if gGameSettings.GameMode = GM_CTF then
2599 begin
2600 // Ôëàã Êðàñíîé êîìàíäû:
2601 SaveFlag(@gFlags[FLAG_RED]);
2602 // Ôëàã Ñèíåé êîìàíäû:
2603 SaveFlag(@gFlags[FLAG_BLUE]);
2604 end;
2605 ///// /////
2607 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2608 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2609 begin
2610 // Î÷êè Êðàñíîé êîìàíäû:
2611 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2612 // Î÷êè Ñèíåé êîìàíäû:
2613 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2614 end;
2615 ///// /////
2616 end;
2618 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2619 var
2620 dw: DWORD;
2621 b: Byte;
2622 str: String;
2623 boo: Boolean;
2625 procedure LoadPanelArray(var panels: TPanelArray);
2626 var
2627 PAMem: TBinMemoryReader;
2628 i, id: Integer;
2629 begin
2630 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2631 PAMem := TBinMemoryReader.Create();
2632 PAMem.LoadFromMemory(Mem);
2634 for i := 0 to Length(panels)-1 do
2635 if panels[i].SaveIt then
2636 begin
2637 // ID ïàíåëè:
2638 PAMem.ReadInt(id);
2639 if id <> i then
2640 begin
2641 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2642 end;
2643 // Çàãðóæàåì ïàíåëü:
2644 panels[i].LoadState(PAMem);
2645 end;
2647 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2648 PAMem.Free();
2649 end;
2651 procedure LoadFlag(flag: PFlag);
2652 begin
2653 // Ñèãíàòóðà ôëàãà:
2654 Mem.ReadDWORD(dw);
2655 if dw <> FLAG_SIGNATURE then // 'FLAG'
2656 begin
2657 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2658 end;
2659 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2660 Mem.ReadByte(flag^.RespawnType);
2661 // Ñîñòîÿíèå ôëàãà:
2662 Mem.ReadByte(flag^.State);
2663 // Íàïðàâëåíèå ôëàãà:
2664 Mem.ReadByte(b);
2665 if b = 1 then
2666 flag^.Direction := D_LEFT
2667 else // b = 2
2668 flag^.Direction := D_RIGHT;
2669 // Îáúåêò ôëàãà:
2670 Obj_LoadState(@flag^.Obj, Mem);
2671 end;
2673 begin
2674 if Mem = nil then
2675 Exit;
2677 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2678 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2679 LoadPanelArray(gWalls);
2680 // Çàãðóæàåì ïàíåëè ôîíà:
2681 LoadPanelArray(gRenderBackgrounds);
2682 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2683 LoadPanelArray(gRenderForegrounds);
2684 // Çàãðóæàåì ïàíåëè âîäû:
2685 LoadPanelArray(gWater);
2686 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2687 LoadPanelArray(gAcid1);
2688 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2689 LoadPanelArray(gAcid2);
2690 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2691 LoadPanelArray(gSteps);
2692 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2693 LoadPanelArray(gLifts);
2694 ///// /////
2696 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2697 g_GFX_Init();
2698 mapCreateGrid();
2700 ///// Çàãðóæàåì ìóçûêó: /////
2701 // Ñèãíàòóðà ìóçûêè:
2702 Mem.ReadDWORD(dw);
2703 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2704 begin
2705 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2706 end;
2707 // Íàçâàíèå ìóçûêè:
2708 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2709 Mem.ReadString(str);
2710 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2711 Mem.ReadDWORD(dw);
2712 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2713 Mem.ReadBoolean(boo);
2714 // Çàïóñêàåì ýòó ìóçûêó:
2715 gMusic.SetByName(str);
2716 gMusic.SpecPause := boo;
2717 gMusic.Play();
2718 gMusic.Pause(True);
2719 gMusic.SetPosition(dw);
2720 ///// /////
2722 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2723 Mem.ReadInt(gTotalMonsters);
2724 ///// /////
2726 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2727 if gGameSettings.GameMode = GM_CTF then
2728 begin
2729 // Ôëàã Êðàñíîé êîìàíäû:
2730 LoadFlag(@gFlags[FLAG_RED]);
2731 // Ôëàã Ñèíåé êîìàíäû:
2732 LoadFlag(@gFlags[FLAG_BLUE]);
2733 end;
2734 ///// /////
2736 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2737 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2738 begin
2739 // Î÷êè Êðàñíîé êîìàíäû:
2740 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2741 // Î÷êè Ñèíåé êîìàíäû:
2742 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2743 end;
2744 ///// /////
2745 end;
2747 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2748 var
2749 Arr: TPanelArray;
2750 begin
2751 Result := nil;
2752 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2753 Arr := PanelByID[PanelID].PWhere^;
2754 PanelArrayID := PanelByID[PanelID].PArrID;
2755 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2756 end;
2758 end.