NPC_AI.cpp 23 KB

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