9
3

NPC_AI.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. ///<summary>Gets a list of all the entities in the hate list</summary>
  63. vector<Entity*>* GetHateList();
  64. /* Combat related functions */
  65. bool BrainCastSpell(Spell* spell, Spawn* cast_on, bool calculate_run_loc = true);
  66. /// <summary></summary>
  67. /// <param name=""></param>
  68. /// <param name=""></param>
  69. virtual bool ProcessSpell(Entity* target, float distance);
  70. /// <summary></summary>
  71. /// <returns>True if a buff starts casting</returns>
  72. bool CheckBuffs();
  73. /// <summary>Has the NPC make a melee attack</summary>
  74. /// <param name="target">The target to attack</param>
  75. /// <param name="distance">The current distance from the target</param>
  76. void ProcessMelee(Entity* target, float distance);
  77. /* Encounter related functions */
  78. /// <summary>Adds the given entity and its group and raid members to the encounter list</summary>
  79. /// <param name="entity">Entity we are adding to the encounter list</param>
  80. void AddToEncounter(Entity* entity);
  81. /// <summary>Checks to see if the given entity can loot the corpse</summary>
  82. /// <param name="entity">Entity trying to loot</param>
  83. /// <returns>True if the entity can loot</returns>
  84. bool CheckLootAllowed(Entity* entity);
  85. /// <summary>Gets the size of the encounter list</summary>
  86. /// <returns>The size of the list as an int8</returns>
  87. int8 GetEncounterSize();
  88. /// <summary>Clears the encounter list</summary>
  89. void ClearEncounter();
  90. /// <summary>Gets a copy of the encounter list</summary>
  91. /// <returns>A copy of the encounter list as a vector<Entity*>*</returns>
  92. vector<int32>* GetEncounter();
  93. /// <summary>Checks to see if a player is in the encounter</summary>
  94. /// <returns>True if the encounter list contains a player</returns>
  95. bool PlayerInEncounter() { return m_playerInEncounter; }
  96. /* Helper functions*/
  97. /// <summary>Gets the NPC this brain controls</summary>
  98. /// <returns>The NPC this brain controls</returns>
  99. NPC* GetBody() { return m_body; }
  100. /// <summary>Checks to see if the NPC can cast</summary>
  101. /// <returns>True if the NPC can cast</returns>
  102. bool HasRecovered();
  103. /// <summary>Tells the NPC to move closer to the given target</summary>
  104. /// <param name="target">The target to move closer to</param>
  105. void MoveCloser(Spawn* target);
  106. protected:
  107. // m_body = the npc this brain controls
  108. NPC* m_body;
  109. // m_spellRecovery = time stamp for when the npc can cast again
  110. int32 m_spellRecovery;
  111. private:
  112. // MHateList = mutex to lock and unlock the hate list
  113. Mutex MHateList;
  114. // m_hatelist = the list that stores all the hate,
  115. // entity is the entity this npc hates and the int32 is the value for how much we hate the entity
  116. map<int32, sint32> m_hatelist;
  117. // m_lastTick = the last time we ran this brain
  118. int32 m_lastTick;
  119. // m_tick = the amount of time between Think() calls in milliseconds
  120. int16 m_tick;
  121. // m_encounter = list of players (entities) that will get a reward (xp/loot) for killing this npc
  122. vector<int32> m_encounter;
  123. map<int32, int32> m_encounter_playerlist;
  124. // MEncounter = mutex to lock and unlock the encounter list
  125. Mutex MEncounter;
  126. //m_playerInEncounter = true if a player is added to the encounter
  127. bool m_playerInEncounter;
  128. };
  129. // Extension of the default brain for combat pets
  130. class CombatPetBrain : public Brain {
  131. public:
  132. CombatPetBrain(NPC* body);
  133. virtual ~CombatPetBrain();
  134. void Think();
  135. };
  136. class NonCombatPetBrain : public Brain {
  137. public:
  138. NonCombatPetBrain(NPC* body);
  139. virtual ~NonCombatPetBrain();
  140. void Think();
  141. };
  142. class BlankBrain : public Brain {
  143. public:
  144. BlankBrain(NPC* body);
  145. virtual ~BlankBrain();
  146. void Think();
  147. };
  148. class LuaBrain : public Brain {
  149. public:
  150. LuaBrain(NPC* body);
  151. virtual ~LuaBrain();
  152. void Think();
  153. };
  154. class DumbFirePetBrain : public Brain {
  155. public:
  156. DumbFirePetBrain(NPC* body, Entity* target, int32 expire_time);
  157. virtual ~DumbFirePetBrain();
  158. void Think();
  159. void AddHate(Entity* entity, sint32 hate);
  160. private:
  161. int32 m_expireTime;
  162. };
  163. #endif