init.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. local rcvboxes = {
  2. { -3/16, -3/16, -8/16 , 3/16, 3/16 , -13/32 }, -- the smaller bump
  3. { -1/32, -1/32, -3/2 , 1/32, 1/32 , -1/2 }, -- the wire through the block
  4. { -2/32, -1/2 , -.5 , 2/32, 0 , -.5002+3/32 }, -- the vertical wire bit
  5. { -2/32, -1/2 , -7/16+0.002 , 2/32, -14/32, 16/32+0.001 } -- the horizontal wire
  6. }
  7. local down_rcvboxes = {
  8. {-6/16, -8/16, -6/16, 6/16, -7/16, 6/16}, -- Top plate
  9. {-2/16, -6/16, -2/16, 2/16, -7/16, 2/16}, -- Bump
  10. {-1/16, -8/16, -1/16, 1/16, -24/16, 1/16}, -- Wire through the block
  11. {-1/16, -8/16, 6/16, 1/16, -7/16, 8/16}, -- Plate extension (North)
  12. {-1/16, -8/16, -6/16, 1/16, -7/16, -8/16}, -- Plate extension (South)
  13. {-8/16, -8/16, 1/16, -6/16, -7/16, -1/16}, -- Plate extension (West)
  14. {6/16, -8/16, 1/16, 8/16, -7/16, -1/16}, -- Plate extension (East)
  15. }
  16. local up_rcvboxes = {
  17. {-6/16, -8/16, -6/16, 6/16, -7/16, 6/16}, -- Top plate
  18. {-2/16, -6/16, -2/16, 2/16, -7/16, 2/16}, -- Bump
  19. {-1/16, -6/16, -1/16, 1/16, 24/16, 1/16}, -- Wire through the block
  20. {-1/16, -8/16, 6/16, 1/16, -7/16, 8/16}, -- Plate extension (North)
  21. {-1/16, -8/16, -6/16, 1/16, -7/16, -8/16}, -- Plate extension (South)
  22. {-8/16, -8/16, 1/16, -6/16, -7/16, -1/16}, -- Plate extension (West)
  23. {6/16, -8/16, 1/16, 8/16, -7/16, -1/16}, -- Plate extension (East)
  24. }
  25. local receiver_get_rules = function (node)
  26. local rules = { {x = 1, y = 0, z = 0},
  27. {x = -2, y = 0, z = 0}}
  28. if node.param2 == 2 then
  29. rules = mesecon.rotate_rules_left(rules)
  30. elseif node.param2 == 3 then
  31. rules = mesecon.rotate_rules_right(mesecon.rotate_rules_right(rules))
  32. elseif node.param2 == 0 then
  33. rules = mesecon.rotate_rules_right(rules)
  34. end
  35. return rules
  36. end
  37. mesecon.register_node("mesecons_receiver:receiver", {
  38. drawtype = "nodebox",
  39. paramtype = "light",
  40. paramtype2 = "facedir",
  41. is_ground_content = false,
  42. sunlight_propagates = true,
  43. walkable = false,
  44. on_rotate = false,
  45. selection_box = {
  46. type = "fixed",
  47. fixed = { -3/16, -8/16, -8/16, 3/16, 3/16, 8/16 }
  48. },
  49. node_box = {
  50. type = "fixed",
  51. fixed = rcvboxes
  52. },
  53. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  54. drop = "mesecons:wire_00000000_off",
  55. sounds = default.node_sound_defaults(),
  56. }, {
  57. tiles = {
  58. "receiver_top_off.png",
  59. "receiver_bottom_off.png",
  60. "receiver_lr_off.png",
  61. "receiver_lr_off.png",
  62. "receiver_fb_off.png",
  63. "receiver_fb_off.png",
  64. },
  65. mesecons = {conductor = {
  66. state = mesecon.state.off,
  67. rules = receiver_get_rules,
  68. onstate = "mesecons_receiver:receiver_on"
  69. }}
  70. }, {
  71. tiles = {
  72. "receiver_top_on.png",
  73. "receiver_bottom_on.png",
  74. "receiver_lr_on.png",
  75. "receiver_lr_on.png",
  76. "receiver_fb_on.png",
  77. "receiver_fb_on.png",
  78. },
  79. mesecons = {conductor = {
  80. state = mesecon.state.on,
  81. rules = receiver_get_rules,
  82. offstate = "mesecons_receiver:receiver_off"
  83. }}
  84. })
  85. mesecon.register_node("mesecons_receiver:receiver_up", {
  86. drawtype = "nodebox",
  87. paramtype = "light",
  88. paramtype2 = "facedir",
  89. is_ground_content = false,
  90. sunlight_propagates = true,
  91. walkable = false,
  92. on_rotate = false,
  93. selection_box = {
  94. type = "fixed",
  95. fixed = up_rcvboxes
  96. },
  97. node_box = {
  98. type = "fixed",
  99. fixed = up_rcvboxes
  100. },
  101. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  102. drop = "mesecons:wire_00000000_off",
  103. sounds = default.node_sound_defaults(),
  104. }, {
  105. tiles = {"mesecons_wire_off.png"},
  106. mesecons = {conductor = {
  107. state = mesecon.state.off,
  108. rules = {{x=1, y=0, z=0},
  109. {x=-1, y=0, z=0},
  110. {x=0, y=0, z=1},
  111. {x=0, y=0, z=-1},
  112. {x=0, y=1, z=0},
  113. {x=0, y=2, z=0}},
  114. onstate = "mesecons_receiver:receiver_up_on"
  115. }}
  116. }, {
  117. tiles = {"mesecons_wire_on.png"},
  118. mesecons = {conductor = {
  119. state = mesecon.state.on,
  120. rules = {{x=1, y=0, z=0},
  121. {x=-1, y=0, z=0},
  122. {x=0, y=0, z=1},
  123. {x=0, y=0, z=-1},
  124. {x=0, y=1, z=0},
  125. {x=0, y=2, z=0}},
  126. offstate = "mesecons_receiver:receiver_up_off"
  127. }}
  128. })
  129. mesecon.register_node("mesecons_receiver:receiver_down", {
  130. drawtype = "nodebox",
  131. paramtype = "light",
  132. paramtype2 = "facedir",
  133. is_ground_content = false,
  134. sunlight_propagates = true,
  135. walkable = false,
  136. on_rotate = false,
  137. selection_box = {
  138. type = "fixed",
  139. fixed = down_rcvboxes
  140. },
  141. node_box = {
  142. type = "fixed",
  143. fixed = down_rcvboxes
  144. },
  145. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  146. drop = "mesecons:wire_00000000_off",
  147. sounds = default.node_sound_defaults(),
  148. }, {
  149. tiles = {"mesecons_wire_off.png"},
  150. mesecons = {conductor = {
  151. state = mesecon.state.off,
  152. rules = {{x=1,y=0, z=0},
  153. {x=-1,y=0, z=0},
  154. {x=0, y=0, z=1},
  155. {x=0, y=0, z=-1},
  156. {x=0, y=-2,z=0}},
  157. onstate = "mesecons_receiver:receiver_down_on"
  158. }}
  159. }, {
  160. tiles = {"mesecons_wire_on.png"},
  161. mesecons = {conductor = {
  162. state = mesecon.state.on,
  163. rules = {{x=1,y=0, z=0},
  164. {x=-1,y=0, z=0},
  165. {x=0, y=0, z=1},
  166. {x=0, y=0, z=-1},
  167. {x=0, y=-2,z=0}},
  168. offstate = "mesecons_receiver:receiver_down_off"
  169. }}
  170. })
  171. function mesecon.receiver_get_pos_from_rcpt(pos, param2)
  172. local rules = {{x = 2, y = 0, z = 0}}
  173. if param2 == nil then param2 = minetest.get_node(pos).param2 end
  174. local rcvtype = "mesecons_receiver:receiver_off"
  175. local dir = minetest.facedir_to_dir(param2)
  176. if dir.x == 1 then
  177. -- No action needed
  178. elseif dir.z == -1 then
  179. rules = mesecon.rotate_rules_left(rules)
  180. elseif dir.x == -1 then
  181. rules = mesecon.rotate_rules_right(mesecon.rotate_rules_right(rules))
  182. elseif dir.z == 1 then
  183. rules = mesecon.rotate_rules_right(rules)
  184. elseif dir.y == -1 then
  185. rules = mesecon.rotate_rules_up(rules)
  186. rcvtype = "mesecons_receiver:receiver_up_off"
  187. elseif dir.y == 1 then
  188. rules = mesecon.rotate_rules_down(rules)
  189. rcvtype = "mesecons_receiver:receiver_down_off"
  190. end
  191. local np = { x = pos.x + rules[1].x,
  192. y = pos.y + rules[1].y,
  193. z = pos.z + rules[1].z}
  194. return np, rcvtype
  195. end
  196. function mesecon.receiver_place(rcpt_pos)
  197. local node = minetest.get_node(rcpt_pos)
  198. local pos, rcvtype = mesecon.receiver_get_pos_from_rcpt(rcpt_pos, node.param2)
  199. local nn = minetest.get_node(pos)
  200. local param2 = minetest.dir_to_facedir(minetest.facedir_to_dir(node.param2))
  201. if string.find(nn.name, "mesecons:wire_") ~= nil then
  202. minetest.set_node(pos, {name = rcvtype, param2 = param2})
  203. mesecon.on_placenode(pos, nn)
  204. end
  205. end
  206. function mesecon.receiver_remove(rcpt_pos, dugnode)
  207. local pos = mesecon.receiver_get_pos_from_rcpt(rcpt_pos, dugnode.param2)
  208. local nn = minetest.get_node(pos)
  209. if string.find(nn.name, "mesecons_receiver:receiver_") ~= nil then
  210. local node = {name = "mesecons:wire_00000000_off"}
  211. minetest.set_node(pos, node)
  212. mesecon.on_placenode(pos, node)
  213. end
  214. end
  215. minetest.register_on_placenode(function (pos, node)
  216. if minetest.get_item_group(node.name, "mesecon_needs_receiver") == 1 then
  217. mesecon.receiver_place(pos)
  218. end
  219. end)
  220. minetest.register_on_dignode(function(pos, node)
  221. if minetest.get_item_group(node.name, "mesecon_needs_receiver") == 1 then
  222. mesecon.receiver_remove(pos, node)
  223. end
  224. end)
  225. minetest.register_on_placenode(function (pos, node)
  226. if string.find(node.name, "mesecons:wire_") ~= nil then
  227. local rules = { {x = 2, y = 0, z = 0},
  228. {x =-2, y = 0, z = 0},
  229. {x = 0, y = 0, z = 2},
  230. {x = 0, y = 0, z =-2},
  231. {x = 0, y = 2, z = 0},
  232. {x = 0, y = -2, z = 0}}
  233. local i = 1
  234. while rules[i] ~= nil do
  235. local np = { x = pos.x + rules[i].x,
  236. y = pos.y + rules[i].y,
  237. z = pos.z + rules[i].z}
  238. if minetest.get_item_group(minetest.get_node(np).name, "mesecon_needs_receiver") == 1 then
  239. mesecon.receiver_place(np)
  240. end
  241. i = i + 1
  242. end
  243. end
  244. end)
  245. function mesecon.buttonlike_onrotate(pos, node)
  246. minetest.after(0, mesecon.receiver_remove, pos, node)
  247. minetest.after(0, mesecon.receiver_place, pos)
  248. end