init.lua 2.6 KB

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