NPC_AI.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "NPC_AI.h"
  17. #include "Combat.h"
  18. #include "zoneserver.h"
  19. #include "Spells.h"
  20. #include "../common/Log.h"
  21. #include "LuaInterface.h"
  22. #include "World.h"
  23. #include "Rules/Rules.h"
  24. extern RuleManager rule_manager;
  25. extern LuaInterface* lua_interface;
  26. extern World world;
  27. /* The NEW AI code */
  28. Brain::Brain(NPC* npc) {
  29. // Set the npc this brain will controll
  30. m_body = npc;
  31. // Set the default time between calls to think to 250 miliseconds (1/4 a second)
  32. m_tick = 250;
  33. m_lastTick = Timer::GetCurrentTime2();
  34. m_spellRecovery = 0;
  35. m_playerInEncounter = false;
  36. // Set up the mutex for the hate list
  37. MHateList.SetName("Brain::m_hatelist");
  38. // Set up the mutex for the encounter list
  39. MEncounter.SetName("Brain::m_encounter");
  40. }
  41. Brain::~Brain() {
  42. }
  43. void Brain::Think() {
  44. // Get the entity this NPC hates the most,
  45. // GetMostHated() will handle dead spawns so no need to check the health in this function
  46. Entity* target = GetMostHated();
  47. // If mezzed, stunned or feared we can't do anything so skip
  48. if (!m_body->IsMezzedOrStunned() || !m_body->IsFeared()) {
  49. // Not mezzed or stunned
  50. // Get the distance to the runback location
  51. float run_back_distance = m_body->GetRunbackDistance();
  52. if (target) {
  53. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s has %s targeted.", m_body->GetName(), target->GetName());
  54. // NPC has an entity that it hates
  55. // If the NPC is not in combat then put them in combat
  56. if (!m_body->EngagedInCombat()) {
  57. m_body->ClearRunningLocations();
  58. m_body->InCombat(true);
  59. }
  60. // Set the NPC's target to the most hated entity if it is not already.
  61. if (m_body->GetTarget() != target) {
  62. m_body->SetTarget(target);
  63. m_body->FaceTarget(target);
  64. }
  65. // Check to see if the NPC has exceeded the max chase distance
  66. if (run_back_distance > MAX_CHASE_DISTANCE) {
  67. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Run back distance is greater then max chase distance, run_back_distance = %f", run_back_distance);
  68. // Over the max chase distance, Check to see if the target is is a client
  69. Client* client = target->GetZone()->GetClientBySpawn(target);
  70. if (client)
  71. {
  72. // Target is a client so send encounter break messages
  73. if (m_body->HasSpawnGroup())
  74. client->SimpleMessage(CHANNEL_COLOR_WHITE, "This encounter will no longer give encounter rewards.");
  75. else
  76. client->Message(CHANNEL_COLOR_WHITE, "%s is no longer worth any experience or treasure.", m_body->GetName());
  77. }
  78. // Clear the hate list for this NPC
  79. ClearHate();
  80. // Clear the encounter list
  81. ClearEncounter();
  82. }
  83. else {
  84. // Still within max chase distance lets to the combat stuff now
  85. float distance = m_body->GetDistance(target);
  86. if(!m_body->IsCasting() && (!HasRecovered() || !ProcessSpell(target, distance))) {
  87. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is attempting melee on %s.", m_body->GetName(), target->GetName());
  88. m_body->FaceTarget(target);
  89. ProcessMelee(target, distance);
  90. }
  91. }
  92. }
  93. else {
  94. // Nothing in the hate list
  95. bool wasInCombat = m_body->EngagedInCombat();
  96. // Check to see if the NPC is still flagged as in combat for some reason
  97. if (m_body->EngagedInCombat()) {
  98. // If it is set the combat flag to false
  99. m_body->InCombat(false);
  100. // Do not set a players pet to full health once they stop combat
  101. if (!m_body->IsPet() || (m_body->IsPet() && !m_body->GetOwner()->IsPlayer()))
  102. m_body->SetHP(m_body->GetTotalHP());
  103. }
  104. CheckBuffs();
  105. // If run back distance is greater then 0 then run back
  106. if ((wasInCombat || m_body->m_runningBack) && run_back_distance > 1) {
  107. m_body->Runback();
  108. }
  109. else if (m_body->m_runningBack)
  110. {
  111. m_body->SetX(m_body->GetRunbackLocation()->x,false);
  112. m_body->SetZ(m_body->GetRunbackLocation()->z,false);
  113. m_body->SetY(m_body->GetRunbackLocation()->y,true);
  114. if (m_body->GetRunbackLocation()->gridid > 0)
  115. m_body->SetLocation(m_body->GetRunbackLocation()->gridid);
  116. m_body->ClearRunback();
  117. m_body->GetZone()->movementMgr->StopNavigation((Entity*)m_body);
  118. m_body->ClearRunningLocations();
  119. }
  120. // If encounter size is greater then 0 then clear it
  121. if (GetEncounterSize() > 0)
  122. ClearEncounter();
  123. }
  124. }
  125. }
  126. sint32 Brain::GetHate(Entity* entity) {
  127. // We will use this variable to return the value, default to 0
  128. sint32 ret = 0;
  129. // Lock the hate list, not altering it so do a read lock
  130. MHateList.readlock(__FUNCTION__, __LINE__);
  131. // First check to see if the given entity is even in the hate list
  132. if (m_hatelist.count(entity->GetID()) > 0)
  133. // Entity in the hate list so get the hate value for the entity
  134. ret = m_hatelist[entity->GetID()];
  135. // Unlock the hate list
  136. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  137. // return the hate
  138. return ret;
  139. }
  140. void Brain::AddHate(Entity* entity, sint32 hate) {
  141. // Lock the hate list, we are altering the list so use write lock
  142. MHateList.writelock(__FUNCTION__, __LINE__);
  143. if (m_hatelist.count(entity->GetID()) > 0)
  144. m_hatelist[entity->GetID()] += hate;
  145. else
  146. m_hatelist.insert(std::pair<int32, sint32>(entity->GetID(), hate));
  147. if (entity->HatedBy.count(m_body->GetID()) == 0)
  148. entity->HatedBy.insert(m_body->GetID());
  149. // Unlock the list
  150. MHateList.releasewritelock(__FUNCTION__, __LINE__);
  151. }
  152. void Brain::ClearHate() {
  153. // Lock the hate list, we are altering the list so use a write lock
  154. MHateList.writelock(__FUNCTION__, __LINE__);
  155. map<int32, sint32>::iterator itr;
  156. for (itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  157. Spawn* spawn = m_body->GetZone()->GetSpawnByID(itr->first);
  158. if (spawn && spawn->IsEntity())
  159. ((Entity*)spawn)->HatedBy.erase(itr->first);
  160. }
  161. // Clear the list
  162. m_hatelist.clear();
  163. // Unlock the hate list
  164. MHateList.releasewritelock(__FUNCTION__, __LINE__);
  165. }
  166. void Brain::ClearHate(Entity* entity) {
  167. // Lock the hate list, we could potentially modify the list so use write lock
  168. MHateList.writelock(__FUNCTION__, __LINE__);
  169. // Check to see if the given entity is in the hate list
  170. if (m_hatelist.count(entity->GetID()) > 0)
  171. // Erase the entity from the hate list
  172. m_hatelist.erase(entity->GetID());
  173. entity->HatedBy.erase(m_body->GetID());
  174. // Unlock the hate list
  175. MHateList.releasewritelock(__FUNCTION__, __LINE__);
  176. }
  177. Entity* Brain::GetMostHated() {
  178. map<int32, sint32>::iterator itr;
  179. int32 ret = 0;
  180. sint32 hate = 0;
  181. // Lock the hate list, not going to alter it so use a read lock
  182. MHateList.readlock(__FUNCTION__, __LINE__);
  183. if (m_hatelist.size() > 0) {
  184. // Loop through the list looking for the entity that this NPC hates the most
  185. for(itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  186. // Compare the hate value for the current iteration to our stored highest value
  187. if(itr->second > hate) {
  188. // New high value store the entity
  189. ret = itr->first;
  190. // Store the value to compare with the rest of the entities
  191. hate = itr->second;
  192. }
  193. }
  194. }
  195. // Unlock the list
  196. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  197. Entity* hated = (Entity*)GetBody()->GetZone()->GetSpawnByID(ret);
  198. // Check the reult to see if it is still alive
  199. if(hated && hated->GetHP() <= 0) {
  200. // Entity we got was dead so remove it from the list
  201. ClearHate(hated);
  202. // Call this function again now that we removed the dead entity
  203. hated = GetMostHated();
  204. }
  205. // Return our result
  206. return hated;
  207. }
  208. sint8 Brain::GetHatePercentage(Entity* entity) {
  209. float percentage = 0.0;
  210. MHateList.readlock(__FUNCTION__, __LINE__);
  211. if (entity && m_hatelist.count(entity->GetID()) > 0 && m_hatelist[entity->GetID()] > 0) {
  212. sint32 total_hate = 0;
  213. map<int32, sint32>::iterator itr;
  214. for (itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++)
  215. total_hate += itr->second;
  216. percentage = m_hatelist[entity->GetID()] / total_hate;
  217. }
  218. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  219. return (sint8)(percentage * 100);
  220. }
  221. vector<Entity*>* Brain::GetHateList() {
  222. vector<Entity*>* ret = new vector<Entity*>;
  223. map<int32, sint32>::iterator itr;
  224. // Lock the list
  225. MHateList.readlock(__FUNCTION__, __LINE__);
  226. // Loop over the list storing the values into the new list
  227. for (itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  228. Entity* ent = (Entity*)GetBody()->GetZone()->GetSpawnByID(itr->first);
  229. if (ent)
  230. ret->push_back(ent);
  231. }
  232. // Unlock the list
  233. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  234. // Return the copy of the list
  235. return ret;
  236. }
  237. void Brain::MoveCloser(Entity* target) {
  238. if (target && m_body->GetFollowTarget() != target)
  239. m_body->SetFollowTarget(target);
  240. if (m_body->GetFollowTarget() && !m_body->following) {
  241. m_body->CalculateRunningLocation(true);
  242. //m_body->ClearRunningLocations();
  243. m_body->following = true;
  244. }
  245. }
  246. bool Brain::ProcessSpell(Entity* target, float distance) {
  247. if(rand()%100 > m_body->GetCastPercentage() || m_body->IsStifled() || m_body->IsFeared())
  248. return false;
  249. Spell* spell = m_body->GetNextSpell(distance);
  250. if(spell){
  251. Spawn* spell_target = 0;
  252. if(spell->GetSpellData()->friendly_spell == 1){
  253. vector<Spawn*>* group = m_body->GetSpawnGroup();
  254. if(group && group->size() > 0){
  255. vector<Spawn*>::iterator itr;
  256. for(itr = group->begin(); itr != group->end(); itr++){
  257. if((!spell_target && (*itr)->GetHP() > 0 && (*itr)->GetHP() < (*itr)->GetTotalHP()) || (spell_target && (*itr)->GetHP() > 0 && spell_target->GetHP() > (*itr)->GetHP()))
  258. spell_target = *itr;
  259. }
  260. }
  261. if(!spell_target)
  262. spell_target = m_body;
  263. safe_delete(group);
  264. }
  265. else
  266. spell_target = target;
  267. m_body->GetZone()->ProcessSpell(spell, m_body, spell_target);
  268. m_spellRecovery = (int32)(Timer::GetCurrentTime2() + (spell->GetSpellData()->cast_time * 10) + (spell->GetSpellData()->recovery * 10) + 2000);
  269. return true;
  270. }
  271. return false;
  272. }
  273. bool Brain::CheckBuffs() {
  274. if (!m_body->GetZone()->GetSpellProcess() || m_body->EngagedInCombat() || m_body->IsCasting() || m_body->IsMezzedOrStunned() || !m_body->Alive() || m_body->IsStifled() || !HasRecovered())
  275. return false;
  276. Spell* spell = m_body->GetNextBuffSpell();
  277. if (spell) {
  278. m_body->CalculateRunningLocation(true);
  279. m_body->GetZone()->ProcessSpell(spell, m_body, m_body);
  280. m_spellRecovery = (int32)(Timer::GetCurrentTime2() + (spell->GetSpellData()->cast_time * 10) + (spell->GetSpellData()->recovery * 10) + 2000);
  281. return true;
  282. }
  283. return false;
  284. }
  285. void Brain::ProcessMelee(Entity* target, float distance) {
  286. if(distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  287. MoveCloser(target);
  288. else {
  289. if (target) {
  290. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is within melee range of %s.", m_body->GetName(), target->GetName());
  291. if (m_body->AttackAllowed(target)) {
  292. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is allowed to attack %s.", m_body->GetName(), target->GetName());
  293. if (m_body->PrimaryWeaponReady() && !m_body->IsDazed() && !m_body->IsFeared()) {
  294. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s swings its primary weapon at %s.", m_body->GetName(), target->GetName());
  295. m_body->SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  296. m_body->MeleeAttack(target, distance, true);
  297. m_body->GetZone()->CallSpawnScript(m_body, SPAWN_SCRIPT_AUTO_ATTACK_TICK, target);
  298. }
  299. if (m_body->SecondaryWeaponReady() && !m_body->IsDazed()) {
  300. m_body->SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  301. m_body->MeleeAttack(target, distance, false);
  302. }
  303. }
  304. }
  305. }
  306. }
  307. bool Brain::HasRecovered() {
  308. if(m_spellRecovery > Timer::GetCurrentTime2())
  309. return false;
  310. m_spellRecovery = 0;
  311. return true;
  312. }
  313. void Brain::AddToEncounter(Entity* entity) {
  314. // If player pet then set the entity to the pets owner
  315. if (entity->IsPet() && ((NPC*)entity)->GetOwner()->IsPlayer())
  316. entity = ((NPC*)entity)->GetOwner();
  317. // If player or bot then get the group
  318. int32 group_id = 0;
  319. if (entity->IsPlayer() || entity->IsBot()) {
  320. m_playerInEncounter = true;
  321. if (entity->GetGroupMemberInfo())
  322. group_id = entity->GetGroupMemberInfo()->group_id;
  323. }
  324. // Insert the entity into the encounter list, if there is a group add all group members as well
  325. // TODO: add raid members
  326. MEncounter.writelock(__FUNCTION__, __LINE__);
  327. if (group_id > 0) {
  328. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  329. deque<GroupMemberInfo*>::iterator itr;
  330. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(group_id);
  331. for (itr = members->begin(); itr != members->end(); itr++) {
  332. if ((*itr)->client)
  333. m_encounter.push_back((*itr)->client->GetPlayer()->GetID());
  334. }
  335. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  336. }
  337. else {
  338. m_encounter.push_back(entity->GetID());
  339. }
  340. MEncounter.releasewritelock(__FUNCTION__, __LINE__);
  341. }
  342. bool Brain::CheckLootAllowed(Entity* entity) {
  343. bool ret = false;
  344. vector<int32>::iterator itr;
  345. // Check the encounter list to see if the given entity is in it, if so return true.
  346. MEncounter.readlock(__FUNCTION__, __LINE__);
  347. for (itr = m_encounter.begin(); itr != m_encounter.end(); itr++) {
  348. if ((*itr) == entity->GetID()) {
  349. // found the entity in the encounter list, set return value to true and break the loop
  350. ret = true;
  351. break;
  352. }
  353. }
  354. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  355. return ret;
  356. }
  357. int8 Brain::GetEncounterSize() {
  358. int8 ret = 0;
  359. MEncounter.readlock(__FUNCTION__, __LINE__);
  360. ret = (int8)m_encounter.size();
  361. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  362. return ret;
  363. }
  364. vector<int32>* Brain::GetEncounter() {
  365. vector<int32>* ret = new vector<int32>;
  366. vector<int32>::iterator itr;
  367. // Lock the list
  368. MEncounter.readlock(__FUNCTION__, __LINE__);
  369. // Loop over the list storing the values into the new list
  370. for (itr = m_encounter.begin(); itr != m_encounter.end(); itr++)
  371. ret->push_back(*itr);
  372. // Unlock the list
  373. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  374. // Return the copy of the list
  375. return ret;
  376. }
  377. void Brain::ClearEncounter() {
  378. MEncounter.writelock(__FUNCTION__, __LINE__);
  379. m_encounter.clear();
  380. m_playerInEncounter = false;
  381. MEncounter.releasewritelock(__FUNCTION__, __LINE__);
  382. }
  383. /* Example of how to extend the default AI */
  384. CombatPetBrain::CombatPetBrain(NPC* body) : Brain(body) {
  385. // Make sure to have the " : Brain(body)" so it calls the parent class constructor
  386. // to set up the AI properly
  387. }
  388. CombatPetBrain::~CombatPetBrain() {
  389. }
  390. void CombatPetBrain::Think() {
  391. // We are extending the base brain so make sure to call the parent Think() function.
  392. // If we want to override then we could remove Brain::Think()
  393. Brain::Think();
  394. // All this Brain does is make the pet follow its owner, the combat comes from the default brain
  395. if (GetBody()->EngagedInCombat() || !GetBody()->IsPet() || GetBody()->IsMezzedOrStunned())
  396. return;
  397. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Pet AI code called for %s", GetBody()->GetName());
  398. // If owner is a player and player has stay set then return out
  399. if (GetBody()->GetOwner()->IsPlayer() && ((Player*)GetBody()->GetOwner())->GetInfoStruct()->pet_movement == 1)
  400. return;
  401. // Set target to owner
  402. Entity* target = GetBody()->GetOwner();
  403. GetBody()->SetTarget(target);
  404. // Get distance from the owner
  405. float distance = GetBody()->GetDistance(target);
  406. // If out of melee range then move closer
  407. if (distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  408. MoveCloser(target);
  409. }
  410. /* Example of how to override the default AI */
  411. NonCombatPetBrain::NonCombatPetBrain(NPC* body) : Brain(body) {
  412. // Make sure to have the " : Brain(body)" so it calls the parent class constructor
  413. // to set up the AI properly
  414. }
  415. NonCombatPetBrain::~NonCombatPetBrain() {
  416. }
  417. void NonCombatPetBrain::Think() {
  418. // All this Brain does is make the pet follow its owner
  419. if (!GetBody()->IsPet() || GetBody()->IsMezzedOrStunned())
  420. return;
  421. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Pet AI code called for %s", GetBody()->GetName());
  422. // Set target to owner
  423. Entity* target = GetBody()->GetOwner();
  424. GetBody()->SetTarget(target);
  425. // Get distance from the owner
  426. float distance = GetBody()->GetDistance(target);
  427. // If out of melee range then move closer
  428. if (distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  429. MoveCloser(target);
  430. }
  431. BlankBrain::BlankBrain(NPC* body) : Brain(body) {
  432. // Make sure to have the " : Brain(body)" so it calls the parent class constructor
  433. // to set up the AI properly
  434. SetTick(50000);
  435. }
  436. BlankBrain::~BlankBrain() {
  437. }
  438. void BlankBrain::Think() {
  439. }
  440. LuaBrain::LuaBrain(NPC* body) : Brain(body) {
  441. }
  442. LuaBrain::~LuaBrain() {
  443. }
  444. void LuaBrain::Think() {
  445. if (!lua_interface)
  446. return;
  447. const char* script = GetBody()->GetSpawnScript();
  448. if(script) {
  449. if (!lua_interface->RunSpawnScript(script, "Think", GetBody(), GetBody()->GetTarget())) {
  450. lua_interface->LogError("LUA LuaBrain error: was unable to call the Think function in the spawn script (%s)", script);
  451. }
  452. }
  453. else {
  454. LogWrite(NPC_AI__ERROR, 0, "NPC_AI", "Lua brain set on a spawn that doesn't have a script...");
  455. }
  456. }
  457. DumbFirePetBrain::DumbFirePetBrain(NPC* body, Entity* target, int32 expire_time) : Brain(body) {
  458. m_expireTime = Timer::GetCurrentTime2() + expire_time;
  459. AddHate(target, INT_MAX);
  460. }
  461. DumbFirePetBrain::~DumbFirePetBrain() {
  462. }
  463. void DumbFirePetBrain::AddHate(Entity* entity, sint32 hate) {
  464. if (!GetMostHated())
  465. Brain::AddHate(entity, hate);
  466. }
  467. void DumbFirePetBrain::Think() {
  468. Entity* target = GetMostHated();
  469. if (target) {
  470. if (!GetBody()->IsMezzedOrStunned()) {
  471. // If the NPC is not in combat then put them in combat
  472. if (!GetBody()->EngagedInCombat()) {
  473. //GetBody()->ClearRunningLocations();
  474. GetBody()->CalculateRunningLocation(true);
  475. GetBody()->InCombat(true);
  476. }
  477. // Set the NPC's target to the most hated entity if it is not already.
  478. if (GetBody()->GetTarget() != target) {
  479. GetBody()->SetTarget(target);
  480. GetBody()->FaceTarget(target);
  481. }
  482. float distance = GetBody()->GetDistance(target);
  483. if(GetBody()->CheckLoS(target) && !GetBody()->IsCasting() && (!HasRecovered() || !ProcessSpell(target, distance))) {
  484. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is attempting melee on %s.", GetBody()->GetName(), target->GetName());
  485. GetBody()->FaceTarget(target);
  486. ProcessMelee(target, distance);
  487. }
  488. }
  489. }
  490. else {
  491. // No hated target or time expired, kill this mob
  492. if (GetBody()->GetHP() > 0) {
  493. GetBody()->KillSpawn(GetBody());
  494. LogWrite(NPC_AI__DEBUG, 7, "NPC AI", "Dumbfire being killed because there is no target.");
  495. }
  496. }
  497. if (Timer::GetCurrentTime2() > m_expireTime) {
  498. if (GetBody()->GetHP() > 0) {
  499. GetBody()->KillSpawn(GetBody());
  500. LogWrite(NPC_AI__DEBUG, 7, "NPC AI", "Dumbfire being killed because timer expired.");
  501. }
  502. }
  503. }