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 "QueryResult.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)
125{
126 ItemTemplate const* itemProto = sObjectMgr->GetItemTemplate(item_id);
127
128 if (!itemProto)
129 return 0;
130 if (!itemProto->RandomSuffix)
131 return 0;
132
133 RandomPropertiesPointsEntry const* randomProperty = sRandomPropertiesPointsStore.LookupEntry(itemProto->ItemLevel);
134 if (!randomProperty)
135 return 0;
136
137 uint32 suffixFactor;
138 switch (itemProto->InventoryType)
139 {
140 // Items of that type don`t have points
142 case INVTYPE_BAG:
143 case INVTYPE_TABARD:
144 case INVTYPE_AMMO:
145 case INVTYPE_QUIVER:
146 case INVTYPE_RELIC:
147 return 0;
148 // Select point coefficient
149 case INVTYPE_HEAD:
150 case INVTYPE_BODY:
151 case INVTYPE_CHEST:
152 case INVTYPE_LEGS:
153 case INVTYPE_2HWEAPON:
154 case INVTYPE_ROBE:
155 suffixFactor = 0;
156 break;
158 case INVTYPE_WAIST:
159 case INVTYPE_FEET:
160 case INVTYPE_HANDS:
161 case INVTYPE_TRINKET:
162 suffixFactor = 1;
163 break;
164 case INVTYPE_NECK:
165 case INVTYPE_WRISTS:
166 case INVTYPE_FINGER:
167 case INVTYPE_SHIELD:
168 case INVTYPE_CLOAK:
169 case INVTYPE_HOLDABLE:
170 suffixFactor = 2;
171 break;
172 case INVTYPE_WEAPON:
175 suffixFactor = 3;
176 break;
177 case INVTYPE_RANGED:
178 case INVTYPE_THROWN:
180 suffixFactor = 4;
181 break;
182 default:
183 return 0;
184 }
185 // Select rare/epic modifier
186 switch (itemProto->Quality)
187 {
189 return randomProperty->UncommonPropertiesPoints[suffixFactor];
191 return randomProperty->RarePropertiesPoints[suffixFactor];
193 return randomProperty->EpicPropertiesPoints[suffixFactor];
196 return 0; // not have random properties
197 default:
198 break;
199 }
200 return 0;
201}
std::uint32_t uint32
Definition: Define.h:107
@ 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
#define sObjectMgr
Definition: ObjectMgr.h:1635
@ 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
DBCStorage< RandomPropertiesPointsEntry > sRandomPropertiesPointsStore(RandomPropertiesPointsfmt)
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)
84{
85 if (!entry)
86 return 0;
87
88 if (entry == -1)
89 return 0;
90
91 EnchantmentStore::const_iterator tab = RandomItemEnch.find(entry);
92 if (tab == RandomItemEnch.end())
93 {
94 LOG_ERROR("sql.sql", "Item RandomProperty / RandomSuffix id #{} used in `item_template` but it does not have records in `item_enchantment_template` table.", entry);
95 return 0;
96 }
97
98 double dRoll = rand_chance();
99 float fCount = 0;
100
101 for (EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
102 {
103 fCount += ench_iter->chance;
104
105 if (fCount > dRoll)
106 return ench_iter->ench;
107 }
108
109 //we could get here only if sum of all enchantment chances is lower than 100%
110 dRoll = (irand(0, (int)std::floor(fCount * 100) + 1)) / 100;
111 fCount = 0;
112
113 for (EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
114 {
115 fCount += ench_iter->chance;
116
117 if (fCount > dRoll)
118 return ench_iter->ench;
119 }
120
121 return 0;
122}
#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:44

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

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

◆ LoadRandomEnchantmentsTable()

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

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