RacesOfValheim/JotunnModStub/ZdoVisualSync.cs
kanym 00d4fd6e46 fix(skeleton): stop the fire aura borrowing another mod's VFX by name
The tier-5 fire aura looked for "vfx_smoke", then "vfx_surtling_death".
Neither name exists in Valheim, checked against the game's own asset
manifests, so both lookups came back null and the aura has never been
visible in a plain install.

The dead branch was not harmless. PrefabManager.Cache resolves by asset
name across everything in memory, other mods' asset bundles included, and
its tie-break for a duplicate name ends in "newest wins". A name vanilla
does not use has no vanilla candidate to beat, so it is a free slot.

Magic_Supremacy fills it. The lookup resolved to
'EncounterCreatureRikoMountain_DO/vfx_smoke', a child node of one of its
creatures carrying 'club_trail_riko_DO' on 'Dreanegade/ParticleAlphaEmission'
- a weapon trail, on an emissive shader, sprayed through a 4.5m sphere on
the player. That is the purple smoke around the burning skull users
reported, on every client that could see the skeleton.

The aura now draws nothing on purpose, which costs it nothing it had. Its
damage, radius and tick are untouched. The two identical EnsureFireAuraVFX
copies in SkeletonRace and ZdoVisualSync are replaced by one
SkeletonVisuals.EnsureFireAura, shaped like DraughVisuals.EnsureAura; the
call sites stay, so reinstating the visual needs no change in either file.
The comment on it records the whole chain and the way to build it properly,
through BorrowEffect, the way HeadFlame already does.

Also keeps a resolution log on the model lookup: every magic mod in the
report ships an object called "Skeleton", which is this race's own tier-0
model name. The vanilla creature won during the repro; root=False in that
log is what says it stopped winning.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-25 15:54:21 +02:00

288 lines
12 KiB
C#

