PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/server/game/AI/ScriptedAI/ScriptedFollowerAI.cpp

https://github.com/skypeak/VoragineCore
C++ | 407 lines | 307 code | 63 blank | 37 comment | 88 complexity | 94938161342391cf6d8f4fef4c94c7e9 MD5 | raw file
  1. /*
  2. * Copyright (C) 2005-2011 MaNGOS <http://www.getmangos.com/>
  3. *
  4. * Copyright (C) 2008-2011 Trinity <http://www.trinitycore.org/>
  5. *
  6. * Copyright (C) 2010-2011 VoragineCore <http://www.projectvoragine.com/>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "gamePCH.h"
  23. /* ScriptData
  24. SDName: FollowerAI
  25. SD%Complete: 50
  26. SDComment: This AI is under development
  27. SDCategory: Npc
  28. EndScriptData */
  29. #include "ScriptPCH.h"
  30. #include "ScriptedFollowerAI.h"
  31. #include "Group.h"
  32. const float MAX_PLAYER_DISTANCE = 100.0f;
  33. enum ePoints
  34. {
  35. POINT_COMBAT_START = 0xFFFFFF
  36. };
  37. FollowerAI::FollowerAI(Creature* pCreature) : ScriptedAI(pCreature),
  38. m_uiLeaderGUID(0),
  39. m_uiUpdateFollowTimer(2500),
  40. m_uiFollowState(STATE_FOLLOW_NONE),
  41. m_pQuestForFollow(NULL)
  42. {}
  43. void FollowerAI::AttackStart(Unit* pWho)
  44. {
  45. if (!pWho)
  46. return;
  47. if (me->Attack(pWho, true))
  48. {
  49. me->AddThreat(pWho, 0.0f);
  50. me->SetInCombatWith(pWho);
  51. pWho->SetInCombatWith(me);
  52. if (me->HasUnitState(UNIT_STAT_FOLLOW))
  53. me->ClearUnitState(UNIT_STAT_FOLLOW);
  54. if (IsCombatMovement())
  55. me->GetMotionMaster()->MoveChase(pWho);
  56. }
  57. }
  58. //This part provides assistance to a player that are attacked by pWho, even if out of normal aggro range
  59. //It will cause me to attack pWho that are attacking _any_ player (which has been confirmed may happen also on offi)
  60. //The flag (type_flag) is unconfirmed, but used here for further research and is a good candidate.
  61. bool FollowerAI::AssistPlayerInCombat(Unit* pWho)
  62. {
  63. if (!pWho || !pWho->getVictim())
  64. return false;
  65. //experimental (unknown) flag not present
  66. if (!(me->GetCreatureInfo()->type_flags & CREATURE_TYPEFLAGS_AID_PLAYERS))
  67. return false;
  68. //not a player
  69. if (!pWho->getVictim()->GetCharmerOrOwnerPlayerOrPlayerItself())
  70. return false;
  71. //never attack friendly
  72. if (me->IsFriendlyTo(pWho))
  73. return false;
  74. //too far away and no free sight?
  75. if (me->IsWithinDistInMap(pWho, MAX_PLAYER_DISTANCE) && me->IsWithinLOSInMap(pWho))
  76. {
  77. //already fighting someone?
  78. if (!me->getVictim())
  79. {
  80. AttackStart(pWho);
  81. return true;
  82. }
  83. else
  84. {
  85. pWho->SetInCombatWith(me);
  86. me->AddThreat(pWho, 0.0f);
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. void FollowerAI::MoveInLineOfSight(Unit* pWho)
  93. {
  94. if (!me->HasUnitState(UNIT_STAT_STUNNED) && pWho->isTargetableForAttack() && pWho->isInAccessiblePlaceFor(me))
  95. {
  96. if (HasFollowState(STATE_FOLLOW_INPROGRESS) && AssistPlayerInCombat(pWho))
  97. return;
  98. if (!me->canFly() && me->GetDistanceZ(pWho) > CREATURE_Z_ATTACK_RANGE)
  99. return;
  100. if (me->IsHostileTo(pWho))
  101. {
  102. float fAttackRadius = me->GetAttackDistance(pWho);
  103. if (me->IsWithinDistInMap(pWho, fAttackRadius) && me->IsWithinLOSInMap(pWho))
  104. {
  105. if (!me->getVictim())
  106. {
  107. pWho->RemoveAurasByType(SPELL_AURA_MOD_STEALTH);
  108. AttackStart(pWho);
  109. }
  110. else if (me->GetMap()->IsDungeon())
  111. {
  112. pWho->SetInCombatWith(me);
  113. me->AddThreat(pWho, 0.0f);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. void FollowerAI::JustDied(Unit* /*pKiller*/)
  120. {
  121. if (!HasFollowState(STATE_FOLLOW_INPROGRESS) || !m_uiLeaderGUID || !m_pQuestForFollow)
  122. return;
  123. //TODO: need a better check for quests with time limit.
  124. if (Player* pPlayer = GetLeaderForFollower())
  125. {
  126. if (Group* pGroup = pPlayer->GetGroup())
  127. {
  128. for (GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
  129. {
  130. if (Player* pMember = pRef->getSource())
  131. {
  132. if (pMember->GetQuestStatus(m_pQuestForFollow->GetQuestId()) == QUEST_STATUS_INCOMPLETE)
  133. pMember->FailQuest(m_pQuestForFollow->GetQuestId());
  134. }
  135. }
  136. }
  137. else
  138. {
  139. if (pPlayer->GetQuestStatus(m_pQuestForFollow->GetQuestId()) == QUEST_STATUS_INCOMPLETE)
  140. pPlayer->FailQuest(m_pQuestForFollow->GetQuestId());
  141. }
  142. }
  143. }
  144. void FollowerAI::JustRespawned()
  145. {
  146. m_uiFollowState = STATE_FOLLOW_NONE;
  147. if (!IsCombatMovement())
  148. SetCombatMovement(true);
  149. if (me->getFaction() != me->GetCreatureInfo()->faction_A)
  150. me->setFaction(me->GetCreatureInfo()->faction_A);
  151. Reset();
  152. }
  153. void FollowerAI::EnterEvadeMode()
  154. {
  155. me->RemoveAllAuras();
  156. me->DeleteThreatList();
  157. me->CombatStop(true);
  158. me->SetLootRecipient(NULL);
  159. if (HasFollowState(STATE_FOLLOW_INPROGRESS))
  160. {
  161. sLog->outDebug("TSCR: FollowerAI left combat, returning to CombatStartPosition.");
  162. if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
  163. {
  164. float fPosX, fPosY, fPosZ;
  165. me->GetPosition(fPosX, fPosY, fPosZ);
  166. me->GetMotionMaster()->MovePoint(POINT_COMBAT_START, fPosX, fPosY, fPosZ);
  167. }
  168. }
  169. else
  170. {
  171. if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
  172. me->GetMotionMaster()->MoveTargetedHome();
  173. }
  174. Reset();
  175. }
  176. void FollowerAI::UpdateAI(const uint32 uiDiff)
  177. {
  178. if (HasFollowState(STATE_FOLLOW_INPROGRESS) && !me->getVictim())
  179. {
  180. if (m_uiUpdateFollowTimer <= uiDiff)
  181. {
  182. if (HasFollowState(STATE_FOLLOW_COMPLETE) && !HasFollowState(STATE_FOLLOW_POSTEVENT))
  183. {
  184. sLog->outDebug("TSCR: FollowerAI is set completed, despawns.");
  185. me->ForcedDespawn();
  186. return;
  187. }
  188. bool bIsMaxRangeExceeded = true;
  189. if (Player* pPlayer = GetLeaderForFollower())
  190. {
  191. if (HasFollowState(STATE_FOLLOW_RETURNING))
  192. {
  193. sLog->outDebug("TSCR: FollowerAI is returning to leader.");
  194. RemoveFollowState(STATE_FOLLOW_RETURNING);
  195. me->GetMotionMaster()->MoveFollow(pPlayer, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
  196. return;
  197. }
  198. if (Group* pGroup = pPlayer->GetGroup())
  199. {
  200. for (GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
  201. {
  202. Player* pMember = pRef->getSource();
  203. if (pMember && me->IsWithinDistInMap(pMember, MAX_PLAYER_DISTANCE))
  204. {
  205. bIsMaxRangeExceeded = false;
  206. break;
  207. }
  208. }
  209. }
  210. else
  211. {
  212. if (me->IsWithinDistInMap(pPlayer, MAX_PLAYER_DISTANCE))
  213. bIsMaxRangeExceeded = false;
  214. }
  215. }
  216. if (bIsMaxRangeExceeded)
  217. {
  218. sLog->outDebug("TSCR: FollowerAI failed because player/group was to far away or not found");
  219. me->ForcedDespawn();
  220. return;
  221. }
  222. m_uiUpdateFollowTimer = 1000;
  223. }
  224. else
  225. m_uiUpdateFollowTimer -= uiDiff;
  226. }
  227. UpdateFollowerAI(uiDiff);
  228. }
  229. void FollowerAI::UpdateFollowerAI(const uint32 /*uiDiff*/)
  230. {
  231. if (!UpdateVictim())
  232. return;
  233. DoMeleeAttackIfReady();
  234. }
  235. void FollowerAI::MovementInform(uint32 uiMotionType, uint32 uiPointId)
  236. {
  237. if (uiMotionType != POINT_MOTION_TYPE || !HasFollowState(STATE_FOLLOW_INPROGRESS))
  238. return;
  239. if (uiPointId == POINT_COMBAT_START)
  240. {
  241. if (GetLeaderForFollower())
  242. {
  243. if (!HasFollowState(STATE_FOLLOW_PAUSED))
  244. AddFollowState(STATE_FOLLOW_RETURNING);
  245. }
  246. else
  247. me->ForcedDespawn();
  248. }
  249. }
  250. void FollowerAI::StartFollow(Player* pLeader, uint32 uiFactionForFollower, const Quest* pQuest)
  251. {
  252. if (me->getVictim())
  253. {
  254. sLog->outDebug("TSCR: FollowerAI attempt to StartFollow while in combat.");
  255. return;
  256. }
  257. if (HasFollowState(STATE_FOLLOW_INPROGRESS))
  258. {
  259. sLog->outError("TSCR: FollowerAI attempt to StartFollow while already following.");
  260. return;
  261. }
  262. //set variables
  263. m_uiLeaderGUID = pLeader->GetGUID();
  264. if (uiFactionForFollower)
  265. me->setFaction(uiFactionForFollower);
  266. m_pQuestForFollow = pQuest;
  267. if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE)
  268. {
  269. me->GetMotionMaster()->Clear();
  270. me->GetMotionMaster()->MoveIdle();
  271. sLog->outDebug("TSCR: FollowerAI start with WAYPOINT_MOTION_TYPE, set to MoveIdle.");
  272. }
  273. me->SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
  274. AddFollowState(STATE_FOLLOW_INPROGRESS);
  275. me->GetMotionMaster()->MoveFollow(pLeader, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
  276. sLog->outDebug("TSCR: FollowerAI start follow %s (GUID " UI64FMTD ")", pLeader->GetName(), m_uiLeaderGUID);
  277. }
  278. Player* FollowerAI::GetLeaderForFollower()
  279. {
  280. if (Player* pLeader = Unit::GetPlayer(*me, m_uiLeaderGUID))
  281. {
  282. if (pLeader->isAlive())
  283. return pLeader;
  284. else
  285. {
  286. if (Group* pGroup = pLeader->GetGroup())
  287. {
  288. for (GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
  289. {
  290. Player* pMember = pRef->getSource();
  291. if (pMember && pMember->isAlive() && me->IsWithinDistInMap(pMember, MAX_PLAYER_DISTANCE))
  292. {
  293. sLog->outDebug("TSCR: FollowerAI GetLeader changed and returned new leader.");
  294. m_uiLeaderGUID = pMember->GetGUID();
  295. return pMember;
  296. break;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. sLog->outDebug("TSCR: FollowerAI GetLeader can not find suitable leader.");
  303. return NULL;
  304. }
  305. void FollowerAI::SetFollowComplete(bool bWithEndEvent)
  306. {
  307. if (me->HasUnitState(UNIT_STAT_FOLLOW))
  308. {
  309. me->ClearUnitState(UNIT_STAT_FOLLOW);
  310. me->StopMoving();
  311. me->GetMotionMaster()->Clear();
  312. me->GetMotionMaster()->MoveIdle();
  313. }
  314. if (bWithEndEvent)
  315. AddFollowState(STATE_FOLLOW_POSTEVENT);
  316. else
  317. {
  318. if (HasFollowState(STATE_FOLLOW_POSTEVENT))
  319. RemoveFollowState(STATE_FOLLOW_POSTEVENT);
  320. }
  321. AddFollowState(STATE_FOLLOW_COMPLETE);
  322. }
  323. void FollowerAI::SetFollowPaused(bool bPaused)
  324. {
  325. if (!HasFollowState(STATE_FOLLOW_INPROGRESS) || HasFollowState(STATE_FOLLOW_COMPLETE))
  326. return;
  327. if (bPaused)
  328. {
  329. AddFollowState(STATE_FOLLOW_PAUSED);
  330. if (me->HasUnitState(UNIT_STAT_FOLLOW))
  331. {
  332. me->ClearUnitState(UNIT_STAT_FOLLOW);
  333. me->StopMoving();
  334. me->GetMotionMaster()->Clear();
  335. me->GetMotionMaster()->MoveIdle();
  336. }
  337. }
  338. else
  339. {
  340. RemoveFollowState(STATE_FOLLOW_PAUSED);
  341. if (Player* pLeader = GetLeaderForFollower())
  342. me->GetMotionMaster()->MoveFollow(pLeader, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
  343. }
  344. }