Advertisement
civilwargeeky

Quarry 3.5.4

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