functions.lua 3.8 KB

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