init.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. --register stoppers for movestones/pistons
  2. mesecon.mvps_stoppers = {}
  3. mesecon.mvps_unmov = {}
  4. function mesecon:is_mvps_stopper(node, pushdir, stack, stackid)
  5. local get_stopper = mesecon.mvps_stoppers[node.name]
  6. if type (get_stopper) == "function" then
  7. get_stopper = get_stopper(node, pushdir, stack, stackid)
  8. end
  9. return get_stopper
  10. end
  11. function mesecon:register_mvps_stopper(nodename, get_stopper)
  12. if get_stopper == nil then
  13. get_stopper = true
  14. end
  15. mesecon.mvps_stoppers[nodename] = get_stopper
  16. end
  17. -- Objects that cannot be moved (e.g. movestones)
  18. function mesecon:register_mvps_unmov(objectname)
  19. mesecon.mvps_unmov[objectname] = true;
  20. end
  21. function mesecon:is_mvps_unmov(objectname)
  22. return mesecon.mvps_unmov[objectname]
  23. end
  24. function mesecon:mvps_process_stack(stack)
  25. -- update mesecons for placed nodes ( has to be done after all nodes have been added )
  26. for _, n in ipairs(stack) do
  27. nodeupdate(n.pos)
  28. mesecon.on_placenode(n.pos, minetest.env:get_node(n.pos))
  29. mesecon:update_autoconnect(n.pos)
  30. end
  31. end
  32. function mesecon:mvps_get_stack(pos, dir, maximum)
  33. -- determine the number of nodes to be pushed
  34. local np = {x = pos.x, y = pos.y, z = pos.z}
  35. local nodes = {}
  36. while true do
  37. local nn = minetest.env:get_node_or_nil(np)
  38. if not nn or #nodes > maximum then
  39. -- don't push at all, something is in the way (unloaded map or too many nodes)
  40. return nil
  41. end
  42. if nn.name == "air"
  43. or minetest.registered_nodes[nn.name].liquidtype ~= "none" then --is liquid
  44. break
  45. end
  46. table.insert (nodes, {node = nn, pos = np})
  47. np = mesecon:addPosRule(np, dir)
  48. end
  49. return nodes
  50. end
  51. function mesecon:mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: direction of push; maximum: maximum nodes to be pushed
  52. local nodes = mesecon:mvps_get_stack(pos, dir, maximum)
  53. if not nodes then return end
  54. -- determine if one of the nodes blocks the push
  55. for id, n in ipairs(nodes) do
  56. if mesecon:is_mvps_stopper(n.node, dir, nodes, id) then
  57. return
  58. end
  59. end
  60. -- remove all nodes
  61. for _, n in ipairs(nodes) do
  62. n.meta = minetest.env:get_meta(n.pos):to_table()
  63. minetest.env:remove_node(n.pos)
  64. end
  65. -- update mesecons for removed nodes ( has to be done after all nodes have been removed )
  66. for _, n in ipairs(nodes) do
  67. mesecon.on_dignode(n.pos, n.node)
  68. mesecon:update_autoconnect(n.pos)
  69. end
  70. -- add nodes
  71. for _, n in ipairs(nodes) do
  72. np = mesecon:addPosRule(n.pos, dir)
  73. minetest.env:add_node(np, n.node)
  74. minetest.env:get_meta(np):from_table(n.meta)
  75. end
  76. local oldstack = mesecon:tablecopy(nodes)
  77. for i in ipairs(nodes) do
  78. nodes[i].pos = mesecon:addPosRule(nodes[i].pos, dir)
  79. end
  80. for _, n in ipairs(nodes) do
  81. mesecon.on_placenode(n.pos, n.node)
  82. mesecon:update_autoconnect(n.pos)
  83. end
  84. return true, nodes, oldstack
  85. end
  86. function mesecon:mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: direction of pull (matches push direction for sticky pistons)
  87. np = mesecon:addPosRule(pos, dir)
  88. nn = minetest.env:get_node(np)
  89. if minetest.registered_nodes[nn.name].liquidtype == "none"
  90. and not mesecon:is_mvps_stopper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then
  91. local meta = minetest.env:get_meta(np):to_table()
  92. minetest.env:remove_node(np)
  93. minetest.env:add_node(pos, nn)
  94. minetest.env:get_meta(pos):from_table(meta)
  95. nodeupdate(np)
  96. nodeupdate(pos)
  97. mesecon.on_dignode(np, nn)
  98. mesecon:update_autoconnect(np)
  99. mesecon:update_autoconnect(pos)
  100. mesecon.on_placenode(pos, nn)
  101. end
  102. return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
  103. end
  104. function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: direction of pull
  105. local lpos = {x=pos.x-direction.x, y=pos.y-direction.y, z=pos.z-direction.z} -- 1 away
  106. local lnode = minetest.env:get_node(lpos)
  107. local lpos2 = {x=pos.x-direction.x*2, y=pos.y-direction.y*2, z=pos.z-direction.z*2} -- 2 away
  108. local lnode2 = minetest.env:get_node(lpos2)
  109. if lnode.name ~= "ignore" and lnode.name ~= "air" and minetest.registered_nodes[lnode.name].liquidtype == "none" then return end
  110. if lnode2.name == "ignore" or lnode2.name == "air" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") then return end
  111. local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z}
  112. repeat
  113. lnode2 = minetest.env:get_node(lpos2)
  114. minetest.env:add_node(oldpos, {name=lnode2.name})
  115. nodeupdate(oldpos)
  116. oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z}
  117. lpos2.x = lpos2.x-direction.x
  118. lpos2.y = lpos2.y-direction.y
  119. lpos2.z = lpos2.z-direction.z
  120. lnode = minetest.env:get_node(lpos2)
  121. until lnode.name=="air" or lnode.name=="ignore" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none")
  122. minetest.env:remove_node(oldpos)
  123. end
  124. function mesecon:mvps_move_objects(pos, dir, nodestack)
  125. local objects_to_move = {}
  126. -- Move object at tip of stack
  127. local pushpos = mesecon:addPosRule(pos, -- get pos at tip of stack
  128. {x = dir.x * (#nodestack),
  129. y = dir.y * (#nodestack),
  130. z = dir.z * (#nodestack)})
  131. local objects = minetest.env:get_objects_inside_radius(pushpos, 1)
  132. for _, obj in ipairs(objects) do
  133. table.insert(objects_to_move, obj)
  134. end
  135. -- Move objects lying/standing on the stack (before it was pushed - oldstack)
  136. if tonumber(minetest.setting_get("movement_gravity")) > 0 and dir.y == 0 then
  137. -- If gravity positive and dir horizontal, push players standing on the stack
  138. for _, n in ipairs(nodestack) do
  139. local p_above = mesecon:addPosRule(n.pos, {x=0, y=1, z=0})
  140. local objects = minetest.env:get_objects_inside_radius(p_above, 1)
  141. for _, obj in ipairs(objects) do
  142. table.insert(objects_to_move, obj)
  143. end
  144. end
  145. end
  146. for _, obj in ipairs(objects_to_move) do
  147. local entity = obj:get_luaentity()
  148. if not entity or not mesecon:is_mvps_unmov(entity.name) then
  149. local np = mesecon:addPosRule(obj:getpos(), dir)
  150. local nn = minetest.env:get_node(np)
  151. if not minetest.registered_nodes[nn.name].walkable then
  152. obj:setpos(np)
  153. end
  154. end
  155. end
  156. end
  157. mesecon:register_mvps_stopper("default:chest_locked")
  158. mesecon:register_mvps_stopper("default:furnace")