diff --git a/JotunnModStub/Factions/FactionHostilityPatch.cs b/JotunnModStub/Factions/FactionHostilityPatch.cs index f8f873f..b445dbe 100644 --- a/JotunnModStub/Factions/FactionHostilityPatch.cs +++ b/JotunnModStub/Factions/FactionHostilityPatch.cs @@ -212,6 +212,14 @@ namespace RacesOfValheim Character monster = Traverse.Create(__instance).Field("m_character").Value; if (monster == null) return; + // A piece an allied race built is never a target, with or without anyone around. + // See FactionPieceTruce. + if (targetStatic.GetValue() is Component target && FactionPieceTruce.Protects(target.gameObject, monster)) + { + targetStatic.SetValue(null); + return; + } + foreach (Player player in Player.GetAllPlayers()) { if (Vector3.Distance(player.transform.position, monster.transform.position) > FactionCheckRange) continue; diff --git a/JotunnModStub/Factions/FactionPieceTruce.cs b/JotunnModStub/Factions/FactionPieceTruce.cs new file mode 100644 index 0000000..76321aa --- /dev/null +++ b/JotunnModStub/Factions/FactionPieceTruce.cs @@ -0,0 +1,120 @@ +using HarmonyLib; +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// 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. + /// + public static class FactionPieceTruce + { + public const string BuilderRaceKey = "RacesOfValheim_BuilderRace"; + + /// True if this piece was built by a race whose ally is, + /// and the monster holds no grudge against the builder. + 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(); + 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); + } + + /// A new piece: stamped by its builder the moment it is placed. + [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(); + if (nview == null || !nview.IsValid() || !nview.IsOwner()) return; + Stamp(nview); + } + } + + /// + /// 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. + /// + [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(); + 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); + } + } + + /// + /// 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. + /// + [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); + } + } + } +}