diff --git a/src/CNStructs.hpp b/src/CNStructs.hpp index 2233635..7f8eb85 100644 --- a/src/CNStructs.hpp +++ b/src/CNStructs.hpp @@ -32,8 +32,8 @@ #define MAPNUM(x) ((x) & 0xffffffff) #define PLAYERID(x) ((x) >> 32) -// macro for chunk position type -#define CHUNKPOS std::tuple +// typedef for chunk position tuple +typedef std::tuple ChunkPos; // TODO: rewrite U16toU8 & U8toU16 to not use codecvt diff --git a/src/ChunkManager.cpp b/src/ChunkManager.cpp index 45a9b38..4f37714 100644 --- a/src/ChunkManager.cpp +++ b/src/ChunkManager.cpp @@ -4,11 +4,11 @@ #include "settings.hpp" #include "MobManager.hpp" -std::map ChunkManager::chunks; +std::map ChunkManager::chunks; void ChunkManager::init() {} // stubbed -void ChunkManager::newChunk(CHUNKPOS pos) { +void ChunkManager::newChunk(ChunkPos pos) { Chunk *chunk = new Chunk(); chunk->players = std::set(); @@ -17,7 +17,7 @@ void ChunkManager::newChunk(CHUNKPOS pos) { chunks[pos] = chunk; } -void ChunkManager::populateNewChunk(Chunk* chunk, CHUNKPOS pos) {// add the new chunk to every player and mob that's near it +void ChunkManager::populateNewChunk(Chunk* chunk, ChunkPos pos) {// add the new chunk to every player and mob that's near it for (Chunk *c : grabChunks(pos)) { if (c == chunk) continue; @@ -31,7 +31,7 @@ void ChunkManager::populateNewChunk(Chunk* chunk, CHUNKPOS pos) {// add the new } void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) { - CHUNKPOS pos = grabChunk(posX, posY, instanceID); + ChunkPos pos = grabChunk(posX, posY, instanceID); bool newChunkUsed = false; @@ -54,7 +54,7 @@ void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) { } void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock) { - CHUNKPOS pos = grabChunk(posX, posY, instanceID); + ChunkPos pos = grabChunk(posX, posY, instanceID); bool newChunkUsed = false; @@ -76,7 +76,7 @@ void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* populateNewChunk(chunk, pos); } -bool ChunkManager::removePlayer(CHUNKPOS chunkPos, CNSocket* sock) { +bool ChunkManager::removePlayer(ChunkPos chunkPos, CNSocket* sock) { if (!checkChunk(chunkPos)) return false; // do nothing if chunk doesn't even exist @@ -96,7 +96,7 @@ bool ChunkManager::removePlayer(CHUNKPOS chunkPos, CNSocket* sock) { return false; } -bool ChunkManager::removeNPC(CHUNKPOS chunkPos, int32_t id) { +bool ChunkManager::removeNPC(ChunkPos chunkPos, int32_t id) { if (!checkChunk(chunkPos)) return false; // do nothing if chunk doesn't even exist @@ -116,7 +116,7 @@ bool ChunkManager::removeNPC(CHUNKPOS chunkPos, int32_t id) { return false; } -void ChunkManager::destroyChunk(CHUNKPOS chunkPos) { +void ChunkManager::destroyChunk(ChunkPos chunkPos) { if (!checkChunk(chunkPos)) return; // chunk doesn't exist, we don't need to do anything @@ -158,15 +158,15 @@ void ChunkManager::destroyChunk(CHUNKPOS chunkPos) { delete chunk; } -bool ChunkManager::checkChunk(CHUNKPOS chunk) { +bool ChunkManager::checkChunk(ChunkPos chunk) { return chunks.find(chunk) != chunks.end(); } -CHUNKPOS ChunkManager::grabChunk(int posX, int posY, uint64_t instanceID) { +ChunkPos ChunkManager::grabChunk(int posX, int posY, uint64_t instanceID) { return std::make_tuple(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID); } -std::vector ChunkManager::grabChunks(CHUNKPOS chunk) { +std::vector ChunkManager::grabChunks(ChunkPos chunk) { std::vector chnks; chnks.reserve(9); @@ -177,7 +177,7 @@ std::vector ChunkManager::grabChunks(CHUNKPOS chunk) { // grabs surrounding chunks if they exist for (int i = -1; i < 2; i++) { for (int z = -1; z < 2; z++) { - CHUNKPOS pos = std::make_tuple(x+i, y+z, inst); + ChunkPos pos = std::make_tuple(x+i, y+z, inst); // if chunk exists, add it to the vector if (checkChunk(pos)) @@ -214,8 +214,8 @@ std::vector ChunkManager::getDeltaChunks(std::vector from, std:: /* * inefficient algorithm to get all chunks from a specific instance */ -std::vector ChunkManager::getChunksInMap(uint64_t mapNum) { - std::vector chnks; +std::vector ChunkManager::getChunksInMap(uint64_t mapNum) { + std::vector chnks; for (auto it = ChunkManager::chunks.begin(); it != ChunkManager::chunks.end(); it++) { if (std::get<2>(it->first) == mapNum) { @@ -240,10 +240,10 @@ bool ChunkManager::inPopulatedChunks(int posX, int posY, uint64_t instanceID) { void ChunkManager::createInstance(uint64_t instanceID) { - std::vector templateChunks = ChunkManager::getChunksInMap(MAPNUM(instanceID)); // base instance chunks + std::vector templateChunks = ChunkManager::getChunksInMap(MAPNUM(instanceID)); // base instance chunks if (ChunkManager::getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already std::cout << "Creating instance " << instanceID << std::endl; - for (CHUNKPOS &coords : templateChunks) { + for (ChunkPos &coords : templateChunks) { for (int npcID : chunks[coords]->NPCs) { // make a copy of each NPC in the template chunks and put them in the new instance int newID = NPCManager::nextId++; @@ -268,9 +268,9 @@ void ChunkManager::createInstance(uint64_t instanceID) { void ChunkManager::destroyInstance(uint64_t instanceID) { - std::vector instanceChunks = ChunkManager::getChunksInMap(instanceID); + std::vector instanceChunks = ChunkManager::getChunksInMap(instanceID); std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl; - for (CHUNKPOS& coords : instanceChunks) { + for (ChunkPos& coords : instanceChunks) { destroyChunk(coords); } } @@ -279,9 +279,9 @@ void ChunkManager::destroyInstanceIfEmpty(uint64_t instanceID) { if (PLAYERID(instanceID) == 0) return; // don't clean up overworld/IZ chunks - std::vector sourceChunkCoords = getChunksInMap(instanceID); + std::vector sourceChunkCoords = getChunksInMap(instanceID); - for (CHUNKPOS& coords : sourceChunkCoords) { + for (ChunkPos& coords : sourceChunkCoords) { Chunk* chunk = chunks[coords]; if (chunk->players.size() > 0) diff --git a/src/ChunkManager.hpp b/src/ChunkManager.hpp index 68349bc..ba6815b 100644 --- a/src/ChunkManager.hpp +++ b/src/ChunkManager.hpp @@ -25,20 +25,20 @@ namespace ChunkManager { void init(); void cleanup(); - extern std::map chunks; + extern std::map chunks; - void newChunk(CHUNKPOS pos); - void populateNewChunk(Chunk* chunk, CHUNKPOS pos); + void newChunk(ChunkPos pos); + void populateNewChunk(Chunk* chunk, ChunkPos pos); void addNPC(int posX, int posY, uint64_t instanceID, int32_t id); void addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock); - bool removePlayer(CHUNKPOS chunkPos, CNSocket* sock); - bool removeNPC(CHUNKPOS chunkPos, int32_t id); - bool checkChunk(CHUNKPOS chunk); - void destroyChunk(CHUNKPOS chunkPos); - CHUNKPOS grabChunk(int posX, int posY, uint64_t instanceID); - std::vector grabChunks(CHUNKPOS chunkPos); + bool removePlayer(ChunkPos chunkPos, CNSocket* sock); + bool removeNPC(ChunkPos chunkPos, int32_t id); + bool checkChunk(ChunkPos chunk); + void destroyChunk(ChunkPos chunkPos); + ChunkPos grabChunk(int posX, int posY, uint64_t instanceID); + std::vector grabChunks(ChunkPos chunkPos); std::vector getDeltaChunks(std::vector from, std::vector to); - std::vector getChunksInMap(uint64_t mapNum); + std::vector getChunksInMap(uint64_t mapNum); bool inPopulatedChunks(int posX, int posY, uint64_t instanceID); void createInstance(uint64_t); diff --git a/src/NPC.hpp b/src/NPC.hpp index 3d38028..8b7400b 100644 --- a/src/NPC.hpp +++ b/src/NPC.hpp @@ -8,7 +8,7 @@ public: sNPCAppearanceData appearanceData; NPCClass npcClass; uint64_t instanceID; - CHUNKPOS chunkPos; + ChunkPos chunkPos; std::vector currentChunks; BaseNPC() {}; diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 162cb87..884f496 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -185,7 +185,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) { npc->appearanceData.iY = Y; npc->appearanceData.iZ = Z; - CHUNKPOS newPos = ChunkManager::grabChunk(X, Y, npc->instanceID); + ChunkPos newPos = ChunkManager::grabChunk(X, Y, npc->instanceID); // nothing to be done (but we should also update currentChunks to add/remove stale chunks) if (newPos == npc->chunkPos) { diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 27d6413..040ed4d 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -229,7 +229,7 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) { void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t instanceID) { PlayerView& view = players[sock]; - CHUNKPOS newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID); + ChunkPos newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID); // nothing to be done if (newPos == view.chunkPos) diff --git a/src/PlayerManager.hpp b/src/PlayerManager.hpp index fe24c28..d95a97b 100644 --- a/src/PlayerManager.hpp +++ b/src/PlayerManager.hpp @@ -13,7 +13,7 @@ struct WarpLocation; struct PlayerView { - CHUNKPOS chunkPos; + ChunkPos chunkPos; std::vector currentChunks; Player *plr; time_t lastHeartbeat;