Fix incorrect power transmission of buttons
[MineClone/MineClone2.git] / mods / ITEMS / REDSTONE / mesecons_torch / init.lua
blob35e339d3e1ca0783e1b18a50ef481541c1f3f61f
1 --MESECON TORCHES
3 local rotate_torch_rules = function (rules, param2)
4 if param2 == 5 then
5 return mesecon.rotate_rules_right(rules)
6 elseif param2 == 2 then
7 return mesecon.rotate_rules_right(mesecon.rotate_rules_right(rules)) --180 degrees
8 elseif param2 == 4 then
9 return mesecon.rotate_rules_left(rules)
10 elseif param2 == 1 then
11 return mesecon.rotate_rules_down(rules)
12 elseif param2 == 0 then
13 return mesecon.rotate_rules_up(rules)
14 else
15 return rules
16 end
17 end
19 local torch_get_output_rules = function()
20 return mesecon.rules.mcl_alldirs_spread
21 end
23 local torch_get_input_rules = function(node)
24 local rules = {{x = -2, y = 0, z = 0},
25 {x = -1, y = 1, z = 0}}
27 return rotate_torch_rules(rules, node.param2)
28 end
30 minetest.register_craft({
31 output = 'mesecons_torch:mesecon_torch_on',
32 recipe = {
33 {"mesecons:redstone"},
34 {"mcl_core:stick"},}
37 mcl_torches.register_torch("mesecon_torch_off", "Redstone Torch (off)",
38 nil,
39 nil,
40 "jeija_torches_off.png",
41 "mcl_torches_torch_floor.obj", "mcl_torches_torch_wall.obj",
42 {"jeija_torches_off.png"},
44 {dig_immediate=3, dig_by_water=1, not_in_creative_inventory=1},
45 mcl_sounds.node_sound_wood_defaults(),
47 mesecons = {receptor = {
48 state = mesecon.state.off,
49 rules = torch_get_output_rules
50 }},
51 drop = "mesecons_torch:mesecon_torch_on",
52 _doc_items_create_entry = false,
56 mcl_torches.register_torch("mesecon_torch_on", "Redstone Torch",
57 "Redstone torches are redstone components which invert the signal of surrounding redstone components. An active component will become inactive, and an inactive component will become active. Redstone torches can be used as a quick and easy way to send a redstone to a redstone trail.",
58 [[Redstone torches can generally be placed at the side and on the top of full solid opaque blocks. The following exceptions apply:
59 • Glass, fence, wall, hopper: Can only be placed on top
60 • Upside-down slab/stair: Can only be placed on top
61 • Soul sand, mob spawner: Placement possible
62 • Glowstone and pistons: No placement possible]],
63 "jeija_torches_on.png",
64 "mcl_torches_torch_floor.obj", "mcl_torches_torch_wall.obj",
65 {"jeija_torches_on.png"},
67 {dig_immediate=3, dig_by_water=1,},
68 mcl_sounds.node_sound_wood_defaults(),
70 mesecons = {receptor = {
71 state = mesecon.state.on,
72 rules = torch_get_output_rules
77 minetest.register_node("mesecons_torch:redstoneblock", {
78 description = "Block of Redstone",
79 _doc_items_longdesc = "A block of redstone permanently supplies redstone power to its surrounding blocks.",
80 tiles = {"redstone_redstone_block.png"},
81 stack_max = 64,
82 groups = {pickaxey=1},
83 sounds = mcl_sounds.node_sound_stone_defaults(),
84 is_ground_content = false,
85 mesecons = {receptor = {
86 state = mesecon.state.on,
87 rules = mesecon.rules.alldirs,
88 }},
89 _mcl_blast_resistance = 30,
90 _mcl_hardness = 5,
93 minetest.register_craft({
94 output = "mesecons_torch:redstoneblock",
95 recipe = {
96 {'mesecons:wire_00000000_off','mesecons:wire_00000000_off','mesecons:wire_00000000_off'},
97 {'mesecons:wire_00000000_off','mesecons:wire_00000000_off','mesecons:wire_00000000_off'},
98 {'mesecons:wire_00000000_off','mesecons:wire_00000000_off','mesecons:wire_00000000_off'},
102 minetest.register_craft({
103 output = 'mesecons:wire_00000000_off 9',
104 recipe = {
105 {'mesecons_torch:redstoneblock'},
109 minetest.register_abm({
110 label = "Redstone torch inversion",
111 nodenames = {"mesecons_torch:mesecon_torch_off","mesecons_torch:mesecon_torch_off_wall","mesecons_torch:mesecon_torch_on","mesecons_torch:mesecon_torch_on_wall"},
112 interval = 1,
113 chance = 1,
114 action = function(pos, node)
115 local is_powered = false
116 for _, rule in ipairs(torch_get_input_rules(node)) do
117 local src = vector.add(pos, rule)
118 if mesecon.is_power_on(src) then
119 is_powered = true
123 if is_powered then
124 if node.name == "mesecons_torch:mesecon_torch_on" then
125 minetest.swap_node(pos, {name="mesecons_torch:mesecon_torch_off", param2=node.param2})
126 mesecon.receptor_off(pos, torch_get_output_rules(node))
127 elseif node.name == "mesecons_torch:mesecon_torch_on_wall" then
128 minetest.swap_node(pos, {name="mesecons_torch:mesecon_torch_off_wall", param2=node.param2})
129 mesecon.receptor_off(pos, torch_get_output_rules(node))
131 elseif node.name == "mesecons_torch:mesecon_torch_off" then
132 minetest.swap_node(pos, {name="mesecons_torch:mesecon_torch_on", param2=node.param2})
133 mesecon.receptor_on(pos, torch_get_output_rules(node))
134 elseif node.name == "mesecons_torch:mesecon_torch_off_wall" then
135 minetest.swap_node(pos, {name="mesecons_torch:mesecon_torch_on_wall", param2=node.param2})
136 mesecon.receptor_on(pos, torch_get_output_rules(node))
141 if minetest.get_modpath("doc") then
142 doc.add_entry_alias("nodes", "mesecons_torch:mesecon_torch_on", "nodes", "mesecons_torch:mesecon_torch_off")
143 doc.add_entry_alias("nodes", "mesecons_torch:mesecon_torch_on", "nodes", "mesecons_torch:mesecon_torch_off_wall")