Add world coord -> map coord conversion method

- Fix some coordinate-related bugs with markers
This commit is contained in:
Hugues Ross 2020-04-10 17:52:09 -04:00
parent c27d96b89f
commit a5ded9bc11
2 changed files with 20 additions and 6 deletions

View File

@ -3,8 +3,8 @@ local player_maps = {};
-- Show a map to a player from the ID
-- id: The map ID
-- player_x: The X position (in map coordinates)
-- player_z: The Z position (in map coordinates)
-- player_x: The X position (in world coordinates)
-- player_z: The Z position (in world coordinates)
-- player_name: The name of the player to show to
local function show_map_id_formspec(id, player_x, player_z, player_name)
cartographer.fill_local(id, player_x, player_z);
@ -12,6 +12,7 @@ local function show_map_id_formspec(id, player_x, player_z, player_name)
player_maps[player_name] = id;
local map = cartographer.get_map(id);
player_x, player_z = cartographer.to_map_coordinates(map, player_x, player_z)
local formspec = cartographer.get_map_formspec_map(data, map, player_x, player_z);
if _cartographer.marker_count > 0 then
formspec = formspec .. string.format("style_type[button,image_button;noclip=true;border=true] container[%f,0.5]", map.w * 0.25) -- TODO: Don't assume map.w * 0.25
@ -56,7 +57,7 @@ local function show_map_meta(meta, player)
id = map_from_meta(meta, player_x, player_z);
end
show_map_id_formspec(id, player_x, player_z, player:get_player_name());
show_map_id_formspec(id, pos.x, pos.z, player:get_player_name());
end
-- Called when a player sends input to the server from a formspec
@ -76,11 +77,10 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
minetest.chat_send_all(k .. ": " .. tostring(marker));
if marker or k == "clear_marker" then
local pos = player:get_pos();
local player_x = tochunk(pos.x);
local player_z = tochunk(pos.z);
local player_x, player_z = cartographer.to_map_coordinates(map, pos.x, pos.z);
cartographer.set_marker(map, player_x, player_z, marker);
show_map_id_formspec(player_maps[player:get_player_name()], player_x, player_z, player:get_player_name());
show_map_id_formspec(player_maps[player:get_player_name()], pos.x, pos.z, player:get_player_name());
elseif k == "quit" then
player_maps[player:get_player_name()] = nil;
end

View File

@ -78,6 +78,20 @@ function cartographer.fill_local(id, x, z)
end
end
-- Convert a position in world coordinates to the given map's coordinate system
-- map: The map to use as reference, or nil to use the default size (map scale of 1)
-- x: The x position, in world coordinates
-- z: The z position, in world coordinates
--
-- Returns The converted x and z coordinates
function cartographer.to_map_coordinates(map, x, z)
if not map or map.scale == 0 then
return tochunk(x), tochunk(z);
end
return math.floor(tochunk(x) / map.scale + 0.5), math.floor(tochunk(z) / map.scale + 0.5);
end
local timer = 0;
minetest.register_globalstep(function(dt)