init.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. if not minetest.global_exists("trunkgen") then trunkgen = {} end
  2. trunkgen.modpath = minetest.get_modpath("trunkgen")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. -- Utility function.
  6. trunkgen.set_node = function(pos, node)
  7. if minetest.get_node(pos).name == "air" then
  8. minetest.set_node(pos, node)
  9. end
  10. end
  11. -- Generates a trunk base.
  12. trunkgen.generate_bole = function(pos, nodename)
  13. local pr = {}
  14. function pr:next(min, max)
  15. return math_random(min, max)
  16. end
  17. local sides =
  18. {
  19. {x=pos.x-1, y=pos.y, z=pos.z},
  20. {x=pos.x+1, y=pos.y, z=pos.z},
  21. {x=pos.x, y=pos.y, z=pos.z-1},
  22. {x=pos.x, y=pos.y, z=pos.z+1},
  23. }
  24. for i=1, #sides do
  25. if pr:next(0, 1) == 0 then
  26. local p = sides[i]
  27. local n = pr:next(0, pr:next(1, 2))
  28. for j=0, n do
  29. local z = {x=p.x, y=p.y+j, z=p.z}
  30. trunkgen.set_node(z, {name=nodename})
  31. end
  32. end
  33. end
  34. local corners =
  35. {
  36. {x=pos.x-1, y=pos.y, z=pos.z-1},
  37. {x=pos.x+1, y=pos.y, z=pos.z+1},
  38. {x=pos.x+1, y=pos.y, z=pos.z-1},
  39. {x=pos.x-1, y=pos.y, z=pos.z+1},
  40. }
  41. for i=1, #corners do
  42. if pr:next(0, 1) == 0 then
  43. local p = corners[i]
  44. local z = {x=p.x, y=p.y, z=p.z}
  45. trunkgen.set_node(z, {name=nodename})
  46. end
  47. end
  48. end
  49. -- Generates a jungletree-style leaf-branch.
  50. trunkgen.generate_jungletree_branches = function(pos, tname, lnames, minh, maxh)
  51. local n = math_random(1, 3) -- How many?
  52. if type(lnames) == "string" then
  53. lnames = {[1]=lnames}
  54. end
  55. assert(type(lnames) == "table")
  56. local positions =
  57. {
  58. {x=pos.x+1, y=pos.y, z=pos.z},
  59. {x=pos.x-1, y=pos.y, z=pos.z},
  60. {x=pos.x, y=pos.y, z=pos.z+1},
  61. {x=pos.x, y=pos.y, z=pos.z-1},
  62. {x=pos.x+1, y=pos.y, z=pos.z+1},
  63. {x=pos.x+1, y=pos.y, z=pos.z-1},
  64. {x=pos.x-1, y=pos.y, z=pos.z+1},
  65. {x=pos.x-1, y=pos.y, z=pos.z-1},
  66. }
  67. for i=1, n do
  68. local h = math_random(minh, maxh) -- How high?
  69. local p = vector.new(positions[math_random(1, #positions)]) -- Where?
  70. p.y = p.y+h
  71. -- Make!
  72. trunkgen.set_node(p, {name=tname})
  73. local lp =
  74. {
  75. {x=p.x+1, y=p.y, z=p.z},
  76. {x=p.x-1, y=p.y, z=p.z},
  77. {x=p.x, y=p.y, z=p.z+1},
  78. {x=p.x, y=p.y, z=p.z-1},
  79. {x=p.x+1, y=p.y, z=p.z+1},
  80. {x=p.x+1, y=p.y, z=p.z-1},
  81. {x=p.x-1, y=p.y, z=p.z+1},
  82. {x=p.x-1, y=p.y, z=p.z-1},
  83. }
  84. for j=1, #lp do
  85. local p = lp[j]
  86. trunkgen.set_node(p, {name=lnames[math_random(1, #lnames)]})
  87. end
  88. end
  89. end
  90. function trunkgen.check_trunk(pos, dist, trunk)
  91. for y = pos.y, pos.y + dist, 1 do
  92. local p = {x=pos.x, y=y, z=pos.z}
  93. if minetest.get_node(p).name == "air" then
  94. minetest.set_node(p, {name=trunk})
  95. end
  96. end
  97. end