trees.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. local c_air = minetest.get_content_id("air")
  2. local c_ignore = minetest.get_content_id("ignore")
  3. local c_tree = minetest.get_content_id("default:tree")
  4. local c_leaves = minetest.get_content_id("default:leaves")
  5. local c_apple = minetest.get_content_id("default:apple")
  6. function default.grow_tree(data, a, pos, is_apple_tree, seed)
  7. --[[
  8. NOTE: Tree-placing code is currently duplicated in the engine
  9. and in games that have saplings; both are deprecated but not
  10. replaced yet
  11. ]]--
  12. local pr = PseudoRandom(seed)
  13. local th = pr:next(4, 5)
  14. local x, y, z = pos.x, pos.y, pos.z
  15. for yy = y, y+th-1 do
  16. local vi = a:index(x, yy, z)
  17. if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
  18. data[vi] = c_tree
  19. end
  20. end
  21. y = y+th-1 -- (x, y, z) is now last piece of trunk
  22. local leaves_a = VoxelArea:new{MinEdge={x=-2, y=-1, z=-2}, MaxEdge={x=2, y=2, z=2}}
  23. local leaves_buffer = {}
  24. -- Force leaves near the trunk
  25. local d = 1
  26. for xi = -d, d do
  27. for yi = -d, d do
  28. for zi = -d, d do
  29. leaves_buffer[leaves_a:index(xi, yi, zi)] = true
  30. end
  31. end
  32. end
  33. -- Add leaves randomly
  34. for iii = 1, 8 do
  35. local d = 1
  36. local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
  37. local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
  38. local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
  39. for xi = 0, d do
  40. for yi = 0, d do
  41. for zi = 0, d do
  42. leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
  43. end
  44. end
  45. end
  46. end
  47. -- Add the leaves
  48. for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
  49. for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
  50. for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
  51. if a:contains(x+xi, y+yi, z+zi) then
  52. local vi = a:index(x+xi, y+yi, z+zi)
  53. if data[vi] == c_air or data[vi] == c_ignore then
  54. if leaves_buffer[leaves_a:index(xi, yi, zi)] then
  55. if is_apple_tree and pr:next(1, 100) <= 10 then
  56. data[vi] = c_apple
  57. else
  58. data[vi] = c_leaves
  59. end
  60. end
  61. end
  62. end
  63. end
  64. end
  65. end
  66. end
  67. local c_jungletree = minetest.get_content_id("default:jungletree")
  68. local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
  69. function default.grow_jungletree(data, a, pos, seed)
  70. --[[
  71. NOTE: Tree-placing code is currently duplicated in the engine
  72. and in games that have saplings; both are deprecated but not
  73. replaced yet
  74. ]]--
  75. local pr = PseudoRandom(seed)
  76. local x, y, z = pos.x, pos.y, pos.z
  77. for xi = -1, 1 do
  78. for zi = -1, 1 do
  79. if pr:next(1, 3) >= 2 then
  80. local vi1 = a:index(x+xi, y, z+zi)
  81. local vi2 = a:index(x+xi, y-1, z+zi)
  82. if a:contains(x+xi, y-1, z+zi) and data[vi2] == c_air then
  83. data[vi2] = c_jungletree
  84. elseif a:contains(x+xi, y, z+zi) and data[vi1] == c_air then
  85. data[vi1] = c_jungletree
  86. end
  87. end
  88. end
  89. end
  90. local th = pr:next(8, 12)
  91. for yy = y, y+th-1 do
  92. local vi = a:index(x, yy, z)
  93. if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
  94. data[vi] = c_jungletree
  95. end
  96. end
  97. y = y+th-1 -- (x, y, z) is now last piece of trunk
  98. local leaves_a = VoxelArea:new{MinEdge={x=-3, y=-2, z=-3}, MaxEdge={x=3, y=2, z=3}}
  99. local leaves_buffer = {}
  100. -- Force leaves near the trunk
  101. local d = 1
  102. for xi = -d, d do
  103. for yi = -d, d do
  104. for zi = -d, d do
  105. leaves_buffer[leaves_a:index(xi, yi, zi)] = true
  106. end
  107. end
  108. end
  109. -- Add leaves randomly
  110. for iii = 1, 30 do
  111. local d = 1
  112. local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
  113. local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
  114. local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
  115. for xi = 0, d do
  116. for yi = 0, d do
  117. for zi = 0, d do
  118. leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
  119. end
  120. end
  121. end
  122. end
  123. -- Add the leaves
  124. for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
  125. for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
  126. for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
  127. if a:contains(x+xi, y+yi, z+zi) then
  128. local vi = a:index(x+xi, y+yi, z+zi)
  129. if data[vi] == c_air or data[vi] == c_ignore then
  130. if leaves_buffer[leaves_a:index(xi, yi, zi)] then
  131. data[vi] = c_jungleleaves
  132. end
  133. end
  134. end
  135. end
  136. end
  137. end
  138. end