diff options
author | ubq323 <ubq323@ubq323.website> | 2023-06-18 22:28:37 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-06-18 22:28:37 +0100 |
commit | f55c9f415a081a175e48b51db894f23e59ce47a2 (patch) | |
tree | ee63f98d5c4f1a2a11e9800ced9c2861eb3085a7 /common | |
parent | 598bd4332deddd5f4c506689cbd136f495835ea7 (diff) |
fix line lengths
Diffstat (limited to 'common')
-rw-r--r-- | common/chunk.lua | 5 | ||||
-rw-r--r-- | common/class.lua | 9 | ||||
-rw-r--r-- | common/coords.lua | 58 | ||||
-rw-r--r-- | common/map.lua | 36 |
4 files changed, 73 insertions, 35 deletions
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) |