From 3b43fecc3b3bf9492e131a81bf94f470361a3885 Mon Sep 17 00:00:00 2001 From: Tangent Date: Tue, 18 Aug 2026 14:39:54 -0600 Subject: [PATCH] reorganizing map gen into modules-ish --- src/map_nextgen.lua | 118 ++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/src/map_nextgen.lua b/src/map_nextgen.lua index 7d007ac..02f0fd0 100644 --- a/src/map_nextgen.lua +++ b/src/map_nextgen.lua @@ -2,66 +2,78 @@ local simplex = require "lib.simplex" -local generate = function() - simplex.Seed(math.random()) +local simplex_map = {} +simplex_map.min = function(map) + local r = map[1][1] + for i=1,#map do + for j=1,#map[1] do + if r > map[i][j] then r = map[i][j] end + end + end + return r +end +simplex_map.max = function(map) + local r = map[1][1] + for i=1,#map do + for j=1,#map[1] do + if r < map[i][j] then r = map[i][j] end + end + end + return r +end + +simplex_map.normalize = function(map, new_min, new_max) + local minimum = simplex_map.min(map) + local initialRange = simplex_map.max(map) - minimum + local finalRange = new_max - new_min + for i = 1, #map do + for j = 1, #map[1] do + map[i][j] = (map[i][j] - minimum) / initialRange * finalRange + new_min + end + end + map.min = simplex_map.min(map) + map.max = simplex_map.max(map) + return map -- superfluous +end + +simplex_map.generate = function(size, scale, min, max) + if type(size) == "table" then + scale = size.scale + min = size.min + max = size.max + size = size.size + end + + simplex.Seed(math.random()) + local map = {} + map.size = size + + scale = scale or 0.005 + for x = 1, map.size do + map[x] = {} + for y = 1, map.size do + map[x][y] = simplex.Noise2D(x * scale, y * scale) + end + end + + min = min or 0 + max = max or 1 + simplex_map.normalize(map, min, max) + + return map +end + +local generate = function() local map = {} map.size = 500 map.tile_size = 1 -- TODO tile_size really shouldn't be the responsibility of the map generator - it's a rendering detail, not a map feature - map.altitude = {} -- TEMP only one noise map + map.altitude = {} - local scale = 0.005 - local min, max = math.huge, -math.huge + map.terrain = simplex_map.generate(map.size) for x = 1, map.size do - map.altitude[x] = {} for y = 1, map.size do - local value = simplex.Noise2D(x * scale, y * scale) - if value > max then max = value end - if value < min then min = value end - map.altitude[x][y] = value - end - end - print("Altitude", "Min:", min, "Max:", max) - - -- TEMP hacked in copy to fix the issue so I can make sure everything is correct aaa - local function min(t) - local r = t[1][1] - for i=1,#t do - for j=1,#t[1] do - if r > t[i][j] then r = t[i][j] end - end - end - return r - end - local function max(t) - local r = t[1][1] - for i=1,#t do - for j=1,#t[1] do - if r < t[i][j] then r = t[i][j] end - end - end - return r - end - local function normalize(map, new_min, new_max) - local minimum = min(map) - local initialRange = max(map) - minimum - local finalRange = new_max - new_min - for i = 1, #map do - for j = 1, #map[1] do - map[i][j] = (map[i][j] - minimum) / initialRange * finalRange + new_min - end - end - map.min = min(map) - map.max = max(map) - return map -- superfluous - end - normalize(map.altitude, 0, 1) - - map.terrain = {} - for x = 1, map.size do - map.terrain[x] = {} - for y = 1, map.size do - local value = map.altitude[x][y] + local value = map.terrain[x][y] map.terrain[x][y] = {value, value, value, 1} end end