Advertisement
civilwargeeky

Quarry 3.3.2

Dec 9th, 2013
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 46.86 KB | None | 0 0
  1. --[[
  2. Version 3.3.2
  3. Recent Changes:
  4.   Fixed GPS. Now works
  5.   Fixed bug stopping rednet from working.
  6.   New Internals: Made event system and consolidated mining loops
  7.   New Restarting Logic!
  8.     As long as position is recorded properly, it should always restart properly
  9.   New Argument logic!
  10.     Arguments are much more modular for me to make, and they are no longer case-sensitive. Improved items changed interface
  11.   New Arguments!
  12.     -enderChest [t/f or slot number] You can now use an enderchest! Try it out. There are directions
  13.     -startDown [number] The quarry will now start this many blocks down from its starting point (or up if invert). Great for getting diamonds and things from surface. Chest should still be placed on the top (or use ender chest)
  14.     -manualPos [xPos] [zPos] [yPos] [facing] Turtle ever gotten it's position wrong, but you don't want to reset? Use this! Check "quarry -help" for changes
  15.   Renamed several arguments
  16.   Arguments are no longer ignored if you use "-default", you just won't be prompted for anything.
  17.   Boolean (t/f) arguments now accept "yes" as well as "true"
  18.   Fixed small issue in GPS location that may have broken it.
  19. ]]
  20. --Defining things
  21. civilTable = nil; _G.civilTable = {}; setmetatable(civilTable, {__index = _G}); setfenv(1,civilTable)
  22. -------Defaults for Arguments----------
  23. --Arguments assignable by text
  24. x,y,z = 3,3,3 --These are just in case tonumber fails
  25. inverted = false --False goes from top down, true goes from bottom up [Default false]
  26. rednetEnabled = false --Default rednet on or off  [Default false]
  27. --Arguments assignable by tArgs
  28. dropSide = "front" --Side it will eject to when full or done [Default "front"]
  29. careAboutResources = true --Will not stop mining once inventory full if false [Default true]
  30. doCheckFuel = true --Perform fuel check [Default true]
  31. doRefuel = false --Whenever it comes to start location will attempt to refuel from inventory [Default false]
  32. invCheckFreq = 10 --Will check for inventory full every <-- moved spaces [Default 10]
  33. keepOpen = 1 --How many inventory slots it will attempt to keep open at all times [Default 1]
  34. fuelSafety = "moderate" --How much fuel it will ask for: safe, moderate, and loose [Default moderate]
  35. saveFile = "Civil_Quarry_Restore" --Where it saves restore data [Default "Civil_Quarry_Restore"]
  36. doBackup = true --If it will keep backups for session persistence [Default true]
  37. numberOfStacksPerRun = 8 --How many stacks (number of items) the turtle expects (on average) to have before it must dump off. Not in arguments. [Default 8]
  38. gpsEnabled = false -- If option is enabled, will attempt to find position via GPS api [Default false]
  39. gpsTimeout = 3 --The number of seconds the program will wait to get GPS coords. Not in arguments [Default 3]
  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. startDown = 0 --How many blocks to start down from the top of the mine [Default 0]
  44. enderChestEnabled = false --Whether or not to use an ender chest [Default false]
  45. enderChestSlot = 16 --What slot to put the ender chest in [Default 16]
  46. --Standard number slots for fuel (you shouldn't care)
  47. fuelTable = { --Will add in this amount of fuel to requirement.
  48. safe = 1000,
  49. moderate = 200,
  50. loose = 0 } --Default 1000, 200, 0
  51. --Standard rednet channels
  52. channels = {
  53. send = os.getComputerID()  ,
  54. receive = os.getComputerID() + 100 ,
  55. confirm = "Confirm"
  56. }
  57.  
  58. local help_paragraph = [[
  59. Welcome!: Welcome to quarry help. Below are help entries for all parameters. Examples and tips are at the bottom.
  60. -Default: This will force no prompts. If you use this and nothing else, only defaults will be used.
  61. -dim: [length] [width] [height] This sets the dimensions for the quarry
  62. -invert: [t/f] If true, quarry will be inverted (go up instead of down)
  63. -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
  64. -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
  65. -doRefuel: [t/f] If true, the turtle will refuel itself with coal and planks it finds on its mining run
  66. -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...
  67. -chest: [side] This specifies what side the chest at the end will be on. You can say "top", "bottom", "front", "left", or "right"
  68. -enderChest: This one is special. If you use "-enderChest true" then it will use an enderChest in the default slot. However, you can also do "-enderChest [slot]" then it will take the ender chest from whatever slot you tell it to. Like 7... or 14... or whatever.
  69. -GPS: [t/f] If true 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
  70. -sendChannel: [number] This is what channel your turtle will send rednet messages on
  71. -receiveChannel: [number] This is what channel your turtle will receive rednet messages on
  72. -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
  73. -logging: [t/f] If true, will record information about its mining run in a folder at the end of the mining run
  74. -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...)
  75. -saveFile: [word] This is what the backup file will be called
  76. -logFolder: [word] The folder that quarry logs will be stored in
  77. -logExtension: [word] The extension given to each quarry log (e.g. ".txt" or ".notepad" or whatever)
  78. -invCheckFreq: [number] This is how often the turtle will check if it has the proper amount of slots open
  79. -keepOpen: [number] This is the number of the slots the turtle will make sure are open. It will check every invCheckFreq blocks
  80. -careAboutResources: [t/f] Who cares about the materials! If set to false, it will just keep mining when its inventory is full
  81. -startDown: [number] If you set this, the turtle will go down this many blocks from the start before starting its quarry
  82.   =
  83.   C _ |
  84.       |
  85.       |
  86.       |
  87.       |_ _ _ _ >
  88. -manualPos: [xPos] [zPos] [yPos] [facing] This is for advanced use. If the server reset when the turtle was in the middle of a 100x100x100 quarry, fear not, you can now manually set the position of the turtle. yPos is always positive. The turtle's starting position is 0, 1, 1, 0. Facing is measured 0 - 3. 0 is forward, and it progresses clockwise. Example- "-manualPos 65 30 30 2"
  89. -help: Thats what this is :D
  90. Examples: Everything below is examples and tips for use
  91. Important Note:
  92.  None of the above parameters are necessary. They all have default values, and the above are just if you want to change them.
  93. Examples [1]:
  94.  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".
  95.   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?
  96. Examples [2]:
  97.   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.
  98. Examples [2] [cont.]:
  99.   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
  100.   "quarry -dim 40x20x9 -invert false -startDown 45 -rednet true -enderChest 12 -restore"
  101.   BAM. Now you can just let that turtle do it's thing
  102. Tips:
  103.  The order of the parameters doesn't matter. "quarry -invert false -rednet true" is the same as "quarry -rednet true -invert false"
  104.  
  105.   Capitalization doesn't matter. "quarry -iNVErt FALSe" does the same thing as "quarry -invert false"
  106. Tips [cont.]:
  107.  For [t/f] parameters, you can also use "yes" and "no" so "quarry -invert yes"
  108.  
  109.  For [t/f] parameters, it only cares about the first letter. So you can use "quarry -invert t" or "quarry -invert y"
  110. Tips [cont.]:
  111.  If you are playing with fuel turned off, the program will automatically change settings for you so you don't have to :D
  112.  
  113.   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.
  114. Internal Config:
  115.   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.
  116.  
  117.   You can also use this if there are settings that you don't like the default value of.
  118. ]]
  119.  
  120. --Parsing help for display
  121. --[[The way the help table works:
  122. All help indexes are numbered. There is a help[i].title that contains the title,
  123. and the other lines are in help[i][1] - help[i][#help[i] ]
  124. Different lines (e.g. other than first) start with a space.
  125. As of now, the words are not wrapped, fix that later]]
  126. local help = {}
  127. local i = 0
  128. local titlePattern = ".-%:" --Find the beginning of the line, then characters, then a ":"
  129. local textPattern = "%:.+" --Find a ":", then characters until the end of the line
  130. for a in help_paragraph:gmatch("\n?.-\n") do --Matches in between newlines
  131. local current = string.sub(a,1,-2).."" --Concatenate Trick
  132. if string.sub(current,1,1) ~= " " then
  133. i = i + 1
  134. help[i] = {}
  135. help[i].title = string.sub(string.match(current, titlePattern),1,-2)..""
  136. help[i][1] = string.sub(string.match(current,textPattern) or " ",3,-1)
  137. elseif string.sub(current,1,1) == " " then
  138. table.insert(help[i], string.sub(current,2, -1).."")
  139. end
  140. end
  141.  
  142.  
  143. local supportsRednet = (peripheral.wrap("right") ~= nil)
  144.  
  145. local tArgs = {...}
  146. --You don't care about these
  147.       xPos,yPos,zPos,facing,percent,mined,moved,relxPos, rowCheck, connected, isInPath, layersDone, attacked, startY
  148.     = 0,   1,   1,   0,     0,      0,    0,    1,       "right",  false,     true,     1,          0,        0
  149.  
  150. local totals = {cobble = 0, fuel = 0, other = 0} -- Total for display (cannot go inside function)
  151. local function count() --Done any time inventory dropped and at end
  152. slot = {}        --1: Cobble 2: Fuel 3:Other
  153. for i=1, 16 do   --[1] is type, [2] is number
  154. slot[i] = {}
  155. slot[i][2] = turtle.getItemCount(i)
  156. end
  157. slot[1][1] = 1   -- = Assumes Cobble/Main
  158. for i=1, 16 do   --Cobble Check
  159. turtle.select(i)
  160. if turtle.compareTo(1)  then
  161. slot[i][1] = 1
  162. totals.cobble = totals.cobble + slot[i][2]
  163. elseif turtle.refuel(0) then
  164. slot[i][1] = 2
  165. totals.fuel = totals.fuel + slot[i][2]
  166. else
  167. slot[i][1] = 3
  168. totals.other = totals.other + slot[i][2]
  169. end
  170. end
  171. turtle.select(1)
  172. end
  173.  
  174. local getFuel = turtle.getFuelLevel --This is for cleanup at the end
  175. do --Common variable name...
  176. local flag = turtle.getFuelLevel() == "unlimited"--Unlimited screws up my calculations
  177. if flag then --Fuel is disabled
  178.   turtle.getFuelLevel = function() return math.huge end --Infinite Fuel
  179. end --There is no "else" because it will already return the regular getFuel
  180. end
  181. local checkFuel = turtle.getFuelLevel --Just an alias for backwards compat
  182.  
  183.  -----------------------------------------------------------------
  184. --Input Phase
  185. local function screen(xPos,yPos)
  186. xPos, yPos = xPos or 1, yPos or 1
  187. term.setCursorPos(xPos,yPos); term.clear(); end
  188. local function screenLine(xPos,yPos)
  189. term.setCursorPos(xPos,yPos); term.clearLine(); end
  190.  
  191. screen(1,1)
  192. print("----- Welcome to Quarry! -----")
  193. print("")
  194.  
  195. local sides = {top = "top", right = "right", left = "left", bottom = "bottom", front = "front"} --Used to whitelist sides
  196. local changedT, tArgsWithUpper = {}, {}
  197. changedT.new = function(key, value) table.insert(changedT,{key, value}) end --Numeric list of lists
  198. local function capitalize(text) return (string.upper(string.sub(text,1,1))..string.sub(text,2,-1)) end
  199. for i=1, #tArgs do tArgsWithUpper[i] = tArgs[i]; tArgsWithUpper[tArgsWithUpper[i]] = i; tArgs[i] = tArgs[i]:lower(); tArgs[tArgs[i]] = i end --My signature key-value pair system, now with upper
  200.  
  201. local restoreFound, restoreFoundSwitch = false --Initializing so they are in scope
  202. function addParam(name, displayText, formatString, forcePrompt, trigger, variableOverride) --To anyone that doesn't understand this very well, probably not your best idea to go in here.
  203.   if trigger == nil then trigger = true end --Defaults to being able to run
  204.   if not trigger then return end --This is what the trigger is for. Will not run if trigger not there
  205.   if restoreFoundSwitch or tArgs["-default"] then forcePrompt = false end --Don't want to prompt if these
  206.   local toGetText = name:lower() --Because all params are now lowered
  207.   local formatType = formatString:match("^%a+"):lower() or error("Format String Unknown: "..formatString) --Type of format string
  208.   local args = formatString:sub(({formatString:find(formatType)})[2] + 2).."" --Everything in formatString but the type and space
  209.   local variable = variableOverride or name --Goes first to the override for name
  210.   local func = loadstring("return "..variable)
  211.   setfenv(func,getfenv(1))
  212.   local originalValue = assert(func)() --This is the default value, for checking to add to changed table
  213.   if originalValue == nil then error("From addParam, \""..variable.."\" returned nil",2) end --I may have gotten a wrong variable name
  214.   local givenValue, toRet --Initializing for use
  215.   if tArgs["-"..toGetText] then
  216.     givenValue = tArgsWithUpper[tArgs["-"..toGetText]+1] --This is the value after the desired parameter
  217.   elseif forcePrompt then
  218.     write(displayText.."? ")
  219.     givenValue = io.read()
  220.   end
  221.   if formatType == "force" then --This is the one exception. Should return true if givenValue is nothing
  222.     toRet = (tArgs["-"..toGetText] and true) or false --Will return true if param exists, otherwise false
  223.   end
  224.   if not (givenValue or toRet) then return end --Don't do anything if you aren't given anything. Leave it as default, except for "force"
  225.   if formatType == "boolean" then --All the format strings will be basically be put through a switch statement
  226.     toRet = givenValue:sub(1,1):lower() == "y" or givenValue:sub(1,1):lower() == "t" --Accepts true or yes
  227.     if formatString == "boolean special" then
  228.       toRet = givenValue:sub(1,1):lower() ~= "n" and givenValue:sub(1,1):lower() ~= "f" --Accepts anything but false or no
  229.     end
  230.   elseif formatType == "string" then
  231.     toRet = givenValue:match("^[%w%.]+") --Basically anything not a space or control character etc
  232.   elseif formatType == "number" then
  233.     toRet = tonumber(givenValue) --Note this is a local, not the above so we don't change anything
  234.     if not toRet then return end --We need a number... Otherwise compare errors
  235.     toRet = math.abs(math.floor(toRet)) --Get proper integers
  236.     local startNum, endNum = formatString:match("(%d+)%-(%d+)") --Gets range of numbers
  237.     startNum, endNum = tonumber(startNum), tonumber(endNum)
  238.     if not ((toRet >= startNum) and (toRet <= endNum)) then return end --Can't use these
  239.   elseif formatType == "side" then
  240.     local exclusionTab = {} --Ignore the wizardry here. Just getting arguments without format string
  241.     for a in args:gmatch("%S+") do exclusionTab[a] = true end --This makes a list of the sides to not include
  242.     if not exclusionTab[givenValue] then toRet = sides[givenValue] end --If side is not excluded
  243.   elseif formatType == "list" then
  244.     toRet = {}
  245.     for a in args:gmatch("[^,]") do
  246.       table.insert(toRet,a)
  247.     end
  248.   elseif formatType == "force" then --Do nothing, everything is already done
  249.   else error("Improper formatType",2)
  250.   end
  251.   if toRet == nil then return end --Don't want to set variables to nil... That's bad
  252.   tempParam = toRet --This is what loadstring will see :D
  253.   local func = loadstring(variable.." = tempParam")
  254.   setfenv(func, getfenv(1))
  255.   func()
  256.   tempParam = nil --Cleanup of global
  257.   if toRet ~= originalValue then
  258.     changedT.new(displayText, tostring(toRet))
  259.   end
  260.   return toRet
  261. end
  262.  
  263. --Check if it is a turtle
  264. if not(turtle or tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"]) then
  265.   print("This is not a turtle, you might be looking for the \"Companion Rednet Program\" \nCheck My forum thread for that")
  266.   print("Press 'q' to quit, or any other key to start help ")
  267.   if ({os.pullEvent("char")})[2] ~= "q" then tArgs.help = true else error("",0) end
  268. end
  269.  
  270.  
  271. if tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"] then
  272. print("You have selected help, press any key to continue"); print("Use arrow keys to naviate, q to quit"); os.pullEvent("key")
  273. local pos = 1
  274. local key = 0
  275. while pos <= #help and key ~= keys.q do
  276. if pos < 1 then pos = 1 end
  277. screen(1,1)
  278. print(help[pos].title)
  279. for a=1, #help[pos] do print(help[pos][a]) end
  280. repeat
  281. _, key = os.pullEvent("key")
  282. until key == 200 or key == 208 or key == keys.q
  283. if key == 200 then pos = pos - 1 end
  284. if key == 208 then pos = pos + 1 end
  285. end
  286. error("",0)
  287. end
  288.  
  289. --Saving
  290. addParam("doBackup", "Backup Save File", "boolean")
  291. addParam("saveFile", "Save File Name", "string")
  292.  
  293. restoreFound = fs.exists(saveFile)
  294. restoreFoundSwitch = (tArgs["-restore"] or tArgs["-resume"]) and restoreFound
  295. if restoreFoundSwitch then
  296.   local file = fs.open(saveFile,"r")
  297.   local test = file.readAll() ~= ""
  298.   file.close()
  299.   if test then
  300.     os.run(getfenv(1),saveFile)
  301.     if gpsEnabled then --If it had saved gps coordinates
  302.       print("Found GPS Start Coordinates")
  303.       local currLoc = {gps.locate(gpsTimeout)} or {}
  304.       if #currLoc > 0 and #gpsStartPos > 0 and #gpsSecondPos > 0 then --Cover all the different positions I'm using
  305.         print("GPS Position Successfully Read")
  306.         if currLoc[1] == gpsStartPos[1] and currLoc[3] == gpsStartPos[3] then --X coord, y coord, z coord in that order
  307.           xPos, yPos, zPos = 0,1,1
  308.           if facing ~= 0 then turnTo(0) end
  309.           print("Is at start")
  310.         else
  311.           if inverted then --yPos setting
  312.           ------------------------------------------------FIX THIS
  313.           end
  314.           local function copyTable(tab) local toRet = {}; for a, b in pairs(tab) do toRet[a] = b end; return toRet end
  315.           local a, b = copyTable(gpsStartPos), copyTable(gpsSecondPos) --For convenience
  316.           if b[3] - a[3] == -1 then--If went north (-Z)
  317.             a[1] = a[1] - 1 --Shift x one to west to create a "zero"
  318.             xPos, zPos = -currLoc[3] + a[3], currLoc[1] + -a[1]
  319.           elseif b[1] - a[1] == 1 then--If went east (+X)
  320.             a[3] = a[3] - 1 --Shift z up one to north to create a "zero"
  321.             xPos, zPos = currLoc[1] + -a[1], currLoc[3] + -a[3]
  322.           elseif b[3] - a[3] == 1 then--If went south (+Z)
  323.             a[1] = a[1] + 1 --Shift x one to east to create a "zero"
  324.             xPos, zPos = currLoc[3] + a[3], -currLoc[1] + a[3]
  325.           elseif b[1] - a[1] == -1 then--If went west (-X)
  326.             a[3] = a[3] + 1 --Shift z down one to south to create a "zero"
  327.             xPos, zPos = -currLoc[1] + a[1], -currLoc[3] + a[3]
  328.           else
  329.             print("Improper Coordinates")
  330.             print("GPS Locate Failed, Using Standard Methods")        ----Maybe clean this up a bit to use flags instead.
  331.           end  
  332.         end
  333.         print("X Pos: ",xPos)
  334.         print("Y Pos: ",yPos)
  335.         print("Z Pos: ",zPos)
  336.         print("Facing: ",facing)
  337.       else
  338.         print("GPS Locate Failed, Using Standard Methods")
  339.       end    
  340.     print("Restore File read successfully. Starting in 3"); sleep(3)
  341.     end
  342.   else
  343.     fs.delete(saveFile)
  344.     print("Restore file was empty, sorry, aborting")
  345.     error("",0)
  346.   end
  347. else --If turtle is just starting
  348.   events = {} --This is the event queue :D
  349.   originalFuel = checkFuel() --For use in logging. To see how much fuel is REALLY used
  350. end
  351.  
  352. --Dimesnions
  353. if tArgs["-dim"] then local num = tArgs["-dim"];
  354. x = tonumber(tArgs[num + 1]) or x; z = tonumber(tArgs[num + 2]) or z; y = tonumber(tArgs[num + 3]) or y
  355. elseif not (tArgs["-default"] or restoreFoundSwitch) then
  356. print("What dimensions?")
  357. print("")
  358. --This will protect from negatives, letters, and decimals
  359. term.write("Length? ")
  360. x = math.floor(math.abs(tonumber(io.read()) or x))
  361. term.write("Width? ")
  362. z = math.floor(math.abs(tonumber(io.read()) or z))
  363. term.write("Height? ")
  364. y = math.floor(math.abs(tonumber(io.read()) or y))
  365. changedT.new("Length",x); changedT.new("Width",z); changedT.new("Height",y)
  366. end
  367. --Invert
  368. addParam("invert", "Inverted","boolean", true, nil, "inverted")
  369. addParam("startDown","Start Down","number 1-256")
  370. --Inventory
  371. addParam("chest", "Chest Drop Side", "side front", nil, nil, "dropSide")
  372. addParam("enderChest","Ender Chest Enabled","boolean special", nil, nil, "enderChestEnabled") --This will accept anything (including numbers) thats not "f" or "n"
  373. addParam("enderChest", "Ender Chest Slot", "number 1-16", nil, nil, "enderChestSlot") --This will get the number slot if given
  374. --Rednet
  375. addParam("rednet", "Rednet Enabled","boolean",true, supportsRednet, "rednetEnabled")
  376. addParam("gps", "GPS Location Services", "force", nil, (not restoreFoundSwitch) and supportsRednet, "gpsEnabled" ) --Has these triggers so that does not record position if restarted.
  377. if gpsEnabled and not restoreFoundSwitch then
  378.   gpsStartPos = {gps.locate(gpsTimeout)} --Stores position in array
  379.   gpsEnabled = #gpsStartPos > 0 --Checks if location received properly. If not, position is not saved
  380. end
  381. addParam("sendChannel", "Rednet Send Channel", "number 1-65535", false, supportsRednet, "channels.send")
  382. addParam("receiveChannel","Rednet Receive Channel", "number 1-65535", false, supportsRednet, "channels.receive")
  383. --Fuel
  384. addParam("doRefuel", "Refuel from Inventory","boolean", nil, turtle.getFuelLevel() ~= math.huge) --math.huge due to my changes
  385. addParam("doCheckFuel", "Check Fuel", "boolean", nil, turtle.getFuelLevel() ~= math.huge)
  386. --Logging
  387. addParam("logging", "Logging", "boolean")
  388. addParam("logFolder", "Log Folder", "string")
  389. addParam("logExtension","Log Extension", "string")
  390. --Misc
  391. addParam("startY", "Start Y","number 1-256")
  392. addParam("invCheckFreq","Inventory Check Frequency","number 1-342")
  393. addParam("keepOpen", "Slots to Keep Open", "number 1-15")
  394. addParam("careAboutResources", "Care About Resources","boolean")
  395. --Manual Position
  396. if tArgs["-manualpos"] then --Gives current coordinates in xPos,zPos,yPos, facing
  397.   local a = tArgs["-manualpos"]
  398.   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
  399.   changedT.new("xPos",xPos); changedT.new("zPos",zPos); changedT.new("yPos",yPos); changedT.new("facing",facing)
  400.   restoreFoundSwitch = true --So it doesn't do beginning of quarry behavior
  401. end
  402.  
  403. local function saveProgress(extras) --Session persistence
  404. exclusions = { modem = true, }
  405. if doBackup then
  406. local toWrite = ""
  407. for a,b in pairs(getfenv(1)) do
  408.   if not exclusions[a] then
  409.       --print(a ,"   ", b, "   ", type(b)) --Debug
  410.     if type(b) == "string" then b = "\""..b.."\"" end
  411.     if type(b) == "table" then b = textutils.serialize(b) end
  412.     if type(b) ~= "function" then
  413.       toWrite = toWrite..a.." = "..tostring(b).."\n"
  414.     end
  415.   end
  416. end
  417. toWrite = toWrite.."doCheckFuel = false\n" --It has already used fuel, so calculation unnesesary
  418. local file
  419. repeat
  420.   file = fs.open(saveFile,"w")
  421. until file --WHY DOES IT SAY ATTEMPT TO INDEX NIL!!!
  422. file.write(toWrite)
  423. if type(extras) == "table" then
  424.   for a, b in pairs(extras) do
  425.     file.write(a.." = "..tostring(b))
  426.   end
  427. end
  428. file.close()
  429. end
  430. end
  431.  
  432. local area = x*z
  433. local volume = x*y*z
  434. local lastHeight = y%3
  435. layers = math.ceil(y/3)
  436. local yMult = layers --This is basically a smart y/3 for movement
  437. local moveVolume = (area * yMult) --Kept for display percent
  438. --Calculating Needed Fuel--
  439. local exStack = numberOfStacksPerRun --Expected stacks of items before full
  440. neededFuel = yMult * x * z + --This is volume it will run through
  441.       (startDown + y)*(2+1/(64*exStack)) + --This is simplified and includes the y to get up times up and down + how many times it will drop stuff off
  442.       (x+z)*(yMult + 1/(64*exStack))  --Simplified as well: It is getting to start of row plus getting to start of row how many times will drop off stuff
  443.       --Original equation: x*z*y/3 + y * 2 + (x+z) * y/3 + (x + z + y) * (1/64*8)
  444. neededFuel = math.ceil(neededFuel)
  445.  
  446. --Getting Fuel
  447. if doCheckFuel and checkFuel() < neededFuel then
  448. neededFuel = neededFuel + fuelTable[fuelSafety] --For safety
  449.   print("Not enough fuel")
  450.   print("Current: ",checkFuel()," Needed: ",neededFuel)
  451.   print("Starting SmartFuel...")
  452.   sleep(2) --So they can read everything.
  453.   term.clear()
  454.   local oneFuel, neededFuelItems
  455.   local currSlot = 0
  456.   local function output(text, x, y) --For displaying fuel
  457.     local currX, currY = term.getCursorPos()
  458.     term.setCursorPos(x,y)
  459.     term.clearLine()
  460.     term.write(text)
  461.     term.setCursorPos(currX,currY)
  462.     end
  463.   local function roundTo(num, target) --For stacks of fuel
  464.     if num >= target then return target elseif num < 0 then return 0 else return num end
  465.   end
  466.   local function updateScreen()
  467.     output("Welcome to SmartFuel! Now Refueling...", 1,1)
  468.     output("Currently taking fuel from slot "..currSlot,1,2)
  469.     output("Current single fuel: "..tostring(oneFuel or 0),1,3)
  470.     output("Current estimate of needed fuel: ",1,4)
  471.     output("Single Items: "..math.ceil(neededFuelItems or 0),4,5)
  472.     output("Stacks:       "..math.ceil((neededFuelItems or 0) / 64),4,6)
  473.     output("Needed Fuel: "..tostring(neededFuel),1,12)
  474.     output("Current Fuel: "..tostring(checkFuel()),1,13)
  475.   end
  476.   while checkFuel() <= neededFuel do
  477.     currSlot = currSlot + 1
  478.     turtle.select(currSlot)
  479.     updateScreen()
  480.     while turtle.getItemCount(currSlot) == 0 do sleep(1.5) end
  481.     repeat
  482.       local previous = checkFuel()
  483.       turtle.refuel(1)
  484.       oneFuel = checkFuel() - previous
  485.       updateScreen()
  486.     until (oneFuel or 0) > 0 --Not an if to prevent errors if fuel taken out prematurely.
  487.     neededFuelItems = (neededFuel - checkFuel()) / oneFuel
  488.     turtle.refuel(math.ceil(roundTo(neededFuelItems, 64))) --Change because can only think about 64 at once.
  489.     if turtle.getItemCount(roundTo(currSlot + 1, 16)) == 0 then --Resets if no more fuel
  490.       currSlot = 0
  491.     end
  492.     neededFuelItems = (neededFuel - checkFuel()) / oneFuel
  493.   end
  494. end
  495. --Ender Chest Obtaining
  496. if enderChestEnabled then
  497.   while turtle.getItemCount(enderChestSlot) ~= 1 do
  498.     screen(1,1)
  499.     print("You have decided to use an Ender Chest!")
  500.     print("Please place one Ender Chest in slot ",enderChestSlot)
  501.     sleep(1)
  502.   end
  503.   print("Ender Chest in slot ",enderChestSlot, " checks out")
  504.   sleep(2)
  505. end
  506. --Initial Rednet Handshake
  507. if rednetEnabled then
  508. screen(1,1)
  509. print("Rednet is Enabled")
  510. print("The Channel to open is "..channels.send)
  511. modem = peripheral.wrap("right")
  512. modem.open(channels.receive)
  513. local i = 0
  514. repeat
  515. local id = os.startTimer(3)
  516. i=i+1
  517. print("Sending Initial Message "..i)
  518. modem.transmit(channels.send, channels.receive, "{ 'Initial' }")
  519. local message
  520. repeat
  521. local event, idCheck, channel,_,locMessage, distance = os.pullEvent()
  522. message = locMessage
  523. until (event == "timer" and idCheck == id) or (event == "modem_message" and channel == channels.receive and message == channels.confirm)
  524. until message == channels.confirm
  525. connected = true
  526. print("Connection Confirmed!")
  527. sleep(1.5)
  528. end
  529. local function biometrics(sendChannel)
  530. local commands = { Confirm = "Confirm" }
  531. local toSend = { ["x"] = x, ["y"] = (layers), ["z"] = z, --The y calc is weird...
  532.     ["xPos"] = xPos, ["yPos"] = yPos, ["zPos"] = zPos,
  533.     ["percent"] = percent, ["mined" ]= mined,
  534.     ["fuel"] = checkFuel(), ["moved"] = moved,
  535.     ["remainingBlocks"] = (volume-mined), ["ID"] = os.getComputerID(),
  536.     ["isInPath"] = isInPath, --Whether it is going back to start
  537.     ["volume"] = volume, ["area"] = area}
  538. modem.transmit(channels.send, channels.receive, textutils.serialize(toSend))
  539. id = os.startTimer(0.1)
  540. local event, message
  541. repeat
  542. local locEvent, idCheck, confirm, _, locMessage, distance = os.pullEvent()
  543. event, message = locEvent, locMessage
  544. until (event == "timer" and idCheck == id) or (event == "modem_message" and confirm == channels.receive)
  545. if event == "modem_message" then connected = true else connected = false end
  546. --Stuff to do for different commands
  547. end
  548. --Showing changes to settings
  549. screen(1,1)
  550. print("Your selected settings:")
  551. if #changedT == 0 then
  552. print("Completely Default")
  553. else
  554. for i=1, #changedT do
  555. print(changedT[i][1],": ",changedT[i][2]) --Name and Value
  556. end
  557. end
  558. print("\nStarting in 3"); sleep(1); print("2"); sleep(1); print("1"); sleep(1.5) --Dramatic pause at end
  559.  
  560.  
  561.  
  562. ----------------------------------------------------------------
  563. --Define ALL THE FUNCTIONS
  564. function eventAdd(...)
  565.   return table.insert(events,1, {...}) or true
  566. end
  567. function eventGet(pos)
  568.   return events[tonumber(pos) or #events]
  569. end
  570. function eventPop(pos)
  571.   return table.remove(events,tonumber(pos) or #events) or false --This will return value popped, tonumber returns nil if fail, so default to end
  572. end
  573. function eventRun(value, ...)
  574.   local argsList = {...}
  575.   if type(value) == "string" then
  576.     if value:sub(-1) ~= ")" then --So supports both "up()" and "up"
  577.       value = value .. "("
  578.       for a, b in pairs(argsList) do --Appending arguments
  579.         local toAppend
  580.         if type(b) == "table" then toAppend = textutils.serialize(b)
  581.         elseif type(b) == "string" then toAppend = "\""..tostring(b).."\"" --They weren't getting strings around them
  582.         else toAppend = tostring(b) end
  583.         value = value .. (toAppend or "true") .. ", "
  584.       end
  585.       if value:sub(-1) ~= "(" then --If no args, do not want to cut off
  586.         value = value:sub(1,-3)..""
  587.       end
  588.       value = value .. ")"
  589.     end
  590.     --print(value) --Debug
  591.     local func = loadstring(value)
  592.     setfenv(func, getfenv(1))
  593.     return func()
  594.   end
  595. end
  596.      
  597. function runAllEvents()
  598.   while #events > 0 do
  599.     local toRun = eventGet()
  600.     --print(toRun[1]) --Debug
  601.     eventRun(unpack(toRun))
  602.     eventPop()
  603.   end
  604. end
  605.  
  606. function display() --This is just the last screen that displays at the end
  607. screen(1,1)
  608. print("Total Blocks Mined: "..mined)
  609. print("Current Fuel Level: "..turtle.getFuelLevel())
  610. print("Cobble: "..totals.cobble)
  611. print("Usable Fuel: "..totals.fuel)
  612. print("Other: "..totals.other)
  613. if rednetEnabled then
  614. print("")
  615. print("Sent Stop Message")
  616. finalTable = {{["Mined: "] = mined}, {["Cobble: "] = totals.cobble}, {["Fuel: "] = totals.fuel},
  617.     {["Other: "] = totals.other}, {["Fuel: "] = checkFuel()} }
  618. modem.transmit(channels.send,channels.receive,"stop")
  619. modem.transmit(channels.send,channels.receive,textutils.serialize(finalTable))
  620. modem.close(channels.receive)
  621. end
  622. if doBackup then fs.delete(saveFile) end
  623. end
  624. function updateDisplay() --Runs in Mine(), display information to the screen in a certain place
  625. screen(1,1)
  626. print("Blocks Mined")
  627. print(mined)
  628. print("Percent Complete")
  629. print(percent.."%")
  630. print("Fuel")
  631. print(checkFuel())
  632.   -- screen(1,1)
  633.   -- print("Xpos: ")
  634.   -- print(xPos)
  635.   -- print("RelXPos: ")
  636.   -- print(relxPos)
  637.   -- print("Z Pos: ")
  638.   -- print(zPos)
  639.   -- print("Y pos: ")
  640.   -- print(yPos)
  641. if rednetEnabled then
  642. screenLine(1,7)
  643. print("Connected: "..tostring(connected))
  644. end
  645. end
  646. function logMiningRun(textExtension, extras) --Logging mining runs
  647. if logging then
  648. local number
  649. if not fs.isDir(logFolder) then
  650.   fs.delete(logFolder)
  651.   fs.makeDir(logFolder)
  652.   number = 1
  653. else
  654.   local i = 0
  655.   repeat
  656.     i = i + 1
  657.   until not fs.exists(logFolder.."/Quarry_Log_"..tostring(i)..(textExtension or ""))
  658.   number = i
  659. end
  660. handle = fs.open(logFolder.."/Quarry_Log_"..tostring(number)..(textExtension or ""),"w")
  661. local function write(...)
  662.   for a, b in ipairs({...}) do
  663.     handle.write(tostring(b))
  664.   end
  665.   handle.write("\n")
  666. end
  667. write("Welcome to the Quarry Logs!")
  668. write("Entry Number: ",number)
  669. write("Dimensions (X Z Y): ",x," ",z," ", y)
  670. write("Blocks Mined: ", mined)
  671. write("  Cobble: ", totals.cobble)
  672. write("  Usable Fuel: ", totals.fuel)
  673. write("  Other: ",totals.other)
  674. write("Total Fuel Used: ",  (originalFuel or (neededFuel + checkFuel()))- checkFuel()) --Protect against errors with some precision
  675. write("Expected Fuel Use: ", neededFuel)
  676. handle.close()
  677. end
  678. end
  679. function isFull(slots)
  680.   slots = slots or 16
  681.   local numUsed = 0
  682.   sleep(0)
  683.   for i=1, 16 do
  684.     if turtle.getItemCount(i) > 0 then numUsed = numUsed + 1 end
  685.   end
  686.   if numUsed > slots then
  687.     return true
  688.   end
  689.   return false
  690. end
  691. function dig(doAdd, func)
  692.   doAdd = doAdd or true
  693.   func = func or turtle.dig
  694.   if func() then
  695.     if doAdd then
  696.       mined = mined + 1
  697.     end
  698.     return true
  699.   end
  700.   return false
  701. end
  702. if not inverted then --Regular functions :) I switch definitions for optimizatoin (I thinK)
  703.   function digUp(doAdd)
  704.     return dig(doAdd,turtle.digUp)
  705.   end
  706.   function digDown(doAdd)
  707.     return dig(doAdd,turtle.digDown)
  708.   end
  709. else
  710.   function digDown(doAdd)
  711.     return dig(doAdd,turtle.digUp)
  712.   end
  713.   function digUp(doAdd)
  714.     return dig(doAdd,turtle.digDown)
  715.   end
  716. end
  717. function relativeXCalc()
  718.   if rowCheck == "right" then relxPos = xPos else relxPos = (x-xPos)+1 end
  719. end
  720. function forward(doAdd)
  721.   if doAdd == nil then doAdd = true end
  722.   if turtle.forward() then
  723.     if doAdd then
  724.       moved = moved + 1
  725.     end
  726.     if facing == 0 then
  727.       xPos = xPos + 1
  728.     elseif facing == 1 then
  729.       zPos = zPos + 1
  730.     elseif facing == 2 then
  731.       xPos = xPos - 1
  732.     elseif facing == 3 then
  733.       zPos = zPos - 1
  734.     else
  735.       error("Function forward, facing should be 0 - 3, got "..tostring(facing),2)
  736.     end
  737.     relativeXCalc()
  738.     return true
  739.   end
  740.   return false
  741. end
  742. function up(sneak)
  743.   sneak = sneak or 1
  744.   if inverted and sneak == 1 then
  745.     down(-1)
  746.   else
  747.     while not turtle.up() do --Absolute dig, not relative
  748.       if not dig(true, turtle.digDown) then
  749.         attackUp()
  750.         sleep(0.5)
  751.       end
  752.     end
  753.     yPos = yPos - sneak --Oh! I feel so clever
  754.   end                   --This works because inverted :)
  755.   saveProgress()
  756. end
  757. function down(sneak)
  758.   sneak = sneak or 1
  759.   local count = 0
  760.   if inverted and sneak == 1 then
  761.     up(-1)
  762.   else
  763.     while not turtle.down() do
  764.       count = count + 1
  765.       if not dig(true, turtle.digDown) then --This is absolute dig down, not relative
  766.         attackDown()
  767.         sleep(0.2)
  768.       end
  769.       if count > 20 then bedrock() end
  770.     end
  771.     yPos = yPos + sneak
  772.   end
  773.   saveProgress()
  774. end
  775. function right(num)
  776.   num = num or 1
  777.   for i=1, num do facing = coterminal(facing+1); saveProgress(); turtle.turnRight() end
  778. end
  779. function left(num)
  780.   num = num or 1
  781.   for i=1, num do facing = coterminal(facing-1); saveProgress(); turtle.turnLeft() end
  782. end
  783. function attack(doAdd, func)
  784.   doAdd = doAdd or true
  785.   func = func or turtle.attack
  786.   if func() then
  787.     if doAdd then
  788.       attacked = attacked + 1
  789.     end
  790.     return true
  791.   end
  792.   return false
  793. end
  794. function attackUp(doAdd)
  795.   if inverted then
  796.     return attack(doAdd, turtle.attackDown)
  797.   else
  798.     return attack(doAdd, turtle.attackUp)
  799.   end
  800. end
  801. function attackDown(doAdd)
  802.   if inverted then
  803.     return attack(doAdd, turtle.attackUp)
  804.   else
  805.     return attack(doAdd, turtle.attackDown)
  806.   end
  807. end
  808.  
  809.  
  810. function mine(doDigDown, doDigUp, outOfPath,doCheckInv) -- Basic Move Forward
  811. if doCheckInv == nil then doCheckInv = true end
  812. if doDigDown == nil then doDigDown = true end
  813. if doDigUp == nil then doDigUp = true end
  814. if outOfPath == nil then outOfPath = false end
  815. if inverted then
  816.   doDigUp, doDigDown = doDigDown, doDigUp --Just Switch the two if inverted
  817. end
  818. if doRefuel and checkFuel() <= fuelTable[fuelSafety]/2 then
  819.   for i=1, 16 do
  820.   if turtle.getItemCount(i) > 0 then
  821.     turtle.select(i)
  822.     if checkFuel() < 200 + fuelTable[fuelSafety] then
  823.       turtle.refuel()
  824.     end
  825.   end
  826.   end
  827. end
  828. local count = 0
  829. while not forward(not outOfPath) do
  830.   sleep(0) --Calls coroutine.yield to prevent errors
  831.   count = count + 1
  832.   if not dig() then
  833.     attack()
  834.   end
  835.   if count > 10 then
  836.     attack()
  837.     sleep(0.2)
  838.   end
  839.   if count > 50 then
  840.     if turtle.getFuelLevel() == 0 then --Don't worry about inf fuel because I modified this function
  841.       saveProgress({doCheckFuel = true})
  842.       error("No more fuel",0)
  843.     elseif yPos > (startY-7) then --If it is near bedrock
  844.       bedrock()
  845.     else --Otherwise just sleep for a bit to avoid sheeps
  846.       sleep(1)
  847.     end
  848.   end
  849. end
  850. checkSanity() --Not kidding... This is necessary
  851. saveProgress(tab)
  852. if doDigUp then
  853. while turtle.detectUp() do
  854.   sleep(0) --Calls coroutine.yield
  855.   if not digUp() then
  856.     attackUp()
  857.     count = count + 1
  858.   end
  859.   if count > 50 and yPos > (startY-7) then --Same deal with bedrock as above
  860.     bedrock()
  861.   end
  862.   end
  863. end
  864. if doDigDown then
  865.  digDown()
  866. end
  867. percent = math.ceil(moved/moveVolume*100)
  868. updateDisplay()
  869. isInPath = (not outOfPath) --For rednet
  870. if doCheckInv and careAboutResources then
  871. if moved%invCheckFreq == 0 then
  872.  if isFull(16-keepOpen) then dropOff() end
  873. end; end
  874. if rednetEnabled then biometrics() end
  875. end
  876. --Insanity Checking
  877. function checkSanity()
  878.   if isInPath and not (facing == 0 or facing == 2) and #events == 0 then --If mining and not facing proper direction and not in a turn
  879.     turnTo(0)
  880.     rowCheck = "right"
  881.   end
  882.   if xPos < 0 or xPos > x or zPos < 0 or zPos > z or yPos < 0 then
  883.     saveProgress()
  884.     print("Oops. Detected that quarry was outside of predefined boundaries.")
  885.     print("Please go to my forum thread and report this with a short description of what happened")
  886.     print("If you could also run \"pastebin put Civil_Quarry_Restore\" and give me that code it would be great")
  887.     error("",0)
  888.   end
  889. end
  890.  
  891. local function fromBoolean(input) --Like a calculator
  892. if input then return 1 end
  893. return 0
  894. end
  895. local function multBoolean(first,second) --Boolean multiplication
  896. return (fromBoolean(first) * fromBoolean(second)) == 1
  897. end
  898. function coterminal(num, limit) --I knew this would come in handy :D
  899. limit = limit or 4 --This is for facing
  900. return math.abs((limit*fromBoolean(num < 0))-(math.abs(num)%limit))
  901. end
  902. if tArgs["-manualpos"] then
  903.   facing = coterminal(facing) --Done to improve support for "-manualPos"
  904.   if facing == 0 then rowCheck = "right" elseif facing == 2 then rowCheck = "left" end --Ditto
  905.   relativeXCalc() --Ditto
  906. end
  907.  
  908. --Direction: Front = 0, Right = 1, Back = 2, Left = 3
  909.  
  910. function turnTo(num)
  911.   num = num or facing
  912.   num = coterminal(num) --Prevent errors
  913.   local turnRight = true
  914.   if facing-num == 1 or facing-num == -3 then turnRight = false end --0 - 1 = -3, 1 - 0 = 1, 2 - 1 = 1
  915.   while facing ~= num do          --The above is used to smartly turn
  916.     if turnRight then
  917.       right()
  918.     else
  919.       left()
  920.     end
  921.   end
  922. end
  923. function goto(x,z,y, toFace)
  924. --Will first go to desired z pos, then x pos, y pos varies
  925. x = x or 1; y = y or 1; z = z or 1; toFace = toFace or facing
  926. if yPos > y then --Will go up first if below position
  927.   while yPos~=y do up() end
  928. end
  929. if zPos > z then
  930.   turnTo(3)
  931. elseif zPos < z then
  932.   turnTo(1)
  933. end
  934. while zPos ~= z do mine(false,false,true,false) end
  935. if xPos > x then
  936.   turnTo(2)
  937. elseif xPos < x then
  938.   turnTo(0)
  939. end
  940. while xPos ~= x do mine(false,false,true,false) end
  941. if yPos < y then --Will go down after if above position
  942.   while yPos~=y do down() end
  943. end
  944. turnTo(toFace,"right")
  945. saveProgress()
  946. end
  947. function drop(side, final, allowSkip)
  948. side = sides[side] or "front"    --The final number means that it will
  949. if final then final = 0 else final = 1 end --drop a whole stack at the end
  950. local allowSkip = allowSkip or (final == 0) --This will allow drop(side,t/f, rednetConnected)
  951. count()
  952. if doRefuel then
  953.   for i=1, 16 do
  954.     if slot[i][1] == 2 then
  955.       turtle.select(i); turtle.refuel()
  956.     end
  957.   end
  958.   turtle.select(1)
  959. end
  960. if side == "right" then turnTo(1) end
  961. if side == "left" then turnTo(3) end
  962. local whereDetect, whereDrop1, whereDropAll
  963. local _1 = slot[1][2] - final --All but one if final, all if not final
  964. if side == "top" then
  965. whereDetect = turtle.detectUp ; whereDrop = turtle.dropUp
  966. elseif side == "bottom" then
  967. whereDetect = turtle.detectDown ; whereDrop = turtle.dropDown
  968. else
  969. whereDetect = turtle.detect; whereDrop = turtle.drop
  970. end
  971. local function waitDrop(val) --This will just drop, but wait if it can't
  972.   val = val or 64
  973.   local try = 1
  974.   while not whereDrop(val) do
  975.     print("Chest Full, Try "..try)
  976.     try = try + 1
  977.     sleep(2)
  978.   end
  979. end
  980. repeat
  981. local detected = whereDetect()
  982. if detected then
  983.   waitDrop(_1)
  984.   for i=1, 2 do --This is so I quit flipping missing items when chests are partially filled
  985.     for i=2, 16 do
  986.       if turtle.getItemCount(i) > 0 then
  987.         turtle.select(i)
  988.         waitDrop(nil, i)
  989.       end
  990.     end
  991.   end
  992. elseif not allowSkip then
  993.   print("Waiting for chest placement place a chest to continue")
  994.   while not whereDetect() do
  995.     sleep(1)
  996.   end
  997. end
  998. until detected or allowSkip
  999. if not allowSkip then totals.cobble = totals.cobble - 1 end
  1000. turtle.select(1)
  1001. end
  1002. function dropOff() --Not local because called in mine()
  1003. local currX,currZ,currY,currFacing = xPos, zPos, yPos, facing
  1004. if careAboutResources and not enderChestEnabled then --Regularly
  1005. eventAdd("goto", 1,1,currY,2) --Need this step for "-startDown"
  1006. eventAdd("goto(0,1,1,2)")
  1007. eventAdd("drop", dropSide,false)
  1008. eventAdd("turnTo(0)")
  1009. eventAdd("mine",false,false,true,false)
  1010. eventAdd("goto(1,1,1, 0)")
  1011. eventAdd("goto", 1, 1, currY, 0)
  1012. eventAdd("goto", currX,currZ,currY,currFacing)
  1013. elseif careAboutResources then --If using an enderChest
  1014. eventAdd("turnTo",currFacing-2)
  1015. eventAdd("dig",false)
  1016. eventAdd("turtle.select",enderChestSlot)
  1017. eventAdd("turtle.place")
  1018. eventAdd("drop","front",false)
  1019. eventAdd("turtle.select", enderChestSlot)
  1020. eventAdd("dig",false)
  1021. eventAdd("turnTo",currFacing)
  1022. eventAdd("turtle.select(1)")
  1023. end
  1024. runAllEvents()
  1025. return true
  1026. end
  1027. function bedrock()
  1028. if checkFuel() == 0 then error("No Fuel",0) end
  1029. local origin = {x = xPos, y = yPos, z = zPos}
  1030. print("Bedrock Detected")
  1031. if turtle.detectUp() then
  1032. print("Block Above")
  1033. local var
  1034. if facing == 0 then var = 2 elseif facing == 2 then var = 0 else error("Was facing left or right on bedrock") end
  1035. goto(xPos,zPos,yPos,var)
  1036. for i=1, relxPos do mine(true,true); end
  1037. end
  1038. goto(0,1,1,2)
  1039. drop(dropSide, true)
  1040. turnTo(0)
  1041. display()
  1042. print("\nFound bedrock at these coordinates: ")
  1043. print(origin.x," Was position in row\n",origin.z," Was row in layer\n",origin.y," Blocks down from start")
  1044. error("",0)
  1045. end
  1046.  
  1047. function endOfRowTurn(startZ, wasFacing, mineFunctionTable)
  1048. local halfFacing = 1
  1049. local toFace = coterminal(wasFacing + 2) --Opposite side
  1050. if zPos == startZ then
  1051.   if facing ~= halfFacing then turnTo(halfFacing) end
  1052.   mine(unpack(mineFunctionTable or {}))
  1053. end
  1054. if facing ~= toFace then
  1055.   turnTo(toFace)
  1056. end
  1057. end
  1058.  
  1059. -------------------------------------------------------------------------------------
  1060. --Pre-Mining Stuff dealing with session persistence
  1061. runAllEvents()
  1062. if toQuit then error("",0) end --This means that it was stopped coming for its last drop
  1063.  
  1064. local doDigDown, doDigUp = (lastHeight ~= 1), (lastHeight == 0) --Used in lastHeight
  1065. if not restoreFoundSwitch then --Regularly
  1066.   --Check if it is a mining turtle
  1067.   if not isMiningTurtle then
  1068.     local a, b = turtle.dig()
  1069.     if a then mined = mined + 1; isMiningTurtle = true
  1070.     elseif b == "Nothing to dig with" then
  1071.       print("This is not a mining turtle. To make a mining turtle, craft me together with a diamond pickaxe")
  1072.       error("",0)
  1073.     end
  1074.   end
  1075.   mine(false,false,true) --Get into quarry by going forward one
  1076.   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.
  1077.   gpsSecondPos = {gps.locate(gpsTimeout)} --Note: Does not run this if it has already been restarted.
  1078. end
  1079.   for i = 1, startDown do
  1080.     eventAdd("down") --Add a bunch of down events to get to where it needs to be.
  1081.   end
  1082.   runAllEvents()
  1083.   if not(y == 1 or y == 2) then down() end --Go down. If y is one or two, it doesn't need to do this.
  1084. else --restore found
  1085.   if doDigDown then digDown() end
  1086.   if doDigUp then digUp() end  --Get blocks missed before stopped
  1087. end
  1088. --Mining Loops--------------------------------------------------------------------------
  1089. turtle.select(1)
  1090. while layersDone <= layers do -------------Height---------
  1091. local lastLayer = layersDone == layers --If this is the last layer
  1092. local secondToLastLayer = (layersDone + 1) == layers --This is for the going down at the end of a layer.
  1093. moved = moved + 1 --To account for the first position in row as "moved"
  1094. if doDigDown then digDown() end --This is because it doesn't mine first block in layer
  1095. if not restoreFoundSwitch then rowCheck = "right" end
  1096. relativeXCalc()
  1097. while zPos <= z do -------------Width----------
  1098. while relxPos < x do ------------Length---------
  1099. 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
  1100. end ---------------Length End-------
  1101. if zPos ~= z then --If not on last row of section
  1102.   local func
  1103.   if rowCheck == "right" then --Swithcing to next row
  1104.   func = "right"; rowCheck = "left"; else func = "left"; rowCheck = "right" end --Which way to turn
  1105.     eventAdd("endOfRowTurn", zPos, facing , {not lastLayer or (doDigDown and lastLayer), not lastLayer or (doDigUp and lastLayer)}) --The table is passed to the mine function
  1106.     runAllEvents()
  1107. else break
  1108. end
  1109. end ---------------Width End--------
  1110. eventAdd("goto",1,1,yPos,0) --Goto start of layer
  1111. runAllEvents()
  1112. if not lastLayer then --If there is another layer
  1113.   for i=1, 2+fromBoolean(not(lastHeight~=0 and secondToLastLayer)) do down() end --The fromBoolean stuff means that if lastheight is 1 and last and layer, will only go down two
  1114. end
  1115. layersDone = layersDone + 1
  1116. restoreFoundSwitch = false --This is done so that rowCheck works properly upon restore
  1117. end ---------------Height End-------
  1118.  
  1119. eventAdd("goto",1,1,yPos,2) --Allows for startDown variable
  1120. eventAdd("goto",0,1,1,2)
  1121.  
  1122. --Output to a chest or sit there
  1123. if enderChestEnabled and not turtle.detect() then
  1124.   eventAdd("turtle.select",enderChestSlot)
  1125.   eventAdd("turtle.place")
  1126.   eventAdd("turtle.select(1)")
  1127. end
  1128. eventAdd("drop",dropSide, true)
  1129. eventAdd("turnTo(0)")
  1130.  
  1131. --Display was moved above to be used in bedrock function
  1132. eventAdd("display")
  1133. --Log current mining run
  1134. eventAdd("logMiningRun",logExtension)
  1135. toQuit = true --I'll use this flag to clean up
  1136. runAllEvents()
  1137. --Cleanup
  1138. turtle.getFuelLevel = getFuel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement