dracoix

Turtle Line 1.4

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