PlayerGroups.cpp 28 KB

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