farming.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. -- Localize for performance.
  2. local math_random = math.random
  3. -- For testing.
  4. local FAST_CRYSTAL_GROWTH = false
  5. -- Crystals are NOT plants, they're more "like" stony material.
  6. -- They should take a long time to grow.
  7. function mese_crystals.get_grow_time()
  8. if FAST_CRYSTAL_GROWTH then
  9. return 5
  10. end
  11. return 60*math.random(15, 30)
  12. end
  13. function mese_crystals.get_long_grow_time()
  14. if FAST_CRYSTAL_GROWTH then
  15. return 5
  16. end
  17. return 60*math.random(60, 120)
  18. end
  19. local check_lava = function(pos)
  20. local name = minetest.get_node(pos).name
  21. if minetest.get_item_group(name, "lava") > 0 then
  22. return 1
  23. else
  24. return 0
  25. end
  26. end
  27. -- Get the location of a valid glow mineral.
  28. local function get_glowminerals(pos)
  29. local t = {
  30. vector.offset(pos, 0, -2, 0),
  31. vector.offset(pos, 1, -1, 0),
  32. vector.offset(pos, -1, -1, 0),
  33. vector.offset(pos, 0, -1, 1),
  34. vector.offset(pos, 0, -1, -1),
  35. }
  36. for k = 1, #t do
  37. local n = minetest.get_node(t[k])
  38. if n.name == "glowstone:minerals" then
  39. return t[k]
  40. end
  41. end
  42. end
  43. local grow_mese_crystal_ore = function(pos, node)
  44. -- Crystals grown outside the nether should grow much slower.
  45. local nether = true
  46. if pos.y > -30770 then
  47. nether = false
  48. end
  49. local pos1 = {x = pos.x, y = pos.y, z = pos.z}
  50. pos1.y = pos1.y - 1
  51. local name = minetest.get_node(pos1).name
  52. if name ~= "default:obsidian" then
  53. -- Cannot grow, do not restart timer.
  54. return
  55. end
  56. local lava_count = 0
  57. pos1.z = pos.z - 1
  58. lava_count = lava_count + check_lava(pos1)
  59. pos1.z = pos.z + 1
  60. lava_count = lava_count + check_lava(pos1)
  61. pos1.z = pos.z
  62. pos1.x = pos.x - 1
  63. lava_count = lava_count + check_lava(pos1)
  64. pos1.x = pos.x + 1
  65. lava_count = lava_count + check_lava(pos1)
  66. pos1.x = pos.x
  67. pos1.y = pos1.y -1
  68. lava_count = lava_count + check_lava(pos1)
  69. -- Not enough lava!
  70. if lava_count < 2 then
  71. minetest.get_node_timer(pos):start(mese_crystals.get_long_grow_time())
  72. return
  73. end
  74. local keepgrowing = false
  75. if node.name == "mese_crystals:mese_crystal_ore4" then
  76. local gm = get_glowminerals(pos)
  77. if gm then
  78. node.name = "mese_crystals:mese_crystal_ore5"
  79. minetest.swap_node(pos, node)
  80. minetest.set_node(gm, {name="cavestuff:coal_dust"}) -- Black sand.
  81. minetest.check_single_for_falling(gm)
  82. -- Last stage, does not need node timer.
  83. end
  84. elseif node.name == "mese_crystals:mese_crystal_ore3" then
  85. node.name = "mese_crystals:mese_crystal_ore4"
  86. minetest.swap_node(pos, node)
  87. keepgrowing = true
  88. elseif node.name == "mese_crystals:mese_crystal_ore2" then
  89. node.name = "mese_crystals:mese_crystal_ore3"
  90. minetest.swap_node(pos, node)
  91. keepgrowing = true
  92. elseif node.name == "mese_crystals:mese_crystal_ore1" then
  93. node.name = "mese_crystals:mese_crystal_ore2"
  94. minetest.swap_node(pos, node)
  95. keepgrowing = true
  96. end
  97. if keepgrowing then
  98. if nether then
  99. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  100. else
  101. minetest.get_node_timer(pos):start(mese_crystals.get_long_grow_time())
  102. end
  103. end
  104. end
  105. function mese_crystals.on_timer(pos, elapsed)
  106. local node = minetest.get_node(pos)
  107. return grow_mese_crystal_ore(pos, node)
  108. end