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