summaryrefslogtreecommitdiff
path: root/common/chunk.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-02-04 23:03:19 +0000
committerubq323 <ubq323@ubq323.website>2023-02-04 23:03:19 +0000
commit0dc1276df57aa16b4f0eaecf54fb5cd8f00115c6 (patch)
tree0d5672f6f05f56022ed834ad35c1c2b2df52c21c /common/chunk.lua
parent1ebd7d9b7b62c8e05d527611a1944ed1a876b890 (diff)
many many optimizations and refactorings; introduction of Map to support multiple chunks, modify worldgen and client drawing to support multiple chunks
Diffstat (limited to 'common/chunk.lua')
-rw-r--r--common/chunk.lua23
1 files changed, 13 insertions, 10 deletions
diff --git a/common/chunk.lua b/common/chunk.lua
index 1d3faa6..affdab8 100644
--- a/common/chunk.lua
+++ b/common/chunk.lua
@@ -1,9 +1,8 @@
local json = require"common.dkjson"
local class = require"common.class"
+local coords = require"common.coords"
-local CHUNK_SIZE = 64
-
--- for now tiles shall be booleans
+local CHUNK_SIZE = require"common.constants".CHUNK_SIZE
local function index_ok(offq,offr)
return 0<=offq and 0<=offr and offq<CHUNK_SIZE and offr<CHUNK_SIZE
@@ -17,12 +16,15 @@ local function index(offq,offr)
end
local Chunk = class()
-function Chunk.make(cls,tiles)
- return setmetatable({tiles=tiles},cls)
+function Chunk.make(cls,cp,tiles)
+ return setmetatable({cp=cp,tiles=tiles},cls)
end
function Chunk.at(self,hoffs)
- if not index_ok(hoffs.q,hoffs.r) then return nil end
- return self.tiles[index(hoffs.q,hoffs.r)]
+ return self:_atqr(hoffs.q,hoffs.r)
+end
+function Chunk._atqr(self,q,r)
+ if not index_ok(q,r) then return nil end
+ return self.tiles[index(q,r)]
end
function Chunk.set_at(self,hoffs,tile)
if not index_ok(hoffs.q,hoffs.r) then return end
@@ -30,17 +32,18 @@ function Chunk.set_at(self,hoffs,tile)
end
function Chunk.data_packet(self)
- return json.encode{t="chunk",tiles=self.tiles}
+ return json.encode{t="chunk",tiles=self.tiles,u=self.cp.u,v=self.cp.v}
end
function Chunk.from_packet_data(packet)
-- assuming packet has already been json.decoded
-- since otherwise how would we know it's a chunk packet
- return Chunk:make(packet.tiles)
+
+ local cp = coords.ChunkPos:make(packet.u,packet.v)
+ return Chunk:make(cp,packet.tiles)
end
return {
Chunk=Chunk,
- SIZE=CHUNK_SIZE,
index=index,
index_ok=index_ok,
}