Add support for displaying markers on maps

This commit is contained in:
Hugues Ross 2020-03-17 06:52:15 -04:00
parent 8f786cda9a
commit 23da5f8900
2 changed files with 32 additions and 2 deletions

View File

@ -10,6 +10,7 @@ function cartographer.create_map(x, z, w, h, filled, detail, scale)
detail = detail,
scale = scale,
fill = {},
markers = {},
};
_cartographer.maps[id] = map;
@ -46,6 +47,28 @@ function cartographer.fill(map, x, z, w, h)
end
end
function cartographer.set_marker(map, x, z, marker)
if map == nil or x < map.x or x > map.x + map.w or z < map.z or z > map.z + map.h then
return;
end
if not map.markers[x] then
map.markers[x] = {
[z] = marker,
};
else
map.markers[x][z] = marker;
end
end
function cartographer.get_marker(map, x, z)
if map == nil or x < map.x or x > map.x + map.w or z < map.z or z > map.z + map.h or not map.markers[x] then
return nil;
end
return map.markers[x][z];
end
function cartographer.fill_local(id, x, z)
local map = cartographer.get_map(id);

View File

@ -15,7 +15,7 @@ local map_formspec_prefix = [[
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, detail, map_scale, is_visible, user)
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
for i = x,x + w,1 do
@ -50,6 +50,13 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
str = str..tile:format(fx, fy - (height * 0.05), scale, scale, biome .. "." .. tostring(get_variant(random)) .. ".png" .. mod)
end
if get_marker then
local marker = get_marker(user, i, j);
if marker then
str = str..tile:format(fx, fy - (height * 0.05), scale, scale, marker .. ".png")
end
end
if i == player_x and j == player_y then
str = str..marker:format(fx, fy - (height * 0.05), scale, scale, "cartographer_player_icon.png", 2, 500)
end
@ -65,5 +72,5 @@ function cartographer.get_map_formspec(data, x, y, w, h, detail)
end
function cartographer.get_map_formspec_map(data, map, x, 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, map.detail, map.scale, cartographer.is_filled, map);
return map_formspec_prefix:format(map.w * scale, map.h * scale)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, cartographer.is_filled, cartographer.get_marker, map);
end