2012-12-10 19:46:24 +01:00
|
|
|
function gate_rotate_rules(node)
|
|
|
|
for rotations = 0, node.param2 - 1 do
|
|
|
|
rules = mesecon:rotate_rules_left(rules)
|
|
|
|
end
|
|
|
|
return rules
|
|
|
|
end
|
2012-12-10 17:19:30 +01:00
|
|
|
|
2012-12-10 19:46:24 +01:00
|
|
|
function gate_get_output_rules(node)
|
|
|
|
rules = {{x=1, y=0, z=0}}
|
|
|
|
return gate_rotate_rules(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
function gate_get_input_rules_oneinput(node)
|
|
|
|
rules = {{x=-1, y=0, z=0}, {x=1, y=0, z=0}}
|
|
|
|
return gate_rotate_rules(node)
|
|
|
|
end
|
2012-12-10 17:19:30 +01:00
|
|
|
|
2012-12-10 19:46:24 +01:00
|
|
|
function gate_get_input_rules_twoinputs(node)
|
|
|
|
rules = {
|
2012-09-05 06:16:32 +02:00
|
|
|
{x=0, y=0, z=1},
|
|
|
|
{x=0, y=0, z=-1},
|
2012-12-10 19:46:24 +01:00
|
|
|
{x=1, y=0, z=0}}
|
|
|
|
return gate_rotate_rules(node)
|
|
|
|
end
|
|
|
|
|
2014-01-10 16:38:02 +01:00
|
|
|
function update_gate(pos, node, rulename, newstate)
|
|
|
|
yc_update_real_portstates(pos, node, rulename, newstate)
|
2012-12-10 19:46:24 +01:00
|
|
|
gate = get_gate(pos)
|
|
|
|
L = rotate_ports(
|
|
|
|
yc_get_real_portstates(pos),
|
2013-12-01 04:13:00 +01:00
|
|
|
minetest.get_node(pos).param2
|
2012-12-10 19:46:24 +01:00
|
|
|
)
|
|
|
|
if gate == "diode" then
|
|
|
|
set_gate(pos, L.a)
|
|
|
|
elseif gate == "not" then
|
|
|
|
set_gate(pos, not L.a)
|
|
|
|
elseif gate == "nand" then
|
|
|
|
set_gate(pos, not(L.b and L.d))
|
|
|
|
elseif gate == "and" then
|
|
|
|
set_gate(pos, L.b and L.d)
|
|
|
|
elseif gate == "xor" then
|
|
|
|
set_gate(pos, (L.b and not L.d) or (not L.b and L.d))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function set_gate(pos, on)
|
|
|
|
gate = get_gate(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-12-10 19:46:24 +01:00
|
|
|
if on ~= gate_state(pos) then
|
2014-04-20 21:44:58 +02:00
|
|
|
if mesecon.do_overheat(pos) then
|
2012-12-10 19:46:24 +01:00
|
|
|
pop_gate(pos)
|
2012-09-05 06:16:32 +02:00
|
|
|
else
|
2013-12-01 02:20:01 +01:00
|
|
|
local node = minetest.get_node(pos)
|
2012-12-10 19:46:24 +01:00
|
|
|
if on then
|
2013-12-01 02:20:01 +01:00
|
|
|
minetest.swap_node(pos, {name = "mesecons_gates:"..gate.."_on", param2=node.param2})
|
2012-12-10 19:46:24 +01:00
|
|
|
mesecon:receptor_on(pos,
|
2013-12-01 02:20:01 +01:00
|
|
|
gate_get_output_rules(node))
|
2012-12-10 19:46:24 +01:00
|
|
|
else
|
2013-12-01 02:20:01 +01:00
|
|
|
minetest.swap_node(pos, {name = "mesecons_gates:"..gate.."_off", param2=node.param2})
|
2012-12-10 19:46:24 +01:00
|
|
|
mesecon:receptor_off(pos,
|
2013-12-01 02:20:01 +01:00
|
|
|
gate_get_output_rules(node))
|
2012-12-10 19:46:24 +01:00
|
|
|
end
|
2012-09-05 06:16:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-10 19:46:24 +01:00
|
|
|
function get_gate(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
return minetest.registered_nodes[minetest.get_node(pos).name].mesecons_gate
|
2012-12-10 17:19:30 +01:00
|
|
|
end
|
|
|
|
|
2012-12-10 19:46:24 +01:00
|
|
|
function gate_state(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
name = minetest.get_node(pos).name
|
2012-12-10 19:46:24 +01:00
|
|
|
return string.find(name, "_on") ~= nil
|
2012-12-10 17:19:30 +01:00
|
|
|
end
|
|
|
|
|
2012-12-10 19:46:24 +01:00
|
|
|
function pop_gate(pos)
|
|
|
|
gate = get_gate(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
minetest.remove_node(pos)
|
2014-04-20 21:44:58 +02:00
|
|
|
minetest.after(0.2, function (pos)
|
|
|
|
mesecon:receptor_off(pos, mesecon.rules.flat)
|
|
|
|
end , pos) -- wait for pending parsings
|
2013-12-01 04:13:00 +01:00
|
|
|
minetest.add_item(pos, "mesecons_gates:"..gate.."_off")
|
2012-12-10 19:46:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function rotate_ports(L, param2)
|
|
|
|
for rotations=0, param2-1 do
|
|
|
|
port = L.a
|
|
|
|
L.a = L.b
|
|
|
|
L.b = L.c
|
|
|
|
L.c = L.d
|
|
|
|
L.d = port
|
|
|
|
end
|
|
|
|
return L
|
2012-12-10 17:19:30 +01:00
|
|
|
end
|
|
|
|
|
2012-12-10 19:46:24 +01:00
|
|
|
gates = {
|
|
|
|
{name = "diode", inputnumber = 1},
|
|
|
|
{name = "not" , inputnumber = 1},
|
|
|
|
{name = "nand" , inputnumber = 2},
|
|
|
|
{name = "and" , inputnumber = 2},
|
|
|
|
{name = "xor" , inputnumber = 2}}
|
|
|
|
|
2013-01-06 20:09:54 +01:00
|
|
|
local onoff, drop, nodename, description, groups
|
2013-01-09 16:56:25 +01:00
|
|
|
for _, gate in ipairs(gates) do
|
2012-12-10 19:46:24 +01:00
|
|
|
if gate.inputnumber == 1 then
|
|
|
|
get_rules = gate_get_input_rules_oneinput
|
|
|
|
elseif gate.inputnumber == 2 then
|
|
|
|
get_rules = gate_get_input_rules_twoinputs
|
2012-09-01 10:23:15 +02:00
|
|
|
end
|
2012-12-10 19:46:24 +01:00
|
|
|
for on = 0, 1 do
|
|
|
|
nodename = "mesecons_gates:"..gate.name
|
2012-09-01 10:23:15 +02:00
|
|
|
if on == 1 then
|
|
|
|
onoff = "on"
|
2012-09-02 07:06:25 +02:00
|
|
|
drop = nodename.."_off"
|
|
|
|
nodename = nodename.."_"..onoff
|
2012-09-05 06:16:32 +02:00
|
|
|
description = "You hacker you!"
|
2013-06-05 06:45:19 +02:00
|
|
|
groups = {dig_immediate=2, not_in_creative_inventory=1, overheat = 1}
|
2012-09-01 10:23:15 +02:00
|
|
|
else
|
2012-09-01 22:07:18 +02:00
|
|
|
onoff = "off"
|
2013-01-06 20:09:54 +01:00
|
|
|
drop = nil
|
2012-09-02 07:06:25 +02:00
|
|
|
nodename = nodename.."_"..onoff
|
2012-12-10 19:46:24 +01:00
|
|
|
description = gate.name.." Gate"
|
2013-06-05 06:45:19 +02:00
|
|
|
groups = {dig_immediate=2, overheat = 1}
|
2012-09-01 10:23:15 +02:00
|
|
|
end
|
|
|
|
|
2012-09-02 08:27:23 +02:00
|
|
|
tiles = "jeija_microcontroller_bottom.png^"..
|
|
|
|
"jeija_gate_"..onoff..".png^"..
|
2012-12-10 19:46:24 +01:00
|
|
|
"jeija_gate_"..gate.name..".png"
|
2012-09-02 08:27:23 +02:00
|
|
|
|
2012-09-05 06:22:29 +02:00
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{-8/16, -8/16, -8/16, 8/16, -7/16, 8/16 },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-12-10 17:19:30 +01:00
|
|
|
local mesecon_state
|
|
|
|
if on == 1 then
|
|
|
|
mesecon_state = mesecon.state.on
|
|
|
|
else
|
|
|
|
mesecon_state = mesecon.state.off
|
|
|
|
end
|
|
|
|
|
2012-09-01 10:23:15 +02:00
|
|
|
minetest.register_node(nodename, {
|
2012-09-02 01:10:23 +02:00
|
|
|
description = description,
|
2012-09-01 22:48:16 +02:00
|
|
|
paramtype = "light",
|
2012-09-05 06:16:32 +02:00
|
|
|
paramtype2 = "facedir",
|
2012-09-01 22:48:16 +02:00
|
|
|
drawtype = "nodebox",
|
2012-09-02 08:27:23 +02:00
|
|
|
tiles = {tiles},
|
|
|
|
inventory_image = tiles,
|
2012-09-01 22:48:16 +02:00
|
|
|
selection_box = node_box,
|
|
|
|
node_box = node_box,
|
2012-09-01 10:23:15 +02:00
|
|
|
walkable = true,
|
|
|
|
on_construct = function(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-09-01 10:23:15 +02:00
|
|
|
update_gate(pos)
|
|
|
|
end,
|
|
|
|
groups = groups,
|
2012-09-01 22:07:18 +02:00
|
|
|
drop = drop,
|
2013-03-07 02:51:57 +01:00
|
|
|
sounds = default.node_sound_stone_defaults(),
|
2012-12-10 19:46:24 +01:00
|
|
|
mesecons_gate = gate.name,
|
2012-12-10 17:19:30 +01:00
|
|
|
mesecons =
|
|
|
|
{
|
|
|
|
receptor =
|
|
|
|
{
|
|
|
|
state = mesecon_state,
|
2012-12-10 19:46:24 +01:00
|
|
|
rules = gate_get_output_rules
|
2012-12-10 17:19:30 +01:00
|
|
|
},
|
|
|
|
effector =
|
|
|
|
{
|
|
|
|
rules = get_rules,
|
2012-12-10 19:46:24 +01:00
|
|
|
action_change = update_gate
|
2012-12-10 17:19:30 +01:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 10:23:15 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-02 02:07:00 +02:00
|
|
|
minetest.register_craft({
|
|
|
|
output = 'mesecons_gates:diode_off',
|
|
|
|
recipe = {
|
|
|
|
{'', '', ''},
|
|
|
|
{'mesecons:mesecon', 'mesecons_torch:mesecon_torch_on', 'mesecons_torch:mesecon_torch_on'},
|
|
|
|
{'', '', ''},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = 'mesecons_gates:not_off',
|
|
|
|
recipe = {
|
|
|
|
{'', '', ''},
|
|
|
|
{'mesecons:mesecon', 'mesecons_torch:mesecon_torch_on', 'mesecons:mesecon'},
|
|
|
|
{'', '', ''},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = 'mesecons_gates:and_off',
|
|
|
|
recipe = {
|
|
|
|
{'mesecons:mesecon', '', ''},
|
|
|
|
{'', 'mesecons_materials:silicon', 'mesecons:mesecon'},
|
|
|
|
{'mesecons:mesecon', '', ''},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = 'mesecons_gates:nand_off',
|
|
|
|
recipe = {
|
|
|
|
{'mesecons:mesecon', '', ''},
|
|
|
|
{'', 'mesecons_materials:silicon', 'mesecons_torch:mesecon_torch_on'},
|
|
|
|
{'mesecons:mesecon', '', ''},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = 'mesecons_gates:xor_off',
|
|
|
|
recipe = {
|
|
|
|
{'mesecons:mesecon', '', ''},
|
|
|
|
{'', 'mesecons_materials:silicon', 'mesecons_materials:silicon'},
|
|
|
|
{'mesecons:mesecon', '', ''},
|
|
|
|
},
|
|
|
|
})
|