std::shared_ptr<ChunkBase> World::getChunk(int x, int y, int z) { ChunkPosition cp = ChunkPosition(x, y, z); auto chunk = chunkMap.find(cp); if(chunk == chunkMap.end()) { return shared_ptr<ChunkBase>(new EmptyChunk(GlobalThread::world, x, y, z)); } else { return chunk->second; } }
void ServerConnector::loadAndPruneChunks() { ChunkPosition currentPosition = world().chunkPosition(me()->v_position.x, me()->v_position.z); QList<ChunkPosition> wantedChunks; // The chunks we still want to be active // Create a list of the wanted chunks for(int x = - i_viewDistance; x < i_viewDistance+1; ++x) { for(int z = - i_viewDistance; z < i_viewDistance+1; ++z) { ChunkPosition position = ChunkPosition(currentPosition.first + x, currentPosition.second + z); wantedChunks.push_back(position); } } // Let's see if we have to prune unwanted ones... for (int i = 0; i < m_loadedChunks.size(); ++i) { ChunkPosition processingChunk = m_loadedChunks.at(i); // If the chunk is wanted if(wantedChunks.contains(processingChunk)) { // delete it from the wanted ones wantedChunks.removeOne(processingChunk); } else { // The chunk is unwanted, get rid of it ChunkConnectEvent* event = new ChunkConnectEvent(processingChunk, ChunkConnectEvent::ChunkConnection_Disconnect); emit postEvent(event); // Delete it from the loaded chunks list m_loadedChunks.removeAt(i); i--; } } // Now we load the chunks that were not in the loaded chunks for (int i = 0; i < wantedChunks.size(); ++i) { ChunkPosition processingChunk = wantedChunks.at(i); ChunkConnectEvent* event = new ChunkConnectEvent(processingChunk, ChunkConnectEvent::ChunkConnection_Connect); emit postEvent(event); m_loadedChunks.push_back(processingChunk); } }
ChunkBase::ChunkBase(World& world, int xPos, int yPos, int zPos) : world(&world), pos(ChunkPosition(xPos, yPos, zPos)), loaded(false), unloaded(false), renderUpdateNeeded(false), firstPass(nullptr), secondPass(nullptr) { }