init.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. enhanced_leafdecay = enhanced_leafdecay or {}
  2. enhanced_leafdecay.modpath = minetest.get_modpath("enhanced_leafdecay")
  3. local LEAFDECAY_MIN_TIME = 1
  4. local LEAFDECAY_MAX_TIME = 20
  5. local DEFAULT_RANGE = 3
  6. -- Shall return a function suitable for `on_construct` callback.
  7. -- Note: not called by Voxelmanip, schematics.
  8. enhanced_leafdecay.make_leaf_constructor =
  9. function(args)
  10. local functor = function(pos)
  11. local timer = minetest.get_node_timer(pos)
  12. timer:start(math.random(LEAFDECAY_MIN_TIME * 10, LEAFDECAY_MAX_TIME * 10) / 10)
  13. end
  14. return functor
  15. end
  16. -- Shall return a function suitable for `on_timer` callback.
  17. enhanced_leafdecay.make_leaf_nodetimer =
  18. function(args)
  19. local r = (args.range or DEFAULT_RANGE)
  20. local t = (args.tree or "group:tree")
  21. local functor = function(pos, elapsed)
  22. -- If we can find a trunk right away, then done.
  23. if utility.find_node_near_not_world_edge(pos, r, t) then
  24. return
  25. end
  26. -- If `ignore` is nearby, we can't assume there isn't a trunk.
  27. -- We'll need to restart the timer.
  28. if utility.find_node_near_not_world_edge(pos, r, "ignore") then
  29. return true
  30. end
  31. -- Drop stuff.
  32. local node = minetest.get_node(pos)
  33. -- Pass node name, because passing a node table gives wrong results.
  34. local stacks = minetest.get_node_drops(node.name)
  35. for k, v in ipairs(stacks) do
  36. if v ~= node.name or minetest.get_item_group(node.name, "leafdecay_drop") ~= 0 then
  37. local loc = {
  38. x = pos.x - 0.5 + math.random(),
  39. y = pos.y - 0.5 + math.random(),
  40. z = pos.z - 0.5 + math.random(),
  41. }
  42. minetest.add_item(loc, v)
  43. end
  44. end
  45. -- Remove node.
  46. minetest.remove_node(pos)
  47. minetest.check_for_falling(pos)
  48. instability.check_unsupported_around(pos)
  49. end
  50. return functor
  51. end
  52. -- Shall return a function suitable for `on_destruct` callback.
  53. -- Note: not called by Voxelmanip, schematics.
  54. enhanced_leafdecay.make_tree_destructor =
  55. function(args)
  56. local r = (args.range or DEFAULT_RANGE)
  57. local l = (args.leaves or "group:leaves")
  58. local functor = function(pos)
  59. local minp = {x=pos.x-r, y=pos.y-r, z=pos.z-r}
  60. local maxp = {x=pos.x+r, y=pos.y+r, z=pos.z+r}
  61. local leaves = minetest.find_nodes_in_area(minp, maxp, l)
  62. -- Start nodetimers for all leaves in the vicinity.
  63. for k, v in ipairs(leaves) do
  64. local timer = minetest.get_node_timer(v)
  65. if not timer:is_started() then
  66. timer:start(math.random(LEAFDECAY_MIN_TIME * 10, LEAFDECAY_MAX_TIME * 10) / 10)
  67. end
  68. end
  69. end
  70. return functor
  71. end