RacesOfValheim/JotunnModStub/Dvergar/DvergarAbilities.cs
kanym b0d18b0654 feat(text): on-screen messages come from translation files
Every message the mod puts in the middle or corner of the screen now reads
its text through ModText.Get, and messages.json holds 191 texts in English
and Italian. Values computed in code become $1, $2 in the text, so a
translator can put them wherever the sentence needs them. Branching
messages get one key per branch.

A few messages travel through a field before they are shown: thralls'
refusals and the building limits now hold a token or key there. Boss and
form names inside messages use tokens too, the bosses the game's own, so
"The Elder" arrives translated.

ModText.Get now localizes each argument before inserting it. A token
argument left for a later pass ran into the text after it: "$1's power"
became the unknown token "enemy_eikthyr's".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 20:30:03 +02:00

207 lines
8.5 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.IsDownIgnoringHeldKeys())
{
TryRunicPing(player);
}
if (rules.RunicOvercharge && DvergarConfigManagement.RunicOverchargeKey.Value.IsDownIgnoringHeldKeys())
{
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, ModText.Get("rov_msg_dvergar_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 ? ModText.Get("rov_msg_dvergar_runic_ping_echoes_answer", marked) : ModText.Get("rov_msg_dvergar_runic_ping_stone_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, ModText.Get("rov_msg_dvergar_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, ModText.Get("rov_msg_dvergar_runic_overcharge_next_blow_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;
}
}
}