daily pastebin goal
76%
SHARE
TWEET

Quarry 3.6.3

civilwargeeky Feb 17th, 2015 (edited) 5,118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Civilwargeeky's Quarry Program--
  2.   VERSION = "3.6.3"
  3. --[[
  4. Recent Changes:
  5.   Parameter Files! Create a file of parameters, and use -file to load it!
  6.     Works will with -forcePrompt
  7.   Quarry no longer goes to start at end of row!
  8.   Turtle can go left!
  9.   QuadCopters! Check Lyqyd's thread
  10. New Parameters:
  11.     -overfuel/fuelMultiplier [number]: This number is is what neededFuel is multiplied by when fuel is low.
  12.     -version: This will display the current version number and end the program
  13.     -file [fileName]: This will load a custom configuration file (basically a list of parameters). "##" starts comment lines. In the future "#" will start programs to run (but only through shell)
  14.     -preciseTotals [t/f]: If true, turtle will write exactly what it mined to the logs. It may also transmit it over rednet.
  15.     -forcePrompt [param]: This will add to a list of parameters to force prompt for. So if you say "-forcePrompt doRefuel" it will prompt you "Length","Width","Height","Invert","Do Refuel" etc.
  16. ]]
  17. --Defining things
  18. civilTable = nil; _G.civilTable = {}; setmetatable(civilTable, {__index = getfenv()}); 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. excessFuelAmount = math.huge --How much fuel the turtle will get maximum. Limited by turtle.getFuelLimit in recent CC [Default math.huge]
  34. fuelMultiplier = 1 --How much extra fuel turtle will ask for when it does need fuel [Default 1]
  35. saveFile = "Civil_Quarry_Restore" --Where it saves restore data [Default "Civil_Quarry_Restore"]
  36. autoResume = true --If true, turtle will auto-restart when loaded. [Default true]
  37. startupRename = "oldStartup.quarry" --What the startup is temporarily renamed to [Default "oldStartup.quarry"]
  38. startupName = "startup" --What the turtle auto-resumes with [Default "startup"]
  39. doBackup = true --If it will keep backups for session persistence [Default true]
  40. uniqueExtras = 8 --How many different items (besides cobble) the turtle expects. [Default 8]
  41. maxTries = 200 --How many times turtle will try to dig a block before it "counts" bedrock [Default 200]
  42. gpsEnabled = false -- If option is enabled, will attempt to find position via GPS api [Default false]
  43. gpsTimeout = 3 --The number of seconds the program will wait to get GPS coords. Not in arguments [Default 3]
  44. legacyRednet = false --Use this if playing 1.4.7
  45. logging = true --Whether or not the turtle will log mining runs. [Default ...still deciding]
  46. logFolder = "Quarry_Logs" --What folder the turtle will store logs in [Default "Quarry_Logs"]
  47. logExtension = "" --The extension of the file (e.g. ".txt") [Default ""]
  48. flatBedrock = false --If true, will go down to bedrock to set startDown [Default false]
  49. startDown = 0 --How many blocks to start down from the top of the mine [Default 0]
  50. enderChestEnabled = false --Whether or not to use an ender chest [Default false]
  51. enderChestSlot = 16 --What slot to put the ender chest in [Default 16]
  52. fuelChestEnabled = false --Whether or not to use a fuel chest [Default false]
  53. fuelChestSlot = 15 --What slot to put the fuel chest in [Default 15]
  54. preciseTotals = false --If true, will record exact totals and names for all materials [Default false]
  55. goLeftNotRight = false --Quarry to left, not right (parameter is "left") [Default false]
  56. oreQuarry = false --Enables ore quarry functionality [Default false]
  57. oreQuarryBlacklistName = "oreQuarryBlacklist.txt" --This is the file that will be parsed for item names [Default "oreQuarryBlacklist"]
  58. dumpCompareItems = true --If ore quarry, the turtle will dump items compared to (like cobblestone) [Default true]
  59. inventoryMax = 16 --The max number of slots in the turtle inventory [Default 16] (Not assignable by parameter)
  60. quadEnabled = false --Whether or not to request a quadRotor when out of fuel [Default false]
  61. quadTimeout = 60 * 5 --How long the turtle will wait for a quadRotor [Default 5 minutes]
  62. --Standard number slots for fuel (you shouldn't care)
  63. fuelTable = { --Will add in this amount of fuel to requirement.
  64. safe = 1000,
  65. moderate = 200,
  66. loose = 0 } --Default 1000, 200, 0
  67. --Standard rednet channels
  68. channels = {
  69. send = os.getComputerID() + 1  ,
  70. receive = os.getComputerID() + 101 ,
  71. confirm = "Turtle Quarry Receiver",
  72. message = "Civil's Quarry",
  73. fingerprint = "quarry"
  74. }
  75.  
  76. --AVERAGE USER: YOU DON'T CARE BELOW THIS POINT
  77.  
  78. local help_paragraph = [[
  79. Welcome!: Welcome to quarry help. Below are help entries for all parameters. Examples and tips are at the bottom.
  80. -default: This will force no prompts. If you use this and nothing else, only defaults will be used.
  81. -dim: [length] [width] [height] This sets the dimensions for the quarry
  82. -invert: [t/f] If true, quarry will be inverted (go up instead of down)
  83. -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
  84. -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
  85. -autoResume / autoRestore: Turtle will automatically resume if stopped. Replaces startup
  86. -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.
  87. -oreQuarry: [t/f] If you are using a newer version of CC, you won't have to put in any compare blocks. (CC 1.64+)
  88. -blacklist: [file name] If using oreQuarry, this is the blacklist file it will read. Example --
  89.   minecraft:stone
  90.   minecraft:sand
  91.   ThermalExpansion:Sponge
  92.   ThermalFoundation:Storage
  93.  
  94.   Note: If you have bspkrsCore, look
  95.   for "UniqueNames.txt" in your config
  96. -file: [file name] Will load a file of parameters. One parameter per line. # is a comment line (See the forum thread for more detailed directions)
  97. -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.
  98. -doRefuel: [t/f] If true, the turtle will refuel itself with coal and planks it finds on its mining run
  99. -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...
  100. -overfuel: [number] When fuel is below required, fuel usage is multiplied by this. Large numbers permit more quarries without refueling
  101. -fuelMultiplier: [number] See overfuel
  102. -uniqueExtras: [number] The expected number of slots filled with low-stacking items like ore. Higher numbers request more fuel.
  103. -maxFuel: [number] How much the turtle will fuel to max (limited by turtle in most cases)
  104. -chest: [side] This specifies what side the chest at the end will be on. You can say "top", "bottom", "front", "left", or "right"
  105. -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.
  106. -fuelChest: See the above, but for a fueling chest. Reccommend use with -maxFuel and -doCheckFuel false
  107. -preciseTotals: [t/f] If true, will record a detailed file of everything it mined.
  108. -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
  109. -quad: [t/f] This forces the use of GPS. Make sure you have a network set up. This will request to be refueled by a quadrotor from Lyqyd's mod if the turtle is out of fuel
  110. -quadTimeout: [number] The amount of time the turtle will wait for a quadRotor
  111. -sendChannel: [number] This is what channel your turtle will send rednet messages on
  112. -receiveChannel: [number] This is what channel your turtle will receive rednet messages on
  113. -legacyRednet: [t/f] Check true if using 1.4.7
  114. -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
  115. -startupRename: [file name] What to rename any existing startup to.
  116. -startupName: [file name] What the turtle will save its startup file to.
  117. -extraDropItems: [force] If oreQuarry then this will prompt the user for extra items to drop, but not compare to (like cobblestone)
  118. -dumpCompareItems: [t/f] If oreQuarry and this is true, the turtle will dump off compare blocks instead of storing them in a chest
  119. -oldOreQuarry: [t/f] If you are using new CC versions, you can use this to use the old oreQuarry.
  120. -left: [t/f] If true, turtle will quarry to the left instead of the right
  121. -maxTries: [number] This is the number of times the turtle will try to dig before deciding its run into bedrock.
  122. -forcePrompt: [parameter] Whatever parameter you specify, it will always prompt you, like it does now for invert and dim.
  123. -logging: [t/f] If true, will record information about its mining run in a folder at the end of the mining run
  124. -preciseTotals: [t/f] If true (and turtle.inspect exists), it will log a detailed record of every block the turtle mines (may eventually send it over rednet)
  125. -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...)
  126. -saveFile: [word] This is what the backup file will be called
  127. -logFolder: [word] The folder that quarry logs will be stored in
  128. -logExtension: [word] The extension given to each quarry log (e.g. ".txt" or ".notepad" or whatever)
  129. -keepOpen: [number] This is the number of the slots the turtle will make sure are open. It will check every time it mines
  130. -careAboutResources: [t/f] Who cares about the materials! If set to false, it will just keep mining when its inventory is full
  131. -startDown: [number] If you set this, the turtle will go down this many blocks from the start before starting its quarry
  132.   =
  133.   C _ |
  134.       |
  135.       |
  136.       |
  137.       |_ _ _ _ >
  138. -flatBedrock: [t/f] If true, turtle will find bedrock and "zero" itself so it ends on bedrock level
  139. -promptAll: This is the opposite of -Default, it prompts for everything
  140. -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"
  141. -version: Displays the current quarry version and stops the program
  142. -help: Thats what this is :D
  143. Examples: Everything below is examples and tips for use
  144. Important Note:
  145.   None of the above parameters are necessary. They all have default values, and the above are just if you want to change them.
  146. Examples [1]:
  147.   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".
  148.   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?
  149. Examples [2]:
  150.   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.
  151. Examples [2] [cont.]:
  152.   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
  153.   "quarry -dim 40x20x9 -invert false -startDown 45 -rednet true -enderChest 12 -restore"
  154.   BAM. Now you can just let that turtle do it's thing
  155. Tips:
  156.   The order of the parameters doesn't matter. "quarry -invert false -rednet true" is the same as "quarry -rednet true -invert false"
  157.  
  158.   Capitalization doesn't matter. "quarry -iNVErt FALSe" does the same thing as "quarry -invert false"
  159. Tips [cont.]:
  160.   For [t/f] parameters, you can also use "yes" and "no" so "quarry -invert yes"
  161.  
  162.   For [t/f] parameters, it only cares about the first letter. So you can use "quarry -invert t" or "quarry -invert y"
  163. Tips [cont.]:
  164.   If you are playing with fuel turned off, the program will automatically change settings for you so you don't have to :D
  165.  
  166.   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.
  167. Internal Config:
  168.   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.
  169.  
  170.   You can also use this if there are settings that you don't like the default value of.
  171. ]]
  172. --NOTE: BIOS 114 MEANS YOU FORGOT A COLON
  173. --NOTE: THIS ALSO BREAKS IF YOU REMOVE "REDUNDANT" WHITESPACE
  174. --Parsing help for display
  175. --[[The way the help table works:
  176. All help indexes are numbered. There is a help[i].title that contains the title,
  177. and the other lines are in help[i][1] - help[i][#help[i] ]
  178. Different lines (e.g. other than first) start with a space.
  179. As of now, the words are not wrapped, fix that later]]
  180. local help = {}
  181. local i = 0
  182. local titlePattern = ".-%:" --Find the beginning of the line, then characters, then a ":"
  183. local textPattern = "%:.+" --Find a ":", then characters until the end of the line
  184. for a in help_paragraph:gmatch("\n?.-\n") do --Matches in between newlines
  185.   local current = string.sub(a,1,-2).."" --Concatenate Trick
  186.   if string.sub(current,1,1) ~= " " then
  187.     i = i + 1
  188.     help[i] = {}
  189.     help[i].title = string.sub(string.match(current, titlePattern),1,-2)..""
  190.     help[i][1] = string.sub(string.match(current,textPattern) or " ",3,-1)
  191.   elseif string.sub(current,1,1) == " " then
  192.     table.insert(help[i], string.sub(current,2, -1).."")
  193.   end
  194. end
  195.  
  196. local supportsRednet
  197. if peripheral.find then
  198.   supportsRednet = peripheral.find("modem") or false
  199. else
  200.   supportsRednet = (peripheral.getType("right") == "modem") or false
  201. end
  202.  
  203. --Pre-defining variables that need to be saved
  204.       xPos,yPos,zPos,facing,percent,mined,moved,relxPos, rowCheck, connected, isInPath, layersDone, attacked, startY, chestFull, gotoDest, atChest, fuelLevel, numDropOffs, allowedItems, compareSlots, dumpSlots, selectedSlot, extraDropItems, oldOreQuarry, specialSlots, relzPos
  205.     = 0,   1,   1,   0,     0,      0,    0,    1,       true   ,  false,     true,     1,          0,        0,      false,     "",       false,   0,         0,           {},             {},           {},      1,            false,          false,        {},           0
  206.  
  207. local statusString
  208.  
  209. --Initializing various inventory management tables
  210. for i=1, inventoryMax do
  211.   allowedItems[i] = 0 --Number of items allowed in slot when dropping items
  212.   dumpSlots[i] = false --Does this slot contain junk items?
  213. end --compareSlots is a table of the compare slots, not all slots with a condition
  214. totals = {cobble = 0, fuel = 0, other = 0} -- Total for display (cannot go inside function), this goes up here because many functions use it
  215.  
  216.  
  217. local function newSpecialSlot(index, value)
  218.   value = tonumber(value) or 0 --We only want numerical indexes
  219.   if value ~= 0 and specialSlots[value] then error("Failed making special slot: "..index.."\nSlot "..tonumber(value).." already taken",2) end --Can't overwrite other slots
  220.   specialSlots[index] = value
  221.   specialSlots[value] = index
  222.   return true
  223. end
  224.  
  225. function resetDumpSlots()
  226.     for i=1, inventoryMax do
  227.       if oldOreQuarry then
  228.         if turtle.getItemCount(i) > 0 and i~= specialSlots.enderChest then
  229.           dumpSlots[i] = true
  230.         else
  231.           dumpSlots[i] = false
  232.         end
  233.       else
  234.         dumpSlots[i] = false
  235.       end
  236.     end
  237.     if not oldOreQuarry and specialSlots.enderChest == 1 then
  238.       dumpSlots[2] = true
  239.     elseif not oldOreQuarry then
  240.       dumpSlots[1] = true
  241.     end
  242. end
  243.  
  244. 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
  245.  
  246. --NOTE: rowCheck is a bit. true = "right", false = "left"
  247.  
  248. local foundBedrock = false
  249.  
  250. local checkFuel, checkFuelLimit
  251. if turtle then --Function inits
  252.   checkFuel = turtle.getFuelLevel
  253.   if turtle.getFuelLevel() == "unlimited" then --Fuel is disabled --Unlimited screws up my calculations
  254.     checkFuel = function() return math.huge end --Infinite Fuel
  255.   end --There is no "else" because it will already return the regular getFuel
  256.   if turtle.getFuelLimit then
  257.     checkFuelLimit = function() return math.min(turtle.getFuelLimit(), excessFuelAmount) end --Return the limiting one
  258.     if turtle.getFuelLimit() == "unlimited" then
  259.       checkFuelLimit = function() return math.huge end
  260.     end
  261.   else
  262.     checkFuelLimit = function() return excessFuelAmount end --If the function doesn't exist
  263.   end
  264.  
  265.  
  266.   turtle.select(1) --To ensure this is correct
  267. end
  268.  
  269.  
  270. function select(slot)
  271.   if slot ~= selectedSlot and slot > 0 and slot <= inventoryMax then
  272.     selectedSlot = slot
  273.     return turtle.select(slot), selectedSlot
  274.   end
  275. end
  276.  
  277.  
  278.  -----------------------------------------------------------------
  279. --Input Phase
  280. local function screen(xPos,yPos)
  281. xPos, yPos = xPos or 1, yPos or 1
  282. term.setCursorPos(xPos,yPos); term.clear(); end
  283. local function screenLine(xPos,yPos)
  284. term.setCursorPos(xPos,yPos); term.clearLine(); end
  285.  
  286. screen(1,1)
  287. print("----- Welcome to Quarry! -----")
  288. print("")
  289.  
  290. local sides = {top = "top", right = "right", left = "left", bottom = "bottom", front = "front"} --Used to whitelist sides
  291. local tArgs --Will be set in initializeArgs
  292. local originalArgs = {...}
  293. local changedT, tArgsWithUpper, forcePrompts = {}, {}, {}
  294. changedT.new = function(key, value) table.insert(changedT,{key, value}) end --Numeric list of lists
  295. changedT.remove = function() table.remove(changedT) end
  296. local function capitalize(text) return (string.upper(string.sub(text,1,1))..string.sub(text,2,-1)) end
  297. local function initializeArgs()
  298.   tArgs = copyTable(originalArgs) --"Reset" tArgs
  299.   for i=1, #tArgs do --My signature key-value pair system, now with upper
  300.     tArgsWithUpper[i] = tArgs[i]
  301.     tArgsWithUpper[tArgsWithUpper[i]] = i
  302.     tArgs[i] = tArgs[i]:lower()
  303.     tArgs[tArgs[i]] = i
  304.     if tArgs[i] == "-forceprompt" and i ~= #tArgs then --If the prompt exists then add it to the list of prompts
  305.       forcePrompts[tArgs[i+1]:lower()] = true
  306.     end
  307.   end
  308. end
  309. initializeArgs()
  310.  
  311. local restoreFound, restoreFoundSwitch = false --Initializing so they are in scope
  312. function parseParam(name, displayText, formatString, forcePrompt, trigger, variableOverride, variableExists) --To anyone that doesn't understand this very well, probably not your best idea to go in here.
  313.   if variableExists ~= false then variableExists = true end --Almost all params should have the variable exist. Some don't exist unless invoked
  314.   if trigger == nil then trigger = true end --Defaults to being able to run
  315.   if not trigger then return end --This is what the trigger is for. Will not run if trigger not there
  316.   if restoreFoundSwitch or tArgs["-default"] then forcePrompt = false end --Don't want to prompt if these. Default is no variable because resuming
  317.   if not restoreFoundSwitch and (tArgs["-promptall"] or forcePrompts[name:lower()]) then forcePrompt = true end --Won't prompt if resuming, can prompt all or checks list of prompts
  318.   local toGetText = name:lower() --Because all params are now lowered
  319.   local formatType = formatString:match("^%a+"):lower() or error("Format String Unknown: "..formatString) --Type of format string
  320.   local args = formatString:sub(({formatString:find(formatType)})[2] + 2).."" --Everything in formatString but the type and space
  321.   local variable = variableOverride or name --Goes first to the override for name
  322.   local func = loadstring("return "..variable)
  323.   setfenv(func,getfenv(1))
  324.   local originalValue = assert(func)() --This is the default value, for checking to add to changed table
  325.   if originalValue == nil and variableExists then error("From addParam, \""..variable.."\" returned nil",2) end --I may have gotten a wrong variable name
  326.   local givenValue, toRet --Initializing for use
  327.   if tArgs["-"..toGetText] then
  328.     givenValue = tArgsWithUpper[tArgs["-"..toGetText]+1] --This is the value after the desired parameter
  329.   elseif forcePrompt then
  330.     write(displayText.."? ")
  331.     givenValue = io.read()
  332.   end
  333.   if formatType == "force" then --This is the one exception. Should return true if givenValue is nothing
  334.     toRet = (tArgs["-"..toGetText] and true) or false --Will return true if param exists, otherwise false
  335.   end
  336.   if not (givenValue or toRet) or (type(givenValue) == "string" and #givenValue == 0) then return end --Don't do anything if you aren't given anything. Leave it as default, except for "force"
  337.   if formatType == "boolean" then --All the format strings will be basically be put through a switch statement
  338.     toRet = givenValue:sub(1,1):lower() ~= "n" and givenValue:sub(1,1):lower() ~= "f" --Accepts anything but false or no
  339.   elseif formatType == "string" then
  340.     toRet = givenValue:match("^[%w%./]+") --Basically anything not a space or control character etc
  341.   elseif formatType == "number" or formatType == "float" then
  342.     toRet = tonumber(givenValue) --Note this is a local, not the above so we don't change anything
  343.     if not toRet then return end --We need a number... Otherwise compare errors
  344.     if formatType == "number" then toRet = math.floor(toRet) end --Get proper integers
  345.     local startNum, endNum = formatString:match("(%d+)%-(%d+)") --Gets range of numbers
  346.     startNum, endNum = tonumber(startNum), tonumber(endNum)
  347.     if not ((toRet >= startNum) and (toRet <= endNum)) then return end --Can't use these
  348.   elseif formatType == "side" then
  349.     local exclusionTab = {} --Ignore the wizardry here. Just getting arguments without format string
  350.     for a in args:gmatch("%S+") do exclusionTab[a] = true end --This makes a list of the sides to not include
  351.     if not exclusionTab[givenValue] then toRet = sides[givenValue] end --If side is not excluded
  352.   elseif formatType == "list" then
  353.     toRet = {}
  354.     for a in args:gmatch("[^,]") do
  355.       table.insert(toRet,a)
  356.     end
  357.   elseif formatType == "force" then --Do nothing, everything is already done
  358.   else error("Improper formatType",2)
  359.   end
  360.   if toRet == nil then return end --Don't want to set variables to nil... That's bad
  361.   tempParam = toRet --This is what loadstring will see :D
  362.   local func = loadstring(variable.." = tempParam")
  363.   setfenv(func, getfenv(1))
  364.   func()
  365.   tempParam = nil --Cleanup of global
  366.   if toRet ~= originalValue and displayText ~= "" then
  367.     changedT.new(displayText, tostring(toRet))
  368.   end
  369.   return toRet
  370. end
  371.  
  372. local paramLookup = {}
  373. local function addParam(...)
  374.   local args = {...}
  375.   if not paramLookup[args[1]] then
  376.     local toRet = copyTable(args)
  377.     for i=2, table.maxn(toRet) do --Have to do this because table.remove breaks on nil
  378.       toRet[i-1] = toRet[i]
  379.     end
  380.     table.remove(toRet)
  381.     paramLookup[args[1]] = toRet
  382.   end
  383.   return parseParam(unpack(args, 1, table.maxn(args)))
  384. end
  385.  
  386. local function paramAlias(original, alias)
  387.   local a = paramLookup[original]
  388.   if a then
  389.     if a[5] == nil then a[5] = original end --This is variableOverride because the originals won't put a variable override
  390.     parseParam(alias, unpack(a, 1, table.maxn(a)))
  391.   else
  392.     error("In paramAlias: '"..original.."' did not exist",2)
  393.   end
  394. end
  395.  
  396. --Check if it is a turtle
  397. if not(turtle or tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"]) then --If all of these are false then
  398.   print("This is not a turtle, you might be looking for the \"Companion Rednet Program\" \nCheck My forum thread for that")
  399.   print("Press 'q' to quit, or any other key to start help ")
  400.   if ({os.pullEvent("char")})[2] ~= "q" then tArgs.help = true else error("",0) end
  401. end
  402.  
  403. if tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"] then
  404.   print("You have selected help, press any key to continue"); print("Use arrow keys to navigate, q to quit"); os.pullEvent("key")
  405.   local pos = 1
  406.   local key = 0
  407.   while pos <= #help and key ~= keys.q do
  408.     if pos < 1 then pos = 1 end
  409.     screen(1,1)
  410.     print(help[pos].title)
  411.     for a=1, #help[pos] do print(help[pos][a]) end
  412.     repeat
  413.       _, key = os.pullEvent("key")
  414.     until key == 200 or key == 208 or key == keys.q
  415.     if key == 200 then pos = pos - 1 end
  416.     if key == 208 then pos = pos + 1 end
  417.   end
  418.   error("",0)
  419. end
  420.  
  421. if tArgs["-version"] or tArgs["version"] then
  422.   print("QUARRY VERSION: ",VERSION)
  423.   error("",0) --Exit not so gracefully
  424. end
  425.  
  426. --Loading custom parameter lists
  427. local function split(str, sep)
  428.   assert(#sep == 1, "Split seperator too long. Got '"..sep.."'")
  429.   if not str:match(sep) then return {str} end --So everything else isn't pointless
  430.   local toRet = {}
  431.   toRet[1] = str:match("^([^"..sep.."]-)"..sep)
  432.   for i in str:gmatch(sep.."([^"..sep.."]*)") do --Matches not seperator chars
  433.     toRet[#toRet+1] = i
  434.   end
  435.   return toRet
  436. end
  437.  
  438. if addParam("file","Custom Parameters","string", false, nil, "parameterFile", false) and parameterFile then --Do note, this will load even if it is resuming.
  439.   if not fs.exists(parameterFile) then
  440.     print("WARNING: '"..parameterFile.."' DOES NOT EXIST. FILE NOT LOADED")
  441.     sleep(3)
  442.     changedT.remove()
  443.   else
  444.     local file = fs.open(parameterFile, "r")
  445.     local text = file.readAll()
  446.     file.close()
  447.     text = text.."\n" --So that all replacements work properly
  448.     text = text:gsub("#[^\n]-\n","") --Replace program codes/comment lines+
  449.     local commands = {} --Contains all the parameters
  450.     local append = table.insert
  451.     for _, a in pairs(split(text,"\n")) do
  452.       local words = split(a," ")
  453.       if not a:match("-") then --This means only one command per line
  454.         append(originalArgs,"-"..words[1])
  455.         for i=2, #words do
  456.           append(originalArgs, words[i])
  457.         end
  458.       else --Otherwise the dashes are already ordered where we want!
  459.         for i=1, #words do
  460.           append(originalArgs, words[i])
  461.         end
  462.       end
  463.     end
  464.     initializeArgs() --Manipulate the args again, because we modified them
  465.     print("Finished loading file: ",tArgs[tArgs["-file"]+1])
  466.     sleep(0.5) --Give em a sec
  467.   end
  468. end
  469.  
  470.  
  471.  
  472. --Saving
  473. addParam("doBackup", "Backup Save File", "boolean")
  474. addParam("saveFile", "Save File Name", "string")
  475.  
  476. restoreFound = fs.exists(saveFile)
  477. restoreFoundSwitch = (tArgs["-restore"] or tArgs["-resume"] or tArgs["-atchest"]) and restoreFound and doBackup
  478. if restoreFoundSwitch then
  479.   local file = fs.open(saveFile,"r")
  480.   local test = file.readAll() ~= ""
  481.   file.close()
  482.   if test then
  483.     os.run(getfenv(1),saveFile) --This is where the actual magic happens
  484.     numResumed = numResumed + 1
  485.     if checkFuel() ~= math.huge then --If turtle uses fuel
  486.       if fuelLevel - checkFuel() == 1 then
  487.         if facing == 0 then xPos = xPos + 1
  488.         elseif facing == 2 then xPos = xPos - 1
  489.         elseif facing == 1 then zPos = zPos + 1
  490.         elseif facing == 3 then zPos = zPos - 1 end
  491.       elseif fuelLevel - checkFuel() ~= 0 then
  492.         print("Very Strange Fuel in Restore Section...")
  493.         print("Current: ",checkFuel())
  494.         print("Saved: ",fuelLevel)
  495.         print("Difference: ",fuelLevel - checkFuel())
  496.         os.pullEvent("char")
  497.       end
  498.      end
  499.     if gpsEnabled then --If it had saved gps coordinates
  500.       print("Found GPS Start Coordinates")
  501.       local currLoc = {gps.locate(gpsTimeout)} or {}
  502.       local backupPos = {xPos, yPos, zPos} --This is for comparing to later
  503.       if #currLoc > 0 and #gpsStartPos > 0 and #gpsSecondPos > 0 then --Cover all the different positions I'm using
  504.         print("GPS Position Successfully Read")
  505.         if currLoc[1] == gpsStartPos[1] and currLoc[3] == gpsStartPos[3] then --X coord, y coord, z coord in that order
  506.           xPos, yPos, zPos = 0,1,1
  507.           if facing ~= 0 then turnTo(0) end
  508.           print("Is at start")
  509.         else
  510.           if inverted then --yPos setting
  511.           ------------------------------------------------FIX THIS
  512.           end
  513.           local a, b = copyTable(gpsStartPos), copyTable(gpsSecondPos) --For convenience
  514.           local flag = true --So we can account for left quarry
  515.           if b[3] - a[3] == -1 then--If went north (-Z)
  516.             a[1] = a[1] - 1 --Shift x one to west to create a "zero"
  517.             xPos, zPos = -currLoc[3] + a[3], currLoc[1] + -a[1]
  518.           elseif b[1] - a[1] == 1 then--If went east (+X)
  519.             a[3] = a[3] - 1 --Shift z up one to north to create a "zero"
  520.             xPos, zPos = currLoc[1] + -a[1], currLoc[3] + -a[3]
  521.           elseif b[3] - a[3] == 1 then--If went south (+Z)
  522.             a[1] = a[1] + 1 --Shift x one to east to create a "zero"
  523.             xPos, zPos = currLoc[3] + a[3], -currLoc[1] + a[3]
  524.           elseif b[1] - a[1] == -1 then--If went west (-X)
  525.             a[3] = a[3] + 1 --Shift z down one to south to create a "zero"
  526.             xPos, zPos = -currLoc[1] + a[1], -currLoc[3] + a[3]
  527.           else
  528.             flag = false
  529.             print("Improper Coordinates")
  530.             print("GPS Locate Failed, Using Standard Methods")        ----Maybe clean this up a bit to use flags instead.
  531.           end
  532.           if flag and goLeftNotRight then --This accounts for left quarry (barred to left only because there might be errors in a regular, causing neg/0
  533.             zPos = math.abs(zPos-1) + 1
  534.           end
  535.         end
  536.         print("X Pos: ",xPos)
  537.         print("Y Pos: ",yPos)
  538.         print("Z Pos: ",zPos)
  539.         print("Facing: ",facing)
  540.         for i=1, 3, 2 do --We want 1 and 3, but 2 could be coming back to start.
  541.           if backupPos[i] ~= currLoc[i] then
  542.             events = {} --We want to remove event queue if not in proper place, so won't turn at end of row or things.
  543.           end
  544.         end
  545.       else
  546.         print("GPS Locate Failed, Using Standard Methods")
  547.       end
  548.     print("Restore File read successfully. Starting in 3"); sleep(3)
  549.     end
  550.   else
  551.     fs.delete(saveFile)
  552.     print("Restore file was empty, sorry, aborting")
  553.     error("",0)
  554.   end
  555. else --If turtle is just starting
  556.   events = {} --This is the event queue :D
  557.   originalFuel = checkFuel() --For use in logging. To see how much fuel is REALLY used
  558. end
  559.  
  560. --Dimensions
  561. if tArgs["-dim"] then
  562.   local a,b,c = x,y,z
  563.   local num = tArgs["-dim"]
  564.   x = tonumber(tArgs[num + 1]) or x; z = tonumber(tArgs[num + 2]) or z; y = tonumber(tArgs[num + 3]) or y
  565.   if a ~= x then changedT.new("Length", x) end
  566.   if c ~= z then changedT.new("Width", z) end
  567.   if b ~= y then changedT.new("Height", y) end
  568. elseif not (tArgs["-default"] or restoreFoundSwitch) then
  569.   print("What dimensions?")
  570.   print("")
  571.   --This will protect from negatives, letters, and decimals
  572.   term.write("Length? ")
  573.   x = math.floor(math.abs(tonumber(io.read()) or x))
  574.   term.write("Width? ")
  575.   z = math.floor(math.abs(tonumber(io.read()) or z))
  576.   term.write("Height? ")
  577.   y = math.floor(math.abs(tonumber(io.read()) or y))
  578.   changedT.new("Length",x); changedT.new("Width",z); changedT.new("Height",y)
  579. end
  580. --Params: parameter/variable name, display name, type, force prompt, boolean condition, variable name override
  581. --Invert
  582. addParam("flatBedrock","Go to bedrock", "boolean") --Not done before GPS because GPS only runs on restart
  583. addParam("invert", "Inverted","boolean", true, not flatBedrock, "inverted") --Not flat bedrock, because invert will be set to false
  584. addParam("startDown","Start Down","number 1-256", nil, not flatBedrock)
  585. addParam("left","Left Quarry","boolean", nil, nil, "goLeftNotRight")
  586. --Inventory
  587. addParam("chest", "Chest Drop Side", "side front", nil, nil, "dropSide")
  588. addParam("enderChest","Ender Chest Enabled","boolean special", nil, nil, "enderChestEnabled") --This will accept anything (including numbers) thats not "f" or "n"
  589. addParam("enderChest", "Ender Chest Slot", "number 1-16", nil, nil, "enderChestSlot") --This will get the number slot if given
  590. newSpecialSlot("enderChest",enderChestEnabled and enderChestSlot or 0) --This allows for easy checking and getting --This makes everything better (0)
  591. addParam("fuelChest","Fuel Chest Enabled","boolean special", nil, nil, "fuelChestEnabled") --See above notes
  592. addParam("fuelChest", "Fuel Chest Slot", "number 1-16", nil, nil, "fuelChestSlot")
  593. newSpecialSlot("fuelChest", fuelChestEnabled and fuelChestSlot or 0)
  594. --Rednet
  595. addParam("rednet", "Rednet Enabled","boolean",true, supportsRednet, "rednetEnabled")
  596. addParam("sendChannel", "Rednet Send Channel", "number 1-65535", false, supportsRednet, "channels.send")
  597. addParam("receiveChannel","Rednet Receive Channel", "number 1-65535", false, supportsRednet, "channels.receive")
  598. addParam("fingerprint","Sending Fingerprint", "string", false, supportsRednet, "channels.fingerprint")
  599. addParam("legacyRednet","Legacy Rednet","boolean", false, supportsRednet)
  600. --Quad Rotor --Must be before GPS
  601. if addParam("quad", "Quad Rotor Enabled","boolean",nil, rednetEnabled, "quadEnabled") then --This returns true if param found :3
  602.   gpsEnabled = true
  603. end
  604. addParam("quadTimeout","Quad Rotor Timeout","number 1-1000000", nil, quadEnabled) --The amount of time to wait for a quadRotor
  605. --GPS
  606. addParam("gps", "GPS Location Services", "force", nil, (not restoreFoundSwitch) and supportsRednet and not quadEnabled, "gpsEnabled" ) --Has these triggers so that does not record position if restarted.
  607. if gpsEnabled and not restoreFoundSwitch then
  608.   gpsStartPos = {gps.locate(gpsTimeout)} --Stores position in array
  609.   gpsEnabled = #gpsStartPos > 0 --Checks if location received properly. If not, position is not saved
  610.   if quadEnabled and not gpsEnabled then
  611.     error("You have no GPS network. You may not use Quad Rotors",0)
  612.   end
  613. end
  614. --Fuel
  615. addParam("uniqueExtras","Unique Items", "number 0-15")
  616. addParam("doRefuel", "Refuel from Inventory","boolean", nil, checkFuel() ~= math.huge) --math.huge due to my changes
  617. addParam("doCheckFuel", "Check Fuel", "boolean", nil, checkFuel() ~= math.huge)
  618. excessFuelAmount = excessFuelAmount or math.huge --Math.huge apparently doesn't save properly (Without saving, this is the config, on save it is actually set to nil if math.huge)
  619. addParam("maxFuel", "Max Fuel", "number 1-999999999", nil, checkFuel() ~= math.huge, "excessFuelAmount")
  620. addParam("fuelMultiplier", "Fuel Multiplier", "float 1-9001", nil, checkFuel() ~= math.huge)
  621. paramAlias("fuelMultiplier","fuelRequestMultiplier")
  622. paramAlias("fuelMultiplier","overFuel")
  623. --Logging
  624. addParam("logging", "Logging", "boolean")
  625. addParam("logFolder", "Log Folder", "string")
  626. addParam("logExtension","Log Extension", "string")
  627. --Misc
  628. addParam("startY", "Start Y","number 1-256")
  629. addParam("maxTries","Tries Before Bedrock", "number 1-9001")
  630. --Inventory
  631. addParam("keepOpen", "Slots to Keep Open", "number 1-15")
  632. addParam("careAboutResources", "Care About Resources","boolean")
  633. addParam("preciseTotals","Precise Totals","boolean", nil, turtle.getItemDetail ~= nil)
  634. if preciseTotals and not restoreFoundSwitch then
  635.   exactTotals = {} --Don't want to initialize if we aren't using this
  636. end
  637. --Auto Startup
  638. addParam("autoResume", "Auto Resume", "boolean", nil, doBackup)
  639. paramAlias("autoResume","autoRestart")
  640. addParam("startupRename", "Startup Rename","string", nil, autoResume)
  641. addParam("startupName", "Startup File", "string", nil, autoResume)
  642. --Ore Quarry
  643. addParam("oreQuarry", "Ore Quarry", "boolean" )
  644. if oreQuarry and not turtle.inspect then
  645.   oldOreQuarry = true
  646.   oreQuarry = false
  647. end
  648. --Old Ore
  649. addParam("oldOreQuarry", "Old Ore Quarry", "boolean")
  650. addParam("dumpCompareItems", "Dump Compare Items", "boolean", nil, oldOreQuarry) --Do not dump compare items if not oreQuarry
  651. addParam("extraDropItems", "", "force", nil, oldOreQuarry) --Prompt for extra dropItems
  652. paramAlias("extraDropItems","extraDumpItems") --changed to Dump
  653. --New Ore
  654. addParam("blacklist","Ore Blacklist", "string", nil, oreQuarry, "oreQuarryBlacklistName")
  655. paramAlias("blacklist","blacklistFile")
  656. --Mod Related
  657.  
  658.  
  659. --for flatBedrock
  660. if flatBedrock then
  661.   inverted = false
  662. end
  663.  
  664. --Auto Startup functions
  665. local function doAutoResumeStuff()
  666.   if fs.exists(startupName) then
  667.     if fs.exists(startupRename) then fs.delete(startupRename) end
  668.     fs.move(startupName, startupRename)
  669.   end
  670.   local file = fs.open(startupName,"w") --Startup File
  671.   file.writeLine( --The below is on the left because spacing
  672. [[
  673. --This is an auto-generated startup
  674. --Made by civilwargeeky's Variable Size Quarry
  675. print("Now Resuming Quarry")
  676. print("Press any key to quit. You have 5 seconds.")
  677. function deleteStuff()
  678.   fs.delete("]]..startupName..[[")
  679.   if fs.exists("]]..startupRename..[[") then
  680.     fs.move("]]..startupRename.."\",\""..startupName..[[")
  681.   end
  682. end
  683. local event
  684. if fs.exists("]]..saveFile..[[") then
  685.   for i=5,1,-1 do
  686.     print(i)
  687.     os.startTimer(1)
  688.     event = os.pullEvent()
  689.     if event == "key" then break end
  690.   end
  691.   if event == "timer" then
  692.     os.run({},"]]..shell.getRunningProgram()..[[","-resume")
  693.   else
  694.  
  695.     deleteStuff()
  696.   end
  697. else
  698.   print("Never mind, no save file found")
  699.   deleteStuff()
  700. end
  701.   ]])
  702.   file.close()
  703. end
  704. if autoResume and not restoreFoundSwitch then --Don't do for restore because would overwrite renamed thing. Can't edit mid-run because no shell in restarted
  705.   doAutoResumeStuff()
  706. end
  707. --oreQuarry blacklist
  708. local blacklist = { "minecraft:air",  "minecraft:bedrock", "minecraft:cobblestone", "minecraft:dirt", "minecraft:ice", "minecraft:ladder", "minecraft:netherrack", "minecraft:sand", "minecraft:sandstone",
  709.   "minecraft:snow", "minecraft:snow_layer", "minecraft:stone", "minecraft:gravel", "minecraft:grass", "minecraft:torch" }
  710. for a,b in pairs(blacklist) do
  711.   blacklist[b], blacklist[b] = true, nil --Switch
  712. end
  713. if fs.exists(oreQuarryBlacklistName) then --Loading user-defined blacklist
  714.   local file = fs.open(oreQuarryBlacklistName, "r")
  715.   blacklist = {}
  716.   for a in file:readAll():gmatch("[^,]+") do
  717.     blacklist[a:match("%S+:%S+")] = true --Grab only the actual characters, not whitespaces
  718.   end
  719.   file:close()
  720. end
  721.  
  722. --Manual Position
  723. if tArgs["-manualpos"] then --Gives current coordinates in xPos,zPos,yPos, facing
  724.   local a = tArgs["-manualpos"]
  725.   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
  726.   changedT.new("xPos",xPos); changedT.new("zPos",zPos); changedT.new("yPos",yPos); changedT.new("facing",facing)
  727.   restoreFoundSwitch = true --So it doesn't do beginning of quarry behavior
  728.   for i=0,4 do tArgs[a+i] = "" end --Get rid of this argument from future restores
  729. end
  730. 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.
  731.   local neededLayer = math.floor((yPos+1)/3)*3-1 --Make it a proper layer, +- because mining rows are 2, 5, etc.
  732.   if neededLayer > 2 and neededLayer%3 ~= 2 then --If turtle was not on a proper mining layer
  733.     print("Last known pos was not in proper layer, restarting quarry")
  734.     sleep(4)
  735.     neededLayer = 2
  736.   end
  737.   if ((neededLayer-2)/3) % 2 == 1 then neededLayer = neededLayer - 3 end --Because with weird end of row, just go to last basic row
  738.   xPos, zPos, yPos, facing, rowCheck, layersDone = 0,1,1, 0, true, math.ceil(neededLayer/3)
  739.   doAutoResumeStuff() --This was probably deleted when they hit a key to launch with -atChest
  740.   events = {{"goto",1,1,neededLayer, 0}}
  741. end
  742.  
  743.  
  744. local function saveProgress(extras) --Session persistence
  745. exclusions = { modem = true, }
  746. if doBackup then
  747. local toWrite = ""
  748. for a,b in pairs(getfenv(1)) do
  749.   if not exclusions[a] then
  750.       --print(a ,"   ", b, "   ", type(b)) --Debug
  751.     if type(b) == "string" then b = "\""..b.."\"" end
  752.     if type(b) == "table" then b = textutils.serialize(b) end
  753.     if type(b) ~= "function" then
  754.       toWrite = toWrite..a.." = "..tostring(b).."\n"
  755.     end
  756.   end
  757. end
  758. toWrite = toWrite.."doCheckFuel = false\n" --It has already used fuel, so calculation unnecessary
  759. local file
  760. repeat
  761.   file = fs.open(saveFile,"w")
  762. until file
  763. file.write(toWrite)
  764. if type(extras) == "table" then
  765.   for a, b in pairs(extras) do
  766.     file.write(a.." = "..tostring(b).."\n")
  767.   end
  768. end
  769. if checkFuel() ~= math.huge then --Used for location comparing
  770.   file.write("fuelLevel = "..tostring(checkFuel()).."\n")
  771. end
  772. file.close()
  773. end
  774. end
  775.  
  776. local area = x*z
  777. local volume = x*y*z
  778. local lastHeight = y%3
  779. layers = math.ceil(y/3)
  780. local yMult = layers --This is basically a smart y/3 for movement
  781. local moveVolume = (area * yMult) --Kept for display percent
  782. --Calculating Needed Fuel--
  783. do --Because many local variables unneeded elsewhere
  784.   local changeYFuel = 2*(y + startDown)
  785.   local dropOffSupplies = 2*(x + z + y + startDown) --Assumes turtle as far away as possible, and coming back
  786.   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.
  787.                                                                                      --max of 15 full stacks because once one item is picked up, slot is "full". Ceil to count for initial back and forth
  788.   if enderChestEnabled then frequency = 0 end --Never goes back to start
  789.   neededFuel = moveVolume + changeYFuel + (frequency * dropOffSupplies) + ((x + z) * layers) --x + z *layers because turtle has to come back from far corner every layer
  790.   neededFuel = neededFuel + fuelTable[fuelSafety] --For safety
  791. end
  792.  
  793. if neededFuel > checkFuelLimit() and doCheckFuel then--Checks for if refueling goes over turtle fuel limit
  794.   if not (doRefuel or fuelChestEnabled) then
  795.     screen()
  796.     print("Turtle cannot hold enough fuel\n")
  797.     print("Options: \n1. Select a smaller size \n2. Enable Mid-Run Refueling (RECOMMENDED) \n3. Turn fuel checking off (only if fuel chest) \n4. Do nothing")
  798.     local _, key = os.pullEvent("char")
  799.     if key == "1" then
  800.       screen(); print("Okay"); error("",0)
  801.     elseif key == "3" then
  802.       doCheckFuel = false
  803.     elseif key == "4" then
  804.       --pass
  805.     else --Not for number two because this is default
  806.       doRefuel = true
  807.     end
  808.   end
  809.   neededFuel = checkFuelLimit()-checkFuel()-1
  810. end
  811.  
  812.  
  813. --Getting Fuel
  814. local hasRefueled --This is for oldOreQuarry prompting
  815. if doCheckFuel and checkFuel() < neededFuel then
  816.   neededFuel = math.min(math.floor(neededFuel * fuelMultiplier), checkFuelLimit()-checkFuel()-1) --Does the same as above, but not verbose because optional
  817.   hasRefueled = true
  818.   print("Not enough fuel")
  819.   print("Current: ",checkFuel()," Needed: ",neededFuel)
  820.   print("Starting SmartFuel...")
  821.   sleep(2) --So they can read everything.
  822.   term.clear()
  823.   local oneFuel, neededFuelItems = 0,0 --Initializing Variables
  824.   local currSlot = 0
  825.   local function output(text, x, y) --For displaying fuel statistics
  826.     local currX, currY = term.getCursorPos()
  827.     term.setCursorPos(x,y)
  828.     term.clearLine()
  829.     term.write(text)
  830.     term.setCursorPos(currX,currY)
  831.   end
  832.   local function roundTo(num, target) --For stacks of fuel and turtle slots when undergoing addition/subtraction
  833.     if num >= target then return target elseif num < 0 then return 0 else return num end
  834.   end
  835.   local function updateScreen()
  836.     output("Welcome to SmartFuel! Now Refueling...", 1,1)
  837.     output("Fuel Request Multiplier: "..tostring(fuelMultiplier).."x",1,2)
  838.     output("Currently taking fuel from slot "..currSlot,1,3)
  839.     output("Current single fuel: "..tostring(oneFuel or 0),1,4)
  840.     output("Current estimate of needed fuel: ",1,4)
  841.     output("Single Items: "..math.ceil(neededFuelItems),4,6)
  842.     output("Stacks:       "..math.ceil(neededFuelItems / 64),4,7)
  843.     output("Needed Fuel: "..tostring(neededFuel),1,12)
  844.     output("Current Fuel: "..tostring(checkFuel()),1,13)
  845.   end
  846.   while checkFuel() < neededFuel do
  847.     currSlot = currSlot + 1
  848.     select(currSlot)
  849.     if currSlot ~= 1 and not turtle.refuel(0) then --If it's not the first slot, and not fuel, go back to start
  850.       currSlot = 1; select(currSlot)
  851.     end
  852.     updateScreen()
  853.     while turtle.getItemCount(currSlot) == 0 do
  854.       sleep(1.5)
  855.     end
  856.     repeat --TODO: Probably unnecessary loop, remove later
  857.       local previous = checkFuel()
  858.       turtle.refuel(1)
  859.       oneFuel = checkFuel() - previous
  860.       updateScreen()
  861.     until (oneFuel or 0) > 0 --Not an if to prevent errors if fuel taken out prematurely.
  862.     neededFuelItems = math.ceil((neededFuel - checkFuel()) / oneFuel)
  863.     turtle.refuel(roundTo(neededFuelItems, 64)) --Change because can only think about 64 at once.
  864.     if turtle.getItemCount(roundTo(currSlot + 1, inventoryMax)) == 0 then --Resets if no more fuel
  865.       currSlot = 0
  866.     end
  867.     neededFuelItems = math.ceil((neededFuel - checkFuel()) / oneFuel) --This line is not repeated uselessly, it's for the display function
  868.   end
  869.   select(1)
  870. end
  871. --Ender Chest Obtaining
  872. function promptSpecialSlot(specialSlot, name)
  873.   while turtle.getItemCount(specialSlots[specialSlot]) ~= 1 do
  874.     screen(1,1)
  875.     print("You have decided to use a ",name,"!")
  876.     print("Please place one ",name," in slot ",specialSlots[specialSlot])
  877.     sleep(1)
  878.   end
  879.   print(name," in slot ",specialSlots[specialSlot], " checks out")
  880. end
  881. function checkSpecialSlot(specialSlot, name)
  882.  if restoreFoundSwitch and turtle.getItemCount(specialSlots[specialSlot]) == 0 then --If the turtle was stopped while dropping off items.
  883.     select(specialSlots[specialSlot])
  884.     turtle.dig()
  885.     select(1)
  886.   end
  887.   promptSpecialSlot(specialSlot, name)
  888.   allowedItems[specialSlots[specialSlot]] = 1
  889.   sleep(2)
  890. end
  891. if enderChestEnabled then
  892.   checkSpecialSlot("enderChest","Ender Chest")
  893. end
  894. if fuelChestEnabled then
  895.   checkSpecialSlot("fuelChest","Fuel Chest")
  896. end
  897. --Fuel Chest Obtaining
  898.  
  899. --Setting which slots are marked as compare slots
  900. if oldOreQuarry then
  901.   if not restoreFoundSwitch then --We don't want to reset compare blocks every restart
  902.     local counter = 0
  903.     for i=1, inventoryMax do if turtle.getItemCount(i) > 0 and i ~= specialSlots.enderChest then counter = counter+1 end end --If the slot has items, but isn't enderChest slot if it is enabled
  904.  
  905.     screen(1,1)
  906.     print("You have selected an Ore Quarry!")
  907.     if counter == 0 or hasRefueled then --If there are no compare slots, or the turtle has refueled, and probably has fuel in inventory
  908.       print("Please place your compare blocks in the first slots\n")
  909.  
  910.       print("Press Enter when done")
  911.       repeat until ({os.pullEvent("key")})[2] == 28 --Should wait for enter key to be pressed
  912.     else
  913.       print("Registering slots as compare slots")
  914.       sleep(1)
  915.     end
  916.     for i=1, inventoryMax do
  917.       if turtle.getItemCount(i) > 0 then
  918.         if i ~= specialSlots.enderChest then
  919.           table.insert(compareSlots, i) --Compare slots are ones compared to while mining. Conditions are because we Don't want to compare to enderChest
  920.           allowedItems[i] = 1 --Blacklist is for dropping off items. The number is maximum items allowed in slot when dropping off
  921.           dumpSlots[i] = true --We also want to ignore all excess of these items, like dirt
  922.         end
  923.       end
  924.     end
  925.     if extraDropItems then
  926.       screen(1,1)
  927.       print("Put in extra drop items now\n")
  928.       print("Press Enter when done")
  929.       repeat until ({os.pullEvent("key")})[2] == 28 --Should wait for enter key to be pressed
  930.       for i=1,inventoryMax do
  931.         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.
  932.           dumpSlots[i] = true
  933.           allowedItems[i] = 1
  934.         end
  935.       end
  936.     end
  937.     --This is could go very wrong if this isn't here
  938.     if #compareSlots >= inventoryMax-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
  939.   end
  940.   local counter = 0
  941.   for a, b in pairs(compareSlots) do if  turtle.getItemCount(b) > 0 then counter = counter + 1 end end
  942.   if counter == 0 then
  943.     screen(1,1)
  944.     print("You have an ore quarry without any compare slots. Continue? y/n")
  945.     if ({os.pullEvent("char")})[2] ~= "y" then error("",0) end
  946.   end
  947. elseif not oreQuarry then --This was screwing up dumpCompareItems
  948.   dumpCompareItems = false --If not an ore quarry, this should definitely be false
  949.   if specialSlots.enderChest == 1 then
  950.     dumpSlots[2] = true
  951.   else
  952.     dumpSlots[1] = true
  953.   end
  954. end
  955.  
  956. --Rednet Handshake
  957. function newMessageID()
  958.   return math.random(1,2000000000)
  959. end
  960. function sendMessage(send, receive, message)
  961.   if legacyRednet then
  962.     if type(message) == "table" then message = textutils.serialize(message) end
  963.     return modem.transmit(send, receive, message)
  964.   end
  965.   return modem.transmit(send , receive, {fingerprint = channels.fingerprint, id = newMessageID(), message = message})
  966. end
  967. if rednetEnabled then
  968.   screen(1,1)
  969.   print("Rednet is Enabled")
  970.   print("The Channel to open is "..channels.send)
  971.   if peripheral.find then
  972.     modem = peripheral.find("modem")
  973.   else
  974.     modem = peripheral.wrap("right")
  975.   end
  976.   modem.open(channels.receive)
  977.   local i = 0
  978.     repeat
  979.       local id = os.startTimer(3)
  980.       i=i+1
  981.       print("Sending Initial Message "..i)
  982.       sendMessage(channels.send, channels.receive, channels.message)
  983.       local message = {} --Have to initialize as table to prevent index nil
  984.       repeat
  985.         local event, idCheck, channel,_,locMessage, distance = os.pullEvent()
  986.         if locMessage then message = locMessage end
  987.         if legacyRednet then --For that one guy that uses 1.4.7
  988.           message = {message = message}
  989.         end
  990.       until (event == "timer" and idCheck == id) or (event == "modem_message" and channel == channels.receive and type(message) == "table")
  991.     until message.message == channels.confirm
  992.   connected = true
  993.   print("Connection Confirmed!")
  994.   sleep(1.5)
  995. end
  996. function biometrics(isAtBedrock, requestQuad)
  997.   if not rednetEnabled then return end --This function won't work if rednet not enabled :P
  998.   local toSend = { label = os.getComputerLabel() or "No Label", id = os.getComputerID(),
  999.     percent = percent, zPos = relzPos, xPos = relxPos, yPos = yPos,
  1000.     layersDone = layersDone, x = x, z = z, layers = layers,
  1001.     openSlots = getNumOpenSlots(), mined = mined, moved = moved,
  1002.     chestFull = chestFull, isAtChest = (xPos == 0 and yPos == 1 and zPos == 1),
  1003.     isGoingToNextLayer = (gotoDest == "layerStart"), foundBedrock = foundBedrock,
  1004.     fuel = checkFuel(), volume = volume, status = statusString,
  1005.     }
  1006.   if requestQuad and isInPath then --If we are requesting a quadRotor to send help
  1007.     if not gps.locate(gpsTimeout) then
  1008.       print("\nOH NOES! Trying to reach quadrotor, but can't get GPS position!")
  1009.       sleep(1)
  1010.     else
  1011.       toSend.firstPos = gpsStartPos
  1012.       toSend.secondPos = gpsSecondPos
  1013.       toSend.emergencyLocation = {gps.locate(gpsTimeout)}
  1014.     end
  1015.   end
  1016.   sendMessage(channels.send, channels.receive, toSend)
  1017.   id = os.startTimer(0.1)
  1018.   local event, received
  1019.   repeat
  1020.     local locEvent, idCheck, confirm, _, locMessage, distance = os.pullEvent()
  1021.     event, received = locEvent, locMessage or {message = ""}
  1022.     if legacyRednet and type(received) == "string" then
  1023.       received = {message = received}
  1024.     end
  1025.   until (event == "timer" and idCheck == id) or (event == "modem_message" and confirm == channels.receive and type(received) == "table")
  1026.   if event == "modem_message" then connected = true else connected = false end
  1027.   local message = received.message:lower()
  1028.   if message == "stop" or message == "quit" or message == "kill" then error("Rednet said to stop...",0) end
  1029.   if message == "return" then
  1030.     endingProcedure()
  1031.     error('Rednet said go back to start...',0)
  1032.   end
  1033.   if message == "drop" then
  1034.     dropOff()
  1035.   end
  1036.   if message == "pause" then
  1037.     print("\nTurtle is paused. Send 'resume' or press any character to resume")
  1038.     statusString = "Paused"
  1039.     repeat
  1040.       sleep(1) --The turtle sends out periodic messages, which will clear the receiver's queue and send a message (if it exists)
  1041.       sendMessage(channels.send, channels.receive, toSend) --This may be a bit overkill, sending the whole message again, but whatever.
  1042.       local event, idCheck, confirm, _, message, distance = os.pullEvent()
  1043.     until (event == "modem_message" and confirm == channels.receive and (message.message == "resume" or message.message == "unpause" or message.message == "pause")) or (event == "char")
  1044.     statusString = nil
  1045.   end
  1046.   if message == "refuel" then
  1047.     print("\nEngaging in emergency refueling")
  1048.     emergencyRefuel()
  1049.   end
  1050.  
  1051. end
  1052. --Showing changes to settings
  1053. screen(1,1)
  1054. print("Your selected settings:")
  1055. if #changedT == 0 then
  1056. print("Completely Default")
  1057. else
  1058. for i=1, #changedT do
  1059. print(changedT[i][1],": ",changedT[i][2]) --Name and Value
  1060. end
  1061. end
  1062. print("\nStarting in 3"); sleep(1); print("2"); sleep(1); print("1"); sleep(1.5) --Dramatic pause at end
  1063.  
  1064.  
  1065.  
  1066. ----------------------------------------------------------------
  1067. --Define ALL THE FUNCTIONS
  1068. --Event System Functions
  1069. function eventAddAt(pos, ...)
  1070.   return table.insert(events,pos, {...}) or true
  1071. end
  1072. function eventAdd(...) --Just a wrapper
  1073.   return eventAddAt(1, ...)
  1074. end
  1075. function eventGet(pos)
  1076.   return events[tonumber(pos) or #events]
  1077. end
  1078. function eventPop(pos)
  1079.   return table.remove(events,tonumber(pos) or #events) or false --This will return value popped, tonumber returns nil if fail, so default to end
  1080. end
  1081. function eventRun(value, ...)
  1082.   local argsList = {...}
  1083.   if type(value) == "string" then
  1084.     if value:sub(-1) ~= ")" then --So supports both "up()" and "up"
  1085.       value = value .. "("
  1086.       for a, b in pairs(argsList) do --Appending arguments
  1087.         local toAppend
  1088.         if type(b) == "table" then toAppend = textutils.serialize(b)
  1089.         elseif type(b) == "string" then toAppend = "\""..tostring(b).."\"" --They weren't getting strings around them
  1090.         else toAppend = tostring(b) end
  1091.         value = value .. (toAppend or "true") .. ", "
  1092.       end
  1093.       if value:sub(-1) ~= "(" then --If no args, do not want to cut off
  1094.         value = value:sub(1,-3)..""
  1095.       end
  1096.       value = value .. ")"
  1097.     end
  1098.     --print(value) --Debug
  1099.     local func = loadstring(value)
  1100.     setfenv(func, getfenv(1))
  1101.     return func()
  1102.   end
  1103. end
  1104. function eventClear(pos)
  1105.   if pos then events[pos] = nil else events = {} end
  1106. end
  1107. function runAllEvents()
  1108.   while #events > 0 do
  1109.     local toRun = eventGet()
  1110.     --print(toRun[1]) --Debug
  1111.     eventRun(unpack(toRun))
  1112.     eventPop()
  1113.   end
  1114. end
  1115.  
  1116. --Display Related Functions
  1117. function display() --This is just the last screen that displays at the end
  1118.   screen(1,1)
  1119.   print("Total Blocks Mined: "..mined)
  1120.   print("Current Fuel Level: "..checkFuel())
  1121.   print("Cobble: "..totals.cobble)
  1122.   print("Usable Fuel: "..totals.fuel)
  1123.   print("Other: "..totals.other)
  1124.   if rednetEnabled then
  1125.     print("")
  1126.     print("Sent Stop Message")
  1127.     if legacyRednet then --This was the traditional stopping signal
  1128.       print("Sent Legacy Stop")
  1129.       sendMessage(channels.send, channels.receive, "stop")
  1130.     end
  1131.     local finalTable = {mined = mined, cobble = totals.cobble, fuelblocks = totals.fuel,
  1132.         other = totals.other, fuel = checkFuel(), isDone = true }
  1133.     sendMessage(channels.send,channels.receive, finalTable)
  1134.     modem.close(channels.receive)
  1135.   end
  1136.   if doBackup then
  1137.     fs.delete(saveFile)
  1138.     if autoResume then --Getting rid of the original startup files and replacing
  1139.       fs.delete(startupName)
  1140.       if fs.exists(startupRename) then
  1141.         fs.move(startupRename, startupName)
  1142.       end
  1143.     end
  1144.   end
  1145. end
  1146. function updateDisplay() --Runs in Mine(), display information to the screen in a certain place
  1147. screen(1,1)
  1148. print("Blocks Mined")
  1149. print(mined)
  1150. print("Percent Complete")
  1151. print(percent.."%")
  1152. print("Fuel")
  1153. print(checkFuel())
  1154.   -- screen(1,1)
  1155.   -- print("Xpos: ")
  1156.   -- print(xPos)
  1157.   -- print("RelXPos: ")
  1158.   -- print(relxPos)
  1159.   -- print("Z Pos: ")
  1160.   -- print(zPos)
  1161.   -- print("Y pos: ")
  1162.   -- print(yPos)
  1163. if rednetEnabled then
  1164. screenLine(1,7)
  1165. print("Connected: "..tostring(connected))
  1166. end
  1167. end
  1168. --Utility functions
  1169. local function pad(str, length, side)
  1170.   toRet = ""
  1171.   if side == "right" then
  1172.     toRet = str
  1173.   end
  1174.   for i=1, length do
  1175.     toRet = toRet.." "
  1176.   end
  1177.   if side == "left" then
  1178.     toRet = toRet..str
  1179.   end
  1180.   return toRet
  1181. end
  1182. function logMiningRun(textExtension, extras) --Logging mining runs
  1183.   if not logging then return end
  1184.   local number, name = 0
  1185.   if not fs.isDir(logFolder) then
  1186.     fs.delete(logFolder)
  1187.     fs.makeDir(logFolder)
  1188.   end
  1189.   repeat
  1190.     number = number + 1 --Number will be at least 2
  1191.     name = logFolder.."/Quarry_Log_"..tostring(number)..(textExtension or "")
  1192.   until not fs.exists(name)
  1193.   local handle = fs.open(name,"w")
  1194.   local function write(...)
  1195.     for a, b in ipairs({...}) do
  1196.       handle.write(tostring(b))
  1197.     end
  1198.     handle.write("\n")
  1199.   end
  1200.   local function boolToText(bool) if bool then return "Yes" else return "No" end end
  1201.   write("Welcome to the Quarry Logs!")
  1202.   write("Entry Number: ",number)
  1203.   write("Quarry Version: ",VERSION)
  1204.   write("Dimensions (X Z Y): ",x," ",z," ", y)
  1205.   write("Blocks Mined: ", mined)
  1206.   write("  Cobble: ", totals.cobble)
  1207.   write("  Usable Fuel: ", totals.fuel)
  1208.   write("  Other: ",totals.other)
  1209.   write("Total Fuel Used: ",  (originalFuel or (neededFuel + checkFuel()))- checkFuel()) --Protect against errors with some precision
  1210.   write("Expected Fuel Use: ", neededFuel)
  1211.   write("Days to complete mining run: ",os.day()-originalDay)
  1212.   write("Day Started: ", originalDay)
  1213.   write("Number of times resumed: ", numResumed)
  1214.   write("Was an ore quarry? ",boolToText(oreQuarry or oldOreQuarry))
  1215.   write("Was inverted? ",boolToText(invert))
  1216.   write("Was using rednet? ",boolToText(rednetEnabled))
  1217.   write("Chest was on the ",dropSide," side")
  1218.   if startDown > 0 then write("Started ",startDown," blocks down") end
  1219.   if exactTotals then
  1220.     write("\n==DETAILED TOTALS==")
  1221.     for a,b in pairs(exactTotals) do
  1222.       write(pad(a, 15, "right"),":",pad(tostring(b),({term.getSize()})[1]-15-1, "left"))
  1223.     end
  1224.   end
  1225.   handle.close()
  1226. end
  1227. --Inventory related functions
  1228. function isFull(slots) --Checks if there are more than "slots" used inventory slots.
  1229.   slots = slots or inventoryMax
  1230.   local numUsed = 0
  1231.   sleep(0)
  1232.   for i=1, inventoryMax do
  1233.     if turtle.getItemCount(i) > 0 then numUsed = numUsed + 1 end
  1234.   end
  1235.   if numUsed > slots then
  1236.     return true
  1237.   end
  1238.   return false
  1239. end
  1240. function countUsedSlots() --Returns number of slots with items in them, as well as a table of item counts
  1241.   local toRet, toRetTab = 0, {}
  1242.   for i=1, inventoryMax do
  1243.     local a = turtle.getItemCount(i)
  1244.     if a > 0 then toRet = toRet + 1 end
  1245.     table.insert(toRetTab, a)
  1246.   end
  1247.   return toRet, toRetTab
  1248. end
  1249. function getSlotsTable() --Just get the table from above
  1250.   local _, toRet = countUsedSlots()
  1251.   return toRet
  1252. end
  1253. function getChangedSlots(tab1, tab2) --Returns a table of changed slots. Format is {slotNumber, numberChanged}
  1254.   local toRet = {}
  1255.   for i=1, math.min(#tab1, #tab2) do
  1256.     diff = math.abs(tab2[i]-tab1[i])
  1257.     if diff > 0 then
  1258.       table.insert(toRet, {i, diff})
  1259.     end
  1260.   end
  1261.   return toRet
  1262. end
  1263. function getFirstChanged(tab1, tab2) --Just a wrapper. Probably not needed
  1264.   local a = getChangedSlots(tab1,tab2)
  1265.   return (a[1] or {"none"})[1]
  1266. end
  1267.  
  1268. function getRep(which, list) --Gets a representative slot of a type. Expectation is a sequential table of types
  1269.   for a,b in pairs(list) do
  1270.     if b == which then return a end
  1271.   end
  1272.   return false
  1273. end
  1274. function assignTypes(types, count) --The parameters allow a preexisting table to be used, like a table from the original compareSlots...
  1275.   types, count = types or {1}, count or 1 --Table of types and current highest type
  1276.   for i=1, inventoryMax do
  1277.     if turtle.getItemCount(i) > 0 and not specialSlots[i] then --Not special slots so we don't count ender chests
  1278.       select(i)
  1279.       for k=1, count do
  1280.         if turtle.compareTo(getRep(k, types)) then types[i] = k end
  1281.       end
  1282.       if not types[i] then
  1283.         count = count + 1
  1284.         types[i] = count
  1285.       end
  1286.       if oreQuarry then
  1287.         if blacklist[turtle.getItemDetail().name] then
  1288.           dumpSlots[i] = true
  1289.         else
  1290.           dumpSlots[i] = false
  1291.         end
  1292.       end
  1293.     end
  1294.   end
  1295.   select(1)
  1296.   return types, count
  1297. end
  1298. function getTableOfType(which, list) --Returns a table of all the slots of which type
  1299.   local toRet = {}
  1300.   for a, b in pairs(list) do
  1301.     if b == which then
  1302.       table.insert(toRet, a)
  1303.     end
  1304.   end
  1305.   return toRet
  1306. end
  1307.  
  1308. --This is so the turtle will properly get types, otherwise getRep of a type might not be a dumpSlot, even though it should be.
  1309. if not restoreFoundSwitch then --We only want this to happen once
  1310.   if oldOreQuarry then --If its not ore quarry, this screws up type assigning
  1311.     initialTypes, initialCount = assignTypes()
  1312.   else
  1313.     initialTypes, initialCount = {1}, 1
  1314.   end
  1315. end
  1316.  
  1317. function count(add) --Done any time inventory dropped and at end, true=add, false=nothing, nil=subtract
  1318.   local mod = -1
  1319.   if add then mod = 1 end
  1320.   if add == false then mod = 0 end
  1321.   slot = {}        --1: Filler 2: Fuel 3:Other --[1] is type, [2] is number
  1322.   for i=1, inventoryMax do
  1323.     slot[i] = {}
  1324.     slot[i][2] = turtle.getItemCount(i)
  1325.   end
  1326.  
  1327.   local function iterate(toSet , rawTypes, set)
  1328.     for _, a in pairs(getTableOfType(toSet, rawTypes)) do --Get all slots matching type
  1329.       slot[a][1] = set --Set official type to "set"
  1330.     end
  1331.   end
  1332.  
  1333.   --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
  1334.   local rawTypes, numTypes = assignTypes(copyTable(initialTypes), initialCount) --This gets increasingly numbered types, copyTable because assignTypes will modify it
  1335.  
  1336.   for i=1, numTypes do
  1337.     if (select(getRep(i, rawTypes)) or true) and turtle.refuel(0) then --Selects the rep slot, checks if it is fuel
  1338.       iterate(i, rawTypes, 2) --This type is fuel
  1339.     elseif dumpSlots[getRep(i,(oreQuarry and rawTypes) or initialTypes)] then --If the rep of this slot is a dump item. This is initial types so that the rep is in dump slots. rawTypes if oreQuarry to get newly assigned dumps
  1340.       iterate(i, rawTypes, 1) --This type is cobble/filler
  1341.     else
  1342.       iterate(i, rawTypes, 3) --This type is other
  1343.     end
  1344.   end
  1345.  
  1346.   for i=1,inventoryMax do
  1347.     if not specialSlots[i] then --Do nothing!
  1348.       if exactTotals then
  1349.         local data = turtle.getItemDetail()
  1350.         exactTotals[data.name] = (exactTotals[data.name] or 0) + data.count
  1351.       end
  1352.       if slot[i][1] == 1 then totals.cobble = totals.cobble + (slot[i][2] * mod)
  1353.       elseif slot[i][1] == 2 then totals.fuel = totals.fuel + (slot[i][2] * mod)
  1354.       elseif slot[i][1] == 3 then totals.other = totals.other + (slot[i][2] * mod) end
  1355.     end
  1356.   end
  1357.  
  1358.   select(1)
  1359. end
  1360.  
  1361. --Mining functions
  1362. function dig(doAdd, mineFunc, inspectFunc) --Note, turtle will not bother comparing if not given an inspectFunc
  1363.   if doAdd == nil then doAdd = true end
  1364.   mineFunc = mineFunc or turtle.dig
  1365.   local function retTab(tab) if type(tab) == "table" then return tab end end --Please ignore the stupid one-line trickery. I felt special writing that. (Unless it breaks, then its cool)
  1366.     --Mine if not in blacklist. inspectFunc returns success and (table or string) so retTab filters out the string and the extra table prevents errors.
  1367.   if not oreQuarry or not inspectFunc or not blacklist[(retTab(({inspectFunc()})[2]) or {name = "none"}).name] then --Will stop at first false, last part won't run if one of first are false
  1368.    if mineFunc() then
  1369.      if doAdd then
  1370.        mined = mined + 1
  1371.      end
  1372.      return true
  1373.    else
  1374.      return false
  1375.    end
  1376.   end
  1377.   return true --This only runs if oreQuarry but item not in blacklist
  1378. end
  1379.  
  1380. --Refuel Functions
  1381. function emergencyRefuel()
  1382.   local continueEvac = true --This turns false if more fuel is acquired
  1383.   if fuelChestEnabled then --This is pretty much the only place that this will be used
  1384.     if not fuelChestPhase then --Since I want to do things with return of enderRefuel, I will just make a special system. All of this is for backup safety.
  1385.       fuelChestPhase = 0 --Global variable will be saved
  1386.       fuelChestProperFacing = facing
  1387.     end
  1388.     if fuelChestPhase == 0 then
  1389.       turnTo(coterminal(fuelChestProperFacing+2))
  1390.       dig(false)
  1391.       fuelChestPhase = 1
  1392.       saveProgress()
  1393.     end
  1394.     if fuelChestPhase == 1 then
  1395.       select(specialSlots.fuelChest)
  1396.       turtle.place()
  1397.       fuelChestPhase = 2
  1398.       saveProgress()
  1399.     end
  1400.     if fuelChestPhase == 2 then
  1401.       if not enderRefuel() then --Returns false if slots are full
  1402.         select(specialSlots.fuelChest)
  1403.         turtle.drop() --Somehow stuff got in here...
  1404.       end
  1405.       fuelChestPhase = 3
  1406.       saveProgress()
  1407.     end
  1408.     if fuelChestPhase == 3 then
  1409.       select(specialSlots.fuelChest)
  1410.       dig(false)
  1411.       select(1)
  1412.       fuelChestPhase = 4
  1413.       saveProgress()
  1414.     end
  1415.     if fuelChestPhase == 4 then
  1416.       turnTo(fuelChestProperFacing)
  1417.       fuelChestProperFacing = nil --Getting rid of saved values
  1418.       fuelChestPhase = nil
  1419.       continueEvac = false
  1420.     end
  1421.   elseif quadEnabled then --Ask for a quadRotor
  1422.     screen()
  1423.     print("Attempting an emergency Quad Rotor refuel")
  1424.     print("The turtle will soon send a message, then wait ",quadTimeout," seconds before moving on")
  1425.     print("Press any key to break timer")
  1426.     biometrics(nil, true)
  1427.     local timer, counter, counterID, event, id  = os.startTimer(quadTimeout), 0, os.startTimer(1)
  1428.     local startInventory = getSlotsTable()
  1429.     repeat
  1430.       if id == counterID then counter = counter + 1; counterID = os.startTimer(1) end
  1431.       screenLine(1,6)
  1432.       print("Seconds elapsed: ",counter)
  1433.       event, id = os.pullEvent() --Waits for a key or fuel or the timer
  1434.     until (event == "timer" and id == timer) or event == "key" or event == "turtle_inventory" --Key event just makes turtle go back to start
  1435.     if event == "turtle_inventory" then --If fuel was actually delivered
  1436.       local slot = getFirstChanged(startInventory, getSlotsTable())
  1437.       select(slot)
  1438.       local initialFuel = checkFuel()
  1439.       midRunRefuel(slot)
  1440.       if checkFuel() > initialFuel then
  1441.         print("Fuel delivered! Evac aborted")
  1442.         continueEvac = false
  1443.       else
  1444.         print("What did you send the turtle? Not fuel >:(")
  1445.         print("Continuing evac")
  1446.       end
  1447.       sleep(1)
  1448.     end
  1449.   elseif doRefuel then --Attempt an emergency refueling
  1450.     screen()
  1451.     print("Attempting an emergency refuel")
  1452.     print("Fuel Level:    ",checkFuel())
  1453.     print("Distance Back: ",(xPos+zPos+yPos+1))
  1454.     print("Categorizing Items")
  1455.     count(false) --Do not add count, but categorize
  1456.     local fuelSwitch, initialFuel = false, checkFuel() --Fuel switch so we don't go over limit (in emergency...)
  1457.     print("Going through available fuel slots")
  1458.     for i=1, inventoryMax do
  1459.       if fuelSwitch then break end
  1460.       if turtle.getItemCount(i) > 0 and slot[i][1] == 2 then --If there are items and type 2 (fuel)
  1461.         select(i)
  1462.         fuelSwitch = midRunRefuel(i) --See above "function drop" for usage
  1463.       end
  1464.     end
  1465.     select(1) --Cleanup
  1466.     print("Done fueling")
  1467.     if checkFuel() > initialFuel then
  1468.       continueEvac = false
  1469.       print("Evac Aborted")
  1470.     else
  1471.       print("Evac is a go, returning to base")
  1472.       sleep(1.5) --Pause for reading
  1473.     end
  1474.   end
  1475.   return continueEvac
  1476. end
  1477.  
  1478.  
  1479. function digUp(doAdd, ignoreInspect)--Regular functions :) I switch definitions for optimization (I think)
  1480.   return dig(doAdd, turtle.digUp, (not ignoreInspect and turtle.inspectUp) or nil)
  1481. end
  1482. function digDown(doAdd, ignoreInspect)
  1483.   return dig(doAdd, turtle.digDown, (not ignoreInspect and turtle.inspectDown) or nil)
  1484. end
  1485. if inverted then --If inverted, switch the options
  1486.   digUp, digDown = digDown, digUp
  1487. end
  1488.  
  1489. function smartDig(doDigUp, doDigDown) --This function is used only in mine when oldOreQuarry
  1490.   if inverted then doDigUp, doDigDown = doDigDown, doDigUp end --Switching for invert
  1491.   local blockAbove, blockBelow = doDigUp and turtle.detectUp(), doDigDown and turtle.detectDown() --These control whether or not the turtle digs
  1492.   local index = 1
  1493.   for i=1, #compareSlots do
  1494.     if not (blockAbove or blockBelow) then break end --We don't want to go selecting if there is nothing to dig
  1495.     index = i --To access out of scope
  1496.     select(compareSlots[i])
  1497.     if blockAbove and turtle.compareUp() then blockAbove = false end
  1498.     if blockBelow and turtle.compareDown() then blockBelow = false end
  1499.   end
  1500.   table.insert(compareSlots, 1, table.remove(compareSlots, index)) --This is so the last selected slot is the first slot checked, saving a select call
  1501.   if blockAbove then dig(true, turtle.digUp) end
  1502.   if blockBelow then dig(true, turtle.digDown) end
  1503. end
  1504.  
  1505. function relxCalc()
  1506.   if layersDone % 2 == 1 then
  1507.     relzPos = zPos
  1508.   else
  1509.     relzPos = (z-zPos) + 1
  1510.   end
  1511.   if relzPos % 2 == 1 then
  1512.     relxPos = xPos
  1513.   else
  1514.     relxPos = (x-xPos)+1
  1515.   end
  1516.   if layersDone % 2 == 0 and z % 2 == 1 then
  1517.     relxPos = (x-relxPos)+1
  1518.   end
  1519. end
  1520. function horizontalMove(movement, posAdd, doAdd)
  1521.   if doAdd == nil then doAdd = true end
  1522.   if movement() then
  1523.     if doAdd then
  1524.       moved = moved + 1
  1525.     end
  1526.     if facing == 0 then
  1527.       xPos = xPos + 1
  1528.     elseif facing == 1 then
  1529.       zPos = zPos + 1
  1530.     elseif facing == 2 then
  1531.       xPos = xPos - 1
  1532.     elseif facing == 3 then
  1533.       zPos = zPos - 1
  1534.     else
  1535.       error("Function forward, facing should be 0 - 3, got "..tostring(facing),2)
  1536.     end
  1537.     relxCalc()
  1538.     return true
  1539.   end
  1540.   return false
  1541. end
  1542. function forward(doAdd)
  1543.   return horizontalMove(turtle.forward, 1, doAdd)
  1544. end
  1545. function back(doAdd)
  1546.   return horizontalMove(turtle.back, -1, doAdd)
  1547. end
  1548. function verticalMove(moveFunc, yDiff, digFunc, attackFunc)
  1549.   local count = 0
  1550.   while not moveFunc() do
  1551.     if not digFunc(true, true) then --True True is doAdd, and ignoreInspect
  1552.       attackFunc()
  1553.       sleep(0.5)
  1554.       count = count + 1
  1555.       if count > maxTries and yPos > (startY-7) then bedrock() end
  1556.     end
  1557.   end
  1558.   yPos = yDiff + yPos
  1559.   saveProgress()
  1560.   biometrics()
  1561.   return true
  1562. end
  1563. function up() --Uses other function if inverted
  1564.   verticalMove(inverted and turtle.down or turtle.up, -1, digUp, attackUp) --Other functions deal with invert already
  1565. end
  1566. function down()
  1567.   verticalMove(inverted and turtle.up or turtle.down, 1, digDown, attackDown)
  1568. end
  1569.  
  1570.  
  1571. function right(num)
  1572.   num = num or 1
  1573.   for i=1, num do
  1574.     facing = coterminal(facing+1)
  1575.     saveProgress()
  1576.     if not goLeftNotRight then turtle.turnRight() --Normally
  1577.     else turtle.turnLeft() end --Left Quarry
  1578.   end
  1579. end
  1580. function left(num)
  1581.   num = num or 1
  1582.   for i=1, num do
  1583.   facing = coterminal(facing-1)
  1584.   saveProgress()
  1585.   if not goLeftNotRight then turtle.turnLeft() --Normally
  1586.   else turtle.turnRight() end --Left Quarry
  1587. end
  1588. end
  1589.  
  1590. function attack(doAdd, func)
  1591.   doAdd = doAdd or true
  1592.   func = func or turtle.attack
  1593.   if func() then
  1594.     if doAdd then
  1595.       attacked = attacked + 1
  1596.     end
  1597.     return true
  1598.   end
  1599.   return false
  1600. end
  1601. function attackUp(doAdd)
  1602.   if inverted then
  1603.     return attack(doAdd, turtle.attackDown)
  1604.   else
  1605.     return attack(doAdd, turtle.attackUp)
  1606.   end
  1607. end
  1608. function attackDown(doAdd)
  1609.   if inverted then
  1610.     return attack(doAdd, turtle.attackUp)
  1611.   else
  1612.     return attack(doAdd, turtle.attackDown)
  1613.   end
  1614. end
  1615.  
  1616. function detect(func)
  1617.   func = func or turtle.detect
  1618.   return func()
  1619. end
  1620. function detectUp(ignoreInvert)
  1621.   if inverted and not ignoreInvert then return detect(turtle.detectDown)
  1622.   else return detect(turtle.detectUp) end
  1623. end
  1624. function detectDown(ignoreInvert)
  1625.   if inverted and not ignoreInvert then return detect(turtle.detectUp)
  1626.   else return detect(turtle.detectDown) end
  1627. end
  1628.  
  1629.  
  1630.  
  1631. function mine(doDigDown, doDigUp, outOfPath,doCheckInv) -- Basic Move Forward
  1632.   if doCheckInv == nil then doCheckInv = true end
  1633.   if doDigDown == nil then doDigDown = true end
  1634.   if doDigUp == nil then doDigUp = true end
  1635.   if outOfPath == nil then outOfPath = false end
  1636.   isInPath = (not outOfPath) --For rednet
  1637.   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...
  1638.     local continueEvac = false --It will be set true unless at start
  1639.     if xPos ~= 0 then
  1640.       continueEvac = emergencyRefuel() --This is a huge list of things to do in an emergency
  1641.     end
  1642.     if continueEvac then
  1643.       eventClear() --Clear any annoying events for evac
  1644.       local currPos = yPos
  1645.       endingProcedure() --End the program
  1646.       print("Turtle ran low on fuel so was brought back to start for you :)\n\nTo resume where you left off, use '-startDown "..tostring(currPos-1).."' when you start")
  1647.       error("",0)
  1648.     end
  1649.   end
  1650.   local count = 0
  1651.   if not outOfPath then dig() end  --This speeds up the quarry by a decent amount if there are more mineable blocks than air
  1652.   while not forward(not outOfPath) do
  1653.     sleep(0) --Calls coroutine.yield to prevent errors
  1654.     count = count + 1
  1655.     if not dig() then
  1656.       attack()
  1657.     end
  1658.     if count > 10 then
  1659.       attack()
  1660.       sleep(0.2)
  1661.     end
  1662.     if count > maxTries then
  1663.       if checkFuel() == 0 then --Don't worry about inf fuel because I modified this function
  1664.         saveProgress({doCheckFuel = true, doRefuel = true})
  1665.         os.reboot()
  1666.       elseif yPos > (startY-7) and turtle.detect() then --If it is near bedrock
  1667.         bedrock()
  1668.       else --Otherwise just sleep for a bit to avoid sheeps
  1669.         sleep(1)
  1670.       end
  1671.     end
  1672.   end
  1673.   checkSanity() --Not kidding... This is necessary
  1674.   saveProgress(tab)
  1675.  
  1676.   if not oldOreQuarry then
  1677.     if doDigUp then--The digging up and down part
  1678.       sleep(0) --Calls coroutine.yield
  1679.       if not digUp(true) and detectUp() then --This is relative: will dig down first on invert
  1680.         if not attackUp() then
  1681.           if yPos > (startY-7) then bedrock() end --Checking for bedrock, but respecting user wishes
  1682.         end
  1683.       end
  1684.     end
  1685.     if doDigDown then
  1686.      digDown(true) --This needs to be absolute as well
  1687.     end
  1688.   else --If oldQuarry
  1689.     smartDig(doDigUp,doDigDown)
  1690.   end
  1691.   percent = math.ceil(moved/moveVolume*100)
  1692.   updateDisplay()
  1693.   if doCheckInv and careAboutResources then
  1694.     if isFull(inventoryMax-keepOpen) then
  1695.       if not ((oreQuarry or oldOreQuarry) and dumpCompareItems) then
  1696.         dropOff()
  1697.       else
  1698.         local currInv = getSlotsTable()
  1699.         drop(nil, false, true) --This also takes care of counting.
  1700.         if #getChangedSlots(currInv, getSlotsTable()) <= 2 then --This is so if the inventory is full of useful stuff, it still has to drop it
  1701.           dropOff()
  1702.         end
  1703.       end
  1704.     end
  1705.   end
  1706.   biometrics()
  1707. end
  1708. --Insanity Checking
  1709. function checkSanity()
  1710.   if not isInPath then --I don't really care if its not in the path.
  1711.     return true
  1712.   end
  1713.   if not (facing == 0 or facing == 2) and #events == 0 then --If mining and not facing proper direction and not in a turn
  1714.     turnTo(0)
  1715.     rowCheck = true
  1716.   end
  1717.   if xPos < 0 or xPos > x or zPos < 0 or zPos > z or yPos < 0 then
  1718.     saveProgress()
  1719.     print("I have gone outside boundaries, attempting to fix (maybe)")
  1720.     if xPos > x then goto(x, zPos, yPos, 2) end --I could do this with some fancy math, but this is much easier
  1721.     if xPos < 0 then goto(1, zPos, yPos, 0) end
  1722.     if zPos > z then goto(xPos, z, yPos, 3) end
  1723.     if zPos < 0 then goto(xPos, 1, yPos, 1) end
  1724.     relxCalc() --Get relxPos properly
  1725.     eventClear()
  1726.  
  1727.     --[[
  1728.     print("Oops. Detected that quarry was outside of predefined boundaries.")
  1729.     print("Please go to my forum thread and report this with a short description of what happened")
  1730.     print("If you could also run \"pastebin put Civil_Quarry_Restore\" and give me that code it would be great")
  1731.     error("",0)]]
  1732.   end
  1733. end
  1734.  
  1735. local function fromBoolean(input) --Like a calculator
  1736. if input then return 1 end
  1737. return 0
  1738. end
  1739. local function multBoolean(first,second) --Boolean multiplication
  1740. return (fromBoolean(first) * fromBoolean(second)) == 1
  1741. end
  1742. function coterminal(num, limit) --I knew this would come in handy :D
  1743. limit = limit or 4 --This is for facing
  1744. return math.abs((limit*fromBoolean(num < 0))-(math.abs(num)%limit))
  1745. end
  1746. if tArgs["-manualpos"] then
  1747.   facing = coterminal(facing) --Done to improve support for "-manualPos"
  1748.   if facing == 0 then rowCheck = true elseif facing == 2 then rowCheck = false end --Ditto
  1749.   relxCalc() --Ditto
  1750. end
  1751.  
  1752. --Direction: Front = 0, Right = 1, Back = 2, Left = 3
  1753. function turnTo(num)
  1754.   num = num or facing
  1755.   num = coterminal(num) --Prevent errors
  1756.   local turnRight = true
  1757.   if facing-num == 1 or facing-num == -3 then turnRight = false end --0 - 1 = -3, 1 - 0 = 1, 2 - 1 = 1
  1758.   while facing ~= num do          --The above is used to smartly turn
  1759.     if turnRight then
  1760.       right()
  1761.     else
  1762.       left()
  1763.     end
  1764.   end
  1765. end
  1766. function goto(x,z,y, toFace, destination, updateStatus)
  1767.   --Will first go to desired z pos, then x pos, y pos varies
  1768.   x = x or 1; y = y or 1; z = z or 1; toFace = toFace or facing
  1769.   gotoDest = destination or "" --This is used by biometrics.
  1770.   statusString = "Going somewhere"
  1771.   --Possible destinations: layerStart, quarryStart
  1772.   if yPos > y then --Will go up first if below position
  1773.     while yPos~=y do up() end
  1774.   end
  1775.   if zPos > z then
  1776.     turnTo(3)
  1777.   elseif zPos < z then
  1778.     turnTo(1)
  1779.   end
  1780.   while zPos ~= z do mine(false,false,true,false) end
  1781.   if xPos > x then
  1782.     turnTo(2)
  1783.   elseif xPos < x then
  1784.     turnTo(0)
  1785.   end
  1786.   while xPos ~= x do mine(false,false,true,false) end
  1787.   if yPos < y then --Will go down after if above position
  1788.     while yPos~=y do down() end
  1789.   end
  1790.   turnTo(toFace)
  1791.   saveProgress()
  1792.   gotoDest = ""
  1793.   statusString = nil
  1794. end
  1795. function getNumOpenSlots()
  1796.   local toRet = 0
  1797.   for i=1, inventoryMax do
  1798.     if turtle.getItemCount(i) == 0 then
  1799.       toRet = toRet + 1
  1800.     end
  1801.   end
  1802.   return toRet
  1803. end
  1804.  
  1805. --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.
  1806. local function waitDrop(slot, allowed, whereDrop) --This will just drop, but wait if it can't
  1807.   allowed = allowed or 0
  1808.   while turtle.getItemCount(slot) > allowed do --No more half items stuck in slot!
  1809.     local tries = 1
  1810.     while not whereDrop(turtle.getItemCount(slot)-allowed) do --Drop off only the amount needed
  1811.       screen(1,1)
  1812.       print("Chest Full, Try "..tries)
  1813.       chestFull = true
  1814.       biometrics()--To send that the chest is full
  1815.       tries = tries + 1
  1816.       sleep(2)
  1817.     end
  1818.     chestFull = false
  1819.   end
  1820. end
  1821.  
  1822. function midRunRefuel(i, allowed)
  1823.   allowed = allowed or allowedItems[i]
  1824.   local numToRefuel = turtle.getItemCount(i)-allowed
  1825.   if checkFuel() >= checkFuelLimit() then return true end --If it doesn't need fuel, then signal to not take more
  1826.   local firstCheck = checkFuel()
  1827.   if numToRefuel > 0 then turtle.refuel(1) else return false end --This is so we can see how many fuel we need.
  1828.   local singleFuel
  1829.   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
  1830.   --Refuel      The lesser of   max allowable or         remaining fuel space         /    either inf or a single fuel (which can be 0)
  1831.   turtle.refuel(math.min(numToRefuel-1, math.ceil((checkFuelLimit()-checkFuel()) / singleFuel))) --The refueling part of the the doRefuel option
  1832.   if checkFuel() >= checkFuelLimit() then return true end --Do not need any more fuel
  1833.   return false --Turtle can still be fueled
  1834. end
  1835.  
  1836. function enderRefuel() --Assumes a) An enderchest is in front of it b) It needs fuel
  1837.   local slot
  1838.   for a,b in ipairs(getSlotsTable()) do
  1839.     if b == 0 then slot = a; break end
  1840.   end
  1841.   if not slot then return false end --No room for fueling
  1842.   select(slot)
  1843.   repeat
  1844.     print("Required Fuel: ",excessFuelAmount)
  1845.     print("Current Fuel: ",checkFuel())
  1846.     local tries = 0
  1847.     while not turtle.suck() do
  1848.       sleep(1)
  1849.       statusString = "No Fuel in Ender Chest"
  1850.       biometrics() --Let user know that fuel chest is empty
  1851.       print(statusString,". Try: ",tries)
  1852.       tries = tries + 1
  1853.     end
  1854.     statusString = nil
  1855.   until midRunRefuel(slot, 0) --Returns true when should not refuel any more
  1856.   if not turtle.drop() then turtle.dropDown() end --If cannot put fuel back, just drop it, full fuel chest = user has too much fuel already
  1857.   return true -- :D
  1858. end
  1859.  
  1860.  
  1861. function drop(side, final, compareDump)
  1862.   side = sides[side] or "front"
  1863.   local dropFunc, detectFunc, dropFacing = turtle.drop, turtle.detect, facing+2
  1864.   if side == "top" then dropFunc, detectFunc = turtle.dropUp, turtle.detectUp end
  1865.   if side == "bottom" then dropFunc, detectFunc = turtle.dropDown, turtle.detectDown end
  1866.   if side == "right" then turnTo(1); dropFacing = 0 end
  1867.   if side == "left" then turnTo(3); dropFacing = 0 end
  1868.   local properFacing = facing --Capture the proper direction to be facing
  1869.  
  1870.   count(true) --Count number of items before drop. True means add. This is before chest detect, because could be final
  1871.  
  1872.   while not compareDump and not detectFunc() do
  1873.     if final then return end --If final, we don't need a chest to be placed, but there can be
  1874.     chestFull = true
  1875.     biometrics() --Let the user know there is a problem with chest
  1876.     screen(1,1) --Clear screen
  1877.     print("Waiting for chest placement on ",side," side (when facing quarry)")
  1878.     sleep(2)
  1879.   end
  1880.   chestFull = false
  1881.  
  1882.   local fuelSwitch = false --If doRefuel, this can switch so it won't overfuel
  1883.   for i=1,inventoryMax do
  1884.     --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
  1885.     if turtle.getItemCount(i) > 0 then --Saves time, stops bugs
  1886.       if slot[i][1] == 1 and dumpCompareItems then turnTo(dropFacing) --Turn around to drop junk, not store it. dumpComapareItems is global config
  1887.       else turnTo(properFacing) --Turn back to proper position... or do nothing if already there
  1888.       end
  1889.       select(i)
  1890.       if doRefuel and slot[i][1] == 2 then --Intelligently refuels to fuel limit
  1891.         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
  1892.           fuelSwitch = midRunRefuel(i)
  1893.         end
  1894.       elseif not compareDump or (compareDump and slot[i][1] == 1) then --This stops all wanted items from being dropped off in a compareDump
  1895.         waitDrop(i, allowedItems[i], dropFunc)
  1896.       end
  1897.     end
  1898.   end
  1899.  
  1900.   if oldOreQuarry or compareDump then count(nil) end--Subtract the items still there if oreQuarry
  1901.   resetDumpSlots() --So that slots gone aren't counted as dump slots next
  1902.  
  1903.   select(1) --For fanciness sake
  1904.  
  1905. end
  1906.  
  1907. function dropOff() --Not local because called in mine()
  1908.   local currX,currZ,currY,currFacing = xPos, zPos, yPos, facing
  1909.   if careAboutResources then
  1910.     if not enderChestEnabled then --Regularly
  1911.       eventAdd("goto", 1,1,currY,2) --Need this step for "-startDown"
  1912.       eventAdd("goto(0,1,1,2)")
  1913.       eventAdd("drop", dropSide,false)
  1914.       eventAdd("turnTo(0)")
  1915.       eventAdd("mine",false,false,true,false)
  1916.       eventAdd("goto(1,1,1, 0)")
  1917.       eventAdd("goto", 1, 1, currY, 0)
  1918.       eventAdd("goto", currX,currZ,currY,currFacing)
  1919.     else --If using an enderChest
  1920.       if turtle.getItemCount(specialSlots.enderChest) ~= 1 then eventAdd("promptSpecialSlot('enderChest','Ender Chest')") end
  1921.       eventAdd("turnTo",currFacing-2)
  1922.       eventAdd("dig",false)
  1923.       eventAdd("select",specialSlots.enderChest)
  1924.       eventAdd("turtle.place")
  1925.       eventAdd("drop","front",false)
  1926.       eventAdd("turnTo",currFacing-2)
  1927.       eventAdd("select", specialSlots.enderChest)
  1928.       eventAdd("dig",false)
  1929.       eventAdd("turnTo",currFacing)
  1930.       eventAdd("select(1)")
  1931.     end
  1932.     runAllEvents()
  1933.     numDropOffs = numDropOffs + 1 --Analytics tracking
  1934.   end
  1935.   return true
  1936. end
  1937. function endingProcedure() --Used both at the end and in "biometrics"
  1938.   eventAdd("goto",1,1,yPos,2,"quarryStart") --Allows for startDown variable
  1939.   eventAdd("goto",0,1,1,2, "quarryStart") --Go back to base
  1940.   runAllEvents()
  1941.   --Output to a chest or sit there
  1942.   if enderChestEnabled then
  1943.     if dropSide == "right" then eventAdd("turnTo(1)") end --Turn to proper drop side
  1944.     if dropSide == "left" then eventAdd("turnTo(3)") end
  1945.     eventAdd("dig(false)") --This gets rid of a block in front of the turtle.
  1946.     eventAdd("select",specialSlots.enderChest)
  1947.     eventAdd("turtle.place")
  1948.     eventAdd("select(1)")
  1949.   end
  1950.   eventAdd("drop",dropSide, true)
  1951.   eventAdd("turnTo(0)")
  1952.  
  1953.   --Display was moved above to be used in bedrock function
  1954.   eventAdd("display")
  1955.   --Log current mining run
  1956.   eventAdd("logMiningRun",logExtension)
  1957.   toQuit = true --I'll use this flag to clean up (legacy)
  1958.   runAllEvents()
  1959. end
  1960. function bedrock()
  1961.   foundBedrock = true --Let everyone know
  1962.   if rednetEnabled then biometrics() end
  1963.   if checkFuel() == 0 then error("No Fuel",0) end
  1964.   local origin = {x = xPos, y = yPos, z = zPos}
  1965.   print("Bedrock Detected")
  1966.   if turtle.detectUp() and not turtle.digUp() then
  1967.     print("Block Above")
  1968.     turnTo(facing+2)
  1969.     repeat
  1970.       if not forward(false) then --Tries to go back out the way it came
  1971.         if not attck() then --Just making sure not mob-blocked
  1972.           if not dig() then --Now we know its bedrock
  1973.             turnTo(facing+1) --Try going a different direction
  1974.           end
  1975.         end
  1976.       end
  1977.     until not turtle.detectUp() or turtle.digUp() --These should be absolute and we don't care about about counting resources here.
  1978.   end
  1979.   up() --Go up two to avoid any bedrock.
  1980.   up()
  1981.   eventClear() --Get rid of any excess events that may be run. Don't want that.
  1982.   endingProcedure()
  1983.   print("\nFound bedrock at these coordinates: ")
  1984.   print(origin.x," Was position in row\n",origin.z," Was row in layer\n",origin.y," Blocks down from start")
  1985.   error("",0)
  1986. end
  1987.  
  1988. function endOfRowTurn(startZ, wasFacing, mineFunctionTable)
  1989. local halfFacing = ((layersDone % 2 == 1) and 1) or 3
  1990. local toFace = coterminal(wasFacing + 2) --Opposite side
  1991. if zPos == startZ then
  1992.   if facing ~= halfFacing then turnTo(halfFacing) end
  1993.   mine(unpack(mineFunctionTable or {}))
  1994. end
  1995. if facing ~= toFace then
  1996.   turnTo(toFace)
  1997. end
  1998. end
  1999.  
  2000.  
  2001. -------------------------------------------------------------------------------------
  2002. --Pre-Mining Stuff dealing with session persistence
  2003. runAllEvents()
  2004. if toQuit then error("",0) end --This means that it was stopped coming for its last drop
  2005.  
  2006. local doDigDown, doDigUp = (lastHeight ~= 1), (lastHeight == 0) --Used in lastHeight
  2007. if not restoreFoundSwitch then --Regularly
  2008.   --Check if it is a mining turtle
  2009.   if not isMiningTurtle then
  2010.     local a, b = turtle.dig()
  2011.     if a then
  2012.       mined = mined + 1
  2013.       isMiningTurtle = true
  2014.     elseif b == "Nothing to dig with" or b == "No tool to dig with" then
  2015.       print("This is not a mining turtle. To make a mining turtle, craft me together with a diamond pickaxe")
  2016.       error("",0)
  2017.     end
  2018.   end
  2019.   mine(false,false,true) --Get into quarry by going forward one
  2020.   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.
  2021.     gpsSecondPos = {gps.locate(gpsTimeout)} --Note: Does not run this if it has already been restarted.
  2022.   end
  2023.   for i = 1, startDown do
  2024.     eventAdd("down") --Add a bunch of down events to get to where it needs to be.
  2025.   end
  2026.   runAllEvents()
  2027.   if flatBedrock then
  2028.     while (detectDown() and digDown(false, true)) or not detectDown() do --None of these functions are non-invert protected because inverse always false here
  2029.       down()
  2030.       startDown = startDown + 1
  2031.     end
  2032.     startDown = startDown - y + 1
  2033.     for i=1, y-2 do
  2034.       up() --It has hit bedrock, now go back up for proper 3 wide mining
  2035.     end
  2036.   elseif not(y == 1 or y == 2) then
  2037.     down() --Go down to align properly. If y is one or two, it doesn't need to do this.
  2038.   end
  2039. else --restore found
  2040.   if not(layersDone == layers and not doDigDown) then digDown() end
  2041.   if not(layersDone == layers and not doDigUp) then digUp() end  --Get blocks missed before stopped
  2042. end
  2043. --Mining Loops--------------------------------------------------------------------------
  2044. select(1)
  2045. while layersDone <= layers do -------------Height---------
  2046. local lastLayer = layersDone == layers --If this is the last layer
  2047. local secondToLastLayer = (layersDone + 1) == layers --This is a check for going down at the end of a layer.
  2048. moved = moved + 1 --To account for the first position in row as "moved"
  2049. if not(layersDone == layers and not doDigDown) then digDown() end --This is because it doesn't mine first block in layer
  2050. if not restoreFoundSwitch and layersDone % 2 == 1 then rowCheck = true end
  2051. relxCalc()
  2052. while relzPos <= z do -------------Width----------
  2053. while relxPos < x do ------------Length---------
  2054. 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
  2055. end ---------------Length End-------
  2056. if relzPos ~= z then --If not on last row of section
  2057.   local func
  2058.   if rowCheck == true then --Switching to next row
  2059.   func = "right"; rowCheck = false; else func = false; rowCheck = true end --Which way to turn
  2060.     eventAdd("endOfRowTurn", zPos, facing , {not lastLayer or (doDigDown and lastLayer), not lastLayer or (doDigUp and lastLayer)}) --The table is passed to the mine function
  2061.     runAllEvents()
  2062. else break
  2063. end
  2064. end ---------------Width End--------
  2065. if layersDone % 2 == 0 then --Will only go back to start on non-even layers
  2066.   eventAdd("goto",1,1,yPos,0, "layerStart") --Goto start of layer
  2067. else
  2068.   eventAdd("turnTo",coterminal(facing-2))
  2069. end
  2070. if not lastLayer then --If there is another layer
  2071.   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
  2072. end
  2073. eventAdd("relxCalc")
  2074. layersDone = layersDone + 1
  2075. restoreFoundSwitch = false --This is done so that rowCheck works properly upon restore
  2076. runAllEvents()
  2077. end ---------------Height End-------
  2078.  
  2079. endingProcedure() --This takes care of getting to start, dropping in chest, and displaying ending screen
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top