Advertisement
civilwargeeky

Quarry 3.4.0

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