mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2025-09-28 18:57:35 +00:00
Dynamically allocate the Player struct in PlayerView.
This way we're not always passing it around by value. Note that the Player structs in CNSharedData are still pass-by-reference. Will probably change this later.
This commit is contained in:
@@ -30,15 +30,18 @@ void PlayerManager::init() {
|
||||
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_REGEN, PlayerManager::revivePlayer);
|
||||
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_EXIT, PlayerManager::exitGame);
|
||||
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_SPECIAL_STATE_SWITCH, PlayerManager::setSpecialSwitchPlayer);
|
||||
|
||||
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VEHICLE_ON, PlayerManager::enterPlayerVehicle);
|
||||
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VEHICLE_OFF, PlayerManager::exitPlayerVehicle);
|
||||
}
|
||||
|
||||
void PlayerManager::addPlayer(CNSocket* key, Player plr) {
|
||||
Player *p = new Player();
|
||||
|
||||
memcpy(p, &plr, sizeof(Player));
|
||||
|
||||
players[key] = PlayerView();
|
||||
players[key].viewable = std::list<CNSocket*>();
|
||||
players[key].plr = plr;
|
||||
players[key].plr = p;
|
||||
players[key].lastHeartbeat = 0;
|
||||
|
||||
std::cout << U16toU8(plr.PCStyle.szFirstName) << " " << U16toU8(plr.PCStyle.szLastName) << " has joined!" << std::endl;
|
||||
@@ -54,21 +57,22 @@ void PlayerManager::removePlayer(CNSocket* key) {
|
||||
|
||||
// now sent PC_EXIT packet
|
||||
sP_FE2CL_PC_EXIT exitPacket;
|
||||
exitPacket.iID = players[key].plr.iID;
|
||||
exitPacket.iID = players[key].plr->iID;
|
||||
|
||||
otherSock->sendPacket((void*)&exitPacket, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
||||
}
|
||||
|
||||
delete cachedView.plr;
|
||||
players.erase(key);
|
||||
|
||||
std::cout << U16toU8(cachedView.plr.PCStyle.szFirstName) << U16toU8(cachedView.plr.PCStyle.szLastName) << " has left!" << std::endl;
|
||||
std::cout << U16toU8(cachedView.plr->PCStyle.szFirstName) << U16toU8(cachedView.plr->PCStyle.szLastName) << " has left!" << std::endl;
|
||||
std::cout << players.size() << " players" << std::endl;
|
||||
}
|
||||
|
||||
void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
|
||||
players[sock].plr.x = X;
|
||||
players[sock].plr.y = Y;
|
||||
players[sock].plr.z = Z;
|
||||
players[sock].plr->x = X;
|
||||
players[sock].plr->y = Y;
|
||||
players[sock].plr->z = Z;
|
||||
|
||||
std::vector<CNSocket*> noView;
|
||||
std::vector<CNSocket*> yesView;
|
||||
@@ -78,8 +82,8 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
|
||||
if (pair.first == sock)
|
||||
continue; // ignore our own connection
|
||||
|
||||
int diffX = abs(pair.second.plr.x - X); // the map is like a grid, X and Y are your position on the map, Z is the height. very different from other games...
|
||||
int diffY = abs(pair.second.plr.y - Y);
|
||||
int diffX = abs(pair.second.plr->x - X); // the map is like a grid, X and Y are your position on the map, Z is the height. very different from other games...
|
||||
int diffY = abs(pair.second.plr->y - Y);
|
||||
|
||||
if (diffX < settings::VIEWDISTANCE && diffY < settings::VIEWDISTANCE) {
|
||||
yesView.push_back(pair.first);
|
||||
@@ -96,9 +100,9 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
|
||||
if (std::find(noView.begin(), noView.end(), otherSock) != noView.end()) {
|
||||
|
||||
// sock shouldn't be visible, send PC_EXIT packet
|
||||
exitPacket.iID = players[sock].plr.iID;
|
||||
exitPacket.iID = players[sock].plr->iID;
|
||||
otherSock->sendPacket((void*)&exitPacket, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
||||
exitPacket.iID = players[otherSock].plr.iID;
|
||||
exitPacket.iID = players[otherSock].plr->iID;
|
||||
sock->sendPacket((void*)&exitPacket, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
||||
|
||||
// remove them from the viewable list
|
||||
@@ -115,34 +119,34 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
|
||||
if (std::find(players[sock].viewable.begin(), players[sock].viewable.end(), otherSock) == players[sock].viewable.end()) {
|
||||
// this needs to be added to the viewable players, send PC_ENTER
|
||||
|
||||
Player otherPlr = players[otherSock].plr;
|
||||
Player plr = players[sock].plr;
|
||||
Player *otherPlr = players[otherSock].plr;
|
||||
Player *plr = players[sock].plr;
|
||||
|
||||
newPlayer.PCAppearanceData.iID = plr.iID;
|
||||
newPlayer.PCAppearanceData.iHP = plr.HP;
|
||||
newPlayer.PCAppearanceData.iLv = plr.level;
|
||||
newPlayer.PCAppearanceData.iX = plr.x;
|
||||
newPlayer.PCAppearanceData.iY = plr.y;
|
||||
newPlayer.PCAppearanceData.iZ = plr.z;
|
||||
newPlayer.PCAppearanceData.iAngle = plr.angle;
|
||||
newPlayer.PCAppearanceData.PCStyle = plr.PCStyle;
|
||||
newPlayer.PCAppearanceData.Nano = plr.Nanos[plr.activeNano];
|
||||
newPlayer.PCAppearanceData.iPCState = plr.iPCState;
|
||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, plr.Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||
newPlayer.PCAppearanceData.iID = plr->iID;
|
||||
newPlayer.PCAppearanceData.iHP = plr->HP;
|
||||
newPlayer.PCAppearanceData.iLv = plr->level;
|
||||
newPlayer.PCAppearanceData.iX = plr->x;
|
||||
newPlayer.PCAppearanceData.iY = plr->y;
|
||||
newPlayer.PCAppearanceData.iZ = plr->z;
|
||||
newPlayer.PCAppearanceData.iAngle = plr->angle;
|
||||
newPlayer.PCAppearanceData.PCStyle = plr->PCStyle;
|
||||
newPlayer.PCAppearanceData.Nano = plr->Nanos[plr->activeNano];
|
||||
newPlayer.PCAppearanceData.iPCState = plr->iPCState;
|
||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, plr->Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||
|
||||
otherSock->sendPacket((void*)&newPlayer, P_FE2CL_PC_NEW, sizeof(sP_FE2CL_PC_NEW));
|
||||
|
||||
newPlayer.PCAppearanceData.iID = otherPlr.iID;
|
||||
newPlayer.PCAppearanceData.iHP = otherPlr.HP;
|
||||
newPlayer.PCAppearanceData.iLv = otherPlr.level;
|
||||
newPlayer.PCAppearanceData.iX = otherPlr.x;
|
||||
newPlayer.PCAppearanceData.iY = otherPlr.y;
|
||||
newPlayer.PCAppearanceData.iZ = otherPlr.z;
|
||||
newPlayer.PCAppearanceData.iAngle = otherPlr.angle;
|
||||
newPlayer.PCAppearanceData.PCStyle = otherPlr.PCStyle;
|
||||
newPlayer.PCAppearanceData.Nano = otherPlr.Nanos[otherPlr.activeNano];
|
||||
newPlayer.PCAppearanceData.iPCState = otherPlr.iPCState;
|
||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, otherPlr.Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||
newPlayer.PCAppearanceData.iID = otherPlr->iID;
|
||||
newPlayer.PCAppearanceData.iHP = otherPlr->HP;
|
||||
newPlayer.PCAppearanceData.iLv = otherPlr->level;
|
||||
newPlayer.PCAppearanceData.iX = otherPlr->x;
|
||||
newPlayer.PCAppearanceData.iY = otherPlr->y;
|
||||
newPlayer.PCAppearanceData.iZ = otherPlr->z;
|
||||
newPlayer.PCAppearanceData.iAngle = otherPlr->angle;
|
||||
newPlayer.PCAppearanceData.PCStyle = otherPlr->PCStyle;
|
||||
newPlayer.PCAppearanceData.Nano = otherPlr->Nanos[otherPlr->activeNano];
|
||||
newPlayer.PCAppearanceData.iPCState = otherPlr->iPCState;
|
||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, otherPlr->Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||
|
||||
sock->sendPacket((void*)&newPlayer, P_FE2CL_PC_NEW, sizeof(sP_FE2CL_PC_NEW));
|
||||
|
||||
@@ -158,8 +162,8 @@ std::list<CNSocket*> PlayerManager::getNearbyPlayers(int x, int y, int dist) {
|
||||
std::list<CNSocket*> plrs;
|
||||
|
||||
for (auto pair : players) {
|
||||
int diffX = abs(pair.second.plr.x - x);
|
||||
int diffY = abs(pair.second.plr.x - x);
|
||||
int diffX = abs(pair.second.plr->x - x);
|
||||
int diffY = abs(pair.second.plr->x - x);
|
||||
|
||||
if (diffX < dist && diffY < dist)
|
||||
plrs.push_back(pair.first);
|
||||
@@ -266,12 +270,12 @@ void PlayerManager::movePlayer(CNSocket* sock, CNPacketData* data) {
|
||||
sP_CL2FE_REQ_PC_MOVE* moveData = (sP_CL2FE_REQ_PC_MOVE*)data->buf;
|
||||
updatePlayerPosition(sock, moveData->iX, moveData->iY, moveData->iZ);
|
||||
|
||||
players[sock].plr.angle = moveData->iAngle;
|
||||
players[sock].plr->angle = moveData->iAngle;
|
||||
uint64_t tm = getTime();
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_MOVE, moveResponse);
|
||||
|
||||
moveResponse.iID = players[sock].plr.iID;
|
||||
moveResponse.iID = players[sock].plr->iID;
|
||||
moveResponse.cKeyValue = moveData->cKeyValue;
|
||||
|
||||
moveResponse.iX = moveData->iX;
|
||||
@@ -302,7 +306,7 @@ void PlayerManager::stopPlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_STOP, stopResponse);
|
||||
|
||||
stopResponse.iID = players[sock].plr.iID;
|
||||
stopResponse.iID = players[sock].plr->iID;
|
||||
|
||||
stopResponse.iX = stopData->iX;
|
||||
stopResponse.iY = stopData->iY;
|
||||
@@ -327,7 +331,7 @@ void PlayerManager::jumpPlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_JUMP, jumpResponse);
|
||||
|
||||
jumpResponse.iID = players[sock].plr.iID;
|
||||
jumpResponse.iID = players[sock].plr->iID;
|
||||
jumpResponse.cKeyValue = jumpData->cKeyValue;
|
||||
|
||||
jumpResponse.iX = jumpData->iX;
|
||||
@@ -358,7 +362,7 @@ void PlayerManager::jumppadPlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_JUMPPAD, jumppadResponse);
|
||||
|
||||
jumppadResponse.iPC_ID = players[sock].plr.iID;
|
||||
jumppadResponse.iPC_ID = players[sock].plr->iID;
|
||||
jumppadResponse.cKeyValue = jumppadData->cKeyValue;
|
||||
|
||||
jumppadResponse.iX = jumppadData->iX;
|
||||
@@ -387,7 +391,7 @@ void PlayerManager::launchPlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_LAUNCHER, launchResponse);
|
||||
|
||||
launchResponse.iPC_ID = players[sock].plr.iID;
|
||||
launchResponse.iPC_ID = players[sock].plr->iID;
|
||||
|
||||
launchResponse.iX = launchData->iX;
|
||||
launchResponse.iY = launchData->iY;
|
||||
@@ -417,7 +421,7 @@ void PlayerManager::ziplinePlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_ZIPLINE, ziplineResponse);
|
||||
|
||||
ziplineResponse.iPC_ID = players[sock].plr.iID;
|
||||
ziplineResponse.iPC_ID = players[sock].plr->iID;
|
||||
ziplineResponse.iCliTime = ziplineData->iCliTime;
|
||||
ziplineResponse.iSvrTime = tm;
|
||||
ziplineResponse.iX = ziplineData->iX;
|
||||
@@ -454,7 +458,7 @@ void PlayerManager::movePlatformPlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_MOVEPLATFORM, platResponse);
|
||||
|
||||
platResponse.iPC_ID = players[sock].plr.iID;
|
||||
platResponse.iPC_ID = players[sock].plr->iID;
|
||||
platResponse.iCliTime = platformData->iCliTime;
|
||||
platResponse.iSvrTime = tm;
|
||||
platResponse.iX = platformData->iX;
|
||||
@@ -488,7 +492,7 @@ void PlayerManager::moveSlopePlayer(CNSocket* sock, CNPacketData* data) {
|
||||
|
||||
INITSTRUCT(sP_FE2CL_PC_SLOPE, slopeResponse);
|
||||
|
||||
slopeResponse.iPC_ID = players[sock].plr.iID;
|
||||
slopeResponse.iPC_ID = players[sock].plr->iID;
|
||||
slopeResponse.iCliTime = slopeData->iCliTime;
|
||||
slopeResponse.iSvrTime = tm;
|
||||
slopeResponse.iX = slopeData->iX;
|
||||
@@ -570,17 +574,17 @@ void PlayerManager::revivePlayer(CNSocket* sock, CNPacketData* data) {
|
||||
if (data->size != sizeof(sP_CL2FE_REQ_PC_REGEN))
|
||||
return;
|
||||
|
||||
Player plr = PlayerManager::getPlayer(sock);
|
||||
Player *plr = PlayerManager::getPlayer(sock);
|
||||
|
||||
// players respawn at same spot they died at for now...
|
||||
sP_CL2FE_REQ_PC_REGEN* reviveData = (sP_CL2FE_REQ_PC_REGEN*)data->buf;
|
||||
INITSTRUCT(sP_FE2CL_REP_PC_REGEN_SUCC, response);
|
||||
response.bMoveLocation = reviveData->eIL;
|
||||
response.PCRegenData.iMapNum = reviveData->iIndex;
|
||||
response.PCRegenData.iHP = 1000 * plr.level;
|
||||
response.PCRegenData.iX = plr.x;
|
||||
response.PCRegenData.iY = plr.y;
|
||||
response.PCRegenData.iZ = plr.z;
|
||||
response.PCRegenData.iHP = 1000 * plr->level;
|
||||
response.PCRegenData.iX = plr->x;
|
||||
response.PCRegenData.iY = plr->y;
|
||||
response.PCRegenData.iZ = plr->z;
|
||||
|
||||
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_REGEN_SUCC, sizeof(sP_FE2CL_REP_PC_REGEN_SUCC));
|
||||
}
|
||||
@@ -598,8 +602,7 @@ void PlayerManager::enterPlayerVehicle(CNSocket* sock, CNPacketData* data) {
|
||||
otherSock->sendPacket((void*)&pkt, P_FE2CL_PC_EQUIP_CHANGE, sizeof(sP_FE2CL_PC_EQUIP_CHANGE));
|
||||
}
|
||||
|
||||
plrv.plr.iPCState = 8;
|
||||
updatePlayer(sock, plrv.plr);
|
||||
plrv.plr->iPCState = 8;
|
||||
|
||||
sock->sendPacket((void*)&response, P_FE2CL_PC_VEHICLE_ON_SUCC, sizeof(sP_FE2CL_PC_VEHICLE_ON_SUCC));
|
||||
}
|
||||
@@ -617,8 +620,7 @@ void PlayerManager::exitPlayerVehicle(CNSocket* sock, CNPacketData* data) {
|
||||
otherSock->sendPacket((void*)&pkt, P_FE2CL_PC_EQUIP_CHANGE, sizeof(sP_FE2CL_PC_EQUIP_CHANGE));
|
||||
}
|
||||
|
||||
plrv.plr.iPCState = 0;
|
||||
updatePlayer(sock, plrv.plr);
|
||||
plrv.plr->iPCState = 0;
|
||||
|
||||
sock->sendPacket((void*)&response, P_FE2CL_PC_VEHICLE_OFF_SUCC, sizeof(sP_FE2CL_PC_VEHICLE_OFF_SUCC));
|
||||
}
|
||||
@@ -633,14 +635,7 @@ void PlayerManager::setSpecialSwitchPlayer(CNSocket* sock, CNPacketData* data) {
|
||||
}
|
||||
|
||||
#pragma region Helper methods
|
||||
Player PlayerManager::getPlayer(CNSocket* key) {
|
||||
Player *PlayerManager::getPlayer(CNSocket* key) {
|
||||
return players[key].plr;
|
||||
}
|
||||
|
||||
void PlayerManager::updatePlayer(CNSocket* key, Player plr) {
|
||||
PlayerView plrv = players[key];
|
||||
plrv.plr = plr;
|
||||
|
||||
players[key] = plrv;
|
||||
}
|
||||
#pragma endregion
|
||||
#pragma endregion
|
||||
|
Reference in New Issue
Block a user