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