NPC_AI.cpp 20 KB

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