diff options
author | ubq323 <ubq323@ubq323.website> | 2023-01-31 00:30:47 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-01-31 00:30:47 +0000 |
commit | 91a22ac34a3e6dc8536298537796e101424a430c (patch) | |
tree | 2bcdd770d695ff0a3fef8ba33590029754e4cc3f /common | |
parent | b851e37a5a44e2c6a022f1c87c3d2da494ccb0e6 (diff) |
terrain data is sent from server to client
Diffstat (limited to 'common')
-rw-r--r-- | common/chunk.lua | 33 |
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 { |