The body swap read the race off the ZDO and gave up when there was none, which is exactly the situation on the menu preview - FejdStartup instantiates a Player and calls PlayerProfile.LoadPlayerData on it, so the preview knows its own custom data and therefore its race, but it has no business owning a network view. It now falls back to m_customData the way every other race in the mod already does, so a Skeleton character looks like one before you press Start. The tier falls back the same way. In the menu there is no world loaded to have killed anything, so the preview is the base skeleton whatever the character has done elsewhere. feat(dvergar): age into a starred dverger's colours One star at Bonemass, two at Fader. Nothing is picked by eye. A starred creature in Valheim is not a second model: it is the same one recoloured by LevelEffects, whose LevelSetup list holds a hue/saturation/value offset per star. Those offsets are read off the Dverger prefab at runtime and applied to the race's own slate blue-grey, so the tiers land on the tints the game itself uses and keep following them if the prefab changes. The tier comes from the ZDO key DvergarRace already publishes, so a remote Dvergar is coloured at their own standing rather than the observer's - which matters as soon as anyone runs personal progression. Crossing a tier pushes a colour refresh through immediately, since the tint is only written from the UpdateColors postfix and Valheim runs that on its own schedule. Same treatment the health malus already gets a few lines above. The hood is deliberately untouched.
120 lines
5 KiB
C#
120 lines
5 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);
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
}
|