leafscatter.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. hb4 = hb4 or {}
  2. hb4.leafscatter = hb4.leafscatter or {}
  3. hb4.leafscatter.remove = function(pos, minp, maxp, leaf, chance)
  4. local sminp = {x=pos.x+minp.x, y=pos.y+minp.y, z=pos.z+minp.z}
  5. local smaxp = {x=pos.x+maxp.x, y=pos.y+maxp.y+1, z=pos.z+maxp.z}
  6. local ominp, omaxp = utility.sort_positions(sminp, smaxp)
  7. local random = math.random
  8. local getn = minetest.get_node
  9. local rmnode = minetest.remove_node
  10. for x = ominp.x, omaxp.x, 1 do
  11. for z = ominp.z, omaxp.z, 1 do
  12. for y = ominp.y, omaxp.y, 1 do
  13. if random(1, chance) == 1 then
  14. local pp = {x=x, y=y, z=z}
  15. local nn = getn(pp).name
  16. if nn == leaf then
  17. rmnode(pp)
  18. end
  19. end
  20. end
  21. end
  22. end
  23. end
  24. hb4.leafscatter.add = function(pos, minp, maxp, leaf, chance)
  25. local sminp = {x=pos.x+minp.x, y=pos.y+minp.y, z=pos.z+minp.z}
  26. local smaxp = {x=pos.x+maxp.x, y=pos.y+maxp.y+1, z=pos.z+maxp.z}
  27. local ominp, omaxp = utility.sort_positions(sminp, smaxp)
  28. local random = math.random
  29. local getn = minetest.get_node
  30. local setn = minetest.add_node
  31. local leafnear = function(p)
  32. local p_ = {
  33. {x=p.x-1, y=p.y, z=p.z},
  34. {x=p.x+1, y=p.y, z=p.z},
  35. {x=p.x, y=p.y-1, z=p.z},
  36. {x=p.x, y=p.y+1, z=p.z},
  37. {x=p.x, y=p.y, z=p.z-1},
  38. {x=p.x, y=p.y, z=p.z+1},
  39. }
  40. for k, v in ipairs(p_) do
  41. if getn(v).name == leaf then
  42. return true
  43. end
  44. end
  45. end
  46. for x = ominp.x, omaxp.x, 1 do
  47. for z = ominp.z, omaxp.z, 1 do
  48. for y = ominp.y, omaxp.y, 1 do
  49. if random(1, chance) == 1 then
  50. local pp = {x=x, y=y, z=z}
  51. local nn = getn(pp).name
  52. if nn == "air" then
  53. if leafnear(pp) then
  54. setn(pp, {name=leaf})
  55. end
  56. end
  57. end
  58. end
  59. end
  60. end
  61. end