RacesOfValheim/JotunnModStub/Dvergar/DvergarRace.cs
kanym 76b89a8e5a fix(dvergar): put the eyes on the head instead of on the camera
Both faults reported came from the same wrong anchor. Character.m_eye is the
camera's eye point, not the model's: it lives outside the scaled rig, so on a
Dvergar - who renders at 78% - the quads hung where a full-height human's face
would have been, a foot above the head. And it turns with the look direction
rather than with the face, so they pointed wherever the player was looking rather
than wherever the head was.

They now hang off the player root, unscaled, and are placed onto the head bone in
world space every frame: position from the bone, orientation from the player's
own facing, and every offset multiplied by the race's visual scale so they shrink
with the body. That also means the placement constants are honest metres measured
from the base of the skull, which is what makes them tunable at all.

Driven per frame from the two places that already tick a Dvergar - the race
script for the local player, the visual controller for remote ones - because
world-space placement has to be re-applied rather than inherited.
2026-08-05 20:14:08 +02:00

124 lines
5.2 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 int lastAppliedStarTier = -1;
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);
// Every frame, because the eyes are placed onto the head bone in world space rather
// than parented to it - see ApplyEyes for why.
DvergarVisuals.ApplyEyes(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);
}
// The skin tint is only written from the UpdateColors postfix, which Valheim runs on
// its own schedule - so crossing into a new star tier would otherwise not show until
// something else happened to refresh the visuals. Push one through, the same way the
// health malus above pushes a food update.
int starTier = DvergarVisuals.GetStarTier(state);
if (starTier != lastAppliedStarTier)
{
lastAppliedStarTier = starTier;
VisEquipment visEq = player.GetComponent<VisEquipment>();
if (visEq != null) Traverse.Create(visEq).Method("UpdateColors").GetValue();
}
DvergarAbilities.Tick(player, state);
}
}
}