RacesOfValheim/JotunnModStub/Trol/TrolPatches.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

228 lines
9.6 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
public static class TrolSkinColorPatch
{
// Golden blonde hair
private static readonly Color HairColor = new Color(0.9f, 0.8f, 0.4f, 1f);
public static void Postfix(VisEquipment __instance)
{
if (!__instance.m_isPlayer) return;
Player player = __instance.GetComponent<Player>();
if (player == null) return;
// ZDO first (in-game + remote players), then m_customData fallback
// (main menu preview and first spawn frame — Player.m_localPlayer is null there)
// DO NOT check player == Player.m_localPlayer in the fallback.
string race = "Human";
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
race = nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
else if (player.m_customData != null &&
player.m_customData.TryGetValue("Race", out string savedRace))
race = savedRace;
if (race != "Trol") return;
// Apply skin, hair, beard colors here...
if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null)
{
// Peacock blue (deep greenish-blue)
Color trolColor = new Color(0.1f, 0.6f, 0.7f, 1f);
__instance.m_bodyModel.material.SetColor("_SkinColor", trolColor);
}
// Hair
var hairInst = Traverse.Create(__instance).Field("m_hairItemInstance").GetValue<GameObject>();
if (hairInst != null)
foreach (var r in hairInst.GetComponentsInChildren<Renderer>())
if (r.material != null) r.material.SetColor("_SkinColor", HairColor);
// Beard
var beardInst = Traverse.Create(__instance).Field("m_beardItemInstance").GetValue<GameObject>();
if (beardInst != null)
foreach (var r in beardInst.GetComponentsInChildren<Renderer>())
if (r.material != null) r.material.SetColor("_SkinColor", HairColor);
}
}
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
public static class Trol_VisEquipment_UpdateVisuals_Patch
{
public static string GetPlayerRace(VisEquipment visEq)
{
Player player = visEq.GetComponent<Player>();
if (player != null)
{
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
return nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
// No Player.m_localPlayer check — works for menu preview too
if (player.m_customData != null &&
player.m_customData.TryGetValue("Race", out string r))
return r;
}
return "Human";
}
public static void Postfix(VisEquipment __instance)
{
if (!__instance.m_isPlayer) return;
string race = GetPlayerRace(__instance);
if (race == "Trol")
{
// Apply model changes here...
}
else
{
// Revert changes here if needed
}
}
}
[HarmonyPatch(typeof(Player), "Update")]
public static class Trol_PlayerUpdate_Patch
{
// Published by each player onto their own ZDO. The size a Trol should be depends on that
// Trol's progression and True Form, both of which used to be read from whoever happened to
// be looking - so observers sized remote Trols from their own tier, and never saw True
// Form at all because the status effect only exists on the transformed player's client.
public const string StateKey = "RacesOfValheim_State";
public const string TrueFormKey = "RacesOfValheim_TrolTrueForm";
private static float lastRemoteLog = 0f;
private static float ScaleForState(int state)
{
if (state >= 6) return 1.30f;
if (state >= 4) return 1.25f;
return 1.2f;
}
public static void Postfix(Player __instance)
{
if (__instance == null) return;
ZNetView nview = __instance.GetComponent<ZNetView>();
bool zdoReady = nview != null && nview.IsValid();
string race = "Human";
if (zdoReady) race = nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
else if (__instance.m_customData != null && __instance.m_customData.TryGetValue("Race", out string savedRace)) race = savedRace;
bool isLocal = __instance == Player.m_localPlayer;
bool isTrueFormActive;
int state;
if (isLocal)
{
state = BossProgressionManager.GetCurrentProgressionState();
isTrueFormActive = __instance.GetSEMan() != null &&
__instance.GetSEMan().HaveStatusEffect("SE_TrolTrueForm".GetStableHashCode());
// Publish so everyone else can size this character correctly.
if (zdoReady)
{
nview.GetZDO().Set(StateKey, state);
nview.GetZDO().Set(TrueFormKey, isTrueFormActive);
}
}
else
{
state = zdoReady ? nview.GetZDO().GetInt(StateKey, BossProgressionManager.MinState) : BossProgressionManager.MinState;
isTrueFormActive = zdoReady && nview.GetZDO().GetBool(TrueFormKey, false);
}
GameObject visual = Traverse.Create(__instance).Field("m_visual").GetValue<GameObject>();
if (visual == null) return;
float targetScale = ScaleForState(state);
if (isTrueFormActive)
{
targetScale = state >= 7 ? 2.5f : 2.0f;
}
if (!isLocal && Time.time - lastRemoteLog > 2f)
{
lastRemoteLog = Time.time;
Jotunn.Logger.LogInfo(
$"[TrolSync] remote '{__instance.GetPlayerName()}' race='{race}' zdoReady={zdoReady} " +
$"state={state} trueForm={isTrueFormActive} target={targetScale:F2} " +
$"currentScale={visual.transform.localScale.x:F2} rootScale={__instance.transform.localScale.x:F2}");
}
if (race == "Trol")
{
// Local player has a coroutine that smooth-scales during the active transformation,
// so don't fight it here. Otherwise, lerp the scale (for remote players, or to
// maintain base size).
bool isTransforming = isLocal && TrolRace.Instance != null && TrolRace.Instance.isTransforming;
if (!isTransforming)
{
if (Mathf.Abs(visual.transform.localScale.x - targetScale) > 0.005f)
{
float newScale = Mathf.Lerp(visual.transform.localScale.x, targetScale, Time.deltaTime * 5f);
visual.transform.localScale = new Vector3(newScale, newScale, newScale);
}
else if (visual.transform.localScale.x != targetScale)
{
visual.transform.localScale = new Vector3(targetScale, targetScale, targetScale);
}
}
}
else if (!RaceScale.ManagesOwnScale(race) && Mathf.Abs(visual.transform.localScale.x - 1f) > 0.001f)
{
// Races that size themselves are left alone. Without this guard the reset fires on
// every frame for a Furling or a Dvergur too, undoing their scale as fast as they
// set it, because both are applied from a postfix on this same method.
visual.transform.localScale = Vector3.one;
}
}
}
[HarmonyPatch(typeof(Player), "GetMaxCarryWeight")]
public static class Trol_GetMaxCarryWeight_Patch
{
public static void Postfix(Player __instance, ref float __result)
{
if (__instance == null) return;
string race = "Human"; ZNetView nview = __instance.GetComponent<ZNetView>(); if (nview != null && nview.IsValid()) race = nview.GetZDO().GetString("RacesOfValheim_Race", "Human"); else if (__instance.m_customData != null && __instance.m_customData.TryGetValue("Race", out string savedRace)) race = savedRace; if (race == "Trol")
{
// Base carry weight is 300. We add 200 to make it 500.
// If they wear Megingjord, it will become 500 + 150 = 650.
__result += 200f;
int state = BossProgressionManager.GetCurrentProgressionState();
if (state >= 4)
{
__result += 50f; // After Bonemass
}
if (state >= 6)
{
__result += 50f; // After Yagluth
}
}
}
}
[HarmonyPatch(typeof(Player), "GetBodyArmor")]
public static class Trol_GetBodyArmor_Patch
{
public static void Postfix(Player __instance, ref float __result)
{
if (__instance == Player.m_localPlayer && TrolRace.IsTrol())
{
// +3 Armor per progression state (boss defeated)
int state = BossProgressionManager.GetCurrentProgressionState();
__result += 3f * state;
}
}
}
}