From 0d86a5cf4b84e03af518d4b8dceb2385672032a8 Mon Sep 17 00:00:00 2001
From: ubq323 <ubq323@ubq323.website>
Date: Mon, 6 Feb 2023 12:19:58 +0000
Subject: clientside chunk unloading when not near. also refactor coordinate
 things slightly

---
 common/coords.lua | 15 +++++++++++++++
 common/map.lua    | 16 ++++++++++++++++
 2 files changed, 31 insertions(+)

(limited to 'common')

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}
-- 
cgit v1.2.3