RacesOfValheim/JotunnModStub/Dvergar/DvergarAbilities.cs
kanym 3607d7323d feat(dvergar): make ability numbers and keybinds BepInEx-configurable
Adds DvergarConfigManagement.cs rather than reusing the DvergarConfig
name: that existing class is a different kind of thing - identity
constants that must never change (renaming RaceId would orphan saved
characters), hand-tuned cosmetic placement numbers (eye/hood position),
and per-tier tables (health malus, smithing/trader discounts) left alone
under the same boundary drawn for every other race's progression table.

Only the genuine ability knobs move: Runic Ping and Runic Overcharge
(cost, radius, duration, cooldown, marker light) each get a Configuration
Manager section, plus mining and weapon-affinity percentages under
Passive Combat, plus the two keybinds. DvergarConfig's fields for those
now forward to DvergarConfigManagement (same const-to-property trick used
for Skadi), so all 109 existing call sites across 11 files kept working
unchanged except the two keybind consumers (DvergarAbilities,
RaceCompendium's in-game ability text), which now read the live
configured key so a rebind stays reflected everywhere it's shown.

Weapon-affinity DamagePercent/StaminaPercent are overridden with the live
config value inside DvergarProgression.GetRules rather than baked into
the static WeaponAffinities array, since that array evaluates at
class-load time - before RacesOfValheim.Awake() calls Bind - and would
risk a null-reference on the ConfigEntry.

Config sections stay bound alphabetically by race (Aswid, Draugh,
Dvergar, Skadi) in RacesOfValheim.Awake().
2026-08-06 13:56:00 +02:00

207 lines
8.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The Dvergar's two eitr abilities.
///
/// Runic Ping (Yagluth) - spend eitr to light up ore veins and hidden enemies nearby.
/// Runic Overcharge (Fader) - spend eitr to load the next blow of a favoured weapon.
///
/// Both are driven from DvergarRace.Update and both read their cost, radius, duration and
/// cooldown out of DvergarConfig.
/// </summary>
public static class DvergarAbilities
{
private static float lastPingTime = -1000f;
private static float lastOverchargeTime = -1000f;
public static void Tick(Player player, int state)
{
// Ability input is gated exactly like every other race's: RaceInput mirrors
// Player.TakeInput, so nothing fires while a map, chest or chat window has focus.
if (!RaceInput.Allowed) return;
DvergarProgression.DvergarRules rules = DvergarProgression.GetRules(state);
if (rules.RunicPing && DvergarConfigManagement.RunicPingKey.Value.IsDown())
{
TryRunicPing(player);
}
if (rules.RunicOvercharge && DvergarConfigManagement.RunicOverchargeKey.Value.IsDown())
{
TryRunicOvercharge(player);
}
}
// ─── Runic Ping ──────────────────────────────────────────────────────
private static void TryRunicPing(Player player)
{
if (Time.time - lastPingTime < DvergarConfig.RunicPingCooldown) return;
if (player.GetEitr() < DvergarConfig.RunicPingEitrCost)
{
player.Message(MessageHud.MessageType.Center, "Not enough eitr");
return;
}
player.UseEitr(DvergarConfig.RunicPingEitrCost);
lastPingTime = Time.time;
DvergarStatusEffects.ApplyCooldownIcon(DvergarConfig.RunicPingCdSE, DvergarConfig.RunicPingCooldown);
int marked = MarkSurroundings(player);
player.Message(MessageHud.MessageType.Center,
marked > 0 ? $"Runic Ping: {marked} echoes answer" : "Runic Ping: the stone is silent");
}
/// <summary>
/// Lights every ore vein and every hostile within the radius for the duration. The marker
/// is a local point light parented to the target, the same trick the Aswid's Auspex uses -
/// nothing is replicated, so it costs other players nothing.
/// </summary>
private static int MarkSurroundings(Player player)
{
Vector3 center = player.transform.position;
HashSet<GameObject> alreadyMarked = new HashSet<GameObject>();
int marked = 0;
foreach (Collider collider in Physics.OverlapSphere(center, DvergarConfig.RunicPingRadius))
{
if (collider == null) continue;
GameObject vein = FindVeinRoot(collider);
if (vein == null || !alreadyMarked.Add(vein)) continue;
SpawnMarker(vein.transform, DvergarConfig.RunicPingOreColor);
marked++;
}
foreach (Character character in Character.GetAllCharacters())
{
if (character == null || character == player || character.IsDead() || character.IsTamed()) continue;
if (Vector3.Distance(character.transform.position, center) > DvergarConfig.RunicPingRadius) continue;
if (!BaseAI.IsEnemy(player, character)) continue;
if (!alreadyMarked.Add(character.gameObject)) continue;
SpawnMarker(character.transform, DvergarConfig.RunicPingEnemyColor);
marked++;
}
return marked;
}
/// <summary>
/// The mineable object a collider belongs to, or null. Both halves matter: the component
/// check keeps ordinary terrain out, and the name check keeps plain rock and trees out of
/// what is meant to read as an ore reading.
/// </summary>
private static GameObject FindVeinRoot(Collider collider)
{
GameObject root = null;
MineRock5 mineRock5 = collider.GetComponentInParent<MineRock5>();
if (mineRock5 != null) root = mineRock5.gameObject;
if (root == null)
{
MineRock mineRock = collider.GetComponentInParent<MineRock>();
if (mineRock != null) root = mineRock.gameObject;
}
if (root == null)
{
Destructible destructible = collider.GetComponentInParent<Destructible>();
if (destructible != null && destructible.m_destructibleType != DestructibleType.Tree)
{
root = destructible.gameObject;
}
}
if (root == null) return null;
string name = root.name.ToLower();
foreach (string veinName in DvergarConfig.RunicPingVeinNames)
{
if (name.Contains(veinName)) return root;
}
return null;
}
private static void SpawnMarker(Transform target, Color color)
{
GameObject markerObj = new GameObject("DvergarRunicMark");
markerObj.transform.SetParent(target, false);
markerObj.transform.localPosition = Vector3.up;
Light light = markerObj.AddComponent<Light>();
light.type = LightType.Point;
light.color = color;
light.range = DvergarConfig.RunicPingLightRange;
light.intensity = DvergarConfig.RunicPingLightIntensity;
light.shadows = LightShadows.None;
if (DvergarRace.Instance != null)
{
DvergarRace.Instance.StartCoroutine(DestroyAfter(markerObj, DvergarConfig.RunicPingDuration));
}
else
{
// No driver to run the coroutine on: better a marker that never appears than one
// that never goes away.
Object.Destroy(markerObj);
}
}
private static IEnumerator DestroyAfter(GameObject obj, float seconds)
{
yield return new WaitForSeconds(seconds);
if (obj != null) Object.Destroy(obj);
}
// ─── Runic Overcharge ────────────────────────────────────────────────
private static void TryRunicOvercharge(Player player)
{
if (Time.time - lastOverchargeTime < DvergarConfig.RunicOverchargeCooldown) return;
int overchargeHash = DvergarConfig.OverchargeSE.GetStableHashCode();
if (player.GetSEMan().HaveStatusEffect(overchargeHash)) return; // already loaded
if (player.GetEitr() < DvergarConfig.RunicOverchargeEitrCost)
{
player.Message(MessageHud.MessageType.Center, "Not enough eitr");
return;
}
player.UseEitr(DvergarConfig.RunicOverchargeEitrCost);
lastOverchargeTime = Time.time;
DvergarStatusEffects.ApplyCooldownIcon(DvergarConfig.OverchargeCdSE, DvergarConfig.RunicOverchargeCooldown);
player.GetSEMan().AddStatusEffect(overchargeHash, true);
StatusEffect se = player.GetSEMan().GetStatusEffect(overchargeHash);
// The charge is held, not permanent: it lapses if no favoured blow lands in time.
if (se != null) se.m_ttl = DvergarConfig.RunicOverchargeWindow;
player.Message(MessageHud.MessageType.Center, "Runic Overcharge: your next blow is loaded");
}
/// <summary>
/// Spends a held charge. Called from the damage patch once it has confirmed the blow was
/// struck with a weapon the Dvergar is at home with.
/// </summary>
public static bool ConsumeOvercharge(Player player)
{
if (player == null) return false;
int hash = DvergarConfig.OverchargeSE.GetStableHashCode();
if (!player.GetSEMan().HaveStatusEffect(hash)) return false;
player.GetSEMan().RemoveStatusEffect(hash, true);
return true;
}
}
}