TrinityCore
TotemAI.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 "TotemAI.h"
19#include "CellImpl.h"
20#include "Creature.h"
21#include "GridNotifiers.h"
22#include "GridNotifiersImpl.h"
23#include "ObjectAccessor.h"
24#include "SpellInfo.h"
25#include "SpellMgr.h"
26#include "Totem.h"
27
29{
30 if (creature->IsTotem())
32
33 return PERMIT_BASE_NO;
34}
35
36TotemAI::TotemAI(Creature* creature, uint32 scriptId) : NullCreatureAI(creature, scriptId), _victimGUID()
37{
38 ASSERT(creature->IsTotem(), "TotemAI: AI assigned to a non-totem creature (%s)!", creature->GetGUID().ToString().c_str());
39}
40
42{
44 return;
45
46 if (!me->IsAlive() || me->IsNonMeleeSpellCast(false))
47 return;
48
49 // Search spell
50 SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(me->ToTotem()->GetSpell(), me->GetMap()->GetDifficultyID());
51 if (!spellInfo)
52 return;
53
54 // Get spell range
55 float max_range = spellInfo->GetMaxRange(false);
56
57 // SpellModOp::Range not applied in this place just because not existence range mods for attacking totems
58
59 // pointer to appropriate target if found any
61
62 // Search victim if no, not attackable, or out of range, or friendly (possible in case duel end)
63 if (!victim || !victim->isTargetableForAttack() || !me->IsWithinDistInMap(victim, max_range) || me->IsFriendlyTo(victim) || !me->CanSeeOrDetect(victim))
64 {
65 victim = nullptr;
66 float extraSearchRadius = max_range > 0.0f ? EXTRA_CELL_SEARCH_RADIUS : 0.0f;
69 Cell::VisitAllObjects(me, checker, max_range + extraSearchRadius);
70 }
71
72 // If have target
73 if (victim)
74 {
75 // remember
76 _victimGUID = victim->GetGUID();
77
78 // attack
79 me->CastSpell(victim, me->ToTotem()->GetSpell());
80 }
81 else
83}
84
85void TotemAI::AttackStart(Unit* /*victim*/)
86{
87}
@ PERMIT_BASE_PROACTIVE
Definition: CreatureAI.h:47
@ PERMIT_BASE_NO
Definition: CreatureAI.h:44
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
#define EXTRA_CELL_SEARCH_RADIUS
Definition: ObjectDefines.h:47
#define sSpellMgr
Definition: SpellMgr.h:849
@ TOTEM_ACTIVE
Definition: Totem.h:26
Creature *const me
Definition: CreatureAI.h:61
Difficulty GetDifficultyID() const
Definition: Map.h:324
bool IsEmpty() const
Definition: ObjectGuid.h:319
std::string ToString() const
Definition: ObjectGuid.cpp:554
void Clear()
Definition: ObjectGuid.h:286
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
float GetMaxRange(bool positive=false, WorldObject *caster=nullptr, Spell *spell=nullptr) const
Definition: SpellInfo.cpp:3768
void UpdateAI(uint32 diff) override
Definition: TotemAI.cpp:41
static int32 Permissible(Creature const *creature)
Definition: TotemAI.cpp:28
TotemAI(Creature *creature, uint32 scriptId={})
Definition: TotemAI.cpp:36
void AttackStart(Unit *victim) override
Definition: TotemAI.cpp:85
ObjectGuid _victimGUID
Definition: TotemAI.h:37
uint32 GetSpell(uint8 slot=0) const
Definition: Totem.h:39
TotemType GetTotemType() const
Definition: Totem.h:42
Definition: Unit.h:627
bool isTargetableForAttack(bool checkFakeDeath=true) const
Definition: Unit.cpp:8168
Totem * ToTotem()
Definition: Unit.h:1753
bool IsNonMeleeSpellCast(bool withDelayed, bool skipChanneled=false, bool skipAutorepeat=false, bool isAutoshoot=false, bool skipInstant=true) const
Definition: Unit.cpp:3059
bool IsAlive() const
Definition: Unit.h:1164
bool IsTotem() const
Definition: Unit.h:742
Map * GetMap() const
Definition: Object.h:624
Unit * GetCharmerOrOwnerOrSelf() const
Definition: Object.cpp:2244
SpellCastResult CastSpell(CastSpellTargetArg const &targets, uint32 spellId, CastSpellExtraArgs const &args={ })
Definition: Object.cpp:2896
bool CanSeeOrDetect(WorldObject const *obj, bool implicitDetect=false, bool distanceCheck=false, bool checkAlert=false) const
Definition: Object.cpp:1514
bool IsWithinDistInMap(WorldObject const *obj, float dist2compare, bool is3D=true, bool incOwnRadius=true, bool incTargetRadius=true) const
Definition: Object.cpp:1147
bool IsFriendlyTo(WorldObject const *target) const
Definition: Object.cpp:2865
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
static void VisitAllObjects(WorldObject const *obj, T &visitor, float radius, bool dont_load=true)
Definition: CellImpl.h:203