NPC_AI.cpp 24 KB

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