Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
SkillExtraItems.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "DatabaseEnv.h"
7#include "Log.h"
8#include "Player.h"
9#include "SkillExtraItems.h"
10#include <map>
11
12// some type definitions
13// no use putting them in the header file, they're only used in this .cpp
14
15// struct to store information about extra item creation
16// one entry for every spell that is able to create an extra item
18{
19 // the spell id of the specialization required to create extra items
21 // the chance to create one additional item
23 // maximum number of extra items created per crafting
25
28
31};
32
33// map to store the extra item creation info, the key is the spellId of the creation spell, the mapped value is the assigned SkillExtraItemEntry
34typedef std::map<uint32, SkillExtraItemEntry> SkillExtraItemMap;
35
37
38// loads the extra item creation info from DB
40{
41 uint32 oldMSTime = getMSTime();
42
43 SkillExtraItemStore.clear(); // need for reload
44
45 // 0 1 2 3
46 QueryResult result = WorldDatabase.Query("SELECT spellId, requiredSpecialization, additionalCreateChance, additionalMaxNum FROM skill_extra_item_template");
47
48 if (!result)
49 {
50 SF_LOG_ERROR("server.loading", ">> Loaded 0 spell specialization definitions. DB table `skill_extra_item_template` is empty.");
51 return;
52 }
53
54 uint32 count = 0;
55
56 do
57 {
58 Field* fields = result->Fetch();
59
60 uint32 spellId = fields[0].GetUInt32();
61
62 if (!sSpellMgr->GetSpellInfo(spellId))
63 {
64 SF_LOG_ERROR("sql.sql", "Skill specialization %u has non-existent spell id in `skill_extra_item_template`!", spellId);
65 continue;
66 }
67
68 uint32 requiredSpecialization = fields[1].GetUInt32();
69 if (!sSpellMgr->GetSpellInfo(requiredSpecialization))
70 {
71 SF_LOG_ERROR("sql.sql", "Skill specialization %u have not existed required specialization spell id %u in `skill_extra_item_template`!", spellId, requiredSpecialization);
72 continue;
73 }
74
75 float additionalCreateChance = fields[2].GetFloat();
76 if (additionalCreateChance <= 0.0f)
77 {
78 SF_LOG_ERROR("sql.sql", "Skill specialization %u has too low additional create chance in `skill_extra_item_template`!", spellId);
79 continue;
80 }
81
82 uint8 additionalMaxNum = fields[3].GetUInt8();
83 if (!additionalMaxNum)
84 {
85 SF_LOG_ERROR("sql.sql", "Skill specialization %u has 0 max number of extra items in `skill_extra_item_template`!", spellId);
86 continue;
87 }
88
89 SkillExtraItemEntry& skillExtraItemEntry = SkillExtraItemStore[spellId];
90
91 skillExtraItemEntry.requiredSpecialization = requiredSpecialization;
92 skillExtraItemEntry.additionalCreateChance = additionalCreateChance;
93 skillExtraItemEntry.additionalMaxNum = additionalMaxNum;
94
95 ++count;
96 } while (result->NextRow());
97
98 SF_LOG_INFO("server.loading", ">> Loaded %u spell specialization definitions in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
99}
100
101bool CanCreateExtraItems(Player* player, uint32 spellId, float& additionalChance, uint8& additionalMax)
102{
103 // get the info for the specified spell
104 SkillExtraItemMap::const_iterator ret = SkillExtraItemStore.find(spellId);
105 if (ret == SkillExtraItemStore.end())
106 return false;
107
108 SkillExtraItemEntry const* specEntry = &ret->second;
109
110 // if no entry, then no extra items can be created
111 if (!specEntry)
112 return false;
113
114 // the player doesn't have the required specialization, return false
115 if (!player->HasSpell(specEntry->requiredSpecialization))
116 return false;
117
118 // set the arguments to the appropriate values
119 additionalChance = specEntry->additionalCreateChance;
120 additionalMax = specEntry->additionalMaxNum;
121
122 // enable extra item creation
123 return true;
124}
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
#define SF_LOG_INFO(filterType__,...)
Definition Log.h:137
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
Definition QueryResult.h:48
bool CanCreateExtraItems(Player *player, uint32 spellId, float &additionalChance, uint8 &additionalMax)
void LoadSkillExtraItemTable()
std::map< uint32, SkillExtraItemEntry > SkillExtraItemMap
SkillExtraItemMap SkillExtraItemStore
#define sSpellMgr
Definition SpellMgr.h:744
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:22
uint32 getMSTime()
Definition Timer.h:12
Definition Field.h:16
uint8 GetUInt8() const
Definition Field.h:26
float GetFloat() const
Definition Field.h:177
uint32 GetUInt32() const
Definition Field.h:105
bool HasSpell(uint32 spell) const override
Definition Player.cpp:4674
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
Definition Main.cpp:40
uint32 requiredSpecialization
float additionalCreateChance
SkillExtraItemEntry()
uint8 additionalMaxNum
SkillExtraItemEntry(uint32 rS, float aCC, uint8 aMN)