functions.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. --Helper function to see if there is open space to place a double wide node.
  2. --All wide nodes should extend to the right.
  3. --Returns true if item should be placeable.
  4. --[[
  5. ### Use this code on nodes that need to check space to side.
  6. Replace placed_node with the node that should be placed. Leave blank to use the default invisible item.
  7. after_place_node = function(pos, placer, itemstack)
  8. if not epic.space_to_side(pos, placed_node) then
  9. minetest.remove_node(pos)
  10. return itemstack
  11. end
  12. end,
  13. ### Use this code on nodes that need to remove the empty node.
  14. after_dig_node = function(pos, oldnode)
  15. epic.remove_side_node(pos, oldnode)
  16. end,
  17. ]]
  18. fdir_table = {
  19. { 1, 0 },
  20. { 0, -1 },
  21. { -1, 0 },
  22. { 0, 1 },
  23. { 1, 0 },
  24. { 0, -1 },
  25. { -1, 0 },
  26. { 0, 1 },
  27. }
  28. function epic.space_to_side(pos, placed_node)
  29. local node = minetest.get_node(pos)
  30. local fdir = node.param2 % 32
  31. local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
  32. local node2 = minetest.get_node(pos2)
  33. local node2def = minetest.registered_nodes[node2.name] or nil
  34. if not node2def.buildable_to then
  35. return false
  36. else
  37. local placed_node = placed_node or 'epic:empty'
  38. minetest.set_node(pos2,{name = placed_node, param2=fdir})
  39. return true
  40. end
  41. end
  42. function epic.remove_side_node(pos, oldnode)
  43. local fdir = oldnode.param2 % 32
  44. local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
  45. local node2 = minetest.get_node(pos2).name
  46. if minetest.get_item_group(node2, 'empty_node') > 0 then
  47. minetest.remove_node(pos2)
  48. end
  49. end
  50. function epic.space_to_front(pos)
  51. local node = minetest.get_node(pos)
  52. local fdir = node.param2 % 32
  53. local pos2 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]}
  54. local node2 = minetest.get_node(pos2)
  55. local node2def = minetest.registered_nodes[node2.name] or nil
  56. if not node2def.buildable_to then
  57. return false
  58. else
  59. minetest.set_node(pos2,{name = 'epic:empty'})
  60. return true
  61. end
  62. end
  63. function epic.remove_front_node(pos, oldnode)
  64. local fdir = oldnode.param2 % 32
  65. local pos2 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]}
  66. local node2 = minetest.get_node(pos2).name
  67. if minetest.get_item_group(node2, 'empty_node') > 0 then
  68. minetest.remove_node(pos2)
  69. end
  70. end
  71. function epic.space_for_picnic(pos, placed_node)
  72. local node = minetest.get_node(pos)
  73. local fdir = node.param2 % 32
  74. local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]} --Side node
  75. local pos3 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]} --Across node
  76. local node2 = minetest.get_node(pos2)
  77. local node3 = minetest.get_node(pos3)
  78. local node2def = minetest.registered_nodes[node2.name] or nil
  79. local node3def = minetest.registered_nodes[node3.name] or nil
  80. if not node2def.buildable_to or not node3def.buildable_to then
  81. return false
  82. else
  83. local placed_node = placed_node or 'epic:empty'
  84. minetest.set_node(pos2,{name = placed_node, param2=fdir})
  85. minetest.set_node(pos3,{name = placed_node, param2=fdir})
  86. return true
  87. end
  88. end
  89. function epic.remove_picnic_nodes(pos, oldnode)
  90. local fdir = oldnode.param2 % 32
  91. local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]} --Side node
  92. local pos3 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]} --Across node
  93. local node2 = minetest.get_node(pos2).name
  94. local node3 = minetest.get_node(pos3).name
  95. if minetest.get_item_group(node2, 'empty_node') > 0 and minetest.get_item_group(node3, 'empty_node') >0 then
  96. minetest.remove_node(pos2)
  97. minetest.remove_node(pos3)
  98. end
  99. end