init.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. -- Function that get the input/output rules of the delayer
  2. local delayer_get_output_rules = function(node)
  3. local rules = {{x = 0, y = 0, z = 1}}
  4. for i = 0, node.param2 do
  5. rules = mesecon:rotate_rules_left(rules)
  6. end
  7. return rules
  8. end
  9. local delayer_get_input_rules = function(node)
  10. local rules = {{x = 0, y = 0, z = -1}}
  11. for i = 0, node.param2 do
  12. rules = mesecon:rotate_rules_left(rules)
  13. end
  14. return rules
  15. end
  16. -- Functions that are called after the delay time
  17. local delayer_turnon = function(params)
  18. local rules = delayer_get_output_rules(params.node)
  19. mesecon:receptor_on(params.pos, rules)
  20. end
  21. local delayer_turnoff = function(params)
  22. local rules = delayer_get_output_rules(params.node)
  23. mesecon:receptor_off(params.pos, rules)
  24. end
  25. local delayer_activate = function(pos, node)
  26. local def = minetest.registered_nodes[node.name]
  27. local time = def.delayer_time
  28. mesecon:swap_node(pos, def.delayer_onstate)
  29. minetest.after(time, delayer_turnon , {pos = pos, node = node})
  30. end
  31. local delayer_deactivate = function(pos, node)
  32. local def = minetest.registered_nodes[node.name]
  33. local time = def.delayer_time
  34. mesecon:swap_node(pos, def.delayer_offstate)
  35. minetest.after(time, delayer_turnoff, {pos = pos, node = node})
  36. end
  37. -- Register the 2 (states) x 4 (delay times) delayers
  38. for i = 1, 4 do
  39. local groups = {}
  40. if i == 1 then
  41. groups = {bendy=2,snappy=1,dig_immediate=2}
  42. else
  43. groups = {bendy=2,snappy=1,dig_immediate=2, not_in_creative_inventory=1}
  44. end
  45. local delaytime
  46. if i == 1 then delaytime = 0.1
  47. elseif i == 2 then delaytime = 0.3
  48. elseif i == 3 then delaytime = 0.5
  49. elseif i == 4 then delaytime = 1.0 end
  50. boxes = {{ -6/16, -8/16, -6/16, 6/16, -7/16, 6/16 }, -- the main slab
  51. { -2/16, -7/16, -4/16, 2/16, -26/64, -3/16 }, -- the jeweled "on" indicator
  52. { -3/16, -7/16, -3/16, 3/16, -26/64, -2/16 },
  53. { -4/16, -7/16, -2/16, 4/16, -26/64, 2/16 },
  54. { -3/16, -7/16, 2/16, 3/16, -26/64, 3/16 },
  55. { -2/16, -7/16, 3/16, 2/16, -26/64, 4/16 },
  56. { -6/16, -7/16, -6/16, -4/16, -27/64, -4/16 }, -- the timer indicator
  57. { -8/16, -8/16, -1/16, -6/16, -7/16, 1/16 }, -- the two wire stubs
  58. { 6/16, -8/16, -1/16, 8/16, -7/16, 1/16 }}
  59. minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), {
  60. description = "Delayer",
  61. drawtype = "nodebox",
  62. tiles = {
  63. "mesecons_delayer_off_"..tostring(i)..".png",
  64. "mesecons_delayer_bottom.png",
  65. "mesecons_delayer_ends_off.png",
  66. "mesecons_delayer_ends_off.png",
  67. "mesecons_delayer_sides_off.png",
  68. "mesecons_delayer_sides_off.png"
  69. },
  70. inventory_image = "mesecons_delayer_off_1.png",
  71. wield_image = "mesecons_delayer_off_1.png",
  72. walkable = true,
  73. selection_box = {
  74. type = "fixed",
  75. fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
  76. },
  77. node_box = {
  78. type = "fixed",
  79. fixed = boxes
  80. },
  81. groups = groups,
  82. paramtype = "light",
  83. paramtype2 = "facedir",
  84. sunlight_propagates = true,
  85. is_ground_content = true,
  86. drop = 'mesecons_delayer:delayer_off_1',
  87. on_punch = function (pos, node)
  88. if node.name=="mesecons_delayer:delayer_off_1" then
  89. mesecon:swap_node(pos,"mesecons_delayer:delayer_off_2")
  90. elseif node.name=="mesecons_delayer:delayer_off_2" then
  91. mesecon:swap_node(pos,"mesecons_delayer:delayer_off_3")
  92. elseif node.name=="mesecons_delayer:delayer_off_3" then
  93. mesecon:swap_node(pos,"mesecons_delayer:delayer_off_4")
  94. elseif node.name=="mesecons_delayer:delayer_off_4" then
  95. mesecon:swap_node(pos,"mesecons_delayer:delayer_off_1")
  96. end
  97. end,
  98. delayer_time = delaytime,
  99. delayer_onstate = "mesecons_delayer:delayer_on_"..tostring(i),
  100. mesecons = {
  101. receptor =
  102. {
  103. state = mesecon.state.off,
  104. rules = delayer_get_output_rules
  105. },
  106. effector =
  107. {
  108. rules = delayer_get_input_rules,
  109. action_on = delayer_activate
  110. }
  111. }
  112. })
  113. minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), {
  114. description = "You hacker you",
  115. drawtype = "nodebox",
  116. tiles = {
  117. "mesecons_delayer_on_"..tostring(i)..".png",
  118. "mesecons_delayer_bottom.png",
  119. "mesecons_delayer_ends_on.png",
  120. "mesecons_delayer_ends_on.png",
  121. "mesecons_delayer_sides_on.png",
  122. "mesecons_delayer_sides_on.png"
  123. },
  124. walkable = true,
  125. selection_box = {
  126. type = "fixed",
  127. fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
  128. },
  129. node_box = {
  130. type = "fixed",
  131. fixed = boxes
  132. },
  133. groups = {bendy = 2, snappy = 1, dig_immediate = 2, not_in_creative_inventory = 1},
  134. paramtype = "light",
  135. paramtype2 = "facedir",
  136. sunlight_propagates = true,
  137. is_ground_content = true,
  138. drop = 'mesecons_delayer:delayer_off_1',
  139. on_punch = function (pos, node)
  140. if node.name=="mesecons_delayer:delayer_on_1" then
  141. mesecon:swap_node(pos,"mesecons_delayer:delayer_on_2")
  142. elseif node.name=="mesecons_delayer:delayer_on_2" then
  143. mesecon:swap_node(pos,"mesecons_delayer:delayer_on_3")
  144. elseif node.name=="mesecons_delayer:delayer_on_3" then
  145. mesecon:swap_node(pos,"mesecons_delayer:delayer_on_4")
  146. elseif node.name=="mesecons_delayer:delayer_on_4" then
  147. mesecon:swap_node(pos,"mesecons_delayer:delayer_on_1")
  148. end
  149. end,
  150. delayer_time = delaytime,
  151. delayer_offstate = "mesecons_delayer:delayer_off_"..tostring(i),
  152. mesecons = {
  153. receptor =
  154. {
  155. state = mesecon.state.on,
  156. rules = delayer_get_output_rules
  157. },
  158. effector =
  159. {
  160. rules = delayer_get_input_rules,
  161. action_off = delayer_deactivate
  162. }
  163. }
  164. })
  165. end
  166. minetest.register_craft({
  167. output = "mesecons_delayer:delayer_off_1",
  168. recipe = {
  169. {"mesecons_torch:mesecon_torch_on", "group:mesecon_conductor_craftable", "mesecons_torch:mesecon_torch_on"},
  170. {"default:cobble","default:cobble", "default:cobble"},
  171. }
  172. })