DEADSOFTWARE

more fixes to grid; dunno, seems to work again
[d2df-sdl.git] / src / game / g_map.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_map;
19 interface
21 uses
22 e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
23 g_phys, wadreader, BinEditor, g_panel, g_grid, z_aabbtree, md5, binheap, xprofiler;
25 type
26 TMapInfo = record
27 Map: String;
28 Name: String;
29 Description: String;
30 Author: String;
31 MusicName: String;
32 SkyName: String;
33 Height: Word;
34 Width: Word;
35 end;
37 PRespawnPoint = ^TRespawnPoint;
38 TRespawnPoint = record
39 X, Y: Integer;
40 Direction: TDirection;
41 PointType: Byte;
42 end;
44 PFlagPoint = ^TFlagPoint;
45 TFlagPoint = TRespawnPoint;
47 PFlag = ^TFlag;
48 TFlag = record
49 Obj: TObj;
50 RespawnType: Byte;
51 State: Byte;
52 Count: Integer;
53 CaptureTime: LongWord;
54 Animation: TAnimation;
55 Direction: TDirection;
56 end;
58 function g_Map_Load(Res: String): Boolean;
59 function g_Map_GetMapInfo(Res: String): TMapInfo;
60 function g_Map_GetMapsList(WADName: String): SArray;
61 function g_Map_Exist(Res: String): Boolean;
62 procedure g_Map_Free();
63 procedure g_Map_Update();
65 procedure g_Map_DrawPanels (PanelType: Word); // unaccelerated
66 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
68 procedure g_Map_DrawBack(dx, dy: Integer);
69 function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
70 PanelType: Word; b1x3: Boolean=false): Boolean;
71 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
72 procedure g_Map_EnableWall(ID: DWORD);
73 procedure g_Map_DisableWall(ID: DWORD);
74 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
75 procedure g_Map_SetLift(ID: DWORD; t: Integer);
76 procedure g_Map_ReAdd_DieTriggers();
77 function g_Map_IsSpecialTexture(Texture: String): Boolean;
79 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
80 function g_Map_GetPointCount(PointType: Byte): Word;
82 function g_Map_HaveFlagPoints(): Boolean;
84 procedure g_Map_ResetFlag(Flag: Byte);
85 procedure g_Map_DrawFlags();
87 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
89 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
90 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
92 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
94 // returns wall index in `gWalls` or -1
95 function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
97 type
98 TForEachPanelCB = function (pan: TPanel): Boolean; // return `true` to stop
100 function g_Map_HasAnyPanelAtPoint (x, y: Integer; panelType: Word): Boolean;
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, lastDistSq: 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 distSq: 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 distSq := (prevx-x0)*(prevx-x0)+(prevy-y0)*(prevy-y0);
391 if (distSq < lastDistSq) then
392 begin
393 wasHit := true;
394 lastDistSq := distSq;
395 lastX := prevx;
396 lastY := prevy;
397 end;
398 end;
399 end;
401 begin
402 result := false;
403 if (gMapGrid = nil) then exit;
404 lastDistSq := (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_HasAnyPanelAtPoint (x, y: Integer; 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;
435 end;
437 var
438 tagmask: Integer = 0;
439 begin
440 result := false;
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.forEachAtPoint(x, y, 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 result := true;
2332 end;
2334 var
2335 tagmask: Integer = 0;
2336 begin
2337 if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
2338 if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
2339 if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
2340 if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
2341 if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
2342 if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
2343 if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
2345 if (tagmask = 0) then begin result := false; exit; end; // just in case
2347 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('*solids');
2348 if gdbg_map_use_accel_coldet then
2349 begin
2350 {if gdbg_map_use_tree_coldet then
2351 begin
2352 //e_WriteLog(Format('coldet query: x=%d; y=%d; w=%d; h=%d', [X, Y, Width, Height]), MSG_NOTIFY);
2353 result := (mapTree.aabbQuery(X, Y, Width, Height, checker, tagmask) <> nil);
2354 if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
2355 begin
2356 //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
2357 g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
2358 end;
2359 end
2360 else}
2361 begin
2362 if (Width = 1) and (Height = 1) then
2363 begin
2364 result := gMapGrid.forEachAtPoint(X, Y, checker, tagmask);
2365 end
2366 else
2367 begin
2368 result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask);
2369 end;
2370 end;
2371 end
2372 else
2373 begin
2374 result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
2375 end;
2376 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2377 end;
2380 function g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
2381 var
2382 cctype: Integer = 3; // priority: 0: water was hit, 1: acid1 was hit, 2: acid2 was hit; 3: nothing was hit
2383 texid: DWORD;
2385 // slightly different from the old code, but meh...
2386 function checker (pan: TPanel; tag: Integer): Boolean;
2387 begin
2388 result := false; // don't stop, ever
2389 //if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit;
2390 // check priorities
2391 case cctype of
2392 0: if ((tag and GridTagWater) = 0) then exit; // allowed: water
2393 1: if ((tag and (GridTagWater or GridTagAcid1)) = 0) then exit; // allowed: water, acid1
2394 //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // allowed: water, acid1, acid2
2395 end;
2396 // collision?
2397 //if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit;
2398 // yeah
2399 texid := pan.GetTextureID();
2400 // water? water has the highest priority, so stop right here
2401 if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end;
2402 // acid2?
2403 if ((tag and GridTagAcid2) <> 0) then cctype := 2;
2404 // acid1?
2405 if ((tag and GridTagAcid1) <> 0) then cctype := 1;
2406 end;
2408 begin
2409 if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquids');
2410 if gdbg_map_use_accel_coldet then
2411 begin
2412 texid := TEXTURE_NONE;
2413 {if gdbg_map_use_tree_coldet then
2414 begin
2415 mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2416 end
2417 else}
2418 begin
2419 if (Width = 1) and (Height = 1) then
2420 begin
2421 gMapGrid.forEachAtPoint(X, Y, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2422 end
2423 else
2424 begin
2425 gMapGrid.forEachInAABB(X, Y, Width, Height, checker, (GridTagWater or GridTagAcid1 or GridTagAcid2));
2426 end;
2427 end;
2428 result := texid;
2429 end
2430 else
2431 begin
2432 result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
2433 end;
2434 if (profMapCollision <> nil) then profMapCollision.sectionEnd();
2435 end;
2437 procedure g_Map_EnableWall(ID: DWORD);
2438 begin
2439 with gWalls[ID] do
2440 begin
2441 Enabled := True;
2442 //g_Mark(X, Y, Width, Height, MARK_DOOR, True);
2444 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2445 end;
2446 end;
2448 procedure g_Map_DisableWall(ID: DWORD);
2449 begin
2450 with gWalls[ID] do
2451 begin
2452 Enabled := False;
2453 //g_Mark(X, Y, Width, Height, MARK_DOOR, False);
2455 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2456 end;
2457 end;
2459 procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0);
2460 var
2461 tp: TPanel;
2462 begin
2463 case PanelType of
2464 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2465 tp := gWalls[ID];
2466 PANEL_FORE:
2467 tp := gRenderForegrounds[ID];
2468 PANEL_BACK:
2469 tp := gRenderBackgrounds[ID];
2470 PANEL_WATER:
2471 tp := gWater[ID];
2472 PANEL_ACID1:
2473 tp := gAcid1[ID];
2474 PANEL_ACID2:
2475 tp := gAcid2[ID];
2476 PANEL_STEP:
2477 tp := gSteps[ID];
2478 else
2479 Exit;
2480 end;
2482 tp.NextTexture(AnimLoop);
2483 if g_Game_IsServer and g_Game_IsNet then
2484 MH_SEND_PanelTexture(PanelType, ID, AnimLoop);
2485 end;
2487 procedure g_Map_SetLift(ID: DWORD; t: Integer);
2488 begin
2489 if gLifts[ID].LiftType = t then
2490 Exit;
2492 with gLifts[ID] do
2493 begin
2494 LiftType := t;
2497 g_Mark(X, Y, Width, Height, MARK_LIFT, False);
2499 if LiftType = 0 then
2500 g_Mark(X, Y, Width, Height, MARK_LIFTUP, True)
2501 else if LiftType = 1 then
2502 g_Mark(X, Y, Width, Height, MARK_LIFTDOWN, True)
2503 else if LiftType = 2 then
2504 g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
2505 else if LiftType = 3 then
2506 g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
2509 if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
2510 end;
2511 end;
2513 function g_Map_GetPoint(PointType: Byte; var RespawnPoint: TRespawnPoint): Boolean;
2514 var
2515 a: Integer;
2516 PointsArray: Array of TRespawnPoint;
2517 begin
2518 Result := False;
2519 SetLength(PointsArray, 0);
2521 if RespawnPoints = nil then
2522 Exit;
2524 for a := 0 to High(RespawnPoints) do
2525 if RespawnPoints[a].PointType = PointType then
2526 begin
2527 SetLength(PointsArray, Length(PointsArray)+1);
2528 PointsArray[High(PointsArray)] := RespawnPoints[a];
2529 end;
2531 if PointsArray = nil then
2532 Exit;
2534 RespawnPoint := PointsArray[Random(Length(PointsArray))];
2535 Result := True;
2536 end;
2538 function g_Map_GetPointCount(PointType: Byte): Word;
2539 var
2540 a: Integer;
2541 begin
2542 Result := 0;
2544 if RespawnPoints = nil then
2545 Exit;
2547 for a := 0 to High(RespawnPoints) do
2548 if RespawnPoints[a].PointType = PointType then
2549 Result := Result + 1;
2550 end;
2552 function g_Map_HaveFlagPoints(): Boolean;
2553 begin
2554 Result := (FlagPoints[FLAG_RED] <> nil) and (FlagPoints[FLAG_BLUE] <> nil);
2555 end;
2557 procedure g_Map_ResetFlag(Flag: Byte);
2558 begin
2559 with gFlags[Flag] do
2560 begin
2561 Obj.X := -1000;
2562 Obj.Y := -1000;
2563 Obj.Vel.X := 0;
2564 Obj.Vel.Y := 0;
2565 Direction := D_LEFT;
2566 State := FLAG_STATE_NONE;
2567 if FlagPoints[Flag] <> nil then
2568 begin
2569 Obj.X := FlagPoints[Flag]^.X;
2570 Obj.Y := FlagPoints[Flag]^.Y;
2571 Direction := FlagPoints[Flag]^.Direction;
2572 State := FLAG_STATE_NORMAL;
2573 end;
2574 Count := -1;
2575 end;
2576 end;
2578 procedure g_Map_DrawFlags();
2579 var
2580 i, dx: Integer;
2581 Mirror: TMirrorType;
2582 begin
2583 if gGameSettings.GameMode <> GM_CTF then
2584 Exit;
2586 for i := FLAG_RED to FLAG_BLUE do
2587 with gFlags[i] do
2588 if State <> FLAG_STATE_CAPTURED then
2589 begin
2590 if State = FLAG_STATE_NONE then
2591 continue;
2593 if Direction = D_LEFT then
2594 begin
2595 Mirror := M_HORIZONTAL;
2596 dx := -1;
2597 end
2598 else
2599 begin
2600 Mirror := M_NONE;
2601 dx := 1;
2602 end;
2604 Animation.Draw(Obj.X+dx, Obj.Y+1, Mirror);
2606 if g_debug_Frames then
2607 begin
2608 e_DrawQuad(Obj.X+Obj.Rect.X,
2609 Obj.Y+Obj.Rect.Y,
2610 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
2611 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
2612 0, 255, 0);
2613 end;
2614 end;
2615 end;
2617 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
2618 var
2619 dw: DWORD;
2620 b: Byte;
2621 str: String;
2622 boo: Boolean;
2624 procedure SavePanelArray(var panels: TPanelArray);
2625 var
2626 PAMem: TBinMemoryWriter;
2627 i: Integer;
2628 begin
2629 // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé:
2630 PAMem := TBinMemoryWriter.Create((Length(panels)+1) * 40);
2632 i := 0;
2633 while i < Length(panels) do
2634 begin
2635 if panels[i].SaveIt then
2636 begin
2637 // ID ïàíåëè:
2638 PAMem.WriteInt(i);
2639 // Ñîõðàíÿåì ïàíåëü:
2640 panels[i].SaveState(PAMem);
2641 end;
2642 Inc(i);
2643 end;
2645 // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé:
2646 PAMem.SaveToMemory(Mem);
2647 PAMem.Free();
2648 end;
2650 procedure SaveFlag(flag: PFlag);
2651 begin
2652 // Ñèãíàòóðà ôëàãà:
2653 dw := FLAG_SIGNATURE; // 'FLAG'
2654 Mem.WriteDWORD(dw);
2655 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2656 Mem.WriteByte(flag^.RespawnType);
2657 // Ñîñòîÿíèå ôëàãà:
2658 Mem.WriteByte(flag^.State);
2659 // Íàïðàâëåíèå ôëàãà:
2660 if flag^.Direction = D_LEFT then
2661 b := 1
2662 else // D_RIGHT
2663 b := 2;
2664 Mem.WriteByte(b);
2665 // Îáúåêò ôëàãà:
2666 Obj_SaveState(@flag^.Obj, Mem);
2667 end;
2669 begin
2670 Mem := TBinMemoryWriter.Create(1024 * 1024); // 1 MB
2672 ///// Ñîõðàíÿåì ñïèñêè ïàíåëåé: /////
2673 // Ñîõðàíÿåì ïàíåëè ñòåí è äâåðåé:
2674 SavePanelArray(gWalls);
2675 // Ñîõðàíÿåì ïàíåëè ôîíà:
2676 SavePanelArray(gRenderBackgrounds);
2677 // Ñîõðàíÿåì ïàíåëè ïåðåäíåãî ïëàíà:
2678 SavePanelArray(gRenderForegrounds);
2679 // Ñîõðàíÿåì ïàíåëè âîäû:
2680 SavePanelArray(gWater);
2681 // Ñîõðàíÿåì ïàíåëè êèñëîòû-1:
2682 SavePanelArray(gAcid1);
2683 // Ñîõðàíÿåì ïàíåëè êèñëîòû-2:
2684 SavePanelArray(gAcid2);
2685 // Ñîõðàíÿåì ïàíåëè ñòóïåíåé:
2686 SavePanelArray(gSteps);
2687 // Ñîõðàíÿåì ïàíåëè ëèôòîâ:
2688 SavePanelArray(gLifts);
2689 ///// /////
2691 ///// Ñîõðàíÿåì ìóçûêó: /////
2692 // Ñèãíàòóðà ìóçûêè:
2693 dw := MUSIC_SIGNATURE; // 'MUSI'
2694 Mem.WriteDWORD(dw);
2695 // Íàçâàíèå ìóçûêè:
2696 Assert(gMusic <> nil, 'g_Map_SaveState: gMusic = nil');
2697 if gMusic.NoMusic then
2698 str := ''
2699 else
2700 str := gMusic.Name;
2701 Mem.WriteString(str, 64);
2702 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2703 dw := gMusic.GetPosition();
2704 Mem.WriteDWORD(dw);
2705 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2706 boo := gMusic.SpecPause;
2707 Mem.WriteBoolean(boo);
2708 ///// /////
2710 ///// Ñîõðàíÿåì êîëè÷åñòâî ìîíñòðîâ: /////
2711 Mem.WriteInt(gTotalMonsters);
2712 ///// /////
2714 //// Ñîõðàíÿåì ôëàãè, åñëè ýòî CTF: /////
2715 if gGameSettings.GameMode = GM_CTF then
2716 begin
2717 // Ôëàã Êðàñíîé êîìàíäû:
2718 SaveFlag(@gFlags[FLAG_RED]);
2719 // Ôëàã Ñèíåé êîìàíäû:
2720 SaveFlag(@gFlags[FLAG_BLUE]);
2721 end;
2722 ///// /////
2724 ///// Ñîõðàíÿåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2725 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2726 begin
2727 // Î÷êè Êðàñíîé êîìàíäû:
2728 Mem.WriteSmallInt(gTeamStat[TEAM_RED].Goals);
2729 // Î÷êè Ñèíåé êîìàíäû:
2730 Mem.WriteSmallInt(gTeamStat[TEAM_BLUE].Goals);
2731 end;
2732 ///// /////
2733 end;
2735 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
2736 var
2737 dw: DWORD;
2738 b: Byte;
2739 str: String;
2740 boo: Boolean;
2742 procedure LoadPanelArray(var panels: TPanelArray);
2743 var
2744 PAMem: TBinMemoryReader;
2745 i, id: Integer;
2746 begin
2747 // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé:
2748 PAMem := TBinMemoryReader.Create();
2749 PAMem.LoadFromMemory(Mem);
2751 for i := 0 to Length(panels)-1 do
2752 if panels[i].SaveIt then
2753 begin
2754 // ID ïàíåëè:
2755 PAMem.ReadInt(id);
2756 if id <> i then
2757 begin
2758 raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
2759 end;
2760 // Çàãðóæàåì ïàíåëü:
2761 panels[i].LoadState(PAMem);
2762 end;
2764 // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí:
2765 PAMem.Free();
2766 end;
2768 procedure LoadFlag(flag: PFlag);
2769 begin
2770 // Ñèãíàòóðà ôëàãà:
2771 Mem.ReadDWORD(dw);
2772 if dw <> FLAG_SIGNATURE then // 'FLAG'
2773 begin
2774 raise EBinSizeError.Create('g_Map_LoadState: LoadFlag: Wrong Flag Signature');
2775 end;
2776 // Âðåìÿ ïåðåïîÿâëåíèÿ ôëàãà:
2777 Mem.ReadByte(flag^.RespawnType);
2778 // Ñîñòîÿíèå ôëàãà:
2779 Mem.ReadByte(flag^.State);
2780 // Íàïðàâëåíèå ôëàãà:
2781 Mem.ReadByte(b);
2782 if b = 1 then
2783 flag^.Direction := D_LEFT
2784 else // b = 2
2785 flag^.Direction := D_RIGHT;
2786 // Îáúåêò ôëàãà:
2787 Obj_LoadState(@flag^.Obj, Mem);
2788 end;
2790 begin
2791 if Mem = nil then
2792 Exit;
2794 ///// Çàãðóæàåì ñïèñêè ïàíåëåé: /////
2795 // Çàãðóæàåì ïàíåëè ñòåí è äâåðåé:
2796 LoadPanelArray(gWalls);
2797 // Çàãðóæàåì ïàíåëè ôîíà:
2798 LoadPanelArray(gRenderBackgrounds);
2799 // Çàãðóæàåì ïàíåëè ïåðåäíåãî ïëàíà:
2800 LoadPanelArray(gRenderForegrounds);
2801 // Çàãðóæàåì ïàíåëè âîäû:
2802 LoadPanelArray(gWater);
2803 // Çàãðóæàåì ïàíåëè êèñëîòû-1:
2804 LoadPanelArray(gAcid1);
2805 // Çàãðóæàåì ïàíåëè êèñëîòû-2:
2806 LoadPanelArray(gAcid2);
2807 // Çàãðóæàåì ïàíåëè ñòóïåíåé:
2808 LoadPanelArray(gSteps);
2809 // Çàãðóæàåì ïàíåëè ëèôòîâ:
2810 LoadPanelArray(gLifts);
2811 ///// /////
2813 // Îáíîâëÿåì êàðòó ñòîëêíîâåíèé è ñåòêó:
2814 g_GFX_Init();
2815 mapCreateGrid();
2817 ///// Çàãðóæàåì ìóçûêó: /////
2818 // Ñèãíàòóðà ìóçûêè:
2819 Mem.ReadDWORD(dw);
2820 if dw <> MUSIC_SIGNATURE then // 'MUSI'
2821 begin
2822 raise EBinSizeError.Create('g_Map_LoadState: Wrong Music Signature');
2823 end;
2824 // Íàçâàíèå ìóçûêè:
2825 Assert(gMusic <> nil, 'g_Map_LoadState: gMusic = nil');
2826 Mem.ReadString(str);
2827 // Ïîçèöèÿ ïðîèãðûâàíèÿ ìóçûêè:
2828 Mem.ReadDWORD(dw);
2829 // Ñòîèò ëè ìóçûêà íà ñïåö-ïàóçå:
2830 Mem.ReadBoolean(boo);
2831 // Çàïóñêàåì ýòó ìóçûêó:
2832 gMusic.SetByName(str);
2833 gMusic.SpecPause := boo;
2834 gMusic.Play();
2835 gMusic.Pause(True);
2836 gMusic.SetPosition(dw);
2837 ///// /////
2839 ///// Çàãðóæàåì êîëè÷åñòâî ìîíñòðîâ: /////
2840 Mem.ReadInt(gTotalMonsters);
2841 ///// /////
2843 //// Çàãðóæàåì ôëàãè, åñëè ýòî CTF: /////
2844 if gGameSettings.GameMode = GM_CTF then
2845 begin
2846 // Ôëàã Êðàñíîé êîìàíäû:
2847 LoadFlag(@gFlags[FLAG_RED]);
2848 // Ôëàã Ñèíåé êîìàíäû:
2849 LoadFlag(@gFlags[FLAG_BLUE]);
2850 end;
2851 ///// /////
2853 ///// Çàãðóæàåì êîëè÷åñòâî ïîáåä, åñëè ýòî TDM/CTF: /////
2854 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
2855 begin
2856 // Î÷êè Êðàñíîé êîìàíäû:
2857 Mem.ReadSmallInt(gTeamStat[TEAM_RED].Goals);
2858 // Î÷êè Ñèíåé êîìàíäû:
2859 Mem.ReadSmallInt(gTeamStat[TEAM_BLUE].Goals);
2860 end;
2861 ///// /////
2862 end;
2864 function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
2865 var
2866 Arr: TPanelArray;
2867 begin
2868 Result := nil;
2869 if (PanelID < 0) or (PanelID > High(PanelByID)) then Exit;
2870 Arr := PanelByID[PanelID].PWhere^;
2871 PanelArrayID := PanelByID[PanelID].PArrID;
2872 Result := Addr(Arr[PanelByID[PanelID].PArrID]);
2873 end;
2875 end.