Guest User

StripminerSimple

a guest
Aug 17th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  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 FindChest()
  51.   while not ChestFound do
  52.     for i = 1, 4 do
  53.       if turtle.detect() then
  54.         print("Chest found... starting mine operation!")
  55.         return
  56.       else
  57.         turtle.turnRight()
  58.       end
  59.     end
  60.     print("No Chest found.. place a chest near turtle to start mining")
  61.     sleep(3)
  62.   end
  63. end
  64.  
  65. local function mineToBase(amount)
  66.   turtle.select(fuelSlot + 1)
  67.   for i = 1, amount do
  68.     if CheckFuel() then
  69.       down()
  70.     else
  71.       print("No fuel available! :(")
  72.       return
  73.     end
  74.     turtle.placeUp()
  75.   end
  76.   print("Reached target y-level.. start mining")
  77. end
  78.  
  79. local function moveToTop(amount)
  80.   turtle.select(fuelSlot + 1)
  81.   for i = 1, amount do
  82.     if CheckFuel() then
  83.       up()
  84.     else
  85.       print("No fuel available! :(")
  86.       return
  87.     end
  88.     turtle.placeDown()
  89.   end
  90.   print("Reached start level.. empting inventory")
  91. end
  92.  
  93. -- Returns false if inventory full
  94. local function ContinueDigging()
  95.   -- Check inventory
  96.   if (turtle.getItemCount(16) > 0) then
  97.     return false
  98.   end
  99.  
  100.   return true
  101. end
  102.  
  103. local function CheckFuel()
  104.   if (turtle.getFuelLevel() >= 100) then
  105.     return true
  106.   else
  107.     local oldSlot = turtle.getSelectedSlot()
  108.     turtle.select(FuelSlot)
  109.     if turtle.refuel(100) then
  110.       turtle.select(oldSlot)
  111.       return true
  112.     else
  113.       turtle.select(oldSlot)
  114.       return false
  115.     end
  116.   end
  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