cartographer/items.lua
Hugues Ross 7bb3b544e2 Bugfixes
- Fix crash on map placement
- Add temp map texture
- Fix player marker on map
2020-03-20 08:02:30 -04:00

136 lines
4.9 KiB
Lua

local function map_from_meta(meta, player_x, player_y)
local size = meta:get_int("cartographer:size") or 10;
local detail = meta:get_int("cartographer:detail") or 1;
local scale = meta:get_int("cartographer:scale") or 1;
local total_size = size * scale;
local map_x = math.floor((player_x + 10)/total_size) * total_size - 10
local map_y = math.floor((player_y + 10)/total_size) * total_size - 10;
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)) .. "]")
return id;
end
-- The map item/node
minetest.register_node("cartographer:map", {
description = "Map",
inventory_image = "cartographer_map.png",
tiles = { "cartographer_map.png" },
drawtype = "signlike",
paramtype = "light",
paramtype2 = "wallmounted",
stack_max = 1,
on_use = function(stack, player, _)
local pos = player:get_pos();
local name = player:get_player_name();
local player_x = tochunk(pos.x);
local player_y = tochunk(pos.z);
local meta = stack:get_meta();
local id = meta:get_int("cartographer:map_id");
if id == 0 then
id = map_from_meta(meta, player_x, player_y);
end
cartographer.fill_local(id, player_x, player_y);
minetest.show_formspec(player:get_player_name(), "map", cartographer.get_map_formspec_map(data, cartographer.get_map(id), player_x, player_y));
return stack;
end,
-- Called when this node is placed in the world. Copies map data from the
-- item to the node.
-- pos: The position of the node
-- placer: The object that placed this node (can be nil)
-- stack: The itemstack that was placed
-- pointed_thing: The current pointed thing
after_place_node = function(pos, placer, stack, pointed_thing)
local meta = stack:get_meta():to_table();
local node_meta = minetest.get_meta(pos);
node_meta:from_table(meta);
-- Consume the item after placing
return false;
end,
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
local meta = minetest.get_meta(pos);
local player_x = tochunk(pos.x);
local player_y = tochunk(pos.z);
local id = meta:get_int("cartographer:map_id");
if id == 0 then
id = map_from_meta(meta, player_x, player_y);
end
cartographer.fill_local(id, player_x, player_y);
minetest.show_formspec(player:get_player_name(), "map", cartographer.get_map_formspec_map(data, cartographer.get_map(id), player_x, player_y));
end,
});
function cartographer.create_map_item(size, detail, scale)
local map = ItemStack("cartographer:map");
local meta = map:get_meta();
meta:set_int("cartographer:size", size);
meta:set_int("cartographer:detail", detail);
meta:set_int("cartographer:scale", scale);
meta:set_string("description", "Empty Map\nUse to set the initial location");
return map;
end
function cartographer.copy_map_item(stack)
local meta = stack:get_meta();
local size = meta:get_int("cartographer:size");
local detail = meta:get_int("cartographer:detail");
local scale = meta:get_int("cartographer:scale");
local copy = cartographer.create_map_item(size, detail, scale);
local copy_meta = copy:get_meta();
local id = meta:get_int("cartographer:map_id");
local new_id = 0;
if id > 0 then
local src = cartographer.get_map(id);
new_id = cartographer.create_map(src.x, src.z, src.w, src.h, false, src.detail, src.scale);
local dest = cartographer.get_map(new_id);
for k,v in pairs(src.fill) do
dest.fill[k] = v;
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)) .. "]")
end
return copy;
end
function cartographer.resize_map_item(meta, size)
local old_size = meta:get_int("cartographer:size");
if old_size >= size then
return;
end
meta:set_int("cartographer:size", size);
local id = meta:get_int("cartographer:map_id");
if id > 0 then
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)) .. "]")
end
end