index 05ec0b05e12524832d707d295b6ea4343a307e27..04e19241b541fb591877fa8e7f4089cf233ddeb6 100644 (file)
--- a/src/game/g_monsters.pas
+++ b/src/game/g_monsters.pas
* 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
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;
property GameDirection: TDirection read FDirection write FDirection;
property StartID: Integer read FStartID;
+
+ property proxyId: Integer read mProxyId;
+ property arrIdx: Integer read mArrIdx;
end;
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 ();
TMonsterGrid = specialize TBodyGridBase<TMonster>;
var
- monsGrid: TMonsterGrid = nil;
+ monsGrid: TMonsterGrid = nil; // DO NOT USE! public for debugging only!
var
//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;
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
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();
begin
monsGrid.Free();
monsGrid := TMonsterGrid.Create(x, y, w, h);
+ //clearUidMap(); // why not?
+ e_LogWritefln('%s', ['Recreated monster tree']);
end;
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;
begin
if Mem = nil then exit;
- g_Monsters_Free();
+ g_Monsters_Free(false);
// Çàãðóæàåì èíôîðìàöèþ öåëåóêàçàòåëÿ
Mem.ReadInt(pt_x);
FFirePainTime := 0;
FFireAttacker := 0;
- proxyId := -1;
- arrIdx := -1;
+ mProxyId := -1;
+ mArrIdx := -1;
trapCheckFrameId := 0;
if FMonsterType in [MONSTER_ROBO, MONSTER_BARREL] then
begin
FObj.Rect.Y := FObj.Rect.Y + FObj.Rect.Height-12;
FObj.Rect.Height := 12;
+ positionChanged();
end;
// Óðîí áûë ñèëüíûì => ñëàáûå - â êàøó:
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;
end;
end;
+procedure monsPostLoad ();
+begin
+end;
+
procedure TMonster.LoadState(var Mem: TBinMemoryReader);
var
i: Integer;
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
end;
+begin
+ g_SetPostLoadHook(monsPostLoad);
end.