mirror of
https://github.com/theFox6/microexpansion.git
synced 2024-11-19 22:03:54 +01:00
made drives store cells that store items and can be acessed from chest
This commit is contained in:
parent
d949aaccf4
commit
745a2afcd1
4
init.lua
4
init.lua
@ -11,6 +11,10 @@ local worldpath = microexpansion.worldpath -- Worldpath pointer
|
||||
microexpansion.gui_bg = "bgcolor[#080808BB;true]background[5,5;1,1;gui_formbg.png;true]"
|
||||
microexpansion.gui_slots = "listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]"
|
||||
|
||||
microexpansion.settings = {
|
||||
huge_stacks = minetest.settings:get_bool("microexpansion_huge_stacks")
|
||||
}
|
||||
|
||||
-- logger
|
||||
function microexpansion.log(content, log_type)
|
||||
assert(content, "microexpansion.log: missing content")
|
||||
|
@ -3,6 +3,55 @@
|
||||
local me = microexpansion
|
||||
local network = me.network
|
||||
|
||||
local function update_ctrl(pos,node)
|
||||
local network = me.get_network(pos)
|
||||
local size = network:get_item_capacity()
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size("main", me.int_to_stacks(size))
|
||||
end
|
||||
|
||||
function me.insert_item(stack, inv, listname)
|
||||
if me.settings.huge_stacks == false then
|
||||
inv:add_item(listname, stack)
|
||||
return
|
||||
end
|
||||
local stack_name
|
||||
local stack_count
|
||||
if type(stack) == "string" then
|
||||
local split_string = stack:split(" ")
|
||||
stack_name = split_string[1]
|
||||
if (#split_string > 1) then
|
||||
stack_count = tonumber(split_string[2])
|
||||
else
|
||||
stack_count = 1
|
||||
end
|
||||
else
|
||||
stack_name = stack:get_name()
|
||||
stack_count = stack:get_count()
|
||||
end
|
||||
local found = false
|
||||
for i = 0, inv:get_size(listname) do
|
||||
local inside = inv:get_stack(listname, i)
|
||||
if inside:get_name() == stack_name then
|
||||
local total_count = inside:get_count() + stack_count
|
||||
-- bigger item count is not possible we only have unsigned 16 bit
|
||||
if total_count <= math.pow(2,16) then
|
||||
if not inside:set_count(total_count) then
|
||||
minetest.log("error"," adding items to stack in microexpansion network failed")
|
||||
print("stack is now " .. inside:to_string())
|
||||
end
|
||||
inv:set_stack(listname, i, inside)
|
||||
found = true
|
||||
break;
|
||||
end
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
inv:add_item(listname, stack)
|
||||
end
|
||||
end
|
||||
|
||||
-- [register node] Controller
|
||||
me.register_node("ctrl", {
|
||||
description = "ME Controller",
|
||||
@ -34,8 +83,7 @@ me.register_node("ctrl", {
|
||||
},
|
||||
groups = { cracky = 1, me_connect = 1, },
|
||||
connect_sides = "nobottom",
|
||||
status = "unstable",
|
||||
|
||||
me_update = update_ctrl,
|
||||
after_place_node = function(pos, player)
|
||||
local name = player:get_player_name()
|
||||
local meta = minetest.get_meta(pos)
|
||||
@ -58,6 +106,40 @@ me.register_node("ctrl", {
|
||||
after_dig_node = function(pos)
|
||||
me.update_connected_machines(pos)
|
||||
end,
|
||||
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
local inside_stack = inv:get_stack(listname, index)
|
||||
local stack_name = stack:get_name()
|
||||
-- improve performance by skipping unnessecary calls
|
||||
if inside_stack:get_name() ~= stack_name or inside_stack:get_count() >= inside_stack:get_stack_max() then
|
||||
if inv:get_stack(listname, index+1):get_name() ~= "" then
|
||||
return stack:get_count()
|
||||
end
|
||||
end
|
||||
local max_slots = inv:get_size(listname)
|
||||
local max_items = math.floor(max_slots * 99)
|
||||
|
||||
local slots, items = 0, 0
|
||||
-- Get amount of items in drive
|
||||
for i = 1, max_slots do
|
||||
local stack = inv:get_stack("main", i)
|
||||
if stack:get_name() ~= "" then
|
||||
slots = slots + 1
|
||||
local num = stack:get_count()
|
||||
if num == 0 then num = 1 end
|
||||
items = items + num
|
||||
end
|
||||
end
|
||||
return math.max(math.min(stack:get_count(),max_items-items),0)
|
||||
end,
|
||||
on_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
inv:remove_item(listname, stack)
|
||||
me.insert_item(stack, inv, listname)
|
||||
end,
|
||||
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
return math.min(stack:get_count(),stack:get_stack_max())
|
||||
end,
|
||||
machine = {
|
||||
type = "transporter",
|
||||
},
|
||||
@ -82,7 +164,6 @@ me.register_machine("cable", {
|
||||
},
|
||||
paramtype = "light",
|
||||
groups = { crumbly = 1, },
|
||||
status = "unstable",
|
||||
after_place_node = me.update_connected_machines,
|
||||
after_dig_node = me.update_connected_machines,
|
||||
machine = {
|
||||
|
@ -58,14 +58,14 @@ function me.get_connected_network(start_pos)
|
||||
if me.get_node(npos).name == "microexpansion:ctrl" then
|
||||
local network = me.get_network(npos)
|
||||
if network then
|
||||
return network
|
||||
return network,npos
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function me.update_connected_machines(start_pos)
|
||||
--print("updating connected machines")
|
||||
print("updating connected machines")
|
||||
for npos in me.connected_nodes(start_pos) do
|
||||
me.update_node(npos)
|
||||
end
|
||||
|
@ -3,23 +3,18 @@
|
||||
-- @field #table controller_pos the position of the controller
|
||||
-- @field #number power_load the power currently provided to the network
|
||||
-- @field #number power_storage the power that can be stored for the next tick
|
||||
-- @field #table items the itemstacks stored in the networks drives
|
||||
-- @field #number item_capacity the amount of items that can be stored in the networks drives
|
||||
local network = {
|
||||
power_load = 0,
|
||||
power_storage = 0,
|
||||
--for testing it's at 3200 will be set to 0 when the drives work
|
||||
item_capacity = 3200,
|
||||
power_storage = 0
|
||||
}
|
||||
microexpansion.network = network
|
||||
|
||||
--- construct a new network
|
||||
-- @function [parent=#network] new
|
||||
-- @param #table o the object to become a network or nil
|
||||
-- @return #table the new network object
|
||||
function network:new(o)
|
||||
local n = setmetatable(o or {}, {__index = self})
|
||||
o.items = {}
|
||||
return o
|
||||
return setmetatable(o or {}, {__index = self})
|
||||
end
|
||||
|
||||
--- check if a node can be connected
|
||||
@ -105,3 +100,30 @@ end
|
||||
function network:remove_overload()
|
||||
self.power_load = math.min(self.power_load, self.power_storage)
|
||||
end
|
||||
|
||||
--- get a drives item capacity
|
||||
-- @function get_drive_capacity
|
||||
-- @param #table pos the position of the drive
|
||||
-- @return #number the number of items that can be stored in the drive
|
||||
local function get_drive_capacity(pos)
|
||||
local cap = 0
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
for i = 1, inv:get_size("main") do
|
||||
cap = cap + microexpansion.get_cell_size(inv:get_stack("main", i):get_name())
|
||||
end
|
||||
return cap
|
||||
end
|
||||
|
||||
--- get the item capacity of a network
|
||||
-- @function [parent=#network] get_item_capacity
|
||||
-- @return #number the total number of items that can be stored in the network
|
||||
function network:get_item_capacity()
|
||||
local cap = 0
|
||||
for npos in microexpansion.connected_nodes(self.controller_pos) do
|
||||
if microexpansion.get_node(npos).name == "microexpansion:drive" then
|
||||
cap = cap + get_drive_capacity(npos)
|
||||
end
|
||||
end
|
||||
return cap
|
||||
end
|
||||
|
@ -33,15 +33,16 @@ end
|
||||
|
||||
-- [function] Get cell size
|
||||
function microexpansion.get_cell_size(name)
|
||||
local item = minetest.registered_craftitems[name]
|
||||
if item then
|
||||
return item.microexpansion.drive.capacity
|
||||
if minetest.get_item_group(name, "microexpansion_cell") == 0 then
|
||||
return 0
|
||||
end
|
||||
local item = minetest.registered_craftitems[name]
|
||||
return item.microexpansion.drive.capacity
|
||||
end
|
||||
|
||||
-- [function] Calculate max stacks
|
||||
function microexpansion.int_to_stacks(int)
|
||||
return math.floor(int / 99)
|
||||
return math.ceil(int / 99)
|
||||
end
|
||||
|
||||
-- [function] Calculate number of pages
|
||||
@ -54,6 +55,7 @@ function microexpansion.move_inv(inv1, inv2)
|
||||
local finv, tinv = inv1.inv, inv2.inv
|
||||
local fname, tname = inv1.name, inv2.name
|
||||
|
||||
--FIXME only as many as allowed in a drive
|
||||
for i,v in ipairs(finv:get_list(fname) or {}) do
|
||||
if tinv and tinv:room_for_item(tname, v) then
|
||||
local leftover = tinv:add_item( tname, v )
|
||||
@ -64,37 +66,3 @@ function microexpansion.move_inv(inv1, inv2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- [function] Update cell description
|
||||
function microexpansion.cell_desc(inv, listname, spos)
|
||||
local stack = inv:get_stack(listname, spos)
|
||||
|
||||
if stack:get_name() ~= "" then
|
||||
local meta = stack:get_meta()
|
||||
local base_desc = minetest.registered_craftitems[stack:get_name()].microexpansion.base_desc
|
||||
local max_slots = inv:get_size("main")
|
||||
local max_items = math.floor(max_slots * 99)
|
||||
|
||||
local slots, items = 0, 0
|
||||
-- Get amount of items in drive
|
||||
for i = 1, max_items do
|
||||
local stack = inv:get_stack("main", i)
|
||||
local item = stack:get_name()
|
||||
if item ~= "" then
|
||||
slots = slots + 1
|
||||
local num = stack:get_count()
|
||||
if num == 0 then num = 1 end
|
||||
items = items + num
|
||||
end
|
||||
end
|
||||
|
||||
-- Calculate Percentage
|
||||
local percent = math.floor(items / max_items * 100)
|
||||
|
||||
-- Update description
|
||||
meta:set_string("description", base_desc.."\n"..
|
||||
minetest.colorize("grey", tostring(items).."/"..tostring(max_items).." Items ("..tostring(percent).."%)"))
|
||||
-- Update stack
|
||||
inv:set_stack(listname, spos, stack)
|
||||
end
|
||||
end
|
||||
|
@ -8,24 +8,39 @@ local function chest_formspec(pos, start_id, listname, page_max, query)
|
||||
local page_number = ""
|
||||
local buttons = ""
|
||||
local query = query or ""
|
||||
local net,cp = me.get_connected_network(pos)
|
||||
|
||||
if not listname then
|
||||
list = "label[3,2;" .. minetest.colorize("red", "No drive!") .. "]"
|
||||
if cp then
|
||||
if listname and net:get_item_capacity() > 0 then
|
||||
if listname == "main" then
|
||||
list = "list[nodemeta:"..cp.x..","..cp.y..","..cp.z..";" .. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
|
||||
else
|
||||
list = "list[context;" .. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
|
||||
end
|
||||
list = list .. [[
|
||||
list[current_player;main;0,5.5;8,1;]
|
||||
list[current_player;main;0,6.73;8,3;8]
|
||||
listring[nodemeta:]]..cp.x..","..cp.y..","..cp.z..[[;main]
|
||||
listring[current_player;main]
|
||||
]]
|
||||
buttons = [[
|
||||
button[3.56,4.35;1.8,0.9;tochest;To Drive]
|
||||
tooltip[tochest;Move everything from your inventory to the ME network.]
|
||||
button[5.4,4.35;0.8,0.9;prev;<]
|
||||
button[7.25,4.35;0.8,0.9;next;>]
|
||||
tooltip[prev;Previous]
|
||||
tooltip[next;Next]
|
||||
field[0.29,4.6;2.2,1;filter;;]]..query..[[]
|
||||
button[2.1,4.5;0.8,0.5;search;?]
|
||||
button[2.75,4.5;0.8,0.5;clear;X]
|
||||
tooltip[search;Search]
|
||||
tooltip[clear;Reset]
|
||||
]]
|
||||
else
|
||||
list = "label[3,2;" .. minetest.colorize("red", "No connected drives!") .. "]"
|
||||
end
|
||||
else
|
||||
list = "list[current_name;" .. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
|
||||
buttons = [[
|
||||
button[3.56,4.35;1.8,0.9;tochest;To Drive]
|
||||
tooltip[tochest;Move everything from your inventory to the ME network.]
|
||||
button[5.4,4.35;0.8,0.9;prev;<]
|
||||
button[7.25,4.35;0.8,0.9;next;>]
|
||||
tooltip[prev;Previous]
|
||||
tooltip[next;Next]
|
||||
field[0.29,4.6;2.2,1;filter;;]]..query..[[]
|
||||
button[2.1,4.5;0.8,0.5;search;?]
|
||||
button[2.75,4.5;0.8,0.5;clear;X]
|
||||
tooltip[search;Search]
|
||||
tooltip[clear;Reset]
|
||||
]]
|
||||
list = "label[3,2;" .. minetest.colorize("red", "No connected network!") .. "]"
|
||||
end
|
||||
if page_max then
|
||||
page_number = "label[6.15,4.5;" .. math.floor((start_id / 32)) + 1 ..
|
||||
@ -40,10 +55,6 @@ local function chest_formspec(pos, start_id, listname, page_max, query)
|
||||
list ..
|
||||
[[
|
||||
label[0,-0.23;ME Chest]
|
||||
list[current_player;main;0,5.5;8,1;]
|
||||
list[current_player;main;0,6.73;8,3;8]
|
||||
listring[current_name;main]
|
||||
listring[current_player;main]
|
||||
field_close_on_enter[filter;false]
|
||||
]]..
|
||||
page_number ..
|
||||
@ -55,18 +66,13 @@ local function update_chest(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
if network == nil then
|
||||
inv:set_size("main", 0)
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("formspec", chest_formspec(pos, 1))
|
||||
return
|
||||
end
|
||||
local items = network.items
|
||||
local size = network.item_capacity
|
||||
local size = network:get_item_capacity()
|
||||
local page_max = me.int_to_pagenum(size) + 1
|
||||
inv:set_size("main", me.int_to_stacks(size))
|
||||
if items then
|
||||
inv:set_list("main", items)
|
||||
end
|
||||
|
||||
meta:set_string("inv_name", "main")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
||||
end
|
||||
@ -98,77 +104,30 @@ microexpansion.register_node("chest", {
|
||||
update_chest(pos)
|
||||
end
|
||||
end,
|
||||
can_dig = function(pos, player)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
return inv:is_empty("main")
|
||||
end,
|
||||
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||
if listname == "main" then
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
local max_slots = inv:get_size(listname)
|
||||
local max_items = math.floor(max_slots * 99)
|
||||
|
||||
local slots, items = 0, 0
|
||||
-- Get amount of items in drive
|
||||
for i = 1, max_items do
|
||||
local stack = inv:get_stack("main", i)
|
||||
local item = stack:get_name()
|
||||
if item ~= "" then
|
||||
slots = slots + 1
|
||||
local num = stack:get_count()
|
||||
if num == 0 then num = 1 end
|
||||
items = items + num
|
||||
end
|
||||
end
|
||||
|
||||
return math.min(stack:get_count(),max_items-items)
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end,
|
||||
on_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||
if listname == "main" then
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
inv:remove_item(listname, stack)
|
||||
local stackname = stack:get_name()
|
||||
local found = false
|
||||
for i = 0, inv:get_size(listname) do
|
||||
local inside = inv:get_stack(listname, i)
|
||||
if inside:get_name() == stackname then
|
||||
inside:set_count(inside:get_count() + stack:get_count())
|
||||
inv:set_stack(listname, i, inside)
|
||||
found = true
|
||||
break;
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
inv:add_item(listname, stack)
|
||||
end
|
||||
local network = me.get_connected_network(pos)
|
||||
network.items = inv:get_list("main")
|
||||
me.update_connected_machines(pos)
|
||||
end
|
||||
end,
|
||||
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
return math.min(stack:get_count(),stack:get_stack_max())
|
||||
end,
|
||||
on_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
if listname == "search" then
|
||||
local _,cp = me.get_connected_network(pos)
|
||||
local inv = minetest.get_meta(cp):get_inventory()
|
||||
inv:remove_item("main", stack)
|
||||
end
|
||||
local network = me.get_connected_network(pos)
|
||||
network.items = inv:get_list("main")
|
||||
me.update_connected_machines(pos)
|
||||
end,
|
||||
on_receive_fields = function(pos, formname, fields, sender)
|
||||
local network,cp = me.get_connected_network(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local page = meta:get_int("page")
|
||||
local inv_name = meta:get_string("inv_name")
|
||||
local inv = meta:get_inventory()
|
||||
local page_max = math.floor(inv:get_size("main") / 32) + 1
|
||||
local network = me.get_connected_network(pos)
|
||||
local own_inv = meta:get_inventory()
|
||||
local ctrl_inv
|
||||
if cp then
|
||||
ctrl_inv = minetest.get_meta(cp):get_inventory()
|
||||
end
|
||||
local inv
|
||||
if inv_name == "main" then
|
||||
inv = ctrl_inv
|
||||
else
|
||||
inv = own_inv
|
||||
end
|
||||
local page_max = math.floor(inv:get_size(inv_name) / 32) + 1
|
||||
if inv_name == "none" then
|
||||
return
|
||||
end
|
||||
@ -185,32 +144,32 @@ microexpansion.register_node("chest", {
|
||||
meta:set_int("page", page - 32)
|
||||
meta:set_string("formspec", chest_formspec(pos, page - 32, inv_name, page_max))
|
||||
elseif fields.search or fields.key_enter_field == "filter" then
|
||||
inv:set_size("search", 0)
|
||||
own_inv:set_size("search", 0)
|
||||
if fields.filter == "" then
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("inv_name", "main")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
||||
else
|
||||
local tab = {}
|
||||
for i = 1, inv:get_size("main") do
|
||||
local match = inv:get_stack("main", i):get_name():find(fields.filter)
|
||||
for i = 1, ctrl_inv:get_size("main") do
|
||||
local match = ctrl_inv:get_stack("main", i):get_name():find(fields.filter)
|
||||
if match then
|
||||
tab[#tab + 1] = inv:get_stack("main", i)
|
||||
tab[#tab + 1] = ctrl_inv:get_stack("main", i)
|
||||
end
|
||||
end
|
||||
inv:set_list("search", tab)
|
||||
own_inv:set_list("search", tab)
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("inv_name", "search")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "search", page_max, fields.filter))
|
||||
end
|
||||
elseif fields.clear then
|
||||
inv:set_size("search", 0)
|
||||
own_inv:set_size("search", 0)
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("inv_name", "main")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
||||
elseif fields.tochest then
|
||||
local pinv = minetest.get_inventory({type="player", name=sender:get_player_name()})
|
||||
microexpansion.move_inv({ inv=pinv, name="main" }, { inv=inv, name="main" })
|
||||
microexpansion.move_inv({ inv=pinv, name="main" }, { inv=ctrl_inv, name="main" })
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
@ -2,79 +2,57 @@
|
||||
|
||||
local me = microexpansion
|
||||
|
||||
-- [me chest] Get formspec
|
||||
local function chest_formspec(pos, start_id, listname, page_max, query)
|
||||
local list
|
||||
local page_number = ""
|
||||
local buttons = ""
|
||||
local query = query or ""
|
||||
local function update_drive(pos)
|
||||
--FIXME: check if we got connected/disconnected and reroute items
|
||||
end
|
||||
|
||||
if not listname then
|
||||
list = "label[3,2;" .. minetest.colorize("red", "No cell!") .. "]"
|
||||
else
|
||||
list = "list[current_name;" .. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
|
||||
buttons = [[
|
||||
button[3.56,4.35;1.8,0.9;tochest;To Drive]
|
||||
tooltip[tochest;Move everything from your inventory to the ME drive.]
|
||||
button[5.4,4.35;0.8,0.9;prev;<]
|
||||
button[7.25,4.35;0.8,0.9;next;>]
|
||||
tooltip[prev;Previous]
|
||||
tooltip[next;Next]
|
||||
field[0.29,4.6;2.2,1;filter;;]]..query..[[]
|
||||
button[2.1,4.5;0.8,0.5;search;?]
|
||||
button[2.75,4.5;0.8,0.5;clear;X]
|
||||
tooltip[search;Search]
|
||||
tooltip[clear;Reset]
|
||||
]]
|
||||
end
|
||||
if page_max then
|
||||
page_number = "label[6.15,4.5;" .. math.floor((start_id / 32)) + 1 ..
|
||||
"/" .. page_max .."]"
|
||||
end
|
||||
|
||||
return [[
|
||||
size[9,9.5]
|
||||
]]..
|
||||
microexpansion.gui_bg ..
|
||||
microexpansion.gui_slots ..
|
||||
list ..
|
||||
[[
|
||||
label[0,-0.23;ME Chest]
|
||||
list[current_name;cells;8.06,1.8;1,1;]
|
||||
list[current_player;main;0,5.5;8,1;]
|
||||
list[current_player;main;0,6.73;8,3;8]
|
||||
listring[current_name;main]
|
||||
listring[current_player;main]
|
||||
field_close_on_enter[filter;false]
|
||||
]]..
|
||||
page_number ..
|
||||
buttons
|
||||
local function write_to_cell(cell, items, item_count)
|
||||
local size = microexpansion.get_cell_size(cell:get_name())
|
||||
local item_meta = cell:get_meta()
|
||||
item_meta:set_string("items", minetest.serialize(items))
|
||||
local base_desc = minetest.registered_craftitems[cell:get_name()].microexpansion.base_desc
|
||||
-- Calculate Percentage
|
||||
local percent = math.floor(item_count / size * 100)
|
||||
-- Update description
|
||||
item_meta:set_string("description", base_desc.."\n"..
|
||||
minetest.colorize("grey", tostring(item_count).."/"..tostring(size).." Items ("..tostring(percent).."%)"))
|
||||
return cell
|
||||
end
|
||||
|
||||
-- [me chest] Register node
|
||||
microexpansion.register_node("chest", {
|
||||
description = "ME Chest",
|
||||
usedfor = "Can interact with items in ME storage cells",
|
||||
microexpansion.register_node("drive", {
|
||||
description = "ME Drive",
|
||||
usedfor = "Stores items into ME storage cells",
|
||||
tiles = {
|
||||
"chest_top",
|
||||
"chest_top",
|
||||
"chest_side",
|
||||
"chest_side",
|
||||
"chest_side",
|
||||
"chest_front",
|
||||
"drive_full",
|
||||
},
|
||||
is_ground_content = false,
|
||||
groups = { cracky = 1 },
|
||||
groups = { cracky = 1, me_connect = 1 },
|
||||
paramtype = "light",
|
||||
paramtype2 = "facedir",
|
||||
|
||||
me_update = update_drive,
|
||||
on_construct = function(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("formspec", chest_formspec(pos, 1))
|
||||
meta:set_string("inv_name", "none")
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("formspec",
|
||||
"size[9,9.5]"..
|
||||
microexpansion.gui_bg ..
|
||||
microexpansion.gui_slots ..
|
||||
[[
|
||||
label[0,-0.23;ME Drive]
|
||||
list[context;main;0,0.3;8,4]
|
||||
list[current_player;main;0,5.5;8,1;]
|
||||
list[current_player;main;0,6.73;8,3;8]
|
||||
listring[current_name;main]
|
||||
listring[current_player;main]
|
||||
field_close_on_enter[filter;false]
|
||||
]])
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size("cells", 1)
|
||||
inv:set_size("main", 10)
|
||||
end,
|
||||
can_dig = function(pos, player)
|
||||
local meta = minetest.get_meta(pos)
|
||||
@ -82,146 +60,115 @@ microexpansion.register_node("chest", {
|
||||
return inv:is_empty("main")
|
||||
end,
|
||||
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||
if listname == "main" then
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
local max_slots = inv:get_size(listname)
|
||||
local max_items = math.floor(max_slots * 99)
|
||||
|
||||
local slots, items = 0, 0
|
||||
-- Get amount of items in drive
|
||||
for i = 1, max_items do
|
||||
local stack = inv:get_stack("main", i)
|
||||
local item = stack:get_name()
|
||||
if item ~= "" then
|
||||
slots = slots + 1
|
||||
local num = stack:get_count()
|
||||
if num == 0 then num = 1 end
|
||||
items = items + num
|
||||
end
|
||||
end
|
||||
|
||||
return math.min(stack:get_count(),max_items-items)
|
||||
elseif listname == "cells" then
|
||||
if minetest.get_item_group(stack:get_name(), "microexpansion_cell") ~= 0 then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
end
|
||||
if minetest.get_item_group(stack:get_name(), "microexpansion_cell") ~= 0 then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end,
|
||||
on_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||
if listname == "main" then
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
inv:remove_item(listname, stack)
|
||||
local stackname = stack:get_name()
|
||||
local found = false
|
||||
for i = 0, inv:get_size(listname) do
|
||||
local inside = inv:get_stack(listname, i)
|
||||
if inside:get_name() == stackname then
|
||||
inside:set_count(inside:get_count() + stack:get_count())
|
||||
inv:set_stack(listname, i, inside)
|
||||
found = true
|
||||
break;
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
inv:add_item(listname, stack)
|
||||
end
|
||||
microexpansion.cell_desc(inv, "cells", 1)
|
||||
elseif listname == "cells" then
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
local items = minetest.deserialize(stack:get_meta():get_string("items"))
|
||||
local size = me.get_cell_size(stack:get_name())
|
||||
local page_max = me.int_to_pagenum(size) + 1
|
||||
inv:set_size("main", me.int_to_stacks(size))
|
||||
if items then
|
||||
inv:set_list("main", items)
|
||||
end
|
||||
meta:set_string("inv_name", "main")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
||||
end
|
||||
end,
|
||||
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
if listname == "cells" then
|
||||
local t = minetest.get_us_time()
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
local tab = {}
|
||||
local new_stack = inv:get_stack(listname, 1)
|
||||
local item_meta = new_stack:get_meta()
|
||||
for i = 1, inv:get_size("main") do
|
||||
if inv:get_stack("main", i):get_name() ~= "" then
|
||||
tab[#tab + 1] = inv:get_stack("main", i):to_string()
|
||||
end
|
||||
end
|
||||
item_meta:set_string("items", minetest.serialize(tab))
|
||||
inv:set_stack(listname, 1, new_stack)
|
||||
inv:set_size("main", 0)
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("formspec", chest_formspec(pos, 1))
|
||||
return new_stack:get_count()
|
||||
end
|
||||
return math.min(stack:get_count(),stack:get_stack_max())
|
||||
end,
|
||||
on_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
if listname == "search" then
|
||||
inv:remove_item("main", stack)
|
||||
end
|
||||
microexpansion.cell_desc(inv, "cells", 1)
|
||||
end,
|
||||
on_receive_fields = function(pos, formname, fields, sender)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local page = meta:get_int("page")
|
||||
local inv_name = meta:get_string("inv_name")
|
||||
local inv = meta:get_inventory()
|
||||
local page_max = math.floor(inv:get_size("main") / 32) + 1
|
||||
local cell_stack = inv:get_stack("cells", 1)
|
||||
if inv_name == "none" then
|
||||
me.update_connected_machines(pos)
|
||||
local network,cp = me.get_connected_network(pos)
|
||||
if network == nil then
|
||||
return
|
||||
end
|
||||
if fields.next then
|
||||
if page + 32 > inv:get_size(inv_name) then
|
||||
return
|
||||
local ctrl_meta = minetest.get_meta(cp)
|
||||
local ctrl_inv = ctrl_meta:get_inventory()
|
||||
local items = minetest.deserialize(stack:get_meta():get_string("items"))
|
||||
if items == nil then
|
||||
print("no items")
|
||||
me.update_connected_machines(pos)
|
||||
return
|
||||
end
|
||||
for _,stack in pairs(items) do
|
||||
me.insert_item(stack, ctrl_inv, "main")
|
||||
end
|
||||
me.update_connected_machines(pos)
|
||||
end,
|
||||
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
--FIXME sometimes items vanish if one cell is filled
|
||||
local meta = minetest.get_meta(pos)
|
||||
local own_inv = meta:get_inventory()
|
||||
local network,cp = me.get_connected_network(pos)
|
||||
if network == nil then
|
||||
return stack:get_count()
|
||||
end
|
||||
local ctrl_meta = minetest.get_meta(cp)
|
||||
local ctrl_inv = ctrl_meta:get_inventory()
|
||||
local cells = {}
|
||||
for i = 1, own_inv:get_size("main") do
|
||||
local cell = own_inv:get_stack("main", i)
|
||||
local name = cell:get_name()
|
||||
if name ~= "" then
|
||||
table.insert(cells, i, cell)
|
||||
end
|
||||
meta:set_int("page", page + 32)
|
||||
meta:set_string("formspec", chest_formspec(pos, page + 32, inv_name, page_max))
|
||||
elseif fields.prev then
|
||||
if page - 32 < 1 then
|
||||
return
|
||||
end
|
||||
meta:set_int("page", page - 32)
|
||||
meta:set_string("formspec", chest_formspec(pos, page - 32, inv_name, page_max))
|
||||
elseif fields.search or fields.key_enter_field == "filter" then
|
||||
inv:set_size("search", 0)
|
||||
if fields.filter == "" then
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("inv_name", "main")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
||||
else
|
||||
local tab = {}
|
||||
for i = 1, microexpansion.get_cell_size(cell_stack:get_name()) do
|
||||
local match = inv:get_stack("main", i):get_name():find(fields.filter)
|
||||
if match then
|
||||
tab[#tab + 1] = inv:get_stack("main", i)
|
||||
end
|
||||
local cell_idx = next(cells)
|
||||
local items_in_cell_count = 0
|
||||
local cell_items = {}
|
||||
if cell_idx == nil then
|
||||
minetest.log("warning","too many items to store in drive")
|
||||
return stack:get_count()
|
||||
end
|
||||
|
||||
for i = 1, ctrl_inv:get_size("main") do
|
||||
local stack_inside = ctrl_inv:get_stack("main", i)
|
||||
local stack_name = stack_inside:get_name()
|
||||
if stack_name ~= "" then
|
||||
local item_count = stack_inside:get_count()
|
||||
while item_count ~= 0 and cell_idx ~= nil do
|
||||
local size = microexpansion.get_cell_size(cells[cell_idx]:get_name())
|
||||
if size < items_in_cell_count + item_count then
|
||||
local rest = size - items_in_cell_count
|
||||
item_count = item_count - rest
|
||||
table.insert(cell_items,stack_name.." "..rest)
|
||||
items_in_cell_count = items_in_cell_count + rest
|
||||
|
||||
own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count))
|
||||
items_in_cell_count = 0
|
||||
cell_items = {}
|
||||
cell_idx = next(cells, cell_idx)
|
||||
if cell_idx == nil then
|
||||
minetest.log("info","too many items to store in drive")
|
||||
end
|
||||
else
|
||||
items_in_cell_count = items_in_cell_count + item_count
|
||||
table.insert(cell_items,stack_inside:to_string())
|
||||
item_count = 0
|
||||
end
|
||||
end
|
||||
inv:set_list("search", tab)
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("inv_name", "search")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "search", page_max, fields.filter))
|
||||
end
|
||||
elseif fields.clear then
|
||||
inv:set_size("search", 0)
|
||||
meta:set_int("page", 1)
|
||||
meta:set_string("inv_name", "main")
|
||||
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
||||
elseif fields.tochest then
|
||||
local pinv = minetest.get_inventory({type="player", name=sender:get_player_name()})
|
||||
microexpansion.move_inv({ inv=pinv, name="main" }, { inv=inv, name="main" })
|
||||
if cell_idx == nil then
|
||||
break
|
||||
end
|
||||
end
|
||||
while cell_idx ~= nil do
|
||||
own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count))
|
||||
items_in_cell_count = 0
|
||||
cell_items = {}
|
||||
cell_idx = next(cells, cell_idx)
|
||||
end
|
||||
|
||||
return stack:get_count()
|
||||
end,
|
||||
on_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||
local network,cp = me.get_connected_network(pos)
|
||||
if network == nil then
|
||||
return
|
||||
end
|
||||
local ctrl_meta = minetest.get_meta(cp)
|
||||
local ctrl_inv = ctrl_meta:get_inventory()
|
||||
local items = minetest.deserialize(stack:get_meta():get_string("items"))
|
||||
if items == nil then
|
||||
me.update_connected_machines(pos)
|
||||
return
|
||||
end
|
||||
for _,stack in pairs(items) do
|
||||
--this returns 99 (max count) even if it removes more
|
||||
ctrl_inv:remove_item("main", stack)
|
||||
end
|
||||
print(stack:to_string())
|
||||
|
||||
me.update_connected_machines(pos)
|
||||
end,
|
||||
})
|
||||
|
@ -11,5 +11,5 @@ dofile(module_path.."/api.lua")
|
||||
dofile(module_path.."/storage.lua")
|
||||
|
||||
-- Load machines
|
||||
--dofile(module_path.."/drive.lua")
|
||||
dofile(module_path.."/chest.lua")
|
||||
dofile(module_path.."/drive.lua")
|
||||
|
2
settingtypes.txt
Normal file
2
settingtypes.txt
Normal file
@ -0,0 +1,2 @@
|
||||
#Stack items in networks higher than their max size.
|
||||
microexpansion_huge_stacks (huge stacks) bool true
|
Before Width: | Height: | Size: 203 B After Width: | Height: | Size: 203 B |
Loading…
Reference in New Issue
Block a user