Mr_Rockers

StripMiner

Aug 1st, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. local fuelComparisonSlotNumber = 1
  2. local torchComparisonSlotNumber = 2
  3. local freeSpaceSlotNumber = 3
  4. local returnToBase = false
  5. local reachedLimit = false
  6. local limit = 0
  7.  
  8. local fwdMoves = 0
  9. local maxMoves = 0
  10.  
  11. function cls()
  12.     term.clear()
  13.     term.setCursorPos(1,1)
  14. end
  15.  
  16. function hasTorchesLeft()
  17.     for i = freeSpaceSlotNumber, 16 do
  18.         turtle.select(i)
  19.         if turtle.compareTo(torchComparisonSlotNumber) then return true end
  20.     end
  21.     return false
  22. end
  23.  
  24. function hasFuelLeft()
  25.     for i = freeSpaceSlotNumber, 16 do
  26.         turtle.select(i)
  27.         if turtle.compareTo(fuelComparisonSlotNumber) then return true end
  28.     end
  29.     return false
  30. end
  31.  
  32. function inventoryFull()
  33.     local result = true
  34.     for i = freeSpaceSlotNumber, 16 do
  35.         if turtle.getItemCount(i) == 0 then
  36.             result = false
  37.         end
  38.     end
  39.     return result
  40. end
  41.  
  42. function recalculateMaxMoves()
  43.     maxMoves = turtle.getFuelLevel()
  44. end
  45.  
  46. function refuel()
  47.     for i = freeSpaceSlotNumber, 16 do
  48.         turtle.select(i)
  49.         if turtle.compareTo(fuelComparisonSlotNumber) then turtle.refuel() end
  50.     end
  51.     recalculateMaxMoves()
  52.     turtle.select(freeSpaceSlotNumber)
  53. end
  54.  
  55. function placeTorch()
  56.     for i = freeSpaceSlotNumber, 16 do
  57.         turtle.select(i)
  58.         if turtle.compareTo(torchComparisonSlotNumber) then break end
  59.     end
  60.     turtle.placeDown()
  61.     turtle.select(freeSpaceSlotNumber)
  62. end
  63.  
  64. function deposit()
  65.     for i = freeSpaceSlotNumber, 16 do
  66.         turtle.select(i)
  67.         if (turtle.compareTo(fuelComparisonSlotNumber) == false) and (turtle.compareTo(torchComparisonSlotNumber) == false) then
  68.             turtle.dropDown()
  69.         end
  70.     end
  71.  
  72.     --Indefinite Calls Below!
  73.     if reachedLimit == false then
  74.         if hasTorchesLeft() and hasFuelLeft() then
  75.             returnToBase = false
  76.             mine()
  77.         end
  78.     end
  79. end
  80.  
  81. function mine()
  82.     refuel()
  83.     --Refuel turtle.
  84.  
  85.     turtle.turnLeft()
  86.     --Turn left.
  87.  
  88.     turtle.up()
  89.         recalculateMaxMoves()
  90.     --Move turtle up one and recalculate fuel levels.
  91.  
  92.     while returnToBase == false do
  93.         if turtle.detect() then turtle.dig() end
  94.         --If there's a block infront then dig it.  
  95.    
  96.         if turtle.forward() then
  97.             recalculateMaxMoves()
  98.             fwdMoves = fwdMoves + 1
  99.         end
  100.         --If the turtle succeeded in moving forward, then add on 1 to fwdMoves and recalculate
  101.         --Fuel consumption.
  102.        
  103.         if turtle.detectUp() then turtle.digUp() end
  104.         --If there is a block above the turtle, dig up.    
  105.  
  106.         if turtle.detectDown() then turtle.digDown() end
  107.         --If there is a block below the turtle, dig down.
  108.        
  109.         if fwdMoves % 5 == 0 then placeTorch() end
  110.         --If the turtle has moved 5 places then place a torch.
  111.  
  112.         if maxMoves <= fwdMoves + 1 then
  113.             cls()
  114.             print("Ran out of fuel!")
  115.             returnToBase = true
  116.         end
  117.         --If the max amount of moves is less than or equal to the amount
  118.         --the turtle has moved forward, then return back.
  119.  
  120.         if inventoryFull() then
  121.             cls()
  122.             print("Inventory full!")
  123.             returnToBase = true
  124.         end
  125.         --If the inventory is full, return back.
  126.  
  127.         if hasTorchesLeft() == false then
  128.             cls()
  129.             print("Ran out of torches!")
  130.             returnToBase = true
  131.         end
  132.         --If the turtle has run out of torches, return back.
  133.  
  134.         if fwdMoves >= limit then
  135.             cls()
  136.             print("Turtle has reached limit!")
  137.             returnToBase = true
  138.             reachedLimit = true
  139.         end
  140.  
  141.     end
  142.  
  143.     turtle.turnRight()
  144.     turtle.turnRight()
  145.     --Rotate the turtle 180 degrees.
  146.    
  147.     for i = 1, fwdMoves do
  148.         local fwdSuccess = false
  149.         while fwdSuccess == false do
  150.             if turtle.detect() then turtle.dig() end
  151.             fwdSuccess = turtle.forward()
  152.         end
  153.     end
  154.     --Go back to base.
  155.  
  156.     fwdMoves = 0
  157.     --Reset fwdMoves
  158.    
  159.     turtle.down()
  160.     --Go back down to ground level.
  161.  
  162.     turtle.turnLeft()
  163.     --Face forward.
  164.  
  165.     deposit()
  166.     --Deposit all items except coal and torches.
  167.  
  168. end
  169.  
  170. function start()
  171.     print("Strip Mining Program")
  172.     print("(Items will drop here!)")
  173.         os.sleep(2)
  174.         cls()
  175.     print("How far would you like the turtle to go?")
  176.         limit = tonumber(read())
  177.         cls()
  178.     print("Beginning mining process...")
  179.         os.sleep(3)
  180.         cls()
  181.     mine()
  182. end
  183.  
  184. start()
  185. cls()
  186. print("Program end.")
Advertisement
Add Comment
Please, Sign In to add comment