functions.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. --minetest.chat_send_player("MustTest", "Nether flora timer @ " .. minetest.pos_to_string(pos) .. "!")
  50. local node = minetest.get_node(pos)
  51. if nethervine.flora_spread(pos, node) then
  52. minetest.get_node_timer(pos):start(math_random(nethervine.flora_mintime, nethervine.flora_maxtime))
  53. else
  54. -- Else timer should stop, cannot grow anymore.
  55. minetest.get_node_timer(pos):stop()
  56. end
  57. end
  58. function nethervine.on_flora_punch(pos, node, puncher, pt)
  59. if nethervine.surface_can_spawn_flora({x=pos.x, y=pos.y-1, z=pos.z}) then
  60. minetest.get_node_timer(pos):start(math_random(nethervine.flora_mintime, nethervine.flora_maxtime))
  61. end
  62. end
  63. -- Called by the bonemeal mod.
  64. -- Returns 'true' or 'false' to indicate if a mushroom was spawned.
  65. function nethervine.flora_spread(pos, node)
  66. local minp = {x=pos.x-2, y=pos.y-2, z=pos.z-2}
  67. local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
  68. local dirt = minetest.find_nodes_in_area_under_air(minp, maxp, nethervine.flora_surfaces)
  69. if not dirt or #dirt == 0 then
  70. return false
  71. end
  72. local density = nethervine.flora_density_for_surface({x=pos.x, y=pos.y-1, z=pos.z})
  73. local pos0 = vector.subtract(pos, 4)
  74. local pos1 = vector.add(pos, 4)
  75. if #minetest.find_nodes_in_area(pos0, pos1, "group:netherflora") > density then
  76. return false -- Max flora reached.
  77. end
  78. local randp = dirt[math_random(1, #dirt)]
  79. local airp = {x=randp.x, y=randp.y+1, z=randp.z}
  80. local airn = minetest.get_node_or_nil(airp)
  81. if not airn or airn.name ~= "air" then
  82. return false
  83. end
  84. -- Nether flora grows in nether regardless of light level.
  85. if pos.y < -25000 then
  86. minetest.add_node(airp, {name = node.name})
  87. return true
  88. end
  89. return false
  90. end
  91. function nethervine.flora_density_for_surface(pos)
  92. local cold = 0
  93. if minetest.find_node_near(pos, 5, {
  94. "group:snow",
  95. "group:snowy",
  96. "group:ice",
  97. "group:cold",
  98. }) then
  99. cold = -6
  100. end
  101. -- High heat makes nether plants grow denser.
  102. local heat = 0
  103. if minetest.find_node_near(pos, 3, "group:lava") then
  104. heat = 4
  105. end
  106. local minerals = 0
  107. if minetest.find_node_near(pos, 3, "glowstone:minerals") then
  108. minerals = 1
  109. end
  110. if minetest.get_node(pos).name == "rackstone:dauthsand" then
  111. return 4 + minerals + heat + cold
  112. end
  113. -- Default flower density.
  114. return 1 + minerals + heat + cold
  115. end