-
Notifications
You must be signed in to change notification settings - Fork 294
/
PetAI.cpp
366 lines (308 loc) · 12.9 KB
/
PetAI.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "PetAI.h"
#include "Errors.h"
#include "Pet.h"
#include "Player.h"
#include "DBCStores.h"
#include "Spell.h"
#include "SpellMgr.h"
#include "Creature.h"
#include "World.h"
#include "Util.h"
int PetAI::Permissible(const Creature* creature)
{
if (creature->IsPet())
return PERMIT_BASE_SPECIAL;
return PERMIT_BASE_NO;
}
PetAI::PetAI(Creature* c) : CreatureAI(c), i_tracker(TIME_INTERVAL_LOOK), inCombat(false)
{
m_AllySet.clear();
UpdateAllies();
}
void PetAI::MoveInLineOfSight(Unit* u)
{
if (m_creature->getVictim())
return;
if (m_creature->IsPet() && ((Pet*)m_creature)->GetModeFlags() & PET_MODE_DISABLE_ACTIONS)
return;
if (!m_creature->GetCharmInfo() || !m_creature->GetCharmInfo()->HasReactState(REACT_AGGRESSIVE))
return;
if (u->isTargetableForAttack() && u->isInAccessablePlaceFor(m_creature) &&
(m_creature->IsHostileTo(u) || u->IsHostileTo(m_creature->GetCharmerOrOwner())))
{
float attackRadius = m_creature->GetAttackDistance(u);
if (m_creature->IsWithinDistInMap(u, attackRadius) && m_creature->GetDistanceZ(u) <= CREATURE_Z_ATTACK_RANGE)
{
if (m_creature->IsWithinLOSInMap(u))
{
AttackStart(u);
u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
}
}
}
}
void PetAI::AttackStart(Unit* u)
{
if (!u || ((Pet*)m_creature)->getPetType() == MINI_PET)
return;
if (m_creature->Attack(u, true))
{
// TMGs call CreatureRelocation which via MoveInLineOfSight can call this function
// thus with the following clear the original TMG gets invalidated and crash, doh
// hope it doesn't start to leak memory without this :-/
// i_pet->Clear();
HandleMovementOnAttackStart(u);
inCombat = true;
}
}
void PetAI::EnterEvadeMode()
{
}
bool PetAI::IsVisible(Unit* pl) const
{
return _isVisible(pl);
}
bool PetAI::_needToStop() const
{
// This is needed for charmed creatures, as once their target was reset other effects can trigger threat
if (m_creature->isCharmed() && m_creature->getVictim() == m_creature->GetCharmer())
return true;
return !m_creature->getVictim()->isTargetableForAttack();
}
void PetAI::_stopAttack()
{
inCombat = false;
bool useDefaultMovement = true;
if (Unit* owner = m_creature->GetCharmerOrOwner())
{
if (CharmInfo* charmInfo = m_creature->GetCharmInfo())
{
if (Pet* pet = (Pet*)m_creature)
{
if (charmInfo->HasCommandState(COMMAND_FOLLOW))
pet->GetMotionMaster()->MoveFollow(owner, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
else if (charmInfo->HasCommandState(COMMAND_STAY))
{
//if stay command is already set but we dont have stay pos set then we need to establish current pos as stay position
if (!pet->IsStayPosSet())
pet->SetStayPosition();
pet->GetMotionMaster()->MovePoint(0, pet->GetStayPosX(), pet->GetStayPosY(), pet->GetStayPosZ(), false);
}
useDefaultMovement = false;
}
}
}
m_creature->AttackStop();
if (useDefaultMovement)
{
m_creature->GetMotionMaster()->Clear(false);
m_creature->GetMotionMaster()->MoveIdle();
}
}
void PetAI::UpdateAI(const uint32 diff)
{
if (!m_creature->isAlive())
return;
Unit* owner = m_creature->GetCharmerOrOwner();
Unit* victim = m_creature->getVictim();
if (m_updateAlliesTimer <= diff)
// UpdateAllies self set update timer
UpdateAllies();
else
m_updateAlliesTimer -= diff;
if (inCombat && (!m_creature->getVictim() || (m_creature->IsPet() && ((Pet*)m_creature)->GetModeFlags() & PET_MODE_DISABLE_ACTIONS)))
_stopAttack();
// Autocast (casted only in combat or persistent spells in any state)
if (!m_creature->IsNonMeleeSpellCasted(false))
{
typedef std::vector<std::pair<Unit*, Spell*> > TargetSpellList;
TargetSpellList targetSpellStore;
for (uint8 i = 0; i < m_creature->GetPetAutoSpellSize(); ++i)
{
uint32 spellID = m_creature->GetPetAutoSpellOnPos(i);
if (!spellID)
continue;
SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellID);
if (!spellInfo)
continue;
if (m_creature->GetCharmInfo() && m_creature->GetCharmInfo()->GetGlobalCooldownMgr().HasGlobalCooldown(spellInfo))
continue;
// ignore some combinations of combat state and combat/noncombat spells
if (!inCombat)
{
// ignore attacking spells, and allow only self/around spells
if (!IsPositiveSpell(spellInfo->Id))
continue;
// non combat spells allowed
// only pet spells have IsNonCombatSpell and not fit this reqs:
// Consume Shadows, Lesser Invisibility, so ignore checks for its
if (!IsNonCombatSpell(spellInfo))
{
int32 duration = GetSpellDuration(spellInfo);
int32 cooldown = GetSpellRecoveryTime(spellInfo);
// allow only spell not on cooldown
if (cooldown != 0 && duration < cooldown)
continue;
}
}
// just ignore non-combat spells
else if (IsNonCombatSpell(spellInfo))
continue;
Spell* spell = new Spell(m_creature, spellInfo, false);
if (inCombat && !m_creature->hasUnitState(UNIT_STAT_FOLLOW) && spell->CanAutoCast(m_creature->getVictim()))
{
targetSpellStore.push_back(TargetSpellList::value_type(m_creature->getVictim(), spell));
continue;
}
else
{
bool spellUsed = false;
for (GuidSet::const_iterator tar = m_AllySet.begin(); tar != m_AllySet.end(); ++tar)
{
Unit* Target = m_creature->GetMap()->GetUnit(*tar);
// only buff targets that are in combat, unless the spell can only be cast while out of combat
if (!Target)
continue;
if (spell->CanAutoCast(Target))
{
targetSpellStore.push_back(TargetSpellList::value_type(Target, spell));
spellUsed = true;
break;
}
}
if (!spellUsed)
delete spell;
}
}
// found units to cast on to
if (!targetSpellStore.empty())
{
uint32 index = urand(0, targetSpellStore.size() - 1);
Spell* spell = targetSpellStore[index].second;
Unit* target = targetSpellStore[index].first;
targetSpellStore.erase(targetSpellStore.begin() + index);
SpellCastTargets targets;
targets.setUnitTarget(target);
if (!m_creature->HasInArc(M_PI_F, target))
{
m_creature->SetInFront(target);
if (target->GetTypeId() == TYPEID_PLAYER)
m_creature->SendCreateUpdateToPlayer((Player*)target);
if (owner && owner->GetTypeId() == TYPEID_PLAYER)
m_creature->SendCreateUpdateToPlayer((Player*)owner);
}
m_creature->AddCreatureSpellCooldown(spell->m_spellInfo->Id);
spell->SpellStart(&targets);
}
// deleted cached Spell objects
for (TargetSpellList::const_iterator itr = targetSpellStore.begin(); itr != targetSpellStore.end(); ++itr)
delete itr->second;
}
else if (m_creature->hasUnitState(UNIT_STAT_FOLLOW_MOVE))
m_creature->InterruptNonMeleeSpells(false);
if (victim = m_creature->getVictim())
{
// i_pet.getVictim() can't be used for check in case stop fighting, i_pet.getVictim() clear at Unit death etc.
if (_needToStop())
{
DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "PetAI (guid = %u) is stopping attack.", m_creature->GetGUIDLow());
_stopAttack();
return;
}
// required to be stopped cases
if (m_creature->IsStopped() && m_creature->IsNonMeleeSpellCasted(false))
{
if (m_creature->hasUnitState(UNIT_STAT_FOLLOW_MOVE))
m_creature->InterruptNonMeleeSpells(false);
}
else if (m_creature->CanReachWithMeleeAttack(victim))
{
if (DoMeleeAttackIfReady())
// if pet misses its target, it will also be the first in threat list
victim->AddThreat(m_creature);
else
AttackStart(victim);
}
}
else if (owner && m_creature->GetCharmInfo())
{
if (owner->isInCombat() && !(m_creature->GetCharmInfo()->HasReactState(REACT_PASSIVE) || m_creature->GetCharmInfo()->HasCommandState(COMMAND_STAY)))
{
AttackStart(owner->getAttackerForHelper());
}
else if (m_creature->GetCharmInfo()->HasCommandState(COMMAND_FOLLOW))
{
// The distance is to prevent the pet from running around to reach the owners back when walking towards it
// and the reason for increasing it more than the follow distance is to prevent the same thing
// from happening when the owner turns and twists (as this increases the distance between them)
if (!m_creature->hasUnitState(UNIT_STAT_FOLLOW) && !owner->IsWithinDistInMap(m_creature, (PET_FOLLOW_DIST * 2)))
m_creature->GetMotionMaster()->MoveFollow(owner, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
// This is to stop the pet from following you when you're close to each other, to support the above condition.
else if (m_creature->hasUnitState(UNIT_STAT_FOLLOW))
{
m_creature->GetMotionMaster()->Clear(false);
m_creature->GetMotionMaster()->MoveIdle();
}
}
}
}
bool PetAI::_isVisible(Unit* u) const
{
return m_creature->IsWithinDist(u, sWorld.getConfig(CONFIG_FLOAT_SIGHT_GUARDER))
&& u->isVisibleForOrDetect(m_creature, m_creature, true);
}
void PetAI::UpdateAllies()
{
Unit* owner = m_creature->GetCharmerOrOwner();
Group* pGroup = nullptr;
m_updateAlliesTimer = 10 * IN_MILLISECONDS; // update friendly targets every 10 seconds, lesser checks increase performance
if (!owner)
return;
else if (owner->GetTypeId() == TYPEID_PLAYER)
pGroup = ((Player*)owner)->GetGroup();
// only pet and owner/not in group->ok
if (m_AllySet.size() == 2 && !pGroup)
return;
// owner is in group; group members filled in already (no raid -> subgroupcount = whole count)
if (pGroup && !pGroup->isRaidGroup() && m_AllySet.size() == (pGroup->GetMembersCount() + 2))
return;
m_AllySet.clear();
m_AllySet.insert(m_creature->GetObjectGuid());
if (pGroup) // add group
{
for (GroupReference* itr = pGroup->GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* target = itr->getSource();
if (!target || !pGroup->SameSubGroup((Player*)owner, target))
continue;
if (target->GetObjectGuid() == owner->GetObjectGuid())
continue;
m_AllySet.insert(target->GetObjectGuid());
}
}
else // remove group
m_AllySet.insert(owner->GetObjectGuid());
}
void PetAI::AttackedBy(Unit* attacker)
{
// when attacked, fight back in case 1)no victim already AND 2)not set to passive AND 3)not set to stay, unless can it can reach attacker with melee attack anyway
if (!m_creature->getVictim() && m_creature->GetCharmInfo() && !m_creature->GetCharmInfo()->HasReactState(REACT_PASSIVE))
AttackStart(attacker);
}