Guest User

floor

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