2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ItemEnchantmentMgr.h"
22 #include "Database/DatabaseEnv.h"
24 #include "ObjectMgr.h"
25 #include "ProgressBar.h"
36 : ench(0), chance(0) {}
38 EnchStoreItem(uint32 _ench
, float _chance
)
39 : ench(_ench
), chance(_chance
) {}
42 typedef std::vector
<EnchStoreItem
> EnchStoreList
;
43 typedef UNORDERED_MAP
<uint32
, EnchStoreList
> EnchantmentStore
;
45 static EnchantmentStore RandomItemEnch
;
47 void LoadRandomEnchantmentsTable()
49 RandomItemEnch
.clear(); // for reload case
51 EnchantmentStore::const_iterator tab
;
56 QueryResult
* result
= WorldDatabase
.Query("SELECT entry, ench, chance FROM item_enchantment_template");
60 BarGoLink
bar(result
->GetRowCount());
64 Field
* fields
= result
->Fetch();
67 entry
= fields
[0].GetUInt32();
68 ench
= fields
[1].GetUInt32();
69 chance
= fields
[2].GetFloat();
71 if (chance
> 0.000001f
&& chance
<= 100.0f
)
72 RandomItemEnch
[entry
].push_back(EnchStoreItem(ench
, chance
));
76 while (result
->NextRow());
81 sLog
.outString(">> Loaded %u Item Enchantment definitions", count
);
86 sLog
.outErrorDb(">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty.");
90 uint32
GetItemEnchantMod(uint32 entry
)
94 EnchantmentStore::const_iterator tab
= RandomItemEnch
.find(entry
);
96 if (tab
== RandomItemEnch
.end())
98 sLog
.outErrorDb("Item RandomProperty / RandomSuffix id #%u used in `item_template` but it doesn't have records in `item_enchantment_template` table.", entry
);
102 double dRoll
= rand_chance();
105 for (EnchStoreList::const_iterator ench_iter
= tab
->second
.begin(); ench_iter
!= tab
->second
.end(); ++ench_iter
)
107 fCount
+= ench_iter
->chance
;
109 if (fCount
> dRoll
) return ench_iter
->ench
;
112 // we could get here only if sum of all enchantment chances is lower than 100%
113 dRoll
= (irand(0, (int)floor(fCount
* 100) + 1)) / 100;
116 for (EnchStoreList::const_iterator ench_iter
= tab
->second
.begin(); ench_iter
!= tab
->second
.end(); ++ench_iter
)
118 fCount
+= ench_iter
->chance
;
120 if (fCount
> dRoll
) return ench_iter
->ench
;
126 uint32
GenerateEnchSuffixFactor(uint32 item_id
)
128 ItemPrototype
const* itemProto
= ObjectMgr::GetItemPrototype(item_id
);
132 if (!itemProto
->RandomSuffix
)
135 RandomPropertiesPointsEntry
const* randomProperty
= sRandomPropertiesPointsStore
.LookupEntry(itemProto
->ItemLevel
);
140 switch (itemProto
->InventoryType
)
142 // Items of that type don`t have points
143 case INVTYPE_NON_EQUIP
:
150 // Select point coefficient
155 case INVTYPE_2HWEAPON
:
159 case INVTYPE_SHOULDERS
:
163 case INVTYPE_TRINKET
:
171 case INVTYPE_HOLDABLE
:
175 case INVTYPE_WEAPONMAINHAND
:
176 case INVTYPE_WEAPONOFFHAND
:
181 case INVTYPE_RANGEDRIGHT
:
187 // Select rare/epic modifier
188 switch (itemProto
->Quality
)
190 case ITEM_QUALITY_UNCOMMON
:
191 return randomProperty
->UncommonPropertiesPoints
[suffixFactor
];
192 case ITEM_QUALITY_RARE
:
193 return randomProperty
->RarePropertiesPoints
[suffixFactor
];
194 case ITEM_QUALITY_EPIC
:
195 return randomProperty
->EpicPropertiesPoints
[suffixFactor
];
196 case ITEM_QUALITY_LEGENDARY
:
197 case ITEM_QUALITY_ARTIFACT
:
198 return 0; // not have random properties