OregonCore  revision fb2a440-git
Your Favourite TBC server
SkillExtraItems.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "SkillExtraItems.h"
19 #include "Database/DatabaseEnv.h"
20 #include "Log.h"
21 #include "Player.h"
22 #include <map>
23 
24 // some type definitions
25 // no use putting them in the header file, they're only used in this .cpp
26 
27 // struct to store information about extra item creation
28 // one entry for every spell that is able to create an extra item
30 {
31  // the spell id of the specialization required to create extra items
33  // the chance to create one additional item
35  // maximum number of extra items created per crafting
37 
39  : requiredSpecialization(0), additionalCreateChance(0.0f), additionalMaxNum(0) {}
40 
41  SkillExtraItemEntry(uint32 rS, float aCC, uint8 aMN)
42  : requiredSpecialization(rS), additionalCreateChance(aCC), additionalMaxNum(aMN) {}
43 };
44 
45 // map to store the extra item creation info, the key is the spellId of the creation spell, the mapped value is the assigned SkillExtraItemEntry
46 typedef std::map<uint32, SkillExtraItemEntry> SkillExtraItemMap;
47 
49 
50 // loads the extra item creation info from DB
52 {
53  uint32 count = 0;
54 
55  SkillExtraItemStore.clear(); // need for reload
56 
57  // 0 1 2 3
58  QueryResult_AutoPtr result = WorldDatabase.Query("SELECT spellId, requiredSpecialization, additionalCreateChance, additionalMaxNum FROM skill_extra_item_template");
59 
60  if (result)
61  {
62 
63  do
64  {
65  Field* fields = result->Fetch();
66 
67  uint32 spellId = fields[0].GetUInt32();
68 
69  if (!sSpellStore.LookupEntry(spellId))
70  {
71  sLog.outError("Skill specialization %u has non-existent spell id in skill_extra_item_template!", spellId);
72  continue;
73  }
74 
75  uint32 requiredSpecialization = fields[1].GetUInt32();
76  if (!sSpellStore.LookupEntry(requiredSpecialization))
77  {
78  sLog.outError("Skill specialization %u has invalid required specialization spell id %u in skill_extra_item_template!", spellId, requiredSpecialization);
79  continue;
80  }
81 
82  float additionalCreateChance = fields[2].GetFloat();
83  if (additionalCreateChance <= 0.0f)
84  {
85  sLog.outError("Skill specialization %u has too low additional create chance in skill_extra_item_template!", spellId);
86  continue;
87  }
88 
89  uint8 additionalMaxNum = fields[3].GetUInt8();
90  if (!additionalMaxNum)
91  {
92  sLog.outError("Skill specialization %u has 0 max number of extra items in skill_extra_item_template!", spellId);
93  continue;
94  }
95 
96  SkillExtraItemEntry& skillExtraItemEntry = SkillExtraItemStore[spellId];
97 
98  skillExtraItemEntry.requiredSpecialization = requiredSpecialization;
99  skillExtraItemEntry.additionalCreateChance = additionalCreateChance;
100  skillExtraItemEntry.additionalMaxNum = additionalMaxNum;
101 
102  ++count;
103  }
104  while (result->NextRow());
105 
106  sLog.outString(">> Loaded %u spell specialization definitions", count);
107  }
108  else
109  sLog.outString(">> Loaded 0 spell specialization definitions. DB table skill_extra_item_template is empty.");
110 }
111 
112 bool canCreateExtraItems(Player* player, uint32 spellId, float& additionalChance, uint8& additionalMax)
113 {
114  // get the info for the specified spell
115  SkillExtraItemMap::const_iterator ret = SkillExtraItemStore.find(spellId);
116  if (ret == SkillExtraItemStore.end())
117  return false;
118 
119  SkillExtraItemEntry const* specEntry = &ret->second;
120 
121  // if no entry, then no extra items can be created
122  if (!specEntry)
123  return false;
124 
125  // the player doesn't have the required specialization, return false
126  if (!player->HasSpell(specEntry->requiredSpecialization))
127  return false;
128 
129  // set the arguments to the appropriate values
130  additionalChance = specEntry->additionalCreateChance;
131  additionalMax = specEntry->additionalMaxNum;
132 
133  // enable extra item creation
134  return true;
135 }
136 
DatabaseType WorldDatabase
Accessor to the world database.
Definition: Main.cpp:53
Definition: Field.h:24
#define sLog
Log class singleton.
Definition: Log.h:187
QueryResult_AutoPtr Query(const char *sql)
Definition: Database.cpp:383
SkillExtraItemMap SkillExtraItemStore
void LoadSkillExtraItemTable()
DBCStorage< SpellEntry > sSpellStore(SpellEntryfmt)
bool HasSpell(uint32 spell) const override
Definition: Player.cpp:3755
SkillExtraItemEntry(uint32 rS, float aCC, uint8 aMN)
ACE_UINT8 uint8
Definition: Define.h:73
bool canCreateExtraItems(Player *player, uint32 spellId, float &additionalChance, uint8 &additionalMax)
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
ACE_UINT32 uint32
Definition: Define.h:71
Definition: Player.h:922
std::map< uint32, SkillExtraItemEntry > SkillExtraItemMap