PlayerGroups.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. #include "PlayerGroups.h"
  17. #include "../common/Log.h"
  18. #include "World.h"
  19. #include "Spells.h"
  20. #include "LuaInterface.h"
  21. #include "Bots/Bot.h"
  22. #include "SpellProcess.h"
  23. #include "Rules/Rules.h"
  24. extern ZoneList zone_list;
  25. extern RuleManager rule_manager;
  26. /******************************************************** PlayerGroup ********************************************************/
  27. PlayerGroup::PlayerGroup(int32 id) {
  28. m_id = id;
  29. MGroupMembers.SetName("MGroupMembers");
  30. }
  31. PlayerGroup::~PlayerGroup() {
  32. Disband();
  33. }
  34. bool PlayerGroup::AddMember(Entity* member) {
  35. // Check to make sure the entity we are adding is valid
  36. if (!member) {
  37. LogWrite(GROUP__ERROR, 0, "Group", "New member is null");
  38. return false;
  39. }
  40. // Make sure entity we are adding isn't already in a group by checking if it has a GroupMemberInfo pointer
  41. if (member->GetGroupMemberInfo()) {
  42. LogWrite(GROUP__ERROR, 0, "Group", "New member (%s) already has a group", member->GetName());
  43. return false;
  44. }
  45. // Create a new GroupMemberInfo and assign it to the new member
  46. GroupMemberInfo* gmi = new GroupMemberInfo;
  47. gmi->group_id = m_id;
  48. gmi->member = member;
  49. gmi->leader = false;
  50. if (member->IsPlayer())
  51. gmi->client = member->GetZone()->GetClientBySpawn(member);
  52. else
  53. gmi->client = 0;
  54. gmi->mentor_target_char_id = 0;
  55. member->SetGroupMemberInfo(gmi);
  56. member->group_id = gmi->group_id;
  57. MGroupMembers.writelock();
  58. m_members.push_back(gmi);
  59. member->UpdateGroupMemberInfo(true, true);
  60. MGroupMembers.releasewritelock();
  61. SendGroupUpdate();
  62. return true;
  63. }
  64. bool PlayerGroup::RemoveMember(Entity* member) {
  65. GroupMemberInfo* gmi = member->GetGroupMemberInfo();
  66. if (!gmi) {
  67. return false;
  68. }
  69. bool ret = false;
  70. MGroupMembers.writelock();
  71. member->SetGroupMemberInfo(0);
  72. deque<GroupMemberInfo*>::iterator erase_itr = m_members.end();
  73. deque<GroupMemberInfo*>::iterator itr;
  74. for (itr = m_members.begin(); itr != m_members.end(); itr++) {
  75. if (gmi == *itr)
  76. erase_itr = itr;
  77. if(member->IsPlayer() && (*itr)->mentor_target_char_id == ((Player*)member)->GetCharacterID() && (*itr)->client)
  78. {
  79. (*itr)->mentor_target_char_id = 0;
  80. (*itr)->client->GetPlayer()->EnableResetMentorship();
  81. }
  82. if ((*itr)->client)
  83. (*itr)->client->GetPlayer()->SetCharSheetChanged(true);
  84. }
  85. if (erase_itr != m_members.end()) {
  86. ret = true;
  87. m_members.erase(erase_itr);
  88. }
  89. MGroupMembers.releasewritelock();
  90. member->SetGroupMemberInfo(nullptr);
  91. safe_delete(gmi);
  92. if (member->IsBot())
  93. ((Bot*)member)->Camp();
  94. return ret;
  95. }
  96. void PlayerGroup::Disband() {
  97. deque<GroupMemberInfo*>::iterator itr;
  98. MGroupMembers.writelock();
  99. for (itr = m_members.begin(); itr != m_members.end(); itr++) {
  100. if ((*itr)->member) {
  101. (*itr)->member->SetGroupMemberInfo(0);
  102. if ((*itr)->member->IsBot())
  103. ((Bot*)(*itr)->member)->Camp();
  104. }
  105. if((*itr)->mentor_target_char_id && (*itr)->client)
  106. {
  107. (*itr)->mentor_target_char_id = 0;
  108. (*itr)->client->GetPlayer()->EnableResetMentorship();
  109. }
  110. if ((*itr)->client)
  111. (*itr)->client->GetPlayer()->SetCharSheetChanged(true);
  112. safe_delete(*itr);
  113. }
  114. m_members.clear();
  115. MGroupMembers.releasewritelock();
  116. }
  117. void PlayerGroup::SendGroupUpdate(Client* exclude) {
  118. deque<GroupMemberInfo*>::iterator itr;
  119. MGroupMembers.readlock(__FUNCTION__, __LINE__);
  120. for (itr = m_members.begin(); itr != m_members.end(); itr++) {
  121. GroupMemberInfo* gmi = *itr;
  122. if (gmi->client && gmi->client != exclude && !gmi->client->IsZoning())
  123. gmi->client->GetPlayer()->SetCharSheetChanged(true);
  124. }
  125. MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  126. }
  127. void PlayerGroup::SimpleGroupMessage(const char* message) {
  128. deque<GroupMemberInfo*>::iterator itr;
  129. MGroupMembers.readlock(__FUNCTION__, __LINE__);
  130. for(itr = m_members.begin(); itr != m_members.end(); itr++) {
  131. GroupMemberInfo* info = *itr;
  132. if(info->client)
  133. info->client->SimpleMessage(CHANNEL_GROUP_CHAT, message);
  134. }
  135. MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  136. }
  137. void PlayerGroup::GroupChatMessage(Spawn* from, int32 language, const char* message) {
  138. deque<GroupMemberInfo*>::iterator itr;
  139. MGroupMembers.readlock(__FUNCTION__, __LINE__);
  140. for(itr = m_members.begin(); itr != m_members.end(); itr++) {
  141. GroupMemberInfo* info = *itr;
  142. if(info && info->client && info->client->GetCurrentZone())
  143. info->client->GetCurrentZone()->HandleChatMessage(info->client, from, 0, CHANNEL_GROUP_SAY, message, 0, 0, true, language);
  144. }
  145. MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  146. }
  147. bool PlayerGroup::MakeLeader(Entity* new_leader) {
  148. deque<GroupMemberInfo*>::iterator itr;
  149. MGroupMembers.readlock(__FUNCTION__, __LINE__);
  150. for (itr = m_members.begin(); itr != m_members.end(); itr++) {
  151. GroupMemberInfo* info = *itr;
  152. if (info->leader) {
  153. info->leader = false;
  154. break;
  155. }
  156. }
  157. MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  158. new_leader->GetGroupMemberInfo()->leader = true;
  159. SendGroupUpdate();
  160. return true;
  161. }
  162. /******************************************************** PlayerGroupManager ********************************************************/
  163. PlayerGroupManager::PlayerGroupManager() {
  164. m_nextGroupID = 1;
  165. MPendingInvites.SetName("PlayerGroupManager::m_pendingInvites");
  166. }
  167. PlayerGroupManager::~PlayerGroupManager() {
  168. MPendingInvites.writelock(__FUNCTION__, __LINE__);
  169. m_pendingInvites.clear();
  170. MPendingInvites.releasewritelock(__FUNCTION__, __LINE__);
  171. std::unique_lock lock(MGroups);
  172. map<int32, PlayerGroup*>::iterator itr;
  173. for (itr = m_groups.begin(); itr != m_groups.end(); itr++)
  174. safe_delete(itr->second);
  175. m_groups.clear();
  176. }
  177. bool PlayerGroupManager::AddGroupMember(int32 group_id, Entity* member) {
  178. std::shared_lock lock(MGroups);
  179. bool ret = false;
  180. if (m_groups.count(group_id) > 0) {
  181. PlayerGroup* group = m_groups[group_id];
  182. ret = group->AddMember(member);
  183. }
  184. return ret;
  185. }
  186. bool PlayerGroupManager::RemoveGroupMember(int32 group_id, Entity* member) {
  187. bool ret = false;
  188. bool remove = false;
  189. Client* client = 0;
  190. if(member->GetGroupMemberInfo()->mentor_target_char_id)
  191. {
  192. if(member->IsPlayer())
  193. {
  194. Player* tmpPlayer = (Player*)member;
  195. member->GetGroupMemberInfo()->mentor_target_char_id = 0;
  196. tmpPlayer->EnableResetMentorship();
  197. }
  198. }
  199. GroupLock(__FUNCTION__, __LINE__);
  200. if (m_groups.count(group_id) > 0) {
  201. PlayerGroup* group = m_groups[group_id];
  202. if (member->IsPlayer())
  203. client = member->GetGroupMemberInfo()->client;
  204. ret = group->RemoveMember(member);
  205. // If only 1 person left in the group set a flag to remove the group
  206. if (group->Size() == 1)
  207. remove = true;
  208. }
  209. ReleaseGroupLock(__FUNCTION__, __LINE__);
  210. if (client)
  211. RemoveGroupBuffs(group_id, client);
  212. // Call RemoveGroup outside the locks as it uses the same locks
  213. if (remove)
  214. RemoveGroup(group_id);
  215. return ret;
  216. }
  217. void PlayerGroupManager::NewGroup(Entity* leader) {
  218. std::unique_lock lock(MGroups);
  219. // Highly doubt this will ever be needed but putting it in any way, basically bump the id and ensure
  220. // no active group is currently using this id, if we hit the max for an int32 then reset the id to 1
  221. while (m_groups.count(m_nextGroupID) > 0) {
  222. // If m_nextGroupID is at its max then reset it to 1, else increment it
  223. if (m_nextGroupID == 4294967295)
  224. m_nextGroupID = 1;
  225. else
  226. m_nextGroupID++;
  227. }
  228. // Create a new group with the valid ID we got from above
  229. PlayerGroup* new_group = new PlayerGroup(m_nextGroupID);
  230. // Add the new group to the list (need to do this first, AddMember needs ref to the PlayerGroup ptr -> UpdateGroupMemberInfo)
  231. m_groups[m_nextGroupID] = new_group;
  232. // Add the leader to the group
  233. new_group->AddMember(leader);
  234. leader->GetGroupMemberInfo()->leader = true;
  235. }
  236. void PlayerGroupManager::RemoveGroup(int32 group_id) {
  237. std::unique_lock lock(MGroups);
  238. // Check to see if the id is in the list
  239. if (m_groups.count(group_id) > 0) {
  240. // Get a pointer to the group
  241. PlayerGroup* group = m_groups[group_id];
  242. // Erase the group from the list
  243. m_groups.erase(group_id);
  244. // Delete the group
  245. safe_delete(group);
  246. }
  247. }
  248. int8 PlayerGroupManager::Invite(Player* leader, Entity* member) {
  249. int8 ret = 255; // Should be changed, if it is not then we have an unknown error
  250. // Lock the pending invite list so we can work with it
  251. MPendingInvites.writelock(__FUNCTION__, __LINE__);
  252. if (!member || (member->IsBot() && ((Bot*)member)->IsImmediateCamp()))
  253. ret = 6; // failure, not a valid target
  254. else if (member->IsNPC() && (!member->IsBot() /*|| !member->IsMec()*/))
  255. ret = 6;
  256. else if (leader == member)
  257. ret = 5; // failure, can't invite yourself
  258. else if (member->GetGroupMemberInfo())
  259. ret = 1; // failure, member already in a group
  260. // Check to see if the target of the invite already has a pending invite
  261. else if (m_pendingInvites.count(member->GetName()) > 0)
  262. ret = 2; // Target already has an invite
  263. // Check to see if the player that invited is already in a group
  264. else if (leader->GetGroupMemberInfo()) {
  265. // Read lock the group list so we can get the size of the inviters group
  266. GroupLock(__FUNCTION__, __LINE__);
  267. int32 group_size = m_groups[leader->GetGroupMemberInfo()->group_id]->Size();
  268. ReleaseGroupLock(__FUNCTION__, __LINE__);
  269. // Check to see if the group is full
  270. if (m_groups[leader->GetGroupMemberInfo()->group_id]->Size() >= 6)
  271. ret = 3; // Group full
  272. // Group isn't full so add the member to the pending invite list
  273. else {
  274. m_pendingInvites[member->GetName()] = leader->GetName();
  275. ret = 0; // Success
  276. }
  277. }
  278. // Inviter is not in a group
  279. else {
  280. // Check to see if the inviter has a pending invite himself
  281. if (m_pendingInvites.count(leader->GetName()) > 0)
  282. ret = 4; // inviter already has a pending group invite
  283. // No pending invites for the inviter add both the inviter and the target of the invite to the list
  284. else {
  285. m_pendingInvites[leader->GetName()] = leader->GetName();
  286. m_pendingInvites[member->GetName()] = leader->GetName();
  287. ret = 0; // success
  288. }
  289. }
  290. // Release the lock on pending invites
  291. MPendingInvites.releasewritelock(__FUNCTION__, __LINE__);
  292. /* testing purposes only */
  293. if (ret == 0 && member->IsNPC())
  294. AcceptInvite(member);
  295. return ret;
  296. }
  297. int8 PlayerGroupManager::AcceptInvite(Entity* member) {
  298. int8 ret = 3; // default to unknown error
  299. MPendingInvites.writelock(__FUNCTION__, __LINE__);
  300. if (m_pendingInvites.count(member->GetName()) > 0) {
  301. string leader = m_pendingInvites[member->GetName()];
  302. Client* client_leader = zone_list.GetClientByCharName(leader);
  303. if (client_leader) {
  304. if (m_pendingInvites.count(leader) > 0) {
  305. NewGroup(client_leader->GetPlayer());
  306. m_pendingInvites.erase(leader);
  307. }
  308. // Remove from invite list and add to the group
  309. m_pendingInvites.erase(member->GetName());
  310. GroupMessage(client_leader->GetPlayer()->GetGroupMemberInfo()->group_id, "%s has joined the group.", member->GetName());
  311. AddGroupMember(client_leader->GetPlayer()->GetGroupMemberInfo()->group_id, member);
  312. ret = 0; // success
  313. }
  314. else {
  315. // Was unable to find the leader, remove from the invite list
  316. m_pendingInvites.erase(member->GetName());
  317. ret = 2; // failure, can't find leader
  318. }
  319. }
  320. else
  321. ret = 1; // failure, no pending invite
  322. MPendingInvites.releasewritelock(__FUNCTION__, __LINE__);
  323. return ret;
  324. }
  325. void PlayerGroupManager::DeclineInvite(Entity* member) {
  326. MPendingInvites.writelock(__FUNCTION__, __LINE__);
  327. if (m_pendingInvites.count(member->GetName()) > 0) {
  328. string leader = m_pendingInvites[member->GetName()];
  329. m_pendingInvites.erase(member->GetName());
  330. if (m_pendingInvites.count(leader) > 0)
  331. m_pendingInvites.erase(leader);
  332. }
  333. MPendingInvites.releasewritelock(__FUNCTION__, __LINE__);
  334. }
  335. bool PlayerGroupManager::IsGroupIDValid(int32 group_id) {
  336. std::shared_lock lock(MGroups);
  337. bool ret = false;
  338. ret = m_groups.count(group_id) > 0;
  339. return ret;
  340. }
  341. void PlayerGroupManager::SendGroupUpdate(int32 group_id, Client* exclude) {
  342. std::shared_lock lock(MGroups);
  343. if (m_groups.count(group_id) > 0) {
  344. m_groups[group_id]->SendGroupUpdate(exclude);
  345. }
  346. }
  347. PlayerGroup* PlayerGroupManager::GetGroup(int32 group_id) {
  348. if (m_groups.count(group_id) > 0)
  349. return m_groups[group_id];
  350. return 0;
  351. }
  352. void PlayerGroupManager::ClearPendingInvite(Entity* member) {
  353. MPendingInvites.writelock(__FUNCTION__, __LINE__);
  354. if (m_pendingInvites.count(member->GetName()) > 0)
  355. m_pendingInvites.erase(member->GetName());
  356. MPendingInvites.releasewritelock(__FUNCTION__, __LINE__);
  357. }
  358. void PlayerGroupManager::RemoveGroupBuffs(int32 group_id, Client* client) {
  359. SpellEffects* se = 0;
  360. Spell* spell = 0;
  361. LuaSpell* luaspell = 0;
  362. EQ2Packet* packet = 0;
  363. Entity* pet = 0;
  364. Player* player = 0;
  365. Entity* charmed_pet = 0;
  366. PlayerGroup* group = 0;
  367. MGroups.lock_shared();
  368. if (m_groups.count(group_id) > 0)
  369. group = m_groups[group_id];
  370. if (group && client) {
  371. /* first remove all spell effects this group member has on them from other group members */
  372. player = client->GetPlayer();
  373. bool recoup_lock = true;
  374. for (int i = 0; i < NUM_SPELL_EFFECTS; i++) {
  375. if(recoup_lock) {
  376. player->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  377. recoup_lock = false;
  378. se = player->GetSpellEffects();
  379. }
  380. if (se && se[i].spell_id != 0xFFFFFFFF) {
  381. //If the client is the caster, don't remove the spell
  382. if (se[i].caster == player)
  383. continue;
  384. luaspell = se[i].spell;
  385. spell = luaspell->spell;
  386. /* is this a friendly group spell? */
  387. if (spell && spell->GetSpellData()->group_spell && spell->GetSpellData()->friendly_spell) {
  388. player->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  389. recoup_lock = true;
  390. // we have to remove our spell effect mutex lock since RemoveSpellEffect needs a write lock to remove it
  391. //Remove all group buffs not cast by this player
  392. player->RemoveSpellEffect(luaspell);
  393. player->RemoveSpellBonus(luaspell);
  394. player->RemoveSkillBonus(spell->GetSpellID());
  395. //Also remove group buffs from pets
  396. pet = 0;
  397. charmed_pet = 0;
  398. if (player->HasPet()){
  399. pet = player->GetPet();
  400. pet = player->GetCharmedPet();
  401. }
  402. if (pet){
  403. pet->RemoveSpellEffect(luaspell);
  404. pet->RemoveSpellBonus(luaspell);
  405. }
  406. if (charmed_pet){
  407. charmed_pet->RemoveSpellEffect(luaspell);
  408. charmed_pet->RemoveSpellBonus(luaspell);
  409. }
  410. }
  411. }
  412. }
  413. if(!recoup_lock) { // we previously set a readlock that we now release
  414. player->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  415. }
  416. packet = client->GetPlayer()->GetSkills()->GetSkillPacket(client->GetVersion());
  417. if (packet)
  418. client->QueuePacket(packet);
  419. }
  420. MGroups.unlock_shared();
  421. }
  422. int32 PlayerGroupManager::GetGroupSize(int32 group_id) {
  423. std::shared_lock lock(MGroups);
  424. int32 ret = 0;
  425. if (m_groups.count(group_id) > 0)
  426. ret = m_groups[group_id]->Size();
  427. return ret;
  428. }
  429. void PlayerGroupManager::SendGroupQuests(int32 group_id, Client* client) {
  430. std::shared_lock lock(MGroups);
  431. GroupMemberInfo* info = 0;
  432. if (m_groups.count(group_id) > 0) {
  433. m_groups[group_id]->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  434. deque<GroupMemberInfo*>* members = m_groups[group_id]->GetMembers();
  435. deque<GroupMemberInfo*>::iterator itr;
  436. for (itr = members->begin(); itr != members->end(); itr++) {
  437. info = *itr;
  438. if (info->client) {
  439. LogWrite(PLAYER__DEBUG, 0, "Player", "Send Quest Journal...");
  440. info->client->SendQuestJournal(false, client);
  441. client->SendQuestJournal(false, info->client);
  442. }
  443. }
  444. m_groups[group_id]->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  445. }
  446. }
  447. bool PlayerGroupManager::HasGroupCompletedQuest(int32 group_id, int32 quest_id) {
  448. std::shared_lock lock(MGroups);
  449. bool questComplete = true;
  450. GroupMemberInfo* info = 0;
  451. if (m_groups.count(group_id) > 0) {
  452. m_groups[group_id]->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  453. deque<GroupMemberInfo*>* members = m_groups[group_id]->GetMembers();
  454. deque<GroupMemberInfo*>::iterator itr;
  455. for (itr = members->begin(); itr != members->end(); itr++) {
  456. info = *itr;
  457. if (info->client) {
  458. bool isComplete = info->client->GetPlayer()->GetCompletedQuest(quest_id);
  459. if(!isComplete)
  460. {
  461. questComplete = isComplete;
  462. break;
  463. }
  464. }
  465. }
  466. m_groups[group_id]->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  467. }
  468. return questComplete;
  469. }
  470. void PlayerGroupManager::SimpleGroupMessage(int32 group_id, const char* message) {
  471. std::shared_lock lock(MGroups);
  472. if (m_groups.count(group_id) > 0)
  473. m_groups[group_id]->SimpleGroupMessage(message);
  474. }
  475. void PlayerGroupManager::GroupMessage(int32 group_id, const char* message, ...) {
  476. va_list argptr;
  477. char buffer[4096];
  478. buffer[0] = 0;
  479. va_start(argptr, message);
  480. vsnprintf(buffer, sizeof(buffer), message, argptr);
  481. va_end(argptr);
  482. SimpleGroupMessage(group_id, buffer);
  483. }
  484. void PlayerGroupManager::GroupChatMessage(int32 group_id, Spawn* from, int32 language, const char* message) {
  485. std::shared_lock lock(MGroups);
  486. if (m_groups.count(group_id) > 0)
  487. m_groups[group_id]->GroupChatMessage(from, language, message);
  488. }
  489. bool PlayerGroupManager::MakeLeader(int32 group_id, Entity* new_leader) {
  490. std::shared_lock lock(MGroups);
  491. if (m_groups.count(group_id) > 0)
  492. return m_groups[group_id]->MakeLeader(new_leader);
  493. return false;
  494. }
  495. void PlayerGroupManager::UpdateGroupBuffs() {
  496. map<int32, PlayerGroup*>::iterator itr;
  497. deque<GroupMemberInfo*>::iterator member_itr;
  498. deque<GroupMemberInfo*>::iterator target_itr;
  499. map<int32, SkillBonusValue*>::iterator itr_skills;
  500. MaintainedEffects* me = nullptr;
  501. LuaSpell* luaspell = nullptr;
  502. Spell* spell = nullptr;
  503. Entity* group_member = nullptr;
  504. SkillBonus* sb = nullptr;
  505. EQ2Packet* packet = nullptr;
  506. int32 i = 0;
  507. PlayerGroup* group = nullptr;
  508. Player* caster = nullptr;
  509. vector<int32> new_target_list;
  510. vector<int32> char_list;
  511. Client* client = nullptr;
  512. bool has_effect = false;
  513. vector<BonusValues*>* sb_list = nullptr;
  514. BonusValues* bv = nullptr;
  515. Entity* pet = nullptr;
  516. Entity* charmed_pet = nullptr;
  517. for (itr = m_groups.begin(); itr != m_groups.end(); itr++) {
  518. group = itr->second;
  519. /* loop through the group members and see if any of them have any maintained spells that are group buffs and friendly.
  520. if so, update the list of targets and apply/remove effects as needed */
  521. vector<Player*> players;
  522. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  523. for (member_itr = group->GetMembers()->begin(); member_itr != group->GetMembers()->end(); member_itr++) {
  524. if ((*member_itr)->client)
  525. caster = (*member_itr)->client->GetPlayer();
  526. else caster = 0;
  527. if (!caster)
  528. continue;
  529. if (!caster->GetMaintainedSpellBySlot(0))
  530. continue;
  531. players.push_back(caster);
  532. }
  533. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  534. vector<Player*>::iterator vitr;
  535. for (vitr = players.begin(); vitr != players.end(); vitr++) {
  536. caster = *vitr;
  537. caster->GetMaintainedMutex()->readlock(__FUNCTION__, __LINE__);
  538. // go through the player's maintained spells
  539. me = caster->GetMaintainedSpells();
  540. for (i = 0; i < NUM_MAINTAINED_EFFECTS; i++) {
  541. if (me[i].spell_id == 0xFFFFFFFF)
  542. continue;
  543. luaspell = me[i].spell;
  544. if (!luaspell)
  545. continue;
  546. if (!luaspell->caster)
  547. {
  548. LogWrite(PLAYER__ERROR, 0, "Player", "Bad luaspell, caster is NULL, spellid: %u", me[i].spell_id);
  549. continue;
  550. }
  551. spell = luaspell->spell;
  552. if (spell && spell->GetSpellData()->group_spell && spell->GetSpellData()->friendly_spell &&
  553. (spell->GetSpellData()->target_type == SPELL_TARGET_GROUP_AE || spell->GetSpellData()->target_type == SPELL_TARGET_RAID_AE)) {
  554. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  555. luaspell->char_id_targets.clear();
  556. for (target_itr = group->GetMembers()->begin(); target_itr != group->GetMembers()->end(); target_itr++) {
  557. group_member = (*target_itr)->member;
  558. if (!group_member)
  559. continue;
  560. if (group_member == caster)
  561. continue;
  562. client = (*target_itr)->client;
  563. has_effect = false;
  564. if (group_member->GetSpellEffect(spell->GetSpellID(), caster)) {
  565. has_effect = true;
  566. }
  567. if(!has_effect && (std::find(luaspell->removed_targets.begin(),
  568. luaspell->removed_targets.end(), group_member->GetID()) != luaspell->removed_targets.end())) {
  569. continue;
  570. }
  571. // Check if player is within range of the caster
  572. if (!rule_manager.GetGlobalRule(R_Spells, EnableCrossZoneGroupBuffs)->GetInt8() &&
  573. (group_member->GetZone() != caster->GetZone() || caster->GetDistance(group_member) > spell->GetSpellData()->radius)) {
  574. if (has_effect) {
  575. group_member->RemoveSpellEffect(luaspell);
  576. group_member->RemoveSpellBonus(luaspell);
  577. group_member->RemoveSkillBonus(spell->GetSpellID());
  578. if (client) {
  579. packet = ((Player*)group_member)->GetSkills()->GetSkillPacket(client->GetVersion());
  580. if (packet)
  581. client->QueuePacket(packet);
  582. }
  583. //Also remove group buffs from pet
  584. if (group_member->HasPet()) {
  585. pet = group_member->GetPet();
  586. charmed_pet = group_member->GetCharmedPet();
  587. if (pet) {
  588. pet->RemoveSpellEffect(luaspell);
  589. pet->RemoveSpellBonus(luaspell);
  590. }
  591. if (charmed_pet) {
  592. charmed_pet->RemoveSpellEffect(luaspell);
  593. charmed_pet->RemoveSpellBonus(luaspell);
  594. }
  595. }
  596. }
  597. continue;
  598. }
  599. if(group_member->GetZone() != caster->GetZone())
  600. {
  601. SpellProcess::AddSelfAndPetToCharTargets(luaspell, group_member);
  602. }
  603. else
  604. {
  605. //this group member is a target of the spell
  606. new_target_list.push_back(group_member->GetID());
  607. }
  608. if (has_effect)
  609. continue;
  610. pet = 0;
  611. charmed_pet = 0;
  612. if (group_member->HasPet()) {
  613. pet = group_member->GetPet();
  614. charmed_pet = group_member->GetCharmedPet();
  615. }
  616. group_member->AddSpellEffect(luaspell, luaspell->timer.GetRemainingTime() != 0 ? luaspell->timer.GetRemainingTime() : 0);
  617. if (pet)
  618. pet->AddSpellEffect(luaspell, luaspell->timer.GetRemainingTime() != 0 ? luaspell->timer.GetRemainingTime() : 0);
  619. if (charmed_pet)
  620. charmed_pet->AddSpellEffect(luaspell, luaspell->timer.GetRemainingTime() != 0 ? luaspell->timer.GetRemainingTime() : 0);
  621. if (pet)
  622. new_target_list.push_back(pet->GetID());
  623. if (charmed_pet)
  624. new_target_list.push_back(charmed_pet->GetID());
  625. // look for a spell bonus on caster's spell
  626. sb_list = caster->GetAllSpellBonuses(luaspell);
  627. for (int32 x = 0; x < sb_list->size(); x++) {
  628. bv = sb_list->at(x);
  629. group_member->AddSpellBonus(luaspell, bv->type, bv->value, bv->class_req, bv->race_req, bv->faction_req);
  630. if (pet)
  631. pet->AddSpellBonus(luaspell, bv->type, bv->value, bv->class_req, bv->race_req, bv->faction_req);
  632. if (charmed_pet)
  633. charmed_pet->AddSpellBonus(luaspell, bv->type, bv->value, bv->class_req, bv->race_req, bv->faction_req);
  634. }
  635. sb_list->clear();
  636. safe_delete(sb_list);
  637. // look for a skill bonus on the caster's spell
  638. sb = caster->GetSkillBonus(me[i].spell_id);
  639. if (sb) {
  640. for (itr_skills = sb->skills.begin(); itr_skills != sb->skills.end(); itr_skills++)
  641. group_member->AddSkillBonus(sb->spell_id, (*itr_skills).second->skill_id, (*itr_skills).second->value);
  642. }
  643. if (client) {
  644. packet = ((Player*)group_member)->GetSkills()->GetSkillPacket(client->GetVersion());
  645. if (packet)
  646. client->QueuePacket(packet);
  647. }
  648. }
  649. luaspell->targets.swap(new_target_list);
  650. SpellProcess::AddSelfAndPet(luaspell, caster);
  651. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  652. new_target_list.clear();
  653. }
  654. }
  655. caster->GetMaintainedMutex()->releasereadlock(__FUNCTION__, __LINE__);
  656. }
  657. }
  658. }
  659. bool PlayerGroupManager::IsInGroup(int32 group_id, Entity* member) {
  660. std::shared_lock lock(MGroups);
  661. bool ret = false;
  662. if (m_groups.count(group_id) > 0) {
  663. m_groups[group_id]->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  664. deque<GroupMemberInfo*>* members = m_groups[group_id]->GetMembers();
  665. for (int8 i = 0; i < members->size(); i++) {
  666. if (member == members->at(i)->member) {
  667. ret = true;
  668. break;
  669. }
  670. }
  671. m_groups[group_id]->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  672. }
  673. return ret;
  674. }
  675. Entity* PlayerGroupManager::IsPlayerInGroup(int32 group_id, int32 character_id) {
  676. std::shared_lock lock(MGroups);
  677. Entity* ret = nullptr;
  678. if (m_groups.count(group_id) > 0) {
  679. m_groups[group_id]->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  680. deque<GroupMemberInfo*>* members = m_groups[group_id]->GetMembers();
  681. for (int8 i = 0; i < members->size(); i++) {
  682. if (members->at(i)->member && members->at(i)->member->IsPlayer() && character_id == ((Player*)members->at(i)->member)->GetCharacterID()) {
  683. ret = members->at(i)->member;
  684. break;
  685. }
  686. }
  687. m_groups[group_id]->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  688. }
  689. return ret;
  690. }
  691. void PlayerGroup::RemoveClientReference(Client* remove) {
  692. deque<GroupMemberInfo*>::iterator itr;
  693. MGroupMembers.writelock();
  694. for (itr = m_members.begin(); itr != m_members.end(); itr++) {
  695. GroupMemberInfo* gmi = *itr;
  696. if (gmi->client && gmi->client == remove)
  697. {
  698. gmi->client = 0;
  699. gmi->member = 0;
  700. break;
  701. }
  702. }
  703. MGroupMembers.releasewritelock();
  704. }
  705. void PlayerGroup::UpdateGroupMemberInfo(Entity* ent, bool groupMembersLocked) {
  706. Player* player = (Player*)ent;
  707. if (!player || !player->GetGroupMemberInfo())
  708. return;
  709. if(!groupMembersLocked)
  710. MGroupMembers.writelock();
  711. GroupMemberInfo* group_member_info = player->GetGroupMemberInfo();
  712. player->GetGroupMemberInfo()->class_id = player->GetAdventureClass();
  713. group_member_info->hp_max = player->GetTotalHP();
  714. group_member_info->hp_current = player->GetHP();
  715. group_member_info->level_max = player->GetLevel();
  716. group_member_info->level_current = player->GetLevel();
  717. group_member_info->name = string(player->GetName());
  718. group_member_info->power_current = player->GetPower();
  719. group_member_info->power_max = player->GetTotalPower();
  720. group_member_info->race_id = player->GetRace();
  721. if (player->GetZone())
  722. group_member_info->zone = player->GetZone()->GetZoneDescription();
  723. else
  724. group_member_info->zone = "Unknown";
  725. if(!groupMembersLocked)
  726. MGroupMembers.releasewritelock();
  727. }
  728. Entity* PlayerGroup::GetGroupMemberByPosition(Entity* seeker, int32 mapped_position) {
  729. Entity* ret = nullptr;
  730. deque<GroupMemberInfo*>::iterator itr;
  731. MGroupMembers.readlock();
  732. int32 count = 1;
  733. for (itr = m_members.begin(); itr != m_members.end(); itr++) {
  734. if ((*itr)->member == seeker) {
  735. continue;
  736. }
  737. count++;
  738. if(count >= mapped_position) {
  739. ret = (Entity*)(*itr)->member;
  740. break;
  741. }
  742. }
  743. MGroupMembers.releasereadlock();
  744. return ret;
  745. }