diff --git a/apps/openmw/mwmechanics/aicombataction.cpp b/apps/openmw/mwmechanics/aicombataction.cpp index df636dd56d..0ccd0b6ec6 100644 --- a/apps/openmw/mwmechanics/aicombataction.cpp +++ b/apps/openmw/mwmechanics/aicombataction.cpp @@ -182,29 +182,11 @@ namespace MWMechanics } } - float bestArrowRating = 0; MWWorld::Ptr bestArrow; - for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) - { - float rating = rateWeapon(*it, actor, enemy, ESM::Weapon::Arrow); - if (rating > bestArrowRating) - { - bestArrowRating = rating; - bestArrow = *it; - } - } + float bestArrowRating = rateAmmo(actor, enemy, bestArrow, ESM::Weapon::Arrow); - float bestBoltRating = 0; MWWorld::Ptr bestBolt; - for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) - { - float rating = rateWeapon(*it, actor, enemy, ESM::Weapon::Bolt); - if (rating > bestBoltRating) - { - bestBoltRating = rating; - bestBolt = *it; - } - } + float bestBoltRating = rateAmmo(actor, enemy, bestBolt, ESM::Weapon::Bolt); for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) { @@ -277,25 +259,9 @@ namespace MWMechanics } } - float bestArrowRating = 0; - for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) - { - float rating = rateWeapon(*it, actor, enemy, ESM::Weapon::Arrow); - if (rating > bestArrowRating) - { - bestArrowRating = rating; - } - } + float bestArrowRating = rateAmmo(actor, enemy, ESM::Weapon::Arrow); - float bestBoltRating = 0; - for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) - { - float rating = rateWeapon(*it, actor, enemy, ESM::Weapon::Bolt); - if (rating > bestBoltRating) - { - bestBoltRating = rating; - } - } + float bestBoltRating = rateAmmo(actor, enemy, ESM::Weapon::Bolt); for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) { diff --git a/apps/openmw/mwmechanics/weaponpriority.cpp b/apps/openmw/mwmechanics/weaponpriority.cpp index d06e73c932..5a0bd9c154 100644 --- a/apps/openmw/mwmechanics/weaponpriority.cpp +++ b/apps/openmw/mwmechanics/weaponpriority.cpp @@ -8,6 +8,7 @@ #include "../mwworld/class.hpp" #include "../mwworld/esmstore.hpp" +#include "../mwworld/inventorystore.hpp" #include "npcstats.hpp" #include "combat.hpp" @@ -111,6 +112,33 @@ namespace MWMechanics return rating + bonus; } + float rateAmmo(const MWWorld::Ptr &actor, const MWWorld::Ptr &enemy, MWWorld::Ptr &bestAmmo, ESM::Weapon::Type ammoType) + { + float bestAmmoRating = 0.f; + if (!actor.getClass().hasInventoryStore(actor)) + return bestAmmoRating; + + MWWorld::InventoryStore& store = actor.getClass().getInventoryStore(actor); + + for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it) + { + float rating = rateWeapon(*it, actor, enemy, ammoType); + if (rating > bestAmmoRating) + { + bestAmmoRating = rating; + bestAmmo = *it; + } + } + + return bestAmmoRating; + } + + float rateAmmo(const MWWorld::Ptr &actor, const MWWorld::Ptr &enemy, ESM::Weapon::Type ammoType) + { + MWWorld::Ptr emptyPtr; + return rateAmmo(actor, enemy, emptyPtr, ammoType); + } + float vanillaRateWeaponAndAmmo(const MWWorld::Ptr& weapon, const MWWorld::Ptr& ammo, const MWWorld::Ptr& actor, const MWWorld::Ptr& enemy) { const MWWorld::Store& gmst = MWBase::Environment::get().getWorld()->getStore().get(); diff --git a/apps/openmw/mwmechanics/weaponpriority.hpp b/apps/openmw/mwmechanics/weaponpriority.hpp index 90a767c4ac..f5c9a11596 100644 --- a/apps/openmw/mwmechanics/weaponpriority.hpp +++ b/apps/openmw/mwmechanics/weaponpriority.hpp @@ -1,6 +1,8 @@ #ifndef OPENMW_WEAPON_PRIORITY_H #define OPENMW_WEAPON_PRIORITY_H +#include + #include "../mwworld/ptr.hpp" namespace MWMechanics @@ -8,6 +10,9 @@ namespace MWMechanics float rateWeapon (const MWWorld::Ptr& item, const MWWorld::Ptr& actor, const MWWorld::Ptr& enemy, int type=-1, float arrowRating=0.f, float boltRating=0.f); + float rateAmmo(const MWWorld::Ptr &actor, const MWWorld::Ptr &enemy, MWWorld::Ptr &bestAmmo, ESM::Weapon::Type ammoType); + float rateAmmo(const MWWorld::Ptr &actor, const MWWorld::Ptr &enemy, ESM::Weapon::Type ammoType); + float vanillaRateWeaponAndAmmo(const MWWorld::Ptr& weapon, const MWWorld::Ptr& ammo, const MWWorld::Ptr& actor, const MWWorld::Ptr& enemy); }