[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / SkillDiscovery.cpp
blob9e2ba8f6f820e7e8f32dc5f3291776d8e8799503
1 /*
2 * Copyright (C) 2005-2009 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 "Database/DatabaseEnv.h"
20 #include "Log.h"
21 #include "ProgressBar.h"
22 #include "Policies/SingletonImp.h"
23 #include "ObjectAccessor.h"
24 #include "World.h"
25 #include "Util.h"
26 #include "SkillDiscovery.h"
27 #include "SpellMgr.h"
28 #include <map>
30 struct SkillDiscoveryEntry
32 uint32 spellId; // discavered spell
33 uint32 reqSkillValue; // skill level limitation
34 float chance; // chance
36 SkillDiscoveryEntry()
37 : spellId(0), reqSkillValue(0), chance(0) {}
39 SkillDiscoveryEntry(uint16 _spellId, uint32 req_skill_val, float _chance)
40 : spellId(_spellId), reqSkillValue(req_skill_val), chance(_chance) {}
43 typedef std::list<SkillDiscoveryEntry> SkillDiscoveryList;
44 typedef UNORDERED_MAP<int32, SkillDiscoveryList> SkillDiscoveryMap;
46 static SkillDiscoveryMap SkillDiscoveryStore;
48 void LoadSkillDiscoveryTable()
51 SkillDiscoveryStore.clear(); // need for reload
53 uint32 count = 0;
55 // 0 1 2 3
56 QueryResult *result = WorldDatabase.Query("SELECT spellId, reqSpell, reqSkillValue, chance FROM skill_discovery_template");
58 if (result)
60 barGoLink bar(result->GetRowCount());
62 std::ostringstream ssNonDiscoverableEntries;
66 Field *fields = result->Fetch();
67 bar.step();
69 uint32 spellId = fields[0].GetUInt32();
70 int32 reqSkillOrSpell = fields[1].GetInt32();
71 uint32 reqSkillValue = fields[2].GetInt32();
72 float chance = fields[3].GetFloat();
74 if( chance <= 0 ) // chance
76 ssNonDiscoverableEntries << "spellId = " << spellId << " reqSkillOrSpell = " << reqSkillOrSpell
77 << " reqSkillValue = " << reqSkillValue << " chance = " << chance << "(chance problem)\n";
78 continue;
81 if(reqSkillOrSpell > 0) // spell case
83 SpellEntry const* spellEntry = sSpellStore.LookupEntry(reqSkillOrSpell);
84 if( !spellEntry )
86 sLog.outErrorDb("Spell (ID: %u) have not existed spell (ID: %i) in `reqSpell` field in `skill_discovery_template` table",spellId,reqSkillOrSpell);
87 continue;
90 // mechanic discovery
91 if (spellEntry->Mechanic != MECHANIC_DISCOVERY &&
92 // explicit discovery ability
93 !IsExplicitDiscoverySpell(spellEntry))
95 sLog.outErrorDb("Spell (ID: %u) not have have MECHANIC_DISCOVERY (28) value in Mechanic field in spell.dbc and not 100% chance random discovery ability but listed in `skill_discovery_template` table",spellId);
96 continue;
99 SkillDiscoveryStore[reqSkillOrSpell].push_back( SkillDiscoveryEntry(spellId, reqSkillValue, chance) );
101 else if( reqSkillOrSpell == 0 ) // skill case
103 SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(spellId);
104 SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(spellId);
106 if(lower==upper)
108 sLog.outErrorDb("Spell (ID: %u) not listed in `SkillLineAbility.dbc` but listed with `reqSpell`=0 in `skill_discovery_template` table",spellId);
109 continue;
112 for(SkillLineAbilityMap::const_iterator _spell_idx = lower; _spell_idx != upper; ++_spell_idx)
114 SkillDiscoveryStore[-int32(_spell_idx->second->skillId)].push_back( SkillDiscoveryEntry(spellId, reqSkillValue, chance) );
117 else
119 sLog.outErrorDb("Spell (ID: %u) have negative value in `reqSpell` field in `skill_discovery_template` table",spellId);
120 continue;
123 ++count;
124 } while (result->NextRow());
126 delete result;
128 sLog.outString();
129 sLog.outString( ">> Loaded %u skill discovery definitions", count );
130 if(!ssNonDiscoverableEntries.str().empty())
131 sLog.outErrorDb("Some items can't be successfully discovered: have in chance field value < 0.000001 in `skill_discovery_template` DB table . List:\n%s",ssNonDiscoverableEntries.str().c_str());
133 else
135 sLog.outString();
136 sLog.outString( ">> Loaded 0 skill discovery definitions. DB table `skill_discovery_template` is empty." );
140 uint32 GetExplicitDiscoverySpell(uint32 spellId, Player* player)
142 // explicit discovery spell chances (always success if case exist)
143 // in this case we have both skill and spell
144 SkillDiscoveryMap::iterator tab = SkillDiscoveryStore.find(spellId);
145 if(tab == SkillDiscoveryStore.end())
146 return 0;
148 SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(spellId);
149 SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(spellId);
150 uint32 skillvalue = lower != upper ? player->GetSkillValue(lower->second->skillId) : 0;
152 float full_chance = 0;
153 for(SkillDiscoveryList::iterator item_iter = tab->second.begin(); item_iter != tab->second.end(); ++item_iter)
154 if(item_iter->reqSkillValue <= skillvalue)
155 if(!player->HasSpell(item_iter->spellId))
156 full_chance += item_iter->chance;
158 float rate = full_chance / 100.0f;
159 float roll = rand_chance() * rate; // roll now in range 0..full_chance
161 for(SkillDiscoveryList::iterator item_iter = tab->second.begin(); item_iter != tab->second.end(); ++item_iter)
163 if(item_iter->reqSkillValue > skillvalue)
164 continue;
166 if(player->HasSpell(item_iter->spellId))
167 continue;
169 if(item_iter->chance > roll)
170 return item_iter->spellId;
172 roll -= item_iter->chance;
175 return 0;
178 uint32 GetSkillDiscoverySpell(uint32 skillId, uint32 spellId, Player* player)
180 uint32 skillvalue = skillId ? player->GetSkillValue(skillId) : 0;
182 // check spell case
183 SkillDiscoveryMap::iterator tab = SkillDiscoveryStore.find(spellId);
185 if(tab != SkillDiscoveryStore.end())
187 for(SkillDiscoveryList::iterator item_iter = tab->second.begin(); item_iter != tab->second.end(); ++item_iter)
189 if( roll_chance_f(item_iter->chance * sWorld.getRate(RATE_SKILL_DISCOVERY))
190 && item_iter->reqSkillValue <= skillvalue
191 && !player->HasSpell(item_iter->spellId) )
192 return item_iter->spellId;
195 return 0;
198 if(!skillId)
199 return 0;
201 // check skill line case
202 tab = SkillDiscoveryStore.find(-(int32)skillId);
203 if(tab != SkillDiscoveryStore.end())
205 for(SkillDiscoveryList::iterator item_iter = tab->second.begin(); item_iter != tab->second.end(); ++item_iter)
207 if( roll_chance_f(item_iter->chance * sWorld.getRate(RATE_SKILL_DISCOVERY))
208 && item_iter->reqSkillValue <= skillvalue
209 && !player->HasSpell(item_iter->spellId) )
210 return item_iter->spellId;
213 return 0;
216 return 0;