DEADSOFTWARE

field namefix: `FLive` -> `FAlive`; `live` -> `alive`
[d2df-sdl.git] / src / game / g_monsters.pas
index 8c2f2d7e399093feed5c4d58481ffe2ded58cf95..5719ca4ff300322779f697deeb1bbb08510a196b 100644 (file)
@@ -14,6 +14,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
 {$INCLUDE ../shared/a_modes.inc}
+{$M+}
 {.$DEFINE D2F_DEBUG_MONS_MOVE}
 unit g_monsters;
 
@@ -90,6 +91,9 @@ type
     function findNewPrey(): Boolean;
     procedure ActivateTriggers();
 
+    procedure setGameX (v: Integer); inline;
+    procedure setGameY (v: Integer); inline;
+
   public
     FNoRespawn: Boolean;
     FFireTime: Integer;
@@ -101,7 +105,7 @@ type
     function Collide(Panel: TPanel): Boolean; overload;
     function Collide(X, Y: Integer): Boolean; overload;
     function TeleportTo(X, Y: Integer; silent: Boolean; dir: Byte): Boolean;
-    function Live(): Boolean;
+    function alive(): Boolean;
     procedure SetHealth(aH: Integer);
     procedure Push(vx, vy: Integer);
     function Damage(aDamage: Word; VelX, VelY: Integer; SpawnerUID: Word; t: Byte): Boolean;
@@ -134,8 +138,17 @@ type
 
     procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
 
+    procedure setPosition (ax, ay: Integer; callPosChanged: Boolean=true); inline;
+
     procedure getMapBox (out x, y, w, h: Integer); inline;
 
+  public
+    property Obj: TObj read FObj;
+
+    property proxyId: Integer read mProxyId;
+    property arrIdx: Integer read mArrIdx;
+
+  published
     property MonsterType: Byte read FMonsterType;
     property MonsterHealth: Integer read FHealth write FHealth;
     property MonsterAmmo: Integer read FAmmo write FAmmo;
@@ -148,12 +161,11 @@ type
     property MonsterPain: Integer read FPain write FPain;
     property MonsterAnim: Byte read FCurAnim write FCurAnim;
 
-    property Obj: TObj read FObj;
     property UID: Word read FUID write FUID;
     property SpawnTrigger: Integer read FSpawnTrigger write FSpawnTrigger;
 
-    property GameX: Integer read FObj.X write FObj.X;
-    property GameY: Integer read FObj.Y write FObj.Y;
+    property GameX: Integer read FObj.X write setGameX;
+    property GameY: Integer read FObj.Y write setGameY;
     property GameVelX: Integer read FObj.Vel.X write FObj.Vel.X;
     property GameVelY: Integer read FObj.Vel.Y write FObj.Vel.Y;
     property GameAccelX: Integer read FObj.Accel.X write FObj.Accel.X;
@@ -161,9 +173,6 @@ type
     property GameDirection: TDirection read FDirection write FDirection;
 
     property StartID: Integer read FStartID;
-
-    property proxyId: Integer read mProxyId;
-    property arrIdx: Integer read mArrIdx;
   end;
 
 
@@ -173,7 +182,7 @@ procedure g_Mons_InitTree (x, y, w, h: Integer);
 procedure g_Monsters_LoadData ();
 procedure g_Monsters_FreeData ();
 procedure g_Monsters_Init ();
-procedure g_Monsters_Free ();
+procedure g_Monsters_Free (clearGrid: Boolean=true);
 function g_Monsters_Create (MonsterType: Byte; X, Y: Integer; Direction: TDirection;
   AdjCoord: Boolean = False; ForcedUID: Integer = -1): TMonster;
 procedure g_Monsters_Update ();
@@ -249,7 +258,8 @@ uses
   e_log, g_main, g_sound, g_gfx, g_player, g_game,
   g_weapons, g_triggers, MAPDEF, g_items, g_options,
   g_console, g_map, Math, SysUtils, g_menu, wadreader,
-  g_language, g_netmsg;
+  g_language, g_netmsg, idpool;
+
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -452,8 +462,8 @@ const
        LeftAnim: Boolean;
        wX, wY: Integer; // Îòêóäà âûëåòèò ïóëÿ
        AnimSpeed: Array [ANIM_SLEEP..ANIM_PAIN] of Byte;
