Advertisement
civilwargeeky

Quarry 3.5.0

May 31st, 2014
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 65.26 KB | None | 0 0
  1. --[[
  2. Version 3.5.0
  3. Recent Changes:
  4.   Ore Quarry!!!
  5.   Completely redid the system of dropping items off
  6.   Improved session restoring for those using fuel
  7.   Added New Rednet Support
  8.   New Arguments!
  9.     -oreQuarry [t/f] This will start an oreQuarry :D
  10.     -dumpCompareItems [t/f] If true, the oreQuarry will drop off junk compare items
  11.     -extraDropItems [force] Tells the oreQuarry that you want to have extra drop-off items, like cobblestone
  12.     -atChest [force] Using this with -resume will tell the turtle that it is at its chest, and needs to go back to where it was
  13. ]]
  14. --Defining things
  15. civilTable = nil; _G.civilTable = {}; setmetatable(civilTable, {__index = _G}); setfenv(1,civilTable)
  16. VERSION = "3.5.0" --Adding this so I can differentiate versions on error messages
  17. originalDay = os.day() --Used in logging
  18. numResumed = 0 --Number of times turtle has been resumed
  19. -------Defaults for Arguments----------
  20. --Arguments assignable by text
  21. x,y,z = 3,3,3 --These are just in case tonumber fails
  22. inverted = false --False goes from top down, true goes from bottom up [Default false]
  23. rednetEnabled = false --Default rednet on or off  [Default false]
  24. --Arguments assignable by tArgs
  25. dropSide = "front" --Side it will eject to when full or done [Default "front"]
  26. careAboutResources = true --Will not stop mining once inventory full if false [Default true]
  27. doCheckFuel = true --Perform fuel check [Default true]
  28. doRefuel = false --Whenever it comes to start location will attempt to refuel from inventory [Default false]
  29. invCheckFreq = 5 --Will check for inventory full every <-- moved spaces [Default 10]
  30. keepOpen = 1 --How many inventory slots it will attempt to keep open at all times [Default 1]
  31. fuelSafety = "moderate" --How much fuel it will ask for: safe, moderate, and loose [Default moderate]
  32. saveFile = "Civil_Quarry_Restore" --Where it saves restore data [Default "Civil_Quarry_Restore"]
  33. doBackup = true --If it will keep backups for session persistence [Default true]
  34. uniqueExtras = 8 --How many different items (besides cobble) the turtle expects. [Default 8]
  35. maxTries = 50 --How many times turtle will try to dig a block before it "counts" bedrock [Default 50]
  36. gpsEnabled = false -- If option is enabled, will attempt to find position via GPS api [Default false]
  37. gpsTimeout = 3 --The number of seconds the program will wait to get GPS coords. Not in arguments [Default 3]
  38. logging = true --Whether or not the turtle will log mining runs. [Default ...still deciding]
  39. logFolder = "Quarry_Logs" --What folder the turtle will store logs in [Default "Quarry_Logs"]
  40. logExtension = "" --The extension of the file (e.g. ".txt") [Default ""]
  41. startDown = 0 --How many blocks to start down from the top of the mine [Default 0]
  42. enderChestEnabled = false --Whether or not to use an ender chest [Default false]
  43. enderChestSlot = 16 --What slot to put the ender chest in [Default 16]
  44. oreQuarry = false --Enables ore quarry functionailty [Default false]
  45. dumpCompareItems = true --If ore quarry, the turtle will dump items compared to (like cobblestone) [Default true]
  46. --Standard number slots for fuel (you shouldn't care)
  47. fuelTable = { --Will add in this amount of fuel to requirement.
  48. safe = 1000,
  49. moderate = 200,
  50. loose = 0 } --Default 1000, 200, 0
  51. --Standard rednet channels
  52. channels = {
  53. send = os.getComputerID() + 1  ,
  54. receive = os.getComputerID() + 101 ,
  55. confirm = "Turtle Quarry Receiver",
  56. message = "Civil's Quarry",
  57. }
  58.  
  59. --AVERAGE USER: YOU DON'T CARE BELOW THIS POINT
  60.  
  61. local help_paragraph = [[
  62. Welcome!: Welcome to quarry help. Below are help entries for all parameters. Examples and tips are at the bottom.
  63. -Default: This will force no prompts. If you use this and nothing else, only defaults will be used.
  64. -dim: [length] [width] [height] This sets the dimensions for the quarry
  65. -invert: [t/f] If true, quarry will be inverted (go up instead of down)
  66. -rednet: [t/f] If true and you have a wireless modem on the turtle, will attempt to make a rednet connection for sending important information to a screen
  67. -restore / -resume: If your quarry stopped in the middle of its run, use this to resume at the point where the turtle was. Not guarenteed to work properly. For more accurate location finding, check out the -GPS parameter
  68. -oreQuarry: [t/f] If true, the turtle will use ore quarry mode. It will not mine the blocks that are placed in the turtle initially. So if you put in stone, it will ignore stone blocks and only mine ores.
  69. -atChest: [force] This is for use with "-restore," this will tell the restarting turtle that it is at its home chest, so that if it had gotten lost, it now knows where it is.
  70. -doRefuel: [t/f] If true, the turtle will refuel itself with coal and planks it finds on its mining run
  71. -doCheckFuel: [t/f] If you for some reason don't want the program to check fuel usage, set to false. This is honestly a hold-over from when the refueling algorithm was awful...
  72. -uniqueExtras: [number] The expected number of slots filled with low-stacking items like ore. Higher numbers request more fuel.
  73. -chest: [side] This specifies what side the chest at the end will be on. You can say "top", "bottom", "front", "left", or "right"
  74. -enderChest: This one is special. If you use "-enderChest true" then it will use an enderChest in the default slot. However, you can also do "-enderChest [slot]" then it will take the ender chest from whatever slot you tell it to. Like 7... or 14... or whatever.
  75. -GPS: [force] If you use "-GPS" and there is a GPS network, then the turtle will record its first two positions to precisly calculate its position if it has to restart. This will only take two GPS readings
  76. -sendChannel: [number] This is what channel your turtle will send rednet messages on
  77. -receiveChannel: [number] This is what channel your turtle will receive rednet messages on
  78. -startY: [current Y coord] Randomly encountering bedrock? This is the parameter for you! Just give it what y coordinate you are at right now. If it is not within bedrock range, it will never say it found bedrock
  79. -extraDropItems: [force] If oreQuarry then this will prompt the user for extra items to drop, but not compare to (like cobblestone)
  80. -dumpCompareItems: [t/f] If oreQuarry and this is true, the turtle will dump off compare blocks instead of storing them in a chest
  81. -maxTries: [number] This is the number of times the turtle will try to dig before deciding its run into bedrock.
  82. -logging: [t/f] If true, will record information about its mining run in a folder at the end of the mining run
  83. -doBackup: [t/f] If false, will not back up important information and cannot restore, but will not make an annoying file (Actually I don't really know why anyone would use this...)
  84. -saveFile: [word] This is what the backup file will be called
  85. -logFolder: [word] The folder that quarry logs will be stored in
  86. -logExtension: [word] The extension given to each quarry log (e.g. ".txt" or ".notepad" or whatever)
  87. -invCheckFreq: [number] This is how often the turtle will check if it has the proper amount of slots open
  88. -keepOpen: [number] This is the number of the slots the turtle will make sure are open. It will check every invCheckFreq blocks
  89. -careAboutResources: [t/f] Who cares about the materials! If set to false, it will just keep mining when its inventory is full
  90. -startDown: [number] If you set this, the turtle will go down this many blocks from the start before starting its quarry
  91.   =
  92.   C _ |
  93.       |
  94.       |
  95.       |
  96.       |_ _ _ _ >
  97. -manualPos: [xPos] [zPos] [yPos] [facing] This is for advanced use. If the server reset when the turtle was in the middle of a 100x100x100 quarry, fear not, you can now manually set the position of the turtle. yPos is always positive. The turtle's starting position is 0, 1, 1, 0. Facing is measured 0 - 3. 0 is forward, and it progresses clockwise. Example- "-manualPos 65 30 30 2"
  98. -help: Thats what this is :D
  99. Examples: Everything below is examples and tips for use
  100. Important Note:
  101.  None of the above parameters are necessary. They all have default values, and the above are just if you want to change them.
  102. Examples [1]:
  103.  Want to just start a quarry from the interface, without going through menus? It's easy! Just use some parameters. Assume you called the program "quarry." To start a 10x6x3 quarry, you just type in "quarry -dim 10 6 3 -default".
  104.   You just told it to start a quarry with dimensions 10x6x3, and "-default" means it won't prompt you about invert or rednet. Wasn't that easy?
  105. Examples [2]:
  106.   Okay, so you've got the basics of this now, so if you want, you can type in really long strings of stuff to make the quarry do exactly what you want. Now, say you want a 40x20x9, but you want it to go down to diamond level, and you're on the surface (at y = 64). You also want it to send rednet messages to your computer so you can see how its doing.
  107. Examples [2] [cont.]:
  108.   Oh yeah! You also want it to use an ender chest in slot 12 and restart if the server crashes. Yeah, you can do that. You would type
  109.   "quarry -dim 40x20x9 -invert false -startDown 45 -rednet true -enderChest 12 -restore"
  110.   BAM. Now you can just let that turtle do it's thing
  111. Tips:
  112.  The order of the parameters doesn't matter. "quarry -invert false -rednet true" is the same as "quarry -rednet true -invert false"
  113.  
  114.   Capitalization doesn't matter. "quarry -iNVErt FALSe" does the same thing as "quarry -invert false"
  115. Tips [cont.]:
  116.  For [t/f] parameters, you can also use "yes" and "no" so "quarry -invert yes"
  117.  
  118.  For [t/f] parameters, it only cares about the first letter. So you can use "quarry -invert t" or "quarry -invert y"
  119. Tips [cont.]:
  120.  If you are playing with fuel turned off, the program will automatically change settings for you so you don't have to :D
  121.  
  122.   If you want, you can load this program onto a computer, and use "quarry -help" so you can have help with the parameters whenever you want.
  123. Internal Config:
  124.   At the top of this program is an internal configuration file. If there is some setup that you use all the time, you can just change the config value at the top and run "quarry -default" for a quick setup.
  125.  
  126.   You can also use this if there are settings that you don't like the default value of.
  127. ]]
  128.  
  129. --Parsing help for display
  130. --[[The way the help table works:
  131. All help indexes are numbered. There is a help[i].title that contains the title,
  132. and the other lines are in help[i][1] - help[i][#help[i] ]
  133. Different lines (e.g. other than first) start with a space.
  134. As of now, the words are not wrapped, fix that later]]
  135. local help = {}
  136. local i = 0
  137. local titlePattern = ".-%:" --Find the beginning of the line, then characters, then a ":"
  138. local textPattern = "%:.+" --Find a ":", then characters until the end of the line
  139. for a in help_paragraph:gmatch("\n?.-\n") do --Matches in between newlines
  140. local current = string.sub(a,1,-2).."" --Concatenate Trick
  141. if string.sub(current,1,1) ~= " " then
  142. i = i + 1
  143. help[i] = {}
  144. help[i].title = string.sub(string.match(current, titlePattern),1,-2)..""
  145. help[i][1] = string.sub(string.match(current,textPattern) or " ",3,-1)
  146. elseif string.sub(current,1,1) == " " then
  147. table.insert(help[i], string.sub(current,2, -1).."")
  148. end
  149. end
  150.  
  151.  
  152. local supportsRednet = (peripheral.wrap("right") ~= nil)
  153.  
  154. local tArgs = {...}
  155. --Pre-defining variables
  156.      xPos,yPos,zPos,facing,percent,mined,moved,relxPos, rowCheck, connected, isInPath, layersDone, attacked, startY, chestFull, gotoDest, atChest, fuelLevel, numDropOffs, allowedItems, compareSlots, dumpSlots, selectedSlot, extraDropItems
  157.    = 0,   1,   1,   0,     0,      0,    0,    1,       true   ,  false,     true,     1,          0,        0,      false,     "",       false,   0,         0,           {},             {},           {},      1,            false
  158.    
  159. for i=1, 16 do --Initializing various inventory management tables
  160.  allowedItems[i] = 0 --Number of items allowed in slot when dropping items
  161.  dumpSlots[i] = false --Does this slot contain junk items?
  162. end --compareSlots is a table of the compare slots, not all slots with a condition
  163. totals = {cobble = 0, fuel = 0, other = 0} -- Total for display (cannot go inside function), this goes up here because many functions use it
  164.  
  165. function resetDumpSlots()
  166.    for i=1, 16 do
  167.      if oreQuarry then
  168.        if turtle.getItemCount(i) > 0 and i~= enderChestSlot then
  169.          dumpSlots[i] = true
  170.        else
  171.          dumpSlots[i] = false
  172.        end
  173.      else
  174.        dumpSlots[i] = false
  175.      end
  176.    end
  177.    if not oreQuarry and enderChestSlot == 1 then
  178.      dumpSlots[2] = true
  179.    elseif not oreQuarry then
  180.      dumpSlots[1] = true
  181.    end
  182. end
  183.        
  184.  
  185. local function copyTable(tab) local toRet = {}; for a, b in pairs(tab) do toRet[a] = b end; return toRet end --This goes up here because it is a basic utility
  186.  
  187. --NOTE: rowCheck is a bit. true = "right", false = "left"
  188.    
  189. local foundBedrock = false
  190.  
  191. local getFuel, checkFuel
  192. if turtle then
  193.  getFuel = turtle.getFuelLevel  --This is for cleanup at the end
  194.  do --Common variable name...
  195.  local flag = turtle.getFuelLevel() == "unlimited"--Unlimited screws up my calculations
  196.  if flag then --Fuel is disabled
  197.    turtle.getFuelLevel = function() return math.huge end --Infinite Fuel
  198.  end --There is no "else" because it will already return the regular getFuel
  199.  end
  200.  checkFuel = turtle.getFuelLevel --Just an alias for backwards compat
  201. end
  202.  
  203. turtle.select(1) --To ensure this is correct
  204. function select(slot)
  205.  if slot ~= selectedSlot then
  206.    selectedSlot = slot
  207.    return turtle.select(slot), selectedSlot
  208.  end
  209. end
  210.  
  211.  
  212. -----------------------------------------------------------------
  213. --Input Phase
  214. local function screen(xPos,yPos)
  215. xPos, yPos = xPos or 1, yPos or 1
  216. term.setCursorPos(xPos,yPos); term.clear(); end
  217. local function screenLine(xPos,yPos)
  218. term.setCursorPos(xPos,yPos); term.clearLine(); end
  219.  
  220. screen(1,1)
  221. print("----- Welcome to Quarry! -----")
  222. print("")
  223.  
  224. local sides = {top = "top", right = "right", left = "left", bottom = "bottom", front = "front"} --Used to whitelist sides
  225. local changedT, tArgsWithUpper = {}, {}
  226. changedT.new = function(key, value) table.insert(changedT,{key, value}) end --Numeric list of lists
  227. local function capitalize(text) return (string.upper(string.sub(text,1,1))..string.sub(text,2,-1)) end
  228. for i=1, #tArgs do tArgsWithUpper[i] = tArgs[i]; tArgsWithUpper[tArgsWithUpper[i]] = i; tArgs[i] = tArgs[i]:lower(); tArgs[tArgs[i]] = i end --My signature key-value pair system, now with upper
  229.  
  230. local restoreFound, restoreFoundSwitch = false --Initializing so they are in scope
  231. function addParam(name, displayText, formatString, forcePrompt, trigger, variableOverride) --To anyone that doesn't understand this very well, probably not your best idea to go in here.
  232.   if trigger == nil then trigger = true end --Defaults to being able to run
  233.   if not trigger then return end --This is what the trigger is for. Will not run if trigger not there
  234.   if restoreFoundSwitch or tArgs["-default"] then forcePrompt = false end --Don't want to prompt if these
  235.   local toGetText = name:lower() --Because all params are now lowered
  236.   local formatType = formatString:match("^%a+"):lower() or error("Format String Unknown: "..formatString) --Type of format string
  237.   local args = formatString:sub(({formatString:find(formatType)})[2] + 2).."" --Everything in formatString but the type and space
  238.   local variable = variableOverride or name --Goes first to the override for name
  239.   local func = loadstring("return "..variable)
  240.   setfenv(func,getfenv(1))
  241.   local originalValue = assert(func)() --This is the default value, for checking to add to changed table
  242.   if originalValue == nil then error("From addParam, \""..variable.."\" returned nil",2) end --I may have gotten a wrong variable name
  243.   local givenValue, toRet --Initializing for use
  244.   if tArgs["-"..toGetText] then
  245.     givenValue = tArgsWithUpper[tArgs["-"..toGetText]+1] --This is the value after the desired parameter
  246.   elseif forcePrompt then
  247.     write(displayText.."? ")
  248.     givenValue = io.read()
  249.   end
  250.   if formatType == "force" then --This is the one exception. Should return true if givenValue is nothing
  251.     toRet = (tArgs["-"..toGetText] and true) or false --Will return true if param exists, otherwise false
  252.   end
  253.   if not (givenValue or toRet) then return end --Don't do anything if you aren't given anything. Leave it as default, except for "force"
  254.   if formatType == "boolean" then --All the format strings will be basically be put through a switch statement
  255.     toRet = givenValue:sub(1,1):lower() == "y" or givenValue:sub(1,1):lower() == "t" --Accepts true or yes
  256.     if formatString == "boolean special" then
  257.       toRet = givenValue:sub(1,1):lower() ~= "n" and givenValue:sub(1,1):lower() ~= "f" --Accepts anything but false or no
  258.     end
  259.   elseif formatType == "string" then
  260.     toRet = givenValue:match("^[%w%.]+") --Basically anything not a space or control character etc
  261.   elseif formatType == "number" then
  262.     toRet = tonumber(givenValue) --Note this is a local, not the above so we don't change anything
  263.     if not toRet then return end --We need a number... Otherwise compare errors
  264.     toRet = math.abs(math.floor(toRet)) --Get proper integers
  265.     local startNum, endNum = formatString:match("(%d+)%-(%d+)") --Gets range of numbers
  266.     startNum, endNum = tonumber(startNum), tonumber(endNum)
  267.     if not ((toRet >= startNum) and (toRet <= endNum)) then return end --Can't use these
  268.   elseif formatType == "side" then
  269.     local exclusionTab = {} --Ignore the wizardry here. Just getting arguments without format string
  270.     for a in args:gmatch("%S+") do exclusionTab[a] = true end --This makes a list of the sides to not include
  271.     if not exclusionTab[givenValue] then toRet = sides[givenValue] end --If side is not excluded
  272.   elseif formatType == "list" then
  273.     toRet = {}
  274.     for a in args:gmatch("[^,]") do
  275.       table.insert(toRet,a)
  276.     end
  277.   elseif formatType == "force" then --Do nothing, everything is already done
  278.   else error("Improper formatType",2)
  279.   end
  280.   if toRet == nil then return end --Don't want to set variables to nil... That's bad
  281.   tempParam = toRet --This is what loadstring will see :D
  282.   local func = loadstring(variable.." = tempParam")
  283.   setfenv(func, getfenv(1))
  284.   func()
  285.   tempParam = nil --Cleanup of global
  286.   if toRet ~= originalValue and displayText ~= "" then
  287.     changedT.new(displayText, tostring(toRet))
  288.   end
  289.   return toRet
  290. end
  291.  
  292. --Check if it is a turtle
  293. if not(turtle or tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"]) then --If all of these are false then
  294.   print("This is not a turtle, you might be looking for the \"Companion Rednet Program\" \nCheck My forum thread for that")
  295.   print("Press 'q' to quit, or any other key to start help ")
  296.   if ({os.pullEvent("char")})[2] ~= "q" then tArgs.help = true else error("",0) end
  297. end
  298.  
  299. if tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"] then
  300.   print("You have selected help, press any key to continue"); print("Use arrow keys to naviate, q to quit"); os.pullEvent("key")
  301.   local pos = 1
  302.   local key = 0
  303.   while pos <= #help and key ~= keys.q do
  304.     if pos < 1 then pos = 1 end
  305.     screen(1,1)
  306.     print(help[pos].title)
  307.     for a=1, #help[pos] do print(help[pos][a]) end
  308.     repeat
  309.       _, key = os.pullEvent("key")
  310.     until key == 200 or key == 208 or key == keys.q
  311.     if key == 200 then pos = pos - 1 end
  312.     if key == 208 then pos = pos + 1 end
  313.   end
  314.   error("",0)
  315. end
  316.  
  317. --Saving
  318. addParam("doBackup", "Backup Save File", "boolean")
  319. addParam("saveFile", "Save File Name", "string")
  320.  
  321. restoreFound = fs.exists(saveFile)
  322. restoreFoundSwitch = (tArgs["-restore"] or tArgs["-resume"] or tArgs["-atchest"]) and restoreFound
  323. if restoreFoundSwitch then
  324.   local file = fs.open(saveFile,"r")
  325.   local test = file.readAll() ~= ""
  326.   file.close()
  327.   if test then
  328.     os.run(getfenv(1),saveFile) --This is where the actual magic happens
  329.     numResumed = numResumed + 1
  330.     if turtle.getFuelLevel() ~= math.huge then --If turtle uses fuel
  331.       if fuelLevel - turtle.getFuelLevel() == 1 then
  332.         if facing == 0 then xPos = xPos + 1
  333.         elseif facing == 2 then xPos = xPos - 1
  334.         elseif facing == 1 then zPos = zPos + 1
  335.         elseif facing == 3 then zPos = zPos - 1 end
  336.       elseif fuelLevel - turtle.getFuelLevel() ~= 0 then
  337.         print("Very Strange Fuel in Restore Section...")
  338.         print("Current: ",turtle.getFuelLevel())
  339.         print("Saved: ",fuelLevel)
  340.         print("Difference: ",fuelLevel - turtle.getFuelLevel())
  341.         os.pullEvent("char")
  342.       end
  343.      end
  344.     if gpsEnabled then --If it had saved gps coordinates
  345.       print("Found GPS Start Coordinates")
  346.       local currLoc = {gps.locate(gpsTimeout)} or {}
  347.       local backupPos = {xPos, yPos, zPos} --This is for comparing to later
  348.       if #currLoc > 0 and #gpsStartPos > 0 and #gpsSecondPos > 0 then --Cover all the different positions I'm using
  349.         print("GPS Position Successfully Read")
  350.         if currLoc[1] == gpsStartPos[1] and currLoc[3] == gpsStartPos[3] then --X coord, y coord, z coord in that order
  351.           xPos, yPos, zPos = 0,1,1
  352.           if facing ~= 0 then turnTo(0) end
  353.           print("Is at start")
  354.         else
  355.           if inverted then --yPos setting
  356.           ------------------------------------------------FIX THIS
  357.           end
  358.           local a, b = copyTable(gpsStartPos), copyTable(gpsSecondPos) --For convenience
  359.           if b[3] - a[3] == -1 then--If went north (-Z)
  360.             a[1] = a[1] - 1 --Shift x one to west to create a "zero"
  361.             xPos, zPos = -currLoc[3] + a[3], currLoc[1] + -a[1]
  362.           elseif b[1] - a[1] == 1 then--If went east (+X)
  363.             a[3] = a[3] - 1 --Shift z up one to north to create a "zero"
  364.             xPos, zPos = currLoc[1] + -a[1], currLoc[3] + -a[3]
  365.           elseif b[3] - a[3] == 1 then--If went south (+Z)
  366.             a[1] = a[1] + 1 --Shift x one to east to create a "zero"
  367.             xPos, zPos = currLoc[3] + a[3], -currLoc[1] + a[3]
  368.           elseif b[1] - a[1] == -1 then--If went west (-X)
  369.             a[3] = a[3] + 1 --Shift z down one to south to create a "zero"
  370.             xPos, zPos = -currLoc[1] + a[1], -currLoc[3] + a[3]
  371.           else
  372.             print("Improper Coordinates")
  373.             print("GPS Locate Failed, Using Standard Methods")        ----Maybe clean this up a bit to use flags instead.
  374.           end  
  375.         end
  376.         print("X Pos: ",xPos)
  377.         print("Y Pos: ",yPos)
  378.         print("Z Pos: ",zPos)
  379.         print("Facing: ",facing)
  380.         for i=1, 3, 2 do --We want 1 and 3, but 2 could be coming back to start.
  381.           if backupPos[i] ~= currLoc[i] then
  382.             events = {} --We want to remove event queue if not in proper place, so won't turn at end of row or things.
  383.           end
  384.         end
  385.       else
  386.         print("GPS Locate Failed, Using Standard Methods")
  387.       end    
  388.     print("Restore File read successfully. Starting in 3"); sleep(3)
  389.     end
  390.   else
  391.     fs.delete(saveFile)
  392.     print("Restore file was empty, sorry, aborting")
  393.     error("",0)
  394.   end
  395. else --If turtle is just starting
  396.   events = {} --This is the event queue :D
  397.   originalFuel = checkFuel() --For use in logging. To see how much fuel is REALLY used
  398. end
  399.  
  400. --Dimensions
  401. if tArgs["-dim"] then
  402.   local a,b,c = x,y,z
  403.   local num = tArgs["-dim"]
  404.   x = tonumber(tArgs[num + 1]) or x; z = tonumber(tArgs[num + 2]) or z; y = tonumber(tArgs[num + 3]) or y
  405.   if a ~= x then changedT.new("Length", x) end
  406.   if c ~= z then changedT.new("Width", z) end
  407.   if b ~= y then changedT.new("Height", y) end
  408. elseif not (tArgs["-default"] or restoreFoundSwitch) then
  409.   print("What dimensions?")
  410.   print("")
  411.   --This will protect from negatives, letters, and decimals
  412.   term.write("Length? ")
  413.   x = math.floor(math.abs(tonumber(io.read()) or x))
  414.   term.write("Width? ")
  415.   z = math.floor(math.abs(tonumber(io.read()) or z))
  416.   term.write("Height? ")
  417.   y = math.floor(math.abs(tonumber(io.read()) or y))
  418.   changedT.new("Length",x); changedT.new("Width",z); changedT.new("Height",y)
  419. end
  420. --Params: parameter/variable name, display name, type, force prompt, boolean condition, variable name override
  421. --Invert
  422. addParam("invert", "Inverted","boolean", true, nil, "inverted")
  423. addParam("startDown","Start Down","number 1-256")
  424. --Inventory
  425. addParam("chest", "Chest Drop Side", "side front", nil, nil, "dropSide")
  426. addParam("enderChest","Ender Chest Enabled","boolean special", nil, nil, "enderChestEnabled") --This will accept anything (including numbers) thats not "f" or "n"
  427. addParam("enderChest", "Ender Chest Slot", "number 1-16", nil, nil, "enderChestSlot") --This will get the number slot if given
  428. if not enderChestEnabled then enderChestSlot = 0 end --This makes everything better
  429. --Rednet
  430. addParam("rednet", "Rednet Enabled","boolean",true, supportsRednet, "rednetEnabled")
  431. addParam("gps", "GPS Location Services", "force", nil, (not restoreFoundSwitch) and supportsRednet, "gpsEnabled" ) --Has these triggers so that does not record position if restarted.
  432. if gpsEnabled and not restoreFoundSwitch then
  433.   gpsStartPos = {gps.locate(gpsTimeout)} --Stores position in array
  434.   gpsEnabled = #gpsStartPos > 0 --Checks if location received properly. If not, position is not saved
  435. end
  436. addParam("sendChannel", "Rednet Send Channel", "number 1-65535", false, supportsRednet, "channels.send")
  437. addParam("receiveChannel","Rednet Receive Channel", "number 1-65535", false, supportsRednet, "channels.receive")
  438. --Fuel
  439. addParam("uniqueExtras","Unique Items", "number 0-15")
  440. addParam("doRefuel", "Refuel from Inventory","boolean", nil, turtle.getFuelLevel() ~= math.huge) --math.huge due to my changes
  441. addParam("doCheckFuel", "Check Fuel", "boolean", nil, turtle.getFuelLevel() ~= math.huge)
  442. --Logging
  443. addParam("logging", "Logging", "boolean")
  444. addParam("logFolder", "Log Folder", "string")
  445. addParam("logExtension","Log Extension", "string")
  446. --Misc
  447. addParam("startY", "Start Y","number 1-256")
  448. addParam("invCheckFreq","Inventory Check Frequency","number 1-342")
  449. addParam("keepOpen", "Slots to Keep Open", "number 1-15")
  450. addParam("careAboutResources", "Care About Resources","boolean")
  451. addParam("maxTries","Tries Before Bedrock", "number 1-9001")
  452. --Ore Quarry
  453. addParam("oreQuarry", "Ore Quarry", "boolean" )
  454. addParam("dumpCompareItems", "Dump Compare Items", "boolean", nil, oreQuarry) --Do not dump compare items if not oreQuarry
  455. addParam("extraDropItems", "", "force", nil, oreQuarry) --Prompt for extra dropItems
  456. addParam("extraDumpItems", "", "force", nil, oreQuarry, "extraDropItems") --changed to Dump
  457.  
  458. --Manual Position
  459. if tArgs["-manualpos"] then --Gives current coordinates in xPos,zPos,yPos, facing
  460.   local a = tArgs["-manualpos"]
  461.   xPos, zPos, yPos, facing = tonumber(tArgs[a+1]) or xPos, tonumber(tArgs[a+2]) or zPos, tonumber(tArgs[a+3]) or yPos, tonumber(tArgs[a+4]) or facing
  462.   changedT.new("xPos",xPos); changedT.new("zPos",zPos); changedT.new("yPos",yPos); changedT.new("facing",facing)
  463.   restoreFoundSwitch = true --So it doesn't do beginning of quarry behavior
  464. end
  465. if addParam("atChest", "Is at Chest", "force") then --This sets position to 0,1,1, facing forward, and queues the turtle to go back to proper row.
  466.   local neededLayer = math.floor((yPos+1)/3)*3-1 --Make it a proper layer, +- because mining rows are 2, 5, etc.
  467.   if neededLayer > 2 and neededLayer%3 ~= 2 then --If turtle was not on a proper mining layer
  468.     print("Last known pos was not in proper layer, restarting quarry")
  469.     sleep(4)
  470.     neededLayer = 2
  471.   end
  472.   xPos, zPos, yPos, facing, rowCheck, layersDone = 0,1,1, 0, true, math.ceil(neededLayer/3)
  473.   events = {{"goto",1,1,neededLayer, 0}}
  474. end
  475.  
  476.  
  477. local function saveProgress(extras) --Session persistence
  478. exclusions = { modem = true, }
  479. if doBackup then
  480. local toWrite = ""
  481. for a,b in pairs(getfenv(1)) do
  482.   if not exclusions[a] then
  483.       --print(a ,"   ", b, "   ", type(b)) --Debug
  484.     if type(b) == "string" then b = "\""..b.."\"" end
  485.     if type(b) == "table" then b = textutils.serialize(b) end
  486.     if type(b) ~= "function" then
  487.       toWrite = toWrite..a.." = "..tostring(b).."\n"
  488.     end
  489.   end
  490. end
  491. toWrite = toWrite.."doCheckFuel = false\n" --It has already used fuel, so calculation unnesesary
  492. local file
  493. repeat
  494.   file = fs.open(saveFile,"w")
  495. until file --WHY DOES IT SAY ATTEMPT TO INDEX NIL!!!
  496. file.write(toWrite)
  497. if type(extras) == "table" then
  498.   for a, b in pairs(extras) do
  499.     file.write(a.." = "..tostring(b))
  500.   end
  501. end
  502. if turtle.getFuelLevel() ~= math.huge then --Used for location comparing
  503.   file.write("fuelLevel = "..tostring(turtle.getFuelLevel()))
  504. end
  505. file.close()
  506. end
  507. end
  508.  
  509. local area = x*z
  510. local volume = x*y*z
  511. local lastHeight = y%3
  512. layers = math.ceil(y/3)
  513. local yMult = layers --This is basically a smart y/3 for movement
  514. local moveVolume = (area * yMult) --Kept for display percent
  515. --Calculating Needed Fuel--
  516. do --Because many local variables unneeded elsewhere
  517.   local numItems = uniqueExtras --Convenience
  518.   local itemSize = extrasStackSize
  519.   local changeYFuel = 2*(y + startDown)
  520.   local dropOffSupplies = 2*(x + z + y + startDown) --Assumes turtle as far away as possible, and coming back
  521.   local frequency = math.floor(((volume/(64*(16-numItems))) ) --This is complicated: volume / inventory space of turtle, defined as (16-num unique stacks)
  522.                                  * (layers/y)) --This is the ratio of height to actual height mined. Close to 1/3 usually, so divide above by 3
  523.   if enderChestEnabled then frequency = 0 end
  524.   neededFuel = moveVolume + changeYFuel + frequency * dropOffSupplies
  525. end
  526.  
  527. --Getting Fuel
  528. local hasRefueled --This is for oreQuarry prompting
  529. if doCheckFuel and checkFuel() < neededFuel then
  530.   hasRefueled = true
  531.   neededFuel = neededFuel + fuelTable[fuelSafety] --For safety
  532.   print("Not enough fuel")
  533.   print("Current: ",checkFuel()," Needed: ",neededFuel)
  534.   print("Starting SmartFuel...")
  535.   sleep(2) --So they can read everything.
  536.   term.clear()
  537.   local oneFuel, neededFuelItems
  538.   local currSlot = 0
  539.   local function output(text, x, y) --For displaying fuel
  540.     local currX, currY = term.getCursorPos()
  541.     term.setCursorPos(x,y)
  542.     term.clearLine()
  543.     term.write(text)
  544.     term.setCursorPos(currX,currY)
  545.     end
  546.   local function roundTo(num, target) --For stacks of fuel
  547.     if num >= target then return target elseif num < 0 then return 0 else return num end
  548.   end
  549.   local function updateScreen()
  550.     output("Welcome to SmartFuel! Now Refueling...", 1,1)
  551.     output("Currently taking fuel from slot "..currSlot,1,2)
  552.     output("Current single fuel: "..tostring(oneFuel or 0),1,3)
  553.     output("Current estimate of needed fuel: ",1,4)
  554.     output("Single Items: "..math.ceil(neededFuelItems or 0),4,5)
  555.     output("Stacks:       "..math.ceil((neededFuelItems or 0) / 64),4,6)
  556.     output("Needed Fuel: "..tostring(neededFuel),1,12)
  557.     output("Current Fuel: "..tostring(checkFuel()),1,13)
  558.   end
  559.   while checkFuel() <= neededFuel do
  560.     currSlot = currSlot + 1
  561.     select(currSlot)
  562.     updateScreen()
  563.     while turtle.getItemCount(currSlot) == 0 do sleep(1.5) end
  564.     repeat
  565.       local previous = checkFuel()
  566.       turtle.refuel(1)
  567.       oneFuel = checkFuel() - previous
  568.       updateScreen()
  569.     until (oneFuel or 0) > 0 --Not an if to prevent errors if fuel taken out prematurely.
  570.     neededFuelItems = (neededFuel - checkFuel()) / oneFuel
  571.     turtle.refuel(math.ceil(roundTo(neededFuelItems, 64))) --Change because can only think about 64 at once.
  572.     if turtle.getItemCount(roundTo(currSlot + 1, 16)) == 0 then --Resets if no more fuel
  573.       currSlot = 0
  574.     end
  575.     neededFuelItems = (neededFuel - checkFuel()) / oneFuel
  576.   end
  577. end
  578. --Ender Chest Obtaining
  579. function promptEnderChest()
  580.   while turtle.getItemCount(enderChestSlot) ~= 1 do
  581.     screen(1,1)
  582.     print("You have decided to use an Ender Chest!")
  583.     print("Please place one Ender Chest in slot ",enderChestSlot)
  584.     sleep(1)
  585.   end
  586.   print("Ender Chest in slot ",enderChestSlot, " checks out")
  587. end
  588. if enderChestEnabled then
  589.     if restoreFoundSwitch and turtle.getItemCount(enderChestSlot) == 0 then --If the turtle was stopped while dropping off items.
  590.       select(enderChestSlot)
  591.       turtle.dig()
  592.       select(1)
  593.     end
  594.   promptEnderChest()
  595.   allowedItems[enderChestSlot] = 64
  596.   sleep(2)
  597. end
  598. --Setting which slots are marked as compare slots
  599. if oreQuarry then
  600.   if not restoreFoundSwitch then --We don't want to reset compare blocks every restart
  601.     local counter = 0
  602.     for i=1, 16 do if turtle.getItemCount(i) > 0 and i ~= enderChestSlot then counter = counter+1 end end --If the slot has items, but isn't enderChest slot if it is enabled
  603.  
  604.     screen(1,1)
  605.     print("You have selected an Ore Quarry!")
  606.     if counter == 0 or hasRefueled then --If there are no compare slots, or the turtle has refueled, and probably has fuel in inventory
  607.       print("Please place your compare blocks in the first slots\n")
  608.      
  609.       print("Press Enter when done")
  610.       repeat until ({os.pullEvent("key")})[2] == 28 --Should wait for enter key to be pressed
  611.     else
  612.       print("Registering slots as compare slots")
  613.       sleep(1)
  614.     end
  615.     for i=1, 16 do
  616.       if turtle.getItemCount(i) > 0 then
  617.         if i ~= enderChestSlot then
  618.           table.insert(compareSlots, i) --Compare slots are ones compared to while mining. Conditions are because we Don't want to compare to enderChest
  619.           allowedItems[i] = 1 --Blacklist is for dropping off items. The number is maximum items allowed in slot when dropping off
  620.           dumpSlots[i] = true --We also want to ignore all excess of these items, like dirt
  621.         end
  622.       end
  623.     end
  624.     if extraDropItems then
  625.       screen(1,1)
  626.       print("Put in extra drop items now\n")
  627.       print("Press Enter when done")
  628.       repeat until ({os.pullEvent("key")})[2] == 28 --Should wait for enter key to be pressed
  629.       for i=1,16 do
  630.         if not dumpSlots[i] and turtle.getItemCount(i) > 0 then --I don't want to modify from above, so I check it hasn't been assigned.
  631.           dumpSlots[i] = true
  632.           allowedItems[i] = 1
  633.         end
  634.       end
  635.     end
  636.     --This is could go very wrong if this isn't here
  637.     if #compareSlots >= 16-keepOpen then screen(1,1); error("You have more quarry compare items than keep open slots, the turtle will continuously come back to start. Please fix.",0) end
  638.   end
  639.   local counter = 0
  640.   for a, b in pairs(compareSlots) do if  turtle.getItemCount(b) > 0 then counter = counter + 1 end end
  641.   if counter == 0 then
  642.     screen(1,1)
  643.     print("You have an ore quarry without any compare slots. Continue? y/n")
  644.     if ({os.pullEvent("char")})[2] ~= "y" then error("",0) end
  645.   end
  646. else
  647.   dumpCompareItems = false --If not an ore quarry, this should definately be false
  648.   if enderChestSlot == 1 then
  649.     dumpSlots[2] = true
  650.   else
  651.     dumpSlots[1] = true
  652.   end
  653. end
  654.  
  655. --Initial Rednet Handshake
  656. if rednetEnabled then
  657. screen(1,1)
  658. print("Rednet is Enabled")
  659. print("The Channel to open is "..channels.send)
  660. modem = peripheral.wrap("right")
  661. modem.open(channels.receive)
  662. local i = 0
  663.   repeat
  664.     local id = os.startTimer(3)
  665.     i=i+1
  666.     print("Sending Initial Message "..i)
  667.     modem.transmit(channels.send, channels.receive, channels.message)
  668.     local message
  669.     repeat
  670.       local event, idCheck, channel,_,locMessage, distance = os.pullEvent()
  671.       message = locMessage
  672.     until (event == "timer" and idCheck == id) or (event == "modem_message" and channel == channels.receive and message == channels.confirm)
  673.   until message == channels.confirm
  674. connected = true
  675. print("Connection Confirmed!")
  676. sleep(1.5)
  677. end
  678. function biometrics(isAtBedrock)
  679.   if not rednetEnabled then return end --This function won't work if rednet not enabled :P
  680.   local toSend = { label = os.getComputerLabel() or "No Label", id = os.getComputerID(),
  681.     percent = percent, relxPos = relxPos, zPos = zPos, xPos = xPos, yPos = yPos,
  682.     layersDone = layersDone, x = x, z = z, layers = layers,
  683.     openSlots = getNumOpenSlots(), mined = mined, moved = moved,
  684.     chestFull = chestFull, isAtChest = (xPos == 0 and yPos == 1 and zPos == 1),
  685.     isGoingToNextLayer = (gotoDest == "layerStart"), foundBedrock = foundBedrock,
  686.     fuel = turtle.getFuelLevel(), volume = volume,
  687.     }
  688.   modem.transmit(channels.send, channels.receive, textutils.serialize(toSend))
  689.   id = os.startTimer(0.1)
  690.   local event, message
  691.   repeat
  692.     local locEvent, idCheck, confirm, _, locMessage, distance = os.pullEvent()
  693.     event, message = locEvent, locMessage or ""
  694.   until (event == "timer" and idCheck == id) or (event == "modem_message" and confirm == channels.receive)
  695.   if event == "modem_message" then connected = true else connected = false end
  696.   message = message:lower()
  697.   if message == "stop" then error("Rednet said to stop...",0) end
  698.   if message == "return" then
  699.     endingProcedure()
  700.     error('Rednet said go back to start...',0)
  701.   end
  702.   if message == "drop" then
  703.     dropOff()
  704.   end
  705. end
  706. --Showing changes to settings
  707. screen(1,1)
  708. print("Your selected settings:")
  709. if #changedT == 0 then
  710. print("Completely Default")
  711. else
  712. for i=1, #changedT do
  713. print(changedT[i][1],": ",changedT[i][2]) --Name and Value
  714. end
  715. end
  716. print("\nStarting in 3"); sleep(1); print("2"); sleep(1); print("1"); sleep(1.5) --Dramatic pause at end
  717.  
  718.  
  719.  
  720. ----------------------------------------------------------------
  721. --Define ALL THE FUNCTIONS
  722. --Event System Functions
  723. function eventAdd(...)
  724.   return table.insert(events,1, {...}) or true
  725. end
  726. function eventGet(pos)
  727.   return events[tonumber(pos) or #events]
  728. end
  729. function eventPop(pos)
  730.   return table.remove(events,tonumber(pos) or #events) or false --This will return value popped, tonumber returns nil if fail, so default to end
  731. end
  732. function eventRun(value, ...)
  733.   local argsList = {...}
  734.   if type(value) == "string" then
  735.     if value:sub(-1) ~= ")" then --So supports both "up()" and "up"
  736.       value = value .. "("
  737.       for a, b in pairs(argsList) do --Appending arguments
  738.         local toAppend
  739.         if type(b) == "table" then toAppend = textutils.serialize(b)
  740.         elseif type(b) == "string" then toAppend = "\""..tostring(b).."\"" --They weren't getting strings around them
  741.         else toAppend = tostring(b) end
  742.         value = value .. (toAppend or "true") .. ", "
  743.       end
  744.       if value:sub(-1) ~= "(" then --If no args, do not want to cut off
  745.         value = value:sub(1,-3)..""
  746.       end
  747.       value = value .. ")"
  748.     end
  749.     --print(value) --Debug
  750.     local func = loadstring(value)
  751.     setfenv(func, getfenv(1))
  752.     return func()
  753.   end
  754. end
  755. function eventClear(pos)
  756.   if pos then events[pos] = nil else events = {} end
  757. end  
  758. function runAllEvents()
  759.   while #events > 0 do
  760.     local toRun = eventGet()
  761.     --print(toRun[1]) --Debug
  762.     eventRun(unpack(toRun))
  763.     eventPop()
  764.   end
  765. end
  766.  
  767. --Display Related Functions
  768. function display() --This is just the last screen that displays at the end
  769.   screen(1,1)
  770.   print("Total Blocks Mined: "..mined)
  771.   print("Current Fuel Level: "..turtle.getFuelLevel())
  772.   print("Cobble: "..totals.cobble)
  773.   print("Usable Fuel: "..totals.fuel)
  774.   print("Other: "..totals.other)
  775.   if rednetEnabled then
  776.     print("")
  777.     print("Sent Stop Message")
  778.     local finalTable = {mined = mined, cobble = totals.cobble, fuelblocks = totals.fuel,
  779.         other = totals.other, fuel = checkFuel() }
  780.     modem.transmit(channels.send,channels.receive,"stop")
  781.     sleep(0.5)
  782.     modem.transmit(channels.send,channels.receive,textutils.serialize(finalTable))
  783.     modem.close(channels.receive)
  784.   end
  785.   if doBackup then fs.delete(saveFile) end
  786. end
  787. function updateDisplay() --Runs in Mine(), display information to the screen in a certain place
  788. screen(1,1)
  789. print("Blocks Mined")
  790. print(mined)
  791. print("Percent Complete")
  792. print(percent.."%")
  793. print("Fuel")
  794. print(checkFuel())
  795.   -- screen(1,1)
  796.   -- print("Xpos: ")
  797.   -- print(xPos)
  798.   -- print("RelXPos: ")
  799.   -- print(relxPos)
  800.   -- print("Z Pos: ")
  801.   -- print(zPos)
  802.   -- print("Y pos: ")
  803.   -- print(yPos)
  804. if rednetEnabled then
  805. screenLine(1,7)
  806. print("Connected: "..tostring(connected))
  807. end
  808. end
  809. --Utility functions
  810. function logMiningRun(textExtension, extras) --Logging mining runs
  811.   if not logging then return end
  812.   local number, name = 0
  813.   if not fs.isDir(logFolder) then
  814.     fs.delete(logFolder)
  815.     fs.makeDir(logFolder)
  816.   end
  817.   repeat
  818.     number = number + 1 --Number will be at least 2
  819.     name = logFolder.."/Quarry_Log_"..tostring(number)..(textExtension or "")
  820.   until not fs.exists(name)
  821.   local handle = fs.open(name,"w")
  822.   local function write(...)
  823.     for a, b in ipairs({...}) do
  824.       handle.write(tostring(b))
  825.     end
  826.     handle.write("\n")
  827.   end
  828.   local function boolToText(bool) if bool then return "Yes" else return "No" end end
  829.   write("Welcome to the Quarry Logs!")
  830.   write("Entry Number: ",number)
  831.   write("Quarry Version: ",VERSION)
  832.   write("Dimensions (X Z Y): ",x," ",z," ", y)
  833.   write("Blocks Mined: ", mined)
  834.   write("  Cobble: ", totals.cobble)
  835.   write("  Usable Fuel: ", totals.fuel)
  836.   write("  Other: ",totals.other)
  837.   write("Total Fuel Used: ",  (originalFuel or (neededFuel + checkFuel()))- checkFuel()) --Protect against errors with some precision
  838.   write("Expected Fuel Use: ", neededFuel)
  839.   write("Days to complete mining run: ",os.day()-originalDay)
  840.   write("Day Started: ", originalDay)
  841.   write("Number of times resumed: ", numResumed)
  842.   write("Was an ore quarry? ",boolToText(oreQuarry))
  843.   write("Was inverted? ",boolToText(invert))
  844.   write("Was using rednet? ",boolToText(rednetEnabled))
  845.   write("Chest was on the ",dropSide," side")
  846.   if startDown > 0 then write("Started ",startDown," blocks down") end
  847.   handle.close()
  848. end
  849. --Inventory related functions
  850. function isFull(slots) --Checks if there are more than "slots" used inventory slots.
  851.   slots = slots or 16
  852.   local numUsed = 0
  853.   sleep(0)
  854.   for i=1, 16 do
  855.     if turtle.getItemCount(i) > 0 then numUsed = numUsed + 1 end
  856.   end
  857.   if numUsed > slots then
  858.     return true
  859.   end
  860.   return false
  861. end
  862. function countUsedSlots() --Returns number of slots with items in them, as well as a table of item counts
  863.   local toRet, toRetTab = 0, {}
  864.   for i=1, 16 do
  865.     local a = turtle.getItemCount(i)
  866.     if a > 0 then toRet = toRet + 1 end
  867.     table.insert(toRetTab, a)
  868.   end
  869.   return toRet, toRetTab
  870. end
  871. function getSlotsTable() --Just get the table from above
  872.   local _, toRet = countUsedSlots()
  873.   return toRet
  874. end
  875. function getChangedSlots(tab1, tab2) --Returns a table of changed slots. Format is {slotNumber, numberChanged}
  876.   local toRet = {}
  877.   for i=1, min(#tab1, #tab2) do
  878.     diff = math.abs(tab2[i]-tab1[i])
  879.     if diff > 0 then
  880.       table.insert(toRet, {i, diff})
  881.     end
  882.   end
  883.   return toRet
  884. end
  885. function getFirstChanged(tab1, tab2) --Just a wrapper. Probably not needed
  886.   local a = getChangedSlots(tab1,tab2)
  887.   return a[1][1]
  888. end
  889.  
  890. function getRep(which, list) --Gets a representative slot of a type. Expectation is a sequential table of types
  891.   for a,b in pairs(list) do
  892.     if b == which then return a end
  893.   end
  894.   return false
  895. end
  896. function assignTypes(types, count) --The parameters allow a preexisting table to be used, like a table from the origianl compareSlots...
  897.   types, count = types or {1}, count or 1 --Table of types and current highest type
  898.   for i=1, 16 do
  899.     if turtle.getItemCount(i) > 0 then
  900.       select(i)
  901.       for k=1, count do
  902.         if turtle.compareTo(getRep(k, types)) then types[i] = k end
  903.       end
  904.       if not types[i] then
  905.         count = count + 1
  906.         types[i] = count
  907.       end
  908.      
  909.     end
  910.   end
  911.   select(1)
  912.   return types, count
  913. end
  914. function getTableOfType(which, list) --Returns a table of all the slots of which type
  915.   local toRet = {}
  916.   for a, b in pairs(list) do
  917.     if b == which then
  918.       table.insert(toRet, a)
  919.     end
  920.   end
  921.   return toRet
  922. end
  923.  
  924. --This is so the turtle will properly get types, otherwise getRep of a type might not be a dumpSlot, even though it should be.
  925. if not restoreFoundSwitch then --We only want this to happen once
  926.   if oreQuarry then --If its not ore quarry, this screws up type assigning
  927.     initialTypes, initialCount = assignTypes()
  928.   else
  929.     initialTypes, initialCount = {1}, 1
  930.   end
  931. end
  932.  
  933. function count(add) --Done any time inventory dropped and at end, param is add or subtract
  934.   local mod = -1
  935.   if add then mod = 1 end
  936.   slot = {}        --1: Filler 2: Fuel 3:Other --[1] is type, [2] is number
  937.   for i=1, 16 do  
  938.     slot[i] = {}
  939.     slot[i][2] = turtle.getItemCount(i)
  940.   end
  941.  
  942.   local function iterate(toSet , rawTypes, set)
  943.     for _, a in pairs(getTableOfType(toSet, rawTypes)) do --Get all slots matching type
  944.       slot[a][1] = set --Set official type to "set"
  945.     end
  946.   end
  947.  
  948.   --This assigns "dumb" types to all slots based on comparing, then based on knowledge of dump type slots, changes all slots matching a dump type to one. Otherwise, if the slot contains fuel, it is 2, else 3
  949.   local rawTypes, numTypes = assignTypes(copyTable(initialTypes), initialCount) --This gets increasingly numbered types, copyTable because assignTypes will modify it
  950.  
  951.   for i=1, numTypes do
  952.     if (select(getRep(i, rawTypes)) or true) and turtle.refuel(0) then --Selects the rep slot, checks if it is fuel
  953.       iterate(i, rawTypes, 2) --This type is fuel
  954.     elseif dumpSlots[getRep(i,initialTypes)] then --If the rep of this slot is a dump item. This is intitial types so that the rep is in dump slots
  955.       iterate(i, rawTypes, 1) --This type is cobble/filler
  956.     else
  957.       iterate(i, rawTypes, 3) --This type is other
  958.     end
  959.   end
  960.    
  961.     for i=1,16 do
  962.       if i == enderChestSlot then --Do nothing!
  963.       elseif slot[i][1] == 1 then totals.cobble = totals.cobble + (slot[i][2] * mod)
  964.       elseif slot[i][1] == 2 then totals.fuel = totals.fuel + (slot[i][2] * mod)
  965.       elseif slot[i][1] == 3 then totals.other = totals.other + (slot[i][2] * mod) end
  966.     end
  967.  
  968.   select(1)
  969. end
  970.  
  971. --Mining functions
  972. function dig(doAdd, func)
  973.   if doAdd == nil then doAdd = true end
  974.   func = func or turtle.dig
  975.   if func() then
  976.     if doAdd then
  977.       mined = mined + 1
  978.     end
  979.     return true
  980.   end
  981.   return false
  982. end
  983.  
  984.  
  985.  
  986. function digUp(doAdd)--Regular functions :) I switch definitions for optimization (I think)
  987.   return dig(doAdd, turtle.digUp)
  988. end
  989. function digDown(doAdd)
  990.   return dig(doAdd, turtle.digDown)
  991. end
  992. if inverted then --If inverted, switch the options
  993.   digUp, digDown = digDown, digUp
  994. end
  995.  
  996. function smartDig(digUp, digDown) --This function is used only in mine when oreQuarry
  997.   local blockAbove, blockBelow = digUp and turtle.detectUp(), digDown and turtle.detectDown() --These control whether or not the turtle digs
  998.   local index = 1
  999.   for i=1, #compareSlots do
  1000.     if not (blockAbove or blockBelow) then break end --We don't want to go selecting if there is nothing to dig
  1001.     index = i --To access out of scope
  1002.     select(compareSlots[i])
  1003.     if blockAbove and turtle.compareUp() then blockAbove = false end
  1004.     if blockBelow and turtle.compareDown() then blockBelow = false end
  1005.   end
  1006.   table.insert(compareSlots, 1, table.remove(compareSlots, index)) --This is so the last selected slot is the first slot checked, saving a turtle.select call
  1007.   if blockAbove then dig(true, turtle.digUp) end
  1008.   if blockBelow then dig(true, turtle.digDown) end
  1009. end
  1010.  
  1011. function setRowCheckFromPos()
  1012.   rowCheck = (zPos % 2 == 1) --It will turn right at odd rows
  1013. end
  1014. function relxCalc()
  1015.   if rowCheck then relxPos = xPos else relxPos = (x-xPos)+1 end
  1016. end
  1017. function forward(doAdd)
  1018.   if doAdd == nil then doAdd = true end
  1019.   if turtle.forward() then
  1020.     if doAdd then
  1021.       moved = moved + 1
  1022.     end
  1023.     if facing == 0 then
  1024.       xPos = xPos + 1
  1025.     elseif facing == 1 then
  1026.       zPos = zPos + 1
  1027.     elseif facing == 2 then
  1028.       xPos = xPos - 1
  1029.     elseif facing == 3 then
  1030.       zPos = zPos - 1
  1031.     else
  1032.       error("Function forward, facing should be 0 - 3, got "..tostring(facing),2)
  1033.     end
  1034.     relxCalc()
  1035.     return true
  1036.   end
  1037.   return false
  1038. end
  1039. function up(sneak)
  1040.   sneak = sneak or 1
  1041.   if inverted and sneak == 1 then
  1042.     down(-1)
  1043.   else
  1044.     while not turtle.up() do --Absolute dig, not relative
  1045.       if not dig(true, turtle.digUp) then
  1046.         attackUp()
  1047.         sleep(0.5)
  1048.       end
  1049.     end
  1050.     yPos = yPos - sneak --Oh! I feel so clever
  1051.   end                   --This works because inverted :)
  1052.   saveProgress()
  1053.   biometrics()
  1054. end
  1055. function down(sneak)
  1056.   sneak = sneak or 1
  1057.   local count = 0
  1058.   if inverted and sneak == 1 then
  1059.     up(-1)
  1060.   else
  1061.     while not turtle.down() do
  1062.       count = count + 1
  1063.       if not dig(true, turtle.digDown) then --This is absolute dig down, not relative
  1064.         attackDown()
  1065.         sleep(0.2)
  1066.       end
  1067.       if count > 20 then bedrock() end
  1068.     end
  1069.     yPos = yPos + sneak
  1070.   end
  1071.   saveProgress()
  1072.   biometrics()
  1073. end
  1074. function right(num)
  1075.   num = num or 1
  1076.   for i=1, num do facing = coterminal(facing+1); saveProgress(); turtle.turnRight() end
  1077. end
  1078. function left(num)
  1079.   num = num or 1
  1080.   for i=1, num do facing = coterminal(facing-1); saveProgress(); turtle.turnLeft() end
  1081. end
  1082. function attack(doAdd, func)
  1083.   doAdd = doAdd or true
  1084.   func = func or turtle.attack
  1085.   if func() then
  1086.     if doAdd then
  1087.       attacked = attacked + 1
  1088.     end
  1089.     return true
  1090.   end
  1091.   return false
  1092. end
  1093. function attackUp(doAdd)
  1094.   if inverted then
  1095.     return attack(doAdd, turtle.attackDown)
  1096.   else
  1097.     return attack(doAdd, turtle.attackUp)
  1098.   end
  1099. end
  1100. function attackDown(doAdd)
  1101.   if inverted then
  1102.     return attack(doAdd, turtle.attackUp)
  1103.   else
  1104.     return attack(doAdd, turtle.attackDown)
  1105.   end
  1106. end
  1107.  
  1108.  
  1109. function mine(doDigDown, doDigUp, outOfPath,doCheckInv) -- Basic Move Forward
  1110.   if doCheckInv == nil then doCheckInv = true end
  1111.   if doDigDown == nil then doDigDown = true end
  1112.   if doDigUp == nil then doDigUp = true end
  1113.   if outOfPath == nil then outOfPath = false end
  1114.   isInPath = (not outOfPath) --For rednet
  1115.   if inverted then
  1116.     doDigUp, doDigDown = doDigDown, doDigUp --Just Switch the two if inverted
  1117.   end
  1118.   if doRefuel and checkFuel() <= fuelTable[fuelSafety]/2 then
  1119.     for i=1, 16 do
  1120.     if turtle.getItemCount(i) > 0 then
  1121.       select(i)
  1122.       if checkFuel() < 200 + fuelTable[fuelSafety] then
  1123.         turtle.refuel()
  1124.       end
  1125.     end
  1126.     end
  1127.     select(1)
  1128.   end
  1129.   local count = 0
  1130.   while not forward(not outOfPath) do
  1131.     sleep(0) --Calls coroutine.yield to prevent errors
  1132.     count = count + 1
  1133.     if not dig() then
  1134.       attack()
  1135.     end
  1136.     if count > 10 then
  1137.       attack()
  1138.       sleep(0.2)
  1139.     end
  1140.     if count > maxTries then
  1141.       if turtle.getFuelLevel() == 0 then --Don't worry about inf fuel because I modified this function
  1142.         saveProgress({doCheckFuel = true})
  1143.         error("No more fuel",0)
  1144.       elseif yPos > (startY-7) and turtle.detect() then --If it is near bedrock
  1145.         bedrock()
  1146.       else --Otherwise just sleep for a bit to avoid sheeps
  1147.         sleep(1)
  1148.       end
  1149.     end
  1150.   end
  1151.   checkSanity() --Not kidding... This is necessary
  1152.   saveProgress(tab)
  1153.   if not oreQuarry then --The digging up and down part
  1154.     if doDigUp then
  1155.       while turtle.detectUp() do
  1156.         sleep(0) --Calls coroutine.yield
  1157.         if not dig(true,turtle.digUp) then --This needs to be an absolute, because we are switching doDigUp/Down
  1158.           if not attackUp() then
  1159.             if yPos > (startY-7) then bedrock() end --Checking for bedrock, but respecting user wishes
  1160.           end
  1161.         end
  1162.       end
  1163.     end
  1164.     if doDigDown then
  1165.      dig(true,turtle.digDown) --This needs to be absolute as well
  1166.     end
  1167.   else
  1168.     smartDig(doDigUp, doDigDown)
  1169.   end
  1170.   percent = math.ceil(moved/moveVolume*100)
  1171.   updateDisplay()
  1172.   if doCheckInv and careAboutResources then
  1173.     if moved%invCheckFreq == 0 then
  1174.      if isFull(16-keepOpen) then dropOff() end
  1175.     end
  1176.   end
  1177.   biometrics()
  1178. end
  1179. --Insanity Checking
  1180. function checkSanity()
  1181.   if not isInPath then --I don't really care if its not in the path.
  1182.     return true
  1183.   end
  1184.   if not (facing == 0 or facing == 2) and #events == 0 then --If mining and not facing proper direction and not in a turn
  1185.     turnTo(0)
  1186.     rowCheck = true
  1187.   end
  1188.   if xPos < 0 or xPos > x or zPos < 0 or zPos > z or yPos < 0 then
  1189.     saveProgress()
  1190.     print("I have gone outside boundaries, attempting to fix (maybe)")
  1191.     if xPos > x then goto(x, zPos, yPos, 2) end --I could do this with some fancy math, but this is much easier
  1192.     if xPos < 0 then goto(1, zPos, yPos, 0) end
  1193.     if zPos > z then goto(xPos, z, yPos, 3) end
  1194.     if zPos < 0 then goto(xPos, 1, yPos, 1) end
  1195.     setRowCheckFromPos() --Row check right (maybe left later)
  1196.     relxCalc() --Get relxPos properly
  1197.     eventClear()
  1198.    
  1199.     --[[
  1200.     print("Oops. Detected that quarry was outside of predefined boundaries.")
  1201.     print("Please go to my forum thread and report this with a short description of what happened")
  1202.     print("If you could also run \"pastebin put Civil_Quarry_Restore\" and give me that code it would be great")
  1203.     error("",0)]]
  1204.   end
  1205. end
  1206.  
  1207. local function fromBoolean(input) --Like a calculator
  1208. if input then return 1 end
  1209. return 0
  1210. end
  1211. local function multBoolean(first,second) --Boolean multiplication
  1212. return (fromBoolean(first) * fromBoolean(second)) == 1
  1213. end
  1214. function coterminal(num, limit) --I knew this would come in handy :D
  1215. limit = limit or 4 --This is for facing
  1216. return math.abs((limit*fromBoolean(num < 0))-(math.abs(num)%limit))
  1217. end
  1218. if tArgs["-manualpos"] then
  1219.   facing = coterminal(facing) --Done to improve support for "-manualPos"
  1220.   if facing == 0 then rowCheck = true elseif facing == 2 then rowCheck = false end --Ditto
  1221.   relxCalc() --Ditto
  1222. end
  1223.  
  1224. --Direction: Front = 0, Right = 1, Back = 2, Left = 3
  1225. function turnTo(num)
  1226.   num = num or facing
  1227.   num = coterminal(num) --Prevent errors
  1228.   local turnRight = true
  1229.   if facing-num == 1 or facing-num == -3 then turnRight = false end --0 - 1 = -3, 1 - 0 = 1, 2 - 1 = 1
  1230.   while facing ~= num do          --The above is used to smartly turn
  1231.     if turnRight then
  1232.       right()
  1233.     else
  1234.       left()
  1235.     end
  1236.   end
  1237. end
  1238. function goto(x,z,y, toFace, destination)
  1239.   --Will first go to desired z pos, then x pos, y pos varies
  1240.   x = x or 1; y = y or 1; z = z or 1; toFace = toFace or facing
  1241.   gotoDest = destination or "" --This is used by biometrics
  1242.   --Possible destinations: layerStart, quarryStart
  1243.   if yPos > y then --Will go up first if below position
  1244.     while yPos~=y do up() end
  1245.   end
  1246.   if zPos > z then
  1247.     turnTo(3)
  1248.   elseif zPos < z then
  1249.     turnTo(1)
  1250.   end
  1251.   while zPos ~= z do mine(false,false,true,false) end
  1252.   if xPos > x then
  1253.     turnTo(2)
  1254.   elseif xPos < x then
  1255.     turnTo(0)
  1256.   end
  1257.   while xPos ~= x do mine(false,false,true,false) end
  1258.   if yPos < y then --Will go down after if above position
  1259.     while yPos~=y do down() end
  1260.   end
  1261.   turnTo(toFace)
  1262.   saveProgress()
  1263.   gotoDest = ""
  1264. end
  1265. function getNumOpenSlots()
  1266.   local toRet = 0
  1267.   for i=1, 16 do
  1268.     if turtle.getItemCount(i) == 0 then
  1269.       toRet = toRet + 1
  1270.     end
  1271.   end
  1272.   return toRet
  1273. end
  1274.  
  1275. --[[
  1276. function drop(side, final, allowSkip)
  1277. side = sides[side] or "front"    --The final number means that it will
  1278. if final then final = 0 else final = 1 end --drop a whole stack at the end
  1279. local allowSkip = allowSkip or (final == 0) --This will allow drop(side,t/f, rednetConnected)
  1280. count()
  1281. if doRefuel then
  1282.   for i=1, 16 do
  1283.     if slot[i][1] == 2 then
  1284.       select(i); turtle.refuel()
  1285.     end
  1286.   end
  1287.   select(1)
  1288. end
  1289. if side == "right" then turnTo(1) end
  1290. if side == "left" then turnTo(3) end
  1291. local whereDetect, whereDrop1, whereDropAll
  1292. local _1 = slot[1][2] - final --All but one if final, all if not final
  1293. if side == "top" then
  1294. whereDetect = turtle.detectUp ; whereDrop = turtle.dropUp
  1295. elseif side == "bottom" then
  1296. whereDetect = turtle.detectDown ; whereDrop = turtle.dropDown
  1297. else
  1298. whereDetect = turtle.detect; whereDrop = turtle.drop
  1299. end
  1300. local function waitDrop(val) --This will just drop, but wait if it can't
  1301.   val = val or 64
  1302.   local try = 1
  1303.   while not whereDrop(val) do
  1304.     print("Chest Full, Try "..try)
  1305.     chestFull = true
  1306.     if rednetEnabled then --To send that the chest is full
  1307.       biometrics()
  1308.     end
  1309.     try = try + 1
  1310.     sleep(2)
  1311.   end
  1312.   chestFull = false
  1313. end
  1314. repeat
  1315. local detected = whereDetect()
  1316. if detected then
  1317.   waitDrop(_1)
  1318.   for i=1, 2 do --This is so I quit flipping missing items when chests are partially filled
  1319.     for i=2, 16 do
  1320.       if turtle.getItemCount(i) > 0 then
  1321.         select(i)
  1322.         waitDrop(nil, i)
  1323.       end
  1324.     end
  1325.   end
  1326. elseif not allowSkip then
  1327.   print("Waiting for chest placement place a chest to continue")
  1328.   while not whereDetect() do
  1329.     sleep(1)
  1330.   end
  1331. end
  1332. until detected or allowSkip
  1333. if not allowSkip then totals.cobble = totals.cobble - 1 end
  1334. select(1)
  1335. end
  1336. ]]
  1337.  
  1338. --Ideas: Bring in inventory change-checking functions, count blocks that have been put in, so it will wait until all blocks have been put in.
  1339. local function waitDrop(slot, allowed, whereDrop) --This will just drop, but wait if it can't
  1340.   allowed = allowed or 0
  1341.   while turtle.getItemCount(slot) > allowed do --No more half items stuck in slot!
  1342.     local tries = 1
  1343.     while not whereDrop(turtle.getItemCount(slot)-allowed) do --Drop off only the amount needed
  1344.       screen(1,1)
  1345.       print("Chest Full, Try "..tries)
  1346.       chestFull = true
  1347.       biometrics()--To send that the chest is full
  1348.       tries = tries + 1
  1349.       sleep(2)
  1350.     end
  1351.     chestFull = false
  1352.   end
  1353. end
  1354.  
  1355. function drop(side, final)
  1356.   side = sides[side] or "front"
  1357.   local dropFunc, detectFunc, dropFacing = turtle.drop, turtle.detect, facing+2
  1358.   if side == "top" then dropFunc, detectFunc = turtle.dropUp, turtle.detectUp end
  1359.   if side == "bottom" then dropFunc, detectFunc = turtle.dropDown, turtle.detectDown end
  1360.   if side == "right" then turnTo(1); dropFacing = 0 end
  1361.   if side == "left" then turnTo(3); dropFacing = 0 end
  1362.   local properFacing = facing --Capture the proper direction to be facing
  1363.  
  1364.   count(true) --Count number of items before drop. True means add. This is before chest detect, because could be final
  1365.  
  1366.   while not detectFunc() do
  1367.     if final then return end --If final, we don't need a chest to be placed, we just won't drop.
  1368.     chestFull = true
  1369.     biometrics() --Let the user know there is a problem with chest
  1370.     screen(1,1) --Clear screen
  1371.     print("Waiting for chest placement on ",side," side (when facing quarry)")
  1372.     sleep(2)
  1373.   end
  1374.   chestFull = false
  1375.  
  1376.   for i=1,16 do
  1377.     --if final then allowedItems[i] = 0 end --0 items allowed in all slots if final ----It is already set to 1, so just remove comment if want change
  1378.     if turtle.getItemCount(i) > 0 then --Saves time, stops bugs
  1379.       if slot[i][1] == 1 and dumpCompareItems then turnTo(dropFacing) --Turn around to drop junk, not store it. dumpComapareItems is global config
  1380.       else turnTo(properFacing) --Turn back to proper position... or do nothing if already there
  1381.       end
  1382.       select(i)
  1383.       if doRefuel and slot[i][1] == 2 then turtle.refuel(turtle.getItemCount(i)-allowedItems[i]) --Refueling option working
  1384.       else waitDrop(i, allowedItems[i], dropFunc)
  1385.       end
  1386.     end
  1387.   end
  1388.  
  1389.   if oreQuarry then count(false) end--Subtract the items still there if oreQuarry
  1390.   resetDumpSlots() --So that slots gone aren't counted as dump slots next
  1391.  
  1392.   select(1) --For fanciness sake
  1393.  
  1394. end
  1395.  
  1396. function dropOff() --Not local because called in mine()
  1397.   local currX,currZ,currY,currFacing = xPos, zPos, yPos, facing
  1398.   if careAboutResources then
  1399.     if not enderChestEnabled then --Regularly
  1400.       eventAdd("goto", 1,1,currY,2) --Need this step for "-startDown"
  1401.       eventAdd("goto(0,1,1,2)")
  1402.       eventAdd("drop", dropSide,false)
  1403.       eventAdd("turnTo(0)")
  1404.       eventAdd("mine",false,false,true,false)
  1405.       eventAdd("goto(1,1,1, 0)")
  1406.       eventAdd("goto", 1, 1, currY, 0)
  1407.       eventAdd("goto", currX,currZ,currY,currFacing)
  1408.     else --If using an enderChest
  1409.       if turtle.getItemCount(enderChestSlot) ~= 1 then eventAdd("promptEnderChest()") end
  1410.       eventAdd("turnTo",currFacing-2)
  1411.       eventAdd("dig",false)
  1412.       eventAdd("select",enderChestSlot)
  1413.       eventAdd("turtle.place")
  1414.       eventAdd("drop","front",false)
  1415.       eventAdd("select", enderChestSlot)
  1416.       eventAdd("dig",false)
  1417.       eventAdd("turnTo",currFacing)
  1418.       eventAdd("select(1)")
  1419.     end
  1420.   runAllEvents()
  1421.   numDropOffs = numDropOffs + 1 --Analytics tracking
  1422.   end
  1423. return true
  1424. end
  1425. function endingProcedure() --Used both at the end and in "biometrics"
  1426.   eventAdd("goto",1,1,yPos,2,"quarryStart") --Allows for startDown variable
  1427.   eventAdd("goto",0,1,1,2, "quarryStart") --Go back to base
  1428.   runAllEvents()
  1429.   --Output to a chest or sit there
  1430.   if enderChestEnabled then
  1431.     if dropSide == "right" then eventAdd("turnTo(1)") end --Turn to proper drop side
  1432.     if dropSide == "left" then eventAdd("turnTo(3)") end
  1433.     eventAdd("dig(false)") --This gets rid of a block in front of the turtle.
  1434.     eventAdd("select",enderChestSlot)
  1435.     eventAdd("turtle.place")
  1436.     eventAdd("select(1)")
  1437.   end
  1438.   eventAdd("drop",dropSide, true)
  1439.   eventAdd("turnTo(0)")
  1440.  
  1441.   --Display was moved above to be used in bedrock function
  1442.   eventAdd("display")
  1443.   --Log current mining run
  1444.   eventAdd("logMiningRun",logExtension)
  1445.   toQuit = true --I'll use this flag to clean up
  1446.   runAllEvents()
  1447.   --Cleanup
  1448.   turtle.getFuelLevel = getFuel
  1449. end
  1450. function bedrock()
  1451.   foundBedrock = true --Let everyone know
  1452.   if rednetEnabled then biometrics() end
  1453.   if checkFuel() == 0 then error("No Fuel",0) end
  1454.   local origin = {x = xPos, y = yPos, z = zPos}
  1455.   print("Bedrock Detected")
  1456.   if turtle.detectUp() then
  1457.     print("Block Above")
  1458.     local var
  1459.     if facing == 0 then var = 2 elseif facing == 2 then var = 0 else error("Was facing left or right on bedrock") end
  1460.     goto(xPos,zPos,yPos,var)
  1461.     for i=1, relxPos do mine(false, false); end
  1462.   end
  1463.   eventClear() --Get rid of any excess events that may be run. Don't want that.
  1464.   endingProcedure()
  1465.   print("\nFound bedrock at these coordinates: ")
  1466.   print(origin.x," Was position in row\n",origin.z," Was row in layer\n",origin.y," Blocks down from start")
  1467.   error("",0)
  1468. end
  1469.  
  1470. function endOfRowTurn(startZ, wasFacing, mineFunctionTable)
  1471. local halfFacing = 1
  1472. local toFace = coterminal(wasFacing + 2) --Opposite side
  1473. if zPos == startZ then
  1474.   if facing ~= halfFacing then turnTo(halfFacing) end
  1475.   mine(unpack(mineFunctionTable or {}))
  1476. end
  1477. if facing ~= toFace then
  1478.   turnTo(toFace)
  1479. end
  1480. end
  1481.  
  1482.  
  1483. -------------------------------------------------------------------------------------
  1484. --Pre-Mining Stuff dealing with session persistence
  1485. runAllEvents()
  1486. if toQuit then error("",0) end --This means that it was stopped coming for its last drop
  1487.  
  1488. local doDigDown, doDigUp = (lastHeight ~= 1), (lastHeight == 0) --Used in lastHeight
  1489. if not restoreFoundSwitch then --Regularly
  1490.   --Check if it is a mining turtle
  1491.   if not isMiningTurtle then
  1492.     local a, b = turtle.dig()
  1493.     if a then mined = mined + 1; isMiningTurtle = true
  1494.     elseif b == "Nothing to dig with" then
  1495.       print("This is not a mining turtle. To make a mining turtle, craft me together with a diamond pickaxe")
  1496.       error("",0)
  1497.     end
  1498.   end
  1499.   mine(false,false,true) --Get into quarry by going forward one
  1500.   if gpsEnabled and not restoreFoundSwitch then --The initial locate is done in the arguments. This is so I can figure out what quadrant the turtle is in.
  1501.     gpsSecondPos = {gps.locate(gpsTimeout)} --Note: Does not run this if it has already been restarted.
  1502.   end
  1503.   for i = 1, startDown do
  1504.     eventAdd("down") --Add a bunch of down events to get to where it needs to be.
  1505.   end
  1506.   runAllEvents()
  1507.   if not(y == 1 or y == 2) then down() end --Go down. If y is one or two, it doesn't need to do this.
  1508. else --restore found
  1509.   if not(layersDone == layers and not doDigDown) then digDown() end
  1510.   if not(layersDone == layers and not doDigUp) then digUp() end  --Get blocks missed before stopped
  1511. end
  1512. --Mining Loops--------------------------------------------------------------------------
  1513. select(1)
  1514. while layersDone <= layers do -------------Height---------
  1515. local lastLayer = layersDone == layers --If this is the last layer
  1516. local secondToLastLayer = (layersDone + 1) == layers --This is for the going down at the end of a layer.
  1517. moved = moved + 1 --To account for the first position in row as "moved"
  1518. if not(layersDone == layers and not doDigDown) then digDown() end --This is because it doesn't mine first block in layer
  1519. if not restoreFoundSwitch then rowCheck = true end
  1520. relxCalc()
  1521. while zPos <= z do -------------Width----------
  1522. while relxPos < x do ------------Length---------
  1523. mine(not lastLayer or (doDigDown and lastLayer), not lastLayer or (doDigUp and lastLayer)) --This will be the idiom that I use for the mine function
  1524. end ---------------Length End-------
  1525. if zPos ~= z then --If not on last row of section
  1526.   local func
  1527.   if rowCheck == true then --Swithcing to next row
  1528.   func = "right"; rowCheck = false; else func = false; rowCheck = true end --Which way to turn
  1529.     eventAdd("endOfRowTurn", zPos, facing , {not lastLayer or (doDigDown and lastLayer), not lastLayer or (doDigUp and lastLayer)}) --The table is passed to the mine function
  1530.     runAllEvents()
  1531. else break
  1532. end
  1533. end ---------------Width End--------
  1534. eventAdd("goto",1,1,yPos,0, "layerStart") --Goto start of layer
  1535. if not lastLayer then --If there is another layer
  1536.   for i=1, 2+fromBoolean(not(lastHeight~=0 and secondToLastLayer)) do eventAdd("down()") end --The fromBoolean stuff means that if lastheight is 1 and last and layer, will only go down two
  1537. end
  1538. eventAdd("setRowCheckFromPos")
  1539. eventAdd("relxCalc")
  1540. layersDone = layersDone + 1
  1541. restoreFoundSwitch = false --This is done so that rowCheck works properly upon restore
  1542. runAllEvents()
  1543. end ---------------Height End-------
  1544.  
  1545. endingProcedure() --This takes care of getting to start, dropping in chest, and displaying ending screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement