X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;ds=inline;f=src%2Fgame%2Fg_monsters.pas;h=a437ff9bdcd13e7c1ea72bc56b75efe61c4a72cb;hb=15ef0fb2d44e381afb8d0ea07cdb31fd3903ba7b;hp=ea7222f3194fb0006e04a62ade5769fbd8f07e3e;hpb=966e88968e6a2ba968187fe82144874aae35d02a;p=d2df-sdl.git
diff --git a/src/game/g_monsters.pas b/src/game/g_monsters.pas
index ea7222f..a437ff9 100644
--- a/src/game/g_monsters.pas
+++ b/src/game/g_monsters.pas
@@ -14,6 +14,8 @@
* along with this program. If not, see .
*)
{$INCLUDE ../shared/a_modes.inc}
+{$M+}
+{.$DEFINE D2F_DEBUG_MONS_MOVE}
unit g_monsters;
interface
@@ -79,15 +81,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;
@@ -96,6 +91,9 @@ type
function findNewPrey(): Boolean;
procedure ActivateTriggers();
+ procedure setGameX (v: Integer); inline;
+ procedure setGameY (v: Integer); inline;
+
public
FNoRespawn: Boolean;
FFireTime: Integer;
@@ -140,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;
@@ -154,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;
@@ -176,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 ();
@@ -200,6 +206,8 @@ function g_Mons_ByIdx (uid: Integer): TMonster; inline;
// can return null
function g_Mons_ByIdx_NC (uid: Integer): TMonster; inline;
+function g_Mons_TotalCount (): Integer; inline;
+
function g_Mons_IsAnyAliveAt (x, y: Integer; width, height: Integer): Boolean;
function g_Mons_ForEach (cb: TEachMonsterCB): Boolean;
@@ -236,7 +244,7 @@ type
TMonsterGrid = specialize TBodyGridBase;
var
- monsGrid: TMonsterGrid = nil;
+ monsGrid: TMonsterGrid = nil; // DO NOT USE! public for debugging only!
var
@@ -250,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;
+
// ////////////////////////////////////////////////////////////////////////// //
@@ -306,35 +315,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;
@@ -442,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));
@@ -532,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 ();
@@ -539,6 +560,7 @@ var
idx: Integer;
begin
for idx := 0 to High(uidMap) do uidMap[idx] := nil;
+ freeInds.clear();
end;
@@ -568,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;
@@ -696,7 +705,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
@@ -956,6 +965,7 @@ begin
g_Sound_CreateWADEx('SOUND_MONSTER_FISH_ATTACK', GameWAD+':MSOUNDS\FISH_ATTACK');
+ freeInds := TIdPool.Create();
clearUidMap();
monCheckTrapLastFrameId := 0;
end;
@@ -1174,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();
@@ -1181,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();
@@ -1199,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;
@@ -1224,8 +1243,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;
@@ -1261,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
@@ -1274,6 +1296,9 @@ begin
g_Sound_PlayExAt('SOUND_MONSTER_TRUP', FObj.X, FObj.Y);
Exit;
end;
+ end;
+ end;
+ end;
end;
procedure g_Monsters_Update();
@@ -1320,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();
@@ -1334,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);
@@ -1366,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);
@@ -1388,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);
@@ -1407,7 +1434,7 @@ var
begin
if Mem = nil then exit;
- g_Monsters_Free();
+ g_Monsters_Free(false);
// Çàãðóæàåì èíôîðìàöèþ öåëåóêàçàòåëÿ
Mem.ReadInt(pt_x);
@@ -1470,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
@@ -1658,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;
@@ -1716,8 +1748,8 @@ begin
FFirePainTime := 0;
FFireAttacker := 0;
- proxyId := -1;
- arrIdx := -1;
+ mProxyId := -1;
+ mArrIdx := -1;
trapCheckFrameId := 0;
if FMonsterType in [MONSTER_ROBO, MONSTER_BARREL] then
@@ -1931,6 +1963,7 @@ begin
begin
FObj.Rect.Y := FObj.Rect.Y + FObj.Rect.Height-12;
FObj.Rect.Height := 12;
+ positionChanged();
end;
// Óðîí áûë ñèëüíûì => ñëàáûå - â êàøó:
@@ -1992,20 +2025,24 @@ 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
+ begin
+ freeInds.release(mArrIdx);
+ gMonsters[mArrIdx] := nil;
+ end;
+ mArrIdx := -1;
uidMap[FUID] := nil;
@@ -2029,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
@@ -4334,6 +4374,7 @@ begin
end;
end;
+
procedure TMonster.LoadState(var Mem: TBinMemoryReader);
var
i: Integer;
@@ -4350,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
@@ -4489,7 +4535,6 @@ begin
if (result = nil) then raise Exception.Create('g_Mons_ByIdx: invalid monster id');
end;
-
// can return null
function g_Mons_ByIdx_NC (uid: Integer): TMonster; inline;
begin
@@ -4497,6 +4542,11 @@ begin
result := gMonsters[uid];
end;
+function g_Mons_TotalCount (): Integer; inline;
+begin
+ result := Length(gMonsters);
+end;
+
function g_Mons_ForEach (cb: TEachMonsterCB): Boolean;
var