Files
claudeandClaude Opus 5.5 2c5f6e4e7d
Ashlar checks / tooling (push) Successful in 5s
Add hit reactions for real time combat
With [Game] real time combat and the new [Game] hit reactions setting on
(default), melee hits cause short reactions:

- A connecting hit queues a flinch of 0.2 to 0.7 s, growing with damage
  up to a quarter of max health and halved at zero attack strength. It
  starts when the hit's stagger is applied, so Lua's ignoreStagger and
  god mode prevent it.
- A blocked hit or parry makes the blocker recoil for 0.1 to 0.25 s, and
  a blocked blow at 0.75 strength or more staggers the attacker for 0.25
  to 0.6 s.
- During any reaction, an attack wind-up is interrupted and no new attack
  starts. Hit animations still play where the content has them; the
  timer works without them, as on the Game Template.

The rules are in mwmechanics/hitreaction.cpp. The shield block and parry
resolution moves into resolveMeleeBlock, shared by NPCs and creatures.
The combat scenario now counts reactions. Also moves a stray line of the
ai active blocking docs back under that setting.

Closes #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 03:50:14 -05:00

86 lines
3.3 KiB
C++

#include "hitreaction.hpp"
#include <algorithm>
#include <components/debug/debuglog.hpp>
#include <components/settings/values.hpp>
#include "../mwworld/class.hpp"
#include "../mwworld/ptr.hpp"
#include "activeblock.hpp"
#include "combat.hpp"
#include "creaturestats.hpp"
namespace MWMechanics
{
float getFlinchDuration(float damage, float maxHealth, float attackStrength)
{
if (damage <= 0.f)
return 0.f;
const float ratio = std::clamp(damage / std::max(maxHealth, 1.f) / sFullFlinchDamageRatio, 0.f, 1.f);
const float strength = std::clamp(attackStrength, 0.f, 1.f);
return (sMinFlinch + (sMaxFlinch - sMinFlinch) * ratio) * (0.5f + 0.5f * strength);
}
float getBlockRecoilDuration(float attackStrength)
{
return 0.1f + 0.15f * std::clamp(attackStrength, 0.f, 1.f);
}
float getBlockedAttackerStaggerDuration(float attackStrength)
{
const float strength = std::clamp(attackStrength, 0.f, 1.f);
if (strength < sStaggerStrength)
return 0.f;
return 0.25f + 0.35f * (strength - sStaggerStrength) / (1.f - sStaggerStrength);
}
bool usesHitReactions()
{
return Settings::game().mRealTimeCombat && Settings::game().mHitReactions;
}
void queueFlinch(const MWWorld::Ptr& victim, float damage, float attackStrength)
{
if (!usesHitReactions() || victim.isEmpty() || !victim.getClass().isActor())
return;
CreatureStats& stats = victim.getClass().getCreatureStats(victim);
stats.setPendingFlinch(getFlinchDuration(damage, stats.getHealth().getModified(), attackStrength));
}
float resolveMeleeBlock(const MWWorld::Ptr& attacker, const MWWorld::Ptr& victim, const MWWorld::Ptr& weapon,
float damage, float attackStrength)
{
if (blockMeleeAttack(attacker, victim, weapon, damage, attackStrength))
{
applyBlockReactions(attacker, victim, attackStrength);
return 0.f;
}
const float damageThrough = weaponBlockMeleeAttack(attacker, victim, weapon, damage, attackStrength);
if (damageThrough < damage)
applyBlockReactions(attacker, victim, attackStrength);
else
queueFlinch(victim, damage, attackStrength);
return damageThrough;
}
void applyBlockReactions(const MWWorld::Ptr& attacker, const MWWorld::Ptr& blocker, float attackStrength)
{
if (!usesHitReactions())
return;
const float recoil = getBlockRecoilDuration(attackStrength);
CreatureStats& blockerStats = blocker.getClass().getCreatureStats(blocker);
blockerStats.setReactionTime(std::max(blockerStats.getReactionTime(), recoil));
const float stagger = getBlockedAttackerStaggerDuration(attackStrength);
if (stagger > 0.f && !attacker.isEmpty() && attacker.getClass().isActor())
{
CreatureStats& attackerStats = attacker.getClass().getCreatureStats(attacker);
attackerStats.setReactionTime(std::max(attackerStats.getReactionTime(), stagger));
}
Log(Debug::Verbose) << "Ashlar reaction: " << blocker.getCellRef().getRefId() << " recoils " << recoil
<< "s, " << attacker.getCellRef().getRefId() << " staggers " << stagger << "s";
}
}