diff --git a/JotunnModStub/Alfar/AlfarPowers.cs b/JotunnModStub/Alfar/AlfarPowers.cs index 6b0ea70..8a02b61 100644 --- a/JotunnModStub/Alfar/AlfarPowers.cs +++ b/JotunnModStub/Alfar/AlfarPowers.cs @@ -182,35 +182,64 @@ namespace RacesOfValheim Vector3 direction = aim.forward; float distance = AlfarPowerConfig.SkridDistance.Value; - if (!Pay(player, AlfarPowerConfig.SkridCost.Value)) return; - // Stop short of whatever is in the way, so the blink never ends inside a wall. Vector3 destination = origin + direction * distance; - RaycastHit[] hits = Physics.RaycastAll(origin, direction, distance, ~0, QueryTriggerInteraction.Ignore); - float nearest = float.MaxValue; - foreach (RaycastHit hit in hits) + if (NearestHit(player, origin, direction, distance, out RaycastHit wall)) { - if (hit.collider.GetComponentInParent() == player) continue; - if (hit.distance < nearest) + destination = wall.point - direction * 0.6f; + } + + // Back down to the player's own height: the origin was at the chest. The floor is found + // with a short ray down from the landing point, never with GetSolidHeight: that one casts + // from 1000 m up, so under any roof (a crypt, a cave, a house) it returned the roof and + // put the Alfr on top of it. A flat 1 m drop also sank the feet under the floor when the + // blink was aimed at it. + const float FloorSearch = 3f; + if (NearestHit(player, destination, Vector3.down, FloorSearch, out RaycastHit floor)) + { + destination.y = floor.point.y + 0.05f; + } + else if (player.InInterior()) + { + // No floor under the landing point inside a dungeon: it would end in the void the + // dungeon floats in. Cancel before paying. + return; + } + else + { + destination.y -= 1.0f; + if (ZoneSystem.instance != null) { - nearest = hit.distance; - destination = hit.point - direction * 0.6f; + // Terrain only, so a roof overhead is never mistaken for the ground. + float ground = ZoneSystem.instance.GetGroundHeight(destination); + if (destination.y < ground) destination.y = ground + 0.1f; } } - // Back down to the player's own height: the origin was at the chest. - destination.y -= 1.0f; - if (ZoneSystem.instance != null) - { - float ground = ZoneSystem.instance.GetSolidHeight(destination); - if (destination.y < ground) destination.y = ground + 0.1f; - } + if (!Pay(player, AlfarPowerConfig.SkridCost.Value)) return; Teleport(player, destination); StartCooldown("skrid", AlfarPowerConfig.SkridCooldown.Value); AlfarFx.Spawn("fx_JotunWitch_LightningBolt_Explosion", origin); } + /// The nearest solid hit along a ray, skipping the caster's own colliders. + private static bool NearestHit(Player player, Vector3 origin, Vector3 direction, float distance, out RaycastHit nearest) + { + nearest = default; + bool found = false; + foreach (RaycastHit hit in Physics.RaycastAll(origin, direction, distance, ~0, QueryTriggerInteraction.Ignore)) + { + if (hit.collider.GetComponentInParent() == player) continue; + if (!found || hit.distance < nearest.distance) + { + nearest = hit; + found = true; + } + } + return found; + } + /// /// Moves the body without the teleport fade: the rigidbody is repositioned and its velocity /// zeroed, and the fall tracker is reset so a blink up a cliff does not count as a fall.