9
3

PlayerGroups.cpp 26 KB

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