functions.lua 680 B

1234567891011121314151617181920212223242526272829
  1. local math_random = math.random
  2. -- Note: this function assumes it is only executed at mapgen time. It bypasses
  3. -- node callbacks!
  4. function dryleaves.replace_leaves(minp, maxp, coverage)
  5. local leaves = {}
  6. if #dryleaves.list == 0 then
  7. return leaves
  8. end
  9. local targets = minetest.find_nodes_in_area(minp, maxp, "group:leaves")
  10. if #targets == 0 then
  11. return leaves
  12. end
  13. for idx = 1, #targets, 1 do
  14. if math_random(0, 100) < coverage then
  15. local pos = targets[idx]
  16. local leaf = dryleaves.list[math_random(1, #dryleaves.list)]
  17. -- There is no need to run callbacks.
  18. minetest.swap_node(pos, {name=leaf})
  19. leaves[#leaves + 1] = pos
  20. end
  21. end
  22. return leaves
  23. end