1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-01 10:09:48 +00:00

Use string_view in the CharacterController

This commit is contained in:
Evil Eye 2022-07-26 17:23:00 +02:00
parent 45a94bdf32
commit 55134d1e31
6 changed files with 44 additions and 40 deletions

View file

@ -31,7 +31,7 @@ namespace
{ {
//chooses an attack depending on probability to avoid uniformity //chooses an attack depending on probability to avoid uniformity
std::string chooseBestAttack(const ESM::Weapon* weapon); std::string_view chooseBestAttack(const ESM::Weapon* weapon);
osg::Vec3f AimDirToMovingTarget(const MWWorld::Ptr& actor, const MWWorld::Ptr& target, const osg::Vec3f& vLastTargetPos, osg::Vec3f AimDirToMovingTarget(const MWWorld::Ptr& actor, const MWWorld::Ptr& target, const osg::Vec3f& vLastTargetPos,
float duration, int weapType, float strength); float duration, int weapType, float strength);
@ -682,10 +682,8 @@ namespace MWMechanics
namespace namespace
{ {
std::string chooseBestAttack(const ESM::Weapon* weapon) std::string_view chooseBestAttack(const ESM::Weapon* weapon)
{ {
std::string attackType;
if (weapon != nullptr) if (weapon != nullptr)
{ {
//the more damage attackType deals the more probability it has //the more damage attackType deals the more probability it has
@ -696,16 +694,13 @@ std::string chooseBestAttack(const ESM::Weapon* weapon)
auto& prng = MWBase::Environment::get().getWorld()->getPrng(); auto& prng = MWBase::Environment::get().getWorld()->getPrng();
float roll = Misc::Rng::rollClosedProbability(prng) * (slash + chop + thrust); float roll = Misc::Rng::rollClosedProbability(prng) * (slash + chop + thrust);
if(roll <= slash) if(roll <= slash)
attackType = "slash"; return "slash";
else if(roll <= (slash + thrust)) else if(roll <= (slash + thrust))
attackType = "thrust"; return "thrust";
else else
attackType = "chop"; return "chop";
} }
else return MWMechanics::CharacterController::getRandomAttackType();
attackType = MWMechanics::CharacterController::getRandomAttackType();
return attackType;
} }
osg::Vec3f AimDirToMovingTarget(const MWWorld::Ptr& actor, const MWWorld::Ptr& target, const osg::Vec3f& vLastTargetPos, osg::Vec3f AimDirToMovingTarget(const MWWorld::Ptr& actor, const MWWorld::Ptr& target, const osg::Vec3f& vLastTargetPos,

View file

@ -428,8 +428,9 @@ void CharacterController::refreshJumpAnims(JumpingState jump, bool force)
return; return;
} }
std::string weapShortGroup = getWeaponShortGroup(mWeaponType); std::string_view weapShortGroup = getWeaponShortGroup(mWeaponType);
std::string jumpAnimName = "jump" + weapShortGroup; std::string jumpAnimName = "jump";
jumpAnimName += weapShortGroup;
MWRender::Animation::BlendMask jumpmask = MWRender::Animation::BlendMask_All; MWRender::Animation::BlendMask jumpmask = MWRender::Animation::BlendMask_All;
if (!weapShortGroup.empty() && !mAnimation->hasAnimation(jumpAnimName)) if (!weapShortGroup.empty() && !mAnimation->hasAnimation(jumpAnimName))
jumpAnimName = fallbackShortWeaponGroup("jump", &jumpmask); jumpAnimName = fallbackShortWeaponGroup("jump", &jumpmask);
@ -490,13 +491,13 @@ void CharacterController::onClose() const
} }
} }
std::string CharacterController::getWeaponAnimation(int weaponType) const std::string_view CharacterController::getWeaponAnimation(int weaponType) const
{ {
std::string weaponGroup = getWeaponType(weaponType)->mLongGroup; std::string_view weaponGroup = getWeaponType(weaponType)->mLongGroup;
if (isRealWeapon(weaponType) && !mAnimation->hasAnimation(weaponGroup)) if (isRealWeapon(weaponType) && !mAnimation->hasAnimation(weaponGroup))
{ {
static const std::string oneHandFallback = getWeaponType(ESM::Weapon::LongBladeOneHand)->mLongGroup; static const std::string_view oneHandFallback = getWeaponType(ESM::Weapon::LongBladeOneHand)->mLongGroup;
static const std::string twoHandFallback = getWeaponType(ESM::Weapon::LongBladeTwoHand)->mLongGroup; static const std::string_view twoHandFallback = getWeaponType(ESM::Weapon::LongBladeTwoHand)->mLongGroup;
const ESM::WeaponType* weapInfo = getWeaponType(weaponType); const ESM::WeaponType* weapInfo = getWeaponType(weaponType);
@ -512,7 +513,7 @@ std::string CharacterController::getWeaponAnimation(int weaponType) const
return weaponGroup; return weaponGroup;
} }
std::string CharacterController::getWeaponShortGroup(int weaponType) const std::string_view CharacterController::getWeaponShortGroup(int weaponType) const
{ {
if (weaponType == ESM::Weapon::HandToHand && !mPtr.getClass().isBipedal(mPtr)) if (weaponType == ESM::Weapon::HandToHand && !mPtr.getClass().isBipedal(mPtr))
return {}; return {};
@ -529,8 +530,8 @@ std::string CharacterController::fallbackShortWeaponGroup(const std::string& bas
return baseGroupName; return baseGroupName;
} }
static const std::string oneHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeOneHand); static const std::string_view oneHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeOneHand);
static const std::string twoHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeTwoHand); static const std::string_view twoHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeTwoHand);
std::string groupName = baseGroupName; std::string groupName = baseGroupName;
const ESM::WeaponType* weapInfo = getWeaponType(mWeaponType); const ESM::WeaponType* weapInfo = getWeaponType(mWeaponType);
@ -583,20 +584,26 @@ void CharacterController::refreshMovementAnims(CharacterState movement, bool for
MWRender::Animation::BlendMask movemask = MWRender::Animation::BlendMask_All; MWRender::Animation::BlendMask movemask = MWRender::Animation::BlendMask_All;
std::string weapShortGroup = getWeaponShortGroup(mWeaponType); std::string_view weapShortGroup = getWeaponShortGroup(mWeaponType);
// Non-biped creatures don't use spellcasting-specific movement animations. // Non-biped creatures don't use spellcasting-specific movement animations.
if(!isRealWeapon(mWeaponType) && !mPtr.getClass().isBipedal(mPtr)) if(!isRealWeapon(mWeaponType) && !mPtr.getClass().isBipedal(mPtr))
weapShortGroup.clear(); weapShortGroup = {};
if (swimpos == std::string::npos && !weapShortGroup.empty()) if (swimpos == std::string::npos && !weapShortGroup.empty())
{ {
std::string weapMovementAnimName; std::string weapMovementAnimName;
// Spellcasting stance turning is a special case // Spellcasting stance turning is a special case
if (mWeaponType == ESM::Weapon::Spell && isTurning()) if (mWeaponType == ESM::Weapon::Spell && isTurning())
weapMovementAnimName = weapShortGroup + movementAnimName; {
weapMovementAnimName = weapShortGroup;
weapMovementAnimName += movementAnimName;
}
else else
weapMovementAnimName = movementAnimName + weapShortGroup; {
weapMovementAnimName = movementAnimName;
weapMovementAnimName += weapShortGroup;
}
if (!mAnimation->hasAnimation(weapMovementAnimName)) if (!mAnimation->hasAnimation(weapMovementAnimName))
weapMovementAnimName = fallbackShortWeaponGroup(movementAnimName, &movemask); weapMovementAnimName = fallbackShortWeaponGroup(movementAnimName, &movemask);
@ -705,10 +712,11 @@ void CharacterController::refreshIdleAnims(CharacterState idle, bool force)
if (fallback || mIdleState == CharState_Idle || mIdleState == CharState_SpecialIdle) if (fallback || mIdleState == CharState_Idle || mIdleState == CharState_SpecialIdle)
{ {
std::string weapShortGroup = getWeaponShortGroup(mWeaponType); std::string_view weapShortGroup = getWeaponShortGroup(mWeaponType);
if (!weapShortGroup.empty()) if (!weapShortGroup.empty())
{ {
std::string weapIdleGroup = idleGroup + weapShortGroup; std::string weapIdleGroup = idleGroup;
weapIdleGroup += weapShortGroup;
if (!mAnimation->hasAnimation(weapIdleGroup)) if (!mAnimation->hasAnimation(weapIdleGroup))
weapIdleGroup = fallbackShortWeaponGroup(idleGroup); weapIdleGroup = fallbackShortWeaponGroup(idleGroup);
idleGroup = weapIdleGroup; idleGroup = weapIdleGroup;
@ -2631,7 +2639,7 @@ void CharacterController::setVisibility(float visibility) const
mAnimation->setAlpha(visibility); mAnimation->setAlpha(visibility);
} }
std::string CharacterController::getMovementBasedAttackType() const std::string_view CharacterController::getMovementBasedAttackType() const
{ {
float *move = mPtr.getClass().getMovementSettings(mPtr).mPosition; float *move = mPtr.getClass().getMovementSettings(mPtr).mPosition;
if (std::abs(move[1]) > std::abs(move[0]) + 0.2f) // forward-backward if (std::abs(move[1]) > std::abs(move[0]) + 0.2f) // forward-backward
@ -2730,12 +2738,12 @@ void CharacterController::castSpell(const std::string& spellId, bool manualSpell
action.prepare(mPtr); action.prepare(mPtr);
} }
void CharacterController::setAIAttackType(const std::string& attackType) void CharacterController::setAIAttackType(std::string_view attackType)
{ {
mAttackType = attackType; mAttackType = attackType;
} }
std::string CharacterController::getRandomAttackType() std::string_view CharacterController::getRandomAttackType()
{ {
MWBase::World* world = MWBase::Environment::get().getWorld(); MWBase::World* world = MWBase::Environment::get().getWorld();
float random = Misc::Rng::rollProbability(world->getPrng()); float random = Misc::Rng::rollProbability(world->getPrng());

View file

@ -183,7 +183,7 @@ class CharacterController : public MWRender::Animation::TextKeyListener
bool mIsMovingBackward{false}; bool mIsMovingBackward{false};
osg::Vec2f mSmoothedSpeed; osg::Vec2f mSmoothedSpeed;
std::string getMovementBasedAttackType() const; std::string_view getMovementBasedAttackType() const;
void clearStateAnimation(std::string &anim) const; void clearStateAnimation(std::string &anim) const;
void resetCurrentJumpState(); void resetCurrentJumpState();
@ -227,8 +227,8 @@ class CharacterController : public MWRender::Animation::TextKeyListener
std::string fallbackShortWeaponGroup(const std::string& baseGroupName, MWRender::Animation::BlendMask* blendMask = nullptr) const; std::string fallbackShortWeaponGroup(const std::string& baseGroupName, MWRender::Animation::BlendMask* blendMask = nullptr) const;
std::string getWeaponAnimation(int weaponType) const; std::string_view getWeaponAnimation(int weaponType) const;
std::string getWeaponShortGroup(int weaponType) const; std::string_view getWeaponShortGroup(int weaponType) const;
bool getAttackingOrSpell() const; bool getAttackingOrSpell() const;
void setAttackingOrSpell(bool attackingOrSpell) const; void setAttackingOrSpell(bool attackingOrSpell) const;
@ -289,8 +289,8 @@ public:
void setVisibility(float visibility) const; void setVisibility(float visibility) const;
void castSpell(const std::string& spellId, bool manualSpell=false); void castSpell(const std::string& spellId, bool manualSpell=false);
void setAIAttackType(const std::string& attackType); void setAIAttackType(std::string_view attackType);
static std::string getRandomAttackType(); static std::string_view getRandomAttackType();
bool readyToPrepareAttack() const; bool readyToPrepareAttack() const;
bool readyToStartAttack() const; bool readyToStartAttack() const;

View file

@ -692,7 +692,7 @@ namespace MWRender
mAnimVelocities.clear(); mAnimVelocities.clear();
} }
bool Animation::hasAnimation(const std::string &anim) const bool Animation::hasAnimation(std::string_view anim) const
{ {
AnimSourceList::const_iterator iter(mAnimSources.begin()); AnimSourceList::const_iterator iter(mAnimSources.begin());
for(;iter != mAnimSources.end();++iter) for(;iter != mAnimSources.end();++iter)

View file

@ -385,7 +385,7 @@ public:
virtual void updatePtr(const MWWorld::Ptr &ptr); virtual void updatePtr(const MWWorld::Ptr &ptr);
bool hasAnimation(const std::string &anim) const; bool hasAnimation(std::string_view anim) const;
// Specifies the axis' to accumulate on. Non-accumulated axis will just // Specifies the axis' to accumulate on. Non-accumulated axis will just
// move visually, but not affect the actual movement. Each x/y/z value // move visually, but not affect the actual movement. Each x/y/z value

View file

@ -5,6 +5,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <string_view>
namespace SceneUtil namespace SceneUtil
{ {
@ -57,12 +58,12 @@ namespace SceneUtil
return mTextKeyByTime.empty(); return mTextKeyByTime.empty();
} }
auto findGroupStart(const std::string &groupName) const auto findGroupStart(std::string_view groupName) const
{ {
return std::find_if(mTextKeyByTime.begin(), mTextKeyByTime.end(), IsGroupStart{groupName}); return std::find_if(mTextKeyByTime.begin(), mTextKeyByTime.end(), IsGroupStart{groupName});
} }
bool hasGroupStart(const std::string &groupName) const bool hasGroupStart(std::string_view groupName) const
{ {
return mGroups.count(groupName) > 0; return mGroups.count(groupName) > 0;
} }
@ -70,7 +71,7 @@ namespace SceneUtil
private: private:
struct IsGroupStart struct IsGroupStart
{ {
const std::string &mGroupName; std::string_view mGroupName;
bool operator ()(const std::multimap<float, std::string>::value_type& value) const bool operator ()(const std::multimap<float, std::string>::value_type& value) const
{ {
@ -79,7 +80,7 @@ namespace SceneUtil
} }
}; };
std::set<std::string> mGroups; std::set<std::string, std::less<>> mGroups;
std::multimap<float, std::string> mTextKeyByTime; std::multimap<float, std::string> mTextKeyByTime;
}; };
} }