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.
This commit is contained in:
parent
4c3da61c3c
commit
fac6e3986c
1 changed files with 41 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using HarmonyLib;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
|
|
@ -91,4 +92,44 @@ namespace RacesOfValheim
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
[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<Character>("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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue