Merge pull request 'feat(trenta): grow a crown of branches, and fix(draugh): let everyone see the rot' (#12) from trenta-crown-and-draugh-vfx into main

Reviewed-on: #12
This commit is contained in:
kanym 2026-08-05 13:52:56 +02:00
commit 9b48223932
7 changed files with 345 additions and 70 deletions

View file

@ -112,71 +112,14 @@ namespace RacesOfValheim
}
}
/// <summary>
/// The local player's half of the aura. The build itself moved to DraughVisuals so that
/// RacesVisualController can grow the same one on every remote Draugh - this script only
/// ever sees the player at the keyboard, which is why nobody else could see the rot.
/// </summary>
private void EnsureVFX(bool enable)
{
if (enable && Player.m_localPlayer != null)
{
if (draughVFX == null)
{
GameObject draugrPrefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>("Draugr");
if (draugrPrefab != null)
{
draughVFX = new GameObject("DraughAuraVFX");
draughVFX.transform.SetParent(Player.m_localPlayer.transform, false);
// Centro del giocatore invece che il pivot (piedi)
draughVFX.transform.localPosition = new Vector3(0, 1f, 0);
// Find all ParticleSystems in Draugr prefab
ParticleSystem[] pss = draugrPrefab.GetComponentsInChildren<ParticleSystem>(true);
foreach (ParticleSystem ps in pss)
{
// We only want looping ambient effects (flies, smoke)
if (ps.main.loop)
{
GameObject psCopy = Instantiate(ps.gameObject, draughVFX.transform);
psCopy.name = ps.gameObject.name;
psCopy.SetActive(true);
// Entrambi (fumo e mosche) partono esattamente dal centro
psCopy.transform.localPosition = Vector3.zero;
psCopy.transform.localRotation = ps.transform.localRotation;
string psName = psCopy.name.ToLower();
if (psName.Contains("flies"))
{
// Flies effect: leave as is
}
else if (psName.Contains("drip") || psName.Contains("drop") || psName.Contains("rain") || psName.Contains("splash"))
{
// Effetto 3 (pioggia): disattivato
psCopy.SetActive(false);
}
else
{
// Smoke effect: scale reduced to 1.5f (-40% of 2.5f)
psCopy.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
var main = psCopy.GetComponent<ParticleSystem>().main;
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
}
}
}
}
}
if (draughVFX != null && !draughVFX.activeSelf)
{
draughVFX.SetActive(true);
}
}
else
{
if (draughVFX != null && draughVFX.activeSelf)
{
draughVFX.SetActive(false);
}
}
DraughVisuals.EnsureAura(Player.m_localPlayer, enable, ref draughVFX);
}
public System.Collections.IEnumerator UndyingWillRoutine(Player p)

View file

@ -0,0 +1,121 @@
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The Draugh's rot: the drifting smoke and the cloud of flies a Draugr carries, copied off the
/// Draugr prefab and hung on a player.
///
/// This used to live inside DraughRace, which is a single MonoBehaviour that only ever looks at
/// Player.m_localPlayer - so the aura existed on exactly one client, on exactly one player, and
/// everyone else in a multiplayer session saw a perfectly clean corpse walking around. The build
/// is here instead, taking a Player, so the local player and every remote one get the same
/// treatment from their own client.
///
/// Nothing here is networked, and it does not need to be: every client can tell a Draugh from
/// the race on the ZDO and grow its own copy of the particles.
/// </summary>
public static class DraughVisuals
{
public const string RaceId = "Draugh";
private const string AuraName = "DraughAuraVFX";
// Chest height rather than the pivot, which sits at the feet.
private static readonly Vector3 AuraLocalPosition = new Vector3(0f, 1f, 0f);
// The Draugr's smoke is authored for a creature that stands taller than a player.
private static readonly Vector3 SmokeScale = new Vector3(1.5f, 1.5f, 1.5f);
/// <summary>
/// Reads the race off the ZDO so remote players are recognised, falling back to
/// m_customData for the main-menu preview which has no ZNetView.
/// </summary>
public static string GetPlayerRace(Player player)
{
if (player == null) return "Human";
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "");
if (!string.IsNullOrEmpty(race)) return race;
}
if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string custom))
{
return custom;
}
return "Human";
}
public static bool IsDraughPlayer(Player player) => GetPlayerRace(player) == RaceId;
/// <summary>
/// Puts the aura on the player, or takes it away. Safe to call every frame: the particles
/// are built once and then only toggled. The caller owns the instance - it is per-player,
/// and a static would give the second Draugh in view the first one's aura.
/// </summary>
public static void EnsureAura(Player player, bool enable, ref GameObject aura)
{
if (enable)
{
if (player == null) return;
if (aura == null) aura = BuildAura(player);
if (aura != null && !aura.activeSelf) aura.SetActive(true);
}
else if (aura != null && aura.activeSelf)
{
aura.SetActive(false);
}
}
/// <summary>
/// Clones the Draugr's own looping particle systems onto the player. Looping is the filter
/// that separates the ambient rot from one-shot hit and death effects.
/// </summary>
private static GameObject BuildAura(Player player)
{
GameObject draugrPrefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>("Draugr");
if (draugrPrefab == null) return null;
GameObject aura = new GameObject(AuraName);
aura.transform.SetParent(player.transform, false);
aura.transform.localPosition = AuraLocalPosition;
foreach (ParticleSystem ps in draugrPrefab.GetComponentsInChildren<ParticleSystem>(true))
{
if (!ps.main.loop) continue;
GameObject copy = Object.Instantiate(ps.gameObject, aura.transform);
copy.name = ps.gameObject.name;
copy.SetActive(true);
// Both the smoke and the flies start from the same point, the player's centre.
copy.transform.localPosition = Vector3.zero;
copy.transform.localRotation = ps.transform.localRotation;
string name = copy.name.ToLower();
if (name.Contains("flies"))
{
// The flies are the whole point - left exactly as the Draugr wears them.
}
else if (name.Contains("drip") || name.Contains("drop") || name.Contains("rain") || name.Contains("splash"))
{
// Dripping water reads as rain coming off the player. Not wanted.
copy.SetActive(false);
}
else
{
copy.transform.localScale = SmokeScale;
ParticleSystem.MainModule main = copy.GetComponent<ParticleSystem>().main;
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
}
}
return aura;
}
}
}

