Project SkyFire Core
SkyFire 5.4.8 server core API documentation
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"
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
18{
20 float chance;
21
23 : ench(0), chance(0) { }
24
25 EnchStoreItem(uint32 _ench, float _chance)
26 : ench(_ench), chance(_chance) { }
27};
28
29typedef std::vector<EnchStoreItem> EnchStoreList;
31
33
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
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
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
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;
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:
159 suffixFactor = 3;
160 break;
161 case INVTYPE_RANGED:
162 case INVTYPE_THROWN:
164 suffixFactor = 4;
165 break;
166 default:
167 return 0;
168 }
169 // Select rare/epic modifier
170 switch (itemProto->Quality)
171 {
173 return randomProperty->UncommonPropertiesPoints[suffixFactor];
175 return randomProperty->RarePropertiesPoints[suffixFactor];
177 return randomProperty->EpicPropertiesPoints[suffixFactor];
180 return 0; // not have random properties
181 default:
182 break;
183 }
184 return 0;
185}
DBCStorage< RandomPropertiesPointsEntry > sRandomPropertiesPointsStore(RandomPropertiesPointsfmt)
std::int32_t int32
Definition Define.h:73
std::uint32_t uint32
Definition Define.h:77
uint32 GenerateEnchSuffixFactor(uint32 item_id)
UNORDERED_MAP< uint32, EnchStoreList > EnchantmentStore
void LoadRandomEnchantmentsTable()
static EnchantmentStore RandomItemEnch
uint32 GetItemEnchantMod(int32 entry)
std::vector< EnchStoreItem > EnchStoreList
@ INVTYPE_BODY
@ INVTYPE_FINGER
@ INVTYPE_HEAD
@ INVTYPE_CLOAK
@ INVTYPE_ROBE
@ INVTYPE_HOLDABLE
@ INVTYPE_TRINKET
@ INVTYPE_RELIC
@ INVTYPE_RANGED
@ INVTYPE_THROWN
@ INVTYPE_WAIST
@ INVTYPE_RANGEDRIGHT
@ INVTYPE_WRISTS
@ INVTYPE_WEAPON
@ INVTYPE_WEAPONMAINHAND
@ INVTYPE_WEAPONOFFHAND
@ INVTYPE_2HWEAPON
@ INVTYPE_BAG
@ INVTYPE_NECK
@ INVTYPE_SHOULDERS
@ INVTYPE_FEET
@ INVTYPE_NON_EQUIP
@ INVTYPE_AMMO
@ INVTYPE_QUIVER
@ INVTYPE_SHIELD
@ INVTYPE_TABARD
@ INVTYPE_LEGS
@ INVTYPE_CHEST
@ INVTYPE_HANDS
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
#define SF_LOG_INFO(filterType__,...)
Definition Log.h:137
#define sObjectMgr
Definition ObjectMgr.h:1617
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
Definition QueryResult.h:48
@ ITEM_QUALITY_UNCOMMON
@ ITEM_QUALITY_RARE
@ ITEM_QUALITY_LEGENDARY
@ ITEM_QUALITY_ARTIFACT
@ ITEM_QUALITY_EPIC
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:22
uint32 getMSTime()
Definition Timer.h:12
#define UNORDERED_MAP
double rand_chance(void)
Definition Util.cpp:55
Definition Field.h:16
float GetFloat() const
Definition Field.h:177
uint32 GetUInt32() const
Definition Field.h:105
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
Definition Main.cpp:40
EnchStoreItem(uint32 _ench, float _chance)
uint32 InventoryType
uint32 EpicPropertiesPoints[5]
uint32 UncommonPropertiesPoints[5]
uint32 RarePropertiesPoints[5]