Guest User

Untitled

a guest
May 27th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.69 KB | None | 0 0
  1. -- 'Hole' program, version 2
  2.  
  3. -- Stores dimensions for the hole
  4. local size, depth = 0,0
  5.  
  6. -- Stores current turtle position, relative to start point
  7. local xPos, yPos, zPos = 0,0,0
  8. local xDirSav, zDirSav = 0,0
  9.  
  10. -- Stores current direction, positive x is directly forwards from start point
  11. -- positive z is directly right
  12. local xDir, zDir = 1,0
  13.  
  14. -- Stores last saved position
  15. local xSay, ySav, zSav = 0,0,0
  16.  
  17. -- **************************************************
  18. -- Movement / Turning functions
  19.  
  20. local function moveForward()
  21.     -- Account for falling blocks, such as sand and gravel
  22.     while turtle.detect() do
  23.         turtle.dig()
  24.         sleep(0.8)
  25.     end
  26.     -- If there is any mob infront of the turtle, or we are out of fuel
  27.     -- DEV NOTE: Will be modified later to refuel, or message over rednet wifi
  28.     while not turtle.forward() do
  29.         sleep(1)
  30.     end
  31.    
  32.     -- Update position
  33.     xPos = xPos + xDir
  34.     zPos = zPos + zDir
  35. end
  36.  
  37. local function moveUp()
  38.     -- Account for falling blocks
  39.     while turtle.detectUp() do
  40.         turtle.digUp()
  41.         sleep(0.8)
  42.     end
  43.     --Update position
  44.     yPos = yPos + 1
  45.    
  46.     turtle.up()
  47. end
  48.  
  49. local function moveDown()
  50.     -- No need to account for falling blocks
  51.     turtle.digDown()
  52.    
  53.     -- Update position
  54.     yPos = yPos - 1
  55.    
  56.     turtle.down()
  57. end
  58.  
  59. local function turnLeft()
  60.     -- Update direction vector
  61.     zDir, xDir = -xDir, zDir
  62.    
  63.     turtle.turnLeft()
  64. end
  65.  
  66. local function turnRight()
  67.     -- Update direction vector
  68.     zDir, xDir = xDir, -zDir
  69.    
  70.     turtle.turnRight()
  71. end
  72.  
  73. local function turnAround()
  74.     -- Update direction
  75.     zDir, xDir = -zDir, -xDir
  76.    
  77.     turtle.turnLeft()
  78.     turtle.turnLeft()
  79. end
  80.  
  81. -- Turns the turtle to the prompted direction
  82. local function turnToDir(x, z)
  83.     if x == 1 and xDir == 0 then
  84.         if zDir == 1 then
  85.             turnLeft()
  86.         else
  87.             turnRight()
  88.         end
  89.     elseif x == 1 and xDir == -1 then
  90.         turnAround()
  91.     elseif x == -1 and xDir == 0 then
  92.         if zDir == 1 then
  93.             turnRight()
  94.         else
  95.             turnLeft()
  96.         end
  97.     elseif x == -1 and xDir == 1 then
  98.         turnAround()
  99.     elseif z == 1 and zDir == 0 then
  100.         if xDir == 1 then
  101.             turnRight()
  102.         else
  103.             turnLeft()
  104.         end
  105.     elseif z == 1 and zDir == -1 then
  106.         turnAround()
  107.     elseif z == -1 and zDir == 0 then
  108.         if xDir == 1 then
  109.             turnLeft()
  110.         else
  111.             turnRight()
  112.         end
  113.     elseif z == -1 and zDir == 1 then
  114.         turnAround()
  115.     else
  116.         return false
  117.     end
  118.     return true
  119. end
  120.  
  121. -- Takes in a coordinate and moves the turtle there
  122. -- USE NOTE: Turtle will be facing in negative x direction upon completion.
  123. -- This is to do with returning home, so it's facing the chest
  124. local function moveToPos(x, y, z)
  125.  
  126.     -- Move the turtle to the required X value
  127.         if x > xPos then
  128.             turnToDir(1, 0)
  129.             while x > xPos do
  130.                 moveForward()
  131.             end
  132.         elseif x < xPos then
  133.             turnToDir(-1, 0)
  134.             while x < xPos do
  135.                 moveForward()
  136.             end
  137.         end
  138.    
  139.     -- Move the turtle to the required Z value
  140.         if z > zPos then
  141.             turnToDir(0, 1)
  142.             while z > zPos do
  143.                 moveForward()
  144.             end
  145.         elseif z < zPos then
  146.             turnToDir(0, -1)
  147.             while z < zPos do
  148.                 moveForward()
  149.             end
  150.         end
  151.        
  152.     -- Move the turtle to the required Y value
  153.         if y > yPos then
  154.             while y > yPos do
  155.                 moveUp()
  156.             end
  157.         else
  158.             while y < yPos do
  159.                 moveDown()
  160.             end
  161.         end
  162.        
  163.     turnToDir(-1, 0)
  164. end
  165.  
  166. -- End of movement functions
  167. -- **************************************************
  168.    
  169. -- ++++++++++++++++++++++++++++++++++++++++++++++++++
  170. -- Logic functions
  171.  
  172. -- Saves the current dig position and direction
  173. local function saveDig()
  174.     xSav = xPos
  175.     ySav = yPos
  176.     zSav = zPos
  177.     xDirSav = xDir
  178.     zDirSav = zDir
  179. end
  180.  
  181. -- Moves the turlte back to the last saved position and direction
  182. local function resumeDig()
  183.     moveToPos(xSav, ySav, zSav)
  184.     turnToDir(xDirSav, zDirSav)
  185. end
  186.  
  187. -- Dumps the load into a chest or thin air
  188. local function dumpLoad()
  189.     for n = 1, 9 do
  190.         turtle.select(n)
  191.         turtle.drop()
  192.     end
  193. end
  194.  
  195. -- Simply checks if the turtle is full
  196. -- DEV NOTE: this is kind of brute-forcish, I wonder if there's a better way?
  197. local function isFull()
  198.     local retVal = true
  199.     for n = 1, 9 do
  200.         if turtle.getItemCount(n) == 0 then
  201.             retVal = false
  202.         end
  203.     end
  204.     return retVal
  205. end
  206.  
  207. -- This function controls the digging of the hole
  208. local function digOut()
  209. -- This for loop controls how many layers we are going to dig
  210.     for n = 1, depth do
  211.     -- Similarly, this one control how many 'strips' we are going to mine on this level
  212.         for nn = 1, size do
  213.         -- And this one controls how long each of those 'strips' is, and also if the
  214.         -- turtle is full, it controls dumping the contents in the home chest
  215.             for nnn = 2, size  do
  216.                 moveForward()
  217.                 if isFull() then
  218.                     saveDig()
  219.                     moveToPos(0, 0, 0)
  220.                     dumpLoad()
  221.                     resumeDig()
  222.                 end
  223.             end
  224.             -- This determines the direction the turtle will turn at the end of the
  225.             -- strip, since it alternates every other strip
  226.             if nn ~= size then
  227.                 if nn % 2 == 0 then
  228.                     turnLeft()
  229.                     moveForward()
  230.                     turnLeft()
  231.                 else
  232.                     turnRight()
  233.                     moveForward()
  234.                     turnRight()
  235.                 end
  236.             end
  237.         end
  238.         -- Move to the home corner when the current layer is finished
  239.         moveToPos(0, yPos, 0)
  240.         -- If this was no the last layer, then start digging the next one, otherwise,
  241.         -- head home and empty contents into the chest
  242.         if m ~= depth then
  243.             turnAround()
  244.             moveDown()
  245.         elseif m == depth then
  246.             moveToPos(0, 0, 0)
  247.             dumpLoad()
  248.         end
  249.     end
  250. end
  251.  
  252. -- End of logic functions
  253. -- ++++++++++++++++++++++++++++++++++++++++++++++++++
  254.  
  255. -- Main function controls flow of program
  256. local function main(argc, argv)
  257.     if argc ~= 2 then
  258.         print("Usage: hole <diameter> <depth>")
  259.         return false
  260.     end
  261.    
  262.     size = argv[1]
  263.     depth = argv[2]
  264.    
  265.     print("Beginning dig...")
  266.     digOut()
  267.     print("Finishing dig...")
  268.    
  269. end
  270.  
  271. main(#{...}, {...})
Advertisement
Add Comment
Please, Sign In to add comment