init.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. -- chat-controllable switch node for mesecons
  2. -- Licenced under LGPLv3
  3. -- heavily influenced by:
  4. -- - Minecraft mod Cyclic's password trigger (idea)
  5. -- - mesecons_detector:object_detector_* (code)
  6. -- - h-v-smacker's technic fork's Mk2 prospector (code)
  7. local range = tonumber(minetest.settings:get("mesecons_microphone_range") or 50)
  8. local default_hotword = tostring(minetest.settings:get("mesecons_microphone_default_hotword") or "microphone")
  9. --register nodes
  10. local base_texture = "default_steel_block.png"
  11. local mic_textures = {
  12. ['on'] = base_texture.."^[combine:16x16:0,0=mesecons_microphone_mic_on.png",
  13. ['off'] = base_texture.."^[combine:16x16:0,0=mesecons_microphone_mic_off.png"
  14. }
  15. for _,node_state in pairs({"off","on"}) do
  16. minetest.register_node("mesecons_microphone:microphone_"..node_state, {
  17. description = "Microphone",
  18. tiles = {base_texture,base_texture,mic_textures[node_state],mic_textures[node_state],mic_textures[node_state],mic_textures[node_state]},
  19. paramtype = "light",
  20. is_ground_content = false,
  21. walkable = true,
  22. groups = {cracky=3,not_in_creative_inventory=(node_state=="on" and 1 or 0),mesecons_microphone=1},
  23. drop = "mesecons_microphone:microphone_off",
  24. mesecons = {receptor = {
  25. state = mesecon.state[node_state],
  26. rules = mesecon.rules.pplate
  27. }},
  28. after_place_node = function(pos,placer)
  29. if placer and placer:is_player() then
  30. local meta = minetest.get_meta(pos)
  31. meta:set_string("owner",placer:get_player_name())
  32. meta:set_string("mesecons_microphone_hotword",default_hotword)
  33. meta:set_string("formspec",""..
  34. "formspec_version[4]"..
  35. "size[9,3]"..
  36. "field[0.3,1;5,1;hotword;Set A Hotword;${mesecons_microphone_hotword}]"..
  37. "button_exit[5.5,1;3,1;save_hotword;Save]")
  38. end
  39. end,
  40. on_receive_fields = function(pos, formname, fields, sender)
  41. local name = sender:get_player_name()
  42. if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, {protection_bypass=true}) then
  43. minetest.record_protection_violation(pos, name)
  44. return
  45. end
  46. local meta = minetest.get_meta(pos)
  47. if fields.save_hotword then
  48. meta:set_string("mesecons_microphone_hotword",fields.hotword)
  49. minetest.chat_send_player(name,"Microphone updated to: "..fields.hotword)
  50. end
  51. end,
  52. sounds = default.node_sound_stone_defaults(),
  53. on_blast = mesecon.on_blastnode
  54. })
  55. end
  56. local function swap_microphone(pos)
  57. local node = minetest.get_node(pos)
  58. if not node then return false end
  59. if node.name=="mesecons_microphone:microphone_off" then
  60. minetest.swap_node(pos,{name="mesecons_microphone:microphone_on"})
  61. mesecon.receptor_on(pos,mesecon.rules.pplate)
  62. elseif node.name=="mesecons_microphone:microphone_on" then
  63. minetest.swap_node(pos,{name="mesecons_microphone:microphone_off"})
  64. mesecon.receptor_off(pos,mesecon.rules.pplate)
  65. end
  66. return true
  67. end
  68. minetest.register_on_chat_message(function(name, message)
  69. if not name or not message then return end
  70. local player = minetest.get_player_by_name(name)
  71. if not player then return end
  72. local pos = player:get_pos()
  73. local min_pos = {x=pos.x-range, y=pos.y-range, z=pos.z-range}
  74. local max_pos = {x=pos.x+range, y=pos.y+range, z=pos.z+range}
  75. local nodes = minetest.find_nodes_in_area(min_pos,max_pos,"group:mesecons_microphone")
  76. if #nodes == 0 then return false end
  77. local activated = false
  78. for _,pos in pairs(nodes) do
  79. local meta = minetest.get_meta(pos)
  80. if meta:get_string("owner") == name and meta:get_string("mesecons_microphone_hotword") == minetest.strip_colors(message) then
  81. minetest.log("action",name.." activated a microphone at "..minetest.pos_to_string(pos).." with the phrase: "..message)
  82. activated = true
  83. if not swap_microphone(pos) then
  84. minetest.log("error",name.." tried to swap a non-microphone node: "..minetest.pos_to_string(pos))
  85. end
  86. end
  87. end
  88. return activated
  89. end)
  90. minetest.register_craft({
  91. output = 'mesecons_microphone:microphone_off',
  92. recipe = {
  93. {"default:steel_ingot", "wool:black", "default:steel_ingot"},
  94. {"default:steel_ingot", "wool:black", "default:steel_ingot"},
  95. {"default:steel_ingot", "group:mesecon_conductor_craftable", "default:steel_ingot"},
  96. },
  97. })
  98. print("[OK] Mesecon Microphone")