DEADSOFTWARE

render: completely remove opengl calls form game code
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
17 unit g_map;
19 interface
21 uses
22 SysUtils, Classes, mempool,
23 g_base, r_graphics, g_basic, MAPDEF, g_textures,
24 g_phys, utils, 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): SSArray;
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 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
69 PanelType: Word; b1x3: Boolean=false): Boolean;
70 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
72 procedure g_Map_EnableWallGUID (pguid: Integer);
73 procedure g_Map_DisableWallGUID (pguid: Integer);
74 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
76 // HACK!!!
77 procedure g_Map_EnableWall_XXX (ID: DWORD);
78 procedure g_Map_DisableWall_XXX (ID: DWORD);
79 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
81 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
83 procedure g_Map_ReAdd_DieTriggers();
84 function g_Map_IsSpecialTexture(Texture: String): Boolean;
86 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
87 function g_Map_GetPointCount(PointType: Byte): Word;
88 function g_Map_GetRandomPointType(): Byte;
90 function g_Map_HaveFlagPoints(): Boolean;
92 procedure g_Map_ResetFlag(Flag: Byte);
94 procedure g_Map_SaveState (st: TStream);
95 procedure g_Map_LoadState (st: TStream);
97 // returns panel or nil
98 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
99 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
101 // returns panel or nil
102 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
103 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
105 type
106 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
108 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
109 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
111 // trace liquid, stepping by `dx` and `dy`
112 // return last seen liquid coords, and `false` if we're started outside of the liquid
113 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
116 // return `true` from `cb` to stop
117 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
119 procedure g_Map_NetSendInterestingPanels (); // yay!
122 procedure g_Map_ProfilersBegin ();
123 procedure g_Map_ProfilersEnd ();
126 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
129 function g_Map_MinX (): Integer; inline;
130 function g_Map_MinY (): Integer; inline;
131 function g_Map_MaxX (): Integer; inline;
132 function g_Map_MaxY (): Integer; inline;
134 const
135 NNF_NO_NAME = 0;
136 NNF_NAME_BEFORE = 1;
137 NNF_NAME_EQUALS = 2;
138 NNF_NAME_AFTER = 3;
140 function g_Texture_NumNameFindStart(name: String): Boolean;
141 function g_Texture_NumNameFindNext(var newName: String): Byte;
143 const
144 RESPAWNPOINT_PLAYER1 = 1;
145 RESPAWNPOINT_PLAYER2 = 2;
146 RESPAWNPOINT_DM = 3;
147 RESPAWNPOINT_RED = 4;
148 RESPAWNPOINT_BLUE = 5;
150 FLAG_NONE = 0;
151 FLAG_RED = 1;
152 FLAG_BLUE = 2;
153 FLAG_DOM = 3;
155 FLAG_STATE_NONE = 0;
156 FLAG_STATE_NORMAL = 1;
157 FLAG_STATE_DROPPED = 2;
158 FLAG_STATE_CAPTURED = 3;
159 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
160 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
162 FLAG_TIME = 720; // 20 seconds
164 SKY_STRETCH: Single = 1.5;
166 const
167 GridTagInvalid = 0;
169 (* draw order:
170 PANEL_BACK
171 PANEL_STEP
172 PANEL_WALL
173 PANEL_CLOSEDOOR
174 PANEL_ACID1
175 PANEL_ACID2
176 PANEL_WATER
177 PANEL_FORE
178 *)
179 // sorted by draw priority
180 GridTagBack = 1 shl 0; // gRenderBackgrounds
181 GridTagStep = 1 shl 1; // gSteps
182 GridTagWall = 1 shl 2; // gWalls
183 GridTagDoor = 1 shl 3; // gWalls
184 GridTagAcid1 = 1 shl 4; // gAcid1
185 GridTagAcid2 = 1 shl 5; // gAcid2
186 GridTagWater = 1 shl 6; // gWater
187 GridTagFore = 1 shl 7; // gRenderForegrounds
188 // the following are invisible
189 GridTagLift = 1 shl 8; // gLifts
190 GridTagBlockMon = 1 shl 9; // gBlockMon
192 GridTagSolid = (GridTagWall or GridTagDoor);
193 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
194 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
196 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
199 type
200 TBinHeapPanelDrawCmp = class
201 public
202 class function less (const a, b: TPanel): Boolean; inline;
203 end;
205 TBinHeapPanelDraw = specialize TBinaryHeapBase<TPanel, TBinHeapPanelDrawCmp>;
207 var
208 gWalls: TPanelArray;
209 gRenderBackgrounds: TPanelArray;
210 gRenderForegrounds: TPanelArray;
211 gWater, gAcid1, gAcid2: TPanelArray;
212 gSteps: TPanelArray;
213 gLifts: TPanelArray;
214 gBlockMon: TPanelArray;
215 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
216 //gDOMFlags: array of TFlag;
217 gMapInfo: TMapInfo;
218 gBackSize: TDFPoint;
219 gDoorMap: array of array of DWORD;
220 gLiftMap: array of array of DWORD;
221 gWADHash: TMD5Digest;
222 BackID: DWORD = DWORD(-1);
223 gExternalResources: array of TDiskFileInfo = nil;
224 gMovingWallIds: array of Integer = nil;
226 gdbg_map_use_accel_render: Boolean = true;
227 gdbg_map_use_accel_coldet: Boolean = true;
228 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
229 gDrawPanelList: TBinHeapPanelDraw = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
231 gCurrentMap: TDynRecord = nil;
232 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
233 gTestMap: String = '';
236 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
239 type
240 TPanelGrid = specialize TBodyGridBase<TPanel>;
242 var
243 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
246 implementation
248 uses
249 e_input, g_main, e_log, e_texture, e_res, g_items, g_gfx, g_console,
250 g_weapons, g_game, g_sound, e_sound, CONFIG,
251 g_options, g_triggers, g_player,
252 Math, g_monsters, g_saveload, g_language, g_netmsg,
253 sfs, xstreams, hashtable, wadreader,
254 ImagingTypes, Imaging, ImagingUtility,
255 ImagingGif, ImagingNetworkGraphics,
256 g_res_downloader;
258 const
259 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
260 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
261 FLAG_SIGNATURE = $47414C46; // 'FLAG'
264 // ////////////////////////////////////////////////////////////////////////// //
265 procedure mapWarningCB (const msg: AnsiString; line, col: Integer);
266 begin
267 if (line > 0) then
268 begin
269 e_LogWritefln('parse error at (%s,%s): %s', [line, col, msg], TMsgType.Warning);
270 end
271 else
272 begin
273 e_LogWritefln('parse error: %s', [msg], TMsgType.Warning);
274 end;
275 end;
278 // ////////////////////////////////////////////////////////////////////////// //
279 var
280 panByGUID: array of TPanel = nil;
283 // ////////////////////////////////////////////////////////////////////////// //
284 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
285 begin
286 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
287 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
288 end;
291 // return `true` from `cb` to stop
292 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
293 var
294 pan: TPanel;
295 begin
296 result := nil;
297 if not assigned(cb) then exit;
298 for pan in panByGUID do
299 begin
300 if cb(pan) then begin result := pan; exit; end;
301 end;
302 end;
305 procedure g_Map_NetSendInterestingPanels ();
306 var
307 pan: TPanel;
308 begin
309 if g_Game_IsServer and g_Game_IsNet then
310 begin
311 for pan in panByGUID do
312 begin
313 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
314 end;
315 end;
316 end;
319 // ////////////////////////////////////////////////////////////////////////// //
320 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
321 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
322 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
323 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
326 // ////////////////////////////////////////////////////////////////////////// //
327 var
328 dfmapdef: TDynMapDef = nil;
331 procedure loadMapDefinition ();
332 var
333 pr: TTextParser = nil;
334 st: TStream = nil;
335 WAD: TWADFile = nil;
336 begin
337 if (dfmapdef <> nil) then exit;
339 try
340 e_LogWritefln('parsing "mapdef.txt"...', []);
341 st := e_OpenResourceRO(DataDirs, 'mapdef.txt');
342 e_LogWritefln('found local "mapdef.txt"', []);
343 except
344 st := nil;
345 end;
347 if (st = nil) then
348 begin
349 WAD := TWADFile.Create();
350 if not WAD.ReadFile(GameWAD) then
351 begin
352 //raise Exception.Create('cannot load "game.wad"');
353 st := nil;
354 end
355 else
356 begin
357 st := WAD.openFileStream('mapdef.txt');
358 end;
359 end;
361 try
362 if (st = nil) then
363 begin
364 //raise Exception.Create('cannot open "mapdef.txt"');
365 e_LogWriteln('using default "mapdef.txt"...');
366 pr := TStrTextParser.Create(defaultMapDef);
367 end
368 else
369 begin
370 pr := TFileTextParser.Create(st);
371 end;
372 except on e: Exception do
373 begin
374 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
375 raise;
376 end;
377 end;
379 try
380 dfmapdef := TDynMapDef.Create(pr);
381 except
382 on e: TDynParseException do
383 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
384 on e: Exception do
385 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
386 end;
388 st.Free();
389 WAD.Free();
390 end;
393 // ////////////////////////////////////////////////////////////////////////// //
394 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
395 var
396 wst: TSFSMemoryChunkStream = nil;
397 begin
398 result := nil;
399 if (dataLen < 4) then exit;
401 if (dfmapdef = nil) then writeln('need to load mapdef');
402 loadMapDefinition();
403 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
405 wst := TSFSMemoryChunkStream.Create(data, dataLen);
406 try
407 result := dfmapdef.parseMap(wst);
408 except
409 on e: TDynParseException do
410 begin
411 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
412 wst.Free();
413 result := nil;
414 exit;
415 end;
416 on e: Exception do
417 begin
418 e_LogWritefln('ERROR: %s', [e.message]);
419 wst.Free();
420 result := nil;
421 exit;
422 end;
423 end;
425 //e_LogWriteln('map parsed.');
426 end;
429 // ////////////////////////////////////////////////////////////////////////// //
430 var
431 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
432 NNF_PureExt: String; // extension postfix
433 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
434 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
437 function g_Texture_NumNameFindStart(name: String): Boolean;
438 var
439 i, j: Integer;
441 begin
442 Result := False;
443 NNF_PureName := '';
444 NNF_PureExt := '';
445 NNF_FirstNum := -1;
446 NNF_CurrentNum := -1;
448 for i := Length(name) downto 1 do
449 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
450 begin
451 if i = Length(name) then
452 begin // Íåò öèôð â êîíöå ñòðîêè
453 Exit;
454 end
455 else
456 begin
457 j := i + 1;
458 while (j <= Length(name)) and (name[j] <> '.') do inc(j);
459 NNF_PureName := Copy(name, 1, i);
460 NNF_PureExt := Copy(name, j);
461 name := Copy(name, i + 1, j - i - 1);
462 Break;
463 end;
464 end;
466 // Íå ïåðåâåñòè â ÷èñëî:
467 if not TryStrToInt(name, NNF_FirstNum) then
468 Exit;
470 NNF_CurrentNum := 0;
472 Result := True;
473 end;
476 function g_Texture_NumNameFindNext(var newName: String): Byte;
477 begin
478 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
479 begin
480 newName := '';
481 Result := NNF_NO_NAME;
482 Exit;
483 end;
485 newName := NNF_PureName + IntToStr(NNF_CurrentNum) + NNF_PureExt;
487 if NNF_CurrentNum < NNF_FirstNum then
488 Result := NNF_NAME_BEFORE
489 else
490 if NNF_CurrentNum > NNF_FirstNum then
491 Result := NNF_NAME_AFTER
492 else
493 Result := NNF_NAME_EQUALS;
495 Inc(NNF_CurrentNum);
496 end;
499 // ////////////////////////////////////////////////////////////////////////// //
500 function panelTypeToTag (panelType: Word): Integer;
501 begin
502 case panelType of
503 PANEL_WALL: result := GridTagWall; // gWalls
504 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
505 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
506 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
507 PANEL_WATER: result := GridTagWater; // gWater
508 PANEL_ACID1: result := GridTagAcid1; // gAcid1
509 PANEL_ACID2: result := GridTagAcid2; // gAcid2
510 PANEL_STEP: result := GridTagStep; // gSteps
511 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
512 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
513 else result := GridTagInvalid;
514 end;
515 end;
518 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
519 begin
520 if (a.tag < b.tag) then begin result := true; exit; end;
521 if (a.tag > b.tag) then begin result := false; exit; end;
522 result := (a.arrIdx < b.arrIdx);
523 end;
525 var
526 Textures: TLevelTextureArray = nil;
527 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
528 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
529 RespawnPoints: array of TRespawnPoint;
530 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
531 //DOMFlagPoints: Array of TFlagPoint;
534 procedure g_Map_ProfilersBegin ();
535 begin
536 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
537 if (profMapCollision <> nil) then profMapCollision.mainBegin(g_profile_collision);
538 // create sections
539 if g_profile_collision and (profMapCollision <> nil) then
540 begin
541 profMapCollision.sectionBegin('*solids');
542 profMapCollision.sectionEnd();
543 profMapCollision.sectionBegin('liquids');
544 profMapCollision.sectionEnd();
545 end;
546 end;
548 procedure g_Map_ProfilersEnd ();
549 begin
550 if (profMapCollision <> nil) then profMapCollision.mainEnd();
551 end;
554 // wall index in `gWalls` or -1
555 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
556 var
557 ex, ey: Integer;
558 begin
559 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, (GridTagWall or GridTagDoor));
560 if (result <> nil) then
561 begin
562 if (hitx <> nil) then hitx^ := ex;
563 if (hity <> nil) then hity^ := ey;
564 end
565 else
566 begin
567 if (hitx <> nil) then hitx^ := x1;
568 if (hity <> nil) then hity^ := y1;
569 end;
570 end;
572 // returns panel or nil
573 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
574 var
575 ex, ey: Integer;
576 begin
577 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, tag);
578 if (result <> nil) then
579 begin
580 if (hitx <> nil) then hitx^ := ex;
581 if (hity <> nil) then hity^ := ey;
582 end
583 else
584 begin
585 if (hitx <> nil) then hitx^ := x1;
586 if (hity <> nil) then hity^ := y1;
587 end;
588 end;
591 function xxPanAtPointChecker (pan: TPanel; panelType: Word): Boolean; inline;
592 begin
593 if ((pan.tag and GridTagLift) <> 0) then
594 begin
595 // stop if the lift of the right type
596 result :=
597 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
598 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
599 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
600 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT)));
601 exit;
602 end;
603 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
604 end;
606 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
607 var
608 tagmask: Integer = 0;
609 mwit: PPanel;
610 it: TPanelGrid.Iter;
611 begin
612 result := false;
614 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
615 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
616 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
617 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
618 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
619 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
620 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
622 if (tagmask = 0) then exit;// just in case
624 if ((tagmask and GridTagLift) <> 0) then
625 begin
626 // slow
627 it := mapGrid.forEachAtPoint(x, y, tagmask);
628 for mwit in it do if (xxPanAtPointChecker(mwit^, PanelType)) then begin result := true; break; end;
629 end
630 else
631 begin
632 // fast
633 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true);
634 result := (it.length <> 0); // firsthit
635 end;
636 it.release();
637 end;
640 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
641 var
642 it: TPanelGrid.Iter;
643 begin
644 result := nil;
645 if (tagmask = 0) then exit;
646 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true); // firsthit
647 if (it.length <> 0) then result := it.first^;
648 it.release();
649 end;
652 function g_Map_IsSpecialTexture(Texture: String): Boolean;
653 begin
654 Result := (Texture = TEXTURE_NAME_WATER) or
655 (Texture = TEXTURE_NAME_ACID1) or
656 (Texture = TEXTURE_NAME_ACID2);
657 end;
659 procedure CreateDoorMap();
660 var
661 PanelArray: Array of record
662 X, Y: Integer;
663 Width, Height: Word;
664 Active: Boolean;
665 PanelID: DWORD;
666 end;
667 a, b, c, m, i, len: Integer;
668 ok: Boolean;
669 begin
670 if gWalls = nil then
671 Exit;
673 i := 0;
674 len := 128;
675 SetLength(PanelArray, len);
677 for a := 0 to High(gWalls) do
678 if gWalls[a].Door then
679 begin
680 PanelArray[i].X := gWalls[a].X;
681 PanelArray[i].Y := gWalls[a].Y;
682 PanelArray[i].Width := gWalls[a].Width;
683 PanelArray[i].Height := gWalls[a].Height;
684 PanelArray[i].Active := True;
685 PanelArray[i].PanelID := a;
687 i := i + 1;
688 if i = len then
689 begin
690 len := len + 128;
691 SetLength(PanelArray, len);
692 end;
693 end;
695 // Íåò äâåðåé:
696 if i = 0 then
697 begin
698 PanelArray := nil;
699 Exit;
700 end;
702 SetLength(gDoorMap, 0);
704 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
706 for a := 0 to i-1 do
707 if PanelArray[a].Active then
708 begin
709 PanelArray[a].Active := False;
710 m := Length(gDoorMap);
711 SetLength(gDoorMap, m+1);
712 SetLength(gDoorMap[m], 1);
713 gDoorMap[m, 0] := PanelArray[a].PanelID;
714 ok := True;
716 while ok do
717 begin
718 ok := False;
720 for b := 0 to i-1 do
721 if PanelArray[b].Active then
722 for c := 0 to High(gDoorMap[m]) do
723 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
724 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
725 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
726 PanelArray[b].Width, PanelArray[b].Height,
727 gWalls[gDoorMap[m, c]].X,
728 gWalls[gDoorMap[m, c]].Y,
729 gWalls[gDoorMap[m, c]].Width,
730 gWalls[gDoorMap[m, c]].Height) then
731 begin
732 PanelArray[b].Active := False;
733 SetLength(gDoorMap[m],
734 Length(gDoorMap[m])+1);
735 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
736 ok := True;
737 Break;
738 end;
739 end;
741 g_Game_StepLoading();
742 end;
744 PanelArray := nil;
745 end;
747 procedure CreateLiftMap();
748 var
749 PanelArray: Array of record
750 X, Y: Integer;
751 Width, Height: Word;
752 Active: Boolean;
753 end;
754 a, b, c, len, i, j: Integer;
755 ok: Boolean;
756 begin
757 if gLifts = nil then
758 Exit;
760 len := Length(gLifts);
761 SetLength(PanelArray, len);
763 for a := 0 to len-1 do
764 begin
765 PanelArray[a].X := gLifts[a].X;
766 PanelArray[a].Y := gLifts[a].Y;
767 PanelArray[a].Width := gLifts[a].Width;
768 PanelArray[a].Height := gLifts[a].Height;
769 PanelArray[a].Active := True;
770 end;
772 SetLength(gLiftMap, len);
773 i := 0;
775 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
777 for a := 0 to len-1 do
778 if PanelArray[a].Active then
779 begin
780 PanelArray[a].Active := False;
781 SetLength(gLiftMap[i], 32);
782 j := 0;
783 gLiftMap[i, j] := a;
784 ok := True;
786 while ok do
787 begin
788 ok := False;
789 for b := 0 to len-1 do
790 if PanelArray[b].Active then
791 for c := 0 to j do
792 if g_CollideAround(PanelArray[b].X,
793 PanelArray[b].Y,
794 PanelArray[b].Width,
795 PanelArray[b].Height,
796 PanelArray[gLiftMap[i, c]].X,
797 PanelArray[gLiftMap[i, c]].Y,
798 PanelArray[gLiftMap[i, c]].Width,
799 PanelArray[gLiftMap[i, c]].Height) then
800 begin
801 PanelArray[b].Active := False;
802 j := j+1;
803 if j > High(gLiftMap[i]) then
804 SetLength(gLiftMap[i],
805 Length(gLiftMap[i])+32);
807 gLiftMap[i, j] := b;
808 ok := True;
810 Break;
811 end;
812 end;
814 SetLength(gLiftMap[i], j+1);
815 i := i+1;
817 g_Game_StepLoading();
818 end;
820 SetLength(gLiftMap, i);
822 PanelArray := nil;
823 end;
825 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
826 var
827 len: Integer;
828 panels: ^TPanelArray;
829 pan: TPanel;
830 pguid: Integer;
831 begin
832 Result := -1;
834 case PanelRec.PanelType of
835 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
836 PANEL_BACK: panels := @gRenderBackgrounds;
837 PANEL_FORE: panels := @gRenderForegrounds;
838 PANEL_WATER: panels := @gWater;
839 PANEL_ACID1: panels := @gAcid1;
840 PANEL_ACID2: panels := @gAcid2;
841 PANEL_STEP: panels := @gSteps;
842 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
843 PANEL_BLOCKMON: panels := @gBlockMon;
844 else exit;
845 end;
847 len := Length(panels^);
848 SetLength(panels^, len+1);
850 pguid := Length(panByGUID);
851 SetLength(panByGUID, pguid+1); //FIXME!
852 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
853 assert(pguid >= 0);
854 assert(pguid < Length(panByGUID));
855 panByGUID[pguid] := pan;
856 panels^[len] := pan;
857 pan.arrIdx := len;
858 pan.proxyId := -1;
859 pan.tag := panelTypeToTag(PanelRec.PanelType);
861 PanelRec.user['panel_guid'] := pguid;
863 //result := len;
864 result := pguid;
865 end;
868 function CreateNullTexture(RecName: String): Integer;
869 begin
870 RecName := toLowerCase1251(RecName);
871 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
872 if TextNameHash.get(RecName, result) then exit; // i found her!
874 SetLength(Textures, Length(Textures)+1);
875 result := High(Textures);
877 with Textures[High(Textures)] do
878 begin
879 TextureName := RecName;
880 Width := 1;
881 Height := 1;
882 Anim := False;
883 TextureID := LongWord(TEXTURE_NONE);
884 end;
886 TextNameHash.put(RecName, result);
887 end;
890 function extractWadName (resourceName: string): string;
891 var
892 posN: Integer;
893 begin
894 posN := Pos(':', resourceName);
895 if posN > 0 then
896 Result:= Copy(resourceName, 0, posN-1)
897 else
898 Result := '';
899 end;
902 procedure addResToExternalResList (res: AnsiString);
903 var
904 uname: AnsiString;
905 f: Integer;
906 fi: TDiskFileInfo;
907 begin
908 if g_Game_IsClient or not g_Game_IsNet then exit;
909 if (length(res) = 0) then exit; // map wad
910 //res := extractWadName(res);
911 //if (length(res) = 0) then exit; // map wad
912 uname := toLowerCase1251(res);
913 // do not add duplicates
914 for f := 0 to High(gExternalResources) do
915 begin
916 if (gExternalResources[f].userName = uname) then exit;
917 end;
918 //writeln('***(000) addResToExternalResList: res=[', res, ']');
919 // add new resource
920 fi.userName := uname;
921 if not findFileCI(res) then exit;
922 //writeln('***(001) addResToExternalResList: res=[', res, ']');
923 fi.diskName := res;
924 if (not GetDiskFileInfo(res, fi)) then
925 begin
926 fi.tag := -1;
927 end
928 else
929 begin
930 //writeln('***(002) addResToExternalResList: res=[', res, ']');
931 fi.tag := 0; // non-zero means "cannot caclucate hash"
932 try
933 fi.hash := MD5File(fi.diskName);
934 except
935 fi.tag := -1;
936 end;
937 end;
938 //e_LogWritefln('addext: res=[%s]; uname=[%s]; diskName=[%s]', [res, fi.userName, fi.diskName]);
939 SetLength(gExternalResources, length(gExternalResources)+1);
940 gExternalResources[High(gExternalResources)] := fi;
941 end;
944 procedure compactExtResList ();
945 var
946 src, dest: Integer;
947 begin
948 src := 0;
949 dest := 0;
950 for src := 0 to High(gExternalResources) do
951 begin
952 if (gExternalResources[src].tag = 0) then
953 begin
954 // copy it
955 if (dest <> src) then gExternalResources[dest] := gExternalResources[src];
956 Inc(dest);
957 end;
958 end;
959 if (dest <> length(gExternalResources)) then SetLength(gExternalResources, dest);
960 end;
963 function GetReplacementWad (WadName: AnsiString): AnsiString;
964 begin
965 result := '';
966 if WadName <> '' then
967 begin
968 result := WadName;
969 if g_Game_IsClient then result := g_Res_FindReplacementWad(WadName);
970 if (result = WadName) then result := e_FindWad(WadDirs, result)
971 end;
972 end;
975 procedure generateExternalResourcesList (map: TDynRecord);
976 begin
977 SetLength(gExternalResources, 0);
978 addResToExternalResList(GetReplacementWad(g_ExtractWadName(map.MusicName)));
979 addResToExternalResList(GetReplacementWad(g_ExtractWadName(map.SkyName)));
980 end;
983 function CreateTexture (RecName: AnsiString; Map: string; log: Boolean): Integer;
984 var
985 WAD: TWADFile;
986 TextureData: Pointer;
987 WADName: String;
988 a, ResLength: Integer;
989 begin
990 RecName := toLowerCase1251(RecName);
991 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
992 if TextNameHash.get(RecName, result) then
993 begin
994 // i found her!
995 //e_LogWritefln('texture ''%s'' already loaded (%s)', [RecName, result]);
996 exit;
997 end;
999 Result := -1;
1001 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
1004 if Textures <> nil then
1005 begin
1006 for a := 0 to High(Textures) do
1007 begin
1008 if (Textures[a].TextureName = RecName) then
1009 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
1010 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
1011 Result := a;
1012 Exit;
1013 end;
1014 end;
1015 end;
1018 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
1019 if (RecName = TEXTURE_NAME_WATER) or
1020 (RecName = TEXTURE_NAME_ACID1) or
1021 (RecName = TEXTURE_NAME_ACID2) then
1022 begin
1023 SetLength(Textures, Length(Textures)+1);
1025 with Textures[High(Textures)] do
1026 begin
1027 TextureName := RecName;
1028 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
1029 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
1030 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
1032 Anim := False;
1033 end;
1035 result := High(Textures);
1036 TextNameHash.put(RecName, result);
1037 Exit;
1038 end;
1040 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
1041 WADName := GetReplacementWad(g_ExtractWadName(RecName));
1042 if (WADName <> '') then addResToExternalResList(WADName);
1043 if WADName = '' then WADName := Map; //WADName := GameDir+'/wads/'+WADName else
1045 WAD := TWADFile.Create();
1046 WAD.ReadFile(WADName);
1048 //txname := RecName;
1050 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
1051 begin
1052 FreeMem(TextureData);
1053 RecName := 'COMMON\ALIEN';
1054 end;
1057 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
1058 begin
1059 SetLength(Textures, Length(Textures)+1);
1060 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
1061 begin
1062 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1063 SetLength(Textures, Length(Textures)-1);
1064 result := -1;
1065 Exit;
1066 end;
1067 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
1068 FreeMem(TextureData);
1069 Textures[High(Textures)].TextureName := RecName;
1070 Textures[High(Textures)].Anim := False;
1072 result := High(Textures);
1073 TextNameHash.put(RecName, result);
1074 end
1075 else // Íåò òàêîãî ðåóñðñà â WAD'å
1076 begin
1077 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
1078 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1079 if log and (not BadTextNameHash.get(RecName, a)) then
1080 begin
1081 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1082 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1083 end;
1084 BadTextNameHash.put(RecName, -1);
1085 end;
1087 WAD.Free();
1088 end;
1091 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
1092 var
1093 WAD: TWADFile;
1094 TextureWAD: PChar = nil;
1095 TextData: Pointer = nil;
1096 TextureData: Pointer = nil;
1097 cfg: TConfig = nil;
1098 WADName: String;
1099 ResLength: Integer;
1100 TextureResource: String;
1101 _width, _height, _framecount, _speed: Integer;
1102 _backanimation: Boolean;
1103 //imgfmt: string;
1104 ia: TDynImageDataArray = nil;
1105 f, c, frdelay, frloop: Integer;
1106 begin
1107 RecName := toLowerCase1251(RecName);
1108 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
1109 if TextNameHash.get(RecName, result) then
1110 begin
1111 // i found her!
1112 //e_LogWritefln('animated texture ''%s'' already loaded (%s)', [RecName, result]);
1113 exit;
1114 end;
1116 result := -1;
1118 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1120 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1121 if BadTextNameHash.get(RecName, f) then
1122 begin
1123 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1124 exit;
1125 end;
1127 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1128 WADName := GetReplacementWad(g_ExtractWadName(RecName));
1129 if (WADName <> '') then addResToExternalResList(WADName);
1130 if WADName = '' then WADName := Map; //WADName := GameDir+'/wads/'+WADName else
1132 WAD := TWADFile.Create();
1133 try
1134 //if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1136 WAD.ReadFile(WADName);
1138 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1139 begin
1140 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1141 if log and (not BadTextNameHash.get(RecName, f)) then
1142 begin
1143 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1144 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1145 end;
1146 BadTextNameHash.put(RecName, -1);
1147 exit;
1148 end;
1150 {TEST
1151 if WADName = Map then
1152 begin
1153 //FreeMem(TextureWAD);
1154 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1155 end;
1158 WAD.FreeWAD();
1160 if ResLength < 6 then
1161 begin
1162 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), TMsgType.Warning);
1163 BadTextNameHash.put(RecName, -1);
1164 exit;
1165 end;
1167 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1168 if isWadData(TextureWAD, ResLength) then
1169 begin
1170 // íåò, ýòî ñóïåðìåí!
1171 if not WAD.ReadMemory(TextureWAD, ResLength) then
1172 begin
1173 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), TMsgType.Warning);
1174 BadTextNameHash.put(RecName, -1);
1175 exit;
1176 end;
1178 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1179 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1180 begin
1181 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), TMsgType.Warning);
1182 BadTextNameHash.put(RecName, -1);
1183 exit;
1184 end;
1186 cfg := TConfig.CreateMem(TextData, ResLength);
1188 TextureResource := cfg.ReadStr('', 'resource', '');
1189 if TextureResource = '' then
1190 begin
1191 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), TMsgType.Warning);
1192 BadTextNameHash.put(RecName, -1);
1193 exit;
1194 end;
1196 _width := cfg.ReadInt('', 'framewidth', 0);
1197 _height := cfg.ReadInt('', 'frameheight', 0);
1198 _framecount := cfg.ReadInt('', 'framecount', 0);
1199 _speed := cfg.ReadInt('', 'waitcount', 0);
1200 _backanimation := cfg.ReadBool('', 'backanimation', False);
1202 cfg.Free();
1203 cfg := nil;
1205 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1206 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1207 begin
1208 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), TMsgType.Warning);
1209 BadTextNameHash.put(RecName, -1);
1210 exit;
1211 end;
1213 WAD.Free();
1214 WAD := nil;
1216 SetLength(Textures, Length(Textures)+1);
1217 with Textures[High(Textures)] do
1218 begin
1219 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1220 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1221 begin
1222 TextureName := RecName;
1223 Width := _width;
1224 Height := _height;
1225 Anim := True;
1226 FramesCount := _framecount;
1227 Speed := _speed;
1228 result := High(Textures);
1229 TextNameHash.put(RecName, result);
1230 end
1231 else
1232 begin
1233 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1234 if log and (not BadTextNameHash.get(RecName, f)) then
1235 begin
1236 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1237 end;
1238 BadTextNameHash.put(RecName, -1);
1239 end;
1240 end;
1241 end
1242 else
1243 begin
1244 // try animated image
1246 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1247 if length(imgfmt) = 0 then
1248 begin
1249 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1250 exit;
1251 end;
1253 GlobalMetadata.ClearMetaItems();
1254 GlobalMetadata.ClearMetaItemsForSaving();
1255 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1256 begin
1257 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), TMsgType.Warning);
1258 BadTextNameHash.put(RecName, -1);
1259 exit;
1260 end;
1261 if length(ia) = 0 then
1262 begin
1263 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), TMsgType.Warning);
1264 BadTextNameHash.put(RecName, -1);
1265 exit;
1266 end;
1268 WAD.Free();
1269 WAD := nil;
1271 _width := ia[0].width;
1272 _height := ia[0].height;
1273 _framecount := length(ia);
1274 _speed := 1;
1275 _backanimation := false;
1276 frdelay := -1;
1277 frloop := -666;
1278 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1279 begin
1280 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1281 try
1282 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1283 frdelay := f;
1284 if f < 0 then f := 0;
1285 // rounding ;-)
1286 c := f mod 28;
1287 if c < 13 then c := 0 else c := 1;
1288 f := (f div 28)+c;
1289 if f < 1 then f := 1 else if f > 255 then f := 255;
1290 _speed := f;
1291 except
1292 end;
1293 end;
1294 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1295 begin
1296 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1297 try
1298 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1299 frloop := f;
1300 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1301 except
1302 end;
1303 end;
1304 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1305 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1306 f := ord(_backanimation);
1307 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]), TMsgType.Notify);
1309 SetLength(Textures, Length(Textures)+1);
1310 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1311 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1312 begin
1313 Textures[High(Textures)].TextureName := RecName;
1314 Textures[High(Textures)].Width := _width;
1315 Textures[High(Textures)].Height := _height;
1316 Textures[High(Textures)].Anim := True;
1317 Textures[High(Textures)].FramesCount := length(ia);
1318 Textures[High(Textures)].Speed := _speed;
1319 result := High(Textures);
1320 TextNameHash.put(RecName, result);
1321 //writeln(' CREATED!');
1322 end
1323 else
1324 begin
1325 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1326 if log and (not BadTextNameHash.get(RecName, f)) then
1327 begin
1328 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), TMsgType.Warning);
1329 end;
1330 BadTextNameHash.put(RecName, -1);
1331 end;
1332 end;
1333 finally
1334 for f := 0 to High(ia) do FreeImage(ia[f]);
1335 WAD.Free();
1336 cfg.Free();
1337 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1338 if (TextData <> nil) then FreeMem(TextData);
1339 if (TextureData <> nil) then FreeMem(TextureData);
1340 end;
1341 end;
1343 procedure CreateItem(Item: TDynRecord);
1344 begin
1345 if g_Game_IsClient then Exit;
1347 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1348 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1349 Exit;
1351 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1352 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1353 end;
1355 procedure CreateArea(Area: TDynRecord);
1356 var
1357 a: Integer;
1358 id: DWORD = 0;
1359 begin
1360 case Area.AreaType of
1361 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1362 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1363 begin
1364 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1365 with RespawnPoints[High(RespawnPoints)] do
1366 begin
1367 X := Area.X;
1368 Y := Area.Y;
1369 Direction := TDirection(Area.Direction);
1371 case Area.AreaType of
1372 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1373 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1374 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1375 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1376 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1377 end;
1378 end;
1379 end;
1381 AREA_REDFLAG, AREA_BLUEFLAG:
1382 begin
1383 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1385 if FlagPoints[a] <> nil then Exit;
1387 New(FlagPoints[a]);
1389 with FlagPoints[a]^ do
1390 begin
1391 X := Area.X-FLAGRECT.X;
1392 Y := Area.Y-FLAGRECT.Y;
1393 Direction := TDirection(Area.Direction);
1394 end;
1396 with gFlags[a] do
1397 begin
1398 case a of
1399 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1400 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1401 end;
1403 Animation := TAnimation.Create(id, True, 8);
1404 Obj.Rect := FLAGRECT;
1406 g_Map_ResetFlag(a);
1407 end;
1408 end;
1410 AREA_DOMFLAG:
1411 begin
1412 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1413 with DOMFlagPoints[High(DOMFlagPoints)] do
1414 begin
1415 X := Area.X;
1416 Y := Area.Y;
1417 Direction := TDirection(Area.Direction);
1418 end;
1420 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1421 end;
1422 end;
1423 end;
1425 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1426 var
1427 _trigger: TTrigger;
1428 tp: TPanel;
1429 begin
1430 result := -1;
1431 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1433 with _trigger do
1434 begin
1435 mapId := Trigger.id;
1436 mapIndex := amapIdx;
1437 X := Trigger.X;
1438 Y := Trigger.Y;
1439 Width := Trigger.Width;
1440 Height := Trigger.Height;
1441 Enabled := Trigger.Enabled;
1442 TexturePanelGUID := atpanid;
1443 TriggerType := Trigger.TriggerType;
1444 ActivateType := Trigger.ActivateType;
1445 Keys := Trigger.Keys;
1446 trigPanelGUID := atrigpanid;
1447 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1448 if TexturePanelGUID <> -1 then
1449 begin
1450 tp := g_Map_PanelByGUID(TexturePanelGUID);
1451 if (tp <> nil) then tp.hasTexTrigger := True;
1452 end;
1453 end;
1455 result := Integer(g_Triggers_Create(_trigger, Trigger));
1456 end;
1458 procedure CreateMonster(monster: TDynRecord);
1459 var
1460 a: Integer;
1461 mon: TMonster;
1462 begin
1463 if g_Game_IsClient then Exit;
1465 if (gGameSettings.GameType = GT_SINGLE)
1466 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1467 begin
1468 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1470 if gTriggers <> nil then
1471 begin
1472 for a := 0 to High(gTriggers) do
1473 begin
1474 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1475 begin
1476 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1477 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1478 end;
1479 end;
1480 end;
1482 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1483 end;
1484 end;
1486 procedure g_Map_ReAdd_DieTriggers();
1488 function monsDieTrig (mon: TMonster): Boolean;
1489 var
1490 a: Integer;
1491 //tw: TStrTextWriter;
1492 begin
1493 result := false; // don't stop
1494 mon.ClearTriggers();
1495 for a := 0 to High(gTriggers) do
1496 begin
1497 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1498 begin
1499 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1501 tw := TStrTextWriter.Create();
1502 try
1503 gTriggers[a].trigData.writeTo(tw);
1504 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1505 finally
1506 tw.Free();
1507 end;
1509 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1510 end;
1511 end;
1512 end;
1514 begin
1515 if g_Game_IsClient then Exit;
1517 g_Mons_ForEach(monsDieTrig);
1518 end;
1520 procedure mapCreateGrid ();
1521 var
1522 mapX0: Integer = $3fffffff;
1523 mapY0: Integer = $3fffffff;
1524 mapX1: Integer = -$3fffffff;
1525 mapY1: Integer = -$3fffffff;
1527 procedure calcBoundingBox (constref panels: TPanelArray);
1528 var
1529 idx: Integer;
1530 pan: TPanel;
1531 begin
1532 for idx := 0 to High(panels) do
1533 begin
1534 pan := panels[idx];
1535 if not pan.visvalid then continue;
1536 if (pan.Width < 1) or (pan.Height < 1) then continue;
1537 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1538 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1539 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1540 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1541 end;
1542 end;
1544 procedure addPanelsToGrid (constref panels: TPanelArray);
1545 var
1546 idx: Integer;
1547 pan: TPanel;
1548 newtag: Integer;
1549 begin
1550 //tag := panelTypeToTag(tag);
1551 for idx := 0 to High(panels) do
1552 begin
1553 pan := panels[idx];
1554 if not pan.visvalid then continue;
1555 if (pan.proxyId <> -1) then
1556 begin
1557 {$IF DEFINED(D2F_DEBUG)}
1558 e_WriteLog(Format('DUPLICATE wall #%d(%d) enabled (%d); type:%08x', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.PanelType]), TMsgType.Notify);
1559 {$ENDIF}
1560 continue;
1561 end;
1562 case pan.PanelType of
1563 PANEL_WALL: newtag := GridTagWall;
1564 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1565 PANEL_BACK: newtag := GridTagBack;
1566 PANEL_FORE: newtag := GridTagFore;
1567 PANEL_WATER: newtag := GridTagWater;
1568 PANEL_ACID1: newtag := GridTagAcid1;
1569 PANEL_ACID2: newtag := GridTagAcid2;
1570 PANEL_STEP: newtag := GridTagStep;
1571 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1572 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1573 else continue; // oops
1574 end;
1575 pan.tag := newtag;
1577 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1578 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1579 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1580 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1582 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1583 begin
1584 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1585 end;
1587 {$ENDIF}
1588 end;
1589 end;
1591 begin
1592 mapGrid.Free();
1593 mapGrid := nil;
1595 calcBoundingBox(gWalls);
1596 calcBoundingBox(gRenderBackgrounds);
1597 calcBoundingBox(gRenderForegrounds);
1598 calcBoundingBox(gWater);
1599 calcBoundingBox(gAcid1);
1600 calcBoundingBox(gAcid2);
1601 calcBoundingBox(gSteps);
1602 calcBoundingBox(gLifts);
1603 calcBoundingBox(gBlockMon);
1605 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1607 if (mapX0 > 0) then mapX0 := 0;
1608 if (mapY0 > 0) then mapY0 := 0;
1610 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1611 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1613 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1614 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1616 addPanelsToGrid(gWalls);
1617 addPanelsToGrid(gRenderBackgrounds);
1618 addPanelsToGrid(gRenderForegrounds);
1619 addPanelsToGrid(gWater);
1620 addPanelsToGrid(gAcid1);
1621 addPanelsToGrid(gAcid2);
1622 addPanelsToGrid(gSteps);
1623 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1624 addPanelsToGrid(gBlockMon);
1626 mapGrid.dumpStats();
1628 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1629 end;
1632 function g_Map_Load(Res: String): Boolean;
1633 const
1634 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1635 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1636 type
1637 PTRec = ^TTRec;
1638 TTRec = record
1639 //TexturePanel: Integer;
1640 tnum: Integer;
1641 id: Integer;
1642 trigrec: TDynRecord;
1643 // texture pane;
1644 texPanelIdx: Integer;
1645 texPanel: TDynRecord;
1646 // "action" panel
1647 actPanelIdx: Integer;
1648 actPanel: TDynRecord;
1649 end;
1650 var
1651 WAD, TestWAD: TWADFile;
1652 //mapReader: TDynRecord = nil;
1653 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1654 panels: TDynField = nil; //TPanelsRec1Array;
1655 items: TDynField = nil; //TItemsRec1Array;
1656 monsters: TDynField = nil; //TMonsterRec1Array;
1657 areas: TDynField = nil; //TAreasRec1Array;
1658 triggers: TDynField = nil; //TTriggersRec1Array;
1659 b, c, k: Integer;
1660 PanelID: DWORD;
1661 AddTextures: TAddTextureArray;
1662 TriggersTable: array of TTRec;
1663 FileName, mapResName, TexName, s: AnsiString;
1664 Data: Pointer;
1665 Len: Integer;
1666 ok, isAnim: Boolean;
1667 CurTex, ntn: Integer;
1668 rec, texrec: TDynRecord;
1669 pttit: PTRec;
1670 pannum, trignum, cnt, tgpid: Integer;
1671 stt: UInt64;
1672 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1673 //moveActive: Boolean;
1674 pan: TPanel;
1675 mapOk: Boolean = false;
1676 usedTextures: THashStrInt = nil; // key: mapTextureList
1677 begin
1678 mapGrid.Free();
1679 mapGrid := nil;
1680 TestWAD := nil;
1681 Data := nil;
1683 //gCurrentMap.Free();
1684 //gCurrentMap := nil;
1686 panByGUID := nil;
1688 Result := False;
1689 gMapInfo.Map := Res;
1690 TriggersTable := nil;
1691 //mapReader := nil;
1693 sfsGCDisable(); // temporary disable removing of temporary volumes
1694 try
1695 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1696 if (gCurrentMap = nil) then
1697 begin
1698 FileName := g_ExtractWadName(Res);
1699 e_LogWritefln('Loading map WAD [%s] (res=[%s])', [FileName, Res], TMsgType.Notify);
1700 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1702 WAD := TWADFile.Create();
1703 if not WAD.ReadFile(FileName) then
1704 begin
1705 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1706 WAD.Free();
1707 Exit;
1708 end;
1710 if gTestMap <> '' then
1711 begin
1712 s := g_ExtractWadName(gTestMap);
1713 TestWAD := TWADFile.Create();
1714 if not TestWAD.ReadFile(s) then
1715 begin
1716 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1717 TestWAD.Free();
1718 TestWAD := nil;
1719 end;
1720 end;
1722 if TestWAD <> nil then
1723 begin
1724 mapResName := g_ExtractFileName(gTestMap);
1725 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1726 begin
1727 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1728 Data := nil;
1729 end else
1730 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1731 TestWAD.Free();
1732 TestWAD := nil;
1733 end;
1735 if Data = nil then
1736 begin
1737 //k8: why loader ignores path here?
1738 mapResName := g_ExtractFileName(Res);
1739 if not WAD.GetMapResource(mapResName, Data, Len) then
1740 begin
1741 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1742 WAD.Free();
1743 Exit;
1744 end;
1745 end;
1747 WAD.Free();
1749 if (Len < 4) then
1750 begin
1751 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1752 FreeMem(Data);
1753 exit;
1754 end;
1756 // Çàãðóçêà êàðòû:
1757 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1758 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1760 stt := getTimeMicro();
1762 try
1763 gCurrentMap := g_Map_ParseMap(Data, Len);
1764 except
1765 gCurrentMap.Free();
1766 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1767 FreeMem(Data);
1768 gCurrentMapFileName := '';
1769 Exit;
1770 end;
1772 FreeMem(Data);
1774 if (gCurrentMap = nil) then
1775 begin
1776 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1777 gCurrentMapFileName := '';
1778 exit;
1779 end;
1780 end
1781 else
1782 begin
1783 stt := getTimeMicro();
1784 end;
1786 //gCurrentMap := mapReader;
1788 generateExternalResourcesList(gCurrentMap);
1789 mapTextureList := gCurrentMap['texture'];
1790 // get all other lists here too
1791 panels := gCurrentMap['panel'];
1792 triggers := gCurrentMap['trigger'];
1793 items := gCurrentMap['item'];
1794 areas := gCurrentMap['area'];
1795 monsters := gCurrentMap['monster'];
1797 // Çàãðóçêà îïèñàíèÿ êàðòû:
1798 e_WriteLog(' Reading map info...', TMsgType.Notify);
1799 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1801 with gMapInfo do
1802 begin
1803 Name := gCurrentMap.MapName;
1804 Description := gCurrentMap.MapDesc;
1805 Author := gCurrentMap.MapAuthor;
1806 MusicName := gCurrentMap.MusicName;
1807 SkyName := gCurrentMap.SkyName;
1808 Height := gCurrentMap.Height;
1809 Width := gCurrentMap.Width;
1810 end;
1812 // Çàãðóçêà òåêñòóð:
1813 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1814 // Äîáàâëåíèå òåêñòóð â Textures[]:
1815 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1816 begin
1817 e_WriteLog(' Loading textures:', TMsgType.Notify);
1818 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1820 // find used textures
1821 usedTextures := THashStrInt.Create();
1822 try
1823 if (panels <> nil) and (panels.count > 0) then
1824 begin
1825 for rec in panels do
1826 begin
1827 texrec := rec.TextureRec;
1828 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1829 end;
1830 end;
1832 cnt := -1;
1833 for rec in mapTextureList do
1834 begin
1835 Inc(cnt);
1836 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1837 begin
1838 rec.tagInt := -1; // just in case
1839 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1840 end
1841 else
1842 begin
1843 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1844 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1845 {$ENDIF}
1846 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1847 if rec.Anim then
1848 begin
1849 // Àíèìèðîâàííàÿ òåêñòóðà
1850 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1851 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]));
1852 end
1853 else
1854 begin
1855 // Îáû÷íàÿ òåêñòóðà
1856 ntn := CreateTexture(rec.Resource, FileName, True);
1857 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1858 end;
1859 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1861 rec.tagInt := ntn; // remember texture number
1862 end;
1863 g_Game_StepLoading();
1864 end;
1865 finally
1866 usedTextures.Free();
1867 end;
1869 // set panel tagInt to texture index
1870 if (panels <> nil) then
1871 begin
1872 for rec in panels do
1873 begin
1874 texrec := rec.TextureRec;
1875 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1876 end;
1877 end;
1878 end;
1881 // Çàãðóçêà òðèããåðîâ
1882 gTriggerClientID := 0;
1883 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1884 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1886 // Çàãðóçêà ïàíåëåé
1887 e_WriteLog(' Loading panels...', TMsgType.Notify);
1888 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1890 // check texture numbers for panels
1891 if (panels <> nil) and (panels.count > 0) then
1892 begin
1893 for rec in panels do
1894 begin
1895 if (rec.tagInt < 0) then
1896 begin
1897 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1898 result := false;
1899 gCurrentMap.Free();
1900 gCurrentMap := nil;
1901 gCurrentMapFileName := '';
1902 exit;
1903 end;
1904 end;
1905 end;
1907 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1908 if (triggers <> nil) and (triggers.count > 0) then
1909 begin
1910 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1911 //SetLength(TriggersTable, triggers.count);
1912 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1914 SetLength(TriggersTable, triggers.count);
1915 trignum := -1;
1916 for rec in triggers do
1917 begin
1918 Inc(trignum);
1919 pttit := @TriggersTable[trignum];
1920 pttit.trigrec := rec;
1921 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1922 pttit.texPanelIdx := -1; // will be fixed later
1923 pttit.texPanel := rec.TexturePanelRec;
1924 // action panel
1925 pttit.actPanelIdx := -1;
1926 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1927 // set flag
1928 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1929 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1930 // update progress
1931 g_Game_StepLoading();
1932 end;
1933 end;
1935 // Ñîçäàåì ïàíåëè
1936 if (panels <> nil) and (panels.count > 0) then
1937 begin
1938 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1939 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1941 pannum := -1;
1942 for rec in panels do
1943 begin
1944 Inc(pannum);
1945 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1946 texrec := nil;
1947 SetLength(AddTextures, 0);
1948 CurTex := -1;
1949 ok := false;
1951 if (mapTextureList <> nil) then
1952 begin
1953 texrec := rec.TextureRec;
1954 ok := (texrec <> nil);
1955 end;
1957 if ok then
1958 begin
1959 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1960 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1961 ok := false;
1962 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1963 begin
1964 if rec.userPanelTrigRef then
1965 begin
1966 // e_LogWritefln('trigref for panel %s', [pannum]);
1967 ok := True;
1968 end;
1969 end;
1970 end;
1972 if ok then
1973 begin
1974 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1975 s := texrec.Resource;
1977 // Ñïåö-òåêñòóðû çàïðåùåíû
1978 if g_Map_IsSpecialTexture(s) then
1979 begin
1980 ok := false
1981 end
1982 else
1983 begin
1984 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1985 ok := g_Texture_NumNameFindStart(s);
1986 end;
1988 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1989 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1990 if ok then
1991 begin
1992 k := NNF_NAME_BEFORE;
1993 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1994 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1995 begin
1996 k := g_Texture_NumNameFindNext(TexName);
1998 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1999 begin
2000 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
2001 if texrec.Anim then
2002 begin
2003 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
2004 isAnim := True;
2005 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2006 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2007 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2008 if not ok then
2009 begin
2010 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
2011 isAnim := False;
2012 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2013 ok := CreateTexture(TexName, FileName, False) >= 0;
2014 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2015 end;
2016 end
2017 else
2018 begin
2019 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
2020 isAnim := False;
2021 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2022 ok := CreateTexture(TexName, FileName, False) >= 0;
2023 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2024 if not ok then
2025 begin
2026 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
2027 isAnim := True;
2028 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2029 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2030 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2031 end;
2032 end;
2034 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
2035 if ok then
2036 begin
2038 for c := 0 to High(Textures) do
2039 begin
2040 if (Textures[c].TextureName = TexName) then
2041 begin
2042 SetLength(AddTextures, Length(AddTextures)+1);
2043 AddTextures[High(AddTextures)].Texture := c;
2044 AddTextures[High(AddTextures)].Anim := isAnim;
2045 break;
2046 end;
2047 end;
2049 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
2050 begin
2051 SetLength(AddTextures, Length(AddTextures)+1);
2052 AddTextures[High(AddTextures)].Texture := c;
2053 AddTextures[High(AddTextures)].Anim := isAnim;
2054 end;
2055 end;
2056 end
2057 else
2058 begin
2059 if k = NNF_NAME_EQUALS then
2060 begin
2061 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
2062 SetLength(AddTextures, Length(AddTextures)+1);
2063 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
2064 AddTextures[High(AddTextures)].Anim := texrec.Anim;
2065 CurTex := High(AddTextures);
2066 ok := true;
2067 end
2068 else // NNF_NO_NAME
2069 begin
2070 ok := false;
2071 end;
2072 end;
2073 end; // while ok...
2075 ok := true;
2076 end; // if ok - åñòü ñìåæíûå òåêñòóðû
2077 end; // if ok - ññûëàþòñÿ òðèããåðû
2079 if not ok then
2080 begin
2081 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
2082 SetLength(AddTextures, 1);
2083 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
2084 AddTextures[0].Anim := false;
2085 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
2086 CurTex := 0;
2087 end;
2089 //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);
2091 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
2093 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
2094 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
2095 PanelID := CreatePanel(rec, AddTextures, CurTex);
2096 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
2097 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
2099 // setup lifts
2100 moveSpeed := rec.moveSpeed;
2101 //moveStart := rec.moveStart;
2102 //moveEnd := rec.moveEnd;
2103 //moveActive := rec['move_active'].value;
2104 if not moveSpeed.isZero then
2105 begin
2106 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
2107 gMovingWallIds[High(gMovingWallIds)] := PanelID;
2108 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
2109 end;
2111 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2113 g_Game_StepLoading();
2114 end;
2115 end;
2117 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2118 for b := 0 to High(TriggersTable) do
2119 begin
2120 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2121 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2122 end;
2124 // create map grid, init other grids (for monsters, for example)
2125 e_WriteLog('Creating map grid', TMsgType.Notify);
2126 mapCreateGrid();
2128 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2129 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2130 begin
2131 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2132 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2133 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2134 trignum := -1;
2135 for rec in triggers do
2136 begin
2137 Inc(trignum);
2138 tgpid := TriggersTable[trignum].actPanelIdx;
2139 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2140 TriggersTable[trignum].tnum := trignum;
2141 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
2142 end;
2143 end;
2145 //FIXME: use hashtable!
2146 for pan in panByGUID do
2147 begin
2148 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2149 begin
2150 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2151 end;
2152 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2153 begin
2154 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2155 end;
2156 end;
2158 // Çàãðóçêà ïðåäìåòîâ
2159 e_WriteLog(' Loading items...', TMsgType.Notify);
2160 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2162 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2163 if (items <> nil) and not gLoadGameMode then
2164 begin
2165 e_WriteLog(' Spawning items...', TMsgType.Notify);
2166 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2167 for rec in items do CreateItem(rec);
2168 end;
2170 // Çàãðóçêà îáëàñòåé
2171 e_WriteLog(' Loading areas...', TMsgType.Notify);
2172 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2174 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2175 if areas <> nil then
2176 begin
2177 e_WriteLog(' Creating areas...', TMsgType.Notify);
2178 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2179 for rec in areas do CreateArea(rec);
2180 end;
2182 // Çàãðóçêà ìîíñòðîâ
2183 e_WriteLog(' Loading monsters...', TMsgType.Notify);
2184 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2186 gTotalMonsters := 0;
2188 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2189 if (monsters <> nil) and not gLoadGameMode then
2190 begin
2191 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
2192 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2193 for rec in monsters do CreateMonster(rec);
2194 end;
2196 //gCurrentMap := mapReader; // this will be our current map now
2197 gCurrentMapFileName := Res;
2198 //mapReader := nil;
2200 // Çàãðóçêà íåáà
2201 if (gMapInfo.SkyName <> '') then
2202 begin
2203 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
2204 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2205 s := e_GetResourcePath(WadDirs, gMapInfo.SkyName, g_ExtractWadName(Res));
2206 if g_Texture_CreateWAD(BackID, s, gTextureFilter) then
2207 g_Game_SetupScreenSize
2208 else
2209 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]))
2210 end;
2212 // Çàãðóçêà ìóçûêè
2213 ok := False;
2214 if gMapInfo.MusicName <> '' then
2215 begin
2216 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
2217 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2219 s := e_GetResourcePath(WadDirs, gMapInfo.MusicName, g_ExtractWadName(Res));
2220 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2221 ok := True
2222 else
2223 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2224 end;
2226 // Îñòàëüíûå óñòàíâêè
2227 CreateDoorMap();
2228 CreateLiftMap();
2230 g_Items_Init();
2231 g_Weapon_Init();
2232 g_Monsters_Init();
2234 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2235 if not gLoadGameMode then g_GFX_Init();
2237 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2238 mapTextureList := nil;
2239 panels := nil;
2240 items := nil;
2241 areas := nil;
2242 triggers := nil;
2243 TriggersTable := nil;
2244 AddTextures := nil;
2246 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2247 if ok and (not gLoadGameMode) then
2248 begin
2249 gMusic.SetByName(gMapInfo.MusicName);
2250 gMusic.Play();
2251 end
2252 else
2253 begin
2254 gMusic.SetByName('');
2255 end;
2257 stt := getTimeMicro()-stt;
2258 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2259 mapOk := true;
2260 finally
2261 sfsGCEnable(); // enable releasing unused volumes
2262 //mapReader.Free();
2263 e_UnpressAllKeys; // why not?
2264 if not mapOk then
2265 begin
2266 gCurrentMap.Free();
2267 gCurrentMap := nil;
2268 gCurrentMapFileName := '';
2269 end;
2270 end;
2272 compactExtResList();
2273 e_WriteLog('Done loading map.', TMsgType.Notify);
2274 Result := True;
2275 end;
2278 function g_Map_GetMapInfo(Res: String): TMapInfo;
2279 var
2280 WAD: TWADFile;
2281 mapReader: TDynRecord;
2282 //Header: TMapHeaderRec_1;
2283 FileName: String;
2284 Data: Pointer;
2285 Len: Integer;
2286 begin
2287 FillChar(Result, SizeOf(Result), 0);
2288 FileName := g_ExtractWadName(Res);
2290 WAD := TWADFile.Create();
2291 if not WAD.ReadFile(FileName) then
2292 begin
2293 WAD.Free();
2294 Exit;
2295 end;
2297 //k8: it ignores path again
2298 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2299 begin
2300 WAD.Free();
2301 Exit;
2302 end;
2304 WAD.Free();
2306 try
2307 mapReader := g_Map_ParseMap(Data, Len);
2308 except
2309 mapReader := nil;
2310 FreeMem(Data);
2311 exit;
2312 end;
2314 FreeMem(Data);
2316 if (mapReader = nil) then exit;
2318 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2319 begin
2320 Result.Name := mapReader.MapName;
2321 Result.Description := mapReader.MapDesc;
2322 Result.Map := Res;
2323 Result.Author := mapReader.MapAuthor;
2324 Result.Height := mapReader.Height;
2325 Result.Width := mapReader.Width;
2326 end
2327 else
2328 begin
2329 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2330 //ZeroMemory(@Header, SizeOf(Header));
2331 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2332 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2333 Result.Map := Res;
2334 Result.Author := '';
2335 Result.Height := 0;
2336 Result.Width := 0;
2337 end;
2339 mapReader.Free();
2340 end;
2342 function g_Map_GetMapsList(WADName: string): SSArray;
2343 var
2344 WAD: TWADFile;
2345 a: Integer;
2346 ResList: SSArray;
2347 begin
2348 Result := nil;
2349 WAD := TWADFile.Create();
2350 if not WAD.ReadFile(WADName) then
2351 begin
2352 WAD.Free();
2353 Exit;
2354 end;
2355 ResList := WAD.GetMapResources();
2356 if ResList <> nil then
2357 begin
2358 for a := 0 to High(ResList) do
2359 begin
2360 SetLength(Result, Length(Result)+1);
2361 Result[High(Result)] := ResList[a];
2362 end;
2363 end;
2364 WAD.Free();
2365 end;
2367 function g_Map_Exist(Res: string): Boolean;
2368 var
2369 WAD: TWADFile;
2370 FileName, mnn: string;
2371 ResList: SSArray;
2372 a: Integer;
2373 begin
2374 Result := False;
2376 FileName := addWadExtension(g_ExtractWadName(Res));
2378 WAD := TWADFile.Create;
2379 if not WAD.ReadFile(FileName) then
2380 begin
2381 WAD.Free();
2382 Exit;
2383 end;
2385 ResList := WAD.GetMapResources();
2386 WAD.Free();
2388 mnn := g_ExtractFileName(Res);
2389 if ResList <> nil then
2390 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2391 begin
2392 Result := True;
2393 Exit;
2394 end;
2395 end;
2397 procedure g_Map_Free(freeTextures: Boolean=true);
2398 var
2399 a: Integer;
2401 procedure FreePanelArray(var panels: TPanelArray);
2402 var
2403 i: Integer;
2405 begin
2406 if panels <> nil then
2407 begin
2408 for i := 0 to High(panels) do
2409 panels[i].Free();
2410 panels := nil;
2411 end;
2412 end;
2414 begin
2415 g_GFX_Free();
2416 g_Weapon_Free();
2417 g_Items_Free();
2418 g_Triggers_Free();
2419 g_Monsters_Free();
2421 RespawnPoints := nil;
2422 if FlagPoints[FLAG_RED] <> nil then
2423 begin
2424 Dispose(FlagPoints[FLAG_RED]);
2425 FlagPoints[FLAG_RED] := nil;
2426 end;
2427 if FlagPoints[FLAG_BLUE] <> nil then
2428 begin
2429 Dispose(FlagPoints[FLAG_BLUE]);
2430 FlagPoints[FLAG_BLUE] := nil;
2431 end;
2432 //DOMFlagPoints := nil;
2434 //gDOMFlags := nil;
2436 if (Length(gCurrentMapFileName) <> 0) then
2437 begin
2438 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2439 end
2440 else
2441 begin
2442 e_LogWritefln('g_Map_Free: no previous map.', []);
2443 end;
2445 if freeTextures then
2446 begin
2447 e_LogWritefln('g_Map_Free: clearing textures...', []);
2448 if (Textures <> nil) then
2449 begin
2450 for a := 0 to High(Textures) do
2451 begin
2452 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2453 begin
2454 if Textures[a].Anim then
2455 begin
2456 g_Frames_DeleteByID(Textures[a].FramesID)
2457 end
2458 else
2459 begin
2460 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2461 begin
2462 e_DeleteTexture(Textures[a].TextureID);
2463 end;
2464 end;
2465 end;
2466 end;
2467 Textures := nil;
2468 end;
2469 TextNameHash.Free();
2470 TextNameHash := nil;
2471 BadTextNameHash.Free();
2472 BadTextNameHash := nil;
2473 gCurrentMapFileName := '';
2474 gCurrentMap.Free();
2475 gCurrentMap := nil;
2476 end;
2478 panByGUID := nil;
2480 FreePanelArray(gWalls);
2481 FreePanelArray(gRenderBackgrounds);
2482 FreePanelArray(gRenderForegrounds);
2483 FreePanelArray(gWater);
2484 FreePanelArray(gAcid1);
2485 FreePanelArray(gAcid2);
2486 FreePanelArray(gSteps);
2487 FreePanelArray(gLifts);
2488 FreePanelArray(gBlockMon);
2489 gMovingWallIds := nil;
2491 if BackID <> DWORD(-1) then
2492 begin
2493 gBackSize.X := 0;
2494 gBackSize.Y := 0;
2495 e_DeleteTexture(BackID);
2496 BackID := DWORD(-1);
2497 end;
2499 g_Game_StopAllSounds(False);
2500 gMusic.FreeSound();
2501 g_Sound_Delete(gMapInfo.MusicName);
2503 gMapInfo.Name := '';
2504 gMapInfo.Description := '';
2505 gMapInfo.MusicName := '';
2506 gMapInfo.Height := 0;
2507 gMapInfo.Width := 0;
2509 gDoorMap := nil;
2510 gLiftMap := nil;
2511 end;
2513 procedure g_Map_Update();
2514 var
2515 a, d, j: Integer;
2516 m: Word;
2517 s: String;
2518 b: Byte;
2520 procedure UpdatePanelArray(var panels: TPanelArray);
2521 var
2522 i: Integer;
2524 begin
2525 for i := 0 to High(panels) do panels[i].Update();
2526 end;
2528 begin
2529 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2531 UpdatePanelArray(gWalls);
2532 UpdatePanelArray(gRenderBackgrounds);
2533 UpdatePanelArray(gRenderForegrounds);
2534 UpdatePanelArray(gWater);
2535 UpdatePanelArray(gAcid1);
2536 UpdatePanelArray(gAcid2);
2537 UpdatePanelArray(gSteps);
2539 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2541 if gGameSettings.GameMode = GM_CTF then
2542 begin
2543 for a := FLAG_RED to FLAG_BLUE do
2544 begin
2545 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2546 begin
2547 with gFlags[a] do
2548 begin
2549 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2551 m := g_Obj_Move(@Obj, True, True);
2553 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2555 // Ñîïðîòèâëåíèå âîçäóõà
2556 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2558 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2559 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2560 begin
2561 g_Map_ResetFlag(a);
2562 gFlags[a].CaptureTime := 0;
2563 if a = FLAG_RED then
2564 s := _lc[I_PLAYER_FLAG_RED]
2565 else
2566 s := _lc[I_PLAYER_FLAG_BLUE];
2567 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2569 if (((gPlayer1 <> nil) and (((gPlayer1.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer1.Team = TEAM_BLUE) and (a = FLAG_BLUE))))
2570 or ((gPlayer2 <> nil) and (((gPlayer2.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer2.Team = TEAM_BLUE) and (a = FLAG_BLUE))))) then
2571 b := 0
2572 else
2573 b := 1;
2575 if not sound_ret_flag[b].IsPlaying() then
2576 sound_ret_flag[b].Play();
2578 if g_Game_IsNet then
2579 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2580 Continue;
2581 end;
2583 if Count > 0 then Count -= 1;
2585 // Èãðîê áåðåò ôëàã
2586 if gPlayers <> nil then
2587 begin
2588 j := Random(Length(gPlayers)) - 1;
2589 for d := 0 to High(gPlayers) do
2590 begin
2591 Inc(j);
2592 if j > High(gPlayers) then j := 0;
2593 if gPlayers[j] <> nil then
2594 begin
2595 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2596 begin
2597 if gPlayers[j].GetFlag(a) then Break;
2598 end;
2599 end;
2600 end;
2601 end;
2602 end;
2603 end;
2604 end;
2605 end;
2606 end;
2608 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2609 PanelType: Word; b1x3: Boolean=false): Boolean;
2610 var
2611 a, h: Integer;
2612 begin
2613 Result := False;
2615 if WordBool(PanelType and PANEL_WALL) then
2616 if gWalls <> nil then
2617 begin
2618 h := High(gWalls);
2620 for a := 0 to h do
2621 if gWalls[a].Enabled and
2622 g_Collide(X, Y, Width, Height,
2623 gWalls[a].X, gWalls[a].Y,
2624 gWalls[a].Width, gWalls[a].Height) then
2625 begin
2626 Result := True;
2627 Exit;
2628 end;
2629 end;
2631 if WordBool(PanelType and PANEL_WATER) then
2632 if gWater <> nil then
2633 begin
2634 h := High(gWater);
2636 for a := 0 to h do
2637 if g_Collide(X, Y, Width, Height,
2638 gWater[a].X, gWater[a].Y,
2639 gWater[a].Width, gWater[a].Height) then
2640 begin
2641 Result := True;
2642 Exit;
2643 end;
2644 end;
2646 if WordBool(PanelType and PANEL_ACID1) then
2647 if gAcid1 <> nil then
2648 begin
2649 h := High(gAcid1);
2651 for a := 0 to h do
2652 if g_Collide(X, Y, Width, Height,
2653 gAcid1[a].X, gAcid1[a].Y,
2654 gAcid1[a].Width, gAcid1[a].Height) then
2655 begin
2656 Result := True;
2657 Exit;
2658 end;
2659 end;
2661 if WordBool(PanelType and PANEL_ACID2) then
2662 if gAcid2 <> nil then
2663 begin
2664 h := High(gAcid2);
2666 for a := 0 to h do
2667 if g_Collide(X, Y, Width, Height,
2668 gAcid2[a].X, gAcid2[a].Y,
2669 gAcid2[a].Width, gAcid2[a].Height) then
2670 begin
2671 Result := True;
2672 Exit;
2673 end;
2674 end;
2676 if WordBool(PanelType and PANEL_STEP) then
2677 if gSteps <> nil then
2678 begin
2679 h := High(gSteps);
2681 for a := 0 to h do
2682 if g_Collide(X, Y, Width, Height,
2683 gSteps[a].X, gSteps[a].Y,
2684 gSteps[a].Width, gSteps[a].Height) then
2685 begin
2686 Result := True;
2687 Exit;
2688 end;
2689 end;
2691 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2692 if gLifts <> nil then
2693 begin
2694 h := High(gLifts);
2696 for a := 0 to h do
2697 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = LIFTTYPE_UP)) or
2698 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = LIFTTYPE_DOWN)) or
2699 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = LIFTTYPE_LEFT)) or
2700 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = LIFTTYPE_RIGHT))) and
2701 g_Collide(X, Y, Width, Height,
2702 gLifts[a].X, gLifts[a].Y,
2703 gLifts[a].Width, gLifts[a].Height) then
2704 begin
2705 Result := True;
2706 Exit;
2707 end;
2708 end;
2710 if WordBool(PanelType and PANEL_BLOCKMON) then
2711 if gBlockMon <> nil then
2712 begin
2713 h := High(gBlockMon);
2715 for a := 0 to h do
2716 if ( (not b1x3) or
2717 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2718 g_Collide(X, Y, Width, Height,
2719 gBlockMon[a].X, gBlockMon[a].Y,
2720 gBlockMon[a].Width, gBlockMon[a].Height) then
2721 begin
2722 Result := True;
2723 Exit;
2724 end;
2725 end;
2726 end;
2728 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2729 var
2730 texid: DWORD;
2732 function checkPanels (constref panels: TPanelArray): Boolean;
2733 var
2734 a: Integer;
2735 begin
2736 result := false;
2737 if panels = nil then exit;
2738 for a := 0 to High(panels) do
2739 begin
2740 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2741 begin
2742 result := true;
2743 texid := panels[a].GetTextureID();
2744 exit;
2745 end;
2746 end;
2747 end;
2749 begin
2750 texid := LongWord(TEXTURE_NONE);
2751 result := texid;
2752 if not checkPanels(gWater) then
2753 if not checkPanels(gAcid1) then
2754 if not checkPanels(gAcid2) then exit;
2755 result := texid;
2756 end;
2759 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2760 const
2761 SlowMask = GridTagLift or GridTagBlockMon;
2763 function checker (pan: TPanel; tag: Integer): Boolean;
2764 begin
2766 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2767 begin
2768 result := pan.Enabled;
2769 exit;
2770 end;
2773 if ((tag and GridTagLift) <> 0) then
2774 begin
2775 result :=
2776 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2777 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2778 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2779 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2780 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2781 exit;
2782 end;
2784 if ((tag and GridTagBlockMon) <> 0) then
2785 begin
2786 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2787 exit;
2788 end;
2790 // other shit
2791 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2792 result := true; // i found her!
2793 end;
2795 var
2796 tagmask: Integer = 0;
2797 mwit: PPanel;
2798 it: TPanelGrid.Iter;
2799 pan: TPanel;
2800 begin
2801 result := false;
2802 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2803 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2804 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2805 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2806 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2807 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2808 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2810 if (tagmask = 0) then exit; // just in case
2812 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2813 if gdbg_map_use_accel_coldet then
2814 begin
2815 if ((tagmask and SlowMask) <> 0) then
2816 begin
2817 // slow
2818 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2819 for mwit in it do
2820 begin
2821 pan := mwit^;
2822 if ((pan.tag and GridTagLift) <> 0) then
2823 begin
2824 result :=
2825 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2826 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2827 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2828 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2829 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2830 end
2831 else if ((pan.tag and GridTagBlockMon) <> 0) then
2832 begin
2833 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2834 end
2835 else
2836 begin
2837 // other shit
2838 result := true; // i found her!
2839 end;
2840 if (result) then break;
2841 end;
2842 end
2843 else
2844 begin
2845 // fast
2846 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2847 result := (it.length > 0);
2848 end;
2849 it.release();
2850 end
2851 else
2852 begin
2853 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2854 end;
2855 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2856 end;
2859 // returns `true` if we need to stop
2860 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2861 begin
2862 result := false;
2863 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2864 // check priorities
2865 case cctype of
2866 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2867 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2868 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2869 end;
2870 // collision?
2871 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2872 // yeah
2873 texid := pan.GetTextureID();
2874 // water? water has the highest priority, so stop right here
2875 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2876 // acid2?
2877 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2878 // acid1?
2879 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2880 end;
2882 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2883 var
2884 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2885 mwit: PPanel;
2886 it: TPanelGrid.Iter;
2887 begin
2888 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2889 if gdbg_map_use_accel_coldet then
2890 begin
2891 result := LongWord(TEXTURE_NONE);
2892 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2893 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2894 it.release();
2895 end
2896 else
2897 begin
2898 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2899 end;
2900 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2901 end;
2904 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2905 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2906 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2909 procedure g_Map_EnableWallGUID (pguid: Integer);
2910 var
2911 pan: TPanel;
2912 begin
2913 //pan := gWalls[ID];
2914 pan := g_Map_PanelByGUID(pguid);
2915 if (pan = nil) then exit;
2916 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2918 pan.Enabled := True;
2919 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2921 mapGrid.proxyEnabled[pan.proxyId] := true;
2922 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2923 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2925 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2926 // mark platform as interesting
2927 pan.setDirty();
2929 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2930 //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);
2931 {$ENDIF}
2932 end;
2935 procedure g_Map_DisableWallGUID (pguid: Integer);
2936 var
2937 pan: TPanel;
2938 begin
2939 //pan := gWalls[ID];
2940 pan := g_Map_PanelByGUID(pguid);
2941 if (pan = nil) then exit;
2942 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2944 pan.Enabled := False;
2945 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
2947 mapGrid.proxyEnabled[pan.proxyId] := false;
2948 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2950 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2951 // mark platform as interesting
2952 pan.setDirty();
2954 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2955 //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);
2956 {$ENDIF}
2957 end;
2960 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
2961 var
2962 tp: TPanel;
2963 begin
2964 tp := g_Map_PanelByGUID(pguid);
2965 if (tp = nil) then exit;
2966 tp.NextTexture(AnimLoop);
2967 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
2968 end;
2971 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
2972 var
2973 pan: TPanel;
2974 begin
2975 //pan := gLifts[ID];
2976 pan := g_Map_PanelByGUID(pguid);
2977 if (pan = nil) then exit;
2978 if not pan.isGLift then exit;
2980 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
2982 with {gLifts[ID]} pan do
2983 begin
2984 LiftType := t;
2986 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
2987 //TODO: make separate lift tags, and change tag here
2989 case LiftType of
2990 LIFTTYPE_UP: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
2991 LIFTTYPE_DOWN: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
2992 LIFTTYPE_LEFT: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
2993 LIFTTYPE_RIGHT: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
2994 end;
2996 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2997 // mark platform as interesting
2998 pan.setDirty();
2999 end;
3000 end;
3003 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
3004 var
3005 a: Integer;
3006 PointsArray: Array of TRespawnPoint;
3007 begin
3008 Result := False;
3009 SetLength(PointsArray, 0);
3011 if RespawnPoints = nil then
3012 Exit;
3014 for a := 0 to High(RespawnPoints) do
3015 if RespawnPoints[a].PointType = PointType then
3016 begin
3017 SetLength(PointsArray, Length(PointsArray)+1);
3018 PointsArray[High(PointsArray)] := RespawnPoints[a];
3019 end;
3021 if PointsArray = nil then
3022 Exit;
3024 RespawnPoint := PointsArray[Random(Length(PointsArray))];
3025 Result := True;
3026 end;
3028 function g_Map_GetPointCount(PointType: Byte): Word;
3029 var
3030 a: Integer;
3031 begin
3032 Result := 0;
3034 if RespawnPoints = nil then
3035 Exit;
3037 for a := 0 to High(RespawnPoints) do
3038 if RespawnPoints[a].PointType = PointType then
3039 Result := Result + 1;
3040 end;
3042 function g_Map_GetRandomPointType(): Byte;
3043 begin
3044 if RespawnPoints = nil then
3045 Result := 255
3046 else
3047 Result := RespawnPoints[Random(Length(RespawnPoints))].PointType;
3048 end;
3050 function g_Map_HaveFlagPoints(): Boolean;
3051 begin
3052 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3053 end;
3055 procedure g_Map_ResetFlag(Flag: Byte);
3056 begin
3057 with gFlags[Flag] do
3058 begin
3059 Obj.X := -1000;
3060 Obj.Y := -1000;
3061 Obj.Vel.X := 0;
3062 Obj.Vel.Y := 0;
3063 Direction := TDirection.D_LEFT;
3064 State := FLAG_STATE_NONE;
3065 if FlagPoints[Flag] <> nil then
3066 begin
3067 Obj.X := FlagPoints[Flag]^.X;
3068 Obj.Y := FlagPoints[Flag]^.Y;
3069 Direction := FlagPoints[Flag]^.Direction;
3070 State := FLAG_STATE_NORMAL;
3071 end;
3072 Count := -1;
3073 end;
3074 end;
3076 procedure g_Map_SaveState (st: TStream);
3077 var
3078 str: String;
3080 procedure savePanels ();
3081 var
3082 pan: TPanel;
3083 begin
3084 // Ñîõðàíÿåì ïàíåëè
3085 utils.writeInt(st, LongInt(Length(panByGUID)));
3086 for pan in panByGUID do pan.SaveState(st);
3087 end;
3089 procedure saveFlag (flag: PFlag);
3090 var
3091 b: Byte;
3092 begin
3093 utils.writeSign(st, 'FLAG');
3094 utils.writeInt(st, Byte(0)); // version
3095 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3096 utils.writeInt(st, Byte(flag^.RespawnType));
3097 // Ñîñòîÿíèå ôëàãà
3098 utils.writeInt(st, Byte(flag^.State));
3099 // Íàïðàâëåíèå ôëàãà
3100 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
3101 utils.writeInt(st, Byte(b));
3102 // Îáúåêò ôëàãà
3103 Obj_SaveState(st, @flag^.Obj);
3104 end;
3106 begin
3107 savePanels();
3109 // Ñîõðàíÿåì ìóçûêó
3110 utils.writeSign(st, 'MUSI');
3111 utils.writeInt(st, Byte(0));
3112 // Íàçâàíèå ìóçûêè
3113 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3114 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3115 utils.writeStr(st, str);
3116 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3117 utils.writeInt(st, LongWord(gMusic.GetPosition()));
3118 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3119 utils.writeBool(st, gMusic.SpecPause);
3121 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3122 utils.writeInt(st, LongInt(gTotalMonsters));
3123 ///// /////
3125 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3126 if (gGameSettings.GameMode = GM_CTF) then
3127 begin
3128 // Ôëàã Êðàñíîé êîìàíäû
3129 saveFlag(@gFlags[FLAG_RED]);
3130 // Ôëàã Ñèíåé êîìàíäû
3131 saveFlag(@gFlags[FLAG_BLUE]);
3132 end;
3133 ///// /////
3135 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3136 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3137 begin
3138 // Î÷êè Êðàñíîé êîìàíäû
3139 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
3140 // Î÷êè Ñèíåé êîìàíäû
3141 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
3142 end;
3143 ///// /////
3144 end;
3147 procedure g_Map_LoadState (st: TStream);
3148 var
3149 dw: DWORD;
3150 str: String;
3151 boo: Boolean;
3153 procedure loadPanels ();
3154 var
3155 pan: TPanel;
3156 begin
3157 // Çàãðóæàåì ïàíåëè
3158 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
3159 for pan in panByGUID do
3160 begin
3161 pan.LoadState(st);
3162 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3163 end;
3164 end;
3166 procedure loadFlag (flag: PFlag);
3167 var
3168 b: Byte;
3169 begin
3170 // Ñèãíàòóðà ôëàãà
3171 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
3172 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
3173 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3174 flag^.RespawnType := utils.readByte(st);
3175 // Ñîñòîÿíèå ôëàãà
3176 flag^.State := utils.readByte(st);
3177 // Íàïðàâëåíèå ôëàãà
3178 b := utils.readByte(st);
3179 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
3180 // Îáúåêò ôëàãà
3181 Obj_LoadState(@flag^.Obj, st);
3182 end;
3184 begin
3185 if (st = nil) then exit;
3187 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3188 loadPanels();
3189 ///// /////
3191 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3192 g_GFX_Init();
3193 //mapCreateGrid();
3195 ///// Çàãðóæàåì ìóçûêó: /////
3196 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
3197 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
3198 // Íàçâàíèå ìóçûêè
3199 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3200 str := utils.readStr(st);
3201 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3202 dw := utils.readLongWord(st);
3203 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3204 boo := utils.readBool(st);
3205 // Çàïóñêàåì ýòó ìóçûêó
3206 gMusic.SetByName(str);
3207 gMusic.SpecPause := boo;
3208 gMusic.Play();
3209 gMusic.Pause(true);
3210 gMusic.SetPosition(dw);
3211 ///// /////
3213 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3214 gTotalMonsters := utils.readLongInt(st);
3215 ///// /////
3217 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3218 if (gGameSettings.GameMode = GM_CTF) then
3219 begin
3220 // Ôëàã Êðàñíîé êîìàíäû
3221 loadFlag(@gFlags[FLAG_RED]);
3222 // Ôëàã Ñèíåé êîìàíäû
3223 loadFlag(@gFlags[FLAG_BLUE]);
3224 end;
3225 ///// /////
3227 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3228 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3229 begin
3230 // Î÷êè Êðàñíîé êîìàíäû
3231 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
3232 // Î÷êè Ñèíåé êîìàíäû
3233 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
3234 end;
3235 ///// /////
3236 end;
3239 // trace liquid, stepping by `dx` and `dy`
3240 // return last seen liquid coords, and `false` if we're started outside of the liquid
3241 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3242 const
3243 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3244 begin
3245 topx := x;
3246 topy := y;
3247 // started outside of the liquid?
3248 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3249 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
3250 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3251 result := true;
3252 while true do
3253 begin
3254 Inc(x, dx);
3255 Inc(y, dy);
3256 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3257 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
3258 topx := x;
3259 topy := y;
3260 end;
3261 end;
3264 begin
3265 DynWarningCB := mapWarningCB;
3266 end.