2021-03-16 22:29:13 +00:00
|
|
|
#include "Groups.hpp"
|
2020-10-04 23:54:08 +00:00
|
|
|
|
2022-07-16 23:19:40 +00:00
|
|
|
#include "servers/CNShardServer.hpp"
|
|
|
|
|
|
|
|
#include "Player.hpp"
|
|
|
|
#include "PlayerManager.hpp"
|
2022-07-31 05:50:03 +00:00
|
|
|
#include "Entities.hpp"
|
2020-10-04 23:54:08 +00:00
|
|
|
|
2021-03-19 21:28:25 +00:00
|
|
|
/*
|
|
|
|
* NOTE: Variadic response packets that list group members are technically
|
|
|
|
* double-variadic, as they have two count members with trailing struct counts,
|
|
|
|
* and are thus incompatible with the generic sendPacket() wrapper.
|
|
|
|
* That means we still have to (carefully) use validOutVarPacket() in this
|
|
|
|
* source file.
|
|
|
|
*/
|
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
using namespace Groups;
|
2020-10-04 23:54:08 +00:00
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
Group::Group(EntityRef leader) {
|
|
|
|
addToGroup(this, leader);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void attachGroupData(std::vector<EntityRef>& pcs, std::vector<EntityRef>& npcs, uint8_t* pivot) {
|
|
|
|
for(EntityRef pcRef : pcs) {
|
|
|
|
sPCGroupMemberInfo* info = (sPCGroupMemberInfo*)pivot;
|
|
|
|
|
|
|
|
Player* plr = PlayerManager::getPlayer(pcRef.sock);
|
|
|
|
info->iPC_ID = plr->iID;
|
|
|
|
info->iPCUID = plr->PCStyle.iPC_UID;
|
|
|
|
info->iNameCheck = plr->PCStyle.iNameCheck;
|
|
|
|
memcpy(info->szFirstName, plr->PCStyle.szFirstName, sizeof(plr->PCStyle.szFirstName));
|
|
|
|
memcpy(info->szLastName, plr->PCStyle.szLastName, sizeof(plr->PCStyle.szLastName));
|
|
|
|
info->iSpecialState = plr->iSpecialState;
|
|
|
|
info->iLv = plr->level;
|
|
|
|
info->iHP = plr->HP;
|
|
|
|
info->iMaxHP = PC_MAXHEALTH(plr->level);
|
|
|
|
// info->iMapType = 0;
|
|
|
|
// info->iMapNum = 0;
|
|
|
|
info->iX = plr->x;
|
|
|
|
info->iY = plr->y;
|
|
|
|
info->iZ = plr->z;
|
|
|
|
if(plr->activeNano > 0) {
|
|
|
|
info->Nano = *plr->getActiveNano();
|
|
|
|
info->bNano = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pivot = (uint8_t*)(info + 1);
|
|
|
|
}
|
|
|
|
for(EntityRef npcRef : npcs) {
|
|
|
|
sNPCGroupMemberInfo* info = (sNPCGroupMemberInfo*)pivot;
|
|
|
|
|
|
|
|
// probably should not assume that the combatant is an
|
|
|
|
// entity, but it works for now
|
|
|
|
BaseNPC* npc = (BaseNPC*)npcRef.getEntity();
|
|
|
|
info->iNPC_ID = npcRef.id;
|
|
|
|
info->iNPC_Type = npc->type;
|
|
|
|
info->iHP = npc->hp;
|
|
|
|
info->iX = npc->x;
|
|
|
|
info->iY = npc->y;
|
|
|
|
info->iZ = npc->z;
|
|
|
|
|
|
|
|
pivot = (uint8_t*)(info + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::addToGroup(Group* group, EntityRef member) {
|
2022-04-23 01:13:00 +00:00
|
|
|
if (member.kind == EntityKind::PLAYER) {
|
|
|
|
Player* plr = PlayerManager::getPlayer(member.sock);
|
|
|
|
plr->group = group;
|
|
|
|
}
|
|
|
|
else if (member.kind == EntityKind::COMBAT_NPC) {
|
|
|
|
CombatNPC* npc = (CombatNPC*)member.getEntity();
|
|
|
|
npc->group = group;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cout << "[WARN] Adding a weird entity type to a group" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
group->members.push_back(member);
|
2022-07-31 00:43:17 +00:00
|
|
|
|
|
|
|
if(member.kind == EntityKind::PLAYER) {
|
|
|
|
std::vector<EntityRef> pcs = group->filter(EntityKind::PLAYER);
|
|
|
|
std::vector<EntityRef> npcs = group->filter(EntityKind::COMBAT_NPC);
|
|
|
|
size_t pcCount = pcs.size();
|
|
|
|
size_t npcCount = npcs.size();
|
|
|
|
|
|
|
|
uint8_t respbuf[CN_PACKET_BUFFER_SIZE];
|
|
|
|
memset(respbuf, 0, CN_PACKET_BUFFER_SIZE);
|
|
|
|
sP_FE2CL_PC_GROUP_JOIN* pkt = (sP_FE2CL_PC_GROUP_JOIN*)respbuf;
|
|
|
|
|
|
|
|
pkt->iID_NewMember = PlayerManager::getPlayer(member.sock)->iID;
|
|
|
|
pkt->iMemberPCCnt = (int32_t)pcCount;
|
|
|
|
pkt->iMemberNPCCnt = (int32_t)npcCount;
|
|
|
|
|
|
|
|
if(!validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_JOIN), pcCount, sizeof(sPCGroupMemberInfo))
|
|
|
|
|| !validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_JOIN) + pcCount * sizeof(sPCGroupMemberInfo), npcCount, sizeof(sNPCGroupMemberInfo))) {
|
|
|
|
std::cout << "[WARN] bad sP_FE2CL_PC_GROUP_JOIN packet size" << std::endl;
|
|
|
|
} else {
|
|
|
|
uint8_t* pivot = (uint8_t*)(pkt + 1);
|
|
|
|
attachGroupData(pcs, npcs, pivot);
|
|
|
|
// PC_GROUP_JOIN_SUCC and PC_GROUP_JOIN carry identical payloads but have different IDs
|
|
|
|
// (and the client does care!) so we need to send one to the new member
|
|
|
|
// and the other to the rest
|
|
|
|
size_t resplen = sizeof(sP_FE2CL_PC_GROUP_JOIN) + pcCount * sizeof(sPCGroupMemberInfo) + npcCount * sizeof(sNPCGroupMemberInfo);
|
|
|
|
member.sock->sendPacket(respbuf, P_FE2CL_PC_GROUP_JOIN_SUCC, resplen);
|
|
|
|
sendToGroup(group, member, respbuf, P_FE2CL_PC_GROUP_JOIN, resplen);
|
|
|
|
}
|
|
|
|
}
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
bool Groups::removeFromGroup(Group* group, EntityRef member) {
|
2022-04-23 01:13:00 +00:00
|
|
|
if (member.kind == EntityKind::PLAYER) {
|
|
|
|
Player* plr = PlayerManager::getPlayer(member.sock);
|
|
|
|
plr->group = nullptr; // no dangling pointers here muahaahahah
|
2022-07-31 00:43:17 +00:00
|
|
|
|
|
|
|
INITSTRUCT(sP_FE2CL_PC_GROUP_LEAVE_SUCC, leavePkt);
|
|
|
|
member.sock->sendPacket(leavePkt, P_FE2CL_PC_GROUP_LEAVE_SUCC);
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
|
|
|
else if (member.kind == EntityKind::COMBAT_NPC) {
|
|
|
|
CombatNPC* npc = (CombatNPC*)member.getEntity();
|
|
|
|
npc->group = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
2022-07-31 00:43:17 +00:00
|
|
|
std::cout << "[WARN] Removing a weird entity type from a group" << std::endl;
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto it = std::find(group->members.begin(), group->members.end(), member);
|
|
|
|
if (it == group->members.end()) {
|
|
|
|
std::cout << "[WARN] Tried to remove a member that isn't in the group" << std::endl;
|
2022-07-31 00:43:17 +00:00
|
|
|
} else {
|
|
|
|
group->members.erase(it);
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
if(member.kind == EntityKind::PLAYER) {
|
|
|
|
std::vector<EntityRef> pcs = group->filter(EntityKind::PLAYER);
|
|
|
|
std::vector<EntityRef> npcs = group->filter(EntityKind::COMBAT_NPC);
|
|
|
|
size_t pcCount = pcs.size();
|
|
|
|
size_t npcCount = npcs.size();
|
|
|
|
|
|
|
|
uint8_t respbuf[CN_PACKET_BUFFER_SIZE];
|
|
|
|
memset(respbuf, 0, CN_PACKET_BUFFER_SIZE);
|
|
|
|
sP_FE2CL_PC_GROUP_LEAVE* pkt = (sP_FE2CL_PC_GROUP_LEAVE*)respbuf;
|
|
|
|
|
|
|
|
pkt->iID_LeaveMember = PlayerManager::getPlayer(member.sock)->iID;
|
|
|
|
pkt->iMemberPCCnt = (int32_t)pcCount;
|
|
|
|
pkt->iMemberNPCCnt = (int32_t)npcCount;
|
|
|
|
|
|
|
|
if(!validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_LEAVE), pcCount, sizeof(sPCGroupMemberInfo))
|
|
|
|
|| !validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_LEAVE) + pcCount * sizeof(sPCGroupMemberInfo), npcCount, sizeof(sNPCGroupMemberInfo))) {
|
|
|
|
std::cout << "[WARN] bad sP_FE2CL_PC_GROUP_LEAVE packet size" << std::endl;
|
|
|
|
} else {
|
|
|
|
uint8_t* pivot = (uint8_t*)(pkt + 1);
|
|
|
|
attachGroupData(pcs, npcs, pivot);
|
|
|
|
sendToGroup(group, respbuf, P_FE2CL_PC_GROUP_LEAVE,
|
|
|
|
sizeof(sP_FE2CL_PC_GROUP_LEAVE) + pcCount * sizeof(sPCGroupMemberInfo) + npcCount * sizeof(sNPCGroupMemberInfo));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group->members.size() == 1) {
|
|
|
|
return removeFromGroup(group, group->members.back());
|
|
|
|
}
|
2022-04-23 01:13:00 +00:00
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
if (group->members.empty()) {
|
|
|
|
delete group; // cleanup memory
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::disbandGroup(Group* group) {
|
|
|
|
// remove everyone from the group!!
|
2022-07-31 00:43:17 +00:00
|
|
|
bool done = false;
|
|
|
|
while(!done) {
|
|
|
|
EntityRef back = group->members.back();
|
|
|
|
done = removeFromGroup(group, back);
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void requestGroup(CNSocket* sock, CNPacketData* data) {
|
2020-10-04 23:54:08 +00:00
|
|
|
sP_CL2FE_REQ_PC_GROUP_INVITE* recv = (sP_CL2FE_REQ_PC_GROUP_INVITE*)data->buf;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2020-10-04 23:54:08 +00:00
|
|
|
Player* otherPlr = PlayerManager::getPlayerFromID(recv->iID_To);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
if (otherPlr == nullptr)
|
2020-10-21 05:24:51 +00:00
|
|
|
return;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
// fail if the group is full or the other player is already in a group
|
2022-04-24 20:50:03 +00:00
|
|
|
if ((plr->group != nullptr && plr->group->filter(EntityKind::PLAYER).size() >= 4) || otherPlr->group != nullptr) {
|
2020-10-04 23:54:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-10-05 00:03:13 +00:00
|
|
|
|
|
|
|
CNSocket* otherSock = PlayerManager::getSockFromID(recv->iID_To);
|
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
if (otherSock == nullptr)
|
|
|
|
return;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_PC_GROUP_INVITE, resp);
|
|
|
|
|
2020-12-31 03:54:48 +00:00
|
|
|
resp.iHostID = plr->iID;
|
2020-10-04 23:54:08 +00:00
|
|
|
|
2020-10-21 05:24:51 +00:00
|
|
|
otherSock->sendPacket((void*)&resp, P_FE2CL_PC_GROUP_INVITE, sizeof(sP_FE2CL_PC_GROUP_INVITE));
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void refuseGroup(CNSocket* sock, CNPacketData* data) {
|
2020-10-04 23:54:08 +00:00
|
|
|
sP_CL2FE_REQ_PC_GROUP_INVITE_REFUSE* recv = (sP_CL2FE_REQ_PC_GROUP_INVITE_REFUSE*)data->buf;
|
|
|
|
|
2020-10-21 05:24:51 +00:00
|
|
|
CNSocket* otherSock = PlayerManager::getSockFromID(recv->iID_From);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
if (otherSock == nullptr)
|
|
|
|
return;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_PC_GROUP_INVITE_REFUSE, resp);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
resp.iID_To = plr->iID;
|
|
|
|
|
2020-10-21 05:24:51 +00:00
|
|
|
otherSock->sendPacket((void*)&resp, P_FE2CL_PC_GROUP_INVITE_REFUSE, sizeof(sP_FE2CL_PC_GROUP_INVITE_REFUSE));
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void joinGroup(CNSocket* sock, CNPacketData* data) {
|
2020-10-04 23:54:08 +00:00
|
|
|
sP_CL2FE_REQ_PC_GROUP_JOIN* recv = (sP_CL2FE_REQ_PC_GROUP_JOIN*)data->buf;
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
|
|
|
Player* otherPlr = PlayerManager::getPlayerFromID(recv->iID_From);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
if (otherPlr == nullptr)
|
2022-07-31 00:43:17 +00:00
|
|
|
return; // disconnect or something
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2022-04-24 20:50:03 +00:00
|
|
|
int size = otherPlr->group == nullptr ? 1 : otherPlr->group->filter(EntityKind::PLAYER).size();
|
2022-04-23 19:58:08 +00:00
|
|
|
|
2020-10-09 00:01:35 +00:00
|
|
|
// fail if the group is full or the other player is already in a group
|
2022-04-23 19:58:08 +00:00
|
|
|
if (plr->group != nullptr || size + 1 > 4) {
|
2020-10-09 16:24:16 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_PC_GROUP_JOIN_FAIL, resp);
|
2020-10-04 23:54:08 +00:00
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_PC_GROUP_JOIN_FAIL, sizeof(sP_FE2CL_PC_GROUP_JOIN_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2022-04-23 01:13:00 +00:00
|
|
|
if (otherPlr->group == nullptr) {
|
|
|
|
// create group
|
2022-07-31 00:43:17 +00:00
|
|
|
EntityRef otherPlrRef = PlayerManager::getSockFromID(recv->iID_From);
|
|
|
|
otherPlr->group = new Group(otherPlrRef);
|
2022-04-23 01:13:00 +00:00
|
|
|
}
|
2022-07-31 00:43:17 +00:00
|
|
|
addToGroup(otherPlr->group, sock);
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void leaveGroup(CNSocket* sock, CNPacketData* data) {
|
2020-10-04 23:54:08 +00:00
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2022-07-31 00:43:17 +00:00
|
|
|
groupKick(plr->group, sock);
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:13:00 +00:00
|
|
|
void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) {
|
2022-04-24 20:50:03 +00:00
|
|
|
auto players = group->filter(EntityKind::PLAYER);
|
2022-07-31 00:43:17 +00:00
|
|
|
for (EntityRef ref : players) {
|
|
|
|
ref.sock->sendPacket(buf, type, size);
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
void Groups::sendToGroup(Group* group, EntityRef excluded, void* buf, uint32_t type, size_t size) {
|
|
|
|
auto players = group->filter(EntityKind::PLAYER);
|
|
|
|
for (EntityRef ref : players) {
|
|
|
|
if(ref != excluded) ref.sock->sendPacket(buf, type, size);
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
void Groups::groupTickInfo(CNSocket* sock) {
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2022-04-23 01:13:00 +00:00
|
|
|
Group* group = plr->group;
|
2022-07-31 00:43:17 +00:00
|
|
|
std::vector<EntityRef> pcs = group->filter(EntityKind::PLAYER);
|
|
|
|
std::vector<EntityRef> npcs = group->filter(EntityKind::COMBAT_NPC);
|
|
|
|
size_t pcCount = pcs.size();
|
|
|
|
size_t npcCount = npcs.size();
|
2022-04-23 01:13:00 +00:00
|
|
|
|
2020-10-04 23:54:08 +00:00
|
|
|
uint8_t respbuf[CN_PACKET_BUFFER_SIZE];
|
2022-07-31 00:43:17 +00:00
|
|
|
memset(respbuf, 0, CN_PACKET_BUFFER_SIZE);
|
|
|
|
sP_FE2CL_PC_GROUP_MEMBER_INFO* pkt = (sP_FE2CL_PC_GROUP_MEMBER_INFO*)respbuf;
|
|
|
|
|
|
|
|
pkt->iID = plr->iID;
|
|
|
|
pkt->iMemberPCCnt = (int32_t)pcCount;
|
|
|
|
pkt->iMemberNPCCnt = (int32_t)npcCount;
|
|
|
|
|
|
|
|
if(!validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_MEMBER_INFO), pcCount, sizeof(sPCGroupMemberInfo))
|
|
|
|
|| !validOutVarPacket(sizeof(sP_FE2CL_PC_GROUP_MEMBER_INFO) + pcCount * sizeof(sPCGroupMemberInfo), npcCount, sizeof(sNPCGroupMemberInfo))) {
|
|
|
|
std::cout << "[WARN] bad sP_FE2CL_PC_GROUP_MEMBER_INFO packet size" << std::endl;
|
|
|
|
} else {
|
|
|
|
uint8_t* pivot = (uint8_t*)(pkt + 1);
|
|
|
|
attachGroupData(pcs, npcs, pivot);
|
|
|
|
sock->sendPacket(respbuf, P_FE2CL_PC_GROUP_MEMBER_INFO,
|
|
|
|
sizeof(sP_FE2CL_PC_GROUP_MEMBER_INFO) + pcCount * sizeof(sPCGroupMemberInfo) + npcCount * sizeof(sNPCGroupMemberInfo));
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
void Groups::groupKick(Group* group, EntityRef ref) {
|
2020-11-08 00:26:44 +00:00
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
// if you are the group leader, destroy your own group and kick everybody
|
|
|
|
if (group->members[0] == ref) {
|
|
|
|
disbandGroup(group);
|
2020-11-08 00:26:44 +00:00
|
|
|
return;
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2022-07-31 00:43:17 +00:00
|
|
|
removeFromGroup(group, ref);
|
2020-10-04 23:54:08 +00:00
|
|
|
}
|
2020-10-09 00:01:35 +00:00
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
void Groups::init() {
|
2021-03-16 21:06:10 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GROUP_INVITE, requestGroup);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GROUP_INVITE_REFUSE, refuseGroup);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GROUP_JOIN, joinGroup);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GROUP_LEAVE, leaveGroup);
|
|
|
|
}
|