comparator read from item_frame

This commit is contained in:
Henry Behrendt 2021-10-09 22:09:34 +02:00
parent 1227b6e1f1
commit 91cfb10097
1 changed files with 33 additions and 48 deletions

View File

@ -36,7 +36,7 @@ local function comparator_turnon(params)
for _, rulename in ipairs(rulenames) do
mesecon.conductor_off(np)
end
end
end
end
mesecon.receptor_on(params.pos, rules, params.voltage)
end
@ -70,7 +70,6 @@ local function container_inventory_nonempty(pos, rule)
-- fullness of a slot = number of items in slot / max stack size for this type of item
local invnode = minetest.get_node(pos)
local invnodedef = minetest.registered_nodes[invnode.name]
-- Ignore stale nodes
if not invnodedef then return 0, false end
@ -90,52 +89,46 @@ local function container_inventory_nonempty(pos, rule)
end
local inv = minetest.get_inventory({type="node", pos=pos})
if not inv then return 0, false end
if (not inv) or inv:is_empty("main") then return 0, false end
if invnode.name=="mcl_jukebox:jukebox" then
if not inv:is_empty("main") then
-- Jukebox contains a disc: Stop music and remove disc
local record = inv:get_stack("main", 1)
if mcl_jukebox.registered_records[record:get_name()] then
local ident = mcl_jukebox.registered_records[record:get_name()][3]
local t = {
id_13 = 1,
id_cat = 2,
id_blocks = 3,
id_chirp = 4,
id_far = 5,
id_mall = 6,
id_mellohi = 7,
id_stal = 8,
id_strad = 9,
id_ward = 10,
id_11 = 11,
id_wait = 12,
id_Pigstep = 13,
}
local vol = t["id_"..ident]
if vol then return vol, false end
end
local record = inv:get_stack("main", 1)
if mcl_jukebox.registered_records[record:get_name()] then
local ident = mcl_jukebox.registered_records[record:get_name()][3]
local t = {
id_13 = 1,
id_cat = 2,
id_blocks = 3,
id_chirp = 4,
id_far = 5,
id_mall = 6,
id_mellohi = 7,
id_stal = 8,
id_strad = 9,
id_ward = 10,
id_11 = 11,
id_wait = 12,
id_Pigstep = 13,
}
local vol = t["id_"..ident]
if vol then return vol, false end
end
elseif invnode.name=="mcl_itemframes:item_frame" then
local meta = minetest.get_meta(pos)
local rotation = meta:get_int("rotation") or 0
return rotation+1, false
else
local item_count = 0
local inv_space = 0
for listname, _ in pairs(inv:get_lists()) do
local stack = inv:get_stack(listname, 1)
local st_size = inv:get_size(listname)
inv_space = inv_space + (64 * st_size)
for i = 1,st_size do
local stack = inv:get_stack(listname, i)
item_count = item_count + (stack:get_count() * 64 / stack:get_stack_max())
end
--if not inv:is_empty(listname) then return true, 15 end
end
if item_count>0 then
local voltage = math.floor(1 + (item_count/inv_space*14))
return voltage, false
@ -148,7 +141,7 @@ end
local function static_signal_output(pos)
local node = minetest.get_node(pos)
local g = minetest.get_item_group(node.name, "comparator_signal")
return g
return g
end
-- whether the comparator should be on according to its inputs
@ -159,24 +152,17 @@ local function comparator_desired_on(pos, node)
if back_rule then
local back_pos = vector.add(pos, back_rule)
local _, _, vo_back = mesecon.is_powered(pos, back_rule)
local vo_coin, spread = container_inventory_nonempty(back_pos, back_rule)
local vo_sso = static_signal_output(back_pos)
if vo_coin>=1 and spread and vo_back<15 then
if vo_coin>=1 and spread and vo_back<15 then
--container through opaque block and block is powerd less than 15 -> ignore powered block
vo_back=0
end
voltage = math.max(vo_back, vo_coin, vo_sso)
end
-- if back input is off, we don't need to check side inputs
if voltage<1 then return false, 0 end
local mode = minetest.registered_nodes[node.name].comparator_mode
local side_voltage = 0
for ri = 2,3 do
@ -188,15 +174,16 @@ local function comparator_desired_on(pos, node)
end
end
local mode = minetest.registered_nodes[node.name].comparator_mode
if mode == "comp" then
-- Comparators in comparison mode
if side_voltage > voltage then
return false, 0
else
return true, voltage
return true, voltage
end
end
-- comparator in subtraction mode
voltage = math.max(voltage-side_voltage, 0)
if voltage>0 then
@ -211,8 +198,8 @@ Compare signal strength
Comparators in comparison mode.
A redstone comparator in comparison mode (front torch down and unpowered) compares its rear input to its two side inputs.
If either side input is greater than the rear input, the comparator output turns off. If neither side input is greater than
A redstone comparator in comparison mode (front torch down and unpowered) compares its rear input to its two side inputs.
If either side input is greater than the rear input, the comparator output turns off. If neither side input is greater than
the rear input, the comparator outputs the same signal strength as its rear input.
The formula for calculating the output signal strength is as follows:
@ -240,9 +227,7 @@ local function update_self(pos, node)
local old_state = mesecon.is_receptor_on(node.name)
local meta = minetest.get_meta(pos)
local old_voltage = old_state and (meta:get_string("mesecon_voltage")~="" and meta:get_int("mesecon_voltage") or 15) or 0
local new_state, new_voltage = comparator_desired_on(pos, node)
if (new_state ~= old_state) or (old_voltage~=new_voltage) then
if old_voltage~=new_voltage then meta:set_int("mesecon_voltage", new_voltage) end
if new_state then