2015-06-29 17:55:56 +00:00
-- WALL BUTTON
2019-03-16 01:00:48 +00:00
-- A button that when pressed emits power for a short moment and then turns off again
2021-05-29 14:12:33 +00:00
local S = minetest.get_translator ( minetest.get_current_modname ( ) )
2015-06-29 17:55:56 +00:00
2020-12-07 03:59:10 +00:00
local button_sounds = { } -- remember button push sounds
2018-01-13 03:08:40 +00:00
local button_get_output_rules = mesecon.rules . wallmounted_get
2017-03-27 23:52:09 +00:00
2017-03-27 23:22:01 +00:00
local boxes_off = {
type = " wallmounted " ,
wall_side = { - 8 / 16 , - 2 / 16 , - 4 / 16 , - 6 / 16 , 2 / 16 , 4 / 16 } ,
wall_bottom = { - 4 / 16 , - 8 / 16 , - 2 / 16 , 4 / 16 , - 6 / 16 , 2 / 16 } ,
wall_top = { - 4 / 16 , 6 / 16 , - 2 / 16 , 4 / 16 , 8 / 16 , 2 / 16 } ,
}
local boxes_on = {
type = " wallmounted " ,
wall_side = { - 8 / 16 , - 2 / 16 , - 4 / 16 , - 7 / 16 , 2 / 16 , 4 / 16 } ,
wall_bottom = { - 4 / 16 , - 8 / 16 , - 2 / 16 , 4 / 16 , - 7 / 16 , 2 / 16 } ,
wall_top = { - 4 / 16 , 7 / 16 , - 2 / 16 , 4 / 16 , 8 / 16 , 2 / 16 } ,
}
2015-06-29 17:55:56 +00:00
2018-05-08 16:22:08 +00:00
-- Push the button
2021-05-25 10:52:25 +00:00
function mesecon . push_button ( pos , node )
2018-05-08 16:22:08 +00:00
-- No-op if button is already pushed
if mesecon.is_receptor_on ( node ) then
return
end
local def = minetest.registered_nodes [ node.name ]
minetest.set_node ( pos , { name = " mesecons_button:button_ " .. def._mcl_button_basename .. " _on " , param2 = node.param2 } )
mesecon.receptor_on ( pos , button_get_output_rules ( node ) )
2020-12-07 03:59:10 +00:00
local sfx = button_sounds [ node.name ]
if sfx then
minetest.sound_play ( sfx , { pos = pos } , true )
end
2018-05-08 16:22:08 +00:00
local timer = minetest.get_node_timer ( pos )
timer : start ( def._mcl_button_timer )
end
2021-05-29 14:12:33 +00:00
local function on_button_place ( itemstack , placer , pointed_thing )
2017-05-27 16:09:11 +00:00
if pointed_thing.type ~= " node " then
-- no interaction possible with entities
return itemstack
end
local under = pointed_thing.under
local node = minetest.get_node ( under )
local def = minetest.registered_nodes [ node.name ]
2017-06-29 11:02:53 +00:00
if not def then return end
2017-05-27 16:09:11 +00:00
local groups = def.groups
-- Check special rightclick action of pointed node
if def and def.on_rightclick then
if not placer : get_player_control ( ) . sneak then
return def.on_rightclick ( under , node , placer , itemstack ,
pointed_thing ) or itemstack , false
end
end
2017-05-27 16:18:54 +00:00
-- If the pointed node is buildable, let's look at the node *behind* that node
if def.buildable_to then
local dir = vector.subtract ( pointed_thing.above , pointed_thing.under )
local actual = vector.subtract ( under , dir )
local actualnode = minetest.get_node ( actual )
def = minetest.registered_nodes [ actualnode.name ]
groups = def.groups
end
2017-05-27 16:09:11 +00:00
-- Only allow placement on full-cube solid opaque nodes
if ( not groups ) or ( not groups.solid ) or ( not groups.opaque ) or ( def.node_box and def.node_box . type ~= " regular " ) then
return itemstack
end
local above = pointed_thing.above
local idef = itemstack : get_definition ( )
2017-05-27 16:28:30 +00:00
local itemstack , success = minetest.item_place_node ( itemstack , placer , pointed_thing )
2017-05-27 16:09:11 +00:00
if success then
if idef.sounds and idef.sounds . place then
2020-04-06 22:55:45 +00:00
minetest.sound_play ( idef.sounds . place , { pos = above , gain = 1 } , true )
2017-05-27 16:09:11 +00:00
end
end
return itemstack
end
2019-03-16 01:00:48 +00:00
local buttonuse = S ( " Use the button to push it. " )
2015-06-29 17:55:56 +00:00
2021-05-29 14:12:33 +00:00
function mesecon . register_button ( basename , description , texture , recipeitem , sounds , plusgroups , button_timer , push_by_arrow , longdesc , button_sound )
2017-11-22 21:41:43 +00:00
local groups_off = table.copy ( plusgroups )
groups_off.attached_node = 1
groups_off.dig_by_water = 1
groups_off.destroy_by_lava_flow = 1
groups_off.dig_by_piston = 1
groups_off.button = 1 -- button (off)
2017-11-22 21:24:05 +00:00
local groups_on = table.copy ( groups_off )
groups_on.not_in_creative_inventory = 1
2017-11-22 21:41:43 +00:00
groups_on.button = 2 -- button (on)
2017-11-22 21:24:05 +00:00
2020-12-07 03:59:10 +00:00
if not button_sound then
button_sound = " mesecons_button_push "
end
button_sounds [ " mesecons_button:button_ " .. basename .. " _off " ] = button_sound
2018-05-08 11:46:39 +00:00
if push_by_arrow then
groups_off.button_push_by_arrow = 1
groups_on.button_push_by_arrow = 1
end
2020-02-19 03:54:17 +00:00
local tt = S ( " Provides redstone power when pushed " )
tt = tt .. " \n " .. S ( " Push duration: @1s " , string.format ( " %.1f " , button_timer ) )
if push_by_arrow then
tt = tt .. " \n " .. S ( " Pushable by arrow " )
end
2017-11-22 21:24:05 +00:00
minetest.register_node ( " mesecons_button:button_ " .. basename .. " _off " , {
drawtype = " nodebox " ,
tiles = { texture } ,
wield_image = " mesecons_button_wield_mask.png^ " .. texture .. " ^mesecons_button_wield_mask.png^[makealpha:255,126,126 " ,
-- FIXME: Use proper 3D inventory image
inventory_image = " mesecons_button_wield_mask.png^ " .. texture .. " ^mesecons_button_wield_mask.png^[makealpha:255,126,126 " ,
wield_scale = { x = 1 , y = 1 , z = 1 } ,
paramtype = " light " ,
paramtype2 = " wallmounted " ,
is_ground_content = false ,
walkable = false ,
sunlight_propagates = true ,
node_box = boxes_off ,
groups = groups_off ,
description = description ,
2020-02-19 03:54:17 +00:00
_tt_help = tt ,
2017-11-22 21:24:05 +00:00
_doc_items_longdesc = longdesc ,
_doc_items_usagehelp = buttonuse ,
on_place = on_button_place ,
node_placement_prediction = " " ,
2021-05-29 14:12:33 +00:00
on_rightclick = function ( pos , node )
2018-05-08 16:22:08 +00:00
mesecon.push_button ( pos , node )
2017-11-22 21:24:05 +00:00
end ,
sounds = sounds ,
mesecons = { receptor = {
state = mesecon.state . off ,
rules = button_get_output_rules ,
} } ,
2018-05-08 16:22:08 +00:00
_mcl_button_basename = basename ,
_mcl_button_timer = button_timer ,
2020-04-17 19:40:13 +00:00
_mcl_blast_resistance = 0.5 ,
2017-11-22 21:24:05 +00:00
_mcl_hardness = 0.5 ,
} )
minetest.register_node ( " mesecons_button:button_ " .. basename .. " _on " , {
drawtype = " nodebox " ,
tiles = { texture } ,
wield_image = " mesecons_button_wield_mask.png^ " .. texture .. " ^mesecons_button_wield_mask.png^[makealpha:255,126,126 " ,
wield_scale = { x = 1 , y = 1 , z = 0.5 } ,
paramtype = " light " ,
paramtype2 = " wallmounted " ,
is_ground_content = false ,
walkable = false ,
sunlight_propagates = true ,
node_box = boxes_on ,
groups = groups_on ,
2021-05-29 14:12:33 +00:00
drop = " mesecons_button:button_ " .. basename .. " _off " ,
2017-11-22 21:24:05 +00:00
_doc_items_create_entry = false ,
node_placement_prediction = " " ,
sounds = sounds ,
mesecons = { receptor = {
state = mesecon.state . on ,
rules = button_get_output_rules
} } ,
2018-05-08 16:22:08 +00:00
_mcl_button_basename = basename ,
_mcl_button_timer = button_timer ,
2018-05-07 22:48:07 +00:00
on_timer = function ( pos , elapsed )
local node = minetest.get_node ( pos )
if node.name == " mesecons_button:button_ " .. basename .. " _on " then --has not been dug
2018-05-08 11:46:39 +00:00
-- Is button pushable by arrow?
if push_by_arrow then
-- If there's an arrow stuck in the button, keep it pressed and check
-- it again later.
local objs = minetest.get_objects_inside_radius ( pos , 1 )
for o = 1 , # objs do
local entity = objs [ o ] : get_luaentity ( )
if entity and entity.name == " mcl_bows:arrow_entity " then
local timer = minetest.get_node_timer ( pos )
timer : start ( button_timer )
return
end
end
end
-- Normal operation: Un-press the button
2018-05-07 22:48:07 +00:00
minetest.set_node ( pos , { name = " mesecons_button:button_ " .. basename .. " _off " , param2 = node.param2 } )
2020-12-07 03:59:10 +00:00
minetest.sound_play ( button_sound , { pos = pos , pitch = 0.9 } , true )
2018-05-07 22:48:07 +00:00
mesecon.receptor_off ( pos , button_get_output_rules ( node ) )
end
end ,
2018-05-08 16:22:08 +00:00
2020-04-17 19:40:13 +00:00
_mcl_blast_resistance = 0.5 ,
2017-11-22 21:24:05 +00:00
_mcl_hardness = 0.5 ,
} )
minetest.register_craft ( {
output = " mesecons_button:button_ " .. basename .. " _off " ,
recipe = { { recipeitem } } ,
} )
end
2015-06-29 17:55:56 +00:00
2017-11-22 21:24:05 +00:00
mesecon.register_button (
" stone " ,
2019-03-16 01:00:48 +00:00
S ( " Stone Button " ) ,
2017-11-22 21:24:05 +00:00
" default_stone.png " ,
" mcl_core:stone " ,
mcl_sounds.node_sound_stone_defaults ( ) ,
{ material_stone = 1 , handy = 1 , pickaxey = 1 } ,
1 ,
2018-05-08 11:46:39 +00:00
false ,
2020-12-07 03:59:10 +00:00
S ( " 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. " ) ,
" mesecons_button_push " )
2017-11-22 21:24:05 +00:00
2017-11-22 21:41:43 +00:00
local woods = {
2019-03-16 01:00:48 +00:00
{ " wood " , " mcl_core:wood " , " default_wood.png " , S ( " Oak Button " ) } ,
{ " acaciawood " , " mcl_core:acaciawood " , " default_acacia_wood.png " , S ( " Acacia Button " ) } ,
{ " birchwood " , " mcl_core:birchwood " , " mcl_core_planks_birch.png " , S ( " Birch Button " ) } ,
{ " darkwood " , " mcl_core:darkwood " , " mcl_core_planks_big_oak.png " , S ( " Dark Oak Button " ) } ,
{ " sprucewood " , " mcl_core:sprucewood " , " mcl_core_planks_spruce.png " , S ( " Spruce Button " ) } ,
{ " junglewood " , " mcl_core:junglewood " , " default_junglewood.png " , S ( " Jungle Button " ) } ,
2022-11-27 02:28:07 +00:00
2022-11-27 03:34:04 +00:00
{ " mangrove_wood " , " mcl_mangrove:mangrove_wood " , " mcl_mangrove_planks.png " , S ( " Mangrove Button " ) } ,
{ " crimson_hyphae_wood " , " mcl_crimson:crimson_hyphae_wood " , " crimson_hyphae_wood.png " , S ( " Crimson Button " ) } ,
{ " warped_hyphae_wood " , " mcl_crimson:warped_hyphae_wood " , " warped_hyphae_wood.png " , S ( " Warped Button " ) } ,
2017-11-22 21:41:43 +00:00
}
for w = 1 , # woods do
mesecon.register_button (
woods [ w ] [ 1 ] ,
woods [ w ] [ 4 ] ,
woods [ w ] [ 3 ] ,
woods [ w ] [ 2 ] ,
mcl_sounds.node_sound_wood_defaults ( ) ,
{ material_wood = 1 , handy = 1 , axey = 1 } ,
1.5 ,
2018-05-08 11:46:39 +00:00
true ,
2020-12-07 03:59:10 +00:00
S ( " 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. Wooden buttons may also be pushed by arrows. " ) ,
" mesecons_button_push_wood " )
2017-11-22 21:41:43 +00:00
minetest.register_craft ( {
type = " fuel " ,
recipe = " mesecons_button:button_ " .. woods [ w ] [ 1 ] .. " _off " ,
burntime = 5 ,
} )
end
2017-01-10 05:43:07 +00:00
2017-03-21 03:27:50 +00:00
-- Add entry aliases for the Help
if minetest.get_modpath ( " doc " ) then
doc.add_entry_alias ( " nodes " , " mesecons_button:button_wood_off " , " nodes " , " mesecons_button:button_wood_on " )
doc.add_entry_alias ( " nodes " , " mesecons_button:button_stone_off " , " nodes " , " mesecons_button:button_stone_on " )
end