Files
claudeandClaude Opus 5.5 a691315db7
Ashlar checks / tooling (push) Successful in 9s
Add a stamina-driven sprint
New [Game] sprint setting, on by default, with sprint speed multiplier
(1.4 times run speed), sprint fatigue drain (10 per second on top of
running, halved at Athletics 100) and sprint fatigue threshold (0.1 of
maximum fatigue to start). A sprint ends when fatigue falls below 1% of
maximum; regeneration keeps fatigue just above 0 while running, so
exactly 0 is never reached.

Sprint is engine action 52, Lua input.ACTION.Sprint and the player's
'Sprint' Lua action, with self.controls.sprint. Defaults are Left Alt
and D-pad up, because L3 is already Sneak. Sprinting needs forward
movement within 45 degrees, and isn't possible while sneaking, in the
block stance, swimming, levitating, knocked down or paralyzed. Pressing
Use with a weapon or spell ready ends the sprint until Sprint is
released, so no attack starts while sprinting. A sprint counts as
running for animations, Athletics and the running fatigue cost. AI
doesn't sprint. With the setting off the control does nothing.

The rules are in mwmechanics/sprint.cpp with unit tests. The movement
scenario gains sprint, tiredsprint and sprintuse phases, run in a
circle from the start point so the player stays on land.

Closes #27

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

93 lines
3.2 KiB
C++

#include "sprint.hpp"
#include <algorithm>
#include <cmath>
#include <components/debug/debuglog.hpp>
#include <components/settings/values.hpp>
#include "../mwworld/class.hpp"
#include "../mwworld/ptr.hpp"
#include "activeblock.hpp"
#include "creaturestats.hpp"
namespace MWMechanics
{
bool isForwardMovement(float forward, float side)
{
return forward > 0.f && forward >= std::abs(side);
}
bool canSprint(const SprintConditions& c, float threshold)
{
if (!c.mHeld || !c.mMovingForward || c.mSneaking || c.mBlocking || c.mSwimming || c.mFlying
|| c.mIncapacitated || c.mCancelled)
return false;
if (c.mCurrentFatigue <= 0.f)
return false;
if (c.mMaxFatigue <= 0.f)
return true;
const float ratio = c.mCurrentFatigue / c.mMaxFatigue;
if (c.mWasSprinting)
return ratio >= sSprintExhaustedRatio;
return ratio >= threshold;
}
float getSprintFatigueDrain(float drain, float athletics)
{
const float skill = std::clamp(athletics, 0.f, 100.f);
return std::max(0.f, drain) * (1.f - 0.5f * skill / 100.f);
}
bool usesSprint()
{
return Settings::game().mSprint;
}
bool isSprinting(const MWWorld::Ptr& actor)
{
if (!usesSprint() || !actor.getClass().isActor())
return false;
return actor.getClass().getCreatureStats(actor).getSprinting();
}
bool updateSprint(const MWWorld::Ptr& actor, float forward, float side, bool sneaking, bool swimming, bool flying)
{
CreatureStats& stats = actor.getClass().getCreatureStats(actor);
const bool wasSprinting = stats.getSprinting();
if (!usesSprint() || !actor.getClass().isNpc())
{
stats.setSprinting(false);
return false;
}
// Pressing Use with a weapon or spell ready cancels the sprint until Sprint is released.
if (!stats.getSprintHeld())
stats.setSprintCancelled(false);
else if (stats.getAttackingOrSpell() && stats.getDrawState() != DrawState::Nothing)
stats.setSprintCancelled(true);
SprintConditions c;
c.mHeld = stats.getSprintHeld();
c.mMovingForward = isForwardMovement(forward, side);
c.mSneaking = sneaking;
c.mBlocking = isInBlockStance(actor);
c.mSwimming = swimming;
c.mFlying = flying;
c.mIncapacitated = stats.getKnockedDown() || stats.isParalyzed() || stats.isDead();
c.mCancelled = stats.getSprintCancelled();
c.mWasSprinting = wasSprinting;
c.mCurrentFatigue = stats.getFatigue().getCurrent();
c.mMaxFatigue = stats.getFatigue().getModified();
const bool sprinting = canSprint(c, Settings::game().mSprintFatigueThreshold);
stats.setSprinting(sprinting);
if (sprinting != wasSprinting)
Log(Debug::Verbose) << "Ashlar sprint: " << actor.getCellRef().getRefId()
<< (sprinting ? " starts" : " ends") << ", fatigue " << c.mCurrentFatigue << "/"
<< c.mMaxFatigue << (c.mCancelled ? ", cancelled by Use" : "");
return sprinting;
}
}