9
3

PlayerGroups.cpp 28 KB

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