DEADSOFTWARE

fixed(?) loading saved games
[d2df-sdl.git] / src / game / g_monsters.pas
index 05ec0b05e12524832d707d295b6ea4343a307e27..04e19241b541fb591877fa8e7f4089cf233ddeb6 100644 (file)
@@ -14,6 +14,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
 {$INCLUDE ../shared/a_modes.inc}
+{.$DEFINE D2F_DEBUG_MONS_MOVE}
 unit g_monsters;
 
 interface
@@ -79,15 +80,8 @@ type
     FFirePainTime: Integer;
     FFireAttacker: Word;
     vilefire: TAnimation;
-
-  {$IF DEFINED(D2F_DEBUG)}
-  public
-  {$ENDIF}
-    proxyId: Integer; // node in dyntree or -1
-    arrIdx: Integer; // in gMonsters
-  {$IF DEFINED(D2F_DEBUG)}
-  private
-  {$ENDIF}
+    mProxyId: Integer; // node in dyntree or -1
+    mArrIdx: Integer; // in gMonsters
 
     FDieTriggers: Array of Integer;
     FSpawnTrigger: Integer;
@@ -167,6 +161,9 @@ type
     property GameDirection: TDirection read FDirection write FDirection;
 
     property StartID: Integer read FStartID;
+
+    property proxyId: Integer read mProxyId;
+    property arrIdx: Integer read mArrIdx;
   end;
 
 
@@ -176,7 +173,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 ();
@@ -238,7 +235,7 @@ type
   TMonsterGrid = specialize TBodyGridBase<TMonster>;
 
 var
-  monsGrid: TMonsterGrid = nil;
+  monsGrid: TMonsterGrid = nil; // DO NOT USE! public for debugging only!
 
 
 var
@@ -308,35 +305,46 @@ end;
 //WARNING! call this after monster position was changed, or coldet will not work right!
 procedure TMonster.positionChanged ();
 var
-  x, y: Integer;
+  x, y, w, h: Integer;
+  nx, ny, nw, nh: Integer;
 begin
   {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}
-  //e_WriteLog(Format('monster #%d(%u): pos=(%d,%d); rpos=(%d,%d)', [arrIdx, UID, FObj.X, FObj.Y, FObj.Rect.X, FObj.Rect.Y]), MSG_NOTIFY);
+  //e_WriteLog(Format('monster #%d(%u): pos=(%d,%d); rpos=(%d,%d)', [mArrIdx, UID, FObj.X, FObj.Y, FObj.Rect.X, FObj.Rect.Y]), MSG_NOTIFY);
   {$ENDIF}
-  if (proxyId = -1) then
+  if (mProxyId = -1) then
   begin
-    proxyId := monsGrid.insertBody(self, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height);
+    mProxyId := monsGrid.insertBody(self, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height);
     {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}
