root/trunk/src/game/ItemEnchantmentMgr.cpp @ 26

Revision 2, 5.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include <stdlib.h>
20#include <functional>
21#include "ItemEnchantmentMgr.h"
22#include "Database/DatabaseEnv.h"
23#include "Log.h"
24#include "ObjectMgr.h"
25#include "ProgressBar.h"
26#include <list>
27#include <vector>
28#include "Util.h"
29
30struct EnchStoreItem
31{
32    uint32  ench;
33    float   chance;
34
35    EnchStoreItem()
36        : ench(0), chance(0) {}
37
38    EnchStoreItem(uint32 _ench, float _chance)
39        : ench(_ench), chance(_chance) {}
40};
41
42typedef std::vector<EnchStoreItem> EnchStoreList;
43typedef HM_NAMESPACE::hash_map<uint32, EnchStoreList> EnchantmentStore;
44
45static EnchantmentStore RandomItemEnch;
46
47void LoadRandomEnchantmentsTable()
48{
49    RandomItemEnch.clear();                                 // for reload case
50
51    EnchantmentStore::iterator tab;
52    uint32 entry, ench;
53    float chance;
54    uint32 count = 0;
55
56    QueryResult *result = WorldDatabase.Query("SELECT entry, ench, chance FROM item_enchantment_template");
57
58    if (result)
59    {
60        barGoLink bar(result->GetRowCount());
61
62        do
63        {
64            Field *fields = result->Fetch();
65            bar.step();
66
67            entry = fields[0].GetUInt32();
68            ench = fields[1].GetUInt32();
69            chance = fields[2].GetFloat();
70
71            if (chance > 0.000001f && chance <= 100.0f)
72                RandomItemEnch[entry].push_back( EnchStoreItem(ench, chance) );
73
74            ++count;
75        } while (result->NextRow());
76
77        delete result;
78
79        sLog.outString();
80        sLog.outString( ">> Loaded %u Item Enchantment definitions", count );
81    }
82    else
83    {
84        sLog.outString();
85        sLog.outErrorDb( ">> Loaded 0 Item Enchantment definitions. DB table `item_enchantment_template` is empty.");
86    }
87}
88
89uint32 GetItemEnchantMod(uint32 entry)
90{
91    if (!entry) return 0;
92
93    EnchantmentStore::iterator tab = RandomItemEnch.find(entry);
94
95    if (tab == RandomItemEnch.end())
96    {
97        sLog.outErrorDb("Item RandomProperty / RandomSuffix id #%u used in `item_template` but it doesn't have records in `item_enchantment_template` table.",entry);
98        return 0;
99    }
100
101    double dRoll = rand_chance();
102    float fCount = 0;
103
104    for(EnchStoreList::iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
105    {
106        fCount += ench_iter->chance;
107
108        if (fCount > dRoll) return ench_iter->ench;
109    }
110
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;
113    fCount = 0;
114
115    for(EnchStoreList::iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter)
116    {
117        fCount += ench_iter->chance;
118
119        if (fCount > dRoll) return ench_iter->ench;
120    }
121
122    return 0;
123}
124
125uint32 GenerateEnchSuffixFactor(uint32 item_id)
126{
127    ItemPrototype const *itemProto = objmgr.GetItemPrototype(item_id);
128
129    if(!itemProto)
130        return 0;
131    if(!itemProto->RandomSuffix)
132        return 0;
133
134    RandomPropertiesPointsEntry const *randomProperty = sRandomPropertiesPointsStore.LookupEntry(itemProto->ItemLevel);
135    if(!randomProperty)
136        return 0;
137
138    uint32 suffixFactor;
139    switch(itemProto->InventoryType)
140    {
141        // Items of that type don`t have points
142        case INVTYPE_NON_EQUIP:
143        case INVTYPE_BAG:
144        case INVTYPE_TABARD:
145        case INVTYPE_AMMO:
146        case INVTYPE_QUIVER:
147        case INVTYPE_RELIC:
148            return 0;
149            // Select point coefficient
150        case INVTYPE_HEAD:
151        case INVTYPE_BODY:
152        case INVTYPE_CHEST:
153        case INVTYPE_LEGS:
154        case INVTYPE_2HWEAPON:
155        case INVTYPE_ROBE:
156            suffixFactor = 0;
157            break;
158        case INVTYPE_SHOULDERS:
159        case INVTYPE_WAIST:
160        case INVTYPE_FEET:
161        case INVTYPE_HANDS:
162        case INVTYPE_TRINKET:
163            suffixFactor = 1;
164            break;
165        case INVTYPE_NECK:
166        case INVTYPE_WRISTS:
167        case INVTYPE_FINGER:
168        case INVTYPE_SHIELD:
169        case INVTYPE_CLOAK:
170        case INVTYPE_HOLDABLE:
171            suffixFactor = 2;
172            break;
173        case INVTYPE_WEAPON:
174        case INVTYPE_WEAPONMAINHAND:
175        case INVTYPE_WEAPONOFFHAND:
176            suffixFactor = 3;
177            break;
178        case INVTYPE_RANGED:
179        case INVTYPE_THROWN:
180        case INVTYPE_RANGEDRIGHT:
181            suffixFactor = 4;
182            break;
183        default:
184            return 0;
185    }
186    // Select rare/epic modifier
187    switch (itemProto->Quality)
188    {
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
198        default:
199            break;
200    }
201    return 0;
202}
Note: See TracBrowser for help on using the browser.