1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <functional> |
---|
23 | #include "ItemEnchantmentMgr.h" |
---|
24 | #include "Database/DatabaseEnv.h" |
---|
25 | #include "Log.h" |
---|
26 | #include "ObjectMgr.h" |
---|
27 | #include "ProgressBar.h" |
---|
28 | #include <list> |
---|
29 | #include <vector> |
---|
30 | #include "Util.h" |
---|
31 | |
---|
32 | struct EnchStoreItem |
---|
33 | { |
---|
34 | uint32 ench; |
---|
35 | float chance; |
---|
36 | |
---|
37 | EnchStoreItem() |
---|
38 | : ench(0), chance(0) {} |
---|
39 | |
---|
40 | EnchStoreItem(uint32 _ench, float _chance) |
---|
41 | : ench(_ench), chance(_chance) {} |
---|
42 | }; |
---|
43 | |
---|
44 | typedef std::vector<EnchStoreItem> EnchStoreList; |
---|
45 | typedef HM_NAMESPACE::hash_map<uint32, EnchStoreList> EnchantmentStore; |
---|
46 | |
---|
47 | static EnchantmentStore RandomItemEnch; |
---|
48 | |
---|
49 | void LoadRandomEnchantmentsTable() |
---|
50 | { |
---|
51 | RandomItemEnch.clear(); // for reload case |
---|
52 | |
---|
53 | EnchantmentStore::iterator tab; |
---|
54 | uint32 entry, ench; |
---|
55 | float chance; |
---|
56 | uint32 count = 0; |
---|
57 | |
---|
58 | QueryResult *result = WorldDatabase.Query("SELECT entry, ench, chance FROM item_enchantment_template"); |
---|
59 | |
---|
60 | if (result) |
---|
61 | { |
---|
62 | barGoLink bar(result->GetRowCount()); |
---|
63 | |
---|
64 | do |
---|
65 | { |
---|
66 | Field *fields = result->Fetch(); |
---|
67 | bar.step(); |
---|
68 | |
---|
69 | entry = fields[0].GetUInt32(); |
---|
70 | ench = fields[1].GetUInt32(); |
---|
71 | chance = fields[2].GetFloat(); |
---|
72 | |
---|
73 | if (chance > 0.000001f && chance <= 100.0f) |
---|
74 | RandomItemEnch[entry].push_back( EnchStoreItem(ench, chance) ); |
---|
75 | |
---|
76 | ++count; |
---|
77 | } while (result->NextRow()); |
---|
78 | |
---|
79 | delete result; |
---|
80 | |
---|
81 | sLog.outString(); |
---|
82 | sLog.outString( ">> Loaded %u Item Enchantment definitions", count ); |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | sLog.outString(); |
---|
87 | sLog.outErrorDb( ">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty."); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | uint32 GetItemEnchantMod(uint32 entry) |
---|
92 | { |
---|
93 | if (!entry) return 0; |
---|
94 | |
---|
95 | EnchantmentStore::iterator tab = RandomItemEnch.find(entry); |
---|
96 | |
---|
97 | if (tab == RandomItemEnch.end()) |
---|
98 | { |
---|
99 | sLog.outErrorDb("Item RandomProperty / RandomSuffix id #%u used in `item_template` but it doesn't have records in `item_enchantment_template` table.",entry); |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | double dRoll = rand_chance(); |
---|
104 | float fCount = 0; |
---|
105 | |
---|
106 | for(EnchStoreList::iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter) |
---|
107 | { |
---|
108 | fCount += ench_iter->chance; |
---|
109 | |
---|
110 | if (fCount > dRoll) return ench_iter->ench; |
---|
111 | } |
---|
112 | |
---|
113 | //we could get here only if sum of all enchantment chances is lower than 100% |
---|
114 | dRoll = (irand(0, (int)floor(fCount * 100) + 1)) / 100; |
---|
115 | fCount = 0; |
---|
116 | |
---|
117 | for(EnchStoreList::iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter) |
---|
118 | { |
---|
119 | fCount += ench_iter->chance; |
---|
120 | |
---|
121 | if (fCount > dRoll) return ench_iter->ench; |
---|
122 | } |
---|
123 | |
---|
124 | return 0; |
---|
125 | } |
---|
126 | |
---|
127 | uint32 GenerateEnchSuffixFactor(uint32 item_id) |
---|
128 | { |
---|
129 | ItemPrototype const *itemProto = objmgr.GetItemPrototype(item_id); |
---|
130 | |
---|
131 | if(!itemProto) |
---|
132 | return 0; |
---|
133 | if(!itemProto->RandomSuffix) |
---|
134 | return 0; |
---|
135 | |
---|
136 | RandomPropertiesPointsEntry const *randomProperty = sRandomPropertiesPointsStore.LookupEntry(itemProto->ItemLevel); |
---|
137 | if(!randomProperty) |
---|
138 | return 0; |
---|
139 | |
---|
140 | uint32 suffixFactor; |
---|
141 | switch(itemProto->InventoryType) |
---|
142 | { |
---|
143 | // Items of that type don`t have points |
---|
144 | case INVTYPE_NON_EQUIP: |
---|
145 | case INVTYPE_BAG: |
---|
146 | case INVTYPE_TABARD: |
---|
147 | case INVTYPE_AMMO: |
---|
148 | case INVTYPE_QUIVER: |
---|
149 | case INVTYPE_RELIC: |
---|
150 | return 0; |
---|
151 | // Select point coefficient |
---|
152 | case INVTYPE_HEAD: |
---|
153 | case INVTYPE_BODY: |
---|
154 | case INVTYPE_CHEST: |
---|
155 | case INVTYPE_LEGS: |
---|
156 | case INVTYPE_2HWEAPON: |
---|
157 | case INVTYPE_ROBE: |
---|
158 | suffixFactor = 0; |
---|
159 | break; |
---|
160 | case INVTYPE_SHOULDERS: |
---|
161 | case INVTYPE_WAIST: |
---|
162 | case INVTYPE_FEET: |
---|
163 | case INVTYPE_HANDS: |
---|
164 | case INVTYPE_TRINKET: |
---|
165 | suffixFactor = 1; |
---|
166 | break; |
---|
167 | case INVTYPE_NECK: |
---|
168 | case INVTYPE_WRISTS: |
---|
169 | case INVTYPE_FINGER: |
---|
170 | case INVTYPE_SHIELD: |
---|
171 | case INVTYPE_CLOAK: |
---|
172 | case INVTYPE_HOLDABLE: |
---|
173 | suffixFactor = 2; |
---|
174 | break; |
---|
175 | case INVTYPE_WEAPON: |
---|
176 | case INVTYPE_WEAPONMAINHAND: |
---|
177 | case INVTYPE_WEAPONOFFHAND: |
---|
178 | suffixFactor = 3; |
---|
179 | break; |
---|
180 | case INVTYPE_RANGED: |
---|
181 | case INVTYPE_THROWN: |
---|
182 | case INVTYPE_RANGEDRIGHT: |
---|
183 | suffixFactor = 4; |
---|
184 | break; |
---|
185 | default: |
---|
186 | return 0; |
---|
187 | } |
---|
188 | // Select rare/epic modifier |
---|
189 | switch (itemProto->Quality) |
---|
190 | { |
---|
191 | case ITEM_QUALITY_UNCOMMON: |
---|
192 | return randomProperty->UncommonPropertiesPoints[suffixFactor]; |
---|
193 | case ITEM_QUALITY_RARE: |
---|
194 | return randomProperty->RarePropertiesPoints[suffixFactor]; |
---|
195 | case ITEM_QUALITY_EPIC: |
---|
196 | return randomProperty->EpicPropertiesPoints[suffixFactor]; |
---|
197 | case ITEM_QUALITY_LEGENDARY: |
---|
198 | case ITEM_QUALITY_ARTIFACT: |
---|
199 | return 0; // not have random properties |
---|
200 | default: |
---|
201 | break; |
---|
202 | } |
---|
203 | return 0; |
---|
204 | } |
---|