AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ItemEnchantmentMgr.cpp File Reference
#include "ItemEnchantmentMgr.h"
#include "DBCStores.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "Util.h"
#include <cmath>
#include <functional>
#include <vector>

Go to the source code of this file.

Classes

struct  EnchStoreItem
 

Typedefs

typedef std::vector< EnchStoreItemEnchStoreList
 
typedef std::unordered_map< uint32, EnchStoreListEnchantmentStore
 

Functions

void LoadRandomEnchantmentsTable ()
 
uint32 GetItemEnchantMod (int32 entry)
 
uint32 GenerateEnchSuffixFactor (uint32 item_id)
 

Variables

static EnchantmentStore RandomItemEnch
 

Typedef Documentation

◆ EnchantmentStore

typedef std::unordered_map<uint32, EnchStoreList> EnchantmentStore

◆ EnchStoreList

typedef std::vector<EnchStoreItem> EnchStoreList

Function Documentation

◆ GenerateEnchSuffixFactor()

uint32 GenerateEnchSuffixFactor ( uint32  item_id)
124{
125 ItemTemplate const* itemProto = sObjectMgr->GetItemTemplate(item_id);
126
127 if (!itemProto)
128 return 0;
129 if (!itemProto->RandomSuffix)
130 return 0;
131
132 RandomPropertiesPointsEntry const* randomProperty = sRandomPropertiesPointsStore.LookupEntry(itemProto->ItemLevel);
133 if (!randomProperty)
134 return 0;
135
136 uint32 suffixFactor;
137 switch (itemProto->InventoryType)
138 {
139 // Items of that type don`t have points
141 case INVTYPE_BAG:
142 case INVTYPE_TABARD:
143 case INVTYPE_AMMO:
144 case INVTYPE_QUIVER:
145 case INVTYPE_RELIC:
146 return 0;
147 // Select point coefficient
148 case INVTYPE_HEAD:
149 case INVTYPE_BODY:
150 case INVTYPE_CHEST:
151 case INVTYPE_LEGS:
152 case INVTYPE_2HWEAPON:
153 case INVTYPE_ROBE:
154 suffixFactor = 0;
155 break;
157 case INVTYPE_WAIST:
158 case INVTYPE_FEET:
159 case INVTYPE_HANDS:
160 case INVTYPE_TRINKET:
161 suffixFactor = 1;
162 break;
163 case INVTYPE_NECK:
164 case INVTYPE_WRISTS:
165 case INVTYPE_FINGER:
166 case INVTYPE_SHIELD:
167 case INVTYPE_CLOAK:
168 case INVTYPE_HOLDABLE:
169 suffixFactor = 2;
170 break;
171 case INVTYPE_WEAPON:
174 suffixFactor = 3;
175 break;
176 case INVTYPE_RANGED:
177 case INVTYPE_THROWN:
179 suffixFactor = 4;
180 break;
181 default:
182 return 0;
183 }
184 // Select rare/epic modifier
185 switch (itemProto->Quality)
186 {
188 return randomProperty->UncommonPropertiesPoints[suffixFactor];
190 return randomProperty->RarePropertiesPoints[suffixFactor];
192 return randomProperty->EpicPropertiesPoints[suffixFactor];
195 return 0; // not have random properties
196 default:
197 break;
198 }
199 return 0;
200}
std::uint32_t uint32
Definition: Define.h:107
DBCStorage< RandomPropertiesPointsEntry > sRandomPropertiesPointsStore(RandomPropertiesPointsfmt)
#define sObjectMgr
Definition: ObjectMgr.h:1632
@ INVTYPE_BODY
Definition: ItemTemplate.h:260
@ INVTYPE_FINGER
Definition: ItemTemplate.h:267
@ INVTYPE_HEAD
Definition: ItemTemplate.h:257
@ INVTYPE_CLOAK
Definition: ItemTemplate.h:272
@ INVTYPE_ROBE
Definition: ItemTemplate.h:276
@ INVTYPE_HOLDABLE
Definition: ItemTemplate.h:279
@ INVTYPE_TRINKET
Definition: ItemTemplate.h:268
@ INVTYPE_RELIC
Definition: ItemTemplate.h:284
@ INVTYPE_RANGED
Definition: ItemTemplate.h:271
@ INVTYPE_THROWN
Definition: ItemTemplate.h:281
@ INVTYPE_WAIST
Definition: ItemTemplate.h:262
@ INVTYPE_RANGEDRIGHT
Definition: ItemTemplate.h:282
@ INVTYPE_WRISTS
Definition: ItemTemplate.h:265
@ INVTYPE_WEAPON
Definition: ItemTemplate.h:269
@ INVTYPE_WEAPONMAINHAND
Definition: ItemTemplate.h:277
@ INVTYPE_WEAPONOFFHAND
Definition: ItemTemplate.h:278
@ INVTYPE_2HWEAPON
Definition: ItemTemplate.h:273
@ INVTYPE_BAG
Definition: ItemTemplate.h:274
@ INVTYPE_NECK
Definition: ItemTemplate.h:258
@ INVTYPE_SHOULDERS
Definition: ItemTemplate.h:259
@ INVTYPE_FEET
Definition: ItemTemplate.h:264
@ INVTYPE_NON_EQUIP
Definition: ItemTemplate.h:256
@ INVTYPE_AMMO
Definition: ItemTemplate.h:280
@ INVTYPE_QUIVER
Definition: ItemTemplate.h:283
@ INVTYPE_SHIELD
Definition: ItemTemplate.h:270
@ INVTYPE_TABARD
Definition: ItemTemplate.h:275
@ INVTYPE_LEGS
Definition: ItemTemplate.h:263
@ INVTYPE_CHEST
Definition: ItemTemplate.h:261
@ INVTYPE_HANDS
Definition: ItemTemplate.h:266
@ ITEM_QUALITY_UNCOMMON
Definition: SharedDefines.h:331
@ ITEM_QUALITY_RARE
Definition: SharedDefines.h:332
@ ITEM_QUALITY_LEGENDARY
Definition: SharedDefines.h:334
@ ITEM_QUALITY_ARTIFACT
Definition: SharedDefines.h:335
@ ITEM_QUALITY_EPIC
Definition: SharedDefines.h:333
Definition: ItemTemplate.h:619
uint32 Quality
Definition: ItemTemplate.h:626
uint32 ItemLevel
Definition: ItemTemplate.h:635
uint32 InventoryType
Definition: ItemTemplate.h:632
int32 RandomSuffix
Definition: ItemTemplate.h:673
Definition: DBCStructure.h:1463
uint32 EpicPropertiesPoints[5]
Definition: DBCStructure.h:1466
uint32 UncommonPropertiesPoints[5]
Definition: DBCStructure.h:1468
uint32 RarePropertiesPoints[5]
Definition: DBCStructure.h:1467

