Add map scale support to the advanced table

This commit is contained in:
Hugues Ross 2020-03-08 10:34:20 -04:00
parent 85eeb529b6
commit 8f786cda9a
4 changed files with 53 additions and 24 deletions

View File

@ -1,3 +1,21 @@
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
minetest.register_node("cartographer:map", {
description = "Map",
inventory_image = "cartographer_map.png",
@ -15,14 +33,7 @@ minetest.register_node("cartographer:map", {
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)) .. "]")
id = map_from_meta(meta, player_x, player_y);
end
cartographer.fill_local(id, player_x, player_y);
@ -50,10 +61,7 @@ minetest.register_node("cartographer:map", {
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);
id = map_from_meta(meta, player_x, player_y);
end
cartographer.fill_local(id, player_x, player_y);
@ -62,11 +70,12 @@ minetest.register_node("cartographer:map", {
end,
});
function cartographer.create_map_item(size, detail)
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;
@ -77,8 +86,9 @@ function cartographer.copy_map_item(stack)
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);
local copy = cartographer.create_map_item(size, detail, scale);
local copy_meta = copy:get_meta();
local id = meta:get_int("cartographer:map_id");
@ -86,7 +96,7 @@ function cartographer.copy_map_item(stack)
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);
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;

View File

@ -1,4 +1,4 @@
function cartographer.create_map(x, z, w, h, filled, detail)
function cartographer.create_map(x, z, w, h, filled, detail, scale)
local id = _cartographer.next_map_id;
local map = {
@ -8,6 +8,7 @@ function cartographer.create_map(x, z, w, h, filled, detail)
w = w,
h = h,
detail = detail,
scale = scale,
fill = {},
};

View File

@ -65,5 +65,5 @@ function cartographer.get_map_formspec(data, x, y, w, h, detail)
end
function cartographer.get_map_formspec_map(data, map, x, y)
return map_formspec_prefix:format(map.w * scale, map.h * scale)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, 1, cartographer.is_filled, map);
return map_formspec_prefix:format(map.w * scale, map.h * scale)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, cartographer.is_filled, map);
end

View File

@ -1,5 +1,7 @@
local SIZE_SMALL = 20;
local SIZE_LARGE = 40;
local SCALE_SMALL = 1;
local SCALE_LARGE = 4;
local separator = [[
formspec_version[3]
@ -74,11 +76,6 @@ function fs.materials(x, y, rank, meta)
]];
data = data:format(x, y, paper, pigment);
if rank > 1 then
data = data .. "label[6,0.25;Stuff x %d]";
data = data:format(0);
end
return data .. "container_end[]";
end
@ -132,6 +129,20 @@ function fs.craft(x, y, pos, rank, meta)
data = data:format(size);
end
if rank > 2 then
local scale = "1x";
if meta:get_int("scale") == SCALE_LARGE then
scale = "4x";
end
data = data .. [[
style[%s;bgcolor=blue]
button[3.5,0;0.5,0.5;1x;1x]
button[4.0,0;0.5,0.5;4x;4x]
]];
data = data:format(scale);
end
local detail = tostring(meta:get_int("detail") + 1);
data = data .. [[
style[%s;bgcolor=blue]
@ -269,6 +280,7 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
elseif fields["craft"] ~= nil then
local size = meta:get_int("size");
local detail = meta:get_int("detail");
local scale = meta:get_int("scale");
local paper = meta:get_int("paper");
local pigment = meta:get_int("pigment");
@ -285,7 +297,7 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
meta:set_int("paper", paper);
meta:set_int("pigment", pigment);
inv:set_stack("output", 1, cartographer.create_map_item(size, 1 + detail));
inv:set_stack("output", 1, cartographer.create_map_item(size, 1 + detail, scale));
end
else
local smeta = stack:get_meta();
@ -354,6 +366,12 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
elseif fields["4"] ~= nil then
meta:set_int("detail", 3);
table_formspec(player:get_player_name(), 2)
elseif fields["1x"] ~= nil then
meta:set_int("scale", SCALE_SMALL);
table_formspec(player:get_player_name(), 2)
elseif fields["4x"] ~= nil then
meta:set_int("scale", SCALE_LARGE);
table_formspec(player:get_player_name(), 2)
elseif fields["tabs"] ~= nil then
table_formspec(player:get_player_name(), tonumber(fields["tabs"]));
end
@ -368,7 +386,7 @@ local function setup_table_node(pos)
meta:get_inventory():set_size("copy_output", 1);
meta:set_int("size", SIZE_SMALL);
meta:set_int("scale", 1);
meta:set_int("scale", SCALE_SMALL);
meta:set_int("detail", 0);
end