Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
cs_pet.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 "Chat.h"
7#include "Language.h"
8#include "ObjectMgr.h"
9#include "Pet.h"
10#include "Player.h"
11#include "ScriptMgr.h"
12
14{
15public:
16 pet_commandscript() : CommandScript("pet_commandscript") { }
17
18 std::vector<ChatCommand> GetCommands() const OVERRIDE
19 {
20 static std::vector<ChatCommand> petCommandTable =
21 {
25 };
26
27 static std::vector<ChatCommand> commandTable =
28 {
29 { "pet", rbac::RBAC_PERM_COMMAND_PET, false, NULL, "", petCommandTable },
30 };
31 return commandTable;
32 }
33 static bool HandlePetCreateCommand(ChatHandler* handler, char const* /*args*/)
34 {
35 Player* player = handler->GetSession()->GetPlayer();
36 Creature* creatureTarget = handler->getSelectedCreature();
37
38 if (!creatureTarget || creatureTarget->IsPet() || creatureTarget->GetTypeId() == TypeID::TYPEID_PLAYER)
39 {
41 handler->SetSentErrorMessage(true);
42 return false;
43 }
44
45 CreatureTemplate const* creatrueTemplate = creatureTarget->GetCreatureTemplate();
46 // Creatures with family 0 crashes the server
47 if (!creatrueTemplate->family)
48 {
49 handler->PSendSysMessage("This creature cannot be tamed. (family id: 0).");
50 handler->SetSentErrorMessage(true);
51 return false;
52 }
53
54 if (player->GetPetGUID())
55 {
56 handler->PSendSysMessage("You already have a pet");
57 handler->SetSentErrorMessage(true);
58 return false;
59 }
60
61 // Everything looks OK, create new pet
62 Pet* pet = new Pet(player, PetType::HUNTER_PET);
63 if (!pet->CreateBaseAtCreature(creatureTarget))
64 {
65 delete pet;
66 handler->PSendSysMessage("Error 1");
67 return false;
68 }
69
70 creatureTarget->setDeathState(DeathState::JUST_DIED);
71 creatureTarget->RemoveCorpse();
72 creatureTarget->SetHealth(0); // just for nice GM-mode view
73
76
77 if (!pet->InitStatsForLevel(creatureTarget->getLevel()))
78 {
79 SF_LOG_ERROR("misc", "InitStatsForLevel() in EffectTameCreature failed! Pet deleted.");
80 handler->PSendSysMessage("Error 2");
81 delete pet;
82 return false;
83 }
84
85 // prepare visual effect for levelup
86 pet->SetUInt32Value(UNIT_FIELD_LEVEL, creatureTarget->getLevel() - 1);
87
88 pet->GetCharmInfo()->SetPetNumber(sObjectMgr->GeneratePetNumber(), true);
89 // this enables pet details window (Shift+P)
91 pet->SetFullHealth();
92
93 pet->GetMap()->AddToMap(pet->ToCreature());
94
95 // visual effect for levelup
96 pet->SetUInt32Value(UNIT_FIELD_LEVEL, creatureTarget->getLevel());
97
98 player->SetMinion(pet, true);
100 player->PetSpellInitialize();
101
102 return true;
103 }
104
105 static bool HandlePetLearnCommand(ChatHandler* handler, char const* args)
106 {
107 if (!*args)
108 return false;
109
110 Player* player = handler->GetSession()->GetPlayer();
111 Pet* pet = player->GetPet();
112
113 if (!pet)
114 {
115 handler->PSendSysMessage("You have no pet");
116 handler->SetSentErrorMessage(true);
117 return false;
118 }
119
120 uint32 spellId = handler->extractSpellIdFromLink((char*)args);
121
122 if (!spellId || !sSpellMgr->GetSpellInfo(spellId))
123 return false;
124
125 // Check if pet already has it
126 if (pet->HasSpell(spellId))
127 {
128 handler->PSendSysMessage("Pet already has spell: %u", spellId);
129 handler->SetSentErrorMessage(true);
130 return false;
131 }
132
133 // Check if spell is valid
134 SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spellId);
135 if (!spellInfo || !SpellMgr::IsSpellValid(spellInfo))
136 {
138 handler->SetSentErrorMessage(true);
139 return false;
140 }
141
142 pet->learnSpell(spellId);
143
144 handler->PSendSysMessage("Pet has learned spell %u", spellId);
145 return true;
146 }
147
148 static bool HandlePetUnlearnCommand(ChatHandler* handler, char const* args)
149 {
150 if (!*args)
151 return false;
152
153 Player* player = handler->GetSession()->GetPlayer();
154 Pet* pet = player->GetPet();
155 if (!pet)
156 {
157 handler->PSendSysMessage("You have no pet");
158 handler->SetSentErrorMessage(true);
159 return false;
160 }
161
162 uint32 spellId = handler->extractSpellIdFromLink((char*)args);
163
164 if (pet->HasSpell(spellId))
165 pet->removeSpell(spellId, false);
166 else
167 handler->PSendSysMessage("Pet doesn't have that spell");
168
169 return true;
170 }
171};
172
174{
175 new pet_commandscript();
176}
std::uint32_t uint32
Definition Define.h:77
#define OVERRIDE
Definition Define.h:60
@ LANG_SELECT_CREATURE
Definition Language.h:13
@ LANG_COMMAND_SPELL_BROKEN
Definition Language.h:468
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
@ TYPEID_PLAYER
Definition Object.h:55
#define sObjectMgr
Definition ObjectMgr.h:1617
@ PET_SAVE_AS_CURRENT
Definition PetDefines.h:22
@ HUNTER_PET
Definition PetDefines.h:12
#define sSpellMgr
Definition SpellMgr.h:744
@ JUST_DIED
Definition Unit.h:504
@ UNIT_FIELD_LEVEL
@ UNIT_FIELD_FACTION_TEMPLATE
@ UNIT_FIELD_CREATED_BY
WorldSession * GetSession()
Definition Chat.h:43
Creature * getSelectedCreature()
Definition Chat.cpp:865
uint32 extractSpellIdFromLink(char *text)
Definition Chat.cpp:1042
void PSendSysMessage(const char *format,...) ATTR_PRINTF(2
Definition Chat.cpp:221
void SetSentErrorMessage(bool val)
Definition Chat.h:114
CommandScript(const char *name)
void RemoveCorpse(bool setSpawnTime=true)
Definition Creature.cpp:230
CreatureTemplate const * GetCreatureTemplate() const
Definition Creature.h:524
void setDeathState(DeathState s) OVERRIDE
bool InitStatsForLevel(uint8 level)
Definition Pet.cpp:828
bool AddToMap(T *)
Definition Map.cpp:504
uint64 GetGUID() const
Definition Object.h:119
TypeID GetTypeId() const
Definition Object.h:131
Creature * ToCreature()
Definition Object.h:207
void SetUInt32Value(uint16 index, uint32 value)
Definition Object.cpp:1038
void SetUInt64Value(uint16 index, uint64 value)
Definition Object.cpp:1079
Definition Pet.h:28
void SavePetToDB(PetSaveMode mode)
Definition Pet.cpp:365
bool removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab=true)
Definition Pet.cpp:1601
bool CreateBaseAtCreature(Creature *creature)
Definition Pet.cpp:755
bool HasSpell(uint32 spell) const
Definition Pet.cpp:1942
void InitPetCreateSpells()
Definition Pet.cpp:1668
bool learnSpell(uint32 spell_id)
Definition Pet.cpp:1528
void PetSpellInitialize()
Definition Player.cpp:15884
Pet * GetPet() const
Definition Player.cpp:15640
static bool IsSpellValid(SpellInfo const *spellInfo, Player *player=NULL, bool msg=true)
Some checks for spells, to prevent adding deprecated/broken spells for trainers, spell book,...
Definition SpellMgr.cpp:319
void SetMinion(Minion *minion, bool apply)
Definition Unit.cpp:2432
uint32 getFaction() const
Definition Unit.h:1695
void SetFullHealth()
Definition Unit.h:1644
uint64 GetPetGUID() const
Definition Unit.h:2087
bool IsPet() const
Definition Unit.h:1511
void SetHealth(uint32 val)
Definition Unit.cpp:5162
CharmInfo * GetCharmInfo()
Definition Unit.h:2148
uint8 getLevel() const
Definition Unit.h:1528
Map * GetMap() const
Definition Object.h:740
Player * GetPlayer() const
std::vector< ChatCommand > GetCommands() const OVERRIDE
Definition cs_pet.cpp:18
static bool HandlePetLearnCommand(ChatHandler *handler, char const *args)
Definition cs_pet.cpp:105
static bool HandlePetUnlearnCommand(ChatHandler *handler, char const *args)
Definition cs_pet.cpp:148
static bool HandlePetCreateCommand(ChatHandler *handler, char const *)
Definition cs_pet.cpp:33
void AddSC_pet_commandscript()
Definition cs_pet.cpp:173
@ RBAC_PERM_COMMAND_PET_LEARN
Definition RBAC.h:369
@ RBAC_PERM_COMMAND_PET_CREATE
Definition RBAC.h:368
@ RBAC_PERM_COMMAND_PET
Definition RBAC.h:367
@ RBAC_PERM_COMMAND_PET_UNLEARN
Definition RBAC.h:370
void SetPetNumber(uint32 petnumber, bool statwindow)
Definition Unit.cpp:5737