RacesOfValheim/JotunnModStub/Dvergur/DvergurRace.cs
kanym 79a54fb533 fix(dvergur): correct the health malus, body scale and hood
Health: the malus was taken off Character.GetMaxHealth, which only made the
getter lie. Character.SetMaxHealth is the one thing that clamps current health
to the new maximum and it never saw a lower number, so the bar shrank while the
value stayed where it was. Player.SetMaxHealth also flashes the bar whenever the
new maximum is above GetMaxHealth(), so against a getter that always read low
every food tick looked like an increase and re-triggered the flash. Taking the
malus off Player.GetTotalFoodValue instead puts it upstream of both, and leaves
Valheim to do its own clamping on an honest number.

Scale: applied from the plugin MonoBehaviour, it was overwritten on the same
frame by Trol's Player.Update postfix, which forced every non-Trol player back
to scale 1. Sizing now happens in a postfix on that same method, the way the
Trol does it, and RaceScale records which races size themselves so they stop
undoing each other. This also fixes the Furling, which was losing the same race.

Hood: the Dverger Circlet is a metal band, not the hood, and instantiating a
skinned mesh leaves its bones pointing at the source prefab so it renders
nowhere. The headgear is now lifted off the Dvergr NPC and rebound to the
player's own bones by name, as the Skeleton does for whole bodies.

Skin is tinted to a dvergr grey, saved and restored through the same
OriginalSkinColor keys the other tinted races use.

Adds a dumpdverger console command: prefab contents live in the asset bundles
and cannot be read outside a running game, so this is how the hood mesh and the
per-star LevelEffects colours get identified.
2026-08-02 22:58:11 +02:00

84 lines
3.2 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Driver for the Dvergur: 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
/// DvergurPatches, since that is the one place it survives; the perks are the other patches in
/// that file; the two eitr abilities are in DvergurAbilities; and every number they use comes
/// from DvergurConfig by way of DvergurProgression.
/// </summary>
public class DvergurRace : MonoBehaviour
{
public static DvergurRace 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()
{
DvergurStatusEffects.RegisterStatusEffects();
}
public static bool IsDvergur()
{
return Player.m_localPlayer != null &&
Player.m_localPlayer.m_customData.ContainsKey("Race") &&
Player.m_localPlayer.m_customData["Race"] == DvergurConfig.RaceId;
}
private void Update()
{
Player player = Player.m_localPlayer;
if (player == null) return;
bool isDvergur = IsDvergur();
DvergurVisuals.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 Dvergur.
float desiredMalus = isDvergur ? DvergurProgression.Current.HealthMalus : 0f;
if (!Mathf.Approximately(desiredMalus, lastAppliedMalus))
{
lastAppliedMalus = desiredMalus;
Traverse.Create(player).Method("UpdateFood", 0f, true).GetValue();
}
if (!isDvergur || 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 (DvergurStatusEffects.StatsSE != null)
{
int hash = DvergurConfig.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(DvergurConfig.StateZdoKey, state);
}
DvergurAbilities.Tick(player, state);
}
}
}