RacesOfValheim/JotunnModStub/Factions/FactionPieceTruce.cs
kanym b566adbfb9 fix(factions): allied mobs spare a race's buildings with nobody home
FactionStaticTargetPatch only vetoed a mob's piece target while an
allied player stood within 100 m. Inside a troll cave that was the
collapse bug: the Trol walks out, the resident Troll (its prefab has
m_attackPlayerObjects) finds nobody to stop it, and the cooking station
built there is in pieces when the Trol comes back.

Every piece now carries the race of its builder on its ZDO, stamped in
Piece.SetCreator at placement, and in Piece.Awake for older pieces the
first time their builder loads them, so no timer and no scan. An
allied mob of that race can't pick the piece as a target and its blows
do it no damage, which also covers a swing at a foreign wall that
would have hit the Trol's workbench next to it. A mob holding a grudge
against the builder gets no such truce.
2026-10-17 15:50:41 +02:00

120 lines
5.4 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Allied mobs leave a race's buildings alone whether or not anyone is home.
///
/// FactionStaticTargetPatch only spares a base while an allied player stands within 100 m of
/// the mob. That left the troll cave bug: a Trol builds a cooking station inside a troll cave,
/// walks out, and the cave's resident Troll - m_attackPlayerObjects is true on its prefab - finds
/// nobody to stop it and smashes the station. The player came back to the materials on the floor.
///
/// So every piece remembers the race of whoever built it (BuilderRaceKey on its ZDO), and an
/// allied mob of that race can neither pick it as a target nor damage it. The damage veto is
/// for the blows meant for something else: a Troll swinging at a Dvergar's wall must not take
/// the Trol's workbench next to it down too. The target veto is what keeps the Troll from
/// standing there forever: it never picks an allied piece, so once the foreign one is gone it
/// has nothing to swing at.
///
/// A piece built before this existed carries no race. It gets one the next time its builder
/// loads it, from Piece.Awake, which runs anyway for every piece in a zone as it loads - so
/// there is no timer and no scan. Until then it is protected only the old way.
/// </summary>
public static class FactionPieceTruce
{
public const string BuilderRaceKey = "RacesOfValheim_BuilderRace";
/// <summary>True if this piece was built by a race whose ally <paramref name="monster"/> is,
/// and the monster holds no grudge against the builder.</summary>
public static bool Protects(GameObject pieceObject, Character monster)
{
if (!FactionsConfig.Hostility || pieceObject == null || monster == null) return false;
if (monster.IsPlayer() || monster.IsTamed()) return false;
ZNetView nview = pieceObject.GetComponentInParent<ZNetView>();
if (nview == null || !nview.IsValid()) return false;
string race = nview.GetZDO().GetString(BuilderRaceKey, "");
if (string.IsNullOrEmpty(race)) return false;
if (!FactionHostilityPatch.IsAlliedMob(race, monster.gameObject.name)) return false;
// A mob fighting the builder right now is no ally of the builder's house either.
long creator = nview.GetZDO().GetLong(ZDOVars.s_creator, 0L);
if (creator != 0L)
{
foreach (Player player in Player.GetAllPlayers())
{
if (player.GetPlayerID() == creator && RaceGrudge.Holds(monster, player)) return false;
}
}
return true;
}
private static void Stamp(ZNetView nview)
{
Player local = Player.m_localPlayer;
if (local == null) return;
string race = FactionHostilityPatch.GetPlayerRace(local);
if (string.IsNullOrEmpty(race)) return;
nview.GetZDO().Set(BuilderRaceKey, race);
}
/// <summary>A new piece: stamped by its builder the moment it is placed.</summary>
[HarmonyPatch(typeof(Piece), nameof(Piece.SetCreator))]
public static class Faction_PieceSetCreator_Patch
{
public static void Postfix(Piece __instance, long uid)
{
if (__instance == null || Player.m_localPlayer == null) return;
if (uid != Player.m_localPlayer.GetPlayerID()) return;
ZNetView nview = __instance.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
Stamp(nview);
}
}
/// <summary>
/// An old piece: stamped by its builder the first time it loads for them. Only the ZDO's
/// owner writes, as everywhere else in the mod; a piece some other player owns right now
/// gets its race on a later load.
/// </summary>
[HarmonyPatch(typeof(Piece), "Awake")]
public static class Faction_PieceAwake_Patch
{
public static void Postfix(Piece __instance)
{
Player local = Player.m_localPlayer;
if (__instance == null || local == null) return;
ZNetView nview = __instance.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
ZDO zdo = nview.GetZDO();
if (zdo.GetLong(ZDOVars.s_creator, 0L) != local.GetPlayerID()) return;
if (!string.IsNullOrEmpty(zdo.GetString(BuilderRaceKey, ""))) return;
Stamp(nview);
}
}
/// <summary>
/// Blows from an allied mob land on nothing. Patched on Damage, the attacker's side, where
/// the mob is a live object; RPC_Damage runs on the piece's owner, who may not have it.
/// </summary>
[HarmonyPatch(typeof(WearNTear), nameof(WearNTear.Damage))]
public static class Faction_PieceDamage_Patch
{
public static bool Prefix(WearNTear __instance, HitData hit)
{
if (hit == null) return true;
Character attacker = hit.GetAttacker();
return !Protects(__instance.gameObject, attacker);
}
}
}
}