View file

@ -52,6 +52,27 @@ namespace RacesOfValheim
public static readonly Color HairColor = new Color(0.06f, 0.28f, 0.08f, 1f);
// ─── Crown of branches ───────────────────────────────────────────────
// A greydwarf shaman wears a crown of horns and dead wood, and it is the one piece of
// vanilla art that says "this one grew rather than was born". The whole trophy is hung on
// the player's head bone and then sunk into the skull until only the branches clear it -
// the same trick the Furling uses to wear a goblin head, aimed at a different result.
public const string CrownTrophyPrefabName = "TrophyGreydwarfShaman";
// Bone the crown is hung from on the player's own rig.
public const string PlayerHeadBoneName = "Head";
// These three are the ones to eyeball in game; everything else about the crown is fixed.
// Valheim's player rig is authored at roughly 100x, which is why bone-local numbers are
// this small - the Furling's goblin head sits at 0.0058 for comparison.
//
// Y is up in the head bone's frame, so lowering it sinks the trophy deeper and shows less
// of the crown. Too high and a shrunken greydwarf face floats out of the player's own; too
// low and the branches disappear into the scalp.
public static readonly Vector3 CrownOffsetPosition = new Vector3(0.0005f, 0.0008f, 0f);
public static readonly Vector3 CrownOffsetRotation = new Vector3(-90f, 0f, 90f);
public static readonly float CrownScale = 0.0070f;
// ─── Progression tables ──────────────────────────────────────────────
// Indexed by progression state; index 0 is unused so the state number indexes directly.
// 1 Base, 2 Eikthyr, 3 The Elder, 4 Bonemass, 5 Moder, 6 Yagluth, 7 The Queen, 8 Fader.

View file

@ -62,6 +62,25 @@ namespace RacesOfValheim
}
}
/// <summary>
/// Hangs the crown of branches on, for every player local and remote, whenever Valheim
/// rebuilds their visuals - which is also the moment a rebuilt body model would have
/// thrown the old crown away with the old head bone.
/// </summary>
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
public static class Trenta_VisEquipment_UpdateVisuals_Patch
{
public static void Postfix(VisEquipment __instance)
{
if (__instance == null || !__instance.m_isPlayer) return;
Player player = __instance.GetComponent<Player>();
if (player == null) return;
TrentaVisuals.ApplyCrown(player);
}
}
// Diet (vegan restriction, digestion doubling, awakened seeds) lives in
// TrentaFoodPatches.cs, the way DraughFoodPatches.cs holds the Draugh's equivalent.

View file

@ -1,3 +1,4 @@
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
@ -44,10 +45,164 @@ namespace RacesOfValheim
// Body scale goes on the player's "Visual" child, not the root transform - scaling the
// root drags the collider and the camera pivot with it. RaceScale owns the list of races
// that resize, so the Trenta has to be added there rather than fighting it per-frame.
//
// Extra meshes (bark, foliage) are hung off a bone of the player's rig and hidden by
// collapsing every unwanted bone to zero scale - a creature prefab's mesh usually ships
// with Read/Write disabled and cannot be carved at runtime. DvergarVisuals.ApplyHood is
// the worked example.
// ─── Crown of branches ───────────────────────────────────────────────
private const string CrownName = "TrentaCrown";
// Resolved once per session, failure included, so a missing prefab cannot turn into a
// per-frame log and the prefab lookup does not run again for every player in view.
private static GameObject cachedCrownVisual;
private static bool crownResolved;
/// <summary>
/// Keeps the crown in step with who the player is. Safe to call every frame: it checks
/// whether the crown is already there and does nothing when it is.
/// </summary>
public static void ApplyCrown(Player player)
{
VisEquipment visEq = player != null ? player.GetComponent<VisEquipment>() : null;
if (visEq == null) return;
if (IsTrentaPlayer(player) && !player.IsDead())
{
EnsureCrown(visEq);
}
else
{
RemoveCrown(visEq);
}
}
/// <summary>
/// Hangs a greydwarf shaman trophy on the player's head, sunk far enough into the skull
/// that only the horns and branches clear it.
///
/// A trophy is a plain static mesh, so unlike the Dvergar's hood there is no rig to
/// collapse and no submesh to isolate - the whole thing is instantiated and then buried,
/// and the player's own head does the hiding. Which means the three numbers in
/// TrentaConfig are the entire look: get the depth wrong and a tiny greydwarf face floats
/// in front of the player's.
/// </summary>
private static void EnsureCrown(VisEquipment visEq)
{
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
Transform playerHead = FindBoneByName(visEq.m_bodyModel.bones, TrentaConfig.PlayerHeadBoneName);
if (playerHead == null) return;
if (playerHead.Find(CrownName) != null) return; // already wearing it
GameObject source = ResolveCrownVisual();
if (source == null) return; // already logged once by the resolver
// Instantiated inactive so the trophy's own Awake never runs - it would register a
// pickup, a hover prompt and a rigidbody on the player's head.
bool wasActive = source.activeSelf;
source.SetActive(false);
GameObject crown = Object.Instantiate(source, playerHead, false);
source.SetActive(wasActive);
crown.name = CrownName;
StripComponents(crown);
crown.SetActive(true);
crown.transform.localPosition = TrentaConfig.CrownOffsetPosition;
crown.transform.localRotation = Quaternion.Euler(TrentaConfig.CrownOffsetRotation);
crown.transform.localScale = Vector3.one * TrentaConfig.CrownScale;
ApplyDitherShader(crown);
}
private static void RemoveCrown(VisEquipment visEq)
{
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
Transform playerHead = FindBoneByName(visEq.m_bodyModel.bones, TrentaConfig.PlayerHeadBoneName);
Transform crown = playerHead != null ? playerHead.Find(CrownName) : null;
if (crown == null) return;
// Destroy is deferred to the end of the frame, so rename first: without that, every
// call until it actually goes away finds it again and queues another destroy.
crown.name = CrownName + "_Removed";
Object.Destroy(crown.gameObject);
}
private static GameObject ResolveCrownVisual()
{
if (crownResolved) return cachedCrownVisual;
crownResolved = true;
ItemDrop trophy = PrefabManager.Cache.GetPrefab<ItemDrop>(TrentaConfig.CrownTrophyPrefabName);
if (trophy == null)
{
Jotunn.Logger.LogWarning(
$"[Trenta] Prefab '{TrentaConfig.CrownTrophyPrefabName}' not found; no crown.");
return null;
}
cachedCrownVisual = trophy.gameObject;
Jotunn.Logger.LogInfo($"[Trenta] Crown source resolved to '{TrentaConfig.CrownTrophyPrefabName}'.");
return cachedCrownVisual;
}
/// <summary>
/// Strips everything that would make a dropped item behave like one: the ItemDrop itself,
/// hover prompts and physics, plus the LODGroup, which is not a MonoBehaviour and would
/// otherwise hide the mesh at the distances it was authored for as a floor pickup.
/// </summary>
private static void StripComponents(GameObject obj)
{
foreach (MonoBehaviour comp in obj.GetComponentsInChildren<MonoBehaviour>(true))
{
Object.DestroyImmediate(comp);
}
foreach (LODGroup lod in obj.GetComponentsInChildren<LODGroup>(true))
{
Object.DestroyImmediate(lod);
}
foreach (Rigidbody body in obj.GetComponentsInChildren<Rigidbody>(true))
{
Object.DestroyImmediate(body);
}
foreach (Collider col in obj.GetComponentsInChildren<Collider>(true))
{
Object.DestroyImmediate(col);
}
}
/// <summary>
/// Without the creature shader the crown ignores the camera's dither fade and fills the
/// screen in first person; its own textures are kept.
/// </summary>
private static void ApplyDitherShader(GameObject obj)
{
Shader dither = Shader.Find("Custom/Creature");
if (dither == null) return;
foreach (Renderer renderer in obj.GetComponentsInChildren<Renderer>(true))
{
if (renderer.material != null) renderer.material.shader = dither;
}
}
private static Transform FindBoneByName(Transform[] bones, string targetBoneName)
{
if (bones == null || string.IsNullOrEmpty(targetBoneName)) return null;
foreach (Transform bone in bones)
{
if (bone != null && bone.name == targetBoneName) return bone;
}
string target = targetBoneName.ToLower();
foreach (Transform bone in bones)
{
if (bone == null) continue;
string name = bone.name.ToLower();
if (name == target || name.Contains(target) || target.Contains(name)) return bone;
}
return null;
}
}
}

