init.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. -- File is reloadable.
  2. if not minetest.global_exists("fruitscatter") then fruitscatter = {} end
  3. fruitscatter.modpath = minetest.get_modpath("fruitscatter")
  4. local find_air_under_leaf = function(leaf, x, ybot, ytop, z)
  5. for y = ybot, ytop, 1 do
  6. local p1 = {x=x, y=y, z=z}
  7. local p2 = {x=x, y=y+1, z=z}
  8. local n1 = minetest.get_node(p1).name
  9. local n2 = minetest.get_node(p2).name
  10. if n1 == "air" and n2 == leaf then
  11. return p1, p2
  12. end
  13. end
  14. end
  15. fruitscatter.scatter_fruit = function(leaf, fruit, minp, maxp, chance)
  16. minp, maxp = utility.sort_positions(minp, maxp)
  17. local random = math.random
  18. for x = minp.x, maxp.x, 1 do
  19. for z = minp.z, maxp.z, 1 do
  20. if random(1, chance) == 1 then
  21. local p1, p2 = find_air_under_leaf(leaf, x, minp.y, maxp.y, z)
  22. if p1 then
  23. minetest.add_node(p1, {name=fruit})
  24. minetest.check_for_falling(p1)
  25. end
  26. end
  27. end
  28. end
  29. end
  30. -- This API is designed to be called by tree mods.
  31. fruitscatter.scatter_fruit_under_leaves = function(pos, leaf, fruit, minp, maxp, chance)
  32. local sminp = {x=pos.x+minp.x, y=pos.y+minp.y, z=pos.z+minp.z}
  33. local smaxp = {x=pos.x+maxp.x, y=pos.y+maxp.y, z=pos.z+maxp.z}
  34. fruitscatter.scatter_fruit(leaf, fruit, sminp, smaxp, chance)
  35. end
  36. if not fruitscatter.run_once then
  37. -- Reloadable.
  38. local name = "fruitscatter:core"
  39. local file = fruitscatter.modpath .. "/init.lua"
  40. reload.register_file(name, file, false)
  41. fruitscatter.run_once = true
  42. end