ThSchm

Untitled

Aug 17th, 2014
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = { ... }
  2.  
  3. local digDownAmount
  4. local digDeepAmount
  5. local stripeAmount
  6.  
  7. local fuelSlot = 1
  8.  
  9. local stripeIndex = 1
  10. local digDeepIndex = 1
  11.  
  12. local inventoryFull = false
  13.  
  14. if not (#tArgs == 3) then
  15.   print ("You need exactly three parameter!")
  16.   return
  17. else
  18.   digDownAmount = tonumber(tArgs[1])
  19.   digDeepAmount = tonumber(tArgs[2])
  20.   stripeAmount = tonumber(tArgs[3])
  21. end
  22.  
  23. local function forward()
  24.   while not turtle.forward() do
  25.     if not turtle.dig() then
  26.       turtle.attack()
  27.       sleep(0.5)
  28.     end
  29.   end
  30. end
  31.  
  32. local function up()
  33.   while not turtle.up() do
  34.     if not turtle.digUp() then
  35.       turtle.attackUp()
  36.       sleep(0.5)
  37.     end
  38.   end
  39. end
  40.  
  41. local function down()
  42.   while not turtle.down() do
  43.     if not turtle.digDown() then
  44.       turtle.attackDown()
  45.       sleep(0.5)
  46.     end
  47.   end
  48. end
  49.  
  50. local function CheckFuel()
  51.   if (turtle.getFuelLevel() >= 100) then
  52.     return true
  53.   else
  54.     local oldSlot = turtle.getSelectedSlot()
  55.     turtle.select(FuelSlot)
  56.     if turtle.refuel(100) then
  57.       turtle.select(oldSlot)
  58.       return true
  59.     else
  60.       turtle.select(oldSlot)
  61.       return false
  62.     end
  63.   end
  64. end
  65.  
  66. local function FindChest()
  67.   while not ChestFound do
  68.     for i = 1, 4 do
  69.       if turtle.detect() then
  70.         print("Chest found... starting mine operation!")
  71.         return
  72.       else
  73.         turtle.turnRight()
  74.       end
  75.     end
  76.     print("No Chest found.. place a chest near turtle to start mining")
  77.     sleep(3)
  78.   end
  79. end
  80.  
  81. local function mineToBase(amount)
  82.   turtle.select(fuelSlot + 1)
  83.   for i = 1, amount do
  84.     if CheckFuel() then
  85.       down()
  86.     else
  87.       print("No fuel available! :(")
  88.       return
  89.     end
  90.     turtle.placeUp()
  91.   end
  92.   print("Reached target y-level.. start mining")
  93. end
  94.  
  95. local function moveToTop(amount)
  96.   turtle.select(fuelSlot + 1)
  97.   for i = 1, amount do
  98.     if CheckFuel() then
  99.       up()
  100.     else
  101.       print("No fuel available! :(")
  102.       return
  103.     end
  104.     turtle.placeDown()
  105.   end
  106.   print("Reached start level.. empting inventory")
  107. end
  108.  
  109. -- Returns false if inventory full
  110. local function ContinueDigging()
  111.   -- Check inventory
  112.   if (turtle.getItemCount(16) > 0) then
  113.     return false
  114.   end
  115.  
  116.   return true
  117. end
  118.  
  119. local function DigRow()
  120.   turtle.dig()
  121.   forward()
  122.   turtle.digDown()
  123.   turtle.digUp()      
  124. end
  125.  
  126. local function EmptyInventory()
  127.   if not turtle.detect() then
  128.     print("No Block to place items into")
  129.     return
  130.   end
  131.   -- Fuel is in slot 1, never empty this
  132.   for i = 2, 16 do
  133.     turtle.select(i)
  134.     turtle.drop()
  135.   end
  136.   print("Inventory successfully emptied")
  137. end
  138.  
  139. local function FirstMining(amount)
  140.   for i = 1, amount do
  141.     if ContinueDigging() and CheckFuel() then
  142.       DigRow()
  143.       digDeepIndex = digDeepIndex + 1
  144.       -- print("digDeepIndex = "..digDeepIndex)
  145.     else
  146.       break
  147.     end
  148.   end
  149.  
  150.   turtle.turnLeft()
  151.   turtle.turnLeft()
  152.   while (digDeepIndex > 1) do
  153.     forward()
  154.     digDeepIndex = digDeepIndex - 1
  155.   end  
  156.   turtle.turnLeft()
  157.   turtle.turnLeft()
  158. end
  159.  
  160. local function MiningLoop(amount)
  161.   for i = 1, amount do
  162.     stripeIndex = i
  163.     FirstMining(digDeepAmount)
  164.     if ContinueDigging() and CheckFuel() then
  165.       -- print("Successfully mined stripe #"..i)
  166.     else
  167.       print("Full inventory or out of fuel, aborted mining stripe #"..i)
  168.       inventoryFull = true
  169.       break
  170.     end
  171.  
  172.     if (i < amount) then
  173.       turtle.turnRight()
  174.       for j = 1, 3 do
  175.           forward()
  176.       end
  177.     turtle.turnLeft()
  178.     end
  179.   end
  180.  
  181.   turtle.turnLeft()
  182.   for i = 1, (stripeIndex - 1) * 3 do
  183.     forward()
  184.   end
  185.   turtle.turnRight()
  186. end
  187.  
  188. local function Main()
  189.   inventoryFull = false
  190.   FindChest()
  191.   mineToBase(digDownAmount)
  192.   MiningLoop(stripeAmount)
  193.   moveToTop(digDownAmount)
  194.   EmptyInventory()
  195.   if not inventoryFull then
  196.     print ("Run successfully!")
  197.     return
  198.   else
  199.     print ("Run partially, another iteration starting...")
  200.     Main()
  201.   end
  202. end
  203.  
  204. print("Place fuel reserves in slot 1 (upper left) if desired and hit any key to start")
  205. os.pullEvent("key")
  206. Main()
Advertisement
Add Comment
Please, Sign In to add comment