Merge branch 'fix-faction-grudge'

This commit is contained in:
kanym 2026-11-06 21:21:20 +01:00
commit cae18b440d
3 changed files with 57 additions and 0 deletions

View file

@ -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;

View file

@ -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;
/// <summary>
/// True while vanilla is deciding whether <paramref name="attacker"/>'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.
/// </summary>
public static bool PlayerStrikes(Character attacker)
{
return strikeDepth > 0 && attacker != null && attacker.IsPlayer();
}
/// <summary>
/// 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.
/// </summary>
[HarmonyPatch]
public static class StrikeScope
{
public static IEnumerable<System.Reflection.MethodBase> 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--;
}
/// <summary>
/// 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.

View file

@ -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);