NPC_AI.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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() && !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. bool ownerExistsAddHate = false;
  201. if(entity->IsPet() && entity->GetOwner()) {
  202. map<int32, sint32>::iterator itr = m_hatelist.find(entity->GetOwner()->GetID());
  203. if(itr == m_hatelist.end()) {
  204. ownerExistsAddHate = true;
  205. }
  206. }
  207. MHateList.releasewritelock(__FUNCTION__, __LINE__);
  208. if(ownerExistsAddHate) {
  209. AddHate(entity->GetOwner(), 0);
  210. }
  211. }
  212. void Brain::ClearHate() {
  213. // Lock the hate list, we are altering the list so use a write lock
  214. MHateList.writelock(__FUNCTION__, __LINE__);
  215. map<int32, sint32>::iterator itr;
  216. for (itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  217. Spawn* spawn = m_body->GetZone()->GetSpawnByID(itr->first);
  218. if (spawn && spawn->IsEntity())
  219. {
  220. ((Entity*)spawn)->MHatedBy.lock();
  221. ((Entity*)spawn)->HatedBy.erase(m_body->GetID());
  222. ((Entity*)spawn)->MHatedBy.unlock();
  223. }
  224. }
  225. // Clear the list
  226. m_hatelist.clear();
  227. // Unlock the hate list
  228. MHateList.releasewritelock(__FUNCTION__, __LINE__);
  229. }
  230. void Brain::ClearHate(Entity* entity) {
  231. // Lock the hate list, we could potentially modify the list so use write lock
  232. MHateList.writelock(__FUNCTION__, __LINE__);
  233. // Check to see if the given entity is in the hate list
  234. if (m_hatelist.count(entity->GetID()) > 0)
  235. // Erase the entity from the hate list
  236. m_hatelist.erase(entity->GetID());
  237. entity->MHatedBy.lock();
  238. entity->HatedBy.erase(m_body->GetID());
  239. entity->MHatedBy.unlock();
  240. // Unlock the hate list
  241. MHateList.releasewritelock(__FUNCTION__, __LINE__);
  242. }
  243. Entity* Brain::GetMostHated() {
  244. map<int32, sint32>::iterator itr;
  245. int32 ret = 0;
  246. sint32 hate = 0;
  247. // Lock the hate list, not going to alter it so use a read lock
  248. MHateList.readlock(__FUNCTION__, __LINE__);
  249. if (m_hatelist.size() > 0) {
  250. // Loop through the list looking for the entity that this NPC hates the most
  251. for(itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  252. // Compare the hate value for the current iteration to our stored highest value
  253. if(itr->second > hate) {
  254. // New high value store the entity
  255. ret = itr->first;
  256. // Store the value to compare with the rest of the entities
  257. hate = itr->second;
  258. }
  259. }
  260. }
  261. // Unlock the list
  262. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  263. Entity* hated = (Entity*)GetBody()->GetZone()->GetSpawnByID(ret);
  264. // Check the reult to see if it is still alive
  265. if(hated && hated->GetHP() <= 0) {
  266. // Entity we got was dead so remove it from the list
  267. ClearHate(hated);
  268. // Call this function again now that we removed the dead entity
  269. hated = GetMostHated();
  270. }
  271. // Return our result
  272. return hated;
  273. }
  274. sint8 Brain::GetHatePercentage(Entity* entity) {
  275. float percentage = 0.0;
  276. MHateList.readlock(__FUNCTION__, __LINE__);
  277. if (entity && m_hatelist.count(entity->GetID()) > 0 && m_hatelist[entity->GetID()] > 0) {
  278. sint32 total_hate = 0;
  279. map<int32, sint32>::iterator itr;
  280. for (itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++)
  281. total_hate += itr->second;
  282. percentage = m_hatelist[entity->GetID()] / total_hate;
  283. }
  284. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  285. return (sint8)(percentage * 100);
  286. }
  287. void Brain::SendHateList(Client* client) {
  288. MHateList.readlock(__FUNCTION__, __LINE__);
  289. client->Message(CHANNEL_COLOR_YELLOW, "%s's HateList", m_body->GetName());
  290. client->Message(CHANNEL_COLOR_YELLOW, "-------------------");
  291. map<int32, sint32>::iterator itr;
  292. if (m_hatelist.size() > 0) {
  293. // Loop through the list looking for the entity that this NPC hates the most
  294. for(itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  295. Entity* ent = (Entity*)GetBody()->GetZone()->GetSpawnByID(itr->first);
  296. // Compare the hate value for the current iteration to our stored highest value
  297. if(ent) {
  298. client->Message(CHANNEL_COLOR_YELLOW, "%s : %i", ent->GetName(), itr->second);
  299. }
  300. else {
  301. client->Message(CHANNEL_COLOR_YELLOW, "%u (cannot identity spawn id->entity) : %i", itr->first, itr->second);
  302. }
  303. }
  304. }
  305. client->Message(CHANNEL_COLOR_YELLOW, "-------------------");
  306. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  307. }
  308. vector<Entity*>* Brain::GetHateList() {
  309. vector<Entity*>* ret = new vector<Entity*>;
  310. map<int32, sint32>::iterator itr;
  311. // Lock the list
  312. MHateList.readlock(__FUNCTION__, __LINE__);
  313. // Loop over the list storing the values into the new list
  314. for (itr = m_hatelist.begin(); itr != m_hatelist.end(); itr++) {
  315. Entity* ent = (Entity*)GetBody()->GetZone()->GetSpawnByID(itr->first);
  316. if (ent)
  317. ret->push_back(ent);
  318. }
  319. // Unlock the list
  320. MHateList.releasereadlock(__FUNCTION__, __LINE__);
  321. // Return the copy of the list
  322. return ret;
  323. }
  324. void Brain::MoveCloser(Spawn* target) {
  325. if (target && m_body->GetFollowTarget() != target)
  326. m_body->SetFollowTarget(target, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat());
  327. if (m_body->GetFollowTarget() && !m_body->following) {
  328. m_body->CalculateRunningLocation(true);
  329. //m_body->ClearRunningLocations();
  330. m_body->following = true;
  331. }
  332. }
  333. bool Brain::ProcessSpell(Entity* target, float distance) {
  334. if(rand()%100 > m_body->GetCastPercentage() || m_body->IsStifled() || m_body->IsFeared())
  335. return false;
  336. Spell* spell = m_body->GetNextSpell(target, distance);
  337. if(spell){
  338. Spawn* spell_target = 0;
  339. if(spell->GetSpellData()->friendly_spell == 1){
  340. vector<Spawn*>* group = m_body->GetSpawnGroup();
  341. if(group && group->size() > 0){
  342. vector<Spawn*>::iterator itr;
  343. for(itr = group->begin(); itr != group->end(); itr++){
  344. if((!spell_target && (*itr)->GetHP() > 0 && (*itr)->GetHP() < (*itr)->GetTotalHP()) || (spell_target && (*itr)->GetHP() > 0 && spell_target->GetHP() > (*itr)->GetHP()))
  345. spell_target = *itr;
  346. }
  347. }
  348. if(!spell_target)
  349. spell_target = m_body;
  350. safe_delete(group);
  351. }
  352. else
  353. spell_target = target;
  354. BrainCastSpell(spell, spell_target, false);
  355. return true;
  356. }
  357. return false;
  358. }
  359. bool Brain::BrainCastSpell(Spell* spell, Spawn* cast_on, bool calculate_run_loc) {
  360. if (spell) {
  361. if(calculate_run_loc) {
  362. m_body->CalculateRunningLocation(true);
  363. }
  364. m_body->GetZone()->ProcessSpell(spell, m_body, cast_on);
  365. m_spellRecovery = (int32)(Timer::GetCurrentTime2() + (spell->GetSpellData()->cast_time * 10) + (spell->GetSpellData()->recovery * 10) + 2000);
  366. return true;
  367. }
  368. return false;
  369. }
  370. bool Brain::CheckBuffs() {
  371. if (!m_body->GetZone()->GetSpellProcess() || m_body->EngagedInCombat() || m_body->IsCasting() || m_body->IsMezzedOrStunned() || !m_body->Alive() || m_body->IsStifled() || !HasRecovered())
  372. return false;
  373. Spell* spell = m_body->GetNextBuffSpell(m_body);
  374. bool casted_on = false;
  375. if(!(casted_on = BrainCastSpell(spell, m_body)) && m_body->IsNPC() && ((NPC*)m_body)->HasSpells()) {
  376. Spawn* target = nullptr;
  377. vector<Spawn*>* group = m_body->GetSpawnGroup();
  378. if(group && group->size() > 0){
  379. vector<Spawn*>::iterator itr;
  380. for(itr = group->begin(); itr != group->end(); itr++){
  381. Spawn* spawn = (*itr);
  382. if(spawn->IsEntity() && spawn != m_body) {
  383. if(target) {
  384. Spell* spell = m_body->GetNextBuffSpell(spawn);
  385. SpellEffects* se = ((Entity*)spawn)->GetSpellEffect(spell->GetSpellData()->id);
  386. if(!se && BrainCastSpell(spell, spawn)) {
  387. casted_on = true;
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. }
  394. safe_delete(group);
  395. }
  396. return casted_on;
  397. }
  398. void Brain::ProcessMelee(Entity* target, float distance) {
  399. if(distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  400. MoveCloser((Spawn*)target);
  401. else {
  402. if (target) {
  403. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is within melee range of %s.", m_body->GetName(), target->GetName());
  404. if (m_body->AttackAllowed(target)) {
  405. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is allowed to attack %s.", m_body->GetName(), target->GetName());
  406. if (m_body->PrimaryWeaponReady() && !m_body->IsDazed() && !m_body->IsFeared()) {
  407. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s swings its primary weapon at %s.", m_body->GetName(), target->GetName());
  408. m_body->SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  409. m_body->MeleeAttack(target, distance, true);
  410. m_body->GetZone()->CallSpawnScript(m_body, SPAWN_SCRIPT_AUTO_ATTACK_TICK, target);
  411. }
  412. if (m_body->SecondaryWeaponReady() && !m_body->IsDazed()) {
  413. m_body->SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  414. m_body->MeleeAttack(target, distance, false);
  415. }
  416. }
  417. }
  418. }
  419. }
  420. bool Brain::HasRecovered() {
  421. if(m_spellRecovery > Timer::GetCurrentTime2())
  422. return false;
  423. m_spellRecovery = 0;
  424. return true;
  425. }
  426. void Brain::AddToEncounter(Entity* entity) {
  427. // If player pet then set the entity to the pets owner
  428. if (entity->IsPet() && entity->GetOwner() && !entity->IsBot()) {
  429. m_encounter.push_back(entity->GetID());
  430. entity = entity->GetOwner();
  431. }
  432. else if(entity->HasPet() && entity->GetPet()) {
  433. m_encounter.push_back(entity->GetPet()->GetID());
  434. }
  435. // If player or bot then get the group
  436. int32 group_id = 0;
  437. if (entity->IsPlayer() || entity->IsBot()) {
  438. m_playerInEncounter = true;
  439. if (entity->GetGroupMemberInfo())
  440. group_id = entity->GetGroupMemberInfo()->group_id;
  441. }
  442. // Insert the entity into the encounter list, if there is a group add all group members as well
  443. // TODO: add raid members
  444. MEncounter.writelock(__FUNCTION__, __LINE__);
  445. if (group_id > 0) {
  446. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  447. deque<GroupMemberInfo*>::iterator itr;
  448. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  449. if (group)
  450. {
  451. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  452. deque<GroupMemberInfo*>* members = group->GetMembers();
  453. for (itr = members->begin(); itr != members->end(); itr++) {
  454. if ((*itr)->member)
  455. {
  456. m_encounter.push_back((*itr)->member->GetID());
  457. if((*itr)->client) {
  458. m_encounter_playerlist.insert(make_pair((*itr)->client->GetPlayer()->GetCharacterID(), (*itr)->client->GetPlayer()->GetID()));
  459. }
  460. }
  461. }
  462. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  463. }
  464. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  465. }
  466. else {
  467. m_encounter.push_back(entity->GetID());
  468. if (entity->IsPlayer())
  469. {
  470. Player* plyr = (Player*)entity;
  471. m_encounter_playerlist.insert(make_pair(plyr->GetCharacterID(), entity->GetID()));
  472. }
  473. }
  474. MEncounter.releasewritelock(__FUNCTION__, __LINE__);
  475. }
  476. bool Brain::CheckLootAllowed(Entity* entity) {
  477. bool ret = false;
  478. vector<int32>::iterator itr;
  479. if(m_body)
  480. {
  481. if(rule_manager.GetGlobalRule(R_Loot, AllowChestUnlockByDropTime)->GetInt8()
  482. && m_body->GetChestDropTime() > 0 && Timer::GetCurrentTime2() >= m_body->GetChestDropTime()+(rule_manager.GetGlobalRule(R_Loot, ChestUnlockedTimeDrop)->GetInt32()*1000))
  483. return true;
  484. if(rule_manager.GetGlobalRule(R_Loot, AllowChestUnlockByTrapTime)->GetInt8()
  485. && m_body->GetTrapOpenedTime() > 0 && Timer::GetCurrentTime2() >= m_body->GetChestDropTime()+(rule_manager.GetGlobalRule(R_Loot, ChestUnlockedTimeTrap)->GetInt32()*1000))
  486. return true;
  487. }
  488. // Check the encounter list to see if the given entity is in it, if so return true.
  489. MEncounter.readlock(__FUNCTION__, __LINE__);
  490. if (entity->IsPlayer())
  491. {
  492. Player* plyr = (Player*)entity;
  493. map<int32, int32>::iterator itr = m_encounter_playerlist.find(plyr->GetCharacterID());
  494. if (itr != m_encounter_playerlist.end())
  495. {
  496. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  497. return true;
  498. }
  499. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  500. return false;
  501. }
  502. for (itr = m_encounter.begin(); itr != m_encounter.end(); itr++) {
  503. if ((*itr) == entity->GetID()) {
  504. // found the entity in the encounter list, set return value to true and break the loop
  505. ret = true;
  506. break;
  507. }
  508. }
  509. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  510. return ret;
  511. }
  512. int8 Brain::GetEncounterSize() {
  513. int8 ret = 0;
  514. MEncounter.readlock(__FUNCTION__, __LINE__);
  515. ret = (int8)m_encounter.size();
  516. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  517. return ret;
  518. }
  519. vector<int32>* Brain::GetEncounter() {
  520. vector<int32>* ret = new vector<int32>;
  521. vector<int32>::iterator itr;
  522. // Lock the list
  523. MEncounter.readlock(__FUNCTION__, __LINE__);
  524. // Loop over the list storing the values into the new list
  525. for (itr = m_encounter.begin(); itr != m_encounter.end(); itr++)
  526. ret->push_back(*itr);
  527. // Unlock the list
  528. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  529. // Return the copy of the list
  530. return ret;
  531. }
  532. bool Brain::IsPlayerInEncounter(int32 char_id) {
  533. bool ret = false;
  534. MEncounter.readlock(__FUNCTION__, __LINE__);
  535. std::map<int32,int32>::iterator itr = m_encounter_playerlist.find(char_id);
  536. if(itr != m_encounter_playerlist.end()) {
  537. ret = true;
  538. }
  539. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  540. return ret;
  541. }
  542. bool Brain::IsEntityInEncounter(int32 id) {
  543. bool ret = false;
  544. MEncounter.readlock(__FUNCTION__, __LINE__);
  545. vector<int32>::iterator itr;
  546. for (itr = m_encounter.begin(); itr != m_encounter.end(); itr++) {
  547. if ((*itr) == id) {
  548. // found the entity in the encounter list, set return value to true and break the loop
  549. ret = true;
  550. break;
  551. }
  552. }
  553. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  554. return ret;
  555. }
  556. void Brain::ClearEncounter() {
  557. MEncounter.writelock(__FUNCTION__, __LINE__);
  558. m_encounter.clear();
  559. m_encounter_playerlist.clear();
  560. m_playerInEncounter = false;
  561. MEncounter.releasewritelock(__FUNCTION__, __LINE__);
  562. }
  563. void Brain::SendEncounterList(Client* client) {
  564. client->Message(CHANNEL_COLOR_YELLOW, "%s's EncounterList", m_body->GetName());
  565. client->Message(CHANNEL_COLOR_YELLOW, "-------------------");
  566. vector<int32>::iterator itr;
  567. // Check the encounter list to see if the given entity is in it, if so return true.
  568. MEncounter.readlock(__FUNCTION__, __LINE__);
  569. for (itr = m_encounter.begin(); itr != m_encounter.end(); itr++) {
  570. Entity* ent = (Entity*)GetBody()->GetZone()->GetSpawnByID((*itr));
  571. // Compare the hate value for the current iteration to our stored highest value
  572. if(ent) {
  573. client->Message(CHANNEL_COLOR_YELLOW, "%s", ent->GetName());
  574. }
  575. else {
  576. client->Message(CHANNEL_COLOR_YELLOW, "%u (cannot identity spawn id->entity)", (*itr));
  577. }
  578. }
  579. client->Message(CHANNEL_COLOR_YELLOW, "-------------------");
  580. MEncounter.releasereadlock(__FUNCTION__, __LINE__);
  581. }
  582. /* Example of how to extend the default AI */
  583. CombatPetBrain::CombatPetBrain(NPC* body) : Brain(body) {
  584. // Make sure to have the " : Brain(body)" so it calls the parent class constructor
  585. // to set up the AI properly
  586. }
  587. CombatPetBrain::~CombatPetBrain() {
  588. }
  589. void CombatPetBrain::Think() {
  590. // We are extending the base brain so make sure to call the parent Think() function.
  591. // If we want to override then we could remove Brain::Think()
  592. Brain::Think();
  593. // All this Brain does is make the pet follow its owner, the combat comes from the default brain
  594. if (GetBody()->EngagedInCombat() || !GetBody()->IsPet() || GetBody()->IsMezzedOrStunned())
  595. return;
  596. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Pet AI code called for %s", GetBody()->GetName());
  597. // If owner is a player and player has stay set then return out
  598. if (GetBody()->GetOwner() && GetBody()->GetOwner()->IsPlayer() && ((Player*)GetBody()->GetOwner())->GetInfoStruct()->get_pet_movement() == 1)
  599. return;
  600. // Set target to owner
  601. Entity* target = GetBody()->GetOwner();
  602. GetBody()->SetTarget(target);
  603. // Get distance from the owner
  604. float distance = GetBody()->GetDistance(target);
  605. // If out of melee range then move closer
  606. if (distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  607. MoveCloser((Spawn*)target);
  608. }
  609. /* Example of how to override the default AI */
  610. NonCombatPetBrain::NonCombatPetBrain(NPC* body) : Brain(body) {
  611. // Make sure to have the " : Brain(body)" so it calls the parent class constructor
  612. // to set up the AI properly
  613. }
  614. NonCombatPetBrain::~NonCombatPetBrain() {
  615. }
  616. void NonCombatPetBrain::Think() {
  617. // All this Brain does is make the pet follow its owner
  618. if (!GetBody()->IsPet() || GetBody()->IsMezzedOrStunned())
  619. return;
  620. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Pet AI code called for %s", GetBody()->GetName());
  621. // Set target to owner
  622. Entity* target = GetBody()->GetOwner();
  623. GetBody()->SetTarget(target);
  624. // Get distance from the owner
  625. float distance = GetBody()->GetDistance(target);
  626. // If out of melee range then move closer
  627. if (distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  628. MoveCloser((Spawn*)target);
  629. }
  630. BlankBrain::BlankBrain(NPC* body) : Brain(body) {
  631. // Make sure to have the " : Brain(body)" so it calls the parent class constructor
  632. // to set up the AI properly
  633. SetTick(50000);
  634. }
  635. BlankBrain::~BlankBrain() {
  636. }
  637. void BlankBrain::Think() {
  638. }
  639. LuaBrain::LuaBrain(NPC* body) : Brain(body) {
  640. }
  641. LuaBrain::~LuaBrain() {
  642. }
  643. void LuaBrain::Think() {
  644. if (!lua_interface)
  645. return;
  646. const char* script = GetBody()->GetSpawnScript();
  647. if(script) {
  648. if (!lua_interface->RunSpawnScript(script, "Think", GetBody(), GetBody()->GetTarget())) {
  649. lua_interface->LogError("LUA LuaBrain error: was unable to call the Think function in the spawn script (%s)", script);
  650. }
  651. }
  652. else {
  653. LogWrite(NPC_AI__ERROR, 0, "NPC_AI", "Lua brain set on a spawn that doesn't have a script...");
  654. }
  655. }
  656. DumbFirePetBrain::DumbFirePetBrain(NPC* body, Entity* target, int32 expire_time) : Brain(body) {
  657. m_expireTime = Timer::GetCurrentTime2() + expire_time;
  658. AddHate(target, INT_MAX);
  659. }
  660. DumbFirePetBrain::~DumbFirePetBrain() {
  661. }
  662. void DumbFirePetBrain::AddHate(Entity* entity, sint32 hate) {
  663. if (!GetMostHated())
  664. Brain::AddHate(entity, hate);
  665. }
  666. void DumbFirePetBrain::Think() {
  667. Entity* target = GetMostHated();
  668. if (target) {
  669. if (!GetBody()->IsMezzedOrStunned()) {
  670. // Set the NPC's target to the most hated entity if it is not already.
  671. if (GetBody()->GetTarget() != target) {
  672. GetBody()->SetTarget(target);
  673. GetBody()->FaceTarget(target, false);
  674. }
  675. // target needs to be identified before combat setting
  676. // If the NPC is not in combat then put them in combat
  677. if (!GetBody()->EngagedInCombat()) {
  678. //GetBody()->ClearRunningLocations();
  679. GetBody()->CalculateRunningLocation(true);
  680. GetBody()->InCombat(true);
  681. }
  682. float distance = GetBody()->GetDistance(target);
  683. if(GetBody()->CheckLoS(target) && !GetBody()->IsCasting() && (!HasRecovered() || !ProcessSpell(target, distance))) {
  684. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "%s is attempting melee on %s.", GetBody()->GetName(), target->GetName());
  685. GetBody()->FaceTarget(target, false);
  686. ProcessMelee(target, distance);
  687. }
  688. }
  689. }
  690. else {
  691. // No hated target or time expired, kill this mob
  692. if (GetBody()->GetHP() > 0) {
  693. GetBody()->KillSpawn(GetBody());
  694. LogWrite(NPC_AI__DEBUG, 7, "NPC AI", "Dumbfire being killed because there is no target.");
  695. }
  696. }
  697. if (Timer::GetCurrentTime2() > m_expireTime) {
  698. if (GetBody()->GetHP() > 0) {
  699. GetBody()->KillSpawn(GetBody());
  700. LogWrite(NPC_AI__DEBUG, 7, "NPC AI", "Dumbfire being killed because timer expired.");
  701. }
  702. }
  703. }