diff options
author | ubq323 <ubq323@ubq323.website> | 2023-02-06 12:19:58 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-02-06 12:19:58 +0000 |
commit | 0d86a5cf4b84e03af518d4b8dceb2385672032a8 (patch) | |
tree | 30778e8a2b380c6bd943303889d1c5243546a81e /client/main.lua | |
parent | 3ad4d4da500a770e4f9cc7aa1bfe42588126a67c (diff) |
clientside chunk unloading when not near. also refactor coordinate things slightly
Diffstat (limited to 'client/main.lua')
-rw-r--r-- | client/main.lua | 32 |
1 files changed, 23 insertions, 9 deletions
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 |