mesecons/mesecons_gates/init.lua

240 lines
4.9 KiB
Lua
Raw Normal View History

2012-12-10 17:19:30 +01:00
local outrules = {
{x=1, y=0, z=0},
}
2012-12-10 17:19:30 +01:00
local oneinput = {
2012-09-05 06:16:32 +02:00
{x=-1, y=0, z=0},
{x=1, y=0, z=0},
}
2012-12-10 17:19:30 +01:00
local twoinputs = {
2012-09-05 06:16:32 +02:00
{x=0, y=0, z=1},
{x=0, y=0, z=-1},
{x=1, y=0, z=0},
}
2012-12-10 17:19:30 +01:00
2012-09-05 06:16:32 +02:00
function get_gate_rules(param2, onlyout, singleinput)
2012-12-10 17:19:30 +01:00
if not param2 then return end
2012-09-05 06:16:32 +02:00
if onlyout then
rules = outrules
else
if singleinput then
rules = oneinput
else
rules = twoinputs
end
end
2012-12-10 17:19:30 +01:00
for rotations = 0, param2 - 1 do
2012-09-05 06:16:32 +02:00
rules = mesecon:rotate_rules_left(rules)
end
return rules
end
2012-12-10 17:19:30 +01:00
function get_gate_rules_one(node)
print("gettin"..dump(node).." | PARAM2: "..node.param2)
return get_gate_rules(node.param2, false, true)
end
function get_gate_rules_two(node)
return get_gate_rules(node.param2, false, false)
end
function get_gate_rules_out(node)
return get_gate_rules(node.param2, true)
end
gates = {"diode", "not", "nand", "and", "xor"}
for g in ipairs(gates) do gate = gates[g]
if g < 3 then
2012-09-05 06:16:32 +02:00
get_rules = get_gate_rules_one
else
2012-09-05 06:16:32 +02:00
get_rules = get_gate_rules_two
end
for on=0,1 do
nodename = "mesecons_gates:"..gate
if on == 1 then
onoff = "on"
drop = nodename.."_off"
nodename = nodename.."_"..onoff
2012-09-05 06:16:32 +02:00
description = "You hacker you!"
2012-12-10 17:19:30 +01:00
groups = {dig_immediate=2, not_in_creative_inventory=1}
else
2012-09-01 22:07:18 +02:00
onoff = "off"
nodename = nodename.."_"..onoff
2012-09-05 06:16:32 +02:00
description = gate.." Gate"
2012-12-10 17:19:30 +01:00
groups = {dig_immediate=2}
end
tiles = "jeija_microcontroller_bottom.png^"..
"jeija_gate_"..onoff..".png^"..
"jeija_gate_"..gate..".png"
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
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",
tiles = {tiles},
inventory_image = tiles,
2012-09-01 22:48:16 +02:00
selection_box = node_box,
node_box = node_box,
walkable = true,
on_construct = function(pos)
2012-09-02 01:10:23 +02:00
local meta = minetest.env:get_meta(pos)
meta:set_int("heat", 0)
update_gate(pos)
end,
groups = groups,
2012-09-01 22:07:18 +02:00
drop = drop,
2012-12-10 17:19:30 +01:00
mesecons =
{
receptor =
{
state = mesecon_state,
rules = get_gate_rules_out
},
effector =
{
rules = get_rules,
action_change = function (pos, node)
update_gate(pos)
end
}
}
})
end
end
function get_gate(pos)
2012-09-05 06:16:32 +02:00
return
string.gsub(
string.gsub(
string.gsub(
minetest.env:get_node(pos).name
, "mesecons_gates:", "") --gate
,"_on", "")
,"_off", "")
end
function gate_state(pos)
name = minetest.env:get_node(pos).name
2012-09-05 06:16:32 +02:00
return string.find(name, "_on") ~= nil
2012-09-02 01:10:23 +02:00
end
function pop_gate(pos)
gate = get_gate(pos)
minetest.env:remove_node(pos)
minetest.after(0.2, yc_overheat_off, pos)
minetest.env:add_item(pos, "mesecons_gates:"..gate.."_off")
end
2012-09-01 22:07:18 +02:00
function set_gate(pos, on)
gate = get_gate(pos)
2012-09-02 01:10:23 +02:00
local meta = minetest.env:get_meta(pos)
2012-09-02 03:26:15 +02:00
if on ~= gate_state(pos) then
yc_heat(meta)
minetest.after(0.5, yc_cool, meta)
if yc_overheat(meta) then
pop_gate(pos)
else
if on then
2012-12-10 17:19:30 +01:00
mesecon:swap_node(pos, "mesecons_gates:"..gate.."_on")
2012-09-05 06:16:32 +02:00
mesecon:receptor_on(pos, get_gate_rules(param2, true))
2012-09-02 01:10:23 +02:00
else
2012-12-10 17:19:30 +01:00
mesecon:swap_node(pos, "mesecons_gates:"..gate.."_off")
mesecon:receptor_off(pos, all_rules)
2012-09-02 01:10:23 +02:00
end
end
end
end
2012-09-05 06:16:32 +02:00
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
end
function update_gate(pos)
gate = get_gate(pos)
2012-09-05 06:16:32 +02:00
L = rotate_ports(
yc_get_real_portstates(pos),
minetest.env:get_node(pos).param2
)
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)
2012-12-10 17:19:30 +01:00
elseif gate == "xor" then
set_gate(pos, (L.b and not L.d) or (not L.b and L.d))
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', '', ''},
},
})