-    monsGrid.getBodyXY(proxyId, x, y);
-    e_WriteLog(Format('monster #%d(%u): inserted into the grid; proxyid=%d; x=%d; y=%d', [arrIdx, UID, proxyId, x, y]), MSG_NOTIFY);
+    monsGrid.getBodyXY(mProxyId, x, y);
+    e_WriteLog(Format('monster #%d:(%u): inserted into the grid; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY);
     {$ENDIF}
   end
   else
   begin
-    monsGrid.getBodyXY(proxyId, x, y);
-    if (FObj.X+FObj.Rect.X = x) and (FObj.Y+FObj.Rect.Y = y) then exit; // nothing to do
-    {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}e_WriteLog(Format('monster #%d(%u): updating tree; proxyid=%d; x=%d; y=%d', [arrIdx, UID, proxyId, x, y]), MSG_NOTIFY);{$ENDIF}
-
-    {$IF TRUE}
-    monsGrid.moveBody(proxyId, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y);
-    {$ELSE}
-    monsGrid.removeBody(proxyId);
-    proxyId := monsGrid.insertBody(self, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height);
-    {$ENDIF}
+    monsGrid.getBodyDims(mProxyId, x, y, w, h);
+    getMapBox(nx, ny, nw, nh);
 
+    if (w <> nw) or (h <> nh) then
+    begin
+      {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}
+      e_WriteLog(Format('monster #%d:(%u): resized; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY);
+      {$ENDIF}
+      monsGrid.moveResizeBody(mProxyId, nx, ny, nw, nh);
+    end
+    else if (x <> nx) or (y <> ny) then
+    begin
+      {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}
+      e_WriteLog(Format('monster #%d:(%u): updating grid; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY);
+      {$ENDIF}
+      monsGrid.moveBody(mProxyId, nx, ny);
+    end
+    else
+    begin
+      exit; // nothing to do
+    end;
     {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}
-    monsGrid.getBodyXY(proxyId, x, y);
-    e_WriteLog(Format('monster #%d(%u): updated tree; proxyid=%d; x=%d; y=%d', [arrIdx, UID, proxyId, x, y]), MSG_NOTIFY);
+    monsGrid.getBodyXY(mProxyId, x, y);
+    e_WriteLog(Format('monster #%d:(%u): updated grid; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY);
     {$ENDIF}
   end;
 end;
@@ -698,7 +706,7 @@ begin
   if gmon_debug_use_sqaccel then
   begin
     mon := monsGrid.forEachInAABB(o.X+o.Rect.X, o.Y+o.Rect.Y, o.Rect.Width, o.Rect.Height, monsCollCheck);
-    if (mon <> nil) then result := mon.arrIdx;
+    if (mon <> nil) then result := mon.mArrIdx;
   end
   else
   begin
@@ -1183,12 +1191,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();
@@ -1201,6 +1213,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;
 
 
@@ -1226,8 +1240,8 @@ begin
 
   mon := TMonster.Create(MonsterType, find_id, ForcedUID);
   gMonsters[find_id] := mon;
-  mon.arrIdx := find_id;
-  mon.proxyId := -1;
+  mon.mArrIdx := find_id;
+  mon.mProxyId := -1;
 
   uidMap[mon.FUID] := mon;
 
@@ -1409,7 +1423,7 @@ var
 begin
   if Mem = nil then exit;
 
-  g_Monsters_Free();
+  g_Monsters_Free(false);
 
   // Çàãðóæàåì èíôîðìàöèþ öåëåóêàçàòåëÿ
   Mem.ReadInt(pt_x);
@@ -1718,8 +1732,8 @@ begin
   FFirePainTime := 0;
   FFireAttacker := 0;
 
-  proxyId := -1;
-  arrIdx := -1;
+  mProxyId := -1;
+  mArrIdx := -1;
   trapCheckFrameId := 0;
 
   if FMonsterType in [MONSTER_ROBO, MONSTER_BARREL] then
@@ -1933,6 +1947,7 @@ begin
       begin
         FObj.Rect.Y := FObj.Rect.Y + FObj.Rect.Height-12;
         FObj.Rect.Height := 12;
+        positionChanged();
       end;
 
     // Óðîí áûë ñèëüíûì => ñëàáûå - â êàøó:
@@ -1994,20 +2009,20 @@ begin
 
   vilefire.Free();
 
-  if (proxyId <> -1) then
+  if (mProxyId <> -1) then
   begin
     if (monsGrid <> nil) then
     begin
-      monsGrid.removeBody(proxyId);
+      monsGrid.removeBody(mProxyId);
       {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}
-      e_WriteLog(Format('monster #%d(%u): removed from tree; proxyid=%d', [arrIdx, UID, proxyId]), MSG_NOTIFY);
+      e_WriteLog(Format('monster #%d:(%u): removed from grid; mProxyid=%d', [mArrIdx, UID, mProxyId]), MSG_NOTIFY);
       {$ENDIF}
     end;
-    proxyId := -1;
+    mProxyId := -1;
   end;
 
-  if (arrIdx <> -1) and (arrIdx < Length(gMonsters)) then gMonsters[arrIdx] := nil;
-  arrIdx := -1;
+  if (mArrIdx <> -1) and (mArrIdx < Length(gMonsters)) then gMonsters[mArrIdx] := nil;
+  mArrIdx := -1;
 
   uidMap[FUID] := nil;
 
@@ -4336,6 +4351,10 @@ begin
   end;
 end;
 
+procedure monsPostLoad ();
+begin
+end;
+
 procedure TMonster.LoadState(var Mem: TBinMemoryReader);
 var
   i: Integer;
@@ -4352,8 +4371,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
@@ -4656,4 +4680,6 @@ begin
 end;
 
 
+begin
+  g_SetPostLoadHook(monsPostLoad);
 end.