RacesOfValheim/JotunnModStub/Dvergur/DvergurStatusEffects.cs
kanym 3ec51aeb45 feat(dvergur): add the Dvergur race scaffold
An eighth race built on the existing per-race pattern: a MonoBehaviour driver
registered from Awake, a progression table keyed on the boss state, an SE_Stats
carrier, and Harmony patches for the individual perks.

The helmet slot stays free. Whatever the player equips keeps every one of its
stats and only its mesh is swapped for the dvergr hood, which is the Skeleton's
helmet handling turned into a replacement rather than a plain hide.

Every percentage the spec left open lives in DvergurConfig as a named value and
is marked TODO there, so tuning the race never means touching the logic. The
knobs are static readonly rather than const so that switching a perk off with a
zero does not fold its guard into dead code.
2026-08-02 21:45:44 +02:00

139 lines
6.1 KiB
C#

using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Passive carrier for the Dvergur. The stamina half of weapon affinity lives here rather than
/// in a Harmony patch because Valheim already routes attack cost through the status effects
/// (SEMan.ModifyAttackStaminaUsage), so there is nothing to intercept.
/// </summary>
public class SE_DvergurStats : SE_Stats
{
public override void Setup(Character character)
{
base.Setup(character);
name = DvergurConfig.StatsSE;
m_name = "Stone and Forge";
m_tooltip = "Small, sturdy, and born to the anvil.";
}
public override void ModifyAttackStaminaUsage(float baseStaminaUse, ref float staminaUse)
{
base.ModifyAttackStaminaUsage(baseStaminaUse, ref staminaUse);
if (!(m_character is Player player) || player != Player.m_localPlayer) return;
if (!DvergurRace.IsDvergur()) return;
int state = BossProgressionManager.GetCurrentProgressionState();
// m_rightItem rather than GetCurrentWeapon(): it is what the rest of the mod reaches
// for, and it is the item the swing is actually paying for.
ItemDrop.ItemData weapon = Traverse.Create(player).Field<ItemDrop.ItemData>("m_rightItem").Value;
if (weapon == null || weapon.m_shared == null) return;
Skills.SkillType skill = weapon.m_shared.m_skillType;
// Mining is a discount on the pickaxe, not a weapon affinity, so it is checked apart.
if (skill == Skills.SkillType.Pickaxes)
{
if (DvergurProgression.GetRules(state).MiningAffinity)
{
staminaUse = Mathf.Max(0f, staminaUse * (1f - DvergurConfig.MiningStaminaPercent));
}
return;
}
DvergurConfig.WeaponAffinity? affinity = DvergurProgression.GetAffinity(skill, state);
if (!affinity.HasValue) return;
// Static by design: the discount is the same the day it unlocks as it is after Fader.
staminaUse = Mathf.Max(0f, staminaUse * (1f - affinity.Value.StaminaPercent) - affinity.Value.StaminaFlat);
}
}
/// <summary>
/// Runic Overcharge, held between the cast and the blow that spends it. Purely a marker with a
/// countdown - DvergurPatches reads it when the next affine hit lands.
/// </summary>
public class SE_DvergurOvercharge : StatusEffect
{
}
/// <summary>
/// Ability cooldowns. They exist only so the HUD has something to count down, the way
/// SE_SkadiAbilityCooldown does: Valheim prints the remaining time of any effect with a m_ttl.
/// </summary>
public class SE_DvergurAbilityCooldown : StatusEffect
{
}
public static class DvergurStatusEffects
{
public static SE_DvergurStats StatsSE { get; private set; }
public static void RegisterStatusEffects()
{
Sprite icon = GetItemIcon("HelmetDverger") ?? GetItemIcon("DvergrKeyFragment") ?? GetItemIcon("Copper");
var stats = ScriptableObject.CreateInstance<SE_DvergurStats>();
stats.name = DvergurConfig.StatsSE;
stats.m_name = "Stone and Forge";
stats.m_icon = icon;
CustomStatusEffect customStats = new CustomStatusEffect(stats, fixReference: false);
ItemManager.Instance.AddStatusEffect(customStats);
StatsSE = customStats.StatusEffect as SE_DvergurStats;
var overcharge = ScriptableObject.CreateInstance<SE_DvergurOvercharge>();
overcharge.name = DvergurConfig.OverchargeSE;
overcharge.m_name = "Runic Overcharge";
overcharge.m_tooltip = "Your next blow with a favoured weapon carries the whole of the charge.";
overcharge.m_icon = GetItemIcon("Eitr") ?? icon;
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(overcharge, fixReference: false));
var pingCd = ScriptableObject.CreateInstance<SE_DvergurAbilityCooldown>();
pingCd.name = DvergurConfig.RunicPingCdSE;
pingCd.m_name = "Runic Ping";
pingCd.m_tooltip = "The runes must settle before you may call on them again.";
pingCd.m_icon = GetItemIcon("DvergrKeyFragment") ?? icon;
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(pingCd, fixReference: false));
var overchargeCd = ScriptableObject.CreateInstance<SE_DvergurAbilityCooldown>();
overchargeCd.name = DvergurConfig.OverchargeCdSE;
overchargeCd.m_name = "Overcharge Cooldown";
overchargeCd.m_tooltip = "The runes are spent.";
overchargeCd.m_icon = GetItemIcon("Eitr") ?? icon;
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(overchargeCd, fixReference: false));
}
/// <summary>
/// Drives the HUD countdown for an ability, mirroring SkadiRace.ApplyCooldownIcon: the
/// effect only has to exist with a m_ttl for the icon to tick down by itself.
/// </summary>
public static void ApplyCooldownIcon(string effectName, float seconds)
{
if (Player.m_localPlayer == null || seconds <= 0f) return;
int hash = effectName.GetStableHashCode();
SEMan seman = Player.m_localPlayer.GetSEMan();
seman.RemoveStatusEffect(hash, true);
seman.AddStatusEffect(hash, true);
StatusEffect se = seman.GetStatusEffect(hash);
if (se != null) se.m_ttl = seconds;
}
private static Sprite GetItemIcon(string prefabName)
{
GameObject prefab = PrefabManager.Cache.GetPrefab<ItemDrop>(prefabName)?.gameObject;
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
if (drop != null && drop.m_itemData.m_shared.m_icons.Length > 0)
{
return drop.m_itemData.m_shared.m_icons[0];
}
return null;
}
}
}