From a5ded9bc115b2531c1fbcb853b6b76afe15da65d Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Fri, 10 Apr 2020 17:52:09 -0400 Subject: [PATCH] Add world coord -> map coord conversion method - Fix some coordinate-related bugs with markers --- items.lua | 12 ++++++------ map_api.lua | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/items.lua b/items.lua index 458a4be..990ac4b 100644 --- a/items.lua +++ b/items.lua @@ -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 diff --git a/map_api.lua b/map_api.lua index 704eed5..cec6198 100644 --- a/map_api.lua +++ b/map_api.lua @@ -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)