chunks are now cleaned up when not in use

This commit is contained in:
CPunch 2020-10-13 17:30:19 -05:00
parent a8c8065920
commit 843b2e6e38
2 changed files with 48 additions and 2 deletions

View File

@ -45,7 +45,9 @@ void ChunkManager::removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* so
chunk->players.erase(sock); // gone chunk->players.erase(sock); // gone
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views // if players and NPCs are empty, free chunk and remove it from surrounding views
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0)
destroyChunk(chunkPos);
} }
void ChunkManager::removeNPC(std::tuple<int, int, int> chunkPos, int32_t id) { void ChunkManager::removeNPC(std::tuple<int, int, int> chunkPos, int32_t id) {
@ -56,7 +58,50 @@ void ChunkManager::removeNPC(std::tuple<int, int, int> chunkPos, int32_t id) {
chunk->NPCs.erase(id); // gone chunk->NPCs.erase(id); // gone
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views // if players and NPCs are empty, free chunk and remove it from surrounding views
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0)
destroyChunk(chunkPos);
}
void ChunkManager::destroyChunk(std::tuple<int, int, int> chunkPos) {
if (!checkChunk(chunkPos))
return; // chunk doesn't exist, we don't need to do anything
Chunk* chunk = chunks[chunkPos];
// unspawn all of the mobs/npcs
for (uint32_t id : chunk->NPCs) {
NPCManager::destroyNPC(id);
}
// we also need to remove it from all NPCs/Players views
for (Chunk* otherChunk : grabChunks(chunkPos)) {
if (otherChunk == chunk)
continue;
// remove from NPCs
for (uint32_t id : otherChunk->NPCs) {
if (std::find(NPCManager::NPCs[id]->currentChunks.begin(), NPCManager::NPCs[id]->currentChunks.end(), chunk) != NPCManager::NPCs[id]->currentChunks.end()) {
NPCManager::NPCs[id]->currentChunks.erase(std::remove(NPCManager::NPCs[id]->currentChunks.begin(), NPCManager::NPCs[id]->currentChunks.end(), chunk), NPCManager::NPCs[id]->currentChunks.end());
}
}
// remove from players
for (CNSocket* sock : otherChunk->players) {
PlayerView* plyr = &PlayerManager::players[sock];
if (std::find(plyr->currentChunks.begin(), plyr->currentChunks.end(), chunk) != plyr->currentChunks.end()) {
plyr->currentChunks.erase(std::remove(plyr->currentChunks.begin(), plyr->currentChunks.end(), chunk), plyr->currentChunks.end());
}
}
}
assert(chunk->players.size() == 0);
// remove from the map
chunks.erase(chunkPos);
delete chunk;
} }
bool ChunkManager::checkChunk(std::tuple<int, int, int> chunk) { bool ChunkManager::checkChunk(std::tuple<int, int, int> chunk) {

View File

@ -31,6 +31,7 @@ namespace ChunkManager {
void removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* sock); void removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* sock);
void removeNPC(std::tuple<int, int, int> chunkPos, int32_t id); void removeNPC(std::tuple<int, int, int> chunkPos, int32_t id);
bool checkChunk(std::tuple<int, int, int> chunk); bool checkChunk(std::tuple<int, int, int> chunk);
void destroyChunk(std::tuple<int, int, int> chunkPos);
std::tuple<int, int, int> grabChunk(int posX, int posY, int instanceID); std::tuple<int, int, int> grabChunk(int posX, int posY, int instanceID);
std::vector<Chunk*> grabChunks(std::tuple<int, int, int> chunkPos); std::vector<Chunk*> grabChunks(std::tuple<int, int, int> chunkPos);
std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to); std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to);