RacesOfValheim/JotunnModStub/Dvergar/DvergarStatusEffects.cs
kanym 5febc588ea feat(dvergar): rename from Dvergur, and give it a lightning affinity
Rename covers the folder, all seven filenames and every identifier, status
effect name and display string. Valheim's own Dverger/dvergr prefab references
are untouched - the names never collided.

The race id is what sits in m_customData and the ZDO, so renaming it alone would
have handed every existing character the selection menu again. DvergarRace
migrates the old id on spawn, before anything else reads it.

Runic Spark is modelled on Skadi's Ice Affinity and deliberately shares its
shape: live from the base tier and growing a flat step per progression state, so
+2 at Base through +16 after Fader. Lightning rather than frost, and like
Skadi's it rides every blow rather than melee only. AddFlat gained the elemental
channels while it was there - lightning would otherwise have landed in generic
damage.

The Trol remote-sync trace fired on a two-second timer for as long as a remote
player existed, which in the main menu is forever: Player.m_localPlayer is null
there, so the character preview counts as remote and it never stopped. Keyed on
the state it reports instead, so it still catches a remote player being sized
wrongly without narrating an idle menu.
2026-08-04 00:22:58 +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 Dvergar. 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_DvergarStats : SE_Stats
{
public override void Setup(Character character)
{
base.Setup(character);
name = DvergarConfig.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 (!DvergarRace.IsDvergar()) 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 (DvergarProgression.GetRules(state).MiningAffinity)
{
staminaUse = Mathf.Max(0f, staminaUse * (1f - DvergarConfig.MiningStaminaPercent));
}
return;
}
DvergarConfig.WeaponAffinity? affinity = DvergarProgression.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 - DvergarPatches reads it when the next affine hit lands.
/// </summary>
public class SE_DvergarOvercharge : 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_DvergarAbilityCooldown : StatusEffect
{
}
public static class DvergarStatusEffects
{
public static SE_DvergarStats StatsSE { get; private set; }
public static void RegisterStatusEffects()
{
Sprite icon = GetItemIcon("HelmetDverger") ?? GetItemIcon("DvergrKeyFragment") ?? GetItemIcon("Copper");
var stats = ScriptableObject.CreateInstance<SE_DvergarStats>();
stats.name = DvergarConfig.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_DvergarStats;
var overcharge = ScriptableObject.CreateInstance<SE_DvergarOvercharge>();
overcharge.name = DvergarConfig.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_DvergarAbilityCooldown>();
pingCd.name = DvergarConfig.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_DvergarAbilityCooldown>();
overchargeCd.name = DvergarConfig.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;
}
}
}