diff --git a/src/ChunkManager.cpp b/src/ChunkManager.cpp index 413159e..798a7e4 100644 --- a/src/ChunkManager.cpp +++ b/src/ChunkManager.cpp @@ -14,7 +14,10 @@ void ChunkManager::newChunk(std::tuple pos) { chunk->players = std::set(); chunk->NPCs = std::set(); - // add the new chunk to every player and mob that's near it + chunks[pos] = chunk; +} + +void ChunkManager::populateNewChunk(Chunk* chunk, std::tuple pos) {// add the new chunk to every player and mob that's near it for (Chunk *c : grabChunks(pos)) { if (c == chunk) continue; @@ -25,32 +28,52 @@ void ChunkManager::newChunk(std::tuple pos) { for (int32_t id : c->NPCs) NPCManager::NPCs[id]->currentChunks.push_back(chunk); } - - chunks[pos] = chunk; } void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) { std::tuple pos = grabChunk(posX, posY, instanceID); + bool newChunkUsed = false; + // make chunk if it doesn't exist! - if (chunks.find(pos) == chunks.end()) + if (chunks.find(pos) == chunks.end()) { newChunk(pos); + newChunkUsed = true; + } Chunk* chunk = chunks[pos]; + if (newChunkUsed) + NPCManager::NPCs[id]->currentChunks.push_back(chunk); + chunk->NPCs.insert(id); + + // we must update other players after the NPC is added to chunk + if (newChunkUsed) + populateNewChunk(chunk, pos); } void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock) { std::tuple pos = grabChunk(posX, posY, instanceID); + bool newChunkUsed = false; + // make chunk if it doesn't exist! - if (chunks.find(pos) == chunks.end()) + if (chunks.find(pos) == chunks.end()) { newChunk(pos); + newChunkUsed = true; + } Chunk* chunk = chunks[pos]; + if (newChunkUsed) + PlayerManager::players[sock].currentChunks.push_back(chunk); + chunk->players.insert(sock); + + // we must update other players after this player is added to chunk + if (newChunkUsed) + populateNewChunk(chunk, pos); } bool ChunkManager::removePlayer(std::tuple chunkPos, CNSocket* sock) { diff --git a/src/ChunkManager.hpp b/src/ChunkManager.hpp index db6633d..2c5e986 100644 --- a/src/ChunkManager.hpp +++ b/src/ChunkManager.hpp @@ -27,6 +27,7 @@ namespace ChunkManager { extern std::map, Chunk*> chunks; void newChunk(std::tuple pos); + void populateNewChunk(Chunk* chunk, std::tuple 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(std::tuple chunkPos, CNSocket* sock); diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 6faaa8b..8573909 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -222,9 +222,9 @@ void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t ins // now, add all the new npcs & players! addPlayerToChunks(ChunkManager::getDeltaChunks(allChunks, view.currentChunks), sock); - ChunkManager::addPlayer(X, Y, view.plr->instanceID, sock); // takes care of adding the player to the chunk if it exists or not view.chunkPos = newPos; view.currentChunks = allChunks; + ChunkManager::addPlayer(X, Y, view.plr->instanceID, sock); // takes care of adding the player to the chunk if it exists or not } void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I) {