From 55134d1e317dfc78285e548837c98d1387bf371b Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Tue, 26 Jul 2022 17:23:00 +0200 Subject: [PATCH] Use string_view in the CharacterController --- apps/openmw/mwmechanics/aicombat.cpp | 17 ++++------- apps/openmw/mwmechanics/character.cpp | 44 ++++++++++++++++----------- apps/openmw/mwmechanics/character.hpp | 10 +++--- apps/openmw/mwrender/animation.cpp | 2 +- apps/openmw/mwrender/animation.hpp | 2 +- components/sceneutil/textkeymap.hpp | 9 +++--- 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/apps/openmw/mwmechanics/aicombat.cpp b/apps/openmw/mwmechanics/aicombat.cpp index 5bb03bb751..17e42442d3 100644 --- a/apps/openmw/mwmechanics/aicombat.cpp +++ b/apps/openmw/mwmechanics/aicombat.cpp @@ -31,7 +31,7 @@ namespace { //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, float duration, int weapType, float strength); @@ -682,10 +682,8 @@ namespace MWMechanics namespace { -std::string chooseBestAttack(const ESM::Weapon* weapon) +std::string_view chooseBestAttack(const ESM::Weapon* weapon) { - std::string attackType; - if (weapon != nullptr) { //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(); float roll = Misc::Rng::rollClosedProbability(prng) * (slash + chop + thrust); if(roll <= slash) - attackType = "slash"; + return "slash"; else if(roll <= (slash + thrust)) - attackType = "thrust"; + return "thrust"; else - attackType = "chop"; + return "chop"; } - else - attackType = MWMechanics::CharacterController::getRandomAttackType(); - - return attackType; + return MWMechanics::CharacterController::getRandomAttackType(); } osg::Vec3f AimDirToMovingTarget(const MWWorld::Ptr& actor, const MWWorld::Ptr& target, const osg::Vec3f& vLastTargetPos, diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index d4a90f24e6..b86f63da82 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -428,8 +428,9 @@ void CharacterController::refreshJumpAnims(JumpingState jump, bool force) return; } - std::string weapShortGroup = getWeaponShortGroup(mWeaponType); - std::string jumpAnimName = "jump" + weapShortGroup; + std::string_view weapShortGroup = getWeaponShortGroup(mWeaponType); + std::string jumpAnimName = "jump"; + jumpAnimName += weapShortGroup; MWRender::Animation::BlendMask jumpmask = MWRender::Animation::BlendMask_All; if (!weapShortGroup.empty() && !mAnimation->hasAnimation(jumpAnimName)) 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)) { - static const std::string oneHandFallback = getWeaponType(ESM::Weapon::LongBladeOneHand)->mLongGroup; - static const std::string twoHandFallback = getWeaponType(ESM::Weapon::LongBladeTwoHand)->mLongGroup; + static const std::string_view oneHandFallback = getWeaponType(ESM::Weapon::LongBladeOneHand)->mLongGroup; + static const std::string_view twoHandFallback = getWeaponType(ESM::Weapon::LongBladeTwoHand)->mLongGroup; const ESM::WeaponType* weapInfo = getWeaponType(weaponType); @@ -512,7 +513,7 @@ std::string CharacterController::getWeaponAnimation(int weaponType) const 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)) return {}; @@ -529,8 +530,8 @@ std::string CharacterController::fallbackShortWeaponGroup(const std::string& bas return baseGroupName; } - static const std::string oneHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeOneHand); - static const std::string twoHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeTwoHand); + static const std::string_view oneHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeOneHand); + static const std::string_view twoHandFallback = getWeaponShortGroup(ESM::Weapon::LongBladeTwoHand); std::string groupName = baseGroupName; 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; - std::string weapShortGroup = getWeaponShortGroup(mWeaponType); + std::string_view weapShortGroup = getWeaponShortGroup(mWeaponType); // Non-biped creatures don't use spellcasting-specific movement animations. if(!isRealWeapon(mWeaponType) && !mPtr.getClass().isBipedal(mPtr)) - weapShortGroup.clear(); + weapShortGroup = {}; if (swimpos == std::string::npos && !weapShortGroup.empty()) { std::string weapMovementAnimName; // Spellcasting stance turning is a special case if (mWeaponType == ESM::Weapon::Spell && isTurning()) - weapMovementAnimName = weapShortGroup + movementAnimName; + { + weapMovementAnimName = weapShortGroup; + weapMovementAnimName += movementAnimName; + } else - weapMovementAnimName = movementAnimName + weapShortGroup; + { + weapMovementAnimName = movementAnimName; + weapMovementAnimName += weapShortGroup; + } if (!mAnimation->hasAnimation(weapMovementAnimName)) weapMovementAnimName = fallbackShortWeaponGroup(movementAnimName, &movemask); @@ -705,10 +712,11 @@ void CharacterController::refreshIdleAnims(CharacterState idle, bool force) if (fallback || mIdleState == CharState_Idle || mIdleState == CharState_SpecialIdle) { - std::string weapShortGroup = getWeaponShortGroup(mWeaponType); + std::string_view weapShortGroup = getWeaponShortGroup(mWeaponType); if (!weapShortGroup.empty()) { - std::string weapIdleGroup = idleGroup + weapShortGroup; + std::string weapIdleGroup = idleGroup; + weapIdleGroup += weapShortGroup; if (!mAnimation->hasAnimation(weapIdleGroup)) weapIdleGroup = fallbackShortWeaponGroup(idleGroup); idleGroup = weapIdleGroup; @@ -2631,7 +2639,7 @@ void CharacterController::setVisibility(float visibility) const mAnimation->setAlpha(visibility); } -std::string CharacterController::getMovementBasedAttackType() const +std::string_view CharacterController::getMovementBasedAttackType() const { float *move = mPtr.getClass().getMovementSettings(mPtr).mPosition; 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); } -void CharacterController::setAIAttackType(const std::string& attackType) +void CharacterController::setAIAttackType(std::string_view attackType) { mAttackType = attackType; } -std::string CharacterController::getRandomAttackType() +std::string_view CharacterController::getRandomAttackType() { MWBase::World* world = MWBase::Environment::get().getWorld(); float random = Misc::Rng::rollProbability(world->getPrng()); diff --git a/apps/openmw/mwmechanics/character.hpp b/apps/openmw/mwmechanics/character.hpp index 2de6713396..323e2784df 100644 --- a/apps/openmw/mwmechanics/character.hpp +++ b/apps/openmw/mwmechanics/character.hpp @@ -183,7 +183,7 @@ class CharacterController : public MWRender::Animation::TextKeyListener bool mIsMovingBackward{false}; osg::Vec2f mSmoothedSpeed; - std::string getMovementBasedAttackType() const; + std::string_view getMovementBasedAttackType() const; void clearStateAnimation(std::string &anim) const; 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 getWeaponAnimation(int weaponType) const; - std::string getWeaponShortGroup(int weaponType) const; + std::string_view getWeaponAnimation(int weaponType) const; + std::string_view getWeaponShortGroup(int weaponType) const; bool getAttackingOrSpell() const; void setAttackingOrSpell(bool attackingOrSpell) const; @@ -289,8 +289,8 @@ public: void setVisibility(float visibility) const; void castSpell(const std::string& spellId, bool manualSpell=false); - void setAIAttackType(const std::string& attackType); - static std::string getRandomAttackType(); + void setAIAttackType(std::string_view attackType); + static std::string_view getRandomAttackType(); bool readyToPrepareAttack() const; bool readyToStartAttack() const; diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index 9a85aabeb2..0933bf4e9e 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -692,7 +692,7 @@ namespace MWRender mAnimVelocities.clear(); } - bool Animation::hasAnimation(const std::string &anim) const + bool Animation::hasAnimation(std::string_view anim) const { AnimSourceList::const_iterator iter(mAnimSources.begin()); for(;iter != mAnimSources.end();++iter) diff --git a/apps/openmw/mwrender/animation.hpp b/apps/openmw/mwrender/animation.hpp index d7c5749df7..1b5f5aa298 100644 --- a/apps/openmw/mwrender/animation.hpp +++ b/apps/openmw/mwrender/animation.hpp @@ -385,7 +385,7 @@ public: 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 // move visually, but not affect the actual movement. Each x/y/z value diff --git a/components/sceneutil/textkeymap.hpp b/components/sceneutil/textkeymap.hpp index ee58bc72a7..019c7eb1f2 100644 --- a/components/sceneutil/textkeymap.hpp +++ b/components/sceneutil/textkeymap.hpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace SceneUtil { @@ -57,12 +58,12 @@ namespace SceneUtil 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}); } - bool hasGroupStart(const std::string &groupName) const + bool hasGroupStart(std::string_view groupName) const { return mGroups.count(groupName) > 0; } @@ -70,7 +71,7 @@ namespace SceneUtil private: struct IsGroupStart { - const std::string &mGroupName; + std::string_view mGroupName; bool operator ()(const std::multimap::value_type& value) const { @@ -79,7 +80,7 @@ namespace SceneUtil } }; - std::set mGroups; + std::set> mGroups; std::multimap mTextKeyByTime; }; }