RacesOfValheim/JotunnModStub/Factions/FactionsConfig.cs
kanym 7a618936f2 fix(factions): grudges survive an owner change and reach kin owned elsewhere
The grudge was a dictionary in the owner's memory: when another client
took over the creature mid-fight, its copy made peace on the next
UpdateTarget. Grudge spreads to kin also skipped kin owned by another
client. The grudge now lives on the creature's ZDO (attacker ZDOID and
an end time in network time), written only by the owner; kin owned
elsewhere get an RPC that their owner handles.
2026-11-06 21:28:13 +01:00

88 lines
6.4 KiB
C#

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;
public static ConfigEntry<float> GrudgeDuration;
public static ConfigEntry<bool> GrudgeSpreadEnabled;
public static ConfigEntry<float> GrudgeSpreadRadius;
// 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 float GrudgeSeconds => GrudgeDuration?.Value ?? 60f;
public static bool GrudgeSpreads => GrudgeSpreadEnabled?.Value ?? false;
public static float GrudgeSpreadMeters => GrudgeSpreadRadius?.Value ?? 20f;
public static void Bind(ConfigFile config)
{
const string factions = "Factions";
// Key kept as "Faction truce" even though it now covers the per-race truces too:
// renaming it would orphan the entry in every .cfg already written, and an existing
// .cfg beats the default in code - anyone who had switched this off would silently
// get the truce back.
HostilityEnabled = config.Bind(factions, "Faction truce", true,
new ConfigDescription("Mobs your race is at peace with leave you alone - they will not open a fight, " +
"hunt you during events, or smash structures while you are nearby, and raids made only " +
"of them are not sent at you. Turn this off and every " +
"creature treats you exactly as vanilla would. Covers BOTH halves, because both " +
"answer the same BaseAI.IsEnemy and a switch over only one of them changes " +
"nothing for most races: the faction rules in FactionHostilityPatch, and the " +
"per-race passives in RaceHostilityManager - Fenrir's wolves, Aswid's " +
"progression list, the Draugh's undead, the Trenta's Black Forest, the Sverm's " +
"invertebrates. Switching this off gives those five races up along with the " +
"faction truce. Hitting a mob still starts a fight: see Faction grudge below, " +
"which is what makes that true."));
GrudgeDuration = config.Bind(factions, "Faction grudge", 60f,
new ConfigDescription("Seconds a creature stays angry at a player of its own faction who hit it. " +
"Without this the truce is absolute in both directions: MonsterAI.UpdateTarget " +
"throws away any target BaseAI.IsEnemy calls a non-enemy, so a truced mob could " +
"not fight back even after being struck - it stood still, or fled because it " +
"was hurt and had no enemy it was allowed to have. Each creature holds one " +
"grudge, against the last player who hit it, and keeps it when another player's " +
"game takes over running that creature. 0 restores the " +
"old absolute truce.",
new AcceptableValueRange<float>(0f, 600f)));
// Joacwoo's request: the truce made the swamp and the crypts a walk for a Skeleton,
// since only the one creature you hit ever fought back.
GrudgeSpreadEnabled = config.Bind(factions, "Grudge spreads to kin", false,
new ConfigDescription("Off: only the creature you hit fights back. On: hit a creature of your " +
"own faction and every creature of that faction near it turns on you too, " +
"for the same Faction grudge time, the way vanilla's Dvergr treat a human. " +
"Peace still holds until you strike first."));
GrudgeSpreadRadius = config.Bind(factions, "Grudge spread radius", 20f,
new ConfigDescription("Meters around the creature you hit within which its kin join the fight, " +
"with Grudge spreads to kin on.",
new AcceptableValueRange<float>(5f, 60f)));
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."));
}
}
}