summaryrefslogtreecommitdiff
path: root/server/worldgen.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-02-04 23:03:19 +0000
committerubq323 <ubq323@ubq323.website>2023-02-04 23:03:19 +0000
commit0dc1276df57aa16b4f0eaecf54fb5cd8f00115c6 (patch)
tree0d5672f6f05f56022ed834ad35c1c2b2df52c21c /server/worldgen.lua
parent1ebd7d9b7b62c8e05d527611a1944ed1a876b890 (diff)
many many optimizations and refactorings; introduction of Map to support multiple chunks, modify worldgen and client drawing to support multiple chunks
Diffstat (limited to 'server/worldgen.lua')
-rw-r--r--server/worldgen.lua49
1 files changed, 49 insertions, 0 deletions
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
+}