NPC_AI.cpp 23 KB

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