2020-03-08 15:34:20 +01:00
|
|
|
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
|
|
|
|
|
2020-02-16 18:55:07 +01:00
|
|
|
minetest.register_node("cartographer:map", {
|
|
|
|
description = "Map",
|
|
|
|
inventory_image = "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
|
2020-03-08 15:34:20 +01:00
|
|
|
id = map_from_meta(meta, player_x, player_y);
|
2020-02-16 18:55:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
cartographer.fill_local(id, player_x, player_y);
|
|
|
|
|
2020-03-08 15:06:26 +01:00
|
|
|
minetest.show_formspec(player:get_player_name(), "map", cartographer.get_map_formspec_map(data, cartographer.get_map(id), player_x, player_y));
|
2020-02-16 18:55:07 +01:00
|
|
|
|
|
|
|
return stack;
|
|
|
|
end,
|
|
|
|
|
|
|
|
after_place_node = function(stack, placer, pointed_thing)
|
|
|
|
local meta = stack:get_meta():to_table();
|
|
|
|
local s = minetest.item_place(stack, placer, pointed_thing);
|
|
|
|
|
|
|
|
local node_meta = minetest.get_meta(pointed_thing);
|
|
|
|
node_meta:from_table(meta);
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
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
|
2020-03-08 15:34:20 +01:00
|
|
|
id = map_from_meta(meta, player_x, player_y);
|
2020-02-16 18:55:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
cartographer.fill_local(id, player_x, player_y);
|
|
|
|
|
2020-03-08 15:06:26 +01:00
|
|
|
minetest.show_formspec(player:get_player_name(), "map", cartographer.get_map_formspec_map(data, cartographer.get_map(id), player_x, player_y));
|
2020-02-16 18:55:07 +01:00
|
|
|
end,
|
|
|
|
});
|
|
|
|
|
2020-03-08 15:34:20 +01:00
|
|
|
function cartographer.create_map_item(size, detail, scale)
|
2020-02-16 18:55:07 +01:00
|
|
|
local map = ItemStack("cartographer:map");
|
|
|
|
local meta = map:get_meta();
|
|
|
|
meta:set_int("cartographer:size", size);
|
|
|
|
meta:set_int("cartographer:detail", detail);
|
2020-03-08 15:34:20 +01:00
|
|
|
meta:set_int("cartographer:scale", scale);
|
2020-02-16 18:55:07 +01:00
|
|
|
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");
|
2020-03-08 15:34:20 +01:00
|
|
|
local scale = meta:get_int("cartographer:scale");
|
2020-02-16 18:55:07 +01:00
|
|
|
|
2020-03-08 15:34:20 +01:00
|
|
|
local copy = cartographer.create_map_item(size, detail, scale);
|
2020-02-16 18:55:07 +01:00
|
|
|
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);
|
|
|
|
|
2020-03-08 15:34:20 +01:00
|
|
|
new_id = cartographer.create_map(src.x, src.z, src.w, src.h, false, src.detail, src.scale);
|
2020-02-16 18:55:07 +01:00
|
|
|
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
|