init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 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, -.5-1/32, -.5 , 2/32, 0 , -.5002+3/32 }, -- the vertical wire bit
  5. { -2/32, -17/32 , -7/16+0.002 , 2/32, -14/32, 16/32+0.001 } -- the horizontal wire
  6. }
  7. local receiver_get_rules = function (node)
  8. local rules = { {x = 1, y = 0, z = 0},
  9. {x = -2, y = 0, z = 0}}
  10. if node.param2 == 2 then
  11. rules = mesecon:rotate_rules_left(rules)
  12. elseif node.param2 == 3 then
  13. rules = mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules))
  14. elseif node.param2 == 0 then
  15. rules = mesecon:rotate_rules_right(rules)
  16. end
  17. return rules
  18. end
  19. minetest.register_node("mesecons_receiver:receiver_on", {
  20. drawtype = "nodebox",
  21. tiles = {
  22. "receiver_top_on.png",
  23. "receiver_bottom_on.png",
  24. "receiver_lr_on.png",
  25. "receiver_lr_on.png",
  26. "receiver_fb_on.png",
  27. "receiver_fb_on.png",
  28. },
  29. paramtype = "light",
  30. paramtype2 = "facedir",
  31. sunlight_propagates = true,
  32. walkable = false,
  33. selection_box = {
  34. type = "fixed",
  35. fixed = { -3/16, -8/16, -8/16, 3/16, 3/16, 8/16 }
  36. },
  37. node_box = {
  38. type = "fixed",
  39. fixed = rcvboxes
  40. },
  41. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  42. drop = "mesecons:wire_00000000_off",
  43. mesecons = {conductor = {
  44. state = mesecon.state.on,
  45. rules = receiver_get_rules,
  46. offstate = "mesecons_receiver:receiver_off"
  47. }}
  48. })
  49. minetest.register_node("mesecons_receiver:receiver_off", {
  50. drawtype = "nodebox",
  51. description = "You hacker you",
  52. tiles = {
  53. "receiver_top_off.png",
  54. "receiver_bottom_off.png",
  55. "receiver_lr_off.png",
  56. "receiver_lr_off.png",
  57. "receiver_fb_off.png",
  58. "receiver_fb_off.png",
  59. },
  60. paramtype = "light",
  61. paramtype2 = "facedir",
  62. sunlight_propagates = true,
  63. walkable = false,
  64. selection_box = {
  65. type = "fixed",
  66. fixed = { -3/16, -8/16, -8/16, 3/16, 3/16, 8/16 }
  67. },
  68. node_box = {
  69. type = "fixed",
  70. fixed = rcvboxes
  71. },
  72. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  73. drop = "mesecons:wire_00000000_off",
  74. mesecons = {conductor = {
  75. state = mesecon.state.off,
  76. rules = receiver_get_rules,
  77. onstate = "mesecons_receiver:receiver_on"
  78. }}
  79. })
  80. mesecon:add_rules("receiver_pos", {{x = 2, y = 0, z = 0}})
  81. mesecon:add_rules("receiver_pos_all", {
  82. {x = 2, y = 0, z = 0},
  83. {x =-2, y = 0, z = 0},
  84. {x = 0, y = 0, z = 2},
  85. {x = 0, y = 0, z =-2}})
  86. function mesecon:receiver_get_pos_from_rcpt(pos, param2)
  87. local rules = mesecon:get_rules("receiver_pos")
  88. if param2 == nil then param2 = minetest.env:get_node(pos).param2 end
  89. if param2 == 2 then
  90. rules = mesecon:rotate_rules_left(rules)
  91. elseif param2 == 3 then
  92. rules = mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules))
  93. elseif param2 == 0 then
  94. rules = mesecon:rotate_rules_right(rules)
  95. end
  96. np = {
  97. x = pos.x + rules[1].x,
  98. y = pos.y + rules[1].y,
  99. z = pos.z + rules[1].z}
  100. return np
  101. end
  102. function mesecon:receiver_place(rcpt_pos)
  103. local node = minetest.env:get_node(rcpt_pos)
  104. local pos = mesecon:receiver_get_pos_from_rcpt(rcpt_pos, node.param2)
  105. local nn = minetest.env:get_node(pos)
  106. if string.find(nn.name, "mesecons:wire_") ~= nil then
  107. minetest.env:dig_node(pos)
  108. if mesecon:is_power_on(rcpt_pos) then
  109. minetest.env:add_node(pos, {name = "mesecons_receiver:receiver_on", param2 = node.param2})
  110. mesecon:receptor_on(pos, receiver_get_rules(node))
  111. else
  112. minetest.env:add_node(pos, {name = "mesecons_receiver:receiver_off", param2 = node.param2})
  113. end
  114. mesecon:update_autoconnect(pos)
  115. end
  116. end
  117. function mesecon:receiver_remove(rcpt_pos, dugnode)
  118. local pos = mesecon:receiver_get_pos_from_rcpt(rcpt_pos, dugnode.param2)
  119. local nn = minetest.env:get_node(pos)
  120. if string.find(nn.name, "mesecons_receiver:receiver_") ~=nil then
  121. minetest.env:dig_node(pos)
  122. local node = {name = "mesecons:wire_00000000_off"}
  123. minetest.env:add_node(pos, node)
  124. mesecon:update_autoconnect(pos)
  125. mesecon.on_placenode(pos, node)
  126. end
  127. end
  128. minetest.register_on_placenode(function (pos, node)
  129. if minetest.get_item_group(node.name, "mesecon_needs_receiver") == 1 then
  130. mesecon:receiver_place(pos)
  131. end
  132. end)
  133. minetest.register_on_dignode(function(pos, node)
  134. if minetest.get_item_group(node.name, "mesecon_needs_receiver") == 1 then
  135. mesecon:receiver_remove(pos, node)
  136. end
  137. end)
  138. minetest.register_on_placenode(function (pos, node)
  139. if string.find(node.name, "mesecons:wire_") ~=nil then
  140. rules = mesecon:get_rules("receiver_pos_all")
  141. local i = 1
  142. while rules[i] ~= nil do
  143. np = {
  144. x = pos.x + rules[i].x,
  145. y = pos.y + rules[i].y,
  146. z = pos.z + rules[i].z}
  147. if minetest.get_item_group(minetest.env:get_node(np).name, "mesecon_needs_receiver") == 1 then
  148. mesecon:receiver_place(np)
  149. end
  150. i = i + 1
  151. end
  152. end
  153. end)