init.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. local modname = minetest.get_current_modname()
  2. local S = minetest.get_translator(modname)
  3. local REACTION_DELAY = 0.5
  4. local function rotate_rules(rules, param2)
  5. if param2 == 0
  6. then
  7. return rules
  8. elseif param2 == 1
  9. then
  10. return mesecon.rotate_rules_left(rules)
  11. elseif param2 == 2
  12. then
  13. return mesecon.rotate_rules_left(mesecon.rotate_rules_left(rules))
  14. elseif param2 == 3
  15. then
  16. return mesecon.rotate_rules_right(rules)
  17. end
  18. end
  19. local rules_in =
  20. {
  21. {x = 0, y = 0, z = -1}
  22. }
  23. local function get_rules_in(node)
  24. return rotate_rules(rules_in, node.param2)
  25. end
  26. local rules_out =
  27. {
  28. {x = 0, y = 0, z = 1},
  29. {x = 0, y = -1, z = 0},
  30. {x = 0, y = 1, z = 0},
  31. {x = -1, y = 0, z = 0},
  32. {x = 1, y = 0, z = 0},
  33. }
  34. local function get_rules_out(node)
  35. return rotate_rules(rules_out, node.param2)
  36. end
  37. local function on_timer(pos)
  38. local meta = minetest.get_meta(pos)
  39. local node = minetest.get_node(pos)
  40. if meta:get_int("powered") == 0
  41. then
  42. minetest.swap_node(pos, {name = modname .. ":knot_on", param2 = node.param2})
  43. mesecon.receptor_on(pos, get_rules_out(node))
  44. else
  45. minetest.swap_node(pos, {name = modname .. ":knot_off", param2 = node.param2})
  46. mesecon.receptor_off(pos, get_rules_out(node))
  47. end
  48. end
  49. local effector =
  50. {
  51. rules = get_rules_in,
  52. action_on = function(pos, node)
  53. local meta = minetest.get_meta(pos)
  54. meta:set_int("powered", 1)
  55. local timer = minetest.get_node_timer(pos)
  56. timer:start(REACTION_DELAY)
  57. end,
  58. action_off = function(pos, node)
  59. local meta = minetest.get_meta(pos)
  60. meta:set_int("powered", 0)
  61. local timer = minetest.get_node_timer(pos)
  62. timer:start(REACTION_DELAY)
  63. end,
  64. }
  65. local node_box =
  66. {
  67. type = "fixed",
  68. fixed =
  69. {
  70. {-0.125, -0.5, -0.5, 0.125, -0.375, -0.125},
  71. {-3/16, -0.5, -0.125, 3/16, -0.25, 0.25},
  72. }
  73. }
  74. minetest.register_node(modname .. ":knot_off",
  75. {
  76. description = S("Mesecon knot"),
  77. tiles = {"mesecons_wire_off.png"},
  78. paramtype = "light",
  79. paramtype2 = "facedir",
  80. sunlight_propagates = true,
  81. groups = {dig_immediate = 3},
  82. drawtype = "nodebox",
  83. node_box = node_box,
  84. on_timer = on_timer,
  85. mesecons =
  86. {
  87. receptor =
  88. {
  89. state = mesecon.state.off,
  90. rules = get_rules_out,
  91. },
  92. effector = effector,
  93. },
  94. drop = modname .. ":knot_off",
  95. })
  96. minetest.register_node(modname .. ":knot_on",
  97. {
  98. description = S("Mesecon knot"),
  99. tiles = {"mesecons_wire_on.png"},
  100. paramtype = "light",
  101. paramtype2 = "facedir",
  102. sunlight_propagates = true,
  103. groups = {dig_immediate = 3},
  104. drawtype = "nodebox",
  105. node_box = node_box,
  106. on_timer = on_timer,
  107. mesecons =
  108. {
  109. receptor =
  110. {
  111. state = mesecon.state.on,
  112. rules = get_rules_out,
  113. },
  114. effector = effector,
  115. },
  116. })