From 43c193279d35dd0294e1e3fb3219365a7d974ab7 Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Sun, 7 Jun 2020 18:31:42 -0400 Subject: [PATCH] items.lua: Parameterize coordinate conversion functions --- init.lua | 19 ++++++++----------- items.lua | 12 +++++++----- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/init.lua b/init.lua index 51bd6ee..615b611 100644 --- a/init.lua +++ b/init.lua @@ -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") (); diff --git a/items.lua b/items.lua index 6effae4..4440aad 100644 --- a/items.lua +++ b/items.lua @@ -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