Advertisement
civilwargeeky

MCM 2

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