using HarmonyLib;
using UnityEngine;
using System.Collections.Generic;
namespace RacesOfValheim
{
[HarmonyPatch(typeof(Player), "Awake")]
public static class Player_Awake_VisualSync_Patch
{
public static void Postfix(Player __instance)
{
if (__instance.gameObject.GetComponent<RacesVisualController>() == null)
{
__instance.gameObject.AddComponent<RacesVisualController>();
}
}
}
[HarmonyPatch(typeof(Player), "OnSpawned")]
public static class Player_OnSpawned_Sync_Patch
{
public static void Postfix(Player __instance)
{
ZNetView nview = __instance.GetComponent<ZNetView>();
if (__instance == Player.m_localPlayer && nview != null && nview.IsValid())
{
if (__instance.m_customData.TryGetValue("Race", out string race))
{
nview.GetZDO().Set("RacesOfValheim_Race", race);
}
}
}
}
public class RacesVisualController : MonoBehaviour
{
private Player m_player;
private string m_lastFenrirForm = "";
private string m_lastSkadiForm = "";
private GameObject m_currentFormInstance = null;
private GameObject m_fireAuraVFX = null;
private GameObject m_draughAuraVFX = null;
private readonly SkeletonVisuals.HeadFlame m_skeletonHeadFlame = new SkeletonVisuals.HeadFlame();
private void Awake()
{
m_player = GetComponent<Player>();
}
private void Update()
{
if (m_player == null) return;
ZNetView nview = m_player.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid()) return;
if (m_player == Player.m_localPlayer) return; // Local player handles their own visuals in Race scripts
if (m_player.IsDead())
{
RevertAllVisuals();
return;
}
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
if (race == "Skeleton")
{
UpdateSkeletonVisuals();
}
else if (race == "Fenrir")
{
UpdateFenrirVisuals();
}
else if (race == "Skadi")
{
UpdateSkadiVisuals();
}
else if (race == "Aswid")
{
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
}
else if (race == DvergarConfig.RaceId)
{
// Scale is handled for every player by the Player.Update postfix; this only keeps
// the hood and the hidden helmet asserted between equipment refreshes.
DvergarVisuals.ApplyHood(m_player);
DvergarVisuals.ApplyEyes(m_player);
}
else
{
RevertAllVisuals();
}
}
private void UpdateAswidVisuals()
{
// Empty for now, relying on VisEquipment patch
}
private void UpdateSkeletonVisuals()
{
ZNetView nview = m_player.GetComponent<ZNetView>();
int state = nview.GetZDO().GetInt("RacesOfValheim_SkeletonState", 0);
var rules = SkeletonProgression.GetRules(state);
// Hide hair and beard
VisEquipment vis = m_player.GetComponent<VisEquipment>();
if (vis != null)
{
// Since Valheim 1.0 these take an item hash, not a prefab name. 0 is the empty slot.
vis.SetHairItem(0);
vis.SetBeardItem(0);
// Hide Helmet
var helmetObj = HarmonyLib.Traverse.Create(vis).Field<GameObject>("m_helmetItemInstance").Value;
SkeletonVisuals.SetEquipmentPieceHidden(helmetObj, true);
// Scale Body Armor
string[] armorFields = { "m_chestItemInstances", "m_legItemInstances", "m_shoulderItemInstances" };
foreach (string field in armorFields)
{
var armorList = HarmonyLib.Traverse.Create(vis).Field<List<GameObject>>(field).Value;
if (armorList != null)
{
foreach (GameObject armor in armorList)
{
if (armor != null && Mathf.Abs(armor.transform.localScale.x - 1.05f) > 0.01f)
{
armor.transform.localScale = new Vector3(1.05f, 1.05f, 1.05f);
}
}
}
}
// Fader used to collapse this bone here too, for the charred twitcher it wore at
// that tier. That model is gone and the skeleton in its place has a jaw of its
// own, so this is only a restore now - the same as the local path. Without it,
// other players would still see a Fader with no mandible.
if (vis.m_bodyModel != null && vis.m_bodyModel.bones != null)
{
foreach (Transform bone in vis.m_bodyModel.bones)
{
if (bone == null || !bone.name.ToLower().Contains("jaw")) continue;
if (bone.localScale == Vector3.zero) bone.localScale = Vector3.one;
break;
}
}
}
// The burning skull, grown locally for whoever we are looking at. The tier is on the
// ZDO, so this client can work out for itself whether they should be alight.
m_skeletonHeadFlame.Apply(m_player, SkeletonConfig.HeadFireVFXEnabled.Value && rules.ModelPrefab == SkeletonProgression.FireModel);
// Apply Scale
Vector3 targetScale = rules.ScaleMultiplier;
if (m_player.transform.localScale != targetScale)
{
m_player.transform.localScale = targetScale;
}
// Deliberately draws nothing - see SkeletonVisuals.EnsureFireAura for why. Called
// anyway so reinstating the visual needs no change here.
SkeletonVisuals.EnsureFireAura(m_player, rules.FireAura, ref m_fireAuraVFX);
}
private void UpdateFenrirVisuals()
{
ZNetView nview = m_player.GetComponent<ZNetView>();
string currentForm = nview.GetZDO().GetString("RacesOfValheim_FenrirForm", "");
if (currentForm != m_lastFenrirForm)
{
// Form changed
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
Transform playerVisual = m_player.transform.Find("Visual");
if (playerVisual != null)
{
if (string.IsNullOrEmpty(currentForm))
{
PlayerVisualHider.Show(playerVisual, "fenrir-remote-revert");
}
else
{
// Same story as the local form: the scale trick this used to hide the body
// is undone every frame by the reset that keeps non-Trol players at normal
// size, so other players saw the man standing in his own wolf too.
PlayerVisualHider.Hide(playerVisual, "fenrir-remote-form");
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(currentForm);
if (prefab != null)
{
Transform visual = prefab.transform.Find("Visual");
if (visual != null)
{
m_currentFormInstance = Instantiate(visual.gameObject, m_player.transform);
m_currentFormInstance.transform.localPosition = Vector3.zero;
m_currentFormInstance.transform.localRotation = Quaternion.identity;
Component animEvent = m_currentFormInstance.GetComponent("AnimEvent");
if (animEvent != null) Destroy(animEvent);
// Clean up colliders on remote visuals
foreach (var col in m_currentFormInstance.GetComponentsInChildren<Collider>())
{
Destroy(col);
}
// Without this the model arrives with nobody writing to its
// Animator and stands in its default pose, sliding wherever the
// player walks. The local form has four behaviours doing this job;
// none of them can run here, because all of them are built around
// Player.m_localPlayer.
m_currentFormInstance.AddComponent<RemoteFenrirAnimator>();
}
}
}
}
m_lastFenrirForm = currentForm;
}
}
private void UpdateSkadiVisuals()
{
ZNetView nview = m_player.GetComponent<ZNetView>();
string currentForm = nview.GetZDO().GetString("RacesOfValheim_SkadiForm", "");
// Flight lives in Character.m_flying, which DrakeFormBehavior only ever sets on the
// flying player's own client. Observers left it false, so their copy of the character
// kept running grounded motion under gravity and sank to the terrain no matter what
// altitude was replicated. Mirror the flag from the ZDO so remote copies stay airborne.
bool flying = nview.GetZDO().GetBool(DrakeFormBehavior.FlyingKey, false);
HarmonyLib.Traverse.Create(m_player).Field("m_flying").SetValue(flying);
// Setting m_flying alone only helps if the remote copy runs Character motion at all.
// Gravity on its rigidbody is what actually drags the body to the terrain while the
// replicated position says otherwise, so switch that off directly for the duration.
Rigidbody body = m_player.GetComponent<Rigidbody>();
if (body != null && body.useGravity == flying)
{
body.useGravity = !flying;
}
}
private void RevertAllVisuals()
{
if (m_player.transform.localScale != Vector3.one) m_player.transform.localScale = Vector3.one;
Transform playerVisual = m_player.transform.Find("Visual");
// Also clears a hidden body left behind if a remote player died mid-transformation.
PlayerVisualHider.Show(playerVisual, "zdosync-revert");
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
SkeletonVisuals.EnsureFireAura(m_player, false, ref m_fireAuraVFX);
DraughVisuals.EnsureAura(m_player, false, ref m_draughAuraVFX);
m_skeletonHeadFlame.Apply(m_player, false);
// 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)
{
Transform headBone = anim.GetBoneTransform(HumanBodyBones.Head);
if (headBone != null)
{
Transform eyes = headBone.Find("AswidEyes");
if (eyes != null) Destroy(eyes.gameObject);
}
}
m_lastFenrirForm = "";
m_lastSkadiForm = "";
}
}
}