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