cartographer/map_formspec.lua

65 lines
2.9 KiB
Lua
Raw Normal View History

2020-02-16 18:55:07 +01:00
local scale = 0.25;
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)];
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]";
local function generate_map(data, x, y, w, h, player_x, player_y, spawn_x, spawn_y, death_x, death_y, detail, is_visible, user)
local str = "";
local random = PcgRandom(x + y + w + h); -- TODO: Better seed
for i = x,x + w,1 do
local fx = (i - x) * (scale - 0.01);
local column = data.generated[i];
for j = y + h,y,-1 do
local fy = (y + h - j) * (scale - 0.01);
if column == nil or column[j] == nil or (is_visible and not is_visible(user, i, j)) then
str = str..tile:format(fx, fy, scale, scale, "cartographer_unknown_biome." .. tostring(get_variant(random)) .. ".png")
else
local name = minetest.get_biome_name(column[j].biome);
local height = column[j].height;
local biome = cartographer.get_biome_texture(name, math.floor(height + 0.5), detail);
2020-02-16 18:55:07 +01:00
if biome == nil then
-- minetest.chat_send_all(name);
2020-02-16 18:55:07 +01:00
str = str..tile:format(fx, fy, scale, scale, "cartographer_unknown_biome." .. tostring(get_variant(random)) .. ".png")
else
if height > 0 then
str = str..tile:format(fx, fy, scale, scale, "cartographer_cliff.png")
end
str = str..tile:format(fx, fy, scale, scale, biome .. "." .. tostring(get_variant(random)) .. ".png")
2020-02-16 18:55:07 +01:00
end
if i == player_x and j == player_y then
str = str..marker:format(fx, fy, scale, scale, "cartographer_player_icon.png", 2, 500)
2020-02-16 18:55:07 +01:00
-- elseif i == death_x and j == death_y then
-- str = str..marker:format(fx, fy - (height * 0.025), scale, scale, "dicon.png")
-- elseif i == spawn_x and j == spawn_y then
-- str = str..marker:format(fx, fy - (height * 0.025), scale, scale, "cartographer_home_icon.png")
end
end
end
end
return str;
end
function cartographer.get_map_formspec(data, x, y, w, h, spawn_x, spawn_y, death_x, death_y, detail)
return map_formspec_prefix:format(w * scale, h * scale)..generate_map(data, x - (w * 0.5), y - (h * 0.5), w, h, x, y, spawn_x, spawn_y, death_x, death_y, detail);
end
function cartographer.get_map_formspec_map(data, map, x, y, spawn_x, spawn_y, death_x, death_y)
return map_formspec_prefix:format(map.w * scale, map.h * scale)..generate_map(data, map.x, map.z, map.w, map.h, x, y, spawn_x, spawn_y, death_x, death_y, map.detail, cartographer.is_filled, map);
end