Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
cs_battlepet.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 "BattlePet.h"
7#include "BattlePetMgr.h"
8#include "Chat.h"
9#include "Player.h"
10#include "ScriptMgr.h"
11
12#include <string>
13#include <vector>
14
15namespace
16{
17 std::string TrimCommandText(std::string text)
18 {
19 std::string::size_type first = text.find_first_not_of(" \t\r\n");
20 if (first == std::string::npos)
21 return "";
22
23 std::string::size_type last = text.find_last_not_of(" \t\r\n");
24 text = text.substr(first, last - first + 1);
25
26 if (text.size() >= 2 && text[0] == '"' && text[text.size() - 1] == '"')
27 text = text.substr(1, text.size() - 2);
28
29 first = text.find_first_not_of(" \t\r\n");
30 if (first == std::string::npos)
31 return "";
32
33 last = text.find_last_not_of(" \t\r\n");
34 return text.substr(first, last - first + 1);
35 }
36
37 std::string GetBattlePetCommandName(char const* args)
38 {
39 return TrimCommandText(args ? args : "");
40 }
41
42 bool IsAllArgument(std::string const& text)
43 {
44 return !text.empty() && !stricmp(text.c_str(), "all");
45 }
46
47 Player* GetBattlePetCommandTarget(ChatHandler* handler)
48 {
49 Player* target = handler->getSelectedPlayer();
50 if (!target)
51 target = handler->GetSession()->GetPlayer();
52
53 if (!target)
54 {
55 handler->PSendSysMessage("No player selected.");
56 handler->SetSentErrorMessage(true);
57 return NULL;
58 }
59
60 if (handler->HasLowerSecurity(target, 0))
61 return NULL;
62
63 return target;
64 }
65
66 std::string GetBattlePetDisplayName(BattlePetMgr* battlePetMgr, BattlePet* battlePet)
67 {
68 if (!battlePet)
69 return "<unknown>";
70
71 std::string nickname = battlePet->GetNickname();
72 if (!nickname.empty())
73 return nickname;
74
75 std::string speciesName = battlePetMgr->GetBattlePetSpeciesName(battlePet);
76 return speciesName.empty() ? std::string("<unknown>") : speciesName;
77 }
78
79 void SendBattlePetDetails(ChatHandler* handler, BattlePetMgr* battlePetMgr, BattlePet* battlePet)
80 {
81 std::string displayName = GetBattlePetDisplayName(battlePetMgr, battlePet);
82 std::string speciesName = battlePetMgr->GetBattlePetSpeciesName(battlePet);
83
84 handler->PSendSysMessage("Battle pet: %s", displayName.c_str());
85 handler->PSendSysMessage(" ID: " UI64FMTD " Species: %u (%s)",
86 uint64(battlePet->GetId()), uint32(battlePet->GetSpecies()),
87 speciesName.empty() ? "unknown" : speciesName.c_str());
88 handler->PSendSysMessage(" Level: %u XP: %u/%u Health: %u/%u",
89 uint32(battlePet->GetLevel()), uint32(battlePet->GetXp()),
91 uint32(battlePet->GetCurrentHealth()), uint32(battlePet->GetMaxHealth()));
92 handler->PSendSysMessage(" Power: %u Speed: %u Quality: %u Breed: %u Flags: %u",
93 uint32(battlePet->GetPower()), uint32(battlePet->GetSpeed()),
94 uint32(battlePet->GetQuality()), uint32(battlePet->GetBreed()),
95 uint32(battlePet->GetFlags()));
96 }
97
98 BattlePet* GetUniqueBattlePetByName(ChatHandler* handler, BattlePetMgr* battlePetMgr, std::string const& name)
99 {
100 if (name.empty())
101 {
102 handler->PSendSysMessage("Usage: .battlepet levelup \"name\" or .battlepet heal \"name\"");
103 handler->SetSentErrorMessage(true);
104 return NULL;
105 }
106
107 std::vector<BattlePet*> matches = battlePetMgr->FindBattlePetNameMatches(name);
108 if (matches.empty())
109 {
110 handler->PSendSysMessage("No battle pet named '%s' was found.", name.c_str());
111 handler->SetSentErrorMessage(true);
112 return NULL;
113 }
114
115 if (matches.size() > 1)
116 {
117 handler->PSendSysMessage("Multiple battle pets named '%s' were found. Rename one or use a unique species name.", name.c_str());
118 for (std::vector<BattlePet*>::const_iterator itr = matches.begin(); itr != matches.end(); ++itr)
119 handler->PSendSysMessage(" " UI64FMTD " - %s level %u",
120 uint64((*itr)->GetId()), GetBattlePetDisplayName(battlePetMgr, *itr).c_str(),
121 uint32((*itr)->GetLevel()));
122
123 handler->SetSentErrorMessage(true);
124 return NULL;
125 }
126
127 return matches.front();
128 }
129}
130
132{
133public:
134 battlepet_commandscript() : CommandScript("battlepet_commandscript") { }
135
136 std::vector<ChatCommand> GetCommands() const OVERRIDE
137 {
138 static std::vector<ChatCommand> battlePetCommandTable =
139 {
143 };
144
145 static std::vector<ChatCommand> commandTable =
146 {
147 { "battlepet", rbac::RBAC_PERM_COMMAND_BATTLEPET, false, NULL, "", battlePetCommandTable },
148 };
149 return commandTable;
150 }
151
152 static bool HandleBattlePetLevelupCommand(ChatHandler* handler, char const* args)
153 {
154 Player* target = GetBattlePetCommandTarget(handler);
155 if (!target)
156 return false;
157
158 BattlePetMgr* battlePetMgr = target->GetBattlePetMgr();
159 BattlePet* battlePet = GetUniqueBattlePetByName(handler, battlePetMgr, GetBattlePetCommandName(args));
160 if (!battlePet)
161 return false;
162
163 if (battlePet->GetLevel() >= BATTLE_PET_MAX_LEVEL)
164 {
165 handler->PSendSysMessage("%s is already max level.", GetBattlePetDisplayName(battlePetMgr, battlePet).c_str());
166 return true;
167 }
168
169 uint8 const oldLevel = battlePet->GetLevel();
170 battlePet->SetLevel(oldLevel + 1);
171 battlePet->SetXp(0);
172 battlePetMgr->UpdateBattlePetLevelAchievement(*battlePet, oldLevel, battlePet->GetLevel());
173 battlePetMgr->SendBattlePetUpdate(battlePet, true);
174 battlePetMgr->SaveToDb();
175
176 handler->PSendSysMessage("Leveled %s from %u to %u for %s.",
177 GetBattlePetDisplayName(battlePetMgr, battlePet).c_str(), uint32(oldLevel),
178 uint32(battlePet->GetLevel()), target->GetName().c_str());
179 return true;
180 }
181
182 static bool HandleBattlePetHealCommand(ChatHandler* handler, char const* args)
183 {
184 Player* target = GetBattlePetCommandTarget(handler);
185 if (!target)
186 return false;
187
188 BattlePetMgr* battlePetMgr = target->GetBattlePetMgr();
189 std::string name = GetBattlePetCommandName(args);
190 if (IsAllArgument(name))
191 {
192 battlePetMgr->HealBattlePets(100);
193 battlePetMgr->SaveToDb();
194 handler->PSendSysMessage("Healed all battle pets for %s.", target->GetName().c_str());
195 return true;
196 }
197
198 BattlePet* battlePet = GetUniqueBattlePetByName(handler, battlePetMgr, name);
199 if (!battlePet)
200 return false;
201
202 battlePet->SetCurrentHealth(battlePet->GetMaxHealth());
203 battlePetMgr->SendBattlePetUpdate(battlePet, false);
204 battlePetMgr->SaveToDb();
205
206 handler->PSendSysMessage("Healed %s for %s.",
207 GetBattlePetDisplayName(battlePetMgr, battlePet).c_str(), target->GetName().c_str());
208 return true;
209 }
210
211 static bool HandleBattlePetDebugCommand(ChatHandler* handler, char const* args)
212 {
213 Player* target = GetBattlePetCommandTarget(handler);
214 if (!target)
215 return false;
216
217 BattlePetMgr* battlePetMgr = target->GetBattlePetMgr();
218 handler->PSendSysMessage("Battle pet debug for %s:", target->GetName().c_str());
219 handler->PSendSysMessage(" Collection: %u pets", battlePetMgr->GetBattlePetCount());
220 handler->PSendSysMessage(" Loadout flags: %u Trap ability: %u Active battle: %s PvP queued: %s",
221 uint32(battlePetMgr->GetLoadoutFlags()), battlePetMgr->GetTrapAbility(),
222 battlePetMgr->HasActivePetBattle() ? "yes" : "no",
223 battlePetMgr->IsPetBattlePvpQueued() ? "yes" : "no");
224 handler->PSendSysMessage(" Current summon: " UI64FMTD, uint64(battlePetMgr->GetCurrentSummonId()));
225
226 if (battlePetMgr->HasActivePetBattle())
227 {
228 ActivePetBattle const& activeBattle = battlePetMgr->GetActivePetBattle();
229 BattlePetAchievementContext const achievementContext =
231 activeBattle.EnemySpecies, activeBattle.EnemyQuality,
232 activeBattle.Winner == PET_BATTLE_WINNER_ALLY,
233 activeBattle.Captured);
234 handler->PSendSysMessage(" Active source: %s Round: %u Winner: %u",
236 activeBattle.RoundID, uint32(activeBattle.Winner));
237 handler->PSendSysMessage(" Enemy species: %u quality: %u health: %u/%u payload: %u",
238 uint32(activeBattle.EnemySpecies), uint32(activeBattle.EnemyQuality),
239 activeBattle.EnemyHealth, activeBattle.EnemyMaxHealth,
240 achievementContext.Payload());
241
242 BattlePet* frontPet = battlePetMgr->GetBattlePet(activeBattle.AllyPetID);
243 if (frontPet)
244 handler->PSendSysMessage(" XP preview: %u from enemy level %u",
245 uint32(BattlePetExperienceReward(frontPet->GetLevel(), activeBattle.EnemyLevel)),
246 uint32(activeBattle.EnemyLevel));
247 }
248
249 for (uint8 slot = 0; slot < BATTLE_PET_MAX_LOADOUT_SLOTS; ++slot)
250 {
251 if (!battlePetMgr->HasLoadoutSlot(slot))
252 {
253 handler->PSendSysMessage(" Slot %u: locked", uint32(slot + 1));
254 continue;
255 }
256
257 BattlePet* battlePet = battlePetMgr->GetBattlePet(battlePetMgr->GetLoadoutSlot(slot));
258 if (!battlePet)
259 {
260 handler->PSendSysMessage(" Slot %u: empty", uint32(slot + 1));
261 continue;
262 }
263
264 handler->PSendSysMessage(" Slot %u: %s level %u health %u/%u",
265 uint32(slot + 1), GetBattlePetDisplayName(battlePetMgr, battlePet).c_str(),
266 uint32(battlePet->GetLevel()), uint32(battlePet->GetCurrentHealth()),
267 uint32(battlePet->GetMaxHealth()));
268 }
269
270 std::string name = GetBattlePetCommandName(args);
271 if (!name.empty())
272 {
273 BattlePet* battlePet = GetUniqueBattlePetByName(handler, battlePetMgr, name);
274 if (!battlePet)
275 return false;
276
277 SendBattlePetDetails(handler, battlePetMgr, battlePet);
278 }
279
280 return true;
281 }
282};
283
uint16 BattlePetExperienceReward(uint8 petLevel, uint8 enemyLevel, uint8 participatingPetCount)
uint16 BattlePetExperienceForNextLevel(uint8 level)
#define BATTLE_PET_MAX_LEVEL
Definition BattlePet.h:12
char const * BattlePetAchievementSourceName(BattlePetAchievementSource source)
Definition BattlePet.h:25
@ PET_BATTLE_WINNER_ALLY
#define BATTLE_PET_MAX_LOADOUT_SLOTS
#define UI64FMTD
Definition Define.h:64
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
#define OVERRIDE
Definition Define.h:60
std::uint64_t uint64
Definition Define.h:76
uint16 GetXp() const
Definition BattlePet.h:220
uint16 GetSpeed() const
Definition BattlePet.h:224
void SetLevel(uint8 level)
uint8 GetLevel() const
Definition BattlePet.h:219
void SetCurrentHealth(uint16 health)
uint8 GetQuality() const
Definition BattlePet.h:225
uint64 GetId() const
Definition BattlePet.h:215
uint16 GetCurrentHealth() const
Definition BattlePet.h:221
uint16 GetSpecies() const
Definition BattlePet.h:216
uint16 GetMaxHealth() const
Definition BattlePet.h:222
void SetXp(uint16 xp)
uint16 GetPower() const
Definition BattlePet.h:223
std::string GetNickname() const
Definition BattlePet.h:217
uint8 GetBreed() const
Definition BattlePet.h:226
uint16 GetFlags() const
Definition BattlePet.h:237
uint8 GetLoadoutFlags() const
bool IsPetBattlePvpQueued() const
uint32 GetTrapAbility() const
uint32 GetBattlePetCount() const
ActivePetBattle const & GetActivePetBattle() const
std::string GetBattlePetSpeciesName(BattlePet const *battlePet) const
uint64 GetLoadoutSlot(uint8 slot) const
uint64 GetCurrentSummonId() const
BattlePetAchievementContext BuildActivePetBattleAchievementContext(uint16 speciesId, uint8 quality, bool won, bool captured) const
bool HasLoadoutSlot(uint8 slot) const
BattlePet * GetBattlePet(uint64 id) const
void UpdateBattlePetLevelAchievement(BattlePet const &battlePet, uint8 initialLevel, uint8 finalLevel)
void SendBattlePetUpdate(BattlePet *battlePet, bool notification)
std::vector< BattlePet * > FindBattlePetNameMatches(std::string const &name) const
bool HasActivePetBattle() const
void HealBattlePets(uint8 percent)
bool HasLowerSecurity(Player *target, uint64 guid, bool strong=false)
Definition Chat.cpp:78
Player * getSelectedPlayer()
Definition Chat.cpp:829
WorldSession * GetSession()
Definition Chat.h:43
void PSendSysMessage(const char *format,...) ATTR_PRINTF(2
Definition Chat.cpp:221
void SetSentErrorMessage(bool val)
Definition Chat.h:114
CommandScript(const char *name)
BattlePetMgr * GetBattlePetMgr()
Definition Player.h:2573
std::string const & GetName() const
Definition Object.h:664
Player * GetPlayer() const
std::vector< ChatCommand > GetCommands() const OVERRIDE
static bool HandleBattlePetDebugCommand(ChatHandler *handler, char const *args)
static bool HandleBattlePetLevelupCommand(ChatHandler *handler, char const *args)
static bool HandleBattlePetHealCommand(ChatHandler *handler, char const *args)
void AddSC_battlepet_commandscript()
@ RBAC_PERM_COMMAND_BATTLEPET
Definition RBAC.h:699
@ RBAC_PERM_COMMAND_BATTLEPET_LEVELUP
Definition RBAC.h:700
@ RBAC_PERM_COMMAND_BATTLEPET_HEAL
Definition RBAC.h:701
@ RBAC_PERM_COMMAND_BATTLEPET_DEBUG
Definition RBAC.h:702
BattlePetAchievementSource GetAchievementSource() const