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