functions.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ropes.can_place_rope_in_node = function(target_node_name)
  2. if ropes.can_extend_into_nodes[target_node_name] == true then
  3. return true
  4. end
  5. local target_def = minetest.registered_nodes[target_node_name]
  6. if target_def then
  7. if target_def.drawtype == "airlike" and ropes.can_extend_into_airlike then
  8. return true
  9. end
  10. if target_def.groups and target_def.groups.ropes_can_extend_into then
  11. return true
  12. end
  13. end
  14. return false
  15. end
  16. ropes.make_rope_on_timer = function(rope_node_name)
  17. return function(pos, elapsed)
  18. local currentend = minetest.get_node(pos)
  19. local currentmeta = minetest.get_meta(pos)
  20. local currentlength = currentmeta:get_int("length_remaining")
  21. local placer_name = currentmeta:get_string("placer")
  22. local newpos = {x=pos.x, y=pos.y-1, z=pos.z}
  23. local newnode = minetest.get_node(newpos)
  24. local oldnode = minetest.get_node(pos)
  25. if currentlength > 1 and (not minetest.is_protected(newpos, placer_name)
  26. or minetest.check_player_privs(placer_name, "protection_bypass")) then
  27. if ropes.can_place_rope_in_node(newnode.name) then
  28. minetest.add_node(newpos, {name=currentend.name, param2=oldnode.param2})
  29. local newmeta = minetest.get_meta(newpos)
  30. newmeta:set_int("length_remaining", currentlength-1)
  31. newmeta:set_string("placer", placer_name)
  32. minetest.set_node(pos, {name=rope_node_name, param2=oldnode.param2})
  33. ropes.move_players_down(pos, 1)
  34. else
  35. local timer = minetest.get_node_timer( pos )
  36. timer:start( 1 )
  37. end
  38. end
  39. end
  40. end
  41. local data = {}
  42. local c_air = minetest.get_content_id("air")
  43. ropes.destroy_rope = function(pos, nodes)
  44. local top = pos.y
  45. local bottom = pos.y-15
  46. local voxel_manip = minetest.get_voxel_manip()
  47. local finished = false
  48. local ids_to_destroy = {}
  49. for _, node in pairs(nodes) do
  50. ids_to_destroy[minetest.get_content_id(node)] = true
  51. end
  52. while not finished do
  53. local emin, emax = voxel_manip:read_from_map({x=pos.x, y=bottom, z=pos.z}, {x=pos.x, y=top, z=pos.z})
  54. voxel_manip:get_data(data)
  55. local voxel_area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  56. bottom = emin.y
  57. for y = top, bottom, -1 do
  58. local index = voxel_area:index(pos.x, y, pos.z)
  59. if ids_to_destroy[data[index]] then
  60. data[index] = c_air
  61. else
  62. finished = true
  63. break
  64. end
  65. end
  66. voxel_manip:set_data(data)
  67. voxel_manip:write_to_map()
  68. voxel_manip:update_map()
  69. top = bottom - 1
  70. bottom = bottom - 15
  71. end
  72. end
  73. ropes.hanging_after_destruct = function(pos, top_node, middle_node, bottom_node)
  74. local node = minetest.get_node(pos)
  75. if node.name == top_node or node.name == middle_node or node.name == bottom_node then
  76. return -- this was done by another ladder or rope node changing this one, don't react
  77. end
  78. pos.y = pos.y + 1 -- one up
  79. local node_above = minetest.get_node(pos)
  80. if node_above.name == middle_node then
  81. minetest.swap_node(pos, {name=bottom_node, param2=node_above.param2})
  82. end
  83. pos.y = pos.y - 2 -- one down
  84. local node_below = minetest.get_node(pos)
  85. if node_below.name == middle_node then
  86. ropes.destroy_rope(pos, {middle_node, bottom_node})
  87. elseif node_below.name == bottom_node then
  88. minetest.swap_node(pos, {name="air"})
  89. end
  90. end
  91. ropes.move_players_down = function(pos, radius)
  92. local all_objects = minetest.get_objects_inside_radius({x=pos.x, y=pos.y+radius, z=pos.z}, radius)
  93. local players = {}
  94. local _,obj
  95. for _,obj in pairs(all_objects) do
  96. if obj:is_player() then
  97. local obj_pos = obj:getpos()
  98. if math.abs(obj_pos.x-pos.x) < 0.5 and math.abs(obj_pos.z-pos.z) < 0.5 then
  99. obj:moveto({x=obj_pos.x, y=obj_pos.y-1, z=obj_pos.z}, true)
  100. end
  101. end
  102. end
  103. end