daily pastebin goal
69%
SHARE
TWEET

Quarry 3.3.1

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