cartographer/map_formspec.lua

154 lines
6.5 KiB
Lua
Raw Normal View History

local TILE_SIZE = 0.25;
local TILE_OFFSET = 0.24; -- Slightly smaller than TILE_SIZE. We overlap tiles slightly to minimize seams
2020-02-16 18:55:07 +01:00
-- 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;
2020-02-16 18:55:07 +01:00
end
local map_formspec_prefix = [[
formspec_version[3]
size[%f,%f]
real_coordinates[true]
style_type[image_button;border=false]
]];
local tile = "image[%f,%f;%f,%f;%s]";
local marker = "animated_image[%f,%f;%f,%f;;%s;%d;%d]";
2020-02-16 18:55:07 +01:00
-- Generate formspec markup for an unknown biome tile
-- x: The x position of the tile
-- y: The y position of the tile
-- variant: The tile variant
--
-- Returns a formspec string
local function unknown_biome_tile(x, y, variant)
return tile:format(x, y, TILE_SIZE, TILE_SIZE, cartographer.skin.unknown_biome_texture .. "." .. tostring(variant) .. ".png");
end
2020-04-10 13:00:29 +02:00
-- Generate formspec markup for a given map
-- data: The mapping data
-- x: The X position of the map (in map coordinates)
-- y: The Z position of the map (in map coordinates)
-- w: The width of the map (in map coordinates)
-- h: The height of the map (in map coordinates)
-- player_x: The X position of the player marker (in map coordinates)
-- player_y: The Y position of the player marker (in map coordinates)
-- detail: The detail level
-- map_scale: Integer scaling factor for displaying a zoomed-out map
-- (Optional) is_visible: Callback to determine if a tile should be drawn
-- (Optional) get_marker: Callback to get the marker for any given tile
-- (Optional) user: userdata passed to the is_visible/get_marker callbacks
--
-- 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)
2020-02-16 18:55:07 +01:00
local str = "";
local noise = PerlinNoiseMap(MAP_NOISE, { x=w + 1, y=h + 1, z=1}):get_2d_map({ x=x, y=y});
2020-04-10 13:00:29 +02:00
player_x = math.floor(player_x / map_scale + 0.5);
player_y = math.floor(player_y / map_scale + 0.5);
2020-02-16 18:55:07 +01:00
for i = x,x + w,1 do
local fx = (i - x) * TILE_OFFSET;
2020-03-07 16:26:24 +01:00
local column = data.generated[i * map_scale];
2020-02-16 18:55:07 +01:00
for j = y + h,y,-1 do
local fy = (y + h - j) * TILE_OFFSET;
2020-03-07 16:26:24 +01:00
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(i - x, j - y, noise));
2020-02-16 18:55:07 +01:00
else
2020-03-07 16:26:24 +01:00
local name = minetest.get_biome_name(column[j * map_scale].biome);
local height = column[j * map_scale].height;
local biome = cartographer.get_biome_texture(name, math.floor(height + 0.5), detail);
2020-02-20 01:56:31 +01:00
2020-04-10 13:14:47 +02:00
if biome then
local depth = math.min(math.max(math.floor(height / 8), -8), 0) * -1
height = math.max(math.min(math.floor(height / 4), 8), 0)
2020-02-16 18:55:07 +01:00
2020-04-10 13:14:47 +02:00
local mod = "";
2020-02-16 18:55:07 +01:00
if height > 0 then
2020-04-10 13:14:47 +02:00
mod = "^[colorize:white:"..tostring(height * 10)
height = height * 0.05;
str = str..tile:format(fx, fy - height + TILE_OFFSET, TILE_SIZE, height + 0.01, cartographer.skin.cliff_texture .. ".png");
2020-04-10 13:14:47 +02:00
elseif depth > 0 then
mod = "^[colorize:#1f1f34:"..tostring(depth * 10)
2020-02-16 18:55:07 +01:00
end
str = str..tile:format(fx, fy - height, TILE_SIZE, TILE_SIZE, biome .. "." .. tostring(get_variant(i - x, j - y, noise)) .. ".png" .. mod)
2020-02-16 18:55:07 +01:00
2020-04-10 13:14:47 +02:00
if get_marker then
local marker = cartographer.get_marker_texture(get_marker(user, i, j), detail);
if marker then
str = str..tile:format(fx, fy - height, TILE_SIZE, TILE_SIZE, marker .. ".png")
2020-04-10 13:14:47 +02:00
end
end
2020-04-10 13:14:47 +02:00
if i == player_x and j == player_y then
str = str..marker:format(fx, fy - height,
TILE_SIZE, TILE_SIZE,
2020-04-10 13:14:47 +02:00
cartographer.skin.player_icon.texture .. ".png",
cartographer.skin.player_icon.frame_count,
cartographer.skin.player_icon.frame_duration);
end
else
str = str .. unknown_biome_tile(fx, fy, get_variant(i - x, j - y, noise));
2020-02-16 18:55:07 +01:00
end
end
end
end
return str;
end
-- Get the formspec for a given map segment
-- data: The map data
-- x: The X position of the map, in map coordinates
-- y: The Y position of the map, in map coordinates
-- w: The width of the map, in map coordinates
-- h: The height of the map, in map coordinates
-- detail: The detail level of the map
--
-- Returns a formspec string, the width of the formspec, and the height of the
-- formspec
-- TODO: Remove data argument
2020-03-08 15:06:26 +01:00
function cartographer.get_map_formspec(data, x, y, w, h, detail)
local formspec_width = (w + 1) * TILE_OFFSET + 0.01;
local formspec_height = (h + 1) * TILE_OFFSET + 0.01;
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(data, x - (w * 0.5), y - (h * 0.5), w, h, x, y, detail, 1),
formspec_width,
formspec_height;
2020-02-16 18:55:07 +01:00
end
-- Get the formspec for a given map table
-- data: The map data
-- map: The map to use
-- x: The X position of the player marker, in map coordinates
-- y: The Y position of the player marker, in map coordinates
--
-- Returns a formspec string, the width of the formspec, and the height of the
-- formspec
-- TODO: Remove data argument
2020-03-08 15:06:26 +01:00
function cartographer.get_map_formspec_map(data, map, x, y)
local formspec_width = (map.w + 1) * TILE_OFFSET + 0.01;
local formspec_height = (map.h + 1) * TILE_OFFSET + 0.01;
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, cartographer.is_filled, cartographer.get_marker, map),
formspec_width,
formspec_height;
2020-02-16 18:55:07 +01:00
end