farming.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. -- Localize for performance.
  2. local math_random = math.random
  3. function mese_crystals.get_grow_time()
  4. return mese_crystals.growtime + math_random(1, 30)
  5. --return mese_crystals.growtime
  6. end
  7. function mese_crystals.get_long_grow_time()
  8. return mese_crystals.longgrowtime + math_random(1, 30)
  9. --return mese_crystals.longgrowtime
  10. end
  11. local check_lava = function(pos)
  12. local name = minetest.get_node(pos).name
  13. if minetest.get_item_group(name, "lava") > 0 then
  14. return 1
  15. else
  16. return 0
  17. end
  18. end
  19. local grow_mese_crystal_ore = function(pos, node)
  20. -- Crystals grown outside the nether should grow much slower.
  21. local nether = true
  22. if pos.y > -30770 then
  23. nether = false
  24. end
  25. local pos1 = {x = pos.x, y = pos.y, z = pos.z}
  26. pos1.y = pos1.y - 1
  27. local name = minetest.get_node(pos1).name
  28. if name ~= "default:obsidian" then
  29. -- Cannot grow, do not restart timer.
  30. return
  31. end
  32. local lava_count = 0
  33. pos1.z = pos.z - 1
  34. lava_count = lava_count + check_lava(pos1)
  35. pos1.z = pos.z + 1
  36. lava_count = lava_count + check_lava(pos1)
  37. pos1.z = pos.z
  38. pos1.x = pos.x - 1
  39. lava_count = lava_count + check_lava(pos1)
  40. pos1.x = pos.x + 1
  41. lava_count = lava_count + check_lava(pos1)
  42. pos1.x = pos.x
  43. pos1.y = pos1.y -1
  44. lava_count = lava_count + check_lava(pos1)
  45. -- Not enough lava!
  46. if lava_count < 2 then
  47. minetest.get_node_timer(pos):start(mese_crystals.get_long_grow_time())
  48. return
  49. end
  50. local keepgrowing = false
  51. if node.name == "mese_crystals:mese_crystal_ore3" then
  52. node.name = "mese_crystals:mese_crystal_ore4"
  53. minetest.swap_node(pos, node)
  54. -- Last stage, does not need node timer.
  55. elseif node.name == "mese_crystals:mese_crystal_ore2" then
  56. node.name = "mese_crystals:mese_crystal_ore3"
  57. minetest.swap_node(pos, node)
  58. keepgrowing = true
  59. elseif node.name == "mese_crystals:mese_crystal_ore1" then
  60. node.name = "mese_crystals:mese_crystal_ore2"
  61. minetest.swap_node(pos, node)
  62. keepgrowing = true
  63. end
  64. if keepgrowing then
  65. if nether then
  66. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  67. else
  68. minetest.get_node_timer(pos):start(mese_crystals.get_long_grow_time())
  69. end
  70. end
  71. end
  72. function mese_crystals.on_timer(pos, elapsed)
  73. local node = minetest.get_node(pos)
  74. return grow_mese_crystal_ore(pos, node)
  75. end