[6982] Implemented gmlevel-based command security
[getmangos.git] / src / game / SkillExtraItems.cpp
blobeebb443d484e73d6f3baa99331a18284dab7132e
1 /*
2 * Copyright (C) 2005-2008 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
19 #include "SkillExtraItems.h"
20 #include "Database/DatabaseEnv.h"
21 #include "Log.h"
22 #include "ProgressBar.h"
23 #include "Player.h"
24 #include <map>
26 // some type definitions
27 // no use putting them in the header file, they're only used in this .cpp
29 // struct to store information about extra item creation
30 // one entry for every spell that is able to create an extra item
31 struct SkillExtraItemEntry
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;
40 SkillExtraItemEntry()
41 : requiredSpecialization(0), additionalCreateChance(0.0f), additionalMaxNum(0) {}
43 SkillExtraItemEntry(uint32 rS, float aCC, uint8 aMN)
44 : requiredSpecialization(rS), additionalCreateChance(aCC), additionalMaxNum(aMN) {}
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
48 typedef std::map<uint32,SkillExtraItemEntry> SkillExtraItemMap;
50 SkillExtraItemMap SkillExtraItemStore;
52 // loads the extra item creation info from DB
53 void LoadSkillExtraItemTable()
55 uint32 count = 0;
57 SkillExtraItemStore.clear(); // need for reload
59 // 0 1 2 3
60 QueryResult *result = WorldDatabase.Query("SELECT spellId, requiredSpecialization, additionalCreateChance, additionalMaxNum FROM skill_extra_item_template");
62 if (result)
64 barGoLink bar(result->GetRowCount());
68 Field *fields = result->Fetch();
69 bar.step();
71 uint32 spellId = fields[0].GetUInt32();
73 if(!sSpellStore.LookupEntry(spellId))
75 sLog.outError("Skill specialization %u has non-existent spell id in `skill_extra_item_template`!", spellId);
76 continue;
79 uint32 requiredSpecialization = fields[1].GetUInt32();
80 if(!sSpellStore.LookupEntry(requiredSpecialization))
82 sLog.outError("Skill specialization %u have not existed required specialization spell id %u in `skill_extra_item_template`!", spellId,requiredSpecialization);
83 continue;
86 float additionalCreateChance = fields[2].GetFloat();
87 if(additionalCreateChance <= 0.0f)
89 sLog.outError("Skill specialization %u has too low additional create chance in `skill_extra_item_template`!", spellId);
90 continue;
93 uint8 additionalMaxNum = fields[3].GetUInt8();
94 if(!additionalMaxNum)
96 sLog.outError("Skill specialization %u has 0 max number of extra items in `skill_extra_item_template`!", spellId);
97 continue;
100 SkillExtraItemEntry& skillExtraItemEntry = SkillExtraItemStore[spellId];
102 skillExtraItemEntry.requiredSpecialization = requiredSpecialization;
103 skillExtraItemEntry.additionalCreateChance = additionalCreateChance;
104 skillExtraItemEntry.additionalMaxNum = additionalMaxNum;
106 ++count;
107 } while (result->NextRow());
109 delete result;
111 sLog.outString();
112 sLog.outString( ">> Loaded %u spell specialization definitions", count );
114 else
116 sLog.outString();
117 sLog.outString( ">> Loaded 0 spell specialization definitions. DB table `skill_extra_item_template` is empty." );
121 bool canCreateExtraItems(Player * player, uint32 spellId, float &additionalChance, uint8 &additionalMax)
123 // get the info for the specified spell
124 SkillExtraItemMap::const_iterator ret = SkillExtraItemStore.find(spellId);
125 if(ret==SkillExtraItemStore.end())
126 return false;
128 SkillExtraItemEntry const* specEntry = &ret->second;
130 // if no entry, then no extra items can be created
131 if(!specEntry)
132 return false;
134 // the player doesn't have the required specialization, return false
135 if(!player->HasSpell(specEntry->requiredSpecialization))
136 return false;
138 // set the arguments to the appropriate values
139 additionalChance = specEntry->additionalCreateChance;
140 additionalMax = specEntry->additionalMaxNum;
142 // enable extra item creation
143 return true;