2017-02-23 16:35:42 +01:00
|
|
|
-- storage/api.lua
|
|
|
|
|
|
|
|
local BASENAME = "microexpansion"
|
|
|
|
|
|
|
|
-- [function] register cell
|
|
|
|
function microexpansion.register_cell(itemstring, def)
|
2017-07-25 19:47:25 +02:00
|
|
|
if not def.inventory_image then
|
|
|
|
def.inventory_image = itemstring
|
|
|
|
end
|
2017-02-23 16:35:42 +01:00
|
|
|
|
2017-07-25 19:47:25 +02:00
|
|
|
-- register craftitem
|
|
|
|
minetest.register_craftitem(BASENAME..":"..itemstring, {
|
|
|
|
description = def.description,
|
|
|
|
inventory_image = BASENAME.."_"..def.inventory_image..".png",
|
|
|
|
groups = {microexpansion_cell = 1},
|
|
|
|
stack_max = 1,
|
|
|
|
microexpansion = {
|
|
|
|
base_desc = def.description,
|
|
|
|
drive = {
|
|
|
|
capacity = def.capacity or 5000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2017-02-23 16:35:42 +01:00
|
|
|
|
2017-07-25 19:47:25 +02:00
|
|
|
-- if recipe, register recipe
|
|
|
|
if def.recipe then
|
2020-03-04 16:46:22 +01:00
|
|
|
microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
|
2017-07-25 19:47:25 +02:00
|
|
|
end
|
2017-02-23 16:35:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Get cell size
|
|
|
|
function microexpansion.get_cell_size(name)
|
2019-05-07 17:49:26 +02:00
|
|
|
if minetest.get_item_group(name, "microexpansion_cell") == 0 then
|
|
|
|
return 0
|
2017-07-25 19:47:25 +02:00
|
|
|
end
|
2019-05-07 17:49:26 +02:00
|
|
|
local item = minetest.registered_craftitems[name]
|
|
|
|
return item.microexpansion.drive.capacity
|
2017-02-23 16:35:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Calculate max stacks
|
|
|
|
function microexpansion.int_to_stacks(int)
|
2019-05-07 17:49:26 +02:00
|
|
|
return math.ceil(int / 99)
|
2017-02-23 16:35:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Calculate number of pages
|
|
|
|
function microexpansion.int_to_pagenum(int)
|
2017-07-25 19:47:25 +02:00
|
|
|
return math.floor(microexpansion.int_to_stacks(int) / 32)
|
2017-02-23 16:35:42 +01:00
|
|
|
end
|
2017-02-23 23:31:02 +01:00
|
|
|
|
2023-10-20 18:31:56 +02:00
|
|
|
--[[ [function] Move items from inv to inv
|
2020-03-03 17:16:35 +01:00
|
|
|
function microexpansion.move_inv(inv1, inv2, max)
|
|
|
|
if max <= 0 then return end
|
|
|
|
local finv, tinv = inv1.inv, inv2.inv
|
|
|
|
local fname, tname = inv1.name, inv2.name
|
|
|
|
local huge = inv2.huge
|
|
|
|
local inserted = 0
|
2017-02-23 23:31:02 +01:00
|
|
|
|
2020-03-03 17:16:35 +01:00
|
|
|
for _,v in ipairs(finv:get_list(fname) or {}) do
|
|
|
|
local left = max-inserted
|
|
|
|
if left <= 0 then
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
if not v:is_empty() then
|
|
|
|
if v:get_count() > left then
|
|
|
|
v = v:peek_item(left)
|
|
|
|
end
|
2020-03-04 16:46:22 +01:00
|
|
|
if tinv and tinv:room_for_item(tname, v) then
|
|
|
|
if huge then
|
|
|
|
microexpansion.insert_item(v, tinv, tname)
|
|
|
|
finv:remove_item(fname, v)
|
|
|
|
else
|
|
|
|
local leftover = tinv:add_item(tname, v)
|
2020-03-03 17:16:35 +01:00
|
|
|
finv:remove_item(fname, v)
|
|
|
|
if leftover and not(leftover:is_empty()) then
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("leftover items when transferring inventory","warning")
|
2020-03-04 16:46:22 +01:00
|
|
|
finv:add_item(fname, leftover)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inserted = inserted + v:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-23 23:31:02 +01:00
|
|
|
end
|
2023-10-20 18:31:56 +02:00
|
|
|
]]
|