diff --git a/JotunnModStub/PlayerVisualHider.cs b/JotunnModStub/PlayerVisualHider.cs index 0f92cbf..6e753e5 100644 --- a/JotunnModStub/PlayerVisualHider.cs +++ b/JotunnModStub/PlayerVisualHider.cs @@ -37,14 +37,18 @@ namespace RacesOfValheim private float m_nextLog; private string m_context = ""; - public static void Hide(Transform playerVisual, string context) + /// 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. + 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(); if (hider == null || !hider.enabled) return; diff --git a/JotunnModStub/Trol/TrolConfig.cs b/JotunnModStub/Trol/TrolConfig.cs index eaecaae..2588e9a 100644 --- a/JotunnModStub/Trol/TrolConfig.cs +++ b/JotunnModStub/Trol/TrolConfig.cs @@ -54,6 +54,8 @@ namespace RacesOfValheim public static ConfigEntry TrueFormUseTrollModel; public static ConfigEntry TrueFormModelSize; public static ConfigEntry TrueFormTrollDamageShare; + public static ConfigEntry TrueFormAttackReach; + public static ConfigEntry TrueFormSlamCrater; // ─── Log Weapon (tier 7+) ────────────────────────────────────────────── public static ConfigEntry 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(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(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(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, diff --git a/JotunnModStub/Trol/TrolTrueFormBehavior.cs b/JotunnModStub/Trol/TrolTrueFormBehavior.cs index 1e52a57..004f99c 100644 --- a/JotunnModStub/Trol/TrolTrueFormBehavior.cs +++ b/JotunnModStub/Trol/TrolTrueFormBehavior.cs @@ -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(); @@ -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"); + + /// + /// 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. + /// + /// 1 if the slam went off, so the caller raises the skill; the AoE does its own hits. + 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() != 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(SlamAoeFallback); + + bool spawned = false; + if (aoePrefab != null) + { + GameObject instance = Object.Instantiate(aoePrefab, impact, Quaternion.identity); + Aoe aoe = instance.GetComponent(); + 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(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; + } + /// How many things the blow landed on. 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(); 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(); } - PlayerVisualHider.Hide(playerVisual, "trol-trueform-remote"); + PlayerVisualHider.Hide(playerVisual, "trol-trueform-remote", keepScale: true); } } } diff --git a/JotunnModStub/Trol/TrolTrueFormModel.cs b/JotunnModStub/Trol/TrolTrueFormModel.cs index 9748bf8..435d8f2 100644 --- a/JotunnModStub/Trol/TrolTrueFormModel.cs +++ b/JotunnModStub/Trol/TrolTrueFormModel.cs @@ -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 profiles = new Dictionary(); @@ -69,6 +73,10 @@ namespace RacesOfValheim public static bool LocalActive => localModel != null; + /// 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. + public static bool LocalAttacking { get; set; } + /// How many OnAttackTrigger events the local troll has fired. Attack routines /// remember the count and wait for it to move. public static int AttackEvents => attackEvents; @@ -101,7 +109,7 @@ namespace RacesOfValheim if (localModel == null) return; localModel.AddComponent(); - 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("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(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 ──────────────── + /// + /// 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. + /// + [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 {