Ashlar checks / tooling (push) Successful in 6s
SCENARIO=combat docker/ashlar-run.sh loads docker/scenarios/combat on top
of the Game Template: our own CC0 Lua content that gives the player a
shield and blade and spawns a hostile duelist with the same kit, cloned
from the template's player record. The player holds Block, then attacks,
and the run prints block counts. The default boot is unchanged.
The engine now logs each successful shield block or parry at verbose
level ("Ashlar block: ..."), so runs can count them.
Documented in docs/ashlar/SCENARIOS.md.
Closes #24
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
7.4 KiB
C++
168 lines
7.4 KiB
C++
#include "activeblock.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
#include <components/esm3/loadarmo.hpp>
|
|
#include <components/esm3/loadgmst.hpp>
|
|
#include <components/debug/debuglog.hpp>
|
|
#include <components/esm3/loadweap.hpp>
|
|
#include <components/sceneutil/positionattitudetransform.hpp>
|
|
#include <components/settings/values.hpp>
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
|
#include "../mwbase/soundmanager.hpp"
|
|
|
|
#include "../mwworld/class.hpp"
|
|
#include "../mwworld/esmstore.hpp"
|
|
#include "../mwworld/inventorystore.hpp"
|
|
|
|
#include "actorutil.hpp"
|
|
#include "creaturestats.hpp"
|
|
#include "weapontype.hpp"
|
|
|
|
namespace MWMechanics
|
|
{
|
|
namespace
|
|
{
|
|
bool hasMeleeWeaponEquipped(const MWWorld::Ptr& actor)
|
|
{
|
|
const MWWorld::InventoryStore& inv = actor.getClass().getInventoryStore(actor);
|
|
const auto weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
|
|
if (weapon == inv.end() || weapon->getType() != ESM::Weapon::sRecordId)
|
|
return false;
|
|
const ESM::Weapon* record = weapon->get<ESM::Weapon>()->mBase;
|
|
return getWeaponType(record->mData.mType)->mWeaponClass == ESM::WeaponType::Melee;
|
|
}
|
|
|
|
}
|
|
|
|
float getAttackAngleDegrees(const MWWorld::Ptr& attacker, const MWWorld::Ptr& blocker)
|
|
{
|
|
const osg::Vec3f toAttacker
|
|
= attacker.getRefData().getPosition().asVec3() - blocker.getRefData().getPosition().asVec3();
|
|
const osg::Vec3f facing = blocker.getRefData().getBaseNode()->getAttitude() * osg::Vec3f(0, 1, 0);
|
|
const osg::Vec3f up(0, 0, 1);
|
|
return osg::RadiansToDegrees(std::atan2(up * (toAttacker ^ facing), toAttacker * facing));
|
|
}
|
|
|
|
bool isInBlockArc(float angleDegrees, float leftAngle, float rightAngle)
|
|
{
|
|
return angleDegrees >= leftAngle && angleDegrees <= rightAngle;
|
|
}
|
|
|
|
bool canHoldBlockStance(bool blockHeld, bool weaponDrawn, bool canBlockWith, bool incapacitated)
|
|
{
|
|
return blockHeld && weaponDrawn && canBlockWith && !incapacitated;
|
|
}
|
|
|
|
float scaleBlockFatigueLoss(float fatigueLoss, float blockSkill)
|
|
{
|
|
const float skill = std::clamp(blockSkill, 0.f, 100.f);
|
|
return fatigueLoss * (1.f - 0.5f * skill / 100.f);
|
|
}
|
|
|
|
float weaponBlockDamage(float damage, float damageTaken)
|
|
{
|
|
return damage * std::clamp(damageTaken, 0.f, 1.f);
|
|
}
|
|
|
|
bool usesActiveBlocking(const MWWorld::Ptr& actor)
|
|
{
|
|
// NPCs and creatures use the stance too unless [Game] ai active blocking is off (see aiblock.cpp).
|
|
return Settings::game().mRealTimeCombat && (actor == getPlayer() || Settings::game().mAiActiveBlocking);
|
|
}
|
|
|
|
bool hasShieldReady(const MWWorld::Ptr& actor)
|
|
{
|
|
if (!actor.getClass().hasInventoryStore(actor))
|
|
return false;
|
|
const MWWorld::InventoryStore& inv = actor.getClass().getInventoryStore(actor);
|
|
const auto shield = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft);
|
|
return shield != inv.end() && shield->getType() == ESM::Armor::sRecordId
|
|
&& MWBase::Environment::get().getMechanicsManager()->isReadyToBlock(actor);
|
|
}
|
|
|
|
bool isInBlockStance(const MWWorld::Ptr& actor)
|
|
{
|
|
if (!actor.getClass().isActor())
|
|
return false;
|
|
const CreatureStats& stats = actor.getClass().getCreatureStats(actor);
|
|
if (!(stats.getBlockHeld() || stats.getAiBlockHeld()) || !usesActiveBlocking(actor))
|
|
return false;
|
|
if (!actor.getClass().hasInventoryStore(actor))
|
|
return false;
|
|
|
|
const bool canBlockWith = hasShieldReady(actor) || hasMeleeWeaponEquipped(actor);
|
|
const bool incapacitated
|
|
= stats.getKnockedDown() || stats.getHitRecovery() || stats.isParalyzed() || stats.isDead();
|
|
|
|
return canHoldBlockStance(true, stats.getDrawState() == DrawState::Weapon, canBlockWith, incapacitated);
|
|
}
|
|
|
|
float weaponBlockMeleeAttack(
|
|
const MWWorld::Ptr& attacker, const MWWorld::Ptr& blocker, const MWWorld::Ptr& weapon, float damage,
|
|
float attackStrength)
|
|
{
|
|
if (damage <= 0.f || !usesActiveBlocking(blocker) || !isInBlockStance(blocker) || hasShieldReady(blocker))
|
|
return damage;
|
|
if (!blocker.getRefData().getBaseNode())
|
|
return damage;
|
|
|
|
const MWWorld::Store<ESM::GameSetting>& gmst
|
|
= MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
|
|
static const float fCombatBlockLeftAngle = gmst.find("fCombatBlockLeftAngle")->mValue.getFloat();
|
|
static const float fCombatBlockRightAngle = gmst.find("fCombatBlockRightAngle")->mValue.getFloat();
|
|
if (!isInBlockArc(getAttackAngleDegrees(attacker, blocker), fCombatBlockLeftAngle, fCombatBlockRightAngle))
|
|
return damage;
|
|
|
|
MWWorld::InventoryStore& inv = blocker.getClass().getInventoryStore(blocker);
|
|
MWWorld::ContainerStoreIterator ownWeapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
|
|
if (ownWeapon == inv.end())
|
|
return damage;
|
|
|
|
const float damageThrough = weaponBlockDamage(damage, Settings::game().mWeaponBlockDamage);
|
|
|
|
MWBase::Environment::get().getSoundManager()->playSound3D(
|
|
blocker, ESM::RefId::stringRefId("Medium Armor Hit"), 1.0f, 1.0f);
|
|
|
|
// The parrying weapon takes the damage it stopped.
|
|
if (ownWeapon->getClass().hasItemHealth(*ownWeapon))
|
|
{
|
|
int health = ownWeapon->getClass().getItemHealth(*ownWeapon);
|
|
health -= std::min(health, static_cast<int>(damage - damageThrough));
|
|
ownWeapon->getCellRef().setCharge(health);
|
|
if (health == 0)
|
|
inv.unequipItem(*ownWeapon);
|
|
}
|
|
|
|
// Fatigue loss as for a shield block, scaled by Block skill.
|
|
CreatureStats& blockerStats = blocker.getClass().getCreatureStats(blocker);
|
|
static const float fFatigueBlockBase = gmst.find("fFatigueBlockBase")->mValue.getFloat();
|
|
static const float fFatigueBlockMult = gmst.find("fFatigueBlockMult")->mValue.getFloat();
|
|
static const float fWeaponFatigueBlockMult = gmst.find("fWeaponFatigueBlockMult")->mValue.getFloat();
|
|
const float normalizedEncumbrance = std::min(1.f, blocker.getClass().getNormalizedEncumbrance(blocker));
|
|
float fatigueLoss = fFatigueBlockBase + normalizedEncumbrance * fFatigueBlockMult;
|
|
if (!weapon.isEmpty())
|
|
fatigueLoss += weapon.getClass().getWeight(weapon) * attackStrength * fWeaponFatigueBlockMult;
|
|
fatigueLoss = scaleBlockFatigueLoss(fatigueLoss, blocker.getClass().getSkill(blocker, ESM::Skill::Block));
|
|
DynamicStat<float> fatigue = blockerStats.getFatigue();
|
|
fatigue.setCurrent(fatigue.getCurrent() - fatigueLoss);
|
|
blockerStats.setFatigue(fatigue);
|
|
|
|
if (blocker == getPlayer())
|
|
blocker.getClass().skillUsageSucceeded(blocker, ESM::Skill::Block, ESM::Skill::Block_Success);
|
|
|
|
logBlock(attacker, blocker, "weapon", damageThrough);
|
|
return damageThrough;
|
|
}
|
|
|
|
void logBlock(const MWWorld::Ptr& attacker, const MWWorld::Ptr& blocker, const char* kind, float damageThrough)
|
|
{
|
|
Log(Debug::Verbose) << "Ashlar block: " << blocker.getCellRef().getRefId() << " blocked "
|
|
<< attacker.getCellRef().getRefId() << " with " << kind << ", "
|
|
<< (isInBlockStance(blocker) ? "stance" : "roll") << ", damage through " << damageThrough;
|
|
}
|
|
}
|