Save pointer to Player struct in CNSocket.

This is an insignificant optimization now, but will be necessary when we
start switching around the the algorithms and datastructures used in
proximity detection.
This commit is contained in:
dongresource 2020-09-12 01:22:58 +02:00
parent 3865249387
commit 579aa9d31d
2 changed files with 10 additions and 1 deletions

View File

@ -132,6 +132,8 @@ enum ACTIVEKEY {
SOCKETKEY_FE
};
struct Player;
class CNSocket;
typedef void (*PacketHandler)(CNSocket* sock, CNPacketData* data);
@ -153,6 +155,7 @@ private:
public:
SOCKET sock;
PacketHandler pHandler;
Player *plr = nullptr;
CNSocket(SOCKET s, PacketHandler ph);

View File

@ -6,6 +6,8 @@
#include "settings.hpp"
#include <assert.h>
#include <algorithm>
#include <vector>
#include <cmath>
@ -45,6 +47,8 @@ void PlayerManager::addPlayer(CNSocket* key, Player plr) {
players[key].plr = p;
players[key].lastHeartbeat = 0;
key->plr = p;
std::cout << U16toU8(plr.PCStyle.szFirstName) << " " << U16toU8(plr.PCStyle.szLastName) << " has joined!" << std::endl;
std::cout << players.size() << " players" << std::endl;
}
@ -66,6 +70,7 @@ void PlayerManager::removePlayer(CNSocket* key) {
std::cout << U16toU8(cachedView.plr->PCStyle.szFirstName) << " " << U16toU8(cachedView.plr->PCStyle.szLastName) << " has left!" << std::endl;
std::cout << players.size() << " players" << std::endl;
key->plr = nullptr;
delete cachedView.plr;
players.erase(key);
}
@ -692,7 +697,8 @@ void PlayerManager::changePlayerGuide(CNSocket *sock, CNPacketData *data) {
#pragma region Helper methods
Player *PlayerManager::getPlayer(CNSocket* key) {
return players[key].plr;
assert(key->plr != nullptr);
return key->plr;
}
WarpLocation PlayerManager::getRespawnPoint(Player *plr) {