DEADSOFTWARE

more code for tracing and other shit; NOTHING IS WORKING YET
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE MAP_DEBUG_ENABLED_FLAG}
18 unit g_map;
20 interface
22 uses
23 e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
24 g_phys, wadreader, BinEditor, g_panel, g_grid, z_aabbtree, md5, binheap, xprofiler;
26 type
27 TMapInfo = record
28 Map: String;
29 Name: String;
30 Description: String;
31 Author: String;
32 MusicName: String;
33 SkyName: String;
34 Height: Word;
35 Width: Word;
36 end;
38 PRespawnPoint = ^TRespawnPoint;
39 TRespawnPoint = record
40 X, Y: Integer;
41 Direction: TDirection;
42 PointType: Byte;
43 end;
45 PFlagPoint = ^TFlagPoint;
46 TFlagPoint = TRespawnPoint;
48 PFlag = ^TFlag;
49 TFlag = record
50 Obj: TObj;
51 RespawnType: Byte;
52 State: Byte;
53 Count: Integer;
54 CaptureTime: LongWord;
55 Animation: TAnimation;
56 Direction: TDirection;
57 end;
59 function g_Map_Load(Res: String): Boolean;
60 function g_Map_GetMapInfo(Res: String): TMapInfo;
61 function g_Map_GetMapsList(WADName: String): SArray;
62 function g_Map_Exist(Res: String): Boolean;
63 procedure g_Map_Free();
64 procedure g_Map_Update();
66 procedure g_Map_DrawPanels (PanelType: Word); // unaccelerated
67 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
69 procedure g_Map_DrawBack(dx, dy: Integer);
70 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
71 PanelType: Word; b1x3: Boolean=false): Boolean;
72 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
73 procedure g_Map_EnableWall(ID: DWORD);
74 procedure g_Map_DisableWall(ID: DWORD);
75 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
76 procedure g_Map_SetLift(ID: DWORD; t: Integer);
77 procedure g_Map_ReAdd_DieTriggers();
78 function g_Map_IsSpecialTexture(Texture: String): Boolean;
80 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
81 function g_Map_GetPointCount(PointType: Byte): Word;
83 function g_Map_HaveFlagPoints(): Boolean;
85 procedure g_Map_ResetFlag(Flag: Byte);
86 procedure g_Map_DrawFlags();
88 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
90 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
91 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
93 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
95 // returns wall index in `gWalls` or -1
96 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
98 type
99 TForEachPanelCB = function (pan: TPanel): Boolean; // return `true` to stop
101 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
103 // trace liquid, stepping by `dx` and `dy`
104 // return last seen liquid coords, and `false` if we're started outside of the liquid
105 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
108 procedure g_Map_ProfilersBegin ();
109 procedure g_Map_ProfilersEnd ();
112 const
113 RESPAWNPOINT_PLAYER1 = 1;
114 RESPAWNPOINT_PLAYER2 = 2;
115 RESPAWNPOINT_DM = 3;
116 RESPAWNPOINT_RED = 4;
117 RESPAWNPOINT_BLUE = 5;
119 FLAG_NONE = 0;
120 FLAG_RED = 1;
121 FLAG_BLUE = 2;
122 FLAG_DOM = 3;
124 FLAG_STATE_NONE = 0;
125 FLAG_STATE_NORMAL = 1;
126 FLAG_STATE_DROPPED = 2;
127 FLAG_STATE_CAPTURED = 3;
128 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
129 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
131 FLAG_TIME = 720; // 20 seconds
133 SKY_STRETCH: Single = 1.5;
135 const
136 GridTagInvalid = 0;
138 (* draw order:
139 PANEL_BACK
140 PANEL_STEP
141 PANEL_WALL
142 PANEL_CLOSEDOOR
143 PANEL_ACID1
144 PANEL_ACID2
145 PANEL_WATER
146 PANEL_FORE
147 *)
148 // sorted by draw priority
149 GridTagBack = 1 shl 0;
150 GridTagStep = 1 shl 1;
151 GridTagWall = 1 shl 2;
152 GridTagDoor = 1 shl 3;
153 GridTagAcid1 = 1 shl 4;
154 GridTagAcid2 = 1 shl 5;
155 GridTagWater = 1 shl 6;
156 GridTagFore = 1 shl 7;
157 // the following are invisible
158 GridTagLift = 1 shl 8;
159 GridTagBlockMon = 1 shl 9;
161 GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore);
164 var
165 gWalls: TPanelArray;
166 gRenderBackgrounds: TPanelArray;
167 gRenderForegrounds: TPanelArray;
168 gWater, gAcid1, gAcid2: TPanelArray;
169 gSteps: TPanelArray;
170 gLifts: TPanelArray;
171 gBlockMon: TPanelArray;
172 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
173 //gDOMFlags: array of TFlag;
174 gMapInfo: TMapInfo;
175 gBackSize: TPoint;
176 gDoorMap: array of array of DWORD;
177 gLiftMap: array of array of DWORD;
178 gWADHash: TMD5Digest;
179 BackID: DWORD = DWORD(-1);
180 gExternalResources: TStringList;
182 gdbg_map_use_accel_render: Boolean = true;
183 gdbg_map_use_accel_coldet: Boolean = true;
184 //gdbg_map_use_tree_draw: Boolean = false;
185 //gdbg_map_use_tree_coldet: Boolean = false;
186 //gdbg_map_dump_coldet_tree_queries: Boolean = false;
187 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
188 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
190 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
193 implementation
195 uses
196 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
197 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
198 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
199 Math, g_monsters, g_saveload, g_language, g_netmsg,
200 utils, sfs,
201 ImagingTypes, Imaging, ImagingUtility,
202 ImagingGif, ImagingNetworkGraphics;
204 const
205 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
206 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
207 FLAG_SIGNATURE = $47414C46; // 'FLAG'
210 type
211 TPanelGrid = specialize TBodyGridBase<TPanel>;
214 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
216 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
217 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
218 end;
222 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
223 begin
224 result := false;
225 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
226 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
227 if (pan.Width < 1) or (pan.Height < 1) then exit;
228 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
229 result := true;
230 end;
234 function panelTypeToTag (panelType: Word): Integer;
235 begin
236 case panelType of
237 PANEL_WALL: result := GridTagWall; // gWalls
238 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
239 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
240 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
241 PANEL_WATER: result := GridTagWater; // gWater
242 PANEL_ACID1: result := GridTagAcid1; // gAcid1
243 PANEL_ACID2: result := GridTagAcid2; // gAcid2
244 PANEL_STEP: result := GridTagStep; // gSteps
245 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
246 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
247 else result := GridTagInvalid;
248 end;
249 end;
252 function dplLess (a, b: TObject): Boolean;
253 var
254 pa, pb: TPanel;
255 begin
256 pa := TPanel(a);
257 pb := TPanel(b);
258 if (pa.tag < pb.tag) then begin result := true; exit; end;
259 if (pa.tag > pb.tag) then begin result := false; exit; end;
260 result := (pa.arrIdx < pb.arrIdx);
261 end;
263 procedure dplClear ();
264 begin
265 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
266 end;
269 type
270 TPanelID = record
271 PWhere: ^TPanelArray;
272 PArrID: Integer;
273 end;
275 var
276 PanelById: array of TPanelID;
277 Textures: TLevelTextureArray;
278 RespawnPoints: Array of TRespawnPoint;
279 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
280 //DOMFlagPoints: Array of TFlagPoint;
281 mapGrid: TPanelGrid = nil;
282 //mapTree: TDynAABBTreeMap = nil;
285 procedure g_Map_ProfilersBegin ();
286 begin
287 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
288 profMapCollision.mainBegin(g_profile_collision);
289 // create sections
290 if g_profile_collision then
291 begin
292 profMapCollision.sectionBegin('*solids');
293 profMapCollision.sectionEnd();
294 profMapCollision.sectionBegin('liquids');
295 profMapCollision.sectionEnd();
296 end;
297 end;
299 procedure g_Map_ProfilersEnd ();
300 begin
301 if (profMapCollision <> nil) then profMapCollision.mainEnd();
302 end;
305 // wall index in `gWalls` or -1
306 (*
307 function g_Map_traceToNearestWallOld (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
309 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
310 var
311 aabb: AABB2D;
312 tmin: Single;
313 begin
314 result := -666.0; // invalid
315 if not pan.Enabled then exit;
316 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
317 if not aabb.valid then exit;
318 if aabb.intersects(ray, @tmin) then
319 begin
320 //if (tmin*tmin > maxDistSq) then exit;
321 if (tmin >= 0.0) then
322 begin
323 //e_WriteLog(Format('sqchecker(%d,%d,%d,%d): panel #%d (%d,%d)-(%d,%d); tmin=%f', [x0, y0, x1, y1, pan.arrIdx, pan.X, pan.Y, pan.Width, pan.Height, tmin]), MSG_NOTIFY);
324 //if (tmin < 0.0) then tmin := 0.0;
325 result := tmin;
326 end;
327 end;
328 end;
330 var
331 qr: TDynAABBTreeMap.TSegmentQueryResult;
332 ray: Ray2D;
333 hxf, hyf: Single;
334 hx, hy: Integer;
335 maxDistSq: Single;
336 begin
337 result := -1;
338 if (mapTree = nil) then exit;
339 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
340 begin
341 maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
342 if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
343 begin
344 result := qr.flesh.arrIdx;
345 if (hitx <> nil) or (hity <> nil) then
346 begin
347 ray := Ray2D.Create(x0, y0, x1, y1);
348 hxf := ray.origX+ray.dirX*qr.time;
349 hyf := ray.origY+ray.dirY*qr.time;
350 while true do
351 begin
352 hx := trunc(hxf);
353 hy := trunc(hyf);
354 if (hx >= qr.flesh.X) and (hy >= qr.flesh.Y) and (hx < qr.flesh.X+qr.flesh.Width) and (hy < qr.flesh.Y+qr.flesh.Height) then
355 begin
356 // go back a little
357 hxf -= ray.dirX;
358 hyf -= ray.dirY;
359 end
360 else
361 begin
362 break;
363 end;
364 end;
365 if (hitx <> nil) then hitx^ := hx;
366 if (hity <> nil) then hity^ := hy;
367 end;
368 end;
369 end;
370 end;
371 *)
374 // wall index in `gWalls` or -1
375 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
376 (*
377 var
378 lastX, lastY, lastDistSq: Integer;
379 wasHit: Boolean = false;
381 // pan=nil: before processing new tile
382 function sqchecker (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
383 var
384 distSq: Integer;
385 begin
386 if (pan = nil) then
387 begin
388 // stop if something was hit at the previous tile
389 result := wasHit;
390 end
391 else
392 begin
393 result := false;
394 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
395 begin
396 if not pan.Enabled then exit;
397 end;
398 distSq := (prevx-x0)*(prevx-x0)+(prevy-y0)*(prevy-y0);
399 if (distSq < lastDistSq) then
400 begin
401 wasHit := true;
402 lastDistSq := distSq;
403 lastX := prevx;
404 lastY := prevy;
405 end;
406 end;
407 end;
408 *)
409 var
410 ex, ey: Integer;
411 begin
412 (*
413 result := false;
414 if (mapGrid = nil) then exit;
415 lastDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+1;
416 lastX := 0;
417 lastY := 0;
418 result := mapGrid.traceRay(x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor));
419 if (hitx <> nil) then hitx^ := lastX;
420 if (hity <> nil) then hity^ := lastY;
421 *)
422 result := (mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor)) <> nil);
423 end;
426 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
428 function checker (pan: TPanel; tag: Integer): Boolean;
429 begin
431 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
432 begin
433 result := pan.Enabled; // stop if wall is enabled
434 exit;
435 end;
438 if ((tag and GridTagLift) <> 0) then
439 begin
440 // stop if the lift of the right type
441 result :=
442 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
443 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
444 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
445 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
446 exit;
447 end;
449 result := true; // otherwise, stop anyway, 'cause `forEachAtPoint()` is guaranteed to call this only for correct panels
450 end;
452 var
453 tagmask: Integer = 0;
454 begin
455 result := false;
457 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
458 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
459 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
460 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
461 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
462 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
463 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
465 if (tagmask = 0) then exit;// just in case
466 if ((tagmask and GridTagLift) <> 0) then
467 begin
468 // slow
469 result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil);
470 end
471 else
472 begin
473 // fast
474 result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil);
475 end;
476 end;
479 function g_Map_IsSpecialTexture(Texture: String): Boolean;
480 begin
481 Result := (Texture = TEXTURE_NAME_WATER) or
482 (Texture = TEXTURE_NAME_ACID1) or
483 (Texture = TEXTURE_NAME_ACID2);
484 end;
486 procedure CreateDoorMap();
487 var
488 PanelArray: Array of record
489 X, Y: Integer;
490 Width, Height: Word;
491 Active: Boolean;
492 PanelID: DWORD;
493 end;
494 a, b, c, m, i, len: Integer;
495 ok: Boolean;
496 begin
497 if gWalls = nil then
498 Exit;
500 i := 0;
501 len := 128;
502 SetLength(PanelArray, len);
504 for a := 0 to High(gWalls) do
505 if gWalls[a].Door then
506 begin
507 PanelArray[i].X := gWalls[a].X;
508 PanelArray[i].Y := gWalls[a].Y;
509 PanelArray[i].Width := gWalls[a].Width;
510 PanelArray[i].Height := gWalls[a].Height;
511 PanelArray[i].Active := True;
512 PanelArray[i].PanelID := a;
514 i := i + 1;
515 if i = len then
516 begin
517 len := len + 128;
518 SetLength(PanelArray, len);
519 end;
520 end;
522 // Íåò äâåðåé:
523 if i = 0 then
524 begin
525 PanelArray := nil;
526 Exit;
527 end;
529 SetLength(gDoorMap, 0);
531 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
533 for a := 0 to i-1 do
534 if PanelArray[a].Active then
535 begin
536 PanelArray[a].Active := False;
537 m := Length(gDoorMap);
538 SetLength(gDoorMap, m+1);
539 SetLength(gDoorMap[m], 1);
540 gDoorMap[m, 0] := PanelArray[a].PanelID;
541 ok := True;
543 while ok do
544 begin
545 ok := False;
547 for b := 0 to i-1 do
548 if PanelArray[b].Active then
549 for c := 0 to High(gDoorMap[m]) do
550 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
551 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
552 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
553 PanelArray[b].Width, PanelArray[b].Height,
554 gWalls[gDoorMap[m, c]].X,
555 gWalls[gDoorMap[m, c]].Y,
556 gWalls[gDoorMap[m, c]].Width,
557 gWalls[gDoorMap[m, c]].Height) then
558 begin
559 PanelArray[b].Active := False;
560 SetLength(gDoorMap[m],
561 Length(gDoorMap[m])+1);
562 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
563 ok := True;
564 Break;
565 end;
566 end;
568 g_Game_StepLoading();
569 end;
571 PanelArray := nil;
572 end;
574 procedure CreateLiftMap();
575 var
576 PanelArray: Array of record
577 X, Y: Integer;
578 Width, Height: Word;
579 Active: Boolean;
580 end;
581 a, b, c, len, i, j: Integer;
582 ok: Boolean;
583 begin
584 if gLifts = nil then
585 Exit;
587 len := Length(gLifts);
588 SetLength(PanelArray, len);
590 for a := 0 to len-1 do
591 begin
592 PanelArray[a].X := gLifts[a].X;
593 PanelArray[a].Y := gLifts[a].Y;
594 PanelArray[a].Width := gLifts[a].Width;
595 PanelArray[a].Height := gLifts[a].Height;
596 PanelArray[a].Active := True;
597 end;
599 SetLength(gLiftMap, len);
600 i := 0;
602 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
604 for a := 0 to len-1 do
605 if PanelArray[a].Active then
606 begin
607 PanelArray[a].Active := False;
608 SetLength(gLiftMap[i], 32);
609 j := 0;
610 gLiftMap[i, j] := a;
611 ok := True;
613 while ok do
614 begin
615 ok := False;
616 for b := 0 to len-1 do
617 if PanelArray[b].Active then
618 for c := 0 to j do
619 if g_CollideAround(PanelArray[b].X,
620 PanelArray[b].Y,
621 PanelArray[b].Width,
622 PanelArray[b].Height,
623 PanelArray[gLiftMap[i, c]].X,
624 PanelArray[gLiftMap[i, c]].Y,
625 PanelArray[gLiftMap[i, c]].Width,
626 PanelArray[gLiftMap[i, c]].Height) then
627 begin
628 PanelArray[b].Active := False;
629 j := j+1;
630 if j > High(gLiftMap[i]) then
631 SetLength(gLiftMap[i],
632 Length(gLiftMap[i])+32);
634 gLiftMap[i, j] := b;
635 ok := True;
637 Break;
638 end;
639 end;
641 SetLength(gLiftMap[i], j+1);
642 i := i+1;
644 g_Game_StepLoading();
645 end;
647 SetLength(gLiftMap, i);
649 PanelArray := nil;
650 end;
652 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
653 CurTex: Integer; sav: Boolean): Integer;
654 var
655 len: Integer;
656 panels: ^TPanelArray;
657 begin
658 Result := -1;
660 case PanelRec.PanelType of
661 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
662 panels := @gWalls;
663 PANEL_BACK:
664 panels := @gRenderBackgrounds;
665 PANEL_FORE:
666 panels := @gRenderForegrounds;
667 PANEL_WATER:
668 panels := @gWater;
669 PANEL_ACID1:
670 panels := @gAcid1;
671 PANEL_ACID2:
672 panels := @gAcid2;
673 PANEL_STEP:
674 panels := @gSteps;
675 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
676 panels := @gLifts;
677 PANEL_BLOCKMON:
678 panels := @gBlockMon;
679 else
680 Exit;
681 end;
683 len := Length(panels^);
684 SetLength(panels^, len + 1);
686 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
687 panels^[len].arrIdx := len;
688 panels^[len].proxyId := -1;
689 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
690 if sav then
691 panels^[len].SaveIt := True;
693 Result := len;
695 len := Length(PanelByID);
696 SetLength(PanelByID, len + 1);
697 PanelByID[len].PWhere := panels;
698 PanelByID[len].PArrID := Result;
699 end;
701 function CreateNullTexture(RecName: String): Integer;
702 begin
703 SetLength(Textures, Length(Textures)+1);
704 result := High(Textures);
706 with Textures[High(Textures)] do
707 begin
708 TextureName := RecName;
709 Width := 1;
710 Height := 1;
711 Anim := False;
712 TextureID := TEXTURE_NONE;
713 end;
714 end;
716 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
717 var
718 WAD: TWADFile;
719 TextureData: Pointer;
720 WADName, txname: String;
721 a, ResLength: Integer;
722 begin
723 Result := -1;
725 if Textures <> nil then
726 for a := 0 to High(Textures) do
727 if Textures[a].TextureName = RecName then
728 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
729 Result := a;
730 Exit;
731 end;
733 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
734 if (RecName = TEXTURE_NAME_WATER) or
735 (RecName = TEXTURE_NAME_ACID1) or
736 (RecName = TEXTURE_NAME_ACID2) then
737 begin
738 SetLength(Textures, Length(Textures)+1);
740 with Textures[High(Textures)] do
741 begin
742 TextureName := RecName;
744 if TextureName = TEXTURE_NAME_WATER then
745 TextureID := TEXTURE_SPECIAL_WATER
746 else
747 if TextureName = TEXTURE_NAME_ACID1 then
748 TextureID := TEXTURE_SPECIAL_ACID1
749 else
750 if TextureName = TEXTURE_NAME_ACID2 then
751 TextureID := TEXTURE_SPECIAL_ACID2;
753 Anim := False;
754 end;
756 result := High(Textures);
757 Exit;
758 end;
760 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
761 WADName := g_ExtractWadName(RecName);
763 WAD := TWADFile.Create();
765 if WADName <> '' then
766 WADName := GameDir+'/wads/'+WADName
767 else
768 WADName := Map;
770 WAD.ReadFile(WADName);
772 txname := RecName;
774 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
775 begin
776 FreeMem(TextureData);
777 RecName := 'COMMON\ALIEN';
778 end;
781 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
782 begin
783 SetLength(Textures, Length(Textures)+1);
784 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
785 Exit;
786 e_GetTextureSize(Textures[High(Textures)].TextureID,
787 @Textures[High(Textures)].Width,
788 @Textures[High(Textures)].Height);
789 FreeMem(TextureData);
790 Textures[High(Textures)].TextureName := {RecName}txname;
791 Textures[High(Textures)].Anim := False;
793 result := High(Textures);
794 end
795 else // Íåò òàêîãî ðåóñðñà â WAD'å
796 begin
797 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
798 if log then
799 begin
800 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
801 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
802 end;
803 end;
805 WAD.Free();
806 end;
808 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
809 var
810 WAD: TWADFile;
811 TextureWAD: PChar = nil;
812 TextData: Pointer = nil;
813 TextureData: Pointer = nil;
814 cfg: TConfig = nil;
815 WADName: String;
816 ResLength: Integer;
817 TextureResource: String;
818 _width, _height, _framecount, _speed: Integer;
819 _backanimation: Boolean;
820 //imgfmt: string;
821 ia: TDynImageDataArray = nil;
822 f, c, frdelay, frloop: Integer;
823 begin
824 result := -1;
826 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
828 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
829 WADName := g_ExtractWadName(RecName);
831 WAD := TWADFile.Create();
832 try
833 if WADName <> '' then
834 WADName := GameDir+'/wads/'+WADName
835 else
836 WADName := Map;
838 WAD.ReadFile(WADName);
840 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
841 begin
842 if log then
843 begin
844 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
845 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
846 end;
847 exit;
848 end;
850 {TEST
851 if WADName = Map then
852 begin
853 //FreeMem(TextureWAD);
854 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
855 end;
858 WAD.FreeWAD();
860 if ResLength < 6 then
861 begin
862 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
863 exit;
864 end;
866 // ýòî ïòèöà? ýòî ñàìîë¸ò?
867 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
868 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
869 begin
870 // íåò, ýòî ñóïåðìåí!
871 if not WAD.ReadMemory(TextureWAD, ResLength) then
872 begin
873 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
874 exit;
875 end;
877 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
878 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
879 begin
880 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
881 exit;
882 end;
884 cfg := TConfig.CreateMem(TextData, ResLength);
886 TextureResource := cfg.ReadStr('', 'resource', '');
887 if TextureResource = '' then
888 begin
889 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
890 exit;
891 end;
893 _width := cfg.ReadInt('', 'framewidth', 0);
894 _height := cfg.ReadInt('', 'frameheight', 0);
895 _framecount := cfg.ReadInt('', 'framecount', 0);
896 _speed := cfg.ReadInt('', 'waitcount', 0);
897 _backanimation := cfg.ReadBool('', 'backanimation', False);
899 cfg.Free();
900 cfg := nil;
902 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
903 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
904 begin
905 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
906 exit;
907 end;
909 WAD.Free();
910 WAD := nil;
912 SetLength(Textures, Length(Textures)+1);
913 with Textures[High(Textures)] do
914 begin
915 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
916 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
917 begin
918 TextureName := RecName;
919 Width := _width;
920 Height := _height;
921 Anim := True;
922 FramesCount := _framecount;
923 Speed := _speed;
924 result := High(Textures);
925 end
926 else
927 begin
928 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
929 end;
930 end;
931 end
932 else
933 begin
934 // try animated image
936 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
937 if length(imgfmt) = 0 then
938 begin
939 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
940 exit;
941 end;
943 GlobalMetadata.ClearMetaItems();
944 GlobalMetadata.ClearMetaItemsForSaving();
945 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
946 begin
947 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
948 exit;
949 end;
950 if length(ia) = 0 then
951 begin
952 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
953 exit;
954 end;
956 WAD.Free();
957 WAD := nil;
959 _width := ia[0].width;
960 _height := ia[0].height;
961 _framecount := length(ia);
962 _speed := 1;
963 _backanimation := false;
964 frdelay := -1;
965 frloop := -666;
966 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
967 begin
968 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
969 try
970 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
971 frdelay := f;
972 if f < 0 then f := 0;
973 // rounding ;-)
974 c := f mod 28;
975 if c < 13 then c := 0 else c := 1;
976 f := (f div 28)+c;
977 if f < 1 then f := 1 else if f > 255 then f := 255;
978 _speed := f;
979 except
980 end;
981 end;
982 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
983 begin
984 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
985 try
986 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
987 frloop := f;
988 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
989 except
990 end;
991 end;
992 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
993 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
994 f := ord(_backanimation);
995 e_WriteLog(Format('Animated texture file "%s": %d frames (delay:%d; back:%d; frdelay:%d; frloop:%d), %dx%d', [RecName, length(ia), _speed, f, frdelay, frloop, _width, _height]), MSG_NOTIFY);
997 SetLength(Textures, Length(Textures)+1);
998 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
999 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
1000 begin
1001 Textures[High(Textures)].TextureName := RecName;
1002 Textures[High(Textures)].Width := _width;
1003 Textures[High(Textures)].Height := _height;
1004 Textures[High(Textures)].Anim := True;
1005 Textures[High(Textures)].FramesCount := length(ia);
1006 Textures[High(Textures)].Speed := _speed;
1007 result := High(Textures);
1008 //writeln(' CREATED!');
1009 end
1010 else
1011 begin
1012 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
1013 end;
1014 end;
1015 finally
1016 for f := 0 to High(ia) do FreeImage(ia[f]);
1017 WAD.Free();
1018 cfg.Free();
1019 if TextureWAD <> nil then FreeMem(TextureWAD);
1020 if TextData <> nil then FreeMem(TextData);
1021 if TextureData <> nil then FreeMem(TextureData);
1022 end;
1023 end;
1025 procedure CreateItem(Item: TItemRec_1);
1026 begin
1027 if g_Game_IsClient then Exit;
1029 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1030 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1031 Exit;
1033 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1034 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1035 end;
1037 procedure CreateArea(Area: TAreaRec_1);
1038 var
1039 a: Integer;
1040 id: DWORD;
1041 begin
1042 case Area.AreaType of
1043 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1044 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1045 begin
1046 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1047 with RespawnPoints[High(RespawnPoints)] do
1048 begin
1049 X := Area.X;
1050 Y := Area.Y;
1051 Direction := TDirection(Area.Direction);
1053 case Area.AreaType of
1054 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1055 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1056 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1057 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1058 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1059 end;
1060 end;
1061 end;
1063 AREA_REDFLAG, AREA_BLUEFLAG:
1064 begin
1065 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1067 if FlagPoints[a] <> nil then Exit;
1069 New(FlagPoints[a]);
1071 with FlagPoints[a]^ do
1072 begin
1073 X := Area.X-FLAGRECT.X;
1074 Y := Area.Y-FLAGRECT.Y;
1075 Direction := TDirection(Area.Direction);
1076 end;
1078 with gFlags[a] do
1079 begin
1080 case a of
1081 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1082 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1083 end;
1085 Animation := TAnimation.Create(id, True, 8);
1086 Obj.Rect := FLAGRECT;
1088 g_Map_ResetFlag(a);
1089 end;
1090 end;
1092 AREA_DOMFLAG:
1093 begin
1094 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1095 with DOMFlagPoints[High(DOMFlagPoints)] do
1096 begin
1097 X := Area.X;
1098 Y := Area.Y;
1099 Direction := TDirection(Area.Direction);
1100 end;
1102 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1103 end;
1104 end;
1105 end;
1107 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
1108 var
1109 _trigger: TTrigger;
1110 begin
1111 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1113 with _trigger do
1114 begin
1115 X := Trigger.X;
1116 Y := Trigger.Y;
1117 Width := Trigger.Width;
1118 Height := Trigger.Height;
1119 Enabled := ByteBool(Trigger.Enabled);
1120 TexturePanel := Trigger.TexturePanel;
1121 TexturePanelType := fTexturePanel1Type;
1122 ShotPanelType := fTexturePanel2Type;
1123 TriggerType := Trigger.TriggerType;
1124 ActivateType := Trigger.ActivateType;
1125 Keys := Trigger.Keys;
1126 Data.Default := Trigger.DATA;
1127 end;
1129 g_Triggers_Create(_trigger);
1130 end;
1132 procedure CreateMonster(monster: TMonsterRec_1);
1133 var
1134 a: Integer;
1135 mon: TMonster;
1136 begin
1137 if g_Game_IsClient then Exit;
1139 if (gGameSettings.GameType = GT_SINGLE)
1140 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1141 begin
1142 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1144 if gTriggers <> nil then
1145 begin
1146 for a := 0 to High(gTriggers) do
1147 begin
1148 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1149 begin
1150 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1151 end;
1152 end;
1153 end;
1155 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1156 end;
1157 end;
1159 procedure g_Map_ReAdd_DieTriggers();
1161 function monsDieTrig (mon: TMonster): Boolean;
1162 var
1163 a: Integer;
1164 begin
1165 result := false; // don't stop
1166 mon.ClearTriggers();
1167 for a := 0 to High(gTriggers) do
1168 begin
1169 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1170 begin
1171 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1172 end;
1173 end;
1174 end;
1176 begin
1177 if g_Game_IsClient then Exit;
1179 g_Mons_ForEach(monsDieTrig);
1180 end;
1182 function extractWadName(resourceName: string): string;
1183 var
1184 posN: Integer;
1185 begin
1186 posN := Pos(':', resourceName);
1187 if posN > 0 then
1188 Result:= Copy(resourceName, 0, posN-1)
1189 else
1190 Result := '';
1191 end;
1193 procedure addResToExternalResList(res: string);
1194 begin
1195 res := extractWadName(res);
1196 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1197 gExternalResources.Add(res);
1198 end;
1200 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1201 var
1202 textures: TTexturesRec1Array;
1203 mapHeader: TMapHeaderRec_1;
1204 i: integer;
1205 resFile: String = '';
1206 begin
1207 if gExternalResources = nil then
1208 gExternalResources := TStringList.Create;
1210 gExternalResources.Clear;
1211 textures := mapReader.GetTextures();
1212 for i := 0 to High(textures) do
1213 begin
1214 addResToExternalResList(resFile);
1215 end;
1217 textures := nil;
1219 mapHeader := mapReader.GetMapHeader;
1221 addResToExternalResList(mapHeader.MusicName);
1222 addResToExternalResList(mapHeader.SkyName);
1223 end;
1225 procedure mapCreateGrid ();
1226 var
1227 mapX0: Integer = $3fffffff;
1228 mapY0: Integer = $3fffffff;
1229 mapX1: Integer = -$3fffffff;
1230 mapY1: Integer = -$3fffffff;
1232 procedure calcBoundingBox (constref panels: TPanelArray);
1233 var
1234 idx: Integer;
1235 pan: TPanel;
1236 begin
1237 for idx := 0 to High(panels) do
1238 begin
1239 pan := panels[idx];
1240 if not pan.visvalid then continue;
1241 if (pan.Width < 1) or (pan.Height < 1) then continue;
1242 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1243 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1244 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1245 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1246 end;
1247 end;
1249 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1250 var
1251 idx: Integer;
1252 pan: TPanel;
1253 begin
1254 tag := panelTypeToTag(tag);
1255 for idx := High(panels) downto 0 do
1256 begin
1257 pan := panels[idx];
1258 pan.tag := tag;
1259 if not pan.visvalid then continue;
1260 pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1261 // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh...
1262 mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
1263 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
1264 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
1265 begin
1266 e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY);
1267 end;
1268 {$ENDIF}
1269 //mapTree.insertObject(pan, tag, true); // as static object
1270 end;
1271 end;
1273 begin
1274 mapGrid.Free();
1275 mapGrid := nil;
1276 //mapTree.Free();
1277 //mapTree := nil;
1279 calcBoundingBox(gWalls);
1280 calcBoundingBox(gRenderBackgrounds);
1281 calcBoundingBox(gRenderForegrounds);
1282 calcBoundingBox(gWater);
1283 calcBoundingBox(gAcid1);
1284 calcBoundingBox(gAcid2);
1285 calcBoundingBox(gSteps);
1286 calcBoundingBox(gLifts);
1287 calcBoundingBox(gBlockMon);
1289 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1291 mapGrid := TPanelGrid.Create(mapX0-512, mapY0-512, mapX1-mapX0+1+512*2, mapY1-mapY0+1+512*2);
1292 //mapTree := TDynAABBTreeMap.Create();
1294 addPanelsToGrid(gWalls, PANEL_WALL);
1295 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1296 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1297 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1298 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1299 addPanelsToGrid(gWater, PANEL_WATER);
1300 addPanelsToGrid(gAcid1, PANEL_ACID1);
1301 addPanelsToGrid(gAcid2, PANEL_ACID2);
1302 addPanelsToGrid(gSteps, PANEL_STEP);
1303 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1304 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1306 mapGrid.dumpStats();
1307 //e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1308 //mapTree.forEachLeaf(nil);
1309 end;
1311 function g_Map_Load(Res: String): Boolean;
1312 const
1313 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1314 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1315 var
1316 WAD: TWADFile;
1317 MapReader: TMapReader_1;
1318 Header: TMapHeaderRec_1;
1319 _textures: TTexturesRec1Array;
1320 _texnummap: array of Integer; // `_textures` -> `Textures`
1321 panels: TPanelsRec1Array;
1322 items: TItemsRec1Array;
1323 monsters: TMonsterRec1Array;
1324 areas: TAreasRec1Array;
1325 triggers: TTriggersRec1Array;
1326 a, b, c, k: Integer;
1327 PanelID: DWORD;
1328 AddTextures: TAddTextureArray;
1329 texture: TTextureRec_1;
1330 TriggersTable: Array of record
1331 TexturePanel: Integer;
1332 LiftPanel: Integer;
1333 DoorPanel: Integer;
1334 ShotPanel: Integer;
1335 end;
1336 FileName, mapResName, s, TexName: String;
1337 Data: Pointer;
1338 Len: Integer;
1339 ok, isAnim, trigRef: Boolean;
1340 CurTex, ntn: Integer;
1342 begin
1343 mapGrid.Free();
1344 mapGrid := nil;
1345 //mapTree.Free();
1346 //mapTree := nil;
1348 Result := False;
1349 gMapInfo.Map := Res;
1350 TriggersTable := nil;
1351 FillChar(texture, SizeOf(texture), 0);
1353 sfsGCDisable(); // temporary disable removing of temporary volumes
1354 try
1355 // Çàãðóçêà WAD:
1356 FileName := g_ExtractWadName(Res);
1357 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1358 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1360 WAD := TWADFile.Create();
1361 if not WAD.ReadFile(FileName) then
1362 begin
1363 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1364 WAD.Free();
1365 Exit;
1366 end;
1367 //k8: why loader ignores path here?
1368 mapResName := g_ExtractFileName(Res);
1369 if not WAD.GetMapResource(mapResName, Data, Len) then
1370 begin
1371 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1372 WAD.Free();
1373 Exit;
1374 end;
1376 WAD.Free();
1378 // Çàãðóçêà êàðòû:
1379 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1380 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1381 MapReader := TMapReader_1.Create();
1383 if not MapReader.LoadMap(Data) then
1384 begin
1385 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1386 FreeMem(Data);
1387 MapReader.Free();
1388 Exit;
1389 end;
1391 FreeMem(Data);
1392 generateExternalResourcesList(MapReader);
1393 // Çàãðóçêà òåêñòóð:
1394 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1395 _textures := MapReader.GetTextures();
1396 _texnummap := nil;
1398 // Äîáàâëåíèå òåêñòóð â Textures[]:
1399 if _textures <> nil then
1400 begin
1401 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1402 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1403 SetLength(_texnummap, length(_textures));
1405 for a := 0 to High(_textures) do
1406 begin
1407 SetLength(s, 64);
1408 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1409 for b := 1 to Length(s) do
1410 if s[b] = #0 then
1411 begin
1412 SetLength(s, b-1);
1413 Break;
1414 end;
1415 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1416 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1417 // Àíèìèðîâàííàÿ òåêñòóðà:
1418 if ByteBool(_textures[a].Anim) then
1419 begin
1420 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1421 if ntn < 0 then
1422 begin
1423 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1424 ntn := CreateNullTexture(_textures[a].Resource);
1425 end;
1426 end
1427 else // Îáû÷íàÿ òåêñòóðà:
1428 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1429 if ntn < 0 then
1430 begin
1431 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1432 ntn := CreateNullTexture(_textures[a].Resource);
1433 end;
1435 _texnummap[a] := ntn; // fix texture number
1436 g_Game_StepLoading();
1437 end;
1438 end;
1440 // Çàãðóçêà òðèããåðîâ:
1441 gTriggerClientID := 0;
1442 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1443 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1444 triggers := MapReader.GetTriggers();
1446 // Çàãðóçêà ïàíåëåé:
1447 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1448 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1449 panels := MapReader.GetPanels();
1451 // check texture numbers for panels
1452 for a := 0 to High(panels) do
1453 begin
1454 if panels[a].TextureNum > High(_textures) then
1455 begin
1456 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1457 result := false;
1458 exit;
1459 end;
1460 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1461 end;
1463 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1464 if triggers <> nil then
1465 begin
1466 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1467 SetLength(TriggersTable, Length(triggers));
1468 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1470 for a := 0 to High(TriggersTable) do
1471 begin
1472 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1473 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1474 // Ëèôòû:
1475 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1476 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1477 else
1478 TriggersTable[a].LiftPanel := -1;
1479 // Äâåðè:
1480 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1481 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1482 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1483 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1484 else
1485 TriggersTable[a].DoorPanel := -1;
1486 // Òóðåëü:
1487 if triggers[a].TriggerType = TRIGGER_SHOT then
1488 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1489 else
1490 TriggersTable[a].ShotPanel := -1;
1492 g_Game_StepLoading();
1493 end;
1494 end;
1496 // Ñîçäàåì ïàíåëè:
1497 if panels <> nil then
1498 begin
1499 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1500 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1502 for a := 0 to High(panels) do
1503 begin
1504 SetLength(AddTextures, 0);
1505 trigRef := False;
1506 CurTex := -1;
1507 if _textures <> nil then
1508 begin
1509 texture := _textures[panels[a].TextureNum];
1510 ok := True;
1511 end
1512 else
1513 ok := False;
1515 if ok then
1516 begin
1517 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1518 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1519 ok := False;
1520 if (TriggersTable <> nil) and (_textures <> nil) then
1521 for b := 0 to High(TriggersTable) do
1522 if (TriggersTable[b].TexturePanel = a)
1523 or (TriggersTable[b].ShotPanel = a) then
1524 begin
1525 trigRef := True;
1526 ok := True;
1527 Break;
1528 end;
1529 end;
1531 if ok then
1532 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1533 SetLength(s, 64);
1534 CopyMemory(@s[1], @texture.Resource[0], 64);
1535 // Èçìåðÿåì äëèíó:
1536 Len := Length(s);
1537 for c := Len downto 1 do
1538 if s[c] <> #0 then
1539 begin
1540 Len := c;
1541 Break;
1542 end;
1543 SetLength(s, Len);
1545 // Ñïåö-òåêñòóðû çàïðåùåíû:
1546 if g_Map_IsSpecialTexture(s) then
1547 ok := False
1548 else
1549 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1550 ok := g_Texture_NumNameFindStart(s);
1552 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1553 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1554 if ok then
1555 begin
1556 k := NNF_NAME_BEFORE;
1557 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1558 while ok or (k = NNF_NAME_BEFORE) or
1559 (k = NNF_NAME_EQUALS) do
1560 begin
1561 k := g_Texture_NumNameFindNext(TexName);
1563 if (k = NNF_NAME_BEFORE) or
1564 (k = NNF_NAME_AFTER) then
1565 begin
1566 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1567 if ByteBool(texture.Anim) then
1568 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1569 isAnim := True;
1570 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1571 if not ok then
1572 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1573 isAnim := False;
1574 ok := CreateTexture(TexName, FileName, False) >= 0;
1575 end;
1576 end
1577 else
1578 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1579 isAnim := False;
1580 ok := CreateTexture(TexName, FileName, False) >= 0;
1581 if not ok then
1582 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1583 isAnim := True;
1584 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1585 end;
1586 end;
1588 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1589 if ok then
1590 begin
1591 for c := 0 to High(Textures) do
1592 if Textures[c].TextureName = TexName then
1593 begin
1594 SetLength(AddTextures, Length(AddTextures)+1);
1595 AddTextures[High(AddTextures)].Texture := c;
1596 AddTextures[High(AddTextures)].Anim := isAnim;
1597 Break;
1598 end;
1599 end;
1600 end
1601 else
1602 if k = NNF_NAME_EQUALS then
1603 begin
1604 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1605 SetLength(AddTextures, Length(AddTextures)+1);
1606 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1607 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1608 CurTex := High(AddTextures);
1609 ok := True;
1610 end
1611 else // NNF_NO_NAME
1612 ok := False;
1613 end; // while ok...
1615 ok := True;
1616 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1617 end; // if ok - ññûëàþòñÿ òðèããåðû
1619 if not ok then
1620 begin
1621 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1622 SetLength(AddTextures, 1);
1623 AddTextures[0].Texture := panels[a].TextureNum;
1624 AddTextures[0].Anim := ByteBool(texture.Anim);
1625 CurTex := 0;
1626 end;
1628 //e_WriteLog(Format('panel #%d: TextureNum=%d; ht=%d; ht1=%d; atl=%d', [a, panels[a].TextureNum, High(_textures), High(Textures), High(AddTextures)]), MSG_NOTIFY);
1630 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1631 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1633 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1634 if TriggersTable <> nil then
1635 for b := 0 to High(TriggersTable) do
1636 begin
1637 // Òðèããåð äâåðè/ëèôòà:
1638 if (TriggersTable[b].LiftPanel = a) or
1639 (TriggersTable[b].DoorPanel = a) then
1640 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1641 // Òðèããåð ñìåíû òåêñòóðû:
1642 if TriggersTable[b].TexturePanel = a then
1643 triggers[b].TexturePanel := PanelID;
1644 // Òðèããåð "Òóðåëü":
1645 if TriggersTable[b].ShotPanel = a then
1646 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1647 end;
1649 g_Game_StepLoading();
1650 end;
1651 end;
1653 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1654 if (triggers <> nil) and not gLoadGameMode then
1655 begin
1656 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1657 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1658 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1659 for a := 0 to High(triggers) do
1660 begin
1661 if triggers[a].TexturePanel <> -1 then
1662 b := panels[TriggersTable[a].TexturePanel].PanelType
1663 else
1664 b := 0;
1665 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1666 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1667 c := panels[TriggersTable[a].ShotPanel].PanelType
1668 else
1669 c := 0;
1670 CreateTrigger(triggers[a], b, c);
1671 end;
1672 end;
1674 // Çàãðóçêà ïðåäìåòîâ:
1675 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1676 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1677 items := MapReader.GetItems();
1679 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1680 if (items <> nil) and not gLoadGameMode then
1681 begin
1682 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1683 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1684 for a := 0 to High(items) do
1685 CreateItem(Items[a]);
1686 end;
1688 // Çàãðóçêà îáëàñòåé:
1689 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1690 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1691 areas := MapReader.GetAreas();
1693 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1694 if areas <> nil then
1695 begin
1696 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1697 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1698 for a := 0 to High(areas) do
1699 CreateArea(areas[a]);
1700 end;
1702 // Çàãðóçêà ìîíñòðîâ:
1703 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1704 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1705 monsters := MapReader.GetMonsters();
1707 gTotalMonsters := 0;
1709 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1710 if (monsters <> nil) and not gLoadGameMode then
1711 begin
1712 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1713 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1714 for a := 0 to High(monsters) do
1715 CreateMonster(monsters[a]);
1716 end;
1718 // Çàãðóçêà îïèñàíèÿ êàðòû:
1719 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1720 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1721 Header := MapReader.GetMapHeader();
1723 MapReader.Free();
1725 with gMapInfo do
1726 begin
1727 Name := Header.MapName;
1728 Description := Header.MapDescription;
1729 Author := Header.MapAuthor;
1730 MusicName := Header.MusicName;
1731 SkyName := Header.SkyName;
1732 Height := Header.Height;
1733 Width := Header.Width;
1734 end;
1736 // Çàãðóçêà íåáà:
1737 if gMapInfo.SkyName <> '' then
1738 begin
1739 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1740 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1741 FileName := g_ExtractWadName(gMapInfo.SkyName);
1743 if FileName <> '' then
1744 FileName := GameDir+'/wads/'+FileName
1745 else
1746 begin
1747 FileName := g_ExtractWadName(Res);
1748 end;
1750 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1751 if g_Texture_CreateWAD(BackID, s) then
1752 begin
1753 g_Game_SetupScreenSize();
1754 end
1755 else
1756 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1757 end;
1759 // Çàãðóçêà ìóçûêè:
1760 ok := False;
1761 if gMapInfo.MusicName <> '' then
1762 begin
1763 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1764 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1765 FileName := g_ExtractWadName(gMapInfo.MusicName);
1767 if FileName <> '' then
1768 FileName := GameDir+'/wads/'+FileName
1769 else
1770 begin
1771 FileName := g_ExtractWadName(Res);
1772 end;
1774 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1775 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1776 ok := True
1777 else
1778 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1779 end;
1781 // Îñòàëüíûå óñòàíâêè:
1782 CreateDoorMap();
1783 CreateLiftMap();
1785 g_Items_Init();
1786 g_Weapon_Init();
1787 g_Monsters_Init();
1789 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1790 if not gLoadGameMode then
1791 g_GFX_Init();
1793 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1794 _textures := nil;
1795 panels := nil;
1796 items := nil;
1797 areas := nil;
1798 triggers := nil;
1799 TriggersTable := nil;
1800 AddTextures := nil;
1802 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1803 if ok and (not gLoadGameMode) then
1804 begin
1805 gMusic.SetByName(gMapInfo.MusicName);
1806 gMusic.Play();
1807 end
1808 else
1809 gMusic.SetByName('');
1810 finally
1811 sfsGCEnable(); // enable releasing unused volumes
1812 end;
1814 e_WriteLog('Creating map grid', MSG_NOTIFY);
1815 mapCreateGrid();
1817 e_WriteLog('Done loading map.', MSG_NOTIFY);
1818 Result := True;
1819 end;
1821 function g_Map_GetMapInfo(Res: String): TMapInfo;
1822 var
1823 WAD: TWADFile;
1824 MapReader: TMapReader_1;
1825 Header: TMapHeaderRec_1;
1826 FileName: String;
1827 Data: Pointer;
1828 Len: Integer;
1829 begin
1830 FillChar(Result, SizeOf(Result), 0);
1831 FileName := g_ExtractWadName(Res);
1833 WAD := TWADFile.Create();
1834 if not WAD.ReadFile(FileName) then
1835 begin
1836 WAD.Free();
1837 Exit;
1838 end;
1840 //k8: it ignores path again
1841 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1842 begin
1843 WAD.Free();
1844 Exit;
1845 end;
1847 WAD.Free();
1849 MapReader := TMapReader_1.Create();
1851 if not MapReader.LoadMap(Data) then
1852 begin
1853 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1854 ZeroMemory(@Header, SizeOf(Header));
1855 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1856 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1857 end
1858 else
1859 begin
1860 Header := MapReader.GetMapHeader();
1861 Result.Name := Header.MapName;
1862 Result.Description := Header.MapDescription;
1863 end;
1865 FreeMem(Data);
1866 MapReader.Free();
1868 Result.Map := Res;
1869 Result.Author := Header.MapAuthor;
1870 Result.Height := Header.Height;
1871 Result.Width := Header.Width;
1872 end;
1874 function g_Map_GetMapsList(WADName: string): SArray;
1875 var
1876 WAD: TWADFile;
1877 a: Integer;
1878 ResList: SArray;
1879 begin
1880 Result := nil;
1881 WAD := TWADFile.Create();
1882 if not WAD.ReadFile(WADName) then
1883 begin
1884 WAD.Free();
1885 Exit;
1886 end;
1887 ResList := WAD.GetMapResources();
1888 if ResList <> nil then
1889 begin
1890 for a := 0 to High(ResList) do
1891 begin
1892 SetLength(Result, Length(Result)+1);
1893 Result[High(Result)] := ResList[a];
1894 end;
1895 end;
1896 WAD.Free();
1897 end;
1899 function g_Map_Exist(Res: string): Boolean;
1900 var
1901 WAD: TWADFile;
1902 FileName, mnn: string;
1903 ResList: SArray;
1904 a: Integer;
1905 begin
1906 Result := False;
1908 FileName := addWadExtension(g_ExtractWadName(Res));
1910 WAD := TWADFile.Create;
1911 if not WAD.ReadFile(FileName) then
1912 begin
1913 WAD.Free();
1914 Exit;
1915 end;
1917 ResList := WAD.GetMapResources();
1918 WAD.Free();
1920 mnn := g_ExtractFileName(Res);
1921 if ResList <> nil then
1922 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1923 begin
1924 Result := True;
1925 Exit;
1926 end;
1927 end;
1929 procedure g_Map_Free();
1930 var
1931 a: Integer;
1933 procedure FreePanelArray(var panels: TPanelArray);
1934 var
1935 i: Integer;
1937 begin
1938 if panels <> nil then
1939 begin
1940 for i := 0 to High(panels) do
1941 panels[i].Free();
1942 panels := nil;
1943 end;
1944 end;
1946 begin
1947 g_GFX_Free();
1948 g_Weapon_Free();
1949 g_Items_Free();
1950 g_Triggers_Free();
1951 g_Monsters_Free();
1953 RespawnPoints := nil;
1954 if FlagPoints[FLAG_RED] <> nil then
1955 begin
1956 Dispose(FlagPoints[FLAG_RED]);
1957 FlagPoints[FLAG_RED] := nil;
1958 end;
1959 if FlagPoints[FLAG_BLUE] <> nil then
1960 begin
1961 Dispose(FlagPoints[FLAG_BLUE]);
1962 FlagPoints[FLAG_BLUE] := nil;
1963 end;
1964 //DOMFlagPoints := nil;
1966 //gDOMFlags := nil;
1968 if Textures <> nil then
1969 begin
1970 for a := 0 to High(Textures) do
1971 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1972 if Textures[a].Anim then
1973 g_Frames_DeleteByID(Textures[a].FramesID)
1974 else
1975 if Textures[a].TextureID <> TEXTURE_NONE then
1976 e_DeleteTexture(Textures[a].TextureID);
1978 Textures := nil;
1979 end;
1981 FreePanelArray(gWalls);
1982 FreePanelArray(gRenderBackgrounds);
1983 FreePanelArray(gRenderForegrounds);
1984 FreePanelArray(gWater);
1985 FreePanelArray(gAcid1);
1986 FreePanelArray(gAcid2);
1987 FreePanelArray(gSteps);
1988 FreePanelArray(gLifts);
1989 FreePanelArray(gBlockMon);
1991 if BackID <> DWORD(-1) then
1992 begin
1993 gBackSize.X := 0;
1994 gBackSize.Y := 0;
1995 e_DeleteTexture(BackID);
1996 BackID := DWORD(-1);
1997 end;
1999 g_Game_StopAllSounds(False);
2000 gMusic.FreeSound();
2001 g_Sound_Delete(gMapInfo.MusicName);
2003 gMapInfo.Name := '';
2004 gMapInfo.Description := '';
2005 gMapInfo.MusicName := '';
2006 gMapInfo.Height := 0;
2007 gMapInfo.Width := 0;
2009 gDoorMap := nil;
2010 gLiftMap := nil;
2012 PanelByID := nil;
2013 end;
2015 procedure g_Map_Update();
2016 var
2017 a, d, j: Integer;
2018 m: Word;
2019 s: String;
2021 procedure UpdatePanelArray(var panels: TPanelArray);
2022 var
2023 i: Integer;
2025 begin
2026 if panels <> nil then
2027 for i := 0 to High(panels) do
2028 panels[i].Update();
2029 end;
2031 begin
2032 UpdatePanelArray(gWalls);
2033 UpdatePanelArray(gRenderBackgrounds);
2034 UpdatePanelArray(gRenderForegrounds);
2035 UpdatePanelArray(gWater);
2036 UpdatePanelArray(gAcid1);
2037 UpdatePanelArray(gAcid2);
2038 UpdatePanelArray(gSteps);
2040 if gGameSettings.GameMode = GM_CTF then
2041 begin
2042 for a := FLAG_RED to FLAG_BLUE do
2043 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2044 with gFlags[a] do
2045 begin
2046 if gFlags[a].Animation <> nil then
2047 gFlags[a].Animation.Update();
2049 m := g_Obj_Move(@Obj, True, True);
2051 if gTime mod (GAME_TICK*2) <> 0 then
2052 Continue;
2054 // Ñîïðîòèâëåíèå âîçäóõà:
2055 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2057 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2058 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2059 begin
2060 g_Map_ResetFlag(a);
2061 gFlags[a].CaptureTime := 0;
2062 if a = FLAG_RED then
2063 s := _lc[I_PLAYER_FLAG_RED]
2064 else
2065 s := _lc[I_PLAYER_FLAG_BLUE];
2066 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2068 if g_Game_IsNet then
2069 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2070 Continue;
2071 end;
2073 if Count > 0 then
2074 Count := Count - 1;
2076 // Èãðîê áåðåò ôëàã:
2077 if gPlayers <> nil then
2078 begin
2079 j := Random(Length(gPlayers)) - 1;
2081 for d := 0 to High(gPlayers) do
2082 begin
2083 Inc(j);
2084 if j > High(gPlayers) then
2085 j := 0;
2087 if gPlayers[j] <> nil then
2088 if gPlayers[j].Live and
2089 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2090 begin
2091 if gPlayers[j].GetFlag(a) then
2092 Break;
2093 end;
2094 end;
2095 end;
2096 end;
2097 end;
2098 end;
2101 // old algo
2102 procedure g_Map_DrawPanels (PanelType: Word);
2104 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2105 var
2106 idx: Integer;
2107 begin
2108 if (panels <> nil) then
2109 begin
2110 // alas, no visible set
2111 for idx := 0 to High(panels) do
2112 begin
2113 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2114 end;
2115 end;
2116 end;
2118 begin
2119 case PanelType of
2120 PANEL_WALL: DrawPanels(gWalls);
2121 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2122 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2123 PANEL_FORE: DrawPanels(gRenderForegrounds);
2124 PANEL_WATER: DrawPanels(gWater);
2125 PANEL_ACID1: DrawPanels(gAcid1);
2126 PANEL_ACID2: DrawPanels(gAcid2);
2127 PANEL_STEP: DrawPanels(gSteps);
2128 end;
2129 end;
2132 // new algo
2133 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2135 function checker (pan: TPanel; tag: Integer): Boolean;
2136 begin
2137 result := false; // don't stop, ever
2138 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2139 gDrawPanelList.insert(pan);
2140 end;
2142 begin
2143 dplClear();
2144 //tagmask := panelTypeToTag(PanelType);
2146 {if gdbg_map_use_tree_draw then
2147 begin
2148 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2149 end
2150 else}
2151 begin
2152 mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask, true);
2153 end;
2154 // list will be rendered in `g_game.DrawPlayer()`
2155 end;
2158 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2160 function checker (pan: TPanel; tag: Integer): Boolean;
2161 begin
2162 result := false; // don't stop, ever
2163 pan.DrawShadowVolume(lightX, lightY, radius);
2164 end;
2166 begin
2167 {if gdbg_map_use_tree_draw then
2168 begin
2169 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2170 end
2171 else}
2172 begin
2173 mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor), true);
2174 end;
2175 end;
2178 procedure g_Map_DrawBack(dx, dy: Integer);
2179 begin
2180 if gDrawBackGround and (BackID <> DWORD(-1)) then
2181 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2182 else
2183 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2184 end;
2186 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2187 PanelType: Word; b1x3: Boolean=false): Boolean;
2188 var
2189 a, h: Integer;
2190 begin
2191 Result := False;
2193 if WordBool(PanelType and PANEL_WALL) then
2194 if gWalls <> nil then
2195 begin
2196 h := High(gWalls);
2198 for a := 0 to h do
2199 if gWalls[a].Enabled and
2200 g_Collide(X, Y, Width, Height,
2201 gWalls[a].X, gWalls[a].Y,
2202 gWalls[a].Width, gWalls[a].Height) then
2203 begin
2204 Result := True;
2205 Exit;
2206 end;
2207 end;
2209 if WordBool(PanelType and PANEL_WATER) then
2210 if gWater <> nil then
2211 begin
2212 h := High(gWater);
2214 for a := 0 to h do
2215 if g_Collide(X, Y, Width, Height,
2216 gWater[a].X, gWater[a].Y,
2217 gWater[a].Width, gWater[a].Height) then
2218 begin
2219 Result := True;
2220 Exit;
2221 end;
2222 end;
2224 if WordBool(PanelType and PANEL_ACID1) then
2225 if gAcid1 <> nil then
2226 begin
2227 h := High(gAcid1);
2229 for a := 0 to h do
2230 if g_Collide(X, Y, Width, Height,
2231 gAcid1[a].X, gAcid1[a].Y,
2232 gAcid1[a].Width, gAcid1[a].Height) then
2233 begin
2234 Result := True;
2235 Exit;
2236 end;
2237 end;
2239 if WordBool(PanelType and PANEL_ACID2) then
2240 if gAcid2 <> nil then
2241 begin
2242 h := High(gAcid2);
2244 for a := 0 to h do
2245 if g_Collide(X, Y, Width, Height,
2246 gAcid2[a].X, gAcid2[a].Y,
2247 gAcid2[a].Width, gAcid2[a].Height) then
2248 begin
2249 Result := True;
2250 Exit;
2251 end;
2252 end;
2254 if WordBool(PanelType and PANEL_STEP) then
2255 if gSteps <> nil then
2256 begin
2257 h := High(gSteps);
2259 for a := 0 to h do
2260 if g_Collide(X, Y, Width, Height,
2261 gSteps[a].X, gSteps[a].Y,
2262 gSteps[a].Width, gSteps[a].Height) then
2263 begin
2264 Result := True;
2265 Exit;
2266 end;
2267 end;
2269 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2270 if gLifts <> nil then
2271 begin
2272 h := High(gLifts);
2274 for a := 0 to h do
2275 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2276 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2277 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2278 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2279 g_Collide(X, Y, Width, Height,
2280 gLifts[a].X, gLifts[a].Y,
2281 gLifts[a].Width, gLifts[a].Height) then
2282 begin
2283 Result := True;
2284 Exit;
2285 end;
2286 end;
2288 if WordBool(PanelType and PANEL_BLOCKMON) then
2289 if gBlockMon <> nil then
2290 begin
2291 h := High(gBlockMon);
2293 for a := 0 to h do
2294 if ( (not b1x3) or
2295 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2296 g_Collide(X, Y, Width, Height,
2297 gBlockMon[a].X, gBlockMon[a].Y,
2298 gBlockMon[a].Width, gBlockMon[a].Height) then
2299 begin
2300 Result := True;
2301 Exit;
2302 end;
2303 end;
2304 end;
2306 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2307 var
2308 texid: DWORD;
2310 function checkPanels (constref panels: TPanelArray): Boolean;
2311 var
2312 a: Integer;
2313 begin
2314 result := false;
2315 if panels = nil then exit;
2316 for a := 0 to High(panels) do
2317 begin
2318 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2319 begin
2320 result := true;
2321 texid := panels[a].GetTextureID();
2322 exit;
2323 end;
2324 end;
2325 end;
2327 begin
2328 texid := TEXTURE_NONE;
2329 result := texid;
2330 if not checkPanels(gWater) then
2331 if not checkPanels(gAcid1) then
2332 if not checkPanels(gAcid2) then exit;
2333 result := texid;
2334 end;
2337 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2338 const
2339 SlowMask = GridTagLift or GridTagBlockMon;
2340 function checker (pan: TPanel; tag: Integer): Boolean;
2341 begin
2343 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2344 begin
2345 result := pan.Enabled;
2346 exit;
2347 end;
2350 if ((tag and GridTagLift) <> 0) then
2351 begin
2352 result :=
2353 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2354 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2355 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2356 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) {and
2357 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height)};
2358 exit;
2359 end;
2361 if ((tag and GridTagBlockMon) <> 0) then
2362 begin
2363 result := ((not b1x3) or (pan.Width+pan.Height >= 64)); //and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2364 exit;
2365 end;
2367 // other shit
2368 //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2369 result := true; // i found her!
2370 end;
2372 var
2373 tagmask: Integer = 0;
2374 begin
2375 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2376 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2377 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2378 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2379 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2380 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2381 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2383 if (tagmask = 0) then begin result := false; exit; end; // just in case
2385 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2386 if gdbg_map_use_accel_coldet then
2387 begin
2388 {if gdbg_map_use_tree_coldet then
2389 begin
2390 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2391 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2392 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2393 begin
2394 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2395 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2396 end;
2397 end
2398 else}
2399 begin
2400 if (Width = 1) and (Height = 1) then
2401 begin
2402 if ((tagmask and SlowMask) <> 0) then
2403 begin
2404 // slow
2405 result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil);
2406 end
2407 else
2408 begin
2409 // fast
2410 result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil);
2411 end;
2412 end
2413 else
2414 begin
2415 if ((tagmask and SlowMask) <> 0) then
2416 begin
2417 // slow
2418 result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil);
2419 end
2420 else
2421 begin
2422 // fast
2423 result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil);
2424 end;
2425 end;
2426 end;
2427 end
2428 else
2429 begin
2430 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2431 end;
2432 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2433 end;
2436 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2437 var
2438 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2439 texid: DWORD;
2441 // slightly different from the old code, but meh...
2442 function checker (pan: TPanel; tag: Integer): Boolean;
2443 begin
2444 result := false; // don't stop, ever
2445 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2446 // check priorities
2447 case cctype of
2448 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2449 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2450 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2451 end;
2452 // collision?
2453 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2454 // yeah
2455 texid := pan.GetTextureID();
2456 // water? water has the highest priority, so stop right here
2457 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2458 // acid2?
2459 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2460 // acid1?
2461 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2462 end;
2464 begin
2465 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2466 if gdbg_map_use_accel_coldet then
2467 begin
2468 texid := TEXTURE_NONE;
2469 {if gdbg_map_use_tree_coldet then
2470 begin
2471 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2472 end
2473 else}
2474 begin
2475 if (Width = 1) and (Height = 1) then
2476 begin
2477 mapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2478 end
2479 else
2480 begin
2481 mapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2482 end;
2483 end;
2484 result := texid;
2485 end
2486 else
2487 begin
2488 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2489 end;
2490 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2491 end;
2494 procedure g_Map_EnableWall(ID: DWORD);
2495 var
2496 pan: TPanel;
2497 begin
2498 pan := gWalls[ID];
2499 pan.Enabled := True;
2500 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True);
2502 mapGrid.proxyEnabled[pan.proxyId] := true;
2503 //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
2504 //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
2506 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID);
2508 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2509 e_WriteLog(Format('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);
2510 {$ENDIF}
2511 end;
2513 procedure g_Map_DisableWall(ID: DWORD);
2514 var
2515 pan: TPanel;
2516 begin
2517 pan := gWalls[ID];
2518 pan.Enabled := False;
2519 g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False);
2521 mapGrid.proxyEnabled[pan.proxyId] := false;
2522 //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
2524 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID);
2526 {$IFDEF MAP_DEBUG_ENABLED_FLAG}
2527 e_WriteLog(Format('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);
2528 {$ENDIF}
2529 end;
2531 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2532 var
2533 tp: TPanel;
2534 begin
2535 case PanelType of
2536 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2537 tp := gWalls[ID];
2538 PANEL_FORE:
2539 tp := gRenderForegrounds[ID];
2540 PANEL_BACK:
2541 tp := gRenderBackgrounds[ID];
2542 PANEL_WATER:
2543 tp := gWater[ID];
2544 PANEL_ACID1:
2545 tp := gAcid1[ID];
2546 PANEL_ACID2:
2547 tp := gAcid2[ID];
2548 PANEL_STEP:
2549 tp := gSteps[ID];
2550 else
2551 Exit;
2552 end;
2554 tp.NextTexture(AnimLoop);
2555 if g_Game_IsServer and g_Game_IsNet then
2556 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2557 end;
2559 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2560 begin
2561 if gLifts[ID].LiftType = t then
2562 Exit;
2564 with gLifts[ID] do
2565 begin
2566 LiftType := t;
2568 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2569 //TODO: make separate lift tags, and change tag here
2571 if LiftType = 0 then
2572 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2573 else if LiftType = 1 then
2574 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2575 else if LiftType = 2 then
2576 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2577 else if LiftType = 3 then
2578 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2580 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2581 end;
2582 end;
2584 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2585 var
2586 a: Integer;
2587 PointsArray: Array of TRespawnPoint;
2588 begin
2589 Result := False;
2590 SetLength(PointsArray, 0);
2592 if RespawnPoints = nil then
2593 Exit;
2595 for a := 0 to High(RespawnPoints) do
2596 if RespawnPoints[a].PointType = PointType then
2597 begin
2598 SetLength(PointsArray, Length(PointsArray)+1);
2599 PointsArray[High(PointsArray)] := RespawnPoints[a];
2600 end;
2602 if PointsArray = nil then
2603 Exit;
2605 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2606 Result := True;
2607 end;
2609 function g_Map_GetPointCount(PointType: Byte): Word;
2610 var
2611 a: Integer;
2612 begin
2613 Result := 0;
2615 if RespawnPoints = nil then
2616 Exit;
2618 for a := 0 to High(RespawnPoints) do
2619 if RespawnPoints[a].PointType = PointType then
2620 Result := Result + 1;
2621 end;
2623 function g_Map_HaveFlagPoints(): Boolean;
2624 begin
2625 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2626 end;
2628 procedure g_Map_ResetFlag(Flag: Byte);
2629 begin
2630 with gFlags[Flag] do
2631 begin
2632 Obj.X := -1000;
2633 Obj.Y := -1000;
2634 Obj.Vel.X := 0;
2635 Obj.Vel.Y := 0;
2636 Direction := D_LEFT;
2637 State := FLAG_STATE_NONE;
2638 if FlagPoints[Flag] <> nil then
2639 begin
2640 Obj.X := FlagPoints[Flag]^.X;
2641 Obj.Y := FlagPoints[Flag]^.Y;
2642 Direction := FlagPoints[Flag]^.Direction;
2643 State := FLAG_STATE_NORMAL;
2644 end;
2645 Count := -1;
2646 end;
2647 end;
2649 procedure g_Map_DrawFlags();
2650 var
2651 i, dx: Integer;
2652 Mirror: TMirrorType;
2653 begin
2654 if gGameSettings.GameMode <> GM_CTF then
2655 Exit;
2657 for i := FLAG_RED to FLAG_BLUE do
2658 with gFlags[i] do
2659 if State <> FLAG_STATE_CAPTURED then
2660 begin
2661 if State = FLAG_STATE_NONE then
2662 continue;
2664 if Direction = D_LEFT then
2665 begin
2666 Mirror := M_HORIZONTAL;
2667 dx := -1;
2668 end
2669 else
2670 begin
2671 Mirror := M_NONE;
2672 dx := 1;
2673 end;
2675 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2677 if g_debug_Frames then
2678 begin
2679 e_DrawQuad(Obj.X+Obj.Rect.X,
2680 Obj.Y+Obj.Rect.Y,
2681 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2682 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2683 0, 255, 0);
2684 end;
2685 end;
2686 end;
2688 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2689 var
2690 dw: DWORD;
2691 b: Byte;
2692 str: String;
2693 boo: Boolean;
2695 procedure SavePanelArray(var panels: TPanelArray);
2696 var
2697 PAMem: TBinMemoryWriter;
2698 i: Integer;
2699 begin
2700 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2701 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2703 i := 0;
2704 while i < Length(panels) do
2705 begin
2706 if panels[i].SaveIt then
2707 begin
2708 // ID ïàíåëè:
2709 PAMem.WriteInt(i);
2710 // Ñîõðàíÿåì ïàíåëü:
2711 panels[i].SaveState(PAMem);
2712 end;
2713 Inc(i);
2714 end;
2716 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2717 PAMem.SaveToMemory(Mem);
2718 PAMem.Free();
2719 end;
2721 procedure SaveFlag(flag: PFlag);
2722 begin
2723 // Ñèãíàòóðà ôëàãà:
2724 dw := FLAG_SIGNATURE; // 'FLAG'
2725 Mem.WriteDWORD(dw);
2726 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2727 Mem.WriteByte(flag^.RespawnType);
2728 // Ñîñòîÿíèå ôëàãà:
2729 Mem.WriteByte(flag^.State);
2730 // Íàïðàâëåíèå ôëàãà:
2731 if flag^.Direction = D_LEFT then
2732 b := 1
2733 else // D_RIGHT
2734 b := 2;
2735 Mem.WriteByte(b);
2736 // Îáúåêò ôëàãà:
2737 Obj_SaveState(@flag^.Obj, Mem);
2738 end;
2740 begin
2741 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2743 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2744 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2745 SavePanelArray(gWalls);
2746 // Ñîõðàíÿåì ïàíåëè ôîíà:
2747 SavePanelArray(gRenderBackgrounds);
2748 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2749 SavePanelArray(gRenderForegrounds);
2750 // Ñîõðàíÿåì ïàíåëè âîäû:
2751 SavePanelArray(gWater);
2752 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2753 SavePanelArray(gAcid1);
2754 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2755 SavePanelArray(gAcid2);
2756 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2757 SavePanelArray(gSteps);
2758 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2759 SavePanelArray(gLifts);
2760 ///// /////
2762 ///// Ñîõðàíÿåì ìóçûêó: /////
2763 // Ñèãíàòóðà ìóçûêè:
2764 dw := MUSIC_SIGNATURE; // 'MUSI'
2765 Mem.WriteDWORD(dw);
2766 // Íàçâàíèå ìóçûêè:
2767 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2768 if gMusic.NoMusic then
2769 str := ''
2770 else
2771 str := gMusic.Name;
2772 Mem.WriteString(str, 64);
2773 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2774 dw := gMusic.GetPosition();
2775 Mem.WriteDWORD(dw);
2776 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2777 boo := gMusic.SpecPause;
2778 Mem.WriteBoolean(boo);
2779 ///// /////
2781 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2782 Mem.WriteInt(gTotalMonsters);
2783 ///// /////
2785 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2786 if gGameSettings.GameMode = GM_CTF then
2787 begin
2788 // Ôëàã Êðàñíîé êîìàíäû:
2789 SaveFlag(@gFlags[FLAG_RED]);
2790 // Ôëàã Ñèíåé êîìàíäû:
2791 SaveFlag(@gFlags[FLAG_BLUE]);
2792 end;
2793 ///// /////
2795 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2796 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2797 begin
2798 // Î÷êè Êðàñíîé êîìàíäû:
2799 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2800 // Î÷êè Ñèíåé êîìàíäû:
2801 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2802 end;
2803 ///// /////
2804 end;
2806 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2807 var
2808 dw: DWORD;
2809 b: Byte;
2810 str: String;
2811 boo: Boolean;
2813 procedure LoadPanelArray(var panels: TPanelArray);
2814 var
2815 PAMem: TBinMemoryReader;
2816 i, id: Integer;
2817 begin
2818 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2819 PAMem := TBinMemoryReader.Create();
2820 PAMem.LoadFromMemory(Mem);
2822 for i := 0 to Length(panels)-1 do
2823 if panels[i].SaveIt then
2824 begin
2825 // ID ïàíåëè:
2826 PAMem.ReadInt(id);
2827 if id <> i then
2828 begin
2829 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2830 end;
2831 // Çàãðóæàåì ïàíåëü:
2832 panels[i].LoadState(PAMem);
2833 end;
2835 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2836 PAMem.Free();
2837 end;
2839 procedure LoadFlag(flag: PFlag);
2840 begin
2841 // Ñèãíàòóðà ôëàãà:
2842 Mem.ReadDWORD(dw);
2843 if dw <> FLAG_SIGNATURE then // 'FLAG'
2844 begin
2845 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2846 end;
2847 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2848 Mem.ReadByte(flag^.RespawnType);
2849 // Ñîñòîÿíèå ôëàãà:
2850 Mem.ReadByte(flag^.State);
2851 // Íàïðàâëåíèå ôëàãà:
2852 Mem.ReadByte(b);
2853 if b = 1 then
2854 flag^.Direction := D_LEFT
2855 else // b = 2
2856 flag^.Direction := D_RIGHT;
2857 // Îáúåêò ôëàãà:
2858 Obj_LoadState(@flag^.Obj, Mem);
2859 end;
2861 begin
2862 if Mem = nil then
2863 Exit;
2865 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2866 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2867 LoadPanelArray(gWalls);
2868 // Çàãðóæàåì ïàíåëè ôîíà:
2869 LoadPanelArray(gRenderBackgrounds);
2870 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2871 LoadPanelArray(gRenderForegrounds);
2872 // Çàãðóæàåì ïàíåëè âîäû:
2873 LoadPanelArray(gWater);
2874 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2875 LoadPanelArray(gAcid1);
2876 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2877 LoadPanelArray(gAcid2);
2878 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2879 LoadPanelArray(gSteps);
2880 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2881 LoadPanelArray(gLifts);
2882 ///// /////
2884 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2885 g_GFX_Init();
2886 mapCreateGrid();
2888 ///// Çàãðóæàåì ìóçûêó: /////
2889 // Ñèãíàòóðà ìóçûêè:
2890 Mem.ReadDWORD(dw);
2891 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2892 begin
2893 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2894 end;
2895 // Íàçâàíèå ìóçûêè:
2896 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2897 Mem.ReadString(str);
2898 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2899 Mem.ReadDWORD(dw);
2900 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2901 Mem.ReadBoolean(boo);
2902 // Çàïóñêàåì ýòó ìóçûêó:
2903 gMusic.SetByName(str);
2904 gMusic.SpecPause := boo;
2905 gMusic.Play();
2906 gMusic.Pause(True);
2907 gMusic.SetPosition(dw);
2908 ///// /////
2910 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2911 Mem.ReadInt(gTotalMonsters);
2912 ///// /////
2914 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2915 if gGameSettings.GameMode = GM_CTF then
2916 begin
2917 // Ôëàã Êðàñíîé êîìàíäû:
2918 LoadFlag(@gFlags[FLAG_RED]);
2919 // Ôëàã Ñèíåé êîìàíäû:
2920 LoadFlag(@gFlags[FLAG_BLUE]);
2921 end;
2922 ///// /////
2924 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2925 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2926 begin
2927 // Î÷êè Êðàñíîé êîìàíäû:
2928 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2929 // Î÷êè Ñèíåé êîìàíäû:
2930 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2931 end;
2932 ///// /////
2933 end;
2935 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2936 var
2937 Arr: TPanelArray;
2938 begin
2939 Result := nil;
2940 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2941 Arr := PanelByID[PanelID].PWhere^;
2942 PanelArrayID := PanelByID[PanelID].PArrID;
2943 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2944 end;
2947 // trace liquid, stepping by `dx` and `dy`
2948 // return last seen liquid coords, and `false` if we're started outside of the liquid
2949 function g_Map_TraceLiquid (x, y, dx, dy: Integer; out topx, topy: Integer): Boolean;
2950 const
2951 MaskLiquid = GridTagWater or GridTagAcid1 or GridTagAcid2;
2952 begin
2953 topx := x;
2954 topy := y;
2955 // started outside of the liquid?
2956 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end;
2957 if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check
2958 result := true;
2959 while true do
2960 begin
2961 Inc(x, dx);
2962 Inc(y, dy);
2963 if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit
2964 topx := x;
2965 topy := y;
2966 end;
2967 end;
2970 end.