NPC_AI.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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);
  100. /* Helper functions*/
  101. /// <summary>Gets the NPC this brain controls</summary>
  102. /// <returns>The NPC this brain controls</returns>
  103. NPC* GetBody() { return m_body; }
  104. /// <summary>Checks to see if the NPC can cast</summary>
  105. /// <returns>True if the NPC can cast</returns>
  106. bool HasRecovered();
  107. /// <summary>Tells the NPC to move closer to the given target</summary>
  108. /// <param name="target">The target to move closer to</param>
  109. void MoveCloser(Spawn* target);
  110. protected:
  111. // m_body = the npc this brain controls
  112. NPC* m_body;
  113. // m_spellRecovery = time stamp for when the npc can cast again
  114. int32 m_spellRecovery;
  115. private:
  116. // MHateList = mutex to lock and unlock the hate list
  117. Mutex MHateList;
  118. // m_hatelist = the list that stores all the hate,
  119. // entity is the entity this npc hates and the int32 is the value for how much we hate the entity
  120. map<int32, sint32> m_hatelist;
  121. // m_lastTick = the last time we ran this brain
  122. int32 m_lastTick;
  123. // m_tick = the amount of time between Think() calls in milliseconds
  124. int16 m_tick;
  125. // m_encounter = list of players (entities) that will get a reward (xp/loot) for killing this npc
  126. vector<int32> m_encounter;
  127. map<int32, int32> m_encounter_playerlist;
  128. // MEncounter = mutex to lock and unlock the encounter list
  129. Mutex MEncounter;
  130. //m_playerInEncounter = true if a player is added to the encounter
  131. bool m_playerInEncounter;
  132. };
  133. // Extension of the default brain for combat pets
  134. class CombatPetBrain : public Brain {
  135. public:
  136. CombatPetBrain(NPC* body);
  137. virtual ~CombatPetBrain();
  138. void Think();
  139. };
  140. class NonCombatPetBrain : public Brain {
  141. public:
  142. NonCombatPetBrain(NPC* body);
  143. virtual ~NonCombatPetBrain();
  144. void Think();
  145. };
  146. class BlankBrain : public Brain {
  147. public:
  148. BlankBrain(NPC* body);
  149. virtual ~BlankBrain();
  150. void Think();
  151. };
  152. class LuaBrain : public Brain {
  153. public:
  154. LuaBrain(NPC* body);
  155. virtual ~LuaBrain();
  156. void Think();
  157. };
  158. class DumbFirePetBrain : public Brain {
  159. public:
  160. DumbFirePetBrain(NPC* body, Entity* target, int32 expire_time);
  161. virtual ~DumbFirePetBrain();
  162. void Think();
  163. void AddHate(Entity* entity, sint32 hate);
  164. private:
  165. int32 m_expireTime;
  166. };
  167. #endif