Files
claudeandClaude Opus 5.5 4e152c7c4e Add active blocking for NPCs and creatures
With real time combat and the new [Game] 'ai active blocking' setting
(on by default), NPCs and creatures block through the same stance and
arc rules as the player instead of the classic roll.

In combat, an actor with a shield ready and its weapon drawn raises the
stance when an enemy in front of it winds up a melee attack within
reach. Whether it reacts is rolled once per wind-up (10% at 0 Block
skill and agility, up to 90%, scaled by fatigue), and its reaction
delay falls from 0.6 s to 0.15 s with skill. It keeps the block up for
half a second after the wind-up ends so it catches the swing, drops it
after two seconds at most so a held wind-up can't pin it, and starts
no attacks while blocking.

The decision lives in mwmechanics/aiblock.cpp. Combat AI sets a
separate, unsaved CreatureStats flag that is cleared every frame before
AI runs, so a block never outlives the combat package and a Lua block
request is left alone. Actors without a shield never block, and a
creature blocks only if it can carry a shield.

Closes #20

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

67 lines
3.4 KiB
C++

#ifndef OPENMW_MECHANICS_AIBLOCK_H
#define OPENMW_MECHANICS_AIBLOCK_H
#include <functional>
namespace MWWorld
{
class Ptr;
}
namespace MWMechanics
{
// Ashlar active blocking for NPCs and creatures. With [Game] real time combat and [Game] ai active blocking on,
// combat AI raises the same block stance the player uses when an enemy winds up a melee attack within reach.
/// Per-actor state of the AI block decision. Lives in the combat AI's temporary storage.
struct AiBlockState
{
bool mDecided = false; ///< A block was already rolled for the current enemy wind-up.
bool mWillBlock = false; ///< Result of that roll.
bool mBlocking = false; ///< The AI wants the block stance.
float mTimer = 0.f; ///< Reaction delay before blocking, then the hold time after the threat ends.
float mBlockTime = 0.f; ///< How long the current block has been held.
float mCooldown = 0.f; ///< Time before the AI may react again after giving up a long block.
};
struct AiBlockParams
{
float mChance = 0.f; ///< Probability of reacting to a wind-up, 0 to 1.
float mReactionTime = 0.f; ///< Delay between seeing a wind-up and raising the block, in seconds.
float mHoldTime = 0.5f; ///< How long the block stays up after the wind-up ends, to catch the swing.
float mMaxBlockTime = 2.f; ///< Longest block before dropping it, so a held wind-up can't pin the AI.
float mRetryDelay = 1.f; ///< Pause before reacting again after dropping a block at mMaxBlockTime.
};
/// Chance that an actor reacts to a wind-up with a block. Rises with Block skill and agility and falls with
/// fatigue: 10% at 0 skill and agility, capped at 90%.
float getAiBlockChance(float blockSkill, float agility, float fatigueTerm);
/// Reaction delay before an actor raises its block: 0.6 s at 0 skill and agility, down to 0.15 s.
float getAiBlockReactionTime(float blockSkill, float agility);
/// Advances the block decision by \a duration and returns whether the actor should hold the block stance.
/// \a canBlock is false while the actor can't block (no shield, attacking, fleeing...), which drops the block.
/// \a threatened is true while an enemy winds up a melee attack against it. \a roll returns a number in [0, 1)
/// and is called once per wind-up.
bool updateAiBlock(AiBlockState& state, bool canBlock, bool threatened, float duration,
const std::function<float()>& roll, const AiBlockParams& params);
/// True if [Game] ai active blocking applies: NPCs and creatures block only through the stance.
bool usesAiActiveBlocking();
/// True if \a attacker is winding up a melee attack that could reach \a blocker from inside its block arc.
bool isMeleeThreat(const MWWorld::Ptr& blocker, const MWWorld::Ptr& attacker);
/// True if the AI-controlled \a actor could raise a block now: active blocking applies, a shield is ready and the
/// weapon is drawn. \a busy is true while the actor's own attack is in progress.
bool canAiBlock(const MWWorld::Ptr& actor, bool busy);
/// Updates the block decision of the AI-controlled \a actor fighting \a target and sets its AI block flag.
/// Returns whether the actor is blocking.
bool updateAiCombatBlock(
const MWWorld::Ptr& actor, const MWWorld::Ptr& target, AiBlockState& state, bool busy, float duration);
}
#endif