From 0d86a5cf4b84e03af518d4b8dceb2385672032a8 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 6 Feb 2023 12:19:58 +0000 Subject: clientside chunk unloading when not near. also refactor coordinate things slightly --- client/main.lua | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'client') diff --git a/client/main.lua b/client/main.lua index 769fccf..63c2fc7 100644 --- a/client/main.lua +++ b/client/main.lua @@ -104,19 +104,33 @@ function love.update(dt) send_settile(mh,false) end - -- load chunks near player if local_player then local player_cp = local_player.pos:to_hex():containing_chunk() - for du = -1,1 do - for dv = -1,1 do - local cp = player_cp+coords.ChunkPos:make(du,dv) - if map:chunk(cp) == nil then - print("sending chunk request",cp) - map:mark_chunk_loading(cp) - peer:send(json.encode{t="reqchunk",u=cp.u,v=cp.v}) - end + -- load chunks near to player (within 3x3 square) + for _,cp in ipairs(player_cp:neighborhood()) do + if map:chunk(cp) == nil then + print("sending chunk request",cp) + map:mark_chunk_loading(cp) + peer:send(json.encode{t="reqchunk",u=cp.u,v=cp.v}) end end + + -- unload chunks not near player + -- todo maybe: instead of immediately unloading chunks when we move away, + -- have some kind of 'last near' time, so that if player is moving back and forth, + -- we don't repeatedly unload and reload a given chunk + local to_remove = {} + for cp in map:iter_chunks() do + local d = player_cp:orth_dist(cp) + if d > 1 then + table.insert(to_remove,cp) + end + end + for _,cp in ipairs(to_remove) do + map:remove_chunk(cp) + end + + end -- cgit v1.2.3