DEADSOFTWARE

new tree-based weapon hitscan tracer (sometimes it is faster than the old one, someti...
[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;
290 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
291 var
292 aabb: AABB2D;
293 tmin: Single;
294 begin
295 result := -666.0; // invalid
296 if not pan.Enabled then exit;
297 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
298 if not aabb.valid then exit;
299 if aabb.intersects(ray, @tmin) then
300 begin
301 if (tmin >= 0.0) then
302 begin
303 //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);
304 //if (tmin < 0.0) then tmin := 0.0;
305 result := tmin;
306 end;
307 end;
308 end;
310 var
311 qr: TDynAABBTreeMap.TSegmentQueryResult;
312 ray: Ray2D;
313 hxf, hyf: Single;
314 hx, hy: Integer;
315 begin
316 result := -1;
317 if (mapTree = nil) then exit;
318 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
319 begin
320 if (qr.flesh <> nil) then
321 begin
322 result := qr.flesh.arrIdx;
323 if (hitx <> nil) or (hity <> nil) then
324 begin
325 ray := Ray2D.Create(x0, y0, x1, y1);
326 hxf := ray.origX+ray.dirX*qr.time;
327 hyf := ray.origY+ray.dirY*qr.time;
328 while true do
329 begin
330 hx := trunc(hxf);
331 hy := trunc(hyf);
332 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
333 begin
334 // go back a little
335 hxf -= ray.dirX;
336 hyf -= ray.dirY;
337 end
338 else
339 begin
340 break;
341 end;
342 end;
343 if (hitx <> nil) then hitx^ := hx;
344 if (hity <> nil) then hity^ := hy;
345 end;
346 end;
347 end;
348 end;
351 function g_Map_IsSpecialTexture(Texture: String): Boolean;
352 begin
353 Result := (Texture = TEXTURE_NAME_WATER) or
354 (Texture = TEXTURE_NAME_ACID1) or
355 (Texture = TEXTURE_NAME_ACID2);
356 end;
358 procedure CreateDoorMap();
359 var
360 PanelArray: Array of record
361 X, Y: Integer;
362 Width, Height: Word;
363 Active: Boolean;
364 PanelID: DWORD;
365 end;
366 a, b, c, m, i, len: Integer;
367 ok: Boolean;
368 begin
369 if gWalls = nil then
370 Exit;
372 i := 0;
373 len := 128;
374 SetLength(PanelArray, len);
376 for a := 0 to High(gWalls) do
377 if gWalls[a].Door then
378 begin
379 PanelArray[i].X := gWalls[a].X;
380 PanelArray[i].Y := gWalls[a].Y;
381 PanelArray[i].Width := gWalls[a].Width;
382 PanelArray[i].Height := gWalls[a].Height;
383 PanelArray[i].Active := True;
384 PanelArray[i].PanelID := a;
386 i := i + 1;
387 if i = len then
388 begin
389 len := len + 128;
390 SetLength(PanelArray, len);
391 end;
392 end;
394 // Íåò äâåðåé:
395 if i = 0 then
396 begin
397 PanelArray := nil;
398 Exit;
399 end;
401 SetLength(gDoorMap, 0);
403 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
405 for a := 0 to i-1 do
406 if PanelArray[a].Active then
407 begin
408 PanelArray[a].Active := False;
409 m := Length(gDoorMap);
410 SetLength(gDoorMap, m+1);
411 SetLength(gDoorMap[m], 1);
412 gDoorMap[m, 0] := PanelArray[a].PanelID;
413 ok := True;
415 while ok do
416 begin
417 ok := False;
419 for b := 0 to i-1 do
420 if PanelArray[b].Active then
421 for c := 0 to High(gDoorMap[m]) do
422 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
423 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
424 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
425 PanelArray[b].Width, PanelArray[b].Height,
426 gWalls[gDoorMap[m, c]].X,
427 gWalls[gDoorMap[m, c]].Y,
428 gWalls[gDoorMap[m, c]].Width,
429 gWalls[gDoorMap[m, c]].Height) then
430 begin
431 PanelArray[b].Active := False;
432 SetLength(gDoorMap[m],
433 Length(gDoorMap[m])+1);
434 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
435 ok := True;
436 Break;
437 end;
438 end;
440 g_Game_StepLoading();
441 end;
443 PanelArray := nil;
444 end;
446 procedure CreateLiftMap();
447 var
448 PanelArray: Array of record
449 X, Y: Integer;
450 Width, Height: Word;
451 Active: Boolean;
452 end;
453 a, b, c, len, i, j: Integer;
454 ok: Boolean;
455 begin
456 if gLifts = nil then
457 Exit;
459 len := Length(gLifts);
460 SetLength(PanelArray, len);
462 for a := 0 to len-1 do
463 begin
464 PanelArray[a].X := gLifts[a].X;
465 PanelArray[a].Y := gLifts[a].Y;
466 PanelArray[a].Width := gLifts[a].Width;
467 PanelArray[a].Height := gLifts[a].Height;
468 PanelArray[a].Active := True;
469 end;
471 SetLength(gLiftMap, len);
472 i := 0;
474 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
476 for a := 0 to len-1 do
477 if PanelArray[a].Active then
478 begin
479 PanelArray[a].Active := False;
480 SetLength(gLiftMap[i], 32);
481 j := 0;
482 gLiftMap[i, j] := a;
483 ok := True;
485 while ok do
486 begin
487 ok := False;
488 for b := 0 to len-1 do
489 if PanelArray[b].Active then
490 for c := 0 to j do
491 if g_CollideAround(PanelArray[b].X,
492 PanelArray[b].Y,
493 PanelArray[b].Width,
494 PanelArray[b].Height,
495 PanelArray[gLiftMap[i, c]].X,
496 PanelArray[gLiftMap[i, c]].Y,
497 PanelArray[gLiftMap[i, c]].Width,
498 PanelArray[gLiftMap[i, c]].Height) then
499 begin
500 PanelArray[b].Active := False;
501 j := j+1;
502 if j > High(gLiftMap[i]) then
503 SetLength(gLiftMap[i],
504 Length(gLiftMap[i])+32);
506 gLiftMap[i, j] := b;
507 ok := True;
509 Break;
510 end;
511 end;
513 SetLength(gLiftMap[i], j+1);
514 i := i+1;
516 g_Game_StepLoading();
517 end;
519 SetLength(gLiftMap, i);
521 PanelArray := nil;
522 end;
524 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
525 CurTex: Integer; sav: Boolean): Integer;
526 var
527 len: Integer;
528 panels: ^TPanelArray;
529 begin
530 Result := -1;
532 case PanelRec.PanelType of
533 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
534 panels := @gWalls;
535 PANEL_BACK:
536 panels := @gRenderBackgrounds;
537 PANEL_FORE:
538 panels := @gRenderForegrounds;
539 PANEL_WATER:
540 panels := @gWater;
541 PANEL_ACID1:
542 panels := @gAcid1;
543 PANEL_ACID2:
544 panels := @gAcid2;
545 PANEL_STEP:
546 panels := @gSteps;
547 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
548 panels := @gLifts;
549 PANEL_BLOCKMON:
550 panels := @gBlockMon;
551 else
552 Exit;
553 end;
555 len := Length(panels^);
556 SetLength(panels^, len + 1);
558 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
559 panels^[len].ArrIdx := len;
560 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
561 if sav then
562 panels^[len].SaveIt := True;
564 Result := len;
566 len := Length(PanelByID);
567 SetLength(PanelByID, len + 1);
568 PanelByID[len].PWhere := panels;
569 PanelByID[len].PArrID := Result;
570 end;
572 function CreateNullTexture(RecName: String): Integer;
573 begin
574 SetLength(Textures, Length(Textures)+1);
575 result := High(Textures);
577 with Textures[High(Textures)] do
578 begin
579 TextureName := RecName;
580 Width := 1;
581 Height := 1;
582 Anim := False;
583 TextureID := TEXTURE_NONE;
584 end;
585 end;
587 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
588 var
589 WAD: TWADFile;
590 TextureData: Pointer;
591 WADName, txname: String;
592 a, ResLength: Integer;
593 begin
594 Result := -1;
596 if Textures <> nil then
597 for a := 0 to High(Textures) do
598 if Textures[a].TextureName = RecName then
599 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
600 Result := a;
601 Exit;
602 end;
604 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
605 if (RecName = TEXTURE_NAME_WATER) or
606 (RecName = TEXTURE_NAME_ACID1) or
607 (RecName = TEXTURE_NAME_ACID2) then
608 begin
609 SetLength(Textures, Length(Textures)+1);
611 with Textures[High(Textures)] do
612 begin
613 TextureName := RecName;
615 if TextureName = TEXTURE_NAME_WATER then
616 TextureID := TEXTURE_SPECIAL_WATER
617 else
618 if TextureName = TEXTURE_NAME_ACID1 then
619 TextureID := TEXTURE_SPECIAL_ACID1
620 else
621 if TextureName = TEXTURE_NAME_ACID2 then
622 TextureID := TEXTURE_SPECIAL_ACID2;
624 Anim := False;
625 end;
627 result := High(Textures);
628 Exit;
629 end;
631 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
632 WADName := g_ExtractWadName(RecName);
634 WAD := TWADFile.Create();
636 if WADName <> '' then
637 WADName := GameDir+'/wads/'+WADName
638 else
639 WADName := Map;
641 WAD.ReadFile(WADName);
643 txname := RecName;
645 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
646 begin
647 FreeMem(TextureData);
648 RecName := 'COMMON\ALIEN';
649 end;
652 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
653 begin
654 SetLength(Textures, Length(Textures)+1);
655 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
656 Exit;
657 e_GetTextureSize(Textures[High(Textures)].TextureID,
658 @Textures[High(Textures)].Width,
659 @Textures[High(Textures)].Height);
660 FreeMem(TextureData);
661 Textures[High(Textures)].TextureName := {RecName}txname;
662 Textures[High(Textures)].Anim := False;
664 result := High(Textures);
665 end
666 else // Íåò òàêîãî ðåóñðñà â WAD'å
667 begin
668 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
669 if log then
670 begin
671 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
672 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
673 end;
674 end;
676 WAD.Free();
677 end;
679 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
680 var
681 WAD: TWADFile;
682 TextureWAD: PChar = nil;
683 TextData: Pointer = nil;
684 TextureData: Pointer = nil;
685 cfg: TConfig = nil;
686 WADName: String;
687 ResLength: Integer;
688 TextureResource: String;
689 _width, _height, _framecount, _speed: Integer;
690 _backanimation: Boolean;
691 //imgfmt: string;
692 ia: TDynImageDataArray = nil;
693 f, c, frdelay, frloop: Integer;
694 begin
695 result := -1;
697 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
699 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
700 WADName := g_ExtractWadName(RecName);
702 WAD := TWADFile.Create();
703 try
704 if WADName <> '' then
705 WADName := GameDir+'/wads/'+WADName
706 else
707 WADName := Map;
709 WAD.ReadFile(WADName);
711 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
712 begin
713 if log then
714 begin
715 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
716 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
717 end;
718 exit;
719 end;
721 {TEST
722 if WADName = Map then
723 begin
724 //FreeMem(TextureWAD);
725 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
726 end;
729 WAD.FreeWAD();
731 if ResLength < 6 then
732 begin
733 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
734 exit;
735 end;
737 // ýòî ïòèöà? ýòî ñàìîë¸ò?
738 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
739 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
740 begin
741 // íåò, ýòî ñóïåðìåí!
742 if not WAD.ReadMemory(TextureWAD, ResLength) then
743 begin
744 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
745 exit;
746 end;
748 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
749 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
750 begin
751 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
752 exit;
753 end;
755 cfg := TConfig.CreateMem(TextData, ResLength);
757 TextureResource := cfg.ReadStr('', 'resource', '');
758 if TextureResource = '' then
759 begin
760 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
761 exit;
762 end;
764 _width := cfg.ReadInt('', 'framewidth', 0);
765 _height := cfg.ReadInt('', 'frameheight', 0);
766 _framecount := cfg.ReadInt('', 'framecount', 0);
767 _speed := cfg.ReadInt('', 'waitcount', 0);
768 _backanimation := cfg.ReadBool('', 'backanimation', False);
770 cfg.Free();
771 cfg := nil;
773 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
774 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
775 begin
776 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
777 exit;
778 end;
780 WAD.Free();
781 WAD := nil;
783 SetLength(Textures, Length(Textures)+1);
784 with Textures[High(Textures)] do
785 begin
786 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
787 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
788 begin
789 TextureName := RecName;
790 Width := _width;
791 Height := _height;
792 Anim := True;
793 FramesCount := _framecount;
794 Speed := _speed;
795 result := High(Textures);
796 end
797 else
798 begin
799 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
800 end;
801 end;
802 end
803 else
804 begin
805 // try animated image
807 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
808 if length(imgfmt) = 0 then
809 begin
810 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
811 exit;
812 end;
814 GlobalMetadata.ClearMetaItems();
815 GlobalMetadata.ClearMetaItemsForSaving();
816 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
817 begin
818 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
819 exit;
820 end;
821 if length(ia) = 0 then
822 begin
823 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
824 exit;
825 end;
827 WAD.Free();
828 WAD := nil;
830 _width := ia[0].width;
831 _height := ia[0].height;
832 _framecount := length(ia);
833 _speed := 1;
834 _backanimation := false;
835 frdelay := -1;
836 frloop := -666;
837 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
838 begin
839 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
840 try
841 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
842 frdelay := f;
843 if f < 0 then f := 0;
844 // rounding ;-)
845 c := f mod 28;
846 if c < 13 then c := 0 else c := 1;
847 f := (f div 28)+c;
848 if f < 1 then f := 1 else if f > 255 then f := 255;
849 _speed := f;
850 except
851 end;
852 end;
853 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
854 begin
855 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
856 try
857 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
858 frloop := f;
859 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
860 except
861 end;
862 end;
863 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
864 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
865 f := ord(_backanimation);
866 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);
868 SetLength(Textures, Length(Textures)+1);
869 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
870 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
871 begin
872 Textures[High(Textures)].TextureName := RecName;
873 Textures[High(Textures)].Width := _width;
874 Textures[High(Textures)].Height := _height;
875 Textures[High(Textures)].Anim := True;
876 Textures[High(Textures)].FramesCount := length(ia);
877 Textures[High(Textures)].Speed := _speed;
878 result := High(Textures);
879 //writeln(' CREATED!');
880 end
881 else
882 begin
883 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
884 end;
885 end;
886 finally
887 for f := 0 to High(ia) do FreeImage(ia[f]);
888 WAD.Free();
889 cfg.Free();
890 if TextureWAD <> nil then FreeMem(TextureWAD);
891 if TextData <> nil then FreeMem(TextData);
892 if TextureData <> nil then FreeMem(TextureData);
893 end;
894 end;
896 procedure CreateItem(Item: TItemRec_1);
897 begin
898 if g_Game_IsClient then Exit;
900 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
901 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
902 Exit;
904 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
905 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
906 end;
908 procedure CreateArea(Area: TAreaRec_1);
909 var
910 a: Integer;
911 id: DWORD;
912 begin
913 case Area.AreaType of
914 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
915 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
916 begin
917 SetLength(RespawnPoints, Length(RespawnPoints)+1);
918 with RespawnPoints[High(RespawnPoints)] do
919 begin
920 X := Area.X;
921 Y := Area.Y;
922 Direction := TDirection(Area.Direction);
924 case Area.AreaType of
925 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
926 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
927 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
928 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
929 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
930 end;
931 end;
932 end;
934 AREA_REDFLAG, AREA_BLUEFLAG:
935 begin
936 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
938 if FlagPoints[a] <> nil then Exit;
940 New(FlagPoints[a]);
942 with FlagPoints[a]^ do
943 begin
944 X := Area.X-FLAGRECT.X;
945 Y := Area.Y-FLAGRECT.Y;
946 Direction := TDirection(Area.Direction);
947 end;
949 with gFlags[a] do
950 begin
951 case a of
952 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
953 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
954 end;
956 Animation := TAnimation.Create(id, True, 8);
957 Obj.Rect := FLAGRECT;
959 g_Map_ResetFlag(a);
960 end;
961 end;
963 AREA_DOMFLAG:
964 begin
965 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
966 with DOMFlagPoints[High(DOMFlagPoints)] do
967 begin
968 X := Area.X;
969 Y := Area.Y;
970 Direction := TDirection(Area.Direction);
971 end;
973 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
974 end;
975 end;
976 end;
978 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
979 var
980 _trigger: TTrigger;
981 begin
982 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
984 with _trigger do
985 begin
986 X := Trigger.X;
987 Y := Trigger.Y;
988 Width := Trigger.Width;
989 Height := Trigger.Height;
990 Enabled := ByteBool(Trigger.Enabled);
991 TexturePanel := Trigger.TexturePanel;
992 TexturePanelType := fTexturePanel1Type;
993 ShotPanelType := fTexturePanel2Type;
994 TriggerType := Trigger.TriggerType;
995 ActivateType := Trigger.ActivateType;
996 Keys := Trigger.Keys;
997 Data.Default := Trigger.DATA;
998 end;
1000 g_Triggers_Create(_trigger);
1001 end;
1003 procedure CreateMonster(monster: TMonsterRec_1);
1004 var
1005 a: Integer;
1006 mon: TMonster;
1007 begin
1008 if g_Game_IsClient then Exit;
1010 if (gGameSettings.GameType = GT_SINGLE)
1011 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1012 begin
1013 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1015 if gTriggers <> nil then
1016 begin
1017 for a := 0 to High(gTriggers) do
1018 begin
1019 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1020 begin
1021 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1022 end;
1023 end;
1024 end;
1026 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1027 end;
1028 end;
1030 procedure g_Map_ReAdd_DieTriggers();
1032 function monsDieTrig (mon: TMonster): Boolean;
1033 var
1034 a: Integer;
1035 begin
1036 result := false; // don't stop
1037 mon.ClearTriggers();
1038 for a := 0 to High(gTriggers) do
1039 begin
1040 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1041 begin
1042 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1043 end;
1044 end;
1045 end;
1047 begin
1048 if g_Game_IsClient then Exit;
1050 g_Mons_ForEach(monsDieTrig);
1051 end;
1053 function extractWadName(resourceName: string): string;
1054 var
1055 posN: Integer;
1056 begin
1057 posN := Pos(':', resourceName);
1058 if posN > 0 then
1059 Result:= Copy(resourceName, 0, posN-1)
1060 else
1061 Result := '';
1062 end;
1064 procedure addResToExternalResList(res: string);
1065 begin
1066 res := extractWadName(res);
1067 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1068 gExternalResources.Add(res);
1069 end;
1071 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1072 var
1073 textures: TTexturesRec1Array;
1074 mapHeader: TMapHeaderRec_1;
1075 i: integer;
1076 resFile: String = '';
1077 begin
1078 if gExternalResources = nil then
1079 gExternalResources := TStringList.Create;
1081 gExternalResources.Clear;
1082 textures := mapReader.GetTextures();
1083 for i := 0 to High(textures) do
1084 begin
1085 addResToExternalResList(resFile);
1086 end;
1088 textures := nil;
1090 mapHeader := mapReader.GetMapHeader;
1092 addResToExternalResList(mapHeader.MusicName);
1093 addResToExternalResList(mapHeader.SkyName);
1094 end;
1096 procedure mapCreateGrid ();
1097 var
1098 mapX0: Integer = $3fffffff;
1099 mapY0: Integer = $3fffffff;
1100 mapX1: Integer = -$3fffffff;
1101 mapY1: Integer = -$3fffffff;
1103 procedure calcBoundingBox (constref panels: TPanelArray);
1104 var
1105 idx: Integer;
1106 pan: TPanel;
1107 begin
1108 for idx := 0 to High(panels) do
1109 begin
1110 pan := panels[idx];
1111 if not pan.visvalid then continue;
1112 if (pan.Width < 1) or (pan.Height < 1) then continue;
1113 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1114 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1115 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1116 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1117 end;
1118 end;
1120 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1121 var
1122 idx: Integer;
1123 pan: TPanel;
1124 begin
1125 tag := panelTypeToTag(tag);
1126 for idx := High(panels) downto 0 do
1127 begin
1128 pan := panels[idx];
1129 pan.tag := tag;
1130 if not pan.visvalid then continue;
1131 gMapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1132 mapTree.insertObject(pan, tag, true); // as static object
1133 end;
1134 end;
1136 begin
1137 gMapGrid.Free();
1138 gMapGrid := nil;
1139 mapTree.Free();
1140 mapTree := nil;
1142 calcBoundingBox(gWalls);
1143 calcBoundingBox(gRenderBackgrounds);
1144 calcBoundingBox(gRenderForegrounds);
1145 calcBoundingBox(gWater);
1146 calcBoundingBox(gAcid1);
1147 calcBoundingBox(gAcid2);
1148 calcBoundingBox(gSteps);
1149 calcBoundingBox(gLifts);
1150 calcBoundingBox(gBlockMon);
1152 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1154 gMapGrid := TPanelGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1155 mapTree := TDynAABBTreeMap.Create();
1157 addPanelsToGrid(gWalls, PANEL_WALL);
1158 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1159 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1160 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1161 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1162 addPanelsToGrid(gWater, PANEL_WATER);
1163 addPanelsToGrid(gAcid1, PANEL_ACID1);
1164 addPanelsToGrid(gAcid2, PANEL_ACID2);
1165 addPanelsToGrid(gSteps, PANEL_STEP);
1166 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1167 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1169 gMapGrid.dumpStats();
1170 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1171 mapTree.forEachLeaf(nil);
1172 end;
1174 function g_Map_Load(Res: String): Boolean;
1175 const
1176 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1177 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1178 var
1179 WAD: TWADFile;
1180 MapReader: TMapReader_1;
1181 Header: TMapHeaderRec_1;
1182 _textures: TTexturesRec1Array;
1183 _texnummap: array of Integer; // `_textures` -> `Textures`
1184 panels: TPanelsRec1Array;
1185 items: TItemsRec1Array;
1186 monsters: TMonsterRec1Array;
1187 areas: TAreasRec1Array;
1188 triggers: TTriggersRec1Array;
1189 a, b, c, k: Integer;
1190 PanelID: DWORD;
1191 AddTextures: TAddTextureArray;
1192 texture: TTextureRec_1;
1193 TriggersTable: Array of record
1194 TexturePanel: Integer;
1195 LiftPanel: Integer;
1196 DoorPanel: Integer;
1197 ShotPanel: Integer;
1198 end;
1199 FileName, mapResName, s, TexName: String;
1200 Data: Pointer;
1201 Len: Integer;
1202 ok, isAnim, trigRef: Boolean;
1203 CurTex, ntn: Integer;
1205 begin
1206 gMapGrid.Free();
1207 gMapGrid := nil;
1208 mapTree.Free();
1209 mapTree := nil;
1211 Result := False;
1212 gMapInfo.Map := Res;
1213 TriggersTable := nil;
1214 FillChar(texture, SizeOf(texture), 0);
1216 sfsGCDisable(); // temporary disable removing of temporary volumes
1217 try
1218 // Çàãðóçêà WAD:
1219 FileName := g_ExtractWadName(Res);
1220 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1221 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1223 WAD := TWADFile.Create();
1224 if not WAD.ReadFile(FileName) then
1225 begin
1226 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1227 WAD.Free();
1228 Exit;
1229 end;
1230 //k8: why loader ignores path here?
1231 mapResName := g_ExtractFileName(Res);
1232 if not WAD.GetMapResource(mapResName, Data, Len) then
1233 begin
1234 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1235 WAD.Free();
1236 Exit;
1237 end;
1239 WAD.Free();
1241 // Çàãðóçêà êàðòû:
1242 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1243 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1244 MapReader := TMapReader_1.Create();
1246 if not MapReader.LoadMap(Data) then
1247 begin
1248 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1249 FreeMem(Data);
1250 MapReader.Free();
1251 Exit;
1252 end;
1254 FreeMem(Data);
1255 generateExternalResourcesList(MapReader);
1256 // Çàãðóçêà òåêñòóð:
1257 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1258 _textures := MapReader.GetTextures();
1259 _texnummap := nil;
1261 // Äîáàâëåíèå òåêñòóð â Textures[]:
1262 if _textures <> nil then
1263 begin
1264 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1265 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1266 SetLength(_texnummap, length(_textures));
1268 for a := 0 to High(_textures) do
1269 begin
1270 SetLength(s, 64);
1271 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1272 for b := 1 to Length(s) do
1273 if s[b] = #0 then
1274 begin
1275 SetLength(s, b-1);
1276 Break;
1277 end;
1278 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1279 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1280 // Àíèìèðîâàííàÿ òåêñòóðà:
1281 if ByteBool(_textures[a].Anim) then
1282 begin
1283 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1284 if ntn < 0 then
1285 begin
1286 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1287 ntn := CreateNullTexture(_textures[a].Resource);
1288 end;
1289 end
1290 else // Îáû÷íàÿ òåêñòóðà:
1291 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1292 if ntn < 0 then
1293 begin
1294 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1295 ntn := CreateNullTexture(_textures[a].Resource);
1296 end;
1298 _texnummap[a] := ntn; // fix texture number
1299 g_Game_StepLoading();
1300 end;
1301 end;
1303 // Çàãðóçêà òðèããåðîâ:
1304 gTriggerClientID := 0;
1305 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1306 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1307 triggers := MapReader.GetTriggers();
1309 // Çàãðóçêà ïàíåëåé:
1310 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1311 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1312 panels := MapReader.GetPanels();
1314 // check texture numbers for panels
1315 for a := 0 to High(panels) do
1316 begin
1317 if panels[a].TextureNum > High(_textures) then
1318 begin
1319 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1320 result := false;
1321 exit;
1322 end;
1323 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1324 end;
1326 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1327 if triggers <> nil then
1328 begin
1329 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1330 SetLength(TriggersTable, Length(triggers));
1331 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1333 for a := 0 to High(TriggersTable) do
1334 begin
1335 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1336 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1337 // Ëèôòû:
1338 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1339 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1340 else
1341 TriggersTable[a].LiftPanel := -1;
1342 // Äâåðè:
1343 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1344 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1345 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1346 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1347 else
1348 TriggersTable[a].DoorPanel := -1;
1349 // Òóðåëü:
1350 if triggers[a].TriggerType = TRIGGER_SHOT then
1351 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1352 else
1353 TriggersTable[a].ShotPanel := -1;
1355 g_Game_StepLoading();
1356 end;
1357 end;
1359 // Ñîçäàåì ïàíåëè:
1360 if panels <> nil then
1361 begin
1362 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1363 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1365 for a := 0 to High(panels) do
1366 begin
1367 SetLength(AddTextures, 0);
1368 trigRef := False;
1369 CurTex := -1;
1370 if _textures <> nil then
1371 begin
1372 texture := _textures[panels[a].TextureNum];
1373 ok := True;
1374 end
1375 else
1376 ok := False;
1378 if ok then
1379 begin
1380 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1381 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1382 ok := False;
1383 if (TriggersTable <> nil) and (_textures <> nil) then
1384 for b := 0 to High(TriggersTable) do
1385 if (TriggersTable[b].TexturePanel = a)
1386 or (TriggersTable[b].ShotPanel = a) then
1387 begin
1388 trigRef := True;
1389 ok := True;
1390 Break;
1391 end;
1392 end;
1394 if ok then
1395 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1396 SetLength(s, 64);
1397 CopyMemory(@s[1], @texture.Resource[0], 64);
1398 // Èçìåðÿåì äëèíó:
1399 Len := Length(s);
1400 for c := Len downto 1 do
1401 if s[c] <> #0 then
1402 begin
1403 Len := c;
1404 Break;
1405 end;
1406 SetLength(s, Len);
1408 // Ñïåö-òåêñòóðû çàïðåùåíû:
1409 if g_Map_IsSpecialTexture(s) then
1410 ok := False
1411 else
1412 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1413 ok := g_Texture_NumNameFindStart(s);
1415 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1416 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1417 if ok then
1418 begin
1419 k := NNF_NAME_BEFORE;
1420 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1421 while ok or (k = NNF_NAME_BEFORE) or
1422 (k = NNF_NAME_EQUALS) do
1423 begin
1424 k := g_Texture_NumNameFindNext(TexName);
1426 if (k = NNF_NAME_BEFORE) or
1427 (k = NNF_NAME_AFTER) then
1428 begin
1429 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1430 if ByteBool(texture.Anim) then
1431 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1432 isAnim := True;
1433 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1434 if not ok then
1435 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1436 isAnim := False;
1437 ok := CreateTexture(TexName, FileName, False) >= 0;
1438 end;
1439 end
1440 else
1441 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1442 isAnim := False;
1443 ok := CreateTexture(TexName, FileName, False) >= 0;
1444 if not ok then
1445 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1446 isAnim := True;
1447 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1448 end;
1449 end;
1451 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1452 if ok then
1453 begin
1454 for c := 0 to High(Textures) do
1455 if Textures[c].TextureName = TexName then
1456 begin
1457 SetLength(AddTextures, Length(AddTextures)+1);
1458 AddTextures[High(AddTextures)].Texture := c;
1459 AddTextures[High(AddTextures)].Anim := isAnim;
1460 Break;
1461 end;
1462 end;
1463 end
1464 else
1465 if k = NNF_NAME_EQUALS then
1466 begin
1467 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1468 SetLength(AddTextures, Length(AddTextures)+1);
1469 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1470 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1471 CurTex := High(AddTextures);
1472 ok := True;
1473 end
1474 else // NNF_NO_NAME
1475 ok := False;
1476 end; // while ok...
1478 ok := True;
1479 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1480 end; // if ok - ññûëàþòñÿ òðèããåðû
1482 if not ok then
1483 begin
1484 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1485 SetLength(AddTextures, 1);
1486 AddTextures[0].Texture := panels[a].TextureNum;
1487 AddTextures[0].Anim := ByteBool(texture.Anim);
1488 CurTex := 0;
1489 end;
1491 //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);
1493 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1494 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1496 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1497 if TriggersTable <> nil then
1498 for b := 0 to High(TriggersTable) do
1499 begin
1500 // Òðèããåð äâåðè/ëèôòà:
1501 if (TriggersTable[b].LiftPanel = a) or
1502 (TriggersTable[b].DoorPanel = a) then
1503 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1504 // Òðèããåð ñìåíû òåêñòóðû:
1505 if TriggersTable[b].TexturePanel = a then
1506 triggers[b].TexturePanel := PanelID;
1507 // Òðèããåð "Òóðåëü":
1508 if TriggersTable[b].ShotPanel = a then
1509 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1510 end;
1512 g_Game_StepLoading();
1513 end;
1514 end;
1516 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1517 if (triggers <> nil) and not gLoadGameMode then
1518 begin
1519 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1520 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1521 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1522 for a := 0 to High(triggers) do
1523 begin
1524 if triggers[a].TexturePanel <> -1 then
1525 b := panels[TriggersTable[a].TexturePanel].PanelType
1526 else
1527 b := 0;
1528 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1529 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1530 c := panels[TriggersTable[a].ShotPanel].PanelType
1531 else
1532 c := 0;
1533 CreateTrigger(triggers[a], b, c);
1534 end;
1535 end;
1537 // Çàãðóçêà ïðåäìåòîâ:
1538 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1539 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1540 items := MapReader.GetItems();
1542 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1543 if (items <> nil) and not gLoadGameMode then
1544 begin
1545 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1546 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1547 for a := 0 to High(items) do
1548 CreateItem(Items[a]);
1549 end;
1551 // Çàãðóçêà îáëàñòåé:
1552 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1553 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1554 areas := MapReader.GetAreas();
1556 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1557 if areas <> nil then
1558 begin
1559 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1560 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1561 for a := 0 to High(areas) do
1562 CreateArea(areas[a]);
1563 end;
1565 // Çàãðóçêà ìîíñòðîâ:
1566 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1567 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1568 monsters := MapReader.GetMonsters();
1570 gTotalMonsters := 0;
1572 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1573 if (monsters <> nil) and not gLoadGameMode then
1574 begin
1575 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1576 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1577 for a := 0 to High(monsters) do
1578 CreateMonster(monsters[a]);
1579 end;
1581 // Çàãðóçêà îïèñàíèÿ êàðòû:
1582 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1583 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1584 Header := MapReader.GetMapHeader();
1586 MapReader.Free();
1588 with gMapInfo do
1589 begin
1590 Name := Header.MapName;
1591 Description := Header.MapDescription;
1592 Author := Header.MapAuthor;
1593 MusicName := Header.MusicName;
1594 SkyName := Header.SkyName;
1595 Height := Header.Height;
1596 Width := Header.Width;
1597 end;
1599 // Çàãðóçêà íåáà:
1600 if gMapInfo.SkyName <> '' then
1601 begin
1602 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1603 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1604 FileName := g_ExtractWadName(gMapInfo.SkyName);
1606 if FileName <> '' then
1607 FileName := GameDir+'/wads/'+FileName
1608 else
1609 begin
1610 FileName := g_ExtractWadName(Res);
1611 end;
1613 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1614 if g_Texture_CreateWAD(BackID, s) then
1615 begin
1616 g_Game_SetupScreenSize();
1617 end
1618 else
1619 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1620 end;
1622 // Çàãðóçêà ìóçûêè:
1623 ok := False;
1624 if gMapInfo.MusicName <> '' then
1625 begin
1626 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1627 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1628 FileName := g_ExtractWadName(gMapInfo.MusicName);
1630 if FileName <> '' then
1631 FileName := GameDir+'/wads/'+FileName
1632 else
1633 begin
1634 FileName := g_ExtractWadName(Res);
1635 end;
1637 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1638 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1639 ok := True
1640 else
1641 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1642 end;
1644 // Îñòàëüíûå óñòàíâêè:
1645 CreateDoorMap();
1646 CreateLiftMap();
1648 g_Items_Init();
1649 g_Weapon_Init();
1650 g_Monsters_Init();
1652 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1653 if not gLoadGameMode then
1654 g_GFX_Init();
1656 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1657 _textures := nil;
1658 panels := nil;
1659 items := nil;
1660 areas := nil;
1661 triggers := nil;
1662 TriggersTable := nil;
1663 AddTextures := nil;
1665 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1666 if ok and (not gLoadGameMode) then
1667 begin
1668 gMusic.SetByName(gMapInfo.MusicName);
1669 gMusic.Play();
1670 end
1671 else
1672 gMusic.SetByName('');
1673 finally
1674 sfsGCEnable(); // enable releasing unused volumes
1675 end;
1677 e_WriteLog('Creating map grid', MSG_NOTIFY);
1678 mapCreateGrid();
1680 e_WriteLog('Done loading map.', MSG_NOTIFY);
1681 Result := True;
1682 end;
1684 function g_Map_GetMapInfo(Res: String): TMapInfo;
1685 var
1686 WAD: TWADFile;
1687 MapReader: TMapReader_1;
1688 Header: TMapHeaderRec_1;
1689 FileName: String;
1690 Data: Pointer;
1691 Len: Integer;
1692 begin
1693 FillChar(Result, SizeOf(Result), 0);
1694 FileName := g_ExtractWadName(Res);
1696 WAD := TWADFile.Create();
1697 if not WAD.ReadFile(FileName) then
1698 begin
1699 WAD.Free();
1700 Exit;
1701 end;
1703 //k8: it ignores path again
1704 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1705 begin
1706 WAD.Free();
1707 Exit;
1708 end;
1710 WAD.Free();
1712 MapReader := TMapReader_1.Create();
1714 if not MapReader.LoadMap(Data) then
1715 begin
1716 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1717 ZeroMemory(@Header, SizeOf(Header));
1718 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1719 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1720 end
1721 else
1722 begin
1723 Header := MapReader.GetMapHeader();
1724 Result.Name := Header.MapName;
1725 Result.Description := Header.MapDescription;
1726 end;
1728 FreeMem(Data);
1729 MapReader.Free();
1731 Result.Map := Res;
1732 Result.Author := Header.MapAuthor;
1733 Result.Height := Header.Height;
1734 Result.Width := Header.Width;
1735 end;
1737 function g_Map_GetMapsList(WADName: string): SArray;
1738 var
1739 WAD: TWADFile;
1740 a: Integer;
1741 ResList: SArray;
1742 begin
1743 Result := nil;
1744 WAD := TWADFile.Create();
1745 if not WAD.ReadFile(WADName) then
1746 begin
1747 WAD.Free();
1748 Exit;
1749 end;
1750 ResList := WAD.GetMapResources();
1751 if ResList <> nil then
1752 begin
1753 for a := 0 to High(ResList) do
1754 begin
1755 SetLength(Result, Length(Result)+1);
1756 Result[High(Result)] := ResList[a];
1757 end;
1758 end;
1759 WAD.Free();
1760 end;
1762 function g_Map_Exist(Res: string): Boolean;
1763 var
1764 WAD: TWADFile;
1765 FileName, mnn: string;
1766 ResList: SArray;
1767 a: Integer;
1768 begin
1769 Result := False;
1771 FileName := addWadExtension(g_ExtractWadName(Res));
1773 WAD := TWADFile.Create;
1774 if not WAD.ReadFile(FileName) then
1775 begin
1776 WAD.Free();
1777 Exit;
1778 end;
1780 ResList := WAD.GetMapResources();
1781 WAD.Free();
1783 mnn := g_ExtractFileName(Res);
1784 if ResList <> nil then
1785 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1786 begin
1787 Result := True;
1788 Exit;
1789 end;
1790 end;
1792 procedure g_Map_Free();
1793 var
1794 a: Integer;
1796 procedure FreePanelArray(var panels: TPanelArray);
1797 var
1798 i: Integer;
1800 begin
1801 if panels <> nil then
1802 begin
1803 for i := 0 to High(panels) do
1804 panels[i].Free();
1805 panels := nil;
1806 end;
1807 end;
1809 begin
1810 g_GFX_Free();
1811 g_Weapon_Free();
1812 g_Items_Free();
1813 g_Triggers_Free();
1814 g_Monsters_Free();
1816 RespawnPoints := nil;
1817 if FlagPoints[FLAG_RED] <> nil then
1818 begin
1819 Dispose(FlagPoints[FLAG_RED]);
1820 FlagPoints[FLAG_RED] := nil;
1821 end;
1822 if FlagPoints[FLAG_BLUE] <> nil then
1823 begin
1824 Dispose(FlagPoints[FLAG_BLUE]);
1825 FlagPoints[FLAG_BLUE] := nil;
1826 end;
1827 //DOMFlagPoints := nil;
1829 //gDOMFlags := nil;
1831 if Textures <> nil then
1832 begin
1833 for a := 0 to High(Textures) do
1834 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1835 if Textures[a].Anim then
1836 g_Frames_DeleteByID(Textures[a].FramesID)
1837 else
1838 if Textures[a].TextureID <> TEXTURE_NONE then
1839 e_DeleteTexture(Textures[a].TextureID);
1841 Textures := nil;
1842 end;
1844 FreePanelArray(gWalls);
1845 FreePanelArray(gRenderBackgrounds);
1846 FreePanelArray(gRenderForegrounds);
1847 FreePanelArray(gWater);
1848 FreePanelArray(gAcid1);
1849 FreePanelArray(gAcid2);
1850 FreePanelArray(gSteps);
1851 FreePanelArray(gLifts);
1852 FreePanelArray(gBlockMon);
1854 if BackID <> DWORD(-1) then
1855 begin
1856 gBackSize.X := 0;
1857 gBackSize.Y := 0;
1858 e_DeleteTexture(BackID);
1859 BackID := DWORD(-1);
1860 end;
1862 g_Game_StopAllSounds(False);
1863 gMusic.FreeSound();
1864 g_Sound_Delete(gMapInfo.MusicName);
1866 gMapInfo.Name := '';
1867 gMapInfo.Description := '';
1868 gMapInfo.MusicName := '';
1869 gMapInfo.Height := 0;
1870 gMapInfo.Width := 0;
1872 gDoorMap := nil;
1873 gLiftMap := nil;
1875 PanelByID := nil;
1876 end;
1878 procedure g_Map_Update();
1879 var
1880 a, d, j: Integer;
1881 m: Word;
1882 s: String;
1884 procedure UpdatePanelArray(var panels: TPanelArray);
1885 var
1886 i: Integer;
1888 begin
1889 if panels <> nil then
1890 for i := 0 to High(panels) do
1891 panels[i].Update();
1892 end;
1894 begin
1895 UpdatePanelArray(gWalls);
1896 UpdatePanelArray(gRenderBackgrounds);
1897 UpdatePanelArray(gRenderForegrounds);
1898 UpdatePanelArray(gWater);
1899 UpdatePanelArray(gAcid1);
1900 UpdatePanelArray(gAcid2);
1901 UpdatePanelArray(gSteps);
1903 if gGameSettings.GameMode = GM_CTF then
1904 begin
1905 for a := FLAG_RED to FLAG_BLUE do
1906 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1907 with gFlags[a] do
1908 begin
1909 if gFlags[a].Animation <> nil then
1910 gFlags[a].Animation.Update();
1912 m := g_Obj_Move(@Obj, True, True);
1914 if gTime mod (GAME_TICK*2) <> 0 then
1915 Continue;
1917 // Ñîïðîòèâëåíèå âîçäóõà:
1918 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1920 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1921 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1922 begin
1923 g_Map_ResetFlag(a);
1924 gFlags[a].CaptureTime := 0;
1925 if a = FLAG_RED then
1926 s := _lc[I_PLAYER_FLAG_RED]
1927 else
1928 s := _lc[I_PLAYER_FLAG_BLUE];
1929 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1931 if g_Game_IsNet then
1932 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1933 Continue;
1934 end;
1936 if Count > 0 then
1937 Count := Count - 1;
1939 // Èãðîê áåðåò ôëàã:
1940 if gPlayers <> nil then
1941 begin
1942 j := Random(Length(gPlayers)) - 1;
1944 for d := 0 to High(gPlayers) do
1945 begin
1946 Inc(j);
1947 if j > High(gPlayers) then
1948 j := 0;
1950 if gPlayers[j] <> nil then
1951 if gPlayers[j].Live and
1952 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1953 begin
1954 if gPlayers[j].GetFlag(a) then
1955 Break;
1956 end;
1957 end;
1958 end;
1959 end;
1960 end;
1961 end;
1964 // old algo
1965 procedure g_Map_DrawPanels (PanelType: Word);
1967 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
1968 var
1969 idx: Integer;
1970 begin
1971 if (panels <> nil) then
1972 begin
1973 // alas, no visible set
1974 for idx := 0 to High(panels) do
1975 begin
1976 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1977 end;
1978 end;
1979 end;
1981 begin
1982 case PanelType of
1983 PANEL_WALL: DrawPanels(gWalls);
1984 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
1985 PANEL_BACK: DrawPanels(gRenderBackgrounds);
1986 PANEL_FORE: DrawPanels(gRenderForegrounds);
1987 PANEL_WATER: DrawPanels(gWater);
1988 PANEL_ACID1: DrawPanels(gAcid1);
1989 PANEL_ACID2: DrawPanels(gAcid2);
1990 PANEL_STEP: DrawPanels(gSteps);
1991 end;
1992 end;
1995 // new algo
1996 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
1997 function checker (pan: TPanel; tag: Integer): Boolean;
1998 begin
1999 result := false; // don't stop, ever
2000 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2001 gDrawPanelList.insert(pan);
2002 end;
2004 begin
2005 dplClear();
2006 //tagmask := panelTypeToTag(PanelType);
2008 if gdbg_map_use_tree_draw then
2009 begin
2010 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2011 end
2012 else
2013 begin
2014 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2015 end;
2016 // list will be rendered in `g_game.DrawPlayer()`
2017 end;
2020 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2021 function checker (pan: TPanel; tag: Integer): Boolean;
2022 begin
2023 result := false; // don't stop, ever
2024 pan.DrawShadowVolume(lightX, lightY, radius);
2025 end;
2027 begin
2028 if gdbg_map_use_tree_draw then
2029 begin
2030 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2031 end
2032 else
2033 begin
2034 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2035 end;
2036 end;
2039 procedure g_Map_DrawBack(dx, dy: Integer);
2040 begin
2041 if gDrawBackGround and (BackID <> DWORD(-1)) then
2042 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2043 else
2044 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2045 end;
2047 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2048 PanelType: Word; b1x3: Boolean): Boolean;
2049 var
2050 a, h: Integer;
2051 begin
2052 Result := False;
2054 if WordBool(PanelType and PANEL_WALL) then
2055 if gWalls <> nil then
2056 begin
2057 h := High(gWalls);
2059 for a := 0 to h do
2060 if gWalls[a].Enabled and
2061 g_Collide(X, Y, Width, Height,
2062 gWalls[a].X, gWalls[a].Y,
2063 gWalls[a].Width, gWalls[a].Height) then
2064 begin
2065 Result := True;
2066 Exit;
2067 end;
2068 end;
2070 if WordBool(PanelType and PANEL_WATER) then
2071 if gWater <> nil then
2072 begin
2073 h := High(gWater);
2075 for a := 0 to h do
2076 if g_Collide(X, Y, Width, Height,
2077 gWater[a].X, gWater[a].Y,
2078 gWater[a].Width, gWater[a].Height) then
2079 begin
2080 Result := True;
2081 Exit;
2082 end;
2083 end;
2085 if WordBool(PanelType and PANEL_ACID1) then
2086 if gAcid1 <> nil then
2087 begin
2088 h := High(gAcid1);
2090 for a := 0 to h do
2091 if g_Collide(X, Y, Width, Height,
2092 gAcid1[a].X, gAcid1[a].Y,
2093 gAcid1[a].Width, gAcid1[a].Height) then
2094 begin
2095 Result := True;
2096 Exit;
2097 end;
2098 end;
2100 if WordBool(PanelType and PANEL_ACID2) then
2101 if gAcid2 <> nil then
2102 begin
2103 h := High(gAcid2);
2105 for a := 0 to h do
2106 if g_Collide(X, Y, Width, Height,
2107 gAcid2[a].X, gAcid2[a].Y,
2108 gAcid2[a].Width, gAcid2[a].Height) then
2109 begin
2110 Result := True;
2111 Exit;
2112 end;
2113 end;
2115 if WordBool(PanelType and PANEL_STEP) then
2116 if gSteps <> nil then
2117 begin
2118 h := High(gSteps);
2120 for a := 0 to h do
2121 if g_Collide(X, Y, Width, Height,
2122 gSteps[a].X, gSteps[a].Y,
2123 gSteps[a].Width, gSteps[a].Height) then
2124 begin
2125 Result := True;
2126 Exit;
2127 end;
2128 end;
2130 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2131 if gLifts <> nil then
2132 begin
2133 h := High(gLifts);
2135 for a := 0 to h do
2136 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2137 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2138 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2139 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2140 g_Collide(X, Y, Width, Height,
2141 gLifts[a].X, gLifts[a].Y,
2142 gLifts[a].Width, gLifts[a].Height) then
2143 begin
2144 Result := True;
2145 Exit;
2146 end;
2147 end;
2149 if WordBool(PanelType and PANEL_BLOCKMON) then
2150 if gBlockMon <> nil then
2151 begin
2152 h := High(gBlockMon);
2154 for a := 0 to h do
2155 if ( (not b1x3) or
2156 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2157 g_Collide(X, Y, Width, Height,
2158 gBlockMon[a].X, gBlockMon[a].Y,
2159 gBlockMon[a].Width, gBlockMon[a].Height) then
2160 begin
2161 Result := True;
2162 Exit;
2163 end;
2164 end;
2165 end;
2167 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2168 var
2169 texid: DWORD;
2171 function checkPanels (constref panels: TPanelArray): Boolean;
2172 var
2173 a: Integer;
2174 begin
2175 result := false;
2176 if panels = nil then exit;
2177 for a := 0 to High(panels) do
2178 begin
2179 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2180 begin
2181 result := true;
2182 texid := panels[a].GetTextureID();
2183 exit;
2184 end;
2185 end;
2186 end;
2188 begin
2189 texid := TEXTURE_NONE;
2190 result := texid;
2191 if not checkPanels(gWater) then
2192 if not checkPanels(gAcid1) then
2193 if not checkPanels(gAcid2) then exit;
2194 result := texid;
2195 end;
2198 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2199 function checker (pan: TPanel; tag: Integer): Boolean;
2200 begin
2201 result := false; // don't stop, ever
2203 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2204 begin
2205 if not pan.Enabled then exit;
2206 end;
2208 if ((tag and GridTagLift) <> 0) then
2209 begin
2210 result :=
2211 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2212 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2213 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2214 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) and
2215 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2216 exit;
2217 end;
2219 if ((tag and GridTagBlockMon) <> 0) then
2220 begin
2221 result := ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2222 exit;
2223 end;
2225 // other shit
2226 result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2227 end;
2229 var
2230 tagmask: Integer = 0;
2231 begin
2232 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2233 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2234 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2235 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2236 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2237 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2238 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2240 if (tagmask = 0) then begin result := false; exit; end; // just in case
2242 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2243 if gdbg_map_use_accel_coldet then
2244 begin
2245 if gdbg_map_use_tree_coldet then
2246 begin
2247 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2248 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2249 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2250 begin
2251 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2252 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2253 end;
2254 end
2255 else
2256 begin
2257 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2258 end;
2259 end
2260 else
2261 begin
2262 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2263 end;
2264 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2265 end;
2268 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2269 var
2270 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2271 texid: DWORD;
2273 // slightly different from the old code, but meh...
2274 function checker (pan: TPanel; tag: Integer): Boolean;
2275 begin
2276 result := false; // don't stop, ever
2277 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2278 // check priorities
2279 case cctype of
2280 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2281 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2282 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2283 end;
2284 // collision?
2285 if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2286 // yeah
2287 texid := pan.GetTextureID();
2288 // water? water has the highest priority, so stop right here
2289 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2290 // acid2?
2291 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2292 // acid1?
2293 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2294 end;
2296 begin
2297 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2298 if gdbg_map_use_accel_coldet then
2299 begin
2300 texid := TEXTURE_NONE;
2301 if gdbg_map_use_tree_coldet then
2302 begin
2303 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2304 end
2305 else
2306 begin
2307 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2308 end;
2309 result := texid;
2310 end
2311 else
2312 begin
2313 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2314 end;
2315 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2316 end;
2318 procedure g_Map_EnableWall(ID: DWORD);
2319 begin
2320 with gWalls[ID] do
2321 begin
2322 Enabled := True;
2323 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2325 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2326 end;
2327 end;
2329 procedure g_Map_DisableWall(ID: DWORD);
2330 begin
2331 with gWalls[ID] do
2332 begin
2333 Enabled := False;
2334 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2336 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2337 end;
2338 end;
2340 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2341 var
2342 tp: TPanel;
2343 begin
2344 case PanelType of
2345 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2346 tp := gWalls[ID];
2347 PANEL_FORE:
2348 tp := gRenderForegrounds[ID];
2349 PANEL_BACK:
2350 tp := gRenderBackgrounds[ID];
2351 PANEL_WATER:
2352 tp := gWater[ID];
2353 PANEL_ACID1:
2354 tp := gAcid1[ID];
2355 PANEL_ACID2:
2356 tp := gAcid2[ID];
2357 PANEL_STEP:
2358 tp := gSteps[ID];
2359 else
2360 Exit;
2361 end;
2363 tp.NextTexture(AnimLoop);
2364 if g_Game_IsServer and g_Game_IsNet then
2365 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2366 end;
2368 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2369 begin
2370 if gLifts[ID].LiftType = t then
2371 Exit;
2373 with gLifts[ID] do
2374 begin
2375 LiftType := t;
2377 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2379 if LiftType = 0 then
2380 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2381 else if LiftType = 1 then
2382 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2383 else if LiftType = 2 then
2384 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2385 else if LiftType = 3 then
2386 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2388 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2389 end;
2390 end;
2392 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2393 var
2394 a: Integer;
2395 PointsArray: Array of TRespawnPoint;
2396 begin
2397 Result := False;
2398 SetLength(PointsArray, 0);
2400 if RespawnPoints = nil then
2401 Exit;
2403 for a := 0 to High(RespawnPoints) do
2404 if RespawnPoints[a].PointType = PointType then
2405 begin
2406 SetLength(PointsArray, Length(PointsArray)+1);
2407 PointsArray[High(PointsArray)] := RespawnPoints[a];
2408 end;
2410 if PointsArray = nil then
2411 Exit;
2413 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2414 Result := True;
2415 end;
2417 function g_Map_GetPointCount(PointType: Byte): Word;
2418 var
2419 a: Integer;
2420 begin
2421 Result := 0;
2423 if RespawnPoints = nil then
2424 Exit;
2426 for a := 0 to High(RespawnPoints) do
2427 if RespawnPoints[a].PointType = PointType then
2428 Result := Result + 1;
2429 end;
2431 function g_Map_HaveFlagPoints(): Boolean;
2432 begin
2433 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2434 end;
2436 procedure g_Map_ResetFlag(Flag: Byte);
2437 begin
2438 with gFlags[Flag] do
2439 begin
2440 Obj.X := -1000;
2441 Obj.Y := -1000;
2442 Obj.Vel.X := 0;
2443 Obj.Vel.Y := 0;
2444 Direction := D_LEFT;
2445 State := FLAG_STATE_NONE;
2446 if FlagPoints[Flag] <> nil then
2447 begin
2448 Obj.X := FlagPoints[Flag]^.X;
2449 Obj.Y := FlagPoints[Flag]^.Y;
2450 Direction := FlagPoints[Flag]^.Direction;
2451 State := FLAG_STATE_NORMAL;
2452 end;
2453 Count := -1;
2454 end;
2455 end;
2457 procedure g_Map_DrawFlags();
2458 var
2459 i, dx: Integer;
2460 Mirror: TMirrorType;
2461 begin
2462 if gGameSettings.GameMode <> GM_CTF then
2463 Exit;
2465 for i := FLAG_RED to FLAG_BLUE do
2466 with gFlags[i] do
2467 if State <> FLAG_STATE_CAPTURED then
2468 begin
2469 if State = FLAG_STATE_NONE then
2470 continue;
2472 if Direction = D_LEFT then
2473 begin
2474 Mirror := M_HORIZONTAL;
2475 dx := -1;
2476 end
2477 else
2478 begin
2479 Mirror := M_NONE;
2480 dx := 1;
2481 end;
2483 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2485 if g_debug_Frames then
2486 begin
2487 e_DrawQuad(Obj.X+Obj.Rect.X,
2488 Obj.Y+Obj.Rect.Y,
2489 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2490 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2491 0, 255, 0);
2492 end;
2493 end;
2494 end;
2496 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2497 var
2498 dw: DWORD;
2499 b: Byte;
2500 str: String;
2501 boo: Boolean;
2503 procedure SavePanelArray(var panels: TPanelArray);
2504 var
2505 PAMem: TBinMemoryWriter;
2506 i: Integer;
2507 begin
2508 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2509 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2511 i := 0;
2512 while i < Length(panels) do
2513 begin
2514 if panels[i].SaveIt then
2515 begin
2516 // ID ïàíåëè:
2517 PAMem.WriteInt(i);
2518 // Ñîõðàíÿåì ïàíåëü:
2519 panels[i].SaveState(PAMem);
2520 end;
2521 Inc(i);
2522 end;
2524 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2525 PAMem.SaveToMemory(Mem);
2526 PAMem.Free();
2527 end;
2529 procedure SaveFlag(flag: PFlag);
2530 begin
2531 // Ñèãíàòóðà ôëàãà:
2532 dw := FLAG_SIGNATURE; // 'FLAG'
2533 Mem.WriteDWORD(dw);
2534 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2535 Mem.WriteByte(flag^.RespawnType);
2536 // Ñîñòîÿíèå ôëàãà:
2537 Mem.WriteByte(flag^.State);
2538 // Íàïðàâëåíèå ôëàãà:
2539 if flag^.Direction = D_LEFT then
2540 b := 1
2541 else // D_RIGHT
2542 b := 2;
2543 Mem.WriteByte(b);
2544 // Îáúåêò ôëàãà:
2545 Obj_SaveState(@flag^.Obj, Mem);
2546 end;
2548 begin
2549 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2551 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2552 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2553 SavePanelArray(gWalls);
2554 // Ñîõðàíÿåì ïàíåëè ôîíà:
2555 SavePanelArray(gRenderBackgrounds);
2556 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2557 SavePanelArray(gRenderForegrounds);
2558 // Ñîõðàíÿåì ïàíåëè âîäû:
2559 SavePanelArray(gWater);
2560 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2561 SavePanelArray(gAcid1);
2562 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2563 SavePanelArray(gAcid2);
2564 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2565 SavePanelArray(gSteps);
2566 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2567 SavePanelArray(gLifts);
2568 ///// /////
2570 ///// Ñîõðàíÿåì ìóçûêó: /////
2571 // Ñèãíàòóðà ìóçûêè:
2572 dw := MUSIC_SIGNATURE; // 'MUSI'
2573 Mem.WriteDWORD(dw);
2574 // Íàçâàíèå ìóçûêè:
2575 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2576 if gMusic.NoMusic then
2577 str := ''
2578 else
2579 str := gMusic.Name;
2580 Mem.WriteString(str, 64);
2581 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2582 dw := gMusic.GetPosition();
2583 Mem.WriteDWORD(dw);
2584 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2585 boo := gMusic.SpecPause;
2586 Mem.WriteBoolean(boo);
2587 ///// /////
2589 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2590 Mem.WriteInt(gTotalMonsters);
2591 ///// /////
2593 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2594 if gGameSettings.GameMode = GM_CTF then
2595 begin
2596 // Ôëàã Êðàñíîé êîìàíäû:
2597 SaveFlag(@gFlags[FLAG_RED]);
2598 // Ôëàã Ñèíåé êîìàíäû:
2599 SaveFlag(@gFlags[FLAG_BLUE]);
2600 end;
2601 ///// /////
2603 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2604 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2605 begin
2606 // Î÷êè Êðàñíîé êîìàíäû:
2607 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2608 // Î÷êè Ñèíåé êîìàíäû:
2609 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2610 end;
2611 ///// /////
2612 end;
2614 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2615 var
2616 dw: DWORD;
2617 b: Byte;
2618 str: String;
2619 boo: Boolean;
2621 procedure LoadPanelArray(var panels: TPanelArray);
2622 var
2623 PAMem: TBinMemoryReader;
2624 i, id: Integer;
2625 begin
2626 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2627 PAMem := TBinMemoryReader.Create();
2628 PAMem.LoadFromMemory(Mem);
2630 for i := 0 to Length(panels)-1 do
2631 if panels[i].SaveIt then
2632 begin
2633 // ID ïàíåëè:
2634 PAMem.ReadInt(id);
2635 if id <> i then
2636 begin
2637 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2638 end;
2639 // Çàãðóæàåì ïàíåëü:
2640 panels[i].LoadState(PAMem);
2641 end;
2643 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2644 PAMem.Free();
2645 end;
2647 procedure LoadFlag(flag: PFlag);
2648 begin
2649 // Ñèãíàòóðà ôëàãà:
2650 Mem.ReadDWORD(dw);
2651 if dw <> FLAG_SIGNATURE then // 'FLAG'
2652 begin
2653 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2654 end;
2655 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2656 Mem.ReadByte(flag^.RespawnType);
2657 // Ñîñòîÿíèå ôëàãà:
2658 Mem.ReadByte(flag^.State);
2659 // Íàïðàâëåíèå ôëàãà:
2660 Mem.ReadByte(b);
2661 if b = 1 then
2662 flag^.Direction := D_LEFT
2663 else // b = 2
2664 flag^.Direction := D_RIGHT;
2665 // Îáúåêò ôëàãà:
2666 Obj_LoadState(@flag^.Obj, Mem);
2667 end;
2669 begin
2670 if Mem = nil then
2671 Exit;
2673 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2674 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2675 LoadPanelArray(gWalls);
2676 // Çàãðóæàåì ïàíåëè ôîíà:
2677 LoadPanelArray(gRenderBackgrounds);
2678 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2679 LoadPanelArray(gRenderForegrounds);
2680 // Çàãðóæàåì ïàíåëè âîäû:
2681 LoadPanelArray(gWater);
2682 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2683 LoadPanelArray(gAcid1);
2684 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2685 LoadPanelArray(gAcid2);
2686 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2687 LoadPanelArray(gSteps);
2688 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2689 LoadPanelArray(gLifts);
2690 ///// /////
2692 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2693 g_GFX_Init();
2694 mapCreateGrid();
2696 ///// Çàãðóæàåì ìóçûêó: /////
2697 // Ñèãíàòóðà ìóçûêè:
2698 Mem.ReadDWORD(dw);
2699 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2700 begin
2701 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2702 end;
2703 // Íàçâàíèå ìóçûêè:
2704 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2705 Mem.ReadString(str);
2706 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2707 Mem.ReadDWORD(dw);
2708 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2709 Mem.ReadBoolean(boo);
2710 // Çàïóñêàåì ýòó ìóçûêó:
2711 gMusic.SetByName(str);
2712 gMusic.SpecPause := boo;
2713 gMusic.Play();
2714 gMusic.Pause(True);
2715 gMusic.SetPosition(dw);
2716 ///// /////
2718 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2719 Mem.ReadInt(gTotalMonsters);
2720 ///// /////
2722 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2723 if gGameSettings.GameMode = GM_CTF then
2724 begin
2725 // Ôëàã Êðàñíîé êîìàíäû:
2726 LoadFlag(@gFlags[FLAG_RED]);
2727 // Ôëàã Ñèíåé êîìàíäû:
2728 LoadFlag(@gFlags[FLAG_BLUE]);
2729 end;
2730 ///// /////
2732 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2733 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2734 begin
2735 // Î÷êè Êðàñíîé êîìàíäû:
2736 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2737 // Î÷êè Ñèíåé êîìàíäû:
2738 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2739 end;
2740 ///// /////
2741 end;
2743 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2744 var
2745 Arr: TPanelArray;
2746 begin
2747 Result := nil;
2748 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2749 Arr := PanelByID[PanelID].PWhere^;
2750 PanelArrayID := PanelByID[PanelID].PArrID;
2751 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2752 end;
2754 end.