add register_backpack_group

This commit is contained in:
unknown 2021-10-23 23:38:07 -04:00
parent caba7f3599
commit b27307a101
2 changed files with 38 additions and 17 deletions

View File

@ -161,18 +161,12 @@ local function get_inv_slots(data, fs)
fmt("list[current_player;main;%f,%f;%u,1;]", inv_x, inv_y, i3.HOTBAR_LEN))
if bag then
if bag == 1 then
width, size = 10, 0.892
elseif bag == 2 then
width, size = 11, 0.8
elseif bag == 3 then
width, size = 12, 0.726
end
width, size = i3.BAG_DEFS[bag].fs_width, i3.BAG_DEFS[bag].fs_size
end
fs(fmt("style_type[list;size=%f;spacing=%f]", size, spacing),
fmt("list[current_player;main;%f,%f;%u,%u;%u]", inv_x, inv_y + 1.15,
width, (bag and i3.BAG_SIZES[data.bag_size] or i3.INV_SIZE) / width, i3.HOTBAR_LEN),
width, (bag and i3.BAG_DEFS[data.bag_size].size or i3.INV_SIZE) / width, i3.HOTBAR_LEN),
"style_type[list;size=1;spacing=0.15]")
fs("listring[current_player;craft]listring[current_player;main]")
@ -377,7 +371,7 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
if not data.bag:get_stack("main", 1):is_empty() then
fs("hypertext", 1.2, yextra + 0.89, ctn_len - 1.9, 0.8, "bpk",
ES("The inventory is extended by @1 slots", i3.BAG_SIZES[data.bag_size] - i3.INV_SIZE))
ES("The inventory is extended by @1 slots", i3.BAG_DEFS[data.bag_size].size - i3.INV_SIZE))
end
elseif data.subcat == 2 then

View File

@ -14,10 +14,22 @@ i3 = {
MIN_FORMSPEC_VERSION = 4,
SAVE_INTERVAL = 600, -- Player data save interval (in seconds)
BAG_SIZES = {
4*9 + 3,
4*9 + 6,
4*9 + 9,
BAG_DEFS = {
{
size = 4*9 + 3,
fs_size = 0.892,
fs_width = 10,
},
{
size = 4*9 + 6,
fs_size = 0.8,
fs_width = 11,
},
{
size = 4*9 + 9,
fs_size = 0.726,
fs_width = 12,
}
},
SUBCAT = {
@ -925,6 +937,21 @@ function i3.override_tab(tabname, newdef)
end
end
function i3.register_backpack_group(def)
local slots = def.slots
local width = 9 + (slots/3)
assert(slots, "i3.register_backpack: slots definition missing")
assert(slots%3 == 0, "i3.register_backpack: slots not divisable by 3")
i3.BAG_DEFS[#i3.BAG_DEFS+1] = {
size = 4*9 + slots,
fs_size = 8/width,
fs_width = width,
}
return #i3.BAG_DEFS
end
local function init_data(player, info)
local name = player:get_player_name()
i3.data[name] = i3.data[name] or {}
@ -1446,14 +1473,14 @@ local function init_backpack(player)
data.bag_size = convert[data.bag_size]
end
inv:set_size("main", data.bag_size and i3.BAG_SIZES[data.bag_size] or i3.INV_SIZE)
inv:set_size("main", data.bag_size and i3.BAG_DEFS[data.bag_size].size or i3.INV_SIZE)
data.bag = create_inventory(fmt("%s_backpack", name), {
allow_put = function(_inv, listname, _, stack)
local empty = _inv:get_stack(listname, 1):is_empty()
local item_group = minetest.get_item_group(stack:get_name(), "bag")
if empty and item_group > 0 and item_group <= #i3.BAG_SIZES then
if empty and item_group > 0 and item_group <= #i3.BAG_DEFS then
return 1
end
@ -1467,12 +1494,12 @@ local function init_backpack(player)
data.bag_item = stackname
data.bag_size = minetest.get_item_group(stackname, "bag")
inv:set_size("main", i3.BAG_SIZES[data.bag_size])
inv:set_size("main", i3.BAG_DEFS[data.bag_size].size)
set_fs(player)
end,
on_take = function()
for i = i3.INV_SIZE + 1, i3.BAG_SIZES[data.bag_size] do
for i = i3.INV_SIZE + 1, i3.BAG_DEFS[data.bag_size].size do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then