summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-02-03 19:37:28 +0000
committerubq323 <ubq323@ubq323.website>2023-02-03 19:51:07 +0000
commit1ebd7d9b7b62c8e05d527611a1944ed1a876b890 (patch)
treecfc5ddf3fc15985c4369aa12d56de5fa8d6fc7e5 /common
parentec6a391cb9cf0c0feac0fe3615a59cc7cb6db2d5 (diff)
debug drawing change, zoom clamping, partial refactoring of class mechanisms to allow inheritance, minor refactor of noise generator, changes to temp worldgen, rework of class constructor mechanism
Diffstat (limited to 'common')
-rw-r--r--common/.map.lua.kak.Xd2mKc5
-rw-r--r--common/chunk.lua15
-rw-r--r--common/class.lua23
-rw-r--r--common/coords.lua34
-rw-r--r--common/map.lua6
5 files changed, 59 insertions, 24 deletions
diff --git a/common/.map.lua.kak.Xd2mKc b/common/.map.lua.kak.Xd2mKc
new file mode 100644
index 0000000..aa2bfb0
--- /dev/null
+++ b/common/.map.lua.kak.Xd2mKc
@@ -0,0 +1,5 @@
+-- a Map is a 2d array of chunks
+-- it handles loading and unloading of chunks
+-- the specifics of which are then implemented separately for client and server
+-- it will probably also do things relating to entities and multiblock things
+
diff --git a/common/chunk.lua b/common/chunk.lua
index 8e2ec40..1d3faa6 100644
--- a/common/chunk.lua
+++ b/common/chunk.lua
@@ -1,6 +1,7 @@
local json = require"common.dkjson"
+local class = require"common.class"
-local CHUNK_SIZE = 128
+local CHUNK_SIZE = 64
-- for now tiles shall be booleans
@@ -15,10 +16,9 @@ local function index(offq,offr)
return CHUNK_SIZE*offq + offr + 1
end
-local Chunk = {}
-Chunk.__index = Chunk
-function Chunk.make(tiles)
- return setmetatable({tiles=tiles},Chunk)
+local Chunk = class()
+function Chunk.make(cls,tiles)
+ return setmetatable({tiles=tiles},cls)
end
function Chunk.at(self,hoffs)
if not index_ok(hoffs.q,hoffs.r) then return nil end
@@ -35,11 +35,12 @@ end
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)
+ return Chunk:make(packet.tiles)
end
return {
Chunk=Chunk,
SIZE=CHUNK_SIZE,
- index=index
+ index=index,
+ index_ok=index_ok,
}
diff --git a/common/class.lua b/common/class.lua
new file mode 100644
index 0000000..37cf7bd
--- /dev/null
+++ b/common/class.lua
@@ -0,0 +1,23 @@
+local function class()
+ local T = {}
+ T.__index = T
+ return T
+end
+
+local function extend(Base)
+ local T = {}
+ T.__index = T
+ for k,v in pairs(Base) do
+ if k:sub(1,2) == "__" and k~="__index" then
+ T[k]=v
+ end
+ end
+ setmetatable(T,{__index=Base})
+end
+
+return setmetatable({
+ class=class,
+ extend=extend
+},{__call=class})
+
+
diff --git a/common/coords.lua b/common/coords.lua
index 2399e43..351aa53 100644
--- a/common/coords.lua
+++ b/common/coords.lua
@@ -1,3 +1,5 @@
+local class = require"common.class"
+
-- Hex: q,r,s. invariant that q+r+s=0
-- add, subtract
-- constructor takes 3 positions and rounds to closest hex centre.
@@ -15,12 +17,11 @@ local Pos, Hex
local SR3 = math.sqrt(3)
-Hex={}
-Hex.__index = Hex
-function Hex.make(q,r,s)
+local Hex = class()
+function Hex.make(cls,q,r,s)
s=s or -q-r
assert(q+r+s==0,"hex coord doesn't meet invariant")
- return setmetatable({q=q,r=r,s=s},Hex)
+ return setmetatable({q=q,r=r,s=s},cls)
end
function Hex.round(self)
-- return a new Hex rounded to integer coordinates
@@ -40,32 +41,31 @@ function Hex.round(self)
rs = -rq-rr
end
- return Hex.make(rq,rr,rs)
+ return Hex:make(rq,rr,rs)
end
function Hex.to_pos(self)
local x = self.q*SR3 + self.r*(SR3/2)
local y = self.r*(3/2)
- return Pos.make(x,y)
+ return Pos:make(x,y)
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.__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
-Pos = {}
-Pos.__index=Pos
-function Pos.make(x,y)
- return setmetatable({x=x,y=y},Pos)
+Pos = class()
+function Pos.make(cls,x,y)
+ return setmetatable({x=x,y=y},cls)
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)
+ return Pos:make(a*b.x,a*b.y)
elseif type(b) == "number" then
- return Pos.make(a.x*b,a.y*b)
+ return Pos:make(a.x*b,a.y*b)
else
error("can only multiply Pos by scalar")
end
@@ -81,7 +81,7 @@ function Pos.dot(self,other) return self.x*other.x + self.y*other.y end
function Pos.to_hex(self)
local q = self.x*(SR3/3) - self.y*(1/3)
local r = (2/3)*self.y
- return Hex.make(q,r,-q-r)
+ return Hex:make(q,r,-q-r)
end
function Pos.__tostring(self) return string.format("(%.2f,%.2f)",self.x,self.y) end
diff --git a/common/map.lua b/common/map.lua
new file mode 100644
index 0000000..fe4b9e0
--- /dev/null
+++ b/common/map.lua
@@ -0,0 +1,6 @@
+-- a Map is a 2d array of chunks
+-- it handles loading and unloading of chunks
+-- the specifics of which are then implemented separately for client and server
+-- it will probably also do things relating to entities and multiblock things
+
+