microexpansion/modules/storage/terminal.lua

275 lines
8.2 KiB
Lua
Raw Normal View History

2023-12-31 00:52:43 +01:00
-- terminal
-- microexpansion/machines.lua
local me = microexpansion
2020-03-16 09:59:18 +01:00
local pipeworks_enabled = minetest.get_modpath("pipeworks") and true or false
-- [me chest] Get formspec
2019-05-18 07:37:46 +02:00
local function chest_formspec(pos, start_id, listname, page_max, q)
2023-12-31 00:52:43 +01:00
local list
local page_number = ""
local buttons = ""
local query = q or ""
local net,cp = me.get_connected_network(pos)
2017-02-23 23:31:02 +01:00
2023-12-31 00:52:43 +01:00
if cp then
if listname and net:get_item_capacity() > 0 then
local ctrlinvname = net:get_inventory_name()
if listname == "main" then
list = "list[detached:"..ctrlinvname..";"
.. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
else
list = "list[context;" .. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
end
if minetest.get_modpath("i3") then
list = list .. [[
list[current_player;main;0,5.5;9,4;]
]]
else
list = list .. [[
list[current_player;main;0,5.5;8,1;]
list[current_player;main;0,6.73;8,3;8]
]]
end
list = list .. [[
listring[detached:]]..ctrlinvname..[[;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 storage!") .. "]"
end
else
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 ..
"/" .. page_max .."]"
end
2017-02-23 23:31:02 +01:00
2024-01-11 01:10:48 +01:00
if net and not net:powered() then
list = "label[3,2;" .. minetest.colorize("red", "No power!") .. "]"
buttons = ""
page_number = ""
end
2023-12-31 00:52:43 +01:00
return [[
size[9,9.5]
]]..
microexpansion.gui_bg ..
microexpansion.gui_slots ..
list ..
[[
label[0,-0.23;ME Terminal]
field_close_on_enter[filter;false]
]]..
page_number ..
buttons
end
local function update_chest(pos,_,ev)
--for now all events matter
2019-05-18 07:37:46 +02:00
2023-12-31 00:52:43 +01:00
local network = me.get_connected_network(pos)
local meta = minetest.get_meta(pos)
if network == nil then
meta:set_int("page", 1)
meta:set_string("formspec", chest_formspec(pos, 1))
return
end
local size = network:get_item_capacity()
local page_max = me.int_to_pagenum(size) + 1
meta:set_string("inv_name", "main")
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
end
-- [me chest] Register node
2023-12-31 00:52:43 +01:00
me.register_node("term", {
description = "ME Terminal",
usedfor = "Can interact with storage cells in ME networks",
tiles = {
"chest_top",
"chest_top",
"chest_side",
"chest_side",
"chest_side",
"chest_front",
},
recipe = {
2020-03-04 16:46:22 +01:00
{ 1, {
2023-12-31 00:52:43 +01:00
{"default:steel_ingot", "default:chest", "default:steel_ingot"},
{"default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot"},
{"default:steel_ingot", "microexpansion:cable", "default:steel_ingot"},
2020-03-04 16:46:22 +01:00
},
}
},
2023-12-31 00:52:43 +01:00
is_ground_content = false,
groups = { cracky = 1, me_connect = 1, tubedevice = 1, tubedevice_receiver = 1 },
paramtype = "light",
paramtype2 = "facedir",
me_update = update_chest,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("inv_name", "none")
meta:set_int("page", 1)
2020-05-29 16:03:02 +02:00
2023-12-31 00:52:43 +01:00
local own_inv = meta:get_inventory()
own_inv:set_size("src", 1)
2020-05-29 16:03:02 +02:00
2023-12-31 00:52:43 +01:00
local net = me.get_connected_network(pos)
2024-01-11 01:10:48 +01:00
me.send_event(pos, "connect", {net=net})
update_chest(pos)
2023-12-31 00:52:43 +01:00
end,
after_destruct = function(pos)
2024-01-11 01:10:48 +01:00
me.send_event(pos, "disconnect")
2023-12-31 00:52:43 +01:00
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
-- TODO: Check capacity, only allow if room
return stack:get_count()
2020-03-16 09:59:18 +01:00
end,
on_metadata_inventory_put = function(pos, listname, _, stack)
local net = me.get_connected_network(pos)
local inv = net:get_inventory()
2023-12-31 00:52:43 +01:00
me.insert_item(stack, net, inv, "main")
net:set_storage_space(true)
end,
2023-12-31 00:52:43 +01:00
on_metadata_inventory_take = function(pos, listname, _, stack)
local net = me.get_connected_network(pos)
local inv = net:get_inventory()
me.remove_item(net, inv, "main", stack)
end,
tube = {
2020-03-16 09:59:18 +01:00
can_insert = function(pos, _, stack) --pos, node, stack, direction
local net = me.get_connected_network(pos)
local inv = net:get_inventory()
local max_slots = inv:get_size("main")
local max_items = net.capacity_cache
local slots, items = 0, 0
-- Get amount of items in drive
for i = 1, max_slots do
2023-12-31 00:52:43 +01:00
local dstack = inv:get_stack("main", i)
if dstack:get_name() ~= "" then
slots = slots + 1
local num = dstack:get_count()
if num == 0 then num = 1 end
items = items + num
end
2020-03-16 09:59:18 +01:00
end
items = items + stack:get_count()
return max_items > items
end,
insert_object = function(pos, _, stack)
local net = me.get_connected_network(pos)
local inv = net:get_inventory()
2023-12-31 00:52:43 +01:00
local leftovers = me.insert_item(stack, net, inv, "main")
2020-03-16 09:59:18 +01:00
net:set_storage_space(true)
2023-12-31 00:52:43 +01:00
return leftovers
2020-03-16 09:59:18 +01:00
end,
connect_sides = {left=1, right=1, front=1, back=1, top=1, bottom=1},
},
after_place_node = pipeworks_enabled and pipeworks.after_place,
after_dig_node = pipeworks_enabled and pipeworks.after_dig,
2023-12-31 00:52:43 +01:00
on_receive_fields = function(pos, _, fields, sender)
local net,cp = me.get_connected_network(pos)
if net then
if cp then
me.log("network and ctrl_pos","info")
else
me.log("network but no ctrl_pos","warning")
end
else
if cp then
me.log("no network but ctrl_pos","warning")
else
me.log("no network and no ctrl_pos","info")
end
end
local meta = minetest.get_meta(pos)
local page = meta:get_int("page")
local inv_name = meta:get_string("inv_name")
local own_inv = meta:get_inventory()
local ctrl_inv
if cp then
ctrl_inv = net:get_inventory()
else
me.log("no network connected","warning")
return
end
local inv
if inv_name == "main" then
inv = ctrl_inv
assert(inv,"no control inv")
else
inv = own_inv
assert(inv,"no own inv")
end
local page_max = math.floor(inv:get_size(inv_name) / 32) + 1
if inv_name == "none" then
return
end
if fields.next then
if page + 32 > inv:get_size(inv_name) then
return
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
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, 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] = ctrl_inv:get_stack("main", i)
end
end
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
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()})
-- TODO: test and fix, net:set_storage_space(pinv:get_size("main"))
local space = net:get_item_capacity()
2020-03-03 17:16:35 +01:00
local contents = ctrl_inv:get_list("main") or {}
for _,s in pairs(contents) do
2023-12-31 00:52:43 +01:00
if not s:is_empty() then
space = space - s:get_count()
end
2020-03-03 17:16:35 +01:00
end
2023-12-31 00:52:43 +01:00
me.move_inv(net, { inv=pinv, name="main" }, { inv=ctrl_inv, name="main", huge=true }, space)
net:set_storage_space(true)
end
end,
})