init.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. return true, nodes, oldstack
  81. end
  82. function mesecon:mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: direction of pull (matches push direction for sticky pistons)
  83. np = mesecon:addPosRule(pos, dir)
  84. nn = minetest.env:get_node(np)
  85. if minetest.registered_nodes[nn.name].liquidtype == "none"
  86. and not mesecon:is_mvps_stopper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then
  87. local meta = minetest.env:get_meta(np):to_table()
  88. minetest.env:remove_node(np)
  89. minetest.env:add_node(pos, nn)
  90. minetest.env:get_meta(pos):from_table(meta)
  91. nodeupdate(np)
  92. nodeupdate(pos)
  93. mesecon.on_dignode(np, nn)
  94. mesecon:update_autoconnect(np)
  95. end
  96. return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
  97. end
  98. function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: direction of pull
  99. local lpos = {x=pos.x-direction.x, y=pos.y-direction.y, z=pos.z-direction.z} -- 1 away
  100. local lnode = minetest.env:get_node(lpos)
  101. local lpos2 = {x=pos.x-direction.x*2, y=pos.y-direction.y*2, z=pos.z-direction.z*2} -- 2 away
  102. local lnode2 = minetest.env:get_node(lpos2)
  103. if lnode.name ~= "ignore" and lnode.name ~= "air" and minetest.registered_nodes[lnode.name].liquidtype == "none" then return end
  104. if lnode2.name == "ignore" or lnode2.name == "air" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") then return end
  105. local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z}
  106. repeat
  107. lnode2 = minetest.env:get_node(lpos2)
  108. minetest.env:add_node(oldpos, {name=lnode2.name})
  109. nodeupdate(oldpos)
  110. oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z}
  111. lpos2.x = lpos2.x-direction.x
  112. lpos2.y = lpos2.y-direction.y
  113. lpos2.z = lpos2.z-direction.z
  114. lnode = minetest.env:get_node(lpos2)
  115. until lnode.name=="air" or lnode.name=="ignore" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none")
  116. minetest.env:remove_node(oldpos)
  117. end
  118. function mesecon:mvps_move_objects(pos, dir, nodestack)
  119. local objects_to_move = {}
  120. -- Move object at tip of stack
  121. local pushpos = mesecon:addPosRule(pos, -- get pos at tip of stack
  122. {x = dir.x * (#nodestack),
  123. y = dir.y * (#nodestack),
  124. z = dir.z * (#nodestack)})
  125. local objects = minetest.env:get_objects_inside_radius(pushpos, 1)
  126. for _, obj in ipairs(objects) do
  127. table.insert(objects_to_move, obj)
  128. end
  129. -- Move objects lying/standing on the stack (before it was pushed - oldstack)
  130. if tonumber(minetest.setting_get("movement_gravity")) > 0 and dir.y == 0 then
  131. -- If gravity positive and dir horizontal, push players standing on the stack
  132. for _, n in ipairs(nodestack) do
  133. local p_above = mesecon:addPosRule(n.pos, {x=0, y=1, z=0})
  134. local objects = minetest.env:get_objects_inside_radius(p_above, 1)
  135. for _, obj in ipairs(objects) do
  136. table.insert(objects_to_move, obj)
  137. end
  138. end
  139. end
  140. for _, obj in ipairs(objects_to_move) do
  141. local entity = obj:get_luaentity()
  142. if not entity or not mesecon:is_mvps_unmov(entity.name) then
  143. local np = mesecon:addPosRule(obj:getpos(), dir)
  144. local nn = minetest.env:get_node(np)
  145. if not minetest.registered_nodes[nn.name].walkable then
  146. obj:setpos(np)
  147. end
  148. end
  149. end
  150. end
  151. mesecon:register_mvps_stopper("default:chest_locked")
  152. mesecon:register_mvps_stopper("default:furnace")