From ef7848921174412d68e70190eb58b9ce1687ff12 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 13 Feb 2023 16:36:09 +0000 Subject: run length encoding for chunk data, more reasonable player speeds, controls help menu --- client/main.lua | 30 +++++++++++++++++++----------- client/util.lua | 1 + common/chunk.lua | 5 +++-- common/rle.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 common/rle.lua diff --git a/client/main.lua b/client/main.lua index 244925e..6692e13 100644 --- a/client/main.lua +++ b/client/main.lua @@ -21,6 +21,14 @@ local movement = require"movement" -- pprint.setup{show_all=true} +local help_text = [[ +controls: +wasd: move left mouse: place +q,e: zoom in/out right mouse: destroy +shift: sprint +F3: toggle debug +F1: show/hide this help]] + math.randomseed(os.time()) local map = Map:make() @@ -29,8 +37,11 @@ local host,peer _G.debugmode = false +local show_controls = true + function love.keypressed(key,scancode,isrepeat) if scancode == "f3" then _G.debugmode = not _G.debugmode end + if scancode == "f1" then show_controls = not show_controls end end local function draw_player(pl,islocal) @@ -48,8 +59,9 @@ end local remote_players = {} local function update_local_player(pl,dt) - local SPEED = 10 -- units/sec - if love.keyboard.isScancodeDown("lshift") then SPEED = 100 end + local SPEED = 8*math.sqrt(3) -- 8 hexagonheights per second + + if love.keyboard.isScancodeDown("lshift") then SPEED = SPEED*2 end local function kd(code) if love.keyboard.isScancodeDown(code) then return 1 else return 0 end end @@ -183,8 +195,6 @@ function love.draw() local wm = camera:screen_to_world(sm) local hm = wm:to_hex() - sdf_d,sdf_gx,sdf_gy = movement.hex_sdgf(wm, coords.Hex:make(3,3)) - love.graphics.origin() if _G.debugmode and local_player then util.print_good({ @@ -198,24 +208,22 @@ function love.draw() "voob "..tostring(camera.zoom), "-", "fps "..tostring(love.timer.getFPS()), - "-", - "sdf "..tostring(sdf_d) },10,10) - love.graphics.setColor(0,1,0) - love.graphics.setLineWidth(5) - love.graphics.line(sm.x,sm.y, sm.x+50*sdf_gx, sm.y+50*sdf_gy) + end + if show_controls then + util.print_good(help_text,300,200) end end function love.load() - require"profile".start(10,io.open("./trace","w")) + -- require"profile".start(10,io.open("./trace","w")) host = enet.host_create() peer = host:connect(SERVER_HOSTNAME..":8473") end function love.quit() - require"profile".stop() + -- require"profile".stop() peer:disconnect() host:flush() end diff --git a/client/util.lua b/client/util.lua index 1d9b753..f9ec088 100644 --- a/client/util.lua +++ b/client/util.lua @@ -2,6 +2,7 @@ local font = love.graphics.getFont() local text = love.graphics.newText(font) local function print_good(lines,x,y) + if type(lines) ~= "table" then lines = {lines} end for _,line in ipairs(lines) do text:set(line) local w,h = text:getDimensions() diff --git a/common/chunk.lua b/common/chunk.lua index 39fcaaf..563723e 100644 --- a/common/chunk.lua +++ b/common/chunk.lua @@ -1,6 +1,7 @@ local json = require"common.dkjson" local class = require"common.class" local coords = require"common.coords" +local rle = require"common.rle" local CHUNK_SIZE = require"common.constants".CHUNK_SIZE @@ -32,14 +33,14 @@ function Chunk.set_at(self,hoffs,tile) end function Chunk.data_packet(self) - return json.encode{t="chunk",tiles=self.tiles,u=self.cp.u,v=self.cp.v} + return json.encode{t="chunk",tiles=rle.encode(self.tiles),u=self.cp.u,v=self.cp.v} end function Chunk.from_packet_data(cls,packet) -- assuming packet has already been json.decoded -- since otherwise how would we know it's a chunk packet local cp = coords.ChunkPos:make(packet.u,packet.v) - return cls:make(cp,packet.tiles) + return cls:make(cp,rle.decode(packet.tiles)) end return { diff --git a/common/rle.lua b/common/rle.lua new file mode 100644 index 0000000..2944c4c --- /dev/null +++ b/common/rle.lua @@ -0,0 +1,50 @@ +-- run length encoding + +local function encode(l) + local out = {} + + local last = l[1] + local count = 1 + + local function ap() + if count == 1 then + table.insert(out,last) + elseif count == 2 then + table.insert(out,last) + table.insert(out,last) + else + table.insert(out,{count,last}) + end + end + + for ix=2,#l do + local val = l[ix] + if val == last then + count = count + 1 + else + ap() + last = val + count = 1 + end + end + + ap() + + return out +end + +local function decode(l) + local out = {} + for _,r in ipairs(l) do + if type(r) == "table" and #r == 2 then + for i = 1,r[1] do + table.insert(out,r[2]) + end + else + table.insert(out,r) + end + end + return out +end + +return {encode=encode,decode=decode} -- cgit v1.2.3