summaryrefslogtreecommitdiff
path: root/src/islandgen.moon
blob: 95e8b074883c3b2ce94d3c0942c863390b3f8291 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
rng = require("rng")
shim = require("shader_shim")
world = require("world")
color = require("color")
win = require("window")
gen = {}

-- Generate roughly rectangular islands
-- Stategy: generate a series of points, if we're in the first 1/4, go roughly right,
-- in the second 1/4, roughtly up, in the 3rd 1/4 roughly left, and in the 4th quarter roughtly down, finally joining up at the end
-- We find the smallest aabb box in this line, and use points on this box as the other side for triangles.

n_points = 100
point_distance = 1
sides = 4

gen.generate = (seed) ->
	rg = rng.generator(seed)
	normal = (avg, std) ->
		print("normal with", avg, std)
		rg1, rg2 = rg(),rg()
		print("rgs:",rg1, rg2)
		-- Box-Muller transform
		bm = math.sqrt(-2 * math.log(rg1)) * math.cos(2 * math.pi * rg2)
		print("bm was:",bm)
		-- Box-Muller gives us std = e^-0.5 , avg = 0
		((bm / math.exp(-1/2)) * std) + avg
	shoreline = {}
	shoreline[1] = vec2(0,0)
	aabb_sides = {
		{"y",math.max,"b"},
		{"x",math.min,"r"},
		{"y",math.min,"t",},
		{"x",math.max,"l"}
	}
	aabb = {
		b: n_points * point_distance
		r: n_points * point_distance
		t: -n_points * point_distance
		l: -n_points * point_distance
	}
	z = 0
	for i = 1, sides
		for j = 1, n_points / sides
			-- remap i (1->4) to (0 -> 2pi)
			avg_direction = ((i-1)/sides) * (2 * math.pi)
			std_direction = math.pi / 6
			rng_direction = normal(avg_direction, std_direction)
			print("rng_direction ended up being:", rng_direction)
			point = shoreline[#shoreline] + vec2(math.cos(rng_direction) * point_distance, math.sin(rng_direction) * point_distance)
			shoreline[#shoreline + 1] = point
			print("point:", point)
			print("prev:",aabb[aabb_sides[i][3]])
			aabb[aabb_sides[i][3]] = aabb_sides[i][2](aabb[aabb_sides[i][3]],point[aabb_sides[i][1]])
	
	print("Generated shoreline:",shoreline)
	print("And aabb box:" ,aabb)
	-- Now that we have the shorline, generate the geometry
	-- every point after the first needs to generate 2 triangles, 6 vertexes, each vertex is a vec3, 3 floats, 4 bytes per float
	geom = am.buffer(((n_points * 2) - 4) * 6 * 3 * 4) -- 4 corners only get 1 triangle
	gview = geom\view("vec3")
	gview[1] = vec3(shoreline[1].x, shoreline[1].y,z)
	gview[2] = vec3(aabb.l, aabb.b,z)
	gview[3] = vec3(shoreline[npoints - 1].x, shoreline[npoints - 1].y, z)
	gi = 4
	only1 = {[25]: true, [50]: true, [75]: true}
	for i = 2, npoints
		gview[gi] = vec3(shoreline[i].x, shoreline[i].y, z)
		if i < 25
			gview[gi+1] = 1

gen.protogen = (seed) ->
	geom = am.buffer(6 * 3 * 4)
	gview = geom\view("vec3")
	z = 0
	gview[1] = vec3(-10,-10,z)
	gview[2] = vec3(-10,10,z)
	gview[3] = vec3(10,10,z)
	gview[4] = vec3(10,10,z)
	gview[5] = vec3(10,-10,z)
	gview[6] = vec3(-10,-10,z)
	aspect = win.width / win.height
	s_mv = mat4(
		1, 0, 0, 0,
		0, aspect, 0, 0,
		0, 0, 1, 0,
		-0.5, 0.5, 0, 4
	)
	node = shim.land\append(am.depth_test("less")\append(am.bind({
		MV: s_mv
		P: mat4(1)
		land: gview
		lamp1: vec4(0,0,0,0.1)
		lamp2: vec4(0,0,0,0)
		lamp3: vec4(0,0,0,0)
		time: am.current_time!
		background: color.am_color.background
		shadow: color.am_color.shadow
		midground: color.am_color.midground
		foreground: color.am_color.foreground
		highlight: color.am_color.highlight
		outline: color.am_color.outline
		black: color.am_color.black
		world_x: 5
		world_y: 5
	})\append(am.draw("triangles"))))
	node\action(() =>
		bind = self("bind")
		bind.time = am.current_time!
		bind.world_x = world.world_x
		bind.world_y = world.world_y
		print(world.world_x, world.world_y)
		bind.lamp1 = vec4(math.sin(am.current_time!), 0, 0, math.cos(am.current_time! * 3) + math.pi)
	)

	node

gen