References RandomPropertiesPointsEntry::EpicPropertiesPoints, ItemTemplate::InventoryType, INVTYPE_2HWEAPON, INVTYPE_AMMO, INVTYPE_BAG, INVTYPE_BODY, INVTYPE_CHEST, INVTYPE_CLOAK, INVTYPE_FEET, INVTYPE_FINGER, INVTYPE_HANDS, INVTYPE_HEAD, INVTYPE_HOLDABLE, INVTYPE_LEGS, INVTYPE_NECK, INVTYPE_NON_EQUIP, INVTYPE_QUIVER, INVTYPE_RANGED, INVTYPE_RANGEDRIGHT, INVTYPE_RELIC, INVTYPE_ROBE, INVTYPE_SHIELD, INVTYPE_SHOULDERS, INVTYPE_TABARD, INVTYPE_THROWN, INVTYPE_TRINKET, INVTYPE_WAIST, INVTYPE_WEAPON, INVTYPE_WEAPONMAINHAND, INVTYPE_WEAPONOFFHAND, INVTYPE_WRISTS, ITEM_QUALITY_ARTIFACT, ITEM_QUALITY_EPIC, ITEM_QUALITY_LEGENDARY, ITEM_QUALITY_RARE, ITEM_QUALITY_UNCOMMON, ItemTemplate::ItemLevel, ItemTemplate::Quality, ItemTemplate::RandomSuffix, RandomPropertiesPointsEntry::RarePropertiesPoints, sObjectMgr, sRandomPropertiesPointsStore, and RandomPropertiesPointsEntry::UncommonPropertiesPoints.

