summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-02-06 12:19:58 +0000
committerubq323 <ubq323@ubq323.website>2023-02-06 12:19:58 +0000
commit0d86a5cf4b84e03af518d4b8dceb2385672032a8 (patch)
tree30778e8a2b380c6bd943303889d1c5243546a81e
parent3ad4d4da500a770e4f9cc7aa1bfe42588126a67c (diff)
clientside chunk unloading when not near. also refactor coordinate things slightly
-rw-r--r--client/main.lua32
-rw-r--r--common/coords.lua15
-rw-r--r--common/map.lua16
-rw-r--r--server/map.lua1
-rw-r--r--server/noise.lua2
-rw-r--r--server/server.lua5
6 files changed, 60 insertions, 11 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
diff --git a/common/coords.lua b/common/coords.lua
index 111429a..49c2bf2 100644
--- a/common/coords.lua
+++ b/common/coords.lua
@@ -136,6 +136,21 @@ function ChunkPos.extents(self)
local brq,brr = (self.u+1)*CHUNK_SIZE -1, (self.v+1)*CHUNK_SIZE -1
return Hex:make(tlq,tlr), Hex:make(brq,brr)
end
+function ChunkPos.neighborhood(self)
+ -- return all chunkposes within the 3x3 square centered on self, including self
+ local out = {}
+ for du=-1,1 do
+ for dv = -1,1 do
+ table.insert(out,ChunkPos:make(du,dv) + self)
+ end
+ end
+ return out
+end
+function ChunkPos.orth_dist(self,other)
+ local du = math.abs(self.u-other.u)
+ local dv = math.abs(self.v-other.v)
+ return math.max(du,dv)
+end
function ChunkPos.filename(self)
-- filename of chunk with that cp
return "world/c_"..self.u.."_"..self.v..".dat"
diff --git a/common/map.lua b/common/map.lua
index 8161f11..64d353c 100644
--- a/common/map.lua
+++ b/common/map.lua
@@ -10,6 +10,7 @@
local class = require"common.class"
local chunk = require"common.chunk"
+local coords = require"common.coords"
local CHUNK_SIZE = require"common.constants".CHUNK_SIZE
local Map = class()
@@ -66,5 +67,20 @@ function Map.set_at(self,hpos,tile)
if not ch then return nil end
ch:set_at(hoffs,tile)
end
+function Map.iter_chunks(self)
+ -- iterates through all cp,chunk pairs
+ -- chunk might be false
+ -- not guaranteed to be in any particular order
+
+ return coroutine.wrap(function()
+ for u,t in pairs(self.chunks) do
+ for v,ch in pairs(t) do
+ -- if ch is false, won't have a .cp
+ local cp = coords.ChunkPos:make(u,v)
+ coroutine.yield(cp,ch)
+ end
+ end
+ end)
+end
return {Map=Map}
diff --git a/server/map.lua b/server/map.lua
index 845c644..335997a 100644
--- a/server/map.lua
+++ b/server/map.lua
@@ -1,6 +1,7 @@
local Map = require"common.map".Map
local class = require"common.class"
local worldgen = require"worldgen"
+local json = require"common.dkjson"
local MapS = class.extend(Map)
function MapS.obtain(self,cp)
diff --git a/server/noise.lua b/server/noise.lua
index 8a83b60..fe254ac 100644
--- a/server/noise.lua
+++ b/server/noise.lua
@@ -25,7 +25,7 @@ end
function PerlinNoise.vertex(self,ix,iy)
local v = self.grid[ix][iy]
if v then return v end
- vv = random_unit_vec()
+ local vv = random_unit_vec()
self.grid[ix][iy] = vv
return vv
end
diff --git a/server/server.lua b/server/server.lua
index eda6258..725f7f1 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -159,8 +159,11 @@ local ntick = 0
local function tick(ntick)
- if ntick % 30 == 0 then
+ if ntick % 3 == 0 then
print("saving things...")
+ for cp,ch in map:iter_chunks() do
+ print(cp)
+ end
end
end