From c3e8d536cdbfedd8dccdfac24b9fd10299376f1b Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Thu, 25 Oct 2018 16:09:07 +0300 Subject: [PATCH] Implement getNormalizedEnchantmentCharge() method and use it --- apps/openmw/mwgui/sortfilteritemmodel.cpp | 46 +---------------------- apps/openmw/mwgui/tradewindow.cpp | 3 +- apps/openmw/mwgui/windowmanagerimp.cpp | 18 +-------- apps/openmw/mwworld/cellref.cpp | 16 ++++++++ apps/openmw/mwworld/cellref.hpp | 3 ++ 5 files changed, 24 insertions(+), 62 deletions(-) diff --git a/apps/openmw/mwgui/sortfilteritemmodel.cpp b/apps/openmw/mwgui/sortfilteritemmodel.cpp index 5172c85ee8..23f8a121b3 100644 --- a/apps/openmw/mwgui/sortfilteritemmodel.cpp +++ b/apps/openmw/mwgui/sortfilteritemmodel.cpp @@ -86,66 +86,24 @@ namespace if (!leftName.empty()) { const ESM::Enchantment* ench = MWBase::Environment::get().getWorld()->getStore().get().search(leftName); - if (ench) { if (ench->mData.mType == ESM::Enchantment::ConstantEffect) - { leftChargePercent = 101; - } else - { - int maxEnchCharge = ench->mData.mCharge; - if (maxEnchCharge == 0) - { - leftChargePercent = 0; - } - else - { - float enchCharge = left.mBase.getCellRef().getEnchantmentCharge(); - if (enchCharge == -1) - { - leftChargePercent = 100; - } - else - { - leftChargePercent = static_cast(enchCharge / static_cast(maxEnchCharge) * 100); - } - } - } + leftChargePercent = static_cast(left.mBase.getCellRef().getNormalizedEnchantmentCharge(ench->mData.mCharge) * 100); } } if (!rightName.empty()) { const ESM::Enchantment* ench = MWBase::Environment::get().getWorld()->getStore().get().search(rightName); - if (ench) { if (ench->mData.mType == ESM::Enchantment::ConstantEffect) - { rightChargePercent = 101; - } else - { - int maxEnchCharge = ench->mData.mCharge; - if (maxEnchCharge == 0) - { - rightChargePercent = 0; - } - else - { - float enchCharge = right.mBase.getCellRef().getEnchantmentCharge(); - if (enchCharge == -1) - { - rightChargePercent = 100; - } - else - { - rightChargePercent = static_cast(enchCharge / static_cast(maxEnchCharge) * 100); - } - } - } + rightChargePercent = static_cast(right.mBase.getCellRef().getNormalizedEnchantmentCharge(ench->mData.mCharge) * 100); } } diff --git a/apps/openmw/mwgui/tradewindow.cpp b/apps/openmw/mwgui/tradewindow.cpp index ce8193da25..90698dfc4f 100644 --- a/apps/openmw/mwgui/tradewindow.cpp +++ b/apps/openmw/mwgui/tradewindow.cpp @@ -34,8 +34,9 @@ namespace { float price = static_cast(item.getClass().getValue(item)); if (item.getClass().hasItemHealth(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 27ea534a74..5ffed48156 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -1369,22 +1369,7 @@ namespace MWGui const ESM::Enchantment* ench = mStore->get() .find(item.getClass().getEnchantment(item)); - int chargePercent = 100; - - int maxEnchCharge = ench->mData.mCharge; - if (maxEnchCharge == 0) - { - chargePercent = 0; - } - else - { - float enchCharge = item.getCellRef().getEnchantmentCharge(); - if (enchCharge != -1) - { - chargePercent = static_cast(enchCharge / static_cast(maxEnchCharge) * 100); - } - } - + int chargePercent = static_cast(item.getCellRef().getNormalizedEnchantmentCharge(ench->mData.mCharge) * 100); mHud->setSelectedEnchantItem(item, chargePercent); mSpellWindow->setTitle(item.getClass().getName(item)); } @@ -1402,7 +1387,6 @@ namespace MWGui { durabilityPercent = static_cast(item.getClass().getItemNormalizedHealth(item)); } - mHud->setSelectedWeapon(item, durabilityPercent); mInventoryWindow->setTitle(item.getClass().getName(item)); } diff --git a/apps/openmw/mwworld/cellref.cpp b/apps/openmw/mwworld/cellref.cpp index 72ee56e6a1..094669bddf 100644 --- a/apps/openmw/mwworld/cellref.cpp +++ b/apps/openmw/mwworld/cellref.cpp @@ -70,6 +70,22 @@ namespace MWWorld return mCellRef.mEnchantmentCharge; } + float CellRef::getNormalizedEnchantmentCharge(int maxCharge) const + { + if (maxCharge == 0) + { + return 0; + } + else if (mCellRef.mEnchantmentCharge == -1) + { + return 1; + } + else + { + return mCellRef.mEnchantmentCharge / static_cast(maxCharge); + } + } + void CellRef::setEnchantmentCharge(float charge) { if (charge != mCellRef.mEnchantmentCharge) diff --git a/apps/openmw/mwworld/cellref.hpp b/apps/openmw/mwworld/cellref.hpp index 7e27e6ef33..5646bafb04 100644 --- a/apps/openmw/mwworld/cellref.hpp +++ b/apps/openmw/mwworld/cellref.hpp @@ -56,6 +56,9 @@ namespace MWWorld // Remaining enchantment charge. This could be -1 if the charge was not touched yet (i.e. full). float getEnchantmentCharge() const; + // Remaining enchantment charge rescaled to the supplied maximum charge (such as one of the enchantment). + float getNormalizedEnchantmentCharge(int maxCharge) const; + void setEnchantmentCharge(float charge); // For weapon or armor, this is the remaining item health.