DEADSOFTWARE

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