9
3

NPC_AI.cpp 20 KB

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