summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/noise.lua34
-rw-r--r--server/server.lua41
2 files changed, 45 insertions, 30 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}
diff --git a/server/server.lua b/server/server.lua
index 67860ea..08f75c0 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -60,19 +60,42 @@ local function player_move_packet(player,x,y)
return json.encode{t="move",id=player.id,x=x,y=y}
end
+
+-- worldgen
local the_tiles = {}
-local ng = noise.NoiseAgg.make_perlin_octaves(4)
-for q = 1,chunk.SIZE-1 do
- for r = 1,chunk.SIZE-1 do
- local p = coords.Hex.make(q,r):to_pos()
- local nv = ng:at(p.x/20,p.y/20)
- assert(nv ~= 1,"oopsy")
- the_tiles[chunk.index(q,r)] = nv > 0 and 1+math.floor(math.sqrt(nv)*8) or false
+local function p(amp,scale) return {scale=scale,amp=amp,gen=noise.PerlinNoise:make()} end
+local ng = noise.NoiseAgg:make{
+ p(1,20),
+ -- p(0.7,2),
+ p(0.5,15),
+ --p(2,200),
+}
+local ng2 = noise.NoiseAgg:make{p(1,20),p(0.5,15)}
+
+for q = 0,chunk.SIZE-1 do
+ for r = 0,chunk.SIZE-1 do
+ local p = coords.Hex:make(q,r):to_pos()
+ local ix = chunk.index(q,r)
+ local nv = ng:at(p.x,p.y)
+ if nv <= 0 then
+ the_tiles[ix] = false
+ else
+ local nv2 = ng2:at(p.x,p.y)
+ nv2 = math.max(-0.9999999,math.min(0.9999999,nv2*2.5))
+ nv2 = (nv2+1)/2
+ print(nv2)
+
+ local tv = 1+math.floor(nv2*8)
+ assert(1<=tv and tv<=8,"oopsy woopsy")
+ the_tiles[ix] = tv
+ end
end
end
-local the_chunk = Chunk.make(the_tiles)
+local the_chunk = Chunk:make(the_tiles)
print"generated chunk"
+
+
while true do
local ev = host:service(100)
if ev then
@@ -115,7 +138,7 @@ while true do
end
end
elseif op == "settile" then
- local h = coords.Hex.make(j.q,j.r)
+ local h = coords.Hex:make(j.q,j.r)
the_chunk:set_at(h,j.tile)
-- print(player.id,"settile",h,j.tile)
for i,otherplayer in ipairs(playerlist) do