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.
This commit is contained in:
kanym 2026-08-09 09:11:14 +02:00
commit a3b1666036
3 changed files with 78 additions and 3 deletions

View file

@ -0,0 +1,25 @@
using BepInEx.Configuration;
namespace RacesOfValheim
{
/// <summary>
/// Tunables for the Factions truce system, bound as BepInEx ConfigEntry - see
/// CONFIGURATION-MANAGER-HOWTO.md. Bound once, from RacesOfValheim.Awake().
/// </summary>
public static class FactionConfig
{
public static ConfigEntry<bool> 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."));
}
}
}

View file

@ -64,16 +64,65 @@ namespace RacesOfValheim
{ SkeletonVisuals.RaceId, PaludeMobs },
};
/// <summary>
/// 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.
/// </summary>
private static string GetPlayerRace(Player player)
{
if (player == null) return "Human";
ZNetView nview = player.GetComponent<ZNetView>();
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<string> 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<string> mobs)) return false;
string prefabName = monster.gameObject.name.Replace("(Clone)", "").Trim();
return mobs.Contains(prefabName);
if (!FactionConfig.ShareHostilityWithParty.Value)
{
return TryGetOwnFactionMobs(player, out HashSet<string> mobs) && mobs.Contains(prefabName);
}
foreach (Player other in Player.GetAllPlayers())
{
if (TryGetOwnFactionMobs(other, out HashSet<string> mobs) && mobs.Contains(prefabName))
{
return true;
}
}
return false;
}
public static void Postfix(Character a, Character b, ref bool __result)

View file

@ -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);