2 * Copyright (C) 2005-2009 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
) );
75 } while (result
->NextRow());
80 sLog
.outString( ">> Loaded %u Item Enchantment definitions", count
);
85 sLog
.outErrorDb( ">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty.");
89 uint32
GetItemEnchantMod(uint32 entry
)
93 EnchantmentStore::const_iterator tab
= RandomItemEnch
.find(entry
);
95 if (tab
== RandomItemEnch
.end())
97 sLog
.outErrorDb("Item RandomProperty / RandomSuffix id #%u used in `item_template` but it doesn't have records in `item_enchantment_template` table.",entry
);
101 double dRoll
= rand_chance();
104 for(EnchStoreList::const_iterator ench_iter
= tab
->second
.begin(); ench_iter
!= tab
->second
.end(); ++ench_iter
)
106 fCount
+= ench_iter
->chance
;
108 if (fCount
> dRoll
) return ench_iter
->ench
;
111 //we could get here only if sum of all enchantment chances is lower than 100%
112 dRoll
= (irand(0, (int)floor(fCount
* 100) + 1)) / 100;
115 for(EnchStoreList::const_iterator ench_iter
= tab
->second
.begin(); ench_iter
!= tab
->second
.end(); ++ench_iter
)
117 fCount
+= ench_iter
->chance
;
119 if (fCount
> dRoll
) return ench_iter
->ench
;
125 uint32
GenerateEnchSuffixFactor(uint32 item_id
)
127 ItemPrototype
const *itemProto
= ObjectMgr::GetItemPrototype(item_id
);
131 if(!itemProto
->RandomSuffix
)
134 RandomPropertiesPointsEntry
const *randomProperty
= sRandomPropertiesPointsStore
.LookupEntry(itemProto
->ItemLevel
);
139 switch(itemProto
->InventoryType
)
141 // Items of that type don`t have points
142 case INVTYPE_NON_EQUIP
:
149 // Select point coefficient
154 case INVTYPE_2HWEAPON
:
158 case INVTYPE_SHOULDERS
:
162 case INVTYPE_TRINKET
:
170 case INVTYPE_HOLDABLE
:
174 case INVTYPE_WEAPONMAINHAND
:
175 case INVTYPE_WEAPONOFFHAND
:
180 case INVTYPE_RANGEDRIGHT
:
186 // Select rare/epic modifier
187 switch (itemProto
->Quality
)
189 case ITEM_QUALITY_UNCOMMON
:
190 return randomProperty
->UncommonPropertiesPoints
[suffixFactor
];
191 case ITEM_QUALITY_RARE
:
192 return randomProperty
->RarePropertiesPoints
[suffixFactor
];
193 case ITEM_QUALITY_EPIC
:
194 return randomProperty
->EpicPropertiesPoints
[suffixFactor
];
195 case ITEM_QUALITY_LEGENDARY
:
196 case ITEM_QUALITY_ARTIFACT
:
197 return 0; // not have random properties