TrinityCore
Loading...
Searching...
No Matches
npc_guard.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 "GuardAI.h"
19#include "ObjectAccessor.h"
20#include "Player.h"
21#include "Random.h"
22#include "ScriptMgr.h"
23#include "SpellInfo.h"
24
40
42{
43 npc_guard_generic(Creature* creature) : GuardAI(creature)
44 {
46 {
48 });
50 {
52 });
53 }
54
55 void Reset() override
56 {
59 _scheduler.Schedule(Seconds(1), [this](TaskContext context)
60 {
61 // Find a spell that targets friendly and applies an aura (these are generally buffs)
62 if (SpellInfo const* spellInfo = SelectSpell(me, 0, 0, SELECT_TARGET_ANY_FRIEND, 0, 0, 0, 0, SELECT_EFFECT_AURA))
63 DoCast(me, spellInfo->Id);
64
65 context.Repeat(Minutes(10));
66 });
67 }
68
70 {
71 switch (emote)
72 {
73 case TEXT_EMOTE_KISS:
75 break;
76 case TEXT_EMOTE_WAVE:
78 break;
81 break;
82 case TEXT_EMOTE_SHY:
84 break;
85 case TEXT_EMOTE_RUDE:
88 break;
89 default:
90 break;
91 }
92 }
93
94 void ReceiveEmote(Player* player, uint32 textEmote) override
95 {
96 switch (me->GetEntry())
97 {
101 break;
102 default:
103 return;
104 }
105
106 if (!me->IsFriendlyTo(player))
107 return;
108
109 DoReplyToTextEmote(textEmote);
110 }
111
112 void JustEngagedWith(Unit* who) override
113 {
116
117 _combatScheduler.Schedule(Seconds(1), [this](TaskContext meleeContext)
118 {
119 Unit* victim = me->GetVictim();
120 if (!me->isAttackReady() || !me->IsWithinMeleeRange(victim))
121 {
122 meleeContext.Repeat();
123 return;
124 }
125 if (roll_chance_i(20))
126 {
128 {
129 me->resetAttackTimer();
130 DoCastVictim(spellInfo->Id);
131 meleeContext.Repeat();
132 return;
133 }
134 }
135 me->AttackerStateUpdate(victim);
137 meleeContext.Repeat();
138 }).Schedule(Seconds(5), [this](TaskContext spellContext)
139 {
140 bool healing = false;
141 SpellInfo const* spellInfo = nullptr;
142
143 // Select a healing spell if less than 30% hp and ONLY 33% of the time
144 if (me->HealthBelowPct(30) && roll_chance_i(33))
145 spellInfo = SelectSpell(me, 0, 0, SELECT_TARGET_ANY_FRIEND, 0, 0, 0, 0, SELECT_EFFECT_HEALING);
146
147 // No healing spell available, check if we can cast a ranged spell
148 if (spellInfo)
149 healing = true;
150 else
152
153 // Found a spell
154 if (spellInfo)
155 {
156 if (healing)
157 DoCast(me, spellInfo->Id);
158 else
159 DoCastVictim(spellInfo->Id);
160 spellContext.Repeat(Seconds(5));
161 }
162 else
163 spellContext.Repeat(Seconds(1));
164 });
165 }
166
167 void UpdateAI(uint32 diff) override
168 {
169 _scheduler.Update(diff);
170
171 if (!UpdateVictim())
172 return;
173
175 }
176
177private:
180};
181
183{
185 {
187 {
189 });
190 }
191
192 void Reset() override
193 {
195 }
196
197 void JustEngagedWith(Unit* /*who*/) override
198 {
200 }
201
202 void UpdateAI(uint32 diff) override
203 {
204 if (!UpdateVictim())
205 return;
206
207 _scheduler.Update(diff, std::bind(&GuardAI::DoMeleeAttackIfReady, this));
208 }
209
211 {
212 _scheduler.Schedule(Seconds(5), [this](TaskContext banishContext)
213 {
214 Unit* temp = me->GetVictim();
215 if (temp && temp->GetTypeId() == TYPEID_PLAYER)
216 {
217 DoCast(temp, me->GetEntry() == NPC_ALDOR_VINDICATOR ? SPELL_BANISHED_SHATTRATH_S : SPELL_BANISHED_SHATTRATH_A);
218 ObjectGuid playerGUID = temp->GetGUID();
219 banishContext.Schedule(Seconds(9), [this, playerGUID](TaskContext /*exileContext*/)
220 {
221 if (Unit* temp = ObjectAccessor::GetUnit(*me, playerGUID))
222 {
223 temp->CastSpell(temp, SPELL_EXILE, true);
224 temp->CastSpell(temp, SPELL_BANISH_TELEPORT, true);
225 }
226 ScheduleVanish();
227 });
228 }
229 else
230 banishContext.Repeat();
231 });
232 }
233
234private:
236};
237
@ SELECT_TARGET_ANY_FRIEND
Definition CreatureAI.h:61
@ SELECT_TARGET_ANY_ENEMY
Definition CreatureAI.h:58
@ SELECT_EFFECT_DONTCARE
Definition CreatureAI.h:67
@ SELECT_EFFECT_AURA
Definition CreatureAI.h:70
@ SELECT_EFFECT_HEALING
Definition CreatureAI.h:69
uint32_t uint32
Definition Define.h:133
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition Duration.h:27
std::chrono::minutes Minutes
Minutes shorthand typedef.
Definition Duration.h:30
#define NOMINAL_MELEE_RANGE
@ TYPEID_PLAYER
Definition ObjectGuid.h:39
bool roll_chance_i(int chance)
Definition Random.h:59
if(posix_memalign(&__mallocedMemory, __align, __size)) return NULL
#define RegisterCreatureAI(ai_name)
Definition ScriptMgr.h:1139
@ EMOTE_ONESHOT_POINT
@ EMOTE_ONESHOT_FLEX
@ EMOTE_ONESHOT_BOW
@ EMOTE_ONESHOT_SALUTE
@ EMOTE_ONESHOT_WAVE
@ TEXT_EMOTE_CHICKEN
@ TEXT_EMOTE_SHY
@ TEXT_EMOTE_SALUTE
@ TEXT_EMOTE_RUDE
@ TEXT_EMOTE_KISS
@ TEXT_EMOTE_WAVE
@ UNIT_STATE_CASTING
Definition Unit.h:235
bool UpdateVictim()
Creature *const me
Definition CreatureAI.h:82
bool IsInEvadeMode() const
Definition Creature.h:146
TypeID GetTypeId() const
Definition Object.h:93
uint32 GetEntry() const
Definition Object.h:81
uint32 Id
Definition SpellInfo.h:289
TaskContext & Repeat(std::chrono::duration< _Rep, _Period > const &duration)
TaskScheduler & CancelAll()
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
TaskScheduler & Update(success_t const &callback=EmptyCallback)
TaskScheduler & SetValidator(P &&predicate)
Sets a validator which is asked if tasks are allowed to be executed.
void DoMeleeAttackIfReady()
Definition UnitAI.cpp:54
SpellCastResult DoCastVictim(uint32 spellId, CastSpellExtraArgs const &args={})
Definition UnitAI.cpp:166
SpellCastResult DoCast(uint32 spellId)
Definition UnitAI.cpp:106
Definition Unit.h:769
bool IsWithinMeleeRange(Unit const *obj) const
Definition Unit.h:844
bool IsAlive() const
Definition Unit.h:1234
bool HealthBelowPct(int32 pct) const
Definition Unit.h:917
Unit * GetVictim() const
Definition Unit.h:859
bool HasUnitState(const uint32 f) const
Definition Unit.h:876
void HandleEmoteCommand(Emote emoteId)
Definition Unit.cpp:1568
void AttackerStateUpdate(Unit *victim, WeaponAttackType attType=BASE_ATTACK, bool extra=false)
Definition Unit.cpp:2071
bool isAttackReady(WeaponAttackType type=BASE_ATTACK) const
Definition Unit.h:835
void resetAttackTimer(WeaponAttackType type=BASE_ATTACK)
Definition Unit.cpp:598
bool IsFriendlyTo(WorldObject const *target) const
Definition Object.cpp:2801
GuardMisc
Definition npc_guard.cpp:26
@ NPC_STORMWIND_CITY_GUARD
Definition npc_guard.cpp:30
@ NPC_CENARION_HOLD_INFANTRY
Definition npc_guard.cpp:29
@ NPC_STORMWIND_CITY_PATROLLER
Definition npc_guard.cpp:31
@ NPC_ORGRIMMAR_GRUNT
Definition npc_guard.cpp:32
@ SPELL_EXILE
Definition npc_guard.cpp:38
@ NPC_ALDOR_VINDICATOR
Definition npc_guard.cpp:33
@ SPELL_BANISHED_SHATTRATH_S
Definition npc_guard.cpp:36
@ SPELL_BANISH_TELEPORT
Definition npc_guard.cpp:37
@ SPELL_BANISHED_SHATTRATH_A
Definition npc_guard.cpp:35
@ SAY_GUARD_SIL_AGGRO
Definition npc_guard.cpp:27
void AddSC_npc_guard()
SpellInfo const * SelectSpell(Unit *target, uint32 school, uint32 mechanic, SelectTargetType targets, uint32 powerCostMin, uint32 powerCostMax, float rangeMin, float rangeMax, SelectEffect effect)
void UpdateAI(uint32 diff) override
void DoReplyToTextEmote(uint32 emote)
Definition npc_guard.cpp:69
TaskScheduler _scheduler
npc_guard_generic(Creature *creature)
Definition npc_guard.cpp:43
TaskScheduler _combatScheduler
void ReceiveEmote(Player *player, uint32 textEmote) override
Definition npc_guard.cpp:94
void JustEngagedWith(Unit *who) override
void Reset() override
Definition npc_guard.cpp:55
void UpdateAI(uint32 diff) override
void JustEngagedWith(Unit *) override
npc_guard_shattrath_faction(Creature *creature)