Files
TCPlayerbotCore/src/server/game/AI/ScriptedAI/ScriptedFollowerAI.cpp
T

389 lines
11 KiB
C++
Raw Normal View History

2009-08-16 18:34:45 +02:00
/* Copyright (C) 2006 - 2009 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
* This program is free software licensed under GPL version 2
* Please see the included DOCS/LICENSE.TXT for more information */
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
/* ScriptData
SDName: FollowerAI
SD%Complete: 50
SDComment: This AI is under development
SDCategory: Npc
EndScriptData */
2009-10-17 15:51:44 -07:00
#include "ScriptPCH.h"
2010-01-19 11:36:05 +01:00
#include "ScriptedFollowerAI.h"
#include "Group.h"
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
const float MAX_PLAYER_DISTANCE = 100.0f;
2009-10-17 15:51:44 -07:00
2009-10-17 07:42:48 -07:00
enum ePoints
2009-08-16 18:34:45 +02:00
{
POINT_COMBAT_START = 0xFFFFFF
};
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
FollowerAI::FollowerAI(Creature* pCreature) : ScriptedAI(pCreature),
m_uiLeaderGUID(0),
m_uiUpdateFollowTimer(2500),
2010-08-21 20:08:47 +02:00
m_uiFollowState(STATE_FOLLOW_NONE),
m_pQuestForFollow(NULL)
2009-08-16 18:34:45 +02:00
{}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::AttackStart(Unit* pWho)
{
if (!pWho)
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->Attack(pWho, true))
2009-08-16 18:34:45 +02:00
{
2010-04-14 23:07:41 +02:00
me->AddThreat(pWho, 0.0f);
me->SetInCombatWith(pWho);
pWho->SetInCombatWith(me);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->hasUnitState(UNIT_STAT_FOLLOW))
me->clearUnitState(UNIT_STAT_FOLLOW);
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (IsCombatMovement())
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->MoveChase(pWho);
2009-08-16 18:34:45 +02:00
}
}
2009-10-17 15:51:44 -07:00
2009-10-11 18:03:59 +02:00
//This part provides assistance to a player that are attacked by pWho, even if out of normal aggro range
2010-04-14 23:07:41 +02:00
//It will cause me to attack pWho that are attacking _any_ player (which has been confirmed may happen also on offi)
2009-10-11 18:03:59 +02:00
//The flag (type_flag) is unconfirmed, but used here for further research and is a good candidate.
bool FollowerAI::AssistPlayerInCombat(Unit* pWho)
{
if (!pWho || !pWho->getVictim())
return false;
2009-10-17 15:51:44 -07:00
2009-10-11 18:03:59 +02:00
//experimental (unknown) flag not present
if (!(me->GetCreatureInfo()->type_flags & CREATURE_TYPEFLAGS_AID_PLAYERS))
2009-10-11 18:03:59 +02:00
return false;
2009-10-17 15:51:44 -07:00
2009-10-11 18:03:59 +02:00
//not a player
if (!pWho->getVictim()->GetCharmerOrOwnerPlayerOrPlayerItself())
return false;
2009-10-17 15:51:44 -07:00
2009-10-11 18:03:59 +02:00
//never attack friendly
2010-04-14 23:07:41 +02:00
if (me->IsFriendlyTo(pWho))
2009-10-11 18:03:59 +02:00
return false;
2009-10-17 15:51:44 -07:00
2009-10-11 18:03:59 +02:00
//too far away and no free sight?
2010-04-14 23:07:41 +02:00
if (me->IsWithinDistInMap(pWho, MAX_PLAYER_DISTANCE) && me->IsWithinLOSInMap(pWho))
2009-10-11 18:03:59 +02:00
{
//already fighting someone?
2010-04-14 23:07:41 +02:00
if (!me->getVictim())
2009-10-11 18:03:59 +02:00
{
AttackStart(pWho);
return true;
}
else
{
2010-04-14 23:07:41 +02:00
pWho->SetInCombatWith(me);
me->AddThreat(pWho, 0.0f);
2009-10-11 18:03:59 +02:00
return true;
}
}
2009-10-17 15:51:44 -07:00
2009-10-11 18:03:59 +02:00
return false;
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::MoveInLineOfSight(Unit* pWho)
{
2010-04-14 23:07:41 +02:00
if (!me->hasUnitState(UNIT_STAT_STUNNED) && pWho->isTargetableForAttack() && pWho->isInAccessiblePlaceFor(me))
2009-08-16 18:34:45 +02:00
{
2009-10-11 18:03:59 +02:00
if (HasFollowState(STATE_FOLLOW_INPROGRESS) && AssistPlayerInCombat(pWho))
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (!me->canFly() && me->GetDistanceZ(pWho) > CREATURE_Z_ATTACK_RANGE)
2009-08-16 18:34:45 +02:00
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->IsHostileTo(pWho))
2009-08-16 18:34:45 +02:00
{
2010-04-14 23:07:41 +02:00
float fAttackRadius = me->GetAttackDistance(pWho);
if (me->IsWithinDistInMap(pWho, fAttackRadius) && me->IsWithinLOSInMap(pWho))
2009-08-16 18:34:45 +02:00
{
2010-04-14 23:07:41 +02:00
if (!me->getVictim())
2009-08-16 18:34:45 +02:00
{
pWho->RemoveAurasByType(SPELL_AURA_MOD_STEALTH);
2009-08-16 18:34:45 +02:00
AttackStart(pWho);
}
2010-04-14 23:07:41 +02:00
else if (me->GetMap()->IsDungeon())
2009-08-16 18:34:45 +02:00
{
2010-04-14 23:07:41 +02:00
pWho->SetInCombatWith(me);
me->AddThreat(pWho, 0.0f);
2009-08-16 18:34:45 +02:00
}
}
}
}
}
2009-10-17 15:51:44 -07:00
2010-04-19 09:26:37 +02:00
void FollowerAI::JustDied(Unit* /*pKiller*/)
2009-08-16 18:34:45 +02:00
{
2009-08-20 18:50:41 +02:00
if (!HasFollowState(STATE_FOLLOW_INPROGRESS) || !m_uiLeaderGUID || !m_pQuestForFollow)
2009-08-16 18:34:45 +02:00
return;
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
//TODO: need a better check for quests with time limit.
if (Player* pPlayer = GetLeaderForFollower())
{
if (Group* pGroup = pPlayer->GetGroup())
{
2009-10-17 16:20:24 -07:00
for (GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
2009-08-16 18:34:45 +02:00
{
if (Player* pMember = pRef->getSource())
{
2010-01-14 03:55:18 -03:00
if (pMember->GetQuestStatus(m_pQuestForFollow->GetQuestId()) == QUEST_STATUS_INCOMPLETE)
pMember->FailQuest(m_pQuestForFollow->GetQuestId());
2009-08-16 18:34:45 +02:00
}
}
}
else
{
if (pPlayer->GetQuestStatus(m_pQuestForFollow->GetQuestId()) == QUEST_STATUS_INCOMPLETE)
pPlayer->FailQuest(m_pQuestForFollow->GetQuestId());
}
}
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::JustRespawned()
{
2009-08-20 18:50:41 +02:00
m_uiFollowState = STATE_FOLLOW_NONE;
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (!IsCombatMovement())
SetCombatMovement(true);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->getFaction() != me->GetCreatureInfo()->faction_A)
me->setFaction(me->GetCreatureInfo()->faction_A);
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
Reset();
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::EnterEvadeMode()
{
2010-04-14 23:07:41 +02:00
me->RemoveAllAuras();
me->DeleteThreatList();
me->CombatStop(true);
me->SetLootRecipient(NULL);
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
if (HasFollowState(STATE_FOLLOW_INPROGRESS))
2009-08-16 18:34:45 +02:00
{
sLog.outDebug("TSCR: FollowerAI left combat, returning to CombatStartPosition.");
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
2009-08-16 18:34:45 +02:00
{
float fPosX, fPosY, fPosZ;
2010-04-14 23:07:41 +02:00
me->GetPosition(fPosX, fPosY, fPosZ);
me->GetMotionMaster()->MovePoint(POINT_COMBAT_START, fPosX, fPosY, fPosZ);
2009-08-16 18:34:45 +02:00
}
}
else
{
2010-04-14 23:07:41 +02:00
if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
me->GetMotionMaster()->MoveTargetedHome();
2009-08-16 18:34:45 +02:00
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
Reset();
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::UpdateAI(const uint32 uiDiff)
{
2010-04-14 23:07:41 +02:00
if (HasFollowState(STATE_FOLLOW_INPROGRESS) && !me->getVictim())
2009-08-16 18:34:45 +02:00
{
2009-10-27 17:07:40 -07:00
if (m_uiUpdateFollowTimer <= uiDiff)
2009-08-16 18:34:45 +02:00
{
2009-08-20 18:50:41 +02:00
if (HasFollowState(STATE_FOLLOW_COMPLETE) && !HasFollowState(STATE_FOLLOW_POSTEVENT))
2009-08-16 18:34:45 +02:00
{
sLog.outDebug("TSCR: FollowerAI is set completed, despawns.");
2010-04-14 23:07:41 +02:00
me->ForcedDespawn();
2009-08-16 18:34:45 +02:00
return;
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
bool bIsMaxRangeExceeded = true;
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (Player* pPlayer = GetLeaderForFollower())
{
2009-08-20 18:50:41 +02:00
if (HasFollowState(STATE_FOLLOW_RETURNING))
2009-08-16 18:34:45 +02:00
{
sLog.outDebug("TSCR: FollowerAI is returning to leader.");
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
RemoveFollowState(STATE_FOLLOW_RETURNING);
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->MoveFollow(pPlayer, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
2009-08-16 18:34:45 +02:00
return;
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (Group* pGroup = pPlayer->GetGroup())
{
2009-10-17 16:20:24 -07:00
for (GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
2009-08-16 18:34:45 +02:00
{
Player* pMember = pRef->getSource();
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (pMember && me->IsWithinDistInMap(pMember, MAX_PLAYER_DISTANCE))
2009-08-16 18:34:45 +02:00
{
bIsMaxRangeExceeded = false;
break;
}
}
}
else
{
2010-04-14 23:07:41 +02:00
if (me->IsWithinDistInMap(pPlayer, MAX_PLAYER_DISTANCE))
2009-08-16 18:34:45 +02:00
bIsMaxRangeExceeded = false;
}
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (bIsMaxRangeExceeded)
{
sLog.outDebug("TSCR: FollowerAI failed because player/group was to far away or not found");
2010-04-14 23:07:41 +02:00
me->ForcedDespawn();
2009-08-16 18:34:45 +02:00
return;
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
m_uiUpdateFollowTimer = 1000;
}
else
m_uiUpdateFollowTimer -= uiDiff;
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
UpdateFollowerAI(uiDiff);
}
2009-10-17 15:51:44 -07:00
2010-04-19 09:26:37 +02:00
void FollowerAI::UpdateFollowerAI(const uint32 /*uiDiff*/)
2009-08-16 18:34:45 +02:00
{
if (!UpdateVictim())
return;
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
DoMeleeAttackIfReady();
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::MovementInform(uint32 uiMotionType, uint32 uiPointId)
{
2009-08-20 18:50:41 +02:00
if (uiMotionType != POINT_MOTION_TYPE || !HasFollowState(STATE_FOLLOW_INPROGRESS))
2009-08-16 18:34:45 +02:00
return;
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (uiPointId == POINT_COMBAT_START)
{
if (GetLeaderForFollower())
2009-08-20 18:50:41 +02:00
{
if (!HasFollowState(STATE_FOLLOW_PAUSED))
AddFollowState(STATE_FOLLOW_RETURNING);
}
2009-08-16 18:34:45 +02:00
else
2010-04-14 23:07:41 +02:00
me->ForcedDespawn();
2009-08-16 18:34:45 +02:00
}
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
void FollowerAI::StartFollow(Player* pLeader, uint32 uiFactionForFollower, const Quest* pQuest)
{
2010-04-14 23:07:41 +02:00
if (me->getVictim())
2009-08-16 18:34:45 +02:00
{
sLog.outDebug("TSCR: FollowerAI attempt to StartFollow while in combat.");
2009-08-16 18:34:45 +02:00
return;
}
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
if (HasFollowState(STATE_FOLLOW_INPROGRESS))
2009-08-16 18:34:45 +02:00
{
sLog.outError("TSCR: FollowerAI attempt to StartFollow while already following.");
2009-08-16 18:34:45 +02:00
return;
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
//set variables
m_uiLeaderGUID = pLeader->GetGUID();
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
if (uiFactionForFollower)
2010-04-14 23:07:41 +02:00
me->setFaction(uiFactionForFollower);
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
m_pQuestForFollow = pQuest;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE)
2009-08-16 18:34:45 +02:00
{
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->Clear();
me->GetMotionMaster()->MoveIdle();
sLog.outDebug("TSCR: FollowerAI start with WAYPOINT_MOTION_TYPE, set to MoveIdle.");
2009-08-16 18:34:45 +02:00
}
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
me->SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
AddFollowState(STATE_FOLLOW_INPROGRESS);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->MoveFollow(pLeader, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
2009-10-17 15:51:44 -07:00
2010-08-23 07:51:19 +02:00
sLog.outDebug("TSCR: FollowerAI start follow %s (GUID " UI64FMTD ")", pLeader->GetName(), m_uiLeaderGUID);
2009-08-16 18:34:45 +02:00
}
2009-10-17 15:51:44 -07:00
2009-08-16 18:34:45 +02:00
Player* FollowerAI::GetLeaderForFollower()
{
2010-07-30 15:51:07 +02:00
if (Player* pLeader = Unit::GetPlayer(*me, m_uiLeaderGUID))
2009-08-16 18:34:45 +02:00
{
if (pLeader->isAlive())
return pLeader;
else
{
if (Group* pGroup = pLeader->GetGroup())
{
2009-10-17 16:20:24 -07:00
for (GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
2009-08-16 18:34:45 +02:00
{
Player* pMember = pRef->getSource();
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (pMember && pMember->isAlive() && me->IsWithinDistInMap(pMember, MAX_PLAYER_DISTANCE))
2009-08-16 18:34:45 +02:00
{
sLog.outDebug("TSCR: FollowerAI GetLeader changed and returned new leader.");
2009-08-16 18:34:45 +02:00
m_uiLeaderGUID = pMember->GetGUID();
return pMember;
break;
}
}
}
}
}
2009-10-17 15:51:44 -07:00
sLog.outDebug("TSCR: FollowerAI GetLeader can not find suitable leader.");
2009-08-16 18:34:45 +02:00
return NULL;
}
2009-10-17 15:51:44 -07:00
2009-08-20 02:24:53 +02:00
void FollowerAI::SetFollowComplete(bool bWithEndEvent)
{
2010-04-14 23:07:41 +02:00
if (me->hasUnitState(UNIT_STAT_FOLLOW))
2009-08-20 02:24:53 +02:00
{
2010-04-14 23:07:41 +02:00
me->clearUnitState(UNIT_STAT_FOLLOW);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
me->StopMoving();
me->GetMotionMaster()->Clear();
me->GetMotionMaster()->MoveIdle();
2009-08-20 02:24:53 +02:00
}
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
if (bWithEndEvent)
AddFollowState(STATE_FOLLOW_POSTEVENT);
else
{
if (HasFollowState(STATE_FOLLOW_POSTEVENT))
RemoveFollowState(STATE_FOLLOW_POSTEVENT);
}
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
AddFollowState(STATE_FOLLOW_COMPLETE);
}
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
void FollowerAI::SetFollowPaused(bool bPaused)
{
if (!HasFollowState(STATE_FOLLOW_INPROGRESS) || HasFollowState(STATE_FOLLOW_COMPLETE))
return;
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
if (bPaused)
{
AddFollowState(STATE_FOLLOW_PAUSED);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->hasUnitState(UNIT_STAT_FOLLOW))
2009-08-20 18:50:41 +02:00
{
2010-04-14 23:07:41 +02:00
me->clearUnitState(UNIT_STAT_FOLLOW);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
me->StopMoving();
me->GetMotionMaster()->Clear();
me->GetMotionMaster()->MoveIdle();
2009-08-20 18:50:41 +02:00
}
}
else
{
RemoveFollowState(STATE_FOLLOW_PAUSED);
2009-10-17 15:51:44 -07:00
2009-08-20 18:50:41 +02:00
if (Player* pLeader = GetLeaderForFollower())
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->MoveFollow(pLeader, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
2009-08-20 18:50:41 +02:00
}
2009-08-20 02:24:53 +02:00
}