warvulture

This commit is contained in:
Ferdinando 2026-08-13 19:13:24 +02:00
commit d79f78e9f0
10 changed files with 901 additions and 90 deletions

View file

@ -1,3 +1,4 @@
using System.Collections.Generic;
using Jotunn.Managers;
using UnityEngine;
@ -96,13 +97,78 @@ namespace RacesOfValheim
return BossProgressionManager.GetCurrentProgressionState();
}
// Fire sources in preference order. The torch is an item every world has; the ground
// torch and the fire pit are the building pieces to fall back on.
/// <summary>
/// Fire sources in preference order. BUILDING PIECES ONLY, and that is the bug fix.
///
/// This list used to start with "Torch", which is the ITEM prefab, and an item in Valheim
/// is a live world object: ItemDrop, a Rigidbody and a ZNetView, all of them on the same
/// node the fire particles are on. That the borrow was reaching that node and not a bare
/// flame was already known here - the strip below used to name "the stick mesh, ItemDrop,
/// durability", and the Rigidbody it had to destroy was the one that made an early version
/// of the clone visibly fall away from the skull.
///
/// What was missed is that the copy was made into a holder that was ALREADY SWITCHED ON,
/// so all of it woke up for a frame before the strip could take any of it off - and a woken
/// ZNetView is not a cosmetic mistake. ZNetView.Awake with no init ZDO calls
/// ZDOMan.CreateNewZDO at the object's position, marks it persistent, stamps it with the
/// prefab hash and registers it with the scene; ZNetView.OnDestroy does none of that in
/// reverse - it only releases asset references. So every rebuild of the flame left the
/// world holding a persistent "there is a Torch here" record that nothing was drawing,
/// saved with everything else, and the next time that zone was built from the save every
/// client spawned the torch. That is the whole of the report: a torch dropped on logging
/// in and out, and a portal handing it to everyone.
///
/// A ground torch is a building piece. It has the same fire (both carry fx_Torch_Basic) and
/// no item behaviour at all, so there is nothing on it to bring across by accident. The
/// inactive holder in Build and the two filters below are the lines of defence behind that,
/// for whatever a future source turns out to carry.
/// </summary>
private static readonly string[] FlameSourcePrefabs =
{
"Torch", "piece_groundtorch_wood", "piece_groundtorch", "fire_pit"
"piece_groundtorch_wood", "piece_groundtorch", "fire_pit"
};
/// <summary>
/// Particle systems that are not fire. Matched on the object name, which is how these are
/// named in the prefabs that carry them - an item's pickup glitter above all, which is a
/// ParticleSystem like any other and was worn on the skull along with the flame.
/// </summary>
private static readonly string[] NotFire =
{
"sparkle", "sparcle", "glitter", "pickup", "shine", "twinkle"
};
private static bool IsFire(GameObject candidate)
{
string name = candidate.name.ToLower();
foreach (string word in NotFire)
{
if (name.Contains(word)) return false;
}
return true;
}
// Said once per particle name per session, with everything the borrowed node carries that
// is not a particle. The point is that the next time something rides along, the log names
// it - this bug cost a bug report because nothing ever said "that flame is an item".
private static readonly HashSet<string> loggedParticles = new HashSet<string>();
private static void LogClonedOnce(GameObject source)
{
if (!loggedParticles.Add(source.name)) return;
List<string> passengers = new List<string>();
foreach (Component comp in source.GetComponentsInChildren<Component>(true))
{
if (comp == null || IsPureVfx(comp, true)) continue;
passengers.Add(comp.GetType().Name);
}
Jotunn.Logger.LogInfo(
$"[Skeleton] Head flame: cloned '{source.name}'" +
(passengers.Count == 0 ? "." : ", stripped " + string.Join(", ", passengers.ToArray()) + "."));
}
private static GameObject cachedFlameSource;
private static bool flameSourceResolved;
@ -119,7 +185,13 @@ namespace RacesOfValheim
{
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(name);
if (prefab == null) continue;
if (prefab.GetComponentsInChildren<ParticleSystem>(true).Length == 0) continue;
bool hasFire = false;
foreach (ParticleSystem ps in prefab.GetComponentsInChildren<ParticleSystem>(true))
{
if (IsFire(ps.gameObject)) { hasFire = true; break; }
}
if (!hasFire) continue;
Jotunn.Logger.LogInfo($"[Skeleton] Head flame source resolved to '{name}'.");
cachedFlameSource = prefab;
@ -132,6 +204,108 @@ namespace RacesOfValheim
return null;
}
/// <summary>
/// Strips a borrowed subtree down to what a pure VFX needs, and nothing else.
///
/// Allowlist, not a blocklist: keep the particle systems, their renderers and the lights
/// (and the loose meshes, for the eye glow), destroy every other component whatever it
/// turns out to be. A blocklist of "known bad" types (MonoBehaviour, Collider) missed
/// Rigidbody - not a MonoBehaviour in Unity - and a fire node carrying one meant physics
/// kept acting on the clone independently of the holder being repositioned every frame.
///
/// DestroyImmediate rather than Destroy, because the caller is about to switch the holder
/// on and Destroy does not take effect until the end of the frame - by which time every
/// script it was supposed to remove would have had its Awake.
///
/// Repeated passes, because Unity refuses to destroy a component another one still
/// requires ("Can't remove Humanoid because CharacterDrop depends on it"): the dependent
/// goes on one pass and the component it held down goes on the next. Behaviours first
/// within each pass, for the same reason.
/// </summary>
public static void StripToVfx(GameObject root, bool keepMeshes, string label)
{
if (root == null) return;
for (int pass = 0; pass < 4; pass++)
{
bool anyLeft = false;
foreach (MonoBehaviour behaviour in root.GetComponentsInChildren<MonoBehaviour>(true))
{
if (behaviour == null) continue;
Object.DestroyImmediate(behaviour);
}
foreach (Component comp in root.GetComponentsInChildren<Component>(true))
{
if (comp == null || IsPureVfx(comp, keepMeshes)) continue;
Object.DestroyImmediate(comp);
anyLeft = true;
}
if (!anyLeft) return;
}
// Four passes and something is still holding on. Say what it is: a survivor here is the
// exact shape of bug this whole class exists to prevent, and it is about to be woken.
foreach (Component comp in root.GetComponentsInChildren<Component>(true))
{
if (comp == null || IsPureVfx(comp, keepMeshes)) continue;
Jotunn.Logger.LogWarning(
$"[{label}] '{comp.GetType().Name}' on '{comp.gameObject.name}' could not be stripped " +
"off the borrowed effect; it will run on the player.");
}
}
private static bool IsPureVfx(Component comp, bool keepMeshes)
{
if (comp is Transform || comp is ParticleSystem || comp is ParticleSystemRenderer || comp is Light)
{
return true;
}
return keepMeshes && (comp is MeshRenderer || comp is MeshFilter);
}
/// <summary>
/// Clones one node of a vanilla prefab as pure VFX and hangs it under <paramref name="parent"/>.
///
/// The temporary holder is created INACTIVE and the copy is made inside it. That single
/// line is the point of this method: Unity does not run Awake on an object that is not
/// active in its hierarchy, so ItemDrop, ZNetView, Rigidbody and every script on the
/// borrowed node are destroyed before any of them ever exists as far as the game is
/// concerned. Stripping AFTER the copy is already live - which is what the head flame and
/// the eye glow both used to do - is a frame too late, and one frame is all a ZNetView
/// needs to write a persistent object into the save. Same lesson, same fix, as ModelBorrow.
///
/// The copy is handed back parented to <paramref name="parent"/> and switched on, with its
/// pose left to the caller.
/// </summary>
public static GameObject BorrowEffect(GameObject source, Transform parent, string cloneName,
bool keepMeshes, string label)
{
if (source == null || parent == null) return null;
GameObject holder = new GameObject("BorrowedEffectHolder");
holder.SetActive(false); // BEFORE the Instantiate below.
GameObject copy = Object.Instantiate(source, holder.transform);
copy.name = cloneName;
StripToVfx(copy, keepMeshes, label);
copy.transform.SetParent(parent, false);
Object.DestroyImmediate(holder);
// A prefab can leave its own effects switched off for a script to turn on later, and
// that script is exactly what was just destroyed.
copy.SetActive(true);
foreach (Light light in copy.GetComponentsInChildren<Light>(true)) light.enabled = true;
foreach (Renderer renderer in copy.GetComponentsInChildren<Renderer>(true)) renderer.enabled = true;
return copy;
}
/// <summary>
/// One player's burning skull. Cosmetic only - it deals nothing and blocks nothing.
///
@ -191,13 +365,22 @@ namespace RacesOfValheim
// Real fire is cloned rather than built by hand. The first attempt at this made a
// particle system from scratch on the smoke material, and a hand-rolled gradient
// over a smoke sprite reads as a wisp, not a burning skull.
//
// Every clone goes through BorrowEffect, which builds it inactive and strips it
// before it can wake. Nothing from the source prefab - a script, a Rigidbody, a
// ZNetView - ever gets a frame of life on the player.
foreach (ParticleSystem ps in source.GetComponentsInChildren<ParticleSystem>(true))
{
GameObject copy = Object.Instantiate(ps.gameObject, m_instance.transform);
copy.name = ps.gameObject.name;
if (!IsFire(ps.gameObject)) continue;
GameObject copy = BorrowEffect(ps.gameObject, m_instance.transform,
ps.gameObject.name, false, "Skeleton");
if (copy == null) continue;
LogClonedOnce(ps.gameObject);
copy.transform.localPosition = Vector3.zero;
copy.transform.localRotation = Quaternion.identity;
copy.SetActive(true);
// So the holder's scale actually enlarges the fire rather than just spreading
// out where the particles spawn.
@ -213,22 +396,6 @@ namespace RacesOfValheim
light.intensity *= 0.6f;
}
// Allowlist, not a blocklist: keep only what a pure VFX actually needs (the
// particle systems, their renderers, and the light tweaked above) and destroy
// every other component, whatever it turns out to be. A blocklist of "known bad"
// types (MonoBehaviour, Collider) missed Rigidbody - not a MonoBehaviour in Unity,
// so the old loop never touched it - and a torch's fire-particle node carrying one
// meant physics kept acting on the clone independently of m_instance being
// repositioned onto the head every frame: it visibly fell away while the empty
// parent stayed correctly tracked above it. Anything that made the source a torch
// rather than a bare flame - the stick mesh, ItemDrop, durability, all of it -
// goes the same way.
foreach (Component comp in m_instance.GetComponentsInChildren<Component>(true))
{
if (comp is Transform || comp is ParticleSystem || comp is ParticleSystemRenderer || comp is Light) continue;
Object.Destroy(comp);
}
return true;
}
}