Advertisement
dracoix

Turtle Line

Jul 13th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 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 swap = 1
  25. local aDump = 0
  26. local cPos = 0
  27.  
  28. local function dump()
  29.     for i=1, gDump do
  30.         turtle.back()
  31.     end
  32.     turtle.turnLeft()
  33.     turtle.turnLeft()
  34.     turtle.select(1)
  35.     turtle.drop()
  36.     turtle.select(2)
  37.     turtle.drop()
  38.     turtle.select(3)
  39.     turtle.drop()
  40.     turtle.select(4)
  41.     turtle.drop()
  42.     turtle.select(5)
  43.     turtle.drop()
  44.     turtle.select(6)
  45.     turtle.drop()
  46.     turtle.select(7)
  47.     turtle.drop()
  48.     turtle.select(8)
  49.     turtle.drop()
  50.     turtle.select(9)
  51.     turtle.drop()
  52.     turtle.select(1)
  53.     for i=1, gDump do
  54.         turtle.back()
  55.     end
  56.     turtle.turnLeft()
  57.     turtle.turnLeft()
  58. end
  59.  
  60. local function collect()
  61.     print( "Collect Called" )
  62.     collected = collected + 1
  63.     if math.fmod(collected, 25) == 0 then
  64.         print( "Mined "..collected.." blocks." )
  65.     end
  66.    
  67.     for n=1,9 do
  68.         if turtle.getItemCount(n) == 0 then
  69.             return true
  70.         end
  71.     end
  72.    
  73.     print( "No empty slots left." )
  74.     return false
  75. end
  76.  
  77. local function tryPush()
  78.     if not turtle.forward() then
  79.         if turtle.dig() then
  80.             if not collect() then
  81.                 return false
  82.             end
  83.         else
  84.             -- give sand a chance to fall
  85.             sleep(0.8)
  86.             if turtle.dig() then
  87.                 if not collect() then
  88.                     return false
  89.                 end
  90.             else
  91.                 return false
  92.             end
  93.         end
  94.     end
  95.     return true
  96. end
  97.  
  98. local function tryDown()
  99. print (" Down Called ")
  100.     if not turtle.down() then
  101.         print (" Can't move down, trying dig... ")
  102.         if turtle.digDown() then
  103.             if not collect() then
  104.                 print (" Can't collect down, oddly. ")
  105.                 return false
  106.             end
  107.         end
  108.         if not turtle.down() then
  109.             print (" Can't move down, oddly. ")
  110.             return false
  111.         end
  112.     end
  113.  
  114.     if math.fmod( depth, 10 ) == 0 then
  115.         print( "Descended "..depth.." metres." )
  116.     end
  117.     return true
  118. end
  119.  
  120. local function goHome()
  121. print( " Coming Home... " )
  122.     for i=depth, 0, -1 do
  123.         depth = i
  124.         turtle.up()
  125.     end
  126.     if swap > 0 then
  127.         turtle.turnLeft()
  128.         turtle.turnLeft()
  129.         swap = -1
  130.     end
  131.     for i=cPos, 0, -1 do
  132.         turtle.forward()
  133.         cPos = i
  134.     end
  135.     print ( "Now Home" )
  136.     turtle.turnLeft()
  137.     turtle.turnLeft()
  138.     swap = 1
  139.     dump()
  140. end
  141.  
  142. local function goOut()
  143.     for x=1,size,1 do
  144.         if tryPush() == false then
  145.             goHome()
  146.         end
  147.         cPos = x-1
  148.         print (x)
  149.     end
  150. end
  151.  
  152. local function comeBack()
  153.     for x=size,1,-1 do
  154.         if tryPush() == false then
  155.             goHome()
  156.         end
  157.         cPos = x-1
  158.         print (x)
  159.     end
  160. end
  161.  
  162. local reseal = false
  163. local cPos = 0
  164. local done = false
  165.  
  166. local function digall()
  167.     print( "I'm home and now (re)digging..." )
  168.     if turtle.digDown() then
  169.         reseal = true
  170.     else
  171.         print( "NO HOME BLOCK! Terminating! :(" )
  172.         return false
  173.     end
  174.     for i=0, tStop do
  175.         if swap > 0 then
  176.             goOut()
  177.         else
  178.             comeBack()
  179.         end
  180.         if tryDown() == false then
  181.             print (" Down Failed on Main ")
  182.             goHome()
  183.         end
  184.         depth = depth + 1
  185.         turtle.turnLeft()
  186.         turtle.turnLeft()
  187.         if swap == 1 then
  188.             swap = -1
  189.         else
  190.             swap = 1
  191.         end
  192.     end
  193.     goHome()
  194.     print( "COMPLETE" )
  195. end
  196.  
  197.  
  198.  
  199. digall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement