techage/basic_machines/distributor.lua

561 lines
16 KiB
Lua
Raw Normal View History

2019-03-02 14:24:48 +03:00
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
GPL v3
2019-03-02 14:24:48 +03:00
See LICENSE.txt for more information
2019-03-11 00:33:03 +03:00
TA2/TA3/TA4 Distributor
2019-03-02 14:24:48 +03:00
]]--
-- for lazy programmers
local M = minetest.get_meta
local N = minetest.get_node
2019-05-21 14:15:13 +03:00
-- Consumer Related Data
local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end
2019-03-02 14:24:48 +03:00
local S = techage.S
2019-03-02 14:24:48 +03:00
2019-03-11 00:33:03 +03:00
local SRC_INV_SIZE = 8
2019-03-02 14:24:48 +03:00
local STANDBY_TICKS = 3
local COUNTDOWN_TICKS = 4
2019-03-11 00:33:03 +03:00
local CYCLE_TIME = 4
2020-02-28 00:33:42 +03:00
local INFO = [[Turn port on/off: command = 'port', payload = red/green/blue/yellow=on/off]]
2019-03-02 14:24:48 +03:00
2019-03-11 00:33:03 +03:00
--local Side2Color = {B="red", L="green", F="blue", R="yellow"}
local SlotColors = {"red", "green", "blue", "yellow"}
local Num2Ascii = {"B", "L", "F", "R"}
local FilterCache = {} -- local cache for filter settings
2019-03-02 14:24:48 +03:00
2019-03-11 00:33:03 +03:00
local function filter_settings(pos)
local meta = M(pos)
local param2 = techage.get_node_lvm(pos).param2
2019-03-11 00:33:03 +03:00
local inv = meta:get_inventory()
local filter = minetest.deserialize(meta:get_string("filter")) or {false,false,false,false}
local ItemFilter = {} -- {<item:name> = {dir,...}]
local OpenPorts = {} -- {dir, ...}
local FilterItems = {} -- {<item:name>, <item:name>, ...} for sequencing only
local FilterItemIdx = {} -- {<item:name> = <slot-idx>, <item:name> = <slot-idx>, ...} for sequencing only
2019-03-11 00:33:03 +03:00
-- collect all filter settings
for idx,slot in ipairs(SlotColors) do
if filter[idx] == true then
local side = Num2Ascii[idx]
local out_dir = techage.side_to_outdir(side, param2)
if inv:is_empty(slot) then
table.insert(OpenPorts, out_dir)
else
for idx2,stack in ipairs(inv:get_list(slot)) do
2019-03-11 00:33:03 +03:00
local name = stack:get_name()
if name ~= "" then
if not ItemFilter[name] then
ItemFilter[name] = {}
end
table.insert(ItemFilter[name], out_dir)
table.insert(FilterItems, name)
FilterItemIdx[name] = idx2
2019-03-11 00:33:03 +03:00
end
end
end
2019-03-02 14:24:48 +03:00
end
end
2019-03-11 00:33:03 +03:00
FilterCache[minetest.hash_node_position(pos)] = {
ItemFilter = ItemFilter,
OpenPorts = OpenPorts,
FilterItems = FilterItems,
FilterItemIdx = FilterItemIdx,
2019-03-11 00:33:03 +03:00
}
2019-03-02 14:24:48 +03:00
end
2019-03-11 00:33:03 +03:00
-- Return filter table and list of open ports.
-- (see test data)
local function get_filter_settings(pos)
-- local ItemFilter = {
-- ["default:dirt"] = {1,2},
-- ["default:cobble"] = {4},
-- }
-- local OpenPorts = {3}
-- local FilterItems = {"default:dirt",...}
-- return ItemFilter, OpenPorts, FilterItems
2019-03-11 00:33:03 +03:00
local hash = minetest.hash_node_position(pos)
if FilterCache[hash] == nil then
filter_settings(pos)
2019-03-02 14:24:48 +03:00
end
return FilterCache[hash].ItemFilter, FilterCache[hash].OpenPorts, FilterCache[hash].FilterItems
2019-03-02 14:24:48 +03:00
end
local function get_slot_index(pos, name)
local hash = minetest.hash_node_position(pos)
if FilterCache[hash] == nil then
filter_settings(pos)
end
return FilterCache[hash].FilterItemIdx[name]
end
2019-09-20 22:43:06 +03:00
local function order_checkbox(pos, filter)
local cnt = 0
if CRD(pos).stage == 2 then return "" end
2019-09-20 22:43:06 +03:00
for _,val in ipairs(filter) do
if val then cnt = cnt + 1 end
end
if cnt == 1 then
local order = M(pos):get_int("order") == 1 and "true" or "false"
return "checkbox[2,0;order;1:1;"..order.."]"..
"tooltip[2,0;1,1;"..S("Force order of filter items")..";#0C3D32;#FFFFFF]"
else
M(pos):set_int("order", 0) -- disable sequencing
end
return ""
end
local function blocking_checkbox(pos, filter)
local cnt = 0
local _, open_ports = get_filter_settings(pos)
for _,val in ipairs(filter) do
if val then cnt = cnt + 1 end
end
if cnt > 1 and #open_ports > 0 then
local blocking = M(pos):get_int("blocking") == 1 and "true" or "false"
return "checkbox[2,0;blocking;>>|;"..blocking.."]"..
"tooltip[2,0;1,1;"..S("Block configured items for open ports")..";#0C3D32;#FFFFFF]"
else
M(pos):set_int("blocking", 0) -- disable blocking
end
return ""
end
2020-01-26 01:15:44 +03:00
local function formspec(self, pos, nvm)
2019-09-20 22:43:06 +03:00
local filter = minetest.deserialize(M(pos):get_string("filter")) or {false,false,false,false}
local order = order_checkbox(pos, filter)
local blocking = blocking_checkbox(pos, filter)
return "size[10.5,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;src;0,0;2,4;]"..
order..
blocking..
"image[2,1.5;1,1;techage_form_arrow.png]"..
2020-01-26 01:15:44 +03:00
"image_button[2,3;1,1;"..self:get_state_button_image(nvm)..";state_button;]"..
"tooltip[2,3;1,1;"..self:get_state_tooltip(nvm).."]"..
2019-09-20 22:43:06 +03:00
"checkbox[3,0;filter1;On;"..dump(filter[1]).."]"..
"checkbox[3,1;filter2;On;"..dump(filter[2]).."]"..
"checkbox[3,2;filter3;On;"..dump(filter[3]).."]"..
"checkbox[3,3;filter4;On;"..dump(filter[4]).."]"..
"image[4,0;0.3,1;techage_inv_red.png]"..
"image[4,1;0.3,1;techage_inv_green.png]"..
"image[4,2;0.3,1;techage_inv_blue.png]"..
"image[4,3;0.3,1;techage_inv_yellow.png]"..
"list[context;red;4.5,0;6,1;]"..
"list[context;green;4.5,1;6,1;]"..
"list[context;blue;4.5,2;6,1;]"..
"list[context;yellow;4.5,3;6,1;]"..
"list[current_player;main;1.25,4.5;8,4;]"..
"listring[context;src]"..
"listring[current_player;main]"..
default.get_hotbar_bg(1.25,4.5)
end
2019-03-02 14:24:48 +03:00
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
2019-05-02 00:01:14 +03:00
local inv = M(pos):get_inventory()
2019-03-02 14:24:48 +03:00
local list = inv:get_list(listname)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "src" then
2019-05-21 14:15:13 +03:00
CRD(pos).State:start_if_standby(pos)
2019-03-02 14:24:48 +03:00
return stack:get_count()
elseif list[index]:get_count() == 0 then
stack:set_count(1)
inv:set_stack(listname, index, stack)
return 0
2019-03-02 14:24:48 +03:00
end
return 0
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "src" then
return stack:get_count()
else
local inv = M(pos):get_inventory()
inv:set_stack(listname, index, nil)
return 0
end
2019-03-02 14:24:48 +03:00
end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
local inv = minetest.get_meta(pos):get_inventory()
2019-03-02 14:24:48 +03:00
local stack = inv:get_stack(from_list, from_index)
if from_list == "src" and to_list ~= "src" and not inv:contains_item(to_list, {name = stack:get_name()}) then
stack:set_count(1)
inv:set_stack(to_list, to_index, stack)
return 0
elseif from_list ~= "src" and to_list == "src" then
inv:set_stack(from_list, from_index, nil)
return 0
elseif not inv:contains_item(to_list, {name = stack:get_name()}) then
return 1
else
return 0
end
end
2019-03-02 14:24:48 +03:00
2020-01-26 01:15:44 +03:00
local function push_item(pos, filter, item_name, num_items, nvm)
2019-03-11 00:33:03 +03:00
local idx = 1
local num_pushed = 0
local num_ports = #filter
local amount = math.floor(math.max((num_items + 1) / num_ports, 1))
2019-09-03 23:08:11 +03:00
local num_of_trials = 0
while num_pushed < num_items and num_of_trials <= 8 do
num_of_trials = num_of_trials + 1
2019-03-11 00:33:03 +03:00
local push_dir = filter[idx]
local num_to_push = math.min(amount, num_items - num_pushed)
if techage.push_items(pos, push_dir, ItemStack(item_name.." "..num_to_push)) then
num_pushed = num_pushed + num_to_push
2020-01-26 01:15:44 +03:00
nvm.port_counter[push_dir] = (nvm.port_counter[push_dir] or 0) + num_to_push
2019-03-11 00:33:03 +03:00
end
2019-09-03 23:08:11 +03:00
-- filter start offset
2019-03-11 00:33:03 +03:00
idx = idx + 1
if idx > num_ports then
idx = 1
2019-03-02 14:24:48 +03:00
end
end
2019-03-11 00:33:03 +03:00
return num_pushed
2019-03-02 14:24:48 +03:00
end
2019-03-11 00:33:03 +03:00
-- move items to output slots
2020-01-26 01:15:44 +03:00
local function distributing(pos, inv, crd, nvm)
2019-03-11 00:33:03 +03:00
local item_filter, open_ports = get_filter_settings(pos)
local sum_num_pushed = 0
local num_pushed = 0
2019-09-20 22:43:06 +03:00
local blocking_mode = M(pos):get_int("blocking") == 1
2019-03-02 14:24:48 +03:00
2019-03-11 00:33:03 +03:00
-- start searching after last position
2020-01-26 01:15:44 +03:00
local offs = nvm.last_index or 1
2019-03-02 14:24:48 +03:00
2019-03-11 00:33:03 +03:00
for i = 1, SRC_INV_SIZE do
local idx = ((i + offs - 1) % 8) + 1
local stack = inv:get_stack("src", idx)
local item_name = stack:get_name()
local num_items = stack:get_count()
2019-05-21 14:15:13 +03:00
local num_to_push = math.min(crd.num_items - sum_num_pushed, num_items)
2019-03-11 00:33:03 +03:00
num_pushed = 0
if item_filter[item_name] then
-- Push items based on filter
2020-01-26 01:15:44 +03:00
num_pushed = push_item(pos, item_filter[item_name], item_name, num_to_push, nvm)
2019-09-20 22:43:06 +03:00
elseif blocking_mode and #open_ports > 0 then
-- Push items based on open ports
2020-01-26 01:15:44 +03:00
num_pushed = push_item(pos, open_ports, item_name, num_to_push, nvm)
2019-03-02 14:24:48 +03:00
end
2019-09-20 22:43:06 +03:00
if not blocking_mode and num_pushed == 0 and #open_ports > 0 then
2019-03-11 00:33:03 +03:00
-- Push items based on open ports
2020-01-26 01:15:44 +03:00
num_pushed = push_item(pos, open_ports, item_name, num_to_push, nvm)
2019-03-02 14:24:48 +03:00
end
2019-03-11 00:33:03 +03:00
sum_num_pushed = sum_num_pushed + num_pushed
stack:take_item(num_pushed)
inv:set_stack("src", idx, stack)
2019-05-21 14:15:13 +03:00
if sum_num_pushed >= crd.num_items then
2020-01-26 01:15:44 +03:00
nvm.last_index = idx
2019-03-11 00:33:03 +03:00
break
2019-03-02 14:24:48 +03:00
end
end
2019-03-11 00:33:03 +03:00
if num_pushed == 0 then
2020-01-26 01:15:44 +03:00
crd.State:blocked(pos, nvm)
2019-03-02 14:24:48 +03:00
else
2020-01-26 01:15:44 +03:00
crd.State:keep_running(pos, nvm, COUNTDOWN_TICKS, sum_num_pushed)
2019-03-02 14:24:48 +03:00
end
end
2020-01-26 01:15:44 +03:00
local function sequencing(pos, inv, crd, nvm)
local _,open_ports, filter_items = get_filter_settings(pos)
2020-01-26 01:15:44 +03:00
local offs = nvm.last_index or 1
local num_filters = 0 -- already processed
local num_pushed = 0
local push_dir = open_ports[1] or 1
local blocked = false
while num_pushed < crd.num_items and num_filters < 7 do
local stack = filter_items[offs] and ItemStack(filter_items[offs])
if stack then
if not inv:contains_item("src", stack) then
break
end
if not techage.push_items(pos, push_dir, stack, offs) then
blocked = true
break
end
num_pushed = num_pushed + 1
inv:remove_item("src", stack)
end
offs = (offs % 6) + 1
num_filters = num_filters + 1
end
2020-01-26 01:15:44 +03:00
nvm.last_index = offs
if blocked then
2020-01-26 01:15:44 +03:00
crd.State:blocked(pos, nvm)
elseif num_pushed == 0 then
2020-01-26 01:15:44 +03:00
crd.State:standby(pos, nvm)
else
2020-01-26 01:15:44 +03:00
crd.State:keep_running(pos, nvm, COUNTDOWN_TICKS, num_pushed)
end
end
2019-03-02 14:24:48 +03:00
-- move items to the output slots
local function keep_running(pos, elapsed)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
nvm.port_counter = nvm.port_counter or {}
2019-05-21 14:15:13 +03:00
local crd = CRD(pos)
2019-03-11 00:33:03 +03:00
local inv = M(pos):get_inventory()
if not inv:is_empty("src") then
if M(pos):get_int("order") == 1 then
2020-01-26 01:15:44 +03:00
sequencing(pos, inv, crd, nvm)
else
2020-01-26 01:15:44 +03:00
distributing(pos, inv, crd, nvm)
end
2019-03-11 00:33:03 +03:00
else
2020-01-26 01:15:44 +03:00
crd.State:idle(pos, nvm)
2019-03-11 00:33:03 +03:00
end
2020-01-26 01:15:44 +03:00
return crd.State:is_active(nvm)
2019-03-02 14:24:48 +03:00
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local meta = M(pos)
2019-05-21 14:15:13 +03:00
local crd = CRD(pos)
2019-03-02 14:24:48 +03:00
local filter = minetest.deserialize(meta:get_string("filter"))
if fields.filter1 ~= nil then
filter[1] = fields.filter1 == "true"
elseif fields.filter2 ~= nil then
filter[2] = fields.filter2 == "true"
elseif fields.filter3 ~= nil then
filter[3] = fields.filter3 == "true"
elseif fields.filter4 ~= nil then
filter[4] = fields.filter4 == "true"
elseif fields.order ~= nil and CRD(pos).stage > 2 then
meta:set_int("order", fields.order == "true" and 1 or 0)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
nvm.last_index = 1 -- start from the beginning
2019-09-20 22:43:06 +03:00
elseif fields.blocking ~= nil then
meta:set_int("blocking", fields.blocking == "true" and 1 or 0)
2019-03-02 14:24:48 +03:00
end
meta:set_string("filter", minetest.serialize(filter))
filter_settings(pos)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
2019-03-02 14:24:48 +03:00
if fields.state_button ~= nil then
2020-01-26 01:15:44 +03:00
crd.State:state_button_event(pos, nvm, fields)
2019-03-02 14:24:48 +03:00
else
2020-01-26 01:15:44 +03:00
meta:set_string("formspec", formspec(crd.State, pos, nvm))
2019-03-02 14:24:48 +03:00
end
end
2019-03-11 00:33:03 +03:00
-- techage command to turn on/off filter channels
2019-03-02 14:24:48 +03:00
local function change_filter_settings(pos, slot, val)
local slots = {["red"] = 1, ["green"] = 2, ["blue"] = 3, ["yellow"] = 4}
local meta = M(pos)
local filter = minetest.deserialize(meta:get_string("filter"))
local num = slots[slot] or 1
if num >= 1 and num <= 4 then
filter[num] = val == "on"
end
meta:set_string("filter", minetest.serialize(filter))
filter_settings(pos)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
meta:set_string("formspec", formspec(CRD(pos).State, pos, nvm))
2019-03-02 14:24:48 +03:00
return true
end
2019-03-11 00:33:03 +03:00
local function can_dig(pos, player)
if minetest.is_protected(pos, player:get_player_name()) then
return false
end
local inv = M(pos):get_inventory()
return inv:is_empty("src")
end
2019-03-02 14:24:48 +03:00
2019-03-11 00:33:03 +03:00
local tiles = {}
-- '#' will be replaced by the stage number
-- '{power}' will be replaced by the power PNG
tiles.pas = {
-- up, down, right, left, back, front
"techage_filling_ta#.png^techage_appl_distri.png^techage_frame_ta#_top.png^techage_appl_color_top.png",
2019-03-11 00:33:03 +03:00
"techage_filling_ta#.png^techage_frame_ta#.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_yellow.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_green.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_red.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_blue.png",
}
tiles.act = {
-- up, down, right, left, back, front
{
image = "techage_filling4_ta#.png^techage_appl_distri4.png^techage_frame4_ta#_top.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1.0,
2019-03-02 14:24:48 +03:00
},
},
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_color_top.png",
2019-03-11 00:33:03 +03:00
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_yellow.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_green.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_red.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_distri_blue.png",
}
local function put_items(pos, item)
local order = M(pos):get_int("order") == 1
local inv = M(pos):get_inventory()
if order then
local name = item:get_name()
local idx = get_slot_index(pos, name)
if idx then
local stack = inv:get_stack("src", idx)
if stack:item_fits(item) then
stack:add_item(item)
inv:set_stack("src", idx, stack)
return true
end
end
return false
else
return techage.put_items(inv, "src", item)
end
end
2019-03-11 00:33:03 +03:00
local tubing = {
on_pull_item = function(pos, in_dir, num)
2019-03-02 14:24:48 +03:00
local inv = M(pos):get_inventory()
return techage.get_items(pos, inv, "src", num)
2019-03-02 14:24:48 +03:00
end,
2019-03-11 00:33:03 +03:00
on_push_item = function(pos, in_dir, stack)
2019-06-16 22:06:16 +03:00
CRD(pos).State:start_if_standby(pos)
return put_items(pos, stack)
2019-03-02 14:24:48 +03:00
end,
2019-03-11 00:33:03 +03:00
on_unpull_item = function(pos, in_dir, stack)
return put_items(pos, stack)
2019-03-02 14:24:48 +03:00
end,
on_recv_message = function(pos, src, topic, payload)
if topic == "info" then
return INFO
elseif topic == "port" then
-- "red"/"green"/"blue"/"yellow" = "on"/"off"
local slot, val = techage.ident_value(payload)
return change_filter_settings(pos, slot, val)
2019-03-02 14:24:48 +03:00
else
2020-02-28 00:33:42 +03:00
return CRD(pos).State:on_receive_message(pos, topic, payload)
2019-03-02 14:24:48 +03:00
end
end,
on_node_load = function(pos)
2019-05-21 14:15:13 +03:00
CRD(pos).State:on_node_load(pos)
2019-03-02 14:24:48 +03:00
end,
2019-03-11 00:33:03 +03:00
}
local node_name_ta2, node_name_ta3, node_name_ta4 =
techage.register_consumer("distributor", S("Distributor"), tiles, {
2019-03-11 00:33:03 +03:00
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
formspec = formspec,
tubing = tubing,
after_place_node = function(pos, placer)
local meta = M(pos)
local filter = {false,false,false,false}
meta:set_string("filter", minetest.serialize(filter))
local inv = meta:get_inventory()
inv:set_size('src', 8)
inv:set_size('yellow', 6)
inv:set_size('green', 6)
inv:set_size('red', 6)
inv:set_size('blue', 6)
end,
can_dig = can_dig,
node_timer = keep_running,
on_receive_fields = on_receive_fields,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list)
if from_list ~= "src" or to_list ~= "src" then
filter_settings(pos)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(CRD(pos).State, pos, nvm))
2019-03-11 00:33:03 +03:00
end
end,
on_metadata_inventory_put = function(pos, listname)
if listname ~= "src" then
filter_settings(pos)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(CRD(pos).State, pos, nvm))
2019-03-11 00:33:03 +03:00
end
end,
on_metadata_inventory_take = function(pos, listname)
if listname ~= "src" then
filter_settings(pos)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(CRD(pos).State, pos, nvm))
2019-03-11 00:33:03 +03:00
end
end,
groups = {choppy=2, cracky=2, crumbly=2},
sounds = default.node_sound_wood_defaults(),
num_items = {0,4,12,36},
})
minetest.register_craft({
2019-04-28 17:04:45 +03:00
output = node_name_ta2.." 2",
2019-03-11 00:33:03 +03:00
recipe = {
2019-04-28 17:04:45 +03:00
{"group:wood", "techage:iron_ingot", "group:wood"},
{"techage:tubeS", "default:mese_crystal", "techage:tubeS"},
{"group:wood", "techage:iron_ingot", "group:wood"},
2019-03-11 00:33:03 +03:00
},
})
2019-05-11 02:21:03 +03:00
minetest.register_craft({
output = node_name_ta3,
recipe = {
{"", "techage:iron_ingot", ""},
{"", node_name_ta2, ""},
{"", "techage:vacuum_tube", ""},
},
})
minetest.register_craft({
output = node_name_ta4,
recipe = {
{"", "techage:iron_ingot", ""},
{"", node_name_ta3, ""},
{"", "techage:ta4_wlanchip", ""},
},
})