summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/chunk.lua33
1 files changed, 25 insertions, 8 deletions
diff --git a/common/chunk.lua b/common/chunk.lua
index 432e37b..e210387 100644
--- a/common/chunk.lua
+++ b/common/chunk.lua
@@ -1,17 +1,26 @@
+local json = require"common.dkjson"
+
local CHUNK_SIZE = 128
-- for now tiles shall be booleans
+local function index_ok(offq,offr)
+ return 0<=offq and 0<=offr and offq<CHUNK_SIZE and offr<CHUNK_SIZE
+end
+
local function index(offq,offr)
-- indexes start at 0
-- and go up to (CHUNK_SIZE^2)-1
- assert(0<=offq and 0<=offr and offq<CHUNK_SIZE and offr<CHUNK_SIZE, "chunk hex offset out of bounds")
- return CHUNK_SIZE*offq + offr
+ assert(index_ok(offq,offr), "chunk hex offset out of bounds")
+ return CHUNK_SIZE*offq + offr + 1
end
local Chunk = {}
Chunk.__index = Chunk
-function Chunk.make()
+function Chunk.make(tiles)
+ return setmetatable({tiles=tiles},Chunk)
+end
+function Chunk.gen()
-- todo actual worldgen
local tiles = {}
for i=0,CHUNK_SIZE-1 do
@@ -20,12 +29,20 @@ function Chunk.make()
end
end
- return setmetatable({tiles=tiles},Chunk)
+ return Chunk.make(tiles)
+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)]
+end
+
+function Chunk.data_packet(self)
+ return json.encode{t="chunk",tiles=self.tiles}
end
-function Chunk.tile_at_offset(self,hoffs)
- if not(0<=hoffs.q and 0<=hoffs.r and hoffs.q<CHUNK_SIZE and hoffs.r<CHUNK_SIZE) then return nil end
- local idx = CHUNK_SIZE*hoffs.q + hoffs.r
- return self.tiles[idx]
+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)
end
return {