From f55c9f415a081a175e48b51db894f23e59ce47a2 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 18 Jun 2023 22:28:37 +0100 Subject: fix line lengths --- check.sh | 2 ++ client/chunk.lua | 6 ++++-- client/drawing.lua | 3 ++- client/drawing2.lua | 19 +++++++++++++----- client/game.lua | 37 ++++++++++++++++++++++++---------- client/main.lua | 3 ++- client/movement.lua | 10 ++++----- client/profile.lua | 5 ++++- client/util.lua | 2 -- common/chunk.lua | 5 ++++- common/class.lua | 9 +++++---- common/coords.lua | 58 +++++++++++++++++++++++++++++++++++++---------------- common/map.lua | 36 +++++++++++++++++++++------------ server/chunk.lua | 3 +-- server/noise.lua | 16 --------------- 15 files changed, 133 insertions(+), 81 deletions(-) diff --git a/check.sh b/check.sh index 18a93f0..6c3aa34 100755 --- a/check.sh +++ b/check.sh @@ -2,4 +2,6 @@ luacheck . \ --std=love+luajit --no-unused --no-redefined --ignore 611 \ + --max-line-length 70 \ + -q \ --exclude-files '**/dkjson.lua' --exclude-files '*/common/*' diff --git a/client/chunk.lua b/client/chunk.lua index 73fe949..f02277c 100644 --- a/client/chunk.lua +++ b/client/chunk.lua @@ -12,8 +12,10 @@ end local function tile_to_vattr(tile) -- for now tiles are always numbers -- so this just returns the input - -- this function might move to drawing.lua once it does something less trivial - assert(type(tile) == "number","can't send non-numerical tile to gpu") + -- this function might move to drawing.lua + -- once it does something less trivial + assert(type(tile) == "number", + "can't send non-numerical tile to gpu") return tile end function ChunkC.imesh_from_chunk_data(the_chunk) diff --git a/client/drawing.lua b/client/drawing.lua index e7cb45e..7e9b779 100644 --- a/client/drawing.lua +++ b/client/drawing.lua @@ -65,7 +65,8 @@ local function draw_hex(cpos,color,zoom) love.graphics.polygon("fill",_corners) if zoom > zthr0 then love.graphics.setLineWidth(0.1) - love.graphics.setColor(0,0,0,zoom>zthr1 and 1 or (zoom-zthr0)/(zthr1-zthr0)) + local a = zoom > zthr1 and 1 or (zoom-zthr0)/(zthr1-zthr0) + love.graphics.setColor(0,0,0, a) love.graphics.polygon("line",_corners) end end diff --git a/client/drawing2.lua b/client/drawing2.lua index c894e4c..800df78 100644 --- a/client/drawing2.lua +++ b/client/drawing2.lua @@ -21,8 +21,12 @@ do local function ve(n,f) return {cos(th(n)),sin(th(n)), f} end local function vi(n,f) return {ri*cos(th(n)),ri*sin(th(n)), f} end local function vo(n,f) return {ro*cos(th(n)),ro*sin(th(n)), f} end - local function apv(...) for _,x in ipairs({...}) do table.insert(vertices,x) end end - local function apm(...) for _,x in ipairs({...}) do table.insert(map,x) end end + local function apv(...) + for _,x in ipairs({...}) do table.insert(vertices,x) end + end + local function apm(...) + for _,x in ipairs({...}) do table.insert(map,x) end + end vertices[1] = {0,0, 1} for n=0,5 do apv(ve(n,1)) end @@ -88,7 +92,9 @@ vec4 position(mat4 transform_projection, vec4 vertex_position) { - vec2 instance_pos = vec2( floor(love_InstanceID/CHUNK_SIZE), mod(love_InstanceID, CHUNK_SIZE) ); + vec2 instance_pos = vec2( + floor(love_InstanceID/CHUNK_SIZE), + mod(love_InstanceID, CHUNK_SIZE) ); vertex_position.xy += hex_to_pos * instance_pos; float a = clamp( (zoom-zthr0)/zthrd, 0, 1); @@ -104,7 +110,8 @@ vec4 position(mat4 transform_projection, vec4 vertex_position) #pragma language glsl3 varying vec4 tcol; -vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) +vec4 effect(vec4 color, Image tex, + vec2 texture_coords, vec2 screen_coords) { // return vec4(tcol.xyz,0.5); return tcol; @@ -133,7 +140,9 @@ local function draw_map(camera,map) local tl = htl:to_pos() local br = hbr:to_pos() - if br.x < cam_tl.x or cam_br.x < tl.x or br.y < cam_tl.y or cam_br.y < tl.y then + if br.x < cam_tl.x or cam_br.x < tl.x + or br.y < cam_tl.y or cam_br.y < tl.y + then -- definitely not visible on screen -- this does not catch every single nonvisible chunk goto next diff --git a/client/game.lua b/client/game.lua index c5cdffb..2c259a6 100644 --- a/client/game.lua +++ b/client/game.lua @@ -115,6 +115,10 @@ local function draw_player(pl,islocal) love.graphics.setColor(0.5,0,0) love.graphics.circle("line",pl.pos.x,pl.pos.y,PLAYER_SIZE) end + + if pl.username then + util.print_good(pl.username, pl.pos.x, pl.pos.y) + end end local remote_players = {} @@ -122,7 +126,8 @@ local remote_players = {} local function update_local_player(pl,dt) local SPEED = 8*math.sqrt(3) -- 8 hexagonheights per second - if love.keyboard.isDown("lshift") or love.keyboard.isScancodeDown'kpenter' then + if love.keyboard.isDown("lshift") + or love.keyboard.isScancodeDown'kpenter' then SPEED = SPEED*2 end local function kd(codes) @@ -145,7 +150,10 @@ local function update_local_player(pl,dt) dy = dy * (math.sqrt(3)/2) end - local try_pos = Pos:make(pl.pos.x + SPEED*dt*dx, pl.pos.y + SPEED*dt*dy) + local try_pos = Pos:make( + pl.pos.x + SPEED*dt*dx, + pl.pos.y + SPEED*dt*dy + ) pl.pos = movement.collide_with_terrain(pl.pos,try_pos,map) -- pl.pos = try_pos pl.pos_dirty = true @@ -180,7 +188,10 @@ local function handle_net() -- if op ~= "chunk" then print(ev.channel,ev.data) end if op == "join" then local pl = j.pl - remote_players[pl.id] = {pos=coords.Pos:make(pl.x,pl.y),color=pl.color,id=pl.id} + remote_players[pl.id] = { + pos=coords.Pos:make(pl.x,pl.y), + color=pl.color, id=pl.id + } msgbox.add(pl.id.." joined") elseif op == "leave" then local id = j.id @@ -193,7 +204,10 @@ local function handle_net() remote_players[id].pos.y = y elseif op == "you" then local pl = j.pl - local_player = {pos=coords.Pos:make(pl.x,pl.y),color=pl.color,id=pl.id} + local_player = { + pos=coords.Pos:make(pl.x,pl.y), + color=pl.color, id=pl.id, username=pl.username + } elseif op == "chunk" then local ch = ChunkC:from_packet_data(j) map:add_chunk(ch) @@ -217,7 +231,8 @@ local function update(dt) -- mouse input (place/mine) local msx,msy = love.mouse.getPosition() - local mh = camera:screen_to_world(Pos:make(msx,msy)):to_hex():round() + local mh = + camera:screen_to_world(Pos:make(msx,msy)):to_hex():round() if map:at(mh) == 0 and love.mouse.isDown(1) then map:set_at(mh,selected_tile) send_settile(mh,selected_tile) @@ -237,9 +252,10 @@ local function update(dt) end end -- unload chunks not near player - -- todo maybe: instead of immediately unloading chunks when we move away, - -- have some kind of 'last near' time, so that if player is moving back and forth, - -- we don't repeatedly unload and reload a given chunk + -- todo maybe: instead of immedately unloading chunks when we + -- move away, instead have some kind of 'last near' time, so + -- that if the player is moving back and forth, we don't + -- repeatedly unload and reload a given chunk. local to_remove = {} for cp in map:iter_chunks() do local d = player_cp:orth_dist(cp) @@ -305,7 +321,8 @@ local function draw() "mh "..tostring(hm).." "..tostring(hm:round()), "", "pw "..tostring(local_player.pos), - "ph "..tostring(local_player.pos:to_hex()).." "..tostring(local_player.pos:to_hex():round()), + "ph "..tostring(local_player.pos:to_hex()).." " + ..tostring(local_player.pos:to_hex():round()), "", "voob "..tostring(camera.zoom), "", @@ -374,8 +391,6 @@ function SCENE.load(_username) host = enet.host_create() peer = host:connect(SERVER_HOSTNAME..":8473",2) username = _username - print("got username",_username) - end function SCENE.quit() diff --git a/client/main.lua b/client/main.lua index 7388c67..f68ff79 100644 --- a/client/main.lua +++ b/client/main.lua @@ -27,7 +27,8 @@ function titlescene.draw() love.graphics.print("enter username: ",40,120) love.graphics.setColor(0,0,0,0.8) - love.graphics.rectangle('fill', 60,150, 3+normal_font:getWidth(username), 15) + love.graphics.rectangle('fill', 60,150, + 3+normal_font:getWidth(username), 15) love.graphics.setColor(1,1,1) love.graphics.print(username, 60, 150) diff --git a/client/movement.lua b/client/movement.lua index 2aa870b..5cc6b85 100644 --- a/client/movement.lua +++ b/client/movement.lua @@ -10,8 +10,6 @@ local function clamp(x,minv,maxv) return math.min(math.max(x,minv),maxv) end - - -- https://iquilezles.org/articles/distgradfunctions2d/ local function iqz_hex_sdgf(px,py, r) local kx,ky,kz = -0.866025404,0.5,0.577350269 @@ -42,7 +40,6 @@ local function iqz_hex_sdgf(px,py, r) end - -- rotate by 30 degrees local c30,s30 = math.cos(math.rad(30)), math.sin(math.rad(30)) local function t_in(x,y) return c30*x-s30*y, s30*x+c30*y end @@ -65,7 +62,8 @@ local function hex_sdgf(pos, hex) end local PLAYER_SIZE = require"common.constants".PLAYER_SIZE -local function collide_with_terrain(old_pos, try_pos, map, tries_remaining) +local function collide_with_terrain(old_pos, try_pos, map, + tries_remaining) tries_remaining = tries_remaining or 3 if tries_remaining <= 0 then return old_pos end local try_h = try_pos:to_hex():round() @@ -77,7 +75,9 @@ local function collide_with_terrain(old_pos, try_pos, map, tries_remaining) local push_dist = PLAYER_SIZE - d local push_dir = Pos:make(gx,gy) local new_try_pos = try_pos + (push_dist*push_dir) - return collide_with_terrain(old_pos,new_try_pos,map,tries_remaining-1) + return collide_with_terrain( + old_pos, new_try_pos, map, tries_remaining - 1 + ) end end end diff --git a/client/profile.lua b/client/profile.lua index 47a9806..5ed5b2a 100644 --- a/client/profile.lua +++ b/client/profile.lua @@ -1,7 +1,10 @@ local profile = require"jit.profile" local function start(period,file) local function cb(thread,samples,vmstate) - file:write(profile.dumpstack(thread,"pF;l;",-100), vmstate, " ", samples, "\n") + file:write( + profile.dumpstack(thread,"pF;l;",-100), + vmstate, " ", samples, "\n" + ) end profile.start("vli"..tonumber(period), cb) end diff --git a/client/util.lua b/client/util.lua index efeac31..2c7d90a 100644 --- a/client/util.lua +++ b/client/util.lua @@ -7,8 +7,6 @@ local function print_good(str,x,y) local W,H = love.graphics.getDimensions() if x == "center" then x = (W/2)-(w/2) end if y == "center" then y = (H/2)-(h/2) end - if x < 0 then x = W-w-(-x)+1 end - if y < 0 then y = H-h-(-y)+1 end love.graphics.setColor(0,0,0,0.8) love.graphics.rectangle("fill",x,y,w,h) love.graphics.setColor(1,1,1) diff --git a/common/chunk.lua b/common/chunk.lua index 563723e..51c0a6c 100644 --- a/common/chunk.lua +++ b/common/chunk.lua @@ -33,7 +33,10 @@ function Chunk.set_at(self,hoffs,tile) end function Chunk.data_packet(self) - return json.encode{t="chunk",tiles=rle.encode(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 diff --git a/common/class.lua b/common/class.lua index f5cd46e..8fb84b0 100644 --- a/common/class.lua +++ b/common/class.lua @@ -1,8 +1,9 @@ -- currently a class is a table T with T.__index = T --- then to make an instance of this class, we do setmetatable(instance,T) --- this should be fine for anything we wish to do. it is possible we will eventually --- split this into two separate tables though, perhaps? i don't see why we would ever --- do this though. +-- then to make an instance of this class, we do +-- setmetatable(instance,T) +-- this should be fine for anything we wish to do. +-- it is possible we will eventually split this into two separate +-- tables perhaps? i don't see why we would ever do that though local function class() local T = {} diff --git a/common/coords.lua b/common/coords.lua index fc4aaa5..28579e6 100644 --- a/common/coords.lua +++ b/common/coords.lua @@ -37,7 +37,8 @@ function Hex.round(self) -- return a new Hex rounded to integer coordinates local fq,fr,fs = self.q,self.r,self.s -- round all to nearest integer - -- find which was changed the most, reset that one to be coherent with the other two. + -- find which was changed the most, reset that one to be coherent + -- with the other two. local abs = math.abs local rq,rr,rs = round(fq),round(fr),round(fs) @@ -79,16 +80,26 @@ function Hex.iter_neighbours(self,radius) assert(radius > 0,"radius must be at least 1") return coroutine.wrap(function() for q = -radius,radius do - for r = math.max(-radius,-q-radius), math.min(radius,-q+radius) do + local rmin = math.max(-radius, -q-radius) + local rmax = math.min( radius, -q+radius) + for r = rmin, rmax do coroutine.yield(self+Hex:make(q,r)) end end end) end -function Hex.__add(self,other) return Hex:make(self.q+other.q, self.r+other.r, self.s+other.s) end -function Hex.__sub(self,other) return Hex:make(self.q-other.q, self.r-other.r, self.s-other.s) end -function Hex.__tostring(self) return string.format("H(%.2f,%.2f)",self.q,self.r) end -function Hex.__eq(a,b) return a.q==b.q and a.r==b.r end +function Hex.__add(self,other) + return Hex:make(self.q+other.q, self.r+other.r, self.s+other.s) +end +function Hex.__sub(self,other) + return Hex:make(self.q-other.q, self.r-other.r, self.s-other.s) +end +function Hex.__tostring(self) + return string.format("H(%.2f,%.2f)",self.q,self.r) +end +function Hex.__eq(a,b) + return a.q==b.q and a.r==b.r +end Pos = class() function Pos.new(cls) @@ -102,8 +113,12 @@ end function Pos.make(cls,...) return cls:new():init(...) end -function Pos.__add(self,other) return Pos:make(self.x+other.x,self.y+other.y) end -function Pos.__sub(self,other) return Pos:make(self.x-other.x,self.y-other.y) end +function Pos.__add(self,other) + return Pos:make(self.x+other.x,self.y+other.y) +end +function Pos.__sub(self,other) + return Pos:make(self.x-other.x,self.y-other.y) +end function Pos.__mul(a,b) if type(a) == "number" then return Pos:make(a*b.x,a*b.y) @@ -114,40 +129,49 @@ function Pos.__mul(a,b) end end function Pos.__div(a,b) - assert(type(b) == "number","can only divide Pos by scalar, and can't divide scalar by Pos") + assert(type(b) == "number","can only divide Pos by scalar") return a*(1/b) end function Pos.__eq(a,b) return a.x==b.x and a.y==b.y end function Pos.lensq(self) return self.x^2 + self.y^2 end function Pos.len(self) return math.sqrt(self:lensq()) end function Pos.norm(self) return self/self:len() end -function Pos.dot(self,other) return self.x*other.x + self.y*other.y end +function Pos.dot(self,other) return self.x*other.x+self.y*other.y end function Pos.to_hex(self,into) into = into or Hex:new() local q = self.x*(SR3/3) - self.y*(1/3) local r = (2/3)*self.y return into:init(q,r,-q-r) end -function Pos.__tostring(self) return string.format("(%.2f,%.2f)",self.x,self.y) end +function Pos.__tostring(self) + return string.format("(%.2f,%.2f)",self.x,self.y) +end -- represents coordinates of a chunk --- ie pair of integers. the chunk at spawn is C(0,0), the one to the right of that is C(1,0), etc +-- the chunk at spawn is C(0,0), to the right of that is C(1,0), etc ChunkPos = class() function ChunkPos.make(cls,u,v) return setmetatable({u=u,v=v},cls) end -function ChunkPos.__add(self,other) return ChunkPos:make(self.u+other.u,self.v+other.v) end -function ChunkPos.__sub(self,other) return ChunkPos:make(self.u-other.u,self.v-other.v) end -function ChunkPos.__tostring(self) return string.format("C(%d,%d)",self.u,self.v) end +function ChunkPos.__add(self,other) + return ChunkPos:make(self.u+other.u,self.v+other.v) +end +function ChunkPos.__sub(self,other) + return ChunkPos:make(self.u-other.u,self.v-other.v) +end +function ChunkPos.__tostring(self) + return string.format("C(%d,%d)",self.u,self.v) +end function ChunkPos.__eq(a,b) return a.u==b.u and a.v==b.v end function ChunkPos.extents(self) -- returns Hex of topleft and bottomright local tlq,tlr = self.u*CHUNK_SIZE, self.v*CHUNK_SIZE - local brq,brr = (self.u+1)*CHUNK_SIZE -1, (self.v+1)*CHUNK_SIZE -1 + local brq,brr = (self.u+1)*CHUNK_SIZE-1, (self.v+1)*CHUNK_SIZE-1 return Hex:make(tlq,tlr), Hex:make(brq,brr) end function ChunkPos.neighborhood(self) - -- return all chunkposes within the 3x3 square centered on self, including self + -- return all chunkposes within the 3x3 square centered on self, + -- including self local out = {} for du=-1,1 do for dv = -1,1 do diff --git a/common/map.lua b/common/map.lua index 67e2834..0437a12 100644 --- a/common/map.lua +++ b/common/map.lua @@ -1,15 +1,23 @@ -- a Map is a 2d array of chunks -- it handles loading and unloading of chunks --- each slot in the Map is either a Chunk object (if that chunk is loaded), --- nil (if not loaded), or false (if 'loading'). on the client 'loading' means --- the request for the chunk is currently in flight. on the server it might one day --- mean that terrain generation for that chunk is currently in progress. --- to test whether a chunk is loaded you can do if map:chunk(cp) then ... end --- to test whether a chunk needs to be loaded you do if map:chunk(cp) == nil then ... end. --- it will probably also do things relating to entities and multiblock things - --- note that the Map never creates any Chunks itself, which means it should be agnostic --- to whatever actual Chunk class is being used (ChunkC or ChunkS or whatever) +-- each slot in the Map is either a Chunk if that chunk is loaded, +-- nil if not loaded, or false (if 'loading'). +-- on the client 'loading' means the request for the chunk is +-- currently in flight. +-- on the server it might one day mean that terrain generation +-- for that chunk is currently in progress. +-- +-- to test whether a chunk is loaded you can do +-- if map:chunk(cp) then ... end +-- to test whether a chunk needs to be loaded you do +-- if map:chunk(cp) == nil then ... end +-- +-- it will probably also do things relating to entities +-- and multiblock things +-- +-- note that the Map never creates any Chunks itself, which means +-- it should be agnostic to whatever actual Chunk class is being used +-- (ChunkC or ChunkS or whatever) local class = require"common.class" local coords = require"common.coords" @@ -50,10 +58,12 @@ function Map.at(self,hpos) -- returns tile at world coord hpos -- if that tile's containing chunk isn't loaded, return nil - -- not using the methods for doing this, in order to avoid lots of allocations + -- not using the methods for doing this, to avoid many allocations -- inside the main drawing loop - local cpu,cpv = math.floor(hpos.q/CHUNK_SIZE),math.floor(hpos.r/CHUNK_SIZE) - local hoffq,hoffr = hpos.q-(cpu*CHUNK_SIZE), hpos.r-(cpv*CHUNK_SIZE) + local cpu = math.floor(hpos.q/CHUNK_SIZE) + local cpv = math.floor(hpos.r/CHUNK_SIZE) + local hoffq = hpos.q - (cpu*CHUNK_SIZE) + local hoffr = hpos.r - (cpv*CHUNK_SIZE) local ch = self:_chunkuv(cpu,cpv) if not ch then return nil end return ch:_atqr(hoffq,hoffr) diff --git a/server/chunk.lua b/server/chunk.lua index bb30945..20db704 100644 --- a/server/chunk.lua +++ b/server/chunk.lua @@ -11,8 +11,7 @@ function ChunkS.make(cls,...) end function ChunkS.apply_migrations(self) - -- if tile format has changed and format in db isn't up to date any more - -- then perform updates here + -- if tile format has changed, perform updates here for i,t in ipairs(self.tiles) do if t == false then self.tiles[i] = 0 elseif t == true then self.tiles[i] = 9 end diff --git a/server/noise.lua b/server/noise.lua index c0ca57c..6fd745f 100644 --- a/server/noise.lua +++ b/server/noise.lua @@ -79,20 +79,4 @@ function NoiseAgg.at(self,x,y) return t/n end --- local chars = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. " - --- local scale = 3 - --- for x=1,10-10/40,10/40 do --- for y = 1,10-10/60,10/60 do - --- local n = at(1+(x-1)/scale,1+(y-1)/scale) --- local nn = (n +1)/2 --- local nnn = 1+nn*#chars --- local c = chars:sub(nnn,nnn) --- io.write(c) --- end --- io.write("\n") --- end - return {PerlinNoise=PerlinNoise,NoiseAgg=NoiseAgg} -- cgit v1.2.3