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
98 // "kill" original creature
99 creatureTarget->DespawnOrUnsummon();
100
101 // prepare visual effect for levelup
102 pet->SetLevel(player->GetLevel() - 1);
103
104 // add to world
105 pet->GetMap()->AddToMap(pet->ToCreature());
106
107 // visual effect for levelup
108 pet->SetLevel(player->GetLevel());
109
110 // caster have pet now
111 player->SetMinion(pet, true);
112
114 player->PetSpellInitialize();
115
116 return true;
117 }
118
119 static bool HandlePetLearnCommand(ChatHandler* handler, SpellInfo const* spellInfo)
120 {
121 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
122
123 if (!pet)
124 {
126 handler->SetSentErrorMessage(true);
127 return false;
128 }
129
130 uint32 spellId = spellInfo->Id;
131
132 // Check if pet already has it
133 if (pet->HasSpell(spellId))
134 {
135 handler->PSendSysMessage("Pet already has spell: %u", spellId);
136 handler->SetSentErrorMessage(true);
137 return false;
138 }
139
140 // Check if spell is valid
141 if (!SpellMgr::IsSpellValid(spellInfo))
142 {
144 handler->SetSentErrorMessage(true);
145 return false;
146 }
147
148 pet->learnSpell(spellId);
149
150 handler->PSendSysMessage("Pet has learned spell %u", spellId);
151 return true;
152 }
153
154 static bool HandlePetUnlearnCommand(ChatHandler* handler, SpellInfo const* spellInfo)
155 {
156 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
157 if (!pet)
158 {
160 handler->SetSentErrorMessage(true);
161 return false;
162 }
163
164 uint32 spellId = spellInfo->Id;
165
166 if (pet->HasSpell(spellId))
167 pet->removeSpell(spellId, false);
168 else
169 handler->PSendSysMessage("Pet doesn't have that spell");
170
171 return true;
172 }
173
175 {
176 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
177 Player* owner = pet ? pet->GetOwner() : nullptr;
178 if (!pet || !owner)
179 {
181 handler->SetSentErrorMessage(true);
182 return false;
183 }
184
185 if (!level)
186 level = owner->GetLevel() - pet->GetLevel();
187
188 if (level == 0 || level < -STRONG_MAX_LEVEL || level > STRONG_MAX_LEVEL)
189 {
191 handler->SetSentErrorMessage(true);
192 return false;
193 }
194
195 int32 newLevel = pet->GetLevel() + *level;
196 if (newLevel < 1)
197 newLevel = 1;
198 else if (newLevel > owner->GetLevel())
199 newLevel = owner->GetLevel();
200
201 pet->GivePetLevel(newLevel);
202 return true;
203 }
204};
205
207{
208 new pet_commandscript();
209}
@ 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:266
bool AddToMap(T *)
Definition Map.cpp:517
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:1801
void SavePetToDB(PetSaveMode mode)
Definition Pet.cpp:451
bool removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab=true)
Definition Pet.cpp:1553
bool HasSpell(uint32 spell) const override
Definition Pet.cpp:1703
bool learnSpell(uint32 spell_id)
Definition Pet.cpp:1453
void PetSpellInitialize()
Definition Player.cpp:22471
Pet * GetPet() const
Definition Player.cpp:22060
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:635
void SetMinion(Minion *minion, bool apply)
Definition Unit.cpp:6247
Pet * ToPet()
Definition Unit.h:1822
Pet * CreateTamedPetFrom(Creature *creatureTarget, uint32 spell_id=0)
Definition Unit.cpp:11072
bool IsPet() const
Definition Unit.h:751
void SetLevel(uint8 lvl, bool sendUpdate=true)
Definition Unit.cpp:9956
uint8 GetLevel() const
Definition Unit.h:757
ObjectGuid GetPetGUID() const
Definition Unit.h:1197
Map * GetMap() const
Definition Object.h:411
Player * GetPlayer() const
static bool HandlePetLevelCommand(ChatHandler *handler, Optional< int32 > level)
Definition cs_pet.cpp:174
static bool HandlePetUnlearnCommand(ChatHandler *handler, SpellInfo const *spellInfo)
Definition cs_pet.cpp:154
static bool HandlePetLearnCommand(ChatHandler *handler, SpellInfo const *spellInfo)
Definition cs_pet.cpp:119
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:206
ChatCommandBuilder const [] ChatCommandTable
Definition ChatCommand.h:49
@ RBAC_PERM_COMMAND_PET_LEARN
Definition RBAC.h:353
@ RBAC_PERM_COMMAND_PET_LEVEL
Definition RBAC.h:709
@ RBAC_PERM_COMMAND_PET_CREATE
Definition RBAC.h:352
@ RBAC_PERM_COMMAND_PET_UNLEARN
Definition RBAC.h:354
CreatureFamily family