DEADSOFTWARE

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