reorganizing map gen into modules-ish

This commit is contained in:
2026-08-18 14:39:54 -06:00
parent 200a3f7587
commit 3b43fecc3b
+52 -40
View File
@@ -2,66 +2,78 @@
local simplex = require "lib.simplex"
local generate = function()
simplex.Seed(math.random())
local simplex_map = {}
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
local scale = 0.005
local min, max = math.huge, -math.huge
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
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
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
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
local function normalize(map, new_min, new_max)
local minimum = min(map)
local initialRange = max(map) - minimum
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 = min(map)
map.max = max(map)
map.min = simplex_map.min(map)
map.max = simplex_map.max(map)
return map -- superfluous
end
normalize(map.altitude, 0, 1)
map.terrain = {}
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.terrain[x] = {}
map[x] = {}
for y = 1, map.size do
local value = map.altitude[x][y]
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 = {}
map.terrain = simplex_map.generate(map.size)
for x = 1, map.size do
for y = 1, map.size do
local value = map.terrain[x][y]
map.terrain[x][y] = {value, value, value, 1}
end
end