#include "ChunkManager.hpp" #include "PlayerManager.hpp" #include "NPCManager.hpp" #include "settings.hpp" std::map, Chunk*> ChunkManager::chunks; void ChunkManager::init() {} // stubbed void ChunkManager::addNPC(int posX, int posY, int instanceID, int32_t id) { std::tuple pos = grabChunk(posX, posY, instanceID); // make chunk if it doesn't exist! if (chunks.find(pos) == chunks.end()) { chunks[pos] = new Chunk(); chunks[pos]->players = std::set(); chunks[pos]->NPCs = std::set(); } Chunk* chunk = chunks[pos]; chunk->NPCs.insert(id); } void ChunkManager::addPlayer(int posX, int posY, int instanceID, CNSocket* sock) { std::tuple pos = grabChunk(posX, posY, instanceID); // make chunk if it doesn't exist! if (chunks.find(pos) == chunks.end()) { chunks[pos] = new Chunk(); chunks[pos]->players = std::set(); chunks[pos]->NPCs = std::set(); } Chunk* chunk = chunks[pos]; chunk->players.insert(sock); } void ChunkManager::removePlayer(std::tuple chunkPos, CNSocket* sock) { if (!checkChunk(chunkPos)) return; // do nothing if chunk doesn't even exist Chunk* chunk = chunks[chunkPos]; chunk->players.erase(sock); // gone // 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 chunkPos, int32_t id) { if (!checkChunk(chunkPos)) return; // do nothing if chunk doesn't even exist Chunk* chunk = chunks[chunkPos]; chunk->NPCs.erase(id); // gone // 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 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 chunk) { return chunks.find(chunk) != chunks.end(); } std::tuple ChunkManager::grabChunk(int posX, int posY, int instanceID) { return std::make_tuple(posX / (settings::CHUNKSIZE / 3), posY / (settings::CHUNKSIZE / 3), instanceID); } std::vector ChunkManager::grabChunks(std::tuple chunk) { std::vector chnks; chnks.reserve(9); int x, y, inst; std::tie(x, y, inst) = chunk; // grabs surrounding chunks if they exist for (int i = -1; i < 2; i++) { for (int z = -1; z < 2; z++) { std::tuple pos = std::make_tuple(x+i, y+z, inst); // if chunk exists, add it to the vector if (checkChunk(pos)) chnks.push_back(chunks[pos]); } } return chnks; } // returns the chunks that aren't shared (only from from) std::vector ChunkManager::getDeltaChunks(std::vector from, std::vector to) { std::vector delta; for (Chunk* i : from) { bool found = false; // search for it in the other array for (Chunk* z : to) { if (i == z) { found = true; break; } } // add it to the vector if we didn't find it! if (!found) delta.push_back(i); } return delta; } bool ChunkManager::inPopulatedChunks(int posX, int posY, int instanceID) { auto chunk = ChunkManager::grabChunk(posX, posY, instanceID); auto nearbyChunks = ChunkManager::grabChunks(chunk); for (Chunk *c: nearbyChunks) { if (!c->players.empty()) return true; } return false; }