RacesOfValheim/JotunnModStub/Dvergar/DvergarRace.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

107 lines
4.3 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Driver for the Dvergar: a short, stocky smith who trades a slice of health for an affinity
/// with hammer, bow, axe and crossbow, a knack for mining and the ear of Haldor and Hildir.
///
/// Only the per-frame upkeep lives here. Body scale is a Player.Update postfix in
/// DvergarPatches, since that is the one place it survives; the perks are the other patches in
/// that file; the two eitr abilities are in DvergarAbilities; and every number they use comes
/// from DvergarConfig by way of DvergarProgression.
/// </summary>
public class DvergarRace : MonoBehaviour
{
public static DvergarRace Instance;
// NaN so that the very first frame always counts as a change and pushes the malus through.
private float lastAppliedMalus = float.NaN;
private void Awake()
{
Instance = this;
}
public void CreateModAssets()
{
DvergarStatusEffects.RegisterStatusEffects();
}
/// <summary>
/// The race was called "Dvergur" before it was renamed, and that string is what got written
/// into m_customData and the ZDO. Without this, every character created under the old name
/// would come back with no race at all and be handed the selection menu again.
/// </summary>
public const string LegacyRaceId = "Dvergur";
public static void MigrateLegacyRaceId(Player player)
{
if (player == null || player.m_customData == null) return;
if (!player.m_customData.TryGetValue("Race", out string race) || race != LegacyRaceId) return;
player.m_customData["Race"] = DvergarConfig.RaceId;
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
nview.GetZDO().Set("RacesOfValheim_Race", DvergarConfig.RaceId);
}
Jotunn.Logger.LogInfo($"[Dvergar] Migrated '{LegacyRaceId}' to '{DvergarConfig.RaceId}' for {player.GetPlayerName()}.");
}
public static bool IsDvergar()
{
return Player.m_localPlayer != null &&
Player.m_localPlayer.m_customData.ContainsKey("Race") &&
Player.m_localPlayer.m_customData["Race"] == DvergarConfig.RaceId;
}
private void Update()
{
Player player = Player.m_localPlayer;
if (player == null) return;
bool isDvergar = IsDvergar();
DvergarVisuals.ApplyHood(player);
// Max health is recomputed by Player.UpdateFood, which runs on its own schedule. Left
// to itself the malus would only take effect at the next food tick, so a change of
// race or of tier is pushed through immediately - this is also what makes the health
// bar and the number agree the instant a player becomes (or stops being) a Dvergar.
float desiredMalus = isDvergar ? DvergarProgression.Current.HealthMalus : 0f;
if (!Mathf.Approximately(desiredMalus, lastAppliedMalus))
{
lastAppliedMalus = desiredMalus;
Traverse.Create(player).Method("UpdateFood", 0f, true).GetValue();
}
if (!isDvergar || player.IsDead()) return;
int state = BossProgressionManager.GetCurrentProgressionState();
// Keep the passive carrier attached. It is re-added rather than assumed because a
// respawn, a death or a world load all clear the player's status effects.
if (DvergarStatusEffects.StatsSE != null)
{
int hash = DvergarConfig.StatsSE.GetStableHashCode();
if (!player.GetSEMan().HaveStatusEffect(hash))
{
player.GetSEMan().AddStatusEffect(hash, true);
}
}
// Published for remote clients, matching how the Skeleton replicates its tier.
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
nview.GetZDO().Set(DvergarConfig.StateZdoKey, state);
}
DvergarAbilities.Tick(player, state);
}
}
}