functions.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. -- Time-recursive function.
  2. function mese_crystals.harvest_direction(pos, dir, pname, data)
  3. -- First 3 params will be nil if recursive call.
  4. data = data or {
  5. count = 0,
  6. pos = vector.copy(pos),
  7. dir = vector.copy(dir),
  8. pname = pname,
  9. }
  10. if minetest.test_protection(data.pos, data.pname) then
  11. return
  12. end
  13. local gotten = mese_crystals.harvest_pos(data.pos)
  14. if gotten then
  15. data.count = data.count + 1
  16. data.pos = vector.add(data.pos, data.dir)
  17. if data.count >= 16 then
  18. return
  19. end
  20. minetest.after(0.1, function()
  21. mese_crystals.harvest_direction(nil, nil, nil, data)
  22. end)
  23. end
  24. -- We got at least one.
  25. return gotten
  26. end
  27. function mese_crystals.harvest_pos(pos, user)
  28. local node = minetest.get_node(pos)
  29. local growth_stage = 0
  30. -- Determine growth stage.
  31. if node.name == "mese_crystals:mese_crystal_ore5" then
  32. growth_stage = 5
  33. elseif node.name == "mese_crystals:mese_crystal_ore4" then
  34. growth_stage = 4
  35. elseif node.name == "mese_crystals:mese_crystal_ore3" then
  36. growth_stage = 3
  37. elseif node.name == "mese_crystals:mese_crystal_ore2" then
  38. growth_stage = 2
  39. elseif node.name == "mese_crystals:mese_crystal_ore1" then
  40. growth_stage = 1
  41. else
  42. -- Not a crystal.
  43. return
  44. end
  45. -- Update crystaline plant.
  46. if growth_stage == 5 then
  47. node.name = "mese_crystals:mese_crystal_ore4"
  48. minetest.swap_node(pos, node)
  49. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  50. elseif growth_stage == 4 then
  51. node.name = "mese_crystals:mese_crystal_ore3"
  52. minetest.swap_node(pos, node)
  53. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  54. elseif growth_stage == 3 then
  55. node.name = "mese_crystals:mese_crystal_ore2"
  56. minetest.swap_node(pos, node)
  57. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  58. elseif growth_stage == 2 then
  59. node.name = "mese_crystals:mese_crystal_ore1"
  60. minetest.swap_node(pos, node)
  61. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  62. else
  63. -- Just restart growing timer.
  64. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  65. end
  66. -- Give wielder a harvest.
  67. if growth_stage > 1 then
  68. ambiance.sound_play("default_break_glass", pos, 0.3, 32)
  69. local stack
  70. if math.random(1, 30) == 1 or growth_stage == 5 then
  71. -- Chance to get an actual crystal.
  72. stack = ItemStack("default:mese_crystal")
  73. else
  74. stack = ItemStack("default:mese_crystal_fragment")
  75. end
  76. if user then
  77. local inv = user:get_inventory()
  78. if inv then
  79. stack = inv:add_item("main", stack)
  80. end
  81. end
  82. if not stack:is_empty() then
  83. local obj = minetest.add_item(pos, stack)
  84. if obj then
  85. obj:set_velocity({
  86. x = math.random(-100, 100) / 50,
  87. y = math.random(50, 100) / 10,
  88. z = math.random(-100, 100) / 50,
  89. })
  90. end
  91. end
  92. -- Gotten.
  93. return true
  94. end
  95. end