diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index f7172ac0b9..b8b1e9600d 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -1148,16 +1148,7 @@ namespace MWClass const bool hasHealth = it->getClass().hasItemHealth(*it); if (hasHealth) { - int armorMaxHealth = it->getClass().getItemMaxHealth(*it); - if (armorMaxHealth == 0) - { - ratings[i] = 0; - } - else - { - int armorHealth = it->getClass().getItemHealth(*it); - ratings[i] *= (float(armorHealth) / armorMaxHealth); - } + ratings[i] *= it->getClass().getItemNormalizedHealth(*it); } } } diff --git a/apps/openmw/mwgui/tradewindow.cpp b/apps/openmw/mwgui/tradewindow.cpp index 2fd91fd4a0..ce8193da25 100644 --- a/apps/openmw/mwgui/tradewindow.cpp +++ b/apps/openmw/mwgui/tradewindow.cpp @@ -34,17 +34,8 @@ namespace { float price = static_cast(item.getClass().getValue(item)); if (item.getClass().hasItemHealth(item)) - { - if (item.getClass().getItemMaxHealth(item) == 0) - { - price = 0; - } - else - { - price *= item.getClass().getItemHealth(item); - price /= item.getClass().getItemMaxHealth(item); - } - } + price *= item.getClass().getItemNormalizedHealth(item); + return static_cast(price * count); } diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index 3b26edecb8..27ea534a74 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -1400,17 +1400,9 @@ namespace MWGui int durabilityPercent = 100; if (item.getClass().hasItemHealth(item)) { - int weapmaxhealth = item.getClass().getItemMaxHealth(item); - if (weapmaxhealth == 0) - { - durabilityPercent = 0; - } - else - { - int weaphealth = item.getClass().getItemHealth(item); - durabilityPercent = static_cast(weaphealth / static_cast(weapmaxhealth) * 100); - } + durabilityPercent = static_cast(item.getClass().getItemNormalizedHealth(item)); } + mHud->setSelectedWeapon(item, durabilityPercent); mInventoryWindow->setTitle(item.getClass().getName(item)); } diff --git a/apps/openmw/mwmechanics/combat.cpp b/apps/openmw/mwmechanics/combat.cpp index 2a9ff5e84f..41e2485ce0 100644 --- a/apps/openmw/mwmechanics/combat.cpp +++ b/apps/openmw/mwmechanics/combat.cpp @@ -373,16 +373,7 @@ namespace MWMechanics const bool weaphashealth = weapon.getClass().hasItemHealth(weapon); if (weaphashealth) { - int weapmaxhealth = weapon.getClass().getItemMaxHealth(weapon); - - if (weapmaxhealth == 0) - { - damage = 0; - return; - } - - int weaphealth = weapon.getClass().getItemHealth(weapon); - damage *= (float(weaphealth) / weapmaxhealth); + damage *= weapon.getClass().getItemNormalizedHealth(weapon); } static const float fDamageStrengthBase = MWBase::Environment::get().getWorld()->getStore().get() diff --git a/apps/openmw/mwworld/class.cpp b/apps/openmw/mwworld/class.cpp index 68d5998ac9..70fde33cfb 100644 --- a/apps/openmw/mwworld/class.cpp +++ b/apps/openmw/mwworld/class.cpp @@ -83,6 +83,18 @@ namespace MWWorld return ptr.getCellRef().getCharge(); } + float Class::getItemNormalizedHealth (const ConstPtr& ptr) const + { + if (getItemMaxHealth(ptr) == 0) + { + return 0.f; + } + else + { + return getItemHealth(ptr) / static_cast(getItemMaxHealth(ptr)); + } + } + int Class::getItemMaxHealth (const ConstPtr& ptr) const { throw std::runtime_error ("class does not have item health"); diff --git a/apps/openmw/mwworld/class.hpp b/apps/openmw/mwworld/class.hpp index 132b7e9e87..af88b0dcca 100644 --- a/apps/openmw/mwworld/class.hpp +++ b/apps/openmw/mwworld/class.hpp @@ -112,6 +112,9 @@ namespace MWWorld virtual int getItemHealth (const ConstPtr& ptr) const; ///< Return current item health or throw an exception if class does not have item health + virtual float getItemNormalizedHealth (const ConstPtr& ptr) const; + ///< Return current item health re-scaled to maximum health + virtual int getItemMaxHealth (const ConstPtr& ptr) const; ///< Return item max health or throw an exception, if class does not have item health /// (default implementation: throw an exception)