summaryrefslogtreecommitdiff
path: root/server/noise.lua
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 /server/noise.lua
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 'server/noise.lua')
-rw-r--r--server/noise.lua34
1 files changed, 13 insertions, 21 deletions
diff --git a/server/noise.lua b/server/noise.lua
index 1480448..8a83b60 100644
--- a/server/noise.lua
+++ b/server/noise.lua
@@ -1,11 +1,12 @@
local Pos = require"common.coords".Pos
+local class = require"common.class"
local tau = 2*math.pi
math.randomseed(os.time())
local function random_unit_vec()
local theta = math.random()*tau
- return Pos.make(math.cos(theta),math.sin(theta))
+ return Pos:make(math.cos(theta),math.sin(theta))
end
local function lerp(a,b,t) return (1-t)*a + t*b end
local function smoothstep(x)
@@ -15,14 +16,13 @@ local function smoothstep(x)
end
local function slerp(a,b,t) return lerp(a,b,smoothstep(t)) end
-local NoiseGen = {}
-NoiseGen.__index = NoiseGen
-function NoiseGen.make()
+local PerlinNoise = class()
+function PerlinNoise.make(cls)
local grid = {}
setmetatable(grid,{__index=function(t,k) t[k] = {} return t[k] end})
- return setmetatable({grid=grid},NoiseGen)
+ return setmetatable({grid=grid},cls)
end
-function NoiseGen.vertex(self,ix,iy)
+function PerlinNoise.vertex(self,ix,iy)
local v = self.grid[ix][iy]
if v then return v end
vv = random_unit_vec()
@@ -30,7 +30,7 @@ function NoiseGen.vertex(self,ix,iy)
return vv
end
-function NoiseGen.at(self,x,y)
+function PerlinNoise.at(self,x,y)
local x0 = math.floor(x)
local y0 = math.floor(y)
local x1 = x0 + 1
@@ -41,7 +41,7 @@ function NoiseGen.at(self,x,y)
local v10 = self:vertex(x1,y0)
local v11 = self:vertex(x1,y1)
- local p = Pos.make
+ local p = function(...) return Pos:make(...) end
local d00 = v00:dot(p(x-x0,y-y0))
local d01 = v01:dot(p(x-x0,y-y1))
local d10 = v10:dot(p(x-x1,y-y0))
@@ -53,17 +53,9 @@ function NoiseGen.at(self,x,y)
return z
end
-local NoiseAgg = {}
-NoiseAgg.__index = NoiseAgg
-function NoiseAgg.make(things)
- return setmetatable({things=things or {}},NoiseAgg)
-end
-function NoiseAgg.make_perlin_octaves(nocts)
- local things = {}
- for i=1,nocts do
- table.insert(things,{amp=2^i,scale=2^(nocts-i),gen=NoiseGen.make()})
- end
- return NoiseAgg.make(things)
+local NoiseAgg = class()
+function NoiseAgg.make(cls,things)
+ return setmetatable({things=things or {}},cls)
end
function NoiseAgg.at(self,x,y)
local n = 0
@@ -72,7 +64,7 @@ function NoiseAgg.at(self,x,y)
for _,thing in ipairs(self.things) do
local gen,scale,amp = thing.gen,thing.scale,thing.amp
n = n + amp
- t = t + gen:at(x*scale,y*scale)*amp
+ t = t + gen:at(x/scale,y/scale)*amp
end
return t/n
end
@@ -93,4 +85,4 @@ end
-- io.write("\n")
-- end
-return {NoiseGen=NoiseGen,NoiseAgg=NoiseAgg}
+return {PerlinNoise=PerlinNoise,NoiseAgg=NoiseAgg}