From 7f9cdfc9ae7ad0321fb478675606e9072b83172c Mon Sep 17 00:00:00 2001 From: dongresource Date: Sat, 20 Mar 2021 02:23:53 +0100 Subject: [PATCH] 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. --- src/Chunking.cpp | 16 ++++++++-------- src/CustomCommands.cpp | 8 ++++---- src/Eggs.cpp | 2 +- src/MobAI.cpp | 4 ++-- src/NPC.hpp | 3 +-- src/NPCManager.cpp | 3 +-- src/Player.hpp | 4 ++-- src/PlayerManager.cpp | 6 +----- src/PlayerManager.hpp | 2 +- src/Vendor.cpp | 18 +++++++++--------- 10 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/Chunking.cpp b/src/Chunking.cpp index c9a5563..cdd52a6 100644 --- a/src/Chunking.cpp +++ b/src/Chunking.cpp @@ -26,9 +26,9 @@ static void newChunk(ChunkPos pos) { std::set 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) { diff --git a/src/CustomCommands.cpp b/src/CustomCommands.cpp index dd868d8..4240519 100644 --- a/src/CustomCommands.cpp +++ b/src/CustomCommands.cpp @@ -249,7 +249,7 @@ static void summonWCommand(std::string full, std::vector& args, CNS static void unsummonWCommand(std::string full, std::vector& 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& args, CN static void npcRotateCommand(std::string full, std::vector& 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& 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& args, CNSoc static void whoisCommand(std::string full, std::vector& 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"); diff --git a/src/Eggs.cpp b/src/Eggs.cpp index 54edf58..47805f4 100644 --- a/src/Eggs.cpp +++ b/src/Eggs.cpp @@ -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 diff --git a/src/MobAI.cpp b/src/MobAI.cpp index 410d890..19895bd 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -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 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); diff --git a/src/NPC.hpp b/src/NPC.hpp index 255a55f..6d117b1 100644 --- a/src/NPC.hpp +++ b/src/NPC.hpp @@ -8,7 +8,7 @@ public: NPCClass npcClass; uint64_t instanceID; ChunkPos chunkPos; - std::set* viewableChunks; + std::set viewableChunks; int playersInView; @@ -29,7 +29,6 @@ public: instanceID = iID; chunkPos = std::make_tuple(0, 0, 0); - viewableChunks = new std::set(); 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) { diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 4985983..36560ed 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -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); diff --git a/src/Player.hpp b/src/Player.hpp index ee11559..a8f1693 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -83,10 +83,10 @@ struct Player { uint64_t iFirstUseFlag[2]; ChunkPos chunkPos; - std::set *viewableChunks; + std::set viewableChunks; time_t lastHeartbeat; int suspicionRating; time_t lastShot; - std::vector *buyback; + std::vector buyback; }; diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 434f807..f7e1915 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -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(); p->lastHeartbeat = 0; - p->buyback = new std::vector(); 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) diff --git a/src/PlayerManager.hpp b/src/PlayerManager.hpp index c198baf..48807af 100644 --- a/src/PlayerManager.hpp +++ b/src/PlayerManager.hpp @@ -38,7 +38,7 @@ namespace PlayerManager { template 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) diff --git a/src/Vendor.cpp b/src/Vendor.cpp index cb40391..d8d70a7 100644 --- a/src/Vendor.cpp +++ b/src/Vendor.cpp @@ -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);