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.
143 lines
6.6 KiB
C#
143 lines
6.6 KiB
C#
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
// "Mobs of a faction don't attack players of the same faction" - the core Factions
|
|
// rule from the brainstorm. RaceHostilityManager.cs already grants some of this
|
|
// (Fenrir/Aswid/Draugh/Trenta), but several races have no coverage at all (Trol,
|
|
// Furling, Skadi, Dvergar, Skeleton), and none of it lines up race-for-race with the
|
|
// three Factions groupings. Both patches postfix the same vanilla method, so this one
|
|
// simply layers the full, faction-consistent ruleset on top without touching the
|
|
// older file - a player already exempted there stays exempted, this only adds pairs.
|
|
//
|
|
// Without this, a matching-faction mob keeps fighting the player, which is why the
|
|
// Trader's "E to use" prompt never has a chance to come up: BaseAI.UpdateAI() is
|
|
// still busy chasing/attacking every frame.
|
|
[HarmonyPatch(typeof(BaseAI), nameof(BaseAI.IsEnemy), new System.Type[] { typeof(Character), typeof(Character) })]
|
|
public static class FactionHostilityPatch
|
|
{
|
|
// "Fuling" was in this set and is not a prefab: the Fuling family is named Goblin* in code,
|
|
// so the basic Fuling and the Archer were never exempted for any of the three races that
|
|
// read this set (Trenta, Trol, Furling) while the Berserker and Shaman were. In a camp that
|
|
// is the same as no truce at all - the unexempted Fulings open fire, the player defends
|
|
// themselves, and every Berserker in earshot joins in through damage retaliation, which
|
|
// IsEnemy has no say over. Verified against the asset manifest, not the wiki's display names.
|
|
private static readonly HashSet<string> NeraForestaMobs = new HashSet<string>
|
|
{
|
|
"Greydwarf", "Greydwarf_Elite", "Greydwarf_Shaman",
|
|
"Goblin", "GoblinArcher", "GoblinBrute", "GoblinShaman",
|
|
"Troll",
|
|
};
|
|
|
|
private static readonly HashSet<string> MontagnaMobs = new HashSet<string>
|
|
{
|
|
"Hatchling", // Drake
|
|
"Wolf", "Ulv", "Fenring", "Fenring_Cultist",
|
|
"Dverger", "DvergerMageFire", "DvergerMageIce", "DvergerMageSupport", "DvergerAshlands",
|
|
};
|
|
|
|
private static readonly HashSet<string> PaludeMobs = new HashSet<string>
|
|
{
|
|
"Ghost", "Skeleton", "Skeleton_Poison", // Skeleton, Rancid remains
|
|
"Draugr", "Draugr_Elite",
|
|
"Wraith", "Leech", "Abomination",
|
|
"Blob", "BlobElite", "BlobTar", // Blob, Oozer, Growth
|
|
};
|
|
|
|
// Every race id ever assigned to m_customData["Race"] for the nine playable races,
|
|
// grouped by which faction's mobs they get along with (see RacesOfValheim.cs for
|
|
// where each string is set, and DvergarRace.cs's LegacyRaceId for the old save value).
|
|
private static readonly Dictionary<string, HashSet<string>> RaceFactionMobs = new Dictionary<string, HashSet<string>>
|
|
{
|
|
{ TrentaConfig.RaceId, NeraForestaMobs },
|
|
{ "Trol", NeraForestaMobs },
|
|
{ "Furling", NeraForestaMobs },
|
|
|
|
{ "Skadi", MontagnaMobs },
|
|
{ "Fenrir", MontagnaMobs },
|
|
{ DvergarConfig.RaceId, MontagnaMobs },
|
|
{ DvergarRace.LegacyRaceId, MontagnaMobs },
|
|
|
|
{ "Aswid", PaludeMobs },
|
|
{ "Draugh", PaludeMobs },
|
|
{ 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;
|
|
|
|
string prefabName = monster.gameObject.name.Replace("(Clone)", "").Trim();
|
|
|
|
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)
|
|
{
|
|
if (!__result) return; // already not enemies, nothing to add
|
|
if (a == null || b == null) return;
|
|
|
|
Character playerCharacter = a.IsPlayer() ? a : (b.IsPlayer() ? b : null);
|
|
Character monster = a.IsPlayer() ? b : (b.IsPlayer() ? a : null);
|
|
if (!(playerCharacter is Player p) || monster == null) return;
|
|
|
|
if (IsSameFaction(p, monster))
|
|
{
|
|
__result = false;
|
|
}
|
|
}
|
|
}
|
|
}
|