DEADSOFTWARE

game: restored external resource registering in map loader (lol)
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
17 unit g_map;
19 interface
21 uses
22 SysUtils, Classes, mempool,
23 e_graphics, g_basic, MAPDEF, g_textures,
24 g_phys, utils, g_panel, g_grid, md5, binheap, xprofiler, xparser, xdynrec;
26 type
27 TMapInfo = record
28 Map: String;
29 Name: String;
30 Description: String;
31 Author: String;
32 MusicName: String;
33 SkyName: String;
34 Height: Word;
35 Width: Word;
36 end;
38 PRespawnPoint = ^TRespawnPoint;
39 TRespawnPoint = record
40 X, Y: Integer;
41 Direction: TDirection;
42 PointType: Byte;
43 end;
45 PFlagPoint = ^TFlagPoint;
46 TFlagPoint = TRespawnPoint;
48 PFlag = ^TFlag;
49 TFlag = record
50 Obj: TObj;
51 RespawnType: Byte;
52 State: Byte;
53 Count: Integer;
54 CaptureTime: LongWord;
55 Animation: TAnimation;
56 Direction: TDirection;
57 end;
59 function g_Map_Load(Res: String): Boolean;
60 function g_Map_GetMapInfo(Res: String): TMapInfo;
61 function g_Map_GetMapsList(WADName: String): SSArray;
62 function g_Map_Exist(Res: String): Boolean;
63 procedure g_Map_Free(freeTextures: Boolean=true);
64 procedure g_Map_Update();
66 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
68 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor); // unaccelerated
69 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
71 procedure g_Map_DrawBack(dx, dy: Integer);
72 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
73 PanelType: Word; b1x3: Boolean=false): Boolean;
74 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
76 procedure g_Map_EnableWallGUID (pguid: Integer);
77 procedure g_Map_DisableWallGUID (pguid: Integer);
78 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
80 // HACK!!!
81 procedure g_Map_EnableWall_XXX (ID: DWORD);
82 procedure g_Map_DisableWall_XXX (ID: DWORD);
83 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer);
85 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
87 procedure g_Map_ReAdd_DieTriggers();
88 function g_Map_IsSpecialTexture(Texture: String): Boolean;
90 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
91 function g_Map_GetPointCount(PointType: Byte): Word;
93 function g_Map_HaveFlagPoints(): Boolean;
95 procedure g_Map_ResetFlag(Flag: Byte);
96 procedure g_Map_DrawFlags();
98 procedure g_Map_SaveState (st: TStream);
99 procedure g_Map_LoadState (st: TStream);
101 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
103 // returns panel or nil
104 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
105 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
107 // returns panel or nil
108 // sets `ex` and `ey` to `x1` and `y1` when no hit was detected
109 function g_Map_traceToNearest (x0, y0, x1, y1: Integer; tag: Integer; hitx: PInteger=nil; hity: PInteger=nil): TPanel;
111 type
112 TForEachPanelCB = function (pan: TPanel): Boolean is nested; // return `true` to stop
114 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
115 function g_Map_PanelAtPoint (x, y: Integer; tagmask: Integer=-1): TPanel;
117 // trace liquid, stepping by `dx` and `dy`
118 // return last seen liquid coords, and `false` if we're started outside of the liquid
119 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
122 // return `true` from `cb` to stop
123 function g_Map_ForEachPanel (cb: TForEachPanelCB): TPanel;
125 procedure g_Map_NetSendInterestingPanels (); // yay!
128 procedure g_Map_ProfilersBegin ();
129 procedure g_Map_ProfilersEnd ();
132 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
135 function g_Map_MinX (): Integer; inline;
136 function g_Map_MinY (): Integer; inline;
137 function g_Map_MaxX (): Integer; inline;
138 function g_Map_MaxY (): Integer; inline;
140 const
141 NNF_NO_NAME = 0;
142 NNF_NAME_BEFORE = 1;
143 NNF_NAME_EQUALS = 2;
144 NNF_NAME_AFTER = 3;
146 function g_Texture_NumNameFindStart(name: String): Boolean;
147 function g_Texture_NumNameFindNext(var newName: String): Byte;
149 const
150 RESPAWNPOINT_PLAYER1 = 1;
151 RESPAWNPOINT_PLAYER2 = 2;
152 RESPAWNPOINT_DM = 3;
153 RESPAWNPOINT_RED = 4;
154 RESPAWNPOINT_BLUE = 5;
156 FLAG_NONE = 0;
157 FLAG_RED = 1;
158 FLAG_BLUE = 2;
159 FLAG_DOM = 3;
161 FLAG_STATE_NONE = 0;
162 FLAG_STATE_NORMAL = 1;
163 FLAG_STATE_DROPPED = 2;
164 FLAG_STATE_CAPTURED = 3;
165 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
166 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
168 FLAG_TIME = 720; // 20 seconds
170 SKY_STRETCH: Single = 1.5;
172 const
173 GridTagInvalid = 0;
175 (* draw order:
176 PANEL_BACK
177 PANEL_STEP
178 PANEL_WALL
179 PANEL_CLOSEDOOR
180 PANEL_ACID1
181 PANEL_ACID2
182 PANEL_WATER
183 PANEL_FORE
184 *)
185 // sorted by draw priority
186 GridTagBack = 1 shl 0; // gRenderBackgrounds
187 GridTagStep = 1 shl 1; // gSteps
188 GridTagWall = 1 shl 2; // gWalls
189 GridTagDoor = 1 shl 3; // gWalls
190 GridTagAcid1 = 1 shl 4; // gAcid1
191 GridTagAcid2 = 1 shl 5; // gAcid2
192 GridTagWater = 1 shl 6; // gWater
193 GridTagFore = 1 shl 7; // gRenderForegrounds
194 // the following are invisible
195 GridTagLift = 1 shl 8; // gLifts
196 GridTagBlockMon = 1 shl 9; // gBlockMon
198 GridTagSolid = (GridTagWall or GridTagDoor);
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 isWadData(TextureWAD, ResLength) then
1087 begin
1088 // íåò, ýòî ñóïåðìåí!
1089 if not WAD.ReadMemory(TextureWAD, ResLength) then
1090 begin
1091 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), TMsgType.Warning);
1092 BadTextNameHash.put(RecName, -1);
1093 exit;
1094 end;
1096 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
1097 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
1098 begin
1099 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), TMsgType.Warning);
1100 BadTextNameHash.put(RecName, -1);
1101 exit;
1102 end;
1104 cfg := TConfig.CreateMem(TextData, ResLength);
1106 TextureResource := cfg.ReadStr('', 'resource', '');
1107 if TextureResource = '' then
1108 begin
1109 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), TMsgType.Warning);
1110 BadTextNameHash.put(RecName, -1);
1111 exit;
1112 end;
1114 _width := cfg.ReadInt('', 'framewidth', 0);
1115 _height := cfg.ReadInt('', 'frameheight', 0);
1116 _framecount := cfg.ReadInt('', 'framecount', 0);
1117 _speed := cfg.ReadInt('', 'waitcount', 0);
1118 _backanimation := cfg.ReadBool('', 'backanimation', False);
1120 cfg.Free();
1121 cfg := nil;
1123 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
1124 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
1125 begin
1126 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), TMsgType.Warning);
1127 BadTextNameHash.put(RecName, -1);
1128 exit;
1129 end;
1131 WAD.Free();
1132 WAD := nil;
1134 SetLength(Textures, Length(Textures)+1);
1135 with Textures[High(Textures)] do
1136 begin
1137 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
1138 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
1139 begin
1140 TextureName := RecName;
1141 Width := _width;
1142 Height := _height;
1143 Anim := True;
1144 FramesCount := _framecount;
1145 Speed := _speed;
1146 result := High(Textures);
1147 TextNameHash.put(RecName, result);
1148 end
1149 else
1150 begin
1151 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1152 if log and (not BadTextNameHash.get(RecName, f)) then
1153 begin
1154 e_WriteLog(Format('Error loading animation texture %s', [RecName]), TMsgType.Warning);
1155 end;
1156 BadTextNameHash.put(RecName, -1);
1157 end;
1158 end;
1159 end
1160 else
1161 begin
1162 // try animated image
1164 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
1165 if length(imgfmt) = 0 then
1166 begin
1167 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
1168 exit;
1169 end;
1171 GlobalMetadata.ClearMetaItems();
1172 GlobalMetadata.ClearMetaItemsForSaving();
1173 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
1174 begin
1175 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), TMsgType.Warning);
1176 BadTextNameHash.put(RecName, -1);
1177 exit;
1178 end;
1179 if length(ia) = 0 then
1180 begin
1181 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), TMsgType.Warning);
1182 BadTextNameHash.put(RecName, -1);
1183 exit;
1184 end;
1186 WAD.Free();
1187 WAD := nil;
1189 _width := ia[0].width;
1190 _height := ia[0].height;
1191 _framecount := length(ia);
1192 _speed := 1;
1193 _backanimation := false;
1194 frdelay := -1;
1195 frloop := -666;
1196 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
1197 begin
1198 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
1199 try
1200 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
1201 frdelay := f;
1202 if f < 0 then f := 0;
1203 // rounding ;-)
1204 c := f mod 28;
1205 if c < 13 then c := 0 else c := 1;
1206 f := (f div 28)+c;
1207 if f < 1 then f := 1 else if f > 255 then f := 255;
1208 _speed := f;
1209 except
1210 end;
1211 end;
1212 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
1213 begin
1214 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
1215 try
1216 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
1217 frloop := f;
1218 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
1219 except
1220 end;
1221 end;
1222 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
1223 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
1224 f := ord(_backanimation);
1225 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);
1227 SetLength(Textures, Length(Textures)+1);
1228 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
1229 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1230 begin
1231 Textures[High(Textures)].TextureName := RecName;
1232 Textures[High(Textures)].Width := _width;
1233 Textures[High(Textures)].Height := _height;
1234 Textures[High(Textures)].Anim := True;
1235 Textures[High(Textures)].FramesCount := length(ia);
1236 Textures[High(Textures)].Speed := _speed;
1237 result := High(Textures);
1238 TextNameHash.put(RecName, result);
1239 //writeln(' CREATED!');
1240 end
1241 else
1242 begin
1243 if (BadTextNameHash = nil) then BadTextNameHash := THashStrInt.Create();
1244 if log and (not BadTextNameHash.get(RecName, f)) then
1245 begin
1246 e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), TMsgType.Warning);
1247 end;
1248 BadTextNameHash.put(RecName, -1);
1249 end;
1250 end;
1251 finally
1252 for f := 0 to High(ia) do FreeImage(ia[f]);
1253 WAD.Free();
1254 cfg.Free();
1255 if (TextureWAD <> nil) then FreeMem(TextureWAD);
1256 if (TextData <> nil) then FreeMem(TextData);
1257 if (TextureData <> nil) then FreeMem(TextureData);
1258 end;
1259 end;
1261 procedure CreateItem(Item: TDynRecord);
1262 begin
1263 if g_Game_IsClient then Exit;
1265 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1266 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1267 Exit;
1269 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1270 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1271 end;
1273 procedure CreateArea(Area: TDynRecord);
1274 var
1275 a: Integer;
1276 id: DWORD = 0;
1277 begin
1278 case Area.AreaType of
1279 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1280 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1281 begin
1282 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1283 with RespawnPoints[High(RespawnPoints)] do
1284 begin
1285 X := Area.X;
1286 Y := Area.Y;
1287 Direction := TDirection(Area.Direction);
1289 case Area.AreaType of
1290 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1291 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1292 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1293 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1294 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1295 end;
1296 end;
1297 end;
1299 AREA_REDFLAG, AREA_BLUEFLAG:
1300 begin
1301 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1303 if FlagPoints[a] <> nil then Exit;
1305 New(FlagPoints[a]);
1307 with FlagPoints[a]^ do
1308 begin
1309 X := Area.X-FLAGRECT.X;
1310 Y := Area.Y-FLAGRECT.Y;
1311 Direction := TDirection(Area.Direction);
1312 end;
1314 with gFlags[a] do
1315 begin
1316 case a of
1317 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1318 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1319 end;
1321 Animation := TAnimation.Create(id, True, 8);
1322 Obj.Rect := FLAGRECT;
1324 g_Map_ResetFlag(a);
1325 end;
1326 end;
1328 AREA_DOMFLAG:
1329 begin
1330 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1331 with DOMFlagPoints[High(DOMFlagPoints)] do
1332 begin
1333 X := Area.X;
1334 Y := Area.Y;
1335 Direction := TDirection(Area.Direction);
1336 end;
1338 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1339 end;
1340 end;
1341 end;
1343 function CreateTrigger (amapIdx: Integer; Trigger: TDynRecord; atpanid, atrigpanid: Integer): Integer;
1344 var
1345 _trigger: TTrigger;
1346 tp: TPanel;
1347 begin
1348 result := -1;
1349 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1351 with _trigger do
1352 begin
1353 mapId := Trigger.id;
1354 mapIndex := amapIdx;
1355 X := Trigger.X;
1356 Y := Trigger.Y;
1357 Width := Trigger.Width;
1358 Height := Trigger.Height;
1359 Enabled := Trigger.Enabled;
1360 TexturePanelGUID := atpanid;
1361 TriggerType := Trigger.TriggerType;
1362 ActivateType := Trigger.ActivateType;
1363 Keys := Trigger.Keys;
1364 trigPanelGUID := atrigpanid;
1365 // HACK: used in TPanel.CanChangeTexture. maybe there's a better way?
1366 if TexturePanelGUID <> -1 then
1367 begin
1368 tp := g_Map_PanelByGUID(TexturePanelGUID);
1369 if (tp <> nil) then tp.hasTexTrigger := True;
1370 end;
1371 end;
1373 result := Integer(g_Triggers_Create(_trigger, Trigger));
1374 end;
1376 procedure CreateMonster(monster: TDynRecord);
1377 var
1378 a: Integer;
1379 mon: TMonster;
1380 begin
1381 if g_Game_IsClient then Exit;
1383 if (gGameSettings.GameType = GT_SINGLE)
1384 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1385 begin
1386 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1388 if gTriggers <> nil then
1389 begin
1390 for a := 0 to High(gTriggers) do
1391 begin
1392 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1393 begin
1394 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1395 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1396 end;
1397 end;
1398 end;
1400 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1401 end;
1402 end;
1404 procedure g_Map_ReAdd_DieTriggers();
1406 function monsDieTrig (mon: TMonster): Boolean;
1407 var
1408 a: Integer;
1409 //tw: TStrTextWriter;
1410 begin
1411 result := false; // don't stop
1412 mon.ClearTriggers();
1413 for a := 0 to High(gTriggers) do
1414 begin
1415 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1416 begin
1417 //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
1419 tw := TStrTextWriter.Create();
1420 try
1421 gTriggers[a].trigData.writeTo(tw);
1422 e_LogWritefln('=== trigger #%s ==='#10'%s'#10'---', [a, tw.str]);
1423 finally
1424 tw.Free();
1425 end;
1427 if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
1428 end;
1429 end;
1430 end;
1432 begin
1433 if g_Game_IsClient then Exit;
1435 g_Mons_ForEach(monsDieTrig);
1436 end;
1438 function extractWadName(resourceName: string): string;
1439 var
1440 posN: Integer;
1441 begin
1442 posN := Pos(':', resourceName);
1443 if posN > 0 then
1444 Result:= Copy(resourceName, 0, posN-1)
1445 else
1446 Result := '';
1447 end;
1449 procedure addResToExternalResList(res: string);
1450 begin
1451 //e_LogWritefln('DBG: ***trying external resource %s', [res]);
1452 res := toLowerCase1251(extractWadName(res));
1453 // ignore "standart.wad"
1454 if (res <> '') and (res <> 'standart.wad') and (gExternalResources.IndexOf(res) = -1) then
1455 begin
1456 //e_LogWritefln('DBG: added external resource %s', [res]);
1457 gExternalResources.Add(res);
1458 end;
1459 end;
1461 procedure generateExternalResourcesList({mapReader: TMapReader_1}map: TDynRecord);
1462 //var
1463 //textures: TTexturesRec1Array;
1464 //textures: TDynField;
1465 //trec: TDynRecord;
1466 //mapHeader: TMapHeaderRec_1;
1467 //i: integer;
1468 //resFile: String = '';
1469 begin
1470 if gExternalResources = nil then
1471 gExternalResources := TStringList.Create;
1473 gExternalResources.Clear;
1475 (*
1477 textures := GetTextures(map);
1478 for i := 0 to High(textures) do
1479 begin
1480 addResToExternalResList(resFile);
1481 end;
1484 textures := map['texture'];
1485 if (textures <> nil) then
1486 begin
1487 for trec in textures do
1488 begin
1489 addResToExternalResList(resFile);
1490 end;
1491 end;
1493 textures := nil;
1494 *)
1496 //mapHeader := GetMapHeader(map);
1498 addResToExternalResList(map.MusicName);
1499 addResToExternalResList(map.SkyName);
1500 end;
1503 procedure mapCreateGrid ();
1504 var
1505 mapX0: Integer = $3fffffff;
1506 mapY0: Integer = $3fffffff;
1507 mapX1: Integer = -$3fffffff;
1508 mapY1: Integer = -$3fffffff;
1510 procedure calcBoundingBox (constref panels: TPanelArray);
1511 var
1512 idx: Integer;
1513 pan: TPanel;
1514 begin
1515 for idx := 0 to High(panels) do
1516 begin
1517 pan := panels[idx];
1518 if not pan.visvalid then continue;
1519 if (pan.Width < 1) or (pan.Height < 1) then continue;
1520 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1521 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1522 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1523 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1524 end;
1525 end;
1527 procedure addPanelsToGrid (constref panels: TPanelArray);
1528 var
1529 idx: Integer;
1530 pan: TPanel;
1531 newtag: Integer;
1532 begin
1533 //tag := panelTypeToTag(tag);
1534 for idx := 0 to High(panels) do
1535 begin
1536 pan := panels[idx];
1537 if not pan.visvalid then continue;
1538 if (pan.proxyId <> -1) then
1539 begin
1540 {$IF DEFINED(D2F_DEBUG)}
1541 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);
1542 {$ENDIF}
1543 continue;
1544 end;
1545 case pan.PanelType of
1546 PANEL_WALL: newtag := GridTagWall;
1547 PANEL_OPENDOOR, PANEL_CLOSEDOOR: newtag := GridTagDoor;
1548 PANEL_BACK: newtag := GridTagBack;
1549 PANEL_FORE: newtag := GridTagFore;
1550 PANEL_WATER: newtag := GridTagWater;
1551 PANEL_ACID1: newtag := GridTagAcid1;
1552 PANEL_ACID2: newtag := GridTagAcid2;
1553 PANEL_STEP: newtag := GridTagStep;
1554 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: newtag := GridTagLift;
1555 PANEL_BLOCKMON: newtag := GridTagBlockMon;
1556 else continue; // oops
1557 end;
1558 pan.tag := newtag;
1560 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, newtag);
1561 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1562 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1563 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1565 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1566 begin
1567 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1568 end;
1570 {$ENDIF}
1571 end;
1572 end;
1574 begin
1575 mapGrid.Free();
1576 mapGrid := nil;
1578 calcBoundingBox(gWalls);
1579 calcBoundingBox(gRenderBackgrounds);
1580 calcBoundingBox(gRenderForegrounds);
1581 calcBoundingBox(gWater);
1582 calcBoundingBox(gAcid1);
1583 calcBoundingBox(gAcid2);
1584 calcBoundingBox(gSteps);
1585 calcBoundingBox(gLifts);
1586 calcBoundingBox(gBlockMon);
1588 e_LogWritefln('map dimensions: (%d,%d)-(%d,%d); editor size:(0,0)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1, gMapInfo.Width, gMapInfo.Height]);
1590 if (mapX0 > 0) then mapX0 := 0;
1591 if (mapY0 > 0) then mapY0 := 0;
1593 if (mapX1 < gMapInfo.Width-1) then mapX1 := gMapInfo.Width-1;
1594 if (mapY1 < gMapInfo.Height-1) then mapY1 := gMapInfo.Height-1;
1596 mapGrid := TPanelGrid.Create(mapX0-128, mapY0-128, mapX1-mapX0+1+128*2, mapY1-mapY0+1+128*2);
1597 //mapGrid := TPanelGrid.Create(0, 0, gMapInfo.Width, gMapInfo.Height);
1599 addPanelsToGrid(gWalls);
1600 addPanelsToGrid(gRenderBackgrounds);
1601 addPanelsToGrid(gRenderForegrounds);
1602 addPanelsToGrid(gWater);
1603 addPanelsToGrid(gAcid1);
1604 addPanelsToGrid(gAcid2);
1605 addPanelsToGrid(gSteps);
1606 addPanelsToGrid(gLifts); // it doesn't matter which LIFT type is used here
1607 addPanelsToGrid(gBlockMon);
1609 mapGrid.dumpStats();
1611 g_Mons_InitTree(mapGrid.gridX0, mapGrid.gridY0, mapGrid.gridWidth, mapGrid.gridHeight);
1612 end;
1615 function g_Map_Load(Res: String): Boolean;
1616 const
1617 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1618 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1619 type
1620 PTRec = ^TTRec;
1621 TTRec = record
1622 //TexturePanel: Integer;
1623 tnum: Integer;
1624 id: Integer;
1625 trigrec: TDynRecord;
1626 // texture pane;
1627 texPanelIdx: Integer;
1628 texPanel: TDynRecord;
1629 // "action" panel
1630 actPanelIdx: Integer;
1631 actPanel: TDynRecord;
1632 end;
1633 var
1634 WAD, TestWAD: TWADFile;
1635 //mapReader: TDynRecord = nil;
1636 mapTextureList: TDynField = nil; //TTexturesRec1Array; tagInt: texture index
1637 panels: TDynField = nil; //TPanelsRec1Array;
1638 items: TDynField = nil; //TItemsRec1Array;
1639 monsters: TDynField = nil; //TMonsterRec1Array;
1640 areas: TDynField = nil; //TAreasRec1Array;
1641 triggers: TDynField = nil; //TTriggersRec1Array;
1642 b, c, k: Integer;
1643 PanelID: DWORD;
1644 AddTextures: TAddTextureArray;
1645 TriggersTable: array of TTRec;
1646 FileName, mapResName, TexName, s: AnsiString;
1647 Data: Pointer;
1648 Len: Integer;
1649 ok, isAnim: Boolean;
1650 CurTex, ntn: Integer;
1651 rec, texrec: TDynRecord;
1652 pttit: PTRec;
1653 pannum, trignum, cnt, tgpid: Integer;
1654 stt: UInt64;
1655 moveSpeed{, moveStart, moveEnd}: TDFPoint;
1656 //moveActive: Boolean;
1657 pan: TPanel;
1658 mapOk: Boolean = false;
1659 usedTextures: THashStrInt = nil; // key: mapTextureList
1660 begin
1661 mapGrid.Free();
1662 mapGrid := nil;
1663 TestWAD := nil;
1664 Data := nil;
1666 //gCurrentMap.Free();
1667 //gCurrentMap := nil;
1669 panByGUID := nil;
1671 Result := False;
1672 gMapInfo.Map := Res;
1673 TriggersTable := nil;
1674 //mapReader := nil;
1676 sfsGCDisable(); // temporary disable removing of temporary volumes
1677 try
1678 // Çàãðóçêà WAD (åñëè ó íàñ íåò óæå çàãðóæåíîé êàðòû)
1679 if (gCurrentMap = nil) then
1680 begin
1681 FileName := g_ExtractWadName(Res);
1682 e_WriteLog('Loading map WAD: '+FileName, TMsgType.Notify);
1683 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1685 WAD := TWADFile.Create();
1686 if not WAD.ReadFile(FileName) then
1687 begin
1688 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1689 WAD.Free();
1690 Exit;
1691 end;
1693 if gTestMap <> '' then
1694 begin
1695 s := g_ExtractWadName(gTestMap);
1696 TestWAD := TWADFile.Create();
1697 if not TestWAD.ReadFile(s) then
1698 begin
1699 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_WAD], [s]));
1700 TestWAD.Free();
1701 TestWAD := nil;
1702 end;
1703 end;
1705 if TestWAD <> nil then
1706 begin
1707 mapResName := g_ExtractFileName(gTestMap);
1708 if not TestWAD.GetMapResource(mapResName, Data, Len) then
1709 begin
1710 g_SimpleError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1711 Data := nil;
1712 end else
1713 e_WriteLog('Using test map: '+gTestMap, TMsgType.Notify);
1714 TestWAD.Free();
1715 TestWAD := nil;
1716 end;
1718 if Data = nil then
1719 begin
1720 //k8: why loader ignores path here?
1721 mapResName := g_ExtractFileName(Res);
1722 if not WAD.GetMapResource(mapResName, Data, Len) then
1723 begin
1724 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1725 WAD.Free();
1726 Exit;
1727 end;
1728 end;
1730 WAD.Free();
1732 if (Len < 4) then
1733 begin
1734 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1735 FreeMem(Data);
1736 exit;
1737 end;
1739 // Çàãðóçêà êàðòû:
1740 e_LogWritefln('Loading map: %s', [mapResName], TMsgType.Notify);
1741 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1743 stt := getTimeMicro();
1745 try
1746 gCurrentMap := g_Map_ParseMap(Data, Len);
1747 except
1748 gCurrentMap.Free();
1749 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1750 FreeMem(Data);
1751 gCurrentMapFileName := '';
1752 Exit;
1753 end;
1755 FreeMem(Data);
1757 if (gCurrentMap = nil) then
1758 begin
1759 e_LogWritefln('invalid map file: ''%s''', [mapResName]);
1760 gCurrentMapFileName := '';
1761 exit;
1762 end;
1763 end
1764 else
1765 begin
1766 stt := getTimeMicro();
1767 end;
1769 //gCurrentMap := mapReader;
1771 generateExternalResourcesList(gCurrentMap);
1772 mapTextureList := gCurrentMap['texture'];
1773 // get all other lists here too
1774 panels := gCurrentMap['panel'];
1775 triggers := gCurrentMap['trigger'];
1776 items := gCurrentMap['item'];
1777 areas := gCurrentMap['area'];
1778 monsters := gCurrentMap['monster'];
1780 // Çàãðóçêà îïèñàíèÿ êàðòû:
1781 e_WriteLog(' Reading map info...', TMsgType.Notify);
1782 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1784 with gMapInfo do
1785 begin
1786 Name := gCurrentMap.MapName;
1787 Description := gCurrentMap.MapDesc;
1788 Author := gCurrentMap.MapAuthor;
1789 MusicName := gCurrentMap.MusicName;
1790 SkyName := gCurrentMap.SkyName;
1791 Height := gCurrentMap.Height;
1792 Width := gCurrentMap.Width;
1793 end;
1795 // Çàãðóçêà òåêñòóð:
1796 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1797 // Äîáàâëåíèå òåêñòóð â Textures[]:
1798 if (mapTextureList <> nil) and (mapTextureList.count > 0) then
1799 begin
1800 e_WriteLog(' Loading textures:', TMsgType.Notify);
1801 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], mapTextureList.count-1, False);
1803 // find used textures
1804 usedTextures := THashStrInt.Create();
1805 try
1806 if (panels <> nil) and (panels.count > 0) then
1807 begin
1808 for rec in panels do
1809 begin
1810 texrec := rec.TextureRec;
1811 if (texrec <> nil) then usedTextures.put(toLowerCase1251(texrec.Resource), 42);
1812 end;
1813 end;
1815 cnt := -1;
1816 for rec in mapTextureList do
1817 begin
1818 Inc(cnt);
1819 if not usedTextures.has(toLowerCase1251(rec.Resource)) then
1820 begin
1821 rec.tagInt := -1; // just in case
1822 e_LogWritefln(' Unused texture #%d: %s', [cnt, rec.Resource]);
1823 end
1824 else
1825 begin
1826 {$IF DEFINED(D2F_DEBUG_TXLOAD)}
1827 e_LogWritefln(' Loading texture #%d: %s', [cnt, rec.Resource]);
1828 {$ENDIF}
1829 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1830 if rec.Anim then
1831 begin
1832 // Àíèìèðîâàííàÿ òåêñòóðà
1833 ntn := CreateAnimTexture(rec.Resource, FileName, True);
1834 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [rec.Resource]));
1835 end
1836 else
1837 begin
1838 // Îáû÷íàÿ òåêñòóðà
1839 ntn := CreateTexture(rec.Resource, FileName, True);
1840 if (ntn < 0) then g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [rec.Resource]));
1841 end;
1842 if (ntn < 0) then
1843 begin
1844 ntn := CreateNullTexture(rec.Resource);
1845 end
1846 else
1847 begin
1848 addResToExternalResList(rec.Resource);
1849 end;
1851 rec.tagInt := ntn; // remember texture number
1852 end;
1853 g_Game_StepLoading();
1854 end;
1855 finally
1856 usedTextures.Free();
1857 end;
1859 // set panel tagInt to texture index
1860 if (panels <> nil) then
1861 begin
1862 for rec in panels do
1863 begin
1864 texrec := rec.TextureRec;
1865 if (texrec = nil) then rec.tagInt := -1 else rec.tagInt := texrec.tagInt;
1866 end;
1867 end;
1868 end;
1871 // Çàãðóçêà òðèããåðîâ
1872 gTriggerClientID := 0;
1873 e_WriteLog(' Loading triggers...', TMsgType.Notify);
1874 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1876 // Çàãðóçêà ïàíåëåé
1877 e_WriteLog(' Loading panels...', TMsgType.Notify);
1878 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1880 // check texture numbers for panels
1881 if (panels <> nil) and (panels.count > 0) then
1882 begin
1883 for rec in panels do
1884 begin
1885 if (rec.tagInt < 0) then
1886 begin
1887 e_WriteLog('error loading map: invalid texture index for panel', TMsgType.Fatal);
1888 result := false;
1889 gCurrentMap.Free();
1890 gCurrentMap := nil;
1891 gCurrentMapFileName := '';
1892 exit;
1893 end;
1894 end;
1895 end;
1897 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì)
1898 if (triggers <> nil) and (triggers.count > 0) then
1899 begin
1900 e_WriteLog(' Setting up trigger table...', TMsgType.Notify);
1901 //SetLength(TriggersTable, triggers.count);
1902 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
1904 SetLength(TriggersTable, triggers.count);
1905 trignum := -1;
1906 for rec in triggers do
1907 begin
1908 Inc(trignum);
1909 pttit := @TriggersTable[trignum];
1910 pttit.trigrec := rec;
1911 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
1912 pttit.texPanelIdx := -1; // will be fixed later
1913 pttit.texPanel := rec.TexturePanelRec;
1914 // action panel
1915 pttit.actPanelIdx := -1;
1916 if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
1917 // set flag
1918 if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
1919 if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
1920 // update progress
1921 g_Game_StepLoading();
1922 end;
1923 end;
1925 // Ñîçäàåì ïàíåëè
1926 if (panels <> nil) and (panels.count > 0) then
1927 begin
1928 e_WriteLog(' Setting up trigger links...', TMsgType.Notify);
1929 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], panels.count-1, False);
1931 pannum := -1;
1932 for rec in panels do
1933 begin
1934 Inc(pannum);
1935 //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
1936 texrec := nil;
1937 SetLength(AddTextures, 0);
1938 CurTex := -1;
1939 ok := false;
1941 if (mapTextureList <> nil) then
1942 begin
1943 texrec := rec.TextureRec;
1944 ok := (texrec <> nil);
1945 end;
1947 if ok then
1948 begin
1949 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1950 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð
1951 ok := false;
1952 if (TriggersTable <> nil) and (mapTextureList <> nil) then
1953 begin
1954 if rec.userPanelTrigRef then
1955 begin
1956 // e_LogWritefln('trigref for panel %s', [pannum]);
1957 ok := True;
1958 end;
1959 end;
1960 end;
1962 if ok then
1963 begin
1964 // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1965 s := texrec.Resource;
1967 // Ñïåö-òåêñòóðû çàïðåùåíû
1968 if g_Map_IsSpecialTexture(s) then
1969 begin
1970 ok := false
1971 end
1972 else
1973 begin
1974 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè
1975 ok := g_Texture_NumNameFindStart(s);
1976 end;
1978 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1979 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #
1980 if ok then
1981 begin
1982 k := NNF_NAME_BEFORE;
1983 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû
1984 while ok or (k = NNF_NAME_BEFORE) or (k = NNF_NAME_EQUALS) do
1985 begin
1986 k := g_Texture_NumNameFindNext(TexName);
1988 if (k = NNF_NAME_BEFORE) or (k = NNF_NAME_AFTER) then
1989 begin
1990 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó
1991 if texrec.Anim then
1992 begin
1993 // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1994 isAnim := True;
1995 //e_LogWritefln('000: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1996 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1997 //e_LogWritefln('001: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
1998 if not ok then
1999 begin
2000 // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
2001 isAnim := False;
2002 //e_LogWritefln('002: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2003 ok := CreateTexture(TexName, FileName, False) >= 0;
2004 //e_LogWritefln('003: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2005 end;
2006 end
2007 else
2008 begin
2009 // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
2010 isAnim := False;
2011 //e_LogWritefln('004: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2012 ok := CreateTexture(TexName, FileName, False) >= 0;
2013 //e_LogWritefln('005: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2014 if not ok then
2015 begin
2016 // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
2017 isAnim := True;
2018 //e_LogWritefln('006: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2019 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
2020 //e_LogWritefln('007: pannum=%s; TexName=[%s]; FileName=[%s]', [pannum, TexName, FileName]);
2021 end;
2022 end;
2024 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè
2025 if ok then
2026 begin
2028 for c := 0 to High(Textures) do
2029 begin
2030 if (Textures[c].TextureName = TexName) then
2031 begin
2032 SetLength(AddTextures, Length(AddTextures)+1);
2033 AddTextures[High(AddTextures)].Texture := c;
2034 AddTextures[High(AddTextures)].Anim := isAnim;
2035 break;
2036 end;
2037 end;
2039 if (TextNameHash <> nil) and TextNameHash.get(toLowerCase1251(TexName), c) then
2040 begin
2041 SetLength(AddTextures, Length(AddTextures)+1);
2042 AddTextures[High(AddTextures)].Texture := c;
2043 AddTextures[High(AddTextures)].Anim := isAnim;
2044 end;
2045 end;
2046 end
2047 else
2048 begin
2049 if k = NNF_NAME_EQUALS then
2050 begin
2051 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî
2052 SetLength(AddTextures, Length(AddTextures)+1);
2053 AddTextures[High(AddTextures)].Texture := rec.tagInt; // internal texture number, not map index
2054 AddTextures[High(AddTextures)].Anim := texrec.Anim;
2055 CurTex := High(AddTextures);
2056 ok := true;
2057 end
2058 else // NNF_NO_NAME
2059 begin
2060 ok := false;
2061 end;
2062 end;
2063 end; // while ok...
2065 ok := true;
2066 end; // if ok - åñòü ñìåæíûå òåêñòóðû
2067 end; // if ok - ññûëàþòñÿ òðèããåðû
2069 if not ok then
2070 begin
2071 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó
2072 SetLength(AddTextures, 1);
2073 AddTextures[0].Texture := rec.tagInt; // internal texture number, not map index
2074 AddTextures[0].Anim := false;
2075 if (texrec <> nil) then AddTextures[0].Anim := texrec.Anim;
2076 CurTex := 0;
2077 end;
2079 //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);
2081 //e_LogWritefln('PANADD: pannum=%s', [pannum]);
2083 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
2084 //e_LogWritefln('new panel; tcount=%s; curtex=%s', [Length(AddTextures), CurTex]);
2085 PanelID := CreatePanel(rec, AddTextures, CurTex);
2086 //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
2087 rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
2089 // setup lifts
2090 moveSpeed := rec.moveSpeed;
2091 //moveStart := rec.moveStart;
2092 //moveEnd := rec.moveEnd;
2093 //moveActive := rec['move_active'].value;
2094 if not moveSpeed.isZero then
2095 begin
2096 SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
2097 gMovingWallIds[High(gMovingWallIds)] := PanelID;
2098 //e_LogWritefln('found moving panel ''%s'' (idx=%s; id=%s)', [rec.id, pannum, PanelID]);
2099 end;
2101 //e_LogWritefln('PANEND: pannum=%s', [pannum]);
2103 g_Game_StepLoading();
2104 end;
2105 end;
2107 // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
2108 for b := 0 to High(TriggersTable) do
2109 begin
2110 if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
2111 if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
2112 end;
2114 // create map grid, init other grids (for monsters, for example)
2115 e_WriteLog('Creating map grid', TMsgType.Notify);
2116 mapCreateGrid();
2118 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû
2119 if (triggers <> nil) and (panels <> nil) and (not gLoadGameMode) then
2120 begin
2121 e_LogWritefln(' Creating triggers (%d)...', [triggers.count]);
2122 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
2123 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü
2124 trignum := -1;
2125 for rec in triggers do
2126 begin
2127 Inc(trignum);
2128 tgpid := TriggersTable[trignum].actPanelIdx;
2129 //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
2130 TriggersTable[trignum].tnum := trignum;
2131 TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid);
2132 end;
2133 end;
2135 //FIXME: use hashtable!
2136 for pan in panByGUID do
2137 begin
2138 if (pan.endPosTrigId >= 0) and (pan.endPosTrigId < Length(TriggersTable)) then
2139 begin
2140 pan.endPosTrigId := TriggersTable[pan.endPosTrigId].id;
2141 end;
2142 if (pan.endSizeTrigId >= 0) and (pan.endSizeTrigId < Length(TriggersTable)) then
2143 begin
2144 pan.endSizeTrigId := TriggersTable[pan.endSizeTrigId].id;
2145 end;
2146 end;
2148 // Çàãðóçêà ïðåäìåòîâ
2149 e_WriteLog(' Loading items...', TMsgType.Notify);
2150 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
2152 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû
2153 if (items <> nil) and not gLoadGameMode then
2154 begin
2155 e_WriteLog(' Spawning items...', TMsgType.Notify);
2156 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
2157 for rec in items do CreateItem(rec);
2158 end;
2160 // Çàãðóçêà îáëàñòåé
2161 e_WriteLog(' Loading areas...', TMsgType.Notify);
2162 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
2164 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè
2165 if areas <> nil then
2166 begin
2167 e_WriteLog(' Creating areas...', TMsgType.Notify);
2168 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
2169 for rec in areas do CreateArea(rec);
2170 end;
2172 // Çàãðóçêà ìîíñòðîâ
2173 e_WriteLog(' Loading monsters...', TMsgType.Notify);
2174 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
2176 gTotalMonsters := 0;
2178 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ
2179 if (monsters <> nil) and not gLoadGameMode then
2180 begin
2181 e_WriteLog(' Spawning monsters...', TMsgType.Notify);
2182 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
2183 for rec in monsters do CreateMonster(rec);
2184 end;
2186 //gCurrentMap := mapReader; // this will be our current map now
2187 gCurrentMapFileName := Res;
2188 //mapReader := nil;
2190 // Çàãðóçêà íåáà
2191 if (gMapInfo.SkyName <> '') then
2192 begin
2193 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, TMsgType.Notify);
2194 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
2195 FileName := g_ExtractWadName(gMapInfo.SkyName);
2197 if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
2199 if gTextureFilter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
2200 try
2201 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
2202 if g_Texture_CreateWAD(BackID, s) then
2203 begin
2204 g_Game_SetupScreenSize();
2205 end
2206 else
2207 begin
2208 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
2209 end;
2210 finally
2211 TEXTUREFILTER := GL_NEAREST;
2212 end;
2213 end;
2215 // Çàãðóçêà ìóçûêè
2216 ok := False;
2217 if gMapInfo.MusicName <> '' then
2218 begin
2219 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, TMsgType.Notify);
2220 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
2221 FileName := g_ExtractWadName(gMapInfo.MusicName);
2223 if FileName <> '' then
2224 FileName := GameDir+'/wads/'+FileName
2225 else
2226 begin
2227 FileName := g_ExtractWadName(Res);
2228 end;
2230 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
2231 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
2232 ok := True
2233 else
2234 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
2235 end;
2237 // Îñòàëüíûå óñòàíâêè
2238 CreateDoorMap();
2239 CreateLiftMap();
2241 g_Items_Init();
2242 g_Weapon_Init();
2243 g_Monsters_Init();
2245 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
2246 if not gLoadGameMode then g_GFX_Init();
2248 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
2249 mapTextureList := nil;
2250 panels := nil;
2251 items := nil;
2252 areas := nil;
2253 triggers := nil;
2254 TriggersTable := nil;
2255 AddTextures := nil;
2257 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
2258 if ok and (not gLoadGameMode) then
2259 begin
2260 gMusic.SetByName(gMapInfo.MusicName);
2261 gMusic.Play();
2262 end
2263 else
2264 begin
2265 gMusic.SetByName('');
2266 end;
2268 stt := getTimeMicro()-stt;
2269 e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
2270 mapOk := true;
2271 finally
2272 sfsGCEnable(); // enable releasing unused volumes
2273 //mapReader.Free();
2274 e_UnpressAllKeys; // why not?
2275 if not mapOk then
2276 begin
2277 gCurrentMap.Free();
2278 gCurrentMap := nil;
2279 gCurrentMapFileName := '';
2280 end;
2281 end;
2283 e_WriteLog('Done loading map.', TMsgType.Notify);
2284 Result := True;
2285 end;
2288 function g_Map_GetMapInfo(Res: String): TMapInfo;
2289 var
2290 WAD: TWADFile;
2291 mapReader: TDynRecord;
2292 //Header: TMapHeaderRec_1;
2293 FileName: String;
2294 Data: Pointer;
2295 Len: Integer;
2296 begin
2297 FillChar(Result, SizeOf(Result), 0);
2298 FileName := g_ExtractWadName(Res);
2300 WAD := TWADFile.Create();
2301 if not WAD.ReadFile(FileName) then
2302 begin
2303 WAD.Free();
2304 Exit;
2305 end;
2307 //k8: it ignores path again
2308 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
2309 begin
2310 WAD.Free();
2311 Exit;
2312 end;
2314 WAD.Free();
2316 try
2317 mapReader := g_Map_ParseMap(Data, Len);
2318 except
2319 mapReader := nil;
2320 FreeMem(Data);
2321 exit;
2322 end;
2324 FreeMem(Data);
2326 if (mapReader = nil) then exit;
2328 if (mapReader.Width > 0) and (mapReader.Height > 0) then
2329 begin
2330 Result.Name := mapReader.MapName;
2331 Result.Description := mapReader.MapDesc;
2332 Result.Map := Res;
2333 Result.Author := mapReader.MapAuthor;
2334 Result.Height := mapReader.Height;
2335 Result.Width := mapReader.Width;
2336 end
2337 else
2338 begin
2339 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
2340 //ZeroMemory(@Header, SizeOf(Header));
2341 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
2342 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
2343 Result.Map := Res;
2344 Result.Author := '';
2345 Result.Height := 0;
2346 Result.Width := 0;
2347 end;
2349 mapReader.Free();
2350 end;
2352 function g_Map_GetMapsList(WADName: string): SSArray;
2353 var
2354 WAD: TWADFile;
2355 a: Integer;
2356 ResList: SSArray;
2357 begin
2358 Result := nil;
2359 WAD := TWADFile.Create();
2360 if not WAD.ReadFile(WADName) then
2361 begin
2362 WAD.Free();
2363 Exit;
2364 end;
2365 ResList := WAD.GetMapResources();
2366 if ResList <> nil then
2367 begin
2368 for a := 0 to High(ResList) do
2369 begin
2370 SetLength(Result, Length(Result)+1);
2371 Result[High(Result)] := ResList[a];
2372 end;
2373 end;
2374 WAD.Free();
2375 end;
2377 function g_Map_Exist(Res: string): Boolean;
2378 var
2379 WAD: TWADFile;
2380 FileName, mnn: string;
2381 ResList: SSArray;
2382 a: Integer;
2383 begin
2384 Result := False;
2386 FileName := addWadExtension(g_ExtractWadName(Res));
2388 WAD := TWADFile.Create;
2389 if not WAD.ReadFile(FileName) then
2390 begin
2391 WAD.Free();
2392 Exit;
2393 end;
2395 ResList := WAD.GetMapResources();
2396 WAD.Free();
2398 mnn := g_ExtractFileName(Res);
2399 if ResList <> nil then
2400 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
2401 begin
2402 Result := True;
2403 Exit;
2404 end;
2405 end;
2407 procedure g_Map_Free(freeTextures: Boolean=true);
2408 var
2409 a: Integer;
2411 procedure FreePanelArray(var panels: TPanelArray);
2412 var
2413 i: Integer;
2415 begin
2416 if panels <> nil then
2417 begin
2418 for i := 0 to High(panels) do
2419 panels[i].Free();
2420 panels := nil;
2421 end;
2422 end;
2424 begin
2425 g_GFX_Free();
2426 g_Weapon_Free();
2427 g_Items_Free();
2428 g_Triggers_Free();
2429 g_Monsters_Free();
2431 RespawnPoints := nil;
2432 if FlagPoints[FLAG_RED] <> nil then
2433 begin
2434 Dispose(FlagPoints[FLAG_RED]);
2435 FlagPoints[FLAG_RED] := nil;
2436 end;
2437 if FlagPoints[FLAG_BLUE] <> nil then
2438 begin
2439 Dispose(FlagPoints[FLAG_BLUE]);
2440 FlagPoints[FLAG_BLUE] := nil;
2441 end;
2442 //DOMFlagPoints := nil;
2444 //gDOMFlags := nil;
2446 if (Length(gCurrentMapFileName) <> 0) then
2447 begin
2448 e_LogWritefln('g_Map_Free: previous map was ''%s''...', [gCurrentMapFileName]);
2449 end
2450 else
2451 begin
2452 e_LogWritefln('g_Map_Free: no previous map.', []);
2453 end;
2455 if freeTextures then
2456 begin
2457 e_LogWritefln('g_Map_Free: clearing textures...', []);
2458 if (Textures <> nil) then
2459 begin
2460 for a := 0 to High(Textures) do
2461 begin
2462 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
2463 begin
2464 if Textures[a].Anim then
2465 begin
2466 g_Frames_DeleteByID(Textures[a].FramesID)
2467 end
2468 else
2469 begin
2470 if (Textures[a].TextureID <> LongWord(TEXTURE_NONE)) then
2471 begin
2472 e_DeleteTexture(Textures[a].TextureID);
2473 end;
2474 end;
2475 end;
2476 end;
2477 Textures := nil;
2478 end;
2479 TextNameHash.Free();
2480 TextNameHash := nil;
2481 BadTextNameHash.Free();
2482 BadTextNameHash := nil;
2483 gCurrentMapFileName := '';
2484 gCurrentMap.Free();
2485 gCurrentMap := nil;
2486 end;
2488 panByGUID := nil;
2490 FreePanelArray(gWalls);
2491 FreePanelArray(gRenderBackgrounds);
2492 FreePanelArray(gRenderForegrounds);
2493 FreePanelArray(gWater);
2494 FreePanelArray(gAcid1);
2495 FreePanelArray(gAcid2);
2496 FreePanelArray(gSteps);
2497 FreePanelArray(gLifts);
2498 FreePanelArray(gBlockMon);
2499 gMovingWallIds := nil;
2501 if BackID <> DWORD(-1) then
2502 begin
2503 gBackSize.X := 0;
2504 gBackSize.Y := 0;
2505 e_DeleteTexture(BackID);
2506 BackID := DWORD(-1);
2507 end;
2509 g_Game_StopAllSounds(False);
2510 gMusic.FreeSound();
2511 g_Sound_Delete(gMapInfo.MusicName);
2513 gMapInfo.Name := '';
2514 gMapInfo.Description := '';
2515 gMapInfo.MusicName := '';
2516 gMapInfo.Height := 0;
2517 gMapInfo.Width := 0;
2519 gDoorMap := nil;
2520 gLiftMap := nil;
2521 end;
2523 procedure g_Map_Update();
2524 var
2525 a, d, j: Integer;
2526 m: Word;
2527 s: String;
2528 b: Byte;
2530 procedure UpdatePanelArray(var panels: TPanelArray);
2531 var
2532 i: Integer;
2534 begin
2535 for i := 0 to High(panels) do panels[i].Update();
2536 end;
2538 begin
2539 if g_dbgpan_mplat_step then g_dbgpan_mplat_active := true;
2541 UpdatePanelArray(gWalls);
2542 UpdatePanelArray(gRenderBackgrounds);
2543 UpdatePanelArray(gRenderForegrounds);
2544 UpdatePanelArray(gWater);
2545 UpdatePanelArray(gAcid1);
2546 UpdatePanelArray(gAcid2);
2547 UpdatePanelArray(gSteps);
2549 if g_dbgpan_mplat_step then begin g_dbgpan_mplat_step := false; g_dbgpan_mplat_active := false; end;
2551 if gGameSettings.GameMode = GM_CTF then
2552 begin
2553 for a := FLAG_RED to FLAG_BLUE do
2554 begin
2555 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2556 begin
2557 with gFlags[a] do
2558 begin
2559 if gFlags[a].Animation <> nil then gFlags[a].Animation.Update();
2561 m := g_Obj_Move(@Obj, True, True);
2563 if gTime mod (GAME_TICK*2) <> 0 then Continue;
2565 // Ñîïðîòèâëåíèå âîçäóõà
2566 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2568 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó
2569 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2570 begin
2571 g_Map_ResetFlag(a);
2572 gFlags[a].CaptureTime := 0;
2573 if a = FLAG_RED then
2574 s := _lc[I_PLAYER_FLAG_RED]
2575 else
2576 s := _lc[I_PLAYER_FLAG_BLUE];
2577 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2579 if (((gPlayer1 <> nil) and (((gPlayer1.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer1.Team = TEAM_BLUE) and (a = FLAG_BLUE))))
2580 or ((gPlayer2 <> nil) and (((gPlayer2.Team = TEAM_RED) and (a = FLAG_RED)) or ((gPlayer2.Team = TEAM_BLUE) and (a = FLAG_BLUE))))) then
2581 b := 0
2582 else
2583 b := 1;
2585 if not sound_ret_flag[b].IsPlaying() then
2586 sound_ret_flag[b].Play();
2588 if g_Game_IsNet then
2589 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2590 Continue;
2591 end;
2593 if Count > 0 then Count -= 1;
2595 // Èãðîê áåðåò ôëàã
2596 if gPlayers <> nil then
2597 begin
2598 j := Random(Length(gPlayers)) - 1;
2599 for d := 0 to High(gPlayers) do
2600 begin
2601 Inc(j);
2602 if j > High(gPlayers) then j := 0;
2603 if gPlayers[j] <> nil then
2604 begin
2605 if gPlayers[j].alive and g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2606 begin
2607 if gPlayers[j].GetFlag(a) then Break;
2608 end;
2609 end;
2610 end;
2611 end;
2612 end;
2613 end;
2614 end;
2615 end;
2616 end;
2619 // old algo
2620 procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
2622 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2623 var
2624 idx: Integer;
2625 begin
2626 if (panels <> nil) then
2627 begin
2628 // alas, no visible set
2629 for idx := 0 to High(panels) do
2630 begin
2631 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
2632 end;
2633 end;
2634 end;
2636 begin
2637 case PanelType of
2638 PANEL_WALL: DrawPanels(gWalls);
2639 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2640 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2641 PANEL_FORE: DrawPanels(gRenderForegrounds);
2642 PANEL_WATER: DrawPanels(gWater);
2643 PANEL_ACID1: DrawPanels(gAcid1);
2644 PANEL_ACID2: DrawPanels(gAcid2);
2645 PANEL_STEP: DrawPanels(gSteps);
2646 end;
2647 end;
2650 // new algo
2651 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2652 var
2653 mwit: PPanel;
2654 it: TPanelGrid.Iter;
2655 begin
2656 dplClear();
2657 it := mapGrid.forEachInAABB(x0, y0, wdt, hgt, GridDrawableMask);
2658 for mwit in it do if (((mwit^.tag and GridTagDoor) <> 0) = mwit^.Door) then gDrawPanelList.insert(mwit^);
2659 it.release();
2660 // list will be rendered in `g_game.DrawPlayer()`
2661 end;
2664 procedure g_Map_DrawPanelShadowVolumes (lightX: Integer; lightY: Integer; radius: Integer);
2665 var
2666 mwit: PPanel;
2667 it: TPanelGrid.Iter;
2668 begin
2669 it := mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, (GridTagWall or GridTagDoor));
2670 for mwit in it do mwit^.DrawShadowVolume(lightX, lightY, radius);
2671 it.release();
2672 end;
2675 procedure g_Map_DrawBack(dx, dy: Integer);
2676 begin
2677 if gDrawBackGround and (BackID <> DWORD(-1)) then
2678 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2679 else
2680 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2681 end;
2683 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2684 PanelType: Word; b1x3: Boolean=false): Boolean;
2685 var
2686 a, h: Integer;
2687 begin
2688 Result := False;
2690 if WordBool(PanelType and PANEL_WALL) then
2691 if gWalls <> nil then
2692 begin
2693 h := High(gWalls);
2695 for a := 0 to h do
2696 if gWalls[a].Enabled and
2697 g_Collide(X, Y, Width, Height,
2698 gWalls[a].X, gWalls[a].Y,
2699 gWalls[a].Width, gWalls[a].Height) then
2700 begin
2701 Result := True;
2702 Exit;
2703 end;
2704 end;
2706 if WordBool(PanelType and PANEL_WATER) then
2707 if gWater <> nil then
2708 begin
2709 h := High(gWater);
2711 for a := 0 to h do
2712 if g_Collide(X, Y, Width, Height,
2713 gWater[a].X, gWater[a].Y,
2714 gWater[a].Width, gWater[a].Height) then
2715 begin
2716 Result := True;
2717 Exit;
2718 end;
2719 end;
2721 if WordBool(PanelType and PANEL_ACID1) then
2722 if gAcid1 <> nil then
2723 begin
2724 h := High(gAcid1);
2726 for a := 0 to h do
2727 if g_Collide(X, Y, Width, Height,
2728 gAcid1[a].X, gAcid1[a].Y,
2729 gAcid1[a].Width, gAcid1[a].Height) then
2730 begin
2731 Result := True;
2732 Exit;
2733 end;
2734 end;
2736 if WordBool(PanelType and PANEL_ACID2) then
2737 if gAcid2 <> nil then
2738 begin
2739 h := High(gAcid2);
2741 for a := 0 to h do
2742 if g_Collide(X, Y, Width, Height,
2743 gAcid2[a].X, gAcid2[a].Y,
2744 gAcid2[a].Width, gAcid2[a].Height) then
2745 begin
2746 Result := True;
2747 Exit;
2748 end;
2749 end;
2751 if WordBool(PanelType and PANEL_STEP) then
2752 if gSteps <> nil then
2753 begin
2754 h := High(gSteps);
2756 for a := 0 to h do
2757 if g_Collide(X, Y, Width, Height,
2758 gSteps[a].X, gSteps[a].Y,
2759 gSteps[a].Width, gSteps[a].Height) then
2760 begin
2761 Result := True;
2762 Exit;
2763 end;
2764 end;
2766 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2767 if gLifts <> nil then
2768 begin
2769 h := High(gLifts);
2771 for a := 0 to h do
2772 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = LIFTTYPE_UP)) or
2773 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = LIFTTYPE_DOWN)) or
2774 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = LIFTTYPE_LEFT)) or
2775 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = LIFTTYPE_RIGHT))) and
2776 g_Collide(X, Y, Width, Height,
2777 gLifts[a].X, gLifts[a].Y,
2778 gLifts[a].Width, gLifts[a].Height) then
2779 begin
2780 Result := True;
2781 Exit;
2782 end;
2783 end;
2785 if WordBool(PanelType and PANEL_BLOCKMON) then
2786 if gBlockMon <> nil then
2787 begin
2788 h := High(gBlockMon);
2790 for a := 0 to h do
2791 if ( (not b1x3) or
2792 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2793 g_Collide(X, Y, Width, Height,
2794 gBlockMon[a].X, gBlockMon[a].Y,
2795 gBlockMon[a].Width, gBlockMon[a].Height) then
2796 begin
2797 Result := True;
2798 Exit;
2799 end;
2800 end;
2801 end;
2803 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2804 var
2805 texid: DWORD;
2807 function checkPanels (constref panels: TPanelArray): Boolean;
2808 var
2809 a: Integer;
2810 begin
2811 result := false;
2812 if panels = nil then exit;
2813 for a := 0 to High(panels) do
2814 begin
2815 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2816 begin
2817 result := true;
2818 texid := panels[a].GetTextureID();
2819 exit;
2820 end;
2821 end;
2822 end;
2824 begin
2825 texid := LongWord(TEXTURE_NONE);
2826 result := texid;
2827 if not checkPanels(gWater) then
2828 if not checkPanels(gAcid1) then
2829 if not checkPanels(gAcid2) then exit;
2830 result := texid;
2831 end;
2834 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2835 const
2836 SlowMask = GridTagLift or GridTagBlockMon;
2838 function checker (pan: TPanel; tag: Integer): Boolean;
2839 begin
2841 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2842 begin
2843 result := pan.Enabled;
2844 exit;
2845 end;
2848 if ((tag and GridTagLift) <> 0) then
2849 begin
2850 result :=
2851 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2852 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2853 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2854 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2855 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2856 exit;
2857 end;
2859 if ((tag and GridTagBlockMon) <> 0) then
2860 begin
2861 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2862 exit;
2863 end;
2865 // other shit
2866 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2867 result := true; // i found her!
2868 end;
2870 var
2871 tagmask: Integer = 0;
2872 mwit: PPanel;
2873 it: TPanelGrid.Iter;
2874 pan: TPanel;
2875 begin
2876 result := false;
2877 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2878 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2879 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2880 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2881 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2882 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2883 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2885 if (tagmask = 0) then exit; // just in case
2887 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2888 if gdbg_map_use_accel_coldet then
2889 begin
2890 if ((tagmask and SlowMask) <> 0) then
2891 begin
2892 // slow
2893 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask);
2894 for mwit in it do
2895 begin
2896 pan := mwit^;
2897 if ((pan.tag and GridTagLift) <> 0) then
2898 begin
2899 result :=
2900 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = LIFTTYPE_UP)) or
2901 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = LIFTTYPE_DOWN)) or
2902 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = LIFTTYPE_LEFT)) or
2903 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = LIFTTYPE_RIGHT))) {and
2904 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2905 end
2906 else if ((pan.tag and GridTagBlockMon) <> 0) then
2907 begin
2908 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2909 end
2910 else
2911 begin
2912 // other shit
2913 result := true; // i found her!
2914 end;
2915 if (result) then break;
2916 end;
2917 end
2918 else
2919 begin
2920 // fast
2921 it := mapGrid.forEachInAABB(X, Y, Width, Height, tagmask, false, true); // return first hit
2922 result := (it.length > 0);
2923 end;
2924 it.release();
2925 end
2926 else
2927 begin
2928 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2929 end;
2930 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2931 end;
2934 // returns `true` if we need to stop
2935 function liquidChecker (pan: TPanel; var texid: DWORD; var cctype: Integer): Boolean; inline;
2936 begin
2937 result := false;
2938 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2939 // check priorities
2940 case cctype of
2941 0: if ((pan.tag and GridTagWater) = 0) then exit; // allowed: water
2942 1: if ((pan.tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2943 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2944 end;
2945 // collision?
2946 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2947 // yeah
2948 texid := pan.GetTextureID();
2949 // water? water has the highest priority, so stop right here
2950 if ((pan.tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2951 // acid2?
2952 if ((pan.tag and GridTagAcid2) <> 0) then cctype := 2;
2953 // acid1?
2954 if ((pan.tag and GridTagAcid1) <> 0) then cctype := 1;
2955 end;
2957 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2958 var
2959 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2960 mwit: PPanel;
2961 it: TPanelGrid.Iter;
2962 begin
2963 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2964 if gdbg_map_use_accel_coldet then
2965 begin
2966 result := LongWord(TEXTURE_NONE);
2967 it := mapGrid.forEachInAABB(X, Y, Width, Height, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2968 for mwit in it do if (liquidChecker(mwit^, result, cctype)) then break;
2969 it.release();
2970 end
2971 else
2972 begin
2973 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2974 end;
2975 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2976 end;
2979 procedure g_Map_EnableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_EnableWallGUID(gWalls[ID].guid); end;
2980 procedure g_Map_DisableWall_XXX (ID: DWORD); begin if (ID < Length(gWalls)) then g_Map_DisableWallGUID(gWalls[ID].guid); end;
2981 procedure g_Map_SetLift_XXX (ID: DWORD; t: Integer); begin if (ID < Length(gLifts)) then g_Map_SetLiftGUID(gLifts[ID].guid, t); end;
2984 procedure g_Map_EnableWallGUID (pguid: Integer);
2985 var
2986 pan: TPanel;
2987 begin
2988 //pan := gWalls[ID];
2989 pan := g_Map_PanelByGUID(pguid);
2990 if (pan = nil) then exit;
2991 if pan.Enabled and mapGrid.proxyEnabled[pan.proxyId] then exit;
2993 pan.Enabled := True;
2994 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, true);
2996 mapGrid.proxyEnabled[pan.proxyId] := true;
2997 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2998 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
3000 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3001 // mark platform as interesting
3002 pan.setDirty();
3004 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
3005 //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);
3006 {$ENDIF}
3007 end;
3010 procedure g_Map_DisableWallGUID (pguid: Integer);
3011 var
3012 pan: TPanel;
3013 begin
3014 //pan := gWalls[ID];
3015 pan := g_Map_PanelByGUID(pguid);
3016 if (pan = nil) then exit;
3017 if (not pan.Enabled) and (not mapGrid.proxyEnabled[pan.proxyId]) then exit;
3019 pan.Enabled := False;
3020 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, false);
3022 mapGrid.proxyEnabled[pan.proxyId] := false;
3023 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
3025 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3026 // mark platform as interesting
3027 pan.setDirty();
3029 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
3030 //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);
3031 {$ENDIF}
3032 end;
3035 procedure g_Map_SwitchTextureGUID (pguid: Integer; AnimLoop: Byte = 0);
3036 var
3037 tp: TPanel;
3038 begin
3039 tp := g_Map_PanelByGUID(pguid);
3040 if (tp = nil) then exit;
3041 tp.NextTexture(AnimLoop);
3042 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
3043 end;
3046 procedure g_Map_SetLiftGUID (pguid: Integer; t: Integer);
3047 var
3048 pan: TPanel;
3049 begin
3050 //pan := gLifts[ID];
3051 pan := g_Map_PanelByGUID(pguid);
3052 if (pan = nil) then exit;
3053 if not pan.isGLift then exit;
3055 if ({gLifts[ID]}pan.LiftType = t) then exit; //!FIXME!TRIGANY!
3057 with {gLifts[ID]} pan do
3058 begin
3059 LiftType := t;
3061 g_Mark(X, Y, Width, Height, MARK_LIFT, false);
3062 //TODO: make separate lift tags, and change tag here
3064 case LiftType of
3065 LIFTTYPE_UP: g_Mark(X, Y, Width, Height, MARK_LIFTUP);
3066 LIFTTYPE_DOWN: g_Mark(X, Y, Width, Height, MARK_LIFTDOWN);
3067 LIFTTYPE_LEFT: g_Mark(X, Y, Width, Height, MARK_LIFTLEFT);
3068 LIFTTYPE_RIGHT: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
3069 end;
3071 //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
3072 // mark platform as interesting
3073 pan.setDirty();
3074 end;
3075 end;
3078 function g_Map_GetPoint (PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
3079 var
3080 a: Integer;
3081 PointsArray: Array of TRespawnPoint;
3082 begin
3083 Result := False;
3084 SetLength(PointsArray, 0);
3086 if RespawnPoints = nil then
3087 Exit;
3089 for a := 0 to High(RespawnPoints) do
3090 if RespawnPoints[a].PointType = PointType then
3091 begin
3092 SetLength(PointsArray, Length(PointsArray)+1);
3093 PointsArray[High(PointsArray)] := RespawnPoints[a];
3094 end;
3096 if PointsArray = nil then
3097 Exit;
3099 RespawnPoint := PointsArray[Random(Length(PointsArray))];
3100 Result := True;
3101 end;
3103 function g_Map_GetPointCount(PointType: Byte): Word;
3104 var
3105 a: Integer;
3106 begin
3107 Result := 0;
3109 if RespawnPoints = nil then
3110 Exit;
3112 for a := 0 to High(RespawnPoints) do
3113 if RespawnPoints[a].PointType = PointType then
3114 Result := Result + 1;
3115 end;
3117 function g_Map_HaveFlagPoints(): Boolean;
3118 begin
3119 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
3120 end;
3122 procedure g_Map_ResetFlag(Flag: Byte);
3123 begin
3124 with gFlags[Flag] do
3125 begin
3126 Obj.X := -1000;
3127 Obj.Y := -1000;
3128 Obj.Vel.X := 0;
3129 Obj.Vel.Y := 0;
3130 Direction := TDirection.D_LEFT;
3131 State := FLAG_STATE_NONE;
3132 if FlagPoints[Flag] <> nil then
3133 begin
3134 Obj.X := FlagPoints[Flag]^.X;
3135 Obj.Y := FlagPoints[Flag]^.Y;
3136 Direction := FlagPoints[Flag]^.Direction;
3137 State := FLAG_STATE_NORMAL;
3138 end;
3139 Count := -1;
3140 end;
3141 end;
3143 procedure g_Map_DrawFlags();
3144 var
3145 i, dx: Integer;
3146 Mirror: TMirrorType;
3147 begin
3148 if gGameSettings.GameMode <> GM_CTF then
3149 Exit;
3151 for i := FLAG_RED to FLAG_BLUE do
3152 with gFlags[i] do
3153 if State <> FLAG_STATE_CAPTURED then
3154 begin
3155 if State = FLAG_STATE_NONE then
3156 continue;
3158 if Direction = TDirection.D_LEFT then
3159 begin
3160 Mirror := TMirrorType.Horizontal;
3161 dx := -1;
3162 end
3163 else
3164 begin
3165 Mirror := TMirrorType.None;
3166 dx := 1;
3167 end;
3169 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
3171 if g_debug_Frames then
3172 begin
3173 e_DrawQuad(Obj.X+Obj.Rect.X,
3174 Obj.Y+Obj.Rect.Y,
3175 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
3176 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
3177 0, 255, 0);
3178 end;
3179 end;
3180 end;
3183 procedure g_Map_SaveState (st: TStream);
3184 var
3185 str: String;
3187 procedure savePanels ();
3188 var
3189 pan: TPanel;
3190 begin
3191 // Ñîõðàíÿåì ïàíåëè
3192 utils.writeInt(st, LongInt(Length(panByGUID)));
3193 for pan in panByGUID do pan.SaveState(st);
3194 end;
3196 procedure saveFlag (flag: PFlag);
3197 var
3198 b: Byte;
3199 begin
3200 utils.writeSign(st, 'FLAG');
3201 utils.writeInt(st, Byte(0)); // version
3202 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3203 utils.writeInt(st, Byte(flag^.RespawnType));
3204 // Ñîñòîÿíèå ôëàãà
3205 utils.writeInt(st, Byte(flag^.State));
3206 // Íàïðàâëåíèå ôëàãà
3207 if flag^.Direction = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
3208 utils.writeInt(st, Byte(b));
3209 // Îáúåêò ôëàãà
3210 Obj_SaveState(st, @flag^.Obj);
3211 end;
3213 begin
3214 savePanels();
3216 // Ñîõðàíÿåì ìóçûêó
3217 utils.writeSign(st, 'MUSI');
3218 utils.writeInt(st, Byte(0));
3219 // Íàçâàíèå ìóçûêè
3220 assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
3221 if gMusic.NoMusic then str := '' else str := gMusic.Name;
3222 utils.writeStr(st, str);
3223 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3224 utils.writeInt(st, LongWord(gMusic.GetPosition()));
3225 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3226 utils.writeBool(st, gMusic.SpecPause);
3228 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
3229 utils.writeInt(st, LongInt(gTotalMonsters));
3230 ///// /////
3232 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
3233 if (gGameSettings.GameMode = GM_CTF) then
3234 begin
3235 // Ôëàã Êðàñíîé êîìàíäû
3236 saveFlag(@gFlags[FLAG_RED]);
3237 // Ôëàã Ñèíåé êîìàíäû
3238 saveFlag(@gFlags[FLAG_BLUE]);
3239 end;
3240 ///// /////
3242 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3243 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3244 begin
3245 // Î÷êè Êðàñíîé êîìàíäû
3246 utils.writeInt(st, SmallInt(gTeamStat[TEAM_RED].Goals));
3247 // Î÷êè Ñèíåé êîìàíäû
3248 utils.writeInt(st, SmallInt(gTeamStat[TEAM_BLUE].Goals));
3249 end;
3250 ///// /////
3251 end;
3254 procedure g_Map_LoadState (st: TStream);
3255 var
3256 dw: DWORD;
3257 str: String;
3258 boo: Boolean;
3260 procedure loadPanels ();
3261 var
3262 pan: TPanel;
3263 begin
3264 // Çàãðóæàåì ïàíåëè
3265 if (Length(panByGUID) <> utils.readLongInt(st)) then raise XStreamError.Create('invalid number of saved panels');
3266 for pan in panByGUID do
3267 begin
3268 pan.LoadState(st);
3269 if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
3270 end;
3271 end;
3273 procedure loadFlag (flag: PFlag);
3274 var
3275 b: Byte;
3276 begin
3277 // Ñèãíàòóðà ôëàãà
3278 if not utils.checkSign(st, 'FLAG') then raise XStreamError.Create('invalid flag signature');
3279 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid flag version');
3280 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà
3281 flag^.RespawnType := utils.readByte(st);
3282 // Ñîñòîÿíèå ôëàãà
3283 flag^.State := utils.readByte(st);
3284 // Íàïðàâëåíèå ôëàãà
3285 b := utils.readByte(st);
3286 if (b = 1) then flag^.Direction := TDirection.D_LEFT else flag^.Direction := TDirection.D_RIGHT; // b = 2
3287 // Îáúåêò ôëàãà
3288 Obj_LoadState(@flag^.Obj, st);
3289 end;
3291 begin
3292 if (st = nil) then exit;
3294 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
3295 loadPanels();
3296 ///// /////
3298 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó
3299 g_GFX_Init();
3300 //mapCreateGrid();
3302 ///// Çàãðóæàåì ìóçûêó: /////
3303 if not utils.checkSign(st, 'MUSI') then raise XStreamError.Create('invalid music signature');
3304 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid music version');
3305 // Íàçâàíèå ìóçûêè
3306 assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
3307 str := utils.readStr(st);
3308 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè
3309 dw := utils.readLongWord(st);
3310 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå
3311 boo := utils.readBool(st);
3312 // Çàïóñêàåì ýòó ìóçûêó
3313 gMusic.SetByName(str);
3314 gMusic.SpecPause := boo;
3315 gMusic.Play();
3316 gMusic.Pause(true);
3317 gMusic.SetPosition(dw);
3318 ///// /////
3320 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
3321 gTotalMonsters := utils.readLongInt(st);
3322 ///// /////
3324 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
3325 if (gGameSettings.GameMode = GM_CTF) then
3326 begin
3327 // Ôëàã Êðàñíîé êîìàíäû
3328 loadFlag(@gFlags[FLAG_RED]);
3329 // Ôëàã Ñèíåé êîìàíäû
3330 loadFlag(@gFlags[FLAG_BLUE]);
3331 end;
3332 ///// /////
3334 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
3335 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3336 begin
3337 // Î÷êè Êðàñíîé êîìàíäû
3338 gTeamStat[TEAM_RED].Goals := utils.readSmallInt(st);
3339 // Î÷êè Ñèíåé êîìàíäû
3340 gTeamStat[TEAM_BLUE].Goals := utils.readSmallInt(st);
3341 end;
3342 ///// /////
3343 end;
3346 // trace liquid, stepping by `dx` and `dy`
3347 // return last seen liquid coords, and `false` if we're started outside of the liquid
3348 function g_Map_TraceLiquidNonPrecise (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
3349 const
3350 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
3351 begin
3352 topx := x;
3353 topy := y;
3354 // started outside of the liquid?
3355 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
3356 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then begin result := false; exit; end;
3357 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
3358 result := true;
3359 while true do
3360 begin
3361 Inc(x, dx);
3362 Inc(y, dy);
3363 //if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
3364 if (g_Map_PanelAtPoint(x, y, MaskLiquid) = nil) then exit; // out of the water, just exit
3365 topx := x;
3366 topy := y;
3367 end;
3368 end;
3371 begin
3372 DynWarningCB := mapWarningCB;
3373 end.