sapling.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. local add_tree = function (pos, ofx, ofy, ofz, schem, replace)
  2. -- check for schematic
  3. if not schem then
  4. print ("Schematic not found")
  5. return
  6. end
  7. -- remove sapling and place schematic
  8. minetest.swap_node(pos, {name = "air"})
  9. minetest.place_schematic(
  10. {x = pos.x - ofx, y = pos.y - ofy, z = pos.z - ofz},
  11. schem, 0, replace, false)
  12. end
  13. -- Register Saplings
  14. local register_sapling = function(name, desc, texture, height)
  15. minetest.register_node(name .. "_sapling", {
  16. description = desc .. " Tree Sapling",
  17. drawtype = "plantlike",
  18. tiles = {texture .. ".png"},
  19. inventory_image = texture .. ".png",
  20. wield_image = texture .. ".png",
  21. paramtype = "light",
  22. sunlight_propagates = true,
  23. is_ground_content = false,
  24. walkable = false,
  25. selection_box = {
  26. type = "fixed",
  27. fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
  28. },
  29. groups = {
  30. snappy = 2, dig_immediate = 3, flammable = 2,
  31. epic_trees_sapling = 1, attached_node = 1, sapling = 1
  32. },
  33. sounds = default.node_sound_leaves_defaults(),
  34. grown_height = height,
  35. })
  36. end
  37. register_sapling("epic_trees:banana_tree", "Banana", "banana_tree_sapling", 8)
  38. register_sapling("epic_trees:orange_tree", "Orange", "orange_tree_sapling", 6)
  39. function epic_trees.grow_banana_tree(pos)
  40. add_tree(pos, 3, 0, 3, epic_trees.bananatree)
  41. end
  42. function epic_trees.grow_orange_tree(pos)
  43. add_tree(pos, 1, 0, 1, epic_trees.orangetree)
  44. end
  45. local enough_height = function(pos, height)
  46. local nod = minetest.line_of_sight(
  47. {x = pos.x, y = pos.y + 1, z = pos.z},
  48. {x = pos.x, y = pos.y + height, z = pos.z})
  49. if not nod then
  50. return false -- obstructed
  51. else
  52. return true -- can grow
  53. end
  54. end
  55. local function contains(table, val)
  56. for i=1,#table do
  57. if table[i] == val then
  58. return true
  59. end
  60. end
  61. return false
  62. end
  63. local grow_sapling = function(pos, node)
  64. local nodeu = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
  65. local under = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
  66. if not minetest.registered_nodes[node.name] then
  67. return
  68. end
  69. local height = minetest.registered_nodes[node.name].grown_height
  70. -- do we have enough height to grow sapling into tree?
  71. if not height or not enough_height(pos, height) then
  72. return
  73. end
  74. -- Check if Ethereal Sapling is growing on correct substrate
  75. if node.name == "epic_trees:banana_tree_sapling" and contains({"default:dirt", "default:sand", 'default:dirt_with_grass'}, under) then
  76. epic_trees.grow_banana_tree(pos)
  77. elseif node.name == "epic_trees:orange_tree_sapling" then
  78. if minetest.get_item_group(under, 'soil') > 0 then
  79. epic_trees.grow_orange_tree(pos)
  80. end
  81. end
  82. end
  83. minetest.register_abm({
  84. label = "Epic Trees grow sapling",
  85. nodenames = {"group:epic_trees_sapling"},
  86. interval = 10,
  87. chance = 50,
  88. catch_up = false,
  89. action = function(pos, node)
  90. local light_level = minetest.get_node_light(pos) or 0
  91. if light_level < 13 then
  92. return
  93. end
  94. grow_sapling(pos, node)
  95. end,
  96. })