From 27e35b3ac8e83c5339704ee43ca9ac8b3aba9488 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 14:42:58 -0500 Subject: [PATCH] Add real-time combat: connecting attacks always hit With the new [Game] 'real time combat' setting (on by default), melee and ranged attacks that physically connect always hit. The classic hit chance becomes a damage multiplier clamped to [0.25, 1], so weapon skill, agility, luck, fatigue and the target's evasion still matter and average damage stays near classic balance. Applies to NPCs, creatures and projectiles. Set it to false for Morrowind's hit and miss rolls. Refs #5 Co-Authored-By: Claude Opus 5.5 --- apps/openmw/mwclass/creature.cpp | 6 +++++ apps/openmw/mwclass/npc.cpp | 11 ++++++++ apps/openmw/mwmechanics/combat.cpp | 17 +++++++++++- apps/openmw/mwmechanics/combat.hpp | 7 +++++ apps/openmw_tests/CMakeLists.txt | 2 ++ apps/openmw_tests/mwmechanics/testcombat.cpp | 27 +++++++++++++++++++ components/settings/categories/game.hpp | 2 ++ .../reference/modding/settings/game.rst | 11 ++++++++ files/settings-default.cfg | 5 ++++ 9 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 apps/openmw_tests/mwmechanics/testcombat.cpp diff --git a/apps/openmw/mwclass/creature.cpp b/apps/openmw/mwclass/creature.cpp index b5b1ff15de..fcd1b7b2db 100644 --- a/apps/openmw/mwclass/creature.cpp +++ b/apps/openmw/mwclass/creature.cpp @@ -251,6 +251,10 @@ namespace MWClass victim = result.first; hitPosition = result.second; + // Ashlar: a swing that connects always hits; hit chance scales damage in hit() instead. + if (Settings::game().mRealTimeCombat) + return true; + float hitchance = MWMechanics::getHitChance(ptr, victim, ptr.get()->mBase->mData.mCombat); return Misc::Rng::roll0to99(world->getPrng()) < hitchance; } @@ -339,6 +343,8 @@ namespace MWClass MWMechanics::getHandToHandDamage(ptr, victim, damage, healthdmg, attackStrength); } + damage *= MWMechanics::getRealTimeDamageScale(ptr, victim, ref->mBase->mData.mCombat); + MWMechanics::applyElementalShields(ptr, victim); if (MWMechanics::blockMeleeAttack(ptr, victim, weapon, damage, attackStrength)) diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index b1b298196a..3fdb5eb83a 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -561,6 +561,10 @@ namespace MWClass victim = result.first; hitPosition = result.second; + // Ashlar: a swing that connects always hits; hit chance scales damage in hit() instead. + if (Settings::game().mRealTimeCombat) + return true; + ESM::RefId weapskill = ESM::Skill::HandToHand; if (!weapon.isEmpty()) weapskill = weapon.getClass().getEquipmentSkill(weapon); @@ -630,6 +634,13 @@ namespace MWClass MWMechanics::getHandToHandDamage(ptr, victim, damage, healthdmg, attackStrength); } + { + ESM::RefId skill = ESM::Skill::HandToHand; + if (!weapon.isEmpty()) + skill = weapon.getClass().getEquipmentSkill(weapon); + damage *= MWMechanics::getRealTimeDamageScale(ptr, victim, static_cast(getSkill(ptr, skill))); + } + MWBase::World* world = MWBase::Environment::get().getWorld(); const MWWorld::Store& store = world->getStore().get(); diff --git a/apps/openmw/mwmechanics/combat.cpp b/apps/openmw/mwmechanics/combat.cpp index 194275de5d..94bd047911 100644 --- a/apps/openmw/mwmechanics/combat.cpp +++ b/apps/openmw/mwmechanics/combat.cpp @@ -1,6 +1,7 @@ #include "combat.hpp" +#include #include #include @@ -241,7 +242,8 @@ namespace MWMechanics int skillValue = static_cast(attacker.getClass().getSkill(attacker, weaponSkill)); - if (Misc::Rng::roll0to99(world->getPrng()) >= getHitChance(attacker, victim, skillValue)) + if (!Settings::game().mRealTimeCombat + && Misc::Rng::roll0to99(world->getPrng()) >= getHitChance(attacker, victim, skillValue)) { MWBase::Environment::get().getLuaManager()->onHit(attacker, victim, weapon, projectile, 0, attackStrength, attackWindUp, damage, false, hitPosition, false, @@ -261,6 +263,7 @@ namespace MWMechanics damage += attack[0] + ((attack[1] - attack[0]) * attackStrength); } adjustWeaponDamage(damage, weapon, attacker); + damage *= getRealTimeDamageScale(attacker, victim, skillValue); } reduceWeaponCondition(damage, validVictim, weapon, attacker); @@ -349,6 +352,18 @@ namespace MWMechanics return round(attackTerm - defenseTerm); } + float hitChanceToDamageScale(float hitChance) + { + return std::clamp(hitChance / 100.f, 0.25f, 1.f); + } + + float getRealTimeDamageScale(const MWWorld::Ptr& attacker, const MWWorld::Ptr& victim, int skillValue) + { + if (!Settings::game().mRealTimeCombat) + return 1.f; + return hitChanceToDamageScale(getHitChance(attacker, victim, skillValue)); + } + void applyElementalShields(const MWWorld::Ptr& attacker, const MWWorld::Ptr& victim) { // Don't let elemental shields harm the player in god mode. diff --git a/apps/openmw/mwmechanics/combat.hpp b/apps/openmw/mwmechanics/combat.hpp index 5f2200abd6..bf5bcf11cf 100644 --- a/apps/openmw/mwmechanics/combat.hpp +++ b/apps/openmw/mwmechanics/combat.hpp @@ -39,6 +39,13 @@ namespace MWMechanics /// Get the chance (in percent) for \a attacker to successfully hit \a victim with a given weapon skill value float getHitChance(const MWWorld::Ptr& attacker, const MWWorld::Ptr& victim, int skillValue); + /// Ashlar real-time combat: converts a classic hit chance (in percent) into a damage multiplier. + /// It keeps average damage close to classic combat, with a floor so weak hits still count. + float hitChanceToDamageScale(float hitChance); + + /// Ashlar real-time combat: damage multiplier for a hit that connected, or 1 when the setting is off. + float getRealTimeDamageScale(const MWWorld::Ptr& attacker, const MWWorld::Ptr& victim, int skillValue); + /// Applies damage to attacker based on the victim's elemental shields. void applyElementalShields(const MWWorld::Ptr& attacker, const MWWorld::Ptr& victim); diff --git a/apps/openmw_tests/CMakeLists.txt b/apps/openmw_tests/CMakeLists.txt index 2a503daf14..818619b81f 100644 --- a/apps/openmw_tests/CMakeLists.txt +++ b/apps/openmw_tests/CMakeLists.txt @@ -22,6 +22,8 @@ file(GLOB UNITTEST_SRC_FILES mwlua/testobjectcache.cpp mwlua/testmwscriptbindings.cpp + mwmechanics/testcombat.cpp + mwscript/testscripts.cpp mwsound/testheadcache.cpp diff --git a/apps/openmw_tests/mwmechanics/testcombat.cpp b/apps/openmw_tests/mwmechanics/testcombat.cpp new file mode 100644 index 0000000000..f4afe180b5 --- /dev/null +++ b/apps/openmw_tests/mwmechanics/testcombat.cpp @@ -0,0 +1,27 @@ +#include + +#include + +namespace +{ + using MWMechanics::hitChanceToDamageScale; + + TEST(MWMechanicsCombatTest, hitChanceToDamageScaleKeepsAverageDamageInRange) + { + EXPECT_FLOAT_EQ(hitChanceToDamageScale(50.f), 0.5f); + EXPECT_FLOAT_EQ(hitChanceToDamageScale(80.f), 0.8f); + } + + TEST(MWMechanicsCombatTest, hitChanceToDamageScaleIsFullDamageForCertainHits) + { + EXPECT_FLOAT_EQ(hitChanceToDamageScale(100.f), 1.f); + EXPECT_FLOAT_EQ(hitChanceToDamageScale(180.f), 1.f); + } + + TEST(MWMechanicsCombatTest, hitChanceToDamageScaleHasFloorForUnlikelyHits) + { + EXPECT_FLOAT_EQ(hitChanceToDamageScale(10.f), 0.25f); + EXPECT_FLOAT_EQ(hitChanceToDamageScale(0.f), 0.25f); + EXPECT_FLOAT_EQ(hitChanceToDamageScale(-40.f), 0.25f); + } +} diff --git a/components/settings/categories/game.hpp b/components/settings/categories/game.hpp index 1f6dbed03a..7fd7c1c59c 100644 --- a/components/settings/categories/game.hpp +++ b/components/settings/categories/game.hpp @@ -75,6 +75,8 @@ namespace Settings "actor collision shape type" }; SettingValue mPlayerMovementIgnoresAnimation{ mIndex, "Game", "player movement ignores animation" }; SettingValue mTerrainObstructsFocus{ mIndex, "Game", "terrain obstructs focus" }; + // Ashlar + SettingValue mRealTimeCombat{ mIndex, "Game", "real time combat" }; }; } diff --git a/docs/source/reference/modding/settings/game.rst b/docs/source/reference/modding/settings/game.rst index 66a9d012e0..db70da0efd 100644 --- a/docs/source/reference/modding/settings/game.rst +++ b/docs/source/reference/modding/settings/game.rst @@ -511,3 +511,14 @@ Game Settings If enabled, focused object selection will account for terrain. The default behavior matches Morrowind, allowing objects partially or totally submerged in terrain to gain focus. + +.. omw-setting:: + :title: real time combat + :type: boolean + :range: true, false + :default: true + + Ashlar setting. If enabled, melee and ranged attacks that physically connect always hit. + The classic hit chance instead scales their damage, between 25% and 100%, + so weapon skill, agility, luck, fatigue and the target's evasion still matter. + Disable for Morrowind's hit and miss rolls. diff --git a/files/settings-default.cfg b/files/settings-default.cfg index a8e521948a..fec4777599 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -393,6 +393,11 @@ player movement ignores animation = false # Makes focused object selection account for terrain. terrain obstructs focus = false +# Ashlar: attacks that physically connect always hit. The classic hit +# chance scales their damage instead, so skill and evasion still matter. +# Set to false for classic hit and miss rolls. +real time combat = true + [General] # Anisotropy reduces distortion in textures at low angles (e.g. 0 to 16).