DEADSOFTWARE

Refactor: Define constants for LiftType
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 SysUtils, Classes, mempool,
24 e_graphics, g_basic, MAPDEF, g_textures,
25 g_phys, utils, g_panel, g_grid, md5, binheap, xprofiler, xparser, xdynrec;
27 type
28 TMapInfo = record
29 Map: String;
30 Name: String;
31 Description: String;
32 Author: String;
33 MusicName: String;
34 SkyName: String;
35 Height: Word;
36 Width: Word;
37 end;
39 PRespawnPoint = ^TRespawnPoint;
40 TRespawnPoint = record
41 X, Y: Integer;
42 Direction: TDirection;
43 PointType: Byte;
44 end;
46 PFlagPoint = ^TFlagPoint;
47 TFlagPoint = TRespawnPoint;
49 PFlag = ^TFlag;
50 TFlag = record
51 Obj: TObj;
52 RespawnType: Byte;
53 State: Byte;
54 Count: Integer;
55 CaptureTime: LongWord;
56 Animation: TAnimation;
57 Direction: TDirection;
58 end;
60 function g_Map_Load(Res: String): Boolean;
61 function g_Map_GetMapInfo(Res: String): TMapInfo;
62 function g_Map_GetMapsList(WADName: String): SSArray;
63 function g_Map_Exist(Res: String): Boolean;
64 procedure g_Map_Free(freeTextures: Boolean=true);
65 procedure g_Map_Update();
67 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
69 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor); // unaccelerated
70 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
72 procedure g_Map_DrawBack(dx, dy: Integer);
73 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
74 PanelType: Word; b1x3: Boolean=false): Boolean;
75 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
77 procedure g_Map_EnableWallGUID (pguid: Integer);
78 procedure g_Map_DisableWallGUID (pguid: Integer);
79 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
81 // HACK!!!
82 procedure g_Map_EnableWall_XXX (ID: DWORD);
83 procedure g_Map_DisableWall_XXX (ID: DWORD);
84 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
86 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
88 procedure g_Map_ReAdd_DieTriggers();
89 function g_Map_IsSpecialTexture(Texture: String): Boolean;
91 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
92 function g_Map_GetPointCount(PointType: Byte): Word;
94 function g_Map_HaveFlagPoints(): Boolean;
96 procedure g_Map_ResetFlag(Flag: Byte);
97 procedure g_Map_DrawFlags();
99 procedure g_Map_SaveState (st: TStream);
100 procedure g_Map_LoadState (st: TStream);
102 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
104 // returns panel or nil
105 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
106 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
108 // returns panel or nil
109 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
110 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
112 type
113 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
115 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
116 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
118 // trace liquid, stepping by `dx` and `dy`
119 // return last seen liquid coords, and `false` if we're started outside of the liquid
120 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
123 // return `true` from `cb` to stop
124 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
126 procedure g_Map_NetSendInterestingPanels (); // yay!
129 procedure g_Map_ProfilersBegin ();
130 procedure g_Map_ProfilersEnd ();
133 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
136 function g_Map_MinX (): Integer; inline;
137 function g_Map_MinY (): Integer; inline;
138 function g_Map_MaxX (): Integer; inline;
139 function g_Map_MaxY (): Integer; inline;
141 const
142 NNF_NO_NAME = 0;
143 NNF_NAME_BEFORE = 1;
144 NNF_NAME_EQUALS = 2;
145 NNF_NAME_AFTER = 3;
147 function g_Texture_NumNameFindStart(name: String): Boolean;
148 function g_Texture_NumNameFindNext(var newName: String): Byte;
150 const
151 RESPAWNPOINT_PLAYER1 = 1;
152 RESPAWNPOINT_PLAYER2 = 2;
153 RESPAWNPOINT_DM = 3;
154 RESPAWNPOINT_RED = 4;
155 RESPAWNPOINT_BLUE = 5;
157 FLAG_NONE = 0;
158 FLAG_RED = 1;
159 FLAG_BLUE = 2;
160 FLAG_DOM = 3;
162 FLAG_STATE_NONE = 0;
163 FLAG_STATE_NORMAL = 1;
164 FLAG_STATE_DROPPED = 2;
165 FLAG_STATE_CAPTURED = 3;
166 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
167 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
169 FLAG_TIME = 720; // 20 seconds
171 SKY_STRETCH: Single = 1.5;
173 const
174 GridTagInvalid = 0;
176 (* draw order:
177 PANEL_BACK
178 PANEL_STEP
179 PANEL_WALL
180 PANEL_CLOSEDOOR
181 PANEL_ACID1
182 PANEL_ACID2
183 PANEL_WATER
184 PANEL_FORE
185 *)
186 // sorted by draw priority
187 GridTagBack = 1 shl 0; // gRenderBackgrounds
188 GridTagStep = 1 shl 1; // gSteps
189 GridTagWall = 1 shl 2; // gWalls
190 GridTagDoor = 1 shl 3; // gWalls
191 GridTagAcid1 = 1 shl 4; // gAcid1
192 GridTagAcid2 = 1 shl 5; // gAcid2
193 GridTagWater = 1 shl 6; // gWater
194 GridTagFore = 1 shl 7; // gRenderForegrounds
195 // the following are invisible
196 GridTagLift = 1 shl 8; // gLifts
197 GridTagBlockMon = 1 shl 9; // gBlockMon
199 GridTagObstacle = (GridTagStep or GridTagWall or GridTagDoor);
200 GridTagLiquid = (GridTagAcid1 or GridTagAcid2 or GridTagWater);
202 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
205 type
206 TBinHeapPanelDrawCmp = class
207 public
208 class function less (const a, b: TPanel): Boolean; inline;
209 end;
211 TBinHeapPanelDraw = specialize TBinaryHeapBase<TPanel, TBinHeapPanelDrawCmp>;
213 var
214 gWalls: TPanelArray;
215 gRenderBackgrounds: TPanelArray;
216 gRenderForegrounds: TPanelArray;
217 gWater, gAcid1, gAcid2: TPanelArray;
218 gSteps: TPanelArray;
219 gLifts: TPanelArray;
220 gBlockMon: TPanelArray;
221 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
222 //gDOMFlags: array of TFlag;
223 gMapInfo: TMapInfo;
224 gBackSize: TDFPoint;
225 gDoorMap: array of array of DWORD;
226 gLiftMap: array of array of DWORD;
227 gWADHash: TMD5Digest;
228 BackID: DWORD = DWORD(-1);
229 gExternalResources: TStringList;
230 gMovingWallIds: array of Integer = nil;
232 gdbg_map_use_accel_render: Boolean = true;
233 gdbg_map_use_accel_coldet: Boolean = true;
234 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
235 gDrawPanelList: TBinHeapPanelDraw = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
237 gCurrentMap: TDynRecord = nil;
238 gCurrentMapFileName: AnsiString = ''; // so we can skip texture reloading
239 gTestMap: String = '';
242 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
245 type
246 TPanelGrid = specialize TBodyGridBase<TPanel>;
248 var
249 mapGrid: TPanelGrid = nil; // DO NOT USE! public for debugging only!
252 implementation
254 uses
255 {$INCLUDE ../nogl/noGLuses.inc}
256 e_input, g_main, e_log, e_texture, g_items, g_gfx, g_console,
257 g_weapons, g_game, g_sound, e_sound, CONFIG,
258 g_options, g_triggers, g_player,
259 Math, g_monsters, g_saveload, g_language, g_netmsg,
260 sfs, xstreams, hashtable, wadreader,
261 ImagingTypes, Imaging, ImagingUtility,
262 ImagingGif, ImagingNetworkGraphics;
264 const
265 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
266 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
267 FLAG_SIGNATURE = $47414C46; // 'FLAG'
270 // ////////////////////////////////////////////////////////////////////////// //
271 procedure mapWarningCB (const msg: AnsiString; line, col: Integer);
272 begin
273 if (line > 0) then
274 begin
275 e_LogWritefln('parse error at (%s,%s): %s', [line, col, msg], TMsgType.Warning);
276 end
277 else
278 begin
279 e_LogWritefln('parse error: %s', [msg], TMsgType.Warning);
280 end;
281 end;
284 // ////////////////////////////////////////////////////////////////////////// //
285 var
286 panByGUID: array of TPanel = nil;
289 // ////////////////////////////////////////////////////////////////////////// //
290 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
291 begin
292 //if (panByGUID = nil) or (not panByGUID.get(aguid, result)) then result := nil;
293 if (aguid >= 0) and (aguid < Length(panByGUID)) then result := panByGUID[aguid] else result := nil;
294 end;
297 // return `true` from `cb` to stop
298 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
299 var
300 pan: TPanel;
301 begin
302 result := nil;
303 if not assigned(cb) then exit;
304 for pan in panByGUID do
305 begin
306 if cb(pan) then begin result := pan; exit; end;
307 end;
308 end;
311 procedure g_Map_NetSendInterestingPanels ();
312 var
313 pan: TPanel;
314 begin
315 if g_Game_IsServer and g_Game_IsNet then
316 begin
317 for pan in panByGUID do
318 begin
319 if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
320 end;
321 end;
322 end;
325 // ////////////////////////////////////////////////////////////////////////// //
326 function g_Map_MinX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0 else result := 0; end;
327 function g_Map_MinY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0 else result := 0; end;
328 function g_Map_MaxX (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridX0+mapGrid.gridWidth-1 else result := 0; end;
329 function g_Map_MaxY (): Integer; inline; begin if (mapGrid <> nil) then result := mapGrid.gridY0+mapGrid.gridHeight-1 else result := 0; end;
332 // ////////////////////////////////////////////////////////////////////////// //
333 var
334 dfmapdef: TDynMapDef = nil;
337 procedure loadMapDefinition ();
338 var
339 pr: TTextParser = nil;
340 st: TStream = nil;
341 WAD: TWADFile = nil;
342 begin
343 if (dfmapdef <> nil) then exit;
345 try
346 e_LogWritefln('parsing "mapdef.txt"...', []);
347 st := openDiskFileRO(DataDir+'mapdef.txt');
348 e_LogWritefln('found local "%smapdef.txt"', [DataDir]);
349 except
350 st := nil;
351 end;
353 if (st = nil) then
354 begin
355 WAD := TWADFile.Create();
356 if not WAD.ReadFile(GameWAD) then
357 begin
358 //raise Exception.Create('cannot load "game.wad"');
359 st := nil;
360 end
361 else
362 begin
363 st := WAD.openFileStream('mapdef.txt');
364 end;
365 end;
367 try
368 if (st = nil) then
369 begin
370 //raise Exception.Create('cannot open "mapdef.txt"');
371 e_LogWriteln('using default "mapdef.txt"...');
372 pr := TStrTextParser.Create(defaultMapDef);
373 end
374 else
375 begin
376 pr := TFileTextParser.Create(st);
377 end;
378 except on e: Exception do
379 begin
380 e_LogWritefln('something is VERY wrong here! -- ', [e.message]);
381 raise;
382 end;
383 end;
385 try
386 dfmapdef := TDynMapDef.Create(pr);
387 except
388 on e: TDynParseException do
389 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
390 on e: Exception do
391 raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
392 end;
394 st.Free();
395 WAD.Free();
396 end;
399 // ////////////////////////////////////////////////////////////////////////// //
400 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
401 var
402 wst: TSFSMemoryChunkStream = nil;
403 begin
404 result := nil;
405 if (dataLen < 4) then exit;
407 if (dfmapdef = nil) then writeln('need to load mapdef');
408 loadMapDefinition();
409 if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
411 wst := TSFSMemoryChunkStream.Create(data, dataLen);
412 try
413 result := dfmapdef.parseMap(wst);
414 except
415 on e: TDynParseException do
416 begin
417 e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
418 wst.Free();
419 result := nil;
420 exit;
421 end;
422 on e: Exception do
423 begin
424 e_LogWritefln('ERROR: %s', [e.message]);
425 wst.Free();
426 result := nil;
427 exit;
428 end;
429 end;
431 //e_LogWriteln('map parsed.');
432 end;
435 // ////////////////////////////////////////////////////////////////////////// //
436 var
437 NNF_PureName: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
438 NNF_PureExt: String; // extension postfix
439 NNF_FirstNum: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
440 NNF_CurrentNum: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
443 function g_Texture_NumNameFindStart(name: String): Boolean;
444 var
445 i, j: Integer;
447 begin
448 Result := False;
449 NNF_PureName := '';
450 NNF_PureExt := '';
451 NNF_FirstNum := -1;
452 NNF_CurrentNum := -1;
454 for i := Length(name) downto 1 do
455 if (name[i] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
456 begin
457 if i = Length(name) then
458 begin // Íåò öèôð â êîíöå ñòðîêè
459 Exit;
460 end
461 else
462 begin
463 j := i + 1;
464 while (j <= Length(name)) and (name[j] <> '.') do inc(j);
465 NNF_PureName := Copy(name, 1, i);
466 NNF_PureExt := Copy(name, j);
467 name := Copy(name, i + 1, j - i - 1);
468 Break;
469 end;
470 end;
472 // Íå ïåðåâåñòè â ÷èñëî:
473 if not TryStrToInt(name, NNF_FirstNum) then
474 Exit;
476 NNF_CurrentNum := 0;
478 Result := True;
479 end;
482 function g_Texture_NumNameFindNext(var newName: String): Byte;
483 begin
484 if (NNF_PureName = '') or (NNF_CurrentNum < 0) then
485 begin
486 newName := '';
487 Result := NNF_NO_NAME;
488 Exit;
489 end;
491 newName := NNF_PureName + IntToStr(NNF_CurrentNum) + NNF_PureExt;
493 if NNF_CurrentNum < NNF_FirstNum then
494 Result := NNF_NAME_BEFORE
495 else
496 if NNF_CurrentNum > NNF_FirstNum then
497 Result := NNF_NAME_AFTER
498 else
499 Result := NNF_NAME_EQUALS;
501 Inc(NNF_CurrentNum);
502 end;
505 // ////////////////////////////////////////////////////////////////////////// //
506 function panelTypeToTag (panelType: Word): Integer;
507 begin
508 case panelType of
509 PANEL_WALL: result := GridTagWall; // gWalls
510 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
511 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
512 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
513 PANEL_WATER: result := GridTagWater; // gWater
514 PANEL_ACID1: result := GridTagAcid1; // gAcid1
515 PANEL_ACID2: result := GridTagAcid2; // gAcid2
516 PANEL_STEP: result := GridTagStep; // gSteps
517 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
518 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
519 else result := GridTagInvalid;
520 end;
521 end;
524 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
525 begin
526 if (a.tag < b.tag) then begin result := true; exit; end;
527 if (a.tag > b.tag) then begin result := false; exit; end;
528 result := (a.arrIdx < b.arrIdx);
529 end;
531 procedure dplClear ();
532 begin
533 if (gDrawPanelList = nil) then gDrawPanelList := TBinHeapPanelDraw.Create() else gDrawPanelList.clear();
534 end;
537 var
538 Textures: TLevelTextureArray = nil;
539 TextNameHash: THashStrInt = nil; // key: texture name; value: index in `Textures`
540 BadTextNameHash: THashStrInt = nil; // set; so we won't spam with non-existing texture messages
541 RespawnPoints: array of TRespawnPoint;
542 FlagPoints: array[FLAG_RED..FLAG_BLUE] of PFlagPoint;
543 //DOMFlagPoints: Array of TFlagPoint;
546 procedure g_Map_ProfilersBegin ();
547 begin
548 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
549 if (profMapCollision <> nil) then profMapCollision.mainBegin(g_profile_collision);
550 // create sections
551 if g_profile_collision and (profMapCollision <> nil) then
552 begin
553 profMapCollision.sectionBegin('*solids');
554 profMapCollision.sectionEnd();
555 profMapCollision.sectionBegin('liquids');
556 profMapCollision.sectionEnd();
557 end;
558 end;
560 procedure g_Map_ProfilersEnd ();
561 begin
562 if (profMapCollision <> nil) then profMapCollision.mainEnd();
563 end;
566 // wall index in `gWalls` or -1
567 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
568 var
569 ex, ey: Integer;
570 begin
571 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, (GridTagWall or GridTagDoor));
572 if (result <> nil) then
573 begin
574 if (hitx <> nil) then hitx^ := ex;
575 if (hity <> nil) then hity^ := ey;
576 end
577 else
578 begin
579 if (hitx <> nil) then hitx^ := x1;
580 if (hity <> nil) then hity^ := y1;
581 end;
582 end;
584 // returns panel or nil
585 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
586 var
587 ex, ey: Integer;
588 begin
589 result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, tag);
590 if (result <> nil) then
591 begin
592 if (hitx <> nil) then hitx^ := ex;
593 if (hity <> nil) then hity^ := ey;
594 end
595 else
596 begin
597 if (hitx <> nil) then hitx^ := x1;
598 if (hity <> nil) then hity^ := y1;
599 end;
600 end;
603 function xxPanAtPointChecker (pan: TPanel; panelType: Word): Boolean; inline;
604 begin
605 if ((pan.tag and GridTagLift) <> 0) then
606 begin
607 // stop if the lift of the right type
608 result :=
609 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
610 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
611 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
612 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT)));
613 exit;
614 end;
615 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
616 end;
618 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
619 var
620 tagmask: Integer = 0;
621 mwit: PPanel;
622 it: TPanelGrid.Iter;
623 begin
624 result := false;
626 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
627 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
628 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
629 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
630 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
631 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
632 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
634 if (tagmask = 0) then exit;// just in case
636 if ((tagmask and GridTagLift) <> 0) then
637 begin
638 // slow
639 it := mapGrid.forEachAtPoint(x, y, tagmask);
640 for mwit in it do if (xxPanAtPointChecker(mwit^, PanelType)) then begin result := true; break; end;
641 end
642 else
643 begin
644 // fast
645 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true);
646 result := (it.length <> 0); // firsthit
647 end;
648 it.release();
649 end;
652 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
653 var
654 it: TPanelGrid.Iter;
655 begin
656 result := nil;
657 if (tagmask = 0) then exit;
658 it := mapGrid.forEachAtPoint(x, y, tagmask, false, true); // firsthit
659 if (it.length <> 0) then result := it.first^;
660 it.release();
661 end;
664 function g_Map_IsSpecialTexture(Texture: String): Boolean;
665 begin
666 Result := (Texture = TEXTURE_NAME_WATER) or
667 (Texture = TEXTURE_NAME_ACID1) or
668 (Texture = TEXTURE_NAME_ACID2);
669 end;
671 procedure CreateDoorMap();
672 var
673 PanelArray: Array of record
674 X, Y: Integer;
675 Width, Height: Word;
676 Active: Boolean;
677 PanelID: DWORD;
678 end;
679 a, b, c, m, i, len: Integer;
680 ok: Boolean;
681 begin
682 if gWalls = nil then
683 Exit;
685 i := 0;
686 len := 128;
687 SetLength(PanelArray, len);
689 for a := 0 to High(gWalls) do
690 if gWalls[a].Door then
691 begin
692 PanelArray[i].X := gWalls[a].X;
693 PanelArray[i].Y := gWalls[a].Y;
694 PanelArray[i].Width := gWalls[a].Width;
695 PanelArray[i].Height := gWalls[a].Height;
696 PanelArray[i].Active := True;
697 PanelArray[i].PanelID := a;
699 i := i + 1;
700 if i = len then
701 begin
702 len := len + 128;
703 SetLength(PanelArray, len);
704 end;
705 end;
707 // Íåò äâåðåé:
708 if i = 0 then
709 begin
710 PanelArray := nil;
711 Exit;
712 end;
714 SetLength(gDoorMap, 0);
716 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
718 for a := 0 to i-1 do
719 if PanelArray[a].Active then
720 begin
721 PanelArray[a].Active := False;
722 m := Length(gDoorMap);
723 SetLength(gDoorMap, m+1);
724 SetLength(gDoorMap[m], 1);
725 gDoorMap[m, 0] := PanelArray[a].PanelID;
726 ok := True;
728 while ok do
729 begin
730 ok := False;
732 for b := 0 to i-1 do
733 if PanelArray[b].Active then
734 for c := 0 to High(gDoorMap[m]) do
735 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
736 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
737 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
738 PanelArray[b].Width, PanelArray[b].Height,
739 gWalls[gDoorMap[m, c]].X,
740 gWalls[gDoorMap[m, c]].Y,
741 gWalls[gDoorMap[m, c]].Width,
742 gWalls[gDoorMap[m, c]].Height) then
743 begin
744 PanelArray[b].Active := False;
745 SetLength(gDoorMap[m],
746 Length(gDoorMap[m])+1);
747 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
748 ok := True;
749 Break;
750 end;
751 end;
753 g_Game_StepLoading();
754 end;
756 PanelArray := nil;
757 end;
759 procedure CreateLiftMap();
760 var
761 PanelArray: Array of record
762 X, Y: Integer;
763 Width, Height: Word;
764 Active: Boolean;
765 end;
766 a, b, c, len, i, j: Integer;
767 ok: Boolean;
768 begin
769 if gLifts = nil then
770 Exit;
772 len := Length(gLifts);
773 SetLength(PanelArray, len);
775 for a := 0 to len-1 do
776 begin
777 PanelArray[a].X := gLifts[a].X;
778 PanelArray[a].Y := gLifts[a].Y;
779 PanelArray[a].Width := gLifts[a].Width;
780 PanelArray[a].Height := gLifts[a].Height;
781 PanelArray[a].Active := True;
782 end;
784 SetLength(gLiftMap, len);
785 i := 0;
787 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
789 for a := 0 to len-1 do
790 if PanelArray[a].Active then
791 begin
792 PanelArray[a].Active := False;
793 SetLength(gLiftMap[i], 32);
794 j := 0;
795 gLiftMap[i, j] := a;
796 ok := True;
798 while ok do
799 begin
800 ok := False;
801 for b := 0 to len-1 do
802 if PanelArray[b].Active then
803 for c := 0 to j do
804 if g_CollideAround(PanelArray[b].X,
805 PanelArray[b].Y,
806 PanelArray[b].Width,
807 PanelArray[b].Height,
808 PanelArray[gLiftMap[i, c]].X,
809 PanelArray[gLiftMap[i, c]].Y,
810 PanelArray[gLiftMap[i, c]].Width,
811 PanelArray[gLiftMap[i, c]].Height) then
812 begin
813 PanelArray[b].Active := False;
814 j := j+1;
815 if j > High(gLiftMap[i]) then
816 SetLength(gLiftMap[i],
817 Length(gLiftMap[i])+32);
819 gLiftMap[i, j] := b;
820 ok := True;
822 Break;
823 end;
824 end;
826 SetLength(gLiftMap[i], j+1);
827 i := i+1;
829 g_Game_StepLoading();
830 end;
832 SetLength(gLiftMap, i);
834 PanelArray := nil;
835 end;
837 function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
838 var
839 len: Integer;
840 panels: ^TPanelArray;
841 pan: TPanel;
842 pguid: Integer;
843 begin
844 Result := -1;
846 case PanelRec.PanelType of
847 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: panels := @gWalls;
848 PANEL_BACK: panels := @gRenderBackgrounds;
849 PANEL_FORE: panels := @gRenderForegrounds;
850 PANEL_WATER: panels := @gWater;
851 PANEL_ACID1: panels := @gAcid1;
852 PANEL_ACID2: panels := @gAcid2;
853 PANEL_STEP: panels := @gSteps;
854 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: panels := @gLifts;
855 PANEL_BLOCKMON: panels := @gBlockMon;
856 else exit;
857 end;
859 len := Length(panels^);
860 SetLength(panels^, len+1);
862 pguid := Length(panByGUID);
863 SetLength(panByGUID, pguid+1); //FIXME!
864 pan := TPanel.Create(PanelRec, AddTextures, CurTex, Textures, pguid);
865 assert(pguid >= 0);
866 assert(pguid < Length(panByGUID));
867 panByGUID[pguid] := pan;
868 panels^[len] := pan;
869 pan.arrIdx := len;
870 pan.proxyId := -1;
871 pan.tag := panelTypeToTag(PanelRec.PanelType);
873 PanelRec.user['panel_guid'] := pguid;
875 //result := len;
876 result := pguid;
877 end;
880 function CreateNullTexture(RecName: String): Integer;
881 begin
882 RecName := toLowerCase1251(RecName);
883 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
884 if TextNameHash.get(RecName, result) then exit; // i found her!
886 SetLength(Textures, Length(Textures)+1);
887 result := High(Textures);
889 with Textures[High(Textures)] do
890 begin
891 TextureName := RecName;
892 Width := 1;
893 Height := 1;
894 Anim := False;
895 TextureID := LongWord(TEXTURE_NONE);
896 end;
898 TextNameHash.put(RecName, result);
899 end;
902 function CreateTexture(RecName: AnsiString; Map: string; log: Boolean): Integer;
903 var
904 WAD: TWADFile;
905 TextureData: Pointer;
906 WADName: String;
907 a, ResLength: Integer;
908 begin
909 RecName := toLowerCase1251(RecName);
910 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
911 if TextNameHash.get(RecName, result) then
912 begin
913 // i found her!
914 //e_LogWritefln('texture ''%s'' already loaded (%s)', [RecName, result]);
915 exit;
916 end;
918 Result := -1;
920 if (BadTextNameHash <> nil) and BadTextNameHash.has(RecName) then exit; // don't do it again and again
923 if Textures <> nil then
924 begin
925 for a := 0 to High(Textures) do
926 begin
927 if (Textures[a].TextureName = RecName) then
928 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
929 e_LogWritefln('texture ''%s'' already loaded', [RecName]);
930 Result := a;
931 Exit;
932 end;
933 end;
934 end;
937 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
938 if (RecName = TEXTURE_NAME_WATER) or
939 (RecName = TEXTURE_NAME_ACID1) or
940 (RecName = TEXTURE_NAME_ACID2) then
941 begin
942 SetLength(Textures, Length(Textures)+1);
944 with Textures[High(Textures)] do
945 begin
946 TextureName := RecName;
947 if (TextureName = TEXTURE_NAME_WATER) then TextureID := LongWord(TEXTURE_SPECIAL_WATER)
948 else if (TextureName = TEXTURE_NAME_ACID1) then TextureID := LongWord(TEXTURE_SPECIAL_ACID1)
949 else if (TextureName = TEXTURE_NAME_ACID2) then TextureID := LongWord(TEXTURE_SPECIAL_ACID2);
951 Anim := False;
952 end;
954 result := High(Textures);
955 TextNameHash.put(RecName, result);
956 Exit;
957 end;
959 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
960 WADName := g_ExtractWadName(RecName);
962 WAD := TWADFile.Create();
964 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
966 WAD.ReadFile(WADName);
968 //txname := RecName;
970 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
971 begin
972 FreeMem(TextureData);
973 RecName := 'COMMON\ALIEN';
974 end;
977 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength, log) then
978 begin
979 SetLength(Textures, Length(Textures)+1);
980 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
981 begin
982 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
983 SetLength(Textures, Length(Textures)-1);
984 result := -1;
985 Exit;
986 end;
987 e_GetTextureSize(Textures[High(Textures)].TextureID, @Textures[High(Textures)].Width, @Textures[High(Textures)].Height);
988 FreeMem(TextureData);
989 Textures[High(Textures)].TextureName := RecName;
990 Textures[High(Textures)].Anim := False;
992 result := High(Textures);
993 TextNameHash.put(RecName, result);
994 end
995 else // Íåò òàêîãî ðåóñðñà â WAD'å
996 begin
997 //e_WriteLog(Format('SHIT! Error loading texture %s : %s', [RecName, g_ExtractFilePathName(RecName)]), MSG_WARNING);
998 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
999 if log and (not BadTextNameHash.get(RecName, a)) then
1000 begin
1001 e_WriteLog(Format('Error loading texture %s', [RecName]), TMsgType.Warning);
1002 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1003 end;
1004 BadTextNameHash.put(RecName, -1);
1005 end;
1007 WAD.Free();
1008 end;
1011 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
1012 var
1013 WAD: TWADFile;
1014 TextureWAD: PChar = nil;
1015 TextData: Pointer = nil;
1016 TextureData: Pointer = nil;
1017 cfg: TConfig = nil;
1018 WADName: String;
1019 ResLength: Integer;
1020 TextureResource: String;
1021 _width, _height, _framecount, _speed: Integer;
1022 _backanimation: Boolean;
1023 //imgfmt: string;
1024 ia: TDynImageDataArray = nil;
1025 f, c, frdelay, frloop: Integer;
1026 begin
1027 RecName := toLowerCase1251(RecName);
1028 if (TextNameHash = nil) then TextNameHash := THashStrInt.Create();
1029 if TextNameHash.get(RecName, result) then
1030 begin
1031 // i found her!
1032 //e_LogWritefln('animated texture ''%s'' already loaded (%s)', [RecName, result]);
1033 exit;
1034 end;
1036 result := -1;
1038 //e_LogWritefln('*** Loading animated texture "%s"', [RecName]);
1040 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1041 if BadTextNameHash.get(RecName, f) then
1042 begin
1043 //e_WriteLog(Format('no animation texture %s (don''t worry)', [RecName]), MSG_NOTIFY);
1044 exit;
1045 end;
1047 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
1048 WADName := g_ExtractWadName(RecName);
1050 WAD := TWADFile.Create();
1051 try
1052 if WADName <> '' then WADName := GameDir+'/wads/'+WADName else WADName := Map;
1054 WAD.ReadFile(WADName);
1056 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength, log) then
1057 begin
1058 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1059 if log and (not BadTextNameHash.get(RecName, f)) then
1060 begin
1061 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1062 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
1063 end;
1064 BadTextNameHash.put(RecName, -1);
1065 exit;
1066 end;
1068 {TEST
1069 if WADName = Map then
1070 begin
1071 //FreeMem(TextureWAD);
1072 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
1073 end;
1076 WAD.FreeWAD();
1078 if ResLength < 6 then
1079 begin
1080 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), TMsgType.Warning);
1081 BadTextNameHash.put(RecName, -1);
1082 exit;
1083 end;
1085 // ýòî ïòèöà? ýòî ñàìîë¸ò?
1086 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
1087 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
1088 begin
1089 // íåò, ýòî ñóïåðìåí!
1090 if not WAD.ReadMemory(TextureWAD, ResLength) then
1091 begin
1092 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), TMsgType.Warning);
1093 BadTextNameHash.put(RecName, -1);
1094 exit;
1095 end;
1097 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1098 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1099 begin
1100 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), TMsgType.Warning);
1101 BadTextNameHash.put(RecName, -1);
1102 exit;
1103 end;
1105 cfg := TConfig.CreateMem(TextData, ResLength);
1107 TextureResource := cfg.ReadStr('', 'resource', '');
1108 if TextureResource = '' then
1109 begin
1110 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), TMsgType.Warning);
1111 BadTextNameHash.put(RecName, -1);
1112 exit;
1113 end;
1115 _width := cfg.ReadInt('', 'framewidth', 0);
1116 _height := cfg.ReadInt('', 'frameheight', 0);
1117 _framecount := cfg.ReadInt('', 'framecount', 0);
1118 _speed := cfg.ReadInt('', 'waitcount', 0);
1119 _backanimation := cfg.ReadBool('', 'backanimation', False);
1121 cfg.Free();
1122 cfg := nil;
1124 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1125 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1126 begin
1127 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), TMsgType.Warning);
1128 BadTextNameHash.put(RecName, -1);
1129 exit;
1130 end;
1132 WAD.Free();
1133 WAD := nil;
1135 SetLength(Textures, Length(Textures)+1);
1136 with Textures[High(Textures)] do
1137 begin
1138 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1139 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1140 begin
1141 TextureName := RecName;
1142 Width := _width;
1143 Height := _height;
1144 Anim := True;
1145 FramesCount := _framecount;
1146 Speed := _speed;
1147 result := High(Textures);
1148 TextNameHash.put(RecName, result);
1149 end
1150 else
1151 begin
1152 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1153 if log and (not BadTextNameHash.get(RecName, f)) then
1154 begin
1155 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1156 end;
1157 BadTextNameHash.put(RecName, -1);
1158 end;
1159 end;
1160 end
1161 else
1162 begin
1163 // try animated image
1165 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1166 if length(imgfmt) = 0 then
1167 begin
1168 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1169 exit;
1170 end;
1172 GlobalMetadata.ClearMetaItems();
1173 GlobalMetadata.ClearMetaItemsForSaving();
1174 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1175 begin
1176 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), TMsgType.Warning);
1177 BadTextNameHash.put(RecName, -1);
1178 exit;
1179 end;
1180 if length(ia) = 0 then
1181 begin
1182 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), TMsgType.Warning);
1183 BadTextNameHash.put(RecName, -1);
1184 exit;
1185 end;
1187 WAD.Free();
1188 WAD := nil;
1190 _width := ia[0].width;
1191 _height := ia[0].height;
1192 _framecount := length(ia);
1193 _speed := 1;
1194 _backanimation := false;
1195 frdelay := -1;
1196 frloop := -666;
1197 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1198 begin
1199 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1200 try
1201 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1202 frdelay := f;
1203 if f < 0 then f := 0;
1204 // rounding ;-)
1205 c := f mod 28;
1206 if c < 13 then c := 0 else c := 1;
1207 f := (f div 28)+c;
1208 if f < 1 then f := 1 else if f > 255 then f := 255;
1209 _speed := f;
1210 except
1211 end;
1212 end;
1213 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1214 begin
1215 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1216 try
1217 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1218 frloop := f;
1219 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1220 except
1221 end;
1222 end;
1223 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1224 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1225 f := ord(_backanimation);
1226 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);
1228 SetLength(Textures, Length(Textures)+1);
1229 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1230 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1231 begin
1232 Textures[High(Textures)].TextureName := RecName;
1233 Textures[High(Textures)].Width := _width;
1234 Textures[High(Textures)].Height := _height;
1235 Textures[High(Textures)].Anim := True;
1236 Textures[High(Textures)].FramesCount := length(ia);
1237 Textures[High(Textures)].Speed := _speed;
1238 result := High(Textures);
1239 TextNameHash.put(RecName, result);
1240 //writeln(' CREATED!');
1241 end
1242 else
1243 begin
1244 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1245 if log and (not BadTextNameHash.get(RecName, f)) then
1246 begin
1247 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), TMsgType.Warning);
1248 end;
1249 BadTextNameHash.put(RecName, -1);
1250 end;
1251 end;
1252 finally
1253 for f := 0 to High(ia) do FreeImage(ia[f]);
1254 WAD.Free();
1255 cfg.Free();
1256 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1257 if (TextData <> nil) then FreeMem(TextData);
1258 if (TextureData <> nil) then FreeMem(TextureData);
1259 end;
1260 end;
1262 procedure CreateItem(Item: TDynRecord);
1263 begin
1264 if g_Game_IsClient then Exit;
1266 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1267 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1268 Exit;
1270 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1271 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1272 end;
1274 procedure CreateArea(Area: TDynRecord);
1275 var
1276 a: Integer;
1277 id: DWORD = 0;
1278 begin
1279 case Area.AreaType of
1280 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1281 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1282 begin
1283 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1284 with RespawnPoints[High(RespawnPoints)] do
1285 begin
1286 X := Area.X;
1287 Y := Area.Y;
1288 Direction := TDirection(Area.Direction);
1290 case Area.AreaType of
1291 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1292 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1293 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1294 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1295 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1296 end;
1297 end;
1298 end;
1300 AREA_REDFLAG, AREA_BLUEFLAG:
1301 begin
1302 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1304 if FlagPoints[a] <> nil then Exit;
1306 New(FlagPoints[a]);
1308 with FlagPoints[a]^ do
1309 begin
1310 X := Area.X-FLAGRECT.X;
1311 Y := Area.Y-FLAGRECT.Y;
1312 Direction := TDirection(Area.Direction);
1313 end;
1315 with gFlags[a] do
1316 begin
1317 case a of
1318 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1319 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1320 end;
1322 Animation := TAnimation.Create(id, True, 8);
1323 Obj.Rect := FLAGRECT;
1325 g_Map_ResetFlag(a);
1326 end;
1327 end;
1329 AREA_DOMFLAG:
1330 begin
1331 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1332 with DOMFlagPoints[High(DOMFlagPoints)] do
1333 begin
1334 X := Area.X;
1335 Y := Area.Y;
1336 Direction := TDirection(Area.Direction);
1337 end;
1339 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1340 end;
1341 end;
1342 end;
1344 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1345 var
1346 _trigger: TTrigger;
1347 tp: TPanel;
1348 begin
1349 result := -1;
1350 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1352 with _trigger do
1353 begin
1354 mapId := Trigger.id;
1355 mapIndex := amapIdx;
1356 X := Trigger.X;
1357 Y := Trigger.Y;
1358 Width := Trigger.Width;
1359 Height := Trigger.Height;
1360 Enabled := Trigger.Enabled;
1361 TexturePanelGUID := atpanid;
1362 TriggerType := Trigger.TriggerType;
1363 ActivateType := Trigger.ActivateType;
1364 Keys := Trigger.Keys;
1365 trigPanelGUID := atrigpanid;
1366 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1367 if TexturePanelGUID <> -1 then
1368 begin
1369 tp := g_Map_PanelByGUID(TexturePanelGUID);
1370 if (tp <> nil) then tp.hasTexTrigger := True;
1371 end;
1372 end;
1374 result := Integer(g_Triggers_Create(_trigger, Trigger));
1375 end;
1377 procedure CreateMonster(monster: TDynRecord);
1378 var
1379 a: Integer;
1380 mon: TMonster;
1381 begin
1382 if g_Game_IsClient then Exit;
1384 if (gGameSettings.GameType = GT_SINGLE)
1385 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1386 begin
1387 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1389 if gTriggers <> nil then
1390 begin
1391 for a := 0 to High(gTriggers) do
1392 begin
1393 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1394 begin
1395 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1396 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1397 end;
1398 end;
1399 end;
1401 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1402 end;
1403 end;
1405 procedure g_Map_ReAdd_DieTriggers();
1407 function monsDieTrig (mon: TMonster): Boolean;
1408 var
1409 a: Integer;
1410 //tw: TStrTextWriter;
1411 begin
1412 result := false; // don't stop
1413 mon.ClearTriggers();
1414 for a := 0 to High(gTriggers) do
1415 begin
1416 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1417 begin
1418 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1420 tw := TStrTextWriter.Create();
1421 try
1422 gTriggers[a].trigData.writeTo(tw);
1423 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1424 finally
1425 tw.Free();
1426 end;
1428 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1429 end;
1430 end;
1431 end;
1433 begin
1434 if g_Game_IsClient then Exit;
1436 g_Mons_ForEach(monsDieTrig);
1437 end;
1439 function extractWadName(resourceName: string): string;
1440 var
1441 posN: Integer;
1442 begin
1443 posN := Pos(':', resourceName);
1444 if posN > 0 then
1445 Result:= Copy(resourceName, 0, posN-1)
1446 else
1447 Result := '';
1448 end;
1450 procedure addResToExternalResList(res: string);
1451 begin
1452 res := extractWadName(res);
1453 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1454 gExternalResources.Add(res);
1455 end;
1457 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1458 //var
1459 //textures: TTexturesRec1Array;
1460 //textures: TDynField;
1461 //trec: TDynRecord;
1462 //mapHeader: TMapHeaderRec_1;
1463 //i: integer;
1464 //resFile: String = '';
1465 begin
1466 if gExternalResources = nil then
1467 gExternalResources := TStringList.Create;
1469 gExternalResources.Clear;
1471 (*
1473 textures := GetTextures(map);
1474 for i := 0 to High(textures) do
1475 begin
1476 addResToExternalResList(resFile);
1477 end;
1480 textures := map['texture'];
1481 if (textures <> nil) then
1482 begin
1483 for trec in textures do
1484 begin
1485 addResToExternalResList(resFile);
1486 end;
1487 end;
1489 textures := nil;
1490 *)
1492 //mapHeader := GetMapHeader(map);
1494 addResToExternalResList(map.MusicName);
1495 addResToExternalResList(map.SkyName);
1496 end;
1499 procedure mapCreateGrid ();
1500 var
1501 mapX0: Integer = $3fffffff;
1502 mapY0: Integer = $3fffffff;
1503 mapX1: Integer = -$3fffffff;
1504 mapY1: Integer = -$3fffffff;
1506 procedure calcBoundingBox (constref panels: TPanelArray);
1507 var
1508 idx: Integer;
1509 pan: TPanel;
1510 begin
1511 for idx := 0 to High(panels) do
1512 begin
1513 pan := panels[idx];
1514 if not pan.visvalid then continue;
1515 if (pan.Width < 1) or (pan.Height < 1) then continue;
1516 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1517 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1518 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1519 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1520 end;
1521 end;
1523 procedure addPanelsToGrid (constref panels: TPanelArray);
1524 var
1525 idx: Integer;
1526 pan: TPanel;
1527 newtag: Integer;
1528 begin
1529 //tag := panelTypeToTag(tag);
1530 for idx := 0 to High(panels) do
1531 begin
1532 pan := panels[idx];
1533 if not pan.visvalid then continue;
1534 if (pan.proxyId <> -1) then
1535 begin
1536 {$IF DEFINED(D2F_DEBUG)}
1537 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);
1538 {$ENDIF}
1539 continue;
1540 end;
1541 case pan.PanelType of
1542 PANEL_WALL: newtag := GridTagWall;
1543 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1544 PANEL_BACK: newtag := GridTagBack;
1545 PANEL_FORE: newtag := GridTagFore;
1546 PANEL_WATER: newtag := GridTagWater;
1547 PANEL_ACID1: newtag := GridTagAcid1;
1548 PANEL_ACID2: newtag := GridTagAcid2;
1549 PANEL_STEP: newtag := GridTagStep;
1550 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1551 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1552 else continue; // oops
1553 end;
1554 pan.tag := newtag;
1556 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1557 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1558 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1559 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1561 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1562 begin
1563 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1564 end;
1566 {$ENDIF}
1567 end;
1568 end;
1570 begin
1571 mapGrid.Free();
1572 mapGrid := nil;
1574 calcBoundingBox(gWalls);
1575 calcBoundingBox(gRenderBackgrounds);
1576 calcBoundingBox(gRenderForegrounds);
1577 calcBoundingBox(gWater);
1578 calcBoundingBox(gAcid1);
1579 calcBoundingBox(gAcid2);
1580 calcBoundingBox(gSteps);
1581 calcBoundingBox(gLifts);
1582 calcBoundingBox(gBlockMon);
1584 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1586 if (mapX0 > 0) then mapX0 := 0;
1587 if (mapY0 > 0) then mapY0 := 0;
1589 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1590 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1592 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1593 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1595 addPanelsToGrid(gWalls);
1596 addPanelsToGrid(gRenderBackgrounds);
1597 addPanelsToGrid(gRenderForegrounds);
1598 addPanelsToGrid(gWater);
1599 addPanelsToGrid(gAcid1);
1600 addPanelsToGrid(gAcid2);
1601 addPanelsToGrid(gSteps);
1602 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1603 addPanelsToGrid(gBlockMon);
1605 mapGrid.dumpStats();
1607 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1608 end;
1611 function g_Map_Load(Res: String): Boolean;
1612 const
1613 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1614 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1615 type
1616 PTRec = ^TTRec;
1617 TTRec = record
1618 //TexturePanel: Integer;
1619 tnum: Integer;
1620 id: Integer;
1621 trigrec: TDynRecord;
1622 // texture pane;
1623 texPanelIdx: Integer;
1624 texPanel: TDynRecord;
1625 // "action" panel
1626 actPanelIdx: Integer;
1627 actPanel: TDynRecord;
1628 end;
1629 var
1630 WAD, TestWAD: TWADFile;
1631 //mapReader: TDynRecord = nil;
1632 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1633 panels: TDynField = nil; //TPanelsRec1Array;
1634 items: TDynField = nil; //TItemsRec1Array;
1635 monsters: TDynField = nil; //TMonsterRec1Array;
1636 areas: TDynField = nil; //TAreasRec1Array;
1637 triggers: TDynField = nil; //TTriggersRec1Array;
1638 b, c, k: Integer;
1639 PanelID: DWORD;
1640 AddTextures: TAddTextureArray;
1641 TriggersTable: array of TTRec;
1642 FileName, mapResName, TexName, s: AnsiString;
1643 Data: Pointer;
1644 Len: Integer;
1645 ok, isAnim: Boolean;
1646 CurTex, ntn: Integer;
1647 rec, texrec: TDynRecord;
1648 pttit: PTRec;
1649 pannum, trignum, cnt, tgpid: Integer;
1650 stt: UInt64;
1651 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1652 //moveActive: Boolean;
1653 pan: TPanel;
1654 mapOk: Boolean = false;
1655 usedTextures: THashStrInt = nil; // key: mapTextureList
1656 begin
1657 mapGrid.Free();
1658 mapGrid := nil;
1659 TestWAD := nil;
1660 Data := nil;
1662 //gCurrentMap.Free();
1663 //gCurrentMap := nil;
1665 panByGUID := nil;
1667 Result := False;
1668 gMapInfo.Map := Res;
1669 TriggersTable := nil;
1670 //mapReader := nil;
1672 sfsGCDisable(); // temporary disable removing of temporary volumes
1673 try
1674 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1675 if (gCurrentMap = nil) then
1676 begin
1677 FileName := g_ExtractWadName(Res);
1678 e_WriteLog('Loading map WAD: '+FileName, TMsgType.Notify);
1679 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1681 WAD := TWADFile.Create();
1682 if not WAD.ReadFile(FileName) then
1683 begin
1684 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1685 WAD.Free();
1686 Exit;
1687 end;
1689 if gTestMap <> '' then
1690 begin
1691 s := g_ExtractWadName(gTestMap);
1692 TestWAD := TWADFile.Create();
1693 if not TestWAD.ReadFile(s) then
1694 begin
1695 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1696 TestWAD.Free();
1697 TestWAD := nil;
1698 end;
1699 end;
1701 if TestWAD <> nil then
1702 begin
1703 mapResName := g_ExtractFileName(gTestMap);
1704 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1705 begin
1706 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1707 Data := nil;
1708 end else
1709 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1710 TestWAD.Free();
1711 TestWAD := nil;
1712 end;
1714 if Data = nil then
1715 begin
1716 //k8: why loader ignores path here?
1717 mapResName := g_ExtractFileName(Res);
1718 if not WAD.GetMapResource(mapResName, Data, Len) then
1719 begin
1720 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1721 WAD.Free();
1722 Exit;
1723 end;
1724 end;
1726 WAD.Free();
1728 if (Len < 4) then
1729 begin
1730 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1731 FreeMem(Data);
1732 exit;
1733 end;
1735 // Çàãðóçêà êàðòû:
1736 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1737 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1739 stt := getTimeMicro();
1741 try
1742 gCurrentMap := g_Map_ParseMap(Data, Len);
1743 except
1744 gCurrentMap.Free();
1745 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1746 FreeMem(Data);
1747 gCurrentMapFileName := '';
1748 Exit;
1749 end;
1751 FreeMem(Data);
1753 if (gCurrentMap = nil) then
1754 begin
1755 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1756 gCurrentMapFileName := '';
1757 exit;
1758 end;
1759 end
1760 else
1761 begin
1762 stt := getTimeMicro();
1763 end;
1765 //gCurrentMap := mapReader;
1767 generateExternalResourcesList(gCurrentMap);
1768 mapTextureList := gCurrentMap['texture'];
1769 // get all other lists here too
1770 panels := gCurrentMap['panel'];
1771 triggers := gCurrentMap['trigger'];
1772 items := gCurrentMap['item'];
1773 areas := gCurrentMap['area'];
1774 monsters := gCurrentMap['monster'];
1776 // Çàãðóçêà îïèñàíèÿ êàðòû:
1777 e_WriteLog(' Reading map info...', TMsgType.Notify);
1778 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1780 with gMapInfo do
1781 begin
1782 Name := gCurrentMap.MapName;
1783 Description := gCurrentMap.MapDesc;
1784 Author := gCurrentMap.MapAuthor;
1785 MusicName := gCurrentMap.MusicName;
1786 SkyName := gCurrentMap.SkyName;
1787 Height := gCurrentMap.Height;
1788 Width := gCurrentMap.Width;
1789 end;
1791 // Çàãðóçêà òåêñòóð:
1792 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1793 // Äîáàâëåíèå òåêñòóð â Textures[]:
1794 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1795 begin
1796 e_WriteLog(' Loading textures:', TMsgType.Notify);
1797 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1799 // find used textures
1800 usedTextures := THashStrInt.Create();
1801 try
1802 if (panels <> nil) and (panels.count > 0) then
1803 begin
1804 for rec in panels do
1805 begin
1806 texrec := rec.TextureRec;
1807 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1808 end;
1809 end;
1811 cnt := -1;
1812 for rec in mapTextureList do
1813 begin
1814 Inc(cnt);
1815 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1816 begin
1817 rec.tagInt := -1; // just in case
1818 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1819 end
1820 else
1821 begin
1822 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1823 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1824 {$ENDIF}
1825 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1826 if rec.Anim then
1827 begin
1828 // Àíèìèðîâàííàÿ òåêñòóðà
1829 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1830 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]));
1831 end
1832 else
1833 begin
1834 // Îáû÷íàÿ òåêñòóðà
1835 ntn := CreateTexture(rec.Resource, FileName, True);
1836 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1837 end;
1838 if (ntn < 0) then ntn := CreateNullTexture(rec.Resource);
1840 rec.tagInt := ntn; // remember texture number
1841 end;
1842 g_Game_StepLoading();
1843 end;
1844 finally
1845 usedTextures.Free();
1846 end;
1848 // set panel tagInt to texture index
1849 if (panels <> nil) then
1850 begin
1851 for rec in panels do
1852 begin
1853 texrec := rec.TextureRec;
1854 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1855 end;
1856 end;
1857 end;
1860 // Çàãðóçêà òðèããåðîâ
1861 gTriggerClientID := 0;
1862 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1863 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1865 // Çàãðóçêà ïàíåëåé
1866 e_WriteLog(' Loading panels...', TMsgType.Notify);
1867 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1869 // check texture numbers for panels
1870 if (panels <> nil) and (panels.count > 0) then
1871 begin
1872 for rec in panels do
1873 begin
1874 if (rec.tagInt < 0) then
1875 begin
1876 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1877 result := false;
1878 gCurrentMap.Free();
1879 gCurrentMap := nil;
1880 gCurrentMapFileName := '';
1881 exit;
1882 end;
1883 end;
1884 end;
1886 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1887 if (triggers <> nil) and (triggers.count > 0) then
1888 begin
1889 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1890 //SetLength(TriggersTable, triggers.count);
1891 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1893 SetLength(TriggersTable, triggers.count);
1894 trignum := -1;
1895 for rec in triggers do
1896 begin
1897 Inc(trignum);
1898 pttit := @TriggersTable[trignum];
1899 pttit.trigrec := rec;
1900 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1901 pttit.texPanelIdx := -1; // will be fixed later
1902 pttit.texPanel := rec.TexturePanelRec;
1903 // action panel
1904 pttit.actPanelIdx := -1;
1905 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1906 // set flag
1907 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1908 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1909 // update progress
1910 g_Game_StepLoading();
1911 end;
1912 end;
1914 // Ñîçäàåì ïàíåëè
1915 if (panels <> nil) and (panels.count > 0) then
1916 begin
1917 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1918 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1920 pannum := -1;
1921 for rec in panels do
1922 begin
1923 Inc(pannum);
1924 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1925 texrec := nil;
1926 SetLength(AddTextures, 0);
1927 CurTex := -1;
1928 ok := false;
1930 if (mapTextureList <> nil) then
1931 begin
1932 texrec := rec.TextureRec;
1933 ok := (texrec <> nil);
1934 end;
1936 if ok then
1937 begin
1938 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1939 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1940 ok := false;
1941 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1942 begin
1943 if rec.userPanelTrigRef then
1944 begin
1945 // e_LogWritefln('trigref for panel %s', [pannum]);
1946 ok := True;
1947 end;
1948 end;
1949 end;
1951 if ok then
1952 begin
1953 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1954 s := texrec.Resource;
1956 // Ñïåö-òåêñòóðû çàïðåùåíû
1957 if g_Map_IsSpecialTexture(s) then
1958 begin
1959 ok := false
1960 end
1961 else
1962 begin
1963 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1964 ok := g_Texture_NumNameFindStart(s);
1965 end;
1967 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1968 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1969 if ok then
1970 begin
1971 k := NNF_NAME_BEFORE;
1972 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1973 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1974 begin
1975 k := g_Texture_NumNameFindNext(TexName);
1977 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1978 begin
1979 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1980 if texrec.Anim then
1981 begin
1982 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1983 isAnim := True;
1984 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1985 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1986 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1987 if not ok then
1988 begin
1989 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1990 isAnim := False;
1991 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1992 ok := CreateTexture(TexName, FileName, False) >= 0;
1993 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1994 end;
1995 end
1996 else
1997 begin
1998 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1999 isAnim := False;
2000 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2001 ok := CreateTexture(TexName, FileName, False) >= 0;
2002 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2003 if not ok then
2004 begin
2005 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
2006 isAnim := True;
2007 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2008 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2009 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2010 end;
2011 end;
2013 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
2014 if ok then
2015 begin
2017 for c := 0 to High(Textures) do
2018 begin
2019 if (Textures[c].TextureName = TexName) then
2020 begin
2021 SetLength(AddTextures, Length(AddTextures)+1);
2022 AddTextures[High(AddTextures)].Texture := c;
2023 AddTextures[High(AddTextures)].Anim := isAnim;
2024 break;
2025 end;
2026 end;
2028 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
2029 begin
2030 SetLength(AddTextures, Length(AddTextures)+1);
2031 AddTextures[High(AddTextures)].Texture := c;
2032 AddTextures[High(AddTextures)].Anim := isAnim;
2033 end;
2034 end;
2035 end
2036 else
2037 begin
2038 if k = NNF_NAME_EQUALS then
2039 begin
2040 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
2041 SetLength(AddTextures, Length(AddTextures)+1);
2042 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
2043 AddTextures[High(AddTextures)].Anim := texrec.Anim;
2044 CurTex := High(AddTextures);
2045 ok := true;
2046 end
2047 else // NNF_NO_NAME
2048 begin
2049 ok := false;
2050 end;
2051 end;
2052 end; // while ok...
2054 ok := true;
2055 end; // if ok - åñòü ñìåæíûå òåêñòóðû
2056 end; // if ok - ññûëàþòñÿ òðèããåðû
2058 if not ok then
2059 begin
2060 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
2061 SetLength(AddTextures, 1);
2062 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
2063 AddTextures[0].Anim := false;
2064 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
2065 CurTex := 0;
2066 end;
2068 //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);
2070 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
2072 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
2073 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
2074 PanelID := CreatePanel(rec, AddTextures, CurTex);
2075 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
2076 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
2078 // setup lifts
2079 moveSpeed := rec.moveSpeed;
2080 //moveStart := rec.moveStart;
2081 //moveEnd := rec.moveEnd;
2082 //moveActive := rec['move_active'].value;
2083 if not moveSpeed.isZero then
2084 begin
2085 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
2086 gMovingWallIds[High(gMovingWallIds)] := PanelID;
2087 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
2088 end;
2090 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2092 g_Game_StepLoading();
2093 end;
2094 end;
2096 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2097 for b := 0 to High(TriggersTable) do
2098 begin
2099 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2100 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2101 end;
2103 // create map grid, init other grids (for monsters, for example)
2104 e_WriteLog('Creating map grid', TMsgType.Notify);
2105 mapCreateGrid();
2107 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2108 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2109 begin
2110 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2111 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2112 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2113 trignum := -1;
2114 for rec in triggers do
2115 begin
2116 Inc(trignum);
2117 tgpid := TriggersTable[trignum].actPanelIdx;
2118 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2119 TriggersTable[trignum].tnum := trignum;
2120 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
2121 end;
2122 end;
2124 //FIXME: use hashtable!
2125 for pan in panByGUID do
2126 begin
2127 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2128 begin
2129 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2130 end;
2131 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2132 begin
2133 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2134 end;
2135 end;
2137 // Çàãðóçêà ïðåäìåòîâ
2138 e_WriteLog(' Loading items...', TMsgType.Notify);
2139 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2141 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2142 if (items <> nil) and not gLoadGameMode then
2143 begin
2144 e_WriteLog(' Spawning items...', TMsgType.Notify);
2145 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2146 for rec in items do CreateItem(rec);
2147 end;
2149 // Çàãðóçêà îáëàñòåé
2150 e_WriteLog(' Loading areas...', TMsgType.Notify);
2151 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2153 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2154 if areas <> nil then
2155 begin
2156 e_WriteLog(' Creating areas...', TMsgType.Notify);
2157 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2158 for rec in areas do CreateArea(rec);
2159 end;
2161 // Çàãðóçêà ìîíñòðîâ
2162 e_WriteLog(' Loading monsters...', TMsgType.Notify);
2163 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2165 gTotalMonsters := 0;
2167 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2168 if (monsters <> nil) and not gLoadGameMode then
2169 begin
2170 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
2171 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2172 for rec in monsters do CreateMonster(rec);
2173 end;
2175 //gCurrentMap := mapReader; // this will be our current map now
2176 gCurrentMapFileName := Res;
2177 //mapReader := nil;
2179 // Çàãðóçêà íåáà
2180 if (gMapInfo.SkyName <> '') then
2181 begin
2182 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
2183 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2184 FileName := g_ExtractWadName(gMapInfo.SkyName);
2186 if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
2188 if gTextureFilter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
2189 try
2190 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2191 if g_Texture_CreateWAD(BackID, s) then
2192 begin
2193 g_Game_SetupScreenSize();
2194 end
2195 else
2196 begin
2197 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2198 end;
2199 finally
2200 TEXTUREFILTER := GL_NEAREST;
2201 end;
2202 end;
2204 // Çàãðóçêà ìóçûêè
2205 ok := False;
2206 if gMapInfo.MusicName <> '' then
2207 begin
2208 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
2209 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2210 FileName := g_ExtractWadName(gMapInfo.MusicName);
2212 if FileName <> '' then
2213 FileName := GameDir+'/wads/'+FileName
2214 else
2215 begin
2216 FileName := g_ExtractWadName(Res);
2217 end;
2219 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
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_ClearInputBuffer(); // why not?
2264 if not mapOk then
2265 begin
2266 gCurrentMap.Free();
2267 gCurrentMap := nil;
2268 gCurrentMapFileName := '';
2269 end;
2270 end;
2272 e_WriteLog('Done loading map.', TMsgType.Notify);
2273 Result := True;
2274 end;
2277 function g_Map_GetMapInfo(Res: String): TMapInfo;
2278 var
2279 WAD: TWADFile;
2280 mapReader: TDynRecord;
2281 //Header: TMapHeaderRec_1;
2282 FileName: String;
2283 Data: Pointer;
2284 Len: Integer;
2285 begin
2286 FillChar(Result, SizeOf(Result), 0);
2287 FileName := g_ExtractWadName(Res);
2289 WAD := TWADFile.Create();
2290 if not WAD.ReadFile(FileName) then
2291 begin
2292 WAD.Free();
2293 Exit;
2294 end;
2296 //k8: it ignores path again
2297 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2298 begin
2299 WAD.Free();
2300 Exit;
2301 end;
2303 WAD.Free();
2305 try
2306 mapReader := g_Map_ParseMap(Data, Len);
2307 except
2308 mapReader := nil;
2309 FreeMem(Data);
2310 exit;
2311 end;
2313 FreeMem(Data);
2315 if (mapReader = nil) then exit;
2317 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2318 begin
2319 Result.Name := mapReader.MapName;
2320 Result.Description := mapReader.MapDesc;
2321 Result.Map := Res;
2322 Result.Author := mapReader.MapAuthor;
2323 Result.Height := mapReader.Height;
2324 Result.Width := mapReader.Width;
2325 end
2326 else
2327 begin
2328 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2329 //ZeroMemory(@Header, SizeOf(Header));
2330 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2331 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2332 Result.Map := Res;
2333 Result.Author := '';
2334 Result.Height := 0;
2335 Result.Width := 0;
2336 end;
2338 mapReader.Free();
2339 end;
2341 function g_Map_GetMapsList(WADName: string): SSArray;
2342 var
2343 WAD: TWADFile;
2344 a: Integer;
2345 ResList: SSArray;
2346 begin
2347 Result := nil;
2348 WAD := TWADFile.Create();
2349 if not WAD.ReadFile(WADName) then
2350 begin
2351 WAD.Free();
2352 Exit;
2353 end;
2354 ResList := WAD.GetMapResources();
2355 if ResList <> nil then
2356 begin
2357 for a := 0 to High(ResList) do
2358 begin
2359 SetLength(Result, Length(Result)+1);
2360 Result[High(Result)] := ResList[a];
2361 end;
2362 end;
2363 WAD.Free();
2364 end;
2366 function g_Map_Exist(Res: string): Boolean;
2367 var
2368 WAD: TWADFile;
2369 FileName, mnn: string;
2370 ResList: SSArray;
2371 a: Integer;
2372 begin
2373 Result := False;
2375 FileName := addWadExtension(g_ExtractWadName(Res));
2377 WAD := TWADFile.Create;
2378 if not WAD.ReadFile(FileName) then
2379 begin
2380 WAD.Free();
2381 Exit;
2382 end;
2384 ResList := WAD.GetMapResources();
2385 WAD.Free();
2387 mnn := g_ExtractFileName(Res);
2388 if ResList <> nil then
2389 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2390 begin
2391 Result := True;
2392 Exit;
2393 end;
2394 end;
2396 procedure g_Map_Free(freeTextures: Boolean=true);
2397 var
2398 a: Integer;
2400 procedure FreePanelArray(var panels: TPanelArray);
2401 var
2402 i: Integer;
2404 begin
2405 if panels <> nil then
2406 begin
2407 for i := 0 to High(panels) do
2408 panels[i].Free();
2409 panels := nil;
2410 end;
2411 end;
2413 begin
2414 g_GFX_Free();
2415 g_Weapon_Free();
2416 g_Items_Free();
2417 g_Triggers_Free();
2418 g_Monsters_Free();
2420 RespawnPoints := nil;
2421 if FlagPoints[FLAG_RED] <> nil then
2422 begin
2423 Dispose(FlagPoints[FLAG_RED]);
2424 FlagPoints[FLAG_RED] := nil;
2425 end;
2426 if FlagPoints[FLAG_BLUE] <> nil then
2427 begin
2428 Dispose(FlagPoints[FLAG_BLUE]);
2429 FlagPoints[FLAG_BLUE] := nil;
2430 end;
2431 //DOMFlagPoints := nil;
2433 //gDOMFlags := nil;
2435 if (Length(gCurrentMapFileName) <> 0) then
2436 begin
2437 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2438 end
2439 else
2440 begin
2441 e_LogWritefln('g_Map_Free: no previous map.', []);
2442 end;
2444 if freeTextures then
2445 begin
2446 e_LogWritefln('g_Map_Free: clearing textures...', []);
2447 if (Textures <> nil) then
2448 begin
2449 for a := 0 to High(Textures) do
2450 begin
2451 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2452 begin
2453 if Textures[a].Anim then
2454 begin
2455 g_Frames_DeleteByID(Textures[a].FramesID)
2456 end
2457 else
2458 begin
2459 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2460 begin
2461 e_DeleteTexture(Textures[a].TextureID);
2462 end;
2463 end;
2464 end;
2465 end;
2466 Textures := nil;
2467 end;
2468 TextNameHash.Free();
2469 TextNameHash := nil;
2470 BadTextNameHash.Free();
2471 BadTextNameHash := nil;
2472 gCurrentMapFileName := '';
2473 gCurrentMap.Free();
2474 gCurrentMap := nil;
2475 end;
2477 panByGUID := nil;
2479 FreePanelArray(gWalls);
2480 FreePanelArray(gRenderBackgrounds);
2481 FreePanelArray(gRenderForegrounds);
2482 FreePanelArray(gWater);
2483 FreePanelArray(gAcid1);
2484 FreePanelArray(gAcid2);
2485 FreePanelArray(gSteps);
2486 FreePanelArray(gLifts);
2487 FreePanelArray(gBlockMon);
2488 gMovingWallIds := nil;
2490 if BackID <> DWORD(-1) then
2491 begin
2492 gBackSize.X := 0;
2493 gBackSize.Y := 0;
2494 e_DeleteTexture(BackID);
2495 BackID := DWORD(-1);
2496 end;
2498 g_Game_StopAllSounds(False);
2499 gMusic.FreeSound();
2500 g_Sound_Delete(gMapInfo.MusicName);
2502 gMapInfo.Name := '';
2503 gMapInfo.Description := '';
2504 gMapInfo.MusicName := '';
2505 gMapInfo.Height := 0;
2506 gMapInfo.Width := 0;
2508 gDoorMap := nil;
2509 gLiftMap := nil;
2510 end;
2512 procedure g_Map_Update();
2513 var
2514 a, d, j: Integer;
2515 m: Word;
2516 s: String;
2518 procedure UpdatePanelArray(var panels: TPanelArray);
2519 var
2520 i: Integer;
2522 begin
2523 for i := 0 to High(panels) do panels[i].Update();
2524 end;
2526 begin
2527 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2529 UpdatePanelArray(gWalls);
2530 UpdatePanelArray(gRenderBackgrounds);
2531 UpdatePanelArray(gRenderForegrounds);
2532 UpdatePanelArray(gWater);
2533 UpdatePanelArray(gAcid1);
2534 UpdatePanelArray(gAcid2);
2535 UpdatePanelArray(gSteps);
2537 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2539 if gGameSettings.GameMode = GM_CTF then
2540 begin
2541 for a := FLAG_RED to FLAG_BLUE do
2542 begin
2543 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2544 begin
2545 with gFlags[a] do
2546 begin
2547 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2549 m := g_Obj_Move(@Obj, True, True);
2551 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2553 // Ñîïðîòèâëåíèå âîçäóõà
2554 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2556 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2557 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2558 begin
2559 g_Map_ResetFlag(a);
2560 gFlags[a].CaptureTime := 0;
2561 if a = FLAG_RED then
2562 s := _lc[I_PLAYER_FLAG_RED]
2563 else
2564 s := _lc[I_PLAYER_FLAG_BLUE];
2565 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2567 if g_Game_IsNet then
2568 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2569 Continue;
2570 end;
2572 if Count > 0 then Count -= 1;
2574 // Èãðîê áåðåò ôëàã
2575 if gPlayers <> nil then
2576 begin
2577 j := Random(Length(gPlayers)) - 1;
2578 for d := 0 to High(gPlayers) do
2579 begin
2580 Inc(j);
2581 if j > High(gPlayers) then j := 0;
2582 if gPlayers[j] <> nil then
2583 begin
2584 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2585 begin
2586 if gPlayers[j].GetFlag(a) then Break;
2587 end;
2588 end;
2589 end;
2590 end;
2591 end;
2592 end;
2593 end;
2594 end;
2595 end;
2598 // old algo
2599 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2601 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2602 var
2603 idx: Integer;
2604 begin
2605 if (panels <> nil) then
2606 begin
2607 // alas, no visible set
2608 for idx := 0 to High(panels) do
2609 begin
2610 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2611 end;
2612 end;
2613 end;
2615 begin
2616 case PanelType of
2617 PANEL_WALL: DrawPanels(gWalls);
2618 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2619 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2620 PANEL_FORE: DrawPanels(gRenderForegrounds);
2621 PANEL_WATER: DrawPanels(gWater);
2622 PANEL_ACID1: DrawPanels(gAcid1);
2623 PANEL_ACID2: DrawPanels(gAcid2);
2624 PANEL_STEP: DrawPanels(gSteps);
2625 end;
2626 end;
2629 // new algo
2630 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2631 var
2632 mwit: PPanel;
2633 it: TPanelGrid.Iter;
2634 begin
2635 dplClear();
2636 it := mapGrid.forEachInAABB(x0, y0, wdt, hgt, GridDrawableMask);
2637 for mwit in it do if (((mwit^.tag and GridTagDoor) <> 0) = mwit^.Door) then gDrawPanelList.insert(mwit^);
2638 it.release();
2639 // list will be rendered in `g_game.DrawPlayer()`
2640 end;
2643 procedure g_Map_DrawPanelShadowVolumes (lightX: Integer; lightY: Integer; radius: Integer);
2644 var
2645 mwit: PPanel;
2646 it: TPanelGrid.Iter;
2647 begin
2648 it := mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, (GridTagWall or GridTagDoor));
2649 for mwit in it do mwit^.DrawShadowVolume(lightX, lightY, radius);
2650 it.release();
2651 end;
2654 procedure g_Map_DrawBack(dx, dy: Integer);
2655 begin
2656 if gDrawBackGround and (BackID <> DWORD(-1)) then
2657 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2658 else
2659 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2660 end;
2662 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2663 PanelType: Word; b1x3: Boolean=false): Boolean;
2664 var
2665 a, h: Integer;
2666 begin
2667 Result := False;
2669 if WordBool(PanelType and PANEL_WALL) then
2670 if gWalls <> nil then
2671 begin
2672 h := High(gWalls);
2674 for a := 0 to h do
2675 if gWalls[a].Enabled and
2676 g_Collide(X, Y, Width, Height,
2677 gWalls[a].X, gWalls[a].Y,
2678 gWalls[a].Width, gWalls[a].Height) then
2679 begin
2680 Result := True;
2681 Exit;
2682 end;
2683 end;
2685 if WordBool(PanelType and PANEL_WATER) then
2686 if gWater <> nil then
2687 begin
2688 h := High(gWater);
2690 for a := 0 to h do
2691 if g_Collide(X, Y, Width, Height,
2692 gWater[a].X, gWater[a].Y,
2693 gWater[a].Width, gWater[a].Height) then
2694 begin
2695 Result := True;
2696 Exit;
2697 end;
2698 end;
2700 if WordBool(PanelType and PANEL_ACID1) then
2701 if gAcid1 <> nil then
2702 begin
2703 h := High(gAcid1);
2705 for a := 0 to h do
2706 if g_Collide(X, Y, Width, Height,
2707 gAcid1[a].X, gAcid1[a].Y,
2708 gAcid1[a].Width, gAcid1[a].Height) then
2709 begin
2710 Result := True;
2711 Exit;
2712 end;
2713 end;
2715 if WordBool(PanelType and PANEL_ACID2) then
2716 if gAcid2 <> nil then
2717 begin
2718 h := High(gAcid2);
2720 for a := 0 to h do
2721 if g_Collide(X, Y, Width, Height,
2722 gAcid2[a].X, gAcid2[a].Y,
2723 gAcid2[a].Width, gAcid2[a].Height) then
2724 begin
2725 Result := True;
2726 Exit;
2727 end;
2728 end;
2730 if WordBool(PanelType and PANEL_STEP) then
2731 if gSteps <> nil then
2732 begin
2733 h := High(gSteps);
2735 for a := 0 to h do
2736 if g_Collide(X, Y, Width, Height,
2737 gSteps[a].X, gSteps[a].Y,
2738 gSteps[a].Width, gSteps[a].Height) then
2739 begin
2740 Result := True;
2741 Exit;
2742 end;
2743 end;
2745 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2746 if gLifts <> nil then
2747 begin
2748 h := High(gLifts);
2750 for a := 0 to h do
2751 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = LIFTTYPE_UP)) or
2752 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = LIFTTYPE_DOWN)) or
2753 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = LIFTTYPE_LEFT)) or
2754 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = LIFTTYPE_RIGHT))) and
2755 g_Collide(X, Y, Width, Height,
2756 gLifts[a].X, gLifts[a].Y,
2757 gLifts[a].Width, gLifts[a].Height) then
2758 begin
2759 Result := True;
2760 Exit;
2761 end;
2762 end;
2764 if WordBool(PanelType and PANEL_BLOCKMON) then
2765 if gBlockMon <> nil then
2766 begin
2767 h := High(gBlockMon);
2769 for a := 0 to h do
2770 if ( (not b1x3) or
2771 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2772 g_Collide(X, Y, Width, Height,
2773 gBlockMon[a].X, gBlockMon[a].Y,
2774 gBlockMon[a].Width, gBlockMon[a].Height) then
2775 begin
2776 Result := True;
2777 Exit;
2778 end;
2779 end;
2780 end;
2782 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2783 var
2784 texid: DWORD;
2786 function checkPanels (constref panels: TPanelArray): Boolean;
2787 var
2788 a: Integer;
2789 begin
2790 result := false;
2791 if panels = nil then exit;
2792 for a := 0 to High(panels) do
2793 begin
2794 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2795 begin
2796 result := true;
2797 texid := panels[a].GetTextureID();
2798 exit;
2799 end;
2800 end;
2801 end;
2803 begin
2804 texid := LongWord(TEXTURE_NONE);
2805 result := texid;
2806 if not checkPanels(gWater) then
2807 if not checkPanels(gAcid1) then
2808 if not checkPanels(gAcid2) then exit;
2809 result := texid;
2810 end;
2813 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2814 const
2815 SlowMask = GridTagLift or GridTagBlockMon;
2817 function checker (pan: TPanel; tag: Integer): Boolean;
2818 begin
2820 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2821 begin
2822 result := pan.Enabled;
2823 exit;
2824 end;
2827 if ((tag and GridTagLift) <> 0) then
2828 begin
2829 result :=
2830 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2831 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2832 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2833 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2834 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2835 exit;
2836 end;
2838 if ((tag and GridTagBlockMon) <> 0) then
2839 begin
2840 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2841 exit;
2842 end;
2844 // other shit
2845 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2846 result := true; // i found her!
2847 end;
2849 var
2850 tagmask: Integer = 0;
2851 mwit: PPanel;
2852 it: TPanelGrid.Iter;
2853 pan: TPanel;
2854 begin
2855 result := false;
2856 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2857 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2858 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2859 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2860 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2861 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2862 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2864 if (tagmask = 0) then exit; // just in case
2866 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2867 if gdbg_map_use_accel_coldet then
2868 begin
2869 if ((tagmask and SlowMask) <> 0) then
2870 begin
2871 // slow
2872 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2873 for mwit in it do
2874 begin
2875 pan := mwit^;
2876 if ((pan.tag and GridTagLift) <> 0) then
2877 begin
2878 result :=
2879 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2880 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2881 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2882 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2883 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2884 end
2885 else if ((pan.tag and GridTagBlockMon) <> 0) then
2886 begin
2887 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2888 end
2889 else
2890 begin
2891 // other shit
2892 result := true; // i found her!
2893 end;
2894 if (result) then break;
2895 end;
2896 end
2897 else
2898 begin
2899 // fast
2900 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2901 result := (it.length > 0);
2902 end;
2903 it.release();
2904 end
2905 else
2906 begin
2907 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2908 end;
2909 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2910 end;
2913 // returns `true` if we need to stop
2914 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2915 begin
2916 result := false;
2917 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2918 // check priorities
2919 case cctype of
2920 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2921 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2922 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2923 end;
2924 // collision?
2925 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2926 // yeah
2927 texid := pan.GetTextureID();
2928 // water? water has the highest priority, so stop right here
2929 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2930 // acid2?
2931 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2932 // acid1?
2933 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2934 end;
2936 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2937 var
2938 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2939 mwit: PPanel;
2940 it: TPanelGrid.Iter;
2941 begin
2942 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2943 if gdbg_map_use_accel_coldet then
2944 begin
2945 result := LongWord(TEXTURE_NONE);
2946 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2947 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2948 it.release();
2949 end
2950 else
2951 begin
2952 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2953 end;
2954 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2955 end;
2958 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2959 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2960 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2963 procedure g_Map_EnableWallGUID (pguid: Integer);
2964 var
2965 pan: TPanel;
2966 begin
2967 //pan := gWalls[ID];
2968 pan := g_Map_PanelByGUID(pguid);
2969 if (pan = nil) then exit;
2970 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2972 pan.Enabled := True;
2973 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2975 mapGrid.proxyEnabled[pan.proxyId] := true;
2976 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2977 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2979 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
2980 // mark platform as interesting
2981 pan.setDirty();
2983 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2984 //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);
2985 {$ENDIF}
2986 end;
2989 procedure g_Map_DisableWallGUID (pguid: Integer);
2990 var
2991 pan: TPanel;
2992 begin
2993 //pan := gWalls[ID];
2994 pan := g_Map_PanelByGUID(pguid);
2995 if (pan = nil) then exit;
2996 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
2998 pan.Enabled := False;
2999 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
3001 mapGrid.proxyEnabled[pan.proxyId] := false;
3002 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
3004 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3005 // mark platform as interesting
3006 pan.setDirty();
3008 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
3009 //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);
3010 {$ENDIF}
3011 end;
3014 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
3015 var
3016 tp: TPanel;
3017 begin
3018 tp := g_Map_PanelByGUID(pguid);
3019 if (tp = nil) then exit;
3020 tp.NextTexture(AnimLoop);
3021 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
3022 end;
3025 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
3026 var
3027 pan: TPanel;
3028 begin
3029 //pan := gLifts[ID];
3030 pan := g_Map_PanelByGUID(pguid);
3031 if (pan = nil) then exit;
3032 if not pan.isGLift then exit;
3034 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
3036 with {gLifts[ID]} pan do
3037 begin
3038 LiftType := t;
3040 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
3041 //TODO: make separate lift tags, and change tag here
3043 case LiftType of
3044 LIFTTYPE_UP: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
3045 LIFTTYPE_DOWN: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
3046 LIFTTYPE_LEFT: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
3047 LIFTTYPE_RIGHT: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
3048 end;
3050 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3051 // mark platform as interesting
3052 pan.setDirty();
3053 end;
3054 end;
3057 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
3058 var
3059 a: Integer;
3060 PointsArray: Array of TRespawnPoint;
3061 begin
3062 Result := False;
3063 SetLength(PointsArray, 0);
3065 if RespawnPoints = nil then
3066 Exit;
3068 for a := 0 to High(RespawnPoints) do
3069 if RespawnPoints[a].PointType = PointType then
3070 begin
3071 SetLength(PointsArray, Length(PointsArray)+1);
3072 PointsArray[High(PointsArray)] := RespawnPoints[a];
3073 end;
3075 if PointsArray = nil then
3076 Exit;
3078 RespawnPoint := PointsArray[Random(Length(PointsArray))];
3079 Result := True;
3080 end;
3082 function g_Map_GetPointCount(PointType: Byte): Word;
3083 var
3084 a: Integer;
3085 begin
3086 Result := 0;
3088 if RespawnPoints = nil then
3089 Exit;
3091 for a := 0 to High(RespawnPoints) do
3092 if RespawnPoints[a].PointType = PointType then
3093 Result := Result + 1;
3094 end;
3096 function g_Map_HaveFlagPoints(): Boolean;
3097 begin
3098 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3099 end;
3101 procedure g_Map_ResetFlag(Flag: Byte);
3102 begin
3103 with gFlags[Flag] do
3104 begin
3105 Obj.X := -1000;
3106 Obj.Y := -1000;
3107 Obj.Vel.X := 0;
3108 Obj.Vel.Y := 0;
3109 Direction := TDirection.D_LEFT;
3110 State := FLAG_STATE_NONE;
3111 if FlagPoints[Flag] <> nil then
3112 begin
3113 Obj.X := FlagPoints[Flag]^.X;
3114 Obj.Y := FlagPoints[Flag]^.Y;
3115 Direction := FlagPoints[Flag]^.Direction;
3116 State := FLAG_STATE_NORMAL;
3117 end;
3118 Count := -1;
3119 end;
3120 end;
3122 procedure g_Map_DrawFlags();
3123 var
3124 i, dx: Integer;
3125 Mirror: TMirrorType;
3126 begin
3127 if gGameSettings.GameMode <> GM_CTF then
3128 Exit;
3130 for i := FLAG_RED to FLAG_BLUE do
3131 with gFlags[i] do
3132 if State <> FLAG_STATE_CAPTURED then
3133 begin
3134 if State = FLAG_STATE_NONE then
3135 continue;
3137 if Direction = TDirection.D_LEFT then
3138 begin
3139 Mirror := TMirrorType.Horizontal;
3140 dx := -1;
3141 end
3142 else
3143 begin
3144 Mirror := TMirrorType.None;
3145 dx := 1;
3146 end;
3148 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3150 if g_debug_Frames then
3151 begin
3152 e_DrawQuad(Obj.X+Obj.Rect.X,
3153 Obj.Y+Obj.Rect.Y,
3154 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3155 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3156 0, 255, 0);
3157 end;
3158 end;
3159 end;
3162 procedure g_Map_SaveState (st: TStream);
3163 var
3164 str: String;
3166 procedure savePanels ();
3167 var
3168 pan: TPanel;
3169 begin
3170 // Ñîõðàíÿåì ïàíåëè
3171 utils.writeInt(st, LongInt(Length(panByGUID)));
3172 for pan in panByGUID do pan.SaveState(st);
3173 end;
3175 procedure saveFlag (flag: PFlag);
3176 var
3177 b: Byte;
3178 begin
3179 utils.writeSign(st, 'FLAG');
3180 utils.writeInt(st, Byte(0)); // version
3181 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3182 utils.writeInt(st, Byte(flag^.RespawnType));
3183 // Ñîñòîÿíèå ôëàãà
3184 utils.writeInt(st, Byte(flag^.State));
3185 // Íàïðàâëåíèå ôëàãà
3186 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
3187 utils.writeInt(st, Byte(b));
3188 // Îáúåêò ôëàãà
3189 Obj_SaveState(st, @flag^.Obj);
3190 end;
3192 begin
3193 savePanels();
3195 // Ñîõðàíÿåì ìóçûêó
3196 utils.writeSign(st, 'MUSI');
3197 utils.writeInt(st, Byte(0));
3198 // Íàçâàíèå ìóçûêè
3199 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3200 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3201 utils.writeStr(st, str);
3202 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3203 utils.writeInt(st, LongWord(gMusic.GetPosition()));
3204 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3205 utils.writeBool(st, gMusic.SpecPause);
3207 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3208 utils.writeInt(st, LongInt(gTotalMonsters));
3209 ///// /////
3211 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3212 if (gGameSettings.GameMode = GM_CTF) then
3213 begin
3214 // Ôëàã Êðàñíîé êîìàíäû
3215 saveFlag(@gFlags[FLAG_RED]);
3216 // Ôëàã Ñèíåé êîìàíäû
3217 saveFlag(@gFlags[FLAG_BLUE]);
3218 end;
3219 ///// /////
3221 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3222 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3223 begin
3224 // Î÷êè Êðàñíîé êîìàíäû
3225 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
3226 // Î÷êè Ñèíåé êîìàíäû
3227 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
3228 end;
3229 ///// /////
3230 end;
3233 procedure g_Map_LoadState (st: TStream);
3234 var
3235 dw: DWORD;
3236 str: String;
3237 boo: Boolean;
3239 procedure loadPanels ();
3240 var
3241 pan: TPanel;
3242 begin
3243 // Çàãðóæàåì ïàíåëè
3244 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
3245 for pan in panByGUID do
3246 begin
3247 pan.LoadState(st);
3248 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3249 end;
3250 end;
3252 procedure loadFlag (flag: PFlag);
3253 var
3254 b: Byte;
3255 begin
3256 // Ñèãíàòóðà ôëàãà
3257 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
3258 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
3259 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3260 flag^.RespawnType := utils.readByte(st);
3261 // Ñîñòîÿíèå ôëàãà
3262 flag^.State := utils.readByte(st);
3263 // Íàïðàâëåíèå ôëàãà
3264 b := utils.readByte(st);
3265 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
3266 // Îáúåêò ôëàãà
3267 Obj_LoadState(@flag^.Obj, st);
3268 end;
3270 begin
3271 if (st = nil) then exit;
3273 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3274 loadPanels();
3275 ///// /////
3277 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3278 g_GFX_Init();
3279 //mapCreateGrid();
3281 ///// Çàãðóæàåì ìóçûêó: /////
3282 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
3283 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
3284 // Íàçâàíèå ìóçûêè
3285 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3286 str := utils.readStr(st);
3287 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3288 dw := utils.readLongWord(st);
3289 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3290 boo := utils.readBool(st);
3291 // Çàïóñêàåì ýòó ìóçûêó
3292 gMusic.SetByName(str);
3293 gMusic.SpecPause := boo;
3294 gMusic.Play();
3295 gMusic.Pause(true);
3296 gMusic.SetPosition(dw);
3297 ///// /////
3299 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3300 gTotalMonsters := utils.readLongInt(st);
3301 ///// /////
3303 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3304 if (gGameSettings.GameMode = GM_CTF) then
3305 begin
3306 // Ôëàã Êðàñíîé êîìàíäû
3307 loadFlag(@gFlags[FLAG_RED]);
3308 // Ôëàã Ñèíåé êîìàíäû
3309 loadFlag(@gFlags[FLAG_BLUE]);
3310 end;
3311 ///// /////
3313 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3314 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3315 begin
3316 // Î÷êè Êðàñíîé êîìàíäû
3317 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
3318 // Î÷êè Ñèíåé êîìàíäû
3319 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
3320 end;
3321 ///// /////
3322 end;
3325 // trace liquid, stepping by `dx` and `dy`
3326 // return last seen liquid coords, and `false` if we're started outside of the liquid
3327 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3328 const
3329 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3330 begin
3331 topx := x;
3332 topy := y;
3333 // started outside of the liquid?
3334 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3335 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
3336 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3337 result := true;
3338 while true do
3339 begin
3340 Inc(x, dx);
3341 Inc(y, dy);
3342 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3343 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
3344 topx := x;
3345 topy := y;
3346 end;
3347 end;
3350 begin
3351 DynWarningCB := mapWarningCB;
3352 end.