View file

@ -39,6 +39,7 @@ namespace RacesOfValheim
private string m_lastSkadiForm = "";
private GameObject m_currentFormInstance = null;
private GameObject m_fireAuraVFX = null;
private GameObject m_draughAuraVFX = null;
private float m_lastFlightLog = 0f;
private void Awake()
@ -77,6 +78,18 @@ namespace RacesOfValheim
{
UpdateAswidVisuals();
}
else if (race == DraughVisuals.RaceId)
{
// The smoke and flies a Draugh trails. DraughRace only ever dresses the player at
// the keyboard, so without this every other client saw an ordinary, unrotted one.
DraughVisuals.EnsureAura(m_player, true, ref m_draughAuraVFX);
}
else if (race == TrentaConfig.RaceId)
{
// Keeps the crown of branches on between equipment refreshes, the same way the
// Dvergar's hood is kept on below.
TrentaVisuals.ApplyCrown(m_player);
}
else if (race == "Trol")
{
// Managed externally in TrolPatches.cs
@ -255,6 +268,9 @@ namespace RacesOfValheim
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
EnsureFireAuraVFX(false);
DraughVisuals.EnsureAura(m_player, false, ref m_draughAuraVFX);
// Takes the crown off a Trenta who has died or changed race; a no-op for everyone else.
TrentaVisuals.ApplyCrown(m_player);
Animator anim = m_player.GetComponentInChildren<Animator>();
if (anim != null)

View file

@ -53,7 +53,7 @@ This mod currently introduces 8 custom races:
- **Runes**: In the late game, spend eitr to read the stone around you or to load a single devastating blow.
### 9. Trenta
- **Tree-Born**: Kin to root and leaf. Your skin is green, you eat what grows, and every meal lasts twice as long.
- **Tree-Born**: Kin to root and leaf. Your skin is green, a crown of branches grows from your head, you eat what grows, and every meal lasts twice as long.
- **Photosynthesis**: Stand still in daylight, out from under a roof, and you put down roots - healing steadily and standing up Rested.
- **Awakened Seeds**: Once Eikthyr falls, every seed, cone and acorn wakes up. Eat one where you stand and it becomes the best dish its biome has to offer.
- **The Forest Does Not See You**: Greylings, greydwarves, fulings, trolls, leeches and deathsquitos never treat you as an enemy.
@ -143,7 +143,7 @@ seconds, and their cooldown, as a Moder trophy icon beside the minimap.
- **Fader**: 35% Lifesteal, Max 5 Summons, +275 Max Health.
### Trenta Progression
- **Base (No Bosses)**: Tree-Born. Green skin, a diet of what grows, and every food lasts twice as long. Photosynthesis - stand motionless in daylight and out from under a roof for 10 seconds to put down roots, healing 10% of max health a second for 8 seconds and standing up Rested for 5 minutes (+1 minute per boss already defeated). Greylings, greydwarves, fulings, trolls, leeches and deathsquitos never treat you as an enemy.
- **Base (No Bosses)**: Tree-Born. Green skin, a crown of branches on your head, a diet of what grows, and every food lasts twice as long. Photosynthesis - stand motionless in daylight and out from under a roof for 10 seconds to put down roots, healing 10% of max health a second for 8 seconds and standing up Rested for 5 minutes (+1 minute per boss already defeated). Greylings, greydwarves, fulings, trolls, leeches and deathsquitos never treat you as an enemy.
- **Eikthyr**: Awakened Seeds - every seed, cone, acorn and self-seeding mushroom wakes up. Eating one on the spot consumes it and grants the strongest health or eitr dish of the biome it comes from.
- **The Elder**: Purple Rain ('G' Key) - a spray of toxic pollen over 8m that heals everything that does not fight you for 40 and poisons everything that does for 40. 20 second cooldown.
- **Bonemass**: Swimming costs no stamina, and neither Poison nor Wet can touch you any more.