From fac6e3986c6550f02dd8f5afb52931ba703f85fe Mon Sep 17 00:00:00 2001 From: kanym Date: Sun, 9 Aug 2026 16:46:23 +0200 Subject: [PATCH] fix(factions): stop same-faction mobs from smashing player structures The truce only ever patched BaseAI.IsEnemy, but MonsterAI.UpdateTarget picks a structure to attack (m_targetStatic) through a completely separate gate - m_attackPlayerObjects, a flag baked into the prefab (true for Troll among others) - and never calls IsEnemy at all. A Trol player, exempt from a cave's resident Troll in combat, could still watch it flatten a workbench built moments earlier in its own cave. FactionStaticTargetPatch postfixes UpdateTarget and vetoes the pick if a same-faction player is within 20 m (matching FindRandomStaticTarget's own 10 m search radius with room to spare), rather than trying to intercept the two private target-selection methods themselves. Confirmed fixed by build+playtest. --- .../Factions/FactionHostilityPatch.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/JotunnModStub/Factions/FactionHostilityPatch.cs b/JotunnModStub/Factions/FactionHostilityPatch.cs index 48fc181..1ce5e9b 100644 --- a/JotunnModStub/Factions/FactionHostilityPatch.cs +++ b/JotunnModStub/Factions/FactionHostilityPatch.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using HarmonyLib; +using UnityEngine; namespace RacesOfValheim { @@ -91,4 +92,44 @@ namespace RacesOfValheim } } } + + /// + /// The truce above only ever touches BaseAI.IsEnemy - a mob's decision to smash a nearby + /// piece runs on a completely separate track. MonsterAI.UpdateTarget gates that on + /// m_attackPlayerObjects (a flag baked into the prefab, true for Troll among others) and + /// never once calls IsEnemy: a Troll with nothing to fight picks a m_targetStatic and + /// swings at it regardless of who is standing next to it. That is how a Trol player, + /// exempt from that very Troll's hostility, could still watch it flatten a workbench built + /// two seconds earlier in its own cave. + /// + /// A postfix on UpdateTarget rather than trying to intercept FindClosestStaticPriorityTarget + /// or FindRandomStaticTarget (both private, both called from more than one branch inside it): + /// simpler to let vanilla pick whatever it wants and veto the pick once, right after, than to + /// duplicate its target-selection logic to stop it picking in the first place. + /// + [HarmonyPatch(typeof(MonsterAI), "UpdateTarget")] + public static class FactionStaticTargetPatch + { + // Comfortably past FindRandomStaticTarget's own 10 m search radius, so a truce holds for + // any piece the mob could have picked in the first place. + private const float FactionCheckRange = 20f; + + public static void Postfix(MonsterAI __instance) + { + Traverse targetStatic = Traverse.Create(__instance).Field("m_targetStatic"); + if (targetStatic.GetValue() == null) return; + + Character monster = Traverse.Create(__instance).Field("m_character").Value; + if (monster == null) return; + + foreach (Player player in Player.GetAllPlayers()) + { + if (Vector3.Distance(player.transform.position, monster.transform.position) > FactionCheckRange) continue; + if (!FactionHostilityPatch.IsSameFaction(player, monster)) continue; + + targetStatic.SetValue(null); + return; + } + } + } }