aboutsummaryrefslogtreecommitdiff
path: root/src/game/ItemEnchantmentMgr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/ItemEnchantmentMgr.cpp')
-rw-r--r--src/game/ItemEnchantmentMgr.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/game/ItemEnchantmentMgr.cpp b/src/game/ItemEnchantmentMgr.cpp
index 0f62d1d8d52..b40a398a782 100644
--- a/src/game/ItemEnchantmentMgr.cpp
+++ b/src/game/ItemEnchantmentMgr.cpp
@@ -17,6 +17,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
#include <stdlib.h>
#include <functional>
#include "ItemEnchantmentMgr.h"
@@ -27,41 +28,56 @@
#include <list>
#include <vector>
#include "Util.h"
+
struct EnchStoreItem
{
uint32 ench;
float chance;
+
EnchStoreItem()
: ench(0), chance(0) {}
+
EnchStoreItem(uint32 _ench, float _chance)
: ench(_ench), chance(_chance) {}
};
+
typedef std::vector<EnchStoreItem> EnchStoreList;
typedef UNORDERED_MAP<uint32, EnchStoreList> EnchantmentStore;
+
static EnchantmentStore RandomItemEnch;
+
void LoadRandomEnchantmentsTable()
{
RandomItemEnch.clear(); // for reload case
+
EnchantmentStore::const_iterator tab;
uint32 entry, ench;
float chance;
uint32 count = 0;
+
QueryResult *result = WorldDatabase.Query("SELECT entry, ench, chance FROM item_enchantment_template");
+
if (result)
{
barGoLink bar(result->GetRowCount());
+
do
{
Field *fields = result->Fetch();
bar.step();
+
entry = fields[0].GetUInt32();
ench = fields[1].GetUInt32();
chance = fields[2].GetFloat();
+
if (chance > 0.000001f && chance <= 100.0f)
RandomItemEnch[entry].push_back( EnchStoreItem(ench, chance) );
+
++count;
} while (result->NextRow());
+
delete result;
+
sLog.outString();
sLog.outString( ">> Loaded %u Item Enchantment definitions", count );
}
@@ -71,42 +87,56 @@ void LoadRandomEnchantmentsTable()
sLog.outErrorDb( ">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty.");
}
}
+
uint32 GetItemEnchantMod(uint32 entry)
{
if (!entry) return 0;
+
EnchantmentStore::const_iterator tab = RandomItemEnch.find(entry);
+
if (tab == RandomItemEnch.end())
{
sLog.outErrorDb("Item RandomProperty / RandomSuffix id #%u used in `item_template` but it doesn't have records in `item_enchantment_template` table.",entry);
return 0;
}
+
double dRoll = rand_chance();
float fCount = 0;
+
for(EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
{
fCount += ench_iter->chance;
+
if (fCount > dRoll) return ench_iter->ench;
}
+
//we could get here only if sum of all enchantment chances is lower than 100%
dRoll = (irand(0, (int)floor(fCount * 100) + 1)) / 100;
fCount = 0;
+
for(EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
{
fCount += ench_iter->chance;
+
if (fCount > dRoll) return ench_iter->ench;
}
+
return 0;
}
+
uint32 GenerateEnchSuffixFactor(uint32 item_id)
{
ItemPrototype const *itemProto = objmgr.GetItemPrototype(item_id);
+
if(!itemProto)
return 0;
if(!itemProto->RandomSuffix)
return 0;
+
RandomPropertiesPointsEntry const *randomProperty = sRandomPropertiesPointsStore.LookupEntry(itemProto->ItemLevel);
if(!randomProperty)
return 0;
+
uint32 suffixFactor;
switch(itemProto->InventoryType)
{