Feldherren

miner v1.2

May 6th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function find(itemName)
  2.     for curSlot = 1,16 do
  3.         if turtle.getItemCount(curSlot) >= 1 then
  4.             local data = turtle.getItemDetail(curSlot)
  5.             if data.name == itemName then
  6.                 return curSlot
  7.             end
  8.         end
  9.     end
  10.     return false
  11. end
  12.  
  13. local tArgs = { ... }
  14. if #tArgs ~= 1 then
  15.     print( "Usage: miner <distance>" )
  16.     return
  17. end
  18.  
  19. local maxDist = tonumber( tArgs[1] )
  20. if maxDist < 1 then
  21.     print( "Distance must be positive" )
  22.     return
  23. end
  24.  
  25. local function getFreeInventorySpace()
  26.     local freeSpace = 16
  27.     for curSlot = 1,16 do
  28.         if turtle.getItemCount(curSlot) ~= 0 then
  29.             freeSpace = freeSpace - 1
  30.         end
  31.     end
  32.     return freeSpace
  33. end
  34.  
  35. local function sortInventory()
  36.     local inventory = {}
  37.     for curSlot=1,16 do
  38.         if turtle.getItemCount(curSlot) ~= 0 then
  39.             inventory[curSlot] = turtle.getItemDetail(curSlot)
  40.         else
  41.             inventory[curSlot] = "empty"
  42.         end
  43.     end
  44.     for slotA=1,16 do
  45.         if inventory[slotA] ~= "empty" then
  46.             for slotB=1,16 do
  47.                 if inventory[slotB] ~= "empty" then
  48.                     if slotA ~= slotB then
  49.                         if (inventory[slotA].name == inventory[slotB].name) and (inventory[slotA].damage == inventory[slotB].damage) then
  50.                             if inventory[slotA].count + inventory[slotB].count <= 64 then
  51.                                 turtle.select(slotB)
  52.                                 turtle.transferTo(slotA)
  53.                                 inventory[slotB] = "empty"
  54.                             end
  55.                         end
  56.                     end
  57.                 end
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. local function digAround()
  64.     while turtle.detectUp() do
  65.         turtle.digUp()
  66.     end
  67.     if turtle.detectDown() then
  68.         local success, data = turtle.inspectDown()
  69.         if success then
  70.             if not (data.name == "minecraft:cobblestone") and not (data.name == "minecraft:stone") and not (data.name == "minecraft:sand") and not (data.name == "minecraft:dirt") and not (data.name == "minecraft:gravel") and not (data.name == "chisel:marble2") and not (data.name == "chisel:limestone2") then
  71.                 turtle.digDown()
  72.             end
  73.         end
  74.        
  75.     end
  76.     if find("minecraft:cobblestone") then
  77.         turtle.select(find("minecraft:cobblestone"))
  78.         turtle.placeDown()
  79.     elseif find("minecraft:stone") then
  80.         turtle.select(find("minecraft:stone"))
  81.         turtle.placeDown()
  82.     end
  83. end
  84.  
  85. local function checkFuel(level)
  86.     if (turtle.getFuelLevel() <= level) and (find("minecraft:coal")) then
  87.         turtle.select(find("minecraft:coal"))
  88.         turtle.refuel(1)
  89.     end
  90. end
  91.  
  92. local function returnToStart(travelled, continue)
  93.     if continue then
  94.         print("Returning to deposit items, then continuing")
  95.     else
  96.         print("Returning to deposit items; job done")
  97.     end
  98.     turtle.turnLeft()
  99.     turtle.turnLeft()
  100.     for curDist=0,travelled do
  101.         checkFuel(10)
  102.         local success, data = turtle.inspect()
  103.         if success then
  104.             if data.name ~= "minecraft:chest" then
  105.                 while not turtle.forward() do
  106.                     turtle.dig()
  107.                 end
  108.             end
  109.         else
  110.             while not turtle.forward() do
  111.                 turtle.dig()
  112.             end
  113.         end
  114.     end
  115.  
  116.     local success, data = turtle.inspect()
  117.     if success then
  118.         if data.name == "minecraft:chest" then
  119.             for curSlot = 1,16 do
  120.                 if turtle.getItemCount(curSlot) ~= 0 then
  121.                     block = turtle.getItemDetail(curSlot)
  122.                     if (block.name ~= "minecraft:coal") and (block.name ~= "minecraft:torch") and (block.name ~= "tconstruct:stone_torch") then
  123.                         turtle.select(curSlot)
  124.                         turtle.drop()
  125.                     end
  126.                 end
  127.             end
  128.         end
  129.     end
  130.  
  131.     if continue then
  132.         turtle.turnLeft()
  133.         turtle.turnLeft()
  134.         for curDist=0,travelled-1 do
  135.             checkFuel(10)
  136.             local success, data = turtle.inspect()
  137.             if success then
  138.                 if data.name ~= "minecraft:chest" then
  139.                     while not turtle.forward() do
  140.                         turtle.dig()
  141.                     end
  142.                 end
  143.             else
  144.                 while not turtle.forward() do
  145.                     turtle.dig()
  146.                 end
  147.             end
  148.         end
  149.     end
  150. end
  151.  
  152. local travelDist = 0
  153.  
  154. while travelDist <= maxDist do
  155.     checkFuel(10)
  156.     if (find("minecraft:torch") == false) and (find("tconstruct:stone_torch") == false) then
  157.         break
  158.     end
  159.     sortInventory()
  160.     if getFreeInventorySpace() <= 1 then
  161.         returnToStart(travelDist,true)
  162.     end
  163.     digAround()
  164.     turtle.turnLeft()
  165.     while not turtle.forward() do
  166.         turtle.dig()
  167.     end
  168.     digAround()
  169.     turtle.turnRight()
  170.     turtle.turnRight()
  171.     turtle.forward()
  172.     while not turtle.forward() do
  173.         turtle.dig()
  174.     end
  175.     digAround()
  176.     turtle.turnLeft()
  177.     turtle.turnLeft()
  178.     turtle.forward()
  179.     if (travelDist % 6 == 0) and ((find("minecraft:torch") or (find("tconstruct:stone_torch"))) then
  180.         if find("tconstruct:stone_torch") then
  181.             turtle.select(find("tconstruct:stone_torch"))
  182.         else
  183.             turtle.select(find("minecraft:torch"))
  184.         end
  185.         turtle.place()
  186.     end
  187.     turtle.turnRight()
  188.     if travelDist <= maxDist then
  189.         while not turtle.forward() do
  190.             turtle.dig()
  191.         end
  192.         travelDist = travelDist + 1
  193.     end
  194. end
  195.  
  196. returnToStart(travelDist,false)
Add Comment
Please, Sign In to add comment