index 59b5506a5ee732d02bde51f54f49820dedc7a1c2..d577b235b946c5d6ff143ba4c7930c34af7446e0 100644 (file)
--- a/src/game/g_monsters.pas
+++ b/src/game/g_monsters.pas
public
FNoRespawn: Boolean;
FFireTime: Integer;
+ trapCheckFrameId: DWord; // for `g_weapons.CheckTrap()`
constructor Create(MonsterType: Byte; aID: Integer; ForcedUID: Integer = -1);
destructor Destroy(); override;
property StartID: Integer read FStartID;
end;
-procedure g_Monsters_LoadData();
-procedure g_Monsters_FreeData();
-procedure g_Monsters_Init();
-procedure g_Monsters_Free();
-function g_Monsters_Create(MonsterType: Byte; X, Y: Integer;
- Direction: TDirection; AdjCoord: Boolean = False; ForcedUID: Integer = -1): TMonster;
-procedure g_Monsters_Update();
-procedure g_Monsters_Draw();
-procedure g_Monsters_DrawHealth();
-function g_Monsters_Get(UID: Word): TMonster;
-procedure g_Monsters_killedp();
-procedure g_Monsters_SaveState(var Mem: TBinMemoryWriter);
-procedure g_Monsters_LoadState(var Mem: TBinMemoryReader);
-function g_Monsters_GetIDByName(name: String): Integer;
-function g_Monsters_GetNameByID(MonsterType: Byte): String;
-function g_Monsters_GetKilledBy(MonsterType: Byte): String;
+procedure g_Monsters_LoadData ();
+procedure g_Monsters_FreeData ();
+procedure g_Monsters_Init ();
+procedure g_Monsters_Free ();
+function g_Monsters_Create (MonsterType: Byte; X, Y: Integer; Direction: TDirection;
+ AdjCoord: Boolean = False; ForcedUID: Integer = -1): TMonster;
+procedure g_Monsters_Update ();
+procedure g_Monsters_Draw ();
+procedure g_Monsters_DrawHealth ();
+function g_Monsters_ByUID (UID: Word): TMonster;
+procedure g_Monsters_killedp ();
+procedure g_Monsters_SaveState (var Mem: TBinMemoryWriter);
+procedure g_Monsters_LoadState (var Mem: TBinMemoryReader);
+function g_Monsters_GetIDByName (name: String): Integer;
+function g_Monsters_GetNameByID (MonsterType: Byte): String;
+function g_Monsters_GetKilledBy (MonsterType: Byte): String;
-type
- TEachMonsterCB = function (monidx: Integer; mon: TMonster): Boolean is nested; // return `true` to stop
-function g_Mons_ForEach (cb: TEachMonsterCB): Boolean;
+type
+ TEachMonsterCB = function (mon: TMonster): Boolean is nested; // return `true` to stop
// throws on invalid uid
function g_Mons_ByIdx (uid: Integer): TMonster; inline;
function g_Mons_IsAnyAliveAt (x, y: Integer; width, height: Integer): Boolean;
+function g_Mons_ForEach (cb: TEachMonsterCB): Boolean;
+function g_Mons_ForEachAlive (cb: TEachMonsterCB): Boolean;
+
function g_Mons_ForEachAt (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean;
-function g_Mons_ForEachAtAlive (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean;
+function g_Mons_ForEachAliveAt (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean;
+
+function g_Mons_getNewTrapFrameId (): DWord;
var
g_language, g_netmsg, z_aabbtree;
+// ////////////////////////////////////////////////////////////////////////// //
+var
+ monCheckTrapLastFrameId: DWord;
+
+
// ////////////////////////////////////////////////////////////////////////// //
type
TDynAABBTreeMonsBase = specialize TDynAABBTreeBase<TMonster>;
var
gMonsters: array of TMonster;
+ uidMap: array [0..65535] of TMonster; // monster knows it's index
+
+
+procedure clearUidMap ();
+var
+ idx: Integer;
+begin
+ for idx := 0 to High(uidMap) do uidMap[idx] := nil;
+end;
+
+
+function g_Mons_getNewTrapFrameId (): DWord;
+var
+ f: Integer;
+begin
+ Inc(monCheckTrapLastFrameId);
+ if monCheckTrapLastFrameId = 0 then
+ begin
+ // wraparound
+ monCheckTrapLastFrameId := 1;
+ for f := 0 to High(gMonsters) do
+ begin
+ if (gMonsters[f] <> nil) then gMonsters[f].trapCheckFrameId := 0;
+ end;
+ end;
+ result := monCheckTrapLastFrameId;
+end;
+
var
pt_x: Integer = 0;
Result := False;
end;
+
function BehaviourDamage(SpawnerUID: Word; BH, SelfType: Byte): Boolean;
var
m: TMonster;
UIDType := g_GetUIDType(SpawnerUID);
if UIDType = UID_MONSTER then
begin
- m := g_Monsters_Get(SpawnerUID);
+ m := g_Monsters_ByUID(SpawnerUID);
if m = nil then Exit;
MonsterType := m.FMonsterType;
end;
end;
end;
+
function canShoot(m: Byte): Boolean;
begin
Result := False;
end;
end;
-function isCorpse(o: PObj; immediately: Boolean): Integer;
+
+function isCorpse (o: PObj; immediately: Boolean): Integer;
function monsCollCheck (mon: TMonster; atag: Integer): Boolean;
begin
g_Sound_CreateWADEx('SOUND_MONSTER_FISH_ATTACK', GameWAD+':MSOUNDS\FISH_ATTACK');
monsTree := TDynAABBTreeMons.Create();
+ clearUidMap();
+ monCheckTrapLastFrameId := 0;
end;
procedure g_Monsters_FreeData();
var
a: Integer;
begin
- if gMonsters <> nil then
- for a := 0 to High(gMonsters) do
- gMonsters[a].Free();
-
monsTree.reset();
+ for a := 0 to High(gMonsters) do gMonsters[a].Free();
gMonsters := nil;
+ clearUidMap();
+ monCheckTrapLastFrameId := 0;
end;
function g_Monsters_Create(MonsterType: Byte; X, Y: Integer;
mon.arrIdx := find_id;
mon.treeNode := -1;
+ uidMap[mon.FUID] := mon;
+
// Íàñòðàèâàåì ïîëîæåíèå
with mon do
begin
var
a: Integer;
begin
-// Öåëåóêàçàòåëü:
+ // Öåëåóêàçàòåëü
if gTime mod (GAME_TICK*2) = 0 then
begin
pt_x := pt_x+pt_xs;
pt_y := pt_y+pt_ys;
- if Abs(pt_x) > 246 then
- pt_xs := -pt_xs;
- if Abs(pt_y) > 100 then
- pt_ys := -pt_ys;
+ if abs(pt_x) > 246 then pt_xs := -pt_xs;
+ if abs(pt_y) > 100 then pt_ys := -pt_ys;
end;
gMon := True; // Äëÿ ðàáîòû BlockMon'à
- if gMonsters <> nil then
- for a := 0 to High(gMonsters) do
- if (gMonsters[a] <> nil) then
- if not gMonsters[a].FRemoved then
- begin
- if g_Game_IsClient then
- gMonsters[a].ClientUpdate()
- else
- gMonsters[a].Update();
- end
- else
- begin
- gMonsters[a].Free();
- gMonsters[a] := nil;
- end;
+ for a := 0 to High(gMonsters) do
+ begin
+ if (gMonsters[a] = nil) then continue;
+ if not gMonsters[a].FRemoved then
+ begin
+ if g_Game_IsClient then
+ gMonsters[a].ClientUpdate()
+ else
+ gMonsters[a].Update();
+ end
+ else
+ begin
+ gMonsters[a].Free();
+ gMonsters[a] := nil;
+ end;
+ end;
gMon := False;
end;
end;
end;
-function g_Monsters_Get(UID: Word): TMonster;
-var
- a: Integer;
+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
Result := gMonsters[a];
Break;
end;
+ }
end;
procedure g_Monsters_SaveState(var Mem: TBinMemoryWriter);
treeNode := -1;
arrIdx := -1;
+ trapCheckFrameId := 0;
if FMonsterType in [MONSTER_ROBO, MONSTER_BARREL] then
FBloodKind := BLOOD_SPARKS
end;
// Òåïåðü öåëü - óäàðèâøèé, åñëè òîëüêî íå ñàì ñåáÿ:
- if (SpawnerUID <> FUID) and
- (BehaviourDamage(SpawnerUID, FBehaviour, FMonsterType)) then
+ if (SpawnerUID <> FUID) and (BehaviourDamage(SpawnerUID, FBehaviour, FMonsterType)) then
begin
FTargetUID := SpawnerUID;
FTargetTime := 0;
if FHealth < FMaxHealth then
begin
IncMax(FHealth, Value, FMaxHealth);
- if g_Game_IsServer and g_Game_IsNet then
- MH_SEND_MonsterState(FUID);
+ if g_Game_IsServer and g_Game_IsNet then MH_SEND_MonsterState(FUID);
Result := True;
end;
end;
if (treeNode <> -1) then
begin
- {$IF DEFINED(D2F_DEBUG)}
- e_WriteLog(Format('monster #%d(%u): removed from tree; nodeid=%d', [arrIdx, UID, treeNode]), MSG_NOTIFY);
- {$ENDIF}
- monsTree.removeObject(treeNode);
+ if monsTree.isValidId(treeNode) then
+ begin
+ {$IF DEFINED(D2F_DEBUG)}
+ e_WriteLog(Format('monster #%d(%u): removed from tree; nodeid=%d', [arrIdx, UID, treeNode]), MSG_NOTIFY);
+ {$ENDIF}
+ monsTree.removeObject(treeNode);
+ end;
end;
+ if (arrIdx <> -1) then
+ begin
+ gMonsters[arrIdx] := nil;
+ end;
+
+ uidMap[FUID] := nil;
+
inherited Destroy();
end;
begin
FObj.Accel.X := FObj.Accel.X + vx;
FObj.Accel.Y := FObj.Accel.Y + vy;
- if g_Game_IsServer and g_Game_IsNet then
- MH_SEND_MonsterPos(FUID);
+ if g_Game_IsServer and g_Game_IsNet then MH_SEND_MonsterPos(FUID);
end;
procedure TMonster.SetState(State: Byte; ForceAnim: Byte = 255);
NET_GFX_TELE);
end;
- if g_Game_IsServer and g_Game_IsNet then
- MH_SEND_MonsterPos(FUID);
+ if g_Game_IsServer and g_Game_IsNet then MH_SEND_MonsterPos(FUID);
Result := True;
end;
mon.SetState(STATE_GO);
mon.FNoRespawn := True;
Inc(gTotalMonsters);
- if g_Game_IsNet then MH_SEND_MonsterSpawn(gMonsters[sx].UID);
+ if g_Game_IsNet then MH_SEND_MonsterSpawn(mon.UID);
end;
mon := g_Monsters_Create(MONSTER_SOUL, FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2),
mon.SetState(STATE_GO);
mon.FNoRespawn := True;
Inc(gTotalMonsters);
- if g_Game_IsNet then MH_SEND_MonsterSpawn(gMonsters[sx].UID);
+ if g_Game_IsNet then MH_SEND_MonsterSpawn(mon.UID);
end;
mon := g_Monsters_Create(MONSTER_SOUL, FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2)-15,
mon.SetState(STATE_GO);
mon.FNoRespawn := True;
Inc(gTotalMonsters);
- if g_Game_IsNet then MH_SEND_MonsterSpawn(gMonsters[sx].UID);
+ if g_Game_IsNet then MH_SEND_MonsterSpawn(mon.UID);
end;
- if g_Game_IsNet then MH_SEND_CoopStats(gMonsters[sx].UID);
+
+ if g_Game_IsNet then MH_SEND_CoopStats();
end;
// Ó ýòèõ ìîíñòðîâ íåò òðóïîâ:
mon.shoot(@o, True);
Inc(gTotalMonsters);
- if g_Game_IsNet then MH_SEND_MonsterSpawn(gMonsters[sx].UID);
+ if g_Game_IsNet then MH_SEND_MonsterSpawn(mon.UID);
end;
end;
end;
begin
FFireTime := 100;
FFireAttacker := Attacker;
- if g_Game_IsNet and g_Game_IsServer then
- MH_SEND_MonsterState(FUID);
+ if g_Game_IsNet and g_Game_IsServer then MH_SEND_MonsterState(FUID);
end;
procedure TMonster.OnFireFlame(Times: DWORD = 1);
// ////////////////////////////////////////////////////////////////////////// //
+// throws on invalid uid
+function g_Mons_ByIdx (uid: Integer): TMonster; inline;
+begin
+ result := g_Mons_ByIdx_NC(uid);
+ 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
+ if (uid < 0) or (uid > High(gMonsters)) then begin result := nil; exit; end;
+ result := gMonsters[uid];
+end;
+
+
function g_Mons_ForEach (cb: TEachMonsterCB): Boolean;
var
idx: Integer;
+ mon: TMonster;
begin
result := false;
if (gMonsters = nil) or not assigned(cb) then exit;
for idx := 0 to High(gMonsters) do
begin
- if (gMonsters[idx] <> nil) then
+ mon := gMonsters[idx];
+ if (mon <> nil) then
begin
- result := cb(idx, gMonsters[idx]);
+ result := cb(mon);
if result then exit;
end;
end;
end;
-// throws on invalid uid
-function g_Mons_ByIdx (uid: Integer): TMonster; inline;
-begin
- result := g_Mons_ByIdx_NC(uid);
- 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;
+function g_Mons_ForEachAlive (cb: TEachMonsterCB): Boolean;
+var
+ idx: Integer;
+ mon: TMonster;
begin
- if (uid < 0) or (uid > High(gMonsters)) then begin result := nil; exit; end;
- result := gMonsters[uid];
+ result := false;
+ if (gMonsters = nil) or not assigned(cb) then exit;
+ for idx := 0 to High(gMonsters) do
+ begin
+ mon := gMonsters[idx];
+ if (mon <> nil) and mon.Live then
+ begin
+ result := cb(mon);
+ if result then exit;
+ end;
+ end;
end;
@@ -4413,10 +4485,10 @@ function g_Mons_IsAnyAliveAt (x, y: Integer; width, height: Integer): Boolean;
var
idx: Integer;
+ mon: TMonster;
begin
result := false;
if (width < 1) or (height < 1) then exit;
-
if gmon_debug_use_sqaccel then
begin
result := (monsTree.aabbQuery(x, y, width, height, monsCollCheck) <> nil);
begin
for idx := 0 to High(gMonsters) do
begin
- if (gMonsters[idx] <> nil) and gMonsters[idx].Live then
+ mon := gMonsters[idx];
+ if (mon <> nil) and mon.Live then
begin
- if g_Obj_Collide(x, y, width, height, @gMonsters[idx].Obj) then
+ if g_Obj_Collide(x, y, width, height, @mon.Obj) then
begin
result := true;
exit;
@@ -4443,15 +4516,15 @@ function g_Mons_ForEachAt (x, y: Integer; width, height: Integer; cb: TEachMonst
function monsCollCheck (mon: TMonster; atag: Integer): Boolean;
begin
result := false;
- if g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon.arrIdx, mon);
+ if g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon);
end;
var
idx: Integer;
+ mon: TMonster;
begin
result := false;
if (width < 1) or (height < 1) then exit;
-
if gmon_debug_use_sqaccel then
begin
result := (monsTree.aabbQuery(x, y, width, height, monsCollCheck) <> nil);
begin
for idx := 0 to High(gMonsters) do
begin
- if (gMonsters[idx] <> nil) and gMonsters[idx].Live then
+ mon := gMonsters[idx];
+ if (mon <> nil) and mon.Live then
begin
- if g_Obj_Collide(x, y, width, height, @gMonsters[idx].Obj) then
+ if g_Obj_Collide(x, y, width, height, @mon.Obj) then
begin
- result := cb(idx, gMonsters[idx]);
+ result := cb(mon);
if result then exit;
end;
end;
end;
-function g_Mons_ForEachAtAlive (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean;
+function g_Mons_ForEachAliveAt (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean;
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.arrIdx, mon);
+ if mon.Live and g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon);
end;
var
idx: Integer;
+ mon: TMonster;
begin
result := false;
if (width < 1) or (height < 1) then exit;
-
if gmon_debug_use_sqaccel then
begin
if (width = 1) and (height = 1) then
begin
for idx := 0 to High(gMonsters) do
begin
- if (gMonsters[idx] <> nil) and gMonsters[idx].Live then
+ mon := gMonsters[idx];
+ if (mon <> nil) and mon.Live then
begin
- if g_Obj_Collide(x, y, width, height, @gMonsters[idx].Obj) then
+ if g_Obj_Collide(x, y, width, height, @mon.Obj) then
begin
- result := cb(idx, gMonsters[idx]);
+ result := cb(mon);
if result then exit;
end;
end;