summaryrefslogtreecommitdiff
path: root/server/worldgen.lua
blob: 3f318d166c280fb8fbea715d627149bb9424a8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
local coords = require"common.coords"
local noise = require"noise"
local chunk = require"common.chunk"
local ChunkS = require"chunk".ChunkS
local CHUNK_SIZE = require"common.constants".CHUNK_SIZE

local function p(amp,scale,seed)
	return {
		scale=scale,
		amp=amp,
		gen=noise.PerlinNoise:make(seed)
	}
end

-- whether there is a tile there or not
local surface_ng = noise.NoiseAgg:make{p(1,20,1),p(0.5,15,2)}
-- if there is a tile there, what color should it be
local color_ng = noise.NoiseAgg:make{p(1,20,3),p(0.5,15,4)}


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] = 0
			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 = ChunkS:make(chpos,tiles)
	return the_chunk
end


return {
	gen_chunk=gen_chunk
}