2017-02-09 16:37:14 +01:00
|
|
|
-- microexpansion/machines.lua
|
|
|
|
|
2017-02-23 16:35:42 +01:00
|
|
|
local me = microexpansion
|
|
|
|
|
2017-02-09 16:37:14 +01:00
|
|
|
-- [me chest] Get formspec
|
2019-05-18 07:37:46 +02:00
|
|
|
local function chest_formspec(pos, start_id, listname, page_max, q)
|
2017-02-09 16:37:14 +01:00
|
|
|
local list
|
|
|
|
local page_number = ""
|
2017-05-03 20:28:51 +02:00
|
|
|
local buttons = ""
|
2019-05-18 07:37:46 +02:00
|
|
|
local query = q or ""
|
2019-05-07 17:49:26 +02:00
|
|
|
local net,cp = me.get_connected_network(pos)
|
2017-02-23 23:31:02 +01:00
|
|
|
|
2019-05-07 17:49:26 +02:00
|
|
|
if cp then
|
|
|
|
if listname and net:get_item_capacity() > 0 then
|
2020-03-01 08:23:39 +01:00
|
|
|
local ctrlinvname = net:get_inventory_name()
|
2019-05-07 17:49:26 +02:00
|
|
|
if listname == "main" then
|
2020-03-01 08:23:39 +01:00
|
|
|
list = "list[detached:"..ctrlinvname..";"
|
|
|
|
.. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]"
|
2019-05-07 17:49:26 +02:00
|
|
|
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]
|
2020-03-01 08:23:39 +01:00
|
|
|
listring[detached:]]..ctrlinvname..[[;main]
|
2019-05-07 17:49:26 +02:00
|
|
|
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
|
2017-02-09 16:37:14 +01:00
|
|
|
else
|
2019-05-07 17:49:26 +02:00
|
|
|
list = "label[3,2;" .. minetest.colorize("red", "No connected network!") .. "]"
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
if page_max then
|
2017-05-03 20:28:51 +02:00
|
|
|
page_number = "label[6.15,4.5;" .. math.floor((start_id / 32)) + 1 ..
|
2017-02-09 16:37:14 +01:00
|
|
|
"/" .. page_max .."]"
|
|
|
|
end
|
2017-02-23 23:31:02 +01:00
|
|
|
|
2017-02-23 16:35:42 +01:00
|
|
|
return [[
|
2017-02-23 23:31:02 +01:00
|
|
|
size[9,9.5]
|
2017-02-23 16:35:42 +01:00
|
|
|
]]..
|
|
|
|
microexpansion.gui_bg ..
|
|
|
|
microexpansion.gui_slots ..
|
|
|
|
list ..
|
|
|
|
[[
|
2019-05-18 07:16:11 +02:00
|
|
|
label[0,-0.23;ME Terminal]
|
2017-02-23 16:35:42 +01:00
|
|
|
field_close_on_enter[filter;false]
|
|
|
|
]]..
|
2017-02-23 23:31:02 +01:00
|
|
|
page_number ..
|
2017-05-03 20:28:51 +02:00
|
|
|
buttons
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
|
2019-05-04 13:53:49 +02:00
|
|
|
local function update_chest(pos)
|
|
|
|
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
|
2019-05-07 17:49:26 +02:00
|
|
|
local size = network:get_item_capacity()
|
2019-05-04 13:53:49 +02:00
|
|
|
local page_max = me.int_to_pagenum(size) + 1
|
2019-05-18 07:37:46 +02:00
|
|
|
|
2019-05-04 13:53:49 +02:00
|
|
|
meta:set_string("inv_name", "main")
|
|
|
|
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
|
|
|
end
|
|
|
|
|
2020-03-01 08:23:39 +01:00
|
|
|
--FIXME: items inserted in a search inventory vanish
|
|
|
|
--TODO: add a main inv that transfers to the network
|
|
|
|
|
2017-02-09 16:37:14 +01:00
|
|
|
-- [me chest] Register node
|
2019-05-18 07:16:11 +02:00
|
|
|
microexpansion.register_node("term", {
|
|
|
|
description = "ME Terminal",
|
2019-05-04 13:53:49 +02:00
|
|
|
usedfor = "Can interact with storage cells in ME networks",
|
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",
|
|
|
|
"chest_front",
|
2017-02-09 16:37:14 +01:00
|
|
|
},
|
|
|
|
is_ground_content = false,
|
2019-05-04 13:53:49 +02:00
|
|
|
groups = { cracky = 1, me_connect = 1 },
|
2017-02-09 16:37:14 +01:00
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
2019-05-04 13:53:49 +02:00
|
|
|
me_update = update_chest,
|
2017-02-09 16:37:14 +01:00
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2017-02-23 23:31:02 +01:00
|
|
|
meta:set_string("formspec", chest_formspec(pos, 1))
|
2017-02-09 16:37:14 +01:00
|
|
|
meta:set_string("inv_name", "none")
|
|
|
|
meta:set_int("page", 1)
|
2019-05-04 13:53:49 +02:00
|
|
|
local net = me.get_connected_network(pos)
|
|
|
|
if net then
|
|
|
|
update_chest(pos)
|
|
|
|
end
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_metadata_inventory_take = function(pos, listname, _, stack)
|
2019-05-01 16:52:12 +02:00
|
|
|
if listname == "search" then
|
2020-03-01 08:23:39 +01:00
|
|
|
local net = me.get_connected_network(pos)
|
|
|
|
local inv = net:get_inventory()
|
2019-05-01 16:52:12 +02:00
|
|
|
inv:remove_item("main", stack)
|
|
|
|
end
|
2017-02-09 16:37:14 +01:00
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_receive_fields = function(pos, _, fields, sender)
|
2020-03-01 08:23:39 +01:00
|
|
|
local net,cp = me.get_connected_network(pos)
|
|
|
|
if net then
|
|
|
|
if cp then
|
|
|
|
minetest.log("none","network and ctrl_pos")
|
|
|
|
else
|
|
|
|
minetest.log("warning","network but no ctrl_pos")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if cp then
|
|
|
|
minetest.log("warning","no network but ctrl_pos")
|
|
|
|
else
|
|
|
|
minetest.log("info","no network and no ctrl_pos")
|
|
|
|
end
|
|
|
|
end
|
2017-02-09 16:37:14 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local page = meta:get_int("page")
|
|
|
|
local inv_name = meta:get_string("inv_name")
|
2019-05-07 17:49:26 +02:00
|
|
|
local own_inv = meta:get_inventory()
|
|
|
|
local ctrl_inv
|
|
|
|
if cp then
|
2020-03-01 08:23:39 +01:00
|
|
|
ctrl_inv = net:get_inventory()
|
|
|
|
else
|
|
|
|
minetest.log("warning","no network connected")
|
|
|
|
return
|
2019-05-07 17:49:26 +02:00
|
|
|
end
|
|
|
|
local inv
|
|
|
|
if inv_name == "main" then
|
|
|
|
inv = ctrl_inv
|
2020-03-01 08:23:39 +01:00
|
|
|
assert(inv,"no control inv")
|
2019-05-07 17:49:26 +02:00
|
|
|
else
|
|
|
|
inv = own_inv
|
2020-03-01 08:23:39 +01:00
|
|
|
assert(inv,"no own inv")
|
2019-05-07 17:49:26 +02:00
|
|
|
end
|
|
|
|
local page_max = math.floor(inv:get_size(inv_name) / 32) + 1
|
2017-02-09 16:37:14 +01:00
|
|
|
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)
|
2017-02-23 23:31:02 +01:00
|
|
|
meta:set_string("formspec", chest_formspec(pos, page + 32, inv_name, page_max))
|
2017-02-09 16:37:14 +01:00
|
|
|
elseif fields.prev then
|
|
|
|
if page - 32 < 1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
meta:set_int("page", page - 32)
|
2017-02-23 23:31:02 +01:00
|
|
|
meta:set_string("formspec", chest_formspec(pos, page - 32, inv_name, page_max))
|
2017-02-09 16:37:14 +01:00
|
|
|
elseif fields.search or fields.key_enter_field == "filter" then
|
2019-05-07 17:49:26 +02:00
|
|
|
own_inv:set_size("search", 0)
|
2017-02-09 16:37:14 +01:00
|
|
|
if fields.filter == "" then
|
|
|
|
meta:set_int("page", 1)
|
|
|
|
meta:set_string("inv_name", "main")
|
2017-02-23 23:31:02 +01:00
|
|
|
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
|
2017-02-09 16:37:14 +01:00
|
|
|
else
|
|
|
|
local tab = {}
|
2019-05-07 17:49:26 +02:00
|
|
|
for i = 1, ctrl_inv:get_size("main") do
|
|
|
|
local match = ctrl_inv:get_stack("main", i):get_name():find(fields.filter)
|
2017-02-09 16:37:14 +01:00
|
|
|
if match then
|
2019-05-07 17:49:26 +02:00
|
|
|
tab[#tab + 1] = ctrl_inv:get_stack("main", i)
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
end
|
2019-05-07 17:49:26 +02:00
|
|
|
own_inv:set_list("search", tab)
|
2017-02-09 16:37:14 +01:00
|
|
|
meta:set_int("page", 1)
|
|
|
|
meta:set_string("inv_name", "search")
|
2017-02-23 23:31:02 +01:00
|
|
|
meta:set_string("formspec", chest_formspec(pos, 1, "search", page_max, fields.filter))
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
elseif fields.clear then
|
2019-05-07 17:49:26 +02:00
|
|
|
own_inv:set_size("search", 0)
|
2017-02-09 16:37:14 +01:00
|
|
|
meta:set_int("page", 1)
|
|
|
|
meta:set_string("inv_name", "main")
|
2017-02-23 23:31:02 +01:00
|
|
|
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()})
|
2020-03-03 17:16:35 +01:00
|
|
|
net:set_storage_space(pinv:get_size("main"))
|
|
|
|
local space = net:get_item_capacity()
|
|
|
|
local contents = ctrl_inv:get_list("main") or {}
|
|
|
|
for _,s in pairs(contents) do
|
|
|
|
if not s:is_empty() then
|
|
|
|
space = space - s:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
microexpansion.move_inv({ inv=pinv, name="main" }, { inv=ctrl_inv, name="main",huge=true }, space)
|
|
|
|
net:set_storage_space(true)
|
2017-02-09 16:37:14 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|