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