diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index 92802b4..8a6cdde 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -75,6 +75,7 @@ namespace RacesOfValheim CommandManager.Instance.AddConsoleCommand(new DumpElderCommand()); CommandManager.Instance.AddConsoleCommand(new FenrirPackSpawnCommand()); CommandManager.Instance.AddConsoleCommand(new DumpPotionsCommand()); + CommandManager.Instance.AddConsoleCommand(new ListPrefabsCommand()); } /// @@ -1302,4 +1303,51 @@ namespace RacesOfValheim Console.instance.Print($"[DEBUG] Progression state reset to natural world state: {state}"); } } + + /// + /// Ground truth for "does this prefab actually exist", the same problem that made + /// SummonOnKill's rise VFX ("vfx_skeleton_spawn", "vfx_bloodmagic_aoe") a guess instead of a + /// fact. ZNetScene.GetPrefabNames() is the live registry the running game actually built, so + /// this beats grepping asset bundles offline - those are compressed and unsearchable as plain + /// text, which is exactly why that VFX name had to be confirmed in-game instead. + /// + public class ListPrefabsCommand : ConsoleCommand + { + public override string Name => "listprefabs"; + public override string Help => "[DEBUG] Logs every ZNetScene prefab name containing the given substring (case-insensitive)"; + + public override void Run(string[] args) + { + if (args.Length == 0) + { + Console.instance.Print("Usage: listprefabs "); + return; + } + + if (ZNetScene.instance == null) + { + Console.instance.Print("ZNetScene is not ready yet."); + return; + } + + string filter = args[0]; + System.Collections.Generic.List matches = new System.Collections.Generic.List(); + foreach (string prefabName in ZNetScene.instance.GetPrefabNames()) + { + if (prefabName.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0) + { + matches.Add(prefabName); + } + } + matches.Sort(); + + Console.instance.Print($"[DEBUG] {matches.Count} prefab(s) containing '{filter}':"); + Jotunn.Logger.LogInfo($"[DEBUG] listprefabs '{filter}': {matches.Count} match(es)"); + foreach (string name in matches) + { + Console.instance.Print(" " + name); + Jotunn.Logger.LogInfo(" " + name); + } + } + } } diff --git a/JotunnModStub/RaisedThrall.cs b/JotunnModStub/RaisedThrall.cs index c838ec8..5625274 100644 --- a/JotunnModStub/RaisedThrall.cs +++ b/JotunnModStub/RaisedThrall.cs @@ -32,6 +32,12 @@ namespace RacesOfValheim public const string KeyAnchor = "RoV_draughAnchor"; public const string KeyOrphanAt = "RoV_draughOrphanAt"; + // A hard, station-independent lifespan - unlike the wither above, which only starts once + // an anchor's station is gone. Nothing sets this by default; a thrall raised at a real + // pile never expires on its own. SummonOnKill uses it because its skeletons are never + // bound to a pile in the first place, so the anchor/wither pair has nothing to key off. + public const string KeyExpiresAt = "RoV_thrallExpiresAt"; + // ─── Set on the prefab at registration ─────────────────────────────── /// Groups thralls for follower caps and for a station's per-kind roster. @@ -151,6 +157,10 @@ namespace RacesOfValheim if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return; if (m_character == null || m_character.IsDead()) return; + // A hard cap, checked before anything anchor-based - it has to fire regardless of + // where this thrall is standing or whether it is following, unlike the wither below. + if (TickExpiry()) return; + // Checked before anything else, and NOT skipped while following: a thrall out with its // master still has to wither if the station behind it has fallen. if (TickWither()) return; @@ -161,6 +171,25 @@ namespace RacesOfValheim GoHome(); } + /// Sets a hard lifespan from now, independent of any station anchor. Overwrites + /// any expiry already set. + public void SetExpiry(float seconds) + { + if (m_nview == null || !m_nview.IsValid() || ZNet.instance == null) return; + m_nview.GetZDO().Set(KeyExpiresAt, (ZNet.instance.GetTime() + TimeSpan.FromSeconds(seconds)).Ticks); + } + + /// True once it has killed this one for running out the clock set by SetExpiry. + /// A no-op for any thrall that never had one set (the ZDO key stays at its default 0). + private bool TickExpiry() + { + long expiresAt = m_nview.GetZDO().GetLong(KeyExpiresAt, 0L); + if (expiresAt == 0L || ZNet.instance.GetTime() < new DateTime(expiresAt)) return false; + + Kill(); + return true; + } + /// Starts the clock when the station is gone, and stops the thrall when it runs out. /// Returns true once it has killed this one, so the caller stops bothering with it. private bool TickWither() diff --git a/JotunnModStub/Skeleton/SkeletonBonePile.cs b/JotunnModStub/Skeleton/SkeletonBonePile.cs index 19f22f7..8824612 100644 --- a/JotunnModStub/Skeleton/SkeletonBonePile.cs +++ b/JotunnModStub/Skeleton/SkeletonBonePile.cs @@ -252,6 +252,31 @@ namespace RacesOfValheim if (drops != null) Object.DestroyImmediate(drops); } + // Vanilla skeletons carry no Tameable at all. Without one, Character.RaiseSkill logs + // "is tamed but missing tameable or monster AI script!" every time this thrall lands a + // hit - harmless (it only gates the vanilla creature-leveling system, which none of + // this mod's thralls use), but it was never actually silenced until it showed up in + // testing. Same fix Fenrir already uses for the Ulv and the Fenring: copy a real one + // in from the vanilla Wolf. + if (prefab.GetComponent() == null) + { + GameObject wolf = PrefabManager.Cache.GetPrefab("Wolf"); + Tameable sourceTame = wolf != null ? wolf.GetComponent() : null; + if (sourceTame != null) + { + Tameable copy = prefab.AddComponent(); + foreach (System.Reflection.FieldInfo field in typeof(Tameable).GetFields( + System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) + { + field.SetValue(copy, field.GetValue(sourceTame)); + } + } + else + { + Jotunn.Logger.LogWarning($"[Skeleton] {prefabName}: could not borrow a Tameable from the vanilla Wolf."); + } + } + RaisedThrall thrall = prefab.AddComponent(); thrall.m_kind = kind; thrall.m_commandingRace = RaceId; diff --git a/JotunnModStub/Skeleton/SkeletonPatches.cs b/JotunnModStub/Skeleton/SkeletonPatches.cs index fda8d95..78b1cf2 100644 --- a/JotunnModStub/Skeleton/SkeletonPatches.cs +++ b/JotunnModStub/Skeleton/SkeletonPatches.cs @@ -583,6 +583,18 @@ namespace RacesOfValheim { private static float lastStunTime = 0f; + // SummonOnKill needs to know "did the local Skeleton player damage this creature + // recently", not just "who threw the exact hit that zeroed its health" - a poison + // DOT tick (SE_Poison.UpdateStatusEffect) builds its own HitData every second with no + // attacker set at all, and Character.ApplyDamage overwrites m_lastHit on every one of + // those ticks. Since PoisonAttacks bakes bonus poison onto every Skeleton hit from + // Bonemass on, a large share of kills finish on an attacker-less poison tick rather + // than the swing itself - Character_OnDeath_Patch falls back to this map when that + // happens. Keyed by ZDOID rather than Character, since the character can already be + // gone by the time OnDeath's Prefix runs. + public static readonly System.Collections.Generic.Dictionary RecentSkeletonHits = + new System.Collections.Generic.Dictionary(); + public static bool Prefix(Character __instance, HitData hit) { if (Player.m_localPlayer == null) return true; @@ -601,9 +613,10 @@ namespace RacesOfValheim Player.m_localPlayer.Heal(Player.m_localPlayer.GetMaxHealth(), true); Player.m_localPlayer.GetSEMan().AddStatusEffect("SE_SkeletonResurrectCooldown".GetStableHashCode(), true); - // Spawn VFX - GameObject vfx = PrefabManager.Cache.GetPrefab("vfx_skeleton_spawn") ?? - PrefabManager.Cache.GetPrefab("vfx_bloodmagic_aoe"); + // Spawn VFX - confirmed real via 'listprefabs'; see SummonOnKill below + // for how the previous two guesses here were caught and replaced. + GameObject vfx = PrefabManager.Cache.GetPrefab("fx_summon_skeleton_spawn") ?? + PrefabManager.Cache.GetPrefab("fx_summon_skeleton"); if (vfx != null) { UnityEngine.Object.Instantiate(vfx, Player.m_localPlayer.transform.position, Quaternion.identity); @@ -619,6 +632,8 @@ namespace RacesOfValheim Character attacker = hit.GetAttacker(); if (attacker != null && attacker == Player.m_localPlayer && SkeletonRace.IsSkeleton()) { + RecentSkeletonHits[__instance.GetZDOID()] = Time.time; + // Auto Stun if (rules.AutoStunFirstHit && Time.time - lastStunTime > SkeletonConfig.AutoStunCooldown.Value) { @@ -653,12 +668,49 @@ namespace RacesOfValheim { public static System.Collections.Generic.List activeMinions = new System.Collections.Generic.List(); + // How long a recent Skeleton hit still counts toward a kill once the actual death + // blow turns out to be an attacker-less poison tick. Poison's own TTL (SE_Poison, + // vanilla) can run well past 10s on a heavy stack, so this errs generous - the only + // cost of a false positive here is one extra friendly skeleton, not anything unsafe. + private const float KillAttributionWindow = 20f; + + // How long a SummonOnKill skeleton lasts before it withers on its own. These are never + // bound to a pile, so RaisedThrall's normal anchor/wither pair has nothing to key off - + // this is the hard, station-independent cap added for exactly that case (see + // RaisedThrall.SetExpiry). + private const float SummonLifetimeSeconds = 120f; + public static void Prefix(Character __instance) { if (Player.m_localPlayer == null || !SkeletonRace.IsSkeleton()) return; - + HitData lastHit = HarmonyLib.Traverse.Create(__instance).Field("m_lastHit").GetValue(); - if (lastHit != null && lastHit.GetAttacker() == Player.m_localPlayer) + bool killedByPlayer = lastHit != null && lastHit.GetAttacker() == Player.m_localPlayer; + + ZDOID zdoid = __instance.GetZDOID(); + if (!killedByPlayer && + Character_Damage_Patch.RecentSkeletonHits.TryGetValue(zdoid, out float hitTime) && + Time.time - hitTime <= KillAttributionWindow) + { + // The death blow was very likely an attacker-less poison DOT tick - see the + // comment on RecentSkeletonHits. Credit the kill anyway. + killedByPlayer = true; + } + + // The map only needs to remember live targets; drop this one and anything else + // that's aged out, so it can't grow across a long play session. + Character_Damage_Patch.RecentSkeletonHits.Remove(zdoid); + if (Character_Damage_Patch.RecentSkeletonHits.Count > 0) + { + var stale = new System.Collections.Generic.List(); + foreach (var kv in Character_Damage_Patch.RecentSkeletonHits) + { + if (Time.time - kv.Value > KillAttributionWindow) stale.Add(kv.Key); + } + foreach (var key in stale) Character_Damage_Patch.RecentSkeletonHits.Remove(key); + } + + if (killedByPlayer) { int state = BossProgressionManager.GetCurrentProgressionState(); var rules = SkeletonProgression.GetRules(state); @@ -671,24 +723,85 @@ namespace RacesOfValheim if (activeMinions.Count < rules.MaxSummons) { GameObject minionPrefab = PrefabManager.Cache.GetPrefab(rules.SummonPrefab); - if (minionPrefab != null) + if (minionPrefab == null) + { + Jotunn.Logger.LogWarning($"[Skeleton] SummonOnKill: no '{rules.SummonPrefab}' prefab in the cache; nothing raised from {__instance.m_name}."); + } + else { GameObject minion = UnityEngine.Object.Instantiate(minionPrefab, __instance.transform.position, Quaternion.identity); Character minionChar = minion.GetComponent(); - - if (minionChar != null) + + if (minionChar == null) { - // Make it friendly + Jotunn.Logger.LogWarning($"[Skeleton] SummonOnKill: '{rules.SummonPrefab}' has no Character component; destroying the spawned instance."); + UnityEngine.Object.Destroy(minion); + } + else + { + // Confirmed against the live ZNetScene registry via + // 'listprefabs' - "vfx_skeleton_spawn" and "vfx_bloodmagic_aoe" + // (used below by Auto-Resurrect too) were both guesses that + // never actually existed. fx_summon_skeleton_spawn is the real + // vanilla effect for exactly this moment. + GameObject riseVfx = PrefabManager.Cache.GetPrefab("fx_summon_skeleton_spawn") ?? + PrefabManager.Cache.GetPrefab("fx_summon_skeleton"); + if (riseVfx != null) + { + UnityEngine.Object.Instantiate(riseVfx, minion.transform.position, Quaternion.identity); + } + else + { + Jotunn.Logger.LogWarning("[Skeleton] SummonOnKill: neither 'fx_summon_skeleton_spawn' nor 'fx_summon_skeleton' resolved to a prefab; no rise effect played."); + } + + // The particles above are dressing; the actual "climbing out of + // the ground" motion is the creature's own Animator, played by + // setting this bool - the exact call vanilla's CreatureSpawner + // makes (see Spawner_Skeleton_rise) when m_wakeUpAnimation is + // set. Without it the model is simply active from frame one. + ZSyncAnimation zAnim = minion.GetComponent(); + if (zAnim != null) + { + zAnim.SetBool("wakeup", true); + } + else + { + Jotunn.Logger.LogWarning($"[Skeleton] SummonOnKill: '{rules.SummonPrefab}' has no ZSyncAnimation; cannot play the wakeup animation."); + } + + // Make it friendly - same as RaisedThrall.Bind(), minus the + // station anchor, since this one wasn't raised at a pile. minionChar.m_faction = Character.Faction.Players; BaseAI ai = minion.GetComponent(); - if (ai != null) + if (ai == null) + { + Jotunn.Logger.LogWarning($"[Skeleton] SummonOnKill: '{rules.SummonPrefab}' has no BaseAI; it will stand where it spawned and never follow."); + } + else { MonsterAI monsterAI = minion.GetComponent(); - if (monsterAI != null) + if (monsterAI == null) { + Jotunn.Logger.LogWarning($"[Skeleton] SummonOnKill: '{rules.SummonPrefab}' has a BaseAI but no MonsterAI; cannot set a follow target."); + } + else + { + monsterAI.MakeTame(); monsterAI.SetFollowTarget(Player.m_localPlayer.gameObject); } } + + RaisedThrall raised = minion.GetComponent(); + if (raised == null) + { + Jotunn.Logger.LogWarning($"[Skeleton] SummonOnKill: '{rules.SummonPrefab}' has no RaisedThrall; it will never wither on its own."); + } + else + { + raised.SetExpiry(SummonLifetimeSeconds); + } + activeMinions.Add(minionChar); } } diff --git a/JotunnModStub/Skeleton/SkeletonProgression.cs b/JotunnModStub/Skeleton/SkeletonProgression.cs index 2e6d9d6..b8f4fb7 100644 --- a/JotunnModStub/Skeleton/SkeletonProgression.cs +++ b/JotunnModStub/Skeleton/SkeletonProgression.cs @@ -47,7 +47,12 @@ namespace RacesOfValheim // shorter and wore a 0.85/1.20/0.85 squeeze to compensate. Tied to the model, not a tier. public static readonly UnityEngine.Vector3 FireModelScale = UnityEngine.Vector3.one; - public const string SummonPrefabName = "Skeleton_friendly"; + // Reuses the Bound Bone Pile's own melee thrall (SkeletonBonePile.ThrallMeleeName) rather + // than a standalone prefab, so a Queen/Fader kill raises the same tamed, loot-stripped, + // forced-melee skeleton the pile does - not a separate, unproven asset. "Skeleton_friendly" + // was tried first and never resolved to a real prefab; see the mod's own log for the + // "no prefab in the cache" warning that caught it. + public const string SummonPrefabName = SkeletonBonePile.ThrallMeleeName; // Fader's dead stand taller than everyone else's. Applied to the player's root transform, // which is how this race has always sized itself - so it carries the collider, the camera