root/trunk/src/game/SkillExtraItems.cpp @ 18

Revision 2, 5.2 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 "SkillExtraItems.h"
20#include "Database/DatabaseEnv.h"
21#include "Log.h"
22#include "ProgressBar.h"
23#include "Player.h"
24#include <map>
25
26// some type definitions
27// no use putting them in the header file, they're only used in this .cpp
28
29// struct to store information about extra item creation
30// one entry for every spell that is able to create an extra item
31struct SkillExtraItemEntry
32{
33    // the spell id of the specialization required to create extra items
34    uint32 requiredSpecialization;
35    // the chance to create one additional item
36    float additionalCreateChance;
37    // maximum number of extra items created per crafting
38    uint8 additionalMaxNum;
39
40    SkillExtraItemEntry()
41        : requiredSpecialization(0), additionalCreateChance(0.0f), additionalMaxNum(0) {}
42
43    SkillExtraItemEntry(uint32 rS, float aCC, uint8 aMN)
44        : requiredSpecialization(rS), additionalCreateChance(aCC), additionalMaxNum(aMN) {}
45};
46
47// map to store the extra item creation info, the key is the spellId of the creation spell, the mapped value is the assigned SkillExtraItemEntry
48typedef std::map<uint32,SkillExtraItemEntry> SkillExtraItemMap;
49
50SkillExtraItemMap SkillExtraItemStore;
51
52// loads the extra item creation info from DB
53void LoadSkillExtraItemTable()
54{
55    uint32 count = 0;
56
57    SkillExtraItemStore.clear();                            // need for reload
58
59    //                                                 0        1                       2                       3
60    QueryResult *result = WorldDatabase.PQuery("SELECT spellId, requiredSpecialization, additionalCreateChance, additionalMaxNum FROM skill_extra_item_template");
61
62    if (result)
63    {
64        barGoLink bar(result->GetRowCount());
65
66        do
67        {
68            Field *fields = result->Fetch();
69            bar.step();
70
71            uint32 spellId = fields[0].GetUInt32();
72
73            if(!sSpellStore.LookupEntry(spellId))
74            {
75                sLog.outError("Skill specialization %u has non-existent spell id in `skill_extra_item_template`!", spellId);
76                continue;
77            }
78
79            uint32 requiredSpecialization = fields[1].GetUInt32();
80            if(!sSpellStore.LookupEntry(requiredSpecialization))
81            {
82                sLog.outError("Skill specialization %u have not existed required specialization spell id %u in `skill_extra_item_template`!", spellId,requiredSpecialization);
83                continue;
84            }
85
86            float additionalCreateChance = fields[2].GetFloat();
87            if(additionalCreateChance <= 0.0f)
88            {
89                sLog.outError("Skill specialization %u has too low additional create chance in `skill_extra_item_template`!", spellId);
90                continue;
91            }
92
93            uint8 additionalMaxNum = fields[3].GetUInt8();
94            if(!additionalMaxNum)
95            {
96                sLog.outError("Skill specialization %u has 0 max number of extra items in `skill_extra_item_template`!", spellId);
97                continue;
98            }
99
100            SkillExtraItemEntry& skillExtraItemEntry = SkillExtraItemStore[spellId];
101
102            skillExtraItemEntry.requiredSpecialization = requiredSpecialization;
103            skillExtraItemEntry.additionalCreateChance = additionalCreateChance;
104            skillExtraItemEntry.additionalMaxNum       = additionalMaxNum;
105
106            ++count;
107        } while (result->NextRow());
108
109        delete result;
110
111        sLog.outString();
112        sLog.outString( ">> Loaded %u spell specialization definitions", count );
113    }
114    else
115    {
116        sLog.outString();
117        sLog.outString( ">> Loaded 0 spell specialization definitions. DB table `skill_extra_item_template` is empty." );
118    }
119}
120
121bool canCreateExtraItems(Player * player, uint32 spellId, float &additionalChance, uint8 &additionalMax)
122{
123    // get the info for the specified spell
124    SkillExtraItemMap::const_iterator ret = SkillExtraItemStore.find(spellId);
125    if(ret==SkillExtraItemStore.end())
126        return false;
127
128    SkillExtraItemEntry const* specEntry = &ret->second;
129
130    // if no entry, then no extra items can be created
131    if(!specEntry)
132        return false;
133
134    // the player doesn't have the required specialization, return false
135    if(!player->HasSpell(specEntry->requiredSpecialization))
136        return false;
137
138    // set the arguments to the appropriate values
139    additionalChance = specEntry->additionalCreateChance;
140    additionalMax = specEntry->additionalMaxNum;
141
142    // enable extra item creation
143    return true;
144}
Note: See TracBrowser for help on using the browser.