global_function.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- helper functions
  2. local function process_blossom_queue_item()
  3. local pos = nature.blossomqueue[1][1]
  4. local node = nature.blossomqueue[1][2]
  5. local replace = nature.blossomqueue[1][3]
  6. if (nature.blossomqueue[1][3] == nature.blossom_node and not nature:is_near_water(pos)) then
  7. table.remove(nature.blossomqueue, 1) -- don't grow if it's not near water, pop from queue.
  8. return
  9. end
  10. nature:grow_node(pos, replace) -- now actually grow it.
  11. table.remove(nature.blossomqueue, 1)
  12. end
  13. minetest.register_globalstep(function(dtime)
  14. nature.dtime = dtime
  15. if #nature.blossomqueue > 0 and dtime < 0.2 then
  16. local i = 1
  17. if dtime < 0.1 then
  18. i = i + 4
  19. end
  20. if dtime < 0.05 then
  21. i = i + 10
  22. end
  23. while #nature.blossomqueue > 0 and i > 0 do
  24. process_blossom_queue_item()
  25. i = i - 1
  26. end
  27. end
  28. end)
  29. function nature.enqueue_node(pos, node, replace)
  30. local idx = #nature.blossomqueue
  31. if idx < nature.blossomqueue_max then
  32. local enqueue_prob = 0
  33. if idx < nature.blossomqueue_max * 0.8 then
  34. enqueue_prob = 1
  35. else
  36. -- Reduce queue growth as it gets closer to its max.
  37. enqueue_prob = 1 - (idx - nature.blossomqueue_max * 0.8) / (nature.blossomqueue_max * 0.2)
  38. end
  39. if enqueue_prob == 1 or math.random(100) <= 100 * enqueue_prob then
  40. nature.blossomqueue[idx+1] = {}
  41. nature.blossomqueue[idx+1][1] = pos
  42. nature.blossomqueue[idx+1][2] = node
  43. nature.blossomqueue[idx+1][3] = replace
  44. end
  45. end
  46. end
  47. local function set_young_node(pos)
  48. local meta = minetest.get_meta(pos)
  49. meta:set_int(nature.meta_blossom_time, minetest.get_gametime())
  50. end
  51. local function is_not_young(pos)
  52. local meta = minetest.get_meta(pos)
  53. local blossom_time = meta:get_int(nature.meta_blossom_time)
  54. return not (blossom_time and minetest.get_gametime() - blossom_time < nature.blossom_duration)
  55. end
  56. function nature:grow_node(pos, nodename)
  57. if pos ~= nil then
  58. local light_enough = (minetest.get_node_light(pos, nil) or 0)
  59. >= nature.minimum_growth_light
  60. if is_not_young(pos) and light_enough then
  61. minetest.swap_node(pos, { name = nodename })
  62. set_young_node(pos)
  63. minetest.log("info", nodename .. " has grown at " .. pos.x .. ","
  64. .. pos.y .. "," .. pos.z)
  65. end
  66. end
  67. end
  68. function nature:is_near_water(pos)
  69. return nature.distance_from_water == -1 or minetest.find_node_near(pos, nature.distance_from_water,
  70. { "default:water_source" }) ~= nil
  71. end