TrinityCore
PlayerAI.h
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#ifndef TRINITY_PLAYERAI_H
19#define TRINITY_PLAYERAI_H
20
21#include "UnitAI.h"
22
23class Creature;
24class Spell;
25enum class ChrSpecialization : uint32;
26
28{
29 public:
30 explicit PlayerAI(Player* player);
31
32 Creature* GetCharmer() const;
33
34 // helper functions to determine player info
35 ChrSpecialization GetSpec(Player const* who = nullptr) const;
36 static bool IsPlayerHealer(Player const* who);
37 bool IsHealer(Player const* who = nullptr) const { return (!who || who == me) ? _isSelfHealer : IsPlayerHealer(who); }
38 static bool IsPlayerRangedAttacker(Player const* who);
39 bool IsRangedAttacker(Player const* who = nullptr) const { return (!who || who == me) ? _isSelfRangedAttacker : IsPlayerRangedAttacker(who); }
40
41 protected:
42 struct TargetedSpell : public std::pair<Spell*, Unit*>
43 {
44 TargetedSpell() : pair<Spell*, Unit*>() { }
45 TargetedSpell(Spell* first, Unit* second) : pair<Spell*, Unit*>(first, second) { }
46 explicit operator bool() { return !!first; }
47 };
48 typedef std::pair<TargetedSpell, uint32> PossibleSpell;
49 typedef std::vector<PossibleSpell> PossibleSpellVector;
50
51 Player* const me;
52 void SetIsRangedAttacker(bool state) { _isSelfRangedAttacker = state; } // this allows overriding of the default ranged attacker detection
53
55 {
59 TARGET_SELF
60 };
61 /* Check if the specified spell can be cast on that target.
62 Caller is responsible for cleaning up created Spell object from pointer. */
63 TargetedSpell VerifySpellCast(uint32 spellId, Unit* target);
64 /* Check if the specified spell can be cast on that target.
65 Caller is responsible for cleaning up created Spell object from pointer. */
66 TargetedSpell VerifySpellCast(uint32 spellId, SpellTarget target);
67
68 /* Helper method - checks spell cast, then pushes it onto provided vector if valid. */
69 template<typename T> inline void VerifyAndPushSpellCast(PossibleSpellVector& spells, uint32 spellId, T target, uint32 weight)
70 {
71 if (TargetedSpell spell = VerifySpellCast(spellId, target))
72 spells.push_back({ spell,weight });
73 }
74
75 /* Helper method - selects one spell from the vector and returns it, while deleting everything else.
76 This invalidates the vector, and empties it to prevent accidental misuse. */
77 TargetedSpell SelectSpellCast(PossibleSpellVector& spells);
78 /* Helper method - casts the included spell at the included target */
79 void DoCastAtTarget(TargetedSpell spell);
80
81 virtual Unit* SelectAttackTarget() const;
82 void DoRangedAttackIfReady();
83 void DoAutoAttackIfReady();
84
85 // Cancels all shapeshifts that the player could voluntarily cancel
86 void CancelAllShapeshifts();
87
88 private:
90 bool const _isSelfHealer;
92};
93
95{
96 public:
97 SimpleCharmedPlayerAI(Player* player) : PlayerAI(player), _castCheckTimer(2500), _chaseCloser(false), _forceFacing(true), _isFollowing(false) { }
98 void UpdateAI(uint32 diff) override;
99 void OnCharmed(bool isNew) override;
100
101 protected:
102 bool CanAIAttack(Unit const* who) const override;
103 Unit* SelectAttackTarget() const override;
104
105 private:
106 TargetedSpell SelectAppropriateCastForSpec();
111};
112
113#endif
ChrSpecialization
Definition: DBCEnums.h:357
#define TC_GAME_API
Definition: Define.h:123
uint32_t uint32
Definition: Define.h:142
void VerifyAndPushSpellCast(PossibleSpellVector &spells, uint32 spellId, T target, uint32 weight)
Definition: PlayerAI.h:69
std::pair< TargetedSpell, uint32 > PossibleSpell
Definition: PlayerAI.h:48
Player *const me
Definition: PlayerAI.h:51
void SetIsRangedAttacker(bool state)
Definition: PlayerAI.h:52
SpellTarget
Definition: PlayerAI.h:55
@ TARGET_VICTIM
Definition: PlayerAI.h:57
@ TARGET_NONE
Definition: PlayerAI.h:56
@ TARGET_CHARMER
Definition: PlayerAI.h:58
bool const _isSelfHealer
Definition: PlayerAI.h:90
bool _isSelfRangedAttacker
Definition: PlayerAI.h:91
ChrSpecialization const _selfSpec
Definition: PlayerAI.h:89
bool IsRangedAttacker(Player const *who=nullptr) const
Definition: PlayerAI.h:39
std::vector< PossibleSpell > PossibleSpellVector
Definition: PlayerAI.h:49
bool IsHealer(Player const *who=nullptr) const
Definition: PlayerAI.h:37
SimpleCharmedPlayerAI(Player *player)
Definition: PlayerAI.h:97
Definition: Spell.h:255
Definition: UnitAI.h:50
virtual bool CanAIAttack(Unit const *) const
Definition: UnitAI.h:57
virtual void OnCharmed(bool isNew)
Definition: UnitAI.cpp:49
virtual void UpdateAI(uint32 diff)=0
Definition: Unit.h:627
TargetedSpell(Spell *first, Unit *second)
Definition: PlayerAI.h:45