Version 0.41.3
[MineClone/MineClone2/MineClone2-Fixes.git] / mods / ITEMS / REDSTONE / mesecons_button / init.lua
blob3e34670e0bf783580739c831de229606e95a6bec
1 -- WALL BUTTON
2 -- A button that when pressed emits power for 1 second
3 -- and then turns off again
5 local button_get_output_rules = mesecon.rules.wallmounted_get
7 local boxes_off = {
8 type = "wallmounted",
9 wall_side = { -8/16, -2/16, -4/16, -6/16, 2/16, 4/16 },
10 wall_bottom = { -4/16, -8/16, -2/16, 4/16, -6/16, 2/16 },
11 wall_top = { -4/16, 6/16, -2/16, 4/16, 8/16, 2/16 },
13 local boxes_on = {
14 type = "wallmounted",
15 wall_side = { -8/16, -2/16, -4/16, -7/16, 2/16, 4/16 },
16 wall_bottom = { -4/16, -8/16, -2/16, 4/16, -7/16, 2/16 },
17 wall_top = { -4/16, 7/16, -2/16, 4/16, 8/16, 2/16 },
20 -- Push the button
21 mesecon.push_button = function(pos, node)
22 -- No-op if button is already pushed
23 if mesecon.is_receptor_on(node) then
24 return
25 end
26 local def = minetest.registered_nodes[node.name]
27 minetest.set_node(pos, {name="mesecons_button:button_"..def._mcl_button_basename.."_on", param2=node.param2})
28 mesecon.receptor_on(pos, button_get_output_rules(node))
29 minetest.sound_play("mesecons_button_push", {pos=pos})
30 local timer = minetest.get_node_timer(pos)
31 timer:start(def._mcl_button_timer)
32 end
34 local on_button_place = function(itemstack, placer, pointed_thing)
35 if pointed_thing.type ~= "node" then
36 -- no interaction possible with entities
37 return itemstack
38 end
40 local under = pointed_thing.under
41 local node = minetest.get_node(under)
42 local def = minetest.registered_nodes[node.name]
43 if not def then return end
44 local groups = def.groups
46 -- Check special rightclick action of pointed node
47 if def and def.on_rightclick then
48 if not placer:get_player_control().sneak then
49 return def.on_rightclick(under, node, placer, itemstack,
50 pointed_thing) or itemstack, false
51 end
52 end
54 -- If the pointed node is buildable, let's look at the node *behind* that node
55 if def.buildable_to then
56 local dir = vector.subtract(pointed_thing.above, pointed_thing.under)
57 local actual = vector.subtract(under, dir)
58 local actualnode = minetest.get_node(actual)
59 def = minetest.registered_nodes[actualnode.name]
60 groups = def.groups
61 end
63 -- Only allow placement on full-cube solid opaque nodes
64 if (not groups) or (not groups.solid) or (not groups.opaque) or (def.node_box and def.node_box.type ~= "regular") then
65 return itemstack
66 end
68 local above = pointed_thing.above
70 local idef = itemstack:get_definition()
71 local itemstack, success = minetest.item_place_node(itemstack, placer, pointed_thing)
73 if success then
74 if idef.sounds and idef.sounds.place then
75 minetest.sound_play(idef.sounds.place, {pos=above, gain=1})
76 end
77 end
78 return itemstack
79 end
81 local buttonuse = "Rightclick the button to push it."
83 mesecon.register_button = function(basename, description, texture, recipeitem, sounds, plusgroups, button_timer, push_by_arrow, longdesc)
84 local groups_off = table.copy(plusgroups)
85 groups_off.attached_node=1
86 groups_off.dig_by_water=1
87 groups_off.destroy_by_lava_flow=1
88 groups_off.dig_by_piston=1
89 groups_off.button=1 -- button (off)
91 local groups_on = table.copy(groups_off)
92 groups_on.not_in_creative_inventory=1
93 groups_on.button=2 -- button (on)
95 if push_by_arrow then
96 groups_off.button_push_by_arrow = 1
97 groups_on.button_push_by_arrow = 1
98 end
100 minetest.register_node("mesecons_button:button_"..basename.."_off", {
101 drawtype = "nodebox",
102 tiles = {texture},
103 wield_image = "mesecons_button_wield_mask.png^"..texture.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
104 -- FIXME: Use proper 3D inventory image
105 inventory_image = "mesecons_button_wield_mask.png^"..texture.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
106 wield_scale = { x=1, y=1, z=1},
107 paramtype = "light",
108 paramtype2 = "wallmounted",
109 is_ground_content = false,
110 walkable = false,
111 sunlight_propagates = true,
112 node_box = boxes_off,
113 groups = groups_off,
114 description = description,
115 _doc_items_longdesc = longdesc,
116 _doc_items_usagehelp = buttonuse,
117 on_place = on_button_place,
118 node_placement_prediction = "",
119 on_rightclick = function (pos, node)
120 mesecon.push_button(pos, node)
121 end,
122 sounds = sounds,
123 mesecons = {receptor = {
124 state = mesecon.state.off,
125 rules = button_get_output_rules,
127 _mcl_button_basename = basename,
128 _mcl_button_timer = button_timer,
130 _mcl_blast_resistance = 2.5,
131 _mcl_hardness = 0.5,
134 minetest.register_node("mesecons_button:button_"..basename.."_on", {
135 drawtype = "nodebox",
136 tiles = {texture},
137 wield_image = "mesecons_button_wield_mask.png^"..texture.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
138 inventory_image = "mesecons_button_wield_mask.png^"..texture.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
139 wield_scale = { x=1, y=1, z=0.5},
140 paramtype = "light",
141 paramtype2 = "wallmounted",
142 is_ground_content = false,
143 walkable = false,
144 sunlight_propagates = true,
145 node_box = boxes_on,
146 groups = groups_on,
147 drop = 'mesecons_button:button_'..basename..'_off',
148 _doc_items_create_entry = false,
149 node_placement_prediction = "",
150 sounds = sounds,
151 mesecons = {receptor = {
152 state = mesecon.state.on,
153 rules = button_get_output_rules
155 _mcl_button_basename = basename,
156 _mcl_button_timer = button_timer,
157 on_timer = function(pos, elapsed)
158 local node = minetest.get_node(pos)
159 if node.name=="mesecons_button:button_"..basename.."_on" then --has not been dug
160 -- Is button pushable by arrow?
161 if push_by_arrow then
162 -- If there's an arrow stuck in the button, keep it pressed and check
163 -- it again later.
164 local objs = minetest.get_objects_inside_radius(pos, 1)
165 for o=1, #objs do
166 local entity = objs[o]:get_luaentity()
167 if entity and entity.name == "mcl_bows:arrow_entity" then
168 local timer = minetest.get_node_timer(pos)
169 timer:start(button_timer)
170 return
175 -- Normal operation: Un-press the button
176 minetest.set_node(pos, {name="mesecons_button:button_"..basename.."_off",param2=node.param2})
177 minetest.sound_play("mesecons_button_pop", {pos=pos})
178 mesecon.receptor_off(pos, button_get_output_rules(node))
180 end,
182 _mcl_blast_resistance = 2.5,
183 _mcl_hardness = 0.5,
186 minetest.register_craft({
187 output = "mesecons_button:button_"..basename.."_off",
188 recipe = {{ recipeitem }},
192 mesecon.register_button(
193 "stone",
194 "Stone Button",
195 "default_stone.png",
196 "mcl_core:stone",
197 mcl_sounds.node_sound_stone_defaults(),
198 {material_stone=1,handy=1,pickaxey=1},
200 false,
201 "A stone button is a redstone component made out of stone which can be pushed to provide redstone power. When pushed, it powers adjacent redstone components for 1 second. It can only be placed on solid opaque full cubes (like cobblestone).")
203 local woods = {
204 { "wood", "mcl_core:wood", "default_wood.png", "Oak Button" },
205 { "acaciawood", "mcl_core:acaciawood", "default_acacia_wood.png", "Acacia Button" },
206 { "birchwood", "mcl_core:birchwood", "mcl_core_planks_birch.png", "Birch Button" },
207 { "darkwood", "mcl_core:darkwood", "mcl_core_planks_big_oak.png", "Dark Oak Button" },
208 { "sprucewood", "mcl_core:sprucewood", "mcl_core_planks_spruce.png", "Spruce Button" },
209 { "junglewood", "mcl_core:junglewood", "default_junglewood.png", "Jungle Button" },
212 for w=1, #woods do
213 mesecon.register_button(
214 woods[w][1],
215 woods[w][4],
216 woods[w][3],
217 woods[w][2],
218 mcl_sounds.node_sound_wood_defaults(),
219 {material_wood=1,handy=1,axey=1},
220 1.5,
221 true,
222 "A wooden button is a redstone component made out of wood which can be pushed to provide redstone power. When pushed, it powers adjacent redstone components for 1.5 seconds. It can only be placed on solid opaque full cubes (like cobblestone). Wooden buttons may also be pushed by arrows.")
224 minetest.register_craft({
225 type = "fuel",
226 recipe = "mesecons_button:button_"..woods[w][1].."_off",
227 burntime = 5,
231 -- Add entry aliases for the Help
232 if minetest.get_modpath("doc") then
233 doc.add_entry_alias("nodes", "mesecons_button:button_wood_off", "nodes", "mesecons_button:button_wood_on")
234 doc.add_entry_alias("nodes", "mesecons_button:button_stone_off", "nodes", "mesecons_button:button_stone_on")