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/worldgen.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 server/worldgen.lua (limited to 'server/worldgen.lua') 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 +} -- cgit v1.2.3