]> git.lizzy.rs Git - Crafter.git/blob - mods/redstone/switches.lua
Add buttons
[Crafter.git] / mods / redstone / switches.lua
1 --create torch versions of the nodes
2 for name,def in pairs(minetest.registered_nodes) do
3         if def.drawtype == "normal" and string.match(name, "main:") then
4                 local def2 = table.copy(def)
5                 def2.groups.redstone_torch = 1
6                 def2.drop = name
7                 def2.mod_origin = "redstone"
8                 def2.textures = "dirt.png"
9                 local newname = "redstone:"..string.gsub(name, "main:", "")
10                 def2.name = newname
11                 minetest.register_node(newname,def2)
12         end
13 end
14
15
16 --this removes power from node that the switch is powering
17 local function on_lever_destroy(pos)
18         local param2 = minetest.get_node(pos).param2
19         local self = minetest.get_node(pos)
20         local dir = minetest.facedir_to_dir(self.param2)
21         
22         local pos = vector.add(dir,pos)
23         local node = minetest.get_node(pos)
24         local name = node.name
25         
26         local def = minetest.registered_nodes[name]
27         if def.drawtype == "normal" and string.match(name, "redstone:") then
28                 name = "main:"..string.gsub(name, "redstone:", "")
29                 minetest.set_node(pos, {name=name})
30                 redstone.collect_info(pos)
31         end
32 end
33
34
35 minetest.register_node("redstone:switch_off", {
36     description = "Crafting Table",
37     tiles = {"stone.png"},
38     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4},
39     sounds = main.stoneSound(),
40     paramtype = "light",
41         paramtype2 = "wallmounted",
42         sunlight_propagates = true,
43         walkable = false,
44         drawtype= "nodebox",
45         drop="redstone:switch_off",
46         node_box = {
47                 type = "fixed",
48                 fixed = {
49                                 --left front bottom right back top
50                                 {-0.3, -0.5,  -0.4, 0.3,  -0.4, 0.4},
51                                 {-0.1, -0.5,  -0.3, 0.1,  0, -0.1},
52                         },
53                 },
54     on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
55                 minetest.sound_play("lever", {pos=pos})
56                 minetest.set_node(pos, {name="redstone:switch_on",param2=node.param2})
57                 local dir = minetest.facedir_to_dir(node.param2)
58                 local pos = vector.add(dir,pos)
59                 local name = minetest.get_node(pos).name
60                 local def = minetest.registered_nodes[name]
61                 
62                 if def.drawtype == "normal" and string.match(name, "main:") then
63                         name = "redstone:"..string.gsub(name, "main:", "")
64                         minetest.set_node(pos,{name=name})
65                         redstone.collect_info(pos)
66                 end
67         end,
68         on_destruct = on_lever_destroy,
69 })
70 minetest.register_node("redstone:switch_on", {
71     description = "Crafting Table",
72     tiles = {"stone.png"},
73     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4},
74     sounds = main.stoneSound(),
75     paramtype = "light",
76         paramtype2 = "wallmounted",
77         sunlight_propagates = true,
78         walkable = false,
79         drawtype= "nodebox",
80         drop="redstone:switch_off",
81         node_box = {
82                 type = "fixed",
83                 fixed = {
84                                 --left front bottom right back top
85                                 {-0.3, -0.5,  -0.4, 0.3,  -0.4, 0.4},
86                                 {-0.1, -0.5,  0.3, 0.1,  0, 0.1},
87                         },
88                 },
89     on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
90                 minetest.sound_play("lever", {pos=pos,pitch=0.8})
91                 minetest.set_node(pos, {name="redstone:switch_off",param2=node.param2})
92                 on_lever_destroy(pos)
93         end,
94         on_destruct = on_lever_destroy,
95 })