NPC_AI.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef __NPC_AI_H__
  17. #define __NPC_AI_H__
  18. #include "NPC.h"
  19. #include <vector>
  20. #include <map>
  21. using namespace std;
  22. class Brain {
  23. public:
  24. Brain(NPC* npc);
  25. virtual ~Brain();
  26. /// <summary>The main loop for the brain. This will do all the AI work</summary>
  27. virtual void Think();
  28. /* Timer related functions */
  29. /// <summary>Gets the time between calls to Think()</summary>
  30. /// <returns>Time in miliseconds between calls to Think()</returns>
  31. int16 Tick() { return m_tick; }
  32. /// <summary>Sets the time between calls to Think()</summary>
  33. /// <param name="time">Time in miliseconds</param>
  34. void SetTick(int16 time) { m_tick = time; }
  35. /// <summary>Gets the timestamp of the last call to Think()</summary>
  36. /// <returns>Timestamp of the last call to Think()</returns>
  37. int32 LastTick() { return m_lastTick; }
  38. /// <summary>Sets the last tick to the given time</summary>
  39. /// <param name="time">The time to set the last tick to</param>
  40. void SetLastTick(int32 time) { m_lastTick = time; }
  41. /* Hate related functions */
  42. /// <summary>Gets the amount of hate this npc has towards the given entity</summary>
  43. /// <param name="entity">The entity to check</param>
  44. /// <returns>The amount of hate towards the given entity</returns>
  45. sint32 GetHate(Entity* entity);
  46. /// <summary>Add hate for the given entity to this NPC</summary>
  47. /// <param name="entity">The entity we are adding to this NPC's hate list</param>
  48. /// <param name="hate">The amount of hate to add</param>
  49. virtual void AddHate(Entity* entity, sint32 hate);
  50. /// <summary>Completely clears the hate list for this npc</summary>
  51. void ClearHate();
  52. /// <summary>Removes the given entity from this NPC's hate list</summary>
  53. /// <param name="entity">Entity to remove from this NPC's hate list</param>
  54. void ClearHate(Entity* entity);
  55. /// <summary>Get the entity this NPC hates the most</summary>
  56. /// <returns>The entity this NPC hates the most</returns>
  57. Entity* GetMostHated();
  58. /// <summary>Gets a percentage of hate owned by the given entity</summary>
  59. /// <param name="entity">Entity to get the percentage for</param>
  60. /// <returns>Percentage of hate as a sint8</returns>
  61. sint8 GetHatePercentage(Entity* entity);
  62. void SendHateList(Client* client);
  63. ///<summary>Gets a list of all the entities in the hate list</summary>
  64. vector<Entity*>* GetHateList();
  65. /* Combat related functions */
  66. bool BrainCastSpell(Spell* spell, Spawn* cast_on, bool calculate_run_loc = true);
  67. /// <summary></summary>
  68. /// <param name=""></param>
  69. /// <param name=""></param>
  70. virtual bool ProcessSpell(Entity* target, float distance);
  71. /// <summary></summary>
  72. /// <returns>True if a buff starts casting</returns>
  73. bool CheckBuffs();
  74. /// <summary>Has the NPC make a melee attack</summary>
  75. /// <param name="target">The target to attack</param>
  76. /// <param name="distance">The current distance from the target</param>
  77. void ProcessMelee(Entity* target, float distance);
  78. /* Encounter related functions */
  79. /// <summary>Adds the given entity and its group and raid members to the encounter list</summary>
  80. /// <param name="entity">Entity we are adding to the encounter list</param>
  81. void AddToEncounter(Entity* entity);
  82. /// <summary>Checks to see if the given entity can loot the corpse</summary>
  83. /// <param name="entity">Entity trying to loot</param>
  84. /// <returns>True if the entity can loot</returns>
  85. bool CheckLootAllowed(Entity* entity);
  86. /// <summary>Gets the size of the encounter list</summary>
  87. /// <returns>The size of the list as an int8</returns>
  88. int8 GetEncounterSize();
  89. /// <summary>Clears the encounter list</summary>
  90. void ClearEncounter();
  91. void SendEncounterList(Client* client);
  92. /// <summary>Gets a copy of the encounter list</summary>
  93. /// <returns>A copy of the encounter list as a vector<Entity*>*</returns>
  94. vector<int32>* GetEncounter();
  95. /// <summary>Checks to see if a player is in the encounter</summary>
  96. /// <returns>True if the encounter list contains a player</returns>
  97. bool PlayerInEncounter() { return m_playerInEncounter; }
  98. bool IsPlayerInEncounter(int32 char_id);
  99. bool IsEntityInEncounter(int32 id, bool skip_read_lock = false);
  100. int32 CountPlayerBotInEncounter();
  101. bool AddToEncounter(int32 id);
  102. /* Helper functions*/
  103. /// <summary>Gets the NPC this brain controls</summary>
  104. /// <returns>The NPC this brain controls</returns>
  105. NPC* GetBody() { return m_body; }
  106. /// <summary>Checks to see if the NPC can cast</summary>
  107. /// <returns>True if the NPC can cast</returns>
  108. bool HasRecovered();
  109. /// <summary>Tells the NPC to move closer to the given target</summary>
  110. /// <param name="target">The target to move closer to</param>
  111. void MoveCloser(Spawn* target);
  112. protected:
  113. // m_body = the npc this brain controls
  114. NPC* m_body;
  115. // m_spellRecovery = time stamp for when the npc can cast again
  116. int32 m_spellRecovery;
  117. private:
  118. // MHateList = mutex to lock and unlock the hate list
  119. Mutex MHateList;
  120. // m_hatelist = the list that stores all the hate,
  121. // entity is the entity this npc hates and the int32 is the value for how much we hate the entity
  122. map<int32, sint32> m_hatelist;
  123. // m_lastTick = the last time we ran this brain
  124. int32 m_lastTick;
  125. // m_tick = the amount of time between Think() calls in milliseconds
  126. int16 m_tick;
  127. // m_encounter = list of players (entities) that will get a reward (xp/loot) for killing this npc
  128. vector<int32> m_encounter;
  129. map<int32, int32> m_encounter_playerlist;
  130. // MEncounter = mutex to lock and unlock the encounter list
  131. Mutex MEncounter;
  132. //m_playerInEncounter = true if a player is added to the encounter
  133. bool m_playerInEncounter;
  134. };
  135. // Extension of the default brain for combat pets
  136. class CombatPetBrain : public Brain {
  137. public:
  138. CombatPetBrain(NPC* body);
  139. virtual ~CombatPetBrain();
  140. void Think();
  141. };
  142. class NonCombatPetBrain : public Brain {
  143. public:
  144. NonCombatPetBrain(NPC* body);
  145. virtual ~NonCombatPetBrain();
  146. void Think();
  147. };
  148. class BlankBrain : public Brain {
  149. public:
  150. BlankBrain(NPC* body);
  151. virtual ~BlankBrain();
  152. void Think();
  153. };
  154. class LuaBrain : public Brain {
  155. public:
  156. LuaBrain(NPC* body);
  157. virtual ~LuaBrain();
  158. void Think();
  159. };
  160. class DumbFirePetBrain : public Brain {
  161. public:
  162. DumbFirePetBrain(NPC* body, Entity* target, int32 expire_time);
  163. virtual ~DumbFirePetBrain();
  164. void Think();
  165. void AddHate(Entity* entity, sint32 hate);
  166. private:
  167. int32 m_expireTime;
  168. };
  169. #endif