From 0dc1276df57aa16b4f0eaecf54fb5cd8f00115c6 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 4 Feb 2023 23:03:19 +0000 Subject: many many optimizations and refactorings; introduction of Map to support multiple chunks, modify worldgen and client drawing to support multiple chunks --- server/server.lua | 55 +++++++++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 36 deletions(-) (limited to 'server/server.lua') 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 -- cgit v1.2.3