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.  
  79.     collected = collected + 1
  80.     if math.fmod(collected, 25) == 0 then
  81.         print( "Mined "..collected.." items." )
  82.     end
  83. end
  84. local function refuel()
  85.     local fuelLevel = turtle.getFuelLevel()
  86.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  87.         return
  88.     end
  89.    
  90.     local function tryRefuel()
  91.         for n=1,16 do
  92.             if turtle.getItemCount(n) > 0 then
  93.                 turtle.select(n)
  94.                 if turtle.refuel(1) then
  95.                     turtle.select(1)
  96.                     return true
  97.                 end
  98.             end
  99.         end
  100.         turtle.select(1)
  101.         return false
  102.     end
  103.    
  104.     if not tryRefuel() then
  105.         print( "Add more fuel to continue." )
  106.         while not tryRefuel() do
  107.             sleep(1)
  108.         end
  109.         print( "Resuming Tunnel." )
  110.     end
  111. end
  112. local function tryDig()
  113.     while turtle.detect() do
  114.         if turtle.dig() then
  115.             collect()
  116.             sleep(0.5)
  117.         else
  118.             return false
  119.         end
  120.     end
  121.     return true
  122. end
  123. local function tryDigUp()
  124.     while turtle.detectUp() do
  125.         if turtle.digUp() then
  126.             collect()
  127.             sleep(0.5)
  128.         else
  129.             return false
  130.         end
  131.     end
  132.     return true
  133. end
  134. local function tryDigDown()
  135.  while turtle.detectDown() do
  136.   if turtle.digDown() then
  137.     collect()
  138.     sleep(0.5)
  139.  else
  140.    return false
  141.   end
  142.  end
  143.  return true
  144. end
  145. local function tryUp()
  146.     refuel()
  147.     while not turtle.up() do
  148.         if turtle.detectUp() then
  149.             if not tryDigUp() then
  150.                 return false
  151.             end
  152.         elseif turtle.attackUp() then
  153.             collect()
  154.         else
  155.             sleep( 0.5 )
  156.         end
  157.     end
  158.     return true
  159. end
  160. local function tryDown()
  161.     refuel()
  162.     while not turtle.down() do
  163.         if turtle.detectDown() then
  164.             if not tryDigDown() then
  165.                 return false
  166.             end
  167.         elseif turtle.attackDown() then
  168.             collect()
  169.         else
  170.             sleep( 0.5 )
  171.         end
  172.     end
  173.     return true
  174. end
  175. local function tryForward()
  176.     refuel()
  177.     while not turtle.forward() do
  178.         if turtle.detect() then
  179.             if not tryDig() then
  180.                 return false
  181.             end
  182.         elseif turtle.attack() then
  183.             collect()
  184.         else
  185.             sleep( 0.5 )
  186.         end
  187.     end
  188.     return true
  189. end
  190. local function dumpCobble()
  191.     for slot=3,16 do
  192.         turtle.select(slot)
  193.         local slotQuantity=turtle.getItemCount(slot)
  194.         if slotQuantity > 0 then
  195.             if turtle.compareTo(cobbleSlot) then
  196.                 cobbleDumped = cobbleDumped + slotQuantity
  197.                 turtle.drop()
  198.             end
  199.         end
  200.     end
  201. end
  202. local function dropCobbleWhenNecessary()
  203.     if math.fmod(collected,frequencyOfCobbleRemoval)==0 then
  204.        dumpCobble()
  205.     end
  206. end
  207. local function placeTorchWhenNecessary(x,y)
  208.     if math.fmod(x,8)==1 and math.fmod(y,8)==1 then
  209.         turtle.select(1)
  210.         if (turtle.getItemCount(torchSlot)==0) then
  211.             print("Out of torches, please resupply!")
  212.             return false
  213.         else
  214.             turtle.placeDown()
  215.         end
  216.     end
  217.     return true
  218. end
  219. local function placeEnderChest()
  220.     turtle.select(enderChestSlot)
  221.     turtle.placeDown()
  222. end
  223. local function retrieveEnderChest()
  224.     turtle.select(enderChestSlot)
  225.     turtle.digDown()   
  226. end
  227. local function processContent()
  228.     if (turtle.getItemCount(enderChestSlot)==0) then
  229.         print("No enderchest found in slot"..enderChestSlot.."!")
  230.         return false
  231.     end
  232.     dumpCobble()
  233.     placeEnderChest()
  234.     for slot=3,16 do
  235.         turtle.select(slot)
  236.         local slotQuantity=turtle.getItemCount(slot)
  237.         itemsToEnder = itemsToEnder + slotQuantity
  238.         turtle.dropDown()
  239.     end
  240.     retrieveEnderChest()
  241. end
  242. local function DumpStatistics()
  243.     print( "Statistics:")
  244.     print( string.rep("=",20))
  245.     print( "Mined "..collected.." blocks total." )
  246.     print( "Discarded "..cobbleDumped.." pieces of cobble.")
  247.     print( itemsToEnder.." items send to storage.")
  248.     print( "Statistics:")
  249. end
  250. local function mine()
  251.     print( "Tunneling..." )
  252.     local rotation=0
  253.     for m=1,width do
  254.         for n=1,length-1 do
  255.             tryDigUp()
  256.             tryDigDown()               
  257.             if  doProcessContent and (math.fmod(collected+1,enderDumpInterval)==0) then
  258.                 processContent()
  259.             end
  260.             dropCobbleWhenNecessary(n)
  261.             placeTorchWhenNecessary(n,m)           
  262.             tryDig()
  263.             if not tryForward() then
  264.                 return false
  265.             end
  266.         end    
  267.         tryDigUp()
  268.         tryDigDown()               
  269.         if m < width then
  270.             if rotation==0 then
  271.                 turtle.turnRight()
  272.             else
  273.                 turtle.turnLeft()
  274.             end
  275.             tryDig()
  276.             if not tryForward() then
  277.                 return false
  278.             end
  279.             if rotation==0 then
  280.                 turtle.turnRight()
  281.             else
  282.                 turtle.turnLeft()
  283.             end
  284.             rotation = 1 - rotation
  285.         end
  286.     end
  287.     processContent()
  288.     return true
  289. end
  290.  
  291. if mine() then
  292.     print( "Tunnel complete." )
  293. else
  294.     print( "Tunnel aborted prematurely.")
  295. end
Advertisement
Add Comment
Please, Sign In to add comment