cartographer/items.lua
2020-02-16 12:55:07 -05:00

147 lines
5.4 KiB
Lua

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 spawn_x = nil;
local spawn_y = nil;
if _cartographer.spawns[name] ~= nil then
spawn_x = tochunk(_cartographer.spawns[name].x);
spawn_y = tochunk(_cartographer.spawns[name].z);
end
local death_x = nil;
local death_y = nil;
if _cartographer.last_deaths[name] ~= nil then
death_x = tochunk(_cartographer.last_deaths[name].x);
death_y = tochunk(_cartographer.last_deaths[name].z);
end
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
local size = meta:get_int("cartographer:size") or 10;
local detail = meta:get_int("cartographer:detail") or 1;
local map_x = math.floor((player_x + 10)/size)*size-10
local map_y = math.floor((player_y + 10)/size)*size-10;
id = cartographer.create_map(map_x,map_y,size,size,false, detail);
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 + size)) .. "," .. tostring(fromchunk(map_y + size)) .. "]")
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, spawn_x, spawn_y, death_x, death_y));
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 spawn_x = nil;
local spawn_y = nil;
if _cartographer.spawns[name] ~= nil then
spawn_x = tochunk(_cartographer.spawns[name].x);
spawn_y = tochunk(_cartographer.spawns[name].z);
end
local death_x = nil;
local death_y = nil;
if _cartographer.last_deaths[name] ~= nil then
death_x = tochunk(_cartographer.last_deaths[name].x);
death_y = tochunk(_cartographer.last_deaths[name].z);
end
local player_x = tochunk(pos.x);
local player_y = tochunk(pos.z);
local id = meta:get_int("cartographer:map_id");
if id == 0 then
local map_x = math.floor((player_x + 10)/20)*20-10
local map_y = math.floor((player_y + 10)/20)*20-10;
id = cartographer.create_map(map_x,map_y,20,20,false);
meta:set_int("cartographer:map_id", id);
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, spawn_x, spawn_y, death_x, death_y));
end,
});
function cartographer.create_map_item(size, detail)
local map = ItemStack("cartographer:map");
local meta = map:get_meta();
meta:set_int("cartographer:size", size);
meta:set_int("cartographer:detail", detail);
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 copy = cartographer.create_map_item(size, detail);
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);
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