2018-08-08 01:06:16 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Tubelib Addons 3
|
|
|
|
================
|
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
Copyright (C) 2017-2020 Joachim Stolberg
|
2018-08-08 01:06:16 +02:00
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
AGPL v3
|
2018-08-08 01:06:16 +02:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
distributor.lua:
|
|
|
|
|
|
|
|
A high performance distributor
|
|
|
|
]]--
|
|
|
|
|
2020-11-03 20:19:38 +01:00
|
|
|
-- Load support for I18n
|
|
|
|
local S = tubelib_addons3.S
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
-- for lazy programmers
|
|
|
|
local P = minetest.string_to_pos
|
|
|
|
local M = minetest.get_meta
|
|
|
|
|
2018-08-08 01:06:16 +02:00
|
|
|
local NUM_FILTER_ELEM = 6
|
|
|
|
local NUM_FILTER_SLOTS = 4
|
2018-12-23 19:36:11 +01:00
|
|
|
|
2019-01-13 12:37:44 +01:00
|
|
|
local COUNTDOWN_TICKS = 8
|
2018-12-23 19:36:11 +01:00
|
|
|
local STANDBY_TICKS = 4
|
2018-08-08 01:06:16 +02:00
|
|
|
local CYCLE_TIME = 2
|
|
|
|
|
2019-01-05 18:04:56 +01:00
|
|
|
local function formspec(self, pos, meta)
|
2019-01-06 15:03:22 +01:00
|
|
|
local filter = minetest.deserialize(meta:get_string("filter")) or {false,false,false,false}
|
2018-12-23 19:36:11 +01:00
|
|
|
return "size[10.5,8.5]"..
|
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
|
|
|
"list[context;src;0,0;2,4;]"..
|
|
|
|
"image[2,1.5;1,1;tubelib_gui_arrow.png]"..
|
2019-01-05 18:04:56 +01:00
|
|
|
"image_button[2,3;1,1;"..self:get_state_button_image(meta)..";state_button;]"..
|
2018-12-23 19:36:11 +01: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;tubelib_red.png]"..
|
|
|
|
"image[4,1;0.3,1;tubelib_green.png]"..
|
|
|
|
"image[4,2;0.3,1;tubelib_blue.png]"..
|
|
|
|
"image[4,3;0.3,1;tubelib_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]"
|
|
|
|
end
|
|
|
|
|
2019-01-05 18:04:56 +01:00
|
|
|
local State = tubelib.NodeStates:new({
|
|
|
|
node_name_passive = "tubelib_addons3:distributor",
|
|
|
|
node_name_active = "tubelib_addons3:distributor_active",
|
|
|
|
node_name_defect = "tubelib_addons3:distributor_defect",
|
2020-11-03 20:19:38 +01:00
|
|
|
infotext_name = S("HighPerf Distributor"),
|
2019-01-05 18:04:56 +01:00
|
|
|
cycle_time = CYCLE_TIME,
|
|
|
|
standby_ticks = STANDBY_TICKS,
|
|
|
|
aging_factor = 50,
|
|
|
|
formspec_func = formspec,
|
|
|
|
})
|
2018-08-20 20:53:48 +02:00
|
|
|
|
2018-08-08 01:06:16 +02:00
|
|
|
-- Return the total number of list entries
|
|
|
|
local function invlist_num_entries(list)
|
|
|
|
local res = 0
|
|
|
|
for _,items in ipairs(list) do
|
|
|
|
local name = items:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
res = res + items:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return a flat table with all items
|
|
|
|
local function invlist_entries_as_list(list)
|
|
|
|
local res = {}
|
|
|
|
for _,items in ipairs(list) do
|
|
|
|
local name = items:get_name()
|
|
|
|
local count = items:get_count()
|
|
|
|
if name ~= "" then
|
|
|
|
for i = 1,count do
|
|
|
|
res[#res+1] = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2018-08-19 21:30:04 +02:00
|
|
|
local function AddToTbl(kvTbl, new_items, val)
|
2018-08-08 01:06:16 +02:00
|
|
|
for _, l in ipairs(new_items) do
|
2018-08-20 20:53:48 +02:00
|
|
|
if kvTbl[l] == nil then
|
|
|
|
kvTbl[l] = {val}
|
|
|
|
else
|
|
|
|
kvTbl[l][#kvTbl[l] + 1] = val
|
|
|
|
end
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
|
|
|
return kvTbl
|
|
|
|
end
|
|
|
|
|
2018-08-20 20:53:48 +02:00
|
|
|
local function random_list_elem(list)
|
|
|
|
if list == nil then
|
|
|
|
return nil
|
|
|
|
elseif #list > 1 then
|
|
|
|
return list[math.random(1, #list)]
|
|
|
|
else
|
|
|
|
return list[1]
|
|
|
|
end
|
|
|
|
end
|
2018-08-08 01:06:16 +02:00
|
|
|
|
2020-04-27 19:42:38 +02:00
|
|
|
local function rearrange_table(t)
|
|
|
|
if #t > 1 then
|
|
|
|
local elem = table.remove(t, 1)
|
|
|
|
table.insert(t, elem)
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2018-12-29 21:02:23 +01: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
|
|
|
|
|
|
|
|
local function filter_settings(pos)
|
|
|
|
local hash = minetest.hash_node_position(pos)
|
|
|
|
local meta = M(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local filter = minetest.deserialize(meta:get_string("filter")) or {false,false,false,false}
|
|
|
|
local kvFilterItemNames = {} -- {<item:name> = side,...}
|
|
|
|
local OpenPorts = {} -- {side, ...}
|
|
|
|
|
|
|
|
-- collect all filter settings
|
|
|
|
for idx,slot in ipairs(SlotColors) do
|
|
|
|
local side = Num2Ascii[idx]
|
|
|
|
if filter[idx] == true then
|
|
|
|
local list = inv:get_list(slot)
|
|
|
|
local filter = invlist_entries_as_list(list)
|
|
|
|
AddToTbl(kvFilterItemNames, filter, side)
|
|
|
|
if not next(filter) then
|
|
|
|
OpenPorts[#OpenPorts + 1] = side
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FilterCache[hash] = {
|
|
|
|
kvFilterItemNames = kvFilterItemNames,
|
|
|
|
OpenPorts = OpenPorts,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-08 01:06:16 +02:00
|
|
|
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
|
2019-08-08 15:59:17 +02:00
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
local meta = M(pos)
|
2018-08-08 01:06:16 +02:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local list = inv:get_list(listname)
|
2019-08-08 15:59:17 +02:00
|
|
|
local stack_count = stack:get_count()
|
2018-08-08 01:06:16 +02:00
|
|
|
|
|
|
|
if listname == "src" then
|
2018-12-23 19:36:11 +01:00
|
|
|
if State:get_state(M(pos)) == tubelib.STANDBY then
|
|
|
|
State:start(pos, meta)
|
|
|
|
end
|
2019-08-08 15:59:17 +02:00
|
|
|
return stack_count
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
2019-08-08 15:59:17 +02:00
|
|
|
|
|
|
|
local space_left = NUM_FILTER_ELEM - invlist_num_entries(list)
|
|
|
|
if space_left <= 0 then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
filter_settings(pos)
|
|
|
|
return 1
|
2018-08-08 01:06:16 +02:00
|
|
|
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
|
2019-08-08 15:59:17 +02:00
|
|
|
if listname ~= "src" then
|
|
|
|
filter_settings(pos)
|
|
|
|
end
|
2018-08-08 01:06:16 +02:00
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
|
2018-12-25 22:06:05 +01:00
|
|
|
local meta = M(pos)
|
2018-08-08 01:06:16 +02:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local stack = inv:get_stack(from_list, from_index)
|
|
|
|
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- move items to the output slots
|
2018-12-23 19:36:11 +01:00
|
|
|
local function distributing(pos, meta)
|
2018-08-08 01:06:16 +02:00
|
|
|
local player_name = meta:get_string("player_name")
|
|
|
|
local counter = minetest.deserialize(meta:get_string("item_counter")) or
|
|
|
|
{red=0, green=0, blue=0, yellow=0}
|
|
|
|
|
|
|
|
-- calculate the filter settings only once
|
|
|
|
local hash = minetest.hash_node_position(pos)
|
|
|
|
if FilterCache[hash] == nil then
|
|
|
|
filter_settings(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- read data from Cache
|
|
|
|
local kvFilterItemNames = FilterCache[hash].kvFilterItemNames
|
2018-08-20 20:53:48 +02:00
|
|
|
local open_ports = table.copy(FilterCache[hash].OpenPorts)
|
2018-08-08 01:06:16 +02:00
|
|
|
|
2018-08-19 21:30:04 +02:00
|
|
|
-- no filter configured?
|
2020-04-27 19:42:38 +02:00
|
|
|
if not next(kvFilterItemNames) and not next(open_ports) then
|
|
|
|
State:idle(pos, meta)
|
|
|
|
return
|
|
|
|
end
|
2018-08-08 01:06:16 +02:00
|
|
|
|
2019-01-01 18:53:17 +01:00
|
|
|
local busy = false
|
2018-08-19 21:30:04 +02:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local list = inv:get_list("src")
|
2018-08-20 20:53:48 +02:00
|
|
|
|
|
|
|
-- take one stack from inventory, which fits to one output port
|
|
|
|
local stack
|
|
|
|
local start_idx = math.random(1, 8)
|
|
|
|
for i = start_idx,start_idx+8 do
|
|
|
|
stack = list[(i % 8) + 1]
|
|
|
|
if stack:get_count() > 0 and (kvFilterItemNames[stack:get_name()] or next(open_ports)) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if stack:get_count() > 0 then
|
|
|
|
local name = stack:get_name()
|
2018-09-15 19:22:10 +02:00
|
|
|
local num = stack:get_count()
|
2018-08-20 20:53:48 +02:00
|
|
|
local second_try = false
|
|
|
|
-- try configured output ports
|
2020-04-27 19:42:38 +02:00
|
|
|
for _, side in ipairs(kvFilterItemNames[name] or {}) do -- configured
|
2018-08-20 20:53:48 +02:00
|
|
|
if tubelib.push_items(pos, side, stack, player_name) then
|
|
|
|
stack:set_count(0)
|
|
|
|
local color = Side2Color[side]
|
2018-09-15 19:22:10 +02:00
|
|
|
counter[color] = counter[color] + num
|
2020-04-27 19:42:38 +02:00
|
|
|
rearrange_table(kvFilterItemNames[name])
|
2019-01-01 18:53:17 +01:00
|
|
|
busy = true
|
2020-04-27 19:42:38 +02:00
|
|
|
break
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
2018-08-20 20:53:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- try unconfigured open output ports
|
2020-04-27 19:42:38 +02:00
|
|
|
if not busy then
|
|
|
|
local side = random_list_elem(open_ports)
|
2018-08-20 20:53:48 +02:00
|
|
|
if side then
|
|
|
|
if tubelib.push_items(pos, side, stack, player_name) then
|
|
|
|
stack:set_count(0)
|
|
|
|
local color = Side2Color[side]
|
2018-09-15 19:22:10 +02:00
|
|
|
counter[color] = counter[color] + num
|
2019-01-01 18:53:17 +01:00
|
|
|
busy = true
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-19 21:30:04 +02:00
|
|
|
inv:set_list("src", list)
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
meta:set_string("item_counter", minetest.serialize(counter))
|
2019-01-01 18:53:17 +01:00
|
|
|
if busy then
|
|
|
|
State:keep_running(pos, meta, COUNTDOWN_TICKS, 1)
|
2018-12-25 22:06:05 +01:00
|
|
|
else
|
|
|
|
State:idle(pos, meta)
|
|
|
|
end
|
2018-12-23 19:36:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- move items to the output slots
|
|
|
|
local function keep_running(pos, elapsed)
|
2019-04-23 21:46:08 +02:00
|
|
|
if tubelib.data_not_corrupted(pos) then
|
|
|
|
local meta = M(pos)
|
|
|
|
distributing(pos, meta)
|
|
|
|
return State:is_active(meta)
|
|
|
|
end
|
|
|
|
return false
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
2018-12-23 19:36:11 +01:00
|
|
|
local meta = M(pos)
|
2018-08-08 01:06:16 +02: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"
|
|
|
|
end
|
|
|
|
meta:set_string("filter", minetest.serialize(filter))
|
|
|
|
|
|
|
|
filter_settings(pos)
|
|
|
|
|
2019-01-15 21:01:24 +01:00
|
|
|
if fields.state_button ~= nil then
|
|
|
|
State:state_button_event(pos, fields)
|
|
|
|
else
|
|
|
|
meta:set_string("formspec", formspec(State, pos, meta))
|
|
|
|
end
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- tubelib command to turn on/off filter channels
|
|
|
|
local function change_filter_settings(pos, slot, val)
|
|
|
|
local slots = {["red"] = 1, ["green"] = 2, ["blue"] = 3, ["yellow"] = 4}
|
2018-12-23 19:36:11 +01:00
|
|
|
local meta = M(pos)
|
2018-08-08 01:06:16 +02:00
|
|
|
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)
|
|
|
|
|
2019-01-15 21:01:24 +01:00
|
|
|
meta:set_string("formspec", formspec(State, pos, meta))
|
2018-08-08 01:06:16 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_node("tubelib_addons3:distributor", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("HighPerf Distributor"),
|
2018-08-08 01:06:16 +02:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
'tubelib_distributor.png^tubelib_addons3_node_frame.png',
|
2019-01-01 20:14:43 +01:00
|
|
|
'tubelib_addons3_chest_bottom.png',
|
2018-08-08 01:06:16 +02:00
|
|
|
'tubelib_distributor_yellow.png^tubelib_addons3_node_frame.png',
|
|
|
|
'tubelib_distributor_green.png^tubelib_addons3_node_frame.png',
|
|
|
|
"tubelib_distributor_red.png^tubelib_addons3_node_frame.png",
|
|
|
|
"tubelib_distributor_blue.png^tubelib_addons3_node_frame.png",
|
|
|
|
},
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer)
|
2018-12-23 19:36:11 +01:00
|
|
|
local meta = M(pos)
|
2018-08-08 01:06:16 +02:00
|
|
|
local number = tubelib.add_node(pos, "tubelib_addons3:distributor")
|
|
|
|
local filter = {false,false,false,false}
|
|
|
|
meta:set_string("filter", minetest.serialize(filter))
|
2018-12-23 19:36:11 +01:00
|
|
|
State:node_init(pos, number)
|
|
|
|
meta:set_string("player_name", placer:get_player_name())
|
|
|
|
|
2018-08-08 01:06:16 +02:00
|
|
|
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)
|
|
|
|
meta:set_string("item_counter", minetest.serialize({red=0, green=0, blue=0, yellow=0}))
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
|
2019-01-06 15:03:22 +01:00
|
|
|
can_dig = function(pos, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return false
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
2019-01-06 15:03:22 +01:00
|
|
|
local inv = M(pos):get_inventory()
|
|
|
|
return inv:is_empty("src")
|
2018-08-08 01:06:16 +02:00
|
|
|
end,
|
2020-08-12 20:15:26 +02:00
|
|
|
|
|
|
|
on_dig = function(pos, node, player)
|
|
|
|
State:on_dig_node(pos, node, player)
|
2019-01-06 15:03:22 +01:00
|
|
|
tubelib.remove_node(pos)
|
2018-12-23 19:36:11 +01:00
|
|
|
end,
|
|
|
|
|
2018-08-08 01:06:16 +02:00
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
|
|
|
|
on_timer = keep_running,
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {choppy=2, cracky=2, crumbly=2},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
minetest.register_node("tubelib_addons3:distributor_active", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("HighPerf Distributor"),
|
2018-08-08 01:06:16 +02:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
{
|
|
|
|
image = "tubelib_addons3_distributor_active.png",
|
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
|
|
|
length = 2.0,
|
|
|
|
},
|
|
|
|
},
|
2019-01-01 20:14:43 +01:00
|
|
|
'tubelib_addons3_chest_bottom.png',
|
2018-08-08 01:06:16 +02:00
|
|
|
'tubelib_distributor_yellow.png^tubelib_addons3_node_frame.png',
|
|
|
|
'tubelib_distributor_green.png^tubelib_addons3_node_frame.png',
|
|
|
|
"tubelib_distributor_red.png^tubelib_addons3_node_frame.png",
|
|
|
|
"tubelib_distributor_blue.png^tubelib_addons3_node_frame.png",
|
|
|
|
},
|
|
|
|
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
|
|
|
|
on_timer = keep_running,
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {crumbly=0, not_in_creative_inventory=1},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
minetest.register_node("tubelib_addons3:distributor_defect", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("HighPerf Distributor"),
|
2018-12-23 19:36:11 +01:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
'tubelib_distributor.png^tubelib_addons3_node_frame.png',
|
2019-01-01 20:14:43 +01:00
|
|
|
'tubelib_addons3_chest_bottom.png',
|
2018-12-23 19:36:11 +01:00
|
|
|
'tubelib_distributor_yellow.png^tubelib_addons3_node_frame.png^tubelib_defect.png',
|
|
|
|
'tubelib_distributor_green.png^tubelib_addons3_node_frame.png^tubelib_defect.png',
|
|
|
|
"tubelib_distributor_red.png^tubelib_addons3_node_frame.png^tubelib_defect.png",
|
|
|
|
"tubelib_distributor_blue.png^tubelib_addons3_node_frame.png^tubelib_defect.png",
|
|
|
|
},
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
local meta = M(pos)
|
|
|
|
local number = tubelib.add_node(pos, "tubelib_addons3:distributor")
|
2019-01-06 15:03:22 +01:00
|
|
|
local filter = {false,false,false,false}
|
|
|
|
meta:set_string("filter", minetest.serialize(filter))
|
2018-12-23 19:36:11 +01:00
|
|
|
State:node_init(pos, number)
|
|
|
|
meta:set_string("player_name", placer:get_player_name())
|
|
|
|
|
|
|
|
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)
|
|
|
|
meta:set_string("item_counter", minetest.serialize({red=0, green=0, blue=0, yellow=0}))
|
|
|
|
State:defect(pos, meta)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
|
2019-01-06 15:03:22 +01:00
|
|
|
can_dig = function(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,
|
|
|
|
|
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
|
|
|
tubelib.remove_node(pos)
|
|
|
|
end,
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
|
|
|
|
on_rotate = screwdriver.disallow,
|
2018-12-29 21:02:23 +01:00
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-08 01:06:16 +02:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "tubelib_addons3:distributor",
|
|
|
|
recipe = {
|
2018-08-08 11:37:27 +02:00
|
|
|
{"default:tin_ingot", "tubelib:distributor", ""},
|
|
|
|
{"tubelib:distributor", "default:gold_ingot", ""},
|
|
|
|
{"", "", ""},
|
2018-08-08 01:06:16 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
tubelib.register_node("tubelib_addons3:distributor",
|
|
|
|
{"tubelib_addons3:distributor_active", "tubelib_addons3:distributor_defect"}, {
|
2018-08-08 01:06:16 +02:00
|
|
|
on_pull_item = function(pos, side)
|
2018-12-23 19:36:11 +01:00
|
|
|
return tubelib.get_item(M(pos), "src")
|
2018-08-08 01:06:16 +02:00
|
|
|
end,
|
|
|
|
on_push_item = function(pos, side, item)
|
2018-12-23 19:36:11 +01:00
|
|
|
return tubelib.put_item(M(pos), "src", item)
|
2018-08-08 01:06:16 +02:00
|
|
|
end,
|
|
|
|
on_unpull_item = function(pos, side, item)
|
2018-12-23 19:36:11 +01:00
|
|
|
return tubelib.put_item(M(pos), "src", item)
|
2018-08-08 01:06:16 +02:00
|
|
|
end,
|
|
|
|
on_recv_message = function(pos, topic, payload)
|
2018-12-23 19:36:11 +01:00
|
|
|
if topic == "filter" then
|
2018-08-08 01:06:16 +02:00
|
|
|
return change_filter_settings(pos, payload.slot, payload.val)
|
|
|
|
elseif topic == "counter" then
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return minetest.deserialize(meta:get_string("item_counter")) or
|
|
|
|
{red=0, green=0, blue=0, yellow=0}
|
|
|
|
elseif topic == "clear_counter" then
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_string("item_counter", minetest.serialize({red=0, green=0, blue=0, yellow=0}))
|
2018-12-23 19:36:11 +01:00
|
|
|
else
|
|
|
|
local resp = State:on_receive_message(pos, topic, payload)
|
|
|
|
if resp then
|
|
|
|
return resp
|
|
|
|
else
|
|
|
|
return "unsupported"
|
|
|
|
end
|
2018-08-08 01:06:16 +02:00
|
|
|
end
|
|
|
|
end,
|
2018-12-23 19:36:11 +01:00
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
on_node_load = function(pos)
|
2018-12-23 19:36:11 +01:00
|
|
|
State:on_node_load(pos)
|
|
|
|
end,
|
|
|
|
on_node_repair = function(pos)
|
|
|
|
return State:on_node_repair(pos)
|
2018-12-22 16:58:11 +01:00
|
|
|
end,
|
2018-08-08 01:06:16 +02:00
|
|
|
})
|