Coords ChunkManager::blockToChunkCoords(Coords in) { int newX = std::floor((float)in.x() / (float)Chunk::chunkWidth); int newY = std::floor((float)in.y() / (float)Chunk::chunkWidth); int newZ = std::floor((float)in.z() / (float)Chunk::chunkHeight); Coords result = Coords(newX, newY, newZ); return result; }
void ChunkManager::setCenterChunk(Coords center, bool force) { if (center == _center && !force) return; _center = center; //Delete out-of-range chunks for (chunkMap_type::iterator it = _chunkMap.begin(); it != _chunkMap.end(); /* No increment */) { if (it->first.dist_squared_2D(center) > _visibility * _visibility) { delete it->second; it = _chunkMap.erase(it); //returns next element } else { it++; } } //load missing chunks for (int x = center.x() - _visibility; x <= center.x() + _visibility; x++) { for (int y = center.y() - _visibility; y <= center.y() + _visibility; y++) { for (int z = 0; z <= BlockGrid::gridHeight / Chunk::chunkHeight; z++) { if (z < 0) z = 0; //ensure we don't go too low Coords coords(x, y, z); if (coords.dist_squared_2D(center) < _visibility * _visibility) { Coords coords(x, y, z); chunkMap_type::iterator it = _chunkMap.find(coords); if (it == _chunkMap.end()) { chunkLoader.requestLoadChunk(coords); //_dirtyChunks.push_front(chunk); } } } } } }