Advertisement
dracoix

Turtle Line 2

Jul 13th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 3 then
  3.     print( "Usage: <program> <far> <dump> <stop>" )
  4.     return
  5. end
  6.  
  7. -- Mine in a one-wide pattern until we hit something we can't dig
  8. local size = tonumber( tArgs[1] )
  9. local gDump = tonumber( tArgs[2] )
  10. local tStop = tonumber( tArgs[3] )
  11. if size < 1 then
  12.     print( "Excavate outward must be positive" )
  13.     return
  14. end
  15. if tStop < 1 then
  16.     tStop = 57
  17. end
  18. if gDump < 1 then
  19.     gDump = 1
  20. end
  21.  
  22. local depth = 0
  23. local collected = 0
  24. local aDump = 0
  25. local cPos = 0
  26. local reseal = false
  27. local done = false
  28. local lastDepth = 0
  29.  
  30. local function dump()
  31.     while turtle.back() == false do
  32.         sleep(0.8)
  33.     end
  34.     while turtle.back() == false do
  35.         sleep(0.8)
  36.     end
  37.     turtle.turnLeft()
  38.     turtle.turnLeft()
  39.     turtle.select(1)
  40.     turtle.drop()
  41.     turtle.select(2)
  42.     turtle.drop()
  43.     turtle.select(3)
  44.     turtle.drop()
  45.     turtle.select(4)
  46.     turtle.drop()
  47.     turtle.select(5)
  48.     turtle.drop()
  49.     turtle.select(6)
  50.     turtle.drop()
  51.     turtle.select(7)
  52.     turtle.drop()
  53.     turtle.select(8)
  54.     turtle.drop()
  55.     turtle.select(9)
  56.     turtle.drop()
  57.     turtle.select(1)
  58.     while turtle.back() == false do
  59.         sleep(0.8)
  60.     end
  61.     while turtle.back() == false do
  62.         sleep(0.8)
  63.     end
  64.     turtle.turnLeft()
  65.     turtle.turnLeft()
  66. end
  67.  
  68. local function collect()
  69.     -- print( "Collect Called" )
  70.     collected = collected + 1
  71.     if math.fmod(collected, 25) == 0 then
  72.         print( "Mined "..collected.." blocks." )
  73.     end
  74.    
  75.     for n=1,9 do
  76.         if turtle.getItemCount(n) == 0 then
  77.             return true
  78.         end
  79.     end
  80.    
  81.     print( "No empty slots left." )
  82.     return false
  83. end
  84.  
  85. local function tryPush()
  86.     if not turtle.forward() then
  87.         if turtle.dig() then
  88.             if not collect() then
  89.                 return false
  90.             end
  91.         else
  92.             -- give sand a chance to fall
  93.             sleep(0.8)
  94.             if turtle.dig() then
  95.                 if not collect() then
  96.                     return false
  97.                 end
  98.             else
  99.                 return false
  100.             end
  101.         end
  102.     else
  103.         cPos = cPos + 1
  104.     end
  105.     return true
  106. end
  107.  
  108. local function tryDown()
  109. -- print (" Down Called ")
  110.     if not turtle.down() then
  111.         -- print (" Can't move down, trying dig... ")
  112.         if turtle.digDown() then
  113.             if not collect() then
  114.                 -- print (" Can't collect down, oddly. ")
  115.                 return false
  116.             end
  117.         end
  118.         if not turtle.down() then
  119.             print (" Can't move down, oddly. ")
  120.             return false
  121.         end
  122.     end
  123.  
  124.     if math.fmod( depth, 10 ) == 0 then
  125.         print( "Descended "..depth.." metres." )
  126.     end
  127.     return true
  128. end
  129.  
  130. local function goHome()
  131. print( " Coming Home... " )
  132.     while depth > 0 do
  133.         if turtle.up() == true then
  134.             depth = depth - 1
  135.         else
  136.             print ( "Up is blocked, can't come home" )
  137.         end
  138.        
  139.     end
  140.  
  141.     while cPos > 0 do
  142.         if turtle.back() == false then
  143.             print ( "Back is blocked, can't come home" )
  144.         else
  145.             cPos = cPos - 1
  146.         end
  147.     end
  148.     print ( "Now Home" )
  149.  
  150.     dump()
  151.    
  152.     if not done then
  153.     print ( " Going back to work. " )
  154.         while depth < lastDepth do
  155.             if tryDown() == true then
  156.                 depth = depth + 1
  157.             end
  158.         end
  159.         print ( " Now Working... " )
  160.     end
  161. end
  162.  
  163. local function goOut()
  164.     while cPos < size do
  165.         if tryPush() == false then
  166.             lastDepth = depth
  167.             goHome()
  168.         else
  169.             -- print( "Moving" )
  170.         end
  171.     end
  172. end
  173.  
  174.  
  175. local function comeBack()
  176.     while cPos > 0 do
  177.         if turtle.back() == true then
  178.             cPos = cPos - 1
  179.         else
  180.             -- print ( "Move your ass" )
  181.         end
  182.         -- print (cPos)
  183.     end
  184. end
  185.  
  186. local function digDown()
  187.     if tryDown() == false then
  188.         lastDepth = depth
  189.         goHome()
  190.         return false
  191.     else
  192.         depth = depth + 1
  193.         print ( " Layer: "..depth )
  194.     end
  195. end
  196.  
  197. local function digall()
  198.     while depth < tStop do
  199.         goOut()
  200.         comeBack()
  201.         digDown()
  202.     end
  203.     done = true
  204.     goHome()
  205. end
  206.  
  207. digall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement