TrinityCore
cs_cast.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/* ScriptData
19Name: cast_commandscript
20%Complete: 100
21Comment: All cast related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "Chat.h"
27#include "ChatCommand.h"
28#include "Creature.h"
29#include "Language.h"
30#include "Player.h"
31#include "RBAC.h"
32#include "SpellInfo.h"
33#include "SpellMgr.h"
34#include "WorldSession.h"
35
36using namespace Trinity::ChatCommands;
37
39{
40public:
41 cast_commandscript() : CommandScript("cast_commandscript") { }
42
44 {
45 static ChatCommandTable castCommandTable =
46 {
53 };
54 static ChatCommandTable commandTable =
55 {
56 { "cast", castCommandTable },
57 };
58 return commandTable;
59 }
60
61 static bool CheckSpellExistsAndIsValid(ChatHandler* handler, SpellInfo const* spell)
62 {
63 if (!spell)
64 {
66 handler->SetSentErrorMessage(true);
67 return false;
68 }
69
70 if (!SpellMgr::IsSpellValid(spell, handler->GetSession()->GetPlayer()))
71 {
73 handler->SetSentErrorMessage(true);
74 return false;
75 }
76 return true;
77 }
78
80 {
81 if (triggeredStr)
82 {
83 if (StringStartsWith("triggered", *triggeredStr)) // check if "triggered" starts with *triggeredStr (e.g. "trig", "trigger", etc.)
85 else
86 return std::nullopt;
87 }
88 return TRIGGERED_NONE;
89 }
90
91 static bool HandleCastCommand(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
92 {
93 Unit* target = handler->getSelectedUnit();
94 if (!target)
95 {
97 handler->SetSentErrorMessage(true);
98 return false;
99 }
100
101 if (!CheckSpellExistsAndIsValid(handler, spell))
102 return false;
103
104 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
105 if (!triggerFlags)
106 return false;
107
108 handler->GetSession()->GetPlayer()->CastSpell(target, spell->Id, *triggerFlags);
109
110 return true;
111 }
112
113 static bool HandleCastBackCommand(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
114 {
115 Creature* caster = handler->getSelectedCreature();
116 if (!caster)
117 {
119 handler->SetSentErrorMessage(true);
120 return false;
121 }
122
123 if (!CheckSpellExistsAndIsValid(handler, spell))
124 return false;
125
126 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
127 if (!triggerFlags)
128 return false;
129
130 caster->CastSpell(handler->GetSession()->GetPlayer(), spell->Id, *triggerFlags);
131
132 return true;
133 }
134
135 static bool HandleCastDistCommand(ChatHandler* handler, SpellInfo const* spell, float dist, Optional<std::string> triggeredStr)
136 {
137 if (!CheckSpellExistsAndIsValid(handler, spell))
138 return false;
139
140 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
141 if (!triggerFlags)
142 return false;
143
144 float x, y, z;
145 handler->GetSession()->GetPlayer()->GetClosePoint(x, y, z, dist);
146 handler->GetSession()->GetPlayer()->CastSpell(Position{ x, y, z }, spell->Id, *triggerFlags);
147
148 return true;
149 }
150
151 static bool HandleCastSelfCommand(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
152 {
153 Unit* target = handler->getSelectedUnit();
154 if (!target)
155 {
157 handler->SetSentErrorMessage(true);
158 return false;
159 }
160
161 if (!CheckSpellExistsAndIsValid(handler, spell))
162 return false;
163
164 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
165 if (!triggerFlags)
166 return false;
167
168 target->CastSpell(target, spell->Id, *triggerFlags);
169
170 return true;
171 }
172
173 static bool HandleCastTargetCommad(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
174 {
175 Creature* caster = handler->getSelectedCreature();
176 if (!caster)
177 {
179 handler->SetSentErrorMessage(true);
180 return false;
181 }
182
183 if (!caster->GetVictim())
184 {
186 handler->SetSentErrorMessage(true);
187 return false;
188 }
189
190 if (!CheckSpellExistsAndIsValid(handler, spell))
191 return false;
192
193 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
194 if (!triggerFlags)
195 return false;
196
197 caster->CastSpell(caster->GetVictim(), spell->Id, *triggerFlags);
198
199 return true;
200 }
201
202 static bool HandleCastDestCommand(ChatHandler* handler, SpellInfo const* spell, float x, float y, float z, Optional<std::string> triggeredStr)
203 {
204 Unit* caster = handler->getSelectedUnit();
205 if (!caster)
206 {
208 handler->SetSentErrorMessage(true);
209 return false;
210 }
211
212 if (!CheckSpellExistsAndIsValid(handler, spell))
213 return false;
214
215 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
216 if (!triggerFlags)
217 return false;
218
219 caster->CastSpell(Position{ x, y, z }, spell->Id, *triggerFlags);
220
221 return true;
222 }
223};
224
226{
227 new cast_commandscript();
228}
@ LANG_SELECTED_TARGET_NOT_HAVE_VICTIM
Definition: Language.h:662
@ LANG_COMMAND_NOSPELLFOUND
Definition: Language.h:505
@ LANG_SELECT_CHAR_OR_CREATURE
Definition: Language.h:31
@ LANG_COMMAND_SPELL_BROKEN
Definition: Language.h:548
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
Role Based Access Control related classes definition.
@ TRIGGERED_NONE
Not triggered.
Definition: SpellDefines.h:246
@ TRIGGERED_FULL_DEBUG_MASK
Definition: SpellDefines.h:272
bool StringStartsWith(std::string_view haystack, std::string_view needle)
Definition: Util.h:398
Unit * getSelectedUnit()
Definition: Chat.cpp:212
WorldSession * GetSession()
Definition: Chat.h:42
void PSendSysMessage(const char *fmt, Args &&... args)
Definition: Chat.h:57
Creature * getSelectedCreature()
Definition: Chat.cpp:236
void SetSentErrorMessage(bool val)
Definition: Chat.h:114
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition: Chat.cpp:113
uint32 const Id
Definition: SpellInfo.h:325
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:142
Definition: Unit.h:627
Unit * GetVictim() const
Definition: Unit.h:715
void GetClosePoint(float &x, float &y, float &z, float size, float distance2d=0, float relAngle=0) const
Definition: Object.cpp:3403
SpellCastResult CastSpell(CastSpellTargetArg const &targets, uint32 spellId, CastSpellExtraArgs const &args={ })
Definition: Object.cpp:2896
Player * GetPlayer() const
static bool CheckSpellExistsAndIsValid(ChatHandler *handler, SpellInfo const *spell)
Definition: cs_cast.cpp:61
static bool HandleCastDestCommand(ChatHandler *handler, SpellInfo const *spell, float x, float y, float z, Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:202
static Optional< TriggerCastFlags > GetTriggerFlags(Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:79
static bool HandleCastSelfCommand(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:151
static bool HandleCastBackCommand(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:113
static bool HandleCastTargetCommad(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:173
static bool HandleCastDistCommand(ChatHandler *handler, SpellInfo const *spell, float dist, Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:135
ChatCommandTable GetCommands() const override
Definition: cs_cast.cpp:43
static bool HandleCastCommand(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition: cs_cast.cpp:91
void AddSC_cast_commandscript()
Definition: cs_cast.cpp:225
std::vector< ChatCommandBuilder > ChatCommandTable
Definition: ChatCommand.h:49
@ RBAC_PERM_COMMAND_CAST_SELF
Definition: RBAC.h:184
@ RBAC_PERM_COMMAND_CAST_DEST
Definition: RBAC.h:186
@ RBAC_PERM_COMMAND_CAST
Definition: RBAC.h:181
@ RBAC_PERM_COMMAND_CAST_TARGET
Definition: RBAC.h:185
@ RBAC_PERM_COMMAND_CAST_BACK
Definition: RBAC.h:182
@ RBAC_PERM_COMMAND_CAST_DIST
Definition: RBAC.h:183