Advertisement
migszxc

computer craft StripMiner

Jul 29th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. if #tArgs ~= 2 then
  4.     print("Usage: strip <# of strips> <strip depth>")
  5.     return
  6. end
  7.  
  8. local stripCount=tonumber(tArgs[1])
  9. local stripDepth=tonumber(tArgs[2])
  10. local heading="forward"
  11. local xPos,zPos=0,0
  12.  
  13. function faceLeft()
  14.     if heading =="forward" then
  15.         turtle.turnLeft()
  16.     elseif heading=="backward" then
  17.         turtle.turnRight()
  18.     elseif heading=="left" then
  19.    
  20.     elseif heading=="right" then
  21.         turtle.turnLeft()
  22.         turtle.turnLeft()
  23.     else
  24.         print("error invalid heading fL")
  25.         return
  26.     end
  27.     heading="left"
  28. end
  29.  
  30. function faceRight()
  31.     if heading=="forward" then
  32.         turtle.turnRight()
  33.     elseif heading=="backward" then
  34.         turtle.turnLeft()
  35.     elseif heading=="left" then
  36.         turtle.turnRight()
  37.         turtle.turnRight()
  38.     elseif heading=="right" then
  39.  
  40.     else
  41.         print("error invalid heading fR")
  42.         return
  43.     end
  44.     heading="right"
  45. end
  46.  
  47. function faceForward()
  48.     if heading=="forward" then
  49.  
  50.     elseif heading=="backward" then
  51.         turtle.turnLeft()
  52.         turtle.turnLeft()
  53.     elseif heading=="left" then
  54.         turtle.turnRight()
  55.     elseif heading=="right" then
  56.         turtle.turnLeft()
  57.     else
  58.         print("error invalid heading fF")
  59.         return
  60.     end
  61.     heading="forward"
  62. end
  63.  
  64. function faceBackward()
  65.     if heading=="forward" then
  66.         turtle.turnLeft()
  67.         turtle.turnLeft()
  68.     elseif heading=="backward" then
  69.  
  70.     elseif heading=="left" then
  71.         turtle.turnLeft()
  72.     elseif heading=="right" then
  73.         turtle.turnRight()
  74.     else
  75.         print("error invalid heading fB")
  76.         return
  77.     end
  78.     heading="backward"
  79. end
  80.  
  81. function canRTH()
  82.     local xDistance=math.abs(xPos)
  83.     local zDistance=math.abs(zPos)
  84.     if (xDistance + zDistance + 2) > turtle.getFuelLevel() then
  85.         return false
  86.     else
  87.         return true
  88.     end
  89. end
  90.  
  91. function moveForward()
  92.     while not turtle.forward() do
  93.         if turtle.detect() then
  94.             turtle.dig()
  95.             sleep(0.8)
  96.         end
  97.         if turtle.attack() then
  98.            
  99.         end
  100.     end
  101. end
  102.  
  103. function goTo(x,z)
  104.     local distanceToTarget = xPos-x+zPos-z
  105.    
  106.     if canRTH() and distanceToTarget+2<turtle.getFuelLevel() then
  107.         if z>zPos then
  108.             faceForward()
  109.             for i=1,z-zPos do
  110.                 if turtle.detect() then
  111.                     turtle.dig()
  112.                 end
  113.                 moveForward()
  114.                 if turtle.detectUp() then
  115.                     turtle.digUp()
  116.                 end
  117.                 turtle.suck()
  118.                 zPos=zPos+1
  119.                 print("forwards1")
  120.             end
  121.  
  122.         end
  123.  
  124.         if z<zPos then
  125.             faceBackward()
  126.  
  127.             for i=1,zPos-z do
  128.                 if turtle.detect() then
  129.                     turtle.dig()
  130.                 end
  131.                 moveForward()
  132.                 if turtle.detectUp() then
  133.                     turtle.digUp()
  134.                 end
  135.                 turtle.suck()
  136.                 zPos=zPos-1
  137.                 print("backwards1")
  138.             end
  139.  
  140.         end
  141.  
  142.         if x>xPos then
  143.             faceRight()
  144.  
  145.             for i=1,x-xPos do
  146.                 if turtle.detect() then
  147.                     turtle.dig()
  148.                 end
  149.                 moveForward()
  150.                 if turtle.detectUp() then
  151.                     turtle.digUp()
  152.                 end
  153.                 turtle.suck()
  154.                 xPos=xPos+1
  155.                 print("right1")
  156.             end
  157.  
  158.         end
  159.  
  160.         if x<xPos then
  161.             faceLeft()
  162.  
  163.             for i=1,xPos-x do
  164.                 if turtle.detect() then
  165.                     turtle.dig()
  166.                 end
  167.                     moveForward()
  168.                 if turtle.detectUp() then
  169.                     turtle.digUp()
  170.                 end
  171.                 turtle.suck()
  172.                 xPos=xPos-1
  173.                 print("left1")
  174.             end
  175.  
  176.         end
  177.     else
  178.         local lastX,lastZ=xPos,zPos
  179.         returnToHome()
  180.         print("I need fuel in slot 1, then press enter")
  181.         io.read()
  182.         turtle.select(1)
  183.         turtle.refuel()
  184.         print("Resuming..")
  185.         goTo(lastX,lastZ)
  186.     end
  187. end
  188.  
  189. function returnToHome()
  190.     goTo(0,zPos)
  191.     goTo(0,0)
  192.     faceLeft()
  193.     for i=1,16 do
  194.         turtle.select(i)
  195.         turtle.drop()
  196.     end
  197.     turtle.select(1)
  198.     faceForward()
  199. end
  200.  
  201. goTo(0,1)
  202. for i=1,stripCount do
  203.     sleep(1)
  204.     goTo(-stripDepth,i*3)
  205.     goTo(0,i*3)
  206.     goTo(stripDepth,i*3)
  207. end
  208.  
  209. returnToHome()
  210. print("Finished. Mined " .. stripCount .. "strips for " .. stripDepth .. " blocks each.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement