items.lua: Parameterize coordinate conversion functions

This commit is contained in:
Hugues Ross 2020-06-07 18:31:42 -04:00
parent d38a666f5a
commit 43c193279d
2 changed files with 15 additions and 16 deletions

View File

@ -41,17 +41,14 @@ local function periodic_save()
end
minetest.after(60, periodic_save);
function tochunk(coord)
return math.floor(coord / _cartographer.CHUNK_SIZE);
end
function fromchunk(coord)
return math.floor(coord * _cartographer.CHUNK_SIZE);
end
local chunk = {
to = tochunk,
from = fromchunk,
to = function(coord)
return math.floor(coord / _cartographer.CHUNK_SIZE);
end,
from = function(coord)
return math.floor(coord * _cartographer.CHUNK_SIZE);
end
};
-- Includes
@ -59,7 +56,7 @@ cartographer.skin = loadfile(modpath .. "/skin_api.lua") ();
cartographer.gui = loadfile(modpath .. "/formspec.lua") ();
loadfile(modpath .. "/scanner.lua") (map_data, chunk);
loadfile(modpath .. "/map_api.lua") (chunk);
loadfile(modpath .. "/items.lua") ();
loadfile(modpath .. "/items.lua") (chunk);
_cartographer.generate_marker_formspec = loadfile(modpath .. "/marker_formspec.lua") (_cartographer.marker_lookup, cartographer.gui);
loadfile(modpath .. "/map_formspec.lua") (map_data);
loadfile(modpath .. "/commands.lua") ();

View File

@ -1,3 +1,5 @@
local chunk = ...
-- The list of players looking at maps, and the map IDs that they're looking at
local player_maps = {};
@ -54,7 +56,7 @@ local function map_from_meta(meta, player_x, player_z)
local id = cartographer.create_map(map_x, map_y, size, size, false, detail, scale);
meta:set_int("cartographer:map_id", id);
meta:set_string("description", "Map #" .. tostring(id) .. "\n[" .. tostring(fromchunk(map_x)) .. "," .. tostring(fromchunk(map_y)) .. "] - [" .. tostring(fromchunk(map_x + total_size)) .. "," .. tostring(fromchunk(map_y + total_size)) .. "]")
meta:set_string("description", "Map #" .. tostring(id) .. "\n[" .. tostring(chunk.from(map_x)) .. "," .. tostring(chunk.from(map_y)) .. "] - [" .. tostring(chunk.from(map_x + total_size)) .. "," .. tostring(chunk.from(map_y + total_size)) .. "]")
return id;
end
@ -64,8 +66,8 @@ end
-- player: The player to show the map to
local function show_map_meta(meta, player)
local pos = player:get_pos();
local player_x = tochunk(pos.x);
local player_z = tochunk(pos.z);
local player_x = chunk.to(pos.x);
local player_z = chunk.to(pos.z);
local id = meta:get_int("cartographer:map_id");
if id == 0 then
@ -235,7 +237,7 @@ function cartographer.copy_map_item(stack)
end
copy_meta:set_int("cartographer:map_id", new_id);
copy_meta:set_string("description", "Map #" .. tostring(new_id) .. "\n[" .. tostring(fromchunk(dest.x)) .. "," .. tostring(fromchunk(dest.z)) .. "] - [" .. tostring(fromchunk(dest.x + dest.w)) .. "," .. tostring(fromchunk(dest.z + dest.h)) .. "]")
copy_meta:set_string("description", "Map #" .. tostring(new_id) .. "\n[" .. tostring(chunk.from(dest.x)) .. "," .. tostring(chunk.from(dest.z)) .. "] - [" .. tostring(chunk.from(dest.x + dest.w)) .. "," .. tostring(chunk.from(dest.z + dest.h)) .. "]")
end
return copy;
@ -255,6 +257,6 @@ function cartographer.resize_map_item(meta, size)
local map = cartographer.get_map(id);
cartographer.resize_map(id, size, size);
meta:set_string("description", "Map #" .. tostring(id) .. "\n[" .. tostring(fromchunk(map.x)) .. "," .. tostring(fromchunk(map.z)) .. "] - [" .. tostring(fromchunk(map.x + map.w)) .. "," .. tostring(fromchunk(map.z + map.h)) .. "]")
meta:set_string("description", "Map #" .. tostring(id) .. "\n[" .. tostring(chunk.from(map.x)) .. "," .. tostring(chunk.from(map.z)) .. "] - [" .. tostring(chunk.from(map.x + map.w)) .. "," .. tostring(chunk.from(map.z + map.h)) .. "]")
end
end