RacesOfValheim/JotunnModStub/RaceHostilityManager.cs
Ferdinando a60f5e8217 fix(factions): make the truce switch cover every player truce, not one of five
Reported from a live game: with "Faction truce" off, a Trenta's Greydwarves
stayed friendly. The switch was working - it just was not switched over the
patch that mattered.

Five separate Harmony postfixes answer BaseAI.IsEnemy, and only
FactionHostilityPatch was gated. A Trenta reads its Black Forest truce from
RaceHostilityManager, which had no switch, so turning the faction one off
changed nothing visible. The same held for the Fenrir's wolves, the Aswid's
progression list, the Draugh's undead, the Sverm's invertebrates, the
Furling's Plains roster and the Skeleton's undead.

Gated all four player-facing ones behind the existing flag rather than
adding a switch each: from the player's side there is one question being
asked, and a switch that answers a fifth of it is worse than none.

DvergarMercenaryTrucePatch is deliberately left alone - it is a truce
between a hired thrall and a wild dvergr, two NPCs, and has nothing to do
with whether mobs come after the player.

Key stays "Faction truce" though its scope grew: renaming it would orphan
the entry in every .cfg already written, and the file beats the default in
code, so anyone who had switched it off would silently get the truce back.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-25 19:13:16 +02:00

143 lines
8.2 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
// --- RACE HOSTILITY MANAGER ---
// This script manages which mobs should not attack the player based on their race.
// For now, it manages wolves, ulvs, fenrings, and cultists for the Fenrir race.
[HarmonyPatch(typeof(BaseAI), nameof(BaseAI.IsEnemy), new System.Type[] { typeof(Character), typeof(Character) })]
public static class RaceHostilityManager
{
public static void Postfix(Character a, Character b, ref bool __result)
{
// The per-race truces below answer the same BaseAI.IsEnemy the Factions patches do,
// so the switch has to cover both or it covers nothing: a Trenta reads its Greydwarf
// truce from HERE, not from FactionHostilityPatch, and turning the faction one off
// left the mobs friendly anyway. Same flag rather than a second one, because from
// the player's side there is only one question being asked.
if (!FactionsConfig.Hostility) return;
if (!__result) return; // If already not enemies, skip
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 != null && monster != null)
{
Player p = playerCharacter as Player;
if (p != null)
{
// Reads the ZDO copy of the race first - see GetPlayerRace's comment in
// FactionHostilityPatch for why m_customData alone breaks this for anyone
// but the local client (e.g. a dedicated server owning the mob's AI).
string playerRace = FactionHostilityPatch.GetPlayerRace(p);
// Fenrir Race Logic
if (playerRace == "Fenrir")
{
string prefabName = monster.gameObject.name.ToLower();
if (prefabName.Contains("wolf") || prefabName.Contains("fenring") ||
prefabName.Contains("ulv") || prefabName.Contains("cultist"))
{
__result = false;
}
}
// Aswid Race Logic
if (playerRace == "Aswid")
{
string prefabName = monster.gameObject.name.ToLower();
int state = BossProgressionManager.GetCurrentProgressionState();
if (state >= 2 && prefabName.Contains("boar")) __result = false;
if (state >= 3 && prefabName.Contains("bear")) __result = false;
if (state >= 4 && (prefabName.Contains("leech") || prefabName.Contains("ghost") || prefabName.Contains("wraith"))) __result = false;
if (state >= 5 && prefabName.Contains("wolf")) __result = false;
if (state >= 6 && (prefabName.Contains("lox") || prefabName.Contains("deathsquito"))) __result = false;
if (state >= 7 && (prefabName.Contains("hare") || prefabName.Contains("gjall"))) __result = false;
if (state >= 8 && (prefabName.Contains("asksvin") || prefabName.Contains("volture") || prefabName.Contains("vulture") || prefabName.Contains("valkyrie"))) __result = false;
}
// Draugh Race Logic
if (playerRace == "Draugh")
{
string prefabName = monster.gameObject.name.ToLower();
if (prefabName.Contains("skeleton") || prefabName.Contains("draugr") ||
prefabName.Contains("ghost") || prefabName.Contains("wraith"))
{
__result = false;
}
}
// Trenta Race Logic - base passive, live from the start (no progression
// gate, unlike Aswid's escalating list above).
//
// IsEnemy=false is the only lever this patch has, and it produces one
// behaviour: the mob never initiates. It reads as two different relationships
// only in flavour text - Greyling/Greydwarf/Leech/Deathsquito "don't see" a
// Trenta as prey (root-kin, or simply nothing with blood to sense), while
// Fuling/Troll "choose" not to fight one - but there is no third state to code
// for: vanilla's own damage-response AI already makes any of them retaliate if
// the Trenta strikes first, same as it does for Fenrir's wolves above.
if (playerRace == TrentaConfig.RaceId)
{
string prefabName = monster.gameObject.name.ToLower();
// "fuling" matched nothing: the Fuling family's prefabs are Goblin,
// GoblinArcher, GoblinBrute and GoblinShaman, so the Trenta's truce with them
// never existed despite this file and TrentaConfig both describing it.
// Unlike the other entries here, the goblin name needs its bosses carved back
// out - Yagluth is GoblinKing, and Hildir's arena pair are GoblinBruteBros /
// *_Hildir - because a substring test would otherwise pacify all three.
bool isGoblinKin = prefabName.Contains("goblin")
&& !prefabName.Contains("goblinking")
&& !prefabName.Contains("hildir")
&& !prefabName.Contains("brutebros");
if (prefabName.Contains("greyling") || prefabName.Contains("greydwarf") ||
isGoblinKin || prefabName.Contains("troll") ||
prefabName.Contains("leech") || prefabName.Contains("deathsquito"))
{
__result = false;
}
}
// Sverm Race Logic - Hive Pheromones. No progression gate at all, unlike
// Aswid's escalating list: an invertebrate is kin from the first minute.
//
// That absence of a gate is doing real work rather than saving a few lines.
// Every invertebrate in the game lives from the Swamp onward, so a gated
// version would have handed the race nothing until Bonemass; ungated, a
// newborn Sverm walks through leeches and deathsquitoes, which is the whole
// of its early game.
//
// Bonemass is deliberately NOT here. It is a mass of ooze and it would
// qualify, but a boss that ignores the player is not a boss.
if (playerRace == SvermConfig.RaceId)
{
string prefabName = monster.gameObject.name.ToLower();
// "blob" covers Blob, BlobElite, BlobTar and the Ashlands lava blob
// whatever it turns out to be called - which is why the one prefab name
// still unconfirmed in the design doc costs nothing here.
// "seeker" covers Seeker, SeekerBrood, SeekerBrute and SeekerQueen.
bool isKin = prefabName.Contains("blob")
|| prefabName.Contains("leech")
|| prefabName.Contains("tick")
|| prefabName.Contains("deathsquito")
|| prefabName.Contains("gjall")
|| prefabName.Contains("seeker");
if (isKin && !prefabName.Contains("bonemass"))
{
__result = false;
}
}
}
}
}
}
}