#include "connection.h" #include "vector.h" using std::min; using std::max; Connection::Connection(boost::asio::io_service& io_service_) : io_service(io_service_), socket(io_service_) { } float Connection::distance(float px, float pz) { bool in_width = px > pos.x && px < pos.x+1; bool in_height = pz > pos.z && pz < pos.z+1; if(in_width && in_height) return 0; Vector2 p(px, pz); float a = (p - pos.xz()).length(); float b = (p - (pos.xz() + Vector2(1, 0))).length(); float c = (p - (pos.xz() + Vector2(0, 1))).length(); float d = (p - (pos.xz() + Vector2(1, 1))).length(); float dist = min(min(min(a, b), c), d); if(in_width) dist = min(dist, (float)min(abs(pos.z - pz), abs(pos.z+1 - pz))); if(in_height) dist = min(dist, (float)min(abs(pos.x - px), abs(pos.x+1 - px))); return dist; } std::set > Connection::check_chunks() { // TODO: make configurable const int chunk_size = 32; const int chunk_dist_threshold = chunk_size*4; // list of chunks client should hold std::set > new_chunks; int i = pos.x - chunk_dist_threshold; i -= i % chunk_size; for(; i < pos.x + chunk_dist_threshold; i += chunk_size) { int j = pos.z - chunk_dist_threshold; j -= j % chunk_size; for(; j < pos.z + chunk_dist_threshold; j += chunk_size) { if(chunk_indices.find(std::pair(i, j)) != chunk_indices.end()) continue; float a = i - pos.x + chunk_size/2; float b = j - pos.z + chunk_size/2; if(sqrtf(a*a + b*b) < chunk_dist_threshold) { chunk_indices.insert(std::pair(i, j)); new_chunks.insert(std::pair(i, j)); } } } return new_chunks; }