Use direct members instead of pointers for viewableChunks and buyback

We had avoided putting STL containers into Players back when we thought
Players was still POD and needed to remain POD, but it turned out that
neither were the case all along, so there's no need for the indirection.
This commit is contained in:
dongresource 2021-03-20 02:23:53 +01:00
parent 8afe175bd1
commit 7f9cdfc9ae
10 changed files with 30 additions and 36 deletions

View File

@ -26,9 +26,9 @@ static void newChunk(ChunkPos pos) {
std::set<Chunk*> surroundings = getViewableChunks(pos);
for (Chunk* c : surroundings) {
for (CNSocket* sock : c->players)
PlayerManager::getPlayer(sock)->viewableChunks->insert(chunk);
PlayerManager::getPlayer(sock)->viewableChunks.insert(chunk);
for (int32_t id : c->NPCs)
NPCManager::NPCs[id]->viewableChunks->insert(chunk);
NPCManager::NPCs[id]->viewableChunks.insert(chunk);
}
}
@ -45,9 +45,9 @@ static void deleteChunk(ChunkPos pos) {
for(Chunk* c : surroundings)
{
for (CNSocket* sock : c->players)
PlayerManager::getPlayer(sock)->viewableChunks->erase(chunk);
PlayerManager::getPlayer(sock)->viewableChunks.erase(chunk);
for (int32_t id : c->NPCs)
NPCManager::NPCs[id]->viewableChunks->erase(chunk);
NPCManager::NPCs[id]->viewableChunks.erase(chunk);
}
chunks.erase(pos); // remove from map
@ -353,8 +353,8 @@ void Chunking::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
plr->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
plr->viewableChunks->clear();
plr->viewableChunks->insert(newViewables.begin(), newViewables.end());
plr->viewableChunks.clear();
plr->viewableChunks.insert(newViewables.begin(), newViewables.end());
}
void Chunking::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
@ -389,8 +389,8 @@ void Chunking::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
npc->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
npc->viewableChunks->clear();
npc->viewableChunks->insert(newViewables.begin(), newViewables.end());
npc->viewableChunks.clear();
npc->viewableChunks.insert(newViewables.begin(), newViewables.end());
}
bool Chunking::chunkExists(ChunkPos chunk) {

View File

@ -249,7 +249,7 @@ static void summonWCommand(std::string full, std::vector<std::string>& args, CNS
static void unsummonWCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
if (npc == nullptr) {
Chat::sendServerMessage(sock, "/unsummonW: No NPCs found nearby");
@ -331,7 +331,7 @@ static void toggleAiCommand(std::string full, std::vector<std::string>& args, CN
static void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
if (npc == nullptr) {
Chat::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
@ -413,7 +413,7 @@ static void npcInstanceCommand(std::string full, std::vector<std::string>& args,
return;
}
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
if (npc == nullptr) {
Chat::sendServerMessage(sock, "[NPCI] No NPCs found nearby");
@ -650,7 +650,7 @@ static void flushCommand(std::string full, std::vector<std::string>& args, CNSoc
static void whoisCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
if (npc == nullptr) {
Chat::sendServerMessage(sock, "[WHOIS] No NPCs found nearby");

View File

@ -125,7 +125,7 @@ static void eggStep(CNServer* serv, time_t currTime) {
// check dead eggs and eggs in inactive chunks
for (auto egg : Eggs::Eggs) {
if (!egg.second->dead || !Chunking::inPopulatedChunks(egg.second->viewableChunks))
if (!egg.second->dead || !Chunking::inPopulatedChunks(&egg.second->viewableChunks))
continue;
if (egg.second->deadUntil <= timeStamp) {
// respawn it

View File

@ -119,7 +119,7 @@ bool MobAI::aggroCheck(Mob *mob, time_t currTime) {
CNSocket *closest = nullptr;
int closestDistance = INT_MAX;
for (auto it = mob->viewableChunks->begin(); it != mob->viewableChunks->end(); it++) {
for (auto it = mob->viewableChunks.begin(); it != mob->viewableChunks.end(); it++) {
Chunk* chunk = *it;
for (CNSocket *s : chunk->players) {
Player *plr = PlayerManager::getPlayer(s);
@ -296,7 +296,7 @@ static void useAbilities(Mob *mob, time_t currTime) {
std::vector<int> targetData = {0, 0, 0, 0, 0};
// find the players within range of eruption
for (auto it = mob->viewableChunks->begin(); it != mob->viewableChunks->end(); it++) {
for (auto it = mob->viewableChunks.begin(); it != mob->viewableChunks.end(); it++) {
Chunk* chunk = *it;
for (CNSocket *s : chunk->players) {
Player *plr = PlayerManager::getPlayer(s);

View File

@ -8,7 +8,7 @@ public:
NPCClass npcClass;
uint64_t instanceID;
ChunkPos chunkPos;
std::set<Chunk*>* viewableChunks;
std::set<Chunk*> viewableChunks;
int playersInView;
@ -29,7 +29,6 @@ public:
instanceID = iID;
chunkPos = std::make_tuple(0, 0, 0);
viewableChunks = new std::set<Chunk*>();
playersInView = 0;
};
BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id, NPCClass classType) : BaseNPC(x, y, z, angle, iID, type, id) {

View File

@ -66,7 +66,6 @@ void NPCManager::destroyNPC(int32_t id) {
Eggs::Eggs.erase(id);
// finally, remove it from the map and free it
delete entity->viewableChunks;
NPCs.erase(id);
delete entity;
}
@ -86,7 +85,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I,
}
void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) {
for (auto it = npc->viewableChunks->begin(); it != npc->viewableChunks->end(); it++) {
for (auto it = npc->viewableChunks.begin(); it != npc->viewableChunks.end(); it++) {
Chunk* chunk = *it;
for (CNSocket *s : chunk->players) {
s->sendPacket(buf, type, size);

View File

@ -83,10 +83,10 @@ struct Player {
uint64_t iFirstUseFlag[2];
ChunkPos chunkPos;
std::set<Chunk*> *viewableChunks;
std::set<Chunk*> viewableChunks;
time_t lastHeartbeat;
int suspicionRating;
time_t lastShot;
std::vector<sItemBase> *buyback;
std::vector<sItemBase> buyback;
};

View File

@ -35,9 +35,7 @@ static void addPlayer(CNSocket* key, Player plr) {
players[key] = p;
p->chunkPos = std::make_tuple(0, 0, 0);
p->viewableChunks = new std::set<Chunk*>();
p->lastHeartbeat = 0;
p->buyback = new std::vector<sItemBase>();
std::cout << getPlayerName(p) << " has joined!" << std::endl;
std::cout << players.size() << " players" << std::endl;
@ -64,8 +62,6 @@ void PlayerManager::removePlayer(CNSocket* key) {
std::cout << getPlayerName(plr) << " has left!" << std::endl;
delete plr->buyback;
delete plr->viewableChunks;
delete plr;
players.erase(key);
@ -331,7 +327,7 @@ static void enterPlayer(CNSocket* sock, CNPacketData* data) {
void PlayerManager::sendToViewable(CNSocket* sock, void* buf, uint32_t type, size_t size) {
Player* plr = getPlayer(sock);
for (auto it = plr->viewableChunks->begin(); it != plr->viewableChunks->end(); it++) {
for (auto it = plr->viewableChunks.begin(); it != plr->viewableChunks.end(); it++) {
Chunk* chunk = *it;
for (CNSocket* otherSock : chunk->players) {
if (otherSock == sock)

View File

@ -38,7 +38,7 @@ namespace PlayerManager {
template<class T>
void sendToViewable(CNSocket *sock, T& pkt, uint32_t type) {
Player* plr = getPlayer(sock);
for (auto it = plr->viewableChunks->begin(); it != plr->viewableChunks->end(); it++) {
for (auto it = plr->viewableChunks.begin(); it != plr->viewableChunks.end(); it++) {
Chunk* chunk = *it;
for (CNSocket* otherSock : chunk->players) {
if (otherSock == sock)

View File

@ -104,11 +104,11 @@ static void vendorSell(CNSocket* sock, CNPacketData* data) {
}
// add to buyback list
plr->buyback->push_back(original);
plr->buyback.push_back(original);
// forget oldest member if there's more than 5
if (plr->buyback->size() > 5)
plr->buyback->erase(plr->buyback->begin());
//std::cout << (int)plr->buyback->size() << " items in buyback\n";
if (plr->buyback.size() > 5)
plr->buyback.erase(plr->buyback.begin());
//std::cout << (int)plr->buyback.size() << " items in buyback\n";
// response parameters
resp.iInvenSlotNum = req->iInvenSlotNum;
@ -133,13 +133,13 @@ static void vendorBuyback(CNSocket* sock, CNPacketData* data) {
int idx = req->iListID - 1;
// sanity check
if (idx < 0 || idx >= plr->buyback->size()) {
if (idx < 0 || idx >= plr->buyback.size()) {
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL));
return;
}
// get the item out of the buyback list
sItemBase item = (*plr->buyback)[idx];
sItemBase item = plr->buyback[idx];
/*
* NOTE: The client sends the index of the exact item the user clicked on.
* We then operate on that item, but we remove the *first* identical item
@ -149,18 +149,18 @@ static void vendorBuyback(CNSocket* sock, CNPacketData* data) {
* does the exact same thing, so this *is* the correct thing to do to keep
* them in sync.
*/
for (auto it = plr->buyback->begin(); it != plr->buyback->end(); it++) {
for (auto it = plr->buyback.begin(); it != plr->buyback.end(); it++) {
/*
* XXX: we really need a standard item comparison function that
* will work properly across all builds (ex. with iSerial)
*/
if (it->iType == item.iType && it->iID == item.iID && it->iOpt == item.iOpt
&& it->iTimeLimit == item.iTimeLimit) {
plr->buyback->erase(it);
plr->buyback.erase(it);
break;
}
}
//std::cout << (int)plr->buyback->size() << " items in buyback\n";
//std::cout << (int)plr->buyback.size() << " items in buyback\n";
Items::Item* itemDat = Items::getItemData(item.iID, item.iType);