summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/drawing.lua2
-rw-r--r--client/main.lua10
-rw-r--r--common/chunk.lua33
-rw-r--r--server/server.lua4
4 files changed, 38 insertions, 11 deletions
diff --git a/client/drawing.lua b/client/drawing.lua
index c8be5fd..962fdb7 100644
--- a/client/drawing.lua
+++ b/client/drawing.lua
@@ -55,7 +55,7 @@ local function draw_chunk(camera,the_chunk)
local maxq = minq+(trh.q-tlh.q)+1
for q = minq,maxq do
local h = coords.Hex.make(q,r)
- local t = the_chunk:tile_at_offset(h)
+ local t = the_chunk:at(h)
if t then
draw_hex(h:to_pos())
end
diff --git a/client/main.lua b/client/main.lua
index b2e2b3f..819753f 100644
--- a/client/main.lua
+++ b/client/main.lua
@@ -11,6 +11,7 @@ local drawing = require"drawing"
local coords = require"common.coords"
local Pos = coords.Pos
local camera = require"camera".Camera.make()
+local Chunk = require"common.chunk".Chunk
-- local pprint=require"common.pprint"
-- pprint.setup{show_all=true}
@@ -20,7 +21,7 @@ math.randomseed(os.time())
local host,peer
-local chunk = require"common.chunk".Chunk.make()
+local chunk
local function draw_player(pl,islocal)
local hplsz = PLAYER_SIZE/2
@@ -78,6 +79,7 @@ function love.update(dt)
repeat
local ev = host:service()
if ev and ev.type == "receive" then
+ print(ev.data)
local j = json.decode(ev.data)
local op = j.t
if op == "join" then
@@ -94,6 +96,8 @@ function love.update(dt)
elseif op == "you" then
local pl = j.pl
local_player = {pos=coords.Pos.make(pl.x,pl.y),color=pl.color,id=pl.id}
+ elseif op == "chunk" then
+ chunk = Chunk.from_packet_data(j)
end
end
until not ev
@@ -107,7 +111,9 @@ function love.draw()
end
camera:apply_trans()
- drawing.draw_chunk(camera,chunk)
+ if chunk then
+ drawing.draw_chunk(camera,chunk)
+ end
if local_player then
draw_player(local_player,true)
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 {
diff --git a/server/server.lua b/server/server.lua
index 12d70e5..42ed41d 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -1,5 +1,6 @@
local enet = require"enet"
local json = require"common.dkjson"
+local Chunk = require"common.chunk".Chunk
local unpack = unpack or table.unpack
math.randomseed(os.time())
@@ -56,6 +57,8 @@ local function player_move_packet(player,x,y)
return json.encode{t="move",id=player.id,x=x,y=y}
end
+local the_chunk = Chunk.gen()
+
while true do
local ev = host:service(100)
if ev then
@@ -64,6 +67,7 @@ while true do
table.insert(playerlist,player)
print("connect",player.peer,player.id)
player.peer:send(player_you_packet(player))
+ player.peer:send(the_chunk:data_packet())
for i,otherplayer in ipairs(playerlist) do
if otherplayer ~= player then