RacesOfValheim/JotunnModStub/Trol/TrolPatches.cs
kanym 65b488b5dc feat(trol): skin and hair colour pickers in Trol - Appearance
Two Color entries, Skin Color and Hair Color (hair and beard), defaulting
to the old peacock blue and golden blonde so no Trol changes on update.
Each Trol publishes its choice on its own ZDO and every client paints
that player with it; a Trol on an older build publishes nothing and shows
the defaults. The menu preview, with no ZDO, reads the local config.
The section is kept local by ConfigSync.

README: New in block, Trol features line and a 1.5.10 changelog entry.
2026-10-14 14:14:06 +02:00

245 lines
11 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
public static class TrolSkinColorPatch
{
// Each Trol picks their own colours in Trol - Appearance and publishes them here (see
// Trol_PlayerUpdate_Patch), so everyone sees the colours that player chose. Stored without
// alpha: _SkinColor ignores it.
public const string SkinColorKey = "RacesOfValheim_TrolSkinColor";
public const string HairColorKey = "RacesOfValheim_TrolHairColor";
private static Vector3 ToVec3(Color c) => new Vector3(c.r, c.g, c.b);
private static Color ToColor(Vector3 v) => new Color(v.x, v.y, v.z, 1f);
/// <summary>Puts the local player's chosen colours on their ZDO. Only the owner writes.</summary>
public static void Publish(ZDO zdo)
{
Vector3 skin = ToVec3(TrolConfig.SkinColor.Value);
Vector3 hair = ToVec3(TrolConfig.HairColor.Value);
if (zdo.GetVec3(SkinColorKey, Vector3.negativeInfinity) != skin) zdo.Set(SkinColorKey, skin);
if (zdo.GetVec3(HairColorKey, Vector3.negativeInfinity) != hair) zdo.Set(HairColorKey, hair);
}
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;
// The colours this Trol published. A Trol on an older build publishes none and keeps the
// defaults; with no ZDO at all (the main-menu preview) it is the local player's own
// character, so the local settings are the right answer.
Color skinColor, hairColor;
if (nview != null && nview.IsValid())
{
ZDO zdo = nview.GetZDO();
skinColor = ToColor(zdo.GetVec3(SkinColorKey, ToVec3(TrolConfig.DefaultSkinColor)));
hairColor = ToColor(zdo.GetVec3(HairColorKey, ToVec3(TrolConfig.DefaultHairColor)));
}
else
{
skinColor = TrolConfig.SkinColor.Value;
hairColor = TrolConfig.HairColor.Value;
}
if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null)
{
__instance.m_bodyModel.material.SetColor("_SkinColor", skinColor);
}
// 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 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);
if (race == "Trol") TrolSkinColorPatch.Publish(nview.GetZDO());
}
}
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 ? TrolConfig.TrueFormScaleAdvanced.Value : TrolConfig.TrueFormScaleNormal.Value;
}
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 Dvergar 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 a bonus to make it 500 by default.
// If they wear Megingjord, it will become 500 + 150 = 650.
__result += TrolConfig.CarryWeightBonusBase.Value;
int state = BossProgressionManager.GetCurrentProgressionState();
if (state >= 4)
{
__result += TrolConfig.CarryWeightBonusBonemass.Value;
}
if (state >= 6)
{
__result += TrolConfig.CarryWeightBonusYagluth.Value;
}
}
}
}
[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())
{
int state = BossProgressionManager.GetCurrentProgressionState();
__result += TrolConfig.ArmorBonusPerState.Value * state;
}
}
}
}