1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 10:49:57 +00:00
openmw-tes3mp/apps/openmw/mwmechanics/enchanting.cpp

303 lines
10 KiB
C++
Raw Normal View History

2013-03-28 16:41:00 +00:00
#include "enchanting.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
2013-04-02 18:46:48 +00:00
#include "../mwbase/mechanicsmanager.hpp"
2013-03-30 18:08:42 +00:00
#include "creaturestats.hpp"
#include "npcstats.hpp"
2013-03-28 16:41:00 +00:00
namespace MWMechanics
{
2013-07-31 16:46:32 +00:00
Enchanting::Enchanting()
: mCastStyle(ESM::Enchantment::CastOnce)
, mSelfEnchanting(false)
2013-03-28 16:41:00 +00:00
{}
void Enchanting::setOldItem(MWWorld::Ptr oldItem)
{
mOldItemPtr=oldItem;
if(!itemEmpty())
{
mObjectType = mOldItemPtr.getTypeName();
mOldItemId = mOldItemPtr.getCellRef().mRefID;
}
else
{
mObjectType="";
mOldItemId="";
}
}
void Enchanting::setNewItemName(const std::string& s)
2013-03-28 16:41:00 +00:00
{
mNewItemName=s;
}
void Enchanting::setEffect(ESM::EffectList effectList)
{
mEffectList=effectList;
}
int Enchanting::getCastStyle() const
2013-03-28 16:41:00 +00:00
{
return mCastStyle;
2013-03-28 16:41:00 +00:00
}
void Enchanting::setSoulGem(MWWorld::Ptr soulGem)
{
mSoulGemPtr=soulGem;
}
bool Enchanting::create()
2013-03-28 16:41:00 +00:00
{
const MWWorld::Ptr& player = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWWorld::ContainerStore& store = MWWorld::Class::get(player).getContainerStore(player);
ESM::Enchantment enchantment;
enchantment.mData.mCharge = getGemCharge();
store.remove(mSoulGemPtr, 1, player);
2013-04-08 15:53:41 +00:00
//Exception for Azura Star, new one will be added after enchanting
2014-01-14 08:47:31 +00:00
if(Misc::StringUtils::ciEqual(mSoulGemPtr.get<ESM::Miscellaneous>()->mBase->mId, "Misc_SoulGem_Azura"))
store.add("Misc_SoulGem_Azura", 1, player);
2013-03-29 11:00:09 +00:00
2013-03-30 18:08:42 +00:00
if(mSelfEnchanting)
{
if(getEnchantChance()<std::rand()/static_cast<double> (RAND_MAX)*100)
return false;
2013-03-30 18:08:42 +00:00
MWWorld::Class::get (mEnchanter).skillUsageSucceeded (mEnchanter, ESM::Skill::Enchant, 2);
2013-03-30 18:08:42 +00:00
}
if(mCastStyle==ESM::Enchantment::ConstantEffect)
2013-03-28 16:41:00 +00:00
{
enchantment.mData.mCharge=0;
2013-03-28 16:41:00 +00:00
}
enchantment.mData.mType = mCastStyle;
2013-05-27 18:23:04 +00:00
enchantment.mData.mCost = getEnchantPoints();
enchantment.mEffects = mEffectList;
2013-03-31 21:18:23 +00:00
// Create a new item
MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), mOldItemId, 1);
const MWWorld::Ptr& newItemPtr = ref.getPtr();
2013-03-31 21:18:23 +00:00
// Apply the enchantment
const ESM::Enchantment *enchantmentPtr = MWBase::Environment::get().getWorld()->createRecord (enchantment);
MWWorld::Class::get(newItemPtr).applyEnchantment(newItemPtr, enchantmentPtr->mId, getGemCharge(), mNewItemName);
2013-03-31 21:18:23 +00:00
// Add the new item to player inventory and remove the old one
store.remove(mOldItemPtr, 1, player);
store.add(newItemPtr, 1, player);
2013-04-02 18:46:48 +00:00
2013-04-03 16:02:30 +00:00
if(!mSelfEnchanting)
payForEnchantment();
2013-03-28 16:41:00 +00:00
return true;
2013-03-28 16:41:00 +00:00
}
void Enchanting::nextCastStyle()
2013-03-28 16:41:00 +00:00
{
if (itemEmpty())
{
mCastStyle = ESM::Enchantment::WhenUsed;
2013-03-28 16:41:00 +00:00
return;
}
const bool powerfulSoul = getGemCharge() >= \
2013-05-27 18:47:53 +00:00
MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find ("iSoulAmountForConstantEffect")->getInt();
if ((mObjectType == typeid(ESM::Armor).name()) || (mObjectType == typeid(ESM::Clothing).name()))
{ // Armor or Clothing
switch(mCastStyle)
2013-03-28 16:41:00 +00:00
{
case ESM::Enchantment::WhenUsed:
if (powerfulSoul)
mCastStyle = ESM::Enchantment::ConstantEffect;
return;
default: // takes care of Constant effect too
mCastStyle = ESM::Enchantment::WhenUsed;
return;
2013-03-28 16:41:00 +00:00
}
}
else if(mObjectType == typeid(ESM::Weapon).name())
{ // Weapon
switch(mCastStyle)
2013-03-28 16:41:00 +00:00
{
case ESM::Enchantment::WhenStrikes:
mCastStyle = ESM::Enchantment::WhenUsed;
2013-05-27 18:47:53 +00:00
return;
case ESM::Enchantment::WhenUsed:
2013-05-27 18:47:53 +00:00
if (powerfulSoul)
mCastStyle = ESM::Enchantment::ConstantEffect;
2013-05-27 18:47:53 +00:00
else
mCastStyle = ESM::Enchantment::WhenStrikes;
2013-05-27 18:47:53 +00:00
return;
default: // takes care of Constant effect too
mCastStyle = ESM::Enchantment::WhenStrikes;
2013-05-27 18:47:53 +00:00
return;
2013-03-28 16:41:00 +00:00
}
}
else if(mObjectType == typeid(ESM::Book).name())
{ // Scroll or Book
mCastStyle = ESM::Enchantment::CastOnce;
return;
2013-03-28 16:41:00 +00:00
}
// Fail case
mCastStyle = ESM::Enchantment::CastOnce;
2013-03-28 16:41:00 +00:00
}
2013-05-27 18:47:53 +00:00
/*
* Vanilla enchant cost formula:
*
* Touch/Self: (min + max) * baseCost * 0.025 * duration + area * baseCost * 0.025
* Target: 1.5 * ((min + max) * baseCost * 0.025 * duration + area * baseCost * 0.025)
* Constant eff: (min + max) * baseCost * 2.5 + area * baseCost * 0.025
*
* For multiple effects - cost of each effect is multiplied by number of effects that follows +1.
*
* Note: Minimal value inside formula for 'min' and 'max' is 1. So in vanilla:
* (0 + 0) == (1 + 0) == (1 + 1) => 2 or (2 + 0) == (1 + 2) => 3
*
* Formula on UESPWiki is not entirely correct.
*/
2013-05-27 18:23:04 +00:00
float Enchanting::getEnchantPoints() const
2013-03-28 16:41:00 +00:00
{
2013-05-27 18:47:53 +00:00
if (mEffectList.mList.empty())
// No effects added, cost = 0
return 0;
2013-03-28 16:41:00 +00:00
const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
std::vector<ESM::ENAMstruct> mEffects = mEffectList.mList;
2013-03-30 18:08:42 +00:00
float enchantmentCost = 0;
int effectsLeftCnt = mEffects.size();
float baseCost, magnitudeCost, areaCost;
int magMin, magMax, area;
2013-03-28 16:41:00 +00:00
for (std::vector<ESM::ENAMstruct>::const_iterator it = mEffects.begin(); it != mEffects.end(); ++it)
{
baseCost = (store.get<ESM::MagicEffect>().find(it->mEffectID))->mData.mBaseCost;
// To reflect vanilla behavior
magMin = (it->mMagnMin == 0) ? 1 : it->mMagnMin;
magMax = (it->mMagnMax == 0) ? 1 : it->mMagnMax;
area = (it->mArea == 0) ? 1 : it->mArea;
2013-03-28 16:41:00 +00:00
if (mCastStyle == ESM::Enchantment::ConstantEffect)
2013-03-28 16:41:00 +00:00
{
2013-05-27 18:47:53 +00:00
magnitudeCost = (magMin + magMax) * baseCost * 2.5;
}
else
{
2013-05-27 18:47:53 +00:00
magnitudeCost = (magMin + magMax) * it->mDuration * baseCost * 0.025;
if(it->mRange == ESM::RT_Target)
magnitudeCost *= 1.5;
2013-03-28 16:41:00 +00:00
}
areaCost = area * 0.025 * baseCost;
if (it->mRange == ESM::RT_Target)
2013-05-27 18:47:53 +00:00
areaCost *= 1.5;
2013-03-28 16:41:00 +00:00
enchantmentCost += (magnitudeCost + areaCost) * effectsLeftCnt;
--effectsLeftCnt;
2013-03-28 16:41:00 +00:00
}
return enchantmentCost;
2013-03-28 16:41:00 +00:00
}
2013-04-02 18:46:48 +00:00
2013-05-27 16:08:12 +00:00
float Enchanting::getCastCost() const
{
if (mCastStyle == ESM::Enchantment::ConstantEffect)
2013-05-27 18:47:53 +00:00
return 0;
2013-05-27 18:47:53 +00:00
const float enchantCost = getEnchantPoints();
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
2013-05-27 18:47:53 +00:00
MWMechanics::NpcStats &stats = MWWorld::Class::get(player).getNpcStats(player);
int eSkill = stats.getSkill(ESM::Skill::Enchant).getModified();
2013-05-27 16:08:12 +00:00
2013-05-27 18:47:53 +00:00
/*
* Each point of enchant skill above/under 10 subtracts/adds
* one percent of enchantment cost while minimum is 1.
*/
const float castCost = enchantCost - (enchantCost / 100) * (eSkill - 10);
2013-05-27 16:08:12 +00:00
2013-05-27 18:47:53 +00:00
return (castCost < 1) ? 1 : castCost;
2013-05-27 16:08:12 +00:00
}
2013-04-02 18:46:48 +00:00
int Enchanting::getEnchantPrice() const
{
if(mEnchanter.isEmpty())
return 0;
float priceMultipler = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find ("fEnchantmentValueMult")->getFloat();
2013-05-27 18:23:04 +00:00
int price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mEnchanter, (getEnchantPoints() * priceMultipler), true);
2013-04-02 18:46:48 +00:00
return price;
}
int Enchanting::getGemCharge() const
2013-03-28 16:41:00 +00:00
{
const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
if(soulEmpty())
return 0;
if(mSoulGemPtr.getCellRef().mSoul=="")
return 0;
const ESM::Creature* soul = store.get<ESM::Creature>().find(mSoulGemPtr.getCellRef().mSoul);
return soul->mData.mSoul;
}
2013-04-29 08:19:09 +00:00
float Enchanting::getMaxEnchantValue() const
2013-03-28 16:41:00 +00:00
{
if (itemEmpty())
return 0;
2014-01-27 00:58:04 +00:00
const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
return mOldItemPtr.getClass().getEnchantmentPoints(mOldItemPtr) * store.get<ESM::GameSetting>().find("fEnchantmentMult")->getFloat();
2013-03-28 16:41:00 +00:00
}
bool Enchanting::soulEmpty() const
2013-03-28 16:41:00 +00:00
{
return mSoulGemPtr.isEmpty();
2013-03-28 16:41:00 +00:00
}
bool Enchanting::itemEmpty() const
2013-03-28 16:41:00 +00:00
{
return mOldItemPtr.isEmpty();
2013-03-28 16:41:00 +00:00
}
2013-03-30 18:08:42 +00:00
void Enchanting::setSelfEnchanting(bool selfEnchanting)
{
mSelfEnchanting = selfEnchanting;
}
void Enchanting::setEnchanter(MWWorld::Ptr enchanter)
{
mEnchanter = enchanter;
}
float Enchanting::getEnchantChance() const
2013-03-30 18:08:42 +00:00
{
const NpcStats& npcStats = MWWorld::Class::get (mEnchanter).getNpcStats (mEnchanter);
float chance1 = (npcStats.getSkill (ESM::Skill::Enchant).getModified() +
2014-01-27 00:58:04 +00:00
(0.25 * npcStats.getAttribute (ESM::Attribute::Intelligence).getModified())
+ (0.125 * npcStats.getAttribute (ESM::Attribute::Luck).getModified()));
const MWWorld::Store<ESM::GameSetting>& gmst = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
float chance2 = 7.5 / (gmst.find("fEnchantmentChanceMult")->getFloat() * ((mCastStyle == ESM::Enchantment::ConstantEffect) ?
gmst.find("fEnchantmentConstantChanceMult")->getFloat() : 1 ))
* getEnchantPoints();
2013-03-30 18:08:42 +00:00
return (chance1-chance2);
}
2013-04-02 18:46:48 +00:00
void Enchanting::payForEnchantment() const
{
const MWWorld::Ptr& player = MWBase::Environment::get().getWorld()->getPlayerPtr();
2013-04-02 18:46:48 +00:00
MWWorld::ContainerStore& store = MWWorld::Class::get(player).getContainerStore(player);
store.remove(MWWorld::ContainerStore::sGoldId, getEnchantPrice(), player);
2013-04-02 18:46:48 +00:00
}
2013-03-28 16:41:00 +00:00
}