mdc_tjc

mc_quarry

Apr 11th, 2015
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. -- The identifier of this program is vHc0EfGD
  2. -- This program implements the flying quarry mining turtles
  3.  
  4. -- This is the variable declaration section of the program
  5.  
  6. quarryLength = 11 quarryWidth = quarryLength quarryDepth = 3
  7. xStart = -696 zStart = -3484 yStart = 50 orientationStart = 4
  8. chestX = -701 chestZ = -3484 chestY = 89 chestOrientation = 3
  9. progressX =0 progressZ = 0 progressY = 0 progressOrientation = 0
  10. xCoord = chestX  -- East and west coordinates
  11. zCoord = chestZ  -- North and south coordinates
  12. yCoord = chestY  -- Vertical coordinates
  13. orientation = chestOrientation
  14. directions = {"north", "east", "south", "west"} -- Directions indexed as 1 to 4
  15. xDiff = {0, 1, 0, -1}
  16. zDiff = {-1, 0, 1, 0}
  17.  
  18. -- This is the function declaration section of the program
  19.  
  20. local function tryDig() -- This function digs forward one block
  21.     while turtle.detect() do
  22.         if turtle.dig() then
  23.             sleep(0.5)
  24.         else
  25.             return false
  26.         end
  27.     end
  28.     return true
  29. end
  30.  
  31. local function tryDigUp() -- This function digs up one block
  32.     while turtle.detectUp() do
  33.         if turtle.digUp() then
  34.             sleep(0.5)
  35.         else
  36.             return false
  37.         end
  38.     end
  39.     return true
  40. end
  41.  
  42. local function tryDigDown() -- This function digs down one block
  43.     while turtle.detectDown() do
  44.         if turtle.digDown() then
  45.             sleep(0.5)
  46.         else
  47.             return false
  48.         end
  49.     end
  50.     return true
  51. end
  52.  
  53. function tLeft() -- This function turns the turtle left and updates orientation
  54.    orientation = orientation - 1
  55.    orientation = (orientation - 1) % 4
  56.    orientation = orientation + 1
  57.    turtle.turnLeft()
  58. end
  59.  
  60. function tRight()  -- This function turns the turtle right and updates orientation
  61.    orientation = orientation - 1
  62.    orientation = (orientation + 1) % 4
  63.    orientation = orientation + 1
  64.    turtle.turnRight()
  65. end
  66.    
  67. function mForward()  -- This function moves the turtle forward and updates coordinates
  68.    local moved = false
  69.    while not(moved) do
  70.       moved = turtle.forward()
  71.       end
  72.    xCoord = xCoord + xDiff[orientation]
  73.    zCoord = zCoord + zDiff[orientation]
  74. end
  75.  
  76. function mBack()  -- This function moves the turtle backward and updates coordinates
  77.    local moved = false
  78.    while not(moved) do
  79.       moved = turtle.back()
  80.       end
  81.    xCoord = xCoord - xDiff[orientation]
  82.    zCoord = zCoord - zDiff[orientation]
  83. end
  84.  
  85. function mUp()  -- This function moves the turtle upward and updates coordinates
  86.    local moved = false
  87.    while not(moved) do
  88.       moved = turtle.up()
  89.       end
  90.    yCoord = yCoord + 1
  91. end
  92.    
  93. function mDown()  -- This function moves the turtle downward and updates coordinates
  94.    local moved = false
  95.    while not(moved) do
  96.       moved = turtle.down()
  97.       end
  98.    yCoord = yCoord - 1
  99. end
  100.    
  101. function look(direction)  -- This function turns the turtle to the desired direction
  102.    while direction ~= directions[orientation] do
  103.       tRight()
  104.       end
  105. end
  106.  
  107. function moveTo(xTarget, zTarget, yTarget)
  108.    while yTarget < yCoord do
  109.       mDown()
  110.    end
  111.    while yTarget > yCoord do
  112.       mUp()
  113.    end
  114.  
  115.    if xTarget < xCoord then
  116.       look("west")
  117.       while xTarget < xCoord do
  118.          mForward()
  119.       end
  120.    end
  121.    if xTarget > xCoord then
  122.       look("east")
  123.       while xTarget > xCoord do
  124.          mForward()
  125.       end
  126.    end
  127.      
  128.    if zTarget < zCoord then
  129.       look("north")
  130.       while zTarget < zCoord do
  131.          mForward()
  132.       end
  133.    end
  134.    if zTarget > zCoord then
  135.       look("south")
  136.       while zTarget > zCoord do
  137.          mForward()
  138.       end
  139.    end
  140. end
  141.  
  142. function dumpBlocks(continue)
  143.    progressX =xCoord progressZ = zCoord progressY = yCoord progressOrientation = orientation
  144.    moveTo(chestX, chestZ, chestY) look(directions[chestOrientation])
  145.    for i = 1, 16 do
  146.       turtle.select( i )
  147.       turtle.drop(64)
  148.    end
  149.    turtle.select( 1 )
  150.    if continue == "y" then moveTo(progressX, progressZ, (progressY + 1)) look(directions[progressOrientation])
  151.    mDown()
  152.    else return "true" end
  153. end
  154.    
  155. function cutSquare(size, depth)
  156.    for a = 1, depth do -- Cut the number of squares specified by the depth variable
  157.       for b = 1, size do -- Cut the number of rows specified by the size variable
  158.          for c = 1, size do -- Cut a column of the square
  159.             -- Check if this is the first block of a new square else cut column
  160.             if (b == 1 and c == 1) then tryDigDown() mDown() elseif c > 1 then tryDig() mForward() end
  161.             -- Check if slot #16 has items put into it then dump blocks in chest
  162.             if (c == 5 and turtle.getItemCount(16) > 0) then dumpBlocks("y") end
  163.          end
  164.         if (b < size) then --Position to cut the next column of the square
  165.             if (b % 2 == 1) then --Position to cut the next column of the square
  166.                tRight()
  167.                tryDig()
  168.                mForward()
  169.                tRight()
  170.             else
  171.                tLeft()
  172.                tryDig()
  173.                mForward()
  174.                tLeft()
  175.             end
  176.         end
  177.       end
  178.       yStart = yStart - 1
  179.       moveTo(xStart, zStart, yStart) look(directions[orientationStart])
  180. --    if (a < depth) then
  181. --       tryDigDown()
  182. --       mDown()
  183. --      end
  184.    end
  185. end
  186.  
  187. -- This is the main loop section of the program
  188.  
  189. moveTo(xStart, zStart, yStart) look(directions[orientationStart])
  190. cutSquare(quarryLength, quarryDepth)
  191. dumpBlocks("n")
  192. moveTo(chestX, chestZ, chestY) look(directions[chestOrientation])
  193. yStart = yStart - quarryDepth
  194. print("Fuel level is "..turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment