Infobesity

--Civilwargeeky's Quarry Program-- 3.7.0(a)

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