PlayerGroups.cpp 24 KB

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