Advertisement
Godleydemon

Mining Program

Jun 16th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.38 KB | None | 0 0
  1. function updateResumingFlag()
  2.  
  3.   if (resuming == true) then
  4.     if ((resumeMiningState == currMiningState) and (resumeX == currX) and (resumeY == currY) and (resumeZ == currZ) and (resumeOrient == currOrient)) then
  5.       resuming = false
  6.     end
  7.   end
  8.  
  9. end
  10.  
  11.  
  12. function saveLocation()
  13.  
  14.   -- Write the x, y, z and orientation to the file
  15.   if ((supportResume == true) and (resuming == false)) then
  16.     local outputFile = io.open(oreQuarryLocation, "w")
  17.     outputFile:write(currMiningState)
  18.     outputFile:write("\n")
  19.     outputFile:write(currX)
  20.     outputFile:write("\n")
  21.     outputFile:write(currY)
  22.     outputFile:write("\n")
  23.     outputFile:write(currZ)
  24.     outputFile:write("\n")
  25.     outputFile:write(currOrient)
  26.     outputFile:write("\n")
  27.     outputFile:close()
  28.   end
  29.  
  30. end
  31.  
  32. -- Variables used to support a resume
  33. local startupParamsFile = "OreQuarryParams.txt"
  34. local oreQuarryLocation = "OreQuarryLocation.txt"
  35. local returnToStartFile = "OreQuarryReturn.txt"
  36. local startupBackup = "startup_bak"
  37. local supportResume = true -- Determines whether the turtle is being run in the mode that supports resume
  38. local resuming = false -- Determines whether the turtle is currently in the process of resuming
  39. local resumeX
  40. local resumeY
  41. local resumeZ
  42. local resumeOrient
  43. local resumeMiningState
  44.  
  45. -- Variables to store the current location and orientation of the turtle. x is right, left, y is up, down and
  46. -- z is forward, back with relation to the starting orientation. Y is the actual turtle level, x and z are
  47. -- in relation to the starting point (i.e. the starting point is (0, 0))
  48. local currX
  49. local currY
  50. local currZ
  51. local currOrient
  52. local currMiningState = miningState.START
  53.  
  54. -- Command line parameters
  55. local startHeight -- Represents the height (y co-ord) that the turtle started at
  56. local quarryWidth -- Represents the length of the mines that the turtle will dig
  57.  
  58.  
  59.  
  60.   if (resuming == true) then
  61.     -- Get the stored parameters from the necessary file
  62.     local resumeFile = fs.open(returnToStartFile, "r")
  63.     if (resumeFile ~= nil) then
  64.       -- Restore the parameters from the file
  65.       local beenAtZero = resumeFile.readLine()
  66.       if (beenAtZero == "y") then
  67.         haveBeenAtZeroZeroOnLayer = true
  68.       else
  69.         haveBeenAtZeroZeroOnLayer = false
  70.       end
  71.  
  72.       local miningPointFlag = resumeFile.readLine()
  73.       if (miningPointFlag == "y") then
  74.         returnBackToMiningPoint = true
  75.       else
  76.         returnBackToMiningPoint = false
  77.       end
  78.  
  79.       currX = readNumber(resumeFile)
  80.       currY = readNumber(resumeFile)
  81.       currZ = readNumber(resumeFile)
  82.       currOrient = readNumber(resumeFile)
  83.       levelToReturnTo = readNumber(resumeFile)
  84.       prevMiningState = readNumber(resumeFile)
  85.       orientationAtZeroZero = readNumber(resumeFile)
  86.       resumeFile.close()
  87.  
  88.     else
  89.       writeMessage("Failed to read return to start file", messageLevel.ERROR)
  90.     end
  91.   elseif (supportResume == true) then
  92.  
  93.     local outputFile = io.open(returnToStartFile, "w")
  94.  
  95.     if (haveBeenAtZeroZeroOnLayer == true) then
  96.       outputFile:write("y\n")
  97.     else
  98.       outputFile:write("n\n")
  99.     end
  100.     if (returnBackToMiningPoint == true) then
  101.       outputFile:write("y\n")
  102.     else
  103.       outputFile:write("n\n")
  104.     end
  105.  
  106.     outputFile:write(currX)
  107.     outputFile:write("\n")
  108.     outputFile:write(currY)
  109.     outputFile:write("\n")
  110.     outputFile:write(currZ)
  111.     outputFile:write("\n")
  112.     outputFile:write(currOrient)
  113.     outputFile:write("\n")
  114.     outputFile:write(levelToReturnTo)
  115.     outputFile:write("\n")
  116.     outputFile:write(prevMiningState)
  117.     outputFile:write("\n")
  118.     outputFile:write(orientationAtZeroZero)
  119.     outputFile:write("\n")
  120.  
  121.     outputFile:close()
  122.   end
  123.    
  124.   storedX = currX
  125.   storedY = currY
  126.   storedZ = currZ
  127.   storedOrient = currOrient
  128.  
  129.   -- Store the current location and orientation so that it can be returned to
  130.   currMiningState = miningState.EMPTYINVENTORY
  131.   writeMessage("last item count = "..turtle.getItemCount(lastEmptySlot), messageLevel.DEBUG)
  132.  
  133.   if ((turtle.getItemCount(lastEmptySlot) > 0) or (returnBackToMiningPoint == false)) then
  134.  
  135.     writeMessage("Heading back to surface", messageLevel.INFO)
  136.  
  137.     -- Move down to the correct layer to return via
  138.     if (currY > levelToReturnTo) then
  139.       while (currY > levelToReturnTo) do
  140.         turtleDown()
  141.       end
  142.     elseif (currY < levelToReturnTo) then
  143.       while (currY < levelToReturnTo) do
  144.         turtleUp()
  145.       end
  146.     end
  147.  
  148.     if ((haveBeenAtZeroZeroOnLayer == false) or (orientationAtZeroZero == direction.FORWARD)) then
  149.       -- Move back to the correct X position first
  150.       if (currX > 0) then
  151.         turtleSetOrientation(direction.LEFT)
  152.         while (currX > 0) do
  153.           turtleForward()
  154.         end
  155.       elseif (currX < 0) then
  156.         -- This should never happen
  157.         writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  158.       end
  159.  
  160.       -- Then move back to the correct Z position
  161.       if (currZ > 0) then
  162.         turtleSetOrientation(direction.BACK)
  163.         while (currZ > 0) do
  164.           turtleForward()
  165.         end
  166.       elseif (currZ < 0) then
  167.         -- This should never happen
  168.         writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  169.       end
  170.     else
  171.       -- Move back to the correct Z position first
  172.       if (currZ > 0) then
  173.         turtleSetOrientation(direction.BACK)
  174.         while (currZ > 0) do
  175.           turtleForward()
  176.         end
  177.       elseif (currZ < 0) then
  178.         -- This should never happen
  179.         writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  180.       end
  181.  
  182.       -- Then move back to the correct X position
  183.       if (currX > 0) then
  184.         turtleSetOrientation(direction.LEFT)
  185.         while (currX > 0) do
  186.           turtleForward()
  187.         end
  188.       elseif (currX < 0) then
  189.         -- This should never happen
  190.         writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  191.       end
  192.     end
  193.  
  194.     -- Return to the starting layer
  195.     if (currY < startHeight) then
  196.       while (currY < startHeight) do
  197.         turtleUp()
  198.       end
  199.     elseif (currY > startHeight) then
  200.       -- This should never happen
  201.       writeMessage("Current height is greater than start height in returnToStartAndUnload", messageLevel.ERROR)
  202.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement