DEADSOFTWARE

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