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

155 lines
5.6 KiB
C++

#include <apps/openmw/mwmechanics/aiblock.hpp>
#include <gtest/gtest.h>
namespace
{
using namespace MWMechanics;
constexpr float frame = 0.05f;
AiBlockParams makeParams(float chance, float reaction)
{
AiBlockParams params;
params.mChance = chance;
params.mReactionTime = reaction;
params.mHoldTime = 0.5f;
params.mMaxBlockTime = 2.f;
params.mRetryDelay = 1.f;
return params;
}
float rollZero()
{
return 0.f;
}
float rollHigh()
{
return 0.99f;
}
// Runs \a seconds of frames and returns whether the actor was blocking at the end.
bool run(AiBlockState& state, bool canBlock, bool threatened, float seconds, const AiBlockParams& params,
float (*roll)() = rollZero)
{
bool blocking = false;
for (float t = 0.f; t < seconds - 1e-4f; t += frame)
blocking = updateAiBlock(state, canBlock, threatened, frame, roll, params);
return blocking;
}
TEST(MWMechanicsAiBlockTest, chanceRisesWithBlockSkillAndAgility)
{
EXPECT_FLOAT_EQ(getAiBlockChance(0.f, 0.f, 1.f), 0.1f);
EXPECT_LT(getAiBlockChance(30.f, 40.f, 1.f), getAiBlockChance(60.f, 40.f, 1.f));
EXPECT_LT(getAiBlockChance(50.f, 20.f, 1.f), getAiBlockChance(50.f, 80.f, 1.f));
EXPECT_FLOAT_EQ(getAiBlockChance(100.f, 100.f, 1.f), 0.9f);
EXPECT_FLOAT_EQ(getAiBlockChance(500.f, 500.f, 1.f), 0.9f);
}
TEST(MWMechanicsAiBlockTest, chanceFallsWithFatigue)
{
EXPECT_FLOAT_EQ(getAiBlockChance(50.f, 50.f, 0.5f), 0.5f * getAiBlockChance(50.f, 50.f, 1.f));
EXPECT_FLOAT_EQ(getAiBlockChance(50.f, 50.f, 0.f), 0.f);
EXPECT_FLOAT_EQ(getAiBlockChance(50.f, 50.f, 2.f), getAiBlockChance(50.f, 50.f, 1.f));
}
TEST(MWMechanicsAiBlockTest, reactionGetsFasterWithSkill)
{
EXPECT_FLOAT_EQ(getAiBlockReactionTime(0.f, 0.f), 0.6f);
EXPECT_LT(getAiBlockReactionTime(80.f, 50.f), getAiBlockReactionTime(20.f, 50.f));
EXPECT_FLOAT_EQ(getAiBlockReactionTime(100.f, 0.f), 0.15f);
EXPECT_FLOAT_EQ(getAiBlockReactionTime(200.f, 200.f), 0.15f);
EXPECT_FLOAT_EQ(getAiBlockReactionTime(-20.f, -20.f), 0.6f);
}
TEST(MWMechanicsAiBlockTest, blocksAfterReactionDelay)
{
AiBlockState state;
const AiBlockParams params = makeParams(1.f, 0.3f);
EXPECT_FALSE(run(state, true, true, 0.2f, params));
EXPECT_TRUE(run(state, true, true, 0.2f, params));
}
TEST(MWMechanicsAiBlockTest, zeroReactionBlocksAtOnce)
{
AiBlockState state;
EXPECT_TRUE(updateAiBlock(state, true, true, frame, rollZero, makeParams(1.f, 0.f)));
}
TEST(MWMechanicsAiBlockTest, failedRollMeansNoBlockForThatWindUp)
{
AiBlockState state;
const AiBlockParams params = makeParams(0.5f, 0.f);
EXPECT_FALSE(run(state, true, true, 1.f, params, rollHigh));
// The roll is made once per wind-up, so a later lucky roll doesn't matter until the next one.
EXPECT_FALSE(run(state, true, true, 0.5f, params, rollZero));
// A new wind-up gets a new roll.
EXPECT_FALSE(run(state, true, false, 0.1f, params));
EXPECT_TRUE(run(state, true, true, 0.1f, params, rollZero));
}
TEST(MWMechanicsAiBlockTest, rollIsMadeOncePerWindUp)
{
AiBlockState state;
int calls = 0;
const std::function<float()> roll = [&] {
++calls;
return 0.99f;
};
const AiBlockParams params = makeParams(0.5f, 0.f);
for (int i = 0; i < 20; ++i)
updateAiBlock(state, true, true, frame, roll, params);
EXPECT_EQ(calls, 1);
updateAiBlock(state, true, false, frame, roll, params);
updateAiBlock(state, true, true, frame, roll, params);
EXPECT_EQ(calls, 2);
}
TEST(MWMechanicsAiBlockTest, holdsBlockBrieflyAfterWindUpEnds)
{
AiBlockState state;
const AiBlockParams params = makeParams(1.f, 0.f);
EXPECT_TRUE(run(state, true, true, 0.3f, params));
// The swing lands after the wind-up, so the block stays up for the hold time.
EXPECT_TRUE(run(state, true, false, 0.4f, params));
EXPECT_FALSE(run(state, true, false, 0.2f, params));
}
TEST(MWMechanicsAiBlockTest, dropsBlockWhenItCannotBlock)
{
AiBlockState state;
const AiBlockParams params = makeParams(1.f, 0.f);
EXPECT_TRUE(run(state, true, true, 0.2f, params));
EXPECT_FALSE(updateAiBlock(state, false, true, frame, rollZero, params));
EXPECT_FALSE(state.mBlocking);
}
TEST(MWMechanicsAiBlockTest, neverBlocksWithoutAThreat)
{
AiBlockState state;
EXPECT_FALSE(run(state, true, false, 3.f, makeParams(1.f, 0.f)));
}
TEST(MWMechanicsAiBlockTest, neverBlocksWhenUnable)
{
AiBlockState state;
EXPECT_FALSE(run(state, false, true, 3.f, makeParams(1.f, 0.f)));
}
TEST(MWMechanicsAiBlockTest, endlessWindUpCannotPinTheBlocker)
{
AiBlockState state;
const AiBlockParams params = makeParams(1.f, 0.f);
EXPECT_TRUE(run(state, true, true, 1.5f, params));
// After the longest block the actor drops it so it can attack, and waits before blocking again.
EXPECT_FALSE(run(state, true, true, 0.6f, params));
EXPECT_FALSE(run(state, true, true, 0.3f, params));
// The same wind-up still doesn't get a second roll; a new one does once the pause is over.
EXPECT_FALSE(run(state, true, true, 1.f, params));
EXPECT_FALSE(run(state, true, false, 0.1f, params));
EXPECT_TRUE(run(state, true, true, 0.1f, params));
}
}