Files
claudeandClaude Opus 5.5 28d5d076a0 Make attacks stamina-driven with real time combat
With [Game] real time combat on:
- An attack's fatigue cost is paid when the swing is released, not when
  it lands. The base cost (fFatigueAttackBase plus fFatigueAttackMult
  times encumbrance) scales from half at no wind-up to full at full
  wind-up; the weapon part stays weight * strength * fWeaponFatigueMult.
- No weapon attack can start at zero fatigue or below the new
  [Game] attack fatigue threshold (0.1 of max). The player gets a
  "too tired to attack" message once per press; combat AI waits for
  fatigue to recover instead of winding up. Spells aren't affected.
- Below [Game] attack fatigue cap (0.5 of max), the strongest possible
  attack falls linearly to a quarter strength at zero fatigue.

The rules are in mwmechanics/stamina.cpp, with unit tests. Behavior with
real time combat off is unchanged.

Closes #21

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

95 lines
3.5 KiB
C++

#include "stamina.hpp"
#include <algorithm>
#include <components/esm3/loadgmst.hpp>
#include <components/settings/values.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
#include "actorutil.hpp"
#include "creaturestats.hpp"
namespace MWMechanics
{
namespace
{
bool isGodMode(const MWWorld::Ptr& actor)
{
return actor == getPlayer() && MWBase::Environment::get().getWorld()->getGodModeState();
}
}
float getAttackFatigueCost(float base, float encumbranceMult, float normalizedEncumbrance, float weaponWeight,
float weaponMult, float attackStrength)
{
const float strength = std::clamp(attackStrength, 0.f, 1.f);
const float baseCost = (base + normalizedEncumbrance * encumbranceMult) * (0.5f + 0.5f * strength);
return std::max(0.f, baseCost + weaponWeight * strength * weaponMult);
}
bool hasFatigueToAttack(float currentFatigue, float maxFatigue, float threshold)
{
if (currentFatigue <= 0.f)
return false;
if (maxFatigue <= 0.f)
return true;
return currentFatigue / maxFatigue >= threshold;
}
float getAttackStrengthCap(float currentFatigue, float maxFatigue, float capRatio)
{
if (capRatio <= 0.f || maxFatigue <= 0.f)
return 1.f;
const float t = std::clamp(currentFatigue / maxFatigue / capRatio, 0.f, 1.f);
return sMinAttackStrengthCap + (1.f - sMinAttackStrengthCap) * t;
}
bool usesStaminaAttacks()
{
return Settings::game().mRealTimeCombat;
}
bool isTooTiredToAttack(const MWWorld::Ptr& actor)
{
if (!usesStaminaAttacks() || isGodMode(actor))
return false;
const DynamicStat<float>& fatigue = actor.getClass().getCreatureStats(actor).getFatigue();
return !hasFatigueToAttack(
fatigue.getCurrent(), fatigue.getModified(), Settings::game().mAttackFatigueThreshold);
}
float capAttackStrength(const MWWorld::Ptr& actor, float attackStrength)
{
if (!usesStaminaAttacks() || isGodMode(actor))
return attackStrength;
const DynamicStat<float>& fatigue = actor.getClass().getCreatureStats(actor).getFatigue();
return std::min(attackStrength,
getAttackStrengthCap(fatigue.getCurrent(), fatigue.getModified(), Settings::game().mAttackFatigueCap));
}
void applyAttackFatigueCost(const MWWorld::Ptr& attacker, const MWWorld::Ptr& weapon, float attackStrength)
{
if (isGodMode(attacker))
return;
const MWWorld::Store<ESM::GameSetting>& store
= MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
static const float fFatigueAttackBase = store.find("fFatigueAttackBase")->mValue.getFloat();
static const float fFatigueAttackMult = store.find("fFatigueAttackMult")->mValue.getFloat();
static const float fWeaponFatigueMult = store.find("fWeaponFatigueMult")->mValue.getFloat();
const float weight = weapon.isEmpty() ? 0.f : weapon.getClass().getWeight(weapon);
const float cost = getAttackFatigueCost(fFatigueAttackBase, fFatigueAttackMult,
attacker.getClass().getNormalizedEncumbrance(attacker), weight, fWeaponFatigueMult, attackStrength);
CreatureStats& stats = attacker.getClass().getCreatureStats(attacker);
DynamicStat<float> fatigue = stats.getFatigue();
fatigue.setCurrent(fatigue.getCurrent() - cost);
stats.setFatigue(fatigue);
}
}