diff --git a/JotunnModStub/Factions/FactionHostilityPatch.cs b/JotunnModStub/Factions/FactionHostilityPatch.cs index 04f9e46..2bc10ce 100644 --- a/JotunnModStub/Factions/FactionHostilityPatch.cs +++ b/JotunnModStub/Factions/FactionHostilityPatch.cs @@ -165,6 +165,10 @@ namespace RacesOfValheim if (!__result) return; // already not enemies, nothing to add if (a == null || b == null) return; + // The player's own blow. Peace is the creature's choice, not a shield. See + // RaceGrudge.StrikeScope. + if (RaceGrudge.PlayerStrikes(a)) return; + if (IsThrallTruce(a, b) || IsThrallTruce(b, a)) { __result = false; diff --git a/JotunnModStub/RaceGrudge.cs b/JotunnModStub/RaceGrudge.cs index eb45794..944a260 100644 --- a/JotunnModStub/RaceGrudge.cs +++ b/JotunnModStub/RaceGrudge.cs @@ -125,6 +125,56 @@ namespace RacesOfValheim } } + // How deep we are inside one of vanilla's hit filters (see StrikeScope). A counter rather + // than a bool because Projectile.OnHit calls DoAOE, and the inner exit must not end the + // outer scope. + private static int strikeDepth = 0; + + /// + /// True while vanilla is deciding whether 's own weapon, arrow or + /// area hit lands. Both truce postfixes stand down for that question: the truce is about + /// who a creature attacks, never about whether the player is allowed to strike it. + /// + public static bool PlayerStrikes(Character attacker) + { + return strikeDepth > 0 && attacker != null && attacker.IsPlayer(); + } + + /// + /// THE PLAYER COULD NOT HIT THEIR OWN FACTION AT ALL. Valheim 1.0's hit filters (Attack's + /// melee and area swings, Projectile's direct hit, its AoE and its IsValidTarget, Aoe's + /// ShouldHit) ask BaseAI.IsEnemy(attacker, target) and skip the target when it answers false + /// and the attacker is a player without PvP on - vanilla's guard for tames. Our truce + /// answers false for every allied creature, so the blow passed through it, it took no + /// damage, BaseAI.OnDamaged never ran and no grudge was ever born. That is both reports at + /// once: "it will not allow me" to attack, and kin that never fight back, group grudge on or + /// off, because nobody was ever hit. With PvP on the filter let the hit through, which is + /// why it looked fine in some tests. + /// + /// The scope is open only around those six methods, so everything else that asks IsEnemy + /// with the player first keeps the truce: the green health bar on allied creatures, sneaking + /// past them, the Aswid's aura leaving kin alone. + /// + [HarmonyPatch] + public static class StrikeScope + { + public static IEnumerable TargetMethods() + { + yield return AccessTools.Method(typeof(Attack), "DoMeleeAttack"); + yield return AccessTools.Method(typeof(Attack), "DoAreaAttack"); + yield return AccessTools.Method(typeof(Projectile), "DoAOE"); + yield return AccessTools.Method(typeof(Projectile), "IsValidTarget"); + yield return AccessTools.Method(typeof(Projectile), "OnHit"); + yield return AccessTools.Method(typeof(Aoe), "ShouldHit"); + } + + public static void Prefix() => strikeDepth++; + + // Finalizer rather than Postfix: the count has to come down even when the method + // throws, or every IsEnemy afterwards would ignore the truce. + public static void Finalizer() => strikeDepth--; + } + /// /// 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. diff --git a/JotunnModStub/RaceHostilityManager.cs b/JotunnModStub/RaceHostilityManager.cs index 03f9fa4..13ed8ed 100644 --- a/JotunnModStub/RaceHostilityManager.cs +++ b/JotunnModStub/RaceHostilityManager.cs @@ -22,6 +22,9 @@ namespace RacesOfValheim if (a == null || b == null) return; + // The player's own blow lands on kin too. See RaceGrudge.StrikeScope. + if (RaceGrudge.PlayerStrikes(a)) return; + Character playerCharacter = a.IsPlayer() ? a : (b.IsPlayer() ? b : null); Character monster = a.IsPlayer() ? b : (b.IsPlayer() ? a : null);