-       AnimDeltaRight: Array [ANIM_SLEEP..ANIM_PAIN] of TPoint;
-       AnimDeltaLeft: Array [ANIM_SLEEP..ANIM_PAIN] of TPoint;
+       AnimDeltaRight: Array [ANIM_SLEEP..ANIM_PAIN] of TDFPoint;
+       AnimDeltaLeft: Array [ANIM_SLEEP..ANIM_PAIN] of TDFPoint;
      end =          // SLEEP           GO              DIE             MESS            ATTACK          ATTACK2         PAIN
    ((LeftAnim: False; wX: 54; wY: 32; AnimSpeed:(3, 2, 3, 2, 3, 0, 4); //DEMON
      AnimDeltaRight: ((X:  1; Y:  4), (X:  1; Y:  4), (X:  0; Y:  4), (X:  0; Y:  4), (X:  2; Y:  6), (X:  2; Y:  6), (X:  2; Y:  5));
@@ -542,6 +552,7 @@ const
 var
   gMonsters: array of TMonster;
   uidMap: array [0..65535] of TMonster; // monster knows it's index
+  freeInds: TIdPool = nil;
 
 
 procedure clearUidMap ();
@@ -549,6 +560,7 @@ var
   idx: Integer;
 begin
   for idx := 0 to High(uidMap) do uidMap[idx] := nil;
+  freeInds.clear();
 end;
 
 
@@ -578,29 +590,16 @@ var
   soulcount: Integer = 0;
 
 
-function allocMonster(): DWORD;
+function allocMonster (): DWORD;
 var
-  i, olen: Integer;
+  f, olen: Integer;
 begin
-  for i := 0 to High(gMonsters) do
-  begin
-    if (gMonsters[i] = nil) then
-    begin
-      result := i;
-      exit;
-    end;
-  end;
-
-  olen := Length(gMonsters);
-  if (olen = 0) then
-  begin
-    SetLength(gMonsters, 64);
-    result := 0;
-  end
-  else
+  result := freeInds.alloc();
+  if (result > High(gMonsters)) then
   begin
-    result := olen;
-    SetLength(gMonsters, Length(gMonsters)+32);
+    olen := Length(gMonsters);
+    SetLength(gMonsters, result+64);
+    for f := olen to High(gMonsters) do gMonsters[f] := nil;
   end;
 end;
 
@@ -966,6 +965,7 @@ begin
 
   g_Sound_CreateWADEx('SOUND_MONSTER_FISH_ATTACK', GameWAD+':MSOUNDS\FISH_ATTACK');
 
+  freeInds := TIdPool.Create();
   clearUidMap();
   monCheckTrapLastFrameId := 0;
 end;
@@ -1184,6 +1184,9 @@ begin
   g_Sound_Delete('SOUND_MONSTER_SPIDER_WALK');
 
   g_Sound_Delete('SOUND_MONSTER_FISH_ATTACK');
+
+  freeInds.Free();
+  freeInds := nil;
 end;
 
 procedure g_Monsters_Init();
@@ -1191,12 +1194,16 @@ begin
   soulcount := 0;
 end;
 
-procedure g_Monsters_Free();
+procedure g_Monsters_Free (clearGrid: Boolean=true);
 var
   a: Integer;
 begin
-  monsGrid.Free();
-  monsGrid := nil;
+  e_LogWritefln('Cleared monster data (clearGrid=%s)', [clearGrid]);
+  if (clearGrid) then
+  begin
+    monsGrid.Free();
+    monsGrid := nil;
+  end;
   for a := 0 to High(gMonsters) do gMonsters[a].Free();
   gMonsters := nil;
   clearUidMap();
@@ -1209,6 +1216,8 @@ procedure g_Mons_InitTree (x, y, w, h: Integer);
 begin
   monsGrid.Free();
   monsGrid := TMonsterGrid.Create(x, y, w, h);
+  //clearUidMap(); // why not?
+  e_LogWritefln('%s', ['Recreated monster tree']);
 end;
 
 
@@ -1271,11 +1280,14 @@ begin
   if gMonsters = nil then
     Exit;
 
-// Ïðèêîëèñò ñìååòñÿ íàä ñìåðòüþ èãðîêà:
+  // Ïðèêîëèñò ñìååòñÿ íàä ñìåðòüþ èãðîêà:
   h := High(gMonsters);
   for a := 0 to h do
+  begin
     if (gMonsters[a] <> nil) then
+    begin
       with gMonsters[a] do
+      begin
         if (FMonsterType = MONSTER_MAN) and
            (FState <> MONSTATE_DEAD) and
            (FState <> MONSTATE_SLEEP) and
@@ -1284,6 +1296,9 @@ begin
           g_Sound_PlayExAt('SOUND_MONSTER_TRUP', FObj.X, FObj.Y);
           Exit;
         end;
+      end;
+    end;
+  end;
 end;
 
 procedure g_Monsters_Update();
@@ -1330,9 +1345,12 @@ var
   a: Integer;
 begin
   if gMonsters <> nil then
+  begin
     for a := 0 to High(gMonsters) do
-      if gMonsters[a] <> nil then
-        gMonsters[a].Draw();
+    begin
+      if (gMonsters[a] <> nil) then gMonsters[a].Draw();
+    end;
+  end;
 end;
 
 procedure g_Monsters_DrawHealth();
@@ -1344,29 +1362,19 @@ begin
   e_TextureFontGetSize(gStdFont, fW, fH);
 
   for a := 0 to High(gMonsters) do
+  begin
     if gMonsters[a] <> nil then
     begin
       e_TextureFontPrint(gMonsters[a].FObj.X + gMonsters[a].FObj.Rect.X,
       gMonsters[a].FObj.Y + gMonsters[a].FObj.Rect.Y + gMonsters[a].FObj.Rect.Height - fH,
       IntToStr(gMonsters[a].FHealth), gStdFont);
     end;
+  end;
 end;
 
 function g_Monsters_ByUID (UID: Word): TMonster;
-//var a: Integer;
 begin
   result := uidMap[UID];
-  {
-  Result := nil;
-  if gMonsters <> nil then
-    for a := 0 to High(gMonsters) do
-      if (gMonsters[a] <> nil) and
-         (gMonsters[a].FUID = UID) then
-      begin
-        Result := gMonsters[a];
-        Break;
-      end;
-  }
 end;
 
 procedure g_Monsters_SaveState(var Mem: TBinMemoryWriter);
@@ -1376,11 +1384,16 @@ var
 begin
 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ìîíñòðîâ:
   count := 0;
-  if gMonsters <> nil then
+  if (gMonsters <> nil) then
+  begin
     for i := 0 to High(gMonsters) do
-      if gMonsters[i] <> nil then
-        if gMonsters[i].FMonsterType <> MONSTER_NONE then
-          count := count + 1;
+    begin
+      if (gMonsters[i] <> nil) then
+      begin
+        if (gMonsters[i].FMonsterType <> MONSTER_NONE) then count += 1;
+      end;
+    end;
+  end;
 
   Mem := TBinMemoryWriter.Create((count+1) * 350);
 
@@ -1398,15 +1411,19 @@ begin
 
 // Ñîõðàíÿåì ìîíñòðîâ:
   for i := 0 to High(gMonsters) do
-    if gMonsters[i] <> nil then
-      if gMonsters[i].FMonsterType <> MONSTER_NONE then
+  begin
+    if (gMonsters[i] <> nil) then
+    begin
+      if (gMonsters[i].FMonsterType <> MONSTER_NONE) then
       begin
-      // Òèï ìîíñòðà:
+        // Òèï ìîíñòðà:
         b := gMonsters[i].MonsterType;
         Mem.WriteByte(b);
-      // Ñîõðàíÿåì äàííûå ìîíñòðà:
+        // Ñîõðàíÿåì äàííûå ìîíñòðà:
         gMonsters[i].SaveState(Mem);
       end;
+    end;
+  end;
 end;
 
 procedure g_Monsters_LoadState(var Mem: TBinMemoryReader);
@@ -1417,7 +1434,7 @@ var
 begin
   if Mem = nil then exit;
 
-  g_Monsters_Free();
+  g_Monsters_Free(false);
 
   // Çàãðóæàåì èíôîðìàöèþ öåëåóêàçàòåëÿ
   Mem.ReadInt(pt_x);
@@ -1480,6 +1497,11 @@ end;
 
 { T M o n s t e r : }
 
+procedure TMonster.setGameX (v: Integer); inline; begin FObj.X := v; positionChanged(); end;
+procedure TMonster.setGameY (v: Integer); inline; begin FObj.Y := v; positionChanged(); end;
+procedure TMonster.setPosition (ax, ay: Integer; callPosChanged: Boolean=true); inline; begin FObj.X := ax; FObj.Y := ay; if callPosChanged then positionChanged(); end;
+
+
 procedure TMonster.ActionSound();
 begin
   case FMonsterType of
@@ -1668,8 +1690,8 @@ begin
   FObj.Accel.X := 0;
   FObj.Accel.Y := 0;
   FDirection := FStartDirection;
-  GameX := FStartX;
-  GameY := FStartY;
+  {GameX}FObj.X := FStartX;
+  {GameY}FObj.Y := FStartY;
   FObj.Rect := MONSTERTABLE[FMonsterType].Rect;
   FHealth := MONSTERTABLE[FMonsterType].Health;
   FAmmo := 0;
@@ -1980,7 +2002,7 @@ begin
   Result := False;
   if g_Game_IsClient then
     Exit;
-  if not Live then
+  if not alive then
     Exit;
 
   if FHealth < FMaxHealth then
@@ -2015,7 +2037,11 @@ begin
     mProxyId := -1;
   end;
 
-  if (mArrIdx <> -1) and (mArrIdx < Length(gMonsters)) then gMonsters[mArrIdx] := nil;
+  if (mArrIdx <> -1) and (mArrIdx < Length(gMonsters)) then
+  begin
+    freeInds.release(mArrIdx);
+    gMonsters[mArrIdx] := nil;
+  end;
   mArrIdx := -1;
 
   uidMap[FUID] := nil;
@@ -2040,9 +2066,12 @@ begin
                       o.Y+o.Rect.Y+o.Rect.Height-128, M_NONE);
 
 // Íå â îáëàñòè ðèñîâàíèÿ íå ðåñóåì:
-  if not g_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height,
-                   sX-128, sY-128, sWidth+256, sHeight+256) then
-    Exit;
+  if (g_dbg_scale = 1.0) then
+  begin
+    if not g_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height,
+                     sX-128, sY-128, sWidth+256, sHeight+256) then
+      Exit;
+  end;
 
 // Ýòè ìîíñòðû, óìèðàÿ, íå îñòàâëÿþò òðóïîâ:
   if FState = MONSTATE_DEAD then
