Advertisement
dracoix

Turtle Line 1.4

Jul 16th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.52 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 3 then
  3.     print( "Usage: <program> <far> <dump> <depth>" )
  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 = 255
  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. local bedrock = false
  30. local fcDown = 0
  31. local fcPush = 0
  32.  
  33. local function findFuel()
  34.     if turtle.getFuelLevel() < 1 then
  35.         for i = 1, 16 do -- loop through the slots
  36.             turtle.select(i) -- change to the slot
  37.             if turtle.refuel(0) then -- if it's valid fuel
  38.                 local halfStack = math.ceil(turtle.getItemCount(i)/2) -- work out half of the amount of fuel in the slot
  39.                 turtle.refuel(halfStack) -- consume half the stack as fuel
  40.                 return true
  41.             end
  42.         end
  43.     end
  44.     return false
  45. end
  46.  
  47. local function proxyDown()
  48.     if not turtle.down() then
  49.         print ("Move down failed. Checking fuel...")
  50.         if not findFuel() then
  51.             print ("Out of fuel!")
  52.             return false
  53.         else
  54.             if not turtle.down() then
  55.                 print ("Something in the way! Bedrock?")
  56.                 return false
  57.             end
  58.         end
  59.     end
  60.     return true
  61. end
  62.  
  63. local function proxyForward()
  64.     if not turtle.forward() then
  65.         print ("Move forward failed. Checking fuel...")
  66.         if not findFuel() then
  67.             print ("Out of fuel!")
  68.             return false
  69.         else
  70.             if not turtle.forward() then
  71.                 print ("Something in front!")
  72.                 return false
  73.             end
  74.         end
  75.     end
  76.     return true
  77. end
  78.  
  79. local function proxyUp()
  80.     if not turtle.up() then
  81.         print ("Move forward failed. Checking fuel...")
  82.         if not findFuel() then
  83.             print ("Out of fuel!")
  84.             return false
  85.         else
  86.             if not turtle.up() then
  87.                 print ("Something in front!")
  88.                 return false
  89.             end
  90.         end
  91.     end
  92.     return true
  93. end
  94.  
  95. local function dump()
  96.     while turtle.back() == false do
  97.         sleep(0.8)
  98.     end
  99.     while turtle.back() == false do
  100.         sleep(0.8)
  101.     end
  102.     turtle.turnLeft()
  103.     turtle.turnLeft()
  104.     turtle.select(1)
  105.     turtle.drop()
  106.     turtle.select(2)
  107.     turtle.drop()
  108.     turtle.select(3)
  109.     turtle.drop()
  110.     turtle.select(4)
  111.     turtle.drop()
  112.     turtle.select(5)
  113.     turtle.drop()
  114.     turtle.select(6)
  115.     turtle.drop()
  116.     turtle.select(7)
  117.     turtle.drop()
  118.     turtle.select(8)
  119.     turtle.drop()
  120.     turtle.select(9)
  121.     turtle.drop()
  122.     turtle.select(1)
  123.     while turtle.back() == false do
  124.         sleep(0.8)
  125.     end
  126.     while turtle.back() == false do
  127.         sleep(0.8)
  128.     end
  129.     turtle.turnLeft()
  130.     turtle.turnLeft()
  131. end
  132.  
  133. local function collect()
  134.     -- print( "Collect Called" )
  135.     collected = collected + 1
  136.     if math.fmod(collected, 25) == 0 then
  137.         print( "Mined "..collected.." blocks." )
  138.     end
  139.    
  140.     for n=1,9 do
  141.         if turtle.getItemCount(n) == 0 then
  142.             return true
  143.         end
  144.     end
  145.    
  146.     print( "No empty slots left." )
  147.     print (" Going Home to Dump. ")
  148.     lastDepth = depth
  149.     goHome()
  150.     return false
  151. end
  152.  
  153. local function tryPush()
  154.     if not proxyForward() then
  155.         if turtle.dig() then
  156.             collect()
  157.         else
  158.             fcPush = fcPush + 1
  159.             -- give sand a chance to fall
  160.             sleep(0.8)
  161.             if turtle.dig() then
  162.                 collect()
  163.             else
  164.                 fcPush = fcPush + 1
  165.                 return false
  166.             end
  167.         end
  168.     else
  169.         cPos = cPos + 1
  170.     end
  171.     return true
  172. end
  173.  
  174. local function tryDown()
  175. -- print (" Down Called ")
  176.     if not proxyDown() then
  177.         print (" Can't move down, trying dig... ")
  178.         if turtle.digDown() then
  179.             collect()
  180.         else
  181.             fcDown = fcDown + 1
  182.         end
  183.         if not proxyDown() then
  184.             fcDown = fcDown + 1
  185.             print ("Move down failed.")
  186.         else
  187.             depth = depth + 1
  188.         end
  189.     else
  190.         depth = depth + 1
  191.     end
  192.     print (" I moved down. ")
  193.     return true
  194. end
  195.  
  196. local function goHome()
  197. print( " Coming Home... From Depth: "..depth )
  198.     while depth > 0 do
  199.         if proxyUp() == true then
  200.             print ( "Updiddy: "..depth  )
  201.             depth = depth - 1
  202.         else
  203.             turtle.digUp()
  204.             print ( "Up is blocked, panicing." )
  205.         end
  206.        
  207.     end
  208.  
  209.     while cPos > 0 do
  210.         if turtle.back() == false then
  211.             print ( "Back is blocked, panicing." )
  212.             turtle.turnLeft()
  213.             turtle.turnLeft()
  214.             turtle.dig()
  215.             turtle.turnLeft()
  216.             turtle.turnLeft()
  217.         else
  218.             cPos = cPos - 1
  219.         end
  220.     end
  221.     print ( "Now Home, failed: "..(fcDown+fcPush).." times." )
  222.  
  223.     dump()
  224.    
  225.     if done == false then
  226.     print ( " Going back to work. " )
  227.         while depth < lastDepth do
  228.             if tryDown() == true then
  229.                 -- D
  230.             end
  231.         end
  232.         print ( " Now Working... " )
  233.     end
  234. end
  235.  
  236. local function goOut()
  237.     while cPos < size do
  238.         if tryPush() == false then
  239.             return false
  240.             -- print( "Not Moving " )
  241.         else
  242.             -- print( "Moving" )
  243.         end
  244.     end
  245. end
  246.  
  247.  
  248. local function comeBack()
  249.     while cPos > 0 do
  250.         if turtle.back() == true then
  251.             cPos = cPos - 1
  252.         else
  253.             turtle.turnLeft()
  254.             turtle.turnLeft()
  255.             turtle.dig()
  256.             turtle.turnLeft()
  257.             turtle.turnLeft()
  258.             -- print ( "Move your ass" )
  259.         end
  260.         -- print (cPos)
  261.     end
  262. end
  263.  
  264. local function digDown()
  265.     if tryDown() == false then
  266.         lastDepth = depth
  267.         return false
  268.     else
  269.         -- depth = depth + 1
  270.         print ( " Layer: "..depth )
  271.     end
  272. end
  273.  
  274. local fcTotal = 0
  275.  
  276. local function digall()
  277.     while (done ~= true) do
  278.         fcTotal = fcPush + fcDown
  279.         print ("Fails: "..fcPush.." : "..fcDown)
  280.         if fcTotal > 2 then
  281.             done = true
  282.         end
  283.         if (depth > tStop) then
  284.             done = true
  285.         end
  286.         goOut()
  287.         comeBack()
  288.         if digDown() == false then
  289.             done = true
  290.         end
  291.     end
  292.     print (" Done, Coming Home Last Time ")
  293.     done = true
  294.     goHome()
  295. end
  296.  
  297. digall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement