PlayerGroups.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 __PLAYERGROUPS_H__
  17. #define __PLAYERGROUPS_H__
  18. #include <deque>
  19. #include <map>
  20. #include <mutex>
  21. #include <shared_mutex>
  22. #include "Spells.h"
  23. #include "../common/types.h"
  24. #include "Entity.h"
  25. using namespace std;
  26. // GroupOptions isn't used yet
  27. struct GroupOptions{
  28. int8 loot_method;
  29. int8 loot_items_rarity;
  30. int8 auto_split;
  31. int8 default_yell;
  32. int8 group_autolock;
  33. int8 solo_autolock;
  34. };
  35. /// <summary>All the generic info for the group window, plus a client pointer for players</summary>
  36. struct GroupMemberInfo {
  37. int32 group_id;
  38. string name;
  39. string zone;
  40. sint32 hp_current;
  41. sint32 hp_max;
  42. sint32 power_current;
  43. sint32 power_max;
  44. int16 level_current;
  45. int16 level_max;
  46. int8 race_id;
  47. int8 class_id;
  48. bool leader;
  49. Client* client;
  50. Entity* member;
  51. int32 mentor_target_char_id;
  52. };
  53. /// <summary>Represents a players group in game</summary>
  54. class PlayerGroup {
  55. public:
  56. PlayerGroup(int32 id);
  57. ~PlayerGroup();
  58. /// <summary>Adds a new member to the players group</summary>
  59. /// <param name='member'>Entity to add to the group, can be a Player or NPC</param>
  60. /// <returns>True if the member was added</returns>
  61. bool AddMember(Entity* member);
  62. /// <summary>Removes a member from the players group</summary>
  63. /// <param name='member'>Entity to remove from the player group</param>
  64. /// <returns>True if the member was removed</param>
  65. bool RemoveMember(Entity* member);
  66. /// <summary>Removes all members from the group and destroys the group</summary>
  67. void Disband();
  68. /// <summary>Sends updates to all the clients in the group</summary>
  69. /// <param name='exclude'>Client to exclude from the update</param>
  70. void SendGroupUpdate(Client* exclude = 0);
  71. /// <summary>Gets the total number of members in the group</summary>
  72. /// <returns>int32, number of members in the group</returns>
  73. int32 Size() { return m_members.size(); }
  74. /// <summary>Gets a pointer to the list of members</summary>
  75. /// <returns>deque pointer</returns>
  76. deque<GroupMemberInfo*>* GetMembers() { return &m_members; }
  77. void SimpleGroupMessage(const char* message);
  78. void GroupChatMessage(Spawn* from, int32 language, const char* message);
  79. bool MakeLeader(Entity* new_leader);
  80. bool ShareQuestWithGroup(Client* quest_sharer, Quest* quest);
  81. void RemoveClientReference(Client* remove);
  82. void UpdateGroupMemberInfo(Entity* ent, bool groupMembersLocked=false);
  83. Entity* GetGroupMemberByPosition(Entity* seeker, int32 mapped_position);
  84. Mutex MGroupMembers; // Mutex for the group members
  85. private:
  86. int32 m_id; // ID of this group
  87. deque<GroupMemberInfo*> m_members; // List of members in this group
  88. };
  89. /// <summary>Responsible for managing all the player groups in the world</summary>
  90. class PlayerGroupManager {
  91. public:
  92. PlayerGroupManager();
  93. ~PlayerGroupManager();
  94. /// <summary>Adds a member to a group</summary>
  95. /// <param name='group_id'>ID of the group to add a member to</param>
  96. /// <param name='member'>Entity* to add to the group</param>
  97. /// <returns>True if the member was added to the group</returns>
  98. bool AddGroupMember(int32 group_id, Entity* member);
  99. /// <summary>Removes a member from a group</summary>
  100. /// <param name='group_id'>ID of the group to remove a member from</param>
  101. /// <param name='member'>Entity* to remove from the group</param>
  102. /// <returns>True if the member was removed from the group</returns>
  103. bool RemoveGroupMember(int32 group_id, Entity* member);
  104. /// <summary>Creates a new group with the provided Entity* as the leader</summary>
  105. /// <param name='leader'>The Entity* that will be the leader of the group</param>
  106. void NewGroup(Entity* leader);
  107. /// <summary>Removes the group from the group manager</summary>
  108. /// <param name='group_id'>ID of the group to remove</param>
  109. void RemoveGroup(int32 group_id);
  110. /// <summary>Handles a player inviting another player or NPC to a group</summary>
  111. /// <param name='leader'>Player that sent the invite</param>
  112. /// <param name='member'>Player or NPC that is the target of the invite</param>
  113. /// <returns>Error code if invite was unsuccessful, 0 if successful</returns>
  114. int8 Invite(Player* leader, Entity* member);
  115. /// <summary>Handles accepting of a group invite</summary>
  116. /// <param name='member'>Entity* that is accepting the invite</param>
  117. /// <returns>Error code if accepting the invite failed, 0 if successful<returns>
  118. int8 AcceptInvite(Entity* member);
  119. /// <summary>Handles declining of a group invite</summary>
  120. /// <param name='member'>Entity* that is declining the invite</param>
  121. void DeclineInvite(Entity* member);
  122. /// <summary>Checks to see if there is a group with the given id in the group manager</summary>
  123. /// <param name='group_id'>ID to check for</param>
  124. /// <returns>True if a group with the given ID is found</returns>
  125. bool IsGroupIDValid(int32 group_id);
  126. /// <summary>Send updates to all the clients in the group</summary>
  127. /// <param name='group_id'>ID of the group to send updates to</param>
  128. /// <param name='exclude'>Client* to exclude from the update, usually the one that triggers the update</param>
  129. void SendGroupUpdate(int32 group_id, Client* exclude = 0);
  130. PlayerGroup* GetGroup(int32 group_id);
  131. /// <summary>Read locks the group list, no changes to the list should be made when using this</summary>
  132. /// <param name='function'>Name of the function called from, used for better debugging in the event of a deadlock</param>
  133. /// <param name='line'>Line number that this was called from, used for better debugging in the event of a deadlock</param>
  134. void GroupHardLock(const char* function = 0, int32 line = 0U) { MGroups.lock(); }
  135. void GroupLock(const char* function = 0, int32 line = 0U) { MGroups.lock_shared(); }
  136. /// <summary>Releases the readlock acquired from GroupLock()</summary>
  137. /// <param name='function'>Name of the function called from, used for better debugging in the event of a deadlock</param>
  138. /// <param name='line'>Line number that this was called from, used for better debugging in the event of a deadlock</param>
  139. void ReleaseGroupHardLock(const char* function = 0, int32 line = 0U) { MGroups.unlock(); }
  140. void ReleaseGroupLock(const char* function = 0, int32 line = 0U) { MGroups.unlock_shared(); }
  141. void ClearPendingInvite(Entity* member);
  142. void RemoveGroupBuffs(int32 group_id, Client* client);
  143. int32 GetGroupSize(int32 group_id);
  144. void SendGroupQuests(int32 group_id, Client* client);
  145. bool HasGroupCompletedQuest(int32 group_id, int32 quest_id);
  146. void SimpleGroupMessage(int32 group_id, const char* message);
  147. void GroupMessage(int32 group_id, const char* message, ...);
  148. void GroupChatMessage(int32 group_id, Spawn* from, int32 language, const char* message);
  149. bool MakeLeader(int32 group_id, Entity* new_leader);
  150. void UpdateGroupBuffs();
  151. bool IsInGroup(int32 group_id, Entity* member);
  152. Entity* IsPlayerInGroup(int32 group_id, int32 char_id);
  153. // TODO: Any function below this comment
  154. bool IsSpawnInGroup(int32 group_id, string name); // used in follow
  155. Player* GetGroupLeader(int32 group_id);
  156. private:
  157. int32 m_nextGroupID; // Used to generate a new unique id for new groups
  158. map<int32, PlayerGroup*> m_groups; // int32 is the group id, PlayerGroup* is a pointer to the actual group
  159. map<string, string> m_pendingInvites; // First string is the person invited to the group, second string is the leader of the group
  160. mutable std::shared_mutex MGroups; // Mutex for the group map (m_groups)
  161. Mutex MPendingInvites; // Mutex for the pending invites map (m_pendingInvites)
  162. };
  163. #endif