Merge branch 'war-vulture' into main

Sverm Ravager Strain (replacing the Stinger Morph), Trenta Elder Root, the
Trol food and Black Forest truce work, and the faction FindEnemy patch.

Conflicts resolved:

- FactionHostilityPatch.cs: both sides added a class at the same spot and
  they cover different holes, so both are kept - FactionStaticTargetPatch
  (main, stops same-faction mobs smashing structures) and
  FactionHuntPlayerPatch (war-vulture, closes the HuntPlayer fall-through).

- RaceCompendium.cs: both branches independently fixed the same off-by-one
  in the Trol's progression gates. Took main's wording, which matches the
  code - Axes are in the melee bonus (TrolDamagePatches sends them to
  m_slash), the 4th food slot has no gate at all, and Yagluth is +5 not +10
  (TrolConfig.YagluthMeleeBonus). Kept war-vulture's two new lines, the
  Kindling fire weakness and the Black Forest truce.

- Package/manifest.json: 1.4.8, which is what the root manifest and
  PluginVersion already said.

RacesOfValheim.cs referenced SvermConfig.StingerSE/StingerCdSE, which
war-vulture renamed to Ravager. Git merged those lines without a conflict
and the result did not compile; renamed.

Also in this commit:

- Factions on/off switches (FactionsConfig): "Faction truce" and "Faction
  merchants", both default on, both read live so they take effect without a
  restart. The truce gates its three postfixes; the merchants gate the
  Character.Awake setup plus every behavioural patch, so turning them off
  drops the prompt and the trade window on mobs already awake.

- SerpentMeat sells for 200 instead of 1 at the Fuling bench, moved to keep
  the list's ascending price order (the sell panel renders it as declared).

- DraughFoodPatches mapped "SerpentMeatRaw", which is not a prefab; the
  internal id is SerpentMeat, so serpent meat never cooked for a Draugh.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Ferdinando 2026-08-25 18:52:10 +02:00
commit 34bdf35325
33 changed files with 1829 additions and 573 deletions

View file

@ -70,15 +70,36 @@ namespace RacesOfValheim
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;
string race = GetPlayerRace(player);
if (race == null) return false;
if (!RaceFactionMobs.TryGetValue(race, out HashSet<string> mobs)) return false;
string prefabName = monster.gameObject.name.Replace("(Clone)", "").Trim();
return mobs.Contains(prefabName);
}
// m_customData is the local save file's dictionary: it is only ever populated for
// the client's own m_localPlayer, so IsEnemy running on a remote AI owner (a
// dedicated server, or any other nearby player who ends up owning the mob's ZDO)
// read an empty dictionary here and treated every truce as hostile. Every other
// race-aware patch in this project reads "RacesOfValheim_Race" off the ZDO instead
// because that field actually replicates - check it first, and only fall back to
// m_customData for the single-player/local-owner case where the ZDO hasn't been
// written yet (e.g. brand new character before the race pick's Set() call runs).
public static string GetPlayerRace(Player player)
{
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
string zdoRace = nview.GetZDO().GetString("RacesOfValheim_Race", "");
if (!string.IsNullOrEmpty(zdoRace)) return zdoRace;
}
return player.m_customData.TryGetValue("Race", out string race) ? race : null;
}
public static void Postfix(Character a, Character b, ref bool __result)
{
if (!FactionsConfig.Hostility) return;
if (!__result) return; // already not enemies, nothing to add
if (a == null || b == null) return;
@ -116,6 +137,7 @@ namespace RacesOfValheim
public static void Postfix(MonsterAI __instance)
{
if (!FactionsConfig.Hostility) return;
Traverse targetStatic = Traverse.Create(__instance).Field("m_targetStatic");
if (targetStatic.GetValue() == null) return;
@ -132,4 +154,32 @@ namespace RacesOfValheim
}
}
}
// IsEnemy is not the only way a mob picks the player, and the other way ignores it
// completely. BaseAI.FindEnemy() walks every character asking IsEnemy - which both truce
// patches above answer - but when that walk comes up empty and the creature HuntPlayer()s,
// it falls through to Player.GetClosestPlayer(transform.position, 200f) and returns whoever
// it finds, without asking anything about factions.
//
// That fall-through is why the Black Forest truce looked broken to the Trol's testers:
// MonsterAI.UpdateAI turns hunting on for every IsEventCreature() while an event is running,
// and the events that reach a Trol are "the forest is moving" (Greydwarves), "you are being
// hunted" (Trolls) and the Fuling army - the exact three they named. Anything a SpawnSystem
// entry or a TriggerSpawner flags with huntPlayer arrives by the same door.
//
// Filtering the RESULT rather than restating the rule keeps one source of truth: the public
// instance IsEnemy(Character) calls the static overload both postfixes above hook, so this
// honours whatever the faction sets and RaceHostilityManager's per-race lists already agree
// on. Retaliation is untouched - a mob the player hits is targeted through
// BaseAI.OnDamaged/SetTarget, which never goes near FindEnemy.
[HarmonyPatch(typeof(BaseAI), "FindEnemy")]
public static class FactionHuntPlayerPatch
{
public static void Postfix(BaseAI __instance, ref Character __result)
{
if (!FactionsConfig.Hostility) return;
if (__result == null || !__result.IsPlayer()) return;
if (!__instance.IsEnemy(__result)) __result = null;
}
}
}

View file

