TrinityCore
Loading...
Searching...
No Matches
cs_pet.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "ScriptMgr.h"
19#include "Chat.h"
20#include "ChatCommand.h"
21#include "Language.h"
22#include "Map.h"
23#include "Pet.h"
24#include "Player.h"
25#include "RBAC.h"
26#include "SpellInfo.h"
27#include "SpellMgr.h"
28#include "WorldSession.h"
29
30using namespace Trinity::ChatCommands;
31
33{
34 if (Unit* target = handler->getSelectedUnit())
35 {
36 if (target->GetTypeId() == TYPEID_PLAYER)
37 return target->ToPlayer()->GetPet();
38 if (target->IsPet())
39 return target->ToPet();
40 return nullptr;
41 }
42 Player* player = handler->GetSession()->GetPlayer();
43 return player ? player->GetPet() : nullptr;
44}
45
47{
48public:
49 pet_commandscript() : CommandScript("pet_commandscript") { }
50
51 std::span<ChatCommandBuilder const> GetCommands() const override
52 {
53 static ChatCommandTable petCommandTable =
54 {
59 };
60
61 static ChatCommandTable commandTable =
62 {
63 { "pet", petCommandTable },
64 };
65 return commandTable;
66 }
67 static bool HandlePetCreateCommand(ChatHandler* handler)
68 {
69 Player* player = handler->GetSession()->GetPlayer();
70 Creature* creatureTarget = handler->getSelectedCreature();
71
72 if (!creatureTarget || creatureTarget->IsPet() || creatureTarget->GetTypeId() == TYPEID_PLAYER)
73 {
75 handler->SetSentErrorMessage(true);
76 return false;
77 }
78
79 CreatureTemplate const* creatureTemplate = creatureTarget->GetCreatureTemplate();
80 // Creatures with family CREATURE_FAMILY_NONE crashes the server
81 if (creatureTemplate->family == CREATURE_FAMILY_NONE)
82 {
83 handler->PSendSysMessage("This creature cannot be tamed. Family id: 0 (CREATURE_FAMILY_NONE).");
84 handler->SetSentErrorMessage(true);
85 return false;
86 }
87
88 if (!player->GetPetGUID().IsEmpty())
89 {
90 handler->PSendSysMessage("You already have a pet");
91 handler->SetSentErrorMessage(true);
92 return false;
93 }
94
95 // Everything looks OK, create new pet
96 Pet* pet = player->CreateTamedPetFrom(creatureTarget);
97 if (!pet)
98 {
99 handler->PSendSysMessage("CreateTamedPetFrom returned null.");
100 handler->SetSentErrorMessage(true);
101 return false;
102 }
103
104 // "kill" original creature
105 creatureTarget->DespawnOrUnsummon();
106
107 // add to world
108 pet->GetMap()->AddToMap(pet->ToCreature());
109
110 // visual effect for levelup
111 pet->SendNewlyTamed();
112
113 // caster have pet now
114 player->SetMinion(pet, true);
115
117 player->PetSpellInitialize();
118
119 return true;
120 }
121
122 static bool HandlePetLearnCommand(ChatHandler* handler, SpellInfo const* spellInfo)
123 {
124 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
125
126 if (!pet)
127 {
129 handler->SetSentErrorMessage(true);
130 return false;
131 }
132
133 uint32 spellId = spellInfo->Id;
134
135 // Check if pet already has it
136 if (pet->HasSpell(spellId))
137 {
138 handler->PSendSysMessage("Pet already has spell: %u", spellId);
139 handler->SetSentErrorMessage(true);
140 return false;
141 }
142
143 // Check if spell is valid
144 if (!SpellMgr::IsSpellValid(spellInfo))
145 {
147 handler->SetSentErrorMessage(true);
148 return false;
149 }
150
151 pet->learnSpell(spellId);
152
153 handler->PSendSysMessage("Pet has learned spell %u", spellId);
154 return true;
155 }
156
157 static bool HandlePetUnlearnCommand(ChatHandler* handler, SpellInfo const* spellInfo)
158 {
159 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
160 if (!pet)
161 {
163 handler->SetSentErrorMessage(true);
164 return false;
165 }
166
167 uint32 spellId = spellInfo->Id;
168
169 if (pet->HasSpell(spellId))
170 pet->removeSpell(spellId, false);
171 else
172 handler->PSendSysMessage("Pet doesn't have that spell");
173
174 return true;
175 }
176
178 {
179 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
180 Player* owner = pet ? pet->GetOwner() : nullptr;
181 if (!pet || !owner)
182 {
184 handler->SetSentErrorMessage(true);
185 return false;
186 }
187
188 if (!level)
189 level = owner->GetLevel() - pet->GetLevel();
190
191 if (level == 0 || level < -STRONG_MAX_LEVEL || level > STRONG_MAX_LEVEL)
192 {
194 handler->SetSentErrorMessage(true);
195 return false;
196 }
197
198 int32 newLevel = pet->GetLevel() + *level;
199 if (newLevel < 1)
200 newLevel = 1;
201 else if (newLevel > owner->GetLevel())
202 newLevel = owner->GetLevel();
203
204 pet->GivePetLevel(newLevel);
205 return true;
206 }
207};
208
210{
211 new pet_commandscript();
212}
@ STRONG_MAX_LEVEL
Definition DBCEnums.h:49
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
@ LANG_SELECT_CREATURE
Definition Language.h:32
@ LANG_SELECT_PLAYER_OR_PET
Definition Language.h:1251
@ LANG_BAD_VALUE
Definition Language.h:149
@ LANG_COMMAND_SPELL_BROKEN
Definition Language.h:551
@ TYPEID_PLAYER
Definition ObjectGuid.h:44
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ PET_SAVE_AS_CURRENT
Definition PetDefines.h:43
Role Based Access Control related classes definition.
@ CREATURE_FAMILY_NONE
TypeID GetTypeId() const
Definition BaseEntity.h:166
Unit * getSelectedUnit()
Definition Chat.cpp:216
WorldSession * GetSession()
Definition Chat.h:42
Creature * getSelectedCreature()
Definition Chat.cpp:240
void SetSentErrorMessage(bool val)
Definition Chat.h:127
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:62
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:111
void DespawnOrUnsummon(Milliseconds timeToDespawn=0s, Seconds forceRespawnTime=0s)
CreatureTemplate const * GetCreatureTemplate() const
Definition Creature.h:274
bool AddToMap(T *)
Definition Map.cpp:527
bool IsEmpty() const
Definition ObjectGuid.h:362
Player * ToPlayer()
Definition Object.h:126
Creature * ToCreature()
Definition Object.h:121
Definition Pet.h:40
void GivePetLevel(uint8 level)
Definition Pet.cpp:753
Player * GetOwner() const
Definition Pet.cpp:1809
void SavePetToDB(PetSaveMode mode)
Definition Pet.cpp:451
bool removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab=true)
Definition Pet.cpp:1562
bool HasSpell(uint32 spell) const override
Definition Pet.cpp:1711
void SendNewlyTamed(bool playPingFx=true) const
Definition Pet.cpp:838
bool learnSpell(uint32 spell_id)
Definition Pet.cpp:1462
void PetSpellInitialize()
Definition Player.cpp:22418
Pet * GetPet() const
Definition Player.cpp:22007
uint32 const Id
Definition SpellInfo.h:328
static bool IsSpellValid(SpellInfo const *spellInfo, Player *player=nullptr, bool msg=true)
Some checks for spells, to prevent adding deprecated/broken spells for trainers, spell book,...
Definition SpellMgr.cpp:143
Definition Unit.h:647
void SetMinion(Minion *minion, bool apply)
Definition Unit.cpp:6266
Pet * ToPet()
Definition Unit.h:1838
Pet * CreateTamedPetFrom(Creature *creatureTarget, uint32 spell_id=0)
Definition Unit.cpp:11160
bool IsPet() const
Definition Unit.h:763
uint8 GetLevel() const
Definition Unit.h:769
ObjectGuid GetPetGUID() const
Definition Unit.h:1217
Map * GetMap() const
Definition Object.h:411
Player * GetPlayer() const
static bool HandlePetLevelCommand(ChatHandler *handler, Optional< int32 > level)
Definition cs_pet.cpp:177
static bool HandlePetUnlearnCommand(ChatHandler *handler, SpellInfo const *spellInfo)
Definition cs_pet.cpp:157
static bool HandlePetLearnCommand(ChatHandler *handler, SpellInfo const *spellInfo)
Definition cs_pet.cpp:122
static bool HandlePetCreateCommand(ChatHandler *handler)
Definition cs_pet.cpp:67
std::span< ChatCommandBuilder const > GetCommands() const override
Definition cs_pet.cpp:51
Pet * GetSelectedPlayerPetOrOwn(ChatHandler *handler)
Definition cs_pet.cpp:32
void AddSC_pet_commandscript()
Definition cs_pet.cpp:209
ChatCommandBuilder const [] ChatCommandTable
Definition ChatCommand.h:49
@ RBAC_PERM_COMMAND_PET_LEARN
Definition RBAC.h:355
@ RBAC_PERM_COMMAND_PET_LEVEL
Definition RBAC.h:711
@ RBAC_PERM_COMMAND_PET_CREATE
Definition RBAC.h:354
@ RBAC_PERM_COMMAND_PET_UNLEARN
Definition RBAC.h:356
CreatureFamily family