Files
claudeandClaude Opus 5.5 9e28969629
Ashlar checks / tooling (push) Successful in 5s
Add ledge mantling
New [Game] mantling setting, on by default, with mantle max height (1.1
times the actor's height above its feet) and mantle duration (0.4 s).
When the player jumps or falls into a wall while moving towards it
(within 60 degrees), the movement solver casts the player's collision
shape up, across the edge and down. If it finds a surface flat enough to
stand on, above the feet and within reach, with headroom and room on top,
the player moves up, across and onto it along that checked path. Movement
and jump input are ignored meanwhile, and the climb ends any fall without
fall damage. No animation is needed. A script move or teleport, noclip or
paralysis cancels a climb.

Lua: self.controls.mantle (true by default) vetoes climbs while false,
and the actor's scripts get an AshlarMantle event with the ledge height
when one starts. The engine logs "Ashlar mantle:" lines at verbose level.
Player only; with the setting off nothing changes.

The geometry rules (reach, headroom, room on top, slope, too high, too
low) and the climb path are in mwphysics/mantle.cpp, apart from the
physics calls, with unit tests. The new mantle scenario uses a CC0 box
mesh written for it: the player climbs an 80 unit ledge, doesn't climb it
with the Lua veto, and doesn't climb a 240 unit wall.

Closes #28

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 15:44:40 -05:00

138 lines
4.5 KiB
C++

#include "mantle.hpp"
#include <algorithm>
#include <cmath>
#include "movementsolver.hpp"
namespace MWPhysics::Mantle
{
osg::Vec3f intoWall(const osg::Vec3f& wallNormal)
{
osg::Vec3f dir(-wallNormal.x(), -wallNormal.y(), 0.f);
const float length = dir.length();
if (length < 1e-3f)
return osg::Vec3f();
return dir / length;
}
bool wantsMantle(
bool onGround, bool flying, bool underwater, const osg::Vec3f& moveDir, const osg::Vec3f& wallNormal)
{
if (onGround || flying || underwater)
return false;
// A floor or a ceiling isn't something to climb.
if (isWalkableSlope(wallNormal) || wallNormal.z() < -0.5f)
return false;
const osg::Vec3f into = intoWall(wallNormal);
osg::Vec3f move(moveDir.x(), moveDir.y(), 0.f);
const float length = move.length();
if (into.length2() == 0.f || length < 1e-3f)
return false;
return (move / length) * into >= sMinPushCos;
}
float probeRise(float actorHeight, float maxHeightFactor)
{
return std::max(0.f, actorHeight * maxHeightFactor) + sClearance;
}
const char* resultName(Result result)
{
switch (result)
{
case Result::Ok:
return "ok";
case Result::NoHeadroom:
return "no headroom";
case Result::Blocked:
return "blocked";
case Result::NoSurface:
return "no surface";
case Result::Steep:
return "steep";
case Result::TooLow:
return "too low";
case Result::TooHigh:
return "too high";
}
return "unknown";
}
Result evaluate(const Probe& probe)
{
if (probe.mRiseReached < sMinRise)
return Result::NoHeadroom;
if (probe.mForwardReached < probe.mForwardNeeded)
return Result::Blocked;
if (!probe.mSurfaceHit)
return Result::NoSurface;
if (!isWalkableSlope(probe.mSurfaceNormal))
return Result::Steep;
const float height = probe.mSurfaceFeetZ - probe.mFeetZ;
if (height < sMinHeight)
return Result::TooLow;
if (height > probe.mActorHeight * probe.mMaxHeightFactor)
return Result::TooHigh;
// The surface has to sit below the height the probe crossed at, with the clearance kept.
if (probe.mSurfaceFeetZ - probe.mFeetZ + sClearance > probe.mRiseReached + 1e-3f)
return Result::NoHeadroom;
return Result::Ok;
}
osg::Vec3f pathPosition(const osg::Vec3f& start, float peakZ, const osg::Vec3f& target, float fraction)
{
const float t = std::clamp(fraction, 0.f, 1.f);
// Smoothstep: starts and ends gently.
const float eased = t * t * (3.f - 2.f * t);
const osg::Vec3f up(start.x(), start.y(), peakZ);
const osg::Vec3f across(target.x(), target.y(), peakZ);
const float l1 = std::abs(peakZ - start.z());
const float l2 = (across - up).length();
const float l3 = std::abs(peakZ - target.z());
const float total = l1 + l2 + l3;
if (total <= 1e-4f)
return target;
float d = eased * total;
if (d <= l1)
return start + (up - start) * (l1 > 0.f ? d / l1 : 1.f);
d -= l1;
if (d <= l2)
return up + (across - up) * (l2 > 0.f ? d / l2 : 1.f);
d -= l2;
return across + (target - across) * (l3 > 0.f ? std::min(d / l3, 1.f) : 1.f);
}
State start(const osg::Vec3f& startPos, float peakZ, const osg::Vec3f& target, float duration, float height)
{
State state;
state.mActive = true;
state.mStart = startPos;
state.mPeakZ = std::max({ peakZ, startPos.z(), target.z() });
state.mTarget = target;
state.mLast = startPos;
state.mElapsed = 0.f;
state.mDuration = std::max(duration, 0.01f);
state.mHeight = height;
return state;
}
osg::Vec3f advance(State& state, float dt)
{
if (!state.mActive)
return state.mLast;
state.mElapsed += std::max(dt, 0.f);
const float fraction = state.mElapsed / state.mDuration;
if (fraction >= 1.f)
{
state.mActive = false;
state.mLast = state.mTarget;
return state.mTarget;
}
state.mLast = pathPosition(state.mStart, state.mPeakZ, state.mTarget, fraction);
return state.mLast;
}
}