@@ -2300,7 +2329,7 @@ begin
      (FObj.X > gMapInfo.Width+1000) or (FObj.Y < -1000) then
   begin
     FRemoved := True;
-    if Live and (gLMSRespawn = LMS_RESPAWN_NONE) then
+    if alive and (gLMSRespawn = LMS_RESPAWN_NONE) then
     begin
       Inc(gCoopMonstersKilled);
       if g_Game_IsNet then
@@ -2463,7 +2492,7 @@ begin
         // Åñëè åñòü èãðîê ðÿäîì, ïðîñûïàåìñÿ è èäåì ê íåìó:
           if (gPlayers <> nil) then
             for a := 0 to High(gPlayers) do
-              if (gPlayers[a] <> nil) and (gPlayers[a].Live)
+              if (gPlayers[a] <> nil) and (gPlayers[a].alive)
               and (not gPlayers[a].NoTarget) and (gPlayers[a].FMegaRulez[MR_INVIS] < gTime) then
                 with gPlayers[a] do
                   if g_Look(@FObj, @Obj, FDirection) then
@@ -2481,7 +2510,7 @@ begin
         // Åñëè åñòü ïîäõîäÿùèé ìîíñòð ðÿäîì:
           if gMonsters <> nil then
             for a := 0 to High(gMonsters) do
