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

210 lines
7.7 KiB
C++

#include <apps/openmw/mwphysics/mantle.hpp>
#include <gtest/gtest.h>
#include <cmath>
namespace
{
using namespace MWPhysics::Mantle;
constexpr float sTolerance = 1e-3f;
constexpr float sHeight = 128.f; // actor height
// A wall facing the actor, which moves along +y.
const osg::Vec3f sWall(0.f, -1.f, 0.f);
const osg::Vec3f sForward(0.f, 1.f, 0.f);
// A probe that found a flat ledge `height` above the feet, with full headroom and room on top.
Probe ledge(float height)
{
Probe probe;
probe.mFeetZ = 100.f;
probe.mActorHeight = sHeight;
probe.mMaxHeightFactor = 1.1f;
probe.mRiseReached = probeRise(sHeight, 1.1f);
probe.mForwardNeeded = 60.f;
probe.mForwardReached = 60.f;
probe.mSurfaceHit = true;
probe.mSurfaceFeetZ = probe.mFeetZ + height;
probe.mSurfaceNormal = osg::Vec3f(0.f, 0.f, 1.f);
return probe;
}
TEST(MWPhysicsMantleTest, wantsMantleWhenPushingIntoWallInAir)
{
EXPECT_TRUE(wantsMantle(false, false, false, sForward, sWall));
// Within 60 degrees of straight in.
EXPECT_TRUE(wantsMantle(false, false, false, osg::Vec3f(0.5f, 1.f, 0.f), sWall));
}
TEST(MWPhysicsMantleTest, noMantleOnGroundFlyingOrSwimming)
{
EXPECT_FALSE(wantsMantle(true, false, false, sForward, sWall));
EXPECT_FALSE(wantsMantle(false, true, false, sForward, sWall));
EXPECT_FALSE(wantsMantle(false, false, true, sForward, sWall));
}
TEST(MWPhysicsMantleTest, noMantleWhenMovingAlongOrAwayFromWall)
{
EXPECT_FALSE(wantsMantle(false, false, false, osg::Vec3f(1.f, 0.f, 0.f), sWall));
EXPECT_FALSE(wantsMantle(false, false, false, osg::Vec3f(1.f, 0.3f, 0.f), sWall));
EXPECT_FALSE(wantsMantle(false, false, false, -sForward, sWall));
EXPECT_FALSE(wantsMantle(false, false, false, osg::Vec3f(), sWall));
}
TEST(MWPhysicsMantleTest, floorsAndCeilingsAreNotWalls)
{
EXPECT_FALSE(wantsMantle(false, false, false, sForward, osg::Vec3f(0.f, 0.f, 1.f)));
EXPECT_FALSE(wantsMantle(false, false, false, sForward, osg::Vec3f(0.f, 0.f, -1.f)));
EXPECT_FALSE(wantsMantle(false, false, false, sForward, osg::Vec3f(0.f, -0.6f, -0.8f)));
// A steep overhang-free slope still counts as a wall.
EXPECT_TRUE(wantsMantle(false, false, false, sForward, osg::Vec3f(0.f, -0.94f, 0.34f)));
}
TEST(MWPhysicsMantleTest, intoWallIsHorizontalUnit)
{
const osg::Vec3f into = intoWall(osg::Vec3f(0.f, -0.8f, 0.6f));
EXPECT_NEAR(into.x(), 0.f, sTolerance);
EXPECT_NEAR(into.y(), 1.f, sTolerance);
EXPECT_NEAR(into.z(), 0.f, sTolerance);
EXPECT_EQ(intoWall(osg::Vec3f(0.f, 0.f, 1.f)), osg::Vec3f());
}
TEST(MWPhysicsMantleTest, probeRiseCoversReachAndClearance)
{
EXPECT_NEAR(probeRise(sHeight, 1.1f), sHeight * 1.1f + sClearance, sTolerance);
EXPECT_NEAR(probeRise(sHeight, 0.f), sClearance, sTolerance);
}
TEST(MWPhysicsMantleTest, ledgeWithinReachIsOk)
{
EXPECT_EQ(evaluate(ledge(40.f)), Result::Ok);
EXPECT_EQ(evaluate(ledge(sHeight)), Result::Ok);
EXPECT_EQ(evaluate(ledge(sHeight * 1.1f - 0.5f)), Result::Ok);
}
TEST(MWPhysicsMantleTest, ledgeAboveReachIsTooHigh)
{
EXPECT_EQ(evaluate(ledge(sHeight * 1.1f + 1.f)), Result::TooHigh);
Probe probe = ledge(100.f);
probe.mMaxHeightFactor = 0.5f;
EXPECT_EQ(evaluate(probe), Result::TooHigh);
}
TEST(MWPhysicsMantleTest, surfaceAtFeetIsTooLow)
{
EXPECT_EQ(evaluate(ledge(0.f)), Result::TooLow);
EXPECT_EQ(evaluate(ledge(sMinHeight - 0.5f)), Result::TooLow);
EXPECT_EQ(evaluate(ledge(-10.f)), Result::TooLow);
}
TEST(MWPhysicsMantleTest, lowCeilingIsNoHeadroom)
{
Probe probe = ledge(40.f);
probe.mRiseReached = sMinRise - 1.f;
EXPECT_EQ(evaluate(probe), Result::NoHeadroom);
// The actor can rise, but not far enough to clear the ledge.
probe = ledge(40.f);
probe.mRiseReached = 42.f;
EXPECT_EQ(evaluate(probe), Result::NoHeadroom);
probe.mRiseReached = 40.f + sClearance;
EXPECT_EQ(evaluate(probe), Result::Ok);
}
TEST(MWPhysicsMantleTest, wallAboveReachIsBlocked)
{
Probe probe = ledge(40.f);
probe.mForwardReached = 10.f;
EXPECT_EQ(evaluate(probe), Result::Blocked);
}
TEST(MWPhysicsMantleTest, nothingBeyondTheEdgeIsNoSurface)
{
Probe probe = ledge(40.f);
probe.mSurfaceHit = false;
EXPECT_EQ(evaluate(probe), Result::NoSurface);
}
TEST(MWPhysicsMantleTest, steepTopIsRejected)
{
Probe probe = ledge(40.f);
probe.mSurfaceNormal = osg::Vec3f(0.f, -0.8f, 0.6f); // about 53 degrees
EXPECT_EQ(evaluate(probe), Result::Steep);
probe.mSurfaceNormal = osg::Vec3f(0.f, -0.5f, 0.866f); // 30 degrees
EXPECT_EQ(evaluate(probe), Result::Ok);
}
TEST(MWPhysicsMantleTest, resultNamesAreDistinct)
{
EXPECT_STREQ(resultName(Result::Ok), "ok");
EXPECT_STREQ(resultName(Result::TooHigh), "too high");
EXPECT_STREQ(resultName(Result::TooLow), "too low");
}
TEST(MWPhysicsMantleTest, pathRisesThenCrossesThenSettles)
{
const osg::Vec3f start(0.f, 0.f, 0.f);
const osg::Vec3f target(0.f, 50.f, 80.f);
const float peak = 90.f;
EXPECT_EQ(pathPosition(start, peak, target, 0.f), start);
EXPECT_EQ(pathPosition(start, peak, target, 1.f), target);
EXPECT_EQ(pathPosition(start, peak, target, 2.f), target);
// Never further from the wall than the start, never below it, never above the peak, and it moves forward
// only once it is high enough.
osg::Vec3f last = start;
for (int i = 1; i <= 100; ++i)
{
const osg::Vec3f pos = pathPosition(start, peak, target, i / 100.f);
EXPECT_GE(pos.y(), last.y() - sTolerance);
EXPECT_GE(pos.z(), start.z() - sTolerance);
EXPECT_LE(pos.z(), peak + sTolerance);
if (pos.y() > sTolerance)
EXPECT_NEAR(pos.z(), pos.y() < target.y() - sTolerance ? peak : pos.z(), sTolerance);
last = pos;
}
}
TEST(MWPhysicsMantleTest, pathIsSymmetricInTime)
{
// Eased: the middle of the time is the middle of the path length.
const osg::Vec3f start(0.f, 0.f, 0.f);
const osg::Vec3f target(0.f, 0.f, 100.f);
EXPECT_NEAR(pathPosition(start, 100.f, target, 0.5f).z(), 50.f, sTolerance);
EXPECT_LT(pathPosition(start, 100.f, target, 0.1f).z(), 10.f);
}
TEST(MWPhysicsMantleTest, advanceEndsOnTargetAfterDuration)
{
const osg::Vec3f startPos(0.f, 0.f, 0.f);
const osg::Vec3f target(0.f, 40.f, 60.f);
State state = start(startPos, 64.f, target, 0.4f, 60.f);
EXPECT_TRUE(state.mActive);
EXPECT_NEAR(state.mHeight, 60.f, sTolerance);
int steps = 0;
osg::Vec3f pos;
while (state.mActive && steps < 100)
{
pos = advance(state, 1.f / 60.f);
++steps;
}
EXPECT_FALSE(state.mActive);
EXPECT_EQ(pos, target);
EXPECT_EQ(state.mLast, target);
// 0.4 s at 60 steps a second.
EXPECT_GE(steps, 24);
EXPECT_LE(steps, 25);
// Further steps stay put.
EXPECT_EQ(advance(state, 1.f / 60.f), target);
}
TEST(MWPhysicsMantleTest, startKeepsPeakAboveBothEnds)
{
const State state = start(osg::Vec3f(0.f, 0.f, 10.f), 0.f, osg::Vec3f(0.f, 30.f, 50.f), 0.f, 40.f);
EXPECT_NEAR(state.mPeakZ, 50.f, sTolerance);
EXPECT_GT(state.mDuration, 0.f);
}
}