From a3b16660368e1fbde64fbd2429c6f261b5b9cb3e Mon Sep 17 00:00:00 2001 From: kanym Date: Sun, 9 Aug 2026 09:11:14 +0200 Subject: [PATCH] fix(factions): read player race from ZDO, add party-wide hostility sharing IsSameFaction read player.m_customData directly, which is local save data never networked - correct only for whichever client is asking, wrong for a remote player as seen by whatever peer owns the mob's ZDO (the common case in multiplayer). Switched to the ZDO-synced race value with the same fallback DraughVisuals/TrentaVisuals already use. Also adds FactionConfig.ShareHostilityWithParty (off by default): when on, a player's truce covers every faction any connected player's race belongs to, not just their own - a Dvergar and an Aswid playing together are left alone by both the Mountain's mobs and the Swamp's. --- JotunnModStub/Factions/FactionConfig.cs | 25 +++++++++ .../Factions/FactionHostilityPatch.cs | 55 ++++++++++++++++++- JotunnModStub/RacesOfValheim.cs | 1 + 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 JotunnModStub/Factions/FactionConfig.cs diff --git a/JotunnModStub/Factions/FactionConfig.cs b/JotunnModStub/Factions/FactionConfig.cs new file mode 100644 index 0000000..919fc18 --- /dev/null +++ b/JotunnModStub/Factions/FactionConfig.cs @@ -0,0 +1,25 @@ +using BepInEx.Configuration; + +namespace RacesOfValheim +{ + /// + /// Tunables for the Factions truce system, bound as BepInEx ConfigEntry - see + /// CONFIGURATION-MANAGER-HOWTO.md. Bound once, from RacesOfValheim.Awake(). + /// + public static class FactionConfig + { + public static ConfigEntry ShareHostilityWithParty; + + public static void Bind(ConfigFile config) + { + const string section = "Factions - Hostility"; + + ShareHostilityWithParty = config.Bind(section, "Share Hostility With Party", false, + new ConfigDescription( + "When enabled, a player's truce covers every faction any connected player's race " + + "belongs to - a Dvergar and an Aswid playing together are both left alone by the " + + "Mountain's mobs AND the Swamp's, not just their own. When disabled (default), each " + + "player is only exempted for their own race's faction.")); + } + } +} diff --git a/JotunnModStub/Factions/FactionHostilityPatch.cs b/JotunnModStub/Factions/FactionHostilityPatch.cs index 48fc181..6121780 100644 --- a/JotunnModStub/Factions/FactionHostilityPatch.cs +++ b/JotunnModStub/Factions/FactionHostilityPatch.cs @@ -64,16 +64,65 @@ namespace RacesOfValheim { SkeletonVisuals.RaceId, PaludeMobs }, }; + /// + /// Reads the race off the ZDO so remote players are recognised correctly - the same fix + /// DraughVisuals/TrentaVisuals already needed for their own per-player lookups. + /// player.m_customData is local save data, never networked, so a check reading it directly + /// only ever sees the truth for whichever client is doing the asking; on any other peer + /// (which is who actually owns a given mob's ZDO in multiplayer, most of the time) it is + /// empty or stale, wrong for every remote player. Falls back to m_customData only for the + /// main-menu preview, which has no ZNetView at all. + /// + private static string GetPlayerRace(Player player) + { + if (player == null) return "Human"; + + ZNetView nview = player.GetComponent(); + if (nview != null && nview.IsValid()) + { + string race = nview.GetZDO().GetString("RacesOfValheim_Race", ""); + if (!string.IsNullOrEmpty(race)) return race; + } + + if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string custom)) + { + return custom; + } + + return "Human"; + } + + private static bool TryGetOwnFactionMobs(Player player, out HashSet mobs) + { + return RaceFactionMobs.TryGetValue(GetPlayerRace(player), out mobs); + } + // Shared with FactionMerchantHoverPatch: a player should only get the "E - Talk" // hover prompt for a mob whose faction actually matches their own race. + // + // With FactionConfig.ShareHostilityWithParty on, "their own race" widens to "any + // connected player's race" - a shared truce for the whole party rather than one drawn + // per player, so a Dvergar and an Aswid adventuring together are left alone by both the + // Mountain's mobs and the Swamp's. public static bool IsSameFaction(Player player, Character monster) { if (player == null || monster == null) return false; - if (!player.m_customData.TryGetValue("Race", out string race)) return false; - if (!RaceFactionMobs.TryGetValue(race, out HashSet mobs)) return false; string prefabName = monster.gameObject.name.Replace("(Clone)", "").Trim(); - return mobs.Contains(prefabName); + + if (!FactionConfig.ShareHostilityWithParty.Value) + { + return TryGetOwnFactionMobs(player, out HashSet mobs) && mobs.Contains(prefabName); + } + + foreach (Player other in Player.GetAllPlayers()) + { + if (TryGetOwnFactionMobs(other, out HashSet mobs) && mobs.Contains(prefabName)) + { + return true; + } + } + return false; } public static void Postfix(Character a, Character b, ref bool __result) diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index 4b69a34..f92b5c6 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -30,6 +30,7 @@ namespace RacesOfValheim AswidConfig.Bind(Config); DraughConfig.Bind(Config); DvergarConfigManagement.Bind(Config); + FactionConfig.Bind(Config); FenrirConfig.Bind(Config); FurlingConfig.Bind(Config); SkadiConfig.Bind(Config);