summaryrefslogtreecommitdiff
path: root/server/server.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server/server.lua')
-rw-r--r--server/server.lua19
1 files changed, 14 insertions, 5 deletions
diff --git a/server/server.lua b/server/server.lua
index d441989..a53d9a1 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -5,18 +5,26 @@ local Pos = require'r.pos'
local pairs_except = require'r.pairs_except'
local class = require'r.class'
local rle = require'r.rle'
+local noise = require'r.noise'
local common = require 'common'
+local SIZE = common.SIZE
local host = enet.host_create('*:19683')
local n = 0 local function next_name() n=n+1 return 'helen'..n end
+local function P(amp,scale,seed)
+ return {scale=scale,amp=amp,gen=noise.PerlinNoise(seed)} end
+local noise_gen = noise.NoiseAgg{ P(0.5,17,3), P(1,10,1), P(0.4,6,2) }
+
local chunks = common.ChunkMap()
local Chunk = class()
function Chunk.make(cls, cp, d) local self = setmetatable({cp=cp,d=d},cls)
chunks:add(cp,self) return self end
function Chunk.generate(cls,cp)
- print('\t','generating',cp)
- local d={} for i=1,common.SIZE^2 do d[i] = 1 == math.random(3) end
+ local d={} for x=0,SIZE-1 do for y=0,SIZE-1 do
+ local nv = noise_gen:at(x,y) local fill = nv>0.06
+ if math.random(3) == 2 then fill = not fill end
+ d[1+x*SIZE+y] = (cp.x<0) ~= fill end end
return cls(cp,d) end
function Chunk.packet(self)
return json.encode{ pos=self.cp, d=rle.encode(self.d), type='chunk' } end
@@ -25,6 +33,7 @@ function Chunk.load(cls, cp) local f = io.open(filename(cp),'r')
if not f then return nil end local j = json.decode(assert(f:read"a"))
f:close() return cls(cp,rle.decode(j.d)) end
function Chunk.save(self) local f = io.open(filename(self.cp),'w')
+ print('saving',self.cp)
assert(f:write(json.encode{d=rle.encode(self.d)})) f:close() end
function Chunk.obtain(cls,cp)
if chunks:get(cp) then return chunks:get(cp) end
@@ -51,7 +60,7 @@ local function greet(player)
local function doctor_chunks()
for player in pairs(players) do
- local pcp = player.pos:divmod(common.SIZE)
+ local pcp = player.pos:divmod(SIZE)
for dx = -1,1 do for dy = -1,1 do local cp = pcp+Pos(dx,dy)
if not player.loaded[cp:key()] then
local chunk = Chunk:obtain(cp)
@@ -64,7 +73,7 @@ local function doctor_chunks()
for k,chunk in pairs(chunks.d) do local cp = Pos:unkey(k)
local used = false
for player in pairs(players) do if player.loaded[k] then used=true end end
- if not used then print('unloading',cp) chunk:save() chunks:remove(cp) end end end
+ if not used then chunk:save() chunks:remove(cp) end end end
while true do
local ev = host:service(100) if ev then
@@ -80,7 +89,7 @@ while true do
elseif ev.type=='receive' then local j = json.decode(ev.data)
local pos if j.pos then pos = Pos(j.pos.x,j.pos.y) end
if j.type == 'move' then player.pos=pos send_others(player, j)
- elseif j.type == 'tile' then print('tile',pos,j.tile) chunks:set_tile(pos,j.tile) send_others(player,j)
+ elseif j.type == 'tile' then chunks:set_tile(pos,j.tile) send_others(player,j)
end
end
end