vvenaya

Untitled

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