-
Notifications
You must be signed in to change notification settings - Fork 6k
/
PlayerAI.h
125 lines (108 loc) · 5.52 KB
/
PlayerAI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (C) 2016-2017 TrinityCore <http://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRINITY_PLAYERAI_H
#define TRINITY_PLAYERAI_H
#include "UnitAI.h"
#include "Player.h"
#include "Spell.h"
#include "Creature.h"
class TC_GAME_API PlayerAI : public UnitAI
{
public:
explicit PlayerAI(Player* player) : UnitAI(static_cast<Unit*>(player)), me(player), _selfSpec(PlayerAI::GetPlayerSpec(player)), _isSelfHealer(PlayerAI::IsPlayerHealer(player)), _isSelfRangedAttacker(PlayerAI::IsPlayerRangedAttacker(player)) { }
void OnCharmed(bool /*apply*/) override { } // charm AI application for players is handled by Unit::SetCharmedBy / Unit::RemoveCharmedBy
Creature* GetCharmer() const
{
if (ObjectGuid charmerGUID = me->GetCharmerGUID())
if (charmerGUID.IsCreature())
return ObjectAccessor::GetCreature(*me, charmerGUID);
return nullptr;
}
// helper functions to determine player info
// Return values range from 0 (left-most spec) to 2 (right-most spec). If two specs have the same number of talent points, the left-most of those specs is returned.
static uint8 GetPlayerSpec(Player const* who);
// Return values range from 0 (left-most spec) to 2 (right-most spec). If two specs have the same number of talent points, the left-most of those specs is returned.
uint8 GetSpec(Player const* who = nullptr) const { return (!who || who == me) ? _selfSpec : GetPlayerSpec(who); }
static bool IsPlayerHealer(Player const* who);
bool IsHealer(Player const* who = nullptr) const { return (!who || who == me) ? _isSelfHealer : IsPlayerHealer(who); }
static bool IsPlayerRangedAttacker(Player const* who);
bool IsRangedAttacker(Player const* who = nullptr) const { return (!who || who == me) ? _isSelfRangedAttacker : IsPlayerRangedAttacker(who); }
protected:
struct TargetedSpell : public std::pair<Spell*, Unit*>
{
TargetedSpell() : pair<Spell*, Unit*>() { }
TargetedSpell(Spell* first, Unit* second) : pair<Spell*, Unit*>(first, second) { }
explicit operator bool() { return !!first; }
};
typedef std::pair<TargetedSpell, uint32> PossibleSpell;
typedef std::vector<PossibleSpell> PossibleSpellVector;
Player* const me;
void SetIsRangedAttacker(bool state) { _isSelfRangedAttacker = state; } // this allows overriding of the default ranged attacker detection
enum SpellTarget
{
TARGET_NONE,
TARGET_VICTIM,
TARGET_CHARMER,
TARGET_SELF
};
/* Check if the specified spell can be cast on that target.
Caller is responsible for cleaning up created Spell object from pointer. */
TargetedSpell VerifySpellCast(uint32 spellId, Unit* target);
/* Check if the specified spell can be cast on that target.
Caller is responsible for cleaning up created Spell object from pointer. */
TargetedSpell VerifySpellCast(uint32 spellId, SpellTarget target);
/* Helper method - checks spell cast, then pushes it onto provided vector if valid. */
template<typename T> inline void VerifyAndPushSpellCast(PossibleSpellVector& spells, uint32 spellId, T target, uint32 weight)
{
if (TargetedSpell spell = VerifySpellCast(spellId, target))
spells.push_back({ spell,weight });
}
/* Helper method - selects one spell from the vector and returns it, while deleting everything else.
This invalidates the vector, and empties it to prevent accidental misuse. */
TargetedSpell SelectSpellCast(PossibleSpellVector& spells);
/* Helper method - casts the included spell at the included target */
inline void DoCastAtTarget(TargetedSpell spell)
{
SpellCastTargets targets;
targets.SetUnitTarget(spell.second);
spell.first->prepare(&targets);
}
virtual Unit* SelectAttackTarget() const { return me->GetCharmer() ? me->GetCharmer()->GetVictim() : nullptr; }
void DoRangedAttackIfReady();
void DoAutoAttackIfReady();
// Cancels all shapeshifts that the player could voluntarily cancel
void CancelAllShapeshifts();
private:
uint8 const _selfSpec;
bool const _isSelfHealer;
bool _isSelfRangedAttacker;
};
class TC_GAME_API SimpleCharmedPlayerAI : public PlayerAI
{
public:
SimpleCharmedPlayerAI(Player* player) : PlayerAI(player), _castCheckTimer(500), _chaseCloser(false), _forceFacing(true) { }
void UpdateAI(uint32 diff) override;
void OnCharmed(bool apply) override;
protected:
Unit* SelectAttackTarget() const override;
private:
TargetedSpell SelectAppropriateCastForSpec();
uint32 _castCheckTimer;
bool _chaseCloser;
bool _forceFacing;
};
#endif