Guest User

Untitled

a guest
May 25th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Will mine a hole in the ground
  2.  
  3. local tArgs = {...}
  4. -- If incorrect no of args is given
  5. if #tArgs ~= 2 then
  6.     print("Usage: hole <side> <depth>")
  7.     return
  8. end
  9.  
  10. -- Size will be the length of the sides of our square hole
  11. local size = tonumber(tArgs[1])
  12. -- StopDepth is the number of levels we are mining in the downwards direction
  13. local stopDepth = tonumber(tArgs[2])
  14.  
  15. -- Info about turtle's current position and direction. Note that when the turtle is
  16. -- placed, positive x is directly forwards, positive z is to the right
  17. -- Also note, direction vector components should be mutually exclusive, ie if one
  18. -- holds a value, the other *MUST* be zero
  19. local depth = 0
  20. local xPos, zPos = 0,0
  21. local xDir, zDir = 1,0
  22.  
  23. -- Will be used to save the position and direction of the turtle when we need
  24. -- to refuel/empty the inventory
  25. local xSav, ySav, zSav = 0,0,0
  26. local xDirSav, zDirSav = 0,0
  27.  
  28. -- Check if there are any free slots in the inventory space
  29. local function isFull()
  30.     for n = 1,9 do
  31.         if turtle.getItemCount(n) == 0 then
  32.             return false
  33.         end
  34.     end
  35.     return true
  36. end
  37.  
  38. -- Will dig forwards (if required) and move forwards, will also update position
  39. local function moveForward()
  40.     while turtle.detect() do
  41.         turtle.dig()
  42.         sleep(0.8) -- Gravity blocks
  43.     end
  44.     -- Update position
  45.     xPos = xPos + xDir
  46.     zPos = zPos + zDir
  47.     turtle.forward()
  48. end
  49.  
  50. -- Simply moves the turtle down, digging if necessary
  51. local function moveDown()
  52.     if turtle.detectDown() then
  53.         turtle.digDown()
  54.     end
  55.     -- Update position
  56.     depth = depth + 1
  57.     turtle.down()
  58. end
  59.  
  60. -- Similar to moveDown()
  61. local function moveUp()
  62.     if turtle.detectUp() then
  63.         turtle.digUp()
  64.     end
  65.     depth = depth - 1
  66.     turtle.up()
  67. end
  68.  
  69. -- Turn right, update direction
  70. local function turnRight()
  71.     zDir, xDir = xDir, -zDir
  72.     turtle.turnRight()
  73. end
  74.  
  75. -- Turn left, update direction
  76. local function turnLeft()
  77.     zDir, xDir = -xDir, zDir
  78.     turtle.turnLeft()
  79. end
  80.  
  81. -- This will just dump everything in it's inventory directly infront of it.
  82. local function dumpLoad()
  83.     local stackCount = 0
  84.     for n = 1,9 do
  85.         stackCount = turtle.getItemCount(n)
  86.         turtle.select(n)
  87.         for i = 0, stackCount do
  88.             turtle.drop()
  89.         end
  90.     end
  91. end
  92.  
  93. -- This is where the fun begins, this function (should) make the turtle return to the 'Home' corner
  94. -- ie this is the corner that it started in (but it won't take it to the starting depth)
  95. -- This will be used to reset the turtle in order to dig down another level, or before it returns
  96. -- to it's starting point (and depth)
  97. local function returnCorner()
  98.     if xPos > 0 then
  99.     -- The goal here is to negate the x displacement, the first step is to point it in the negative x
  100.     -- direction
  101.         if xDir > 0 then
  102.             turnRight()
  103.             turnRight()
  104.         elseif zPos ~= 0 then
  105.             if zPos > 0 then
  106.                 turnRight()
  107.             elseif zPos < 0 then
  108.                 turnLeft()
  109.             end
  110.         end
  111.         -- Then we just move it in the negative x direction until it hits 0 displacement
  112.         while xPos > 0 do
  113.             moveForward()
  114.         end
  115.     end
  116.     -- Now we repeat the process for the z displacement, first, orient the turtle in the correct
  117.     -- direction (negative z)
  118.     if zPos > 0 then
  119.         if zDir > 0 then
  120.             turnLeft()
  121.             turnLeft()
  122.         elseif xPos ~= 0 then
  123.             if xPos > 0 then
  124.                 turnLeft()
  125.             elseif xPos < 0 then
  126.                 turnRight()
  127.             end
  128.         end
  129.         -- Then move the turtle forward until it hits 0 displacement
  130.         while zPos > 0 do
  131.             moveForward()
  132.         end
  133.         -- Rotate the turtle so that it faces the direction it started in (towards the inside of the hole,
  134.         -- in the positive x direction)
  135.         turnRight()
  136.     end
  137. end
  138.  
  139. -- This is similar to returnCorener, except it will take the turtle back to the starting height also,
  140. -- where we will generally have a chest for it to spew its contents into
  141. local function returnHome()
  142.     returnCorner()
  143.     while depth > 0 do
  144.         moveUp()
  145.     end
  146.     -- Turn around and empty inv into chest, then turn around again (positive x)
  147.     turnRight()
  148.     turnRight()
  149.     dumpLoad()
  150.     turnRight()
  151.     turnRight()
  152. end
  153.  
  154. -- This should save the turtles current position and direction, so that we can
  155. -- move around (eg empty inventory) and come back to where we left off
  156. local function saveDig()
  157.     xSav = xPos
  158.     zSav = zPos
  159.     ySav = depth
  160.     xDirSav = xDir
  161.     zDirSav = zDir
  162. end
  163.  
  164. -- This function should return the turtle to the saved coordinates, so that we
  165. -- can continue to dig our hole... But it's broken, I think
  166. local function resumeDig()
  167. -- First, orient the turtle in the positive x direction, so we can adjust for our x displacement
  168.     if xDir < 0 then
  169.         turnRight()
  170.         turnRight()
  171.     elseif zDir > 0 then
  172.         turnLeft()
  173.     elseif zDir < 0 then
  174.         turnRight()
  175.     end
  176.     -- If we started from the home corner, then our displacement will be zero, so our move
  177.     -- command becomes a tad more simple, however, if we are inside the hole somewhere for
  178.     -- any reason, then we will need to adjust for our current position
  179.     if xPos == 0 then
  180.         while xPos ~= xSav do
  181.             moveForward()
  182.         end
  183.     elseif xPos > 0 then
  184.         local deltaX = xSav - xPos
  185.         for n = 1,deltaX do
  186.             moveForward()
  187.         end
  188.     end
  189.     -- Orient the turtle in the positive z direction
  190.     turnRight()
  191.     -- Follow the same logic as we did in x
  192.     if zPos == 0 then
  193.         while zPos ~= zSav do
  194.             moveForward()
  195.         end
  196.     elseif zPos > 0 then
  197.         local deltaZ = zSav - zPos
  198.         for n = 1,deltaZ do
  199.             moveForward()
  200.         end
  201.     end
  202.     -- We must also account for the depth that we were digging at we, again, use the same logic
  203.     -- as x and z, just in the y direction
  204.     if depth == 0 then
  205.         while depth ~= ySav do
  206.             moveDown()
  207.         end
  208.     elseif depth > 0 then
  209.         local deltaY = ySav - depth
  210.         for n = 1,deltaY do
  211.             moveDown()
  212.         end
  213.     end
  214.     -- Finally, account for whatever direction we were facing when we left off
  215.     while xDir ~= xDirSav do
  216.         turnRight()
  217.     end
  218.     while zDir ~= zDirSav do
  219.         turnRight()
  220.     end
  221. end
  222.        
  223. -- This finction should dig one complete hole
  224. local function digOut()
  225. -- For every layer we were told to dig, we just need to repeat the same operation,
  226. -- of digging out that layer
  227.     for m = 1,stopDepth do
  228.     -- This is where we dig out the individual layers, this first nested for loop
  229.     -- controls how many colums the turtle will dig (in this case, because we are
  230.     -- digging a square hole, it is the same as the number of rows we are digging)
  231.         for n = 1, size do
  232.         -- This second nested for loop control our digging of one column (along the x axis)
  233.             for nn = 2, size do
  234.                 moveForward()
  235.                 -- If the turtle is full, we tell it to move to the home position (where it
  236.                 -- started) which is where it will spew up on the ground, or into a chest if we
  237.                 -- put one there
  238.                 if isFull() then
  239.                     saveDig()
  240.                     returnHome()
  241.                     resumeDig()
  242.                 end
  243.             end
  244.             -- This controls the turn into the next column at the end of digging the last column,
  245.             -- if the column we just dug was the last one, we want to skip this step.
  246.             if n ~= size then
  247.                 if n % 2 > 0 then
  248.                 -- For odd rows we want to turn right (seeing as we start from row 1 and not row
  249.                 -- 0)
  250.                     turnRight()
  251.                     moveForward()
  252.                     turnRight()
  253.                 else
  254.                 -- And even rows we turn left
  255.                     turnLeft()
  256.                     moveForward()
  257.                     turnLeft()
  258.                 end
  259.             end
  260.         end
  261.         -- Move the turtle to the home corner, ready for either the next layer, or to go home
  262.         returnCorner()
  263.         if m ~= stopDepth then
  264.             moveDown()
  265.         end
  266.     end
  267.     -- Return the turtle to it's starting point when we are done
  268.     returnHome()
  269. end
  270.  -- LUA is a weird language, but this is the equivalent of the main() method
  271. print("Beginning...")
  272. digOut()
  273. print("Finished...")
Advertisement
Add Comment
Please, Sign In to add comment