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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/*
* This file is part of the TrinityCore 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, see <http://www.gnu.org/licenses/>.
*/
#include "UnitAI.h"
#include "Containers.h"
#include "Creature.h"
#include "CreatureAIImpl.h"
#include "Map.h"
#include "MotionMaster.h"
#include "Spell.h"
#include "SpellInfo.h"
#include "SpellMgr.h"
#include <sstream>
void UnitAI::AttackStart(Unit* victim)
{
if (victim && me->Attack(victim, true))
{
// Clear distracted state on attacking
if (me->HasUnitState(UNIT_STATE_DISTRACTED))
{
me->ClearUnitState(UNIT_STATE_DISTRACTED);
me->GetMotionMaster()->Clear();
}
me->GetMotionMaster()->MoveChase(victim);
}
}
void UnitAI::InitializeAI()
{
if (!me->isDead())
Reset();
}
void UnitAI::OnCharmed(bool isNew)
{
if (!isNew)
me->ScheduleAIChange();
}
void UnitAI::AttackStartCaster(Unit* victim, float dist)
{
if (victim && me->Attack(victim, false))
me->GetMotionMaster()->MoveChase(victim, dist);
}
bool UnitAI::DoSpellAttackIfReady(uint32 spellId)
{
if (me->HasUnitState(UNIT_STATE_CASTING) || !me->isAttackReady())
return true;
if (SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spellId, me->GetMap()->GetDifficultyID()))
{
if (me->IsWithinCombatRange(me->GetVictim(), spellInfo->GetMaxRange(false)))
{
me->CastSpell(me->GetVictim(), spellId, me->GetMap()->GetDifficultyID());
me->resetAttackTimer();
return true;
}
}
return false;
}
Unit* UnitAI::SelectTarget(SelectTargetMethod targetType, uint32 position, float dist, bool playerOnly, bool withTank, int32 aura)
{
return SelectTarget(targetType, position, DefaultTargetSelector(me, dist, playerOnly, withTank, aura));
}
void UnitAI::SelectTargetList(std::list<Unit*>& targetList, uint32 num, SelectTargetMethod targetType, uint32 offset, float dist, bool playerOnly, bool withTank, int32 aura)
{
SelectTargetList(targetList, num, targetType, offset, DefaultTargetSelector(me, dist, playerOnly, withTank, aura));
}
SpellCastResult UnitAI::DoCast(uint32 spellId)
{
Unit* target = nullptr;
AITarget aiTargetType = AITARGET_SELF;
if (AISpellInfoType const* info = GetAISpellInfo(spellId, me->GetMap()->GetDifficultyID()))
aiTargetType = info->target;
switch (aiTargetType)
{
default:
case AITARGET_SELF:
target = me;
break;
case AITARGET_VICTIM:
target = me->GetVictim();
break;
case AITARGET_ENEMY:
{
if (SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spellId, me->GetMap()->GetDifficultyID()))
{
DefaultTargetSelector targetSelectorInner(me, spellInfo->GetMaxRange(false), false, true, 0);
auto targetSelector = [&](Unit const* candidate) -> bool
{
if (!candidate->IsPlayer())
{
if (spellInfo->HasAttribute(SPELL_ATTR3_ONLY_ON_PLAYER))
return false;
if (spellInfo->HasAttribute(SPELL_ATTR5_NOT_ON_PLAYER_CONTROLLED_NPC) && candidate->IsControlledByPlayer())
return false;
}
else if (spellInfo->HasAttribute(SPELL_ATTR5_NOT_ON_PLAYER))
return false;
return targetSelectorInner(candidate);
};
target = SelectTarget(SelectTargetMethod::Random, 0, targetSelector);
}
break;
}
case AITARGET_ALLY:
target = me;
break;
case AITARGET_BUFF:
target = me;
break;
case AITARGET_DEBUFF:
{
if (SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spellId, me->GetMap()->GetDifficultyID()))
{
float range = spellInfo->GetMaxRange(false);
DefaultTargetSelector targetSelectorInner(me, range, false, true, -(int32)spellId);
auto targetSelector = [&](Unit const* candidate) -> bool
{
if (!candidate->IsPlayer())
{
if (spellInfo->HasAttribute(SPELL_ATTR3_ONLY_ON_PLAYER))
return false;
if (spellInfo->HasAttribute(SPELL_ATTR5_NOT_ON_PLAYER_CONTROLLED_NPC) && candidate->IsControlledByPlayer())
return false;
}
else if (spellInfo->HasAttribute(SPELL_ATTR5_NOT_ON_PLAYER))
return false;
return targetSelectorInner(candidate);
};
if (!spellInfo->HasAuraInterruptFlag(SpellAuraInterruptFlags::NOT_VICTIM) && targetSelector(me->GetVictim()))
target = me->GetVictim();
else
target = SelectTarget(SelectTargetMethod::Random, 0, targetSelector);
}
break;
}
}
if (target)
return me->CastSpell(target, spellId, false);
return SPELL_FAILED_BAD_TARGETS;
}
SpellCastResult UnitAI::DoCast(Unit* victim, uint32 spellId, CastSpellExtraArgs const& args)
{
if (me->HasUnitState(UNIT_STATE_CASTING) && !(args.TriggerFlags & TRIGGERED_IGNORE_CAST_IN_PROGRESS))
return SPELL_FAILED_SPELL_IN_PROGRESS;
return me->CastSpell(victim, spellId, args);
}
SpellCastResult UnitAI::DoCastVictim(uint32 spellId, CastSpellExtraArgs const& args)
{
if (Unit* victim = me->GetVictim())
return DoCast(victim, spellId, args);
return SPELL_FAILED_BAD_TARGETS;
}
#define UPDATE_TARGET(a) {if (AIInfo->target<a) AIInfo->target=a;}
void UnitAI::FillAISpellInfo()
{
sSpellMgr->ForEachSpellInfo([](SpellInfo const* spellInfo)
{
AISpellInfoType* AIInfo = &AISpellInfo[{ spellInfo->Id, spellInfo->Difficulty }];
if (spellInfo->HasAttribute(SPELL_ATTR0_ALLOW_CAST_WHILE_DEAD))
AIInfo->condition = AICOND_DIE;
else if (spellInfo->IsPassive() || spellInfo->GetDuration() == -1)
AIInfo->condition = AICOND_AGGRO;
else
AIInfo->condition = AICOND_COMBAT;
if (AIInfo->cooldown.count() < int32(spellInfo->RecoveryTime))
AIInfo->cooldown = Milliseconds(spellInfo->RecoveryTime);
if (spellInfo->GetMaxRange(false))
{
for (SpellEffectInfo const& effect : spellInfo->GetEffects())
{
uint32 targetType = effect.TargetA.GetTarget();
if (targetType == TARGET_UNIT_TARGET_ENEMY
|| targetType == TARGET_DEST_TARGET_ENEMY)
UPDATE_TARGET(AITARGET_VICTIM)
else if (targetType == TARGET_UNIT_DEST_AREA_ENEMY)
UPDATE_TARGET(AITARGET_ENEMY)
if (effect.Effect == SPELL_EFFECT_APPLY_AURA)
{
if (targetType == TARGET_UNIT_TARGET_ENEMY)
UPDATE_TARGET(AITARGET_DEBUFF)
else if (spellInfo->IsPositive())
UPDATE_TARGET(AITARGET_BUFF)
}
}
}
AIInfo->realCooldown = Milliseconds(spellInfo->RecoveryTime + spellInfo->StartRecoveryTime);
AIInfo->maxRange = spellInfo->GetMaxRange(false) * 3 / 4;
AIInfo->Effects = 0;
AIInfo->Targets = 0;
for (SpellEffectInfo const& spellEffectInfo : spellInfo->GetEffects())
{
// Spell targets self.
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_CASTER)
AIInfo->Targets |= 1 << (SELECT_TARGET_SELF - 1);
// Spell targets a single enemy.
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_TARGET_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_DEST_TARGET_ENEMY)
AIInfo->Targets |= 1 << (SELECT_TARGET_SINGLE_ENEMY - 1);
// Spell targets AoE at enemy.
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_SRC_AREA_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_DEST_AREA_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_SRC_CASTER ||
spellEffectInfo.TargetA.GetTarget() == TARGET_DEST_DYNOBJ_ENEMY)
AIInfo->Targets |= 1 << (SELECT_TARGET_AOE_ENEMY - 1);
// Spell targets an enemy.
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_TARGET_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_DEST_TARGET_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_SRC_AREA_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_DEST_AREA_ENEMY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_SRC_CASTER ||
spellEffectInfo.TargetA.GetTarget() == TARGET_DEST_DYNOBJ_ENEMY)
AIInfo->Targets |= 1 << (SELECT_TARGET_ANY_ENEMY - 1);
// Spell targets a single friend (or self).
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_CASTER ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_TARGET_ALLY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_TARGET_PARTY)
AIInfo->Targets |= 1 << (SELECT_TARGET_SINGLE_FRIEND - 1);
// Spell targets AoE friends.
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_CASTER_AREA_PARTY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_LASTTARGET_AREA_PARTY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_SRC_CASTER)
AIInfo->Targets |= 1 << (SELECT_TARGET_AOE_FRIEND - 1);
// Spell targets any friend (or self).
if (spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_CASTER ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_TARGET_ALLY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_TARGET_PARTY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_CASTER_AREA_PARTY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_UNIT_LASTTARGET_AREA_PARTY ||
spellEffectInfo.TargetA.GetTarget() == TARGET_SRC_CASTER)
AIInfo->Targets |= 1 << (SELECT_TARGET_ANY_FRIEND - 1);
// Make sure that this spell includes a damage effect.
if (spellEffectInfo.Effect == SPELL_EFFECT_SCHOOL_DAMAGE ||
spellEffectInfo.Effect == SPELL_EFFECT_INSTAKILL ||
spellEffectInfo.Effect == SPELL_EFFECT_ENVIRONMENTAL_DAMAGE ||
spellEffectInfo.Effect == SPELL_EFFECT_HEALTH_LEECH)
AIInfo->Effects |= 1 << (SELECT_EFFECT_DAMAGE - 1);
// Make sure that this spell includes a healing effect (or an apply aura with a periodic heal).
if (spellEffectInfo.Effect == SPELL_EFFECT_HEAL ||
spellEffectInfo.Effect == SPELL_EFFECT_HEAL_MAX_HEALTH ||
spellEffectInfo.Effect == SPELL_EFFECT_HEAL_MECHANICAL ||
(spellEffectInfo.Effect == SPELL_EFFECT_APPLY_AURA && spellEffectInfo.ApplyAuraName == 8))
AIInfo->Effects |= 1 << (SELECT_EFFECT_HEALING - 1);
// Make sure that this spell applies an aura.
if (spellEffectInfo.Effect == SPELL_EFFECT_APPLY_AURA)
AIInfo->Effects |= 1 << (SELECT_EFFECT_AURA - 1);
}
});
}
Unit* UnitAI::FinalizeTargetSelection(std::list<Unit*>& targetList, SelectTargetMethod targetType)
{
// maybe nothing fulfills the predicate
if (targetList.empty())
return nullptr;
switch (targetType)
{
case SelectTargetMethod::MaxThreat:
case SelectTargetMethod::MinThreat:
case SelectTargetMethod::MaxDistance:
case SelectTargetMethod::MinDistance:
return targetList.front();
case SelectTargetMethod::Random:
return Trinity::Containers::SelectRandomContainerElement(targetList);
default:
break;
}
return nullptr;
}
bool UnitAI::PrepareTargetListSelection(std::list<Unit*>& targetList, SelectTargetMethod targetType, uint32 offset)
{
targetList.clear();
ThreatManager& mgr = me->GetThreatManager();
// shortcut: we're gonna ignore the first <offset> elements, and there's at most <offset> elements, so we ignore them all - nothing to do here
if (mgr.GetThreatListSize() <= offset)
return false;
if (targetType == SelectTargetMethod::MaxDistance || targetType == SelectTargetMethod::MinDistance)
{
for (ThreatReference const* ref : mgr.GetUnsortedThreatList())
{
if (ref->IsOffline())
continue;
targetList.push_back(ref->GetVictim());
}
}
else
{
Unit* currentVictim = mgr.GetCurrentVictim();
if (currentVictim)
targetList.push_back(currentVictim);
for (ThreatReference const* ref : mgr.GetSortedThreatList())
{
if (ref->IsOffline())
continue;
Unit* thisTarget = ref->GetVictim();
if (thisTarget != currentVictim)
targetList.push_back(thisTarget);
}
}
// shortcut: the list isn't gonna get any larger
if (targetList.size() <= offset)
{
targetList.clear();
return false;
}
// right now, list is unsorted for DISTANCE types - re-sort by SelectTargetMethod::MaxDistance
if (targetType == SelectTargetMethod::MaxDistance || targetType == SelectTargetMethod::MinDistance)
targetList.sort(Trinity::ObjectDistanceOrderPred(me, targetType == SelectTargetMethod::MinDistance));
// now the list is MAX sorted, reverse for MIN types
if (targetType == SelectTargetMethod::MinThreat)
targetList.reverse();
// ignore the first <offset> elements
while (offset)
{
targetList.pop_front();
--offset;
}
return true;
}
void UnitAI::FinalizeTargetListSelection(std::list<Unit*>& targetList, uint32 num, SelectTargetMethod targetType)
{
if (targetList.size() <= num)
return;
if (targetType == SelectTargetMethod::Random)
Trinity::Containers::RandomResize(targetList, num);
else
targetList.resize(num);
}
std::string UnitAI::GetDebugInfo() const
{
std::stringstream sstr;
sstr << std::boolalpha
<< "Me: " << (me ? me->GetDebugInfo() : "NULL");
return sstr.str();
}
|