Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add friendly NPCs to party state #496

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BossMod/Data/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public sealed class Actor(ulong instanceID, uint oid, int spawnIndex, string nam
public Angle Rotation => PosRot.W.Radians();
public bool Omnidirectional => Utils.CharacterIsOmnidirectional(OID);
public bool IsDeadOrDestroyed => IsDead || IsDestroyed;
public bool IsFriendlyNPC => Type == ActorType.Enemy && IsAlly && IsTargetable;

public ActorStatus? FindStatus(uint sid)
{
Expand Down
26 changes: 24 additions & 2 deletions BossMod/Data/PartyState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
// if player does not exist in world, party is always empty; otherwise player is always in slot 0
// in alliance, two 'other' groups use slots 8-15 and 16-23; alliance members don't have content-ID, but always have actor-ID
// in trust, buddies are considered party members with content-id 0 (but non-zero actor id, they are always in world)
// slots 24-63 are occupied by friendly NPCs, i.e. actors with type = Enemy who have the IsAlly and IsTargetable flags set
// certain modules need to treat NPCs as regular party members for the purpose of mechanic resolution
// we limit to 64 slots to facilitate a bitmask for the entire "party" state fitting inside one ulong
// party slot is considered 'empty' if both ids are 0
public sealed class PartyState
{
public const int PlayerSlot = 0;
public const int MaxPartySize = 8;
public const int MaxAllianceSize = 24;
public const int MaxAllies = 64;

public record struct Member(ulong ContentId, ulong InstanceId, bool InCutscene, string Name)
{
Expand All @@ -21,8 +25,8 @@ public record struct Member(ulong ContentId, ulong InstanceId, bool InCutscene,
}
public static readonly Member EmptySlot = new(0, 0, false, "");

public readonly Member[] Members = Utils.MakeArray(MaxAllianceSize, EmptySlot);
private readonly Actor?[] _actors = new Actor?[MaxAllianceSize]; // transient
public readonly Member[] Members = Utils.MakeArray(MaxAllies, EmptySlot);
private readonly Actor?[] _actors = new Actor?[MaxAllies]; // transient

public Actor? this[int slot] => (slot >= 0 && slot < _actors.Length) ? _actors[slot] : null; // bounds-checking accessor
public Actor? Player() => this[PlayerSlot];
Expand Down Expand Up @@ -56,6 +60,15 @@ public IEnumerable<Actor> WithoutSlot(bool includeDead = false, bool partyOnly =
continue;
yield return player;
}
for (int i = MaxAllianceSize; i < MaxAllies; ++i)
{
var player = _actors[i];
if (player == null)
continue;
if (player.IsDead && !includeDead)
continue;
yield return player;
}
}

public IEnumerable<(int, Actor)> WithSlot(bool includeDead = false, bool partyOnly = false)
Expand All @@ -69,6 +82,15 @@ public IEnumerable<Actor> WithoutSlot(bool includeDead = false, bool partyOnly =
continue;
yield return (i, player);
}
for (int i = MaxAllianceSize; i < MaxAllies; ++i)
{
var player = _actors[i];
if (player == null)
continue;
if (player.IsDead && !includeDead)
continue;
yield return (i, player);
}
}

// find a slot index containing specified player (by instance ID); returns -1 if not found
Expand Down
36 changes: 36 additions & 0 deletions BossMod/Framework/WorldStateGameSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ private unsafe void UpdateParty()
var playerMember = UpdatePartyPlayer(replay, group);
UpdatePartyNormal(group, playerMember);
UpdatePartyAlliance(group);
UpdatePartyNPCs();

// update limit break
var lb = LimitBreakController.Instance();
Expand Down Expand Up @@ -501,6 +502,33 @@ private unsafe void UpdatePartyAlliance(GroupManager.Group* group)
}
}

private unsafe void UpdatePartyNPCs()
{
for (int i = PartyState.MaxAllianceSize; i < PartyState.MaxAllies; ++i)
{
ref var m = ref _ws.Party.Members[i];
if (m.InstanceId != 0)
{
var actor = _ws.Actors.Find(m.InstanceId);
if (!(actor?.IsFriendlyNPC ?? false))
UpdatePartySlot(i, PartyState.EmptySlot);
}
}
foreach (var actor in _ws.Actors)
{
if (!actor.IsFriendlyNPC)
continue;
if (_ws.Party.FindSlot(actor.InstanceID) == -1)
{
var slot = FindFreeNPCAllySlot();
if (slot > 0)
UpdatePartySlot(slot, new PartyState.Member(0, actor.InstanceID, false, actor.Name));
else
Service.Log($"[WorldState] Failed to find empty slot for allied NPC {actor.InstanceID:X}");
}
}
}

private unsafe bool HasBuddy(ulong instanceID)
{
var ui = UIState.Instance();
Expand All @@ -518,6 +546,14 @@ private int FindFreePartySlot()
return -1;
}

private int FindFreeNPCAllySlot()
{
for (int i = PartyState.MaxAllianceSize; i < PartyState.MaxAllies; ++i)
if (!_ws.Party.Members[i].IsValid())
return i;
return -1;
}

private unsafe PartyState.Member BuildPartyMember(PartyMember* m) => m != null ? new(m->ContentId, m->EntityId, (m->Flags & 0x10) != 0, m->NameString) : PartyState.EmptySlot;

private void AddPartyMember(PartyState.Member m)
Expand Down
Loading