Merge pull request #1982 from Capostrophic/condition

Avoid item condition and charge zero divisions
pull/541/head
Bret Curtis 6 years ago committed by GitHub
commit 0fad2449f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -297,7 +297,7 @@ namespace MWClass
{ {
const MWWorld::InventoryStore& invStore = npc.getClass().getInventoryStore(npc); const MWWorld::InventoryStore& invStore = npc.getClass().getInventoryStore(npc);
if (ptr.getCellRef().getCharge() == 0) if (getItemHealth(ptr) == 0)
return std::make_pair(0, "#{sInventoryMessage1}"); return std::make_pair(0, "#{sInventoryMessage1}");
// slots that this item can be equipped in // slots that this item can be equipped in

@ -1148,9 +1148,7 @@ namespace MWClass
const bool hasHealth = it->getClass().hasItemHealth(*it); const bool hasHealth = it->getClass().hasItemHealth(*it);
if (hasHealth) if (hasHealth)
{ {
int armorHealth = it->getClass().getItemHealth(*it); ratings[i] *= it->getClass().getItemNormalizedHealth(*it);
int armorMaxHealth = it->getClass().getItemMaxHealth(*it);
ratings[i] *= (float(armorHealth) / armorMaxHealth);
} }
} }
} }

@ -383,7 +383,7 @@ namespace MWClass
std::pair<int, std::string> Weapon::canBeEquipped(const MWWorld::ConstPtr &ptr, const MWWorld::Ptr &npc) const std::pair<int, std::string> Weapon::canBeEquipped(const MWWorld::ConstPtr &ptr, const MWWorld::Ptr &npc) const
{ {
if (hasItemHealth(ptr) && ptr.getCellRef().getCharge() == 0) if (hasItemHealth(ptr) && getItemHealth(ptr) == 0)
return std::make_pair(0, "#{sInventoryMessage1}"); return std::make_pair(0, "#{sInventoryMessage1}");
// Do not allow equip weapons from inventory during attack // Do not allow equip weapons from inventory during attack

@ -51,7 +51,7 @@ void MerchantRepair::setPtr(const MWWorld::Ptr &actor)
{ {
int maxDurability = iter->getClass().getItemMaxHealth(*iter); int maxDurability = iter->getClass().getItemMaxHealth(*iter);
int durability = iter->getClass().getItemHealth(*iter); int durability = iter->getClass().getItemHealth(*iter);
if (maxDurability == durability) if (maxDurability == durability || maxDurability == 0)
continue; continue;
int basePrice = iter->getClass().getValue(*iter); int basePrice = iter->getClass().getValue(*iter);

@ -91,8 +91,7 @@ namespace
if (ench->mData.mType == ESM::Enchantment::ConstantEffect) if (ench->mData.mType == ESM::Enchantment::ConstantEffect)
leftChargePercent = 101; leftChargePercent = 101;
else else
leftChargePercent = (left.mBase.getCellRef().getEnchantmentCharge() == -1) ? 100 leftChargePercent = static_cast<int>(left.mBase.getCellRef().getNormalizedEnchantmentCharge(ench->mData.mCharge) * 100);
: static_cast<int>(left.mBase.getCellRef().getEnchantmentCharge() / static_cast<float>(ench->mData.mCharge) * 100);
} }
} }
@ -104,8 +103,7 @@ namespace
if (ench->mData.mType == ESM::Enchantment::ConstantEffect) if (ench->mData.mType == ESM::Enchantment::ConstantEffect)
rightChargePercent = 101; rightChargePercent = 101;
else else
rightChargePercent = (right.mBase.getCellRef().getEnchantmentCharge() == -1) ? 100 rightChargePercent = static_cast<int>(right.mBase.getCellRef().getNormalizedEnchantmentCharge(ench->mData.mCharge) * 100);
: static_cast<int>(right.mBase.getCellRef().getEnchantmentCharge() / static_cast<float>(ench->mData.mCharge) * 100);
} }
} }

@ -35,8 +35,7 @@ namespace
float price = static_cast<float>(item.getClass().getValue(item)); float price = static_cast<float>(item.getClass().getValue(item));
if (item.getClass().hasItemHealth(item)) if (item.getClass().hasItemHealth(item))
{ {
price *= item.getClass().getItemHealth(item); price *= item.getClass().getItemNormalizedHealth(item);
price /= item.getClass().getItemMaxHealth(item);
} }
return static_cast<int>(price * count); return static_cast<int>(price * count);
} }

