fix(trol): True Form troll size, log, reach, root during attacks, slam

The troll pulsed several times a second and stayed small because
PlayerVisualHider.Hide put the hidden Visual back to scale 1 every
frame while Trol_PlayerUpdate_Patch grew it back, and the troll follows
that scale. Hide and Show take keepScale now, and the Trol calls pass
it. The camera height, which reads the same scale, stops jumping too.

The log in the troll's hand is the troll's own log model, hung the way
VisEquipment does it, instead of the Trol's, which was built for a
person's hand and came out huge and sideways.

Punch and slam reach further and hit wider, with a new Troll Attack
Reach option (1.3). The player is held still through every attack
clip, throw included.

The ground slam spawns the troll's own troll_groundslam_aoe with the
player's damage, kept off friends, plus its trigger and terrain
effects. It carries chop and pickaxe at least equal to its blunt, so
it breaks trees and rocks, and with Ground Slam Crater on it digs into
the terrain where it lands, never inside a ward without access.
This commit is contained in:
kanym 2026-10-14 17:08:20 +02:00
commit db29f868ea
4 changed files with 190 additions and 33 deletions

View file

@ -37,14 +37,18 @@ namespace RacesOfValheim
private float m_nextLog;
private string m_context = "";
public static void Hide(Transform playerVisual, string context)
/// <param name="keepScale">Leave the Visual's scale alone. For a body that is meant to be
/// sized while hidden: the Trol's True Form troll stands as tall as the Visual does, and
/// resetting it here every frame fought Trol_PlayerUpdate_Patch growing it back - the troll
/// pulsed several times a second and never reached full size.</param>
public static void Hide(Transform playerVisual, string context, bool keepScale = false)
{
if (playerVisual == null) return;
// Undo the two older ways this body could have been hidden, so a form entered right
// after one of those code paths still starts from a sane state.
if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true);
if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
if (!keepScale && playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
// Belt and braces: if anything else ever does deactivate the Visual, at least keep the
// animator from snapping back to its spawn state.
@ -68,12 +72,12 @@ namespace RacesOfValheim
}
}
public static void Show(Transform playerVisual, string context)
public static void Show(Transform playerVisual, string context, bool keepScale = false)
{
if (playerVisual == null) return;
if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true);
if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
if (!keepScale && playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
if (hider == null || !hider.enabled) return;

View file

@ -54,6 +54,8 @@ namespace RacesOfValheim
public static ConfigEntry<bool> TrueFormUseTrollModel;
public static ConfigEntry<float> TrueFormModelSize;
public static ConfigEntry<float> TrueFormTrollDamageShare;
public static ConfigEntry<float> TrueFormAttackReach;
public static ConfigEntry<bool> TrueFormSlamCrater;
// ─── Log Weapon (tier 7+) ──────────────────────────────────────────────
public static ConfigEntry<float> LogWeaponMaxDurability;
@ -142,6 +144,10 @@ namespace RacesOfValheim
new ConfigDescription("How tall the troll stands, as a share of the True Form size set by Scale above. 1 = exactly as tall as the old True Form.", new AcceptableValueRange<float>(0.5f, 2f)));
TrueFormTrollDamageShare = config.Bind(trueForm, "Troll Attack Damage", 0.5f,
new ConfigDescription("Share of a real troll's damage each troll attack deals. Your Clubs skill then scales it the way it scales a club, and the True Form Melee Bonus and tier bonuses add on top.", new AcceptableValueRange<float>(0.1f, 2f)));
TrueFormAttackReach = config.Bind(trueForm, "Troll Attack Reach", 1.3f,
new ConfigDescription("Multiplies how far the troll's punch and slam reach and how wide they hit. 1 = a real troll's reach at the size you stand.", new AcceptableValueRange<float>(0.5f, 3f)));
TrueFormSlamCrater = config.Bind(trueForm, "Ground Slam Crater", true,
new ConfigDescription("The ground slam digs into the ground where it lands, like a pickaxe blow. Never inside a ward you have no access to."));
const string logWeapon = "Trol - Log Weapon";
LogWeaponMaxDurability = config.Bind(logWeapon, "Max Durability", 6f,

View file

@ -33,6 +33,17 @@ namespace RacesOfValheim
public bool IsAttacking => isAttacking;
private void SetAttacking(bool on)
{
isAttacking = on;
TrolTrueFormModel.LocalAttacking = on;
}
private void OnDestroy()
{
TrolTrueFormModel.LocalAttacking = false;
}
private void Start()
{
anim = GetComponentInChildren<Animator>();
@ -54,7 +65,7 @@ namespace RacesOfValheim
{
Player player = Player.m_localPlayer;
if (player == null) return;
PlayerVisualHider.Hide(player.transform.Find("Visual"), "trol-trueform-local");
PlayerVisualHider.Hide(player.transform.Find("Visual"), "trol-trueform-local", keepScale: true);
}
private void Update()
@ -70,7 +81,7 @@ namespace RacesOfValheim
if (FormAnimGuard.Stuck(isAttacking, ref busySince))
{
StopAllCoroutines();
isAttacking = false;
SetAttacking(false);
}
if (isAttacking || player.IsDead()) return;
@ -117,15 +128,15 @@ namespace RacesOfValheim
private IEnumerator ThrowRoutine(TrolTrueFormModel.Profile profile)
{
isAttacking = true;
SetAttacking(true);
Fire(profile.Trigger);
yield return WaitForClip();
isAttacking = false;
SetAttacking(false);
}
private IEnumerator AttackRoutine(TrolTrueFormModel.Profile profile, bool log)
{
isAttacking = true;
SetAttacking(true);
Player player = Player.m_localPlayer;
int baseline = TrolTrueFormModel.AttackEvents;
@ -140,8 +151,18 @@ namespace RacesOfValheim
if (player != null && !player.IsDead())
{
profile.Shared.m_triggerEffect.Create(player.transform.position, player.transform.rotation, player.transform);
int landed = TrolTrueFormHit.Sweep(player, profile, transform.localScale.x);
float scale = transform.localScale.x;
int landed;
if (profile.Type == Attack.AttackType.Area)
{
landed = TrolTrueFormHit.Slam(player, profile, scale);
}
else
{
profile.Shared.m_triggerEffect.Create(player.transform.position, player.transform.rotation, player.transform);
landed = TrolTrueFormHit.Sweep(player, profile, scale);
}
if (landed > 0)
{
player.RaiseSkill(Skills.SkillType.Clubs, 1f);
@ -150,7 +171,7 @@ namespace RacesOfValheim
}
yield return WaitForClip(Time.time - started);
isAttacking = false;
SetAttacking(false);
}
private void Fire(string trigger)
@ -206,24 +227,115 @@ namespace RacesOfValheim
"piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox",
"character_noenv", "vehicle");
private const string SlamAoeFallback = "troll_groundslam_aoe";
private const string CraterPrefab = "digg_v3";
private static readonly int GroundMask = LayerMask.GetMask("terrain", "Default", "static_solid", "piece");
/// <summary>
/// The ground slam, done the way the troll does it: its attack's own spawn-on-trigger, the
/// troll_groundslam_aoe that carries the dust and the area hit. Set up with this player's
/// damage instead of the troll's (m_useAttackSettings) and kept off friends. The first
/// version swept a sphere and spawned nothing, so a slam that hit no creature looked like
/// nothing had happened at all.
///
/// It also breaks things: the slam's damage gets chop and pickaxe at least equal to its
/// blunt, so trees, rocks and ore around the fists take the blow. With Ground Slam Crater on,
/// the ground gives way too - a pickaxe's dig, which TerrainOp does not check against wards on
/// its own, so the ward check is done here.
/// </summary>
/// <returns>1 if the slam went off, so the caller raises the skill; the AoE does its own hits.</returns>
public static int Slam(Player player, TrolTrueFormModel.Profile profile, float costumeScale)
{
float range = Mathf.Max(2f, profile.Range * costumeScale * TrolConfig.TrueFormAttackReach.Value);
Vector3 impact = player.transform.position + player.transform.forward * (range * 0.5f);
// Down onto whatever is under the fists, so the dust and the crater sit on the ground.
bool onTerrain = false;
if (Physics.Raycast(impact + Vector3.up * 3f, Vector3.down, out RaycastHit ground, 8f, GroundMask))
{
impact = ground.point;
onTerrain = ground.collider != null && ground.collider.GetComponent<Heightmap>() != null;
}
HitData.DamageTypes damage = TrolTrueFormModel.DamageFor(player, profile);
damage.m_chop = Mathf.Max(damage.m_chop, damage.m_blunt);
damage.m_pickaxe = Mathf.Max(damage.m_pickaxe, damage.m_blunt);
HitData hitData = new HitData();
hitData.m_damage = damage;
hitData.m_pushForce = profile.PushForce;
hitData.m_skill = Skills.SkillType.Clubs;
hitData.m_toolTier = (short)profile.ToolTier;
hitData.m_dodgeable = true;
hitData.m_blockable = true;
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.SetAttacker(player);
profile.Shared.m_triggerEffect.Create(impact, player.transform.rotation);
profile.Shared.m_hitTerrainEffect.Create(impact, Quaternion.identity);
GameObject aoePrefab = profile.Shared.m_attack.m_spawnOnTrigger;
if (aoePrefab == null) aoePrefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(SlamAoeFallback);
bool spawned = false;
if (aoePrefab != null)
{
GameObject instance = Object.Instantiate(aoePrefab, impact, Quaternion.identity);
Aoe aoe = instance.GetComponent<Aoe>();
if (aoe != null)
{
aoe.m_useAttackSettings = true;
aoe.m_hitFriendly = false;
aoe.m_hitOwner = false;
aoe.m_hitProps = true;
aoe.m_radius = Mathf.Max(aoe.m_radius, range * 0.6f);
aoe.Setup(player, player.transform.forward, -1f, hitData, null, null);
spawned = true;
}
else
{
Object.Destroy(instance);
}
}
// No Aoe to borrow: the sphere, centred on the impact, so the slam still hits.
if (!spawned) SphereHit(player, impact + Vector3.up, range * 0.7f, 180f, damage, profile);
if (TrolConfig.TrueFormSlamCrater.Value && onTerrain && PrivateArea.CheckAccess(impact, 0f, false))
{
GameObject crater = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(CraterPrefab);
if (crater != null) Object.Instantiate(crater, impact, Quaternion.identity);
}
Jotunn.Logger.LogInfo($"[Trol] Ground slam: aoe '{aoePrefab?.name ?? "none"}' spawned={spawned}, " +
$"range {range:0.0}, onTerrain={onTerrain}.");
return 1;
}
/// <returns>How many things the blow landed on.</returns>
public static int Sweep(Player player, TrolTrueFormModel.Profile profile, float costumeScale)
{
Vector3 pos = player.transform.position;
Vector3 forward = player.transform.forward;
float range = Mathf.Max(1.5f, profile.Range * costumeScale);
bool area = profile.Type == Attack.AttackType.Area;
float range = Mathf.Max(2f, profile.Range * costumeScale * TrolConfig.TrueFormAttackReach.Value);
Vector3 center = area
? pos + forward * (range * 0.3f) + Vector3.up
: pos + forward * (range * 0.5f) + Vector3.up * Mathf.Max(1f, profile.Height * costumeScale);
float radius = area ? range * 0.7f : range * 0.5f + 0.5f;
float halfAngle = area ? 180f : Mathf.Max(60f, profile.Angle * 0.5f);
Vector3 center = pos + forward * (range * 0.55f) + Vector3.up * Mathf.Max(1f, profile.Height * costumeScale);
float radius = range * 0.6f + 0.75f;
float halfAngle = Mathf.Max(75f, profile.Angle * 0.5f);
return SphereHit(player, center, radius, halfAngle, TrolTrueFormModel.DamageFor(player, profile), profile);
}
private static int SphereHit(Player player, Vector3 center, float radius, float halfAngle,
HitData.DamageTypes damage, TrolTrueFormModel.Profile profile)
{
Vector3 pos = player.transform.position;
Vector3 forward = player.transform.forward;
Collider[] hits = Physics.OverlapSphere(center, radius, Mask);
FormMelee.LogSwing(FormLabel, hits.Length);
HitData.DamageTypes damage = TrolTrueFormModel.DamageFor(player, profile);
var done = new HashSet<GameObject>();
int landed = 0;
@ -298,7 +410,7 @@ namespace RacesOfValheim
private void LateUpdate()
{
if (player == null) return;
PlayerVisualHider.Hide(player.transform.Find("Visual"), "trol-trueform-remote");
PlayerVisualHider.Hide(player.transform.Find("Visual"), "trol-trueform-remote", keepScale: true);
}
private void Update()
@ -381,7 +493,7 @@ namespace RacesOfValheim
{
existing.gameObject.SetActive(false);
Object.Destroy(existing.gameObject);
PlayerVisualHider.Show(playerVisual, "trol-trueform-remote-revert");
PlayerVisualHider.Show(playerVisual, "trol-trueform-remote-revert", keepScale: true);
}
return;
}
@ -393,7 +505,7 @@ namespace RacesOfValheim
model.AddComponent<RemoteTrolTrueFormAnimator>();
}
PlayerVisualHider.Hide(playerVisual, "trol-trueform-remote");
PlayerVisualHider.Hide(playerVisual, "trol-trueform-remote", keepScale: true);
}
}
}

