1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 21:32:42 +00:00
OpenMW/apps/openmw/mwmechanics/enchanting.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

402 lines
14 KiB
C++
Raw Normal View History

2013-03-28 17:41:00 +01:00
#include "enchanting.hpp"
#include <components/esm3/loadcrea.hpp>
#include <components/esm3/loadmgef.hpp>
2015-04-22 17:58:55 +02:00
#include <components/misc/rng.hpp>
2023-06-27 23:41:06 +02:00
#include <components/settings/values.hpp>
2013-03-28 17:41:00 +01:00
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
2015-02-09 15:01:49 +01:00
#include "../mwworld/esmstore.hpp"
2016-06-17 23:07:16 +09:00
2019-02-19 01:10:55 +03:00
#include "../mwbase/environment.hpp"
2013-04-02 20:46:48 +02:00
#include "../mwbase/mechanicsmanager.hpp"
2019-02-19 01:10:55 +03:00
#include "../mwbase/world.hpp"
2013-03-30 19:08:42 +01:00
2015-08-21 21:12:39 +12:00
#include "actorutil.hpp"
2013-03-30 19:08:42 +01:00
#include "creaturestats.hpp"
#include "spellutil.hpp"
#include "weapontype.hpp"
2013-03-30 19:08:42 +01:00
2013-03-28 17:41:00 +01:00
namespace MWMechanics
{
2013-07-31 18:46:32 +02:00
Enchanting::Enchanting()
: mCastStyle(ESM::Enchantment::CastOnce)
, mSelfEnchanting(false)
, mObjectType(0)
, mWeaponType(-1)
2013-03-28 17:41:00 +01:00
{
}
2017-04-20 20:36:14 +09:00
void Enchanting::setOldItem(const MWWorld::Ptr& oldItem)
2013-03-28 17:41:00 +01:00
{
mOldItemPtr = oldItem;
mWeaponType = -1;
mObjectType = 0;
2013-03-28 17:41:00 +01:00
if (!itemEmpty())
{
mObjectType = mOldItemPtr.getType();
if (mObjectType == ESM::Weapon::sRecordId)
mWeaponType = mOldItemPtr.get<ESM::Weapon>()->mBase->mData.mType;
2013-03-28 17:41:00 +01:00
}
}
void Enchanting::setNewItemName(const std::string& s)
2013-03-28 17:41:00 +01:00
{
mNewItemName = s;
}
2017-04-20 20:36:14 +09:00
void Enchanting::setEffect(const ESM::EffectList& effectList)
2013-03-28 17:41:00 +01:00
{
mEffectList = effectList;
}
int Enchanting::getCastStyle() const
2013-03-28 17:41:00 +01:00
{
return mCastStyle;
2013-03-28 17:41:00 +01:00
}
2017-04-20 20:36:14 +09:00
void Enchanting::setSoulGem(const MWWorld::Ptr& soulGem)
2013-03-28 17:41:00 +01:00
{
mSoulGemPtr = soulGem;
}
bool Enchanting::create()
2013-03-28 17:41:00 +01:00
{
2015-08-21 21:12:39 +12:00
const MWWorld::Ptr& player = getPlayer();
MWWorld::ContainerStore& store = player.getClass().getContainerStore(player);
ESM::Enchantment enchantment;
enchantment.mData.mFlags = 0;
enchantment.mData.mType = mCastStyle;
enchantment.mData.mCost = getBaseCastCost();
2023-07-28 14:01:40 +04:00
enchantment.mRecordFlags = 0;
store.remove(mSoulGemPtr, 1);
2013-04-08 17:53:41 +02:00
// Exception for Azura Star, new one will be added after enchanting
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
auto azurasStarId = ESM::RefId::stringRefId("Misc_SoulGem_Azura");
if (mSoulGemPtr.get<ESM::Miscellaneous>()->mBase->mId == azurasStarId)
store.add(azurasStarId, 1);
2013-03-29 12:00:09 +01:00
2013-03-30 19:08:42 +01:00
if (mSelfEnchanting)
{
auto& prng = MWBase::Environment::get().getWorld()->getPrng();
if (getEnchantChance() <= (Misc::Rng::roll0to99(prng)))
return false;
2013-03-30 19:08:42 +01:00
2024-01-14 20:33:23 +01:00
mEnchanter.getClass().skillUsageSucceeded(
mEnchanter, ESM::Skill::Enchant, ESM::Skill::Enchant_CreateMagicItem);
2013-03-30 19:08:42 +01:00
}
enchantment.mEffects = mEffectList;
2013-03-31 23:18:23 +02:00
int count = getEnchantItemsCount();
if (mCastStyle == ESM::Enchantment::ConstantEffect)
enchantment.mData.mCharge = 0;
else
enchantment.mData.mCharge = getGemCharge() / count;
// Try to find a dynamic enchantment with the same stats, create a new one if not found.
const ESM::Enchantment* enchantmentPtr = getRecord(enchantment);
if (enchantmentPtr == nullptr)
2023-04-20 21:07:53 +02:00
enchantmentPtr = MWBase::Environment::get().getESMStore()->insert(enchantment);
// Apply the enchantment
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
const ESM::RefId& newItemId
2022-08-22 16:55:53 +02:00
= mOldItemPtr.getClass().applyEnchantment(mOldItemPtr, enchantmentPtr->mId, getGemCharge(), mNewItemName);
2013-03-31 23:18:23 +02:00
if (!mSelfEnchanting)
payForEnchantment(count);
// Add the new item to player inventory and remove the old one
store.remove(mOldItemPtr, count);
store.add(newItemId, count);
2013-04-02 20:46:48 +02:00
return true;
2013-03-28 17:41:00 +01:00
}
2022-09-22 21:26:05 +03:00
void Enchanting::nextCastStyle()
2013-03-28 17:41:00 +01:00
{
if (itemEmpty())
return;
const bool powerfulSoul = getGemCharge() >= MWBase::Environment::get()
2023-04-20 21:07:53 +02:00
.getESMStore()
->get<ESM::GameSetting>()
2018-08-29 18:38:12 +03:00
.find("iSoulAmountForConstantEffect")
->mValue.getInteger();
if ((mObjectType == ESM::Armor::sRecordId) || (mObjectType == ESM::Clothing::sRecordId))
{ // Armor or Clothing
switch (mCastStyle)
2013-03-28 17:41:00 +01: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 17:41:00 +01:00
}
}
else if (mWeaponType != -1)
{ // Weapon
ESM::WeaponType::Class weapclass = MWMechanics::getWeaponType(mWeaponType)->mWeaponClass;
switch (mCastStyle)
2013-03-28 17:41:00 +01:00
{
case ESM::Enchantment::WhenStrikes:
if (weapclass == ESM::WeaponType::Melee || weapclass == ESM::WeaponType::Ranged)
mCastStyle = ESM::Enchantment::WhenUsed;
2013-05-27 20:47:53 +02:00
return;
case ESM::Enchantment::WhenUsed:
if (powerfulSoul && weapclass != ESM::WeaponType::Ammo && weapclass != ESM::WeaponType::Thrown)
mCastStyle = ESM::Enchantment::ConstantEffect;
else if (weapclass != ESM::WeaponType::Ranged)
mCastStyle = ESM::Enchantment::WhenStrikes;
2013-05-27 20:47:53 +02:00
return;
default: // takes care of Constant effect too
mCastStyle = ESM::Enchantment::WhenUsed;
if (weapclass != ESM::WeaponType::Ranged)
mCastStyle = ESM::Enchantment::WhenStrikes;
2013-05-27 20:47:53 +02:00
return;
2013-03-28 17:41:00 +01:00
}
}
else if (mObjectType == ESM::Book::sRecordId)
{ // Scroll or Book
mCastStyle = ESM::Enchantment::CastOnce;
return;
2013-03-28 17:41:00 +01:00
}
// Fail case
mCastStyle = ESM::Enchantment::CastOnce;
2013-03-28 17:41:00 +01:00
}
2013-05-27 20:47:53 +02: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.
*/
float Enchanting::getEnchantPoints(bool precise) const
2013-03-28 17:41:00 +01:00
{
2013-05-27 20:47:53 +02:00
if (mEffectList.mList.empty())
// No effects added, cost = 0
return 0;
2023-04-20 21:07:53 +02:00
const MWWorld::ESMStore& store = *MWBase::Environment::get().getESMStore();
const float fEffectCostMult = store.get<ESM::GameSetting>().find("fEffectCostMult")->mValue.getFloat();
const float fEnchantmentConstantDurationMult
= store.get<ESM::GameSetting>().find("fEnchantmentConstantDurationMult")->mValue.getFloat();
2013-03-30 19:08:42 +01:00
float enchantmentCost = 0.f;
float cost = 0.f;
for (const ESM::IndexedENAMstruct& effect : mEffectList.mList)
2013-03-28 17:41:00 +01:00
{
float baseCost = (store.get<ESM::MagicEffect>().find(effect.mData.mEffectID))->mData.mBaseCost;
int magMin = std::max(1, effect.mData.mMagnMin);
int magMax = std::max(1, effect.mData.mMagnMax);
int area = std::max(1, effect.mData.mArea);
float duration = static_cast<float>(effect.mData.mDuration);
if (mCastStyle == ESM::Enchantment::ConstantEffect)
duration = fEnchantmentConstantDurationMult;
2013-03-28 17:41:00 +01:00
cost += ((magMin + magMax) * duration + area) * baseCost * fEffectCostMult * 0.05f;
cost = std::max(1.f, cost);
if (effect.mData.mRange == ESM::RT_Target)
cost *= 1.5f;
2015-01-12 23:28:52 +01:00
enchantmentCost += precise ? cost : std::floor(cost);
2013-03-28 17:41:00 +01:00
}
return enchantmentCost;
2013-03-28 17:41:00 +01:00
}
2013-04-02 20:46:48 +02:00
const ESM::Enchantment* Enchanting::getRecord(const ESM::Enchantment& toFind) const
{
const MWWorld::Store<ESM::Enchantment>& enchantments
2023-04-20 21:07:53 +02:00
= MWBase::Environment::get().getESMStore()->get<ESM::Enchantment>();
MWWorld::Store<ESM::Enchantment>::iterator iter(enchantments.begin());
iter += (enchantments.getSize() - enchantments.getDynamicSize());
for (; iter != enchantments.end(); ++iter)
{
if (iter->mEffects.mList.size() != toFind.mEffects.mList.size())
continue;
if (iter->mData.mFlags != toFind.mData.mFlags || iter->mData.mType != toFind.mData.mType
|| iter->mData.mCost != toFind.mData.mCost || iter->mData.mCharge != toFind.mData.mCharge)
continue;
// Don't choose an ID that came from the content files, would have unintended side effects
if (!enchantments.isDynamic(iter->mId))
continue;
bool mismatch = false;
for (int i = 0; i < static_cast<int>(iter->mEffects.mList.size()); ++i)
{
if (iter->mEffects.mList[i] != toFind.mEffects.mList[i])
{
mismatch = true;
break;
}
}
if (!mismatch)
return &(*iter);
}
return nullptr;
}
2013-05-27 18:08:12 +02:00
int Enchanting::getBaseCastCost() const
2013-05-27 18:08:12 +02:00
{
if (mCastStyle == ESM::Enchantment::ConstantEffect)
2013-05-27 20:47:53 +02:00
return 0;
return static_cast<int>(getEnchantPoints(false));
}
2013-05-27 18:08:12 +02:00
int Enchanting::getEffectiveCastCost() const
{
int baseCost = getBaseCastCost();
2015-08-21 21:12:39 +12:00
MWWorld::Ptr player = getPlayer();
return getEffectiveEnchantmentCastCost(static_cast<float>(baseCost), player);
2013-05-27 18:08:12 +02:00
}
int Enchanting::getEnchantPrice(int count) const
2013-04-02 20:46:48 +02:00
{
if (mEnchanter.isEmpty())
return 0;
2018-08-29 18:38:12 +03:00
float priceMultipler = MWBase::Environment::get()
2023-04-20 21:07:53 +02:00
.getESMStore()
->get<ESM::GameSetting>()
2018-08-29 18:38:12 +03:00
.find("fEnchantmentValueMult")
->mValue.getFloat();
int price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(
mEnchanter, static_cast<int>(getEnchantPoints() * priceMultipler), true);
price *= count * getTypeMultiplier();
return std::max(1, price);
2013-04-02 20:46:48 +02:00
}
int Enchanting::getGemCharge() const
2013-03-28 17:41:00 +01:00
{
2023-04-20 21:07:53 +02:00
const MWWorld::ESMStore& store = *MWBase::Environment::get().getESMStore();
2013-03-28 17:41:00 +01:00
if (soulEmpty())
return 0;
if (mSoulGemPtr.getCellRef().getSoul().empty())
2013-03-28 17:41:00 +01:00
return 0;
const ESM::Creature* soul = store.get<ESM::Creature>().search(mSoulGemPtr.getCellRef().getSoul());
if (soul)
return soul->mData.mSoul;
else
return 0;
2013-03-28 17:41:00 +01:00
}
int Enchanting::getMaxEnchantValue() const
2013-03-28 17:41:00 +01:00
{
if (itemEmpty())
return 0;
2014-01-27 01:58:04 +01:00
2023-04-20 21:07:53 +02:00
const MWWorld::ESMStore& store = *MWBase::Environment::get().getESMStore();
2014-01-27 01:58:04 +01:00
2018-08-29 18:38:12 +03:00
return static_cast<int>(mOldItemPtr.getClass().getEnchantmentPoints(mOldItemPtr)
* store.get<ESM::GameSetting>().find("fEnchantmentMult")->mValue.getFloat());
2013-03-28 17:41:00 +01:00
}
bool Enchanting::soulEmpty() const
2013-03-28 17:41:00 +01:00
{
return mSoulGemPtr.isEmpty();
2013-03-28 17:41:00 +01:00
}
bool Enchanting::itemEmpty() const
2013-03-28 17:41:00 +01:00
{
return mOldItemPtr.isEmpty();
2013-03-28 17:41:00 +01:00
}
2013-03-30 19:08:42 +01:00
void Enchanting::setSelfEnchanting(bool selfEnchanting)
{
mSelfEnchanting = selfEnchanting;
}
2017-04-20 20:36:14 +09:00
void Enchanting::setEnchanter(const MWWorld::Ptr& enchanter)
2013-03-30 19:08:42 +01:00
{
mEnchanter = enchanter;
// Reset cast style
mCastStyle = ESM::Enchantment::CastOnce;
2013-03-30 19:08:42 +01:00
}
int Enchanting::getEnchantChance() const
2013-03-30 19:08:42 +01:00
{
2018-10-08 17:06:30 +03:00
const CreatureStats& stats = mEnchanter.getClass().getCreatureStats(mEnchanter);
2014-01-27 01:58:04 +01:00
const MWWorld::Store<ESM::GameSetting>& gmst
2023-04-20 21:07:53 +02:00
= MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
const float a = static_cast<float>(mEnchanter.getClass().getSkill(mEnchanter, ESM::Skill::Enchant));
const float b = static_cast<float>(stats.getAttribute(ESM::Attribute::Intelligence).getModified());
const float c = static_cast<float>(stats.getAttribute(ESM::Attribute::Luck).getModified());
const float fEnchantmentChanceMult = gmst.find("fEnchantmentChanceMult")->mValue.getFloat();
const float fEnchantmentConstantChanceMult = gmst.find("fEnchantmentConstantChanceMult")->mValue.getFloat();
2014-01-27 01:58:04 +01:00
float x = (a - getEnchantPoints() * fEnchantmentChanceMult * getTypeMultiplier() * getEnchantItemsCount()
+ 0.2f * b + 0.1f * c)
* stats.getFatigueTerm();
if (mCastStyle == ESM::Enchantment::ConstantEffect)
x *= fEnchantmentConstantChanceMult;
2013-03-30 19:08:42 +01:00
return static_cast<int>(x);
2013-03-30 19:08:42 +01:00
}
2013-04-02 20:46:48 +02:00
int Enchanting::getEnchantItemsCount() const
{
int count = 1;
float enchantPoints = getEnchantPoints();
if (mWeaponType != -1 && enchantPoints > 0)
{
ESM::WeaponType::Class weapclass = MWMechanics::getWeaponType(mWeaponType)->mWeaponClass;
if (weapclass == ESM::WeaponType::Thrown || weapclass == ESM::WeaponType::Ammo)
{
MWWorld::Ptr player = getPlayer();
2021-11-06 07:30:28 +03:00
count = player.getClass().getContainerStore(player).count(mOldItemPtr.getCellRef().getRefId());
2023-06-27 23:41:06 +02:00
count = std::clamp<int>(
getGemCharge() * Settings::game().mProjectilesEnchantMultiplier / enchantPoints, 1, count);
}
}
return count;
}
float Enchanting::getTypeMultiplier() const
{
2023-06-27 23:41:06 +02:00
if (Settings::game().mProjectilesEnchantMultiplier > 0 && mWeaponType != -1 && getEnchantPoints() > 0)
{
ESM::WeaponType::Class weapclass = MWMechanics::getWeaponType(mWeaponType)->mWeaponClass;
if (weapclass == ESM::WeaponType::Thrown || weapclass == ESM::WeaponType::Ammo)
return 0.125f;
}
return 1.f;
}
void Enchanting::payForEnchantment(int count) const
2013-04-02 20:46:48 +02:00
{
2015-08-21 21:12:39 +12:00
const MWWorld::Ptr& player = getPlayer();
MWWorld::ContainerStore& store = player.getClass().getContainerStore(player);
2013-04-02 20:46:48 +02:00
int price = getEnchantPrice(count);
store.remove(MWWorld::ContainerStore::sGoldId, price);
// add gold to NPC trading gold pool
CreatureStats& enchanterStats = mEnchanter.getClass().getCreatureStats(mEnchanter);
enchanterStats.setGoldPool(enchanterStats.getGoldPool() + price);
2013-04-02 20:46:48 +02:00
}
2013-03-28 17:41:00 +01:00
}