2017-02-09 16:37:14 +01:00
|
|
|
-- microexpansion/machines.lua
|
|
|
|
|
2017-02-23 16:35:42 +01:00
|
|
|
local me = microexpansion
|
|
|
|
|
2019-05-07 17:49:26 +02:00
|
|
|
local function update_drive(pos)
|
|
|
|
--FIXME: check if we got connected/disconnected and reroute items
|
|
|
|
end
|
2017-02-23 23:31:02 +01:00
|
|
|
|
2019-05-07 17:49:26 +02:00
|
|
|
local function write_to_cell(cell, items, item_count)
|
|
|
|
local size = microexpansion.get_cell_size(cell:get_name())
|
|
|
|
local item_meta = cell:get_meta()
|
2020-03-01 08:23:39 +01:00
|
|
|
--print(dump2(items,"cell_items"))
|
2019-05-07 17:49:26 +02:00
|
|
|
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
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [me chest] Register node
|
2019-05-07 17:49:26 +02:00
|
|
|
microexpansion.register_node("drive", {
|
|
|
|
description = "ME Drive",
|
|
|
|
usedfor = "Stores items into ME storage cells",
|
2017-02-09 16:37:14 +01:00
|
|
|
tiles = {
|
2017-02-24 17:28:36 +01:00
|
|
|
"chest_top",
|
|
|
|
"chest_top",
|
|
|
|
"chest_side",
|
|
|
|
"chest_side",
|
|
|
|
"chest_side",
|
2019-05-07 17:49:26 +02:00
|
|
|
"drive_full",
|
2017-02-09 16:37:14 +01:00
|
|
|
},
|
|
|
|
is_ground_content = false,
|
2019-05-07 17:49:26 +02:00
|
|
|
groups = { cracky = 1, me_connect = 1 },
|
2017-02-09 16:37:14 +01:00
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
2019-05-07 17:49:26 +02:00
|
|
|
me_update = update_drive,
|
2017-02-09 16:37:14 +01:00
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2019-05-07 17:49:26 +02:00
|
|
|
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]
|
|
|
|
]])
|
2017-02-09 16:37:14 +01:00
|
|
|
local inv = meta:get_inventory()
|
2019-05-07 17:49:26 +02:00
|
|
|
inv:set_size("main", 10)
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
can_dig = function(pos)
|
2017-02-09 16:37:14 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return inv:is_empty("main")
|
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
allow_metadata_inventory_put = function(_, _, _, stack)
|
2019-05-07 17:49:26 +02:00
|
|
|
if minetest.get_item_group(stack:get_name(), "microexpansion_cell") ~= 0 then
|
|
|
|
return 1
|
2017-02-09 16:37:14 +01:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_metadata_inventory_put = function(pos, _, _, stack)
|
2019-05-07 17:49:26 +02:00
|
|
|
me.update_connected_machines(pos)
|
2020-03-01 08:23:39 +01:00
|
|
|
local network = me.get_connected_network(pos)
|
2019-05-07 17:49:26 +02:00
|
|
|
if network == nil then
|
|
|
|
return
|
|
|
|
end
|
2020-03-01 08:23:39 +01:00
|
|
|
local ctrl_inv = network:get_inventory()
|
2019-05-07 17:49:26 +02:00
|
|
|
local items = minetest.deserialize(stack:get_meta():get_string("items"))
|
|
|
|
if items == nil then
|
|
|
|
print("no items")
|
|
|
|
me.update_connected_machines(pos)
|
|
|
|
return
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
2020-03-01 08:23:39 +01:00
|
|
|
network:add_storage_slots(#items)
|
2020-02-27 15:33:45 +01:00
|
|
|
for _,s in pairs(items) do
|
|
|
|
me.insert_item(s, ctrl_inv, "main")
|
2019-05-07 17:49:26 +02:00
|
|
|
end
|
|
|
|
me.update_connected_machines(pos)
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
2020-02-27 15:33:45 +01:00
|
|
|
allow_metadata_inventory_take = function(pos,_,_,stack) --args: pos, listname, index, stack, player
|
2019-05-07 17:49:26 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local own_inv = meta:get_inventory()
|
2020-03-01 08:23:39 +01:00
|
|
|
local network = me.get_connected_network(pos)
|
2019-05-07 17:49:26 +02:00
|
|
|
if network == nil then
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
2020-03-01 08:23:39 +01:00
|
|
|
local ctrl_inv = network:get_inventory()
|
2019-05-07 17:49:26 +02:00
|
|
|
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
|
2020-03-01 08:23:39 +01:00
|
|
|
cells[i] = cell
|
2019-05-07 17:49:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local cell_idx = next(cells)
|
2020-03-01 08:23:39 +01:00
|
|
|
local size = microexpansion.get_cell_size(cells[cell_idx]:get_name())
|
2019-05-07 17:49:26 +02:00
|
|
|
local items_in_cell_count = 0
|
|
|
|
local cell_items = {}
|
2020-03-01 08:23:39 +01:00
|
|
|
assert(cell_idx,"cannot take a cell from an empty drive")
|
2019-05-07 17:49:26 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
if size < items_in_cell_count + item_count then
|
2020-03-01 08:23:39 +01:00
|
|
|
local space = size - items_in_cell_count
|
|
|
|
item_count = item_count - space
|
|
|
|
table.insert(cell_items,stack_name.." "..space)
|
|
|
|
items_in_cell_count = items_in_cell_count + space
|
2019-05-07 17:49:26 +02:00
|
|
|
|
|
|
|
own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count))
|
|
|
|
cell_idx = next(cells, cell_idx)
|
2020-03-01 08:23:39 +01:00
|
|
|
size = microexpansion.get_cell_size(cells[cell_idx]:get_name())
|
|
|
|
items_in_cell_count = 0
|
|
|
|
cell_items = {}
|
|
|
|
if cell_idx == nil then
|
|
|
|
--there may be other drives within the network
|
2019-05-07 17:49:26 +02:00
|
|
|
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
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
end
|
2019-05-07 17:49:26 +02:00
|
|
|
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)
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
2019-05-07 17:49:26 +02:00
|
|
|
|
|
|
|
return stack:get_count()
|
2019-05-01 16:52:12 +02:00
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_metadata_inventory_take = function(pos, _, _, stack)
|
2020-03-01 08:23:39 +01:00
|
|
|
local network = me.get_connected_network(pos)
|
2019-05-07 17:49:26 +02:00
|
|
|
if network == nil then
|
|
|
|
return
|
2019-05-01 16:52:12 +02:00
|
|
|
end
|
2020-03-01 08:23:39 +01:00
|
|
|
local ctrl_inv = network:get_inventory()
|
2019-05-07 17:49:26 +02:00
|
|
|
local items = minetest.deserialize(stack:get_meta():get_string("items"))
|
|
|
|
if items == nil then
|
2020-03-01 08:23:39 +01:00
|
|
|
network:update()
|
2019-05-07 17:49:26 +02:00
|
|
|
me.update_connected_machines(pos)
|
2017-02-09 16:37:14 +01:00
|
|
|
return
|
|
|
|
end
|
2019-05-18 07:37:46 +02:00
|
|
|
for _,ostack in pairs(items) do
|
2019-05-07 17:49:26 +02:00
|
|
|
--this returns 99 (max count) even if it removes more
|
2019-05-18 07:37:46 +02:00
|
|
|
ctrl_inv:remove_item("main", ostack)
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
2019-05-07 17:49:26 +02:00
|
|
|
print(stack:to_string())
|
|
|
|
|
2020-03-01 08:23:39 +01:00
|
|
|
network:update()
|
2019-05-07 17:49:26 +02:00
|
|
|
me.update_connected_machines(pos)
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
|
|
|
})
|