PlayerGroups.h 7.4 KB

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