View file

@ -55,6 +55,10 @@ namespace RacesOfValheim
public float PushForce;
public int ToolTier;
public ItemDrop.ItemData.SharedData Shared;
// The weapon's own model, as VisEquipment would hang it in the troll's hand. Only the
// log swings have one worth showing.
public GameObject Attach;
}
private static readonly Dictionary<AttackKind, Profile> profiles = new Dictionary<AttackKind, Profile>();
@ -69,6 +73,10 @@ namespace RacesOfValheim
public static bool LocalActive => localModel != null;
/// <summary>True while the local troll is in the middle of a swing, a slam or a throw.
/// Trol_TrueFormHoldStill_Patch holds the player still for exactly that long.</summary>
public static bool LocalAttacking { get; set; }
/// <summary>How many OnAttackTrigger events the local troll has fired. Attack routines
/// remember the count and wait for it to move.</summary>
public static int AttackEvents => attackEvents;
@ -101,7 +109,7 @@ namespace RacesOfValheim
if (localModel == null) return;
localModel.AddComponent<TrolTrueFormBehavior>();
PlayerVisualHider.Hide(player.transform.Find("Visual"), "trol-trueform-local");
PlayerVisualHider.Hide(player.transform.Find("Visual"), "trol-trueform-local", keepScale: true);
GameObject smoke = PrefabManager.Cache.GetPrefab<GameObject>("vfx_corpse_destruction_medium");
if (smoke != null) Object.Instantiate(smoke, player.transform.position + Vector3.up, Quaternion.identity);
@ -118,8 +126,9 @@ namespace RacesOfValheim
localModel.SetActive(false);
Object.Destroy(localModel);
localModel = null;
LocalAttacking = false;
if (player != null) PlayerVisualHider.Show(player.transform.Find("Visual"), "trol-trueform-revert");
if (player != null) PlayerVisualHider.Show(player.transform.Find("Visual"), "trol-trueform-revert", keepScale: true);
return true;
}
@ -253,17 +262,23 @@ namespace RacesOfValheim
if (wanted == null || rightHandPath == null) return;
Transform hand = model.transform.Find(rightHandPath);
Transform attach = itemPrefab.transform.Find("attach");
if (hand == null || attach == null) return;
if (hand == null) return;
current = Object.Instantiate(attach.gameObject, hand);
// The troll's OWN log, not the Trol's. The Trol's was built for a person's hand - turned
// 90 degrees, pushed 1.5 m along it, scaled to 0.4 - and in the troll's fist that came
// out huge and pointing the wrong way. The troll's log weapon carries a model made for
// exactly this bone, and hanging it the way VisEquipment.AttachItem does (zero offset,
// no rotation, its own scale) puts it where vanilla puts it.
Profile log = Get(AttackKind.LogSwingH) ?? Get(AttackKind.LogSwingV);
GameObject source = log?.Attach;
if (source == null) return;
current = Object.Instantiate(source, hand);
current.name = "TrolTrueFormLog";
current.transform.localPosition = Vector3.zero;
current.transform.localRotation = Quaternion.identity;
// Left at the attach's own scale: the hand bone already carries the costume's, so the
// log grows and shrinks with the troll. Whether it looks right in the fist is a thing
// only an eye can settle - this is the line to change if it does not.
current.transform.localScale = Vector3.one;
current.transform.localScale = source.transform.localScale;
foreach (Collider c in current.GetComponentsInChildren<Collider>(true)) Object.Destroy(c);
}
// ─── Attacks ─────────────────────────────────────────────────────────
@ -316,6 +331,7 @@ namespace RacesOfValheim
PushForce = shared.m_attackForce,
ToolTier = shared.m_toolTier,
Shared = shared,
Attach = weapon.transform.Find("attach")?.gameObject,
};
profiles[kind.Value] = p;
@ -369,6 +385,25 @@ namespace RacesOfValheim
// ─── Native attack loop, shut while the costume is on ────────────────
/// <summary>
/// Holds the troll still through its own attack clips, the way vanilla roots a creature in
/// mid-swing. SkeletonRiseControlsPatch's arrangement: the move direction is the first
/// argument of SetControls, bound by position.
/// </summary>
[HarmonyPatch(typeof(Player), "SetControls")]
public static class Trol_TrueFormHoldStill_Patch
{
public static void Prefix(Player __instance, ref Vector3 __0, ref bool jump, ref bool crouch, ref bool dodge)
{
if (__instance != Player.m_localPlayer || !LocalActive || !LocalAttacking) return;
__0 = Vector3.zero;
jump = false;
crouch = false;
dodge = false;
}
}
[HarmonyPatch(typeof(Humanoid), "StartAttack")]
public static class Trol_TrueFormBlockNativeAttack_Patch
{