Referenced by LootItem::LootItem(), and Item::UpdateItemSuffixFactor().

◆ GetItemEnchantMod()

uint32 GetItemEnchantMod ( int32  entry)
83{
84 if (!entry)
85 return 0;
86
87 if (entry == -1)
88 return 0;
89
90 EnchantmentStore::const_iterator tab = RandomItemEnch.find(entry);
91 if (tab == RandomItemEnch.end())
92 {
93 LOG_ERROR("sql.sql", "Item RandomProperty / RandomSuffix id #{} used in `item_template` but it does not have records in `item_enchantment_template` table.", entry);
94 return 0;
95 }
96
97 double dRoll = rand_chance();
98 float fCount = 0;
99
100 for (EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
101 {
102 fCount += ench_iter->chance;
103
104 if (fCount > dRoll)
105 return ench_iter->ench;
106 }
107
108 //we could get here only if sum of all enchantment chances is lower than 100%
109 dRoll = (irand(0, (int)std::floor(fCount * 100) + 1)) / 100;
110 fCount = 0;
111
112 for (EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
113 {
114 fCount += ench_iter->chance;
115
116 if (fCount > dRoll)
117 return ench_iter->ench;
118 }
119
120 return 0;
121}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
double rand_chance()
Definition: Random.cpp:83
int32 irand(int32 min, int32 max)
Definition: Random.cpp:37
static EnchantmentStore RandomItemEnch
Definition: ItemEnchantmentMgr.cpp:43

References irand(), LOG_ERROR, rand_chance(), and RandomItemEnch.

Referenced by Item::GenerateItemRandomPropertyId(), and ObjectMgr::LoadItemTemplates().

◆ LoadRandomEnchantmentsTable()

void LoadRandomEnchantmentsTable ( )
46{
47 uint32 oldMSTime = getMSTime();
48
49 RandomItemEnch.clear(); // for reload case
50
51 // 0 1 2
52 QueryResult result = WorldDatabase.Query("SELECT entry, ench, chance FROM item_enchantment_template");
53
54 if (result)
55 {
56 uint32 count = 0;
57
58 do
59 {
60 Field* fields = result->Fetch();
61
62 uint32 entry = fields[0].Get<uint32>();
63 uint32 ench = fields[1].Get<uint32>();
64 float chance = fields[2].Get<float>();
65
66 if (chance > 0.000001f && chance <= 100.0f)
67 RandomItemEnch[entry].push_back(EnchStoreItem(ench, chance));
68
69 ++count;
70 } while (result->NextRow());
71
72 LOG_INFO("server.loading", ">> Loaded {} Item Enchantment Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
73 LOG_INFO("server.loading", " ");
74 }
75 else
76 {
77 LOG_WARN("server.loading", ">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty.");
78 LOG_INFO("server.loading", " ");
79 }
80}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_WARN(filterType__,...)
Definition: Log.h:161
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
uint32 getMSTime()
Definition: Timer.h:103
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:27
Class used to access individual fields of database query result.
Definition: Field.h:98
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:112
Definition: ItemEnchantmentMgr.cpp:29

References Field::Get(), getMSTime(), GetMSTimeDiffToNow(), LOG_INFO, LOG_WARN, RandomItemEnch, and WorldDatabase.

Referenced by reload_commandscript::HandleReloadItemEnchantementsCommand(), and World::SetInitialWorldSettings().

Variable Documentation

◆ RandomItemEnch

EnchantmentStore RandomItemEnch
static