DEADSOFTWARE

no more tree for map
[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_ForEachPanelAt (x, y: Integer; cb: TForEachPanelCB; panelType: Word): Boolean;
103 procedure g_Map_ProfilersBegin ();
104 procedure g_Map_ProfilersEnd ();
106 const
107 RESPAWNPOINT_PLAYER1 = 1;
108 RESPAWNPOINT_PLAYER2 = 2;
109 RESPAWNPOINT_DM = 3;
110 RESPAWNPOINT_RED = 4;
111 RESPAWNPOINT_BLUE = 5;
113 FLAG_NONE = 0;
114 FLAG_RED = 1;
115 FLAG_BLUE = 2;
116 FLAG_DOM = 3;
118 FLAG_STATE_NONE = 0;
119 FLAG_STATE_NORMAL = 1;
120 FLAG_STATE_DROPPED = 2;
121 FLAG_STATE_CAPTURED = 3;
122 FLAG_STATE_SCORED = 4; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
123 FLAG_STATE_RETURNED = 5; // Äëÿ ýâåíòîâ ÷åðåç ñåòêó.
125 FLAG_TIME = 720; // 20 seconds
127 SKY_STRETCH: Single = 1.5;
129 const
130 GridTagInvalid = 0;
132 (* draw order:
133 PANEL_BACK
134 PANEL_STEP
135 PANEL_WALL
136 PANEL_CLOSEDOOR
137 PANEL_ACID1
138 PANEL_ACID2
139 PANEL_WATER
140 PANEL_FORE
141 *)
142 // sorted by draw priority
143 GridTagBack = 1 shl 0;
144 GridTagStep = 1 shl 1;
145 GridTagWall = 1 shl 2;
146 GridTagDoor = 1 shl 3;
147 GridTagAcid1 = 1 shl 4;
148 GridTagAcid2 = 1 shl 5;
149 GridTagWater = 1 shl 6;
150 GridTagFore = 1 shl 7;
151 // the following are invisible
152 GridTagLift = 1 shl 8;
153 GridTagBlockMon = 1 shl 9;
156 var
157 gWalls: TPanelArray;
158 gRenderBackgrounds: TPanelArray;
159 gRenderForegrounds: TPanelArray;
160 gWater, gAcid1, gAcid2: TPanelArray;
161 gSteps: TPanelArray;
162 gLifts: TPanelArray;
163 gBlockMon: TPanelArray;
164 gFlags: array [FLAG_RED..FLAG_BLUE] of TFlag;
165 //gDOMFlags: array of TFlag;
166 gMapInfo: TMapInfo;
167 gBackSize: TPoint;
168 gDoorMap: array of array of DWORD;
169 gLiftMap: array of array of DWORD;
170 gWADHash: TMD5Digest;
171 BackID: DWORD = DWORD(-1);
172 gExternalResources: TStringList;
174 gdbg_map_use_accel_render: Boolean = true;
175 gdbg_map_use_accel_coldet: Boolean = true;
176 //gdbg_map_use_tree_draw: Boolean = false;
177 //gdbg_map_use_tree_coldet: Boolean = false;
178 //gdbg_map_dump_coldet_tree_queries: Boolean = false;
179 profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
180 gDrawPanelList: TBinaryHeapObj = nil; // binary heap of all walls we have to render, populated by `g_Map_CollectDrawPanels()`
182 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
185 implementation
187 uses
188 g_main, e_log, SysUtils, g_items, g_gfx, g_console,
189 GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
190 g_options, MAPREADER, g_triggers, g_player, MAPDEF,
191 Math, g_monsters, g_saveload, g_language, g_netmsg,
192 utils, sfs,
193 ImagingTypes, Imaging, ImagingUtility,
194 ImagingGif, ImagingNetworkGraphics;
196 const
197 FLAGRECT: TRectWH = (X:15; Y:12; Width:33; Height:52);
198 MUSIC_SIGNATURE = $4953554D; // 'MUSI'
199 FLAG_SIGNATURE = $47414C46; // 'FLAG'
202 type
203 TPanelGrid = specialize TBodyGridBase<TPanel>;
206 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
208 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
209 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
210 end;
214 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
215 begin
216 result := false;
217 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
218 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
219 if (pan.Width < 1) or (pan.Height < 1) then exit;
220 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
221 result := true;
222 end;
226 function panelTypeToTag (panelType: Word): Integer;
227 begin
228 case panelType of
229 PANEL_WALL: result := GridTagWall; // gWalls
230 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
231 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
232 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
233 PANEL_WATER: result := GridTagWater; // gWater
234 PANEL_ACID1: result := GridTagAcid1; // gAcid1
235 PANEL_ACID2: result := GridTagAcid2; // gAcid2
236 PANEL_STEP: result := GridTagStep; // gSteps
237 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
238 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
239 else result := GridTagInvalid;
240 end;
241 end;
244 function dplLess (a, b: TObject): Boolean;
245 var
246 pa, pb: TPanel;
247 begin
248 //result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
249 pa := TPanel(a);
250 pb := TPanel(b);
251 if (pa.tag < pb.tag) then begin result := true; exit; end;
252 if (pa.tag > pb.tag) then begin result := false; exit; end;
253 result := (pa.ArrIdx < pb.ArrIdx);
254 end;
256 procedure dplClear ();
257 begin
258 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
259 end;
262 type
263 TPanelID = record
264 PWhere: ^TPanelArray;
265 PArrID: Integer;
266 end;
268 var
269 PanelById: array of TPanelID;
270 Textures: TLevelTextureArray;
271 RespawnPoints: Array of TRespawnPoint;
272 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
273 //DOMFlagPoints: Array of TFlagPoint;
274 gMapGrid: TPanelGrid = nil;
275 //mapTree: TDynAABBTreeMap = nil;
278 procedure g_Map_ProfilersBegin ();
279 begin
280 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
281 profMapCollision.mainBegin(g_profile_collision);
282 // create sections
283 if g_profile_collision then
284 begin
285 profMapCollision.sectionBegin('*solids');
286 profMapCollision.sectionEnd();
287 profMapCollision.sectionBegin('liquids');
288 profMapCollision.sectionEnd();
289 end;
290 end;
292 procedure g_Map_ProfilersEnd ();
293 begin
294 if (profMapCollision <> nil) then profMapCollision.mainEnd();
295 end;
298 // wall index in `gWalls` or -1
299 (*
300 function g_Map_traceToNearestWallOld (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
302 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
303 var
304 aabb: AABB2D;
305 tmin: Single;
306 begin
307 result := -666.0; // invalid
308 if not pan.Enabled then exit;
309 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
310 if not aabb.valid then exit;
311 if aabb.intersects(ray, @tmin) then
312 begin
313 //if (tmin*tmin > maxDistSq) then exit;
314 if (tmin >= 0.0) then
315 begin
316 //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);
317 //if (tmin < 0.0) then tmin := 0.0;
318 result := tmin;
319 end;
320 end;
321 end;
323 var
324 qr: TDynAABBTreeMap.TSegmentQueryResult;
325 ray: Ray2D;
326 hxf, hyf: Single;
327 hx, hy: Integer;
328 maxDistSq: Single;
329 begin
330 result := -1;
331 if (mapTree = nil) then exit;
332 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
333 begin
334 maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
335 if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
336 begin
337 result := qr.flesh.arrIdx;
338 if (hitx <> nil) or (hity <> nil) then
339 begin
340 ray := Ray2D.Create(x0, y0, x1, y1);
341 hxf := ray.origX+ray.dirX*qr.time;
342 hyf := ray.origY+ray.dirY*qr.time;
343 while true do
344 begin
345 hx := trunc(hxf);
346 hy := trunc(hyf);
347 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
348 begin
349 // go back a little
350 hxf -= ray.dirX;
351 hyf -= ray.dirY;
352 end
353 else
354 begin
355 break;
356 end;
357 end;
358 if (hitx <> nil) then hitx^ := hx;
359 if (hity <> nil) then hity^ := hy;
360 end;
361 end;
362 end;
363 end;
364 *)
367 // wall index in `gWalls` or -1
368 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
369 var
370 lastX, lastY, lastDist: Integer;
371 wasHit: Boolean = false;
373 // pan=nil: before processing new tile
374 function sqchecker (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
375 var
376 dist: Integer;
377 begin
378 if (pan = nil) then
379 begin
380 // stop if something was hit at the previous tile
381 result := wasHit;
382 end
383 else
384 begin
385 result := false;
386 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
387 begin
388 if not pan.Enabled then exit;
389 end;
390 dist := (prevx-x0)*(prevx-x0)+(prevy-y0)*(prevy-y0);
391 if (dist < lastDist) then
392 begin
393 wasHit := true;
394 lastDist := dist;
395 lastX := prevx;
396 lastY := prevy;
397 end;
398 end;
399 end;
401 begin
402 result := false;
403 if (gMapGrid = nil) then exit;
404 lastDist := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+1;
405 lastX := 0;
406 lastY := 0;
407 result := gMapGrid.traceRay(x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor));
408 if (hitx <> nil) then hitx^ := lastX;
409 if (hity <> nil) then hity^ := lastY;
410 end;
413 function g_Map_ForEachPanelAt (x, y: Integer; cb: TForEachPanelCB; panelType: Word): Boolean;
415 function checker (pan: TPanel; tag: Integer): Boolean;
416 begin
417 result := false; // don't stop, ever
419 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
420 begin
421 if not pan.Enabled then exit;
422 end;
424 result := (x >= pan.X) and (y >= pan.Y) and (x < pan.X+pan.Width) and (y < pan.Y+pan.Height);
425 if not result then exit;
427 if ((tag and GridTagLift) <> 0) then
428 begin
429 result :=
430 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
431 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
432 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
433 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
434 end;
436 // other shit
437 if result then result := cb(pan);
438 end;
440 var
441 tagmask: Integer = 0;
442 begin
443 result := false;
444 if not assigned(cb) then exit;
446 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
447 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
448 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
449 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
450 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
451 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
452 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
454 if (tagmask = 0) then exit;// just in case
455 result := gMapGrid.forEachInAABB(x, y, 1, 1, checker, tagmask);
456 end;
459 function g_Map_IsSpecialTexture(Texture: String): Boolean;
460 begin
461 Result := (Texture = TEXTURE_NAME_WATER) or
462 (Texture = TEXTURE_NAME_ACID1) or
463 (Texture = TEXTURE_NAME_ACID2);
464 end;
466 procedure CreateDoorMap();
467 var
468 PanelArray: Array of record
469 X, Y: Integer;
470 Width, Height: Word;
471 Active: Boolean;
472 PanelID: DWORD;
473 end;
474 a, b, c, m, i, len: Integer;
475 ok: Boolean;
476 begin
477 if gWalls = nil then
478 Exit;
480 i := 0;
481 len := 128;
482 SetLength(PanelArray, len);
484 for a := 0 to High(gWalls) do
485 if gWalls[a].Door then
486 begin
487 PanelArray[i].X := gWalls[a].X;
488 PanelArray[i].Y := gWalls[a].Y;
489 PanelArray[i].Width := gWalls[a].Width;
490 PanelArray[i].Height := gWalls[a].Height;
491 PanelArray[i].Active := True;
492 PanelArray[i].PanelID := a;
494 i := i + 1;
495 if i = len then
496 begin
497 len := len + 128;
498 SetLength(PanelArray, len);
499 end;
500 end;
502 // Íåò äâåðåé:
503 if i = 0 then
504 begin
505 PanelArray := nil;
506 Exit;
507 end;
509 SetLength(gDoorMap, 0);
511 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
513 for a := 0 to i-1 do
514 if PanelArray[a].Active then
515 begin
516 PanelArray[a].Active := False;
517 m := Length(gDoorMap);
518 SetLength(gDoorMap, m+1);
519 SetLength(gDoorMap[m], 1);
520 gDoorMap[m, 0] := PanelArray[a].PanelID;
521 ok := True;
523 while ok do
524 begin
525 ok := False;
527 for b := 0 to i-1 do
528 if PanelArray[b].Active then
529 for c := 0 to High(gDoorMap[m]) do
530 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
531 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
532 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
533 PanelArray[b].Width, PanelArray[b].Height,
534 gWalls[gDoorMap[m, c]].X,
535 gWalls[gDoorMap[m, c]].Y,
536 gWalls[gDoorMap[m, c]].Width,
537 gWalls[gDoorMap[m, c]].Height) then
538 begin
539 PanelArray[b].Active := False;
540 SetLength(gDoorMap[m],
541 Length(gDoorMap[m])+1);
542 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
543 ok := True;
544 Break;
545 end;
546 end;
548 g_Game_StepLoading();
549 end;
551 PanelArray := nil;
552 end;
554 procedure CreateLiftMap();
555 var
556 PanelArray: Array of record
557 X, Y: Integer;
558 Width, Height: Word;
559 Active: Boolean;
560 end;
561 a, b, c, len, i, j: Integer;
562 ok: Boolean;
563 begin
564 if gLifts = nil then
565 Exit;
567 len := Length(gLifts);
568 SetLength(PanelArray, len);
570 for a := 0 to len-1 do
571 begin
572 PanelArray[a].X := gLifts[a].X;
573 PanelArray[a].Y := gLifts[a].Y;
574 PanelArray[a].Width := gLifts[a].Width;
575 PanelArray[a].Height := gLifts[a].Height;
576 PanelArray[a].Active := True;
577 end;
579 SetLength(gLiftMap, len);
580 i := 0;
582 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
584 for a := 0 to len-1 do
585 if PanelArray[a].Active then
586 begin
587 PanelArray[a].Active := False;
588 SetLength(gLiftMap[i], 32);
589 j := 0;
590 gLiftMap[i, j] := a;
591 ok := True;
593 while ok do
594 begin
595 ok := False;
596 for b := 0 to len-1 do
597 if PanelArray[b].Active then
598 for c := 0 to j do
599 if g_CollideAround(PanelArray[b].X,
600 PanelArray[b].Y,
601 PanelArray[b].Width,
602 PanelArray[b].Height,
603 PanelArray[gLiftMap[i, c]].X,
604 PanelArray[gLiftMap[i, c]].Y,
605 PanelArray[gLiftMap[i, c]].Width,
606 PanelArray[gLiftMap[i, c]].Height) then
607 begin
608 PanelArray[b].Active := False;
609 j := j+1;
610 if j > High(gLiftMap[i]) then
611 SetLength(gLiftMap[i],
612 Length(gLiftMap[i])+32);
614 gLiftMap[i, j] := b;
615 ok := True;
617 Break;
618 end;
619 end;
621 SetLength(gLiftMap[i], j+1);
622 i := i+1;
624 g_Game_StepLoading();
625 end;
627 SetLength(gLiftMap, i);
629 PanelArray := nil;
630 end;
632 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
633 CurTex: Integer; sav: Boolean): Integer;
634 var
635 len: Integer;
636 panels: ^TPanelArray;
637 begin
638 Result := -1;
640 case PanelRec.PanelType of
641 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
642 panels := @gWalls;
643 PANEL_BACK:
644 panels := @gRenderBackgrounds;
645 PANEL_FORE:
646 panels := @gRenderForegrounds;
647 PANEL_WATER:
648 panels := @gWater;
649 PANEL_ACID1:
650 panels := @gAcid1;
651 PANEL_ACID2:
652 panels := @gAcid2;
653 PANEL_STEP:
654 panels := @gSteps;
655 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
656 panels := @gLifts;
657 PANEL_BLOCKMON:
658 panels := @gBlockMon;
659 else
660 Exit;
661 end;
663 len := Length(panels^);
664 SetLength(panels^, len + 1);
666 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
667 panels^[len].ArrIdx := len;
668 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
669 if sav then
670 panels^[len].SaveIt := True;
672 Result := len;
674 len := Length(PanelByID);
675 SetLength(PanelByID, len + 1);
676 PanelByID[len].PWhere := panels;
677 PanelByID[len].PArrID := Result;
678 end;
680 function CreateNullTexture(RecName: String): Integer;
681 begin
682 SetLength(Textures, Length(Textures)+1);
683 result := High(Textures);
685 with Textures[High(Textures)] do
686 begin
687 TextureName := RecName;
688 Width := 1;
689 Height := 1;
690 Anim := False;
691 TextureID := TEXTURE_NONE;
692 end;
693 end;
695 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
696 var
697 WAD: TWADFile;
698 TextureData: Pointer;
699 WADName, txname: String;
700 a, ResLength: Integer;
701 begin
702 Result := -1;
704 if Textures <> nil then
705 for a := 0 to High(Textures) do
706 if Textures[a].TextureName = RecName then
707 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
708 Result := a;
709 Exit;
710 end;
712 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
713 if (RecName = TEXTURE_NAME_WATER) or
714 (RecName = TEXTURE_NAME_ACID1) or
715 (RecName = TEXTURE_NAME_ACID2) then
716 begin
717 SetLength(Textures, Length(Textures)+1);
719 with Textures[High(Textures)] do
720 begin
721 TextureName := RecName;
723 if TextureName = TEXTURE_NAME_WATER then
724 TextureID := TEXTURE_SPECIAL_WATER
725 else
726 if TextureName = TEXTURE_NAME_ACID1 then
727 TextureID := TEXTURE_SPECIAL_ACID1
728 else
729 if TextureName = TEXTURE_NAME_ACID2 then
730 TextureID := TEXTURE_SPECIAL_ACID2;
732 Anim := False;
733 end;
735 result := High(Textures);
736 Exit;
737 end;
739 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
740 WADName := g_ExtractWadName(RecName);
742 WAD := TWADFile.Create();
744 if WADName <> '' then
745 WADName := GameDir+'/wads/'+WADName
746 else
747 WADName := Map;
749 WAD.ReadFile(WADName);
751 txname := RecName;
753 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
754 begin
755 FreeMem(TextureData);
756 RecName := 'COMMON\ALIEN';
757 end;
760 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
761 begin
762 SetLength(Textures, Length(Textures)+1);
763 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
764 Exit;
765 e_GetTextureSize(Textures[High(Textures)].TextureID,
766 @Textures[High(Textures)].Width,
767 @Textures[High(Textures)].Height);
768 FreeMem(TextureData);
769 Textures[High(Textures)].TextureName := {RecName}txname;
770 Textures[High(Textures)].Anim := False;
772 result := High(Textures);
773 end
774 else // Íåò òàêîãî ðåóñðñà â WAD'å
775 begin
776 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
777 if log then
778 begin
779 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
780 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
781 end;
782 end;
784 WAD.Free();
785 end;
787 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
788 var
789 WAD: TWADFile;
790 TextureWAD: PChar = nil;
791 TextData: Pointer = nil;
792 TextureData: Pointer = nil;
793 cfg: TConfig = nil;
794 WADName: String;
795 ResLength: Integer;
796 TextureResource: String;
797 _width, _height, _framecount, _speed: Integer;
798 _backanimation: Boolean;
799 //imgfmt: string;
800 ia: TDynImageDataArray = nil;
801 f, c, frdelay, frloop: Integer;
802 begin
803 result := -1;
805 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
807 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
808 WADName := g_ExtractWadName(RecName);
810 WAD := TWADFile.Create();
811 try
812 if WADName <> '' then
813 WADName := GameDir+'/wads/'+WADName
814 else
815 WADName := Map;
817 WAD.ReadFile(WADName);
819 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
820 begin
821 if log then
822 begin
823 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
824 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
825 end;
826 exit;
827 end;
829 {TEST
830 if WADName = Map then
831 begin
832 //FreeMem(TextureWAD);
833 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
834 end;
837 WAD.FreeWAD();
839 if ResLength < 6 then
840 begin
841 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
842 exit;
843 end;
845 // ýòî ïòèöà? ýòî ñàìîë¸ò?
846 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
847 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
848 begin
849 // íåò, ýòî ñóïåðìåí!
850 if not WAD.ReadMemory(TextureWAD, ResLength) then
851 begin
852 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
853 exit;
854 end;
856 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
857 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
858 begin
859 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
860 exit;
861 end;
863 cfg := TConfig.CreateMem(TextData, ResLength);
865 TextureResource := cfg.ReadStr('', 'resource', '');
866 if TextureResource = '' then
867 begin
868 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
869 exit;
870 end;
872 _width := cfg.ReadInt('', 'framewidth', 0);
873 _height := cfg.ReadInt('', 'frameheight', 0);
874 _framecount := cfg.ReadInt('', 'framecount', 0);
875 _speed := cfg.ReadInt('', 'waitcount', 0);
876 _backanimation := cfg.ReadBool('', 'backanimation', False);
878 cfg.Free();
879 cfg := nil;
881 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
882 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
883 begin
884 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
885 exit;
886 end;
888 WAD.Free();
889 WAD := nil;
891 SetLength(Textures, Length(Textures)+1);
892 with Textures[High(Textures)] do
893 begin
894 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
895 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
896 begin
897 TextureName := RecName;
898 Width := _width;
899 Height := _height;
900 Anim := True;
901 FramesCount := _framecount;
902 Speed := _speed;
903 result := High(Textures);
904 end
905 else
906 begin
907 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
908 end;
909 end;
910 end
911 else
912 begin
913 // try animated image
915 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
916 if length(imgfmt) = 0 then
917 begin
918 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
919 exit;
920 end;
922 GlobalMetadata.ClearMetaItems();
923 GlobalMetadata.ClearMetaItemsForSaving();
924 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
925 begin
926 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
927 exit;
928 end;
929 if length(ia) = 0 then
930 begin
931 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
932 exit;
933 end;
935 WAD.Free();
936 WAD := nil;
938 _width := ia[0].width;
939 _height := ia[0].height;
940 _framecount := length(ia);
941 _speed := 1;
942 _backanimation := false;
943 frdelay := -1;
944 frloop := -666;
945 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
946 begin
947 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
948 try
949 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
950 frdelay := f;
951 if f < 0 then f := 0;
952 // rounding ;-)
953 c := f mod 28;
954 if c < 13 then c := 0 else c := 1;
955 f := (f div 28)+c;
956 if f < 1 then f := 1 else if f > 255 then f := 255;
957 _speed := f;
958 except
959 end;
960 end;
961 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
962 begin
963 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
964 try
965 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
966 frloop := f;
967 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
968 except
969 end;
970 end;
971 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
972 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
973 f := ord(_backanimation);
974 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);
976 SetLength(Textures, Length(Textures)+1);
977 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
978 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
979 begin
980 Textures[High(Textures)].TextureName := RecName;
981 Textures[High(Textures)].Width := _width;
982 Textures[High(Textures)].Height := _height;
983 Textures[High(Textures)].Anim := True;
984 Textures[High(Textures)].FramesCount := length(ia);
985 Textures[High(Textures)].Speed := _speed;
986 result := High(Textures);
987 //writeln(' CREATED!');
988 end
989 else
990 begin
991 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
992 end;
993 end;
994 finally
995 for f := 0 to High(ia) do FreeImage(ia[f]);
996 WAD.Free();
997 cfg.Free();
998 if TextureWAD <> nil then FreeMem(TextureWAD);
999 if TextData <> nil then FreeMem(TextData);
1000 if TextureData <> nil then FreeMem(TextureData);
1001 end;
1002 end;
1004 procedure CreateItem(Item: TItemRec_1);
1005 begin
1006 if g_Game_IsClient then Exit;
1008 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1009 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1010 Exit;
1012 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1013 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1014 end;
1016 procedure CreateArea(Area: TAreaRec_1);
1017 var
1018 a: Integer;
1019 id: DWORD;
1020 begin
1021 case Area.AreaType of
1022 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1023 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1024 begin
1025 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1026 with RespawnPoints[High(RespawnPoints)] do
1027 begin
1028 X := Area.X;
1029 Y := Area.Y;
1030 Direction := TDirection(Area.Direction);
1032 case Area.AreaType of
1033 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1034 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1035 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1036 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1037 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1038 end;
1039 end;
1040 end;
1042 AREA_REDFLAG, AREA_BLUEFLAG:
1043 begin
1044 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1046 if FlagPoints[a] <> nil then Exit;
1048 New(FlagPoints[a]);
1050 with FlagPoints[a]^ do
1051 begin
1052 X := Area.X-FLAGRECT.X;
1053 Y := Area.Y-FLAGRECT.Y;
1054 Direction := TDirection(Area.Direction);
1055 end;
1057 with gFlags[a] do
1058 begin
1059 case a of
1060 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1061 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1062 end;
1064 Animation := TAnimation.Create(id, True, 8);
1065 Obj.Rect := FLAGRECT;
1067 g_Map_ResetFlag(a);
1068 end;
1069 end;
1071 AREA_DOMFLAG:
1072 begin
1073 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1074 with DOMFlagPoints[High(DOMFlagPoints)] do
1075 begin
1076 X := Area.X;
1077 Y := Area.Y;
1078 Direction := TDirection(Area.Direction);
1079 end;
1081 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1082 end;
1083 end;
1084 end;
1086 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
1087 var
1088 _trigger: TTrigger;
1089 begin
1090 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1092 with _trigger do
1093 begin
1094 X := Trigger.X;
1095 Y := Trigger.Y;
1096 Width := Trigger.Width;
1097 Height := Trigger.Height;
1098 Enabled := ByteBool(Trigger.Enabled);
1099 TexturePanel := Trigger.TexturePanel;
1100 TexturePanelType := fTexturePanel1Type;
1101 ShotPanelType := fTexturePanel2Type;
1102 TriggerType := Trigger.TriggerType;
1103 ActivateType := Trigger.ActivateType;
1104 Keys := Trigger.Keys;
1105 Data.Default := Trigger.DATA;
1106 end;
1108 g_Triggers_Create(_trigger);
1109 end;
1111 procedure CreateMonster(monster: TMonsterRec_1);
1112 var
1113 a: Integer;
1114 mon: TMonster;
1115 begin
1116 if g_Game_IsClient then Exit;
1118 if (gGameSettings.GameType = GT_SINGLE)
1119 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1120 begin
1121 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1123 if gTriggers <> nil then
1124 begin
1125 for a := 0 to High(gTriggers) do
1126 begin
1127 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1128 begin
1129 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1130 end;
1131 end;
1132 end;
1134 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1135 end;
1136 end;
1138 procedure g_Map_ReAdd_DieTriggers();
1140 function monsDieTrig (mon: TMonster): Boolean;
1141 var
1142 a: Integer;
1143 begin
1144 result := false; // don't stop
1145 mon.ClearTriggers();
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 begin
1156 if g_Game_IsClient then Exit;
1158 g_Mons_ForEach(monsDieTrig);
1159 end;
1161 function extractWadName(resourceName: string): string;
1162 var
1163 posN: Integer;
1164 begin
1165 posN := Pos(':', resourceName);
1166 if posN > 0 then
1167 Result:= Copy(resourceName, 0, posN-1)
1168 else
1169 Result := '';
1170 end;
1172 procedure addResToExternalResList(res: string);
1173 begin
1174 res := extractWadName(res);
1175 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1176 gExternalResources.Add(res);
1177 end;
1179 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1180 var
1181 textures: TTexturesRec1Array;
1182 mapHeader: TMapHeaderRec_1;
1183 i: integer;
1184 resFile: String = '';
1185 begin
1186 if gExternalResources = nil then
1187 gExternalResources := TStringList.Create;
1189 gExternalResources.Clear;
1190 textures := mapReader.GetTextures();
1191 for i := 0 to High(textures) do
1192 begin
1193 addResToExternalResList(resFile);
1194 end;
1196 textures := nil;
1198 mapHeader := mapReader.GetMapHeader;
1200 addResToExternalResList(mapHeader.MusicName);
1201 addResToExternalResList(mapHeader.SkyName);
1202 end;
1204 procedure mapCreateGrid ();
1205 var
1206 mapX0: Integer = $3fffffff;
1207 mapY0: Integer = $3fffffff;
1208 mapX1: Integer = -$3fffffff;
1209 mapY1: Integer = -$3fffffff;
1211 procedure calcBoundingBox (constref panels: TPanelArray);
1212 var
1213 idx: Integer;
1214 pan: TPanel;
1215 begin
1216 for idx := 0 to High(panels) do
1217 begin
1218 pan := panels[idx];
1219 if not pan.visvalid then continue;
1220 if (pan.Width < 1) or (pan.Height < 1) then continue;
1221 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1222 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1223 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1224 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1225 end;
1226 end;
1228 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1229 var
1230 idx: Integer;
1231 pan: TPanel;
1232 begin
1233 tag := panelTypeToTag(tag);
1234 for idx := High(panels) downto 0 do
1235 begin
1236 pan := panels[idx];
1237 pan.tag := tag;
1238 if not pan.visvalid then continue;
1239 gMapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1240 //mapTree.insertObject(pan, tag, true); // as static object
1241 end;
1242 end;
1244 begin
1245 gMapGrid.Free();
1246 gMapGrid := nil;
1247 //mapTree.Free();
1248 //mapTree := nil;
1250 calcBoundingBox(gWalls);
1251 calcBoundingBox(gRenderBackgrounds);
1252 calcBoundingBox(gRenderForegrounds);
1253 calcBoundingBox(gWater);
1254 calcBoundingBox(gAcid1);
1255 calcBoundingBox(gAcid2);
1256 calcBoundingBox(gSteps);
1257 calcBoundingBox(gLifts);
1258 calcBoundingBox(gBlockMon);
1260 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1262 gMapGrid := TPanelGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1263 //mapTree := TDynAABBTreeMap.Create();
1265 addPanelsToGrid(gWalls, PANEL_WALL);
1266 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1267 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1268 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1269 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1270 addPanelsToGrid(gWater, PANEL_WATER);
1271 addPanelsToGrid(gAcid1, PANEL_ACID1);
1272 addPanelsToGrid(gAcid2, PANEL_ACID2);
1273 addPanelsToGrid(gSteps, PANEL_STEP);
1274 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1275 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1277 gMapGrid.dumpStats();
1278 //e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1279 //mapTree.forEachLeaf(nil);
1280 end;
1282 function g_Map_Load(Res: String): Boolean;
1283 const
1284 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1285 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1286 var
1287 WAD: TWADFile;
1288 MapReader: TMapReader_1;
1289 Header: TMapHeaderRec_1;
1290 _textures: TTexturesRec1Array;
1291 _texnummap: array of Integer; // `_textures` -> `Textures`
1292 panels: TPanelsRec1Array;
1293 items: TItemsRec1Array;
1294 monsters: TMonsterRec1Array;
1295 areas: TAreasRec1Array;
1296 triggers: TTriggersRec1Array;
1297 a, b, c, k: Integer;
1298 PanelID: DWORD;
1299 AddTextures: TAddTextureArray;
1300 texture: TTextureRec_1;
1301 TriggersTable: Array of record
1302 TexturePanel: Integer;
1303 LiftPanel: Integer;
1304 DoorPanel: Integer;
1305 ShotPanel: Integer;
1306 end;
1307 FileName, mapResName, s, TexName: String;
1308 Data: Pointer;
1309 Len: Integer;
1310 ok, isAnim, trigRef: Boolean;
1311 CurTex, ntn: Integer;
1313 begin
1314 gMapGrid.Free();
1315 gMapGrid := nil;
1316 //mapTree.Free();
1317 //mapTree := nil;
1319 Result := False;
1320 gMapInfo.Map := Res;
1321 TriggersTable := nil;
1322 FillChar(texture, SizeOf(texture), 0);
1324 sfsGCDisable(); // temporary disable removing of temporary volumes
1325 try
1326 // Çàãðóçêà WAD:
1327 FileName := g_ExtractWadName(Res);
1328 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1329 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1331 WAD := TWADFile.Create();
1332 if not WAD.ReadFile(FileName) then
1333 begin
1334 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1335 WAD.Free();
1336 Exit;
1337 end;
1338 //k8: why loader ignores path here?
1339 mapResName := g_ExtractFileName(Res);
1340 if not WAD.GetMapResource(mapResName, Data, Len) then
1341 begin
1342 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1343 WAD.Free();
1344 Exit;
1345 end;
1347 WAD.Free();
1349 // Çàãðóçêà êàðòû:
1350 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1351 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1352 MapReader := TMapReader_1.Create();
1354 if not MapReader.LoadMap(Data) then
1355 begin
1356 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1357 FreeMem(Data);
1358 MapReader.Free();
1359 Exit;
1360 end;
1362 FreeMem(Data);
1363 generateExternalResourcesList(MapReader);
1364 // Çàãðóçêà òåêñòóð:
1365 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1366 _textures := MapReader.GetTextures();
1367 _texnummap := nil;
1369 // Äîáàâëåíèå òåêñòóð â Textures[]:
1370 if _textures <> nil then
1371 begin
1372 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1373 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1374 SetLength(_texnummap, length(_textures));
1376 for a := 0 to High(_textures) do
1377 begin
1378 SetLength(s, 64);
1379 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1380 for b := 1 to Length(s) do
1381 if s[b] = #0 then
1382 begin
1383 SetLength(s, b-1);
1384 Break;
1385 end;
1386 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1387 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1388 // Àíèìèðîâàííàÿ òåêñòóðà:
1389 if ByteBool(_textures[a].Anim) then
1390 begin
1391 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1392 if ntn < 0 then
1393 begin
1394 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1395 ntn := CreateNullTexture(_textures[a].Resource);
1396 end;
1397 end
1398 else // Îáû÷íàÿ òåêñòóðà:
1399 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1400 if ntn < 0 then
1401 begin
1402 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1403 ntn := CreateNullTexture(_textures[a].Resource);
1404 end;
1406 _texnummap[a] := ntn; // fix texture number
1407 g_Game_StepLoading();
1408 end;
1409 end;
1411 // Çàãðóçêà òðèããåðîâ:
1412 gTriggerClientID := 0;
1413 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1414 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1415 triggers := MapReader.GetTriggers();
1417 // Çàãðóçêà ïàíåëåé:
1418 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1419 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1420 panels := MapReader.GetPanels();
1422 // check texture numbers for panels
1423 for a := 0 to High(panels) do
1424 begin
1425 if panels[a].TextureNum > High(_textures) then
1426 begin
1427 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1428 result := false;
1429 exit;
1430 end;
1431 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1432 end;
1434 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1435 if triggers <> nil then
1436 begin
1437 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1438 SetLength(TriggersTable, Length(triggers));
1439 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1441 for a := 0 to High(TriggersTable) do
1442 begin
1443 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1444 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1445 // Ëèôòû:
1446 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1447 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1448 else
1449 TriggersTable[a].LiftPanel := -1;
1450 // Äâåðè:
1451 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1452 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1453 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1454 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1455 else
1456 TriggersTable[a].DoorPanel := -1;
1457 // Òóðåëü:
1458 if triggers[a].TriggerType = TRIGGER_SHOT then
1459 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1460 else
1461 TriggersTable[a].ShotPanel := -1;
1463 g_Game_StepLoading();
1464 end;
1465 end;
1467 // Ñîçäàåì ïàíåëè:
1468 if panels <> nil then
1469 begin
1470 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1471 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1473 for a := 0 to High(panels) do
1474 begin
1475 SetLength(AddTextures, 0);
1476 trigRef := False;
1477 CurTex := -1;
1478 if _textures <> nil then
1479 begin
1480 texture := _textures[panels[a].TextureNum];
1481 ok := True;
1482 end
1483 else
1484 ok := False;
1486 if ok then
1487 begin
1488 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1489 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1490 ok := False;
1491 if (TriggersTable <> nil) and (_textures <> nil) then
1492 for b := 0 to High(TriggersTable) do
1493 if (TriggersTable[b].TexturePanel = a)
1494 or (TriggersTable[b].ShotPanel = a) then
1495 begin
1496 trigRef := True;
1497 ok := True;
1498 Break;
1499 end;
1500 end;
1502 if ok then
1503 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1504 SetLength(s, 64);
1505 CopyMemory(@s[1], @texture.Resource[0], 64);
1506 // Èçìåðÿåì äëèíó:
1507 Len := Length(s);
1508 for c := Len downto 1 do
1509 if s[c] <> #0 then
1510 begin
1511 Len := c;
1512 Break;
1513 end;
1514 SetLength(s, Len);
1516 // Ñïåö-òåêñòóðû çàïðåùåíû:
1517 if g_Map_IsSpecialTexture(s) then
1518 ok := False
1519 else
1520 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1521 ok := g_Texture_NumNameFindStart(s);
1523 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1524 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1525 if ok then
1526 begin
1527 k := NNF_NAME_BEFORE;
1528 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1529 while ok or (k = NNF_NAME_BEFORE) or
1530 (k = NNF_NAME_EQUALS) do
1531 begin
1532 k := g_Texture_NumNameFindNext(TexName);
1534 if (k = NNF_NAME_BEFORE) or
1535 (k = NNF_NAME_AFTER) then
1536 begin
1537 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1538 if ByteBool(texture.Anim) then
1539 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1540 isAnim := True;
1541 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1542 if not ok then
1543 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1544 isAnim := False;
1545 ok := CreateTexture(TexName, FileName, False) >= 0;
1546 end;
1547 end
1548 else
1549 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1550 isAnim := False;
1551 ok := CreateTexture(TexName, FileName, False) >= 0;
1552 if not ok then
1553 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1554 isAnim := True;
1555 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1556 end;
1557 end;
1559 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1560 if ok then
1561 begin
1562 for c := 0 to High(Textures) do
1563 if Textures[c].TextureName = TexName then
1564 begin
1565 SetLength(AddTextures, Length(AddTextures)+1);
1566 AddTextures[High(AddTextures)].Texture := c;
1567 AddTextures[High(AddTextures)].Anim := isAnim;
1568 Break;
1569 end;
1570 end;
1571 end
1572 else
1573 if k = NNF_NAME_EQUALS then
1574 begin
1575 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1576 SetLength(AddTextures, Length(AddTextures)+1);
1577 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1578 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1579 CurTex := High(AddTextures);
1580 ok := True;
1581 end
1582 else // NNF_NO_NAME
1583 ok := False;
1584 end; // while ok...
1586 ok := True;
1587 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1588 end; // if ok - ññûëàþòñÿ òðèããåðû
1590 if not ok then
1591 begin
1592 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1593 SetLength(AddTextures, 1);
1594 AddTextures[0].Texture := panels[a].TextureNum;
1595 AddTextures[0].Anim := ByteBool(texture.Anim);
1596 CurTex := 0;
1597 end;
1599 //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);
1601 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1602 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1604 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1605 if TriggersTable <> nil then
1606 for b := 0 to High(TriggersTable) do
1607 begin
1608 // Òðèããåð äâåðè/ëèôòà:
1609 if (TriggersTable[b].LiftPanel = a) or
1610 (TriggersTable[b].DoorPanel = a) then
1611 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1612 // Òðèããåð ñìåíû òåêñòóðû:
1613 if TriggersTable[b].TexturePanel = a then
1614 triggers[b].TexturePanel := PanelID;
1615 // Òðèããåð "Òóðåëü":
1616 if TriggersTable[b].ShotPanel = a then
1617 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1618 end;
1620 g_Game_StepLoading();
1621 end;
1622 end;
1624 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1625 if (triggers <> nil) and not gLoadGameMode then
1626 begin
1627 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1628 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1629 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1630 for a := 0 to High(triggers) do
1631 begin
1632 if triggers[a].TexturePanel <> -1 then
1633 b := panels[TriggersTable[a].TexturePanel].PanelType
1634 else
1635 b := 0;
1636 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1637 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1638 c := panels[TriggersTable[a].ShotPanel].PanelType
1639 else
1640 c := 0;
1641 CreateTrigger(triggers[a], b, c);
1642 end;
1643 end;
1645 // Çàãðóçêà ïðåäìåòîâ:
1646 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1647 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1648 items := MapReader.GetItems();
1650 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1651 if (items <> nil) and not gLoadGameMode then
1652 begin
1653 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1654 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1655 for a := 0 to High(items) do
1656 CreateItem(Items[a]);
1657 end;
1659 // Çàãðóçêà îáëàñòåé:
1660 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1661 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1662 areas := MapReader.GetAreas();
1664 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1665 if areas <> nil then
1666 begin
1667 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1668 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1669 for a := 0 to High(areas) do
1670 CreateArea(areas[a]);
1671 end;
1673 // Çàãðóçêà ìîíñòðîâ:
1674 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1675 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1676 monsters := MapReader.GetMonsters();
1678 gTotalMonsters := 0;
1680 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1681 if (monsters <> nil) and not gLoadGameMode then
1682 begin
1683 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1684 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1685 for a := 0 to High(monsters) do
1686 CreateMonster(monsters[a]);
1687 end;
1689 // Çàãðóçêà îïèñàíèÿ êàðòû:
1690 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1691 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1692 Header := MapReader.GetMapHeader();
1694 MapReader.Free();
1696 with gMapInfo do
1697 begin
1698 Name := Header.MapName;
1699 Description := Header.MapDescription;
1700 Author := Header.MapAuthor;
1701 MusicName := Header.MusicName;
1702 SkyName := Header.SkyName;
1703 Height := Header.Height;
1704 Width := Header.Width;
1705 end;
1707 // Çàãðóçêà íåáà:
1708 if gMapInfo.SkyName <> '' then
1709 begin
1710 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1711 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1712 FileName := g_ExtractWadName(gMapInfo.SkyName);
1714 if FileName <> '' then
1715 FileName := GameDir+'/wads/'+FileName
1716 else
1717 begin
1718 FileName := g_ExtractWadName(Res);
1719 end;
1721 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1722 if g_Texture_CreateWAD(BackID, s) then
1723 begin
1724 g_Game_SetupScreenSize();
1725 end
1726 else
1727 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1728 end;
1730 // Çàãðóçêà ìóçûêè:
1731 ok := False;
1732 if gMapInfo.MusicName <> '' then
1733 begin
1734 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1735 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1736 FileName := g_ExtractWadName(gMapInfo.MusicName);
1738 if FileName <> '' then
1739 FileName := GameDir+'/wads/'+FileName
1740 else
1741 begin
1742 FileName := g_ExtractWadName(Res);
1743 end;
1745 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1746 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1747 ok := True
1748 else
1749 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1750 end;
1752 // Îñòàëüíûå óñòàíâêè:
1753 CreateDoorMap();
1754 CreateLiftMap();
1756 g_Items_Init();
1757 g_Weapon_Init();
1758 g_Monsters_Init();
1760 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1761 if not gLoadGameMode then
1762 g_GFX_Init();
1764 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1765 _textures := nil;
1766 panels := nil;
1767 items := nil;
1768 areas := nil;
1769 triggers := nil;
1770 TriggersTable := nil;
1771 AddTextures := nil;
1773 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1774 if ok and (not gLoadGameMode) then
1775 begin
1776 gMusic.SetByName(gMapInfo.MusicName);
1777 gMusic.Play();
1778 end
1779 else
1780 gMusic.SetByName('');
1781 finally
1782 sfsGCEnable(); // enable releasing unused volumes
1783 end;
1785 e_WriteLog('Creating map grid', MSG_NOTIFY);
1786 mapCreateGrid();
1788 e_WriteLog('Done loading map.', MSG_NOTIFY);
1789 Result := True;
1790 end;
1792 function g_Map_GetMapInfo(Res: String): TMapInfo;
1793 var
1794 WAD: TWADFile;
1795 MapReader: TMapReader_1;
1796 Header: TMapHeaderRec_1;
1797 FileName: String;
1798 Data: Pointer;
1799 Len: Integer;
1800 begin
1801 FillChar(Result, SizeOf(Result), 0);
1802 FileName := g_ExtractWadName(Res);
1804 WAD := TWADFile.Create();
1805 if not WAD.ReadFile(FileName) then
1806 begin
1807 WAD.Free();
1808 Exit;
1809 end;
1811 //k8: it ignores path again
1812 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1813 begin
1814 WAD.Free();
1815 Exit;
1816 end;
1818 WAD.Free();
1820 MapReader := TMapReader_1.Create();
1822 if not MapReader.LoadMap(Data) then
1823 begin
1824 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1825 ZeroMemory(@Header, SizeOf(Header));
1826 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1827 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1828 end
1829 else
1830 begin
1831 Header := MapReader.GetMapHeader();
1832 Result.Name := Header.MapName;
1833 Result.Description := Header.MapDescription;
1834 end;
1836 FreeMem(Data);
1837 MapReader.Free();
1839 Result.Map := Res;
1840 Result.Author := Header.MapAuthor;
1841 Result.Height := Header.Height;
1842 Result.Width := Header.Width;
1843 end;
1845 function g_Map_GetMapsList(WADName: string): SArray;
1846 var
1847 WAD: TWADFile;
1848 a: Integer;
1849 ResList: SArray;
1850 begin
1851 Result := nil;
1852 WAD := TWADFile.Create();
1853 if not WAD.ReadFile(WADName) then
1854 begin
1855 WAD.Free();
1856 Exit;
1857 end;
1858 ResList := WAD.GetMapResources();
1859 if ResList <> nil then
1860 begin
1861 for a := 0 to High(ResList) do
1862 begin
1863 SetLength(Result, Length(Result)+1);
1864 Result[High(Result)] := ResList[a];
1865 end;
1866 end;
1867 WAD.Free();
1868 end;
1870 function g_Map_Exist(Res: string): Boolean;
1871 var
1872 WAD: TWADFile;
1873 FileName, mnn: string;
1874 ResList: SArray;
1875 a: Integer;
1876 begin
1877 Result := False;
1879 FileName := addWadExtension(g_ExtractWadName(Res));
1881 WAD := TWADFile.Create;
1882 if not WAD.ReadFile(FileName) then
1883 begin
1884 WAD.Free();
1885 Exit;
1886 end;
1888 ResList := WAD.GetMapResources();
1889 WAD.Free();
1891 mnn := g_ExtractFileName(Res);
1892 if ResList <> nil then
1893 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1894 begin
1895 Result := True;
1896 Exit;
1897 end;
1898 end;
1900 procedure g_Map_Free();
1901 var
1902 a: Integer;
1904 procedure FreePanelArray(var panels: TPanelArray);
1905 var
1906 i: Integer;
1908 begin
1909 if panels <> nil then
1910 begin
1911 for i := 0 to High(panels) do
1912 panels[i].Free();
1913 panels := nil;
1914 end;
1915 end;
1917 begin
1918 g_GFX_Free();
1919 g_Weapon_Free();
1920 g_Items_Free();
1921 g_Triggers_Free();
1922 g_Monsters_Free();
1924 RespawnPoints := nil;
1925 if FlagPoints[FLAG_RED] <> nil then
1926 begin
1927 Dispose(FlagPoints[FLAG_RED]);
1928 FlagPoints[FLAG_RED] := nil;
1929 end;
1930 if FlagPoints[FLAG_BLUE] <> nil then
1931 begin
1932 Dispose(FlagPoints[FLAG_BLUE]);
1933 FlagPoints[FLAG_BLUE] := nil;
1934 end;
1935 //DOMFlagPoints := nil;
1937 //gDOMFlags := nil;
1939 if Textures <> nil then
1940 begin
1941 for a := 0 to High(Textures) do
1942 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1943 if Textures[a].Anim then
1944 g_Frames_DeleteByID(Textures[a].FramesID)
1945 else
1946 if Textures[a].TextureID <> TEXTURE_NONE then
1947 e_DeleteTexture(Textures[a].TextureID);
1949 Textures := nil;
1950 end;
1952 FreePanelArray(gWalls);
1953 FreePanelArray(gRenderBackgrounds);
1954 FreePanelArray(gRenderForegrounds);
1955 FreePanelArray(gWater);
1956 FreePanelArray(gAcid1);
1957 FreePanelArray(gAcid2);
1958 FreePanelArray(gSteps);
1959 FreePanelArray(gLifts);
1960 FreePanelArray(gBlockMon);
1962 if BackID <> DWORD(-1) then
1963 begin
1964 gBackSize.X := 0;
1965 gBackSize.Y := 0;
1966 e_DeleteTexture(BackID);
1967 BackID := DWORD(-1);
1968 end;
1970 g_Game_StopAllSounds(False);
1971 gMusic.FreeSound();
1972 g_Sound_Delete(gMapInfo.MusicName);
1974 gMapInfo.Name := '';
1975 gMapInfo.Description := '';
1976 gMapInfo.MusicName := '';
1977 gMapInfo.Height := 0;
1978 gMapInfo.Width := 0;
1980 gDoorMap := nil;
1981 gLiftMap := nil;
1983 PanelByID := nil;
1984 end;
1986 procedure g_Map_Update();
1987 var
1988 a, d, j: Integer;
1989 m: Word;
1990 s: String;
1992 procedure UpdatePanelArray(var panels: TPanelArray);
1993 var
1994 i: Integer;
1996 begin
1997 if panels <> nil then
1998 for i := 0 to High(panels) do
1999 panels[i].Update();
2000 end;
2002 begin
2003 UpdatePanelArray(gWalls);
2004 UpdatePanelArray(gRenderBackgrounds);
2005 UpdatePanelArray(gRenderForegrounds);
2006 UpdatePanelArray(gWater);
2007 UpdatePanelArray(gAcid1);
2008 UpdatePanelArray(gAcid2);
2009 UpdatePanelArray(gSteps);
2011 if gGameSettings.GameMode = GM_CTF then
2012 begin
2013 for a := FLAG_RED to FLAG_BLUE do
2014 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2015 with gFlags[a] do
2016 begin
2017 if gFlags[a].Animation <> nil then
2018 gFlags[a].Animation.Update();
2020 m := g_Obj_Move(@Obj, True, True);
2022 if gTime mod (GAME_TICK*2) <> 0 then
2023 Continue;
2025 // Ñîïðîòèâëåíèå âîçäóõà:
2026 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2028 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2029 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2030 begin
2031 g_Map_ResetFlag(a);
2032 gFlags[a].CaptureTime := 0;
2033 if a = FLAG_RED then
2034 s := _lc[I_PLAYER_FLAG_RED]
2035 else
2036 s := _lc[I_PLAYER_FLAG_BLUE];
2037 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2039 if g_Game_IsNet then
2040 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2041 Continue;
2042 end;
2044 if Count > 0 then
2045 Count := Count - 1;
2047 // Èãðîê áåðåò ôëàã:
2048 if gPlayers <> nil then
2049 begin
2050 j := Random(Length(gPlayers)) - 1;
2052 for d := 0 to High(gPlayers) do
2053 begin
2054 Inc(j);
2055 if j > High(gPlayers) then
2056 j := 0;
2058 if gPlayers[j] <> nil then
2059 if gPlayers[j].Live and
2060 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2061 begin
2062 if gPlayers[j].GetFlag(a) then
2063 Break;
2064 end;
2065 end;
2066 end;
2067 end;
2068 end;
2069 end;
2072 // old algo
2073 procedure g_Map_DrawPanels (PanelType: Word);
2075 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2076 var
2077 idx: Integer;
2078 begin
2079 if (panels <> nil) then
2080 begin
2081 // alas, no visible set
2082 for idx := 0 to High(panels) do
2083 begin
2084 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2085 end;
2086 end;
2087 end;
2089 begin
2090 case PanelType of
2091 PANEL_WALL: DrawPanels(gWalls);
2092 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2093 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2094 PANEL_FORE: DrawPanels(gRenderForegrounds);
2095 PANEL_WATER: DrawPanels(gWater);
2096 PANEL_ACID1: DrawPanels(gAcid1);
2097 PANEL_ACID2: DrawPanels(gAcid2);
2098 PANEL_STEP: DrawPanels(gSteps);
2099 end;
2100 end;
2103 // new algo
2104 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2105 function checker (pan: TPanel; tag: Integer): Boolean;
2106 begin
2107 result := false; // don't stop, ever
2108 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2109 gDrawPanelList.insert(pan);
2110 end;
2112 begin
2113 dplClear();
2114 //tagmask := panelTypeToTag(PanelType);
2116 {if gdbg_map_use_tree_draw then
2117 begin
2118 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2119 end
2120 else}
2121 begin
2122 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2123 end;
2124 // list will be rendered in `g_game.DrawPlayer()`
2125 end;
2128 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2129 function checker (pan: TPanel; tag: Integer): Boolean;
2130 begin
2131 result := false; // don't stop, ever
2132 pan.DrawShadowVolume(lightX, lightY, radius);
2133 end;
2135 begin
2136 {if gdbg_map_use_tree_draw then
2137 begin
2138 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2139 end
2140 else}
2141 begin
2142 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2143 end;
2144 end;
2147 procedure g_Map_DrawBack(dx, dy: Integer);
2148 begin
2149 if gDrawBackGround and (BackID <> DWORD(-1)) then
2150 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2151 else
2152 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2153 end;
2155 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2156 PanelType: Word; b1x3: Boolean=false): Boolean;
2157 var
2158 a, h: Integer;
2159 begin
2160 Result := False;
2162 if WordBool(PanelType and PANEL_WALL) then
2163 if gWalls <> nil then
2164 begin
2165 h := High(gWalls);
2167 for a := 0 to h do
2168 if gWalls[a].Enabled and
2169 g_Collide(X, Y, Width, Height,
2170 gWalls[a].X, gWalls[a].Y,
2171 gWalls[a].Width, gWalls[a].Height) then
2172 begin
2173 Result := True;
2174 Exit;
2175 end;
2176 end;
2178 if WordBool(PanelType and PANEL_WATER) then
2179 if gWater <> nil then
2180 begin
2181 h := High(gWater);
2183 for a := 0 to h do
2184 if g_Collide(X, Y, Width, Height,
2185 gWater[a].X, gWater[a].Y,
2186 gWater[a].Width, gWater[a].Height) then
2187 begin
2188 Result := True;
2189 Exit;
2190 end;
2191 end;
2193 if WordBool(PanelType and PANEL_ACID1) then
2194 if gAcid1 <> nil then
2195 begin
2196 h := High(gAcid1);
2198 for a := 0 to h do
2199 if g_Collide(X, Y, Width, Height,
2200 gAcid1[a].X, gAcid1[a].Y,
2201 gAcid1[a].Width, gAcid1[a].Height) then
2202 begin
2203 Result := True;
2204 Exit;
2205 end;
2206 end;
2208 if WordBool(PanelType and PANEL_ACID2) then
2209 if gAcid2 <> nil then
2210 begin
2211 h := High(gAcid2);
2213 for a := 0 to h do
2214 if g_Collide(X, Y, Width, Height,
2215 gAcid2[a].X, gAcid2[a].Y,
2216 gAcid2[a].Width, gAcid2[a].Height) then
2217 begin
2218 Result := True;
2219 Exit;
2220 end;
2221 end;
2223 if WordBool(PanelType and PANEL_STEP) then
2224 if gSteps <> nil then
2225 begin
2226 h := High(gSteps);
2228 for a := 0 to h do
2229 if g_Collide(X, Y, Width, Height,
2230 gSteps[a].X, gSteps[a].Y,
2231 gSteps[a].Width, gSteps[a].Height) then
2232 begin
2233 Result := True;
2234 Exit;
2235 end;
2236 end;
2238 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2239 if gLifts <> nil then
2240 begin
2241 h := High(gLifts);
2243 for a := 0 to h do
2244 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2245 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2246 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2247 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2248 g_Collide(X, Y, Width, Height,
2249 gLifts[a].X, gLifts[a].Y,
2250 gLifts[a].Width, gLifts[a].Height) then
2251 begin
2252 Result := True;
2253 Exit;
2254 end;
2255 end;
2257 if WordBool(PanelType and PANEL_BLOCKMON) then
2258 if gBlockMon <> nil then
2259 begin
2260 h := High(gBlockMon);
2262 for a := 0 to h do
2263 if ( (not b1x3) or
2264 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2265 g_Collide(X, Y, Width, Height,
2266 gBlockMon[a].X, gBlockMon[a].Y,
2267 gBlockMon[a].Width, gBlockMon[a].Height) then
2268 begin
2269 Result := True;
2270 Exit;
2271 end;
2272 end;
2273 end;
2275 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2276 var
2277 texid: DWORD;
2279 function checkPanels (constref panels: TPanelArray): Boolean;
2280 var
2281 a: Integer;
2282 begin
2283 result := false;
2284 if panels = nil then exit;
2285 for a := 0 to High(panels) do
2286 begin
2287 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2288 begin
2289 result := true;
2290 texid := panels[a].GetTextureID();
2291 exit;
2292 end;
2293 end;
2294 end;
2296 begin
2297 texid := TEXTURE_NONE;
2298 result := texid;
2299 if not checkPanels(gWater) then
2300 if not checkPanels(gAcid1) then
2301 if not checkPanels(gAcid2) then exit;
2302 result := texid;
2303 end;
2306 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2307 function checker (pan: TPanel; tag: Integer): Boolean;
2308 begin
2309 result := false; // don't stop, ever
2311 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2312 begin
2313 if not pan.Enabled then exit;
2314 end;
2316 if ((tag and GridTagLift) <> 0) then
2317 begin
2318 result :=
2319 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2320 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2321 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2322 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) and
2323 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2324 exit;
2325 end;
2327 if ((tag and GridTagBlockMon) <> 0) then
2328 begin
2329 result := ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2330 exit;
2331 end;
2333 // other shit
2334 result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2335 end;
2337 var
2338 tagmask: Integer = 0;
2339 begin
2340 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2341 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2342 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2343 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2344 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2345 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2346 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2348 if (tagmask = 0) then begin result := false; exit; end; // just in case
2350 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2351 if gdbg_map_use_accel_coldet then
2352 begin
2353 {if gdbg_map_use_tree_coldet then
2354 begin
2355 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2356 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2357 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2358 begin
2359 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2360 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2361 end;
2362 end
2363 else}
2364 begin
2365 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2366 end;
2367 end
2368 else
2369 begin
2370 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2371 end;
2372 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2373 end;
2376 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2377 var
2378 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2379 texid: DWORD;
2381 // slightly different from the old code, but meh...
2382 function checker (pan: TPanel; tag: Integer): Boolean;
2383 begin
2384 result := false; // don't stop, ever
2385 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2386 // check priorities
2387 case cctype of
2388 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2389 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2390 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2391 end;
2392 // collision?
2393 if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2394 // yeah
2395 texid := pan.GetTextureID();
2396 // water? water has the highest priority, so stop right here
2397 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2398 // acid2?
2399 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2400 // acid1?
2401 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2402 end;
2404 begin
2405 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2406 if gdbg_map_use_accel_coldet then
2407 begin
2408 texid := TEXTURE_NONE;
2409 {if gdbg_map_use_tree_coldet then
2410 begin
2411 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2412 end
2413 else}
2414 begin
2415 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2416 end;
2417 result := texid;
2418 end
2419 else
2420 begin
2421 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2422 end;
2423 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2424 end;
2426 procedure g_Map_EnableWall(ID: DWORD);
2427 begin
2428 with gWalls[ID] do
2429 begin
2430 Enabled := True;
2431 //g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2433 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2434 end;
2435 end;
2437 procedure g_Map_DisableWall(ID: DWORD);
2438 begin
2439 with gWalls[ID] do
2440 begin
2441 Enabled := False;
2442 //g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2444 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2445 end;
2446 end;
2448 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2449 var
2450 tp: TPanel;
2451 begin
2452 case PanelType of
2453 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2454 tp := gWalls[ID];
2455 PANEL_FORE:
2456 tp := gRenderForegrounds[ID];
2457 PANEL_BACK:
2458 tp := gRenderBackgrounds[ID];
2459 PANEL_WATER:
2460 tp := gWater[ID];
2461 PANEL_ACID1:
2462 tp := gAcid1[ID];
2463 PANEL_ACID2:
2464 tp := gAcid2[ID];
2465 PANEL_STEP:
2466 tp := gSteps[ID];
2467 else
2468 Exit;
2469 end;
2471 tp.NextTexture(AnimLoop);
2472 if g_Game_IsServer and g_Game_IsNet then
2473 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2474 end;
2476 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2477 begin
2478 if gLifts[ID].LiftType = t then
2479 Exit;
2481 with gLifts[ID] do
2482 begin
2483 LiftType := t;
2486 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2488 if LiftType = 0 then
2489 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2490 else if LiftType = 1 then
2491 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2492 else if LiftType = 2 then
2493 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2494 else if LiftType = 3 then
2495 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2498 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2499 end;
2500 end;
2502 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2503 var
2504 a: Integer;
2505 PointsArray: Array of TRespawnPoint;
2506 begin
2507 Result := False;
2508 SetLength(PointsArray, 0);
2510 if RespawnPoints = nil then
2511 Exit;
2513 for a := 0 to High(RespawnPoints) do
2514 if RespawnPoints[a].PointType = PointType then
2515 begin
2516 SetLength(PointsArray, Length(PointsArray)+1);
2517 PointsArray[High(PointsArray)] := RespawnPoints[a];
2518 end;
2520 if PointsArray = nil then
2521 Exit;
2523 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2524 Result := True;
2525 end;
2527 function g_Map_GetPointCount(PointType: Byte): Word;
2528 var
2529 a: Integer;
2530 begin
2531 Result := 0;
2533 if RespawnPoints = nil then
2534 Exit;
2536 for a := 0 to High(RespawnPoints) do
2537 if RespawnPoints[a].PointType = PointType then
2538 Result := Result + 1;
2539 end;
2541 function g_Map_HaveFlagPoints(): Boolean;
2542 begin
2543 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2544 end;
2546 procedure g_Map_ResetFlag(Flag: Byte);
2547 begin
2548 with gFlags[Flag] do
2549 begin
2550 Obj.X := -1000;
2551 Obj.Y := -1000;
2552 Obj.Vel.X := 0;
2553 Obj.Vel.Y := 0;
2554 Direction := D_LEFT;
2555 State := FLAG_STATE_NONE;
2556 if FlagPoints[Flag] <> nil then
2557 begin
2558 Obj.X := FlagPoints[Flag]^.X;
2559 Obj.Y := FlagPoints[Flag]^.Y;
2560 Direction := FlagPoints[Flag]^.Direction;
2561 State := FLAG_STATE_NORMAL;
2562 end;
2563 Count := -1;
2564 end;
2565 end;
2567 procedure g_Map_DrawFlags();
2568 var
2569 i, dx: Integer;
2570 Mirror: TMirrorType;
2571 begin
2572 if gGameSettings.GameMode <> GM_CTF then
2573 Exit;
2575 for i := FLAG_RED to FLAG_BLUE do
2576 with gFlags[i] do
2577 if State <> FLAG_STATE_CAPTURED then
2578 begin
2579 if State = FLAG_STATE_NONE then
2580 continue;
2582 if Direction = D_LEFT then
2583 begin
2584 Mirror := M_HORIZONTAL;
2585 dx := -1;
2586 end
2587 else
2588 begin
2589 Mirror := M_NONE;
2590 dx := 1;
2591 end;
2593 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2595 if g_debug_Frames then
2596 begin
2597 e_DrawQuad(Obj.X+Obj.Rect.X,
2598 Obj.Y+Obj.Rect.Y,
2599 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2600 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2601 0, 255, 0);
2602 end;
2603 end;
2604 end;
2606 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2607 var
2608 dw: DWORD;
2609 b: Byte;
2610 str: String;
2611 boo: Boolean;
2613 procedure SavePanelArray(var panels: TPanelArray);
2614 var
2615 PAMem: TBinMemoryWriter;
2616 i: Integer;
2617 begin
2618 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2619 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2621 i := 0;
2622 while i < Length(panels) do
2623 begin
2624 if panels[i].SaveIt then
2625 begin
2626 // ID ïàíåëè:
2627 PAMem.WriteInt(i);
2628 // Ñîõðàíÿåì ïàíåëü:
2629 panels[i].SaveState(PAMem);
2630 end;
2631 Inc(i);
2632 end;
2634 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2635 PAMem.SaveToMemory(Mem);
2636 PAMem.Free();
2637 end;
2639 procedure SaveFlag(flag: PFlag);
2640 begin
2641 // Ñèãíàòóðà ôëàãà:
2642 dw := FLAG_SIGNATURE; // 'FLAG'
2643 Mem.WriteDWORD(dw);
2644 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2645 Mem.WriteByte(flag^.RespawnType);
2646 // Ñîñòîÿíèå ôëàãà:
2647 Mem.WriteByte(flag^.State);
2648 // Íàïðàâëåíèå ôëàãà:
2649 if flag^.Direction = D_LEFT then
2650 b := 1
2651 else // D_RIGHT
2652 b := 2;
2653 Mem.WriteByte(b);
2654 // Îáúåêò ôëàãà:
2655 Obj_SaveState(@flag^.Obj, Mem);
2656 end;
2658 begin
2659 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2661 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2662 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2663 SavePanelArray(gWalls);
2664 // Ñîõðàíÿåì ïàíåëè ôîíà:
2665 SavePanelArray(gRenderBackgrounds);
2666 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2667 SavePanelArray(gRenderForegrounds);
2668 // Ñîõðàíÿåì ïàíåëè âîäû:
2669 SavePanelArray(gWater);
2670 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2671 SavePanelArray(gAcid1);
2672 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2673 SavePanelArray(gAcid2);
2674 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2675 SavePanelArray(gSteps);
2676 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2677 SavePanelArray(gLifts);
2678 ///// /////
2680 ///// Ñîõðàíÿåì ìóçûêó: /////
2681 // Ñèãíàòóðà ìóçûêè:
2682 dw := MUSIC_SIGNATURE; // 'MUSI'
2683 Mem.WriteDWORD(dw);
2684 // Íàçâàíèå ìóçûêè:
2685 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2686 if gMusic.NoMusic then
2687 str := ''
2688 else
2689 str := gMusic.Name;
2690 Mem.WriteString(str, 64);
2691 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2692 dw := gMusic.GetPosition();
2693 Mem.WriteDWORD(dw);
2694 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2695 boo := gMusic.SpecPause;
2696 Mem.WriteBoolean(boo);
2697 ///// /////
2699 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2700 Mem.WriteInt(gTotalMonsters);
2701 ///// /////
2703 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2704 if gGameSettings.GameMode = GM_CTF then
2705 begin
2706 // Ôëàã Êðàñíîé êîìàíäû:
2707 SaveFlag(@gFlags[FLAG_RED]);
2708 // Ôëàã Ñèíåé êîìàíäû:
2709 SaveFlag(@gFlags[FLAG_BLUE]);
2710 end;
2711 ///// /////
2713 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2714 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2715 begin
2716 // Î÷êè Êðàñíîé êîìàíäû:
2717 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2718 // Î÷êè Ñèíåé êîìàíäû:
2719 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2720 end;
2721 ///// /////
2722 end;
2724 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2725 var
2726 dw: DWORD;
2727 b: Byte;
2728 str: String;
2729 boo: Boolean;
2731 procedure LoadPanelArray(var panels: TPanelArray);
2732 var
2733 PAMem: TBinMemoryReader;
2734 i, id: Integer;
2735 begin
2736 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2737 PAMem := TBinMemoryReader.Create();
2738 PAMem.LoadFromMemory(Mem);
2740 for i := 0 to Length(panels)-1 do
2741 if panels[i].SaveIt then
2742 begin
2743 // ID ïàíåëè:
2744 PAMem.ReadInt(id);
2745 if id <> i then
2746 begin
2747 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2748 end;
2749 // Çàãðóæàåì ïàíåëü:
2750 panels[i].LoadState(PAMem);
2751 end;
2753 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2754 PAMem.Free();
2755 end;
2757 procedure LoadFlag(flag: PFlag);
2758 begin
2759 // Ñèãíàòóðà ôëàãà:
2760 Mem.ReadDWORD(dw);
2761 if dw <> FLAG_SIGNATURE then // 'FLAG'
2762 begin
2763 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2764 end;
2765 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2766 Mem.ReadByte(flag^.RespawnType);
2767 // Ñîñòîÿíèå ôëàãà:
2768 Mem.ReadByte(flag^.State);
2769 // Íàïðàâëåíèå ôëàãà:
2770 Mem.ReadByte(b);
2771 if b = 1 then
2772 flag^.Direction := D_LEFT
2773 else // b = 2
2774 flag^.Direction := D_RIGHT;
2775 // Îáúåêò ôëàãà:
2776 Obj_LoadState(@flag^.Obj, Mem);
2777 end;
2779 begin
2780 if Mem = nil then
2781 Exit;
2783 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2784 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2785 LoadPanelArray(gWalls);
2786 // Çàãðóæàåì ïàíåëè ôîíà:
2787 LoadPanelArray(gRenderBackgrounds);
2788 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2789 LoadPanelArray(gRenderForegrounds);
2790 // Çàãðóæàåì ïàíåëè âîäû:
2791 LoadPanelArray(gWater);
2792 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2793 LoadPanelArray(gAcid1);
2794 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2795 LoadPanelArray(gAcid2);
2796 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2797 LoadPanelArray(gSteps);
2798 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2799 LoadPanelArray(gLifts);
2800 ///// /////
2802 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2803 g_GFX_Init();
2804 mapCreateGrid();
2806 ///// Çàãðóæàåì ìóçûêó: /////
2807 // Ñèãíàòóðà ìóçûêè:
2808 Mem.ReadDWORD(dw);
2809 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2810 begin
2811 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2812 end;
2813 // Íàçâàíèå ìóçûêè:
2814 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2815 Mem.ReadString(str);
2816 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2817 Mem.ReadDWORD(dw);
2818 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2819 Mem.ReadBoolean(boo);
2820 // Çàïóñêàåì ýòó ìóçûêó:
2821 gMusic.SetByName(str);
2822 gMusic.SpecPause := boo;
2823 gMusic.Play();
2824 gMusic.Pause(True);
2825 gMusic.SetPosition(dw);
2826 ///// /////
2828 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2829 Mem.ReadInt(gTotalMonsters);
2830 ///// /////
2832 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2833 if gGameSettings.GameMode = GM_CTF then
2834 begin
2835 // Ôëàã Êðàñíîé êîìàíäû:
2836 LoadFlag(@gFlags[FLAG_RED]);
2837 // Ôëàã Ñèíåé êîìàíäû:
2838 LoadFlag(@gFlags[FLAG_BLUE]);
2839 end;
2840 ///// /////
2842 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2843 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2844 begin
2845 // Î÷êè Êðàñíîé êîìàíäû:
2846 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2847 // Î÷êè Ñèíåé êîìàíäû:
2848 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2849 end;
2850 ///// /////
2851 end;
2853 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2854 var
2855 Arr: TPanelArray;
2856 begin
2857 Result := nil;
2858 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2859 Arr := PanelByID[PanelID].PWhere^;
2860 PanelArrayID := PanelByID[PanelID].PArrID;
2861 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2862 end;
2864 end.