2017-02-09 16:37:14 +01:00
|
|
|
-- microexpansion/machines.lua
|
|
|
|
|
2017-02-23 16:35:42 +01:00
|
|
|
local me = microexpansion
|
2023-10-14 21:48:43 +02:00
|
|
|
local access_level = microexpansion.constants.security.access_levels
|
2017-02-23 16:35:42 +01:00
|
|
|
|
2020-03-07 18:02:14 +01:00
|
|
|
local netdrives
|
|
|
|
|
|
|
|
-- load drives
|
|
|
|
local function load_drives()
|
|
|
|
local f = io.open(me.worldpath.."/microexpansion_drives", "r")
|
|
|
|
if f then
|
|
|
|
netdrives = minetest.deserialize(f:read("*all")) or {}
|
|
|
|
f:close()
|
2020-03-16 09:59:18 +01:00
|
|
|
--[[
|
2020-03-07 18:02:14 +01:00
|
|
|
if type(res) == "table" then
|
|
|
|
for _,d in pairs(res) do
|
|
|
|
table.insert(netdrives,d)
|
|
|
|
end
|
|
|
|
end
|
2020-03-16 09:59:18 +01:00
|
|
|
]]
|
2020-06-17 09:30:47 +02:00
|
|
|
else
|
|
|
|
netdrives = {}
|
2020-03-07 18:02:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- load now
|
|
|
|
load_drives()
|
|
|
|
|
|
|
|
-- save drives
|
|
|
|
local function save_drives()
|
|
|
|
local f = io.open(me.worldpath.."/microexpansion_drives", "w")
|
|
|
|
f:write(minetest.serialize(netdrives))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- save on server shutdown
|
|
|
|
minetest.register_on_shutdown(save_drives)
|
|
|
|
|
|
|
|
local function get_drive_controller(pos)
|
|
|
|
for i,d in pairs(netdrives) do
|
|
|
|
if d.dpos then
|
|
|
|
if vector.equals(pos, d.dpos) then
|
|
|
|
return d,i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return --false,#netdrives+1
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_drive_controller(dpos,setd,cpos,i)
|
|
|
|
if i then
|
|
|
|
local dt = netdrives[i]
|
|
|
|
if dt then
|
|
|
|
if setd then
|
|
|
|
dt.dpos = dpos
|
|
|
|
end
|
|
|
|
if cpos ~= nil then
|
|
|
|
dt.cpos = cpos
|
|
|
|
end
|
|
|
|
else
|
|
|
|
netdrives[i] = {dpos = dpos, cpos = cpos}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local dt = get_drive_controller(dpos)
|
|
|
|
if dt then
|
|
|
|
if setd then
|
|
|
|
dt.dpos = dpos
|
|
|
|
end
|
|
|
|
if cpos ~= nil then
|
|
|
|
dt.cpos = cpos
|
|
|
|
end
|
|
|
|
else
|
|
|
|
table.insert(netdrives,{dpos = dpos, cpos = cpos})
|
|
|
|
end
|
|
|
|
end
|
2019-05-07 17:49:26 +02:00
|
|
|
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
|
|
|
|
|
2020-10-04 11:48:05 +02:00
|
|
|
local function write_drive_cells(pos,network)
|
2020-03-07 18:02:14 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local own_inv = meta:get_inventory()
|
|
|
|
if network == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local ctrl_inv = network: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
|
|
|
|
cells[i] = cell
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local cell_idx = next(cells)
|
|
|
|
if cell_idx == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local size = microexpansion.get_cell_size(cells[cell_idx]:get_name())
|
|
|
|
local items_in_cell_count = 0
|
|
|
|
local cell_items = {}
|
|
|
|
|
|
|
|
for i = 1, ctrl_inv:get_size("main") do
|
|
|
|
local stack_inside = ctrl_inv:get_stack("main", i)
|
2020-10-04 11:48:05 +02:00
|
|
|
local item_string = stack_inside:to_string()
|
|
|
|
if item_string ~= "" then
|
|
|
|
item_string = item_string:split(" ")
|
2020-03-07 18:02:14 +01:00
|
|
|
local item_count = stack_inside:get_count()
|
2020-10-04 11:48:05 +02:00
|
|
|
if item_count > 1 and item_string[2] ~= tostring(item_count) then
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("stack count differs from second field of the item string","warning")
|
2020-10-04 11:48:05 +02:00
|
|
|
end
|
2020-03-07 18:02:14 +01:00
|
|
|
while item_count ~= 0 and cell_idx ~= nil do
|
2020-10-04 11:48:05 +02:00
|
|
|
--print(("stack to store: %q"):format(table.concat(item_string," ")))
|
2020-03-07 18:02:14 +01:00
|
|
|
if size < items_in_cell_count + item_count then
|
|
|
|
local space = size - items_in_cell_count
|
2020-10-04 11:48:05 +02:00
|
|
|
item_string[2] = tostring(space)
|
|
|
|
table.insert(cell_items,table.concat(item_string," "))
|
2020-03-07 18:02:14 +01:00
|
|
|
items_in_cell_count = items_in_cell_count + space
|
|
|
|
|
|
|
|
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)
|
|
|
|
if cell_idx == nil then
|
|
|
|
--there may be other drives within the network
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("too many items to store in drive","info")
|
2020-03-07 18:02:14 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
size = microexpansion.get_cell_size(cells[cell_idx]:get_name())
|
|
|
|
items_in_cell_count = 0
|
|
|
|
cell_items = {}
|
|
|
|
item_count = item_count - space
|
|
|
|
else
|
|
|
|
items_in_cell_count = items_in_cell_count + item_count
|
2020-10-04 11:48:05 +02:00
|
|
|
item_string[2] = tostring(item_count)
|
|
|
|
table.insert(cell_items,table.concat(item_string," "))
|
2020-03-07 18:02:14 +01:00
|
|
|
item_count = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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 true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function take_all(pos,net)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local own_inv = meta:get_inventory()
|
|
|
|
local ctrl_inv = net:get_inventory()
|
|
|
|
local items = {}
|
|
|
|
for i = 1, own_inv:get_size("main") do
|
|
|
|
local stack = own_inv:get_stack("main", i)
|
|
|
|
local name = stack:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
local its = minetest.deserialize(stack:get_meta():get_string("items"))
|
|
|
|
for _,s in pairs(its) do
|
|
|
|
table.insert(items,s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _,ostack in pairs(items) do
|
|
|
|
--this returns 99 (max count) even if it removes more
|
|
|
|
ctrl_inv:remove_item("main", ostack)
|
|
|
|
print(ostack)
|
|
|
|
end
|
|
|
|
|
|
|
|
net:update()
|
|
|
|
me.send_event(pos,"items")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function add_all(pos,net)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local own_inv = meta:get_inventory()
|
|
|
|
local ctrl_inv = net:get_inventory()
|
|
|
|
local items = {}
|
|
|
|
for i = 1, own_inv:get_size("main") do
|
|
|
|
local stack = own_inv:get_stack("main", i)
|
|
|
|
local name = stack:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
local its = minetest.deserialize(stack:get_meta():get_string("items"))
|
|
|
|
if its then
|
|
|
|
for _,s in pairs(its) do
|
|
|
|
table.insert(items,s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _,ostack in pairs(items) do
|
|
|
|
me.insert_item(ostack, ctrl_inv, "main")
|
|
|
|
print(ostack)
|
|
|
|
end
|
|
|
|
|
|
|
|
net:update()
|
|
|
|
me.send_event(pos,"items",{net = net})
|
|
|
|
end
|
|
|
|
|
|
|
|
function me.disconnect_drive(pos,ncpos)
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("disconnecting drive at "..minetest.pos_to_string(pos),"action")
|
2020-03-07 18:02:14 +01:00
|
|
|
local fc,i = get_drive_controller(pos)
|
|
|
|
if not fc.cpos then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local fnet = me.get_network(fc.cpos)
|
|
|
|
write_drive_cells(pos,fnet)
|
|
|
|
if ncpos then
|
|
|
|
set_drive_controller(pos,false,ncpos,i)
|
|
|
|
else
|
|
|
|
set_drive_controller(pos,false,false,i)
|
|
|
|
end
|
|
|
|
if fnet then
|
|
|
|
take_all(pos,fnet)
|
|
|
|
else
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("drive couldn't take items from its former network","warning")
|
2020-03-07 18:02:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_drive(pos,_,ev)
|
|
|
|
if ev.type~="connect" and ev.type~="disconnect" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local fc,i = get_drive_controller(pos)
|
|
|
|
local cnet = ev.net or me.get_connected_network(pos)
|
|
|
|
if cnet then
|
|
|
|
if not fc then
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("connecting drive at "..minetest.pos_to_string(pos),"action")
|
2020-03-07 18:02:14 +01:00
|
|
|
set_drive_controller(pos,true,cnet.controller_pos,i)
|
|
|
|
add_all(pos,cnet)
|
|
|
|
elseif not fc.cpos then
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("connecting drive at "..minetest.pos_to_string(pos),"action")
|
2020-03-07 18:02:14 +01:00
|
|
|
set_drive_controller(pos,false,cnet.controller_pos,i)
|
|
|
|
add_all(pos,cnet)
|
|
|
|
elseif not vector.equals(fc.cpos,cnet.controller_pos) then
|
2020-10-04 12:12:19 +02:00
|
|
|
microexpansion.log("reconnecting drive at "..minetest.pos_to_string(pos),"action")
|
2020-03-07 18:02:14 +01:00
|
|
|
write_drive_cells(pos,me.get_network(fc.cpos))
|
|
|
|
set_drive_controller(pos,false,cnet.controller_pos,i)
|
|
|
|
add_all(pos,cnet)
|
|
|
|
me.disconnect_drive(pos,cnet.controller_pos)
|
|
|
|
else
|
|
|
|
if ev.origin.name == "microexpansion:ctrl" then
|
|
|
|
me.disconnect_drive(pos,false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if fc then
|
|
|
|
if fc.cpos then
|
|
|
|
me.disconnect_drive(pos,false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-09 16:37:14 +01:00
|
|
|
-- [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
|
|
|
},
|
2020-03-04 16:46:22 +01:00
|
|
|
recipe = {
|
|
|
|
{ 1, {
|
|
|
|
{"default:steel_ingot", "default:chest", "default:steel_ingot" },
|
|
|
|
{"default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" },
|
|
|
|
{"default:steel_ingot", "default:chest", "default:steel_ingot" },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
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)
|
2020-03-07 18:02:14 +01:00
|
|
|
me.send_event(pos,"connect")
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
2023-09-02 13:29:10 +02:00
|
|
|
can_dig = function(pos, player)
|
2023-10-14 21:48:43 +02:00
|
|
|
if not player then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if minetest.is_protected(pos, name) then
|
|
|
|
minetest.record_protection_violation(pos, name)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local net,cp = me.get_connected_network(pos)
|
|
|
|
if not net then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if net:get_access_level(name) < access_level.modify then
|
|
|
|
return false
|
|
|
|
end
|
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,
|
2020-03-07 18:02:14 +01:00
|
|
|
after_destruct = function(pos)
|
|
|
|
me.send_event(pos,"disconnect")
|
|
|
|
end,
|
2023-10-14 21:48:43 +02:00
|
|
|
allow_metadata_inventory_put = function(pos, _, _, stack, player)
|
|
|
|
local network = me.get_connected_network(pos)
|
|
|
|
if network then
|
|
|
|
if network:get_access_level(player) < access_level.interact then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif minetest.is_protected(pos, player) then
|
|
|
|
minetest.record_protection_violation(pos, player)
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if minetest.get_item_group(stack:get_name(), "microexpansion_cell") == 0 then
|
2017-02-09 16:37:14 +01:00
|
|
|
return 0
|
2023-09-02 13:29:10 +02:00
|
|
|
else
|
|
|
|
return 1
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_metadata_inventory_put = function(pos, _, _, stack)
|
2020-03-07 18:02:14 +01:00
|
|
|
me.send_event(pos,"item_cap")
|
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")
|
2020-03-07 18:02:14 +01:00
|
|
|
me.send_event(pos,"items",{net=network})
|
2019-05-07 17:49:26 +02:00
|
|
|
return
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
2020-03-03 17:16:35 +01:00
|
|
|
network:set_storage_space(#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
|
2020-03-07 18:02:14 +01:00
|
|
|
me.send_event(pos,"items",{net=network})
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
2023-09-02 13:29:10 +02:00
|
|
|
allow_metadata_inventory_take = function(pos,_,_,stack, player) --args: pos, listname, index, stack, player
|
2023-10-14 21:48:43 +02:00
|
|
|
local network = me.get_connected_network(pos)
|
|
|
|
if network then
|
|
|
|
write_drive_cells(pos,network)
|
|
|
|
if network:get_access_level(player) < access_level.interact then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif minetest.is_protected(pos, player) then
|
|
|
|
minetest.record_protection_violation(pos, player)
|
|
|
|
return 0
|
|
|
|
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-07 18:02:14 +01:00
|
|
|
me.send_event(pos,"item_cap",{net=network})
|
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()
|
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
|
2020-03-03 17:16:35 +01:00
|
|
|
--print(stack:to_string())
|
2019-05-07 17:49:26 +02:00
|
|
|
|
2020-03-01 08:23:39 +01:00
|
|
|
network:update()
|
2020-03-07 18:02:14 +01:00
|
|
|
me.send_event(pos,"items",{net=network})
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
|
|
|
})
|