vvenaya

Untitled

Jan 1st, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  Expected slot assignment/content
  3.  
  4.  Slot 1: 64 Torches
  5.  Slot 2: 1 Cobble (reserved for cobble compare)
  6.  Slot 3: Enderchest
  7. --]]
  8.  
  9. -- Parameters
  10.  
  11. local torchSlot = 1                     -- Inventory slot of torches
  12. local cobbleSlot = 2                    -- Inventory slot of atleast one cobble
  13. local enderChestSlot = 3                -- Inventory slot of enderchest to be used for offloading
  14. local frequencyOfCobbleRemoval = 30     -- Remove cobble every n blocks mined
  15. local enderDumpInterval = 100 -- Dump content into an enderchest every 100 blocks mined
  16.  
  17. -- Variables
  18.  
  19. local doProcessContent = false
  20. local depth = 0
  21. local collected = 0
  22.  
  23. -- Process commandline
  24.  
  25. local tArgs = { ... }
  26. if #tArgs < 2 then
  27.     print( "Usage: Area <length> <width> [Process]" )  
  28.     print( "======================================" )
  29.     print( "Process If true, content -> Enderchest")
  30.     print( "")
  31.     print( "Expected content in inventory of turtle:" )
  32.     print( "Slot 1: Torches" )
  33.     print( "Slot 2: Cobble (at least 1)")
  34.     print( "Slot 3: Enderchest (only if Process=true)")
  35.     return
  36. end
  37.  
  38. local length = tonumber( tArgs[1] )
  39. if length < 1 then
  40.     print( "Area length must be 1+" )
  41.     return
  42. end
  43.  
  44. local width = 1
  45.  
  46. if #tArgs > 1 then
  47.     width = tonumber( tArgs[2] )
  48.     if width < 1 then  
  49.         print( "Area width must be 1+" )
  50.         return
  51.     end
  52.     if #tArgs > 2 then
  53.         doProcessContent = tArgs[3]:lower()=="true"
  54.     end
  55. end
  56.  
  57. local function checkSlot(slotNr,description)
  58.    if turtle.getItemCount(slotNr)==0 then
  59.       print("Expected item '"..description.."' in slot "..slotNr..".")
  60.       return false
  61.    end
  62.    return true
  63. end
  64.  
  65. -- Check slots
  66.  
  67. if not checkSlot(torchSlot,"torches") then return end
  68. if not checkSlot(cobbleSlot,"cobbleStone (atleast 1)") then return end
  69. if doProcessContent then
  70.    if not checkSlot(enderChestSlot,"enderChest") then return end
  71. end
  72.  
  73. -- Main Code
  74.  
  75. local function collect()
  76.  
  77.     collected = collected + 1
  78.     if math.fmod(collected, 25) == 0 then
  79.         print( "Mined "..collected.." items." )
  80.     end
  81. end
  82. local function refuel()
  83.     local fuelLevel = turtle.getFuelLevel()
  84.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  85.         return
  86.     end
  87.    
  88.     local function tryRefuel()
  89.         for n=1,16 do
  90.             if turtle.getItemCount(n) > 0 then
  91.                 turtle.select(n)
  92.                 if turtle.refuel(1) then
  93.                     turtle.select(1)
  94.                     return true
  95.                 end
  96.             end
  97.         end
  98.         turtle.select(1)
  99.         return false
  100.     end
  101.    
  102.     if not tryRefuel() then
  103.         print( "Add more fuel to continue." )
  104.         while not tryRefuel() do
  105.             sleep(1)
  106.         end
  107.         print( "Resuming Tunnel." )
  108.     end
  109. end
  110. local function tryDig()
  111.     while turtle.detect() do
  112.         if turtle.dig() then
  113.             collect()
  114.             sleep(0.5)
  115.         else
  116.             return false
  117.         end
  118.     end
  119.     return true
  120. end
  121. local function tryDigUp()
  122.     while turtle.detectUp() do
  123.         if turtle.digUp() then
  124.             collect()
  125.             sleep(0.5)
  126.         else
  127.             return false
  128.         end
  129.     end
  130.     return true
  131. end
  132. local function tryDigDown()
  133.  while turtle.detectDown() do
  134.   if turtle.digDown() then
  135.     collect()
  136.     sleep(0.5)
  137.  else
  138.    return false
  139.   end
  140.  end
  141.  return true
  142. end
  143. local function tryUp()
  144.     refuel()
  145.     while not turtle.up() do
  146.         if turtle.detectUp() then
  147.             if not tryDigUp() then
  148.                 return false
  149.             end
  150.         elseif turtle.attackUp() then
  151.             collect()
  152.         else
  153.             sleep( 0.5 )
  154.         end
  155.     end
  156.     return true
  157. end
  158. local function tryDown()
  159.     refuel()
  160.     while not turtle.down() do
  161.         if turtle.detectDown() then
  162.             if not tryDigDown() then
  163.                 return false
  164.             end
  165.         elseif turtle.attackDown() then
  166.             collect()
  167.         else
  168.             sleep( 0.5 )
  169.         end
  170.     end
  171.     return true
  172. end
  173. local function tryForward()
  174.  
  175.     refuel()
  176.     while not turtle.forward() do
  177.         if turtle.detect() then
  178.             if not tryDig() then
  179.                 return false
  180.             end
  181.         elseif turtle.attack() then
  182.             collect()
  183.         else
  184.             sleep( 0.5 )
  185.         end
  186.     end
  187.     return true
  188. end
  189.  
  190. local function dropCobbleWhenNecessary(x)
  191.     if math.fmod(collected,frequencyOfCobbleRemoval)==0 then
  192.         for n=3,16 do
  193.             turtle.select(n)
  194.             if turtle.compareTo(cobbleSlot) then
  195.                 turtle.drop()
  196.             end
  197.         end
  198.     end
  199. end
  200. local function placeTorchWhenNecessary(x,y)
  201.     if math.fmod(x,8)==1 and math.fmod(y,8)==1 then
  202.         turtle.select(1)
  203.         if (turtle.getItemCount(torchSlot)==0) then
  204.             print("Out of torches, please resupply!")
  205.             return false
  206.         else
  207.             turtle.placeDown()
  208.         end
  209.     end
  210.     return true
  211. end
  212. local function processContent()
  213.     if not doProcessContent then
  214.        return
  215.     end
  216.  
  217.     if (turtle.getItemCount(torchSlot)==0) then
  218.         print("No enderchest found Out of torches, please resupply!")
  219.         return false
  220.     end
  221.    
  222.     -- place chest
  223.     turtle.select(enderChestSlot)
  224.     turtle.placeDown()
  225.     --
  226.     print("1")
  227.     for slot=3,16 do
  228.     print("2:"..slot)
  229.         turtle.select(slot)
  230.         print("3")
  231.         if turtle.compareTo(cobbleSlot) then
  232.         print("4a")
  233.             turtle.drop()
  234.         else
  235.         print("4b")
  236.             turtle.dropDown()
  237.         end
  238.         print("5")
  239.     end
  240.     print("6")
  241.     --retrieve chest
  242.     turtle.select(enderChestSlot)
  243.     print("7")
  244.     turtle.digDown()   
  245.     print("8")
  246. end
  247.  
  248. local function mine()
  249.     print( "Tunneling..." )
  250.     local rotation=0
  251.     for m=1,width do
  252.         for n=1,length-1 do
  253.             tryDigUp()
  254.             tryDigDown()               
  255.             if (math.fmod(collected,enderDumpInterval)==collected) then
  256.                 processContent()
  257.             end
  258.             dropCobbleWhenNecessary(n)
  259.             placeTorchWhenNecessary(n,m)           
  260.             tryDig()
  261.             if not tryForward() then
  262.                 return false
  263.             end
  264.         end    
  265.         tryDigUp()
  266.         tryDigDown()               
  267.         if m < width then
  268.             if rotation==0 then
  269.                 turtle.turnRight()
  270.             else
  271.                 turtle.turnLeft()
  272.             end
  273.             tryDig()
  274.             if not tryForward() then
  275.                 return false
  276.             end
  277.             if rotation==0 then
  278.                 turtle.turnRight()
  279.             else
  280.                 turtle.turnLeft()
  281.             end
  282.             rotation = 1 - rotation
  283.         end
  284.     end
  285.     processContent()
  286.     return true
  287. end
  288.  
  289. if mine() then
  290.     print( "Tunnel complete." )
  291. else
  292.     print( "Tunnel aborted prematurely.")
  293. end
  294. print( "Mined "..collected.." blocks total." )
Advertisement
Add Comment
Please, Sign In to add comment