Files
TCPlayerbotCore/src/game/CreatureAI.cpp
T

133 lines
4.0 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
2008-10-14 11:57:03 -05:00
*
* Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-10-02 16:23:55 -05:00
* 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
2008-10-02 16:23:55 -05:00
*/
#include "CreatureAI.h"
2009-01-08 21:48:38 -06:00
#include "Creature.h"
2009-01-30 18:10:05 -06:00
#include "Player.h"
2009-01-08 21:48:38 -06:00
#include "Pet.h"
#include "SpellAuras.h"
2008-10-02 16:23:55 -05:00
2009-01-30 18:10:05 -06:00
void UnitAI::AttackStart(Unit *victim)
2008-10-02 16:23:55 -05:00
{
2009-01-30 18:10:05 -06:00
if(!victim)
return;
if(me->Attack(victim, true))
{
//DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", me->GetName(), victim->GetGUIDLow());
me->GetMotionMaster()->MoveChase(victim);
}
2008-10-02 16:23:55 -05:00
}
2009-01-08 21:48:38 -06:00
void UnitAI::DoMeleeAttackIfReady()
{
//Make sure our attack is ready and we aren't currently casting before checking distance
if (me->isAttackReady() && !me->hasUnitState(UNIT_STAT_CASTING))
{
//If we are within range melee the target
if (me->IsWithinMeleeRange(me->getVictim()))
{
me->AttackerStateUpdate(me->getVictim());
me->resetAttackTimer();
}
}
if (me->haveOffhandWeapon() && me->isAttackReady(OFF_ATTACK) && !me->hasUnitState(UNIT_STAT_CASTING))
{
//If we are within range melee the target
if (me->IsWithinMeleeRange(me->getVictim()))
{
me->AttackerStateUpdate(me->getVictim(), OFF_ATTACK);
me->resetAttackTimer(OFF_ATTACK);
}
}
}
2009-03-05 18:48:56 -06:00
//Enable PlayerAI when charmed
void PlayerAI::OnCharmed(bool apply) { me->IsAIEnabled = apply; }
//Disable CreatureAI when charmed
void CreatureAI::OnCharmed(bool apply) { /*me->IsAIEnabled = !apply;*/ }
2009-03-05 18:48:56 -06:00
2009-01-30 18:10:05 -06:00
void CreatureAI::MoveInLineOfSight(Unit *who)
2009-01-08 21:48:38 -06:00
{
2009-01-30 18:10:05 -06:00
if(!me->getVictim() && me->canStartAttack(who))
AttackStart(who);
2009-02-09 08:06:16 -05:00
if (who->isInCombat() && who->getVictim() && me->GetCreatureType()==CREATURE_TYPE_HUMANOID && me->IsFriendlyTo(who))
{
if (me->GetDistanceZ(who) <= 2 && me->IsWithinLOSInMap(who))
{
float attackRadius = (me->GetAttackDistance(who) *0.5);
if (me->IsWithinDistInMap(who, attackRadius))
{
Unit* target = NULL;
target = who->getVictim();
AttackStart(target);
}
}
}
2009-01-30 18:10:05 -06:00
}
2009-01-28 20:16:11 -06:00
2009-01-30 18:10:05 -06:00
bool CreatureAI::UpdateVictim()
{
if(!me->isInCombat())
return false;
if(Unit *victim = me->SelectVictim())
AttackStart(victim);
return me->getVictim();
}
void CreatureAI::EnterEvadeMode()
{
2009-01-28 20:16:11 -06:00
me->RemoveAllAuras();
me->DeleteThreatList();
me->CombatStop();
me->LoadCreaturesAddon();
me->SetLootRecipient(NULL);
if(me->isAlive())
me->GetMotionMaster()->MoveTargetedHome();
2009-01-08 21:48:38 -06:00
}
void SimpleCharmedAI::UpdateAI(const uint32 /*diff*/)
{
2009-01-28 20:16:11 -06:00
Creature *charmer = (Creature*)me->GetCharmer();
2009-01-08 21:48:38 -06:00
//kill self if charm aura has infinite duration
if(charmer->IsInEvadeMode())
{
2009-01-28 20:16:11 -06:00
Unit::AuraList const& auras = me->GetAurasByType(SPELL_AURA_MOD_CHARM);
2009-01-08 21:48:38 -06:00
for(Unit::AuraList::const_iterator iter = auras.begin(); iter != auras.end(); ++iter)
if((*iter)->GetCasterGUID() == charmer->GetGUID() && (*iter)->IsPermanent())
{
2009-01-28 20:16:11 -06:00
charmer->Kill(me);
2009-01-08 21:48:38 -06:00
return;
}
}
if(!charmer->isInCombat())
2009-01-28 20:16:11 -06:00
me->GetMotionMaster()->MoveFollow(charmer, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
2009-01-08 21:48:38 -06:00
2009-01-28 20:16:11 -06:00
Unit *target = me->getVictim();
2009-01-08 21:48:38 -06:00
if(!target || !charmer->canAttack(target))
2009-01-30 18:10:05 -06:00
AttackStart(charmer->SelectNearestTarget());
2009-01-08 21:48:38 -06:00
}
2009-02-17 20:07:49 -06:00