Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. # Miner
  2.  
  3. local SLOT_FEUL = 1
  4.  
  5. function get_place_dig_suck_drop_in_empty_space()
  6. local success, data = turtle.inspect()
  7. if not success then
  8. return turtle.place, turtle.dig, turtle.suck, turtle.drop
  9. end
  10.  
  11. success, data = turtle.inspectUp()
  12. if not success then
  13. return turtle.placeUp, turtle.digUp, turtle.suckUp, turtle.dropUp
  14. end
  15.  
  16. success, data = turtle.inspectDown()
  17. if not success then
  18. return turtle.placeDown, turtle.digDown, turtle.suckDown, turtle.dropDown
  19. end
  20.  
  21. error("No empty space in front, above, or below the turtle")
  22. end
  23.  
  24. function try_with_rotation(func)
  25. for i=1,4 do
  26. vals = {pcall(func)}
  27. if vals[1] then
  28. for j = 1,(i-1) do
  29. turtle.turnRight()
  30. end
  31. table.remove(vals, 1)
  32. return unpack(vals)
  33. end
  34. turtle.turnLeft()
  35. end
  36.  
  37. error("Could not execute function in any orientation")
  38. end
  39.  
  40. function refuel_from_chest(slot)
  41. print("Refueling from chest in slot " .. tostring(slot) .. ". Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
  42. local place, dig, suck, drop = try_with_rotation(get_place_dig_suck_drop_in_empty_space)
  43. local prev_slot = turtle.getSelectedSlot()
  44. turtle.select(slot)
  45. local success = place()
  46. assert(success, "Could not place chest")
  47. while turtle.getFuelLevel() ~= turtle.getFuelLimit() do
  48. success = suck()
  49. assert(success, "Could not obtain fuel from chest")
  50. success = turtle.refuel()
  51. assert(success, "Could not use item obtained from chest as fuel")
  52. end
  53. success = dig()
  54. assert(success, "Could not remove chest")
  55. print("Finished refueling. Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
  56. turtle.select(prev_slot)
  57. end
  58.  
  59. function handle_fuel()
  60. if turtle.getFuelLevel() / turtle.getFuelLimit() < 0.1 then
  61. refuel_from_chest(SLOT_FEUL)
  62. return true
  63. end
  64. return false
  65. end
  66.  
  67.  
  68. function inspect_vertical()
  69. local _, up_data = turtle.inspectUp()
  70. local _, front_data = turtle.inspect()
  71. local _, down_data = turtle.inspectDown()
  72. return {down_data, front_data, up_data}
  73. end
  74.  
  75. function inspect_vertical_simple()
  76. local up_data = turtle.inspectUp()
  77. local front_data = turtle.inspect()
  78. local down_data = turtle.inspectDown()
  79. return {down_data, front_data, up_data}
  80. end
  81.  
  82. function mine_down_until(condition)
  83. local retval = false
  84. while not condition() do
  85. local block_exists = turtle.inspectDown()
  86. if block_exists then
  87. assert(turtle.digDown(), "Could not dig down")
  88. end
  89. assert(turtle.down(), "Could not move down")
  90. retval = true
  91. end
  92. return retval
  93. end
  94.  
  95. function mine_into_ground()
  96. return mine_down_until(function()
  97. local v_state = inspect_vertical_simple()
  98. return v_state[1] == true and v_state[2] == true
  99. end)
  100. end
  101.  
  102. function mine_into_shaft()
  103. local v_state = inspect_vertical_simple()
  104. if vstate_[1] == true and v_state[2] == true and v_state[3] == false then
  105. assert(turtle.digDown(), "Could not dig down")
  106. assert(turtle.down(), "Could not move down")
  107. assert(turtle.dig(), "Could not dig forwards")
  108. assert(turtle.forward(), "Could not move forward")
  109. return true
  110. end
  111. return false
  112. end
  113.  
  114. function mine_forward_shaft()
  115. turtle.digUp() -- allowed to fail
  116. turtle.digDown() -- allowed to fail
  117.  
  118. local success = turtle.dig()
  119. assert(success, "Unable to dig forward")
  120.  
  121. success = turtle.forward()
  122. assert(success, "Unable to move forward after mining forward")
  123. return true
  124. end
  125.  
  126. function loop_logic()
  127. while true do
  128. logic_funcs = {handle_fuel, mine_into_ground, mine_into_shaft, mine_forward_shaft}
  129. for _, func in ipairs(logic_funcs) do
  130. if func() then
  131. break
  132. end
  133. end
  134. print("Hit enter to continue...")
  135. io.read()
  136. end
  137. end
  138.  
  139. loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement