functions.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_ore4" then
  32. growth_stage = 4
  33. elseif node.name == "mese_crystals:mese_crystal_ore3" then
  34. growth_stage = 3
  35. elseif node.name == "mese_crystals:mese_crystal_ore2" then
  36. growth_stage = 2
  37. elseif node.name == "mese_crystals:mese_crystal_ore1" then
  38. growth_stage = 1
  39. else
  40. -- Not a crystal.
  41. return
  42. end
  43. -- Update crystaline plant.
  44. if growth_stage == 4 then
  45. node.name = "mese_crystals:mese_crystal_ore3"
  46. minetest.swap_node(pos, node)
  47. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  48. elseif growth_stage == 3 then
  49. node.name = "mese_crystals:mese_crystal_ore2"
  50. minetest.swap_node(pos, node)
  51. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  52. elseif growth_stage == 2 then
  53. node.name = "mese_crystals:mese_crystal_ore1"
  54. minetest.swap_node(pos, node)
  55. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  56. else
  57. -- Just restart growing timer.
  58. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  59. end
  60. -- Give wielder a harvest.
  61. if growth_stage > 1 then
  62. ambiance.sound_play("default_break_glass", pos, 0.3, 32)
  63. local stack
  64. if math.random(1, 30) == 1 then
  65. -- Chance to get an actual crystal.
  66. stack = ItemStack("default:mese_crystal")
  67. else
  68. stack = ItemStack("default:mese_crystal_fragment")
  69. end
  70. if user then
  71. local inv = user:get_inventory()
  72. if inv then
  73. stack = inv:add_item("main", stack)
  74. end
  75. end
  76. if not stack:is_empty() then
  77. local obj = minetest.add_item(pos, stack)
  78. if obj then
  79. obj:set_velocity({
  80. x = math.random(-100, 100) / 50,
  81. y = math.random(50, 100) / 10,
  82. z = math.random(-100, 100) / 50,
  83. })
  84. end
  85. end
  86. -- Gotten.
  87. return true
  88. end
  89. end