sapling.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. print("we grow")
  65. local under = minetest.get_node({
  66. x = pos.x,
  67. y = pos.y - 1,
  68. z = pos.z
  69. }).name
  70. if not minetest.registered_nodes[node.name] then
  71. return
  72. end
  73. local height = minetest.registered_nodes[node.name].grown_height
  74. -- do we have enough height to grow sapling into tree?
  75. if not height or not enough_height(pos, height) then
  76. return
  77. end
  78. -- Check if Ethereal Sapling is growing on correct substrate
  79. if node.name == "epic_trees:banana_tree_sapling"
  80. and contains({"default:dirt", "default:sand", 'default:dirt_with_grass'}, under) then
  81. epic_trees.grow_banana_tree(pos)
  82. end
  83. end
  84. minetest.register_abm({
  85. label = "Epic Trees grow sapling",
  86. nodenames = {"group:epic_trees_sapling"},
  87. interval = 10,
  88. chance = 50,
  89. catch_up = false,
  90. action = function(pos, node)
  91. local light_level = minetest.get_node_light(pos) or 0
  92. if light_level < 13 then
  93. return
  94. end
  95. grow_sapling(pos, node)
  96. end,
  97. })