summaryrefslogtreecommitdiff
path: root/src/islandgen.moon
diff options
context:
space:
mode:
authorAlexander M Pickering <alex@cogarr.net>2025-01-21 16:02:51 -0600
committerAlexander M Pickering <alex@cogarr.net>2025-01-21 16:02:51 -0600
commit0370d64b3bd7914be55358817e52bbc8a529a7d3 (patch)
treea717bb9582f8a4c8dc7caf0d455e25113c7b8704 /src/islandgen.moon
parentda9dd31f504d30f33922cdf362a7c01673a6b927 (diff)
downloadggj25-0370d64b3bd7914be55358817e52bbc8a529a7d3.tar.gz
ggj25-0370d64b3bd7914be55358817e52bbc8a529a7d3.tar.bz2
ggj25-0370d64b3bd7914be55358817e52bbc8a529a7d3.zip
work
Diffstat (limited to 'src/islandgen.moon')
-rw-r--r--src/islandgen.moon118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/islandgen.moon b/src/islandgen.moon
new file mode 100644
index 0000000..95e8b07
--- /dev/null
+++ b/src/islandgen.moon
@@ -0,0 +1,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