/
enchanting.cpp
401 lines (338 loc) · 14.5 KB
/
enchanting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "enchanting.hpp"
#include <components/esm3/loadcrea.hpp>
#include <components/esm3/loadmgef.hpp>
#include <components/misc/rng.hpp>
#include <components/settings/values.hpp>
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp"
#include "../mwbase/world.hpp"
#include "actorutil.hpp"
#include "creaturestats.hpp"
#include "spellutil.hpp"
#include "weapontype.hpp"
namespace MWMechanics
{
Enchanting::Enchanting()
: mCastStyle(ESM::Enchantment::CastOnce)
, mSelfEnchanting(false)
, mObjectType(0)
, mWeaponType(-1)
{
}
void Enchanting::setOldItem(const MWWorld::Ptr& oldItem)
{
mOldItemPtr = oldItem;
mWeaponType = -1;
mObjectType = 0;
if (!itemEmpty())
{
mObjectType = mOldItemPtr.getType();
if (mObjectType == ESM::Weapon::sRecordId)
mWeaponType = mOldItemPtr.get<ESM::Weapon>()->mBase->mData.mType;
}
}
void Enchanting::setNewItemName(const std::string& s)
{
mNewItemName = s;
}
void Enchanting::setEffect(const ESM::EffectList& effectList)
{
mEffectList = effectList;
}
int Enchanting::getCastStyle() const
{
return mCastStyle;
}
void Enchanting::setSoulGem(const MWWorld::Ptr& soulGem)
{
mSoulGemPtr = soulGem;
}
bool Enchanting::create()
{
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();
enchantment.mRecordFlags = 0;
store.remove(mSoulGemPtr, 1);
// Exception for Azura Star, new one will be added after enchanting
auto azurasStarId = ESM::RefId::stringRefId("Misc_SoulGem_Azura");
if (mSoulGemPtr.get<ESM::Miscellaneous>()->mBase->mId == azurasStarId)
store.add(azurasStarId, 1);
if (mSelfEnchanting)
{
auto& prng = MWBase::Environment::get().getWorld()->getPrng();
if (getEnchantChance() <= (Misc::Rng::roll0to99(prng)))
return false;
mEnchanter.getClass().skillUsageSucceeded(
mEnchanter, ESM::Skill::Enchant, ESM::Skill::Enchant_CreateMagicItem);
}
enchantment.mEffects = mEffectList;
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)
enchantmentPtr = MWBase::Environment::get().getESMStore()->insert(enchantment);
// Apply the enchantment
const ESM::RefId& newItemId
= mOldItemPtr.getClass().applyEnchantment(mOldItemPtr, enchantmentPtr->mId, getGemCharge(), mNewItemName);
if (!mSelfEnchanting)
payForEnchantment(count);
// Add the new item to player inventory and remove the old one
store.remove(mOldItemPtr, count);
store.add(newItemId, count);
return true;
}
void Enchanting::nextCastStyle()
{
if (itemEmpty())
return;
const bool powerfulSoul = getGemCharge() >= MWBase::Environment::get()
.getESMStore()
->get<ESM::GameSetting>()
.find("iSoulAmountForConstantEffect")
->mValue.getInteger();
if ((mObjectType == ESM::Armor::sRecordId) || (mObjectType == ESM::Clothing::sRecordId))
{ // Armor or Clothing
switch (mCastStyle)
{
case ESM::Enchantment::WhenUsed:
if (powerfulSoul)
mCastStyle = ESM::Enchantment::ConstantEffect;
return;
default: // takes care of Constant effect too
mCastStyle = ESM::Enchantment::WhenUsed;
return;
}
}
else if (mWeaponType != -1)
{ // Weapon
ESM::WeaponType::Class weapclass = MWMechanics::getWeaponType(mWeaponType)->mWeaponClass;
switch (mCastStyle)
{
case ESM::Enchantment::WhenStrikes:
if (weapclass == ESM::WeaponType::Melee || weapclass == ESM::WeaponType::Ranged)
mCastStyle = ESM::Enchantment::WhenUsed;
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;
return;
default: // takes care of Constant effect too
mCastStyle = ESM::Enchantment::WhenUsed;
if (weapclass != ESM::WeaponType::Ranged)
mCastStyle = ESM::Enchantment::WhenStrikes;
return;
}
}
else if (mObjectType == ESM::Book::sRecordId)
{ // Scroll or Book
mCastStyle = ESM::Enchantment::CastOnce;
return;
}
// Fail case
mCastStyle = ESM::Enchantment::CastOnce;
}
/*
* 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
{
if (mEffectList.mList.empty())
// No effects added, cost = 0
return 0;
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();
float enchantmentCost = 0.f;
float cost = 0.f;
for (const ESM::IndexedENAMstruct& effect : mEffectList.mList)
{
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;
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;
enchantmentCost += precise ? cost : std::floor(cost);
}
return enchantmentCost;
}
const ESM::Enchantment* Enchanting::getRecord(const ESM::Enchantment& toFind) const
{
const MWWorld::Store<ESM::Enchantment>& enchantments
= 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;
}
int Enchanting::getBaseCastCost() const
{
if (mCastStyle == ESM::Enchantment::ConstantEffect)
return 0;
return static_cast<int>(getEnchantPoints(false));
}
int Enchanting::getEffectiveCastCost() const
{
int baseCost = getBaseCastCost();
MWWorld::Ptr player = getPlayer();
return getEffectiveEnchantmentCastCost(static_cast<float>(baseCost), player);
}
int Enchanting::getEnchantPrice(int count) const
{
if (mEnchanter.isEmpty())
return 0;
float priceMultipler = MWBase::Environment::get()
.getESMStore()
->get<ESM::GameSetting>()
.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);
}
int Enchanting::getGemCharge() const
{
const MWWorld::ESMStore& store = *MWBase::Environment::get().getESMStore();
if (soulEmpty())
return 0;
if (mSoulGemPtr.getCellRef().getSoul().empty())
return 0;
const ESM::Creature* soul = store.get<ESM::Creature>().search(mSoulGemPtr.getCellRef().getSoul());
if (soul)
return soul->mData.mSoul;
else
return 0;
}
int Enchanting::getMaxEnchantValue() const
{
if (itemEmpty())
return 0;
const MWWorld::ESMStore& store = *MWBase::Environment::get().getESMStore();
return static_cast<int>(mOldItemPtr.getClass().getEnchantmentPoints(mOldItemPtr)
* store.get<ESM::GameSetting>().find("fEnchantmentMult")->mValue.getFloat());
}
bool Enchanting::soulEmpty() const
{
return mSoulGemPtr.isEmpty();
}
bool Enchanting::itemEmpty() const
{
return mOldItemPtr.isEmpty();
}
void Enchanting::setSelfEnchanting(bool selfEnchanting)
{
mSelfEnchanting = selfEnchanting;
}
void Enchanting::setEnchanter(const MWWorld::Ptr& enchanter)
{
mEnchanter = enchanter;
// Reset cast style
mCastStyle = ESM::Enchantment::CastOnce;
}
int Enchanting::getEnchantChance() const
{
const CreatureStats& stats = mEnchanter.getClass().getCreatureStats(mEnchanter);
const MWWorld::Store<ESM::GameSetting>& gmst
= 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();
float x = (a - getEnchantPoints() * fEnchantmentChanceMult * getTypeMultiplier() * getEnchantItemsCount()
+ 0.2f * b + 0.1f * c)
* stats.getFatigueTerm();
if (mCastStyle == ESM::Enchantment::ConstantEffect)
x *= fEnchantmentConstantChanceMult;
return static_cast<int>(x);
}
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();
count = player.getClass().getContainerStore(player).count(mOldItemPtr.getCellRef().getRefId());
count = std::clamp<int>(
getGemCharge() * Settings::game().mProjectilesEnchantMultiplier / enchantPoints, 1, count);
}
}
return count;
}
float Enchanting::getTypeMultiplier() const
{
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
{
const MWWorld::Ptr& player = getPlayer();
MWWorld::ContainerStore& store = player.getClass().getContainerStore(player);
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);
}
}