init.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. local S = minetest.get_translator("homedecor_electrical")
  2. homedecor_electrical = {}
  3. function homedecor_electrical.toggle_switch(pos, node, clicker, itemstack, pointed_thing)
  4. if not clicker then return false end
  5. local playername = clicker:get_player_name()
  6. if minetest.is_protected(pos, playername) then
  7. minetest.record_protection_violation(pos, playername)
  8. return false
  9. end
  10. local sep = string.find(node.name, "_o", -5)
  11. local onoff = string.sub(node.name, sep + 1)
  12. local newname = string.sub(node.name, 1, sep - 1)..((onoff == "off") and "_on" or "_off")
  13. minetest.swap_node(pos, {name = newname, param2 = node.param2})
  14. return true
  15. end
  16. local on_rc
  17. if minetest.get_modpath("mesecons") then
  18. on_rc = function(pos, node, clicker, itemstack, pointed_thing)
  19. local t = homedecor_electrical.toggle_switch(pos, node, clicker, itemstack, pointed_thing)
  20. if not t then return end
  21. if string.find(node.name, "_on", -5) then
  22. mesecon.receptor_off(pos, mesecon.rules.buttonlike_get(node))
  23. else
  24. mesecon.receptor_on(pos, mesecon.rules.buttonlike_get(node))
  25. end
  26. end
  27. end
  28. homedecor.register("power_outlet", {
  29. description = S("Power Outlet"),
  30. tiles = {
  31. "homedecor_outlet_edges.png",
  32. "homedecor_outlet_edges.png",
  33. "homedecor_outlet_edges.png",
  34. "homedecor_outlet_edges.png",
  35. "homedecor_outlet_back.png",
  36. "homedecor_outlet_edges.png"
  37. },
  38. inventory_image = "homedecor_outlet_inv.png",
  39. node_box = {
  40. type = "fixed",
  41. fixed = {
  42. { -0.125, -0.3125, 0.4375, 0.125, 0, 0.5},
  43. }
  44. },
  45. selection_box = {
  46. type = "fixed",
  47. fixed = {
  48. { -0.1875, -0.375, 0.375, 0.1875, 0.0625, 0.5},
  49. }
  50. },
  51. groups = {cracky=3,dig_immediate=2},
  52. walkable = false
  53. })
  54. for _, onoff in ipairs ({"on", "off"}) do
  55. local switch_receptor
  56. if minetest.get_modpath("mesecons") then
  57. switch_receptor = {
  58. receptor = {
  59. state = mesecon.state[onoff],
  60. rules = mesecon.rules.buttonlike_get
  61. }
  62. }
  63. end
  64. local model = {
  65. { -0.125, -0.1875, 0.4375, 0.125, 0.125, 0.5 },
  66. { -0.03125, 0, 0.40625, 0.03125, 0.0625, 0.5 },
  67. }
  68. if onoff == "on" then
  69. model = {
  70. { -0.125, -0.1875, 0.4375, 0.125, 0.125, 0.5 },
  71. { -0.03125, -0.125, 0.40625, 0.03125, -0.0625, 0.5 },
  72. }
  73. end
  74. homedecor.register("light_switch_"..onoff, {
  75. description = S("Light switch"),
  76. tiles = {
  77. "homedecor_light_switch_edges.png",
  78. "homedecor_light_switch_edges.png",
  79. "homedecor_light_switch_edges.png",
  80. "homedecor_light_switch_edges.png",
  81. "homedecor_light_switch_back.png",
  82. "homedecor_light_switch_front_"..onoff..".png"
  83. },
  84. inventory_image = "homedecor_light_switch_inv.png",
  85. node_box = {
  86. type = "fixed",
  87. fixed = model
  88. },
  89. selection_box = {
  90. type = "fixed",
  91. fixed = {
  92. { -0.1875, -0.25, 0.375, 0.1875, 0.1875, 0.5 },
  93. }
  94. },
  95. groups = {
  96. cracky=3, dig_immediate=2, mesecon_needs_receiver=1,
  97. not_in_creative_inventory = (onoff == "on") and 1 or nil
  98. },
  99. walkable = false,
  100. drop = {
  101. items = {
  102. {items = {"homedecor:light_switch_off"}, inherit_color = true },
  103. }
  104. },
  105. mesecons = switch_receptor,
  106. on_rightclick = on_rc
  107. })
  108. end
  109. homedecor.register("doorbell", {
  110. tiles = { "homedecor_doorbell.png" },
  111. inventory_image = "homedecor_doorbell_inv.png",
  112. description = S("Doorbell"),
  113. groups = {snappy=3},
  114. walkable = false,
  115. node_box = {
  116. type = "fixed",
  117. fixed = {
  118. {-0.0625, 0, 0.46875, 0.0625, 0.1875, 0.5}, -- NodeBox1
  119. {-0.03125, 0.0625, 0.45, 0.03125, 0.125, 0.4675}, -- NodeBox2
  120. }
  121. },
  122. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  123. minetest.sound_play("homedecor_doorbell", {
  124. pos = pos,
  125. gain = 1.0,
  126. max_hear_distance = 15
  127. })
  128. end
  129. })
  130. -- crafting
  131. minetest.register_craft( {
  132. output = "homedecor:power_outlet",
  133. recipe = {
  134. {"basic_materials:plastic_sheet", "basic_materials:copper_strip"},
  135. {"basic_materials:plastic_sheet", ""},
  136. {"basic_materials:plastic_sheet", "basic_materials:copper_strip"}
  137. },
  138. })
  139. minetest.register_craft( {
  140. output = "homedecor:light_switch_off",
  141. recipe = {
  142. {"", "basic_materials:plastic_sheet", "basic_materials:copper_strip"},
  143. {"basic_materials:plastic_sheet", "basic_materials:plastic_sheet", "basic_materials:copper_strip"},
  144. {"", "basic_materials:plastic_sheet", "basic_materials:copper_strip"}
  145. },
  146. })
  147. minetest.register_craft( {
  148. output = "homedecor:doorbell",
  149. recipe = {
  150. { "homedecor:light_switch_off", "basic_materials:energy_crystal_simple", "homedecor:speaker_driver" }
  151. },
  152. })
  153. -- aliases
  154. minetest.register_alias("homedecor:light_switch", "homedecor:light_switch_on")