Replace map variant randomness with perlin noise

This commit is contained in:
Hugues Ross 2020-04-18 07:53:32 -04:00
parent af0a801801
commit 62ace2394b
1 changed files with 24 additions and 7 deletions

View File

@ -1,9 +1,26 @@
local TILE_SIZE = 0.25;
local TILE_OFFSET = 0.24; -- Slightly smaller than TILE_SIZE. We overlap tiles slightly to minimize seams
local variant_odds = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4 };
local function get_variant(random)
return variant_odds[random:next(1, 12)];
-- NoiseParams table for tile variations
local MAP_NOISE = {
offset = 0,
scale = 1,
spread = {x = 2, y = 2, z = 2},
seed = minetest.get_mapgen_setting("seed"),
octaves = 2,
persist = 0.63,
lacunarity = 2.0,
flags = "defaults, absvalue",
};
-- Get the variant of the tile at a given position
-- x: The X position of the tile (in map coordinates)
-- z: The Z position of the tile (in map coordinates)
-- noise: A 2d lookup table of perlin noise. Must contain the position [x + 1][y + 1]
--
-- Returns a number from 1 to 4
local function get_variant(x, z, noise)
return math.floor(math.min(noise[x + 1][z + 1] * 3, 3)) + 1;
end
local map_formspec_prefix = [[
@ -43,7 +60,7 @@ end
-- Returns a formspec string
local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_scale, is_visible, get_marker, user)
local str = "";
local random = PcgRandom(x + y + w + h); -- TODO: Better seed
local noise = PerlinNoiseMap(MAP_NOISE, { x=w + 1, y=h + 1, z=1}):get_2d_map({ x=x, y=y});
player_x = math.floor(player_x / map_scale + 0.5);
player_y = math.floor(player_y / map_scale + 0.5);
@ -53,7 +70,7 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
for j = y + h,y,-1 do
local fy = (y + h - j) * TILE_OFFSET;
if column == nil or column[j * map_scale] == nil or (is_visible and not is_visible(user, i, j)) then
str = str .. unknown_biome_tile(fx, fy, get_variant(random));
str = str .. unknown_biome_tile(fx, fy, get_variant(i - x, j - y, noise));
else
local name = minetest.get_biome_name(column[j * map_scale].biome);
local height = column[j * map_scale].height;
@ -73,7 +90,7 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
mod = "^[colorize:#1f1f34:"..tostring(depth * 10)
end
str = str..tile:format(fx, fy - height, TILE_SIZE, TILE_SIZE, biome .. "." .. tostring(get_variant(random)) .. ".png" .. mod)
str = str..tile:format(fx, fy - height, TILE_SIZE, TILE_SIZE, biome .. "." .. tostring(get_variant(i - x, j - y, noise)) .. ".png" .. mod)
if get_marker then
local marker = cartographer.get_marker_texture(get_marker(user, i, j), detail);
@ -90,7 +107,7 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
cartographer.skin.player_icon.frame_duration);
end
else
str = str .. unknown_biome_tile(fx, fy, get_variant(random));
str = str .. unknown_biome_tile(fx, fy, get_variant(i - x, j - y, noise));
end
end
end