DEADSOFTWARE

build "visible panels set" before rendering the frame
[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 {$MODE DELPHI}
17 unit g_map;
19 interface
21 uses
22 e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
23 g_phys, wadreader, BinEditor, g_panel, md5;
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 // build "potentially visible panels" set, so we can avoid looping over all level panels again and again
66 procedure g_Map_BuildPVP (minx, miny, maxx, maxy: Integer);
67 procedure g_Map_ResetPVP ();
68 // do not call this without calling `g_Map_BuildPVP()` or `g_Map_ResetPVP()` first!
69 procedure g_Map_DrawPanels(PanelType: Word);
71 procedure g_Map_DrawBack(dx, dy: Integer);
72 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
73 PanelType: Word; b1x3: Boolean): Boolean;
74 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
75 procedure g_Map_EnableWall(ID: DWORD);
76 procedure g_Map_DisableWall(ID: DWORD);
77 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
78 procedure g_Map_SetLift(ID: DWORD; t: Integer);
79 procedure g_Map_ReAdd_DieTriggers();
80 function g_Map_IsSpecialTexture(Texture: String): Boolean;
82 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
83 function g_Map_GetPointCount(PointType: Byte): Word;
85 function g_Map_HaveFlagPoints(): Boolean;
87 procedure g_Map_ResetFlag(Flag: Byte);
88 procedure g_Map_DrawFlags();
90 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
92 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
93 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
95 const
96 RESPAWNPOINT_PLAYER1 = 1;
97 RESPAWNPOINT_PLAYER2 = 2;
98 RESPAWNPOINT_DM = 3;
99 RESPAWNPOINT_RED = 4;
100 RESPAWNPOINT_BLUE = 5;
102 FLAG_NONE = 0;
103 FLAG_RED = 1;
104 FLAG_BLUE = 2;
105 FLAG_DOM = 3;
107 FLAG_STATE_NONE = 0;
108 FLAG_STATE_NORMAL = 1;
109 FLAG_STATE_DROPPED = 2;
110 FLAG_STATE_CAPTURED = 3;
111 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
112 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
114 FLAG_TIME = 720; // 20 seconds
116 SKY_STRETCH: Single = 1.5;
118 var
119 gWalls: TPanelArray;
120 gRenderBackgrounds: TPanelArray;
121 gRenderForegrounds: TPanelArray;
122 gWater, gAcid1, gAcid2: TPanelArray;
123 gSteps: TPanelArray;
124 gLifts: TPanelArray;
125 gBlockMon: TPanelArray;
126 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
127 //gDOMFlags: array of TFlag;
128 gMapInfo: TMapInfo;
129 gBackSize: TPoint;
130 gDoorMap: array of array of DWORD;
131 gLiftMap: array of array of DWORD;
132 gWADHash: TMD5Digest;
133 BackID: DWORD = DWORD(-1);
134 gExternalResources: TStringList;
136 implementation
138 uses
139 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
140 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
141 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
142 Math, g_monsters, g_saveload, g_language, g_netmsg,
143 utils, sfs,
144 ImagingTypes, Imaging, ImagingUtility,
145 ImagingGif, ImagingNetworkGraphics;
147 const
148 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
149 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
150 FLAG_SIGNATURE = $47414C46; // 'FLAG'
152 type
153 TPanelID = record
154 PWhere: ^TPanelArray;
155 PArrID: Integer;
156 end;
158 var
159 PanelById: array of TPanelID;
160 Textures: TLevelTextureArray;
161 RespawnPoints: Array of TRespawnPoint;
162 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
163 //DOMFlagPoints: Array of TFlagPoint;
166 function g_Map_IsSpecialTexture(Texture: String): Boolean;
167 begin
168 Result := (Texture = TEXTURE_NAME_WATER) or
169 (Texture = TEXTURE_NAME_ACID1) or
170 (Texture = TEXTURE_NAME_ACID2);
171 end;
173 procedure CreateDoorMap();
174 var
175 PanelArray: Array of record
176 X, Y: Integer;
177 Width, Height: Word;
178 Active: Boolean;
179 PanelID: DWORD;
180 end;
181 a, b, c, m, i, len: Integer;
182 ok: Boolean;
183 begin
184 if gWalls = nil then
185 Exit;
187 i := 0;
188 len := 128;
189 SetLength(PanelArray, len);
191 for a := 0 to High(gWalls) do
192 if gWalls[a].Door then
193 begin
194 PanelArray[i].X := gWalls[a].X;
195 PanelArray[i].Y := gWalls[a].Y;
196 PanelArray[i].Width := gWalls[a].Width;
197 PanelArray[i].Height := gWalls[a].Height;
198 PanelArray[i].Active := True;
199 PanelArray[i].PanelID := a;
201 i := i + 1;
202 if i = len then
203 begin
204 len := len + 128;
205 SetLength(PanelArray, len);
206 end;
207 end;
209 // Íåò äâåðåé:
210 if i = 0 then
211 begin
212 PanelArray := nil;
213 Exit;
214 end;
216 SetLength(gDoorMap, 0);
218 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
220 for a := 0 to i-1 do
221 if PanelArray[a].Active then
222 begin
223 PanelArray[a].Active := False;
224 m := Length(gDoorMap);
225 SetLength(gDoorMap, m+1);
226 SetLength(gDoorMap[m], 1);
227 gDoorMap[m, 0] := PanelArray[a].PanelID;
228 ok := True;
230 while ok do
231 begin
232 ok := False;
234 for b := 0 to i-1 do
235 if PanelArray[b].Active then
236 for c := 0 to High(gDoorMap[m]) do
237 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
238 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
239 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
240 PanelArray[b].Width, PanelArray[b].Height,
241 gWalls[gDoorMap[m, c]].X,
242 gWalls[gDoorMap[m, c]].Y,
243 gWalls[gDoorMap[m, c]].Width,
244 gWalls[gDoorMap[m, c]].Height) then
245 begin
246 PanelArray[b].Active := False;
247 SetLength(gDoorMap[m],
248 Length(gDoorMap[m])+1);
249 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
250 ok := True;
251 Break;
252 end;
253 end;
255 g_Game_StepLoading();
256 end;
258 PanelArray := nil;
259 end;
261 procedure CreateLiftMap();
262 var
263 PanelArray: Array of record
264 X, Y: Integer;
265 Width, Height: Word;
266 Active: Boolean;
267 end;
268 a, b, c, len, i, j: Integer;
269 ok: Boolean;
270 begin
271 if gLifts = nil then
272 Exit;
274 len := Length(gLifts);
275 SetLength(PanelArray, len);
277 for a := 0 to len-1 do
278 begin
279 PanelArray[a].X := gLifts[a].X;
280 PanelArray[a].Y := gLifts[a].Y;
281 PanelArray[a].Width := gLifts[a].Width;
282 PanelArray[a].Height := gLifts[a].Height;
283 PanelArray[a].Active := True;
284 end;
286 SetLength(gLiftMap, len);
287 i := 0;
289 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
291 for a := 0 to len-1 do
292 if PanelArray[a].Active then
293 begin
294 PanelArray[a].Active := False;
295 SetLength(gLiftMap[i], 32);
296 j := 0;
297 gLiftMap[i, j] := a;
298 ok := True;
300 while ok do
301 begin
302 ok := False;
303 for b := 0 to len-1 do
304 if PanelArray[b].Active then
305 for c := 0 to j do
306 if g_CollideAround(PanelArray[b].X,
307 PanelArray[b].Y,
308 PanelArray[b].Width,
309 PanelArray[b].Height,
310 PanelArray[gLiftMap[i, c]].X,
311 PanelArray[gLiftMap[i, c]].Y,
312 PanelArray[gLiftMap[i, c]].Width,
313 PanelArray[gLiftMap[i, c]].Height) then
314 begin
315 PanelArray[b].Active := False;
316 j := j+1;
317 if j > High(gLiftMap[i]) then
318 SetLength(gLiftMap[i],
319 Length(gLiftMap[i])+32);
321 gLiftMap[i, j] := b;
322 ok := True;
324 Break;
325 end;
326 end;
328 SetLength(gLiftMap[i], j+1);
329 i := i+1;
331 g_Game_StepLoading();
332 end;
334 SetLength(gLiftMap, i);
336 PanelArray := nil;
337 end;
339 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
340 CurTex: Integer; sav: Boolean): Integer;
341 var
342 len: Integer;
343 panels: ^TPanelArray;
344 begin
345 Result := -1;
347 case PanelRec.PanelType of
348 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
349 panels := @gWalls;
350 PANEL_BACK:
351 panels := @gRenderBackgrounds;
352 PANEL_FORE:
353 panels := @gRenderForegrounds;
354 PANEL_WATER:
355 panels := @gWater;
356 PANEL_ACID1:
357 panels := @gAcid1;
358 PANEL_ACID2:
359 panels := @gAcid2;
360 PANEL_STEP:
361 panels := @gSteps;
362 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
363 panels := @gLifts;
364 PANEL_BLOCKMON:
365 panels := @gBlockMon;
366 else
367 Exit;
368 end;
370 len := Length(panels^);
371 SetLength(panels^, len + 1);
373 panels^[len] := TPanel.Create(PanelRec, AddTextures,
374 CurTex, Textures);
375 if sav then
376 panels^[len].SaveIt := True;
378 Result := len;
380 len := Length(PanelByID);
381 SetLength(PanelByID, len + 1);
382 PanelByID[len].PWhere := panels;
383 PanelByID[len].PArrID := Result;
384 end;
386 function CreateNullTexture(RecName: String): Integer;
387 begin
388 SetLength(Textures, Length(Textures)+1);
389 result := High(Textures);
391 with Textures[High(Textures)] do
392 begin
393 TextureName := RecName;
394 Width := 1;
395 Height := 1;
396 Anim := False;
397 TextureID := TEXTURE_NONE;
398 end;
399 end;
401 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
402 var
403 WAD: TWADFile;
404 TextureData: Pointer;
405 WADName, txname: String;
406 a, ResLength: Integer;
407 begin
408 Result := -1;
410 if Textures <> nil then
411 for a := 0 to High(Textures) do
412 if Textures[a].TextureName = RecName then
413 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
414 Result := a;
415 Exit;
416 end;
418 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
419 if (RecName = TEXTURE_NAME_WATER) or
420 (RecName = TEXTURE_NAME_ACID1) or
421 (RecName = TEXTURE_NAME_ACID2) then
422 begin
423 SetLength(Textures, Length(Textures)+1);
425 with Textures[High(Textures)] do
426 begin
427 TextureName := RecName;
429 if TextureName = TEXTURE_NAME_WATER then
430 TextureID := TEXTURE_SPECIAL_WATER
431 else
432 if TextureName = TEXTURE_NAME_ACID1 then
433 TextureID := TEXTURE_SPECIAL_ACID1
434 else
435 if TextureName = TEXTURE_NAME_ACID2 then
436 TextureID := TEXTURE_SPECIAL_ACID2;
438 Anim := False;
439 end;
441 result := High(Textures);
442 Exit;
443 end;
445 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
446 WADName := g_ExtractWadName(RecName);
448 WAD := TWADFile.Create();
450 if WADName <> '' then
451 WADName := GameDir+'/wads/'+WADName
452 else
453 WADName := Map;
455 WAD.ReadFile(WADName);
457 txname := RecName;
459 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
460 begin
461 FreeMem(TextureData);
462 RecName := 'COMMON\ALIEN';
463 end;
466 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
467 begin
468 SetLength(Textures, Length(Textures)+1);
469 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
470 Exit;
471 e_GetTextureSize(Textures[High(Textures)].TextureID,
472 @Textures[High(Textures)].Width,
473 @Textures[High(Textures)].Height);
474 FreeMem(TextureData);
475 Textures[High(Textures)].TextureName := {RecName}txname;
476 Textures[High(Textures)].Anim := False;
478 result := High(Textures);
479 end
480 else // Íåò òàêîãî ðåóñðñà â WAD'å
481 begin
482 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
483 if log then
484 begin
485 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
486 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
487 end;
488 end;
490 WAD.Free();
491 end;
493 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
494 var
495 WAD: TWADFile;
496 TextureWAD: PChar = nil;
497 TextData: Pointer = nil;
498 TextureData: Pointer = nil;
499 cfg: TConfig = nil;
500 WADName: String;
501 ResLength: Integer;
502 TextureResource: String;
503 _width, _height, _framecount, _speed: Integer;
504 _backanimation: Boolean;
505 //imgfmt: string;
506 ia: TDynImageDataArray = nil;
507 f, c, frdelay, frloop: Integer;
508 begin
509 result := -1;
511 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
513 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
514 WADName := g_ExtractWadName(RecName);
516 WAD := TWADFile.Create();
517 try
518 if WADName <> '' then
519 WADName := GameDir+'/wads/'+WADName
520 else
521 WADName := Map;
523 WAD.ReadFile(WADName);
525 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
526 begin
527 if log then
528 begin
529 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
530 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
531 end;
532 exit;
533 end;
535 {TEST
536 if WADName = Map then
537 begin
538 //FreeMem(TextureWAD);
539 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
540 end;
543 WAD.FreeWAD();
545 if ResLength < 6 then
546 begin
547 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
548 exit;
549 end;
551 // ýòî ïòèöà? ýòî ñàìîë¸ò?
552 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
553 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
554 begin
555 // íåò, ýòî ñóïåðìåí!
556 if not WAD.ReadMemory(TextureWAD, ResLength) then
557 begin
558 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
559 exit;
560 end;
562 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
563 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
564 begin
565 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
566 exit;
567 end;
569 cfg := TConfig.CreateMem(TextData, ResLength);
571 TextureResource := cfg.ReadStr('', 'resource', '');
572 if TextureResource = '' then
573 begin
574 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
575 exit;
576 end;
578 _width := cfg.ReadInt('', 'framewidth', 0);
579 _height := cfg.ReadInt('', 'frameheight', 0);
580 _framecount := cfg.ReadInt('', 'framecount', 0);
581 _speed := cfg.ReadInt('', 'waitcount', 0);
582 _backanimation := cfg.ReadBool('', 'backanimation', False);
584 cfg.Free();
585 cfg := nil;
587 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
588 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
589 begin
590 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
591 exit;
592 end;
594 WAD.Free();
595 WAD := nil;
597 SetLength(Textures, Length(Textures)+1);
598 with Textures[High(Textures)] do
599 begin
600 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
601 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
602 begin
603 TextureName := RecName;
604 Width := _width;
605 Height := _height;
606 Anim := True;
607 FramesCount := _framecount;
608 Speed := _speed;
609 result := High(Textures);
610 end
611 else
612 begin
613 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
614 end;
615 end;
616 end
617 else
618 begin
619 // try animated image
621 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
622 if length(imgfmt) = 0 then
623 begin
624 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
625 exit;
626 end;
628 GlobalMetadata.ClearMetaItems();
629 GlobalMetadata.ClearMetaItemsForSaving();
630 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
631 begin
632 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
633 exit;
634 end;
635 if length(ia) = 0 then
636 begin
637 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
638 exit;
639 end;
641 WAD.Free();
642 WAD := nil;
644 _width := ia[0].width;
645 _height := ia[0].height;
646 _framecount := length(ia);
647 _speed := 1;
648 _backanimation := false;
649 frdelay := -1;
650 frloop := -666;
651 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
652 begin
653 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
654 try
655 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
656 frdelay := f;
657 if f < 0 then f := 0;
658 // rounding ;-)
659 c := f mod 28;
660 if c < 13 then c := 0 else c := 1;
661 f := (f div 28)+c;
662 if f < 1 then f := 1 else if f > 255 then f := 255;
663 _speed := f;
664 except
665 end;
666 end;
667 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
668 begin
669 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
670 try
671 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
672 frloop := f;
673 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
674 except
675 end;
676 end;
677 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
678 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
679 f := ord(_backanimation);
680 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);
682 SetLength(Textures, Length(Textures)+1);
683 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
684 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
685 begin
686 Textures[High(Textures)].TextureName := RecName;
687 Textures[High(Textures)].Width := _width;
688 Textures[High(Textures)].Height := _height;
689 Textures[High(Textures)].Anim := True;
690 Textures[High(Textures)].FramesCount := length(ia);
691 Textures[High(Textures)].Speed := _speed;
692 result := High(Textures);
693 //writeln(' CREATED!');
694 end
695 else
696 begin
697 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
698 end;
699 end;
700 finally
701 for f := 0 to High(ia) do FreeImage(ia[f]);
702 WAD.Free();
703 cfg.Free();
704 if TextureWAD <> nil then FreeMem(TextureWAD);
705 if TextData <> nil then FreeMem(TextData);
706 if TextureData <> nil then FreeMem(TextureData);
707 end;
708 end;
710 procedure CreateItem(Item: TItemRec_1);
711 begin
712 if g_Game_IsClient then Exit;
714 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
715 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
716 Exit;
718 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
719 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
720 end;
722 procedure CreateArea(Area: TAreaRec_1);
723 var
724 a: Integer;
725 id: DWORD;
726 begin
727 case Area.AreaType of
728 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
729 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
730 begin
731 SetLength(RespawnPoints, Length(RespawnPoints)+1);
732 with RespawnPoints[High(RespawnPoints)] do
733 begin
734 X := Area.X;
735 Y := Area.Y;
736 Direction := TDirection(Area.Direction);
738 case Area.AreaType of
739 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
740 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
741 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
742 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
743 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
744 end;
745 end;
746 end;
748 AREA_REDFLAG, AREA_BLUEFLAG:
749 begin
750 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
752 if FlagPoints[a] <> nil then Exit;
754 New(FlagPoints[a]);
756 with FlagPoints[a]^ do
757 begin
758 X := Area.X-FLAGRECT.X;
759 Y := Area.Y-FLAGRECT.Y;
760 Direction := TDirection(Area.Direction);
761 end;
763 with gFlags[a] do
764 begin
765 case a of
766 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
767 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
768 end;
770 Animation := TAnimation.Create(id, True, 8);
771 Obj.Rect := FLAGRECT;
773 g_Map_ResetFlag(a);
774 end;
775 end;
777 AREA_DOMFLAG:
778 begin
779 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
780 with DOMFlagPoints[High(DOMFlagPoints)] do
781 begin
782 X := Area.X;
783 Y := Area.Y;
784 Direction := TDirection(Area.Direction);
785 end;
787 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
788 end;
789 end;
790 end;
792 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
793 var
794 _trigger: TTrigger;
795 begin
796 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
798 with _trigger do
799 begin
800 X := Trigger.X;
801 Y := Trigger.Y;
802 Width := Trigger.Width;
803 Height := Trigger.Height;
804 Enabled := ByteBool(Trigger.Enabled);
805 TexturePanel := Trigger.TexturePanel;
806 TexturePanelType := fTexturePanel1Type;
807 ShotPanelType := fTexturePanel2Type;
808 TriggerType := Trigger.TriggerType;
809 ActivateType := Trigger.ActivateType;
810 Keys := Trigger.Keys;
811 Data.Default := Trigger.DATA;
812 end;
814 g_Triggers_Create(_trigger);
815 end;
817 procedure CreateMonster(monster: TMonsterRec_1);
818 var
819 a, i: Integer;
820 begin
821 if g_Game_IsClient then Exit;
823 if (gGameSettings.GameType = GT_SINGLE)
824 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
825 begin
826 i := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y,
827 TDirection(monster.Direction));
829 if gTriggers <> nil then
830 for a := 0 to High(gTriggers) do
831 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
832 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
833 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
834 gMonsters[i].AddTrigger(a);
836 if monster.MonsterType <> MONSTER_BARREL then
837 Inc(gTotalMonsters);
838 end;
839 end;
841 procedure g_Map_ReAdd_DieTriggers();
842 var
843 i, a: Integer;
844 begin
845 if g_Game_IsClient then Exit;
847 for i := 0 to High(gMonsters) do
848 if gMonsters[i] <> nil then
849 begin
850 gMonsters[i].ClearTriggers();
852 for a := 0 to High(gTriggers) do
853 if gTriggers[a].TriggerType in [TRIGGER_PRESS,
854 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
855 if (gTriggers[a].Data.MonsterID-1) = gMonsters[i].StartID then
856 gMonsters[i].AddTrigger(a);
857 end;
858 end;
860 function extractWadName(resourceName: string): string;
861 var
862 posN: Integer;
863 begin
864 posN := Pos(':', resourceName);
865 if posN > 0 then
866 Result:= Copy(resourceName, 0, posN-1)
867 else
868 Result := '';
869 end;
871 procedure addResToExternalResList(res: string);
872 begin
873 res := extractWadName(res);
874 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
875 gExternalResources.Add(res);
876 end;
878 procedure generateExternalResourcesList(mapReader: TMapReader_1);
879 var
880 textures: TTexturesRec1Array;
881 mapHeader: TMapHeaderRec_1;
882 i: integer;
883 resFile: String = '';
884 begin
885 if gExternalResources = nil then
886 gExternalResources := TStringList.Create;
888 gExternalResources.Clear;
889 textures := mapReader.GetTextures();
890 for i := 0 to High(textures) do
891 begin
892 addResToExternalResList(resFile);
893 end;
895 textures := nil;
897 mapHeader := mapReader.GetMapHeader;
899 addResToExternalResList(mapHeader.MusicName);
900 addResToExternalResList(mapHeader.SkyName);
901 end;
903 function g_Map_Load(Res: String): Boolean;
904 const
905 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
906 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
907 var
908 WAD: TWADFile;
909 MapReader: TMapReader_1;
910 Header: TMapHeaderRec_1;
911 _textures: TTexturesRec1Array;
912 _texnummap: array of Integer; // `_textures` -> `Textures`
913 panels: TPanelsRec1Array;
914 items: TItemsRec1Array;
915 monsters: TMonsterRec1Array;
916 areas: TAreasRec1Array;
917 triggers: TTriggersRec1Array;
918 a, b, c, k: Integer;
919 PanelID: DWORD;
920 AddTextures: TAddTextureArray;
921 texture: TTextureRec_1;
922 TriggersTable: Array of record
923 TexturePanel: Integer;
924 LiftPanel: Integer;
925 DoorPanel: Integer;
926 ShotPanel: Integer;
927 end;
928 FileName, mapResName, s, TexName: String;
929 Data: Pointer;
930 Len: Integer;
931 ok, isAnim, trigRef: Boolean;
932 CurTex, ntn: Integer;
933 begin
934 Result := False;
935 gMapInfo.Map := Res;
936 TriggersTable := nil;
937 FillChar(texture, SizeOf(texture), 0);
939 sfsGCDisable(); // temporary disable removing of temporary volumes
940 try
941 // Çàãðóçêà WAD:
942 FileName := g_ExtractWadName(Res);
943 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
944 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
946 WAD := TWADFile.Create();
947 if not WAD.ReadFile(FileName) then
948 begin
949 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
950 WAD.Free();
951 Exit;
952 end;
953 //k8: why loader ignores path here?
954 mapResName := g_ExtractFileName(Res);
955 if not WAD.GetMapResource(mapResName, Data, Len) then
956 begin
957 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
958 WAD.Free();
959 Exit;
960 end;
962 WAD.Free();
964 // Çàãðóçêà êàðòû:
965 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
966 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
967 MapReader := TMapReader_1.Create();
969 if not MapReader.LoadMap(Data) then
970 begin
971 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
972 FreeMem(Data);
973 MapReader.Free();
974 Exit;
975 end;
977 FreeMem(Data);
978 generateExternalResourcesList(MapReader);
979 // Çàãðóçêà òåêñòóð:
980 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
981 _textures := MapReader.GetTextures();
982 _texnummap := nil;
984 // Äîáàâëåíèå òåêñòóð â Textures[]:
985 if _textures <> nil then
986 begin
987 e_WriteLog(' Loading textures:', MSG_NOTIFY);
988 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
989 SetLength(_texnummap, length(_textures));
991 for a := 0 to High(_textures) do
992 begin
993 SetLength(s, 64);
994 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
995 for b := 1 to Length(s) do
996 if s[b] = #0 then
997 begin
998 SetLength(s, b-1);
999 Break;
1000 end;
1001 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1002 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1003 // Àíèìèðîâàííàÿ òåêñòóðà:
1004 if ByteBool(_textures[a].Anim) then
1005 begin
1006 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1007 if ntn < 0 then
1008 begin
1009 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1010 ntn := CreateNullTexture(_textures[a].Resource);
1011 end;
1012 end
1013 else // Îáû÷íàÿ òåêñòóðà:
1014 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1015 if ntn < 0 then
1016 begin
1017 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1018 ntn := CreateNullTexture(_textures[a].Resource);
1019 end;
1021 _texnummap[a] := ntn; // fix texture number
1022 g_Game_StepLoading();
1023 end;
1024 end;
1026 // Çàãðóçêà òðèããåðîâ:
1027 gTriggerClientID := 0;
1028 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1029 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1030 triggers := MapReader.GetTriggers();
1032 // Çàãðóçêà ïàíåëåé:
1033 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1034 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1035 panels := MapReader.GetPanels();
1037 // check texture numbers for panels
1038 for a := 0 to High(panels) do
1039 begin
1040 if panels[a].TextureNum > High(_textures) then
1041 begin
1042 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1043 result := false;
1044 exit;
1045 end;
1046 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1047 end;
1049 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1050 if triggers <> nil then
1051 begin
1052 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1053 SetLength(TriggersTable, Length(triggers));
1054 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1056 for a := 0 to High(TriggersTable) do
1057 begin
1058 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1059 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1060 // Ëèôòû:
1061 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1062 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1063 else
1064 TriggersTable[a].LiftPanel := -1;
1065 // Äâåðè:
1066 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1067 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1068 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1069 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1070 else
1071 TriggersTable[a].DoorPanel := -1;
1072 // Òóðåëü:
1073 if triggers[a].TriggerType = TRIGGER_SHOT then
1074 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1075 else
1076 TriggersTable[a].ShotPanel := -1;
1078 g_Game_StepLoading();
1079 end;
1080 end;
1082 // Ñîçäàåì ïàíåëè:
1083 if panels <> nil then
1084 begin
1085 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1086 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1088 for a := 0 to High(panels) do
1089 begin
1090 SetLength(AddTextures, 0);
1091 trigRef := False;
1092 CurTex := -1;
1093 if _textures <> nil then
1094 begin
1095 texture := _textures[panels[a].TextureNum];
1096 ok := True;
1097 end
1098 else
1099 ok := False;
1101 if ok then
1102 begin
1103 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1104 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1105 ok := False;
1106 if (TriggersTable <> nil) and (_textures <> nil) then
1107 for b := 0 to High(TriggersTable) do
1108 if (TriggersTable[b].TexturePanel = a)
1109 or (TriggersTable[b].ShotPanel = a) then
1110 begin
1111 trigRef := True;
1112 ok := True;
1113 Break;
1114 end;
1115 end;
1117 if ok then
1118 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1119 SetLength(s, 64);
1120 CopyMemory(@s[1], @texture.Resource[0], 64);
1121 // Èçìåðÿåì äëèíó:
1122 Len := Length(s);
1123 for c := Len downto 1 do
1124 if s[c] <> #0 then
1125 begin
1126 Len := c;
1127 Break;
1128 end;
1129 SetLength(s, Len);
1131 // Ñïåö-òåêñòóðû çàïðåùåíû:
1132 if g_Map_IsSpecialTexture(s) then
1133 ok := False
1134 else
1135 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1136 ok := g_Texture_NumNameFindStart(s);
1138 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1139 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1140 if ok then
1141 begin
1142 k := NNF_NAME_BEFORE;
1143 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1144 while ok or (k = NNF_NAME_BEFORE) or
1145 (k = NNF_NAME_EQUALS) do
1146 begin
1147 k := g_Texture_NumNameFindNext(TexName);
1149 if (k = NNF_NAME_BEFORE) or
1150 (k = NNF_NAME_AFTER) then
1151 begin
1152 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1153 if ByteBool(texture.Anim) then
1154 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1155 isAnim := True;
1156 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1157 if not ok then
1158 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1159 isAnim := False;
1160 ok := CreateTexture(TexName, FileName, False) >= 0;
1161 end;
1162 end
1163 else
1164 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1165 isAnim := False;
1166 ok := CreateTexture(TexName, FileName, False) >= 0;
1167 if not ok then
1168 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1169 isAnim := True;
1170 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1171 end;
1172 end;
1174 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1175 if ok then
1176 begin
1177 for c := 0 to High(Textures) do
1178 if Textures[c].TextureName = TexName then
1179 begin
1180 SetLength(AddTextures, Length(AddTextures)+1);
1181 AddTextures[High(AddTextures)].Texture := c;
1182 AddTextures[High(AddTextures)].Anim := isAnim;
1183 Break;
1184 end;
1185 end;
1186 end
1187 else
1188 if k = NNF_NAME_EQUALS then
1189 begin
1190 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1191 SetLength(AddTextures, Length(AddTextures)+1);
1192 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1193 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1194 CurTex := High(AddTextures);
1195 ok := True;
1196 end
1197 else // NNF_NO_NAME
1198 ok := False;
1199 end; // while ok...
1201 ok := True;
1202 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1203 end; // if ok - ññûëàþòñÿ òðèããåðû
1205 if not ok then
1206 begin
1207 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1208 SetLength(AddTextures, 1);
1209 AddTextures[0].Texture := panels[a].TextureNum;
1210 AddTextures[0].Anim := ByteBool(texture.Anim);
1211 CurTex := 0;
1212 end;
1214 //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);
1216 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1217 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1219 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1220 if TriggersTable <> nil then
1221 for b := 0 to High(TriggersTable) do
1222 begin
1223 // Òðèããåð äâåðè/ëèôòà:
1224 if (TriggersTable[b].LiftPanel = a) or
1225 (TriggersTable[b].DoorPanel = a) then
1226 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1227 // Òðèããåð ñìåíû òåêñòóðû:
1228 if TriggersTable[b].TexturePanel = a then
1229 triggers[b].TexturePanel := PanelID;
1230 // Òðèããåð "Òóðåëü":
1231 if TriggersTable[b].ShotPanel = a then
1232 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1233 end;
1235 g_Game_StepLoading();
1236 end;
1237 end;
1239 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1240 if (triggers <> nil) and not gLoadGameMode then
1241 begin
1242 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1243 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1244 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1245 for a := 0 to High(triggers) do
1246 begin
1247 if triggers[a].TexturePanel <> -1 then
1248 b := panels[TriggersTable[a].TexturePanel].PanelType
1249 else
1250 b := 0;
1251 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1252 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1253 c := panels[TriggersTable[a].ShotPanel].PanelType
1254 else
1255 c := 0;
1256 CreateTrigger(triggers[a], b, c);
1257 end;
1258 end;
1260 // Çàãðóçêà ïðåäìåòîâ:
1261 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1262 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1263 items := MapReader.GetItems();
1265 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1266 if (items <> nil) and not gLoadGameMode then
1267 begin
1268 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1269 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1270 for a := 0 to High(items) do
1271 CreateItem(Items[a]);
1272 end;
1274 // Çàãðóçêà îáëàñòåé:
1275 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1276 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1277 areas := MapReader.GetAreas();
1279 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1280 if areas <> nil then
1281 begin
1282 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1283 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1284 for a := 0 to High(areas) do
1285 CreateArea(areas[a]);
1286 end;
1288 // Çàãðóçêà ìîíñòðîâ:
1289 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1290 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1291 monsters := MapReader.GetMonsters();
1293 gTotalMonsters := 0;
1295 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1296 if (monsters <> nil) and not gLoadGameMode then
1297 begin
1298 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1299 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1300 for a := 0 to High(monsters) do
1301 CreateMonster(monsters[a]);
1302 end;
1304 // Çàãðóçêà îïèñàíèÿ êàðòû:
1305 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1306 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1307 Header := MapReader.GetMapHeader();
1309 MapReader.Free();
1311 with gMapInfo do
1312 begin
1313 Name := Header.MapName;
1314 Description := Header.MapDescription;
1315 Author := Header.MapAuthor;
1316 MusicName := Header.MusicName;
1317 SkyName := Header.SkyName;
1318 Height := Header.Height;
1319 Width := Header.Width;
1320 end;
1322 // Çàãðóçêà íåáà:
1323 if gMapInfo.SkyName <> '' then
1324 begin
1325 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1326 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1327 FileName := g_ExtractWadName(gMapInfo.SkyName);
1329 if FileName <> '' then
1330 FileName := GameDir+'/wads/'+FileName
1331 else
1332 begin
1333 FileName := g_ExtractWadName(Res);
1334 end;
1336 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1337 if g_Texture_CreateWAD(BackID, s) then
1338 begin
1339 g_Game_SetupScreenSize();
1340 end
1341 else
1342 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1343 end;
1345 // Çàãðóçêà ìóçûêè:
1346 ok := False;
1347 if gMapInfo.MusicName <> '' then
1348 begin
1349 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1350 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1351 FileName := g_ExtractWadName(gMapInfo.MusicName);
1353 if FileName <> '' then
1354 FileName := GameDir+'/wads/'+FileName
1355 else
1356 begin
1357 FileName := g_ExtractWadName(Res);
1358 end;
1360 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1361 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1362 ok := True
1363 else
1364 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1365 end;
1367 // Îñòàëüíûå óñòàíâêè:
1368 CreateDoorMap();
1369 CreateLiftMap();
1371 g_Items_Init();
1372 g_Weapon_Init();
1373 g_Monsters_Init();
1375 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1376 if not gLoadGameMode then
1377 g_GFX_Init();
1379 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1380 _textures := nil;
1381 panels := nil;
1382 items := nil;
1383 areas := nil;
1384 triggers := nil;
1385 TriggersTable := nil;
1386 AddTextures := nil;
1388 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1389 if ok and (not gLoadGameMode) then
1390 begin
1391 gMusic.SetByName(gMapInfo.MusicName);
1392 gMusic.Play();
1393 end
1394 else
1395 gMusic.SetByName('');
1396 finally
1397 sfsGCEnable(); // enable releasing unused volumes
1398 end;
1400 e_WriteLog('Done loading map.', MSG_NOTIFY);
1401 Result := True;
1402 end;
1404 function g_Map_GetMapInfo(Res: String): TMapInfo;
1405 var
1406 WAD: TWADFile;
1407 MapReader: TMapReader_1;
1408 Header: TMapHeaderRec_1;
1409 FileName: String;
1410 Data: Pointer;
1411 Len: Integer;
1412 begin
1413 FillChar(Result, SizeOf(Result), 0);
1414 FileName := g_ExtractWadName(Res);
1416 WAD := TWADFile.Create();
1417 if not WAD.ReadFile(FileName) then
1418 begin
1419 WAD.Free();
1420 Exit;
1421 end;
1423 //k8: it ignores path again
1424 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1425 begin
1426 WAD.Free();
1427 Exit;
1428 end;
1430 WAD.Free();
1432 MapReader := TMapReader_1.Create();
1434 if not MapReader.LoadMap(Data) then
1435 begin
1436 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1437 ZeroMemory(@Header, SizeOf(Header));
1438 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1439 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1440 end
1441 else
1442 begin
1443 Header := MapReader.GetMapHeader();
1444 Result.Name := Header.MapName;
1445 Result.Description := Header.MapDescription;
1446 end;
1448 FreeMem(Data);
1449 MapReader.Free();
1451 Result.Map := Res;
1452 Result.Author := Header.MapAuthor;
1453 Result.Height := Header.Height;
1454 Result.Width := Header.Width;
1455 end;
1457 function g_Map_GetMapsList(WADName: string): SArray;
1458 var
1459 WAD: TWADFile;
1460 a: Integer;
1461 ResList: SArray;
1462 begin
1463 Result := nil;
1464 WAD := TWADFile.Create();
1465 if not WAD.ReadFile(WADName) then
1466 begin
1467 WAD.Free();
1468 Exit;
1469 end;
1470 ResList := WAD.GetMapResources();
1471 if ResList <> nil then
1472 begin
1473 for a := 0 to High(ResList) do
1474 begin
1475 SetLength(Result, Length(Result)+1);
1476 Result[High(Result)] := ResList[a];
1477 end;
1478 end;
1479 WAD.Free();
1480 end;
1482 function g_Map_Exist(Res: string): Boolean;
1483 var
1484 WAD: TWADFile;
1485 FileName, mnn: string;
1486 ResList: SArray;
1487 a: Integer;
1488 begin
1489 Result := False;
1491 FileName := addWadExtension(g_ExtractWadName(Res));
1493 WAD := TWADFile.Create;
1494 if not WAD.ReadFile(FileName) then
1495 begin
1496 WAD.Free();
1497 Exit;
1498 end;
1500 ResList := WAD.GetMapResources();
1501 WAD.Free();
1503 mnn := g_ExtractFileName(Res);
1504 if ResList <> nil then
1505 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1506 begin
1507 Result := True;
1508 Exit;
1509 end;
1510 end;
1512 procedure g_Map_Free();
1513 var
1514 a: Integer;
1516 procedure FreePanelArray(var panels: TPanelArray);
1517 var
1518 i: Integer;
1520 begin
1521 if panels <> nil then
1522 begin
1523 for i := 0 to High(panels) do
1524 panels[i].Free();
1525 panels := nil;
1526 end;
1527 end;
1529 begin
1530 g_GFX_Free();
1531 g_Weapon_Free();
1532 g_Items_Free();
1533 g_Triggers_Free();
1534 g_Monsters_Free();
1536 RespawnPoints := nil;
1537 if FlagPoints[FLAG_RED] <> nil then
1538 begin
1539 Dispose(FlagPoints[FLAG_RED]);
1540 FlagPoints[FLAG_RED] := nil;
1541 end;
1542 if FlagPoints[FLAG_BLUE] <> nil then
1543 begin
1544 Dispose(FlagPoints[FLAG_BLUE]);
1545 FlagPoints[FLAG_BLUE] := nil;
1546 end;
1547 //DOMFlagPoints := nil;
1549 //gDOMFlags := nil;
1551 if Textures <> nil then
1552 begin
1553 for a := 0 to High(Textures) do
1554 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1555 if Textures[a].Anim then
1556 g_Frames_DeleteByID(Textures[a].FramesID)
1557 else
1558 if Textures[a].TextureID <> TEXTURE_NONE then
1559 e_DeleteTexture(Textures[a].TextureID);
1561 Textures := nil;
1562 end;
1564 FreePanelArray(gWalls);
1565 FreePanelArray(gRenderBackgrounds);
1566 FreePanelArray(gRenderForegrounds);
1567 FreePanelArray(gWater);
1568 FreePanelArray(gAcid1);
1569 FreePanelArray(gAcid2);
1570 FreePanelArray(gSteps);
1571 FreePanelArray(gLifts);
1572 FreePanelArray(gBlockMon);
1574 if BackID <> DWORD(-1) then
1575 begin
1576 gBackSize.X := 0;
1577 gBackSize.Y := 0;
1578 e_DeleteTexture(BackID);
1579 BackID := DWORD(-1);
1580 end;
1582 g_Game_StopAllSounds(False);
1583 gMusic.FreeSound();
1584 g_Sound_Delete(gMapInfo.MusicName);
1586 gMapInfo.Name := '';
1587 gMapInfo.Description := '';
1588 gMapInfo.MusicName := '';
1589 gMapInfo.Height := 0;
1590 gMapInfo.Width := 0;
1592 gDoorMap := nil;
1593 gLiftMap := nil;
1595 PanelByID := nil;
1596 end;
1598 procedure g_Map_Update();
1599 var
1600 a, d, j: Integer;
1601 m: Word;
1602 s: String;
1604 procedure UpdatePanelArray(var panels: TPanelArray);
1605 var
1606 i: Integer;
1608 begin
1609 if panels <> nil then
1610 for i := 0 to High(panels) do
1611 panels[i].Update();
1612 end;
1614 begin
1615 UpdatePanelArray(gWalls);
1616 UpdatePanelArray(gRenderBackgrounds);
1617 UpdatePanelArray(gRenderForegrounds);
1618 UpdatePanelArray(gWater);
1619 UpdatePanelArray(gAcid1);
1620 UpdatePanelArray(gAcid2);
1621 UpdatePanelArray(gSteps);
1623 if gGameSettings.GameMode = GM_CTF then
1624 begin
1625 for a := FLAG_RED to FLAG_BLUE do
1626 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
1627 with gFlags[a] do
1628 begin
1629 if gFlags[a].Animation <> nil then
1630 gFlags[a].Animation.Update();
1632 m := g_Obj_Move(@Obj, True, True);
1634 if gTime mod (GAME_TICK*2) <> 0 then
1635 Continue;
1637 // Ñîïðîòèâëåíèå âîçäóõà:
1638 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1640 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
1641 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
1642 begin
1643 g_Map_ResetFlag(a);
1644 gFlags[a].CaptureTime := 0;
1645 if a = FLAG_RED then
1646 s := _lc[I_PLAYER_FLAG_RED]
1647 else
1648 s := _lc[I_PLAYER_FLAG_BLUE];
1649 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1651 if g_Game_IsNet then
1652 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
1653 Continue;
1654 end;
1656 if Count > 0 then
1657 Count := Count - 1;
1659 // Èãðîê áåðåò ôëàã:
1660 if gPlayers <> nil then
1661 begin
1662 j := Random(Length(gPlayers)) - 1;
1664 for d := 0 to High(gPlayers) do
1665 begin
1666 Inc(j);
1667 if j > High(gPlayers) then
1668 j := 0;
1670 if gPlayers[j] <> nil then
1671 if gPlayers[j].Live and
1672 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
1673 begin
1674 if gPlayers[j].GetFlag(a) then
1675 Break;
1676 end;
1677 end;
1678 end;
1679 end;
1680 end;
1681 end;
1684 var
1685 pvpset: array of PPanel = nil; // potentially lit panels
1686 pvpb, pvpe: array [0..7] of Integer; // start/end (inclusive) of the correspoinding type panels in pvpset
1687 pvpcount: Integer = -1; // to avoid constant reallocations
1689 function pvpType (panelType: Word): Integer;
1690 begin
1691 case panelType of
1692 PANEL_WALL, PANEL_CLOSEDOOR: result := 0; // gWalls
1693 PANEL_BACK: result := 1; // gRenderBackgrounds
1694 PANEL_FORE: result := 2; // gRenderForegrounds
1695 PANEL_WATER: result := 3; // gWater
1696 PANEL_ACID1: result := 4; // gAcid1
1697 PANEL_ACID2: result := 5; // gAcid2
1698 PANEL_STEP: result := 6; // gSteps
1699 else result := -1;
1700 end;
1701 end;
1703 procedure g_Map_ResetPVP ();
1704 begin
1705 pvpcount := -1; // special
1706 end;
1708 procedure g_Map_BuildPVP (minx, miny, maxx, maxy: Integer);
1709 var
1710 idx: Integer;
1711 tpc: Integer;
1713 procedure checkPanels (var panels: TPanelArray; stp: Integer);
1714 var
1715 idx, x, y, w, h: Integer;
1716 begin
1717 if panels = nil then exit;
1718 tpc := tpc+Length(panels);
1719 if (stp < 0) or (stp > 6) then exit;
1720 pvpb[stp] := pvpcount;
1721 for idx := 0 to High(panels) do
1722 begin
1723 w := panels[idx].Width;
1724 h := panels[idx].Height;
1725 if (w < 1) or (h < 1) then continue;
1726 x := panels[idx].X;
1727 y := panels[idx].Y;
1728 if (x > maxx) or (y > maxy) then continue;
1729 if (x+w <= minx) or (y+h <= miny) then continue;
1730 if pvpcount = length(pvpset) then SetLength(pvpset, pvpcount+32768);
1731 pvpset[pvpcount] := @panels[idx];
1732 Inc(pvpcount);
1733 end;
1734 pvpe[stp] := pvpcount-1;
1735 end;
1737 begin
1738 //e_WriteLog(Format('visible rect: (%d,%d)-(%d,%d)', [minx, miny, maxx, maxy]), MSG_NOTIFY);
1739 pvpcount := 0;
1740 for idx := 0 to High(pvpb) do begin pvpb[idx] := 0; pvpe[idx] := -1; end;
1741 tpc := 0;
1742 checkPanels(gWalls, 0);
1743 checkPanels(gRenderBackgrounds, 1);
1744 checkPanels(gRenderForegrounds, 2);
1745 checkPanels(gWater, 3);
1746 checkPanels(gAcid1, 4);
1747 checkPanels(gAcid2, 5);
1748 checkPanels(gSteps, 6);
1749 //e_WriteLog(Format('total panels: %d; visible panels: %d', [tpc, pvpcount]), MSG_NOTIFY);
1750 end;
1752 procedure g_Map_DrawPanels(PanelType: Word);
1754 procedure DrawPanels (stp: Integer; var panels: TPanelArray; drawDoors: Boolean=False);
1755 var
1756 idx: Integer;
1757 begin
1758 if (panels <> nil) and (stp >= 0) and (stp <= 6) then
1759 begin
1760 if pvpcount < 0 then
1761 begin
1762 // alas, no visible set
1763 for idx := 0 to High(panels) do
1764 begin
1765 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
1766 end;
1767 end
1768 else
1769 begin
1770 // wow, use visible set
1771 if pvpb[stp] <= pvpe[stp] then
1772 begin
1773 for idx := pvpb[stp] to pvpe[stp] do
1774 begin
1775 if not (drawDoors xor pvpset[idx].Door) then pvpset[idx].Draw();
1776 end;
1777 end;
1778 end;
1779 end;
1780 end;
1782 begin
1783 case PanelType of
1784 PANEL_WALL: DrawPanels(0, gWalls);
1785 PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True);
1786 PANEL_BACK: DrawPanels(1, gRenderBackgrounds);
1787 PANEL_FORE: DrawPanels(2, gRenderForegrounds);
1788 PANEL_WATER: DrawPanels(3, gWater);
1789 PANEL_ACID1: DrawPanels(4, gAcid1);
1790 PANEL_ACID2: DrawPanels(5, gAcid2);
1791 PANEL_STEP: DrawPanels(6, gSteps);
1792 end;
1793 end;
1795 procedure g_Map_DrawBack(dx, dy: Integer);
1796 begin
1797 if gDrawBackGround and (BackID <> DWORD(-1)) then
1798 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
1799 else
1800 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
1801 end;
1803 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
1804 PanelType: Word; b1x3: Boolean): Boolean;
1805 var
1806 a, h: Integer;
1807 begin
1808 Result := False;
1810 if WordBool(PanelType and PANEL_WALL) then
1811 if gWalls <> nil then
1812 begin
1813 h := High(gWalls);
1815 for a := 0 to h do
1816 if gWalls[a].Enabled and
1817 g_Collide(X, Y, Width, Height,
1818 gWalls[a].X, gWalls[a].Y,
1819 gWalls[a].Width, gWalls[a].Height) then
1820 begin
1821 Result := True;
1822 Exit;
1823 end;
1824 end;
1826 if WordBool(PanelType and PANEL_WATER) then
1827 if gWater <> nil then
1828 begin
1829 h := High(gWater);
1831 for a := 0 to h do
1832 if g_Collide(X, Y, Width, Height,
1833 gWater[a].X, gWater[a].Y,
1834 gWater[a].Width, gWater[a].Height) then
1835 begin
1836 Result := True;
1837 Exit;
1838 end;
1839 end;
1841 if WordBool(PanelType and PANEL_ACID1) then
1842 if gAcid1 <> nil then
1843 begin
1844 h := High(gAcid1);
1846 for a := 0 to h do
1847 if g_Collide(X, Y, Width, Height,
1848 gAcid1[a].X, gAcid1[a].Y,
1849 gAcid1[a].Width, gAcid1[a].Height) then
1850 begin
1851 Result := True;
1852 Exit;
1853 end;
1854 end;
1856 if WordBool(PanelType and PANEL_ACID2) then
1857 if gAcid2 <> nil then
1858 begin
1859 h := High(gAcid2);
1861 for a := 0 to h do
1862 if g_Collide(X, Y, Width, Height,
1863 gAcid2[a].X, gAcid2[a].Y,
1864 gAcid2[a].Width, gAcid2[a].Height) then
1865 begin
1866 Result := True;
1867 Exit;
1868 end;
1869 end;
1871 if WordBool(PanelType and PANEL_STEP) then
1872 if gSteps <> nil then
1873 begin
1874 h := High(gSteps);
1876 for a := 0 to h do
1877 if g_Collide(X, Y, Width, Height,
1878 gSteps[a].X, gSteps[a].Y,
1879 gSteps[a].Width, gSteps[a].Height) then
1880 begin
1881 Result := True;
1882 Exit;
1883 end;
1884 end;
1886 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
1887 if gLifts <> nil then
1888 begin
1889 h := High(gLifts);
1891 for a := 0 to h do
1892 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
1893 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
1894 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
1895 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
1896 g_Collide(X, Y, Width, Height,
1897 gLifts[a].X, gLifts[a].Y,
1898 gLifts[a].Width, gLifts[a].Height) then
1899 begin
1900 Result := True;
1901 Exit;
1902 end;
1903 end;
1905 if WordBool(PanelType and PANEL_BLOCKMON) then
1906 if gBlockMon <> nil then
1907 begin
1908 h := High(gBlockMon);
1910 for a := 0 to h do
1911 if ( (not b1x3) or
1912 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
1913 g_Collide(X, Y, Width, Height,
1914 gBlockMon[a].X, gBlockMon[a].Y,
1915 gBlockMon[a].Width, gBlockMon[a].Height) then
1916 begin
1917 Result := True;
1918 Exit;
1919 end;
1920 end;
1921 end;
1923 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
1924 var
1925 a, h: Integer;
1926 begin
1927 Result := TEXTURE_NONE;
1929 if gWater <> nil then
1930 begin
1931 h := High(gWater);
1933 for a := 0 to h do
1934 if g_Collide(X, Y, Width, Height,
1935 gWater[a].X, gWater[a].Y,
1936 gWater[a].Width, gWater[a].Height) then
1937 begin
1938 Result := gWater[a].GetTextureID();
1939 Exit;
1940 end;
1941 end;
1943 if gAcid1 <> nil then
1944 begin
1945 h := High(gAcid1);
1947 for a := 0 to h do
1948 if g_Collide(X, Y, Width, Height,
1949 gAcid1[a].X, gAcid1[a].Y,
1950 gAcid1[a].Width, gAcid1[a].Height) then
1951 begin
1952 Result := gAcid1[a].GetTextureID();
1953 Exit;
1954 end;
1955 end;
1957 if gAcid2 <> nil then
1958 begin
1959 h := High(gAcid2);
1961 for a := 0 to h do
1962 if g_Collide(X, Y, Width, Height,
1963 gAcid2[a].X, gAcid2[a].Y,
1964 gAcid2[a].Width, gAcid2[a].Height) then
1965 begin
1966 Result := gAcid2[a].GetTextureID();
1967 Exit;
1968 end;
1969 end;
1970 end;
1972 procedure g_Map_EnableWall(ID: DWORD);
1973 begin
1974 with gWalls[ID] do
1975 begin
1976 Enabled := True;
1977 g_Mark(X, Y, Width, Height, MARK_DOOR, True);
1979 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
1980 end;
1981 end;
1983 procedure g_Map_DisableWall(ID: DWORD);
1984 begin
1985 with gWalls[ID] do
1986 begin
1987 Enabled := False;
1988 g_Mark(X, Y, Width, Height, MARK_DOOR, False);
1990 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
1991 end;
1992 end;
1994 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
1995 var
1996 tp: TPanel;
1997 begin
1998 case PanelType of
1999 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2000 tp := gWalls[ID];
2001 PANEL_FORE:
2002 tp := gRenderForegrounds[ID];
2003 PANEL_BACK:
2004 tp := gRenderBackgrounds[ID];
2005 PANEL_WATER:
2006 tp := gWater[ID];
2007 PANEL_ACID1:
2008 tp := gAcid1[ID];
2009 PANEL_ACID2:
2010 tp := gAcid2[ID];
2011 PANEL_STEP:
2012 tp := gSteps[ID];
2013 else
2014 Exit;
2015 end;
2017 tp.NextTexture(AnimLoop);
2018 if g_Game_IsServer and g_Game_IsNet then
2019 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2020 end;
2022 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2023 begin
2024 if gLifts[ID].LiftType = t then
2025 Exit;
2027 with gLifts[ID] do
2028 begin
2029 LiftType := t;
2031 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2033 if LiftType = 0 then
2034 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2035 else if LiftType = 1 then
2036 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2037 else if LiftType = 2 then
2038 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2039 else if LiftType = 3 then
2040 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2042 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2043 end;
2044 end;
2046 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2047 var
2048 a: Integer;
2049 PointsArray: Array of TRespawnPoint;
2050 begin
2051 Result := False;
2052 SetLength(PointsArray, 0);
2054 if RespawnPoints = nil then
2055 Exit;
2057 for a := 0 to High(RespawnPoints) do
2058 if RespawnPoints[a].PointType = PointType then
2059 begin
2060 SetLength(PointsArray, Length(PointsArray)+1);
2061 PointsArray[High(PointsArray)] := RespawnPoints[a];
2062 end;
2064 if PointsArray = nil then
2065 Exit;
2067 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2068 Result := True;
2069 end;
2071 function g_Map_GetPointCount(PointType: Byte): Word;
2072 var
2073 a: Integer;
2074 begin
2075 Result := 0;
2077 if RespawnPoints = nil then
2078 Exit;
2080 for a := 0 to High(RespawnPoints) do
2081 if RespawnPoints[a].PointType = PointType then
2082 Result := Result + 1;
2083 end;
2085 function g_Map_HaveFlagPoints(): Boolean;
2086 begin
2087 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2088 end;
2090 procedure g_Map_ResetFlag(Flag: Byte);
2091 begin
2092 with gFlags[Flag] do
2093 begin
2094 Obj.X := -1000;
2095 Obj.Y := -1000;
2096 Obj.Vel.X := 0;
2097 Obj.Vel.Y := 0;
2098 Direction := D_LEFT;
2099 State := FLAG_STATE_NONE;
2100 if FlagPoints[Flag] <> nil then
2101 begin
2102 Obj.X := FlagPoints[Flag]^.X;
2103 Obj.Y := FlagPoints[Flag]^.Y;
2104 Direction := FlagPoints[Flag]^.Direction;
2105 State := FLAG_STATE_NORMAL;
2106 end;
2107 Count := -1;
2108 end;
2109 end;
2111 procedure g_Map_DrawFlags();
2112 var
2113 i, dx: Integer;
2114 Mirror: TMirrorType;
2115 begin
2116 if gGameSettings.GameMode <> GM_CTF then
2117 Exit;
2119 for i := FLAG_RED to FLAG_BLUE do
2120 with gFlags[i] do
2121 if State <> FLAG_STATE_CAPTURED then
2122 begin
2123 if State = FLAG_STATE_NONE then
2124 continue;
2126 if Direction = D_LEFT then
2127 begin
2128 Mirror := M_HORIZONTAL;
2129 dx := -1;
2130 end
2131 else
2132 begin
2133 Mirror := M_NONE;
2134 dx := 1;
2135 end;
2137 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2139 if g_debug_Frames then
2140 begin
2141 e_DrawQuad(Obj.X+Obj.Rect.X,
2142 Obj.Y+Obj.Rect.Y,
2143 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2144 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2145 0, 255, 0);
2146 end;
2147 end;
2148 end;
2150 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2151 var
2152 dw: DWORD;
2153 b: Byte;
2154 str: String;
2155 boo: Boolean;
2157 procedure SavePanelArray(var panels: TPanelArray);
2158 var
2159 PAMem: TBinMemoryWriter;
2160 i: Integer;
2161 begin
2162 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2163 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2165 i := 0;
2166 while i < Length(panels) do
2167 begin
2168 if panels[i].SaveIt then
2169 begin
2170 // ID ïàíåëè:
2171 PAMem.WriteInt(i);
2172 // Ñîõðàíÿåì ïàíåëü:
2173 panels[i].SaveState(PAMem);
2174 end;
2175 Inc(i);
2176 end;
2178 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2179 PAMem.SaveToMemory(Mem);
2180 PAMem.Free();
2181 end;
2183 procedure SaveFlag(flag: PFlag);
2184 begin
2185 // Ñèãíàòóðà ôëàãà:
2186 dw := FLAG_SIGNATURE; // 'FLAG'
2187 Mem.WriteDWORD(dw);
2188 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2189 Mem.WriteByte(flag^.RespawnType);
2190 // Ñîñòîÿíèå ôëàãà:
2191 Mem.WriteByte(flag^.State);
2192 // Íàïðàâëåíèå ôëàãà:
2193 if flag^.Direction = D_LEFT then
2194 b := 1
2195 else // D_RIGHT
2196 b := 2;
2197 Mem.WriteByte(b);
2198 // Îáúåêò ôëàãà:
2199 Obj_SaveState(@flag^.Obj, Mem);
2200 end;
2202 begin
2203 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2205 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2206 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2207 SavePanelArray(gWalls);
2208 // Ñîõðàíÿåì ïàíåëè ôîíà:
2209 SavePanelArray(gRenderBackgrounds);
2210 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2211 SavePanelArray(gRenderForegrounds);
2212 // Ñîõðàíÿåì ïàíåëè âîäû:
2213 SavePanelArray(gWater);
2214 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2215 SavePanelArray(gAcid1);
2216 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2217 SavePanelArray(gAcid2);
2218 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2219 SavePanelArray(gSteps);
2220 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2221 SavePanelArray(gLifts);
2222 ///// /////
2224 ///// Ñîõðàíÿåì ìóçûêó: /////
2225 // Ñèãíàòóðà ìóçûêè:
2226 dw := MUSIC_SIGNATURE; // 'MUSI'
2227 Mem.WriteDWORD(dw);
2228 // Íàçâàíèå ìóçûêè:
2229 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2230 if gMusic.NoMusic then
2231 str := ''
2232 else
2233 str := gMusic.Name;
2234 Mem.WriteString(str, 64);
2235 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2236 dw := gMusic.GetPosition();
2237 Mem.WriteDWORD(dw);
2238 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2239 boo := gMusic.SpecPause;
2240 Mem.WriteBoolean(boo);
2241 ///// /////
2243 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2244 Mem.WriteInt(gTotalMonsters);
2245 ///// /////
2247 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2248 if gGameSettings.GameMode = GM_CTF then
2249 begin
2250 // Ôëàã Êðàñíîé êîìàíäû:
2251 SaveFlag(@gFlags[FLAG_RED]);
2252 // Ôëàã Ñèíåé êîìàíäû:
2253 SaveFlag(@gFlags[FLAG_BLUE]);
2254 end;
2255 ///// /////
2257 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2258 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2259 begin
2260 // Î÷êè Êðàñíîé êîìàíäû:
2261 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2262 // Î÷êè Ñèíåé êîìàíäû:
2263 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2264 end;
2265 ///// /////
2266 end;
2268 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2269 var
2270 dw: DWORD;
2271 b: Byte;
2272 str: String;
2273 boo: Boolean;
2275 procedure LoadPanelArray(var panels: TPanelArray);
2276 var
2277 PAMem: TBinMemoryReader;
2278 i, id: Integer;
2279 begin
2280 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2281 PAMem := TBinMemoryReader.Create();
2282 PAMem.LoadFromMemory(Mem);
2284 for i := 0 to Length(panels)-1 do
2285 if panels[i].SaveIt then
2286 begin
2287 // ID ïàíåëè:
2288 PAMem.ReadInt(id);
2289 if id <> i then
2290 begin
2291 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2292 end;
2293 // Çàãðóæàåì ïàíåëü:
2294 panels[i].LoadState(PAMem);
2295 end;
2297 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2298 PAMem.Free();
2299 end;
2301 procedure LoadFlag(flag: PFlag);
2302 begin
2303 // Ñèãíàòóðà ôëàãà:
2304 Mem.ReadDWORD(dw);
2305 if dw <> FLAG_SIGNATURE then // 'FLAG'
2306 begin
2307 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2308 end;
2309 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2310 Mem.ReadByte(flag^.RespawnType);
2311 // Ñîñòîÿíèå ôëàãà:
2312 Mem.ReadByte(flag^.State);
2313 // Íàïðàâëåíèå ôëàãà:
2314 Mem.ReadByte(b);
2315 if b = 1 then
2316 flag^.Direction := D_LEFT
2317 else // b = 2
2318 flag^.Direction := D_RIGHT;
2319 // Îáúåêò ôëàãà:
2320 Obj_LoadState(@flag^.Obj, Mem);
2321 end;
2323 begin
2324 if Mem = nil then
2325 Exit;
2327 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2328 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2329 LoadPanelArray(gWalls);
2330 // Çàãðóæàåì ïàíåëè ôîíà:
2331 LoadPanelArray(gRenderBackgrounds);
2332 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2333 LoadPanelArray(gRenderForegrounds);
2334 // Çàãðóæàåì ïàíåëè âîäû:
2335 LoadPanelArray(gWater);
2336 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2337 LoadPanelArray(gAcid1);
2338 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2339 LoadPanelArray(gAcid2);
2340 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2341 LoadPanelArray(gSteps);
2342 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2343 LoadPanelArray(gLifts);
2344 ///// /////
2346 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé:
2347 g_GFX_Init();
2349 ///// Çàãðóæàåì ìóçûêó: /////
2350 // Ñèãíàòóðà ìóçûêè:
2351 Mem.ReadDWORD(dw);
2352 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2353 begin
2354 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2355 end;
2356 // Íàçâàíèå ìóçûêè:
2357 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2358 Mem.ReadString(str);
2359 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2360 Mem.ReadDWORD(dw);
2361 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2362 Mem.ReadBoolean(boo);
2363 // Çàïóñêàåì ýòó ìóçûêó:
2364 gMusic.SetByName(str);
2365 gMusic.SpecPause := boo;
2366 gMusic.Play();
2367 gMusic.Pause(True);
2368 gMusic.SetPosition(dw);
2369 ///// /////
2371 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2372 Mem.ReadInt(gTotalMonsters);
2373 ///// /////
2375 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2376 if gGameSettings.GameMode = GM_CTF then
2377 begin
2378 // Ôëàã Êðàñíîé êîìàíäû:
2379 LoadFlag(@gFlags[FLAG_RED]);
2380 // Ôëàã Ñèíåé êîìàíäû:
2381 LoadFlag(@gFlags[FLAG_BLUE]);
2382 end;
2383 ///// /////
2385 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2386 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2387 begin
2388 // Î÷êè Êðàñíîé êîìàíäû:
2389 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2390 // Î÷êè Ñèíåé êîìàíäû:
2391 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2392 end;
2393 ///// /////
2394 end;
2396 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2397 var
2398 Arr: TPanelArray;
2399 begin
2400 Result := nil;
2401 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2402 Arr := PanelByID[PanelID].PWhere^;
2403 PanelArrayID := PanelByID[PanelID].PArrID;
2404 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2405 end;
2407 end.