summaryrefslogtreecommitdiff
path: root/connection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'connection.cpp')
-rw-r--r--connection.cpp59
1 files changed, 59 insertions, 0 deletions
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<std::pair<int64_t, int64_t> > 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<std::pair<int64_t, int64_t> > 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<int64_t, int64_t>(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<int64_t, int64_t>(i, j));
+ new_chunks.insert(std::pair<int64_t, int64_t>(i, j));
+ }
+ }
+ }
+
+ return new_chunks;
+}