From 25b26f6fa7305ff99a8de4487b15565ded8750ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 9 Apr 2022 01:06:15 +0300 Subject: [PATCH 1/4] Reduce calls in CharacterController::refreshHitRecoilAnims --- apps/openmw/mwmechanics/character.cpp | 29 ++++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index 1c0a4f2c85..affd25f60e 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -207,15 +207,17 @@ std::string CharacterController::chooseRandomGroup (const std::string& prefix, i void CharacterController::refreshHitRecoilAnims(CharacterState& idle) { - bool recovery = mPtr.getClass().getCreatureStats(mPtr).getHitRecovery(); - bool knockdown = mPtr.getClass().getCreatureStats(mPtr).getKnockedDown(); - bool block = mPtr.getClass().getCreatureStats(mPtr).getBlock(); - bool isSwimming = MWBase::Environment::get().getWorld()->isSwimming(mPtr); - auto& prng = MWBase::Environment::get().getWorld()->getPrng(); + auto* world = MWBase::Environment::get().getWorld(); + auto& charClass = mPtr.getClass(); + auto& stats = charClass.getCreatureStats(mPtr); + bool recovery = stats.getHitRecovery(); + bool knockdown = stats.getKnockedDown(); + bool block = stats.getBlock(); + bool isSwimming = world->isSwimming(mPtr); + auto& prng = world->getPrng(); if(mHitState == CharState_None) { - if ((mPtr.getClass().getCreatureStats(mPtr).getFatigue().getCurrent() < 0 - || mPtr.getClass().getCreatureStats(mPtr).getFatigue().getBase() == 0)) + if (stats.getFatigue().getCurrent() < 0 || stats.getFatigue().getBase() == 0) { mTimeUntilWake = Misc::Rng::rollClosedProbability(prng) * 2 + 1; // Wake up after 1 to 3 seconds if (isSwimming && mAnimation->hasAnimation("swimknockout")) @@ -236,7 +238,7 @@ void CharacterController::refreshHitRecoilAnims(CharacterState& idle) mCurrentHit.erase(); } - mPtr.getClass().getCreatureStats(mPtr).setKnockedDown(true); + stats.setKnockedDown(true); } else if (knockdown) { @@ -255,7 +257,7 @@ void CharacterController::refreshHitRecoilAnims(CharacterState& idle) else { // Knockdown animation is missing. Cancel knockdown state. - mPtr.getClass().getCreatureStats(mPtr).setKnockedDown(false); + stats.setKnockedDown(false); } } else if (recovery) @@ -311,15 +313,14 @@ void CharacterController::refreshHitRecoilAnims(CharacterState& idle) { mCurrentHit.erase(); if (knockdown) - mPtr.getClass().getCreatureStats(mPtr).setKnockedDown(false); + stats.setKnockedDown(false); if (recovery) - mPtr.getClass().getCreatureStats(mPtr).setHitRecovery(false); + stats.setHitRecovery(false); if (block) - mPtr.getClass().getCreatureStats(mPtr).setBlock(false); + stats.setBlock(false); mHitState = CharState_None; } - else if (isKnockedOut() && mPtr.getClass().getCreatureStats(mPtr).getFatigue().getCurrent() > 0 - && mTimeUntilWake <= 0) + else if (isKnockedOut() && stats.getFatigue().getCurrent() > 0 && mTimeUntilWake <= 0) { mHitState = isSwimming ? CharState_SwimKnockDown : CharState_KnockDown; mAnimation->disable(mCurrentHit); From e85a979f10b5f348a4da4703c373f5ad694e8ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 9 Apr 2022 01:20:53 +0300 Subject: [PATCH 2/4] Reduce calls in CharacterController::handleTextKey --- apps/openmw/mwmechanics/character.cpp | 30 ++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index affd25f60e..1d2ce8d44b 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -959,6 +959,8 @@ void CharacterController::handleTextKey(const std::string &groupname, SceneUtil: sndMgr->playSound3D(mPtr, evt.substr(7), 1.0f, 1.0f); return; } + + auto& charClass = mPtr.getClass(); if(evt.compare(0, 10, "soundgen: ") == 0) { std::string soundgen = evt.substr(10); @@ -984,7 +986,7 @@ void CharacterController::handleTextKey(const std::string &groupname, SceneUtil: } } - std::string sound = mPtr.getClass().getSoundIdFromSndGen(mPtr, soundgen); + std::string sound = charClass.getSoundIdFromSndGen(mPtr, soundgen); if(!sound.empty()) { MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); @@ -1007,8 +1009,8 @@ void CharacterController::handleTextKey(const std::string &groupname, SceneUtil: // Not ours, skip it return; } - size_t off = groupname.size()+2; - size_t len = evt.size() - off; + const size_t off = groupname.size()+2; + const size_t len = evt.size() - off; if(groupname == "shield" && evt.compare(off, len, "equip attach") == 0) mAnimation->showCarriedLeft(true); @@ -1019,21 +1021,21 @@ void CharacterController::handleTextKey(const std::string &groupname, SceneUtil: else if(evt.compare(off, len, "unequip detach") == 0) mAnimation->showWeapons(false); else if(evt.compare(off, len, "chop hit") == 0) - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Chop); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Chop); else if(evt.compare(off, len, "slash hit") == 0) - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Slash); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Slash); else if(evt.compare(off, len, "thrust hit") == 0) - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Thrust); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Thrust); else if(evt.compare(off, len, "hit") == 0) { if (groupname == "attack1" || groupname == "swimattack1") - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Chop); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Chop); else if (groupname == "attack2" || groupname == "swimattack2") - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Slash); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Slash); else if (groupname == "attack3" || groupname == "swimattack3") - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Thrust); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Thrust); else - mPtr.getClass().hit(mPtr, mAttackStrength); + charClass.hit(mPtr, mAttackStrength); } else if (!groupname.empty() && (groupname.compare(0, groupname.size()-1, "attack") == 0 || groupname.compare(0, groupname.size()-1, "swimattack") == 0) @@ -1057,11 +1059,11 @@ void CharacterController::handleTextKey(const std::string &groupname, SceneUtil: if (!hasHitKey) { if (groupname == "attack1" || groupname == "swimattack1") - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Chop); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Chop); else if (groupname == "attack2" || groupname == "swimattack2") - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Slash); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Slash); else if (groupname == "attack3" || groupname == "swimattack3") - mPtr.getClass().hit(mPtr, mAttackStrength, ESM::Weapon::AT_Thrust); + charClass.hit(mPtr, mAttackStrength, ESM::Weapon::AT_Thrust); } } else if (evt.compare(off, len, "shoot attach") == 0) @@ -1081,7 +1083,7 @@ void CharacterController::handleTextKey(const std::string &groupname, SceneUtil: } else if (groupname == "shield" && evt.compare(off, len, "block hit") == 0) - mPtr.getClass().block(mPtr); + charClass.block(mPtr); else if (groupname == "containeropen" && evt.compare(off, len, "loot") == 0) MWBase::Environment::get().getWindowManager()->pushGuiMode(MWGui::GM_Container, mPtr); } From dd222b9ef16785277ede71040442be63879f5b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 9 Apr 2022 01:21:34 +0300 Subject: [PATCH 3/4] Reduce calls in CharacterController::updateIdleStormState --- apps/openmw/mwmechanics/character.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index 1d2ce8d44b..8e29f2a5b1 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -1101,9 +1101,10 @@ void CharacterController::updateIdleStormState(bool inwater) return; } - if (MWBase::Environment::get().getWorld()->isInStorm()) + auto* world = MWBase::Environment::get().getWorld(); + if (world->isInStorm()) { - osg::Vec3f stormDirection = MWBase::Environment::get().getWorld()->getStormDirection(); + osg::Vec3f stormDirection = world->getStormDirection(); osg::Vec3f characterDirection = mPtr.getRefData().getBaseNode()->getAttitude() * osg::Vec3f(0,1,0); stormDirection.normalize(); characterDirection.normalize(); From 058da82823eca29de6af2c27603a087206d34a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 9 Apr 2022 01:52:52 +0300 Subject: [PATCH 4/4] Reduce calls in CharacterController::updateState --- apps/openmw/mwmechanics/character.cpp | 59 ++++++++++++--------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index 8e29f2a5b1..b2eb316e24 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -1139,7 +1139,9 @@ bool CharacterController::updateCarriedLeftVisible(const int weaptype) const bool CharacterController::updateState(CharacterState idle) { - auto& prng = MWBase::Environment::get().getWorld()->getPrng(); + auto* world = MWBase::Environment::get().getWorld(); + auto& prng = world->getPrng(); + MWBase::SoundManager* sndMgr = MWBase::Environment::get().getSoundManager(); const MWWorld::Class &cls = mPtr.getClass(); CreatureStats &stats = cls.getCreatureStats(mPtr); @@ -1154,7 +1156,7 @@ bool CharacterController::updateState(CharacterState idle) std::string upSoundId; std::string downSoundId; bool weaponChanged = false; - if (mPtr.getClass().hasInventoryStore(mPtr)) + if (cls.hasInventoryStore(mPtr)) { MWWorld::InventoryStore &inv = cls.getInventoryStore(mPtr); MWWorld::ContainerStoreIterator weapon = getActiveWeapon(mPtr, &weaptype); @@ -1182,13 +1184,13 @@ bool CharacterController::updateState(CharacterState idle) // For biped actors, blend weapon animations with lower body animations with higher priority MWRender::Animation::AnimPriority priorityWeapon(Priority_Weapon); - if (mPtr.getClass().isBipedal(mPtr)) + if (cls.isBipedal(mPtr)) priorityWeapon[MWRender::Animation::BoneGroup_LowerBody] = Priority_WeaponLowerBody; bool forcestateupdate = false; // We should not play equipping animation and sound during weapon->weapon transition - bool isStillWeapon = weaptype != ESM::Weapon::HandToHand && weaptype != ESM::Weapon::Spell && weaptype != ESM::Weapon::None && + const bool isStillWeapon = weaptype != ESM::Weapon::HandToHand && weaptype != ESM::Weapon::Spell && weaptype != ESM::Weapon::None && mWeaponType != ESM::Weapon::HandToHand && mWeaponType != ESM::Weapon::Spell && mWeaponType != ESM::Weapon::None; // If the current weapon type was changed in the middle of attack (e.g. by Equip console command or when bound spell expires), @@ -1241,7 +1243,6 @@ bool CharacterController::updateState(CharacterState idle) if(!downSoundId.empty()) { - MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); sndMgr->playSound3D(mPtr, downSoundId, 1.0f, 1.0f); } } @@ -1297,11 +1298,10 @@ bool CharacterController::updateState(CharacterState idle) if(isWerewolf) { - const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore(); + const MWWorld::ESMStore &store = world->getStore(); const ESM::Sound *sound = store.get().searchRandom("WolfEquip", prng); if(sound) { - MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); sndMgr->playSound3D(mPtr, sound->mId, 1.0f, 1.0f); } } @@ -1311,7 +1311,6 @@ bool CharacterController::updateState(CharacterState idle) if(!upSoundId.empty() && !isStillWeapon) { - MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); sndMgr->playSound3D(mPtr, upSoundId, 1.0f, 1.0f); } } @@ -1329,10 +1328,9 @@ bool CharacterController::updateState(CharacterState idle) if(isWerewolf) { - MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); - if(cls.getCreatureStats(mPtr).getStance(MWMechanics::CreatureStats::Stance_Run) + if(stats.getStance(MWMechanics::CreatureStats::Stance_Run) && mHasMovedInXY - && !MWBase::Environment::get().getWorld()->isSwimming(mPtr) + && !world->isSwimming(mPtr) && mWeaponType == ESM::Weapon::None) { if(!sndMgr->getSoundPlaying(mPtr, "WolfRun")) @@ -1347,7 +1345,7 @@ bool CharacterController::updateState(CharacterState idle) bool ammunition = true; bool isWeapon = false; float weapSpeed = 1.f; - if (mPtr.getClass().hasInventoryStore(mPtr)) + if (cls.hasInventoryStore(mPtr)) { MWWorld::InventoryStore &inv = cls.getInventoryStore(mPtr); MWWorld::ConstContainerStoreIterator weapon = getActiveWeapon(mPtr, &weaptype); @@ -1380,12 +1378,12 @@ bool CharacterController::updateState(CharacterState idle) bool resetIdle = ammunition; if(mUpperBodyState == UpperCharState_WeapEquiped && (mHitState == CharState_None || mHitState == CharState_Block)) { - MWBase::Environment::get().getWorld()->breakInvisibility(mPtr); + world->breakInvisibility(mPtr); mAttackStrength = 0; // Randomize attacks for non-bipedal creatures - if (mPtr.getClass().getType() == ESM::Creature::sRecordId && - !mPtr.getClass().isBipedal(mPtr) && + if (cls.getType() == ESM::Creature::sRecordId && + !cls.isBipedal(mPtr) && (!mAnimation->hasAnimation(mCurrentWeapon) || isRandomAttackAnimation(mCurrentWeapon))) { mCurrentWeapon = chooseRandomAttackAnimation(); @@ -1406,13 +1404,13 @@ bool CharacterController::updateState(CharacterState idle) } std::string spellid = stats.getSpells().getSelectedSpell(); bool isMagicItem = false; - bool canCast = mCastingManualSpell || MWBase::Environment::get().getWorld()->startSpellCast(mPtr); + bool canCast = mCastingManualSpell || world->startSpellCast(mPtr); if (spellid.empty()) { - if (mPtr.getClass().hasInventoryStore(mPtr)) + if (cls.hasInventoryStore(mPtr)) { - MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); + MWWorld::InventoryStore& inv = cls.getInventoryStore(mPtr); if (inv.getSelectedEnchantItem() != inv.end()) { const MWWorld::Ptr& enchantItem = *inv.getSelectedEnchantItem(); @@ -1426,7 +1424,7 @@ bool CharacterController::updateState(CharacterState idle) if (isMagicItem && !useCastingAnimations) { // Enchanted items by default do not use casting animations - MWBase::Environment::get().getWorld()->castSpell(mPtr); + world->castSpell(mPtr); resetIdle = false; } else if(!spellid.empty() && canCast) @@ -1435,7 +1433,7 @@ bool CharacterController::updateState(CharacterState idle) cast.playSpellCastingEffects(spellid, isMagicItem); std::vector effects; - const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore(); + const MWWorld::ESMStore &store = world->getStore(); if (isMagicItem) { const ESM::Enchantment *enchantment = store.get().find(spellid); @@ -1449,7 +1447,7 @@ bool CharacterController::updateState(CharacterState idle) const ESM::MagicEffect *effect = store.get().find(effects.back().mEffectID); // use last effect of list for color of VFX_Hands - const ESM::Static* castStatic = MWBase::Environment::get().getWorld()->getStore().get().find ("VFX_Hands"); + const ESM::Static* castStatic = world->getStore().get().find ("VFX_Hands"); for (size_t iter = 0; iter < effects.size(); ++iter) // play hands vfx for each effect { @@ -1468,7 +1466,7 @@ bool CharacterController::updateState(CharacterState idle) { startKey = "start"; stopKey = "stop"; - MWBase::Environment::get().getWorld()->castSpell(mPtr, mCastingManualSpell); // No "release" text key to use, so cast immediately + world->castSpell(mPtr, mCastingManualSpell); // No "release" text key to use, so cast immediately mCastingManualSpell = false; } else @@ -1497,10 +1495,10 @@ bool CharacterController::updateState(CharacterState idle) } else if(mWeaponType == ESM::Weapon::PickProbe) { - MWWorld::ContainerStoreIterator weapon = mPtr.getClass().getInventoryStore(mPtr).getSlot(MWWorld::InventoryStore::Slot_CarriedRight); + MWWorld::ContainerStoreIterator weapon = cls.getInventoryStore(mPtr).getSlot(MWWorld::InventoryStore::Slot_CarriedRight); MWWorld::Ptr item = *weapon; // TODO: this will only work for the player, and needs to be fixed if NPCs should ever use lockpicks/probes. - MWWorld::Ptr target = MWBase::Environment::get().getWorld()->getFacedObject(); + MWWorld::Ptr target = world->getFacedObject(); std::string resultMessage, resultSound; if(!target.isEmpty()) @@ -1518,8 +1516,7 @@ bool CharacterController::updateState(CharacterState idle) if(!resultMessage.empty()) MWBase::Environment::get().getWindowManager()->messageBox(resultMessage); if(!resultSound.empty()) - MWBase::Environment::get().getSoundManager()->playSound3D(target, resultSound, - 1.0f, 1.0f); + sndMgr->playSound3D(target, resultSound, 1.0f, 1.0f); } else if (ammunition) { @@ -1545,7 +1542,7 @@ bool CharacterController::updateState(CharacterState idle) { if (isWeapon) { - MWWorld::ConstContainerStoreIterator weapon = mPtr.getClass().getInventoryStore(mPtr).getSlot(MWWorld::InventoryStore::Slot_CarriedRight); + MWWorld::ConstContainerStoreIterator weapon = cls.getInventoryStore(mPtr).getSlot(MWWorld::InventoryStore::Slot_CarriedRight); mAttackType = getBestAttack(weapon->get()->mBase); } else @@ -1611,11 +1608,9 @@ bool CharacterController::updateState(CharacterState idle) if(weapclass != ESM::WeaponType::Ranged && weapclass != ESM::WeaponType::Thrown) { - MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); - if(isWerewolf) { - const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore(); + const MWWorld::ESMStore &store = world->getStore(); const ESM::Sound *sound = store.get().searchRandom("WolfSwing", prng); if(sound) sndMgr->playSound3D(mPtr, sound->mId, 1.0f, 1.0f); @@ -1785,9 +1780,9 @@ bool CharacterController::updateState(CharacterState idle) mUpperBodyState = UpperCharState_WeapEquiped; } - if (mPtr.getClass().hasInventoryStore(mPtr)) + if (cls.hasInventoryStore(mPtr)) { - const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); + const MWWorld::InventoryStore& inv = cls.getInventoryStore(mPtr); MWWorld::ConstContainerStoreIterator torch = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft); if(torch != inv.end() && torch->getType() == ESM::Light::sRecordId && updateCarriedLeftVisible(mWeaponType))