Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
ItemEnchantmentMgr.cpp
Go to the documentation of this file.
1
/*
2
* This file is part of Project SkyFire https://www.projectskyfire.org.
3
* See LICENSE.md file for Copyright information
4
*/
5
6
#include "
DatabaseEnv.h
"
7
#include "
DBCStores.h
"
8
#include "
ItemEnchantmentMgr.h
"
9
#include "
Log.h
"
10
#include "
ObjectMgr.h
"
11
#include "
Util.h
"
12
#include <functional>
13
#include <list>
14
#include <stdlib.h>
15
#include <vector>
16
17
struct
EnchStoreItem
18
{
19
uint32
ench
;
20
float
chance
;
21
22
EnchStoreItem
()
23
:
ench
(0),
chance
(0) { }
24
25
EnchStoreItem
(
uint32
_ench,
float
_chance)
26
:
ench
(_ench),
chance
(_chance) { }
27
};
28
29
typedef
std::vector<EnchStoreItem>
EnchStoreList
;
30
typedef
UNORDERED_MAP<uint32, EnchStoreList>
EnchantmentStore
;
31
32
static
EnchantmentStore
RandomItemEnch
;
33
34
void
LoadRandomEnchantmentsTable
()
35
{
36
uint32
oldMSTime =
getMSTime
();
37
38
RandomItemEnch
.clear();
// for reload case
39
40
// 0 1 2
41
QueryResult
result =
WorldDatabase
.Query(
"SELECT entry, ench, chance FROM item_enchantment_template"
);
42
43
if
(result)
44
{
45
uint32
count = 0;
46
47
do
48
{
49
Field
* fields = result->Fetch();
50
51
uint32
entry = fields[0].
GetUInt32
();
52
uint32
ench = fields[1].
GetUInt32
();
53
float
chance = fields[2].
GetFloat
();
54
55
if
(chance > 0.000001f && chance <= 100.0f)
56
RandomItemEnch
[entry].push_back(
EnchStoreItem
(ench, chance));
57
58
++count;
59
}
while
(result->NextRow());
60
61
SF_LOG_INFO
(
"server.loading"
,
">> Loaded %u Item Enchantment definitions in %u ms"
, count,
GetMSTimeDiffToNow
(oldMSTime));
62
}
63
else
64
SF_LOG_ERROR
(
"server.loading"
,
">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty."
);
65
}
66
67
uint32
GetItemEnchantMod
(
int32
entry)
68
{
69
if
(!entry)
70
return
0;
71
72
if
(entry == -1)
73
return
0;
74
75
EnchantmentStore::const_iterator tab =
RandomItemEnch
.find(entry);
76
if
(tab ==
RandomItemEnch
.end())
77
{
78
SF_LOG_ERROR
(
"sql.sql"
,
"Item RandomProperty / RandomSuffix id #%u used in `item_template` but it does not have records in `item_enchantment_template` table."
, entry);
79
return
0;
80
}
81
82
double
dRoll =
rand_chance
();
83
float
fCount = 0;
84
85
for
(EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
86
{
87
fCount += ench_iter->chance;
88
89
if
(fCount > dRoll)
90
return
ench_iter->ench;
91
}
92
93
//we could get here only if sum of all enchantment chances is lower than 100%
94
dRoll = (std::rand() % ((int)floor(fCount * 100) + 1)) / 100;
95
fCount = 0;
96
97
for
(EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
98
{
99
fCount += ench_iter->chance;
100
101
if
(fCount > dRoll)
102
return
ench_iter->ench;
103
}
104
105
return
0;
106
}
107
108
uint32
GenerateEnchSuffixFactor
(
uint32
item_id)
109
{
110
ItemTemplate
const
* itemProto =
sObjectMgr
->GetItemTemplate(item_id);
111
112
if
(!itemProto)
113
return
0;
114
if
(!itemProto->
RandomSuffix
)
115
return
0;
116
117
RandomPropertiesPointsEntry
const
* randomProperty =
sRandomPropertiesPointsStore
.LookupEntry(itemProto->
ItemLevel
);
118
if
(!randomProperty)
119
return
0;
120
121
uint32
suffixFactor;
122
switch
(itemProto->
InventoryType
)
123
{
124
// Items of that type don`t have points
125
case
INVTYPE_NON_EQUIP
:
126
case
INVTYPE_BAG
:
127
case
INVTYPE_TABARD
:
128
case
INVTYPE_AMMO
:
129
case
INVTYPE_QUIVER
:
130
case
INVTYPE_RELIC
:
131
return
0;
132
// Select point coefficient
133
case
INVTYPE_HEAD
:
134
case
INVTYPE_BODY
:
135
case
INVTYPE_CHEST
:
136
case
INVTYPE_LEGS
:
137
case
INVTYPE_2HWEAPON
:
138
case
INVTYPE_ROBE
:
139
suffixFactor = 0;
140
break
;
141
case
INVTYPE_SHOULDERS
:
142
case
INVTYPE_WAIST
:
143
case
INVTYPE_FEET
:
144
case
INVTYPE_HANDS
:
145
case
INVTYPE_TRINKET
:
146
suffixFactor = 1;
147
break
;
148
case
INVTYPE_NECK
:
149
case
INVTYPE_WRISTS
:
150
case
INVTYPE_FINGER
:
151
case
INVTYPE_SHIELD
:
152
case
INVTYPE_CLOAK
:
153
case
INVTYPE_HOLDABLE
:
154
suffixFactor = 2;
155
break
;
156
case
INVTYPE_WEAPON
:
157
case
INVTYPE_WEAPONMAINHAND
:
158
case
INVTYPE_WEAPONOFFHAND
:
159
suffixFactor = 3;
160
break
;
161
case
INVTYPE_RANGED
:
162
case
INVTYPE_THROWN
:
163
case
INVTYPE_RANGEDRIGHT
:
164
suffixFactor = 4;
165
break
;
166
default
:
167
return
0;
168
}
169
// Select rare/epic modifier
170
switch
(itemProto->
Quality
)
171
{
172
case
ITEM_QUALITY_UNCOMMON
:
173
return
randomProperty->
UncommonPropertiesPoints
[suffixFactor];
174
case
ITEM_QUALITY_RARE
:
175
return
randomProperty->
RarePropertiesPoints
[suffixFactor];
176
case
ITEM_QUALITY_EPIC
:
177
return
randomProperty->
EpicPropertiesPoints
[suffixFactor];
178
case
ITEM_QUALITY_LEGENDARY
:
179
case
ITEM_QUALITY_ARTIFACT
:
180
return
0;
// not have random properties
181
default
:
182
break
;
183
}
184
return
0;
185
}
sRandomPropertiesPointsStore
DBCStorage< RandomPropertiesPointsEntry > sRandomPropertiesPointsStore(RandomPropertiesPointsfmt)
DBCStores.h
DatabaseEnv.h
int32
std::int32_t int32
Definition
Define.h:73
uint32
std::uint32_t uint32
Definition
Define.h:77
GenerateEnchSuffixFactor
uint32 GenerateEnchSuffixFactor(uint32 item_id)
Definition
ItemEnchantmentMgr.cpp:108
EnchantmentStore
UNORDERED_MAP< uint32, EnchStoreList > EnchantmentStore
Definition
ItemEnchantmentMgr.cpp:30
LoadRandomEnchantmentsTable
void LoadRandomEnchantmentsTable()
Definition
ItemEnchantmentMgr.cpp:34
RandomItemEnch
static EnchantmentStore RandomItemEnch
Definition
ItemEnchantmentMgr.cpp:32
GetItemEnchantMod
uint32 GetItemEnchantMod(int32 entry)
Definition
ItemEnchantmentMgr.cpp:67
EnchStoreList
std::vector< EnchStoreItem > EnchStoreList
Definition
ItemEnchantmentMgr.cpp:29
ItemEnchantmentMgr.h
INVTYPE_BODY
@ INVTYPE_BODY
Definition
ItemPrototype.h:258
INVTYPE_FINGER
@ INVTYPE_FINGER
Definition
ItemPrototype.h:265
INVTYPE_HEAD
@ INVTYPE_HEAD
Definition
ItemPrototype.h:255
INVTYPE_CLOAK
@ INVTYPE_CLOAK
Definition
ItemPrototype.h:270
INVTYPE_ROBE
@ INVTYPE_ROBE
Definition
ItemPrototype.h:274
INVTYPE_HOLDABLE
@ INVTYPE_HOLDABLE
Definition
ItemPrototype.h:277
INVTYPE_TRINKET
@ INVTYPE_TRINKET
Definition
ItemPrototype.h:266
INVTYPE_RELIC
@ INVTYPE_RELIC
Definition
ItemPrototype.h:282
INVTYPE_RANGED
@ INVTYPE_RANGED
Definition
ItemPrototype.h:269
INVTYPE_THROWN
@ INVTYPE_THROWN
Definition
ItemPrototype.h:279
INVTYPE_WAIST
@ INVTYPE_WAIST
Definition
ItemPrototype.h:260
INVTYPE_RANGEDRIGHT
@ INVTYPE_RANGEDRIGHT
Definition
ItemPrototype.h:280
INVTYPE_WRISTS
@ INVTYPE_WRISTS
Definition
ItemPrototype.h:263
INVTYPE_WEAPON
@ INVTYPE_WEAPON
Definition
ItemPrototype.h:267
INVTYPE_WEAPONMAINHAND
@ INVTYPE_WEAPONMAINHAND
Definition
ItemPrototype.h:275
INVTYPE_WEAPONOFFHAND
@ INVTYPE_WEAPONOFFHAND
Definition
ItemPrototype.h:276
INVTYPE_2HWEAPON
@ INVTYPE_2HWEAPON
Definition
ItemPrototype.h:271
INVTYPE_BAG
@ INVTYPE_BAG
Definition
ItemPrototype.h:272
INVTYPE_NECK
@ INVTYPE_NECK
Definition
ItemPrototype.h:256
INVTYPE_SHOULDERS
@ INVTYPE_SHOULDERS
Definition
ItemPrototype.h:257
INVTYPE_FEET
@ INVTYPE_FEET
Definition
ItemPrototype.h:262
INVTYPE_NON_EQUIP
@ INVTYPE_NON_EQUIP
Definition
ItemPrototype.h:254
INVTYPE_AMMO
@ INVTYPE_AMMO
Definition
ItemPrototype.h:278
INVTYPE_QUIVER
@ INVTYPE_QUIVER
Definition
ItemPrototype.h:281
INVTYPE_SHIELD
@ INVTYPE_SHIELD
Definition
ItemPrototype.h:268
INVTYPE_TABARD
@ INVTYPE_TABARD
Definition
ItemPrototype.h:273
INVTYPE_LEGS
@ INVTYPE_LEGS
Definition
ItemPrototype.h:261
INVTYPE_CHEST
@ INVTYPE_CHEST
Definition
ItemPrototype.h:259
INVTYPE_HANDS
@ INVTYPE_HANDS
Definition
ItemPrototype.h:264
Log.h
SF_LOG_ERROR
#define SF_LOG_ERROR(filterType__,...)
Definition
Log.h:143
SF_LOG_INFO
#define SF_LOG_INFO(filterType__,...)
Definition
Log.h:137
ObjectMgr.h
sObjectMgr
#define sObjectMgr
Definition
ObjectMgr.h:1617
QueryResult
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
Definition
QueryResult.h:48
ITEM_QUALITY_UNCOMMON
@ ITEM_QUALITY_UNCOMMON
Definition
SharedDefines.h:304
ITEM_QUALITY_RARE
@ ITEM_QUALITY_RARE
Definition
SharedDefines.h:305
ITEM_QUALITY_LEGENDARY
@ ITEM_QUALITY_LEGENDARY
Definition
SharedDefines.h:307
ITEM_QUALITY_ARTIFACT
@ ITEM_QUALITY_ARTIFACT
Definition
SharedDefines.h:308
ITEM_QUALITY_EPIC
@ ITEM_QUALITY_EPIC
Definition
SharedDefines.h:306
GetMSTimeDiffToNow
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition
Timer.h:22
getMSTime
uint32 getMSTime()
Definition
Timer.h:12
UNORDERED_MAP
#define UNORDERED_MAP
Definition
UnorderedMap.h:58
rand_chance
double rand_chance(void)
Definition
Util.cpp:55
Util.h
Field
Definition
Field.h:16
Field::GetFloat
float GetFloat() const
Definition
Field.h:177
Field::GetUInt32
uint32 GetUInt32() const
Definition
Field.h:105
WorldDatabase
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
Definition
Main.cpp:40
EnchStoreItem
Definition
ItemEnchantmentMgr.cpp:18
EnchStoreItem::chance
float chance
Definition
ItemEnchantmentMgr.cpp:20
EnchStoreItem::EnchStoreItem
EnchStoreItem()
Definition
ItemEnchantmentMgr.cpp:22
EnchStoreItem::ench
uint32 ench
Definition
ItemEnchantmentMgr.cpp:19
EnchStoreItem::EnchStoreItem
EnchStoreItem(uint32 _ench, float _chance)
Definition
ItemEnchantmentMgr.cpp:25
ItemTemplate
Definition
ItemPrototype.h:628
ItemTemplate::Quality
uint32 Quality
Definition
ItemPrototype.h:644
ItemTemplate::ItemLevel
uint32 ItemLevel
Definition
ItemPrototype.h:656
ItemTemplate::InventoryType
uint32 InventoryType
Definition
ItemPrototype.h:653
ItemTemplate::RandomSuffix
int32 RandomSuffix
Definition
ItemPrototype.h:684
RandomPropertiesPointsEntry
Definition
DBCStructure.h:2136
RandomPropertiesPointsEntry::EpicPropertiesPoints
uint32 EpicPropertiesPoints[5]
Definition
DBCStructure.h:2138
RandomPropertiesPointsEntry::UncommonPropertiesPoints
uint32 UncommonPropertiesPoints[5]
Definition
DBCStructure.h:2140
RandomPropertiesPointsEntry::RarePropertiesPoints
uint32 RarePropertiesPoints[5]
Definition
DBCStructure.h:2139
src
server
game
Entities
Item
ItemEnchantmentMgr.cpp
Generated on
for Project SkyFire Core by
1.17.0