NPC_AI.cpp 19 KB

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