-              if (gMonsters[a] <> nil) and (gMonsters[a].Live) and
+              if (gMonsters[a] <> nil) and (gMonsters[a].alive) and
                  (gMonsters[a].FUID <> FUID) then
               begin
                 // Ìàíüÿêè íàïàäàþò íà âñåõ ìîíñòðîâ, êðîìå äðóçåé
@@ -2683,7 +2712,7 @@ begin
               if b > 1 then b := b * (Random(8 div b) + 1);
               for a := 0 to High(gGibs) do
               begin
-                if gGibs[a].Live and
+                if gGibs[a].alive and
                    g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y+FObj.Rect.Height-4,
                                  FObj.Rect.Width, 8, @gGibs[a].Obj) and (Random(3) = 0) then
                 begin
@@ -3515,7 +3544,7 @@ begin
               if b > 1 then b := b * (Random(8 div b) + 1);
               for a := 0 to High(gGibs) do
               begin
-                if gGibs[a].Live and
+                if gGibs[a].alive and
                    g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y+FObj.Rect.Height-4,
                                  FObj.Rect.Width, 8, @gGibs[a].Obj) and (Random(3) = 0) then
                 begin
@@ -3967,7 +3996,7 @@ begin
   if (gPlayers <> nil) and (FBehaviour <> BH_INSANE) and
   (FBehaviour <> BH_CANNIBAL) and (FBehaviour <> BH_GOOD) then
     for a := 0 to High(gPlayers) do
