fix(factions): grudges survive an owner change and reach kin owned elsewhere
The grudge was a dictionary in the owner's memory: when another client took over the creature mid-fight, its copy made peace on the next UpdateTarget. Grudge spreads to kin also skipped kin owned by another client. The grudge now lives on the creature's ZDO (attacker ZDOID and an end time in network time), written only by the owner; kin owned elsewhere get an RPC that their owner handles.
This commit is contained in:
parent
811e4cac34
commit
7a618936f2
2 changed files with 77 additions and 49 deletions
|
|
@ -59,8 +59,9 @@ namespace RacesOfValheim
|
|||
"Without this the truce is absolute in both directions: MonsterAI.UpdateTarget " +
|
||||
"throws away any target BaseAI.IsEnemy calls a non-enemy, so a truced mob could " +
|
||||
"not fight back even after being struck - it stood still, or fled because it " +
|
||||
"was hurt and had no enemy it was allowed to have. The grudge is per attacker " +
|
||||
"and per creature, on whichever machine runs that creature's AI. 0 restores the " +
|
||||
"was hurt and had no enemy it was allowed to have. Each creature holds one " +
|
||||
"grudge, against the last player who hit it, and keeps it when another player's " +
|
||||
"game takes over running that creature. 0 restores the " +
|
||||
"old absolute truce.",
|
||||
new AcceptableValueRange<float>(0f, 600f)));
|
||||
|
||||
|
|
|
|||
|
|
@ -25,11 +25,13 @@ namespace RacesOfValheim
|
|||
/// which means everything downstream - acquiring, chasing, retaliating, giving up - is vanilla
|
||||
/// behaviour rather than anything reimplemented here.
|
||||
///
|
||||
/// WHERE IT RUNS. On the client that owns the mob's AI, which is the only one whose UpdateTarget
|
||||
/// matters and the only one that runs Character.ApplyDamage for it. BaseAI.OnDamaged is reached
|
||||
/// through the character's damage callback on exactly that machine, so the record is made where
|
||||
/// it will be read. Nothing is replicated and nothing needs to be: another client's copy of the
|
||||
/// same mob is not the one deciding who it chases.
|
||||
/// WHERE IT LIVES. On the creature's own ZDO: who it is angry at and until when, in network time.
|
||||
/// The AI that reads it runs on whichever machine owns the creature, and that can change in the
|
||||
/// middle of a fight (the owner walks off or logs out). A grudge kept in one machine's memory
|
||||
/// went with it, and the new owner's copy of the creature made peace on its next UpdateTarget.
|
||||
/// On the ZDO the new owner reads the same grudge. Only the owner writes it: a hit reaches the
|
||||
/// owner through Character.Damage's RPC anyway, and kin owned elsewhere are told by an RPC of
|
||||
/// our own (RpcName), so no client writes a ZDO it does not own.
|
||||
///
|
||||
/// Vanilla has its own version of this idea in BaseAI.SetAggravated, and it is not usable here:
|
||||
/// it does nothing unless the prefab sets m_aggravatable, which only the dvergr and the seekers
|
||||
|
|
@ -37,67 +39,96 @@ namespace RacesOfValheim
|
|||
/// </summary>
|
||||
public static class RaceGrudge
|
||||
{
|
||||
private struct Grudge
|
||||
{
|
||||
public ZDOID Attacker;
|
||||
public float Until;
|
||||
}
|
||||
private const string RpcName = "RacesOfValheim_Grudge";
|
||||
|
||||
// One grudge per mob, against the last thing that hit it. A creature can only chase one
|
||||
// target anyway, and if two players of the same race are both poking it, the one it wants
|
||||
// is whoever hit it last.
|
||||
private static readonly Dictionary<ZDOID, Grudge> grudges = new Dictionary<ZDOID, Grudge>();
|
||||
private static float lastSweep = 0f;
|
||||
// One grudge per creature, against the last player that hit it. A creature can only chase
|
||||
// one target anyway, and if two players of the same race are both poking it, the one it
|
||||
// wants is whoever hit it last.
|
||||
private static readonly KeyValuePair<int, int> AttackerKey = ZDO.GetHashZDOID("RacesOfValheim_GrudgeAttacker");
|
||||
private static readonly int UntilKey = "RacesOfValheim_GrudgeUntil".GetStableHashCode();
|
||||
|
||||
private static float Duration => FactionsConfig.GrudgeSeconds;
|
||||
|
||||
// Network time, the same on every machine, so an end time written by one owner means the
|
||||
// same thing to the next. Milliseconds in a long: ZDO has no double.
|
||||
private static long NowMs => ZNet.instance != null ? (long)(ZNet.instance.GetTimeSeconds() * 1000.0) : 0L;
|
||||
|
||||
private static ZDO ZdoOf(Character character)
|
||||
{
|
||||
if (character == null || ZDOMan.instance == null) return null;
|
||||
ZDOID id = character.GetZDOID();
|
||||
return id.IsNone() ? null : ZDOMan.instance.GetZDO(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True while <paramref name="monster"/> is owed a hit by <paramref name="player"/>. Both
|
||||
/// truce postfixes ask this first and leave IsEnemy alone when it answers yes.
|
||||
/// truce postfixes ask this first and leave IsEnemy alone when it answers yes. Read on any
|
||||
/// machine; it only matters on the owner's.
|
||||
/// </summary>
|
||||
public static bool Holds(Character monster, Character player)
|
||||
{
|
||||
if (monster == null || player == null) return false;
|
||||
if (Duration <= 0f) return false;
|
||||
|
||||
if (!grudges.TryGetValue(monster.GetZDOID(), out Grudge grudge)) return false;
|
||||
if (Time.time >= grudge.Until) return false;
|
||||
ZDO zdo = ZdoOf(monster);
|
||||
if (zdo == null) return false;
|
||||
|
||||
return grudge.Attacker == player.GetZDOID();
|
||||
}
|
||||
long until = zdo.GetLong(UntilKey, 0L);
|
||||
if (until == 0L || NowMs >= until) return false;
|
||||
|
||||
public static void Remember(Character monster, Character attacker)
|
||||
{
|
||||
if (monster == null || attacker == null) return;
|
||||
if (Duration <= 0f) return;
|
||||
|
||||
grudges[monster.GetZDOID()] = new Grudge
|
||||
{
|
||||
Attacker = attacker.GetZDOID(),
|
||||
Until = Time.time + Duration
|
||||
};
|
||||
|
||||
Sweep();
|
||||
return zdo.GetZDOID(AttackerKey) == player.GetZDOID();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops grudges nobody is owed any more. At most once per duration: a fight is a burst of
|
||||
/// damage events and the list should not be walked on each one.
|
||||
/// Writes the grudge on the creature's ZDO if this machine owns it, and otherwise asks the
|
||||
/// owner to. Returns true when it was written here.
|
||||
/// </summary>
|
||||
private static void Sweep()
|
||||
public static bool Remember(Character monster, Character attacker)
|
||||
{
|
||||
if (Time.time - lastSweep < Duration) return;
|
||||
lastSweep = Time.time;
|
||||
if (monster == null || attacker == null) return false;
|
||||
if (Duration <= 0f) return false;
|
||||
|
||||
List<ZDOID> expired = null;
|
||||
foreach (var entry in grudges)
|
||||
ZNetView nview = monster.GetComponent<ZNetView>();
|
||||
if (nview == null || !nview.IsValid()) return false;
|
||||
|
||||
if (!nview.IsOwner())
|
||||
{
|
||||
if (Time.time < entry.Value.Until) continue;
|
||||
if (expired == null) expired = new List<ZDOID>();
|
||||
expired.Add(entry.Key);
|
||||
nview.InvokeRPC(RpcName, attacker.GetZDOID());
|
||||
return false;
|
||||
}
|
||||
|
||||
Write(nview.GetZDO(), attacker.GetZDOID());
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void Write(ZDO zdo, ZDOID attacker)
|
||||
{
|
||||
zdo.Set(AttackerKey, attacker);
|
||||
zdo.Set(UntilKey, NowMs + (long)(Duration * 1000f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every creature can be told about a grudge by a machine that does not own it. ZNetView's
|
||||
/// InvokeRPC routes to the owner, so the handler always runs where the ZDO may be written,
|
||||
/// and the owner checks it is still the owner: ownership can move while the RPC travels.
|
||||
/// </summary>
|
||||
[HarmonyPatch(typeof(Character), "Awake")]
|
||||
public static class Character_Awake_GrudgeRpc_Patch
|
||||
{
|
||||
public static void Postfix(Character __instance)
|
||||
{
|
||||
if (__instance.IsPlayer()) return;
|
||||
ZNetView nview = __instance.GetComponent<ZNetView>();
|
||||
if (nview == null || !nview.IsValid()) return;
|
||||
|
||||
nview.Register<ZDOID>(RpcName, (sender, attacker) =>
|
||||
{
|
||||
if (__instance == null || !nview.IsValid() || !nview.IsOwner()) return;
|
||||
if (attacker.IsNone() || Duration <= 0f) return;
|
||||
Write(nview.GetZDO(), attacker);
|
||||
__instance.GetBaseAI()?.Alert();
|
||||
});
|
||||
}
|
||||
if (expired == null) return;
|
||||
foreach (ZDOID id in expired) grudges.Remove(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -179,10 +210,8 @@ namespace RacesOfValheim
|
|||
/// Vanilla's Dvergr camp, for every faction ("Grudge spreads to kin", off by default): hit
|
||||
/// a creature that is at peace with you and its kin around it take up the same grudge.
|
||||
///
|
||||
/// Only the creatures this machine runs the AI for are told, because a grudge is read where
|
||||
/// the AI runs (see WHERE IT RUNS above). The creatures around the one that was hit are
|
||||
/// nearly always simulated by the same client; one owned by another player's machine stays
|
||||
/// at peace until it is hit itself.
|
||||
/// Kin this machine owns are written here; kin owned by another player's machine are told
|
||||
/// through RpcName, and their owner writes the grudge and alerts them.
|
||||
/// </summary>
|
||||
private static void SpreadToKin(Character monster, Player player)
|
||||
{
|
||||
|
|
@ -194,12 +223,10 @@ namespace RacesOfValheim
|
|||
foreach (Character other in Character.GetAllCharacters())
|
||||
{
|
||||
if (other == null || other == monster || other.IsPlayer() || other.IsDead() || other.IsTamed()) continue;
|
||||
if (!other.IsOwner()) continue;
|
||||
if (Vector3.Distance(other.transform.position, center) > radius) continue;
|
||||
if (!FactionHostilityPatch.IsAlliedMob(race, other.gameObject.name)) continue;
|
||||
|
||||
Remember(other, player);
|
||||
other.GetBaseAI()?.Alert();
|
||||
if (Remember(other, player)) other.GetBaseAI()?.Alert();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue