DEADSOFTWARE

map ray tracer now using grid instead of tree
[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>;
205 TDynAABBTreePanelBase = specialize TDynAABBTreeBase<TPanel>;
207 TDynAABBTreeMap = class(TDynAABBTreePanelBase)
208 function getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean; override;
209 end;
211 function TDynAABBTreeMap.getFleshAABB (out aabb: AABB2D; pan: TPanel; tag: Integer): Boolean;
212 begin
213 result := false;
214 if (pan = nil) then begin aabb := AABB2D.Create(0, 0, 0, 0); exit; end;
215 aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1);
216 if (pan.Width < 1) or (pan.Height < 1) then exit;
217 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
218 result := true;
219 end;
222 function panelTypeToTag (panelType: Word): Integer;
223 begin
224 case panelType of
225 PANEL_WALL: result := GridTagWall; // gWalls
226 PANEL_OPENDOOR, PANEL_CLOSEDOOR: result := GridTagDoor; // gWalls
227 PANEL_BACK: result := GridTagBack; // gRenderBackgrounds
228 PANEL_FORE: result := GridTagFore; // gRenderForegrounds
229 PANEL_WATER: result := GridTagWater; // gWater
230 PANEL_ACID1: result := GridTagAcid1; // gAcid1
231 PANEL_ACID2: result := GridTagAcid2; // gAcid2
232 PANEL_STEP: result := GridTagStep; // gSteps
233 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: result := GridTagLift; // gLifts -- this is for all lifts
234 PANEL_BLOCKMON: result := GridTagBlockMon; // gBlockMon -- this is for all blockmons
235 else result := GridTagInvalid;
236 end;
237 end;
240 function dplLess (a, b: TObject): Boolean;
241 var
242 pa, pb: TPanel;
243 begin
244 //result := ((a as TPanel).ArrIdx < (b as TPanel).ArrIdx);
245 pa := TPanel(a);
246 pb := TPanel(b);
247 if (pa.tag < pb.tag) then begin result := true; exit; end;
248 if (pa.tag > pb.tag) then begin result := false; exit; end;
249 result := (pa.ArrIdx < pb.ArrIdx);
250 end;
252 procedure dplClear ();
253 begin
254 if (gDrawPanelList = nil) then gDrawPanelList := TBinaryHeapObj.Create(@dplLess) else gDrawPanelList.clear();
255 end;
258 type
259 TPanelID = record
260 PWhere: ^TPanelArray;
261 PArrID: Integer;
262 end;
264 var
265 PanelById: array of TPanelID;
266 Textures: TLevelTextureArray;
267 RespawnPoints: Array of TRespawnPoint;
268 FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
269 //DOMFlagPoints: Array of TFlagPoint;
270 gMapGrid: TPanelGrid = nil;
271 mapTree: TDynAABBTreeMap = nil;
274 procedure g_Map_ProfilersBegin ();
275 begin
276 if (profMapCollision = nil) then profMapCollision := TProfiler.Create('COLSOLID', g_profile_history_size);
277 profMapCollision.mainBegin(g_profile_collision);
278 // create sections
279 if g_profile_collision then
280 begin
281 profMapCollision.sectionBegin('*solids');
282 profMapCollision.sectionEnd();
283 profMapCollision.sectionBegin('liquids');
284 profMapCollision.sectionEnd();
285 end;
286 end;
288 procedure g_Map_ProfilersEnd ();
289 begin
290 if (profMapCollision <> nil) then profMapCollision.mainEnd();
291 end;
294 // wall index in `gWalls` or -1
295 (*
296 function g_Map_traceToNearestWallOld (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
298 function sqchecker (pan: TPanel; var ray: Ray2D): Single;
299 var
300 aabb: AABB2D;
301 tmin: Single;
302 begin
303 result := -666.0; // invalid
304 if not pan.Enabled then exit;
305 aabb := AABB2D.CreateWH(pan.X, pan.Y, pan.Width, pan.Height);
306 if not aabb.valid then exit;
307 if aabb.intersects(ray, @tmin) then
308 begin
309 //if (tmin*tmin > maxDistSq) then exit;
310 if (tmin >= 0.0) then
311 begin
312 //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);
313 //if (tmin < 0.0) then tmin := 0.0;
314 result := tmin;
315 end;
316 end;
317 end;
319 var
320 qr: TDynAABBTreeMap.TSegmentQueryResult;
321 ray: Ray2D;
322 hxf, hyf: Single;
323 hx, hy: Integer;
324 maxDistSq: Single;
325 begin
326 result := -1;
327 if (mapTree = nil) then exit;
328 if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
329 begin
330 maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
331 if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
332 begin
333 result := qr.flesh.arrIdx;
334 if (hitx <> nil) or (hity <> nil) then
335 begin
336 ray := Ray2D.Create(x0, y0, x1, y1);
337 hxf := ray.origX+ray.dirX*qr.time;
338 hyf := ray.origY+ray.dirY*qr.time;
339 while true do
340 begin
341 hx := trunc(hxf);
342 hy := trunc(hyf);
343 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
344 begin
345 // go back a little
346 hxf -= ray.dirX;
347 hyf -= ray.dirY;
348 end
349 else
350 begin
351 break;
352 end;
353 end;
354 if (hitx <> nil) then hitx^ := hx;
355 if (hity <> nil) then hity^ := hy;
356 end;
357 end;
358 end;
359 end;
360 *)
363 // wall index in `gWalls` or -1
364 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
365 var
366 lastX, lastY, lastDist: Integer;
367 wasHit: Boolean = false;
369 // pan=nil: before processing new tile
370 function sqchecker (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
371 var
372 dist: Integer;
373 begin
374 if (pan = nil) then
375 begin
376 // stop if something was hit at the previous tile
377 result := wasHit;
378 end
379 else
380 begin
381 result := false;
382 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
383 begin
384 if not pan.Enabled then exit;
385 end;
386 dist := (prevx-x0)*(prevx-x0)+(prevy-y0)*(prevy-y0);
387 if (dist < lastDist) then
388 begin
389 wasHit := true;
390 lastDist := dist;
391 lastX := prevx;
392 lastY := prevy;
393 end;
394 end;
395 end;
397 begin
398 result := false;
399 if (gMapGrid = nil) then exit;
400 lastDist := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+1;
401 lastX := 0;
402 lastY := 0;
403 result := gMapGrid.traceRay(x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor));
404 if (hitx <> nil) then hitx^ := lastX;
405 if (hity <> nil) then hity^ := lastY;
406 end;
409 function g_Map_ForEachPanelAt (x, y: Integer; cb: TForEachPanelCB; panelType: Word): Boolean;
411 function checker (pan: TPanel; tag: Integer): Boolean;
412 begin
413 result := false; // don't stop, ever
415 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
416 begin
417 if not pan.Enabled then exit;
418 end;
420 result := (x >= pan.X) and (y >= pan.Y) and (x < pan.X+pan.Width) and (y < pan.Y+pan.Height);
421 if not result then exit;
423 if ((tag and GridTagLift) <> 0) then
424 begin
425 result :=
426 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
427 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
428 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
429 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
430 end;
432 // other shit
433 if result then result := cb(pan);
434 end;
436 var
437 tagmask: Integer = 0;
438 begin
439 result := false;
440 if not assigned(cb) then exit;
442 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
443 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
444 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
445 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
446 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
447 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
448 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
450 if (tagmask = 0) then exit;// just in case
451 result := gMapGrid.forEachInAABB(x, y, 1, 1, checker, tagmask);
452 end;
455 function g_Map_IsSpecialTexture(Texture: String): Boolean;
456 begin
457 Result := (Texture = TEXTURE_NAME_WATER) or
458 (Texture = TEXTURE_NAME_ACID1) or
459 (Texture = TEXTURE_NAME_ACID2);
460 end;
462 procedure CreateDoorMap();
463 var
464 PanelArray: Array of record
465 X, Y: Integer;
466 Width, Height: Word;
467 Active: Boolean;
468 PanelID: DWORD;
469 end;
470 a, b, c, m, i, len: Integer;
471 ok: Boolean;
472 begin
473 if gWalls = nil then
474 Exit;
476 i := 0;
477 len := 128;
478 SetLength(PanelArray, len);
480 for a := 0 to High(gWalls) do
481 if gWalls[a].Door then
482 begin
483 PanelArray[i].X := gWalls[a].X;
484 PanelArray[i].Y := gWalls[a].Y;
485 PanelArray[i].Width := gWalls[a].Width;
486 PanelArray[i].Height := gWalls[a].Height;
487 PanelArray[i].Active := True;
488 PanelArray[i].PanelID := a;
490 i := i + 1;
491 if i = len then
492 begin
493 len := len + 128;
494 SetLength(PanelArray, len);
495 end;
496 end;
498 // Íåò äâåðåé:
499 if i = 0 then
500 begin
501 PanelArray := nil;
502 Exit;
503 end;
505 SetLength(gDoorMap, 0);
507 g_Game_SetLoadingText(_lc[I_LOAD_DOOR_MAP], i-1, False);
509 for a := 0 to i-1 do
510 if PanelArray[a].Active then
511 begin
512 PanelArray[a].Active := False;
513 m := Length(gDoorMap);
514 SetLength(gDoorMap, m+1);
515 SetLength(gDoorMap[m], 1);
516 gDoorMap[m, 0] := PanelArray[a].PanelID;
517 ok := True;
519 while ok do
520 begin
521 ok := False;
523 for b := 0 to i-1 do
524 if PanelArray[b].Active then
525 for c := 0 to High(gDoorMap[m]) do
526 if {((gRenderWalls[PanelArray[b].RenderPanelID].TextureID = gRenderWalls[gDoorMap[m, c]].TextureID) or
527 gRenderWalls[PanelArray[b].RenderPanelID].Hide or gRenderWalls[gDoorMap[m, c]].Hide) and}
528 g_CollideAround(PanelArray[b].X, PanelArray[b].Y,
529 PanelArray[b].Width, PanelArray[b].Height,
530 gWalls[gDoorMap[m, c]].X,
531 gWalls[gDoorMap[m, c]].Y,
532 gWalls[gDoorMap[m, c]].Width,
533 gWalls[gDoorMap[m, c]].Height) then
534 begin
535 PanelArray[b].Active := False;
536 SetLength(gDoorMap[m],
537 Length(gDoorMap[m])+1);
538 gDoorMap[m, High(gDoorMap[m])] := PanelArray[b].PanelID;
539 ok := True;
540 Break;
541 end;
542 end;
544 g_Game_StepLoading();
545 end;
547 PanelArray := nil;
548 end;
550 procedure CreateLiftMap();
551 var
552 PanelArray: Array of record
553 X, Y: Integer;
554 Width, Height: Word;
555 Active: Boolean;
556 end;
557 a, b, c, len, i, j: Integer;
558 ok: Boolean;
559 begin
560 if gLifts = nil then
561 Exit;
563 len := Length(gLifts);
564 SetLength(PanelArray, len);
566 for a := 0 to len-1 do
567 begin
568 PanelArray[a].X := gLifts[a].X;
569 PanelArray[a].Y := gLifts[a].Y;
570 PanelArray[a].Width := gLifts[a].Width;
571 PanelArray[a].Height := gLifts[a].Height;
572 PanelArray[a].Active := True;
573 end;
575 SetLength(gLiftMap, len);
576 i := 0;
578 g_Game_SetLoadingText(_lc[I_LOAD_LIFT_MAP], len-1, False);
580 for a := 0 to len-1 do
581 if PanelArray[a].Active then
582 begin
583 PanelArray[a].Active := False;
584 SetLength(gLiftMap[i], 32);
585 j := 0;
586 gLiftMap[i, j] := a;
587 ok := True;
589 while ok do
590 begin
591 ok := False;
592 for b := 0 to len-1 do
593 if PanelArray[b].Active then
594 for c := 0 to j do
595 if g_CollideAround(PanelArray[b].X,
596 PanelArray[b].Y,
597 PanelArray[b].Width,
598 PanelArray[b].Height,
599 PanelArray[gLiftMap[i, c]].X,
600 PanelArray[gLiftMap[i, c]].Y,
601 PanelArray[gLiftMap[i, c]].Width,
602 PanelArray[gLiftMap[i, c]].Height) then
603 begin
604 PanelArray[b].Active := False;
605 j := j+1;
606 if j > High(gLiftMap[i]) then
607 SetLength(gLiftMap[i],
608 Length(gLiftMap[i])+32);
610 gLiftMap[i, j] := b;
611 ok := True;
613 Break;
614 end;
615 end;
617 SetLength(gLiftMap[i], j+1);
618 i := i+1;
620 g_Game_StepLoading();
621 end;
623 SetLength(gLiftMap, i);
625 PanelArray := nil;
626 end;
628 function CreatePanel(PanelRec: TPanelRec_1; AddTextures: TAddTextureArray;
629 CurTex: Integer; sav: Boolean): Integer;
630 var
631 len: Integer;
632 panels: ^TPanelArray;
633 begin
634 Result := -1;
636 case PanelRec.PanelType of
637 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
638 panels := @gWalls;
639 PANEL_BACK:
640 panels := @gRenderBackgrounds;
641 PANEL_FORE:
642 panels := @gRenderForegrounds;
643 PANEL_WATER:
644 panels := @gWater;
645 PANEL_ACID1:
646 panels := @gAcid1;
647 PANEL_ACID2:
648 panels := @gAcid2;
649 PANEL_STEP:
650 panels := @gSteps;
651 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
652 panels := @gLifts;
653 PANEL_BLOCKMON:
654 panels := @gBlockMon;
655 else
656 Exit;
657 end;
659 len := Length(panels^);
660 SetLength(panels^, len + 1);
662 panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
663 panels^[len].ArrIdx := len;
664 panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
665 if sav then
666 panels^[len].SaveIt := True;
668 Result := len;
670 len := Length(PanelByID);
671 SetLength(PanelByID, len + 1);
672 PanelByID[len].PWhere := panels;
673 PanelByID[len].PArrID := Result;
674 end;
676 function CreateNullTexture(RecName: String): Integer;
677 begin
678 SetLength(Textures, Length(Textures)+1);
679 result := High(Textures);
681 with Textures[High(Textures)] do
682 begin
683 TextureName := RecName;
684 Width := 1;
685 Height := 1;
686 Anim := False;
687 TextureID := TEXTURE_NONE;
688 end;
689 end;
691 function CreateTexture(RecName: String; Map: string; log: Boolean): Integer;
692 var
693 WAD: TWADFile;
694 TextureData: Pointer;
695 WADName, txname: String;
696 a, ResLength: Integer;
697 begin
698 Result := -1;
700 if Textures <> nil then
701 for a := 0 to High(Textures) do
702 if Textures[a].TextureName = RecName then
703 begin // Òåêñòóðà ñ òàêèì èìåíåì óæå åñòü
704 Result := a;
705 Exit;
706 end;
708 // Òåêñòóðû ñî ñïåöèàëüíûìè èìåíàìè (âîäà, ëàâà, êèñëîòà):
709 if (RecName = TEXTURE_NAME_WATER) or
710 (RecName = TEXTURE_NAME_ACID1) or
711 (RecName = TEXTURE_NAME_ACID2) then
712 begin
713 SetLength(Textures, Length(Textures)+1);
715 with Textures[High(Textures)] do
716 begin
717 TextureName := RecName;
719 if TextureName = TEXTURE_NAME_WATER then
720 TextureID := TEXTURE_SPECIAL_WATER
721 else
722 if TextureName = TEXTURE_NAME_ACID1 then
723 TextureID := TEXTURE_SPECIAL_ACID1
724 else
725 if TextureName = TEXTURE_NAME_ACID2 then
726 TextureID := TEXTURE_SPECIAL_ACID2;
728 Anim := False;
729 end;
731 result := High(Textures);
732 Exit;
733 end;
735 // Çàãðóæàåì ðåñóðñ òåêñòóðû â ïàìÿòü èç WAD'à:
736 WADName := g_ExtractWadName(RecName);
738 WAD := TWADFile.Create();
740 if WADName <> '' then
741 WADName := GameDir+'/wads/'+WADName
742 else
743 WADName := Map;
745 WAD.ReadFile(WADName);
747 txname := RecName;
749 if (WADName = Map) and WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
750 begin
751 FreeMem(TextureData);
752 RecName := 'COMMON\ALIEN';
753 end;
756 if WAD.GetResource(g_ExtractFilePathName(RecName), TextureData, ResLength) then
757 begin
758 SetLength(Textures, Length(Textures)+1);
759 if not e_CreateTextureMem(TextureData, ResLength, Textures[High(Textures)].TextureID) then
760 Exit;
761 e_GetTextureSize(Textures[High(Textures)].TextureID,
762 @Textures[High(Textures)].Width,
763 @Textures[High(Textures)].Height);
764 FreeMem(TextureData);
765 Textures[High(Textures)].TextureName := {RecName}txname;
766 Textures[High(Textures)].Anim := False;
768 result := High(Textures);
769 end
770 else // Íåò òàêîãî ðåóñðñà â WAD'å
771 begin
772 //e_WriteLog(Format('SHIT! Error loading texture %s : %s : %s', [RecName, txname, g_ExtractFilePathName(RecName)]), MSG_WARNING);
773 if log then
774 begin
775 e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING);
776 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
777 end;
778 end;
780 WAD.Free();
781 end;
783 function CreateAnimTexture(RecName: String; Map: string; log: Boolean): Integer;
784 var
785 WAD: TWADFile;
786 TextureWAD: PChar = nil;
787 TextData: Pointer = nil;
788 TextureData: Pointer = nil;
789 cfg: TConfig = nil;
790 WADName: String;
791 ResLength: Integer;
792 TextureResource: String;
793 _width, _height, _framecount, _speed: Integer;
794 _backanimation: Boolean;
795 //imgfmt: string;
796 ia: TDynImageDataArray = nil;
797 f, c, frdelay, frloop: Integer;
798 begin
799 result := -1;
801 //e_WriteLog(Format('*** Loading animated texture "%s"', [RecName]), MSG_NOTIFY);
803 // ×èòàåì WAD-ðåñóðñ àíèì.òåêñòóðû èç WAD'à â ïàìÿòü:
804 WADName := g_ExtractWadName(RecName);
806 WAD := TWADFile.Create();
807 try
808 if WADName <> '' then
809 WADName := GameDir+'/wads/'+WADName
810 else
811 WADName := Map;
813 WAD.ReadFile(WADName);
815 if not WAD.GetResource(g_ExtractFilePathName(RecName), TextureWAD, ResLength) then
816 begin
817 if log then
818 begin
819 e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
820 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
821 end;
822 exit;
823 end;
825 {TEST
826 if WADName = Map then
827 begin
828 //FreeMem(TextureWAD);
829 if not WAD.GetResource('COMMON/animation', TextureWAD, ResLength) then Halt(1);
830 end;
833 WAD.FreeWAD();
835 if ResLength < 6 then
836 begin
837 e_WriteLog(Format('Animated texture file "%s" too short', [RecName]), MSG_WARNING);
838 exit;
839 end;
841 // ýòî ïòèöà? ýòî ñàìîë¸ò?
842 if (TextureWAD[0] = 'D') and (TextureWAD[1] = 'F') and
843 (TextureWAD[2] = 'W') and (TextureWAD[3] = 'A') and (TextureWAD[4] = 'D') then
844 begin
845 // íåò, ýòî ñóïåðìåí!
846 if not WAD.ReadMemory(TextureWAD, ResLength) then
847 begin
848 e_WriteLog(Format('Animated texture WAD file "%s" is invalid', [RecName]), MSG_WARNING);
849 exit;
850 end;
852 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
853 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
854 begin
855 e_WriteLog(Format('Animated texture file "%s" has invalid INI', [RecName]), MSG_WARNING);
856 exit;
857 end;
859 cfg := TConfig.CreateMem(TextData, ResLength);
861 TextureResource := cfg.ReadStr('', 'resource', '');
862 if TextureResource = '' then
863 begin
864 e_WriteLog(Format('Animated texture WAD file "%s" has no "resource"', [RecName]), MSG_WARNING);
865 exit;
866 end;
868 _width := cfg.ReadInt('', 'framewidth', 0);
869 _height := cfg.ReadInt('', 'frameheight', 0);
870 _framecount := cfg.ReadInt('', 'framecount', 0);
871 _speed := cfg.ReadInt('', 'waitcount', 0);
872 _backanimation := cfg.ReadBool('', 'backanimation', False);
874 cfg.Free();
875 cfg := nil;
877 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
878 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
879 begin
880 e_WriteLog(Format('Animated texture WAD file "%s" has no texture "%s"', [RecName, 'TEXTURES/'+TextureResource]), MSG_WARNING);
881 exit;
882 end;
884 WAD.Free();
885 WAD := nil;
887 SetLength(Textures, Length(Textures)+1);
888 with Textures[High(Textures)] do
889 begin
890 // Ñîçäàåì êàäðû àíèì. òåêñòóðû èç ïàìÿòè:
891 if g_Frames_CreateMemory(@FramesID, '', TextureData, ResLength, _width, _height, _framecount, _backanimation) then
892 begin
893 TextureName := RecName;
894 Width := _width;
895 Height := _height;
896 Anim := True;
897 FramesCount := _framecount;
898 Speed := _speed;
899 result := High(Textures);
900 end
901 else
902 begin
903 if log then e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING);
904 end;
905 end;
906 end
907 else
908 begin
909 // try animated image
911 imgfmt := DetermineMemoryFormat(TextureWAD, ResLength);
912 if length(imgfmt) = 0 then
913 begin
914 e_WriteLog(Format('Animated texture file "%s" has unknown format', [RecName]), MSG_WARNING);
915 exit;
916 end;
918 GlobalMetadata.ClearMetaItems();
919 GlobalMetadata.ClearMetaItemsForSaving();
920 if not LoadMultiImageFromMemory(TextureWAD, ResLength, ia) then
921 begin
922 e_WriteLog(Format('Animated texture file "%s" cannot be loaded', [RecName]), MSG_WARNING);
923 exit;
924 end;
925 if length(ia) = 0 then
926 begin
927 e_WriteLog(Format('Animated texture file "%s" has no frames', [RecName]), MSG_WARNING);
928 exit;
929 end;
931 WAD.Free();
932 WAD := nil;
934 _width := ia[0].width;
935 _height := ia[0].height;
936 _framecount := length(ia);
937 _speed := 1;
938 _backanimation := false;
939 frdelay := -1;
940 frloop := -666;
941 if GlobalMetadata.HasMetaItem(SMetaFrameDelay) then
942 begin
943 //writeln(' frame delay: ', GlobalMetadata.MetaItems[SMetaFrameDelay]);
944 try
945 f := GlobalMetadata.MetaItems[SMetaFrameDelay];
946 frdelay := f;
947 if f < 0 then f := 0;
948 // rounding ;-)
949 c := f mod 28;
950 if c < 13 then c := 0 else c := 1;
951 f := (f div 28)+c;
952 if f < 1 then f := 1 else if f > 255 then f := 255;
953 _speed := f;
954 except
955 end;
956 end;
957 if GlobalMetadata.HasMetaItem(SMetaAnimationLoops) then
958 begin
959 //writeln(' frame loop : ', GlobalMetadata.MetaItems[SMetaAnimationLoops]);
960 try
961 f := GlobalMetadata.MetaItems[SMetaAnimationLoops];
962 frloop := f;
963 if f <> 0 then _backanimation := true; // non-infinite looping == forth-and-back
964 except
965 end;
966 end;
967 //writeln(' creating animated texture with ', length(ia), ' frames (delay:', _speed, '; backloop:', _backanimation, ') from "', RecName, '"...');
968 //for f := 0 to high(ia) do writeln(' frame #', f, ': ', ia[f].width, 'x', ia[f].height);
969 f := ord(_backanimation);
970 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);
972 SetLength(Textures, Length(Textures)+1);
973 // cîçäàåì êàäðû àíèì. òåêñòóðû èç êàðòèíîê
974 if g_CreateFramesImg(ia, @Textures[High(Textures)].FramesID, '', _backanimation) then
975 begin
976 Textures[High(Textures)].TextureName := RecName;
977 Textures[High(Textures)].Width := _width;
978 Textures[High(Textures)].Height := _height;
979 Textures[High(Textures)].Anim := True;
980 Textures[High(Textures)].FramesCount := length(ia);
981 Textures[High(Textures)].Speed := _speed;
982 result := High(Textures);
983 //writeln(' CREATED!');
984 end
985 else
986 begin
987 if log then e_WriteLog(Format('Error loading animation texture "%s" images', [RecName]), MSG_WARNING);
988 end;
989 end;
990 finally
991 for f := 0 to High(ia) do FreeImage(ia[f]);
992 WAD.Free();
993 cfg.Free();
994 if TextureWAD <> nil then FreeMem(TextureWAD);
995 if TextData <> nil then FreeMem(TextData);
996 if TextureData <> nil then FreeMem(TextureData);
997 end;
998 end;
1000 procedure CreateItem(Item: TItemRec_1);
1001 begin
1002 if g_Game_IsClient then Exit;
1004 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) and
1005 ByteBool(Item.Options and ITEM_OPTION_ONLYDM) then
1006 Exit;
1008 g_Items_Create(Item.X, Item.Y, Item.ItemType, ByteBool(Item.Options and ITEM_OPTION_FALL),
1009 gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF, GM_COOP]);
1010 end;
1012 procedure CreateArea(Area: TAreaRec_1);
1013 var
1014 a: Integer;
1015 id: DWORD;
1016 begin
1017 case Area.AreaType of
1018 AREA_DMPOINT, AREA_PLAYERPOINT1, AREA_PLAYERPOINT2,
1019 AREA_REDTEAMPOINT, AREA_BLUETEAMPOINT:
1020 begin
1021 SetLength(RespawnPoints, Length(RespawnPoints)+1);
1022 with RespawnPoints[High(RespawnPoints)] do
1023 begin
1024 X := Area.X;
1025 Y := Area.Y;
1026 Direction := TDirection(Area.Direction);
1028 case Area.AreaType of
1029 AREA_DMPOINT: PointType := RESPAWNPOINT_DM;
1030 AREA_PLAYERPOINT1: PointType := RESPAWNPOINT_PLAYER1;
1031 AREA_PLAYERPOINT2: PointType := RESPAWNPOINT_PLAYER2;
1032 AREA_REDTEAMPOINT: PointType := RESPAWNPOINT_RED;
1033 AREA_BLUETEAMPOINT: PointType := RESPAWNPOINT_BLUE;
1034 end;
1035 end;
1036 end;
1038 AREA_REDFLAG, AREA_BLUEFLAG:
1039 begin
1040 if Area.AreaType = AREA_REDFLAG then a := FLAG_RED else a := FLAG_BLUE;
1042 if FlagPoints[a] <> nil then Exit;
1044 New(FlagPoints[a]);
1046 with FlagPoints[a]^ do
1047 begin
1048 X := Area.X-FLAGRECT.X;
1049 Y := Area.Y-FLAGRECT.Y;
1050 Direction := TDirection(Area.Direction);
1051 end;
1053 with gFlags[a] do
1054 begin
1055 case a of
1056 FLAG_RED: g_Frames_Get(id, 'FRAMES_FLAG_RED');
1057 FLAG_BLUE: g_Frames_Get(id, 'FRAMES_FLAG_BLUE');
1058 end;
1060 Animation := TAnimation.Create(id, True, 8);
1061 Obj.Rect := FLAGRECT;
1063 g_Map_ResetFlag(a);
1064 end;
1065 end;
1067 AREA_DOMFLAG:
1068 begin
1069 {SetLength(DOMFlagPoints, Length(DOMFlagPoints)+1);
1070 with DOMFlagPoints[High(DOMFlagPoints)] do
1071 begin
1072 X := Area.X;
1073 Y := Area.Y;
1074 Direction := TDirection(Area.Direction);
1075 end;
1077 g_Map_CreateFlag(DOMFlagPoints[High(DOMFlagPoints)], FLAG_DOM, FLAG_STATE_NORMAL);}
1078 end;
1079 end;
1080 end;
1082 procedure CreateTrigger(Trigger: TTriggerRec_1; fTexturePanel1Type, fTexturePanel2Type: Word);
1083 var
1084 _trigger: TTrigger;
1085 begin
1086 if g_Game_IsClient and not (Trigger.TriggerType in [TRIGGER_SOUND, TRIGGER_MUSIC]) then Exit;
1088 with _trigger do
1089 begin
1090 X := Trigger.X;
1091 Y := Trigger.Y;
1092 Width := Trigger.Width;
1093 Height := Trigger.Height;
1094 Enabled := ByteBool(Trigger.Enabled);
1095 TexturePanel := Trigger.TexturePanel;
1096 TexturePanelType := fTexturePanel1Type;
1097 ShotPanelType := fTexturePanel2Type;
1098 TriggerType := Trigger.TriggerType;
1099 ActivateType := Trigger.ActivateType;
1100 Keys := Trigger.Keys;
1101 Data.Default := Trigger.DATA;
1102 end;
1104 g_Triggers_Create(_trigger);
1105 end;
1107 procedure CreateMonster(monster: TMonsterRec_1);
1108 var
1109 a: Integer;
1110 mon: TMonster;
1111 begin
1112 if g_Game_IsClient then Exit;
1114 if (gGameSettings.GameType = GT_SINGLE)
1115 or LongBool(gGameSettings.Options and GAME_OPTION_MONSTERS) then
1116 begin
1117 mon := g_Monsters_Create(monster.MonsterType, monster.X, monster.Y, TDirection(monster.Direction));
1119 if gTriggers <> nil then
1120 begin
1121 for a := 0 to High(gTriggers) do
1122 begin
1123 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1124 begin
1125 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1126 end;
1127 end;
1128 end;
1130 if monster.MonsterType <> MONSTER_BARREL then Inc(gTotalMonsters);
1131 end;
1132 end;
1134 procedure g_Map_ReAdd_DieTriggers();
1136 function monsDieTrig (mon: TMonster): Boolean;
1137 var
1138 a: Integer;
1139 begin
1140 result := false; // don't stop
1141 mon.ClearTriggers();
1142 for a := 0 to High(gTriggers) do
1143 begin
1144 if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
1145 begin
1146 if (gTriggers[a].Data.MonsterID-1) = mon.StartID then mon.AddTrigger(a);
1147 end;
1148 end;
1149 end;
1151 begin
1152 if g_Game_IsClient then Exit;
1154 g_Mons_ForEach(monsDieTrig);
1155 end;
1157 function extractWadName(resourceName: string): string;
1158 var
1159 posN: Integer;
1160 begin
1161 posN := Pos(':', resourceName);
1162 if posN > 0 then
1163 Result:= Copy(resourceName, 0, posN-1)
1164 else
1165 Result := '';
1166 end;
1168 procedure addResToExternalResList(res: string);
1169 begin
1170 res := extractWadName(res);
1171 if (res <> '') and (gExternalResources.IndexOf(res) = -1) then
1172 gExternalResources.Add(res);
1173 end;
1175 procedure generateExternalResourcesList(mapReader: TMapReader_1);
1176 var
1177 textures: TTexturesRec1Array;
1178 mapHeader: TMapHeaderRec_1;
1179 i: integer;
1180 resFile: String = '';
1181 begin
1182 if gExternalResources = nil then
1183 gExternalResources := TStringList.Create;
1185 gExternalResources.Clear;
1186 textures := mapReader.GetTextures();
1187 for i := 0 to High(textures) do
1188 begin
1189 addResToExternalResList(resFile);
1190 end;
1192 textures := nil;
1194 mapHeader := mapReader.GetMapHeader;
1196 addResToExternalResList(mapHeader.MusicName);
1197 addResToExternalResList(mapHeader.SkyName);
1198 end;
1200 procedure mapCreateGrid ();
1201 var
1202 mapX0: Integer = $3fffffff;
1203 mapY0: Integer = $3fffffff;
1204 mapX1: Integer = -$3fffffff;
1205 mapY1: Integer = -$3fffffff;
1207 procedure calcBoundingBox (constref panels: TPanelArray);
1208 var
1209 idx: Integer;
1210 pan: TPanel;
1211 begin
1212 for idx := 0 to High(panels) do
1213 begin
1214 pan := panels[idx];
1215 if not pan.visvalid then continue;
1216 if (pan.Width < 1) or (pan.Height < 1) then continue;
1217 if (mapX0 > pan.x0) then mapX0 := pan.x0;
1218 if (mapY0 > pan.y0) then mapY0 := pan.y0;
1219 if (mapX1 < pan.x1) then mapX1 := pan.x1;
1220 if (mapY1 < pan.y1) then mapY1 := pan.y1;
1221 end;
1222 end;
1224 procedure addPanelsToGrid (constref panels: TPanelArray; tag: Integer);
1225 var
1226 idx: Integer;
1227 pan: TPanel;
1228 begin
1229 tag := panelTypeToTag(tag);
1230 for idx := High(panels) downto 0 do
1231 begin
1232 pan := panels[idx];
1233 pan.tag := tag;
1234 if not pan.visvalid then continue;
1235 gMapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag);
1236 mapTree.insertObject(pan, tag, true); // as static object
1237 end;
1238 end;
1240 begin
1241 gMapGrid.Free();
1242 gMapGrid := nil;
1243 mapTree.Free();
1244 mapTree := nil;
1246 calcBoundingBox(gWalls);
1247 calcBoundingBox(gRenderBackgrounds);
1248 calcBoundingBox(gRenderForegrounds);
1249 calcBoundingBox(gWater);
1250 calcBoundingBox(gAcid1);
1251 calcBoundingBox(gAcid2);
1252 calcBoundingBox(gSteps);
1253 calcBoundingBox(gLifts);
1254 calcBoundingBox(gBlockMon);
1256 e_WriteLog(Format('map dimensions: (%d,%d)-(%d,%d)', [mapX0, mapY0, mapX1, mapY1]), MSG_WARNING);
1258 gMapGrid := TPanelGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
1259 mapTree := TDynAABBTreeMap.Create();
1261 addPanelsToGrid(gWalls, PANEL_WALL);
1262 addPanelsToGrid(gWalls, PANEL_CLOSEDOOR);
1263 addPanelsToGrid(gWalls, PANEL_OPENDOOR);
1264 addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
1265 addPanelsToGrid(gRenderForegrounds, PANEL_FORE);
1266 addPanelsToGrid(gWater, PANEL_WATER);
1267 addPanelsToGrid(gAcid1, PANEL_ACID1);
1268 addPanelsToGrid(gAcid2, PANEL_ACID2);
1269 addPanelsToGrid(gSteps, PANEL_STEP);
1270 addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
1271 addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
1273 gMapGrid.dumpStats();
1274 e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
1275 mapTree.forEachLeaf(nil);
1276 end;
1278 function g_Map_Load(Res: String): Boolean;
1279 const
1280 DefaultMusRes = 'Standart.wad:STDMUS\MUS1';
1281 DefaultSkyRes = 'Standart.wad:STDSKY\SKY0';
1282 var
1283 WAD: TWADFile;
1284 MapReader: TMapReader_1;
1285 Header: TMapHeaderRec_1;
1286 _textures: TTexturesRec1Array;
1287 _texnummap: array of Integer; // `_textures` -> `Textures`
1288 panels: TPanelsRec1Array;
1289 items: TItemsRec1Array;
1290 monsters: TMonsterRec1Array;
1291 areas: TAreasRec1Array;
1292 triggers: TTriggersRec1Array;
1293 a, b, c, k: Integer;
1294 PanelID: DWORD;
1295 AddTextures: TAddTextureArray;
1296 texture: TTextureRec_1;
1297 TriggersTable: Array of record
1298 TexturePanel: Integer;
1299 LiftPanel: Integer;
1300 DoorPanel: Integer;
1301 ShotPanel: Integer;
1302 end;
1303 FileName, mapResName, s, TexName: String;
1304 Data: Pointer;
1305 Len: Integer;
1306 ok, isAnim, trigRef: Boolean;
1307 CurTex, ntn: Integer;
1309 begin
1310 gMapGrid.Free();
1311 gMapGrid := nil;
1312 mapTree.Free();
1313 mapTree := nil;
1315 Result := False;
1316 gMapInfo.Map := Res;
1317 TriggersTable := nil;
1318 FillChar(texture, SizeOf(texture), 0);
1320 sfsGCDisable(); // temporary disable removing of temporary volumes
1321 try
1322 // Çàãðóçêà WAD:
1323 FileName := g_ExtractWadName(Res);
1324 e_WriteLog('Loading map WAD: '+FileName, MSG_NOTIFY);
1325 g_Game_SetLoadingText(_lc[I_LOAD_WAD_FILE], 0, False);
1327 WAD := TWADFile.Create();
1328 if not WAD.ReadFile(FileName) then
1329 begin
1330 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_WAD], [FileName]));
1331 WAD.Free();
1332 Exit;
1333 end;
1334 //k8: why loader ignores path here?
1335 mapResName := g_ExtractFileName(Res);
1336 if not WAD.GetMapResource(mapResName, Data, Len) then
1337 begin
1338 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_RES], [mapResName]));
1339 WAD.Free();
1340 Exit;
1341 end;
1343 WAD.Free();
1345 // Çàãðóçêà êàðòû:
1346 e_WriteLog('Loading map: '+mapResName, MSG_NOTIFY);
1347 g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
1348 MapReader := TMapReader_1.Create();
1350 if not MapReader.LoadMap(Data) then
1351 begin
1352 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]));
1353 FreeMem(Data);
1354 MapReader.Free();
1355 Exit;
1356 end;
1358 FreeMem(Data);
1359 generateExternalResourcesList(MapReader);
1360 // Çàãðóçêà òåêñòóð:
1361 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], 0, False);
1362 _textures := MapReader.GetTextures();
1363 _texnummap := nil;
1365 // Äîáàâëåíèå òåêñòóð â Textures[]:
1366 if _textures <> nil then
1367 begin
1368 e_WriteLog(' Loading textures:', MSG_NOTIFY);
1369 g_Game_SetLoadingText(_lc[I_LOAD_TEXTURES], High(_textures), False);
1370 SetLength(_texnummap, length(_textures));
1372 for a := 0 to High(_textures) do
1373 begin
1374 SetLength(s, 64);
1375 CopyMemory(@s[1], @_textures[a].Resource[0], 64);
1376 for b := 1 to Length(s) do
1377 if s[b] = #0 then
1378 begin
1379 SetLength(s, b-1);
1380 Break;
1381 end;
1382 e_WriteLog(Format(' Loading texture #%d: %s', [a, s]), MSG_NOTIFY);
1383 //if g_Map_IsSpecialTexture(s) then e_WriteLog(' SPECIAL!', MSG_NOTIFY);
1384 // Àíèìèðîâàííàÿ òåêñòóðà:
1385 if ByteBool(_textures[a].Anim) then
1386 begin
1387 ntn := CreateAnimTexture(_textures[a].Resource, FileName, True);
1388 if ntn < 0 then
1389 begin
1390 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_ANIM], [s]));
1391 ntn := CreateNullTexture(_textures[a].Resource);
1392 end;
1393 end
1394 else // Îáû÷íàÿ òåêñòóðà:
1395 ntn := CreateTexture(_textures[a].Resource, FileName, True);
1396 if ntn < 0 then
1397 begin
1398 g_SimpleError(Format(_lc[I_GAME_ERROR_TEXTURE_SIMPLE], [s]));
1399 ntn := CreateNullTexture(_textures[a].Resource);
1400 end;
1402 _texnummap[a] := ntn; // fix texture number
1403 g_Game_StepLoading();
1404 end;
1405 end;
1407 // Çàãðóçêà òðèããåðîâ:
1408 gTriggerClientID := 0;
1409 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1410 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS], 0, False);
1411 triggers := MapReader.GetTriggers();
1413 // Çàãðóçêà ïàíåëåé:
1414 e_WriteLog(' Loading panels...', MSG_NOTIFY);
1415 g_Game_SetLoadingText(_lc[I_LOAD_PANELS], 0, False);
1416 panels := MapReader.GetPanels();
1418 // check texture numbers for panels
1419 for a := 0 to High(panels) do
1420 begin
1421 if panels[a].TextureNum > High(_textures) then
1422 begin
1423 e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
1424 result := false;
1425 exit;
1426 end;
1427 panels[a].TextureNum := _texnummap[panels[a].TextureNum];
1428 end;
1430 // Ñîçäàíèå òàáëèöû òðèããåðîâ (ñîîòâåòñòâèå ïàíåëåé òðèããåðàì):
1431 if triggers <> nil then
1432 begin
1433 e_WriteLog(' Setting up trigger table...', MSG_NOTIFY);
1434 SetLength(TriggersTable, Length(triggers));
1435 g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], High(TriggersTable), False);
1437 for a := 0 to High(TriggersTable) do
1438 begin
1439 // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè):
1440 TriggersTable[a].TexturePanel := triggers[a].TexturePanel;
1441 // Ëèôòû:
1442 if triggers[a].TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
1443 TriggersTable[a].LiftPanel := TTriggerData(triggers[a].DATA).PanelID
1444 else
1445 TriggersTable[a].LiftPanel := -1;
1446 // Äâåðè:
1447 if triggers[a].TriggerType in [TRIGGER_OPENDOOR,
1448 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
1449 TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
1450 TriggersTable[a].DoorPanel := TTriggerData(triggers[a].DATA).PanelID
1451 else
1452 TriggersTable[a].DoorPanel := -1;
1453 // Òóðåëü:
1454 if triggers[a].TriggerType = TRIGGER_SHOT then
1455 TriggersTable[a].ShotPanel := TTriggerData(triggers[a].DATA).ShotPanelID
1456 else
1457 TriggersTable[a].ShotPanel := -1;
1459 g_Game_StepLoading();
1460 end;
1461 end;
1463 // Ñîçäàåì ïàíåëè:
1464 if panels <> nil then
1465 begin
1466 e_WriteLog(' Setting up trigger links...', MSG_NOTIFY);
1467 g_Game_SetLoadingText(_lc[I_LOAD_LINK_TRIGGERS], High(panels), False);
1469 for a := 0 to High(panels) do
1470 begin
1471 SetLength(AddTextures, 0);
1472 trigRef := False;
1473 CurTex := -1;
1474 if _textures <> nil then
1475 begin
1476 texture := _textures[panels[a].TextureNum];
1477 ok := True;
1478 end
1479 else
1480 ok := False;
1482 if ok then
1483 begin
1484 // Ñìîòðèì, ññûëàþòñÿ ëè íà ýòó ïàíåëü òðèããåðû.
1485 // Åñëè äà - òî íàäî ñîçäàòü åùå òåêñòóð:
1486 ok := False;
1487 if (TriggersTable <> nil) and (_textures <> nil) then
1488 for b := 0 to High(TriggersTable) do
1489 if (TriggersTable[b].TexturePanel = a)
1490 or (TriggersTable[b].ShotPanel = a) then
1491 begin
1492 trigRef := True;
1493 ok := True;
1494 Break;
1495 end;
1496 end;
1498 if ok then
1499 begin // Åñòü ññûëêè òðèããåðîâ íà ýòó ïàíåëü
1500 SetLength(s, 64);
1501 CopyMemory(@s[1], @texture.Resource[0], 64);
1502 // Èçìåðÿåì äëèíó:
1503 Len := Length(s);
1504 for c := Len downto 1 do
1505 if s[c] <> #0 then
1506 begin
1507 Len := c;
1508 Break;
1509 end;
1510 SetLength(s, Len);
1512 // Ñïåö-òåêñòóðû çàïðåùåíû:
1513 if g_Map_IsSpecialTexture(s) then
1514 ok := False
1515 else
1516 // Îïðåäåëÿåì íàëè÷èå è ïîëîæåíèå öèôð â êîíöå ñòðîêè:
1517 ok := g_Texture_NumNameFindStart(s);
1519 // Åñëè ok, çíà÷èò åñòü öèôðû â êîíöå.
1520 // Çàãðóæàåì òåêñòóðû ñ îñòàëüíûìè #:
1521 if ok then
1522 begin
1523 k := NNF_NAME_BEFORE;
1524 // Öèêë ïî èçìåíåíèþ èìåíè òåêñòóðû:
1525 while ok or (k = NNF_NAME_BEFORE) or
1526 (k = NNF_NAME_EQUALS) do
1527 begin
1528 k := g_Texture_NumNameFindNext(TexName);
1530 if (k = NNF_NAME_BEFORE) or
1531 (k = NNF_NAME_AFTER) then
1532 begin
1533 // Ïðîáóåì äîáàâèòü íîâóþ òåêñòóðó:
1534 if ByteBool(texture.Anim) then
1535 begin // Íà÷àëüíàÿ - àíèìèðîâàííàÿ, èùåì àíèìèðîâàííóþ
1536 isAnim := True;
1537 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1538 if not ok then
1539 begin // Íåò àíèìèðîâàííîé, èùåì îáû÷íóþ
1540 isAnim := False;
1541 ok := CreateTexture(TexName, FileName, False) >= 0;
1542 end;
1543 end
1544 else
1545 begin // Íà÷àëüíàÿ - îáû÷íàÿ, èùåì îáû÷íóþ
1546 isAnim := False;
1547 ok := CreateTexture(TexName, FileName, False) >= 0;
1548 if not ok then
1549 begin // Íåò îáû÷íîé, èùåì àíèìèðîâàííóþ
1550 isAnim := True;
1551 ok := CreateAnimTexture(TexName, FileName, False) >= 0;
1552 end;
1553 end;
1555 // Îíà ñóùåñòâóåò. Çàíîñèì åå ID â ñïèñîê ïàíåëè:
1556 if ok then
1557 begin
1558 for c := 0 to High(Textures) do
1559 if Textures[c].TextureName = TexName then
1560 begin
1561 SetLength(AddTextures, Length(AddTextures)+1);
1562 AddTextures[High(AddTextures)].Texture := c;
1563 AddTextures[High(AddTextures)].Anim := isAnim;
1564 Break;
1565 end;
1566 end;
1567 end
1568 else
1569 if k = NNF_NAME_EQUALS then
1570 begin
1571 // Çàíîñèì òåêóùóþ òåêñòóðó íà ñâîå ìåñòî:
1572 SetLength(AddTextures, Length(AddTextures)+1);
1573 AddTextures[High(AddTextures)].Texture := panels[a].TextureNum;
1574 AddTextures[High(AddTextures)].Anim := ByteBool(texture.Anim);
1575 CurTex := High(AddTextures);
1576 ok := True;
1577 end
1578 else // NNF_NO_NAME
1579 ok := False;
1580 end; // while ok...
1582 ok := True;
1583 end; // if ok - åñòü ñìåæíûå òåêñòóðû
1584 end; // if ok - ññûëàþòñÿ òðèããåðû
1586 if not ok then
1587 begin
1588 // Çàíîñèì òîëüêî òåêóùóþ òåêñòóðó:
1589 SetLength(AddTextures, 1);
1590 AddTextures[0].Texture := panels[a].TextureNum;
1591 AddTextures[0].Anim := ByteBool(texture.Anim);
1592 CurTex := 0;
1593 end;
1595 //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);
1597 // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå íîìåð:
1598 PanelID := CreatePanel(panels[a], AddTextures, CurTex, trigRef);
1600 // Åñëè èñïîëüçóåòñÿ â òðèããåðàõ, òî ñòàâèì òî÷íûé ID:
1601 if TriggersTable <> nil then
1602 for b := 0 to High(TriggersTable) do
1603 begin
1604 // Òðèããåð äâåðè/ëèôòà:
1605 if (TriggersTable[b].LiftPanel = a) or
1606 (TriggersTable[b].DoorPanel = a) then
1607 TTriggerData(triggers[b].DATA).PanelID := PanelID;
1608 // Òðèããåð ñìåíû òåêñòóðû:
1609 if TriggersTable[b].TexturePanel = a then
1610 triggers[b].TexturePanel := PanelID;
1611 // Òðèããåð "Òóðåëü":
1612 if TriggersTable[b].ShotPanel = a then
1613 TTriggerData(triggers[b].DATA).ShotPanelID := PanelID;
1614 end;
1616 g_Game_StepLoading();
1617 end;
1618 end;
1620 // Åñëè íå LoadState, òî ñîçäàåì òðèããåðû:
1621 if (triggers <> nil) and not gLoadGameMode then
1622 begin
1623 e_WriteLog(' Creating triggers...', MSG_NOTIFY);
1624 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_TRIGGERS], 0, False);
1625 // Óêàçûâàåì òèï ïàíåëè, åñëè åñòü:
1626 for a := 0 to High(triggers) do
1627 begin
1628 if triggers[a].TexturePanel <> -1 then
1629 b := panels[TriggersTable[a].TexturePanel].PanelType
1630 else
1631 b := 0;
1632 if (triggers[a].TriggerType = TRIGGER_SHOT) and
1633 (TTriggerData(triggers[a].DATA).ShotPanelID <> -1) then
1634 c := panels[TriggersTable[a].ShotPanel].PanelType
1635 else
1636 c := 0;
1637 CreateTrigger(triggers[a], b, c);
1638 end;
1639 end;
1641 // Çàãðóçêà ïðåäìåòîâ:
1642 e_WriteLog(' Loading triggers...', MSG_NOTIFY);
1643 g_Game_SetLoadingText(_lc[I_LOAD_ITEMS], 0, False);
1644 items := MapReader.GetItems();
1646 // Åñëè íå LoadState, òî ñîçäàåì ïðåäìåòû:
1647 if (items <> nil) and not gLoadGameMode then
1648 begin
1649 e_WriteLog(' Spawning items...', MSG_NOTIFY);
1650 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_ITEMS], 0, False);
1651 for a := 0 to High(items) do
1652 CreateItem(Items[a]);
1653 end;
1655 // Çàãðóçêà îáëàñòåé:
1656 e_WriteLog(' Loading areas...', MSG_NOTIFY);
1657 g_Game_SetLoadingText(_lc[I_LOAD_AREAS], 0, False);
1658 areas := MapReader.GetAreas();
1660 // Åñëè íå LoadState, òî ñîçäàåì îáëàñòè:
1661 if areas <> nil then
1662 begin
1663 e_WriteLog(' Creating areas...', MSG_NOTIFY);
1664 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_AREAS], 0, False);
1665 for a := 0 to High(areas) do
1666 CreateArea(areas[a]);
1667 end;
1669 // Çàãðóçêà ìîíñòðîâ:
1670 e_WriteLog(' Loading monsters...', MSG_NOTIFY);
1671 g_Game_SetLoadingText(_lc[I_LOAD_MONSTERS], 0, False);
1672 monsters := MapReader.GetMonsters();
1674 gTotalMonsters := 0;
1676 // Åñëè íå LoadState, òî ñîçäàåì ìîíñòðîâ:
1677 if (monsters <> nil) and not gLoadGameMode then
1678 begin
1679 e_WriteLog(' Spawning monsters...', MSG_NOTIFY);
1680 g_Game_SetLoadingText(_lc[I_LOAD_CREATE_MONSTERS], 0, False);
1681 for a := 0 to High(monsters) do
1682 CreateMonster(monsters[a]);
1683 end;
1685 // Çàãðóçêà îïèñàíèÿ êàðòû:
1686 e_WriteLog(' Reading map info...', MSG_NOTIFY);
1687 g_Game_SetLoadingText(_lc[I_LOAD_MAP_HEADER], 0, False);
1688 Header := MapReader.GetMapHeader();
1690 MapReader.Free();
1692 with gMapInfo do
1693 begin
1694 Name := Header.MapName;
1695 Description := Header.MapDescription;
1696 Author := Header.MapAuthor;
1697 MusicName := Header.MusicName;
1698 SkyName := Header.SkyName;
1699 Height := Header.Height;
1700 Width := Header.Width;
1701 end;
1703 // Çàãðóçêà íåáà:
1704 if gMapInfo.SkyName <> '' then
1705 begin
1706 e_WriteLog(' Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
1707 g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
1708 FileName := g_ExtractWadName(gMapInfo.SkyName);
1710 if FileName <> '' then
1711 FileName := GameDir+'/wads/'+FileName
1712 else
1713 begin
1714 FileName := g_ExtractWadName(Res);
1715 end;
1717 s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
1718 if g_Texture_CreateWAD(BackID, s) then
1719 begin
1720 g_Game_SetupScreenSize();
1721 end
1722 else
1723 g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
1724 end;
1726 // Çàãðóçêà ìóçûêè:
1727 ok := False;
1728 if gMapInfo.MusicName <> '' then
1729 begin
1730 e_WriteLog(' Loading music: ' + gMapInfo.MusicName, MSG_NOTIFY);
1731 g_Game_SetLoadingText(_lc[I_LOAD_MUSIC], 0, False);
1732 FileName := g_ExtractWadName(gMapInfo.MusicName);
1734 if FileName <> '' then
1735 FileName := GameDir+'/wads/'+FileName
1736 else
1737 begin
1738 FileName := g_ExtractWadName(Res);
1739 end;
1741 s := FileName+':'+g_ExtractFilePathName(gMapInfo.MusicName);
1742 if g_Sound_CreateWADEx(gMapInfo.MusicName, s, True) then
1743 ok := True
1744 else
1745 g_FatalError(Format(_lc[I_GAME_ERROR_MUSIC], [s]));
1746 end;
1748 // Îñòàëüíûå óñòàíâêè:
1749 CreateDoorMap();
1750 CreateLiftMap();
1752 g_Items_Init();
1753 g_Weapon_Init();
1754 g_Monsters_Init();
1756 // Åñëè íå LoadState, òî ñîçäàåì êàðòó ñòîëêíîâåíèé:
1757 if not gLoadGameMode then
1758 g_GFX_Init();
1760 // Ñáðîñ ëîêàëüíûõ ìàññèâîâ:
1761 _textures := nil;
1762 panels := nil;
1763 items := nil;
1764 areas := nil;
1765 triggers := nil;
1766 TriggersTable := nil;
1767 AddTextures := nil;
1769 // Âêëþ÷àåì ìóçûêó, åñëè ýòî íå çàãðóçêà:
1770 if ok and (not gLoadGameMode) then
1771 begin
1772 gMusic.SetByName(gMapInfo.MusicName);
1773 gMusic.Play();
1774 end
1775 else
1776 gMusic.SetByName('');
1777 finally
1778 sfsGCEnable(); // enable releasing unused volumes
1779 end;
1781 e_WriteLog('Creating map grid', MSG_NOTIFY);
1782 mapCreateGrid();
1784 e_WriteLog('Done loading map.', MSG_NOTIFY);
1785 Result := True;
1786 end;
1788 function g_Map_GetMapInfo(Res: String): TMapInfo;
1789 var
1790 WAD: TWADFile;
1791 MapReader: TMapReader_1;
1792 Header: TMapHeaderRec_1;
1793 FileName: String;
1794 Data: Pointer;
1795 Len: Integer;
1796 begin
1797 FillChar(Result, SizeOf(Result), 0);
1798 FileName := g_ExtractWadName(Res);
1800 WAD := TWADFile.Create();
1801 if not WAD.ReadFile(FileName) then
1802 begin
1803 WAD.Free();
1804 Exit;
1805 end;
1807 //k8: it ignores path again
1808 if not WAD.GetMapResource(g_ExtractFileName(Res), Data, Len) then
1809 begin
1810 WAD.Free();
1811 Exit;
1812 end;
1814 WAD.Free();
1816 MapReader := TMapReader_1.Create();
1818 if not MapReader.LoadMap(Data) then
1819 begin
1820 g_Console_Add(Format(_lc[I_GAME_ERROR_MAP_LOAD], [Res]), True);
1821 ZeroMemory(@Header, SizeOf(Header));
1822 Result.Name := _lc[I_GAME_ERROR_MAP_SELECT];
1823 Result.Description := _lc[I_GAME_ERROR_MAP_SELECT];
1824 end
1825 else
1826 begin
1827 Header := MapReader.GetMapHeader();
1828 Result.Name := Header.MapName;
1829 Result.Description := Header.MapDescription;
1830 end;
1832 FreeMem(Data);
1833 MapReader.Free();
1835 Result.Map := Res;
1836 Result.Author := Header.MapAuthor;
1837 Result.Height := Header.Height;
1838 Result.Width := Header.Width;
1839 end;
1841 function g_Map_GetMapsList(WADName: string): SArray;
1842 var
1843 WAD: TWADFile;
1844 a: Integer;
1845 ResList: SArray;
1846 begin
1847 Result := nil;
1848 WAD := TWADFile.Create();
1849 if not WAD.ReadFile(WADName) then
1850 begin
1851 WAD.Free();
1852 Exit;
1853 end;
1854 ResList := WAD.GetMapResources();
1855 if ResList <> nil then
1856 begin
1857 for a := 0 to High(ResList) do
1858 begin
1859 SetLength(Result, Length(Result)+1);
1860 Result[High(Result)] := ResList[a];
1861 end;
1862 end;
1863 WAD.Free();
1864 end;
1866 function g_Map_Exist(Res: string): Boolean;
1867 var
1868 WAD: TWADFile;
1869 FileName, mnn: string;
1870 ResList: SArray;
1871 a: Integer;
1872 begin
1873 Result := False;
1875 FileName := addWadExtension(g_ExtractWadName(Res));
1877 WAD := TWADFile.Create;
1878 if not WAD.ReadFile(FileName) then
1879 begin
1880 WAD.Free();
1881 Exit;
1882 end;
1884 ResList := WAD.GetMapResources();
1885 WAD.Free();
1887 mnn := g_ExtractFileName(Res);
1888 if ResList <> nil then
1889 for a := 0 to High(ResList) do if StrEquCI1251(ResList[a], mnn) then
1890 begin
1891 Result := True;
1892 Exit;
1893 end;
1894 end;
1896 procedure g_Map_Free();
1897 var
1898 a: Integer;
1900 procedure FreePanelArray(var panels: TPanelArray);
1901 var
1902 i: Integer;
1904 begin
1905 if panels <> nil then
1906 begin
1907 for i := 0 to High(panels) do
1908 panels[i].Free();
1909 panels := nil;
1910 end;
1911 end;
1913 begin
1914 g_GFX_Free();
1915 g_Weapon_Free();
1916 g_Items_Free();
1917 g_Triggers_Free();
1918 g_Monsters_Free();
1920 RespawnPoints := nil;
1921 if FlagPoints[FLAG_RED] <> nil then
1922 begin
1923 Dispose(FlagPoints[FLAG_RED]);
1924 FlagPoints[FLAG_RED] := nil;
1925 end;
1926 if FlagPoints[FLAG_BLUE] <> nil then
1927 begin
1928 Dispose(FlagPoints[FLAG_BLUE]);
1929 FlagPoints[FLAG_BLUE] := nil;
1930 end;
1931 //DOMFlagPoints := nil;
1933 //gDOMFlags := nil;
1935 if Textures <> nil then
1936 begin
1937 for a := 0 to High(Textures) do
1938 if not g_Map_IsSpecialTexture(Textures[a].TextureName) then
1939 if Textures[a].Anim then
1940 g_Frames_DeleteByID(Textures[a].FramesID)
1941 else
1942 if Textures[a].TextureID <> TEXTURE_NONE then
1943 e_DeleteTexture(Textures[a].TextureID);
1945 Textures := nil;
1946 end;
1948 FreePanelArray(gWalls);
1949 FreePanelArray(gRenderBackgrounds);
1950 FreePanelArray(gRenderForegrounds);
1951 FreePanelArray(gWater);
1952 FreePanelArray(gAcid1);
1953 FreePanelArray(gAcid2);
1954 FreePanelArray(gSteps);
1955 FreePanelArray(gLifts);
1956 FreePanelArray(gBlockMon);
1958 if BackID <> DWORD(-1) then
1959 begin
1960 gBackSize.X := 0;
1961 gBackSize.Y := 0;
1962 e_DeleteTexture(BackID);
1963 BackID := DWORD(-1);
1964 end;
1966 g_Game_StopAllSounds(False);
1967 gMusic.FreeSound();
1968 g_Sound_Delete(gMapInfo.MusicName);
1970 gMapInfo.Name := '';
1971 gMapInfo.Description := '';
1972 gMapInfo.MusicName := '';
1973 gMapInfo.Height := 0;
1974 gMapInfo.Width := 0;
1976 gDoorMap := nil;
1977 gLiftMap := nil;
1979 PanelByID := nil;
1980 end;
1982 procedure g_Map_Update();
1983 var
1984 a, d, j: Integer;
1985 m: Word;
1986 s: String;
1988 procedure UpdatePanelArray(var panels: TPanelArray);
1989 var
1990 i: Integer;
1992 begin
1993 if panels <> nil then
1994 for i := 0 to High(panels) do
1995 panels[i].Update();
1996 end;
1998 begin
1999 UpdatePanelArray(gWalls);
2000 UpdatePanelArray(gRenderBackgrounds);
2001 UpdatePanelArray(gRenderForegrounds);
2002 UpdatePanelArray(gWater);
2003 UpdatePanelArray(gAcid1);
2004 UpdatePanelArray(gAcid2);
2005 UpdatePanelArray(gSteps);
2007 if gGameSettings.GameMode = GM_CTF then
2008 begin
2009 for a := FLAG_RED to FLAG_BLUE do
2010 if not (gFlags[a].State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
2011 with gFlags[a] do
2012 begin
2013 if gFlags[a].Animation <> nil then
2014 gFlags[a].Animation.Update();
2016 m := g_Obj_Move(@Obj, True, True);
2018 if gTime mod (GAME_TICK*2) <> 0 then
2019 Continue;
2021 // Ñîïðîòèâëåíèå âîçäóõà:
2022 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
2024 // Òàéìàóò ïîòåðÿííîãî ôëàãà, ëèáî îí âûïàë çà êàðòó:
2025 if ((Count = 0) or ByteBool(m and MOVE_FALLOUT)) and g_Game_IsServer then
2026 begin
2027 g_Map_ResetFlag(a);
2028 gFlags[a].CaptureTime := 0;
2029 if a = FLAG_RED then
2030 s := _lc[I_PLAYER_FLAG_RED]
2031 else
2032 s := _lc[I_PLAYER_FLAG_BLUE];
2033 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2035 if g_Game_IsNet then
2036 MH_SEND_FlagEvent(FLAG_STATE_RETURNED, a, 0);
2037 Continue;
2038 end;
2040 if Count > 0 then
2041 Count := Count - 1;
2043 // Èãðîê áåðåò ôëàã:
2044 if gPlayers <> nil then
2045 begin
2046 j := Random(Length(gPlayers)) - 1;
2048 for d := 0 to High(gPlayers) do
2049 begin
2050 Inc(j);
2051 if j > High(gPlayers) then
2052 j := 0;
2054 if gPlayers[j] <> nil then
2055 if gPlayers[j].Live and
2056 g_Obj_Collide(@Obj, @gPlayers[j].Obj) then
2057 begin
2058 if gPlayers[j].GetFlag(a) then
2059 Break;
2060 end;
2061 end;
2062 end;
2063 end;
2064 end;
2065 end;
2068 // old algo
2069 procedure g_Map_DrawPanels (PanelType: Word);
2071 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
2072 var
2073 idx: Integer;
2074 begin
2075 if (panels <> nil) then
2076 begin
2077 // alas, no visible set
2078 for idx := 0 to High(panels) do
2079 begin
2080 if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
2081 end;
2082 end;
2083 end;
2085 begin
2086 case PanelType of
2087 PANEL_WALL: DrawPanels(gWalls);
2088 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
2089 PANEL_BACK: DrawPanels(gRenderBackgrounds);
2090 PANEL_FORE: DrawPanels(gRenderForegrounds);
2091 PANEL_WATER: DrawPanels(gWater);
2092 PANEL_ACID1: DrawPanels(gAcid1);
2093 PANEL_ACID2: DrawPanels(gAcid2);
2094 PANEL_STEP: DrawPanels(gSteps);
2095 end;
2096 end;
2099 // new algo
2100 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
2101 function checker (pan: TPanel; tag: Integer): Boolean;
2102 begin
2103 result := false; // don't stop, ever
2104 if ((tag and GridTagDoor) <> 0) <> pan.Door then exit;
2105 gDrawPanelList.insert(pan);
2106 end;
2108 begin
2109 dplClear();
2110 //tagmask := panelTypeToTag(PanelType);
2112 if gdbg_map_use_tree_draw then
2113 begin
2114 mapTree.aabbQuery(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2115 end
2116 else
2117 begin
2118 gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore));
2119 end;
2120 // list will be rendered in `g_game.DrawPlayer()`
2121 end;
2124 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
2125 function checker (pan: TPanel; tag: Integer): Boolean;
2126 begin
2127 result := false; // don't stop, ever
2128 pan.DrawShadowVolume(lightX, lightY, radius);
2129 end;
2131 begin
2132 if gdbg_map_use_tree_draw then
2133 begin
2134 mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2135 end
2136 else
2137 begin
2138 gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor));
2139 end;
2140 end;
2143 procedure g_Map_DrawBack(dx, dy: Integer);
2144 begin
2145 if gDrawBackGround and (BackID <> DWORD(-1)) then
2146 e_DrawSize(BackID, dx, dy, 0, False, False, gBackSize.X, gBackSize.Y)
2147 else
2148 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
2149 end;
2151 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
2152 PanelType: Word; b1x3: Boolean=false): Boolean;
2153 var
2154 a, h: Integer;
2155 begin
2156 Result := False;
2158 if WordBool(PanelType and PANEL_WALL) then
2159 if gWalls <> nil then
2160 begin
2161 h := High(gWalls);
2163 for a := 0 to h do
2164 if gWalls[a].Enabled and
2165 g_Collide(X, Y, Width, Height,
2166 gWalls[a].X, gWalls[a].Y,
2167 gWalls[a].Width, gWalls[a].Height) then
2168 begin
2169 Result := True;
2170 Exit;
2171 end;
2172 end;
2174 if WordBool(PanelType and PANEL_WATER) then
2175 if gWater <> nil then
2176 begin
2177 h := High(gWater);
2179 for a := 0 to h do
2180 if g_Collide(X, Y, Width, Height,
2181 gWater[a].X, gWater[a].Y,
2182 gWater[a].Width, gWater[a].Height) then
2183 begin
2184 Result := True;
2185 Exit;
2186 end;
2187 end;
2189 if WordBool(PanelType and PANEL_ACID1) then
2190 if gAcid1 <> nil then
2191 begin
2192 h := High(gAcid1);
2194 for a := 0 to h do
2195 if g_Collide(X, Y, Width, Height,
2196 gAcid1[a].X, gAcid1[a].Y,
2197 gAcid1[a].Width, gAcid1[a].Height) then
2198 begin
2199 Result := True;
2200 Exit;
2201 end;
2202 end;
2204 if WordBool(PanelType and PANEL_ACID2) then
2205 if gAcid2 <> nil then
2206 begin
2207 h := High(gAcid2);
2209 for a := 0 to h do
2210 if g_Collide(X, Y, Width, Height,
2211 gAcid2[a].X, gAcid2[a].Y,
2212 gAcid2[a].Width, gAcid2[a].Height) then
2213 begin
2214 Result := True;
2215 Exit;
2216 end;
2217 end;
2219 if WordBool(PanelType and PANEL_STEP) then
2220 if gSteps <> nil then
2221 begin
2222 h := High(gSteps);
2224 for a := 0 to h do
2225 if g_Collide(X, Y, Width, Height,
2226 gSteps[a].X, gSteps[a].Y,
2227 gSteps[a].Width, gSteps[a].Height) then
2228 begin
2229 Result := True;
2230 Exit;
2231 end;
2232 end;
2234 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then
2235 if gLifts <> nil then
2236 begin
2237 h := High(gLifts);
2239 for a := 0 to h do
2240 if ((WordBool(PanelType and (PANEL_LIFTUP)) and (gLifts[a].LiftType = 0)) or
2241 (WordBool(PanelType and (PANEL_LIFTDOWN)) and (gLifts[a].LiftType = 1)) or
2242 (WordBool(PanelType and (PANEL_LIFTLEFT)) and (gLifts[a].LiftType = 2)) or
2243 (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (gLifts[a].LiftType = 3))) and
2244 g_Collide(X, Y, Width, Height,
2245 gLifts[a].X, gLifts[a].Y,
2246 gLifts[a].Width, gLifts[a].Height) then
2247 begin
2248 Result := True;
2249 Exit;
2250 end;
2251 end;
2253 if WordBool(PanelType and PANEL_BLOCKMON) then
2254 if gBlockMon <> nil then
2255 begin
2256 h := High(gBlockMon);
2258 for a := 0 to h do
2259 if ( (not b1x3) or
2260 ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64) ) and
2261 g_Collide(X, Y, Width, Height,
2262 gBlockMon[a].X, gBlockMon[a].Y,
2263 gBlockMon[a].Width, gBlockMon[a].Height) then
2264 begin
2265 Result := True;
2266 Exit;
2267 end;
2268 end;
2269 end;
2271 function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD;
2272 var
2273 texid: DWORD;
2275 function checkPanels (constref panels: TPanelArray): Boolean;
2276 var
2277 a: Integer;
2278 begin
2279 result := false;
2280 if panels = nil then exit;
2281 for a := 0 to High(panels) do
2282 begin
2283 if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then
2284 begin
2285 result := true;
2286 texid := panels[a].GetTextureID();
2287 exit;
2288 end;
2289 end;
2290 end;
2292 begin
2293 texid := TEXTURE_NONE;
2294 result := texid;
2295 if not checkPanels(gWater) then
2296 if not checkPanels(gAcid1) then
2297 if not checkPanels(gAcid2) then exit;
2298 result := texid;
2299 end;
2302 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean;
2303 function checker (pan: TPanel; tag: Integer): Boolean;
2304 begin
2305 result := false; // don't stop, ever
2307 if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
2308 begin
2309 if not pan.Enabled then exit;
2310 end;
2312 if ((tag and GridTagLift) <> 0) then
2313 begin
2314 result :=
2315 ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
2316 (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
2317 (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
2318 (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3))) and
2319 g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2320 exit;
2321 end;
2323 if ((tag and GridTagBlockMon) <> 0) then
2324 begin
2325 result := ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2326 exit;
2327 end;
2329 // other shit
2330 result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height);
2331 end;
2333 var
2334 tagmask: Integer = 0;
2335 begin
2336 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2337 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2338 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2339 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2340 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2341 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2342 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2344 if (tagmask = 0) then begin result := false; exit; end; // just in case
2346 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2347 if gdbg_map_use_accel_coldet then
2348 begin
2349 if gdbg_map_use_tree_coldet then
2350 begin
2351 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2352 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2353 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2354 begin
2355 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2356 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2357 end;
2358 end
2359 else
2360 begin
2361 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2362 end;
2363 end
2364 else
2365 begin
2366 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2367 end;
2368 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2369 end;
2372 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2373 var
2374 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2375 texid: DWORD;
2377 // slightly different from the old code, but meh...
2378 function checker (pan: TPanel; tag: Integer): Boolean;
2379 begin
2380 result := false; // don't stop, ever
2381 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2382 // check priorities
2383 case cctype of
2384 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2385 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2386 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2387 end;
2388 // collision?
2389 if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2390 // yeah
2391 texid := pan.GetTextureID();
2392 // water? water has the highest priority, so stop right here
2393 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2394 // acid2?
2395 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2396 // acid1?
2397 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2398 end;
2400 begin
2401 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2402 if gdbg_map_use_accel_coldet then
2403 begin
2404 texid := TEXTURE_NONE;
2405 if gdbg_map_use_tree_coldet then
2406 begin
2407 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2408 end
2409 else
2410 begin
2411 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2412 end;
2413 result := texid;
2414 end
2415 else
2416 begin
2417 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2418 end;
2419 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2420 end;
2422 procedure g_Map_EnableWall(ID: DWORD);
2423 begin
2424 with gWalls[ID] do
2425 begin
2426 Enabled := True;
2427 //g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2429 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2430 end;
2431 end;
2433 procedure g_Map_DisableWall(ID: DWORD);
2434 begin
2435 with gWalls[ID] do
2436 begin
2437 Enabled := False;
2438 //g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2440 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2441 end;
2442 end;
2444 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2445 var
2446 tp: TPanel;
2447 begin
2448 case PanelType of
2449 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2450 tp := gWalls[ID];
2451 PANEL_FORE:
2452 tp := gRenderForegrounds[ID];
2453 PANEL_BACK:
2454 tp := gRenderBackgrounds[ID];
2455 PANEL_WATER:
2456 tp := gWater[ID];
2457 PANEL_ACID1:
2458 tp := gAcid1[ID];
2459 PANEL_ACID2:
2460 tp := gAcid2[ID];
2461 PANEL_STEP:
2462 tp := gSteps[ID];
2463 else
2464 Exit;
2465 end;
2467 tp.NextTexture(AnimLoop);
2468 if g_Game_IsServer and g_Game_IsNet then
2469 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2470 end;
2472 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2473 begin
2474 if gLifts[ID].LiftType = t then
2475 Exit;
2477 with gLifts[ID] do
2478 begin
2479 LiftType := t;
2482 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2484 if LiftType = 0 then
2485 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2486 else if LiftType = 1 then
2487 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2488 else if LiftType = 2 then
2489 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2490 else if LiftType = 3 then
2491 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2494 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2495 end;
2496 end;
2498 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2499 var
2500 a: Integer;
2501 PointsArray: Array of TRespawnPoint;
2502 begin
2503 Result := False;
2504 SetLength(PointsArray, 0);
2506 if RespawnPoints = nil then
2507 Exit;
2509 for a := 0 to High(RespawnPoints) do
2510 if RespawnPoints[a].PointType = PointType then
2511 begin
2512 SetLength(PointsArray, Length(PointsArray)+1);
2513 PointsArray[High(PointsArray)] := RespawnPoints[a];
2514 end;
2516 if PointsArray = nil then
2517 Exit;
2519 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2520 Result := True;
2521 end;
2523 function g_Map_GetPointCount(PointType: Byte): Word;
2524 var
2525 a: Integer;
2526 begin
2527 Result := 0;
2529 if RespawnPoints = nil then
2530 Exit;
2532 for a := 0 to High(RespawnPoints) do
2533 if RespawnPoints[a].PointType = PointType then
2534 Result := Result + 1;
2535 end;
2537 function g_Map_HaveFlagPoints(): Boolean;
2538 begin
2539 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2540 end;
2542 procedure g_Map_ResetFlag(Flag: Byte);
2543 begin
2544 with gFlags[Flag] do
2545 begin
2546 Obj.X := -1000;
2547 Obj.Y := -1000;
2548 Obj.Vel.X := 0;
2549 Obj.Vel.Y := 0;
2550 Direction := D_LEFT;
2551 State := FLAG_STATE_NONE;
2552 if FlagPoints[Flag] <> nil then
2553 begin
2554 Obj.X := FlagPoints[Flag]^.X;
2555 Obj.Y := FlagPoints[Flag]^.Y;
2556 Direction := FlagPoints[Flag]^.Direction;
2557 State := FLAG_STATE_NORMAL;
2558 end;
2559 Count := -1;
2560 end;
2561 end;
2563 procedure g_Map_DrawFlags();
2564 var
2565 i, dx: Integer;
2566 Mirror: TMirrorType;
2567 begin
2568 if gGameSettings.GameMode <> GM_CTF then
2569 Exit;
2571 for i := FLAG_RED to FLAG_BLUE do
2572 with gFlags[i] do
2573 if State <> FLAG_STATE_CAPTURED then
2574 begin
2575 if State = FLAG_STATE_NONE then
2576 continue;
2578 if Direction = D_LEFT then
2579 begin
2580 Mirror := M_HORIZONTAL;
2581 dx := -1;
2582 end
2583 else
2584 begin
2585 Mirror := M_NONE;
2586 dx := 1;
2587 end;
2589 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2591 if g_debug_Frames then
2592 begin
2593 e_DrawQuad(Obj.X+Obj.Rect.X,
2594 Obj.Y+Obj.Rect.Y,
2595 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2596 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2597 0, 255, 0);
2598 end;
2599 end;
2600 end;
2602 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2603 var
2604 dw: DWORD;
2605 b: Byte;
2606 str: String;
2607 boo: Boolean;
2609 procedure SavePanelArray(var panels: TPanelArray);
2610 var
2611 PAMem: TBinMemoryWriter;
2612 i: Integer;
2613 begin
2614 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2615 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2617 i := 0;
2618 while i < Length(panels) do
2619 begin
2620 if panels[i].SaveIt then
2621 begin
2622 // ID ïàíåëè:
2623 PAMem.WriteInt(i);
2624 // Ñîõðàíÿåì ïàíåëü:
2625 panels[i].SaveState(PAMem);
2626 end;
2627 Inc(i);
2628 end;
2630 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2631 PAMem.SaveToMemory(Mem);
2632 PAMem.Free();
2633 end;
2635 procedure SaveFlag(flag: PFlag);
2636 begin
2637 // Ñèãíàòóðà ôëàãà:
2638 dw := FLAG_SIGNATURE; // 'FLAG'
2639 Mem.WriteDWORD(dw);
2640 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2641 Mem.WriteByte(flag^.RespawnType);
2642 // Ñîñòîÿíèå ôëàãà:
2643 Mem.WriteByte(flag^.State);
2644 // Íàïðàâëåíèå ôëàãà:
2645 if flag^.Direction = D_LEFT then
2646 b := 1
2647 else // D_RIGHT
2648 b := 2;
2649 Mem.WriteByte(b);
2650 // Îáúåêò ôëàãà:
2651 Obj_SaveState(@flag^.Obj, Mem);
2652 end;
2654 begin
2655 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2657 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2658 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2659 SavePanelArray(gWalls);
2660 // Ñîõðàíÿåì ïàíåëè ôîíà:
2661 SavePanelArray(gRenderBackgrounds);
2662 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2663 SavePanelArray(gRenderForegrounds);
2664 // Ñîõðàíÿåì ïàíåëè âîäû:
2665 SavePanelArray(gWater);
2666 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2667 SavePanelArray(gAcid1);
2668 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2669 SavePanelArray(gAcid2);
2670 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2671 SavePanelArray(gSteps);
2672 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2673 SavePanelArray(gLifts);
2674 ///// /////
2676 ///// Ñîõðàíÿåì ìóçûêó: /////
2677 // Ñèãíàòóðà ìóçûêè:
2678 dw := MUSIC_SIGNATURE; // 'MUSI'
2679 Mem.WriteDWORD(dw);
2680 // Íàçâàíèå ìóçûêè:
2681 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2682 if gMusic.NoMusic then
2683 str := ''
2684 else
2685 str := gMusic.Name;
2686 Mem.WriteString(str, 64);
2687 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2688 dw := gMusic.GetPosition();
2689 Mem.WriteDWORD(dw);
2690 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2691 boo := gMusic.SpecPause;
2692 Mem.WriteBoolean(boo);
2693 ///// /////
2695 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2696 Mem.WriteInt(gTotalMonsters);
2697 ///// /////
2699 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2700 if gGameSettings.GameMode = GM_CTF then
2701 begin
2702 // Ôëàã Êðàñíîé êîìàíäû:
2703 SaveFlag(@gFlags[FLAG_RED]);
2704 // Ôëàã Ñèíåé êîìàíäû:
2705 SaveFlag(@gFlags[FLAG_BLUE]);
2706 end;
2707 ///// /////
2709 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2710 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2711 begin
2712 // Î÷êè Êðàñíîé êîìàíäû:
2713 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2714 // Î÷êè Ñèíåé êîìàíäû:
2715 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2716 end;
2717 ///// /////
2718 end;
2720 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2721 var
2722 dw: DWORD;
2723 b: Byte;
2724 str: String;
2725 boo: Boolean;
2727 procedure LoadPanelArray(var panels: TPanelArray);
2728 var
2729 PAMem: TBinMemoryReader;
2730 i, id: Integer;
2731 begin
2732 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2733 PAMem := TBinMemoryReader.Create();
2734 PAMem.LoadFromMemory(Mem);
2736 for i := 0 to Length(panels)-1 do
2737 if panels[i].SaveIt then
2738 begin
2739 // ID ïàíåëè:
2740 PAMem.ReadInt(id);
2741 if id <> i then
2742 begin
2743 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2744 end;
2745 // Çàãðóæàåì ïàíåëü:
2746 panels[i].LoadState(PAMem);
2747 end;
2749 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2750 PAMem.Free();
2751 end;
2753 procedure LoadFlag(flag: PFlag);
2754 begin
2755 // Ñèãíàòóðà ôëàãà:
2756 Mem.ReadDWORD(dw);
2757 if dw <> FLAG_SIGNATURE then // 'FLAG'
2758 begin
2759 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2760 end;
2761 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2762 Mem.ReadByte(flag^.RespawnType);
2763 // Ñîñòîÿíèå ôëàãà:
2764 Mem.ReadByte(flag^.State);
2765 // Íàïðàâëåíèå ôëàãà:
2766 Mem.ReadByte(b);
2767 if b = 1 then
2768 flag^.Direction := D_LEFT
2769 else // b = 2
2770 flag^.Direction := D_RIGHT;
2771 // Îáúåêò ôëàãà:
2772 Obj_LoadState(@flag^.Obj, Mem);
2773 end;
2775 begin
2776 if Mem = nil then
2777 Exit;
2779 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2780 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2781 LoadPanelArray(gWalls);
2782 // Çàãðóæàåì ïàíåëè ôîíà:
2783 LoadPanelArray(gRenderBackgrounds);
2784 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2785 LoadPanelArray(gRenderForegrounds);
2786 // Çàãðóæàåì ïàíåëè âîäû:
2787 LoadPanelArray(gWater);
2788 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2789 LoadPanelArray(gAcid1);
2790 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2791 LoadPanelArray(gAcid2);
2792 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2793 LoadPanelArray(gSteps);
2794 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2795 LoadPanelArray(gLifts);
2796 ///// /////
2798 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2799 g_GFX_Init();
2800 mapCreateGrid();
2802 ///// Çàãðóæàåì ìóçûêó: /////
2803 // Ñèãíàòóðà ìóçûêè:
2804 Mem.ReadDWORD(dw);
2805 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2806 begin
2807 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2808 end;
2809 // Íàçâàíèå ìóçûêè:
2810 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2811 Mem.ReadString(str);
2812 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2813 Mem.ReadDWORD(dw);
2814 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2815 Mem.ReadBoolean(boo);
2816 // Çàïóñêàåì ýòó ìóçûêó:
2817 gMusic.SetByName(str);
2818 gMusic.SpecPause := boo;
2819 gMusic.Play();
2820 gMusic.Pause(True);
2821 gMusic.SetPosition(dw);
2822 ///// /////
2824 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2825 Mem.ReadInt(gTotalMonsters);
2826 ///// /////
2828 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2829 if gGameSettings.GameMode = GM_CTF then
2830 begin
2831 // Ôëàã Êðàñíîé êîìàíäû:
2832 LoadFlag(@gFlags[FLAG_RED]);
2833 // Ôëàã Ñèíåé êîìàíäû:
2834 LoadFlag(@gFlags[FLAG_BLUE]);
2835 end;
2836 ///// /////
2838 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2839 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2840 begin
2841 // Î÷êè Êðàñíîé êîìàíäû:
2842 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2843 // Î÷êè Ñèíåé êîìàíäû:
2844 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2845 end;
2846 ///// /////
2847 end;
2849 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2850 var
2851 Arr: TPanelArray;
2852 begin
2853 Result := nil;
2854 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2855 Arr := PanelByID[PanelID].PWhere^;
2856 PanelArrayID := PanelByID[PanelID].PArrID;
2857 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2858 end;
2860 end.