DEADSOFTWARE

new code for blood particles (other particles are turned off temporarily): almost...
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 e_graphics, g_basic, MAPDEF, g_textures, Classes,
24 g_phys, wadreader, BinEditor, g_panel, g_grid, md5, binheap, xprofiler, xparser, xdynrec;
26 type
27 TMapInfo = record
28 Map: String;
29 Name: String;
30 Description: String;
31 Author: String;
32 MusicName: String;
33 SkyName: String;
34 Height: Word;
35 Width: Word;
36 end;
38 PRespawnPoint = ^TRespawnPoint;
39 TRespawnPoint = record
40 X, Y: Integer;
41 Direction: TDirection;
42 PointType: Byte;
43 end;
45 PFlagPoint = ^TFlagPoint;
46 TFlagPoint = TRespawnPoint;
48 PFlag = ^TFlag;
49 TFlag = record
50 Obj: TObj;
51 RespawnType: Byte;
52 State: Byte;
53 Count: Integer;
54 CaptureTime: LongWord;
55 Animation: TAnimation;
56 Direction: TDirection;
57 end;
59 function g_Map_Load(Res: String): Boolean;
60 function g_Map_GetMapInfo(Res: String): TMapInfo;
61 function g_Map_GetMapsList(WADName: String): SArray;
62 function g_Map_Exist(Res: String): Boolean;
63 procedure g_Map_Free(freeTextures: Boolean=true);
64 procedure g_Map_Update();
66 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
68 procedure g_Map_DrawPanels (PanelType: Word); // unaccelerated
69 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
71 procedure g_Map_DrawBack(dx, dy: Integer);
72 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
73 PanelType: Word; b1x3: Boolean=false): Boolean;
74 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
76 procedure g_Map_EnableWallGUID (pguid: Integer);
77 procedure g_Map_DisableWallGUID (pguid: Integer);
78 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
80 // HACK!!!
81 procedure g_Map_EnableWall_XXX (ID: DWORD);
82 procedure g_Map_DisableWall_XXX (ID: DWORD);
83 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
85 procedure g_Map_SwitchTextureGUID (PanelType: Word; pguid: Integer; AnimLoop: Byte = 0);
87 procedure g_Map_ReAdd_DieTriggers();
88 function g_Map_IsSpecialTexture(Texture: String): Boolean;
90 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
91 function g_Map_GetPointCount(PointType: Byte): Word;
93 function g_Map_HaveFlagPoints(): Boolean;
95 procedure g_Map_ResetFlag(Flag: Byte);
96 procedure g_Map_DrawFlags();
98 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
99 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
101 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
103 // returns panel or nil
104 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
105 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
107 // returns panel or nil
108 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
109 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
111 type
112 TForEachPanelCB = function (pan: TPanel): Boolean; // return `true` to stop
114 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
115 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
117 // trace liquid, stepping by `dx` and `dy`
118 // return last seen liquid coords, and `false` if we're started outside of the liquid
119 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
122 procedure g_Map_ProfilersBegin ();
123 procedure g_Map_ProfilersEnd ();
126 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
129 function g_Map_MinX (): Integer; inline;
130 function g_Map_MinY (): Integer; inline;
131 function g_Map_MaxX (): Integer; inline;
132 function g_Map_MaxY (): Integer; inline;
134 const
135 NNF_NO_NAME = 0;
136 NNF_NAME_BEFORE = 1;
137 NNF_NAME_EQUALS = 2;
138 NNF_NAME_AFTER = 3;
140 function g_Texture_NumNameFindStart(name: String): Boolean;
141 function g_Texture_NumNameFindNext(var newName: String): Byte;
143 const
144 RESPAWNPOINT_PLAYER1 = 1;
145 RESPAWNPOINT_PLAYER2 = 2;
146 RESPAWNPOINT_DM = 3;
147 RESPAWNPOINT_RED = 4;
148 RESPAWNPOINT_BLUE = 5;
150 FLAG_NONE = 0;
151 FLAG_RED = 1;
152 FLAG_BLUE = 2;
153 FLAG_DOM = 3;
155 FLAG_STATE_NONE = 0;
156 FLAG_STATE_NORMAL = 1;
157 FLAG_STATE_DROPPED = 2;
158 FLAG_STATE_CAPTURED = 3;
159 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
160 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
162 FLAG_TIME = 720; // 20 seconds
164 SKY_STRETCH: Single = 1.5;
166 const
167 GridTagInvalid = 0;
169 (* draw order:
170 PANEL_BACK
171 PANEL_STEP
172 PANEL_WALL
173 PANEL_CLOSEDOOR
174 PANEL_ACID1
175 PANEL_ACID2
176 PANEL_WATER
177 PANEL_FORE
178 *)
179 // sorted by draw priority
180 GridTagBack = 1 shl 0; // gRenderBackgrounds
181 GridTagStep = 1 shl 1; // gSteps
182 GridTagWall = 1 shl 2; // gWalls
183 GridTagDoor = 1 shl 3; // gWalls
184 GridTagAcid1 = 1 shl 4; // gAcid1
185 GridTagAcid2 = 1 shl 5; // gAcid2
186 GridTagWater = 1 shl 6; // gWater
187 GridTagFore = 1 shl 7; // gRenderForegrounds
188 // the following are invisible
189 GridTagLift = 1 shl 8; // gLifts
190 GridTagBlockMon = 1 shl 9; // gBlockMon
192 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
193 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
195 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
198 var
199 gWalls: TPanelArray;
200 gRenderBackgrounds: TPanelArray;
201 gRenderForegrounds: TPanelArray;
202 gWater, gAcid1, gAcid2: TPanelArray;
203 gSteps: TPanelArray;
204 gLifts: TPanelArray;
205 gBlockMon: TPanelArray;
206 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
207 //gDOMFlags: array of TFlag;
208 gMapInfo: TMapInfo;
209 gBackSize: TDFPoint;
210 gDoorMap: array of array of DWORD;
211 gLiftMap: array of array of DWORD;
212 gWADHash: TMD5Digest;
213 BackID: DWORD = DWORD(-1);
214 gExternalResources: TStringList;
215 gMovingWallIds: array of Integer = nil;
217 gdbg_map_use_accel_render: Boolean = true;
218 gdbg_map_use_accel_coldet: Boolean = true;
219 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
220 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
222 gCurrentMap: TDynRecord = nil;
223 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
226 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
229 type
230 TPanelGrid = specialize TBodyGridBase<TPanel>;
232 var
233 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
236 implementation
238 uses
239 e_input, g_main, e_log, SysUtils, g_items, g_gfx, g_console,
240 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
241 g_options, g_triggers, g_player,
242 Math, g_monsters, g_saveload, g_language, g_netmsg,
243 utils, sfs, xstreams, hashtable,
244 ImagingTypes, Imaging, ImagingUtility,
245 ImagingGif, ImagingNetworkGraphics;
247 const
248 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
249 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
250 FLAG_SIGNATURE = $47414C46; // 'FLAG'
253 var
254 panByGUID: array of TPanel = nil;
257 // ////////////////////////////////////////////////////////////////////////// //
258 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
259 begin
260 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
261 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
262 end;
265 // ////////////////////////////////////////////////////////////////////////// //
266 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
267 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
268 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
269 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
272 // ////////////////////////////////////////////////////////////////////////// //
273 var
274 dfmapdef: TDynMapDef = nil;
276 procedure loadMapDefinition ();
277 var
278 pr: TTextParser = nil;
279 st: TStream = nil;
280 WAD: TWADFile = nil;
281 begin
282 if (dfmapdef <> nil) then exit;
283 try
284 e_LogWritefln('parsing "mapdef.txt"...', []);
285 st := openDiskFileRO(DataDir+'mapdef.txt');
286 except
287 st := nil;
288 e_LogWritefln('local "%smapdef.txt" not found', [DataDir]);
289 end;
290 if (st = nil) then
291 begin
292 WAD := TWADFile.Create();
293 if not WAD.ReadFile(GameWAD) then
294 begin
295 //raise Exception.Create('cannot load "game.wad"');
296 st := nil;
297 end
298 else
299 begin
300 st := WAD.openFileStream('mapdef.txt');
301 end;
302 end;
304 if (st = nil) then
305 begin
306 //raise Exception.Create('cannot open "mapdef.txt"');
307 e_LogWritefln('using default "mapdef.txt"...', [], MSG_WARNING);
308 pr := TStrTextParser.Create(defaultMapDef);
309 end
310 else
311 begin
312 pr := TFileTextParser.Create(st);
313 end;
315 try
316 dfmapdef := TDynMapDef.Create(pr);
317 except on e: Exception do
318 raise Exception.Create(Format('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.line, pr.col, e.message]));
319 end;
321 st.Free();
322 WAD.Free();
323 end;
326 // ////////////////////////////////////////////////////////////////////////// //
327 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
328 var
329 wst: TSFSMemoryChunkStream = nil;
330 pr: TTextParser = nil;
331 begin
332 result := nil;
333 if (dataLen < 4) then exit;
334 loadMapDefinition();
335 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
337 wst := TSFSMemoryChunkStream.Create(data, dataLen);
339 if (PAnsiChar(data)[0] = 'M') and (PAnsiChar(data)[1] = 'A') and (PAnsiChar(data)[2] = 'P') and (PByte(data)[3] = 1) then
340 begin
341 // binary map
342 try
343 result := dfmapdef.parseBinMap(wst);
344 except on e: Exception do
345 begin
346 e_LogWritefln('ERROR: %s', [e.message]);
347 wst.Free();
348 result := nil;
349 exit;
350 end;
351 end;
352 wst.Free();
353 end
354 else
355 begin
356 // text map
357 pr := TFileTextParser.Create(wst);
358 try
359 result := dfmapdef.parseMap(pr);
360 except on e: Exception do
361 begin
362 if (pr <> nil) then e_LogWritefln('ERROR at (%s,%s): %s', [pr.line, pr.col, e.message])
363 else e_LogWritefln('ERROR: %s', [e.message]);
364 pr.Free(); // will free `wst`
365 result := nil;
366 exit;
367 end;
368 end;
369 pr.Free(); // will free `wst`
370 end;
371 end;
374 // ////////////////////////////////////////////////////////////////////////// //
375 var
376 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
377 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
378 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
381 function g_Texture_NumNameFindStart(name: String): Boolean;
382 var
383 i: Integer;
385 begin
386 Result := False;
387 NNF_PureName := '';
388 NNF_FirstNum := -1;
389 NNF_CurrentNum := -1;
391 for i := Length(name) downto 1 do
392 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
393 begin
394 if i = Length(name) then
395 begin // Íåò öèôð â êîíöå ñòðîêè
396 Exit;
397 end
398 else
399 begin
400 NNF_PureName := Copy(name, 1, i);
401 Delete(name, 1, i);
402 Break;
403 end;
404 end;
406 // Íå ïåðåâåñòè â ÷èñëî:
407 if not TryStrToInt(name, NNF_FirstNum) then
408 Exit;
410 NNF_CurrentNum := 0;
412 Result := True;
413 end;
416 function g_Texture_NumNameFindNext(var newName: String): Byte;
417 begin
418 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
419 begin
420 newName := '';
421 Result := NNF_NO_NAME;
422 Exit;
423 end;
425 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
427 if NNF_CurrentNum < NNF_FirstNum then
428 Result := NNF_NAME_BEFORE
429 else
430 if NNF_CurrentNum > NNF_FirstNum then
431 Result := NNF_NAME_AFTER
432 else
433 Result := NNF_NAME_EQUALS;
435 Inc(NNF_CurrentNum);
436 end;
439 // ////////////////////////////////////////////////////////////////////////// //
440 function panelTypeToTag (panelType: Word): Integer;
441 begin
442 case panelType of
443 PANEL_WALL: result := GridTagWall; // gWalls
444 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
445 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
446 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
447 PANEL_WATER: result := GridTagWater; // gWater
448 PANEL_ACID1: result := GridTagAcid1; // gAcid1
449 PANEL_ACID2: result := GridTagAcid2; // gAcid2
450 PANEL_STEP: result := GridTagStep; // gSteps
451 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
452 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
453 else result := GridTagInvalid;
454 end;
455 end;
458 function dplLess (a, b: TObject): Boolean;
459 var
460 pa, pb: TPanel;
461 begin
462 pa := TPanel(a);
463 pb := TPanel(b);
464 if (pa.tag < pb.tag) then begin result := true; exit; end;
465 if (pa.tag > pb.tag) then begin result := false; exit; end;
466 result := (pa.arrIdx < pb.arrIdx);
467 end;
469 procedure dplClear ();
470 begin
471 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
472 end;
475 var
476 Textures: TLevelTextureArray = nil;
477 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
478 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
479 RespawnPoints: array of TRespawnPoint;
480 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
481 //DOMFlagPoints: Array of TFlagPoint;
484 procedure g_Map_ProfilersBegin ();
485 begin
486 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
487 profMapCollision.mainBegin(g_profile_collision);
488 // create sections
489 if g_profile_collision then
490 begin
491 profMapCollision.sectionBegin('*solids');
492 profMapCollision.sectionEnd();
493 profMapCollision.sectionBegin('liquids');
494 profMapCollision.sectionEnd();
495 end;
496 end;
498 procedure g_Map_ProfilersEnd ();
499 begin
500 if (profMapCollision <> nil) then profMapCollision.mainEnd();
501 end;
504 // wall index in `gWalls` or -1
505 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
506 var
507 ex, ey: Integer;
508 begin
509 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
510 if (result <> nil) then
511 begin
512 if (hitx <> nil) then hitx^ := ex;
513 if (hity <> nil) then hity^ := ey;
514 end
515 else
516 begin
517 if (hitx <> nil) then hitx^ := x1;
518 if (hity <> nil) then hity^ := y1;
519 end;
520 end;
522 // returns panel or nil
523 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
524 var
525 ex, ey: Integer;
526 begin
527 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
528 if (result <> nil) then
529 begin
530 if (hitx <> nil) then hitx^ := ex;
531 if (hity <> nil) then hity^ := ey;
532 end
533 else
534 begin
535 if (hitx <> nil) then hitx^ := x1;
536 if (hity <> nil) then hity^ := y1;
537 end;
538 end;
541 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
543 function checker (pan: TPanel; tag: Integer): Boolean;
544 begin
546 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
547 begin
548 result := pan.Enabled; // stop if wall is enabled
549 exit;
550 end;
553 if ((tag and GridTagLift) <> 0) then
554 begin
555 // stop if the lift of the right type
556 result :=
557 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
558 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
559 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
560 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
561 exit;
562 end;
564 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
565 end;
567 var
568 tagmask: Integer = 0;
569 begin
570 result := false;
572 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
573 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
574 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
575 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
576 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
577 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
578 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
580 if (tagmask = 0) then exit;// just in case
581 if ((tagmask and GridTagLift) <> 0) then
582 begin
583 // slow
584 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
585 end
586 else
587 begin
588 // fast
589 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
590 end;
591 end;
594 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
595 begin
596 result := nil;
597 if (tagmask = 0) then exit;
598 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
599 end;
602 function g_Map_IsSpecialTexture(Texture: String): Boolean;
603 begin
604 Result := (Texture = TEXTURE_NAME_WATER) or
605 (Texture = TEXTURE_NAME_ACID1) or
606 (Texture = TEXTURE_NAME_ACID2);
607 end;
609 procedure CreateDoorMap();
610 var
611 PanelArray: Array of record
612 X, Y: Integer;
613 Width, Height: Word;
614 Active: Boolean;
615 PanelID: DWORD;
616 end;
617 a, b, c, m, i, len: Integer;
618 ok: Boolean;
619 begin
620 if gWalls = nil then
621 Exit;
623 i := 0;
624 len := 128;
625 SetLength(PanelArray, len);
627 for a := 0 to High(gWalls) do
628 if gWalls[a].Door then
629 begin
630 PanelArray[i].X := gWalls[a].X;
631 PanelArray[i].Y := gWalls[a].Y;
632 PanelArray[i].Width := gWalls[a].Width;
633 PanelArray[i].Height := gWalls[a].Height;
634 PanelArray[i].Active := True;
635 PanelArray[i].PanelID := a;
637 i := i + 1;
638 if i = len then
639 begin
640 len := len + 128;
641 SetLength(PanelArray, len);
642 end;
643 end;
645 // Íåò äâåðåé:
646 if i = 0 then
647 begin
648 PanelArray := nil;
649 Exit;
650 end;
652 SetLength(gDoorMap, 0);
654 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
656 for a := 0 to i-1 do
657 if PanelArray[a].Active then
658 begin
659 PanelArray[a].Active := False;
660 m := Length(gDoorMap);
661 SetLength(gDoorMap, m+1);
662 SetLength(gDoorMap[m], 1);
663 gDoorMap[m, 0] := PanelArray[a].PanelID;
664 ok := True;
666 while ok do
667 begin
668 ok := False;
670 for b := 0 to i-1 do
671 if PanelArray[b].Active then
672 for c := 0 to High(gDoorMap[m]) do
673 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
674 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
675 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
676 PanelArray[b].Width, PanelArray[b].Height,
677 gWalls[gDoorMap[m, c]].X,
678 gWalls[gDoorMap[m, c]].Y,
679 gWalls[gDoorMap[m, c]].Width,
680 gWalls[gDoorMap[m, c]].Height) then
681 begin
682 PanelArray[b].Active := False;
683 SetLength(gDoorMap[m],
684 Length(gDoorMap[m])+1);
685 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
686 ok := True;
687 Break;
688 end;
689 end;
691 g_Game_StepLoading();
692 end;
694 PanelArray := nil;
695 end;
697 procedure CreateLiftMap();
698 var
699 PanelArray: Array of record
700 X, Y: Integer;
701 Width, Height: Word;
702 Active: Boolean;
703 end;
704 a, b, c, len, i, j: Integer;
705 ok: Boolean;
706 begin
707 if gLifts = nil then
708 Exit;
710 len := Length(gLifts);
711 SetLength(PanelArray, len);
713 for a := 0 to len-1 do
714 begin
715 PanelArray[a].X := gLifts[a].X;
716 PanelArray[a].Y := gLifts[a].Y;
717 PanelArray[a].Width := gLifts[a].Width;
718 PanelArray[a].Height := gLifts[a].Height;
719 PanelArray[a].Active := True;
720 end;
722 SetLength(gLiftMap, len);
723 i := 0;
725 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
727 for a := 0 to len-1 do
728 if PanelArray[a].Active then
729 begin
730 PanelArray[a].Active := False;
731 SetLength(gLiftMap[i], 32);
732 j := 0;
733 gLiftMap[i, j] := a;
734 ok := True;
736 while ok do
737 begin
738 ok := False;
739 for b := 0 to len-1 do
740 if PanelArray[b].Active then
741 for c := 0 to j do
742 if g_CollideAround(PanelArray[b].X,
743 PanelArray[b].Y,
744 PanelArray[b].Width,
745 PanelArray[b].Height,
746 PanelArray[gLiftMap[i, c]].X,
747 PanelArray[gLiftMap[i, c]].Y,
748 PanelArray[gLiftMap[i, c]].Width,
749 PanelArray[gLiftMap[i, c]].Height) then
750 begin
751 PanelArray[b].Active := False;
752 j := j+1;
753 if j > High(gLiftMap[i]) then
754 SetLength(gLiftMap[i],
755 Length(gLiftMap[i])+32);
757 gLiftMap[i, j] := b;
758 ok := True;
760 Break;
761 end;
762 end;
764 SetLength(gLiftMap[i], j+1);
765 i := i+1;
767 g_Game_StepLoading();
768 end;
770 SetLength(gLiftMap, i);
772 PanelArray := nil;
773 end;
775 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer; sav: Boolean): Integer;
776 var
777 len: Integer;
778 panels: ^TPanelArray;
779 pan: TPanel;
780 pguid: Integer;
781 begin
782 Result := -1;
784 case PanelRec.PanelType of
785 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
786 PANEL_BACK: panels := @gRenderBackgrounds;
787 PANEL_FORE: panels := @gRenderForegrounds;
788 PANEL_WATER: panels := @gWater;
789 PANEL_ACID1: panels := @gAcid1;
790 PANEL_ACID2: panels := @gAcid2;
791 PANEL_STEP: panels := @gSteps;
792 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
793 PANEL_BLOCKMON: panels := @gBlockMon;
794 else exit;
795 end;
797 len := Length(panels^);
798 SetLength(panels^, len+1);
800 pguid := Length(panByGUID);
801 SetLength(panByGUID, pguid+1); //FIXME!
802 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
803 assert(pguid >= 0);
804 assert(pguid < Length(panByGUID));
805 panByGUID[pguid] := pan;
806 panels^[len] := pan;
807 pan.arrIdx := len;
808 pan.proxyId := -1;
809 pan.tag := panelTypeToTag(PanelRec.PanelType);
810 if sav then pan.SaveIt := True;
812 PanelRec.user['panel_guid'] := pguid;
814 //result := len;
815 result := pguid;
816 end;
819 function CreateNullTexture(RecName: String): Integer;
820 begin
821 RecName := toLowerCase1251(RecName);
822 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
823 if TextNameHash.get(RecName, result) then exit; // i found her!
825 SetLength(Textures, Length(Textures)+1);
826 result := High(Textures);
828 with Textures[High(Textures)] do
829 begin
830 TextureName := RecName;
831 Width := 1;
832 Height := 1;
833 Anim := False;
834 TextureID := LongWord(TEXTURE_NONE);
835 end;
837 TextNameHash.put(RecName, result);
838 end;
841 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
842 var
843 WAD: TWADFile;
844 TextureData: Pointer;
845 WADName: String;
846 a, ResLength: Integer;
847 begin
848 RecName := toLowerCase1251(RecName);
849 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
850 if TextNameHash.get(RecName, result) then
851 begin
852 // i found her!
853 //e_LogWritefln('texture ''%s'' already loaded', [RecName]);
854 exit;
855 end;
857 Result := -1;
859 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
862 if Textures <> nil then
863 begin
864 for a := 0 to High(Textures) do
865 begin
866 if (Textures[a].TextureName = RecName) then
867 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
868 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
869 Result := a;
870 Exit;
871 end;
872 end;
873 end;
876 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
877 if (RecName = TEXTURE_NAME_WATER) or
878 (RecName = TEXTURE_NAME_ACID1) or
879 (RecName = TEXTURE_NAME_ACID2) then
880 begin
881 SetLength(Textures, Length(Textures)+1);
883 with Textures[High(Textures)] do
884 begin
885 TextureName := RecName;
886 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
887 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
888 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
890 Anim := False;
891 end;
893 result := High(Textures);
894 TextNameHash.put(RecName, result);
895 Exit;
896 end;
898 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
899 WADName := g_ExtractWadName(RecName);
901 WAD := TWADFile.Create();
903 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
905 WAD.ReadFile(WADName);
907 //txname := RecName;
909 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
910 begin
911 FreeMem(TextureData);
912 RecName := 'COMMON\ALIEN';
913 end;
916 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
917 begin
918 SetLength(Textures, Length(Textures)+1);
919 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
920 begin
921 SetLength(Textures, Length(Textures)-1);
922 Exit;
923 end;
924 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
925 FreeMem(TextureData);
926 Textures[High(Textures)].TextureName := RecName;
927 Textures[High(Textures)].Anim := False;
929 result := High(Textures);
930 TextNameHash.put(RecName, result);
931 end
932 else // Íåò òàêîãî ðåóñðñà â WAD'å
933 begin
934 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
935 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
936 if log and (not BadTextNameHash.get(RecName, a)) then
937 begin
938 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
939 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
940 end;
941 BadTextNameHash.put(RecName, -1);
942 end;
944 WAD.Free();
945 end;
948 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
949 var
950 WAD: TWADFile;
951 TextureWAD: PChar = nil;
952 TextData: Pointer = nil;
953 TextureData: Pointer = nil;
954 cfg: TConfig = nil;
955 WADName: String;
956 ResLength: Integer;
957 TextureResource: String;
958 _width, _height, _framecount, _speed: Integer;
959 _backanimation: Boolean;
960 //imgfmt: string;
961 ia: TDynImageDataArray = nil;
962 f, c, frdelay, frloop: Integer;
963 begin
964 RecName := toLowerCase1251(RecName);
965 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
966 if TextNameHash.get(RecName, result) then
967 begin
968 // i found her!
969 //e_LogWritefln('animated texture ''%s'' already loaded', [RecName]);
970 exit;
971 end;
973 result := -1;
975 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
977 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
978 if BadTextNameHash.get(RecName, f) then
979 begin
980 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
981 exit;
982 end;
984 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
985 WADName := g_ExtractWadName(RecName);
987 WAD := TWADFile.Create();
988 try
989 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
991 WAD.ReadFile(WADName);
993 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
994 begin
995 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
996 if log and (not BadTextNameHash.get(RecName, f)) then
997 begin
998 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
999 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1000 end;
1001 BadTextNameHash.put(RecName, -1);
1002 exit;
1003 end;
1005 {TEST
1006 if WADName = Map then
1007 begin
1008 //FreeMem(TextureWAD);
1009 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1010 end;
1013 WAD.FreeWAD();
1015 if ResLength < 6 then
1016 begin
1017 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
1018 BadTextNameHash.put(RecName, -1);
1019 exit;
1020 end;
1022 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1023 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1024 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1025 begin
1026 // íåò, ýòî ñóïåðìåí!
1027 if not WAD.ReadMemory(TextureWAD, ResLength) then
1028 begin
1029 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
1030 BadTextNameHash.put(RecName, -1);
1031 exit;
1032 end;
1034 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1035 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1036 begin
1037 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
1038 BadTextNameHash.put(RecName, -1);
1039 exit;
1040 end;
1042 cfg := TConfig.CreateMem(TextData, ResLength);
1044 TextureResource := cfg.ReadStr('', 'resource', '');
1045 if TextureResource = '' then
1046 begin
1047 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
1048 BadTextNameHash.put(RecName, -1);
1049 exit;
1050 end;
1052 _width := cfg.ReadInt('', 'framewidth', 0);
1053 _height := cfg.ReadInt('', 'frameheight', 0);
1054 _framecount := cfg.ReadInt('', 'framecount', 0);
1055 _speed := cfg.ReadInt('', 'waitcount', 0);
1056 _backanimation := cfg.ReadBool('', 'backanimation', False);
1058 cfg.Free();
1059 cfg := nil;
1061 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1062 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1063 begin
1064 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
1065 BadTextNameHash.put(RecName, -1);
1066 exit;
1067 end;
1069 WAD.Free();
1070 WAD := nil;
1072 SetLength(Textures, Length(Textures)+1);
1073 with Textures[High(Textures)] do
1074 begin
1075 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1076 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1077 begin
1078 TextureName := RecName;
1079 Width := _width;
1080 Height := _height;
1081 Anim := True;
1082 FramesCount := _framecount;
1083 Speed := _speed;
1084 result := High(Textures);
1085 TextNameHash.put(RecName, result);
1086 end
1087 else
1088 begin
1089 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1090 if log and (not BadTextNameHash.get(RecName, f)) then
1091 begin
1092 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1093 end;
1094 BadTextNameHash.put(RecName, -1);
1095 end;
1096 end;
1097 end
1098 else
1099 begin
1100 // try animated image
1102 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1103 if length(imgfmt) = 0 then
1104 begin
1105 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1106 exit;
1107 end;
1109 GlobalMetadata.ClearMetaItems();
1110 GlobalMetadata.ClearMetaItemsForSaving();
1111 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1112 begin
1113 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
1114 BadTextNameHash.put(RecName, -1);
1115 exit;
1116 end;
1117 if length(ia) = 0 then
1118 begin
1119 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
1120 BadTextNameHash.put(RecName, -1);
1121 exit;
1122 end;
1124 WAD.Free();
1125 WAD := nil;
1127 _width := ia[0].width;
1128 _height := ia[0].height;
1129 _framecount := length(ia);
1130 _speed := 1;
1131 _backanimation := false;
1132 frdelay := -1;
1133 frloop := -666;
1134 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1135 begin
1136 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1137 try
1138 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1139 frdelay := f;
1140 if f < 0 then f := 0;
1141 // rounding ;-)
1142 c := f mod 28;
1143 if c < 13 then c := 0 else c := 1;
1144 f := (f div 28)+c;
1145 if f < 1 then f := 1 else if f > 255 then f := 255;
1146 _speed := f;
1147 except
1148 end;
1149 end;
1150 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1151 begin
1152 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1153 try
1154 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1155 frloop := f;
1156 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1157 except
1158 end;
1159 end;
1160 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1161 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1162 f := ord(_backanimation);
1163 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);
1165 SetLength(Textures, Length(Textures)+1);
1166 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1167 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1168 begin
1169 Textures[High(Textures)].TextureName := RecName;
1170 Textures[High(Textures)].Width := _width;
1171 Textures[High(Textures)].Height := _height;
1172 Textures[High(Textures)].Anim := True;
1173 Textures[High(Textures)].FramesCount := length(ia);
1174 Textures[High(Textures)].Speed := _speed;
1175 result := High(Textures);
1176 TextNameHash.put(RecName, result);
1177 //writeln(' CREATED!');
1178 end
1179 else
1180 begin
1181 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1182 if log and (not BadTextNameHash.get(RecName, f)) then
1183 begin
1184 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
1185 end;
1186 BadTextNameHash.put(RecName, -1);
1187 end;
1188 end;
1189 finally
1190 for f := 0 to High(ia) do FreeImage(ia[f]);
1191 WAD.Free();
1192 cfg.Free();
1193 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1194 if (TextData <> nil) then FreeMem(TextData);
1195 if (TextureData <> nil) then FreeMem(TextureData);
1196 end;
1197 end;
1199 procedure CreateItem(Item: TDynRecord);
1200 begin
1201 if g_Game_IsClient then Exit;
1203 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1204 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1205 Exit;
1207 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1208 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1209 end;
1211 procedure CreateArea(Area: TDynRecord);
1212 var
1213 a: Integer;
1214 id: DWORD = 0;
1215 begin
1216 case Area.AreaType of
1217 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1218 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1219 begin
1220 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1221 with RespawnPoints[High(RespawnPoints)] do
1222 begin
1223 X := Area.X;
1224 Y := Area.Y;
1225 Direction := TDirection(Area.Direction);
1227 case Area.AreaType of
1228 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1229 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1230 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1231 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1232 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1233 end;
1234 end;
1235 end;
1237 AREA_REDFLAG, AREA_BLUEFLAG:
1238 begin
1239 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1241 if FlagPoints[a] <> nil then Exit;
1243 New(FlagPoints[a]);
1245 with FlagPoints[a]^ do
1246 begin
1247 X := Area.X-FLAGRECT.X;
1248 Y := Area.Y-FLAGRECT.Y;
1249 Direction := TDirection(Area.Direction);
1250 end;
1252 with gFlags[a] do
1253 begin
1254 case a of
1255 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1256 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1257 end;
1259 Animation := TAnimation.Create(id, True, 8);
1260 Obj.Rect := FLAGRECT;
1262 g_Map_ResetFlag(a);
1263 end;
1264 end;
1266 AREA_DOMFLAG:
1267 begin
1268 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1269 with DOMFlagPoints[High(DOMFlagPoints)] do
1270 begin
1271 X := Area.X;
1272 Y := Area.Y;
1273 Direction := TDirection(Area.Direction);
1274 end;
1276 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1277 end;
1278 end;
1279 end;
1281 procedure CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer; fTexturePanel1Type, fTexturePanel2Type: Word);
1282 var
1283 _trigger: TTrigger;
1284 begin
1285 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1287 with _trigger do
1288 begin
1289 mapId := Trigger.id;
1290 mapIndex := amapIdx;
1291 X := Trigger.X;
1292 Y := Trigger.Y;
1293 Width := Trigger.Width;
1294 Height := Trigger.Height;
1295 Enabled := Trigger.Enabled;
1296 //TexturePanel := Trigger.TexturePanel;
1297 TexturePanelGUID := atpanid;
1298 TexturePanelType := fTexturePanel1Type;
1299 ShotPanelType := fTexturePanel2Type;
1300 TriggerType := Trigger.TriggerType;
1301 ActivateType := Trigger.ActivateType;
1302 Keys := Trigger.Keys;
1303 trigPanelGUID := atrigpanid;
1304 //trigShotPanelId := ashotpanid;
1305 //Data.Default := Trigger.DATA;
1306 if (Trigger.trigRec = nil) then
1307 begin
1308 trigData := nil;
1309 if (TriggerType <> TRIGGER_SECRET) then
1310 begin
1311 e_LogWritefln('trigger of type %s has no triggerdata; wtf?!', [TriggerType], MSG_WARNING);
1312 end;
1313 end
1314 else
1315 begin
1316 trigData := Trigger.trigRec.clone();
1317 end;
1318 end;
1320 g_Triggers_Create(_trigger);
1321 end;
1323 procedure CreateMonster(monster: TDynRecord);
1324 var
1325 a: Integer;
1326 mon: TMonster;
1327 begin
1328 if g_Game_IsClient then Exit;
1330 if (gGameSettings.GameType = GT_SINGLE)
1331 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1332 begin
1333 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1335 if gTriggers <> nil then
1336 begin
1337 for a := 0 to High(gTriggers) do
1338 begin
1339 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1340 begin
1341 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1342 if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1343 end;
1344 end;
1345 end;
1347 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1348 end;
1349 end;
1351 procedure g_Map_ReAdd_DieTriggers();
1353 function monsDieTrig (mon: TMonster): Boolean;
1354 var
1355 a: Integer;
1356 //tw: TStrTextWriter;
1357 begin
1358 result := false; // don't stop
1359 mon.ClearTriggers();
1360 for a := 0 to High(gTriggers) do
1361 begin
1362 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1363 begin
1364 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1366 tw := TStrTextWriter.Create();
1367 try
1368 gTriggers[a].trigData.writeTo(tw);
1369 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1370 finally
1371 tw.Free();
1372 end;
1374 if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1375 end;
1376 end;
1377 end;
1379 begin
1380 if g_Game_IsClient then Exit;
1382 g_Mons_ForEach(monsDieTrig);
1383 end;
1385 function extractWadName(resourceName: string): string;
1386 var
1387 posN: Integer;
1388 begin
1389 posN := Pos(':', resourceName);
1390 if posN > 0 then
1391 Result:= Copy(resourceName, 0, posN-1)
1392 else
1393 Result := '';
1394 end;
1396 procedure addResToExternalResList(res: string);
1397 begin
1398 res := extractWadName(res);
1399 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1400 gExternalResources.Add(res);
1401 end;
1403 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1404 //var
1405 //textures: TTexturesRec1Array;
1406 //textures: TDynField;
1407 //trec: TDynRecord;
1408 //mapHeader: TMapHeaderRec_1;
1409 //i: integer;
1410 //resFile: String = '';
1411 begin
1412 if gExternalResources = nil then
1413 gExternalResources := TStringList.Create;
1415 gExternalResources.Clear;
1417 (*
1419 textures := GetTextures(map);
1420 for i := 0 to High(textures) do
1421 begin
1422 addResToExternalResList(resFile);
1423 end;
1426 textures := map['texture'];
1427 if (textures <> nil) then
1428 begin
1429 for trec in textures do
1430 begin
1431 addResToExternalResList(resFile);
1432 end;
1433 end;
1435 textures := nil;
1436 *)
1438 //mapHeader := GetMapHeader(map);
1440 addResToExternalResList(map.MusicName);
1441 addResToExternalResList(map.SkyName);
1442 end;
1445 procedure mapCreateGrid ();
1446 var
1447 mapX0: Integer = $3fffffff;
1448 mapY0: Integer = $3fffffff;
1449 mapX1: Integer = -$3fffffff;
1450 mapY1: Integer = -$3fffffff;
1452 procedure calcBoundingBox (constref panels: TPanelArray);
1453 var
1454 idx: Integer;
1455 pan: TPanel;
1456 begin
1457 for idx := 0 to High(panels) do
1458 begin
1459 pan := panels[idx];
1460 if not pan.visvalid then continue;
1461 if (pan.Width < 1) or (pan.Height < 1) then continue;
1462 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1463 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1464 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1465 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1466 end;
1467 end;
1469 procedure addPanelsToGrid (constref panels: TPanelArray);
1470 var
1471 idx: Integer;
1472 pan: TPanel;
1473 newtag: Integer;
1474 begin
1475 //tag := panelTypeToTag(tag);
1476 for idx := 0 to High(panels) do
1477 begin
1478 pan := panels[idx];
1479 if not pan.visvalid then continue;
1480 if (pan.proxyId <> -1) then
1481 begin
1482 {$IF DEFINED(D2F_DEBUG)}
1483 e_WriteLog(Format('DUPLICATE wall #%d(%d) enabled (%d); type:%08x', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.PanelType]), MSG_NOTIFY);
1484 {$ENDIF}
1485 continue;
1486 end;
1487 case pan.PanelType of
1488 PANEL_WALL: newtag := GridTagWall;
1489 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1490 PANEL_BACK: newtag := GridTagBack;
1491 PANEL_FORE: newtag := GridTagFore;
1492 PANEL_WATER: newtag := GridTagWater;
1493 PANEL_ACID1: newtag := GridTagAcid1;
1494 PANEL_ACID2: newtag := GridTagAcid2;
1495 PANEL_STEP: newtag := GridTagStep;
1496 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1497 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1498 else continue; // oops
1499 end;
1500 pan.tag := newtag;
1502 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1503 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1504 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1505 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1507 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1508 begin
1509 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1510 end;
1512 {$ENDIF}
1513 end;
1514 end;
1516 begin
1517 mapGrid.Free();
1518 mapGrid := nil;
1520 calcBoundingBox(gWalls);
1521 calcBoundingBox(gRenderBackgrounds);
1522 calcBoundingBox(gRenderForegrounds);
1523 calcBoundingBox(gWater);
1524 calcBoundingBox(gAcid1);
1525 calcBoundingBox(gAcid2);
1526 calcBoundingBox(gSteps);
1527 calcBoundingBox(gLifts);
1528 calcBoundingBox(gBlockMon);
1530 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1532 if (mapX0 > 0) then mapX0 := 0;
1533 if (mapY0 > 0) then mapY0 := 0;
1535 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1536 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1538 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1539 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1541 addPanelsToGrid(gWalls);
1542 addPanelsToGrid(gRenderBackgrounds);
1543 addPanelsToGrid(gRenderForegrounds);
1544 addPanelsToGrid(gWater);
1545 addPanelsToGrid(gAcid1);
1546 addPanelsToGrid(gAcid2);
1547 addPanelsToGrid(gSteps);
1548 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1549 addPanelsToGrid(gBlockMon);
1551 mapGrid.dumpStats();
1553 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1554 end;
1557 function g_Map_Load(Res: String): Boolean;
1558 const
1559 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1560 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1561 type
1562 PTRec = ^TTRec;
1563 TTRec = record
1564 //TexturePanel: Integer;
1565 texPanIdx: Integer;
1566 LiftPanelIdx: Integer;
1567 DoorPanelIdx: Integer;
1568 ShotPanelIdx: Integer;
1569 MPlatPanelIdx: Integer;
1570 trigrec: TDynRecord;
1571 texPan: TDynRecord;
1572 liftPan: TDynRecord;
1573 doorPan: TDynRecord;
1574 shotPan: TDynRecord;
1575 mplatPan: TDynRecord;
1576 end;
1577 var
1578 WAD: TWADFile;
1579 mapReader: TDynRecord = nil;
1580 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1581 panels: TDynField = nil; //TPanelsRec1Array;
1582 items: TDynField = nil; //TItemsRec1Array;
1583 monsters: TDynField = nil; //TMonsterRec1Array;
1584 areas: TDynField = nil; //TAreasRec1Array;
1585 triggers: TDynField = nil; //TTriggersRec1Array;
1586 b, c, k: Integer;
1587 PanelID: DWORD;
1588 AddTextures: TAddTextureArray;
1589 TriggersTable: array of TTRec;
1590 FileName, mapResName, s, TexName: String;
1591 Data: Pointer;
1592 Len: Integer;
1593 ok, isAnim, trigRef: Boolean;
1594 CurTex, ntn: Integer;
1595 rec, texrec: TDynRecord;
1596 pttit: PTRec;
1597 pannum, trignum, cnt, tgpid: Integer;
1598 stt: UInt64;
1599 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1600 //moveActive: Boolean;
1601 begin
1602 mapGrid.Free();
1603 mapGrid := nil;
1605 gCurrentMap.Free();
1606 gCurrentMap := nil;
1608 panByGUID := nil;
1610 Result := False;
1611 gMapInfo.Map := Res;
1612 TriggersTable := nil;
1613 mapReader := nil;
1615 sfsGCDisable(); // temporary disable removing of temporary volumes
1616 try
1617 // Çàãðóçêà WAD:
1618 FileName := g_ExtractWadName(Res);
1619 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1620 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1622 WAD := TWADFile.Create();
1623 if not WAD.ReadFile(FileName) then
1624 begin
1625 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1626 WAD.Free();
1627 Exit;
1628 end;
1630 //k8: why loader ignores path here?
1631 mapResName := g_ExtractFileName(Res);
1632 if not WAD.GetMapResource(mapResName, Data, Len) then
1633 begin
1634 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1635 WAD.Free();
1636 Exit;
1637 end;
1639 WAD.Free();
1641 if (Len < 4) then
1642 begin
1643 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1644 FreeMem(Data);
1645 exit;
1646 end;
1648 // Çàãðóçêà êàðòû:
1649 e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
1650 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1652 stt := curTimeMicro();
1654 try
1655 mapReader := g_Map_ParseMap(Data, Len);
1656 except
1657 mapReader.Free();
1658 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1659 FreeMem(Data);
1660 Exit;
1661 end;
1663 FreeMem(Data);
1665 generateExternalResourcesList(mapReader);
1666 mapTextureList := mapReader['texture'];
1667 // get all other lists here too
1668 panels := mapReader['panel'];
1669 triggers := mapReader['trigger'];
1670 items := mapReader['item'];
1671 areas := mapReader['area'];
1672 monsters := mapReader['monster'];
1674 // Çàãðóçêà îïèñàíèÿ êàðòû:
1675 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1676 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1678 with gMapInfo do
1679 begin
1680 Name := mapReader.MapName;
1681 Description := mapReader.MapDesc;
1682 Author := mapReader.MapAuthor;
1683 MusicName := mapReader.MusicName;
1684 SkyName := mapReader.SkyName;
1685 Height := mapReader.Height;
1686 Width := mapReader.Width;
1687 end;
1689 // Çàãðóçêà òåêñòóð:
1690 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1691 // Äîáàâëåíèå òåêñòóð â Textures[]:
1692 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1693 begin
1694 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1695 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1697 cnt := -1;
1698 for rec in mapTextureList do
1699 begin
1700 Inc(cnt);
1701 s := rec.Resource;
1702 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1703 e_WriteLog(Format(' Loading texture #%d: %s', [cnt, s]), MSG_NOTIFY);
1704 {$ENDIF}
1705 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1706 if rec.Anim then
1707 begin
1708 // Àíèìèðîâàííàÿ òåêñòóðà
1709 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1710 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1711 end
1712 else
1713 begin
1714 // Îáû÷íàÿ òåêñòóðà
1715 ntn := CreateTexture(rec.Resource, FileName, True);
1716 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1717 end;
1718 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1720 rec.tagInt := ntn; // remember texture number
1721 g_Game_StepLoading();
1722 end;
1724 // set panel tagInt to texture index
1725 if (panels <> nil) then
1726 begin
1727 for rec in panels do
1728 begin
1729 texrec := rec.TextureRec;
1730 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1731 end;
1732 end;
1733 end;
1735 // Çàãðóçêà òðèããåðîâ
1736 gTriggerClientID := 0;
1737 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1738 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1740 // Çàãðóçêà ïàíåëåé
1741 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1742 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1744 // check texture numbers for panels
1745 if (panels <> nil) and (panels.count > 0) then
1746 begin
1747 for rec in panels do
1748 begin
1749 if (rec.tagInt < 0) then
1750 begin
1751 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1752 result := false;
1753 exit;
1754 end;
1755 end;
1756 end;
1758 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1759 if (triggers <> nil) and (triggers.count > 0) then
1760 begin
1761 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1762 //SetLength(TriggersTable, triggers.count);
1763 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1765 for rec in triggers do
1766 begin
1767 SetLength(TriggersTable, Length(TriggersTable)+1);
1768 pttit := @TriggersTable[High(TriggersTable)];
1769 pttit.trigrec := rec;
1770 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1771 pttit.texPan := mapReader.panel[rec.TexturePanel];
1772 pttit.liftPan := nil;
1773 pttit.doorPan := nil;
1774 pttit.shotPan := nil;
1775 pttit.mplatPan := nil;
1776 pttit.texPanIdx := -1;
1777 pttit.LiftPanelIdx := -1;
1778 pttit.DoorPanelIdx := -1;
1779 pttit.ShotPanelIdx := -1;
1780 pttit.MPlatPanelIdx := -1;
1781 // Ëèôòû
1782 if rec.TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1783 begin
1784 pttit.liftPan := mapReader.panel[rec.trigRec.tgPanelID];
1785 end;
1786 // Äâåðè
1787 if rec.TriggerType in [TRIGGER_OPENDOOR, TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5, TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1788 begin
1789 pttit.doorPan := mapReader.panel[rec.trigRec.tgPanelID];
1790 end;
1791 // Òóðåëü
1792 if (rec.TriggerType = TRIGGER_SHOT) then
1793 begin
1794 pttit.shotPan := mapReader.panel[rec.trigRec.tgShotPanelID];
1795 end;
1796 //
1797 if rec.TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1798 begin
1799 pttit.mplatPan := mapReader.panel[rec.trigRec.tgPanelID];
1800 end;
1802 if (pttit.texPan <> nil) then pttit.texPan.userPanelTrigRef := true;
1803 if (pttit.liftPan <> nil) then pttit.liftPan.userPanelTrigRef := true;
1804 if (pttit.doorPan <> nil) then pttit.doorPan.userPanelTrigRef := true;
1805 if (pttit.shotPan <> nil) then pttit.shotPan.userPanelTrigRef := true;
1806 if (pttit.mplatPan <> nil) then pttit.mplatPan.userPanelTrigRef := true;
1808 g_Game_StepLoading();
1809 end;
1810 end;
1812 // Ñîçäàåì ïàíåëè
1813 if (panels <> nil) and (panels.count > 0) then
1814 begin
1815 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1816 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1818 pannum := -1;
1819 for rec in panels do
1820 begin
1821 Inc(pannum);
1822 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1823 texrec := nil;
1824 SetLength(AddTextures, 0);
1825 trigRef := False;
1826 CurTex := -1;
1827 ok := false;
1829 if (mapTextureList <> nil) then
1830 begin
1831 texrec := rec.TextureRec;
1832 ok := (texrec <> nil);
1833 end;
1835 if ok then
1836 begin
1837 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1838 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1839 ok := false;
1840 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1841 begin
1843 for b := 0 to High(TriggersTable) do
1844 begin
1845 if (TriggersTable[b].texPan = rec) or (TriggersTable[b].shotPan = rec) then
1846 begin
1847 trigRef := True;
1848 ok := True;
1849 break;
1850 end;
1851 end;
1853 if rec.userPanelTrigRef then
1854 begin
1855 // e_LogWritefln('trigref for panel %s', [pannum]);
1856 trigRef := True;
1857 ok := True;
1858 end;
1859 end;
1860 end;
1862 if ok then
1863 begin
1864 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1865 s := texrec.Resource;
1867 // Ñïåö-òåêñòóðû çàïðåùåíû
1868 if g_Map_IsSpecialTexture(s) then
1869 begin
1870 ok := false
1871 end
1872 else
1873 begin
1874 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1875 ok := g_Texture_NumNameFindStart(s);
1876 end;
1878 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1879 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1880 if ok then
1881 begin
1882 k := NNF_NAME_BEFORE;
1883 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1884 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1885 begin
1886 k := g_Texture_NumNameFindNext(TexName);
1888 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1889 begin
1890 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1891 if texrec.Anim then
1892 begin
1893 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1894 isAnim := True;
1895 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1896 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1897 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1898 if not ok then
1899 begin
1900 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1901 isAnim := False;
1902 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1903 ok := CreateTexture(TexName, FileName, False) >= 0;
1904 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1905 end;
1906 end
1907 else
1908 begin
1909 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1910 isAnim := False;
1911 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1912 ok := CreateTexture(TexName, FileName, False) >= 0;
1913 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1914 if not ok then
1915 begin
1916 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1917 isAnim := True;
1918 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1919 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1920 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1921 end;
1922 end;
1924 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1925 if ok then
1926 begin
1928 for c := 0 to High(Textures) do
1929 begin
1930 if (Textures[c].TextureName = TexName) then
1931 begin
1932 SetLength(AddTextures, Length(AddTextures)+1);
1933 AddTextures[High(AddTextures)].Texture := c;
1934 AddTextures[High(AddTextures)].Anim := isAnim;
1935 break;
1936 end;
1937 end;
1939 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1940 begin
1941 SetLength(AddTextures, Length(AddTextures)+1);
1942 AddTextures[High(AddTextures)].Texture := c;
1943 AddTextures[High(AddTextures)].Anim := isAnim;
1944 end;
1945 end;
1946 end
1947 else
1948 begin
1949 if k = NNF_NAME_EQUALS then
1950 begin
1951 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1952 SetLength(AddTextures, Length(AddTextures)+1);
1953 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1954 AddTextures[High(AddTextures)].Anim := texrec.Anim;
1955 CurTex := High(AddTextures);
1956 ok := true;
1957 end
1958 else // NNF_NO_NAME
1959 begin
1960 ok := false;
1961 end;
1962 end;
1963 end; // while ok...
1965 ok := true;
1966 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1967 end; // if ok - ññûëàþòñÿ òðèããåðû
1969 if not ok then
1970 begin
1971 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1972 SetLength(AddTextures, 1);
1973 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1974 AddTextures[0].Anim := false;
1975 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
1976 CurTex := 0;
1977 end;
1979 //e_WriteLog(Format('panel #%d: TextureNum=%d; ht=%d; ht1=%d; atl=%d', [a, panels[a].TextureNum, High(mapTextureList), High(Textures), High(AddTextures)]), MSG_NOTIFY);
1981 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1983 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
1984 PanelID := CreatePanel(rec, AddTextures, CurTex, trigRef);
1985 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
1986 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1988 // setup lifts
1989 moveSpeed := rec.moveSpeed;
1990 //moveStart := rec.moveStart;
1991 //moveEnd := rec.moveEnd;
1992 //moveActive := rec['move_active'].varvalue;
1993 if not moveSpeed.isZero then
1994 begin
1995 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1996 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1997 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1998 end;
2000 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2002 g_Game_StepLoading();
2003 end;
2004 end;
2006 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2007 for b := 0 to High(TriggersTable) do
2008 begin
2009 if (TriggersTable[b].texPan <> nil) then TriggersTable[b].texPanIdx := TriggersTable[b].texPan.userPanelId;
2010 if (TriggersTable[b].liftPan <> nil) then TriggersTable[b].LiftPanelIdx := TriggersTable[b].liftPan.userPanelId;
2011 if (TriggersTable[b].doorPan <> nil) then TriggersTable[b].DoorPanelIdx := TriggersTable[b].doorPan.userPanelId;
2012 if (TriggersTable[b].shotPan <> nil) then TriggersTable[b].ShotPanelIdx := TriggersTable[b].shotPan.userPanelId;
2013 if (TriggersTable[b].mplatPan <> nil) then TriggersTable[b].MPlatPanelIdx := TriggersTable[b].mplatPan.userPanelId;
2014 end;
2016 // create map grid, init other grids (for monsters, for example)
2017 e_WriteLog('Creating map grid', MSG_NOTIFY);
2018 mapCreateGrid();
2020 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2021 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2022 begin
2023 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2024 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2025 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2026 trignum := -1;
2027 for rec in triggers do
2028 begin
2029 Inc(trignum);
2030 if (TriggersTable[trignum].texPan <> nil) then b := TriggersTable[trignum].texPan.PanelType else b := 0;
2031 if (TriggersTable[trignum].shotPan <> nil) then c := TriggersTable[trignum].shotPan.PanelType else c := 0;
2032 // we can have only one of those
2033 if (TriggersTable[trignum].LiftPanelIdx <> -1) then tgpid := TriggersTable[trignum].LiftPanelIdx
2034 else if (TriggersTable[trignum].DoorPanelIdx <> -1) then tgpid := TriggersTable[trignum].DoorPanelIdx
2035 else if (TriggersTable[trignum].ShotPanelIdx <> -1) then tgpid := TriggersTable[trignum].ShotPanelIdx
2036 else if (TriggersTable[trignum].MPlatPanelIdx <> -1) then tgpid := TriggersTable[trignum].MPlatPanelIdx
2037 else tgpid := -1;
2038 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2039 CreateTrigger(trignum, rec, TriggersTable[trignum].texPanIdx, tgpid, Word(b), Word(c));
2040 end;
2041 end;
2043 // Çàãðóçêà ïðåäìåòîâ
2044 e_WriteLog(' Loading items...', MSG_NOTIFY);
2045 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2047 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2048 if (items <> nil) and not gLoadGameMode then
2049 begin
2050 e_WriteLog(' Spawning items...', MSG_NOTIFY);
2051 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2052 for rec in items do CreateItem(rec);
2053 end;
2055 // Çàãðóçêà îáëàñòåé
2056 e_WriteLog(' Loading areas...', MSG_NOTIFY);
2057 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2059 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2060 if areas <> nil then
2061 begin
2062 e_WriteLog(' Creating areas...', MSG_NOTIFY);
2063 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2064 for rec in areas do CreateArea(rec);
2065 end;
2067 // Çàãðóçêà ìîíñòðîâ
2068 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
2069 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2071 gTotalMonsters := 0;
2073 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2074 if (monsters <> nil) and not gLoadGameMode then
2075 begin
2076 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
2077 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2078 for rec in monsters do CreateMonster(rec);
2079 end;
2081 gCurrentMap := mapReader; // this will be our current map now
2082 gCurrentMapFileName := Res;
2083 mapReader := nil;
2085 // Çàãðóçêà íåáà
2086 if gMapInfo.SkyName <> '' then
2087 begin
2088 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
2089 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2090 FileName := g_ExtractWadName(gMapInfo.SkyName);
2092 if FileName <> '' then
2093 FileName := GameDir+'/wads/'+FileName
2094 else
2095 begin
2096 FileName := g_ExtractWadName(Res);
2097 end;
2099 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2100 if g_Texture_CreateWAD(BackID, s) then
2101 begin
2102 g_Game_SetupScreenSize();
2103 end
2104 else
2105 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2106 end;
2108 // Çàãðóçêà ìóçûêè
2109 ok := False;
2110 if gMapInfo.MusicName <> '' then
2111 begin
2112 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
2113 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2114 FileName := g_ExtractWadName(gMapInfo.MusicName);
2116 if FileName <> '' then
2117 FileName := GameDir+'/wads/'+FileName
2118 else
2119 begin
2120 FileName := g_ExtractWadName(Res);
2121 end;
2123 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2124 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2125 ok := True
2126 else
2127 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2128 end;
2130 // Îñòàëüíûå óñòàíâêè
2131 CreateDoorMap();
2132 CreateLiftMap();
2134 g_Items_Init();
2135 g_Weapon_Init();
2136 g_Monsters_Init();
2138 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2139 if not gLoadGameMode then g_GFX_Init();
2141 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2142 mapTextureList := nil;
2143 panels := nil;
2144 items := nil;
2145 areas := nil;
2146 triggers := nil;
2147 TriggersTable := nil;
2148 AddTextures := nil;
2150 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2151 if ok and (not gLoadGameMode) then
2152 begin
2153 gMusic.SetByName(gMapInfo.MusicName);
2154 gMusic.Play();
2155 end
2156 else
2157 begin
2158 gMusic.SetByName('');
2159 end;
2161 stt := curTimeMicro()-stt;
2162 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2163 finally
2164 sfsGCEnable(); // enable releasing unused volumes
2165 mapReader.Free();
2166 e_ClearInputBuffer(); // why not?
2167 end;
2169 e_WriteLog('Done loading map.', MSG_NOTIFY);
2170 Result := True;
2171 end;
2174 function g_Map_GetMapInfo(Res: String): TMapInfo;
2175 var
2176 WAD: TWADFile;
2177 mapReader: TDynRecord;
2178 //Header: TMapHeaderRec_1;
2179 FileName: String;
2180 Data: Pointer;
2181 Len: Integer;
2182 begin
2183 FillChar(Result, SizeOf(Result), 0);
2184 FileName := g_ExtractWadName(Res);
2186 WAD := TWADFile.Create();
2187 if not WAD.ReadFile(FileName) then
2188 begin
2189 WAD.Free();
2190 Exit;
2191 end;
2193 //k8: it ignores path again
2194 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2195 begin
2196 WAD.Free();
2197 Exit;
2198 end;
2200 WAD.Free();
2202 try
2203 mapReader := g_Map_ParseMap(Data, Len);
2204 except
2205 mapReader := nil;
2206 end;
2208 FreeMem(Data);
2210 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2211 begin
2212 Result.Name := mapReader.MapName;
2213 Result.Description := mapReader.MapDesc;
2214 Result.Map := Res;
2215 Result.Author := mapReader.MapAuthor;
2216 Result.Height := mapReader.Height;
2217 Result.Width := mapReader.Width;
2218 end
2219 else
2220 begin
2221 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2222 //ZeroMemory(@Header, SizeOf(Header));
2223 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2224 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2225 Result.Map := Res;
2226 Result.Author := '';
2227 Result.Height := 0;
2228 Result.Width := 0;
2229 end;
2231 mapReader.Free();
2232 end;
2234 function g_Map_GetMapsList(WADName: string): SArray;
2235 var
2236 WAD: TWADFile;
2237 a: Integer;
2238 ResList: SArray;
2239 begin
2240 Result := nil;
2241 WAD := TWADFile.Create();
2242 if not WAD.ReadFile(WADName) then
2243 begin
2244 WAD.Free();
2245 Exit;
2246 end;
2247 ResList := WAD.GetMapResources();
2248 if ResList <> nil then
2249 begin
2250 for a := 0 to High(ResList) do
2251 begin
2252 SetLength(Result, Length(Result)+1);
2253 Result[High(Result)] := ResList[a];
2254 end;
2255 end;
2256 WAD.Free();
2257 end;
2259 function g_Map_Exist(Res: string): Boolean;
2260 var
2261 WAD: TWADFile;
2262 FileName, mnn: string;
2263 ResList: SArray;
2264 a: Integer;
2265 begin
2266 Result := False;
2268 FileName := addWadExtension(g_ExtractWadName(Res));
2270 WAD := TWADFile.Create;
2271 if not WAD.ReadFile(FileName) then
2272 begin
2273 WAD.Free();
2274 Exit;
2275 end;
2277 ResList := WAD.GetMapResources();
2278 WAD.Free();
2280 mnn := g_ExtractFileName(Res);
2281 if ResList <> nil then
2282 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2283 begin
2284 Result := True;
2285 Exit;
2286 end;
2287 end;
2289 procedure g_Map_Free(freeTextures: Boolean=true);
2290 var
2291 a: Integer;
2293 procedure FreePanelArray(var panels: TPanelArray);
2294 var
2295 i: Integer;
2297 begin
2298 if panels <> nil then
2299 begin
2300 for i := 0 to High(panels) do
2301 panels[i].Free();
2302 panels := nil;
2303 end;
2304 end;
2306 begin
2307 g_GFX_Free();
2308 g_Weapon_Free();
2309 g_Items_Free();
2310 g_Triggers_Free();
2311 g_Monsters_Free();
2313 RespawnPoints := nil;
2314 if FlagPoints[FLAG_RED] <> nil then
2315 begin
2316 Dispose(FlagPoints[FLAG_RED]);
2317 FlagPoints[FLAG_RED] := nil;
2318 end;
2319 if FlagPoints[FLAG_BLUE] <> nil then
2320 begin
2321 Dispose(FlagPoints[FLAG_BLUE]);
2322 FlagPoints[FLAG_BLUE] := nil;
2323 end;
2324 //DOMFlagPoints := nil;
2326 //gDOMFlags := nil;
2328 if (Length(gCurrentMapFileName) <> 0) then
2329 begin
2330 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2331 end
2332 else
2333 begin
2334 e_LogWritefln('g_Map_Free: no previous map.', []);
2335 end;
2336 if freeTextures then
2337 begin
2338 e_LogWritefln('g_Map_Free: clearing textures...', []);
2339 if (Textures <> nil) then
2340 begin
2341 for a := 0 to High(Textures) do
2342 begin
2343 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2344 begin
2345 if Textures[a].Anim then
2346 begin
2347 g_Frames_DeleteByID(Textures[a].FramesID)
2348 end
2349 else
2350 begin
2351 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2352 begin
2353 e_DeleteTexture(Textures[a].TextureID);
2354 end;
2355 end;
2356 end;
2357 end;
2358 Textures := nil;
2359 end;
2360 TextNameHash.Free();
2361 TextNameHash := nil;
2362 BadTextNameHash.Free();
2363 BadTextNameHash := nil;
2364 end;
2366 panByGUID := nil;
2368 FreePanelArray(gWalls);
2369 FreePanelArray(gRenderBackgrounds);
2370 FreePanelArray(gRenderForegrounds);
2371 FreePanelArray(gWater);
2372 FreePanelArray(gAcid1);
2373 FreePanelArray(gAcid2);
2374 FreePanelArray(gSteps);
2375 FreePanelArray(gLifts);
2376 FreePanelArray(gBlockMon);
2377 gMovingWallIds := nil;
2379 if BackID <> DWORD(-1) then
2380 begin
2381 gBackSize.X := 0;
2382 gBackSize.Y := 0;
2383 e_DeleteTexture(BackID);
2384 BackID := DWORD(-1);
2385 end;
2387 g_Game_StopAllSounds(False);
2388 gMusic.FreeSound();
2389 g_Sound_Delete(gMapInfo.MusicName);
2391 gMapInfo.Name := '';
2392 gMapInfo.Description := '';
2393 gMapInfo.MusicName := '';
2394 gMapInfo.Height := 0;
2395 gMapInfo.Width := 0;
2397 gDoorMap := nil;
2398 gLiftMap := nil;
2399 end;
2401 procedure g_Map_Update();
2402 var
2403 a, d, j: Integer;
2404 m: Word;
2405 s: String;
2407 procedure UpdatePanelArray(var panels: TPanelArray);
2408 var
2409 i: Integer;
2411 begin
2412 for i := 0 to High(panels) do panels[i].Update();
2413 end;
2415 begin
2416 UpdatePanelArray(gWalls);
2417 UpdatePanelArray(gRenderBackgrounds);
2418 UpdatePanelArray(gRenderForegrounds);
2419 UpdatePanelArray(gWater);
2420 UpdatePanelArray(gAcid1);
2421 UpdatePanelArray(gAcid2);
2422 UpdatePanelArray(gSteps);
2424 if gGameSettings.GameMode = GM_CTF then
2425 begin
2426 for a := FLAG_RED to FLAG_BLUE do
2427 begin
2428 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2429 begin
2430 with gFlags[a] do
2431 begin
2432 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2434 m := g_Obj_Move(@Obj, True, True);
2436 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2438 // Ñîïðîòèâëåíèå âîçäóõà
2439 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2441 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2442 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2443 begin
2444 g_Map_ResetFlag(a);
2445 gFlags[a].CaptureTime := 0;
2446 if a = FLAG_RED then
2447 s := _lc[I_PLAYER_FLAG_RED]
2448 else
2449 s := _lc[I_PLAYER_FLAG_BLUE];
2450 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2452 if g_Game_IsNet then
2453 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2454 Continue;
2455 end;
2457 if Count > 0 then Count -= 1;
2459 // Èãðîê áåðåò ôëàã
2460 if gPlayers <> nil then
2461 begin
2462 j := Random(Length(gPlayers)) - 1;
2463 for d := 0 to High(gPlayers) do
2464 begin
2465 Inc(j);
2466 if j > High(gPlayers) then j := 0;
2467 if gPlayers[j] <> nil then
2468 begin
2469 if gPlayers[j].Live and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2470 begin
2471 if gPlayers[j].GetFlag(a) then Break;
2472 end;
2473 end;
2474 end;
2475 end;
2476 end;
2477 end;
2478 end;
2479 end;
2480 end;
2483 // old algo
2484 procedure g_Map_DrawPanels (PanelType: Word);
2486 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2487 var
2488 idx: Integer;
2489 begin
2490 if (panels <> nil) then
2491 begin
2492 // alas, no visible set
2493 for idx := 0 to High(panels) do
2494 begin
2495 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2496 end;
2497 end;
2498 end;
2500 begin
2501 case PanelType of
2502 PANEL_WALL: DrawPanels(gWalls);
2503 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2504 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2505 PANEL_FORE: DrawPanels(gRenderForegrounds);
2506 PANEL_WATER: DrawPanels(gWater);
2507 PANEL_ACID1: DrawPanels(gAcid1);
2508 PANEL_ACID2: DrawPanels(gAcid2);
2509 PANEL_STEP: DrawPanels(gSteps);
2510 end;
2511 end;
2514 // new algo
2515 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2517 function checker (pan: TPanel; tag: Integer): Boolean;
2518 begin
2519 result := false; // don't stop, ever
2520 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2521 gDrawPanelList.insert(pan);
2522 end;
2524 begin
2525 dplClear();
2526 //tagmask := panelTypeToTag(PanelType);
2527 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2528 // list will be rendered in `g_game.DrawPlayer()`
2529 end;
2532 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2534 function checker (pan: TPanel; tag: Integer): Boolean;
2535 begin
2536 result := false; // don't stop, ever
2537 pan.DrawShadowVolume(lightX, lightY, radius);
2538 end;
2540 begin
2541 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2542 end;
2545 procedure g_Map_DrawBack(dx, dy: Integer);
2546 begin
2547 if gDrawBackGround and (BackID <> DWORD(-1)) then
2548 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2549 else
2550 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2551 end;
2553 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2554 PanelType: Word; b1x3: Boolean=false): Boolean;
2555 var
2556 a, h: Integer;
2557 begin
2558 Result := False;
2560 if WordBool(PanelType and PANEL_WALL) then
2561 if gWalls <> nil then
2562 begin
2563 h := High(gWalls);
2565 for a := 0 to h do
2566 if gWalls[a].Enabled and
2567 g_Collide(X, Y, Width, Height,
2568 gWalls[a].X, gWalls[a].Y,
2569 gWalls[a].Width, gWalls[a].Height) then
2570 begin
2571 Result := True;
2572 Exit;
2573 end;
2574 end;
2576 if WordBool(PanelType and PANEL_WATER) then
2577 if gWater <> nil then
2578 begin
2579 h := High(gWater);
2581 for a := 0 to h do
2582 if g_Collide(X, Y, Width, Height,
2583 gWater[a].X, gWater[a].Y,
2584 gWater[a].Width, gWater[a].Height) then
2585 begin
2586 Result := True;
2587 Exit;
2588 end;
2589 end;
2591 if WordBool(PanelType and PANEL_ACID1) then
2592 if gAcid1 <> nil then
2593 begin
2594 h := High(gAcid1);
2596 for a := 0 to h do
2597 if g_Collide(X, Y, Width, Height,
2598 gAcid1[a].X, gAcid1[a].Y,
2599 gAcid1[a].Width, gAcid1[a].Height) then
2600 begin
2601 Result := True;
2602 Exit;
2603 end;
2604 end;
2606 if WordBool(PanelType and PANEL_ACID2) then
2607 if gAcid2 <> nil then
2608 begin
2609 h := High(gAcid2);
2611 for a := 0 to h do
2612 if g_Collide(X, Y, Width, Height,
2613 gAcid2[a].X, gAcid2[a].Y,
2614 gAcid2[a].Width, gAcid2[a].Height) then
2615 begin
2616 Result := True;
2617 Exit;
2618 end;
2619 end;
2621 if WordBool(PanelType and PANEL_STEP) then
2622 if gSteps <> nil then
2623 begin
2624 h := High(gSteps);
2626 for a := 0 to h do
2627 if g_Collide(X, Y, Width, Height,
2628 gSteps[a].X, gSteps[a].Y,
2629 gSteps[a].Width, gSteps[a].Height) then
2630 begin
2631 Result := True;
2632 Exit;
2633 end;
2634 end;
2636 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2637 if gLifts <> nil then
2638 begin
2639 h := High(gLifts);
2641 for a := 0 to h do
2642 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2643 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2644 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2645 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2646 g_Collide(X, Y, Width, Height,
2647 gLifts[a].X, gLifts[a].Y,
2648 gLifts[a].Width, gLifts[a].Height) then
2649 begin
2650 Result := True;
2651 Exit;
2652 end;
2653 end;
2655 if WordBool(PanelType and PANEL_BLOCKMON) then
2656 if gBlockMon <> nil then
2657 begin
2658 h := High(gBlockMon);
2660 for a := 0 to h do
2661 if ( (not b1x3) or
2662 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2663 g_Collide(X, Y, Width, Height,
2664 gBlockMon[a].X, gBlockMon[a].Y,
2665 gBlockMon[a].Width, gBlockMon[a].Height) then
2666 begin
2667 Result := True;
2668 Exit;
2669 end;
2670 end;
2671 end;
2673 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2674 var
2675 texid: DWORD;
2677 function checkPanels (constref panels: TPanelArray): Boolean;
2678 var
2679 a: Integer;
2680 begin
2681 result := false;
2682 if panels = nil then exit;
2683 for a := 0 to High(panels) do
2684 begin
2685 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2686 begin
2687 result := true;
2688 texid := panels[a].GetTextureID();
2689 exit;
2690 end;
2691 end;
2692 end;
2694 begin
2695 texid := LongWord(TEXTURE_NONE);
2696 result := texid;
2697 if not checkPanels(gWater) then
2698 if not checkPanels(gAcid1) then
2699 if not checkPanels(gAcid2) then exit;
2700 result := texid;
2701 end;
2704 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2705 const
2706 SlowMask = GridTagLift or GridTagBlockMon;
2707 function checker (pan: TPanel; tag: Integer): Boolean;
2708 begin
2710 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2711 begin
2712 result := pan.Enabled;
2713 exit;
2714 end;
2717 if ((tag and GridTagLift) <> 0) then
2718 begin
2719 result :=
2720 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2721 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2722 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2723 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2724 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2725 exit;
2726 end;
2728 if ((tag and GridTagBlockMon) <> 0) then
2729 begin
2730 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2731 exit;
2732 end;
2734 // other shit
2735 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2736 result := true; // i found her!
2737 end;
2739 var
2740 tagmask: Integer = 0;
2741 begin
2742 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2743 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2744 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2745 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2746 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2747 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2748 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2750 if (tagmask = 0) then begin result := false; exit; end; // just in case
2752 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2753 if gdbg_map_use_accel_coldet then
2754 begin
2755 if (Width = 1) and (Height = 1) then
2756 begin
2757 if ((tagmask and SlowMask) <> 0) then
2758 begin
2759 // slow
2760 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2761 end
2762 else
2763 begin
2764 // fast
2765 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2766 end;
2767 end
2768 else
2769 begin
2770 if ((tagmask and SlowMask) <> 0) then
2771 begin
2772 // slow
2773 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2774 end
2775 else
2776 begin
2777 // fast
2778 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2779 end;
2780 end;
2781 end
2782 else
2783 begin
2784 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2785 end;
2786 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2787 end;
2790 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2791 var
2792 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2793 texid: DWORD;
2795 // slightly different from the old code, but meh...
2796 function checker (pan: TPanel; tag: Integer): Boolean;
2797 begin
2798 result := false; // don't stop, ever
2799 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2800 // check priorities
2801 case cctype of
2802 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2803 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2804 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2805 end;
2806 // collision?
2807 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2808 // yeah
2809 texid := pan.GetTextureID();
2810 // water? water has the highest priority, so stop right here
2811 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2812 // acid2?
2813 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2814 // acid1?
2815 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2816 end;
2818 begin
2819 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2820 if gdbg_map_use_accel_coldet then
2821 begin
2822 texid := LongWord(TEXTURE_NONE);
2823 if (Width = 1) and (Height = 1) then
2824 begin
2825 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2826 end
2827 else
2828 begin
2829 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2830 end;
2831 result := texid;
2832 end
2833 else
2834 begin
2835 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2836 end;
2837 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2838 end;
2841 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2842 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2843 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2846 procedure g_Map_EnableWallGUID (pguid: Integer);
2847 var
2848 pan: TPanel;
2849 begin
2850 //pan := gWalls[ID];
2851 pan := g_Map_PanelByGUID(pguid);
2852 if (pan = nil) then exit;
2853 pan.Enabled := True;
2854 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2856 mapGrid.proxyEnabled[pan.proxyId] := true;
2857 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2858 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2860 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState({gWalls[ID]}pan.PanelType, pguid);
2862 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2863 //e_WriteLog(Format('ENABLE: wall #%d(%d) enabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY);
2864 {$ENDIF}
2865 end;
2868 procedure g_Map_DisableWallGUID (pguid: Integer);
2869 var
2870 pan: TPanel;
2871 begin
2872 //pan := gWalls[ID];
2873 pan := g_Map_PanelByGUID(pguid);
2874 if (pan = nil) then exit;
2875 pan.Enabled := False;
2876 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2878 mapGrid.proxyEnabled[pan.proxyId] := false;
2879 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2881 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, pguid);
2883 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2884 //e_WriteLog(Format('DISABLE: wall #%d(%d) disabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY);
2885 {$ENDIF}
2886 end;
2889 procedure g_Map_SwitchTextureGUID (PanelType: Word; pguid: Integer; AnimLoop: Byte = 0);
2890 var
2891 tp: TPanel;
2892 begin
2893 tp := g_Map_PanelByGUID(pguid);
2894 if (tp = nil) then exit;
2896 case PanelType of
2897 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: tp := gWalls[ID];
2898 PANEL_FORE: tp := gRenderForegrounds[ID];
2899 PANEL_BACK: tp := gRenderBackgrounds[ID];
2900 PANEL_WATER: tp := gWater[ID];
2901 PANEL_ACID1: tp := gAcid1[ID];
2902 PANEL_ACID2: tp := gAcid2[ID];
2903 PANEL_STEP: tp := gSteps[ID];
2904 else exit;
2905 end;
2908 tp.NextTexture(AnimLoop);
2909 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(PanelType, pguid, AnimLoop);
2910 end;
2913 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2914 var
2915 pan: TPanel;
2916 begin
2917 //pan := gLifts[ID];
2918 pan := g_Map_PanelByGUID(pguid);
2919 if (pan = nil) then exit;
2920 if not pan.isGLift then exit;
2922 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2924 with {gLifts[ID]} pan do
2925 begin
2926 LiftType := t;
2928 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2929 //TODO: make separate lift tags, and change tag here
2931 case LiftType of
2932 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2933 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2934 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2935 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2936 end;
2938 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, pguid);
2939 end;
2940 end;
2943 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2944 var
2945 a: Integer;
2946 PointsArray: Array of TRespawnPoint;
2947 begin
2948 Result := False;
2949 SetLength(PointsArray, 0);
2951 if RespawnPoints = nil then
2952 Exit;
2954 for a := 0 to High(RespawnPoints) do
2955 if RespawnPoints[a].PointType = PointType then
2956 begin
2957 SetLength(PointsArray, Length(PointsArray)+1);
2958 PointsArray[High(PointsArray)] := RespawnPoints[a];
2959 end;
2961 if PointsArray = nil then
2962 Exit;
2964 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2965 Result := True;
2966 end;
2968 function g_Map_GetPointCount(PointType: Byte): Word;
2969 var
2970 a: Integer;
2971 begin
2972 Result := 0;
2974 if RespawnPoints = nil then
2975 Exit;
2977 for a := 0 to High(RespawnPoints) do
2978 if RespawnPoints[a].PointType = PointType then
2979 Result := Result + 1;
2980 end;
2982 function g_Map_HaveFlagPoints(): Boolean;
2983 begin
2984 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2985 end;
2987 procedure g_Map_ResetFlag(Flag: Byte);
2988 begin
2989 with gFlags[Flag] do
2990 begin
2991 Obj.X := -1000;
2992 Obj.Y := -1000;
2993 Obj.Vel.X := 0;
2994 Obj.Vel.Y := 0;
2995 Direction := D_LEFT;
2996 State := FLAG_STATE_NONE;
2997 if FlagPoints[Flag] <> nil then
2998 begin
2999 Obj.X := FlagPoints[Flag]^.X;
3000 Obj.Y := FlagPoints[Flag]^.Y;
3001 Direction := FlagPoints[Flag]^.Direction;
3002 State := FLAG_STATE_NORMAL;
3003 end;
3004 Count := -1;
3005 end;
3006 end;
3008 procedure g_Map_DrawFlags();
3009 var
3010 i, dx: Integer;
3011 Mirror: TMirrorType;
3012 begin
3013 if gGameSettings.GameMode <> GM_CTF then
3014 Exit;
3016 for i := FLAG_RED to FLAG_BLUE do
3017 with gFlags[i] do
3018 if State <> FLAG_STATE_CAPTURED then
3019 begin
3020 if State = FLAG_STATE_NONE then
3021 continue;
3023 if Direction = D_LEFT then
3024 begin
3025 Mirror := M_HORIZONTAL;
3026 dx := -1;
3027 end
3028 else
3029 begin
3030 Mirror := M_NONE;
3031 dx := 1;
3032 end;
3034 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3036 if g_debug_Frames then
3037 begin
3038 e_DrawQuad(Obj.X+Obj.Rect.X,
3039 Obj.Y+Obj.Rect.Y,
3040 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3041 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3042 0, 255, 0);
3043 end;
3044 end;
3045 end;
3048 procedure g_Map_SaveState (var Mem: TBinMemoryWriter);
3049 var
3050 dw: DWORD;
3051 b: Byte;
3052 str: String;
3053 boo: Boolean;
3055 procedure savePanels ();
3056 var
3057 PAMem: TBinMemoryWriter;
3058 pan: TPanel;
3059 begin
3060 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé
3061 PAMem := TBinMemoryWriter.Create((Length(panByGUID)+1) * 40);
3063 for pan in panByGUID do
3064 begin
3065 if true{pan.SaveIt} then
3066 begin
3067 // ID ïàíåëè
3068 //PAMem.WriteInt(i);
3069 // Ñîõðàíÿåì ïàíåëü
3070 pan.SaveState(PAMem);
3071 end;
3072 end;
3074 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé
3075 PAMem.SaveToMemory(Mem);
3076 PAMem.Free();
3077 end;
3079 procedure SaveFlag (flag: PFlag);
3080 begin
3081 // Ñèãíàòóðà ôëàãà
3082 dw := FLAG_SIGNATURE; // 'FLAG'
3083 Mem.WriteDWORD(dw);
3084 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3085 Mem.WriteByte(flag^.RespawnType);
3086 // Ñîñòîÿíèå ôëàãà
3087 Mem.WriteByte(flag^.State);
3088 // Íàïðàâëåíèå ôëàãà
3089 if flag^.Direction = D_LEFT then b := 1 else b := 2; // D_RIGHT
3090 Mem.WriteByte(b);
3091 // Îáúåêò ôëàãà
3092 Obj_SaveState(@flag^.Obj, Mem);
3093 end;
3095 begin
3096 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
3098 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
3099 savePanels();
3100 ///// /////
3102 ///// Ñîõðàíÿåì ìóçûêó: /////
3103 // Ñèãíàòóðà ìóçûêè:
3104 dw := MUSIC_SIGNATURE; // 'MUSI'
3105 Mem.WriteDWORD(dw);
3106 // Íàçâàíèå ìóçûêè:
3107 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3108 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3109 Mem.WriteString(str, 64);
3110 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3111 dw := gMusic.GetPosition();
3112 Mem.WriteDWORD(dw);
3113 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3114 boo := gMusic.SpecPause;
3115 Mem.WriteBoolean(boo);
3116 ///// /////
3118 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3119 Mem.WriteInt(gTotalMonsters);
3120 ///// /////
3122 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3123 if gGameSettings.GameMode = GM_CTF then
3124 begin
3125 // Ôëàã Êðàñíîé êîìàíäû
3126 SaveFlag(@gFlags[FLAG_RED]);
3127 // Ôëàã Ñèíåé êîìàíäû
3128 SaveFlag(@gFlags[FLAG_BLUE]);
3129 end;
3130 ///// /////
3132 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3133 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3134 begin
3135 // Î÷êè Êðàñíîé êîìàíäû
3136 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
3137 // Î÷êè Ñèíåé êîìàíäû
3138 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
3139 end;
3140 ///// /////
3141 end;
3144 procedure g_Map_LoadState (var Mem: TBinMemoryReader);
3145 var
3146 dw: DWORD;
3147 b: Byte;
3148 str: String;
3149 boo: Boolean;
3151 procedure loadPanels ();
3152 var
3153 PAMem: TBinMemoryReader;
3154 pan: TPanel;
3155 begin
3156 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé
3157 PAMem := TBinMemoryReader.Create();
3158 PAMem.LoadFromMemory(Mem);
3160 for pan in panByGUID do
3161 begin
3162 if true{pan.SaveIt} then
3163 begin
3164 // ID ïàíåëè:
3165 //PAMem.ReadInt(id);
3167 if id <> i then raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
3169 // Çàãðóæàåì ïàíåëü
3170 pan.LoadState(PAMem);
3171 //if (panels[i].arrIdx <> i) then raise Exception.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
3172 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3173 end;
3174 end;
3176 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí
3177 PAMem.Free();
3178 end;
3180 procedure LoadFlag(flag: PFlag);
3181 begin
3182 // Ñèãíàòóðà ôëàãà
3183 Mem.ReadDWORD(dw);
3184 // 'FLAG'
3185 if dw <> FLAG_SIGNATURE then raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
3186 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3187 Mem.ReadByte(flag^.RespawnType);
3188 // Ñîñòîÿíèå ôëàãà
3189 Mem.ReadByte(flag^.State);
3190 // Íàïðàâëåíèå ôëàãà
3191 Mem.ReadByte(b);
3192 if b = 1 then flag^.Direction := D_LEFT else flag^.Direction := D_RIGHT; // b = 2
3193 // Îáúåêò ôëàãà
3194 Obj_LoadState(@flag^.Obj, Mem);
3195 end;
3197 begin
3198 if Mem = nil then Exit;
3200 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3201 loadPanels();
3202 ///// /////
3204 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3205 g_GFX_Init();
3206 //mapCreateGrid();
3208 ///// Çàãðóæàåì ìóçûêó: /////
3209 // Ñèãíàòóðà ìóçûêè
3210 Mem.ReadDWORD(dw);
3211 // 'MUSI'
3212 if dw <> MUSIC_SIGNATURE then raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
3213 // Íàçâàíèå ìóçûêè
3214 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3215 Mem.ReadString(str);
3216 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3217 Mem.ReadDWORD(dw);
3218 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3219 Mem.ReadBoolean(boo);
3220 // Çàïóñêàåì ýòó ìóçûêó
3221 gMusic.SetByName(str);
3222 gMusic.SpecPause := boo;
3223 gMusic.Play();
3224 gMusic.Pause(True);
3225 gMusic.SetPosition(dw);
3226 ///// /////
3228 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3229 Mem.ReadInt(gTotalMonsters);
3230 ///// /////
3232 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3233 if gGameSettings.GameMode = GM_CTF then
3234 begin
3235 // Ôëàã Êðàñíîé êîìàíäû
3236 LoadFlag(@gFlags[FLAG_RED]);
3237 // Ôëàã Ñèíåé êîìàíäû
3238 LoadFlag(@gFlags[FLAG_BLUE]);
3239 end;
3240 ///// /////
3242 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3243 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3244 begin
3245 // Î÷êè Êðàñíîé êîìàíäû
3246 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
3247 // Î÷êè Ñèíåé êîìàíäû
3248 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
3249 end;
3250 ///// /////
3251 end;
3254 // trace liquid, stepping by `dx` and `dy`
3255 // return last seen liquid coords, and `false` if we're started outside of the liquid
3256 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3257 const
3258 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3259 begin
3260 topx := x;
3261 topy := y;
3262 // started outside of the liquid?
3263 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3264 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3265 result := true;
3266 while true do
3267 begin
3268 Inc(x, dx);
3269 Inc(y, dy);
3270 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3271 topx := x;
3272 topy := y;
3273 end;
3274 end;
3277 end.