init.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. -- MOVESTONE
  2. -- Non-sticky:
  3. -- Moves along mesecon lines
  4. -- Pushes all blocks in front of it
  5. --
  6. -- Sticky one
  7. -- Moves along mesecon lines
  8. -- Pushes all block in front of it
  9. -- Pull all blocks in its back
  10. function mesecon.get_movestone_direction(pos)
  11. local lpos
  12. local rules = {
  13. {x=0, y=1, z=-1},
  14. {x=0, y=0, z=-1},
  15. {x=0, y=-1, z=-1},
  16. {x=0, y=1, z=1},
  17. {x=0, y=-1, z=1},
  18. {x=0, y=0, z=1},
  19. {x=1, y=0, z=0},
  20. {x=1, y=1, z=0},
  21. {x=1, y=-1, z=0},
  22. {x=-1, y=1, z=0},
  23. {x=-1, y=-1, z=0},
  24. {x=-1, y=0, z=0}}
  25. lpos = {x=pos.x+1, y=pos.y, z=pos.z}
  26. for n = 1, 3 do
  27. if mesecon.is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  28. return {x=0, y=0, z=-1}
  29. end
  30. end
  31. lpos = {x = pos.x-1, y = pos.y, z = pos.z}
  32. for n=4, 6 do
  33. if mesecon.is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  34. return {x=0, y=0, z=1}
  35. end
  36. end
  37. lpos = {x = pos.x, y = pos.y, z = pos.z+1}
  38. for n=7, 9 do
  39. if mesecon.is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  40. return {x=-1, y=0, z=0}
  41. end
  42. end
  43. lpos = {x = pos.x, y = pos.y, z = pos.z-1}
  44. for n=10, 12 do
  45. if mesecon.is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  46. return {x=1, y=0, z=0}
  47. end
  48. end
  49. end
  50. function mesecon.register_movestone(name, def, is_sticky)
  51. local timer_interval = 1 / mesecon.setting("movestone_speed", 3)
  52. local name_active = name.."_active"
  53. local function movestone_move (pos)
  54. if minetest.get_node(pos).name ~= name_active then
  55. return
  56. end
  57. local direction = mesecon.get_movestone_direction(pos)
  58. if not direction then
  59. minetest.set_node(pos, {name = name})
  60. return
  61. end
  62. local frontpos = vector.add(pos, direction)
  63. local backpos = vector.subtract(pos, direction)
  64. -- ### Step 1: Push nodes in front ###
  65. local maxpush = mesecon.setting("movestone_max_push", 50)
  66. local maxpull = mesecon.setting("movestone_max_pull", 50)
  67. local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, maxpush)
  68. if success then
  69. mesecon.mvps_process_stack(stack)
  70. mesecon.mvps_move_objects(frontpos, direction, oldstack)
  71. -- Too large stack/stopper in the way: try again very soon
  72. else
  73. minetest.after(0.05, movestone_move, pos)
  74. return
  75. end
  76. -- ### Step 2: Move the movestone ###
  77. local node = minetest.get_node(pos)
  78. minetest.set_node(frontpos, node)
  79. minetest.remove_node(pos)
  80. mesecon.on_dignode(pos, node)
  81. mesecon.on_placenode(frontpos, node)
  82. minetest.after(timer_interval, movestone_move, frontpos)
  83. -- ### Step 3: If sticky, pull stack behind ###
  84. if is_sticky then
  85. mesecon.mvps_pull_all(backpos, direction, maxpull)
  86. end
  87. end
  88. def.mesecons = {effector = {
  89. action_on = function (pos)
  90. if minetest.get_node(pos).name ~= name_active then
  91. minetest.set_node(pos, {name = name_active})
  92. movestone_move(pos)
  93. end
  94. end,
  95. action_off = function (pos)
  96. minetest.set_node(pos, {name = name})
  97. end
  98. }}
  99. def.drop = name
  100. minetest.register_node(name, def)
  101. -- active node only
  102. local def_active = table.copy(def)
  103. def_active.groups.not_in_creative_inventory = 1
  104. minetest.register_node(name_active, def_active)
  105. end
  106. mesecon.register_movestone("mesecons_movestones:movestone", {
  107. tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
  108. groups = {cracky=3},
  109. description="Movestone",
  110. sounds = default.node_sound_stone_defaults()
  111. }, false)
  112. minetest.register_craft({
  113. output = "mesecons_movestones:movestone 2",
  114. recipe = {
  115. {"default:stone", "default:stone", "default:stone"},
  116. {"group:mesecon_conductor_craftable", "group:mesecon_conductor_craftable", "group:mesecon_conductor_craftable"},
  117. {"default:stone", "default:stone", "default:stone"},
  118. }
  119. })
  120. -- STICKY_MOVESTONE
  121. mesecon.register_movestone("mesecons_movestones:sticky_movestone", {
  122. tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_sticky_movestone.png", "jeija_sticky_movestone.png"},
  123. inventory_image = minetest.inventorycube("jeija_sticky_movestone.png", "jeija_movestone_side.png", "jeija_movestone_side.png"),
  124. groups = {cracky=3},
  125. description="Sticky Movestone",
  126. sounds = default.node_sound_stone_defaults(),
  127. }, true)
  128. minetest.register_craft({
  129. output = "mesecons_movestones:sticky_movestone 2",
  130. recipe = {
  131. {"mesecons_materials:glue", "mesecons_movestones:movestone", "mesecons_materials:glue"},
  132. }
  133. })
  134. -- Don't allow pushing movestones while they're active
  135. mesecon.register_mvps_stopper("mesecons_movestones:movestone_active")
  136. mesecon.register_mvps_stopper("mesecons_movestones:sticky_movestone_active")