diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/server.lua | 55 | ||||
-rw-r--r-- | server/worldgen.lua | 49 |
2 files changed, 68 insertions, 36 deletions
diff --git a/server/server.lua b/server/server.lua index 08f75c0..a7ee04b 100644 --- a/server/server.lua +++ b/server/server.lua @@ -3,8 +3,9 @@ local json = require"common.dkjson" local chunk = require"common.chunk" local Chunk = require"common.chunk".Chunk local noise = require"noise" -local unpack = unpack or table.unpack local coords = require"common.coords" +local worldgen = require"worldgen" +local Map = require"common.map".Map math.randomseed(os.time()) @@ -61,40 +62,15 @@ local function player_move_packet(player,x,y) end --- worldgen -local the_tiles = {} -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 +local map = Map:make() +local function get_or_gen_chunk(cp) + local ch = map:chunk(cp) + if not ch then + ch = worldgen.gen_chunk(cp) + map:add_chunk(cp,ch) end + return ch end -local the_chunk = Chunk:make(the_tiles) -print"generated chunk" - - while true do local ev = host:service(100) @@ -104,7 +80,8 @@ while true do table.insert(playerlist,player) print("connect",player.peer,player.id) player.peer:send(player_you_packet(player)) - player.peer:send(the_chunk:data_packet()) + local central_chunk = get_or_gen_chunk(coords.ChunkPos:make(0,0)) + player.peer:send(central_chunk:data_packet()) for i,otherplayer in ipairs(playerlist) do if otherplayer ~= player then @@ -139,8 +116,8 @@ while true do end elseif op == "settile" then local h = coords.Hex:make(j.q,j.r) - the_chunk:set_at(h,j.tile) - -- print(player.id,"settile",h,j.tile) + map:set_at(h,j.tile) + print(player.id,"settile",h,j.tile) for i,otherplayer in ipairs(playerlist) do if otherplayer ~= player then -- same packet structure s2c as c2s @@ -149,6 +126,12 @@ while true do otherplayer.peer:send(ev.data) end end + elseif op == "reqchunk" then + -- i am not certain this is the best way for this to work + -- i might change it later + local cp = coords.ChunkPos:make(j.u,j.v) + local ch = get_or_gen_chunk(cp) + player.peer:send(ch:data_packet()) end end diff --git a/server/worldgen.lua b/server/worldgen.lua new file mode 100644 index 0000000..5d496a0 --- /dev/null +++ b/server/worldgen.lua @@ -0,0 +1,49 @@ +local coords = require"common.coords" +local noise = require"noise" +local chunk = require"common.chunk" +local CHUNK_SIZE = require"common.constants".CHUNK_SIZE + +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), +-- } + +-- whether there is a tile there or not +local surface_ng = noise.NoiseAgg:make{p(1,20),p(0.5,15)} +-- if there is a tile there, what color should it be +local color_ng = noise.NoiseAgg:make{p(1,20),p(0.5,15)} + + +local function gen_chunk(chpos) + local htl,hbr = chpos:extents() + local tiles = {} + + for q = 0,CHUNK_SIZE-1 do + for r = 0,CHUNK_SIZE-1 do + local p = (htl+coords.Hex:make(q,r)):to_pos() + local ix = chunk.index(q,r) + local nv = surface_ng:at(p.x,p.y) + if nv <= 0 then + tiles[ix] = false + else + local nv2 = color_ng:at(p.x,p.y) + nv2 = math.max(-0.9999999,math.min(0.9999999,nv2*2.5)) + nv2 = (nv2+1)/2 + local tv = 1+math.floor(nv2*8) + assert(1<=tv and tv<=8,"oopsy woopsy") + tiles[ix] = tv + end + end + end + + local the_chunk = chunk.Chunk:make(chpos,tiles) + return the_chunk +end + + +return { + gen_chunk=gen_chunk +} |