RacesOfValheim/JotunnModStub/Sverm/SvermCarapace.cs
2026-08-11 16:30:20 +02:00

133 lines
6.2 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The carapace: chest, legs and helmet, permanently, at every tier.
///
/// WHAT IT IS WORTH IS NOT WRITTEN DOWN. Each tier names a vanilla set in SvermConfig, and the
/// armour value is that set's three pieces read off ObjectDB and priced at quality 4. The race
/// therefore tracks vanilla's own balance forever: if a game patch buffs iron, the Sverm's
/// third tier is buffed with it, and nobody has to remember this file exists.
///
/// The lookup is cached per state and not per frame on purpose - GetBodyArmor runs from the
/// HUD and from every damage calculation, and ObjectDB.GetItemPrefab is a dictionary hit plus
/// a GetComponent on each of three prefabs.
///
/// Resolution is deferred rather than done at startup: ObjectDB is not populated when the
/// plugin wakes, so the first caller inside a world is what fills the cache. A missing prefab
/// contributes zero and logs once, so a renamed vanilla item costs a warning and a soft tier
/// rather than a null-reference in the damage path.
/// </summary>
public static class SvermCarapace
{
private static readonly float[] armorCache = new float[BossProgressionManager.MaxState + 1];
private static readonly bool[] armorCached = new bool[BossProgressionManager.MaxState + 1];
private static float eitrRegenReference = -1f;
/// <summary>
/// Total armour the carapace is worth at this tier: the three reference pieces at quality
/// 4, plus the hand-picked Fader bonus, which is the one number in the race that is chosen
/// rather than derived - there is no biome after the Ashlands to read a set from.
/// </summary>
public static float GetArmor(int state)
{
state = Mathf.Clamp(state, BossProgressionManager.MinState, BossProgressionManager.MaxState);
if (armorCached[state]) return armorCache[state];
if (ObjectDB.instance == null) return 0f; // too early; do not poison the cache
float total = 0f;
string[] set = SvermConfig.ArmorReferenceSets[state];
if (set != null)
{
foreach (string prefabName in set)
{
total += ArmorOf(prefabName);
}
}
if (state >= SvermConfig.AnnealedChitinState) total += SvermConfig.FaderArmorBonus;
armorCache[state] = total;
armorCached[state] = true;
Jotunn.Logger.LogInfo($"[Sverm] Carapace tier {state} priced at {total:0.#} armour " +
$"from {(set == null ? "nothing" : string.Join(" + ", set))} at quality " +
$"{SvermConfig.ArmorReferenceQuality}.");
return total;
}
/// <summary>Armour of one vanilla piece at the reference quality, or 0 with a warning.</summary>
private static float ArmorOf(string prefabName)
{
GameObject prefab = ObjectDB.instance.GetItemPrefab(prefabName);
if (prefab == null)
{
Jotunn.Logger.LogWarning($"[Sverm] No item '{prefabName}' to price the carapace from; " +
"it contributes nothing to this tier.");
return 0f;
}
ItemDrop drop = prefab.GetComponent<ItemDrop>();
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null) return 0f;
// Valheim's own formula: armour + (quality - 1) * armourPerLevel.
return drop.m_itemData.m_shared.m_armor +
(SvermConfig.ArmorReferenceQuality - 1) * drop.m_itemData.m_shared.m_armorPerLevel;
}
/// <summary>
/// Eitr regeneration the carapace hands back, from Eitr Sacs on.
///
/// The eitr-weave set gives REGENERATION, not maximum eitr - the maximum comes from food,
/// which the Sverm eats like anyone else. So sealing the armour slots never blocked magic;
/// it only slowed the recovery, and this gives back a fraction of it. Deliberately a
/// fraction: eitr-weave is light armour and pays for its regeneration in defence, and a
/// Sverm carrying a biome set at quality 4 AND the full regeneration would simply be a
/// better mage than a mage.
/// </summary>
public static float GetEitrRegenBonus(int state)
{
if (state < SvermConfig.EitrSacsState) return 0f;
if (ObjectDB.instance == null) return 0f;
if (eitrRegenReference < 0f)
{
float total = 0f;
foreach (string prefabName in SvermConfig.EitrReferenceSet)
{
GameObject prefab = ObjectDB.instance.GetItemPrefab(prefabName);
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
if (drop != null && drop.m_itemData != null && drop.m_itemData.m_shared != null)
{
total += drop.m_itemData.m_shared.m_eitrRegenModifier;
}
}
eitrRegenReference = total;
Jotunn.Logger.LogInfo($"[Sverm] Eitr Sacs reference: eitr-weave set regenerates {total:0.##}, " +
$"carapace returns {SvermConfig.EitrRegenFraction:P0} of it.");
}
return eitrRegenReference * SvermConfig.EitrRegenFraction;
}
/// <summary>True when this slot is taken by the carapace and cannot hold anything else.</summary>
public static bool IsSealedSlot(ItemDrop.ItemData.ItemType type)
{
foreach (ItemDrop.ItemData.ItemType sealedType in SvermConfig.SealedSlots)
{
if (sealedType == type) return true;
}
return false;
}
/// <summary>The tint the carapace, the helmet and the claws all share at this tier.</summary>
public static Color GetTint(int state)
{
state = Mathf.Clamp(state, BossProgressionManager.MinState, BossProgressionManager.MaxState);
return SvermConfig.CarapaceTintByState[state];
}
}
}