RacesOfValheim/JotunnModStub/Trol/TrolGammelLog.cs
kanym 3485665d4e fix(trol): the True Form log is borrowed from TrollFrost, the real Gammeltroll
"Gammeltroll" is only the developers' name: the prefab is TrollFrost,
whose clips fire the sfx_gameltroll_* sounds and whose Attack TreeThrow
throws a treebundle (dumpanim TrollFrost). No prefab is called
Gammeltroll, so since v1.4.16 every True Form throw fell back to the
physical log. The loose "gammel" substring fallback is gone too: it
matched nothing in vanilla and could match another mod's prefab.
2026-09-30 13:22:48 +02:00

128 lines
6.6 KiB
C#

using System.Collections.Generic;
using Jotunn.Entities;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The thrown log in True Form borrows ONE projectile from the Gammeltroll (Valheim 1.0), the
/// one it fires a whole rose of. The prefab names live in compressed asset bundles, so nothing
/// here is hard-coded past the monster itself: the projectile is read off the Gammeltroll's own
/// weapons at runtime, the same way Attack.Start would find it. Two shapes are handled:
/// - the rose is m_projectiles > 1 on the attack: the attack's projectile already IS one log;
/// - the rose is a single projectile that splits on hit (m_spawnOnHit with m_spawnCount > 1):
/// then the log is the child, and the parent would bring the whole rose along with it.
/// If none of that resolves, GetProjectile returns null and TrolRace falls back to the old
/// physical log. "dumpgammel" in the console logs what was actually found.
///
/// "Gammeltroll" is only the developers' name for it. The prefab is TrollFrost, the Deep North
/// troll: its clips fire the sfx_gameltroll_* sounds, and its 'Attack TreeThrow' shows a
/// 'treebundle' at 2.03s and throws it at 3.54s (dumpanim TrollFrost). No prefab is called
/// Gammeltroll, so from v1.4.16 until this was corrected the lookup found nothing and every
/// True Form throw fell back to the physical log.
/// </summary>
public static class TrolGammelLog
{
private const string GammeltrollPrefab = "TrollFrost";
private static GameObject cachedProjectile;
private static float cachedVelocity;
private static bool warned;
public static GameObject GetProjectile(out float velocity)
{
if (cachedProjectile == null && ZNetScene.instance != null)
{
Resolve(out cachedProjectile, out cachedVelocity, null);
if (cachedProjectile == null && !warned)
{
warned = true;
Jotunn.Logger.LogWarning("[Trol] Gammeltroll log projectile not found - falling back to the physical log throw. Run 'dumpgammel' to see why.");
}
}
velocity = cachedVelocity > 0f ? cachedVelocity : TrolConfig.ThrowLogVelocity.Value;
return cachedProjectile;
}
/// <summary>log is null for the silent path; the dump command passes a sink.</summary>
internal static void Resolve(out GameObject projectile, out float velocity, System.Action<string> log)
{
projectile = null;
velocity = 0f;
GameObject troll = FindGammeltroll();
if (troll == null) { log?.Invoke($"Gammeltroll prefab '{GammeltrollPrefab}' not found in ZNetScene."); return; }
log?.Invoke($"Monster prefab: '{troll.name}'");
Humanoid humanoid = troll.GetComponent<Humanoid>();
if (humanoid == null) { log?.Invoke(" no Humanoid on it."); return; }
// Prefer the attack that fires several projectiles at once (the rose); any other
// projectile attack is only a fallback.
Attack best = null;
foreach (GameObject item in ItemsOf(humanoid))
{
ItemDrop drop = item != null ? item.GetComponent<ItemDrop>() : null;
if (drop == null) continue;
var shared = drop.m_itemData.m_shared;
foreach (Attack attack in new[] { shared.m_attack, shared.m_secondaryAttack })
{
if (attack == null) continue;
log?.Invoke($" item '{item.name}' anim='{attack.m_attackAnimation}' projectile='{(attack.m_attackProjectile != null ? attack.m_attackProjectile.name : "-")}' count={attack.m_projectiles} bursts={attack.m_projectileBursts} vel={attack.m_projectileVel} acc={attack.m_projectileAccuracy}");
if (attack.m_attackProjectile == null) continue;
if (best == null || (attack.m_projectiles > 1 && best.m_projectiles <= 1)) best = attack;
}
}
if (best == null) { log?.Invoke(" no attack with a projectile."); return; }
projectile = best.m_attackProjectile;
velocity = best.m_projectileVel;
Projectile p = projectile.GetComponent<Projectile>();
if (p != null)
{
log?.Invoke($" chosen '{projectile.name}': spawnOnHit='{(p.m_spawnOnHit != null ? p.m_spawnOnHit.name : "-")}' spawnCount={p.m_spawnCount} randomSpawns={(p.m_randomSpawnOnHit != null ? p.m_randomSpawnOnHit.Count : 0)} damage={p.m_damage.GetTotalDamage()}");
if (p.m_spawnOnHit != null && p.m_spawnCount > 1 && p.m_spawnOnHit.GetComponent<Projectile>() != null)
{
projectile = p.m_spawnOnHit;
log?.Invoke($" it splits on hit - using the child '{projectile.name}' instead.");
}
}
log?.Invoke($"=> thrown log: '{projectile.name}', velocity {velocity}");
}
private static GameObject FindGammeltroll()
{
// Exact name only. The old fallback took the first Humanoid with "gammel" anywhere in
// its name, which in vanilla matches nothing and in a modded game matches whatever
// another mod happens to call that - see the prefab-name audit.
return ZNetScene.instance.GetPrefab(GammeltrollPrefab);
}
private static IEnumerable<GameObject> ItemsOf(Humanoid h)
{
if (h.m_defaultItems != null) foreach (var go in h.m_defaultItems) yield return go;
if (h.m_randomWeapon != null) foreach (var go in h.m_randomWeapon) yield return go;
if (h.m_randomSets != null)
foreach (var set in h.m_randomSets)
if (set.m_items != null) foreach (var go in set.m_items) yield return go;
if (h.m_randomItems != null) foreach (var ri in h.m_randomItems) yield return ri.m_prefab;
}
}
public class DumpGammelCommand : ConsoleCommand
{
public override string Name => "dumpgammel";
public override string Help => "[DEBUG] Logs the Gammeltroll's projectile attacks and which one the Trol throws as a log";
public override void Run(string[] args)
{
if (ZNetScene.instance == null) { Console.instance.Print("Load a world first."); return; }
TrolGammelLog.Resolve(out _, out _, line =>
{
Jotunn.Logger.LogInfo("[dumpgammel] " + line);
Console.instance.Print(line);
});
}
}
}