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 "Language.h"
21#include "Log.h"
22#include "Map.h"
23#include "ObjectMgr.h"
24#include "Pet.h"
25#include "Player.h"
26#include "RBAC.h"
27#include "SpellInfo.h"
28#include "SpellMgr.h"
29#include "WorldSession.h"
30
31using namespace Trinity::ChatCommands;
32
34{
35 if (Unit* target = handler->getSelectedUnit())
36 {
37 if (target->GetTypeId() == TYPEID_PLAYER)
38 return target->ToPlayer()->GetPet();
39 if (target->IsPet())
40 return target->ToPet();
41 return nullptr;
42 }
43 Player* player = handler->GetSession()->GetPlayer();
44 return player ? player->GetPet() : nullptr;
45}
46
48{
49public:
50 pet_commandscript() : CommandScript("pet_commandscript") { }
51
53 {
54 static ChatCommandTable petCommandTable =
55 {
60 };
61
62 static ChatCommandTable commandTable =
63 {
64 { "pet", petCommandTable },
65 };
66 return commandTable;
67 }
68 static bool HandlePetCreateCommand(ChatHandler* handler)
69 {
70 Player* player = handler->GetSession()->GetPlayer();
71 Creature* creatureTarget = handler->getSelectedCreature();
72
73 if (!creatureTarget || creatureTarget->IsPet() || creatureTarget->GetTypeId() == TYPEID_PLAYER)
74 {
76 handler->SetSentErrorMessage(true);
77 return false;
78 }
79
80 CreatureTemplate const* creatureTemplate = creatureTarget->GetCreatureTemplate();
81 // Creatures with family CREATURE_FAMILY_NONE crashes the server
82 if (creatureTemplate->family == CREATURE_FAMILY_NONE)
83 {
84 handler->PSendSysMessage("This creature cannot be tamed. Family id: 0 (CREATURE_FAMILY_NONE).");
85 handler->SetSentErrorMessage(true);
86 return false;
87 }
88
89 if (!player->GetPetGUID().IsEmpty())
90 {
91 handler->PSendSysMessage("You already have a pet");
92 handler->SetSentErrorMessage(true);
93 return false;
94 }
95
96 // Everything looks OK, create new pet
97 Pet* pet = player->CreateTamedPetFrom(creatureTarget);
98 if (!pet)
99 {
100 handler->PSendSysMessage("CreateTamedPetFrom returned null.");
101 handler->SetSentErrorMessage(true);
102 return false;
103 }
104
105 // "kill" original creature
106 creatureTarget->DespawnOrUnsummon();
107
108 uint8 level = (creatureTarget->GetLevel() < (player->GetLevel() - 5)) ? (player->GetLevel() - 5) : player->GetLevel();
109
110 // prepare visual effect for levelup
111 pet->SetUInt32Value(UNIT_FIELD_LEVEL, level - 1);
112
113 // add to world
114 pet->GetMap()->AddToMap(pet->ToCreature());
115
116 // visual effect for levelup
117 pet->SetUInt32Value(UNIT_FIELD_LEVEL, level);
118
119 // caster have pet now
120 player->SetMinion(pet, true);
121
122 pet->InitTalentForLevel();
123
125 player->PetSpellInitialize();
126
127 return true;
128 }
129
130 static bool HandlePetLearnCommand(ChatHandler* handler, SpellInfo const* spellInfo)
131 {
132 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
133
134 if (!pet)
135 {
137 handler->SetSentErrorMessage(true);
138 return false;
139 }
140
141 uint32 spellId = spellInfo->Id;
142
143 // Check if pet already has it
144 if (pet->HasSpell(spellId))
145 {
146 handler->PSendSysMessage("Pet already has spell: %u", spellId);
147 handler->SetSentErrorMessage(true);
148 return false;
149 }
150
151 // Check if spell is valid
152 if (!SpellMgr::IsSpellValid(spellInfo))
153 {
155 handler->SetSentErrorMessage(true);
156 return false;
157 }
158
159 pet->learnSpell(spellId);
160
161 handler->PSendSysMessage("Pet has learned spell %u", spellId);
162 return true;
163 }
164
165 static bool HandlePetUnlearnCommand(ChatHandler* handler, SpellInfo const* spellInfo)
166 {
167 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
168 if (!pet)
169 {
171 handler->SetSentErrorMessage(true);
172 return false;
173 }
174
175 uint32 spellId = spellInfo->Id;
176
177 if (pet->HasSpell(spellId))
178 pet->removeSpell(spellId, false);
179 else
180 handler->PSendSysMessage("Pet doesn't have that spell");
181
182 return true;
183 }
184
186 {
187 Pet* pet = GetSelectedPlayerPetOrOwn(handler);
188 Player* owner = pet ? pet->GetOwner() : nullptr;
189 if (!pet || !owner)
190 {
192 handler->SetSentErrorMessage(true);
193 return false;
194 }
195
196 if (!level)
197 level = owner->GetLevel() - pet->GetLevel();
198
199 if (level == 0 || level < -STRONG_MAX_LEVEL || level > STRONG_MAX_LEVEL)
200 {
202 handler->SetSentErrorMessage(true);
203 return false;
204 }
205
206 int32 newLevel = pet->GetLevel() + *level;
207 if (newLevel < 1)
208 newLevel = 1;
209 else if (newLevel > owner->GetLevel())
210 newLevel = owner->GetLevel();
211
212 pet->GivePetLevel(newLevel);
213 return true;
214 }
215};
216
218{
219 new pet_commandscript();
220}
@ STRONG_MAX_LEVEL
Definition DBCEnums.h:53
uint8_t uint8
Definition Define.h:135
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
@ LANG_SELECT_CREATURE
Definition Language.h:32
@ LANG_SELECT_PLAYER_OR_PET
Definition Language.h:1241
@ LANG_BAD_VALUE
Definition Language.h:149
@ LANG_COMMAND_SPELL_BROKEN
Definition Language.h:551
@ TYPEID_PLAYER
Definition ObjectGuid.h:39
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ PET_SAVE_AS_CURRENT
Definition PetDefines.h:42
Role Based Access Control related classes definition.
@ CREATURE_FAMILY_NONE
@ UNIT_FIELD_LEVEL
Unit * getSelectedUnit()
Definition Chat.cpp:314
WorldSession * GetSession()
Definition Chat.h:46
Creature * getSelectedCreature()
Definition Chat.cpp:338
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
void DespawnOrUnsummon(Milliseconds timeToDespawn=0s, Seconds forceRespawnTime=0s)
CreatureTemplate const * GetCreatureTemplate() const
Definition Creature.h:186
bool AddToMap(T *)
Definition Map.cpp:630
bool IsEmpty() const
Definition ObjectGuid.h:172
static Creature * ToCreature(Object *o)
Definition Object.h:186
TypeID GetTypeId() const
Definition Object.h:93
void SetUInt32Value(uint16 index, uint32 value)
Definition Object.cpp:585
static Player * ToPlayer(Object *o)
Definition Object.h:180
Definition Pet.h:40
void GivePetLevel(uint8 level)
Definition Pet.cpp:774
Player * GetOwner() const
Definition Pet.cpp:2000
void InitTalentForLevel()
Definition Pet.cpp:1788
void SavePetToDB(PetSaveMode mode)
Definition Pet.cpp:437
bool removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab=true)
Definition Pet.cpp:1570
bool HasSpell(uint32 spell) const override
Definition Pet.cpp:1896
bool learnSpell(uint32 spell_id)
Definition Pet.cpp:1500
void PetSpellInitialize()
Definition Player.cpp:20596
Pet * GetPet() const
Definition Player.cpp:20286
uint32 Id
Definition SpellInfo.h:289
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:71
Definition Unit.h:769
void SetMinion(Minion *minion, bool apply)
Definition Unit.cpp:5910
Pet * ToPet()
Definition Unit.h:1788
Pet * CreateTamedPetFrom(Creature *creatureTarget, uint32 spell_id=0)
Definition Unit.cpp:10856
bool IsPet() const
Definition Unit.h:884
uint8 GetLevel() const
Definition Unit.h:889
ObjectGuid GetPetGUID() const
Definition Unit.h:1247
Map * GetMap() const
Definition Object.h:449
Player * GetPlayer() const
ChatCommandTable GetCommands() const override
Definition cs_pet.cpp:52
static bool HandlePetLevelCommand(ChatHandler *handler, Optional< int32 > level)
Definition cs_pet.cpp:185
static bool HandlePetUnlearnCommand(ChatHandler *handler, SpellInfo const *spellInfo)
Definition cs_pet.cpp:165
static bool HandlePetLearnCommand(ChatHandler *handler, SpellInfo const *spellInfo)
Definition cs_pet.cpp:130
static bool HandlePetCreateCommand(ChatHandler *handler)
Definition cs_pet.cpp:68
Pet * GetSelectedPlayerPetOrOwn(ChatHandler *handler)
Definition cs_pet.cpp:33
void AddSC_pet_commandscript()
Definition cs_pet.cpp:217
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:50
@ RBAC_PERM_COMMAND_PET_LEARN
Definition RBAC.h:351
@ RBAC_PERM_COMMAND_PET_LEVEL
Definition RBAC.h:707
@ RBAC_PERM_COMMAND_PET_CREATE
Definition RBAC.h:350
@ RBAC_PERM_COMMAND_PET_UNLEARN
Definition RBAC.h:352
CreatureFamily family