-      if (gPlayers[a] <> nil) and (gPlayers[a].Live)
+      if (gPlayers[a] <> nil) and (gPlayers[a].alive)
       and (not gPlayers[a].NoTarget) and (gPlayers[a].FMegaRulez[MR_INVIS] < gTime) then
       begin
         if g_Look(@FObj, @gPlayers[a].Obj, FDirection) then
@@ -3987,7 +4016,7 @@ begin
   // Êèëëåðû è äîáðûå íå òðîãàþò ìîíñòðîâ
   if (gMonsters <> nil) and (FBehaviour <> BH_KILLER) and (FBehaviour <> BH_GOOD) then
     for a := 0 to High(gMonsters) do
-      if (gMonsters[a] <> nil) and (gMonsters[a].Live) and
+      if (gMonsters[a] <> nil) and (gMonsters[a].alive) and
          (gMonsters[a].FUID <> FUID) then
       begin
         if (FBehaviour = BH_CANNIBAL) and (gMonsters[a].FMonsterType <> FMonsterType) then
@@ -4240,7 +4269,7 @@ begin
   Result := True;
 end;
 
-function TMonster.Live(): Boolean;
+function TMonster.alive(): Boolean;
 begin
   Result := (FState <> MONSTATE_DIE) and (FState <> MONSTATE_DEAD) and (FHealth > 0);
 end;
@@ -4345,6 +4374,7 @@ begin
   end;
 end;
 
+
 procedure TMonster.LoadState(var Mem: TBinMemoryReader);
 var
   i: Integer;
@@ -4361,8 +4391,13 @@ begin
   begin
     raise EBinSizeError.Create('TMonster.LoadState: Wrong Monster Signature');
   end;
+  if (uidMap[FUID] <> nil) and (uidMap[FUID] <> self) then raise Exception.Create('internal error in monster loader (0)');
+  uidMap[FUID] := nil;
 // UID ìîíñòðà:
   Mem.ReadWord(FUID);
+  //if (arrIdx = -1) then raise Exception.Create('internal error in monster loader');
+  if (uidMap[FUID] <> nil) then raise Exception.Create('internal error in monster loader (1)');
+  uidMap[FUID] := self;
 // Íàïðàâëåíèå:
   Mem.ReadByte(b);
   if b = 1 then
@@ -4542,7 +4577,7 @@ begin
   for idx := 0 to High(gMonsters) do
   begin
     mon := gMonsters[idx];
-    if (mon <> nil) and mon.Live then
+    if (mon <> nil) and mon.alive then
     begin
       result := cb(mon);
       if result then exit;
@@ -4555,7 +4590,7 @@ function g_Mons_IsAnyAliveAt (x, y: Integer; width, height: Integer): Boolean;
 
   function monsCollCheck (mon: TMonster; atag: Integer): Boolean;
   begin
-    result := mon.Live;// and g_Obj_Collide(x, y, width, height, @mon.Obj));
+    result := mon.alive;// and g_Obj_Collide(x, y, width, height, @mon.Obj));
   end;
 
 var
@@ -4573,7 +4608,7 @@ begin
     for idx := 0 to High(gMonsters) do
     begin
       mon := gMonsters[idx];
-      if (mon <> nil) and mon.Live then
+      if (mon <> nil) and mon.alive then
       begin
         if g_Obj_Collide(x, y, width, height, @mon.Obj) then
         begin
@@ -4608,7 +4643,7 @@ begin
     for idx := 0 to High(gMonsters) do
     begin
       mon := gMonsters[idx];
-      if (mon <> nil) and mon.Live then
+      if (mon <> nil) and mon.alive then
       begin
         if g_Obj_Collide(x, y, width, height, @mon.Obj) then
         begin
@@ -4626,8 +4661,8 @@ function g_Mons_ForEachAliveAt (x, y: Integer; width, height: Integer; cb: TEach
   function monsCollCheck (mon: TMonster; atag: Integer): Boolean;
   begin
     //result := false;
-    //if mon.Live and g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon);
-    if mon.Live then result := cb(mon) else result := false;
+    //if mon.alive and g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon);
+    if mon.alive then result := cb(mon) else result := false;
   end;
 
 var
@@ -4652,7 +4687,7 @@ begin
     for idx := 0 to High(gMonsters) do
     begin
       mon := gMonsters[idx];
-      if (mon <> nil) and mon.Live then
+      if (mon <> nil) and mon.alive then
       begin
         if g_Obj_Collide(x, y, width, height, @mon.Obj) then
         begin