diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 4473cc2382..55f8cb0bfd 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -104,7 +104,7 @@ add_openmw_dir (mwmechanics aicast aiescort aiface aiactivate aicombat recharge repair enchanting pathfinding pathgrid security spellcasting spellresistance disease pickpocket levelledlist combat steering obstacle autocalcspell difficultyscaling aicombataction summoning character actors objects aistate weaponpriority spellpriority weapontype spellutil - spelleffects activeblock aiblock + spelleffects activeblock aiblock stamina ) add_openmw_dir (mwstate diff --git a/apps/openmw/mwmechanics/aicombat.cpp b/apps/openmw/mwmechanics/aicombat.cpp index 3ea0eb3883..9a25690e40 100644 --- a/apps/openmw/mwmechanics/aicombat.cpp +++ b/apps/openmw/mwmechanics/aicombat.cpp @@ -25,6 +25,7 @@ #include "creaturestats.hpp" #include "movement.hpp" #include "pathgrid.hpp" +#include "stamina.hpp" #include "steering.hpp" #include "weapontype.hpp" @@ -664,7 +665,9 @@ namespace MWMechanics void AiCombatStorage::startAttackIfReady(const MWWorld::Ptr& actor, CharacterController& characterController, const ESM::Weapon* weapon, bool distantCombat) { - if (mReadyToAttack && characterController.readyToStartAttack()) + // Ashlar: don't wind up a weapon attack when too tired to start it; wait for fatigue to recover. + if (mReadyToAttack && characterController.readyToStartAttack() + && !(mCurrentAction && mCurrentAction->getSpell().empty() && isTooTiredToAttack(actor))) { if (mAttackCooldown <= 0) { diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index f6f3e444c7..3a1ea30e9f 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -55,6 +55,7 @@ #include "npcstats.hpp" #include "security.hpp" #include "spellcasting.hpp" +#include "stamina.hpp" #include "weapontype.hpp" namespace @@ -1253,6 +1254,10 @@ namespace MWMechanics mAttackWindUp = mAttackStrength; if (mAttackStrength == -1.f) mAttackStrength = std::min(1.f, 0.1f + Misc::Rng::rollClosedProbability(prng)); + // Ashlar: low fatigue caps the attack strength, and the swing's fatigue cost is paid now, on release. + const bool staminaAttack = usesStaminaAttacks(); + if (staminaAttack) + mAttackStrength = capAttackStrength(mPtr, mAttackStrength); ESM::WeaponType::Class weapclass = getWeaponType(mWeaponTypeId)->mWeaponClass; if (weapclass != ESM::WeaponType::Ranged) { @@ -1265,6 +1270,19 @@ namespace MWMechanics playSwishSound(); } + if (staminaAttack) + { + MWWorld::Ptr weapon; + if (mPtr.getClass().hasInventoryStore(mPtr)) + { + MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); + MWWorld::ContainerStoreIterator weaponSlot = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); + if (weaponSlot != inv.end() && weaponSlot->getType() == ESM::Weapon::sRecordId) + weapon = *weaponSlot; + } + applyAttackFatigueCost(mPtr, weapon, mAttackStrength); + } + mReadyToHit = true; } @@ -1507,12 +1525,23 @@ namespace MWMechanics float complete = 0.f; bool animPlaying = false; ESM::WeaponType::Class weapclass = getWeaponType(mWeaponTypeId)->mWeaponClass; + if (!getAttackingOrSpell()) + mTooTiredNotified = false; if (getAttackingOrSpell()) { mResetIdleOnAttackEnd = true; // Ashlar: no new attacks while holding the block stance. - if (mUpperBodyState == UpperBodyState::WeaponEquipped - && (mHitState == CharState_None || mHitState == CharState_Block) && !isInBlockStance(mPtr)) + const bool canStartAttack = mUpperBodyState == UpperBodyState::WeaponEquipped + && (mHitState == CharState_None || mHitState == CharState_Block) && !isInBlockStance(mPtr); + // Ashlar: no weapon attacks below the attack fatigue threshold. Tell the player once per press. + const bool tooTired = canStartAttack && mWeaponTypeId != ESM::WeaponType::Spell + && mWeaponTypeId != ESM::WeaponType::PickProbe && isTooTiredToAttack(mPtr); + if (tooTired && !mTooTiredNotified && mPtr == getPlayer()) + { + MWBase::Environment::get().getWindowManager()->messageBox("You are too tired to attack."); + mTooTiredNotified = true; + } + if (canStartAttack && !tooTired) { mAttackStrength = -1.f; mReadyToHit = false; diff --git a/apps/openmw/mwmechanics/character.hpp b/apps/openmw/mwmechanics/character.hpp index 1333d6716e..0e879b29da 100644 --- a/apps/openmw/mwmechanics/character.hpp +++ b/apps/openmw/mwmechanics/character.hpp @@ -176,6 +176,7 @@ namespace MWMechanics float mAttackWindUp{ -1.f }; float mAttackStrength{ -1.f }; bool mReadyToHit{ false }; + bool mTooTiredNotified{ false }; // Ashlar MWWorld::Ptr mAttackVictim; osg::Vec3f mAttackHitPos; bool mAttackSuccess{ false }; diff --git a/apps/openmw/mwmechanics/combat.cpp b/apps/openmw/mwmechanics/combat.cpp index c0217cbdc5..bbc79959f3 100644 --- a/apps/openmw/mwmechanics/combat.cpp +++ b/apps/openmw/mwmechanics/combat.cpp @@ -34,6 +34,7 @@ #include "pathfinding.hpp" #include "spellcasting.hpp" #include "spellresistance.hpp" +#include "stamina.hpp" namespace { @@ -552,6 +553,10 @@ namespace MWMechanics void applyFatigueLoss(const MWWorld::Ptr& attacker, const MWWorld::Ptr& weapon, float attackStrength) { + // Ashlar: with real time combat, the cost is paid when the swing is released (see stamina.hpp). + if (usesStaminaAttacks()) + return; + // somewhat of a guess, but using the weapon weight makes sense const MWWorld::Store& store = MWBase::Environment::get().getESMStore()->get(); diff --git a/apps/openmw/mwmechanics/stamina.cpp b/apps/openmw/mwmechanics/stamina.cpp new file mode 100644 index 0000000000..9f87dc324a --- /dev/null +++ b/apps/openmw/mwmechanics/stamina.cpp @@ -0,0 +1,94 @@ +#include "stamina.hpp" + +#include + +#include +#include + +#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& 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& 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& store + = MWBase::Environment::get().getESMStore()->get(); + 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 fatigue = stats.getFatigue(); + fatigue.setCurrent(fatigue.getCurrent() - cost); + stats.setFatigue(fatigue); + } +} diff --git a/apps/openmw/mwmechanics/stamina.hpp b/apps/openmw/mwmechanics/stamina.hpp new file mode 100644 index 0000000000..624095f247 --- /dev/null +++ b/apps/openmw/mwmechanics/stamina.hpp @@ -0,0 +1,46 @@ +#ifndef OPENMW_MECHANICS_STAMINA_H +#define OPENMW_MECHANICS_STAMINA_H + +namespace MWWorld +{ + class Ptr; +} + +namespace MWMechanics +{ + // Ashlar stamina-driven attacks. With [Game] real time combat on, an attack's fatigue cost is paid when the swing + // is released and scales with wind-up strength and weapon weight. An actor below [Game] attack fatigue threshold + // can't start an attack, and below [Game] attack fatigue cap its maximum attack strength drops. + + /// Lowest maximum attack strength, reached at zero fatigue. + constexpr float sMinAttackStrengthCap = 0.25f; + + /// Fatigue cost of a real-time attack. The base part (\a base plus \a normalizedEncumbrance times + /// \a encumbranceMult) costs half at no wind-up and in full at full wind-up; the weapon part is + /// \a weaponWeight times \a attackStrength times \a weaponMult, as in the classic formula. + float getAttackFatigueCost(float base, float encumbranceMult, float normalizedEncumbrance, float weaponWeight, + float weaponMult, float attackStrength); + + /// True if an actor with \a currentFatigue out of \a maxFatigue may start an attack. Attacks need positive + /// fatigue and, when \a maxFatigue is positive, a fatigue ratio of at least \a threshold. + bool hasFatigueToAttack(float currentFatigue, float maxFatigue, float threshold); + + /// Highest attack strength allowed at \a currentFatigue out of \a maxFatigue. 1 at or above a ratio of + /// \a capRatio, falling linearly to sMinAttackStrengthCap at zero fatigue. A \a capRatio of 0 disables the cap. + float getAttackStrengthCap(float currentFatigue, float maxFatigue, float capRatio); + + /// True if \a actor uses the stamina-driven attack rules (real time combat on). + bool usesStaminaAttacks(); + + /// True if \a actor is too tired to start a weapon attack under the stamina rules. Always false with the rules off + /// or in god mode for the player. + bool isTooTiredToAttack(const MWWorld::Ptr& actor); + + /// Caps \a attackStrength by \a actor's current fatigue under the stamina rules. + float capAttackStrength(const MWWorld::Ptr& actor, float attackStrength); + + /// Pays the fatigue cost of a released real-time attack with \a weapon (empty for hand-to-hand or natural attacks). + void applyAttackFatigueCost(const MWWorld::Ptr& attacker, const MWWorld::Ptr& weapon, float attackStrength); +} + +#endif diff --git a/apps/openmw_tests/CMakeLists.txt b/apps/openmw_tests/CMakeLists.txt index fb47ff2368..7de5f7ec8b 100644 --- a/apps/openmw_tests/CMakeLists.txt +++ b/apps/openmw_tests/CMakeLists.txt @@ -24,6 +24,7 @@ file(GLOB UNITTEST_SRC_FILES mwmechanics/testactiveblock.cpp mwmechanics/testaiblock.cpp + mwmechanics/teststamina.cpp mwmechanics/testcombat.cpp mwscript/testscripts.cpp diff --git a/apps/openmw_tests/mwmechanics/teststamina.cpp b/apps/openmw_tests/mwmechanics/teststamina.cpp new file mode 100644 index 0000000000..b88e8ed37b --- /dev/null +++ b/apps/openmw_tests/mwmechanics/teststamina.cpp @@ -0,0 +1,85 @@ +#include + +#include + +namespace +{ + using namespace MWMechanics; + + // Morrowind-style GMST defaults: fFatigueAttackBase 2, fFatigueAttackMult 0, fWeaponFatigueMult 1. + constexpr float base = 2.f; + constexpr float encumbranceMult = 0.f; + constexpr float weaponMult = 1.f; + + TEST(MWMechanicsStaminaTest, fullWindUpCostsTheClassicFullStrengthCost) + { + EXPECT_FLOAT_EQ(getAttackFatigueCost(base, encumbranceMult, 0.5f, 20.f, weaponMult, 1.f), 22.f); + } + + TEST(MWMechanicsStaminaTest, quickTapCostsHalfTheBaseAndNothingForTheWeapon) + { + EXPECT_FLOAT_EQ(getAttackFatigueCost(base, encumbranceMult, 0.5f, 20.f, weaponMult, 0.f), 1.f); + } + + TEST(MWMechanicsStaminaTest, costGrowsWithWindUpAndWeaponWeight) + { + const float light = getAttackFatigueCost(base, encumbranceMult, 0.f, 5.f, weaponMult, 0.5f); + const float heavy = getAttackFatigueCost(base, encumbranceMult, 0.f, 30.f, weaponMult, 0.5f); + const float heavyFull = getAttackFatigueCost(base, encumbranceMult, 0.f, 30.f, weaponMult, 1.f); + EXPECT_LT(light, heavy); + EXPECT_LT(heavy, heavyFull); + EXPECT_FLOAT_EQ(heavy, 1.5f + 15.f); + } + + TEST(MWMechanicsStaminaTest, encumbranceAddsToTheBaseCost) + { + EXPECT_FLOAT_EQ(getAttackFatigueCost(2.f, 4.f, 0.5f, 0.f, weaponMult, 1.f), 4.f); + EXPECT_FLOAT_EQ(getAttackFatigueCost(2.f, 4.f, 0.5f, 0.f, weaponMult, 0.f), 2.f); + } + + TEST(MWMechanicsStaminaTest, costClampsStrengthAndIsNeverNegative) + { + EXPECT_FLOAT_EQ(getAttackFatigueCost(base, encumbranceMult, 0.f, 10.f, weaponMult, 2.f), + getAttackFatigueCost(base, encumbranceMult, 0.f, 10.f, weaponMult, 1.f)); + EXPECT_FLOAT_EQ(getAttackFatigueCost(-5.f, 0.f, 0.f, 0.f, weaponMult, 1.f), 0.f); + } + + TEST(MWMechanicsStaminaTest, thresholdBlocksAttacksBelowTheShare) + { + EXPECT_TRUE(hasFatigueToAttack(10.f, 100.f, 0.1f)); + EXPECT_FALSE(hasFatigueToAttack(9.9f, 100.f, 0.1f)); + EXPECT_TRUE(hasFatigueToAttack(100.f, 100.f, 0.1f)); + } + + TEST(MWMechanicsStaminaTest, noAttacksAtZeroOrNegativeFatigueEvenWithNoThreshold) + { + EXPECT_FALSE(hasFatigueToAttack(0.f, 100.f, 0.f)); + EXPECT_FALSE(hasFatigueToAttack(-3.f, 100.f, 0.f)); + EXPECT_TRUE(hasFatigueToAttack(0.1f, 100.f, 0.f)); + } + + TEST(MWMechanicsStaminaTest, zeroMaxFatigueOnlyNeedsPositiveFatigue) + { + EXPECT_TRUE(hasFatigueToAttack(1.f, 0.f, 0.5f)); + EXPECT_FALSE(hasFatigueToAttack(0.f, 0.f, 0.5f)); + } + + TEST(MWMechanicsStaminaTest, capIsFullAboveTheCapShare) + { + EXPECT_FLOAT_EQ(getAttackStrengthCap(100.f, 100.f, 0.5f), 1.f); + EXPECT_FLOAT_EQ(getAttackStrengthCap(50.f, 100.f, 0.5f), 1.f); + } + + TEST(MWMechanicsStaminaTest, capFallsLinearlyToAQuarterAtZeroFatigue) + { + EXPECT_FLOAT_EQ(getAttackStrengthCap(0.f, 100.f, 0.5f), sMinAttackStrengthCap); + EXPECT_FLOAT_EQ(getAttackStrengthCap(25.f, 100.f, 0.5f), 0.625f); + EXPECT_FLOAT_EQ(getAttackStrengthCap(-10.f, 100.f, 0.5f), sMinAttackStrengthCap); + } + + TEST(MWMechanicsStaminaTest, capDisabledByZeroShareOrZeroMaxFatigue) + { + EXPECT_FLOAT_EQ(getAttackStrengthCap(0.f, 100.f, 0.f), 1.f); + EXPECT_FLOAT_EQ(getAttackStrengthCap(0.f, 0.f, 0.5f), 1.f); + } +} diff --git a/components/settings/categories/game.hpp b/components/settings/categories/game.hpp index 0b9b66b64d..7410ab8e7d 100644 --- a/components/settings/categories/game.hpp +++ b/components/settings/categories/game.hpp @@ -82,6 +82,9 @@ namespace Settings SettingValue mWeaponBlockDamage{ mIndex, "Game", "weapon block damage", makeClampSanitizerFloat(0, 1) }; SettingValue mAiActiveBlocking{ mIndex, "Game", "ai active blocking" }; + SettingValue mAttackFatigueThreshold{ mIndex, "Game", "attack fatigue threshold", + makeClampSanitizerFloat(0, 1) }; + SettingValue mAttackFatigueCap{ mIndex, "Game", "attack fatigue cap", makeClampSanitizerFloat(0, 1) }; }; } diff --git a/docs/source/reference/modding/settings/game.rst b/docs/source/reference/modding/settings/game.rst index eecd7e635e..048fce54a7 100644 --- a/docs/source/reference/modding/settings/game.rst +++ b/docs/source/reference/modding/settings/game.rst @@ -563,4 +563,30 @@ Game Settings agility, rising with Block skill and a fifth of agility to at most 90%, and scaled by fatigue. The reaction delay falls from 0.6 to 0.15 seconds over the same range. Actors without a shield never block, and a creature blocks only if it can carry a shield. + +.. omw-setting:: + :title: attack fatigue threshold + :type: float32 + :range: [0, 1] + :default: 0.1 + + Ashlar setting. Share of maximum fatigue below which an actor can't start a weapon attack, with real time combat on. + An actor at zero fatigue or below can never attack. The player gets a "too tired to attack" message once per press, + and NPCs and creatures wait for their fatigue to recover instead of trying to attack. Spells aren't affected. + + With real time combat on, the fatigue cost of a weapon attack is paid when the swing is released rather than when it + lands, so a swing costs fatigue even if it is interrupted or misses. The cost is + ``(fFatigueAttackBase + fFatigueAttackMult * normalized encumbrance) * (0.5 + 0.5 * strength)`` plus + ``weapon weight * strength * fWeaponFatigueMult``, where strength is the wind-up from 0 to 1. A quick tap costs half + the base cost and nothing for the weapon; a full wind-up costs the same as a classic full-strength attack. + +.. omw-setting:: + :title: attack fatigue cap + :type: float32 + :range: [0, 1] + :default: 0.5 + + Ashlar setting. Share of maximum fatigue below which an actor's strongest possible attack shrinks, with real time + combat on. The cap falls linearly from full strength at this share to a quarter strength at zero fatigue, so a tired + actor hits for less and its attacks cost less, instead of only losing accuracy. 0 disables the cap. Disable to give NPCs and creatures the classic block roll. Only used when real time combat is enabled. diff --git a/files/settings-default.cfg b/files/settings-default.cfg index eef7c85078..38da8a851d 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -411,6 +411,15 @@ weapon block damage = 0.5 # melee attack. Disable to give them the classic block roll instead. ai active blocking = true +# Ashlar: with real time combat on, an attack can't start while fatigue is +# below this share of maximum fatigue (or at zero). The player is told why. +attack fatigue threshold = 0.1 + +# Ashlar: with real time combat on, below this share of maximum fatigue the +# strongest possible attack shrinks, down to a quarter strength at zero +# fatigue. 0 disables the cap. +attack fatigue cap = 0.5 + [General] # Anisotropy reduces distortion in textures at low angles (e.g. 0 to 16).