reorganizing map gen into modules-ish
This commit is contained in:
+55
-43
@@ -2,66 +2,78 @@
|
|||||||
|
|
||||||
local simplex = require "lib.simplex"
|
local simplex = require "lib.simplex"
|
||||||
|
|
||||||
local generate = function()
|
local simplex_map = {}
|
||||||
simplex.Seed(math.random())
|
|
||||||
|
|
||||||
local map = {}
|
simplex_map.min = function(map)
|
||||||
map.size = 500
|
local r = map[1][1]
|
||||||
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
|
for i=1,#map do
|
||||||
map.altitude = {} -- TEMP only one noise map
|
for j=1,#map[1] do
|
||||||
|
if r > map[i][j] then r = map[i][j] end
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
local function max(t)
|
simplex_map.max = function(map)
|
||||||
local r = t[1][1]
|
local r = map[1][1]
|
||||||
for i=1,#t do
|
for i=1,#map do
|
||||||
for j=1,#t[1] do
|
for j=1,#map[1] do
|
||||||
if r < t[i][j] then r = t[i][j] end
|
if r < map[i][j] then r = map[i][j] end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
local function normalize(map, new_min, new_max)
|
|
||||||
local minimum = min(map)
|
simplex_map.normalize = function(map, new_min, new_max)
|
||||||
local initialRange = max(map) - minimum
|
local minimum = simplex_map.min(map)
|
||||||
|
local initialRange = simplex_map.max(map) - minimum
|
||||||
local finalRange = new_max - new_min
|
local finalRange = new_max - new_min
|
||||||
for i = 1, #map do
|
for i = 1, #map do
|
||||||
for j = 1, #map[1] do
|
for j = 1, #map[1] do
|
||||||
map[i][j] = (map[i][j] - minimum) / initialRange * finalRange + new_min
|
map[i][j] = (map[i][j] - minimum) / initialRange * finalRange + new_min
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
map.min = min(map)
|
map.min = simplex_map.min(map)
|
||||||
map.max = max(map)
|
map.max = simplex_map.max(map)
|
||||||
return map -- superfluous
|
return map -- superfluous
|
||||||
end
|
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
|
for x = 1, map.size do
|
||||||
map.terrain[x] = {}
|
map[x] = {}
|
||||||
for y = 1, map.size do
|
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}
|
map.terrain[x][y] = {value, value, value, 1}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user