Guest User

Untitled

a guest
May 28th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.61 KB | None | 0 0
  1. -- 'Hole' program, version 2
  2.  
  3. -- Stores data about current mode
  4. local speedy = false
  5. local noFuel = false
  6. local fuelChest = false
  7.  
  8. -- Stores turtle status data
  9. local isRefuelling = false
  10.  
  11. -- Stores dimensions for the hole
  12. local size, depth = 0,0
  13.  
  14. -- Stores current turtle position, relative to start point
  15. local xPos, yPos, zPos = 0,0,0
  16.  
  17.  
  18. -- Stores current direction, positive x is directly forwards from start point
  19. -- positive z is directly right
  20. local xDir, zDir = 1,0
  21.  
  22. -- Stores last saved position (Save 1)
  23. local xSav1, ySav1, zSav1 = 0,0,0
  24. local xDirSav1, zDirSav1 = 0,0
  25. local save1Mutex = 0
  26.  
  27. -- Save 2
  28. local xSav2, ySav2, zSav2 = 0,0,0
  29. local xDirSav2, zDirSav2 = 0,0
  30. local save2Mutex = 0
  31.  
  32. -- **************************************************
  33. -- Movement / Turning functions
  34.  
  35. local function moveForward()
  36.     -- Account for falling blocks, such as sand and gravel
  37.     if not speedy then
  38.         while turtle.detect() do
  39.             turtle.dig()
  40.             sleep(0.8)
  41.         end
  42.     else
  43.         turtle.dig()
  44.     end
  45.     -- If there is any mob infront of the turtle, or we are out of fuel
  46.     -- DEV NOTE: Will be modified later to refuel, or message over rednet wifi
  47.     while not turtle.forward() do
  48.         turtle.attack()
  49.     end
  50.    
  51.     -- Update position
  52.     xPos = xPos + xDir
  53.     zPos = zPos + zDir
  54. end
  55.  
  56. local function moveUp()
  57.     -- Account for falling blocks
  58.     if not speedy then
  59.         while turtle.detectUp() do
  60.             turtle.digUp()
  61.             sleep(0.8)
  62.         end
  63.     else
  64.         turtle.digUp()
  65.     end
  66.     --Update position
  67.     yPos = yPos + 1
  68.    
  69.     while not turtle.up() do
  70.         turtle.attackUp()
  71.     end
  72. end
  73.  
  74. local function moveDown()
  75.     -- No need to account for falling blocks
  76.     turtle.digDown()
  77.    
  78.     -- Update position
  79.     yPos = yPos - 1
  80.    
  81.     while not turtle.down() do
  82.         turtle.attackDown()
  83.     end
  84. end
  85.  
  86. local function turnLeft()
  87.     -- Update direction vector
  88.     zDir, xDir = -xDir, zDir
  89.    
  90.     turtle.turnLeft()
  91. end
  92.  
  93. local function turnRight()
  94.     -- Update direction vector
  95.     zDir, xDir = xDir, -zDir
  96.    
  97.     turtle.turnRight()
  98. end
  99.  
  100. local function turnAround()
  101.     -- Update direction
  102.     zDir, xDir = -zDir, -xDir
  103.    
  104.     turtle.turnLeft()
  105.     turtle.turnLeft()
  106. end
  107.  
  108. -- Turns the turtle to the prompted direction
  109. local function turnToDir(x, z)
  110.     if x == 1 and xDir == 0 then
  111.         if zDir == 1 then
  112.             turnLeft()
  113.         else
  114.             turnRight()
  115.         end
  116.     elseif x == 1 and xDir == -1 then
  117.         turnAround()
  118.     elseif x == -1 and xDir == 0 then
  119.         if zDir == 1 then
  120.             turnRight()
  121.         else
  122.             turnLeft()
  123.         end
  124.     elseif x == -1 and xDir == 1 then
  125.         turnAround()
  126.     elseif z == 1 and zDir == 0 then
  127.         if xDir == 1 then
  128.             turnRight()
  129.         else
  130.             turnLeft()
  131.         end
  132.     elseif z == 1 and zDir == -1 then
  133.         turnAround()
  134.     elseif z == -1 and zDir == 0 then
  135.         if xDir == 1 then
  136.             turnLeft()
  137.         else
  138.             turnRight()
  139.         end
  140.     elseif z == -1 and zDir == 1 then
  141.         turnAround()
  142.     else
  143.         return false
  144.     end
  145.     return true
  146. end
  147.  
  148. -- Takes in a coordinate and moves the turtle there
  149. -- IMPLEMENTATION NOTE: Turtle will be facing in whatever direction it finishes in.
  150. --                      Keep this in mind, as it might affect what happens next.
  151. local function moveToPos(x, y, z)
  152.  
  153.     -- Move the turtle to the required X value
  154.         if x > xPos then
  155.             turnToDir(1, 0)
  156.             while x > xPos do
  157.                 moveForward()
  158.             end
  159.         elseif x < xPos then
  160.             turnToDir(-1, 0)
  161.             while x < xPos do
  162.                 moveForward()
  163.             end
  164.         end
  165.    
  166.     -- Move the turtle to the required Z value
  167.         if z > zPos then
  168.             turnToDir(0, 1)
  169.             while z > zPos do
  170.                 moveForward()
  171.             end
  172.         elseif z < zPos then
  173.             turnToDir(0, -1)
  174.             while z < zPos do
  175.                 moveForward()
  176.             end
  177.         end
  178.        
  179.     -- Move the turtle to the required Y value
  180.         if y > yPos then
  181.             while y > yPos do
  182.                 moveUp()
  183.             end
  184.         else
  185.             while y < yPos do
  186.                 moveDown()
  187.             end
  188.         end
  189. end
  190.  
  191. -- End of movement functions
  192. -- **************************************************
  193.    
  194. -- ++++++++++++++++++++++++++++++++++++++++++++++++++
  195. -- Logic functions
  196.  
  197. -- Simple check to see if we can make it home to refuel
  198. local function outOfFuel()
  199.     local retVal = false
  200.     -- 64 is a buffer, seeing as I cannot check if I'm about to run out of fuel on every
  201.     -- move as that would have me paradoxically order my functions, so I have to allow
  202.     -- for moving up or down also...
  203.     local distToHome = xPos + yPos + zPos + 64
  204.     if turtle.getFuelLevel() <= distToHome then
  205.         retVal = true
  206.     end
  207.     return retVal
  208. end
  209.  
  210. -- Saves the current dig position and direction
  211. local function saveDig()
  212.     local retVal = 0
  213.     if save1Mutex == 0 then
  214.         xSav1 = xPos
  215.         ySav1 = yPos
  216.         zSav1 = zPos
  217.         xDirSav1 = xDir
  218.         zDirSav1 = zDir
  219.         save1Mutex = 1
  220.         retVal = 1
  221.     elseif save2Mutex == 0 then
  222.         xSav2 = xPos
  223.         ySav2 = yPos
  224.         zSav2 = zPos
  225.         xDirSav2 = xDir
  226.         zDirSav2 = zDir
  227.         save2Mutex = 1
  228.         retVal = 2
  229.     end
  230.     return retVal
  231. end
  232.  
  233. -- Dumps the load into a chest or thin air
  234. local function dumpLoad()
  235.     for n = 1, 16 do
  236.         turtle.select(n)
  237.         turtle.drop()
  238.     end
  239. end
  240.  
  241.  
  242. -- Moves the turtle back to the last saved position and direction
  243. local function resumeDig(save)
  244.     if save == 1 then
  245.         moveToPos(xSav1, ySav1, zSav1)
  246.         turnToDir(xDirSav1, zDirSav1)
  247.         save1Mutex = 0
  248.     elseif save == 2 then
  249.         moveToPos(xSav2, ySav2, zSav2)
  250.         turnToDir(xDirSav2, xDirSav2)
  251.         save2Mutex = 0
  252.     end
  253. end
  254.  
  255. -- Move the turtle to the home and fuel chests, dump load and refuel, then
  256. -- resume digging
  257. local function refuel()
  258.     local save
  259.    
  260.     isRefuelling = 1
  261.    
  262.     save = saveDig()
  263.     moveToPos(0, 0, 0)
  264.     turnToDir(-1, 0)
  265.     dumpLoad()
  266.     turnToDir(0, -1)
  267.     turtle.select(1)
  268.     turtle.suck()
  269.     turtle.refuel()
  270.     resumeDig(save)
  271.    
  272.     isRefuelling = 0
  273. end
  274.  
  275.  
  276. -- Simply checks if the turtle is full
  277. -- DEV NOTE: this is kind of brute-forcish, I wonder if there's a better way?
  278. local function isFull()
  279.     local retVal = true
  280.     for n = 1, 16 do
  281.         if turtle.getItemCount(n) == 0 then
  282.             retVal = false
  283.         end
  284.     end
  285.     return retVal
  286. end
  287.  
  288. -- Digs the current strip in the current layer
  289. local function digStrip()
  290.     for i = 2, size do
  291.         moveForward()
  292.         if isFull() then
  293.             local save
  294.             save = saveDig()
  295.             moveToPos(0,0,0)
  296.             turnToDir(-1, 0)
  297.             dumpLoad()
  298.             resumeDig(save)
  299.         end
  300.         -- Are we able to make it home?
  301.         if not noFuel and not isRefuelling and outOfFuel() then
  302.             refuel()
  303.         end
  304.     end
  305. end
  306.  
  307. -- Turns the turtle in the correct direction at the end of digging a strip
  308. local function stripTurn(strips)
  309.     if strips ~= size - 1 and size % 2 == 1 then
  310.         if strips % 2 == 1 then
  311.             turnLeft()
  312.             moveForward()
  313.             turnLeft()
  314.         else
  315.             turnRight()
  316.             moveForward()
  317.             turnRight()
  318.         end
  319.     elseif strips ~= size -1 and size % 2 == 0 then
  320.         if yPos % 2 == 0 and strips % 2 == 0 then
  321.             turnRight()
  322.             moveForward()
  323.             turnRight()
  324.         elseif yPos % 2 == 1 and strips % 2 == 1 then
  325.             turnRight()
  326.             moveForward()
  327.             turnRight()
  328.         else
  329.             turnLeft()
  330.             moveForward()
  331.             turnLeft()
  332.         end
  333.     end
  334. end
  335.  
  336. -- Digs the current layer
  337. local function digLayer()
  338.     local strips = 0
  339.     for i = 1, size do
  340.         digStrip()
  341.         stripTurn(strips)
  342.         strips = strips + 1
  343.     end
  344. end
  345.  
  346.  
  347. -- This function controls the digging of the hole
  348. local function digOut()
  349. -- This for loop controls how many layers we are going to dig
  350.     for n = 1, depth do
  351.         digLayer()
  352.         -- If this was no the last layer, then start digging the next one, otherwise,
  353.         -- head home and empty contents into the chest
  354.         if n ~= depth then
  355.             turnAround()
  356.             moveDown()
  357.         elseif n == depth then
  358.             moveToPos(0, 0, 0)
  359.             turnToDir(-1, 0)
  360.             dumpLoad()
  361.             turnToDir(1, 0)
  362.         end
  363.     end
  364. end
  365.        
  366.  
  367. -- End of logic functions
  368. -- ++++++++++++++++++++++++++++++++++++++++++++++++++
  369.  
  370. -- Main function controls flow of program
  371. local function main(argc, argv)
  372.     if argc < 2 or argc > 4 then
  373.         print("Usage: hole <diameter> <depth> -s -fc")
  374.         print("")
  375.         print("-s : speed mode, does not check for falling blocks")
  376.         print("")
  377.         print("-fc : fuel chest present, should be filled with fuel and placed to the left of the turtle before starting")
  378.         return false
  379.     end
  380.    
  381.     -- Test if the 'speedy' or 'fuel chest' flags were set
  382.     if argv[3] == "-s" then
  383.         speedy = true
  384.         print("Speed mode")
  385.     elseif argv[3] == "-fc" then
  386.         fuelChest = true
  387.         print("Fuel chest mode")
  388.     else
  389.     -- If we're not in fuel chest mode, then ignore fuel checking
  390.         noFuel = true
  391.     end
  392.    
  393.     -- Since users may want fuel chest mode and speedy mode
  394.     if argv[4] == "-fc" then
  395.         fuelChest = true
  396.         print("Fuel chest mode")
  397.     else
  398.     -- If we're not in fuel chest mode, then ignore fuel checking
  399.         noFuel = true
  400.     end
  401.    
  402.     -- Will skip fuel checking
  403.     if turtle.getFuelLevel() == "unlimited" then
  404.         noFuel = true
  405.     end
  406.    
  407.     size = tonumber(argv[1])
  408.     depth = tonumber(argv[2])
  409.    
  410.     print("Beginning dig...")
  411.     digOut()
  412.     print("Finishing dig...")
  413.    
  414. end
  415.  
  416. main(#{...}, {...})
Advertisement
Add Comment
Please, Sign In to add comment