functions.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. -- Localize for performance.
  2. local math_random = math.random
  3. -- Indexed array.
  4. -- List of nodes which nether flora can use as soil.
  5. nethervine.flora_surfaces = {
  6. "rackstone:redrack",
  7. "rackstone:dauthsand",
  8. }
  9. -- List of nearby nether flora that should be notified when a flora node is removed.
  10. nethervine.flora_nodes = {
  11. "nether:grass_1",
  12. "nether:grass_2",
  13. "nether:grass_3",
  14. }
  15. nethervine.flora_mintime = 60*3
  16. nethervine.flora_maxtime = 60*30
  17. function nethervine.surface_can_spawn_flora(pos)
  18. local name = minetest.get_node(pos).name
  19. local nodes = nethervine.flora_surfaces
  20. for i=1, #nodes do
  21. if string.find(nodes[i], "^group:") then
  22. local group = string.sub(nodes[i], 7)
  23. if minetest.get_item_group(name, group) ~= 0 then
  24. return true
  25. end
  26. elseif nodes[i] == name then
  27. return true
  28. end
  29. end
  30. return false
  31. end
  32. function nethervine.on_flora_construct(pos)
  33. if nethervine.surface_can_spawn_flora({x=pos.x, y=pos.y-1, z=pos.z}) then
  34. minetest.get_node_timer(pos):start(math_random(nethervine.flora_mintime, nethervine.flora_maxtime))
  35. end
  36. end
  37. function nethervine.on_flora_destruct(pos)
  38. -- Notify nearby flora.
  39. local minp = {x=pos.x-2, y=pos.y-2, z=pos.z-2}
  40. local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
  41. local flora = minetest.find_nodes_in_area_under_air(minp, maxp, nethervine.flora_nodes)
  42. if flora and #flora > 0 then
  43. for i=1, #flora do
  44. minetest.get_node_timer(flora[i]):start(math_random(nethervine.flora_mintime, nethervine.flora_maxtime))
  45. end
  46. end
  47. end
  48. function nethervine.on_flora_timer(pos, elapsed)
  49. local node = minetest.get_node(pos)
  50. if nethervine.flora_spread(pos, node) then
  51. minetest.get_node_timer(pos):start(math_random(nethervine.flora_mintime, nethervine.flora_maxtime))
  52. else
  53. -- Else timer should stop, cannot grow anymore.
  54. minetest.get_node_timer(pos):stop()
  55. end
  56. end
  57. function nethervine.on_flora_punch(pos, node, puncher, pt)
  58. if nethervine.surface_can_spawn_flora({x=pos.x, y=pos.y-1, z=pos.z}) then
  59. minetest.get_node_timer(pos):start(math_random(nethervine.flora_mintime, nethervine.flora_maxtime))
  60. end
  61. end
  62. -- Called by the bonemeal mod.
  63. -- Returns 'true' or 'false' to indicate if a mushroom was spawned.
  64. function nethervine.flora_spread(pos, node)
  65. local minp = {x=pos.x-2, y=pos.y-2, z=pos.z-2}
  66. local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
  67. local dirt = minetest.find_nodes_in_area_under_air(minp, maxp, nethervine.flora_surfaces)
  68. if not dirt or #dirt == 0 then
  69. return false
  70. end
  71. local density = nethervine.flora_density_for_surface({x=pos.x, y=pos.y-1, z=pos.z})
  72. local pos0 = vector.subtract(pos, 4)
  73. local pos1 = vector.add(pos, 4)
  74. if #minetest.find_nodes_in_area(pos0, pos1, "group:netherflora") > density then
  75. return false -- Max flora reached.
  76. end
  77. local randp = dirt[math_random(1, #dirt)]
  78. local airp = {x=randp.x, y=randp.y+1, z=randp.z}
  79. local airn = minetest.get_node_or_nil(airp)
  80. if not airn or airn.name ~= "air" then
  81. return false
  82. end
  83. -- Nether flora grows in nether regardless of light level.
  84. if pos.y < -25000 then
  85. minetest.add_node(airp, {name = node.name})
  86. return true
  87. end
  88. return false
  89. end
  90. function nethervine.flora_density_for_surface(pos)
  91. local cold = 0
  92. if minetest.find_node_near(pos, 5, {
  93. "group:snow",
  94. "group:snowy",
  95. "group:ice",
  96. "group:cold",
  97. }) then
  98. cold = -6
  99. end
  100. -- High heat makes nether plants grow denser.
  101. local heat = 0
  102. if minetest.find_node_near(pos, 3, "group:lava") then
  103. heat = 4
  104. end
  105. local minerals = 0
  106. if minetest.find_node_near(pos, 3, "glowstone:minerals") then
  107. minerals = 1
  108. end
  109. if minetest.get_node(pos).name == "rackstone:dauthsand" then
  110. return 4 + minerals + heat + cold
  111. end
  112. -- Default flower density.
  113. return 1 + minerals + heat + cold
  114. end