init.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. getactivated = 0
  12. local lpos
  13. local getactivated = 0
  14. local rules = {
  15. {x=0, y=1, z=-1},
  16. {x=0, y=0, z=-1},
  17. {x=0, y=-1, z=-1},
  18. {x=0, y=1, z=1},
  19. {x=0, y=-1, z=1},
  20. {x=0, y=0, z=1},
  21. {x=1, y=0, z=0},
  22. {x=1, y=1, z=0},
  23. {x=1, y=-1, z=0},
  24. {x=-1, y=1, z=0},
  25. {x=-1, y=-1, z=0},
  26. {x=-1, y=0, z=0}}
  27. lpos = {x=pos.x+1, y=pos.y, z=pos.z}
  28. for n = 1, 3 do
  29. if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  30. return {x=0, y=0, z=-1}
  31. end
  32. end
  33. lpos = {x = pos.x-1, y = pos.y, z = pos.z}
  34. for n=4, 6 do
  35. if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  36. return {x=0, y=0, z=1}
  37. end
  38. end
  39. lpos = {x = pos.x, y = pos.y, z = pos.z+1}
  40. for n=7, 9 do
  41. if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  42. return {x=-1, y=0, z=0}
  43. end
  44. end
  45. lpos = {x = pos.x, y = pos.y, z = pos.z-1}
  46. for n=10, 12 do
  47. if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
  48. return {x=1, y=0, z=0}
  49. end
  50. end
  51. end
  52. minetest.register_node("mesecons_movestones:movestone", {
  53. 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"},
  54. paramtype2 = "facedir",
  55. legacy_facedir_simple = true,
  56. groups = {cracky=3},
  57. description="Movestone",
  58. sounds = default.node_sound_stone_defaults(),
  59. mesecons = {effector = {
  60. action_on = function (pos, node)
  61. local direction=mesecon:get_movestone_direction(pos)
  62. if not direction then return end
  63. minetest.env:remove_node(pos)
  64. mesecon:update_autoconnect(pos)
  65. minetest.env:add_entity(pos, "mesecons_movestones:movestone_entity")
  66. end
  67. }}
  68. })
  69. minetest.register_entity("mesecons_movestones:movestone_entity", {
  70. physical = false,
  71. visual = "sprite",
  72. textures = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
  73. collisionbox = {-0.5,-0.5,-0.5, 0.5, 0.5, 0.5},
  74. visual = "cube",
  75. lastdir = {x=0, y=0, z=0},
  76. on_punch = function(self, hitter)
  77. self.object:remove()
  78. hitter:get_inventory():add_item("main", "mesecons_movestones:movestone")
  79. end,
  80. on_step = function(self, dtime)
  81. local pos = self.object:getpos()
  82. pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)
  83. local direction = mesecon:get_movestone_direction(pos)
  84. if not direction then -- no mesecon power
  85. local name = minetest.env:get_node(pos).name
  86. if name ~= "air" and name ~= "ignore"
  87. and minetest.registered_nodes[name].liquidtype == "none" then
  88. mesecon:mvps_push(pos, self.lastdir, MOVESTONE_MAXIMUM_PUSH)
  89. end
  90. minetest.env:add_node(pos, {name="mesecons_movestones:movestone"})
  91. self.object:remove()
  92. return
  93. end
  94. local success, stack, oldstack =
  95. mesecon:mvps_push(pos, direction, MOVESTONE_MAXIMUM_PUSH)
  96. if not success then -- Too large stack/stopper in the way
  97. minetest.env:add_node(pos, {name="mesecons_movestones:movestone"})
  98. self.object:remove()
  99. return
  100. else
  101. mesecon:mvps_process_stack (stack)
  102. mesecon:mvps_move_objects (pos, direction, oldstack)
  103. self.lastdir = direction
  104. end
  105. self.object:setvelocity({x=direction.x*2, y=direction.y*2, z=direction.z*2})
  106. end,
  107. })
  108. minetest.register_craft({
  109. output = '"mesecons_movestones:movestone" 2',
  110. recipe = {
  111. {'"default:stone"', '"default:stone"', '"default:stone"'},
  112. {'"group:mesecon_conductor_craftable"', '"group:mesecon_conductor_craftable"', '"group:mesecon_conductor_craftable"'},
  113. {'"default:stone"', '"default:stone"', '"default:stone"'},
  114. }
  115. })
  116. -- STICKY_MOVESTONE
  117. minetest.register_node("mesecons_movestones:sticky_movestone", {
  118. 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"},
  119. inventory_image = minetest.inventorycube("jeija_sticky_movestone.png", "jeija_movestone_side.png", "jeija_movestone_side.png"),
  120. paramtype2 = "facedir",
  121. legacy_facedir_simple = true,
  122. groups = {cracky=3},
  123. description="Sticky Movestone",
  124. sounds = default.node_sound_stone_defaults(),
  125. mesecons = {effector = {
  126. action_on = function (pos, node)
  127. local direction=mesecon:get_movestone_direction(pos)
  128. if not direction then return end
  129. minetest.env:remove_node(pos)
  130. mesecon:update_autoconnect(pos)
  131. minetest.env:add_entity(pos, "mesecons_movestones:sticky_movestone_entity")
  132. end
  133. }}
  134. })
  135. minetest.register_craft({
  136. output = '"mesecons_movestones:sticky_movestone" 2',
  137. recipe = {
  138. {'"mesecons_materials:glue"', '"mesecons_movestones:movestone"', '"mesecons_materials:glue"'},
  139. }
  140. })
  141. minetest.register_entity("mesecons_movestones:sticky_movestone_entity", {
  142. physical = false,
  143. visual = "sprite",
  144. textures = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_sticky_movestone.png", "jeija_sticky_movestone.png"},
  145. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  146. visual = "cube",
  147. lastdir = {x=0, y=0, z=0},
  148. on_punch = function(self, hitter)
  149. self.object:remove()
  150. hitter:get_inventory():add_item("main", 'mesecons_movestones:sticky_movestone')
  151. end,
  152. on_step = function(self, dtime)
  153. local pos = self.object:getpos()
  154. pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)
  155. local direction = mesecon:get_movestone_direction(pos)
  156. if not direction then -- no mesecon power
  157. local name = minetest.env:get_node(pos).name
  158. if name ~= "air" and name ~= "ignore"
  159. and minetest.registered_nodes[name].liquidtype == "none" then
  160. mesecon:mvps_push(pos, self.lastdir, MOVESTONE_MAXIMUM_PUSH)
  161. end
  162. minetest.env:add_node(pos, {name="mesecons_movestones:sticky_movestone"})
  163. self.object:remove()
  164. return
  165. end
  166. local success, stack, oldstack =
  167. mesecon:mvps_push(pos, direction, MOVESTONE_MAXIMUM_PUSH)
  168. if not success then -- Too large stack/stopper in the way
  169. minetest.env:add_node(pos, {name="mesecons_movestones:sticky_movestone"})
  170. self.object:remove()
  171. return
  172. else
  173. mesecon:mvps_process_stack (stack)
  174. mesecon:mvps_move_objects (pos, direction, oldstack)
  175. self.lastdir = direction
  176. end
  177. self.object:setvelocity({x=direction.x*2, y=direction.y*2, z=direction.z*2})
  178. --STICKY
  179. mesecon:mvps_pull_all(pos, direction)
  180. end,
  181. })
  182. mesecon:register_mvps_unmov("mesecons_movestones:movestone_entity")
  183. mesecon:register_mvps_unmov("mesecons_movestones:sticky_movestone_entity")