DEADSOFTWARE

dbg_scale fixes; made non-sky textures always NEAREST
[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; hasAmbient: Boolean; constref ambColor: TDFColor); // 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 is nested; // 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 // return `true` from `cb` to stop
123 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
125 procedure g_Map_NetSendInterestingPanels (); // yay!
128 procedure g_Map_ProfilersBegin ();
129 procedure g_Map_ProfilersEnd ();
132 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
135 function g_Map_MinX (): Integer; inline;
136 function g_Map_MinY (): Integer; inline;
137 function g_Map_MaxX (): Integer; inline;
138 function g_Map_MaxY (): Integer; inline;
140 const
141 NNF_NO_NAME = 0;
142 NNF_NAME_BEFORE = 1;
143 NNF_NAME_EQUALS = 2;
144 NNF_NAME_AFTER = 3;
146 function g_Texture_NumNameFindStart(name: String): Boolean;
147 function g_Texture_NumNameFindNext(var newName: String): Byte;
149 const
150 RESPAWNPOINT_PLAYER1 = 1;
151 RESPAWNPOINT_PLAYER2 = 2;
152 RESPAWNPOINT_DM = 3;
153 RESPAWNPOINT_RED = 4;
154 RESPAWNPOINT_BLUE = 5;
156 FLAG_NONE = 0;
157 FLAG_RED = 1;
158 FLAG_BLUE = 2;
159 FLAG_DOM = 3;
161 FLAG_STATE_NONE = 0;
162 FLAG_STATE_NORMAL = 1;
163 FLAG_STATE_DROPPED = 2;
164 FLAG_STATE_CAPTURED = 3;
165 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
166 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
168 FLAG_TIME = 720; // 20 seconds
170 SKY_STRETCH: Single = 1.5;
172 const
173 GridTagInvalid = 0;
175 (* draw order:
176 PANEL_BACK
177 PANEL_STEP
178 PANEL_WALL
179 PANEL_CLOSEDOOR
180 PANEL_ACID1
181 PANEL_ACID2
182 PANEL_WATER
183 PANEL_FORE
184 *)
185 // sorted by draw priority
186 GridTagBack = 1 shl 0; // gRenderBackgrounds
187 GridTagStep = 1 shl 1; // gSteps
188 GridTagWall = 1 shl 2; // gWalls
189 GridTagDoor = 1 shl 3; // gWalls
190 GridTagAcid1 = 1 shl 4; // gAcid1
191 GridTagAcid2 = 1 shl 5; // gAcid2
192 GridTagWater = 1 shl 6; // gWater
193 GridTagFore = 1 shl 7; // gRenderForegrounds
194 // the following are invisible
195 GridTagLift = 1 shl 8; // gLifts
196 GridTagBlockMon = 1 shl 9; // gBlockMon
198 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
199 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
201 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
204 var
205 gWalls: TPanelArray;
206 gRenderBackgrounds: TPanelArray;
207 gRenderForegrounds: TPanelArray;
208 gWater, gAcid1, gAcid2: TPanelArray;
209 gSteps: TPanelArray;
210 gLifts: TPanelArray;
211 gBlockMon: TPanelArray;
212 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
213 //gDOMFlags: array of TFlag;
214 gMapInfo: TMapInfo;
215 gBackSize: TDFPoint;
216 gDoorMap: array of array of DWORD;
217 gLiftMap: array of array of DWORD;
218 gWADHash: TMD5Digest;
219 BackID: DWORD = DWORD(-1);
220 gExternalResources: TStringList;
221 gMovingWallIds: array of Integer = nil;
223 gdbg_map_use_accel_render: Boolean = true;
224 gdbg_map_use_accel_coldet: Boolean = true;
225 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
226 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
228 gCurrentMap: TDynRecord = nil;
229 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
232 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
235 type
236 TPanelGrid = specialize TBodyGridBase<TPanel>;
238 var
239 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
242 implementation
244 uses
245 e_input, g_main, e_log, e_texture, SysUtils, g_items, g_gfx, g_console,
246 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
247 g_options, g_triggers, g_player,
248 Math, g_monsters, g_saveload, g_language, g_netmsg,
249 utils, sfs, xstreams, hashtable,
250 ImagingTypes, Imaging, ImagingUtility,
251 ImagingGif, ImagingNetworkGraphics;
253 const
254 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
255 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
256 FLAG_SIGNATURE = $47414C46; // 'FLAG'
259 var
260 panByGUID: array of TPanel = nil;
263 // ////////////////////////////////////////////////////////////////////////// //
264 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
265 begin
266 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
267 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
268 end;
271 // return `true` from `cb` to stop
272 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
273 var
274 pan: TPanel;
275 begin
276 result := nil;
277 if not assigned(cb) then exit;
278 for pan in panByGUID do
279 begin
280 if cb(pan) then begin result := pan; exit; end;
281 end;
282 end;
285 procedure g_Map_NetSendInterestingPanels ();
286 var
287 pan: TPanel;
288 begin
289 if g_Game_IsServer and g_Game_IsNet then
290 begin
291 for pan in panByGUID do
292 begin
293 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
294 end;
295 end;
296 end;
299 // ////////////////////////////////////////////////////////////////////////// //
300 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
301 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
302 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
303 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
306 // ////////////////////////////////////////////////////////////////////////// //
307 var
308 dfmapdef: TDynMapDef = nil;
311 procedure loadMapDefinition ();
312 var
313 pr: TTextParser = nil;
314 st: TStream = nil;
315 WAD: TWADFile = nil;
316 begin
317 if (dfmapdef <> nil) then exit;
319 try
320 e_LogWritefln('parsing "mapdef.txt"...', []);
321 st := openDiskFileRO(DataDir+'mapdef.txt');
322 e_LogWritefln('found local "%smapdef.txt"', [DataDir]);
323 except
324 st := nil;
325 end;
327 if (st = nil) then
328 begin
329 WAD := TWADFile.Create();
330 if not WAD.ReadFile(GameWAD) then
331 begin
332 //raise Exception.Create('cannot load "game.wad"');
333 st := nil;
334 end
335 else
336 begin
337 st := WAD.openFileStream('mapdef.txt');
338 end;
339 end;
341 try
342 if (st = nil) then
343 begin
344 //raise Exception.Create('cannot open "mapdef.txt"');
345 e_LogWriteln('using default "mapdef.txt"...');
346 pr := TStrTextParser.Create(defaultMapDef);
347 end
348 else
349 begin
350 pr := TFileTextParser.Create(st);
351 end;
352 except on e: Exception do
353 begin
354 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
355 raise;
356 end;
357 end;
359 try
360 dfmapdef := TDynMapDef.Create(pr);
361 except
362 on e: TDynParseException do
363 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
364 on e: Exception do
365 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
366 end;
368 st.Free();
369 WAD.Free();
370 end;
373 // ////////////////////////////////////////////////////////////////////////// //
374 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
375 var
376 wst: TSFSMemoryChunkStream = nil;
377 begin
378 result := nil;
379 if (dataLen < 4) then exit;
381 if (dfmapdef = nil) then writeln('need to load mapdef');
382 loadMapDefinition();
383 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
385 wst := TSFSMemoryChunkStream.Create(data, dataLen);
386 try
387 result := dfmapdef.parseMap(wst);
388 except
389 on e: TDynParseException do
390 begin
391 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
392 wst.Free();
393 result := nil;
394 exit;
395 end;
396 on e: Exception do
397 begin
398 e_LogWritefln('ERROR: %s', [e.message]);
399 wst.Free();
400 result := nil;
401 exit;
402 end;
403 end;
405 //e_LogWriteln('map parsed.');
406 end;
409 // ////////////////////////////////////////////////////////////////////////// //
410 var
411 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
412 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
413 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
416 function g_Texture_NumNameFindStart(name: String): Boolean;
417 var
418 i: Integer;
420 begin
421 Result := False;
422 NNF_PureName := '';
423 NNF_FirstNum := -1;
424 NNF_CurrentNum := -1;
426 for i := Length(name) downto 1 do
427 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
428 begin
429 if i = Length(name) then
430 begin // Íåò öèôð â êîíöå ñòðîêè
431 Exit;
432 end
433 else
434 begin
435 NNF_PureName := Copy(name, 1, i);
436 Delete(name, 1, i);
437 Break;
438 end;
439 end;
441 // Íå ïåðåâåñòè â ÷èñëî:
442 if not TryStrToInt(name, NNF_FirstNum) then
443 Exit;
445 NNF_CurrentNum := 0;
447 Result := True;
448 end;
451 function g_Texture_NumNameFindNext(var newName: String): Byte;
452 begin
453 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
454 begin
455 newName := '';
456 Result := NNF_NO_NAME;
457 Exit;
458 end;
460 newName := NNF_PureName + IntToStr(NNF_CurrentNum);
462 if NNF_CurrentNum < NNF_FirstNum then
463 Result := NNF_NAME_BEFORE
464 else
465 if NNF_CurrentNum > NNF_FirstNum then
466 Result := NNF_NAME_AFTER
467 else
468 Result := NNF_NAME_EQUALS;
470 Inc(NNF_CurrentNum);
471 end;
474 // ////////////////////////////////////////////////////////////////////////// //
475 function panelTypeToTag (panelType: Word): Integer;
476 begin
477 case panelType of
478 PANEL_WALL: result := GridTagWall; // gWalls
479 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
480 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
481 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
482 PANEL_WATER: result := GridTagWater; // gWater
483 PANEL_ACID1: result := GridTagAcid1; // gAcid1
484 PANEL_ACID2: result := GridTagAcid2; // gAcid2
485 PANEL_STEP: result := GridTagStep; // gSteps
486 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
487 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
488 else result := GridTagInvalid;
489 end;
490 end;
493 function dplLess (a, b: TObject): Boolean;
494 var
495 pa, pb: TPanel;
496 begin
497 pa := TPanel(a);
498 pb := TPanel(b);
499 if (pa.tag < pb.tag) then begin result := true; exit; end;
500 if (pa.tag > pb.tag) then begin result := false; exit; end;
501 result := (pa.arrIdx < pb.arrIdx);
502 end;
504 procedure dplClear ();
505 begin
506 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
507 end;
510 var
511 Textures: TLevelTextureArray = nil;
512 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
513 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
514 RespawnPoints: array of TRespawnPoint;
515 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
516 //DOMFlagPoints: Array of TFlagPoint;
519 procedure g_Map_ProfilersBegin ();
520 begin
521 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
522 profMapCollision.mainBegin(g_profile_collision);
523 // create sections
524 if g_profile_collision then
525 begin
526 profMapCollision.sectionBegin('*solids');
527 profMapCollision.sectionEnd();
528 profMapCollision.sectionBegin('liquids');
529 profMapCollision.sectionEnd();
530 end;
531 end;
533 procedure g_Map_ProfilersEnd ();
534 begin
535 if (profMapCollision <> nil) then profMapCollision.mainEnd();
536 end;
539 // wall index in `gWalls` or -1
540 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
541 var
542 ex, ey: Integer;
543 begin
544 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor));
545 if (result <> nil) then
546 begin
547 if (hitx <> nil) then hitx^ := ex;
548 if (hity <> nil) then hity^ := ey;
549 end
550 else
551 begin
552 if (hitx <> nil) then hitx^ := x1;
553 if (hity <> nil) then hity^ := y1;
554 end;
555 end;
557 // returns panel or nil
558 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
559 var
560 ex, ey: Integer;
561 begin
562 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, tag);
563 if (result <> nil) then
564 begin
565 if (hitx <> nil) then hitx^ := ex;
566 if (hity <> nil) then hity^ := ey;
567 end
568 else
569 begin
570 if (hitx <> nil) then hitx^ := x1;
571 if (hity <> nil) then hity^ := y1;
572 end;
573 end;
576 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
578 function checker (pan: TPanel; tag: Integer): Boolean;
579 begin
581 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
582 begin
583 result := pan.Enabled; // stop if wall is enabled
584 exit;
585 end;
588 if ((tag and GridTagLift) <> 0) then
589 begin
590 // stop if the lift of the right type
591 result :=
592 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
593 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
594 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
595 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
596 exit;
597 end;
599 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
600 end;
602 var
603 tagmask: Integer = 0;
604 begin
605 result := false;
607 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
608 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
609 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
610 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
611 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
612 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
613 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
615 if (tagmask = 0) then exit;// just in case
616 if ((tagmask and GridTagLift) <> 0) then
617 begin
618 // slow
619 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
620 end
621 else
622 begin
623 // fast
624 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
625 end;
626 end;
629 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
630 begin
631 result := nil;
632 if (tagmask = 0) then exit;
633 result := mapGrid.forEachAtPoint(x, y, nil, tagmask);
634 end;
637 function g_Map_IsSpecialTexture(Texture: String): Boolean;
638 begin
639 Result := (Texture = TEXTURE_NAME_WATER) or
640 (Texture = TEXTURE_NAME_ACID1) or
641 (Texture = TEXTURE_NAME_ACID2);
642 end;
644 procedure CreateDoorMap();
645 var
646 PanelArray: Array of record
647 X, Y: Integer;
648 Width, Height: Word;
649 Active: Boolean;
650 PanelID: DWORD;
651 end;
652 a, b, c, m, i, len: Integer;
653 ok: Boolean;
654 begin
655 if gWalls = nil then
656 Exit;
658 i := 0;
659 len := 128;
660 SetLength(PanelArray, len);
662 for a := 0 to High(gWalls) do
663 if gWalls[a].Door then
664 begin
665 PanelArray[i].X := gWalls[a].X;
666 PanelArray[i].Y := gWalls[a].Y;
667 PanelArray[i].Width := gWalls[a].Width;
668 PanelArray[i].Height := gWalls[a].Height;
669 PanelArray[i].Active := True;
670 PanelArray[i].PanelID := a;
672 i := i + 1;
673 if i = len then
674 begin
675 len := len + 128;
676 SetLength(PanelArray, len);
677 end;
678 end;
680 // Íåò äâåðåé:
681 if i = 0 then
682 begin
683 PanelArray := nil;
684 Exit;
685 end;
687 SetLength(gDoorMap, 0);
689 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
691 for a := 0 to i-1 do
692 if PanelArray[a].Active then
693 begin
694 PanelArray[a].Active := False;
695 m := Length(gDoorMap);
696 SetLength(gDoorMap, m+1);
697 SetLength(gDoorMap[m], 1);
698 gDoorMap[m, 0] := PanelArray[a].PanelID;
699 ok := True;
701 while ok do
702 begin
703 ok := False;
705 for b := 0 to i-1 do
706 if PanelArray[b].Active then
707 for c := 0 to High(gDoorMap[m]) do
708 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
709 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
710 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
711 PanelArray[b].Width, PanelArray[b].Height,
712 gWalls[gDoorMap[m, c]].X,
713 gWalls[gDoorMap[m, c]].Y,
714 gWalls[gDoorMap[m, c]].Width,
715 gWalls[gDoorMap[m, c]].Height) then
716 begin
717 PanelArray[b].Active := False;
718 SetLength(gDoorMap[m],
719 Length(gDoorMap[m])+1);
720 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
721 ok := True;
722 Break;
723 end;
724 end;
726 g_Game_StepLoading();
727 end;
729 PanelArray := nil;
730 end;
732 procedure CreateLiftMap();
733 var
734 PanelArray: Array of record
735 X, Y: Integer;
736 Width, Height: Word;
737 Active: Boolean;
738 end;
739 a, b, c, len, i, j: Integer;
740 ok: Boolean;
741 begin
742 if gLifts = nil then
743 Exit;
745 len := Length(gLifts);
746 SetLength(PanelArray, len);
748 for a := 0 to len-1 do
749 begin
750 PanelArray[a].X := gLifts[a].X;
751 PanelArray[a].Y := gLifts[a].Y;
752 PanelArray[a].Width := gLifts[a].Width;
753 PanelArray[a].Height := gLifts[a].Height;
754 PanelArray[a].Active := True;
755 end;
757 SetLength(gLiftMap, len);
758 i := 0;
760 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
762 for a := 0 to len-1 do
763 if PanelArray[a].Active then
764 begin
765 PanelArray[a].Active := False;
766 SetLength(gLiftMap[i], 32);
767 j := 0;
768 gLiftMap[i, j] := a;
769 ok := True;
771 while ok do
772 begin
773 ok := False;
774 for b := 0 to len-1 do
775 if PanelArray[b].Active then
776 for c := 0 to j do
777 if g_CollideAround(PanelArray[b].X,
778 PanelArray[b].Y,
779 PanelArray[b].Width,
780 PanelArray[b].Height,
781 PanelArray[gLiftMap[i, c]].X,
782 PanelArray[gLiftMap[i, c]].Y,
783 PanelArray[gLiftMap[i, c]].Width,
784 PanelArray[gLiftMap[i, c]].Height) then
785 begin
786 PanelArray[b].Active := False;
787 j := j+1;
788 if j > High(gLiftMap[i]) then
789 SetLength(gLiftMap[i],
790 Length(gLiftMap[i])+32);
792 gLiftMap[i, j] := b;
793 ok := True;
795 Break;
796 end;
797 end;
799 SetLength(gLiftMap[i], j+1);
800 i := i+1;
802 g_Game_StepLoading();
803 end;
805 SetLength(gLiftMap, i);
807 PanelArray := nil;
808 end;
810 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
811 var
812 len: Integer;
813 panels: ^TPanelArray;
814 pan: TPanel;
815 pguid: Integer;
816 begin
817 Result := -1;
819 case PanelRec.PanelType of
820 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
821 PANEL_BACK: panels := @gRenderBackgrounds;
822 PANEL_FORE: panels := @gRenderForegrounds;
823 PANEL_WATER: panels := @gWater;
824 PANEL_ACID1: panels := @gAcid1;
825 PANEL_ACID2: panels := @gAcid2;
826 PANEL_STEP: panels := @gSteps;
827 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
828 PANEL_BLOCKMON: panels := @gBlockMon;
829 else exit;
830 end;
832 len := Length(panels^);
833 SetLength(panels^, len+1);
835 pguid := Length(panByGUID);
836 SetLength(panByGUID, pguid+1); //FIXME!
837 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
838 assert(pguid >= 0);
839 assert(pguid < Length(panByGUID));
840 panByGUID[pguid] := pan;
841 panels^[len] := pan;
842 pan.arrIdx := len;
843 pan.proxyId := -1;
844 pan.tag := panelTypeToTag(PanelRec.PanelType);
846 PanelRec.user['panel_guid'] := pguid;
848 //result := len;
849 result := pguid;
850 end;
853 function CreateNullTexture(RecName: String): Integer;
854 begin
855 RecName := toLowerCase1251(RecName);
856 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
857 if TextNameHash.get(RecName, result) then exit; // i found her!
859 SetLength(Textures, Length(Textures)+1);
860 result := High(Textures);
862 with Textures[High(Textures)] do
863 begin
864 TextureName := RecName;
865 Width := 1;
866 Height := 1;
867 Anim := False;
868 TextureID := LongWord(TEXTURE_NONE);
869 end;
871 TextNameHash.put(RecName, result);
872 end;
875 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
876 var
877 WAD: TWADFile;
878 TextureData: Pointer;
879 WADName: String;
880 a, ResLength: Integer;
881 oldFilter: Integer;
882 begin
883 RecName := toLowerCase1251(RecName);
884 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
885 if TextNameHash.get(RecName, result) then
886 begin
887 // i found her!
888 //e_LogWritefln('texture ''%s'' already loaded', [RecName]);
889 exit;
890 end;
892 Result := -1;
894 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
897 if Textures <> nil then
898 begin
899 for a := 0 to High(Textures) do
900 begin
901 if (Textures[a].TextureName = RecName) then
902 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
903 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
904 Result := a;
905 Exit;
906 end;
907 end;
908 end;
911 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
912 if (RecName = TEXTURE_NAME_WATER) or
913 (RecName = TEXTURE_NAME_ACID1) or
914 (RecName = TEXTURE_NAME_ACID2) then
915 begin
916 SetLength(Textures, Length(Textures)+1);
918 with Textures[High(Textures)] do
919 begin
920 TextureName := RecName;
921 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
922 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
923 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
925 Anim := False;
926 end;
928 result := High(Textures);
929 TextNameHash.put(RecName, result);
930 Exit;
931 end;
933 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
934 WADName := g_ExtractWadName(RecName);
936 WAD := TWADFile.Create();
938 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
940 WAD.ReadFile(WADName);
942 //txname := RecName;
944 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
945 begin
946 FreeMem(TextureData);
947 RecName := 'COMMON\ALIEN';
948 end;
951 oldFilter := TEXTUREFILTER;
952 TEXTUREFILTER := GL_NEAREST;
953 try
954 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
955 begin
956 SetLength(Textures, Length(Textures)+1);
957 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
958 begin
959 SetLength(Textures, Length(Textures)-1);
960 Exit;
961 end;
962 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
963 FreeMem(TextureData);
964 Textures[High(Textures)].TextureName := RecName;
965 Textures[High(Textures)].Anim := False;
967 result := High(Textures);
968 TextNameHash.put(RecName, result);
969 end
970 else // Íåò òàêîãî ðåóñðñà â WAD'å
971 begin
972 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
973 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
974 if log and (not BadTextNameHash.get(RecName, a)) then
975 begin
976 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
977 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
978 end;
979 BadTextNameHash.put(RecName, -1);
980 end;
981 finally
982 TEXTUREFILTER := oldFilter;
983 end;
985 WAD.Free();
986 end;
989 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
990 var
991 WAD: TWADFile;
992 TextureWAD: PChar = nil;
993 TextData: Pointer = nil;
994 TextureData: Pointer = nil;
995 cfg: TConfig = nil;
996 WADName: String;
997 ResLength: Integer;
998 TextureResource: String;
999 _width, _height, _framecount, _speed: Integer;
1000 _backanimation: Boolean;
1001 //imgfmt: string;
1002 ia: TDynImageDataArray = nil;
1003 f, c, frdelay, frloop: Integer;
1004 begin
1005 RecName := toLowerCase1251(RecName);
1006 if (TextNameHash = nil) then TextNameHash := hashNewStrInt();
1007 if TextNameHash.get(RecName, result) then
1008 begin
1009 // i found her!
1010 //e_LogWritefln('animated texture ''%s'' already loaded', [RecName]);
1011 exit;
1012 end;
1014 result := -1;
1016 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1018 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1019 if BadTextNameHash.get(RecName, f) then
1020 begin
1021 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1022 exit;
1023 end;
1025 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1026 WADName := g_ExtractWadName(RecName);
1028 WAD := TWADFile.Create();
1029 try
1030 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1032 WAD.ReadFile(WADName);
1034 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1035 begin
1036 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1037 if log and (not BadTextNameHash.get(RecName, f)) then
1038 begin
1039 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1040 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1041 end;
1042 BadTextNameHash.put(RecName, -1);
1043 exit;
1044 end;
1046 {TEST
1047 if WADName = Map then
1048 begin
1049 //FreeMem(TextureWAD);
1050 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1051 end;
1054 WAD.FreeWAD();
1056 if ResLength < 6 then
1057 begin
1058 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
1059 BadTextNameHash.put(RecName, -1);
1060 exit;
1061 end;
1063 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1064 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1065 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1066 begin
1067 // íåò, ýòî ñóïåðìåí!
1068 if not WAD.ReadMemory(TextureWAD, ResLength) then
1069 begin
1070 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
1071 BadTextNameHash.put(RecName, -1);
1072 exit;
1073 end;
1075 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1076 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1077 begin
1078 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
1079 BadTextNameHash.put(RecName, -1);
1080 exit;
1081 end;
1083 cfg := TConfig.CreateMem(TextData, ResLength);
1085 TextureResource := cfg.ReadStr('', 'resource', '');
1086 if TextureResource = '' then
1087 begin
1088 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
1089 BadTextNameHash.put(RecName, -1);
1090 exit;
1091 end;
1093 _width := cfg.ReadInt('', 'framewidth', 0);
1094 _height := cfg.ReadInt('', 'frameheight', 0);
1095 _framecount := cfg.ReadInt('', 'framecount', 0);
1096 _speed := cfg.ReadInt('', 'waitcount', 0);
1097 _backanimation := cfg.ReadBool('', 'backanimation', False);
1099 cfg.Free();
1100 cfg := nil;
1102 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1103 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1104 begin
1105 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
1106 BadTextNameHash.put(RecName, -1);
1107 exit;
1108 end;
1110 WAD.Free();
1111 WAD := nil;
1113 SetLength(Textures, Length(Textures)+1);
1114 with Textures[High(Textures)] do
1115 begin
1116 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1117 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1118 begin
1119 TextureName := RecName;
1120 Width := _width;
1121 Height := _height;
1122 Anim := True;
1123 FramesCount := _framecount;
1124 Speed := _speed;
1125 result := High(Textures);
1126 TextNameHash.put(RecName, result);
1127 end
1128 else
1129 begin
1130 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1131 if log and (not BadTextNameHash.get(RecName, f)) then
1132 begin
1133 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
1134 end;
1135 BadTextNameHash.put(RecName, -1);
1136 end;
1137 end;
1138 end
1139 else
1140 begin
1141 // try animated image
1143 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1144 if length(imgfmt) = 0 then
1145 begin
1146 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1147 exit;
1148 end;
1150 GlobalMetadata.ClearMetaItems();
1151 GlobalMetadata.ClearMetaItemsForSaving();
1152 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1153 begin
1154 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
1155 BadTextNameHash.put(RecName, -1);
1156 exit;
1157 end;
1158 if length(ia) = 0 then
1159 begin
1160 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
1161 BadTextNameHash.put(RecName, -1);
1162 exit;
1163 end;
1165 WAD.Free();
1166 WAD := nil;
1168 _width := ia[0].width;
1169 _height := ia[0].height;
1170 _framecount := length(ia);
1171 _speed := 1;
1172 _backanimation := false;
1173 frdelay := -1;
1174 frloop := -666;
1175 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1176 begin
1177 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1178 try
1179 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1180 frdelay := f;
1181 if f < 0 then f := 0;
1182 // rounding ;-)
1183 c := f mod 28;
1184 if c < 13 then c := 0 else c := 1;
1185 f := (f div 28)+c;
1186 if f < 1 then f := 1 else if f > 255 then f := 255;
1187 _speed := f;
1188 except
1189 end;
1190 end;
1191 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1192 begin
1193 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1194 try
1195 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1196 frloop := f;
1197 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1198 except
1199 end;
1200 end;
1201 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1202 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1203 f := ord(_backanimation);
1204 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);
1206 SetLength(Textures, Length(Textures)+1);
1207 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1208 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1209 begin
1210 Textures[High(Textures)].TextureName := RecName;
1211 Textures[High(Textures)].Width := _width;
1212 Textures[High(Textures)].Height := _height;
1213 Textures[High(Textures)].Anim := True;
1214 Textures[High(Textures)].FramesCount := length(ia);
1215 Textures[High(Textures)].Speed := _speed;
1216 result := High(Textures);
1217 TextNameHash.put(RecName, result);
1218 //writeln(' CREATED!');
1219 end
1220 else
1221 begin
1222 if (BadTextNameHash = nil) then BadTextNameHash := hashNewStrInt();
1223 if log and (not BadTextNameHash.get(RecName, f)) then
1224 begin
1225 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
1226 end;
1227 BadTextNameHash.put(RecName, -1);
1228 end;
1229 end;
1230 finally
1231 for f := 0 to High(ia) do FreeImage(ia[f]);
1232 WAD.Free();
1233 cfg.Free();
1234 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1235 if (TextData <> nil) then FreeMem(TextData);
1236 if (TextureData <> nil) then FreeMem(TextureData);
1237 end;
1238 end;
1240 procedure CreateItem(Item: TDynRecord);
1241 begin
1242 if g_Game_IsClient then Exit;
1244 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1245 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1246 Exit;
1248 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1249 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1250 end;
1252 procedure CreateArea(Area: TDynRecord);
1253 var
1254 a: Integer;
1255 id: DWORD = 0;
1256 begin
1257 case Area.AreaType of
1258 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1259 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1260 begin
1261 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1262 with RespawnPoints[High(RespawnPoints)] do
1263 begin
1264 X := Area.X;
1265 Y := Area.Y;
1266 Direction := TDirection(Area.Direction);
1268 case Area.AreaType of
1269 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1270 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1271 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1272 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1273 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1274 end;
1275 end;
1276 end;
1278 AREA_REDFLAG, AREA_BLUEFLAG:
1279 begin
1280 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1282 if FlagPoints[a] <> nil then Exit;
1284 New(FlagPoints[a]);
1286 with FlagPoints[a]^ do
1287 begin
1288 X := Area.X-FLAGRECT.X;
1289 Y := Area.Y-FLAGRECT.Y;
1290 Direction := TDirection(Area.Direction);
1291 end;
1293 with gFlags[a] do
1294 begin
1295 case a of
1296 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1297 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1298 end;
1300 Animation := TAnimation.Create(id, True, 8);
1301 Obj.Rect := FLAGRECT;
1303 g_Map_ResetFlag(a);
1304 end;
1305 end;
1307 AREA_DOMFLAG:
1308 begin
1309 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1310 with DOMFlagPoints[High(DOMFlagPoints)] do
1311 begin
1312 X := Area.X;
1313 Y := Area.Y;
1314 Direction := TDirection(Area.Direction);
1315 end;
1317 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1318 end;
1319 end;
1320 end;
1322 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer; fTexturePanel1Type, fTexturePanel2Type: Word): Integer;
1323 var
1324 _trigger: TTrigger;
1325 begin
1326 result := -1;
1327 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1329 with _trigger do
1330 begin
1331 mapId := Trigger.id;
1332 mapIndex := amapIdx;
1333 X := Trigger.X;
1334 Y := Trigger.Y;
1335 Width := Trigger.Width;
1336 Height := Trigger.Height;
1337 Enabled := Trigger.Enabled;
1338 //TexturePanel := Trigger.TexturePanel;
1339 TexturePanelGUID := atpanid;
1340 TexturePanelType := fTexturePanel1Type;
1341 ShotPanelType := fTexturePanel2Type;
1342 TriggerType := Trigger.TriggerType;
1343 ActivateType := Trigger.ActivateType;
1344 Keys := Trigger.Keys;
1345 trigPanelGUID := atrigpanid;
1346 //trigShotPanelId := ashotpanid;
1347 //Data.Default := Trigger.DATA;
1348 end;
1350 result := Integer(g_Triggers_Create(_trigger, Trigger));
1351 end;
1353 procedure CreateMonster(monster: TDynRecord);
1354 var
1355 a: Integer;
1356 mon: TMonster;
1357 begin
1358 if g_Game_IsClient then Exit;
1360 if (gGameSettings.GameType = GT_SINGLE)
1361 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1362 begin
1363 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1365 if gTriggers <> nil then
1366 begin
1367 for a := 0 to High(gTriggers) do
1368 begin
1369 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1370 begin
1371 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1372 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1373 end;
1374 end;
1375 end;
1377 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1378 end;
1379 end;
1381 procedure g_Map_ReAdd_DieTriggers();
1383 function monsDieTrig (mon: TMonster): Boolean;
1384 var
1385 a: Integer;
1386 //tw: TStrTextWriter;
1387 begin
1388 result := false; // don't stop
1389 mon.ClearTriggers();
1390 for a := 0 to High(gTriggers) do
1391 begin
1392 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1393 begin
1394 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1396 tw := TStrTextWriter.Create();
1397 try
1398 gTriggers[a].trigData.writeTo(tw);
1399 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1400 finally
1401 tw.Free();
1402 end;
1404 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1405 end;
1406 end;
1407 end;
1409 begin
1410 if g_Game_IsClient then Exit;
1412 g_Mons_ForEach(monsDieTrig);
1413 end;
1415 function extractWadName(resourceName: string): string;
1416 var
1417 posN: Integer;
1418 begin
1419 posN := Pos(':', resourceName);
1420 if posN > 0 then
1421 Result:= Copy(resourceName, 0, posN-1)
1422 else
1423 Result := '';
1424 end;
1426 procedure addResToExternalResList(res: string);
1427 begin
1428 res := extractWadName(res);
1429 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1430 gExternalResources.Add(res);
1431 end;
1433 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1434 //var
1435 //textures: TTexturesRec1Array;
1436 //textures: TDynField;
1437 //trec: TDynRecord;
1438 //mapHeader: TMapHeaderRec_1;
1439 //i: integer;
1440 //resFile: String = '';
1441 begin
1442 if gExternalResources = nil then
1443 gExternalResources := TStringList.Create;
1445 gExternalResources.Clear;
1447 (*
1449 textures := GetTextures(map);
1450 for i := 0 to High(textures) do
1451 begin
1452 addResToExternalResList(resFile);
1453 end;
1456 textures := map['texture'];
1457 if (textures <> nil) then
1458 begin
1459 for trec in textures do
1460 begin
1461 addResToExternalResList(resFile);
1462 end;
1463 end;
1465 textures := nil;
1466 *)
1468 //mapHeader := GetMapHeader(map);
1470 addResToExternalResList(map.MusicName);
1471 addResToExternalResList(map.SkyName);
1472 end;
1475 procedure mapCreateGrid ();
1476 var
1477 mapX0: Integer = $3fffffff;
1478 mapY0: Integer = $3fffffff;
1479 mapX1: Integer = -$3fffffff;
1480 mapY1: Integer = -$3fffffff;
1482 procedure calcBoundingBox (constref panels: TPanelArray);
1483 var
1484 idx: Integer;
1485 pan: TPanel;
1486 begin
1487 for idx := 0 to High(panels) do
1488 begin
1489 pan := panels[idx];
1490 if not pan.visvalid then continue;
1491 if (pan.Width < 1) or (pan.Height < 1) then continue;
1492 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1493 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1494 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1495 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1496 end;
1497 end;
1499 procedure addPanelsToGrid (constref panels: TPanelArray);
1500 var
1501 idx: Integer;
1502 pan: TPanel;
1503 newtag: Integer;
1504 begin
1505 //tag := panelTypeToTag(tag);
1506 for idx := 0 to High(panels) do
1507 begin
1508 pan := panels[idx];
1509 if not pan.visvalid then continue;
1510 if (pan.proxyId <> -1) then
1511 begin
1512 {$IF DEFINED(D2F_DEBUG)}
1513 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);
1514 {$ENDIF}
1515 continue;
1516 end;
1517 case pan.PanelType of
1518 PANEL_WALL: newtag := GridTagWall;
1519 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1520 PANEL_BACK: newtag := GridTagBack;
1521 PANEL_FORE: newtag := GridTagFore;
1522 PANEL_WATER: newtag := GridTagWater;
1523 PANEL_ACID1: newtag := GridTagAcid1;
1524 PANEL_ACID2: newtag := GridTagAcid2;
1525 PANEL_STEP: newtag := GridTagStep;
1526 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1527 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1528 else continue; // oops
1529 end;
1530 pan.tag := newtag;
1532 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1533 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1534 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1535 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1537 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1538 begin
1539 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1540 end;
1542 {$ENDIF}
1543 end;
1544 end;
1546 begin
1547 mapGrid.Free();
1548 mapGrid := nil;
1550 calcBoundingBox(gWalls);
1551 calcBoundingBox(gRenderBackgrounds);
1552 calcBoundingBox(gRenderForegrounds);
1553 calcBoundingBox(gWater);
1554 calcBoundingBox(gAcid1);
1555 calcBoundingBox(gAcid2);
1556 calcBoundingBox(gSteps);
1557 calcBoundingBox(gLifts);
1558 calcBoundingBox(gBlockMon);
1560 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1562 if (mapX0 > 0) then mapX0 := 0;
1563 if (mapY0 > 0) then mapY0 := 0;
1565 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1566 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1568 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1569 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1571 addPanelsToGrid(gWalls);
1572 addPanelsToGrid(gRenderBackgrounds);
1573 addPanelsToGrid(gRenderForegrounds);
1574 addPanelsToGrid(gWater);
1575 addPanelsToGrid(gAcid1);
1576 addPanelsToGrid(gAcid2);
1577 addPanelsToGrid(gSteps);
1578 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1579 addPanelsToGrid(gBlockMon);
1581 mapGrid.dumpStats();
1583 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1584 end;
1587 function g_Map_Load(Res: String): Boolean;
1588 const
1589 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1590 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1591 type
1592 PTRec = ^TTRec;
1593 TTRec = record
1594 //TexturePanel: Integer;
1595 tnum: Integer;
1596 id: Integer;
1597 trigrec: TDynRecord;
1598 // texture pane;
1599 texPanelIdx: Integer;
1600 texPanel: TDynRecord;
1601 // "action" panel
1602 actPanelIdx: Integer;
1603 actPanel: TDynRecord;
1604 end;
1605 var
1606 WAD: TWADFile;
1607 mapReader: TDynRecord = nil;
1608 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1609 panels: TDynField = nil; //TPanelsRec1Array;
1610 items: TDynField = nil; //TItemsRec1Array;
1611 monsters: TDynField = nil; //TMonsterRec1Array;
1612 areas: TDynField = nil; //TAreasRec1Array;
1613 triggers: TDynField = nil; //TTriggersRec1Array;
1614 b, c, k: Integer;
1615 PanelID: DWORD;
1616 AddTextures: TAddTextureArray;
1617 TriggersTable: array of TTRec;
1618 FileName, mapResName, s, TexName: String;
1619 Data: Pointer;
1620 Len: Integer;
1621 ok, isAnim: Boolean;
1622 CurTex, ntn: Integer;
1623 rec, texrec: TDynRecord;
1624 pttit: PTRec;
1625 pannum, trignum, cnt, tgpid: Integer;
1626 stt: UInt64;
1627 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1628 //moveActive: Boolean;
1629 pan: TPanel;
1630 mapOk: Boolean = false;
1631 begin
1632 mapGrid.Free();
1633 mapGrid := nil;
1635 gCurrentMap.Free();
1636 gCurrentMap := nil;
1638 panByGUID := nil;
1640 Result := False;
1641 gMapInfo.Map := Res;
1642 TriggersTable := nil;
1643 mapReader := nil;
1645 sfsGCDisable(); // temporary disable removing of temporary volumes
1646 try
1647 // Çàãðóçêà WAD:
1648 FileName := g_ExtractWadName(Res);
1649 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1650 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1652 WAD := TWADFile.Create();
1653 if not WAD.ReadFile(FileName) then
1654 begin
1655 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1656 WAD.Free();
1657 Exit;
1658 end;
1660 //k8: why loader ignores path here?
1661 mapResName := g_ExtractFileName(Res);
1662 if not WAD.GetMapResource(mapResName, Data, Len) then
1663 begin
1664 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1665 WAD.Free();
1666 Exit;
1667 end;
1669 WAD.Free();
1671 if (Len < 4) then
1672 begin
1673 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1674 FreeMem(Data);
1675 exit;
1676 end;
1678 // Çàãðóçêà êàðòû:
1679 e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
1680 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1682 stt := getTimeMicro();
1684 try
1685 mapReader := g_Map_ParseMap(Data, Len);
1686 except
1687 mapReader.Free();
1688 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1689 FreeMem(Data);
1690 Exit;
1691 end;
1693 FreeMem(Data);
1695 if (mapReader = nil) then
1696 begin
1697 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1698 exit;
1699 end;
1701 gCurrentMap := mapReader;
1703 generateExternalResourcesList(mapReader);
1704 mapTextureList := mapReader['texture'];
1705 // get all other lists here too
1706 panels := mapReader['panel'];
1707 triggers := mapReader['trigger'];
1708 items := mapReader['item'];
1709 areas := mapReader['area'];
1710 monsters := mapReader['monster'];
1712 // Çàãðóçêà îïèñàíèÿ êàðòû:
1713 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1714 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1716 with gMapInfo do
1717 begin
1718 Name := mapReader.MapName;
1719 Description := mapReader.MapDesc;
1720 Author := mapReader.MapAuthor;
1721 MusicName := mapReader.MusicName;
1722 SkyName := mapReader.SkyName;
1723 Height := mapReader.Height;
1724 Width := mapReader.Width;
1725 end;
1727 // Çàãðóçêà òåêñòóð:
1728 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1729 // Äîáàâëåíèå òåêñòóð â Textures[]:
1730 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1731 begin
1732 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1733 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1735 cnt := -1;
1736 for rec in mapTextureList do
1737 begin
1738 Inc(cnt);
1739 s := rec.Resource;
1740 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1741 e_WriteLog(Format(' Loading texture #%d: %s', [cnt, s]), MSG_NOTIFY);
1742 {$ENDIF}
1743 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1744 if rec.Anim then
1745 begin
1746 // Àíèìèðîâàííàÿ òåêñòóðà
1747 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1748 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1749 end
1750 else
1751 begin
1752 // Îáû÷íàÿ òåêñòóðà
1753 ntn := CreateTexture(rec.Resource, FileName, True);
1754 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1755 end;
1756 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1758 rec.tagInt := ntn; // remember texture number
1759 g_Game_StepLoading();
1760 end;
1762 // set panel tagInt to texture index
1763 if (panels <> nil) then
1764 begin
1765 for rec in panels do
1766 begin
1767 texrec := rec.TextureRec;
1768 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1769 end;
1770 end;
1771 end;
1773 // Çàãðóçêà òðèããåðîâ
1774 gTriggerClientID := 0;
1775 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1776 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1778 // Çàãðóçêà ïàíåëåé
1779 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1780 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1782 // check texture numbers for panels
1783 if (panels <> nil) and (panels.count > 0) then
1784 begin
1785 for rec in panels do
1786 begin
1787 if (rec.tagInt < 0) then
1788 begin
1789 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1790 result := false;
1791 gCurrentMap := nil;
1792 gCurrentMapFileName := '';
1793 exit;
1794 end;
1795 end;
1796 end;
1798 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1799 if (triggers <> nil) and (triggers.count > 0) then
1800 begin
1801 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1802 //SetLength(TriggersTable, triggers.count);
1803 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1805 SetLength(TriggersTable, triggers.count);
1806 trignum := -1;
1807 for rec in triggers do
1808 begin
1809 Inc(trignum);
1810 pttit := @TriggersTable[trignum];
1811 pttit.trigrec := rec;
1812 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1813 pttit.texPanelIdx := -1; // will be fixed later
1814 pttit.texPanel := rec.TexturePanelRec;
1815 // action panel
1816 pttit.actPanelIdx := -1;
1817 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1818 // set flag
1819 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1820 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1821 // update progress
1822 g_Game_StepLoading();
1823 end;
1824 end;
1826 // Ñîçäàåì ïàíåëè
1827 if (panels <> nil) and (panels.count > 0) then
1828 begin
1829 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1830 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1832 pannum := -1;
1833 for rec in panels do
1834 begin
1835 Inc(pannum);
1836 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1837 texrec := nil;
1838 SetLength(AddTextures, 0);
1839 CurTex := -1;
1840 ok := false;
1842 if (mapTextureList <> nil) then
1843 begin
1844 texrec := rec.TextureRec;
1845 ok := (texrec <> nil);
1846 end;
1848 if ok then
1849 begin
1850 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1851 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1852 ok := false;
1853 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1854 begin
1855 if rec.userPanelTrigRef then
1856 begin
1857 // e_LogWritefln('trigref for panel %s', [pannum]);
1858 ok := True;
1859 end;
1860 end;
1861 end;
1863 if ok then
1864 begin
1865 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1866 s := texrec.Resource;
1868 // Ñïåö-òåêñòóðû çàïðåùåíû
1869 if g_Map_IsSpecialTexture(s) then
1870 begin
1871 ok := false
1872 end
1873 else
1874 begin
1875 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1876 ok := g_Texture_NumNameFindStart(s);
1877 end;
1879 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1880 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1881 if ok then
1882 begin
1883 k := NNF_NAME_BEFORE;
1884 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1885 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1886 begin
1887 k := g_Texture_NumNameFindNext(TexName);
1889 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1890 begin
1891 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1892 if texrec.Anim then
1893 begin
1894 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1895 isAnim := True;
1896 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1897 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1898 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1899 if not ok then
1900 begin
1901 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1902 isAnim := False;
1903 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1904 ok := CreateTexture(TexName, FileName, False) >= 0;
1905 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1906 end;
1907 end
1908 else
1909 begin
1910 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1911 isAnim := False;
1912 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1913 ok := CreateTexture(TexName, FileName, False) >= 0;
1914 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1915 if not ok then
1916 begin
1917 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1918 isAnim := True;
1919 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1920 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1921 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1922 end;
1923 end;
1925 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
1926 if ok then
1927 begin
1929 for c := 0 to High(Textures) do
1930 begin
1931 if (Textures[c].TextureName = TexName) then
1932 begin
1933 SetLength(AddTextures, Length(AddTextures)+1);
1934 AddTextures[High(AddTextures)].Texture := c;
1935 AddTextures[High(AddTextures)].Anim := isAnim;
1936 break;
1937 end;
1938 end;
1940 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
1941 begin
1942 SetLength(AddTextures, Length(AddTextures)+1);
1943 AddTextures[High(AddTextures)].Texture := c;
1944 AddTextures[High(AddTextures)].Anim := isAnim;
1945 end;
1946 end;
1947 end
1948 else
1949 begin
1950 if k = NNF_NAME_EQUALS then
1951 begin
1952 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
1953 SetLength(AddTextures, Length(AddTextures)+1);
1954 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
1955 AddTextures[High(AddTextures)].Anim := texrec.Anim;
1956 CurTex := High(AddTextures);
1957 ok := true;
1958 end
1959 else // NNF_NO_NAME
1960 begin
1961 ok := false;
1962 end;
1963 end;
1964 end; // while ok...
1966 ok := true;
1967 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1968 end; // if ok - ññûëàþòñÿ òðèããåðû
1970 if not ok then
1971 begin
1972 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
1973 SetLength(AddTextures, 1);
1974 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
1975 AddTextures[0].Anim := false;
1976 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
1977 CurTex := 0;
1978 end;
1980 //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);
1982 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
1984 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
1985 PanelID := CreatePanel(rec, AddTextures, CurTex);
1986 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
1987 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
1989 // setup lifts
1990 moveSpeed := rec.moveSpeed;
1991 //moveStart := rec.moveStart;
1992 //moveEnd := rec.moveEnd;
1993 //moveActive := rec['move_active'].value;
1994 if not moveSpeed.isZero then
1995 begin
1996 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
1997 gMovingWallIds[High(gMovingWallIds)] := PanelID;
1998 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
1999 end;
2001 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2003 g_Game_StepLoading();
2004 end;
2005 end;
2007 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2008 for b := 0 to High(TriggersTable) do
2009 begin
2010 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2011 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2012 end;
2014 // create map grid, init other grids (for monsters, for example)
2015 e_WriteLog('Creating map grid', MSG_NOTIFY);
2016 mapCreateGrid();
2018 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2019 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2020 begin
2021 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2022 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2023 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2024 trignum := -1;
2025 for rec in triggers do
2026 begin
2027 Inc(trignum);
2028 if (TriggersTable[trignum].texPanel <> nil) then b := TriggersTable[trignum].texPanel.PanelType else b := 0;
2029 if (TriggersTable[trignum].actPanel <> nil) then c := TriggersTable[trignum].actPanel.PanelType else c := 0;
2030 // we can have only one of those
2031 tgpid := TriggersTable[trignum].actPanelIdx;
2032 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2033 TriggersTable[trignum].tnum := trignum;
2034 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid, Word(b), Word(c));
2035 end;
2036 end;
2038 //FIXME: use hashtable!
2039 for pan in panByGUID do
2040 begin
2041 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2042 begin
2043 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2044 end;
2045 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2046 begin
2047 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2048 end;
2049 end;
2051 // Çàãðóçêà ïðåäìåòîâ
2052 e_WriteLog(' Loading items...', MSG_NOTIFY);
2053 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2055 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2056 if (items <> nil) and not gLoadGameMode then
2057 begin
2058 e_WriteLog(' Spawning items...', MSG_NOTIFY);
2059 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2060 for rec in items do CreateItem(rec);
2061 end;
2063 // Çàãðóçêà îáëàñòåé
2064 e_WriteLog(' Loading areas...', MSG_NOTIFY);
2065 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2067 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2068 if areas <> nil then
2069 begin
2070 e_WriteLog(' Creating areas...', MSG_NOTIFY);
2071 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2072 for rec in areas do CreateArea(rec);
2073 end;
2075 // Çàãðóçêà ìîíñòðîâ
2076 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
2077 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2079 gTotalMonsters := 0;
2081 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2082 if (monsters <> nil) and not gLoadGameMode then
2083 begin
2084 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
2085 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2086 for rec in monsters do CreateMonster(rec);
2087 end;
2089 gCurrentMap := mapReader; // this will be our current map now
2090 gCurrentMapFileName := Res;
2091 mapReader := nil;
2093 // Çàãðóçêà íåáà
2094 if (gMapInfo.SkyName <> '') then
2095 begin
2096 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
2097 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2098 FileName := g_ExtractWadName(gMapInfo.SkyName);
2100 if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
2102 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2103 if g_Texture_CreateWAD(BackID, s) then
2104 begin
2105 g_Game_SetupScreenSize();
2106 end
2107 else
2108 begin
2109 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2110 end;
2111 end;
2113 // Çàãðóçêà ìóçûêè
2114 ok := False;
2115 if gMapInfo.MusicName <> '' then
2116 begin
2117 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
2118 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2119 FileName := g_ExtractWadName(gMapInfo.MusicName);
2121 if FileName <> '' then
2122 FileName := GameDir+'/wads/'+FileName
2123 else
2124 begin
2125 FileName := g_ExtractWadName(Res);
2126 end;
2128 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2129 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2130 ok := True
2131 else
2132 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2133 end;
2135 // Îñòàëüíûå óñòàíâêè
2136 CreateDoorMap();
2137 CreateLiftMap();
2139 g_Items_Init();
2140 g_Weapon_Init();
2141 g_Monsters_Init();
2143 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2144 if not gLoadGameMode then g_GFX_Init();
2146 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2147 mapTextureList := nil;
2148 panels := nil;
2149 items := nil;
2150 areas := nil;
2151 triggers := nil;
2152 TriggersTable := nil;
2153 AddTextures := nil;
2155 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2156 if ok and (not gLoadGameMode) then
2157 begin
2158 gMusic.SetByName(gMapInfo.MusicName);
2159 gMusic.Play();
2160 end
2161 else
2162 begin
2163 gMusic.SetByName('');
2164 end;
2166 stt := getTimeMicro()-stt;
2167 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2168 mapOk := true;
2169 finally
2170 sfsGCEnable(); // enable releasing unused volumes
2171 mapReader.Free();
2172 e_ClearInputBuffer(); // why not?
2173 if not mapOk then
2174 begin
2175 gCurrentMap := nil;
2176 gCurrentMapFileName := '';
2177 end;
2178 end;
2180 e_WriteLog('Done loading map.', MSG_NOTIFY);
2181 Result := True;
2182 end;
2185 function g_Map_GetMapInfo(Res: String): TMapInfo;
2186 var
2187 WAD: TWADFile;
2188 mapReader: TDynRecord;
2189 //Header: TMapHeaderRec_1;
2190 FileName: String;
2191 Data: Pointer;
2192 Len: Integer;
2193 begin
2194 FillChar(Result, SizeOf(Result), 0);
2195 FileName := g_ExtractWadName(Res);
2197 WAD := TWADFile.Create();
2198 if not WAD.ReadFile(FileName) then
2199 begin
2200 WAD.Free();
2201 Exit;
2202 end;
2204 //k8: it ignores path again
2205 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2206 begin
2207 WAD.Free();
2208 Exit;
2209 end;
2211 WAD.Free();
2213 try
2214 mapReader := g_Map_ParseMap(Data, Len);
2215 except
2216 mapReader := nil;
2217 FreeMem(Data);
2218 exit;
2219 end;
2221 FreeMem(Data);
2223 if (mapReader = nil) then exit;
2225 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2226 begin
2227 Result.Name := mapReader.MapName;
2228 Result.Description := mapReader.MapDesc;
2229 Result.Map := Res;
2230 Result.Author := mapReader.MapAuthor;
2231 Result.Height := mapReader.Height;
2232 Result.Width := mapReader.Width;
2233 end
2234 else
2235 begin
2236 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2237 //ZeroMemory(@Header, SizeOf(Header));
2238 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2239 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2240 Result.Map := Res;
2241 Result.Author := '';
2242 Result.Height := 0;
2243 Result.Width := 0;
2244 end;
2246 mapReader.Free();
2247 end;
2249 function g_Map_GetMapsList(WADName: string): SArray;
2250 var
2251 WAD: TWADFile;
2252 a: Integer;
2253 ResList: SArray;
2254 begin
2255 Result := nil;
2256 WAD := TWADFile.Create();
2257 if not WAD.ReadFile(WADName) then
2258 begin
2259 WAD.Free();
2260 Exit;
2261 end;
2262 ResList := WAD.GetMapResources();
2263 if ResList <> nil then
2264 begin
2265 for a := 0 to High(ResList) do
2266 begin
2267 SetLength(Result, Length(Result)+1);
2268 Result[High(Result)] := ResList[a];
2269 end;
2270 end;
2271 WAD.Free();
2272 end;
2274 function g_Map_Exist(Res: string): Boolean;
2275 var
2276 WAD: TWADFile;
2277 FileName, mnn: string;
2278 ResList: SArray;
2279 a: Integer;
2280 begin
2281 Result := False;
2283 FileName := addWadExtension(g_ExtractWadName(Res));
2285 WAD := TWADFile.Create;
2286 if not WAD.ReadFile(FileName) then
2287 begin
2288 WAD.Free();
2289 Exit;
2290 end;
2292 ResList := WAD.GetMapResources();
2293 WAD.Free();
2295 mnn := g_ExtractFileName(Res);
2296 if ResList <> nil then
2297 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2298 begin
2299 Result := True;
2300 Exit;
2301 end;
2302 end;
2304 procedure g_Map_Free(freeTextures: Boolean=true);
2305 var
2306 a: Integer;
2308 procedure FreePanelArray(var panels: TPanelArray);
2309 var
2310 i: Integer;
2312 begin
2313 if panels <> nil then
2314 begin
2315 for i := 0 to High(panels) do
2316 panels[i].Free();
2317 panels := nil;
2318 end;
2319 end;
2321 begin
2322 g_GFX_Free();
2323 g_Weapon_Free();
2324 g_Items_Free();
2325 g_Triggers_Free();
2326 g_Monsters_Free();
2328 RespawnPoints := nil;
2329 if FlagPoints[FLAG_RED] <> nil then
2330 begin
2331 Dispose(FlagPoints[FLAG_RED]);
2332 FlagPoints[FLAG_RED] := nil;
2333 end;
2334 if FlagPoints[FLAG_BLUE] <> nil then
2335 begin
2336 Dispose(FlagPoints[FLAG_BLUE]);
2337 FlagPoints[FLAG_BLUE] := nil;
2338 end;
2339 //DOMFlagPoints := nil;
2341 //gDOMFlags := nil;
2343 if (Length(gCurrentMapFileName) <> 0) then
2344 begin
2345 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2346 end
2347 else
2348 begin
2349 e_LogWritefln('g_Map_Free: no previous map.', []);
2350 end;
2351 if freeTextures then
2352 begin
2353 e_LogWritefln('g_Map_Free: clearing textures...', []);
2354 if (Textures <> nil) then
2355 begin
2356 for a := 0 to High(Textures) do
2357 begin
2358 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2359 begin
2360 if Textures[a].Anim then
2361 begin
2362 g_Frames_DeleteByID(Textures[a].FramesID)
2363 end
2364 else
2365 begin
2366 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2367 begin
2368 e_DeleteTexture(Textures[a].TextureID);
2369 end;
2370 end;
2371 end;
2372 end;
2373 Textures := nil;
2374 end;
2375 TextNameHash.Free();
2376 TextNameHash := nil;
2377 BadTextNameHash.Free();
2378 BadTextNameHash := nil;
2379 end;
2381 panByGUID := nil;
2383 FreePanelArray(gWalls);
2384 FreePanelArray(gRenderBackgrounds);
2385 FreePanelArray(gRenderForegrounds);
2386 FreePanelArray(gWater);
2387 FreePanelArray(gAcid1);
2388 FreePanelArray(gAcid2);
2389 FreePanelArray(gSteps);
2390 FreePanelArray(gLifts);
2391 FreePanelArray(gBlockMon);
2392 gMovingWallIds := nil;
2394 if BackID <> DWORD(-1) then
2395 begin
2396 gBackSize.X := 0;
2397 gBackSize.Y := 0;
2398 e_DeleteTexture(BackID);
2399 BackID := DWORD(-1);
2400 end;
2402 g_Game_StopAllSounds(False);
2403 gMusic.FreeSound();
2404 g_Sound_Delete(gMapInfo.MusicName);
2406 gMapInfo.Name := '';
2407 gMapInfo.Description := '';
2408 gMapInfo.MusicName := '';
2409 gMapInfo.Height := 0;
2410 gMapInfo.Width := 0;
2412 gDoorMap := nil;
2413 gLiftMap := nil;
2414 end;
2416 procedure g_Map_Update();
2417 var
2418 a, d, j: Integer;
2419 m: Word;
2420 s: String;
2422 procedure UpdatePanelArray(var panels: TPanelArray);
2423 var
2424 i: Integer;
2426 begin
2427 for i := 0 to High(panels) do panels[i].Update();
2428 end;
2430 begin
2431 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2433 UpdatePanelArray(gWalls);
2434 UpdatePanelArray(gRenderBackgrounds);
2435 UpdatePanelArray(gRenderForegrounds);
2436 UpdatePanelArray(gWater);
2437 UpdatePanelArray(gAcid1);
2438 UpdatePanelArray(gAcid2);
2439 UpdatePanelArray(gSteps);
2441 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2443 if gGameSettings.GameMode = GM_CTF then
2444 begin
2445 for a := FLAG_RED to FLAG_BLUE do
2446 begin
2447 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2448 begin
2449 with gFlags[a] do
2450 begin
2451 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2453 m := g_Obj_Move(@Obj, True, True);
2455 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2457 // Ñîïðîòèâëåíèå âîçäóõà
2458 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2460 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2461 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2462 begin
2463 g_Map_ResetFlag(a);
2464 gFlags[a].CaptureTime := 0;
2465 if a = FLAG_RED then
2466 s := _lc[I_PLAYER_FLAG_RED]
2467 else
2468 s := _lc[I_PLAYER_FLAG_BLUE];
2469 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2471 if g_Game_IsNet then
2472 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2473 Continue;
2474 end;
2476 if Count > 0 then Count -= 1;
2478 // Èãðîê áåðåò ôëàã
2479 if gPlayers <> nil then
2480 begin
2481 j := Random(Length(gPlayers)) - 1;
2482 for d := 0 to High(gPlayers) do
2483 begin
2484 Inc(j);
2485 if j > High(gPlayers) then j := 0;
2486 if gPlayers[j] <> nil then
2487 begin
2488 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2489 begin
2490 if gPlayers[j].GetFlag(a) then Break;
2491 end;
2492 end;
2493 end;
2494 end;
2495 end;
2496 end;
2497 end;
2498 end;
2499 end;
2502 // old algo
2503 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2505 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2506 var
2507 idx: Integer;
2508 begin
2509 if (panels <> nil) then
2510 begin
2511 // alas, no visible set
2512 for idx := 0 to High(panels) do
2513 begin
2514 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2515 end;
2516 end;
2517 end;
2519 begin
2520 case PanelType of
2521 PANEL_WALL: DrawPanels(gWalls);
2522 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2523 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2524 PANEL_FORE: DrawPanels(gRenderForegrounds);
2525 PANEL_WATER: DrawPanels(gWater);
2526 PANEL_ACID1: DrawPanels(gAcid1);
2527 PANEL_ACID2: DrawPanels(gAcid2);
2528 PANEL_STEP: DrawPanels(gSteps);
2529 end;
2530 end;
2533 // new algo
2534 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2536 function checker (pan: TPanel; tag: Integer): Boolean;
2537 begin
2538 result := false; // don't stop, ever
2539 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2540 gDrawPanelList.insert(pan);
2541 end;
2543 begin
2544 dplClear();
2545 //tagmask := panelTypeToTag(PanelType);
2546 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask);
2547 // list will be rendered in `g_game.DrawPlayer()`
2548 end;
2551 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2553 function checker (pan: TPanel; tag: Integer): Boolean;
2554 begin
2555 result := false; // don't stop, ever
2556 pan.DrawShadowVolume(lightX, lightY, radius);
2557 end;
2559 begin
2560 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2561 end;
2564 procedure g_Map_DrawBack(dx, dy: Integer);
2565 begin
2566 if gDrawBackGround and (BackID <> DWORD(-1)) then
2567 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2568 else
2569 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2570 end;
2572 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2573 PanelType: Word; b1x3: Boolean=false): Boolean;
2574 var
2575 a, h: Integer;
2576 begin
2577 Result := False;
2579 if WordBool(PanelType and PANEL_WALL) then
2580 if gWalls <> nil then
2581 begin
2582 h := High(gWalls);
2584 for a := 0 to h do
2585 if gWalls[a].Enabled and
2586 g_Collide(X, Y, Width, Height,
2587 gWalls[a].X, gWalls[a].Y,
2588 gWalls[a].Width, gWalls[a].Height) then
2589 begin
2590 Result := True;
2591 Exit;
2592 end;
2593 end;
2595 if WordBool(PanelType and PANEL_WATER) then
2596 if gWater <> nil then
2597 begin
2598 h := High(gWater);
2600 for a := 0 to h do
2601 if g_Collide(X, Y, Width, Height,
2602 gWater[a].X, gWater[a].Y,
2603 gWater[a].Width, gWater[a].Height) then
2604 begin
2605 Result := True;
2606 Exit;
2607 end;
2608 end;
2610 if WordBool(PanelType and PANEL_ACID1) then
2611 if gAcid1 <> nil then
2612 begin
2613 h := High(gAcid1);
2615 for a := 0 to h do
2616 if g_Collide(X, Y, Width, Height,
2617 gAcid1[a].X, gAcid1[a].Y,
2618 gAcid1[a].Width, gAcid1[a].Height) then
2619 begin
2620 Result := True;
2621 Exit;
2622 end;
2623 end;
2625 if WordBool(PanelType and PANEL_ACID2) then
2626 if gAcid2 <> nil then
2627 begin
2628 h := High(gAcid2);
2630 for a := 0 to h do
2631 if g_Collide(X, Y, Width, Height,
2632 gAcid2[a].X, gAcid2[a].Y,
2633 gAcid2[a].Width, gAcid2[a].Height) then
2634 begin
2635 Result := True;
2636 Exit;
2637 end;
2638 end;
2640 if WordBool(PanelType and PANEL_STEP) then
2641 if gSteps <> nil then
2642 begin
2643 h := High(gSteps);
2645 for a := 0 to h do
2646 if g_Collide(X, Y, Width, Height,
2647 gSteps[a].X, gSteps[a].Y,
2648 gSteps[a].Width, gSteps[a].Height) then
2649 begin
2650 Result := True;
2651 Exit;
2652 end;
2653 end;
2655 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2656 if gLifts <> nil then
2657 begin
2658 h := High(gLifts);
2660 for a := 0 to h do
2661 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2662 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2663 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2664 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2665 g_Collide(X, Y, Width, Height,
2666 gLifts[a].X, gLifts[a].Y,
2667 gLifts[a].Width, gLifts[a].Height) then
2668 begin
2669 Result := True;
2670 Exit;
2671 end;
2672 end;
2674 if WordBool(PanelType and PANEL_BLOCKMON) then
2675 if gBlockMon <> nil then
2676 begin
2677 h := High(gBlockMon);
2679 for a := 0 to h do
2680 if ( (not b1x3) or
2681 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2682 g_Collide(X, Y, Width, Height,
2683 gBlockMon[a].X, gBlockMon[a].Y,
2684 gBlockMon[a].Width, gBlockMon[a].Height) then
2685 begin
2686 Result := True;
2687 Exit;
2688 end;
2689 end;
2690 end;
2692 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2693 var
2694 texid: DWORD;
2696 function checkPanels (constref panels: TPanelArray): Boolean;
2697 var
2698 a: Integer;
2699 begin
2700 result := false;
2701 if panels = nil then exit;
2702 for a := 0 to High(panels) do
2703 begin
2704 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2705 begin
2706 result := true;
2707 texid := panels[a].GetTextureID();
2708 exit;
2709 end;
2710 end;
2711 end;
2713 begin
2714 texid := LongWord(TEXTURE_NONE);
2715 result := texid;
2716 if not checkPanels(gWater) then
2717 if not checkPanels(gAcid1) then
2718 if not checkPanels(gAcid2) then exit;
2719 result := texid;
2720 end;
2723 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2724 const
2725 SlowMask = GridTagLift or GridTagBlockMon;
2726 function checker (pan: TPanel; tag: Integer): Boolean;
2727 begin
2729 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2730 begin
2731 result := pan.Enabled;
2732 exit;
2733 end;
2736 if ((tag and GridTagLift) <> 0) then
2737 begin
2738 result :=
2739 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2740 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2741 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2742 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2743 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2744 exit;
2745 end;
2747 if ((tag and GridTagBlockMon) <> 0) then
2748 begin
2749 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2750 exit;
2751 end;
2753 // other shit
2754 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2755 result := true; // i found her!
2756 end;
2758 var
2759 tagmask: Integer = 0;
2760 begin
2761 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2762 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2763 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2764 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2765 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2766 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2767 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2769 if (tagmask = 0) then begin result := false; exit; end; // just in case
2771 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2772 if gdbg_map_use_accel_coldet then
2773 begin
2774 if (Width = 1) and (Height = 1) then
2775 begin
2776 if ((tagmask and SlowMask) <> 0) then
2777 begin
2778 // slow
2779 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2780 end
2781 else
2782 begin
2783 // fast
2784 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2785 end;
2786 end
2787 else
2788 begin
2789 if ((tagmask and SlowMask) <> 0) then
2790 begin
2791 // slow
2792 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2793 end
2794 else
2795 begin
2796 // fast
2797 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2798 end;
2799 end;
2800 end
2801 else
2802 begin
2803 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2804 end;
2805 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2806 end;
2809 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2810 var
2811 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2812 texid: DWORD;
2814 // slightly different from the old code, but meh...
2815 function checker (pan: TPanel; tag: Integer): Boolean;
2816 begin
2817 result := false; // don't stop, ever
2818 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2819 // check priorities
2820 case cctype of
2821 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2822 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2823 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2824 end;
2825 // collision?
2826 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2827 // yeah
2828 texid := pan.GetTextureID();
2829 // water? water has the highest priority, so stop right here
2830 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2831 // acid2?
2832 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2833 // acid1?
2834 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2835 end;
2837 begin
2838 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2839 if gdbg_map_use_accel_coldet then
2840 begin
2841 texid := LongWord(TEXTURE_NONE);
2842 if (Width = 1) and (Height = 1) then
2843 begin
2844 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2845 end
2846 else
2847 begin
2848 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2849 end;
2850 result := texid;
2851 end
2852 else
2853 begin
2854 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2855 end;
2856 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2857 end;
2860 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2861 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2862 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2865 procedure g_Map_EnableWallGUID (pguid: Integer);
2866 var
2867 pan: TPanel;
2868 begin
2869 //pan := gWalls[ID];
2870 pan := g_Map_PanelByGUID(pguid);
2871 if (pan = nil) then exit;
2872 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2874 pan.Enabled := True;
2875 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2877 mapGrid.proxyEnabled[pan.proxyId] := true;
2878 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2879 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2881 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2882 // mark platform as interesting
2883 pan.setDirty();
2885 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2886 //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);
2887 {$ENDIF}
2888 end;
2891 procedure g_Map_DisableWallGUID (pguid: Integer);
2892 var
2893 pan: TPanel;
2894 begin
2895 //pan := gWalls[ID];
2896 pan := g_Map_PanelByGUID(pguid);
2897 if (pan = nil) then exit;
2898 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2900 pan.Enabled := False;
2901 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2903 mapGrid.proxyEnabled[pan.proxyId] := false;
2904 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2906 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2907 // mark platform as interesting
2908 pan.setDirty();
2910 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2911 //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);
2912 {$ENDIF}
2913 end;
2916 procedure g_Map_SwitchTextureGUID (PanelType: Word; pguid: Integer; AnimLoop: Byte = 0);
2917 var
2918 tp: TPanel;
2919 begin
2920 tp := g_Map_PanelByGUID(pguid);
2921 if (tp = nil) then exit;
2923 case PanelType of
2924 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: tp := gWalls[ID];
2925 PANEL_FORE: tp := gRenderForegrounds[ID];
2926 PANEL_BACK: tp := gRenderBackgrounds[ID];
2927 PANEL_WATER: tp := gWater[ID];
2928 PANEL_ACID1: tp := gAcid1[ID];
2929 PANEL_ACID2: tp := gAcid2[ID];
2930 PANEL_STEP: tp := gSteps[ID];
2931 else exit;
2932 end;
2935 tp.NextTexture(AnimLoop);
2936 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
2937 end;
2940 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2941 var
2942 pan: TPanel;
2943 begin
2944 //pan := gLifts[ID];
2945 pan := g_Map_PanelByGUID(pguid);
2946 if (pan = nil) then exit;
2947 if not pan.isGLift then exit;
2949 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2951 with {gLifts[ID]} pan do
2952 begin
2953 LiftType := t;
2955 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2956 //TODO: make separate lift tags, and change tag here
2958 case LiftType of
2959 0: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2960 1: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2961 2: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2962 3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2963 end;
2965 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2966 // mark platform as interesting
2967 pan.setDirty();
2968 end;
2969 end;
2972 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2973 var
2974 a: Integer;
2975 PointsArray: Array of TRespawnPoint;
2976 begin
2977 Result := False;
2978 SetLength(PointsArray, 0);
2980 if RespawnPoints = nil then
2981 Exit;
2983 for a := 0 to High(RespawnPoints) do
2984 if RespawnPoints[a].PointType = PointType then
2985 begin
2986 SetLength(PointsArray, Length(PointsArray)+1);
2987 PointsArray[High(PointsArray)] := RespawnPoints[a];
2988 end;
2990 if PointsArray = nil then
2991 Exit;
2993 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2994 Result := True;
2995 end;
2997 function g_Map_GetPointCount(PointType: Byte): Word;
2998 var
2999 a: Integer;
3000 begin
3001 Result := 0;
3003 if RespawnPoints = nil then
3004 Exit;
3006 for a := 0 to High(RespawnPoints) do
3007 if RespawnPoints[a].PointType = PointType then
3008 Result := Result + 1;
3009 end;
3011 function g_Map_HaveFlagPoints(): Boolean;
3012 begin
3013 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3014 end;
3016 procedure g_Map_ResetFlag(Flag: Byte);
3017 begin
3018 with gFlags[Flag] do
3019 begin
3020 Obj.X := -1000;
3021 Obj.Y := -1000;
3022 Obj.Vel.X := 0;
3023 Obj.Vel.Y := 0;
3024 Direction := D_LEFT;
3025 State := FLAG_STATE_NONE;
3026 if FlagPoints[Flag] <> nil then
3027 begin
3028 Obj.X := FlagPoints[Flag]^.X;
3029 Obj.Y := FlagPoints[Flag]^.Y;
3030 Direction := FlagPoints[Flag]^.Direction;
3031 State := FLAG_STATE_NORMAL;
3032 end;
3033 Count := -1;
3034 end;
3035 end;
3037 procedure g_Map_DrawFlags();
3038 var
3039 i, dx: Integer;
3040 Mirror: TMirrorType;
3041 begin
3042 if gGameSettings.GameMode <> GM_CTF then
3043 Exit;
3045 for i := FLAG_RED to FLAG_BLUE do
3046 with gFlags[i] do
3047 if State <> FLAG_STATE_CAPTURED then
3048 begin
3049 if State = FLAG_STATE_NONE then
3050 continue;
3052 if Direction = D_LEFT then
3053 begin
3054 Mirror := M_HORIZONTAL;
3055 dx := -1;
3056 end
3057 else
3058 begin
3059 Mirror := M_NONE;
3060 dx := 1;
3061 end;
3063 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3065 if g_debug_Frames then
3066 begin
3067 e_DrawQuad(Obj.X+Obj.Rect.X,
3068 Obj.Y+Obj.Rect.Y,
3069 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3070 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3071 0, 255, 0);
3072 end;
3073 end;
3074 end;
3077 procedure g_Map_SaveState (var Mem: TBinMemoryWriter);
3078 var
3079 dw: DWORD;
3080 b: Byte;
3081 str: String;
3082 boo: Boolean;
3084 procedure savePanels ();
3085 var
3086 PAMem: TBinMemoryWriter;
3087 pan: TPanel;
3088 begin
3089 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé
3090 PAMem := TBinMemoryWriter.Create((Length(panByGUID)+1) * 40);
3092 // Ñîõðàíÿåì ïàíåëè
3093 //Mem.WriteInt(Length(panByGUID));
3094 for pan in panByGUID do pan.SaveState(PAMem);
3096 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé
3097 PAMem.SaveToMemory(Mem);
3098 PAMem.Free();
3099 end;
3101 procedure SaveFlag (flag: PFlag);
3102 begin
3103 // Ñèãíàòóðà ôëàãà
3104 dw := FLAG_SIGNATURE; // 'FLAG'
3105 Mem.WriteDWORD(dw);
3106 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3107 Mem.WriteByte(flag^.RespawnType);
3108 // Ñîñòîÿíèå ôëàãà
3109 Mem.WriteByte(flag^.State);
3110 // Íàïðàâëåíèå ôëàãà
3111 if flag^.Direction = D_LEFT then b := 1 else b := 2; // D_RIGHT
3112 Mem.WriteByte(b);
3113 // Îáúåêò ôëàãà
3114 Obj_SaveState(@flag^.Obj, Mem);
3115 end;
3117 begin
3118 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
3120 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
3121 savePanels();
3122 ///// /////
3124 ///// Ñîõðàíÿåì ìóçûêó: /////
3125 // Ñèãíàòóðà ìóçûêè:
3126 dw := MUSIC_SIGNATURE; // 'MUSI'
3127 Mem.WriteDWORD(dw);
3128 // Íàçâàíèå ìóçûêè:
3129 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3130 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3131 Mem.WriteString(str, 64);
3132 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3133 dw := gMusic.GetPosition();
3134 Mem.WriteDWORD(dw);
3135 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3136 boo := gMusic.SpecPause;
3137 Mem.WriteBoolean(boo);
3138 ///// /////
3140 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3141 Mem.WriteInt(gTotalMonsters);
3142 ///// /////
3144 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3145 if gGameSettings.GameMode = GM_CTF then
3146 begin
3147 // Ôëàã Êðàñíîé êîìàíäû
3148 SaveFlag(@gFlags[FLAG_RED]);
3149 // Ôëàã Ñèíåé êîìàíäû
3150 SaveFlag(@gFlags[FLAG_BLUE]);
3151 end;
3152 ///// /////
3154 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3155 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3156 begin
3157 // Î÷êè Êðàñíîé êîìàíäû
3158 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
3159 // Î÷êè Ñèíåé êîìàíäû
3160 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
3161 end;
3162 ///// /////
3163 end;
3166 procedure g_Map_LoadState (var Mem: TBinMemoryReader);
3167 var
3168 dw: DWORD;
3169 b: Byte;
3170 str: String;
3171 boo: Boolean;
3173 procedure loadPanels ();
3174 var
3175 PAMem: TBinMemoryReader;
3176 pan: TPanel;
3177 //count: LongInt;
3178 begin
3179 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé
3180 PAMem := TBinMemoryReader.Create();
3181 PAMem.LoadFromMemory(Mem);
3183 // Çàãðóæàåì ïàíåëè
3184 //PAMem.ReadInt(count);
3185 //if (count <> Length(panByGUID)) then raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: invalid number of panels');
3186 //if (count <> Length(panByGUID)) then raise EBinSizeError.Create(Format('g_Map_LoadState: LoadPanelArray: invalid number of panels (%d : %d)', [count, Length(panByGUID)]));
3187 for pan in panByGUID do
3188 begin
3189 pan.LoadState(PAMem);
3190 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3191 end;
3193 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí
3194 PAMem.Free();
3195 end;
3197 procedure LoadFlag(flag: PFlag);
3198 begin
3199 // Ñèãíàòóðà ôëàãà
3200 Mem.ReadDWORD(dw);
3201 // 'FLAG'
3202 if dw <> FLAG_SIGNATURE then raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
3203 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3204 Mem.ReadByte(flag^.RespawnType);
3205 // Ñîñòîÿíèå ôëàãà
3206 Mem.ReadByte(flag^.State);
3207 // Íàïðàâëåíèå ôëàãà
3208 Mem.ReadByte(b);
3209 if b = 1 then flag^.Direction := D_LEFT else flag^.Direction := D_RIGHT; // b = 2
3210 // Îáúåêò ôëàãà
3211 Obj_LoadState(@flag^.Obj, Mem);
3212 end;
3214 begin
3215 if Mem = nil then Exit;
3217 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3218 loadPanels();
3219 ///// /////
3221 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3222 g_GFX_Init();
3223 //mapCreateGrid();
3225 ///// Çàãðóæàåì ìóçûêó: /////
3226 // Ñèãíàòóðà ìóçûêè
3227 Mem.ReadDWORD(dw);
3228 // 'MUSI'
3229 if dw <> MUSIC_SIGNATURE then raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
3230 // Íàçâàíèå ìóçûêè
3231 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3232 Mem.ReadString(str);
3233 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3234 Mem.ReadDWORD(dw);
3235 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3236 Mem.ReadBoolean(boo);
3237 // Çàïóñêàåì ýòó ìóçûêó
3238 gMusic.SetByName(str);
3239 gMusic.SpecPause := boo;
3240 gMusic.Play();
3241 gMusic.Pause(True);
3242 gMusic.SetPosition(dw);
3243 ///// /////
3245 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3246 Mem.ReadInt(gTotalMonsters);
3247 ///// /////
3249 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3250 if gGameSettings.GameMode = GM_CTF then
3251 begin
3252 // Ôëàã Êðàñíîé êîìàíäû
3253 LoadFlag(@gFlags[FLAG_RED]);
3254 // Ôëàã Ñèíåé êîìàíäû
3255 LoadFlag(@gFlags[FLAG_BLUE]);
3256 end;
3257 ///// /////
3259 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3260 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3261 begin
3262 // Î÷êè Êðàñíîé êîìàíäû
3263 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
3264 // Î÷êè Ñèíåé êîìàíäû
3265 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
3266 end;
3267 ///// /////
3268 end;
3271 // trace liquid, stepping by `dx` and `dy`
3272 // return last seen liquid coords, and `false` if we're started outside of the liquid
3273 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3274 const
3275 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3276 begin
3277 topx := x;
3278 topy := y;
3279 // started outside of the liquid?
3280 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3281 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3282 result := true;
3283 while true do
3284 begin
3285 Inc(x, dx);
3286 Inc(y, dy);
3287 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3288 topx := x;
3289 topy := y;
3290 end;
3291 end;
3294 end.