diff options
-rw-r--r-- | client/main.ha | 42 | ||||
-rw-r--r-- | todo | 4 |
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)!; + }; + }; +}; + @@ -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 + |