Merge branch 'fix-grudge-multiplayer'
This commit is contained in:
commit
6ddca72144
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 " +
|
"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 " +
|
"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 " +
|
"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 " +
|
"was hurt and had no enemy it was allowed to have. Each creature holds one " +
|
||||||
"and per creature, on whichever machine runs that creature's AI. 0 restores the " +
|
"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.",
|
"old absolute truce.",
|
||||||
new AcceptableValueRange<float>(0f, 600f)));
|
new AcceptableValueRange<float>(0f, 600f)));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,13 @@ namespace RacesOfValheim
|
||||||
/// which means everything downstream - acquiring, chasing, retaliating, giving up - is vanilla
|
/// which means everything downstream - acquiring, chasing, retaliating, giving up - is vanilla
|
||||||
/// behaviour rather than anything reimplemented here.
|
/// 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
|
/// WHERE IT LIVES. On the creature's own ZDO: who it is angry at and until when, in network time.
|
||||||
/// matters and the only one that runs Character.ApplyDamage for it. BaseAI.OnDamaged is reached
|
/// The AI that reads it runs on whichever machine owns the creature, and that can change in the
|
||||||
/// through the character's damage callback on exactly that machine, so the record is made where
|
/// middle of a fight (the owner walks off or logs out). A grudge kept in one machine's memory
|
||||||
/// it will be read. Nothing is replicated and nothing needs to be: another client's copy of the
|
/// went with it, and the new owner's copy of the creature made peace on its next UpdateTarget.
|
||||||
/// same mob is not the one deciding who it chases.
|
/// 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:
|
/// 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
|
/// it does nothing unless the prefab sets m_aggravatable, which only the dvergr and the seekers
|
||||||
|
|
@ -37,67 +39,96 @@ namespace RacesOfValheim
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class RaceGrudge
|
public static class RaceGrudge
|
||||||
{
|
{
|
||||||
private struct Grudge
|
private const string RpcName = "RacesOfValheim_Grudge";
|
||||||
{
|
|
||||||
public ZDOID Attacker;
|
|
||||||
public float Until;
|
|
||||||
}
|
|
||||||
|
|
||||||
// One grudge per mob, against the last thing that hit it. A creature can only chase one
|
// One grudge per creature, against the last player that hit it. A creature can only chase
|
||||||
// target anyway, and if two players of the same race are both poking it, the one it wants
|
// one target anyway, and if two players of the same race are both poking it, the one it
|
||||||
// is whoever hit it last.
|
// wants is whoever hit it last.
|
||||||
private static readonly Dictionary<ZDOID, Grudge> grudges = new Dictionary<ZDOID, Grudge>();
|
private static readonly KeyValuePair<int, int> AttackerKey = ZDO.GetHashZDOID("RacesOfValheim_GrudgeAttacker");
|
||||||
private static float lastSweep = 0f;
|
private static readonly int UntilKey = "RacesOfValheim_GrudgeUntil".GetStableHashCode();
|
||||||
|
|
||||||
private static float Duration => FactionsConfig.GrudgeSeconds;
|
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>
|
/// <summary>
|
||||||
/// True while <paramref name="monster"/> is owed a hit by <paramref name="player"/>. Both
|
/// 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>
|
/// </summary>
|
||||||
public static bool Holds(Character monster, Character player)
|
public static bool Holds(Character monster, Character player)
|
||||||
{
|
{
|
||||||
if (monster == null || player == null) return false;
|
if (monster == null || player == null) return false;
|
||||||
if (Duration <= 0f) return false;
|
if (Duration <= 0f) return false;
|
||||||
|
|
||||||
if (!grudges.TryGetValue(monster.GetZDOID(), out Grudge grudge)) return false;
|
ZDO zdo = ZdoOf(monster);
|
||||||
if (Time.time >= grudge.Until) return false;
|
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)
|
return zdo.GetZDOID(AttackerKey) == player.GetZDOID();
|
||||||
{
|
|
||||||
if (monster == null || attacker == null) return;
|
|
||||||
if (Duration <= 0f) return;
|
|
||||||
|
|
||||||
grudges[monster.GetZDOID()] = new Grudge
|
|
||||||
{
|
|
||||||
Attacker = attacker.GetZDOID(),
|
|
||||||
Until = Time.time + Duration
|
|
||||||
};
|
|
||||||
|
|
||||||
Sweep();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Drops grudges nobody is owed any more. At most once per duration: a fight is a burst of
|
/// Writes the grudge on the creature's ZDO if this machine owns it, and otherwise asks the
|
||||||
/// damage events and the list should not be walked on each one.
|
/// owner to. Returns true when it was written here.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static void Sweep()
|
public static bool Remember(Character monster, Character attacker)
|
||||||
{
|
{
|
||||||
if (Time.time - lastSweep < Duration) return;
|
if (monster == null || attacker == null) return false;
|
||||||
lastSweep = Time.time;
|
if (Duration <= 0f) return false;
|
||||||
|
|
||||||
List<ZDOID> expired = null;
|
ZNetView nview = monster.GetComponent<ZNetView>();
|
||||||
foreach (var entry in grudges)
|
if (nview == null || !nview.IsValid()) return false;
|
||||||
|
|
||||||
|
if (!nview.IsOwner())
|
||||||
{
|
{
|
||||||
if (Time.time < entry.Value.Until) continue;
|
nview.InvokeRPC(RpcName, attacker.GetZDOID());
|
||||||
if (expired == null) expired = new List<ZDOID>();
|
return false;
|
||||||
expired.Add(entry.Key);
|
}
|
||||||
|
|
||||||
|
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>
|
/// <summary>
|
||||||
|
|
@ -179,10 +210,8 @@ namespace RacesOfValheim
|
||||||
/// Vanilla's Dvergr camp, for every faction ("Grudge spreads to kin", off by default): hit
|
/// 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.
|
/// 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
|
/// Kin this machine owns are written here; kin owned by another player's machine are told
|
||||||
/// the AI runs (see WHERE IT RUNS above). The creatures around the one that was hit are
|
/// through RpcName, and their owner writes the grudge and alerts them.
|
||||||
/// nearly always simulated by the same client; one owned by another player's machine stays
|
|
||||||
/// at peace until it is hit itself.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static void SpreadToKin(Character monster, Player player)
|
private static void SpreadToKin(Character monster, Player player)
|
||||||
{
|
{
|
||||||
|
|
@ -194,12 +223,10 @@ namespace RacesOfValheim
|
||||||
foreach (Character other in Character.GetAllCharacters())
|
foreach (Character other in Character.GetAllCharacters())
|
||||||
{
|
{
|
||||||
if (other == null || other == monster || other.IsPlayer() || other.IsDead() || other.IsTamed()) continue;
|
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 (Vector3.Distance(other.transform.position, center) > radius) continue;
|
||||||
if (!FactionHostilityPatch.IsAlliedMob(race, other.gameObject.name)) continue;
|
if (!FactionHostilityPatch.IsAlliedMob(race, other.gameObject.name)) continue;
|
||||||
|
|
||||||
Remember(other, player);
|
if (Remember(other, player)) other.GetBaseAI()?.Alert();
|
||||||
other.GetBaseAI()?.Alert();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue