// 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 #include #include #include #include #include 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(stream), {}); mLua["ease"] = mLua.safe_script(code).get(); } double eval(const std::string& expr) { return mLua.safe_script("return " + expr).get(); } }; 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()); 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); } }