NPC_AI.cpp 22 KB

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