@ -117,7 +117,6 @@ namespace RacesOfValheim
{
new CatalogEntry("BlackMetalScrap", 1),
new CatalogEntry("LoxMeat", 1),
new CatalogEntry("SerpentMeat", 1),
new CatalogEntry("SerpentScale", 1),
new CatalogEntry("LoxPelt", 2),
new CatalogEntry("Flax", 5),
@ -133,6 +132,7 @@ namespace RacesOfValheim
// runtime, so hovering a Fuling merchant once will name them in the log.
new CatalogEntry("Tuna", 50),
new CatalogEntry("CoralCod", 70),
new CatalogEntry("SerpentMeat", 200),
new CatalogEntry("GoblinTotem", 350),
new CatalogEntry("TrophyGoblin", 350),
new CatalogEntry("TrophyGoblinShaman", 350),

View file

@ -33,7 +33,7 @@ namespace RacesOfValheim
public static bool Prefix(Trader __instance)
{
if (((Component)__instance).GetComponent<FactionMerchant>() != null)
if (FactionsConfig.Merchants && ((Component)__instance).GetComponent<FactionMerchant>() != null)
{
return false;
}
@ -63,6 +63,7 @@ namespace RacesOfValheim
{
public static void Postfix(Trader __instance)
{
if (!FactionsConfig.Merchants) return;
if (((Component)__instance).GetComponent<FactionMerchant>() == null) return;
__instance.CancelInvoke("RandomTalk");
}
@ -88,6 +89,7 @@ namespace RacesOfValheim
public static void Postfix(Player __instance)
{
if (!FactionsConfig.Merchants) return;
// Both fields are private in the assembly this project compiles against.
var traverse = Traverse.Create(__instance);
Character hovering = traverse.Field("m_hoveringCreature").GetValue<Character>();
@ -117,7 +119,7 @@ namespace RacesOfValheim
{
public static bool Prefix(Player __instance, GameObject go, bool hold, bool alt)
{
if (go == null) return true;
if (go == null || !FactionsConfig.Merchants) return true;
FactionMerchant merchant = go.GetComponentInParent<FactionMerchant>();
if (merchant == null) return true; // not ours - let vanilla's own resolution run
@ -141,6 +143,7 @@ namespace RacesOfValheim
{
public static bool Prefix(MonsterAI __instance, ref bool __result)
{
if (!FactionsConfig.Merchants) return true;
// BaseAI.m_character is private in the assembly this project compiles against.
Character character = Traverse.Create(__instance).Field("m_character").GetValue<Character>();
if (FactionMerchantRuntime.IsProtected(character))
@ -160,7 +163,7 @@ namespace RacesOfValheim
{
public static bool Prefix(Character __instance, HitData hit)
{
if (hit != null && hit.GetAttacker() == Player.m_localPlayer && FactionMerchantRuntime.IsProtected(__instance))
if (FactionsConfig.Merchants && hit != null && hit.GetAttacker() == Player.m_localPlayer && FactionMerchantRuntime.IsProtected(__instance))
{
return false;
}

View file

@ -22,6 +22,7 @@ namespace RacesOfValheim
public static void Postfix(Character __instance)
{
if (!FactionsConfig.Merchants) return;
if (__instance == null) return;
GameObject go = ((Component)__instance).gameObject;
if (go.GetComponent<FactionMerchant>() != null) return; // already set up

View file

@ -0,0 +1,50 @@
using BepInEx.Configuration;
namespace RacesOfValheim
{
/// <summary>
/// The two switches that turn the Factions feature off, bound as BepInEx ConfigEntry so they
/// show up in Configuration Manager - same shape as WarVultureConfig, which is the house model.
///
/// Two switches rather than one because the halves fail differently. The truce is invisible
/// when it works and only shows up as mobs that ignore you; the merchants are a whole second
/// interaction bolted onto creatures that vanilla never meant to talk. Anyone hunting a mod
/// conflict wants to rule out one without losing the other.
///
/// Both read live, on every call, rather than being cached at patch time: the patches stay
/// installed and answer "not my business" while a switch is off. That costs one bool read per
/// call and buys a toggle that takes effect without restarting the game - with one asymmetry
/// worth knowing about, spelled out on Merchants below.
/// </summary>
public static class FactionsConfig
{
public static ConfigEntry<bool> HostilityEnabled;
public static ConfigEntry<bool> MerchantsEnabled;
// Null-safe, defaulting to ON, for the same reason WarVultureConfig guards its own reads:
// a patch that somehow runs before Bind() should behave like the shipped default rather
// than throw. Awake() binds before harmony.PatchAll(), so this should never fire.
public static bool Hostility => HostilityEnabled?.Value ?? true;
public static bool Merchants => MerchantsEnabled?.Value ?? true;
public static void Bind(ConfigFile config)
{
const string factions = "Factions";
HostilityEnabled = config.Bind(factions, "Faction truce", true,
new ConfigDescription("Mobs of your race's faction leave you alone - they will not open a fight, " +
"hunt you during events, or smash structures near you. Turn this off and every " +
"creature treats you exactly as vanilla would. Does not touch the older per-race " +
"truces in RaceHostilityManager (Fenrir, Aswid, Draugh, Trenta), which have no " +
"switch of their own. Hitting a mob still starts a fight either way: retaliation " +
"runs through BaseAI.OnDamaged, which no truce has a say over."));
MerchantsEnabled = config.Bind(factions, "Faction merchants", true,
new ConfigDescription("Certain mobs of your own faction can be traded with - 'E - Talk' opens a shop " +
"where you sell them what their kind wants. Turn this off and they are ordinary " +
"creatures again. Takes effect immediately for the prompt, the trade window and " +
"the no-hit protection; the hidden Trader component already grafted onto mobs " +
"that woke up earlier stays until they unload, but nothing will read it."));
}
}
}