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.
This commit is contained in:
kanym 2026-10-14 14:12:50 +02:00
commit 65b488b5dc
4 changed files with 56 additions and 9 deletions

View file

@ -31,6 +31,7 @@ namespace RacesOfValheim
"Skeleton - Visuals",
"Sverm - Dorsal Spines",
"Sverm - Emberform",
"Trol - Appearance",
"War Vulture - Model",
"War Vulture - Interface",
};

View file

@ -59,8 +59,23 @@ namespace RacesOfValheim
// ─── Farming ───────────────────────────────────────────────────────────
public static ConfigEntry<float> UnarmedFarmingDamageBonusPerState;
// ─── Appearance ────────────────────────────────────────────────────────
public static ConfigEntry<Color> SkinColor;
public static ConfigEntry<Color> HairColor;
// The colours every Trol wore before these became settings. Also what a Trol on an older
// build of the mod shows as, since that build publishes no colour of its own.
public static readonly Color DefaultSkinColor = new Color(0.1f, 0.6f, 0.7f, 1f);
public static readonly Color DefaultHairColor = new Color(0.9f, 0.8f, 0.4f, 1f);
public static void Bind(ConfigFile config)
{
const string appearance = "Trol - Appearance";
SkinColor = config.Bind(appearance, "Skin Color", DefaultSkinColor,
new ConfigDescription("Your Trol's skin. Other players see it too. The game multiplies it with the skin texture, so it comes out a little darker than the swatch. For the look of Valheim's own trolls try 7FA8C9."));
HairColor = config.Bind(appearance, "Hair Color", DefaultHairColor,
new ConfigDescription("Your Trol's hair and beard. Other players see it too."));
const string keybinds = "Trol - Keybinds";
ThrowRockKey = config.Bind(keybinds, "Throw Rock", new KeyboardShortcut(KeyCode.G),
new ConfigDescription("Throws a rock (or the log you're holding), tier 3+."));

View file

@ -6,8 +6,23 @@ 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);
// 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)
{
@ -28,25 +43,38 @@ namespace RacesOfValheim
if (race != "Trol") return;
// Apply skin, hair, beard colors here...
// 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)
{
// Peacock blue (deep greenish-blue)
Color trolColor = new Color(0.1f, 0.6f, 0.7f, 1f);
__instance.m_bodyModel.material.SetColor("_SkinColor", trolColor);
__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);
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);
if (r.material != null) r.material.SetColor("_SkinColor", hairColor);
}
}
@ -128,6 +156,7 @@ namespace RacesOfValheim
{
nview.GetZDO().Set(StateKey, state);
nview.GetZDO().Set(TrueFormKey, isTrueFormActive);
if (race == "Trol") TrolSkinColorPatch.Publish(nview.GetZDO());
}
}
else