From b566adbfb9a84f31025b3009fc8899d18c91c1ee Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 15:50:41 +0200 Subject: [PATCH 1/2] 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. --- .../Factions/FactionHostilityPatch.cs | 8 ++ JotunnModStub/Factions/FactionPieceTruce.cs | 120 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 JotunnModStub/Factions/FactionPieceTruce.cs 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); + } + } + } +} From 87d0f2642c98894fdae50d9083eb97a8d74fbc92 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 15:51:53 +0200 Subject: [PATCH 2/2] fix(texts): four messages showed as [msg_...] Four tokens the mod printed exist in no vanilla table (checked on Valheim 1.0.20) and were never registered by the mod, so players saw the raw name in brackets. - $msg_eatanother (Trol, a food still being digested): vanilla's $msg_nomore, "You can't eat more $1", called as Player.CanEat calls it. - $msg_canteatanymore (Trol, every stomach full): vanilla's $msg_isfull. - $msg_ate (Aswid, Vae Victis): vanilla's $msg_consumed. - $msg_notenoughmoney (Dvergar bank, Trol totem): a new mod text, rov_msg_not_enough_coins, in English, Italian and Russian. --- JotunnModStub/Aswid/AswidRace.cs | 3 ++- JotunnModStub/Dvergar/DvergarBancoBehaviour.cs | 2 +- JotunnModStub/Translations/English/messages.json | 1 + JotunnModStub/Translations/Italian/messages.json | 1 + JotunnModStub/Translations/Russian/messages.json | 1 + JotunnModStub/Trol/TrolFoodPatches.cs | 10 ++++++---- JotunnModStub/Trol/TrolTotemBehaviour.cs | 2 +- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/JotunnModStub/Aswid/AswidRace.cs b/JotunnModStub/Aswid/AswidRace.cs index b95daff..fb949a3 100644 --- a/JotunnModStub/Aswid/AswidRace.cs +++ b/JotunnModStub/Aswid/AswidRace.cs @@ -1258,7 +1258,8 @@ namespace RacesOfValheim { // Show the Vae Victis message in the center of the screen Player.m_localPlayer.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_vae_victis")); - Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, "$msg_ate " + addedItem.m_shared.m_name); + // "$msg_ate" exists in no vanilla table and players saw [msg_ate]; vanilla's "Consumed" reads the same. + Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, "$msg_consumed " + addedItem.m_shared.m_name); // Vae Victis XP logic - similar to Dead Raiser Player.m_localPlayer.RaiseSkill(Skills.SkillType.BloodMagic, 1f); diff --git a/JotunnModStub/Dvergar/DvergarBancoBehaviour.cs b/JotunnModStub/Dvergar/DvergarBancoBehaviour.cs index 444b065..fcdd94d 100644 --- a/JotunnModStub/Dvergar/DvergarBancoBehaviour.cs +++ b/JotunnModStub/Dvergar/DvergarBancoBehaviour.cs @@ -157,7 +157,7 @@ namespace RacesOfValheim if (CountCoins(inventory) < price) { - player.Message(MessageHud.MessageType.Center, "$msg_notenoughmoney"); + player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_not_enough_coins")); return; } diff --git a/JotunnModStub/Translations/English/messages.json b/JotunnModStub/Translations/English/messages.json index 6e6ee1e..8f7c7fa 100644 --- a/JotunnModStub/Translations/English/messages.json +++ b/JotunnModStub/Translations/English/messages.json @@ -106,6 +106,7 @@ "rov_msg_fenrir_trough_takes_line_runs_deeper": "The trough takes it. The line runs deeper now.", "rov_msg_fenrir_wants_meat_any_meat": "It wants meat. Any meat.", "rov_msg_fenrir_wants_what_yagluth_left_behind": "It wants what Yagluth left behind.", + "rov_msg_not_enough_coins": "Not enough coins", "rov_msg_skadi_are_drake": "You are a Drake!", "rov_msg_skadi_are_fader": "You are Fader!", "rov_msg_skadi_are_moder": "You are Moder!", diff --git a/JotunnModStub/Translations/Italian/messages.json b/JotunnModStub/Translations/Italian/messages.json index 6d3cb6f..f0e20bb 100644 --- a/JotunnModStub/Translations/Italian/messages.json +++ b/JotunnModStub/Translations/Italian/messages.json @@ -106,6 +106,7 @@ "rov_msg_fenrir_trough_takes_line_runs_deeper": "La mangiatoia lo accetta. Ora la discendenza va più a fondo.", "rov_msg_fenrir_wants_meat_any_meat": "Vuole carne. Qualsiasi carne.", "rov_msg_fenrir_wants_what_yagluth_left_behind": "Vuole ciò che Yagluth ha lasciato.", + "rov_msg_not_enough_coins": "Monete insufficienti", "rov_msg_skadi_are_drake": "Sei un Drake!", "rov_msg_skadi_are_fader": "Sei Fader!", "rov_msg_skadi_are_moder": "Sei Moder!", diff --git a/JotunnModStub/Translations/Russian/messages.json b/JotunnModStub/Translations/Russian/messages.json index 2e70a33..3ef6918 100644 --- a/JotunnModStub/Translations/Russian/messages.json +++ b/JotunnModStub/Translations/Russian/messages.json @@ -106,6 +106,7 @@ "rov_msg_fenrir_trough_takes_line_runs_deeper": "Кормушка принимает жертву. Родословная стаи крепнет.", "rov_msg_fenrir_wants_meat_any_meat": "Требуется мясо. Любое мясо.", "rov_msg_fenrir_wants_what_yagluth_left_behind": "Требуется то, что осталось после падения Яглута.", + "rov_msg_not_enough_coins": "Недостаточно монет", "rov_msg_skadi_are_drake": "Вы — Дрейк!", "rov_msg_skadi_are_fader": "Вы — Прародитель!", "rov_msg_skadi_are_moder": "Вы — Матерь!", diff --git a/JotunnModStub/Trol/TrolFoodPatches.cs b/JotunnModStub/Trol/TrolFoodPatches.cs index b0d452d..97232bb 100644 --- a/JotunnModStub/Trol/TrolFoodPatches.cs +++ b/JotunnModStub/Trol/TrolFoodPatches.cs @@ -90,7 +90,7 @@ namespace RacesOfValheim if (f.m_item.m_shared.m_name != item.m_shared.m_name) continue; if (f.CanEatAgain()) return true; - __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); + __instance.Message(MessageHud.MessageType.Center, Localization.instance.Localize("$msg_nomore", item.m_shared.m_name)); __result = false; return false; } @@ -110,12 +110,12 @@ namespace RacesOfValheim if (has4 && food4.m_item.m_shared.m_name == item.m_shared.m_name) { if (canEat4) { TrolFoodManager.SetExtraFood(__instance, item); ConsumeItemEffect(__instance, item); __result = true; return false; } - else { __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); __result = false; return false; } + else { __instance.Message(MessageHud.MessageType.Center, Localization.instance.Localize("$msg_nomore", item.m_shared.m_name)); __result = false; return false; } } if (has5 && food5.m_item.m_shared.m_name == item.m_shared.m_name) { if (state5 && canEat5) { TrolFoodManager.SetExtraFood5(__instance, item); ConsumeItemEffect(__instance, item); __result = true; return false; } - else { __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); __result = false; return false; } + else { __instance.Message(MessageHud.MessageType.Center, Localization.instance.Localize("$msg_nomore", item.m_shared.m_name)); __result = false; return false; } } // 3. A free vanilla slot takes it the vanilla way. @@ -137,7 +137,9 @@ namespace RacesOfValheim return false; } - __instance.Message(MessageHud.MessageType.Center, "$msg_canteatanymore"); + // Vanilla's own "You are full already", the line Player.CanEat shows for a full stomach. + // "$msg_canteatanymore" was never a vanilla token, so players saw [msg_canteatanymore]. + __instance.Message(MessageHud.MessageType.Center, "$msg_isfull"); __result = false; return false; } diff --git a/JotunnModStub/Trol/TrolTotemBehaviour.cs b/JotunnModStub/Trol/TrolTotemBehaviour.cs index cf040b6..d43f028 100644 --- a/JotunnModStub/Trol/TrolTotemBehaviour.cs +++ b/JotunnModStub/Trol/TrolTotemBehaviour.cs @@ -157,7 +157,7 @@ namespace RacesOfValheim if (CountCoins(inventory) < troop.Price) { - player.Message(MessageHud.MessageType.Center, "$msg_notenoughmoney"); + player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_not_enough_coins")); return; }