aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-14 01:17:44 +0100
committerubq323 <ubq323@ubq323.website>2024-04-14 01:17:44 +0100
commitad6b3f23824c4d613f42d7d171f1bd31e0edded8 (patch)
tree267f8d2a52a09fb0f89cc70e5ada289e55168a27
parent6cff8d620cd92f9ddc63c6891b3e669b5294619e (diff)
start of request chunks from client
-rw-r--r--client/main.ha42
-rw-r--r--todo4
2 files changed, 38 insertions, 8 deletions
diff --git a/client/main.ha b/client/main.ha
index de6b2fd..9f43aaf 100644
--- a/client/main.ha
+++ b/client/main.ha
@@ -26,7 +26,7 @@ export fn main() void = {
let offs: []pos = [];
for (let x = -1; x < 3; x+=1) for (let y = -1; y < 3; y+=1)
- append(offs, (1024+x*CHUNKSIZE,y*CHUNKSIZE));
+ append(offs, (1024*1024+x*CHUNKSIZE,y*CHUNKSIZE));
let pictures: []drawing::picture = alloc([],NCHUNKS);
let picture_surfaces: []*sdl2::SDL_Surface = alloc([], NCHUNKS);
@@ -48,11 +48,7 @@ export fn main() void = {
fmt::fatal("couldn't connect to server:",net::strerror(err));
};
- for (let i = 0; i < 3; i += 1) {
- packet_reader::send(conn, (
- i*CHUNKSIZE,i*CHUNKSIZE
- ):packet_reader::packet_reqchunk)!;
- };
+
const pollfd: [1]poll::pollfd = [ poll::pollfd {
fd = conn, events=poll::event::POLLIN, revents = 0
@@ -70,6 +66,7 @@ export fn main() void = {
// in WORLD coords
let mouse_pos: pos = (0,0);
let mouse_down = false;
+ request_visible_chunks(pictures, conn, camera_pos);
for (!quit) {
const did_move = do_movement(&camera_pos);
@@ -218,5 +215,36 @@ fn find_picture_for_chunkdata(camera_pos: pos, world_pos: pos, pictures: []drawi
};
-
+
+fn floor_div(a: i32, b: i32) i32 = {
+ if (a < 0) return -1-floor_div(-(a+1),b);
+ return a / b;
+};
+fn find_chunk(pictures: []drawing::picture, world_pos: pos)
+ nullable *drawing::picture = {
+ for (const pic &.. pictures) {
+ if (pic.world_pos.0 == world_pos.0
+ && pic.world_pos.1 == world_pos.1) {
+ return pic;
+ };
+ };
+ return null;
+};
+
+fn request_visible_chunks(pictures: []drawing::picture, conn: net::socket, camera_pos: pos) void = {
+ const x0 = CHUNKSIZE*floor_div(camera_pos.0,CHUNKSIZE);
+ const y0 = CHUNKSIZE*floor_div(camera_pos.1,CHUNKSIZE);
+
+ for (let dx = 0i32; dx < 3; dx += 1) for (let dy = 0i32; dy < 3; dy += 1) {
+ let world_pos = (x0+CHUNKSIZE*dx, y0+CHUNKSIZE*dy): pos;
+ fmt::printfln("investigating {},{}", world_pos.0, world_pos.1)!;
+ match (find_chunk(pictures,world_pos)) {
+ case *drawing::picture => yield;
+ case null =>
+ fmt::printfln("requesting {},{}", world_pos.0, world_pos.1)!;
+ packet_reader::send(conn, world_pos: packet_reader::packet_reqchunk)!;
+ };
+ };
+};
+
diff --git a/todo b/todo
index 58de6a7..15f4f09 100644
--- a/todo
+++ b/todo
@@ -1,2 +1,4 @@
multiple chunks on server
-server periodically unloads chunks not near any player
+not showing all 3x3 area
+server crashes when no chunks loaded
+