From d3e6bae5d9672daf4fa82b1cda17f25021d679d9 Mon Sep 17 00:00:00 2001 From: gsemaj Date: Sun, 24 Apr 2022 16:50:03 -0400 Subject: [PATCH] Replace group filter operator with function --- src/Groups.cpp | 14 +++++++------- src/Groups.hpp | 2 +- src/MobAI.cpp | 2 +- src/NPCManager.cpp | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Groups.cpp b/src/Groups.cpp index ba85d4c..57418b8 100644 --- a/src/Groups.cpp +++ b/src/Groups.cpp @@ -77,7 +77,7 @@ static void requestGroup(CNSocket* sock, CNPacketData* data) { return; // fail if the group is full or the other player is already in a group - if ((plr->group != nullptr && (*plr->group)[EntityKind::PLAYER].size() >= 4) || otherPlr->group != nullptr) { + if ((plr->group != nullptr && plr->group->filter(EntityKind::PLAYER).size() >= 4) || otherPlr->group != nullptr) { INITSTRUCT(sP_FE2CL_PC_GROUP_INVITE_FAIL, resp); sock->sendPacket((void*)&resp, P_FE2CL_PC_GROUP_INVITE_FAIL, sizeof(sP_FE2CL_PC_GROUP_INVITE_FAIL)); return; @@ -120,7 +120,7 @@ static void joinGroup(CNSocket* sock, CNPacketData* data) { if (otherPlr == nullptr) return; - int size = otherPlr->group == nullptr ? 1 : (*otherPlr->group)[EntityKind::PLAYER].size(); + int size = otherPlr->group == nullptr ? 1 : otherPlr->group->filter(EntityKind::PLAYER).size(); // fail if the group is full or the other player is already in a group if (plr->group != nullptr || size + 1 > 4) { @@ -140,7 +140,7 @@ static void joinGroup(CNSocket* sock, CNPacketData* data) { addToGroup(PlayerManager::getSockFromID(recv->iID_From), otherPlr->group); } addToGroup(sock, otherPlr->group); - auto players = (*otherPlr->group)[EntityKind::PLAYER]; + auto players = otherPlr->group->filter(EntityKind::PLAYER); size_t resplen = sizeof(sP_FE2CL_PC_GROUP_JOIN) + players.size() * sizeof(sPCGroupMemberInfo); uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; @@ -196,7 +196,7 @@ static void leaveGroup(CNSocket* sock, CNPacketData* data) { } void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) { - auto players = (*group)[EntityKind::PLAYER]; + auto players = group->filter(EntityKind::PLAYER); for (int i = 0; i < players.size(); i++) { CNSocket* sock = players[i].sock; sock->sendPacket(buf, type, size); @@ -205,7 +205,7 @@ void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) { void Groups::groupTickInfo(Player* plr) { - auto players = (*plr->group)[EntityKind::PLAYER]; + auto players = plr->group->filter(EntityKind::PLAYER); if (!validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_MEMBER_INFO), players.size(), sizeof(sPCGroupMemberInfo))) { std::cout << "[WARN] bad sP_FE2CL_PC_GROUP_JOIN packet size\n"; @@ -280,7 +280,7 @@ void Groups::groupKick(Player* plr) { return; } - auto players = (*group)[EntityKind::PLAYER]; + auto players = group->filter(EntityKind::PLAYER); if (!validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_LEAVE), players.size() - 1, sizeof(sPCGroupMemberInfo))) { std::cout << "[WARN] bad sP_FE2CL_PC_GROUP_LEAVE packet size\n"; @@ -307,7 +307,7 @@ void Groups::groupKick(Player* plr) { removeFromGroup(sock, group); - players = (*group)[EntityKind::PLAYER]; + players = group->filter(EntityKind::PLAYER); for (int i = 0; i < players.size(); i++) { CNSocket* sockTo = players[i].sock; Player* varPlr = PlayerManager::getPlayer(sock); diff --git a/src/Groups.hpp b/src/Groups.hpp index b437827..05bf569 100644 --- a/src/Groups.hpp +++ b/src/Groups.hpp @@ -14,7 +14,7 @@ struct Group { std::vector members; int32_t conditionBitFlag; - auto operator[](EntityKind kind) { + std::vector filter(EntityKind kind) { std::vector filtered; std::copy_if(members.begin(), members.end(), std::back_inserter(filtered), [kind](EntityRef e) { return e.kind == kind; diff --git a/src/MobAI.cpp b/src/MobAI.cpp index 1c9b860..4299727 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -851,7 +851,7 @@ void MobAI::onDeath(CombatNPC* npc, EntityRef src) { Missions::mobKilled(src.sock, self->type, qitemRolls); } else { - auto players = (*plr->group)[EntityKind::PLAYER]; + auto players = plr->group->filter(EntityKind::PLAYER); for (EntityRef pRef : players) playerRefs.push_back(PlayerManager::getPlayer(pRef.sock)); Combat::genQItemRolls(playerRefs, qitemRolls); for (int i = 0; i < players.size(); i++) { diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 682f549..ee38ece 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -183,7 +183,7 @@ static void handleWarp(CNSocket* sock, int32_t warpId) { uint64_t instanceID = Warps[warpId].instanceID; Player* leader = plr; - if (plr->group != nullptr) leader = PlayerManager::getPlayer((*plr->group)[EntityKind::PLAYER][0].sock); + if (plr->group != nullptr) leader = PlayerManager::getPlayer(plr->group->filter(EntityKind::PLAYER)[0].sock); // if warp requires you to be on a mission, it's gotta be a unique instance if (Warps[warpId].limitTaskID != 0 || instanceID == 14) { // 14 is a special case for the Time Lab @@ -200,7 +200,7 @@ static void handleWarp(CNSocket* sock, int32_t warpId) { if (plr->group == nullptr) PlayerManager::sendPlayerTo(sock, Warps[warpId].x, Warps[warpId].y, Warps[warpId].z, instanceID); else { - auto players = (*plr->group)[EntityKind::PLAYER]; + auto players = plr->group->filter(EntityKind::PLAYER); for (int i = 0; i < players.size(); i++) { CNSocket* sockTo = players[i].sock; Player* otherPlr = PlayerManager::getPlayer(sockTo);