vvenaya

Untitled

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