vvenaya

Untitled

Jan 1st, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  Expected slot assignment/content:
  3.  Slot 1: 64 Torches
  4.  Slot 2: 1 Cobble (reserved for cobble compare)
  5.  Slot 3: Enderchest
  6. --]]
  7.  
  8. -- Parameters
  9.  
  10. local torchSlot = 1                     -- Inventory slot of torches
  11. local cobbleSlot = 2                    -- Inventory slot of atleast one cobble
  12. local enderChestSlot = 3                -- Inventory slot of enderchest to be used for offloading
  13. local frequencyOfCobbleRemoval = 30     -- Remove cobble every n blocks mined
  14. local enderDumpInterval = 100 -- Dump content into an enderchest every 100 blocks mined
  15.  
  16. -- Variables
  17.  
  18. local doProcessContent = false
  19. local depth = 0
  20. local collected = 0
  21. local itemsToEnder = 0
  22. local cobbleDumped = 0
  23.  
  24. -- Process commandline
  25.  
  26. local tArgs = { ... }
  27. if #tArgs < 2 then
  28.     print( "Usage: Area <length> <width> [Process]" )  
  29.     print( "======================================" )
  30.     print( "Process If true, content -> Enderchest")
  31.     print( "")
  32.     print( "Expected content in inventory of turtle:" )
  33.     print( "Slot 1: Torches" )
  34.     print( "Slot 2: Cobble (at least 1)")
  35.     print( "Slot 3: Enderchest (only if Process=true)")
  36.     return
  37. end
  38.  
  39. local length = tonumber( tArgs[1] )
  40. if length < 1 then
  41.     print( "Area length must be 1+" )
  42.     return
  43. end
  44.  
  45. local width = 1
  46.  
  47. if #tArgs > 1 then
  48.     width = tonumber( tArgs[2] )
  49.     if width < 1 then  
  50.         print( "Area width must be 1+" )
  51.         return
  52.     end
  53.     if #tArgs > 2 then
  54.         doProcessContent = tArgs[3]:lower()=="true"
  55.     end
  56. end
  57.  
  58. local function checkSlot(slotNr,description)
  59.    if turtle.getItemCount(slotNr)==0 then
  60.       print("Expected item '"..description.."' in slot "..slotNr..".")
  61.       return false
  62.    end
  63.    return true
  64. end
  65.  
  66. -- Check slots
  67.  
  68. if not checkSlot(torchSlot,"torches") then return end
  69. if not checkSlot(cobbleSlot,"cobbleStone (atleast 1)") then return end
  70. if doProcessContent then
  71.    if not checkSlot(enderChestSlot,"enderChest") then return end
  72. end
  73.  
  74. -- Main Code
  75.  
  76. local function collect()
  77.  
  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
Advertisement
Add Comment
Please, Sign In to add comment