@ -1369,8 +1369,7 @@ namespace MWGui
const ESM::Enchantment* ench = mStore->get<ESM::Enchantment>() const ESM::Enchantment* ench = mStore->get<ESM::Enchantment>()
.find(item.getClass().getEnchantment(item)); .find(item.getClass().getEnchantment(item));
int chargePercent = (item.getCellRef().getEnchantmentCharge() == -1) ? 100 int chargePercent = static_cast<int>(item.getCellRef().getNormalizedEnchantmentCharge(ench->mData.mCharge) * 100);
: static_cast<int>(item.getCellRef().getEnchantmentCharge() / static_cast<float>(ench->mData.mCharge) * 100);
mHud->setSelectedEnchantItem(item, chargePercent); mHud->setSelectedEnchantItem(item, chargePercent);
mSpellWindow->setTitle(item.getClass().getName(item)); mSpellWindow->setTitle(item.getClass().getName(item));
} }
@ -1386,7 +1385,7 @@ namespace MWGui
int durabilityPercent = 100; int durabilityPercent = 100;
if (item.getClass().hasItemHealth(item)) if (item.getClass().hasItemHealth(item))
{ {
durabilityPercent = static_cast<int>(item.getClass().getItemHealth(item) / static_cast<float>(item.getClass().getItemMaxHealth(item)) * 100); durabilityPercent = static_cast<int>(item.getClass().getItemNormalizedHealth(item));
} }
mHud->setSelectedWeapon(item, durabilityPercent); mHud->setSelectedWeapon(item, durabilityPercent);
mInventoryWindow->setTitle(item.getClass().getName(item)); mInventoryWindow->setTitle(item.getClass().getName(item));

@ -371,11 +371,9 @@ namespace MWMechanics
return; return;
const bool weaphashealth = weapon.getClass().hasItemHealth(weapon); const bool weaphashealth = weapon.getClass().hasItemHealth(weapon);
if(weaphashealth) if (weaphashealth)
{ {
int weaphealth = weapon.getClass().getItemHealth(weapon); damage *= weapon.getClass().getItemNormalizedHealth(weapon);
int weapmaxhealth = weapon.getClass().getItemMaxHealth(weapon);
damage *= (float(weaphealth) / weapmaxhealth);
} }
static const float fDamageStrengthBase = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>() static const float fDamageStrengthBase = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()

@ -70,6 +70,22 @@ namespace MWWorld
return mCellRef.mEnchantmentCharge; 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<float>(maxCharge);
}
}
void CellRef::setEnchantmentCharge(float charge) void CellRef::setEnchantmentCharge(float charge)
{ {
if (charge != mCellRef.mEnchantmentCharge) if (charge != mCellRef.mEnchantmentCharge)

@ -56,6 +56,9 @@ namespace MWWorld
// Remaining enchantment charge. This could be -1 if the charge was not touched yet (i.e. full). // Remaining enchantment charge. This could be -1 if the charge was not touched yet (i.e. full).
float getEnchantmentCharge() const; 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); void setEnchantmentCharge(float charge);
// For weapon or armor, this is the remaining item health. // For weapon or armor, this is the remaining item health.

@ -83,6 +83,18 @@ namespace MWWorld
return ptr.getCellRef().getCharge(); return ptr.getCellRef().getCharge();
} }
float Class::getItemNormalizedHealth (const ConstPtr& ptr) const
{
if (getItemMaxHealth(ptr) == 0)
{
return 0.f;
}
else
{
return getItemHealth(ptr) / static_cast<float>(getItemMaxHealth(ptr));
}
}
int Class::getItemMaxHealth (const ConstPtr& ptr) const int Class::getItemMaxHealth (const ConstPtr& ptr) const
{ {
throw std::runtime_error ("class does not have item health"); throw std::runtime_error ("class does not have item health");

@ -112,6 +112,9 @@ namespace MWWorld
virtual int getItemHealth (const ConstPtr& ptr) const; virtual int getItemHealth (const ConstPtr& ptr) const;
///< Return current item health or throw an exception if class does not have item health ///< 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; virtual int getItemMaxHealth (const ConstPtr& ptr) const;
///< Return item max health or throw an exception, if class does not have item health ///< Return item max health or throw an exception, if class does not have item health
/// (default implementation: throw an exception) /// (default implementation: throw an exception)

Loading…
Cancel
Save