Files
claudeandClaude Opus 5.5 53e3d92496
Ashlar checks / tooling (push) Successful in 5s
Modern third-person camera defaults, collision ease-out and aim mode
- Shoulder view, auto shoulder switch, standing preview, deferred preview
  rotation, move 360, smooth view change and [Game] smooth movement are
  on by default.
- Collision ease-out: the camera still snaps in on collision but eases
  back out (0.15 s half-life, 60 u/s minimum speed).
- Aim mode: with a ranged weapon or a spell readied in third person the
  view stays over the shoulder, moves closer and narrows the FOV. Camera
  interface v2 adds getAimWeight and enable/disable functions.
- Pure math in camera/ease.lua with unit tests; SCENARIO=camera with
  PASS/FAIL checks; docs in docs/ashlar/CAMERA.md.
- ashlar-run.sh deletes stored Lua settings before each run so changed
  defaults are tested.

Closes #29

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

110 lines
4.3 KiB
C++

// Ashlar: tests for files/data/scripts/omw/camera/ease.lua, the pure math behind the third person camera's collision
// ease-out and aim mode.
#include <gtest/gtest.h>
#include <components/lua/luastate.hpp>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <string>
namespace
{
using namespace testing;
struct LuaCameraEaseTest : Test
{
LuaUtil::LuaState mLuaState{ nullptr, nullptr };
sol::state_view mLua{ mLuaState.unsafeState() };
LuaCameraEaseTest()
{
const auto path = std::filesystem::path{ OPENMW_PROJECT_SOURCE_DIR } / "files" / "data" / "scripts"
/ "omw" / "camera" / "ease.lua";
std::ifstream stream(path);
const std::string code(std::istreambuf_iterator<char>(stream), {});
mLua["ease"] = mLua.safe_script(code).get<sol::table>();
}
double eval(const std::string& expr) { return mLua.safe_script("return " + expr).get<double>(); }
};
TEST_F(LuaCameraEaseTest, EaseFactorClosesHalfTheGapPerHalfLife)
{
EXPECT_DOUBLE_EQ(eval("ease.easeFactor(0.15, 0.15)"), 0.5);
EXPECT_DOUBLE_EQ(eval("ease.easeFactor(0.3, 0.15)"), 0.75);
EXPECT_DOUBLE_EQ(eval("ease.easeFactor(0, 0.15)"), 0);
EXPECT_DOUBLE_EQ(eval("ease.easeFactor(0.016, 0)"), 1);
}
TEST_F(LuaCameraEaseTest, MovingInIsImmediate)
{
EXPECT_DOUBLE_EQ(eval("ease.recoverDistance(250, 60, 0.016, 0.15, 60)"), 60);
EXPECT_DOUBLE_EQ(eval("ease.recoverDistance(250, 250, 0.016, 0.15, 60)"), 250);
}
TEST_F(LuaCameraEaseTest, MovingOutEasesWithoutOvershoot)
{
// One 60 fps frame from 50 towards 250 covers only part of the gap.
const double step = eval("ease.recoverDistance(50, 250, 1/60, 0.15, 60)");
EXPECT_GT(step, 50 + 60.0 / 60);
EXPECT_LT(step, 70);
// After one half-life of frames, about half the gap is closed.
mLua.safe_script("d = 50; for i = 1, 9 do d = ease.recoverDistance(d, 250, 1/60, 0.15, 0) end");
EXPECT_NEAR(eval("d"), 150, 1);
// Steps shrink as the camera gets closer (ease-out), and it never passes the target.
mLua.safe_script(R"(
d, prev, shrinking = 50, 0, true
for i = 1, 600 do
local n = ease.recoverDistance(d, 250, 1/60, 0.15, 0)
if prev > 0 and n - d > prev + 1e-9 then shrinking = false end
prev, d = n - d, n
end
)");
EXPECT_TRUE(mLua["shrinking"].get<bool>());
EXPECT_LE(eval("d"), 250);
EXPECT_NEAR(eval("d"), 250, 1e-3);
}
TEST_F(LuaCameraEaseTest, MinimumSpeedFinishesTheEase)
{
// Near the target the exponential step would be tiny; the minimum speed takes over and lands exactly.
EXPECT_DOUBLE_EQ(eval("ease.recoverDistance(249.5, 250, 1/60, 0.15, 60)"), 250);
mLua.safe_script("d, frames = 50, 0; while d < 250 and frames < 1000 do "
"d = ease.recoverDistance(d, 250, 1/60, 0.15, 60); frames = frames + 1 end");
EXPECT_DOUBLE_EQ(eval("d"), 250);
EXPECT_LT(eval("frames"), 120); // under two seconds at 60 fps
EXPECT_GT(eval("frames"), 30); // not a snap
}
TEST_F(LuaCameraEaseTest, NoTimeMeansNoMovementOut)
{
EXPECT_DOUBLE_EQ(eval("ease.recoverDistance(50, 250, 0, 0.15, 60)"), 50);
}
TEST_F(LuaCameraEaseTest, ZeroHalfLifeSnaps)
{
EXPECT_DOUBLE_EQ(eval("ease.recoverDistance(50, 250, 1/60, 0, 0)"), 250);
}
TEST_F(LuaCameraEaseTest, ApproachReachesTargetExactly)
{
mLua.safe_script("w = 0; for i = 1, 120 do w = ease.approach(w, 1, 1/60, 0.08) end");
EXPECT_DOUBLE_EQ(eval("w"), 1);
mLua.safe_script("for i = 1, 120 do w = ease.approach(w, 0, 1/60, 0.08) end");
EXPECT_DOUBLE_EQ(eval("w"), 0);
EXPECT_NEAR(eval("ease.approach(0, 1, 0.08, 0.08)"), 0.5, 1e-9);
}
TEST_F(LuaCameraEaseTest, AimFramingBlends)
{
EXPECT_DOUBLE_EQ(eval("ease.aimDistance(200, 0.7, 0)"), 200);
EXPECT_DOUBLE_EQ(eval("ease.aimDistance(200, 0.7, 1)"), 140);
EXPECT_DOUBLE_EQ(eval("ease.aimDistance(200, 0.7, 0.5)"), 170);
EXPECT_DOUBLE_EQ(eval("ease.aimFieldOfView(1.0, 0.9, 1)"), 0.9);
EXPECT_DOUBLE_EQ(eval("ease.aimFieldOfView(1.0, 0.9, 0)"), 1.0);
}
}