From 9fa6460f22be5482239d0b903af985a519f77166 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 1 Jun 2011 19:28:24 +0200 Subject: Initial commit. --- connection.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 connection.cpp (limited to 'connection.cpp') diff --git a/connection.cpp b/connection.cpp new file mode 100644 index 0000000..3b5076c --- /dev/null +++ b/connection.cpp @@ -0,0 +1,59 @@ +#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; +} -- cgit v1.2.3