Guest User

Untitled

a guest
Aug 3rd, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 58.84 KB | None | 0 0
  1. --Civilwargeeky's Quarry Program
  2. VERSION = "3.6.3 TurtleScripts"
  3.  
  4.  
  5. civilTable = nil; _G.civilTable = {}; setmetatable(civilTable, {__index = getfenv()}); setfenv(1,civilTable)
  6. originalDay = os.day()
  7. numResumed = 0
  8.  
  9.  
  10. x,y,z = 3,3,3
  11. inverted = false
  12. rednetEnabled = false
  13.  
  14. dropSide = "front"
  15. careAboutResources = true
  16. doCheckFuel = true
  17. doRefuel = false
  18. keepOpen = 1
  19. fuelSafety = "moderate"
  20. excessFuelAmount = math.huge
  21. fuelMultiplier = 1
  22. saveFile = "Civil_Quarry_Restore"
  23. autoResume = true
  24. startupRename = "oldStartup.quarry"
  25. startupName = "startup"
  26. doBackup = true
  27. uniqueExtras = 8
  28. maxTries = 200
  29. gpsEnabled = false
  30. gpsTimeout = 3
  31. legacyRednet = false
  32. logging = true
  33. logFolder = "Quarry_Logs"
  34. logExtension = ""
  35. flatBedrock = false
  36. startDown = 0
  37. enderChestEnabled = false
  38. enderChestSlot = 16
  39. fuelChestEnabled = false
  40. fuelChestSlot = 15
  41. preciseTotals = false
  42. goLeftNotRight = false
  43. oreQuarry = false
  44. oreQuarryBlacklistName = "oreQuarryBlacklist.txt"
  45. dumpCompareItems = true
  46. inventoryMax = 16
  47. quadEnabled = false
  48. quadTimeout = 60 * 5
  49.  
  50. fuelTable = {
  51. safe = 1000,
  52. moderate = 200,
  53. loose = 0 }
  54.  
  55. channels = {
  56. send = os.getComputerID() + 1 ,
  57. receive = os.getComputerID() + 101 ,
  58. confirm = "Turtle Quarry Receiver",
  59. message = "Civil's Quarry",
  60. fingerprint = "quarry"
  61. }
  62.  
  63.  
  64.  
  65. local help_paragraph = [[
  66. Welcome!: Welcome to quarry help. Below are help entries for all parameters. Examples and tips are at the bottom.
  67. Unfortunately: I had to remove the help section to upload to turtle scripts. So sorry :( Please download from my forum thread instead.
  68. Thread: http://www.computercraft.info/forums2/index.php?/topic/5681-variable-size-quarry-now-with-super-ore-quarry%e2%84%a2/
  69. ]]
  70.  
  71. local help = {}
  72. local i = 0
  73. local titlePattern = ".-%:"
  74. local textPattern = "%:.+"
  75. for a in help_paragraph:gmatch("\n?.-\n") do
  76. local current = string.sub(a,1,-2)..""
  77. if string.sub(current,1,1) ~= " " then
  78. i = i + 1
  79. help[i] = {}
  80. help[i].title = string.sub(string.match(current, titlePattern),1,-2)..""
  81. help[i][1] = string.sub(string.match(current,textPattern) or " ",3,-1)
  82. elseif string.sub(current,1,1) == " " then
  83. table.insert(help[i], string.sub(current,2, -1).."")
  84. end
  85. end
  86.  
  87. local supportsRednet
  88. if peripheral.find then
  89. supportsRednet = peripheral.find("modem") or false
  90. else
  91. supportsRednet = (peripheral.getType("right") == "modem") or false
  92. end
  93.  
  94.  
  95. xPos,yPos,zPos,facing,percent,mined,moved,relxPos, rowCheck, connected, isInPath, layersDone, attacked, startY, chestFull, gotoDest, atChest, fuelLevel, numDropOffs, allowedItems, compareSlots, dumpSlots, selectedSlot, extraDropItems, oldOreQuarry, specialSlots, relzPos
  96. = 0, 1, 1, 0, 0, 0, 0, 1, true , false, true, 1, 0, 0, false, "", false, 0, 0, {}, {}, {}, 1, false, false, {}, 0
  97.  
  98. local statusString
  99.  
  100.  
  101. for i=1, inventoryMax do
  102. allowedItems[i] = 0
  103. dumpSlots[i] = false
  104. end
  105. totals = {cobble = 0, fuel = 0, other = 0}
  106.  
  107.  
  108. local function newSpecialSlot(index, value)
  109. value = tonumber(value) or 0
  110. if value ~= 0 and specialSlots[value] then error("Failed making special slot: "..index.."\nSlot "..tonumber(value).." already taken",2) end
  111. specialSlots[index] = value
  112. specialSlots[value] = index
  113. return true
  114. end
  115.  
  116. function resetDumpSlots()
  117. for i=1, inventoryMax do
  118. if oldOreQuarry then
  119. if turtle.getItemCount(i) > 0 and i~= specialSlots.enderChest then
  120. dumpSlots[i] = true
  121. else
  122. dumpSlots[i] = false
  123. end
  124. else
  125. dumpSlots[i] = false
  126. end
  127. end
  128. if not oldOreQuarry and specialSlots.enderChest == 1 then
  129. dumpSlots[2] = true
  130. elseif not oldOreQuarry then
  131. dumpSlots[1] = true
  132. end
  133. end
  134.  
  135. local function copyTable(tab) local toRet = {}; for a, b in pairs(tab) do toRet[a] = b end; return toRet end
  136.  
  137.  
  138.  
  139. local foundBedrock = false
  140.  
  141. local checkFuel, checkFuelLimit
  142. if turtle then
  143. checkFuel = turtle.getFuelLevel
  144. if turtle.getFuelLevel() == "unlimited" then
  145. checkFuel = function() return math.huge end
  146. end
  147. if turtle.getFuelLimit then
  148. checkFuelLimit = function() return math.min(turtle.getFuelLimit(), excessFuelAmount) end
  149. if turtle.getFuelLimit() == "unlimited" then
  150. checkFuelLimit = function() return math.huge end
  151. end
  152. else
  153. checkFuelLimit = function() return excessFuelAmount end
  154. end
  155.  
  156.  
  157. turtle.select(1)
  158. end
  159.  
  160.  
  161. function select(slot)
  162. if slot ~= selectedSlot and slot > 0 and slot <= inventoryMax then
  163. selectedSlot = slot
  164. return turtle.select(slot), selectedSlot
  165. end
  166. end
  167.  
  168.  
  169.  
  170.  
  171. local function screen(xPos,yPos)
  172. xPos, yPos = xPos or 1, yPos or 1
  173. term.setCursorPos(xPos,yPos); term.clear(); end
  174. local function screenLine(xPos,yPos)
  175. term.setCursorPos(xPos,yPos); term.clearLine(); end
  176.  
  177. screen(1,1)
  178. print("")
  179. print("")
  180.  
  181. local sides = {top = "top", right = "right", left = "left", bottom = "bottom", front = "front"}
  182. local tArgs
  183. local originalArgs = {...}
  184. local changedT, tArgsWithUpper, forcePrompts = {}, {}, {}
  185. changedT.new = function(key, value) table.insert(changedT,{key, value}) end
  186. changedT.remove = function() table.remove(changedT) end
  187. local function capitalize(text) return (string.upper(string.sub(text,1,1))..string.sub(text,2,-1)) end
  188. local function initializeArgs()
  189. tArgs = copyTable(originalArgs)
  190. for i=1, #tArgs do
  191. tArgsWithUpper[i] = tArgs[i]
  192. tArgsWithUpper[tArgsWithUpper[i]] = i
  193. tArgs[i] = tArgs[i]:lower()
  194. tArgs[tArgs[i]] = i
  195. if tArgs[i] == "-forceprompt" and i ~= #tArgs then
  196. forcePrompts[tArgs[i+1]:lower()] = true
  197. end
  198. end
  199. end
  200. initializeArgs()
  201.  
  202. local restoreFound, restoreFoundSwitch = false
  203. function parseParam(name, displayText, formatString, forcePrompt, trigger, variableOverride, variableExists)
  204. if variableExists ~= false then variableExists = true end
  205. if trigger == nil then trigger = true end
  206. if not trigger then return end
  207. if restoreFoundSwitch or tArgs["-default"] then forcePrompt = false end
  208. if not restoreFoundSwitch and (tArgs["-promptall"] or forcePrompts[name:lower()]) then forcePrompt = true end
  209. local toGetText = name:lower()
  210. local formatType = formatString:match("^%a+"):lower() or error("Format String Unknown: "..formatString)
  211. local args = formatString:sub(({formatString:find(formatType)})[2] + 2)..""
  212. local variable = variableOverride or name
  213. local func = loadstring("return "..variable)
  214. setfenv(func,getfenv(1))
  215. local originalValue = assert(func)()
  216. if originalValue == nil and variableExists then error("From addParam, \""..variable.."\" returned nil",2) end
  217. local givenValue, toRet
  218. if tArgs["-"..toGetText] then
  219. givenValue = tArgsWithUpper[tArgs["-"..toGetText]+1]
  220. elseif forcePrompt then
  221. write(displayText.."? ")
  222. givenValue = io.read()
  223. end
  224. if formatType == "force" then
  225. toRet = (tArgs["-"..toGetText] and true) or false
  226. end
  227. if not (givenValue or toRet) or (type(givenValue) == "string" and #givenValue == 0) then return end
  228. if formatType == "boolean" then
  229. toRet = givenValue:sub(1,1):lower() ~= "n" and givenValue:sub(1,1):lower() ~= "f"
  230. elseif formatType == "string" then
  231. toRet = givenValue:match("^[%w%./]+")
  232. elseif formatType == "number" or formatType == "float" then
  233. toRet = tonumber(givenValue)
  234. if not toRet then return end
  235. if formatType == "number" then toRet = math.floor(toRet) end
  236. local startNum, endNum = formatString:match("(%d+)%-(%d+)")
  237. startNum, endNum = tonumber(startNum), tonumber(endNum)
  238. if not ((toRet >= startNum) and (toRet <= endNum)) then return end
  239. elseif formatType == "side" then
  240. local exclusionTab = {}
  241. for a in args:gmatch("%S+") do exclusionTab[a] = true end
  242. if not exclusionTab[givenValue] then toRet = sides[givenValue] end
  243. elseif formatType == "list" then
  244. toRet = {}
  245. for a in args:gmatch("[^,]") do
  246. table.insert(toRet,a)
  247. end
  248. elseif formatType == "force" then
  249. else error("Improper formatType",2)
  250. end
  251. if toRet == nil then return end
  252. tempParam = toRet
  253. local func = loadstring(variable.." = tempParam")
  254. setfenv(func, getfenv(1))
  255. func()
  256. tempParam = nil
  257. if toRet ~= originalValue and displayText ~= "" then
  258. changedT.new(displayText, tostring(toRet))
  259. end
  260. return toRet
  261. end
  262.  
  263. local paramLookup = {}
  264. local function addParam(...)
  265. local args = {...}
  266. if not paramLookup[args[1]] then
  267. local toRet = copyTable(args)
  268. for i=2, table.maxn(toRet) do
  269. toRet[i-1] = toRet[i]
  270. end
  271. table.remove(toRet)
  272. paramLookup[args[1]] = toRet
  273. end
  274. return parseParam(unpack(args, 1, table.maxn(args)))
  275. end
  276.  
  277. local function paramAlias(original, alias)
  278. local a = paramLookup[original]
  279. if a then
  280. if a[5] == nil then a[5] = original end
  281. parseParam(alias, unpack(a, 1, table.maxn(a)))
  282. else
  283. error("In paramAlias: '"..original.."' did not exist",2)
  284. end
  285. end
  286.  
  287.  
  288. if not(turtle or tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"]) then
  289. print("This is not a turtle, you might be looking for the \"Companion Rednet Program\" \nCheck My forum thread for that")
  290. print("Press 'q' to quit, or any other key to start help ")
  291. if ({os.pullEvent("char")})[2] ~= "q" then tArgs.help = true else error("",0) end
  292. end
  293.  
  294. if tArgs["help"] or tArgs["-help"] or tArgs["-?"] or tArgs["?"] then
  295. print("You have selected help, press any key to continue"); print("Use arrow keys to navigate, q to quit"); os.pullEvent("key")
  296. local pos = 1
  297. local key = 0
  298. while pos <= #help and key ~= keys.q do
  299. if pos < 1 then pos = 1 end
  300. screen(1,1)
  301. print(help[pos].title)
  302. for a=1, #help[pos] do print(help[pos][a]) end
  303. repeat
  304. _, key = os.pullEvent("key")
  305. until key == 200 or key == 208 or key == keys.q
  306. if key == 200 then pos = pos - 1 end
  307. if key == 208 then pos = pos + 1 end
  308. end
  309. error("",0)
  310. end
  311.  
  312. if tArgs["-version"] or tArgs["version"] then
  313. print("QUARRY VERSION: ",VERSION)
  314. error("",0)
  315. end
  316.  
  317.  
  318. local function split(str, sep)
  319. assert(#sep == 1, "Split seperator too long. Got '"..sep.."'")
  320. if not str:match(sep) then return {str} end
  321. local toRet = {}
  322. toRet[1] = str:match("^([^"..sep.."]-)"..sep)
  323. for i in str:gmatch(sep.."([^"..sep.."]*)") do
  324. toRet[#toRet+1] = i
  325. end
  326. return toRet
  327. end
  328.  
  329. if addParam("file","Custom Parameters","string", false, nil, "parameterFile", false) and parameterFile then
  330. if not fs.exists(parameterFile) then
  331. print("WARNING: '"..parameterFile.."' DOES NOT EXIST. FILE NOT LOADED")
  332. sleep(3)
  333. changedT.remove()
  334. else
  335. local file = fs.open(parameterFile, "r")
  336. local text = file.readAll()
  337. file.close()
  338. text = text.."\n"
  339. text = text:gsub("#[^\n]-\n","")
  340. local commands = {}
  341. local append = table.insert
  342. for _, a in pairs(split(text,"\n")) do
  343. local words = split(a," ")
  344. if not a:match("-") then
  345. append(originalArgs,"-"..words[1])
  346. for i=2, #words do
  347. append(originalArgs, words[i])
  348. end
  349. else
  350. for i=1, #words do
  351. append(originalArgs, words[i])
  352. end
  353. end
  354. end
  355. initializeArgs()
  356. print("Finished loading file: ",tArgs[tArgs["-file"]+1])
  357. sleep(0.5)
  358. end
  359. end
  360.  
  361.  
  362.  
  363.  
  364. addParam("doBackup", "Backup Save File", "boolean")
  365. addParam("saveFile", "Save File Name", "string")
  366.  
  367. restoreFound = fs.exists(saveFile)
  368. restoreFoundSwitch = (tArgs["-restore"] or tArgs["-resume"] or tArgs["-atchest"]) and restoreFound and doBackup
  369. if restoreFoundSwitch then
  370. local file = fs.open(saveFile,"r")
  371. local test = file.readAll() ~= ""
  372. file.close()
  373. if test then
  374. os.run(getfenv(1),saveFile)
  375. numResumed = numResumed + 1
  376. if checkFuel() ~= math.huge then
  377. if fuelLevel - checkFuel() == 1 then
  378. if facing == 0 then xPos = xPos + 1
  379. elseif facing == 2 then xPos = xPos - 1
  380. elseif facing == 1 then zPos = zPos + 1
  381. elseif facing == 3 then zPos = zPos - 1 end
  382. elseif fuelLevel - checkFuel() ~= 0 then
  383. print("Very Strange Fuel in Restore Section...")
  384. print("Current: ",checkFuel())
  385. print("Saved: ",fuelLevel)
  386. print("Difference: ",fuelLevel - checkFuel())
  387. os.pullEvent("char")
  388. end
  389. end
  390. if gpsEnabled then
  391. print("Found GPS Start Coordinates")
  392. local currLoc = {gps.locate(gpsTimeout)} or {}
  393. local backupPos = {xPos, yPos, zPos}
  394. if #currLoc > 0 and #gpsStartPos > 0 and #gpsSecondPos > 0 then
  395. print("GPS Position Successfully Read")
  396. if currLoc[1] == gpsStartPos[1] and currLoc[3] == gpsStartPos[3] then
  397. xPos, yPos, zPos = 0,1,1
  398. if facing ~= 0 then turnTo(0) end
  399. print("Is at start")
  400. else
  401. if inverted then
  402.  
  403. end
  404. local a, b = copyTable(gpsStartPos), copyTable(gpsSecondPos)
  405. local flag = true
  406. if b[3] - a[3] == -1 then
  407. a[1] = a[1] - 1
  408. xPos, zPos = -currLoc[3] + a[3], currLoc[1] + -a[1]
  409. elseif b[1] - a[1] == 1 then
  410. a[3] = a[3] - 1
  411. xPos, zPos = currLoc[1] + -a[1], currLoc[3] + -a[3]
  412. elseif b[3] - a[3] == 1 then
  413. a[1] = a[1] + 1
  414. xPos, zPos = currLoc[3] + a[3], -currLoc[1] + a[3]
  415. elseif b[1] - a[1] == -1 then
  416. a[3] = a[3] + 1
  417. xPos, zPos = -currLoc[1] + a[1], -currLoc[3] + a[3]
  418. else
  419. flag = false
  420. print("Improper Coordinates")
  421. print("GPS Locate Failed, Using Standard Methods")
  422. end
  423. if flag and goLeftNotRight then
  424. zPos = math.abs(zPos-1) + 1
  425. end
  426. end
  427. print("X Pos: ",xPos)
  428. print("Y Pos: ",yPos)
  429. print("Z Pos: ",zPos)
  430. print("Facing: ",facing)
  431. for i=1, 3, 2 do
  432. if backupPos[i] ~= currLoc[i] then
  433. events = {}
  434. end
  435. end
  436. else
  437. print("GPS Locate Failed, Using Standard Methods")
  438. end
  439. print("Restore File read successfully. Starting in 3"); sleep(3)
  440. end
  441. else
  442. fs.delete(saveFile)
  443. print("Restore file was empty, sorry, aborting")
  444. error("",0)
  445. end
  446. else
  447. events = {}
  448. originalFuel = checkFuel()
  449. end
  450.  
  451.  
  452. if tArgs["-dim"] then
  453. local a,b,c = x,y,z
  454. local num = tArgs["-dim"]
  455. x = tonumber(tArgs[num + 1]) or x; z = tonumber(tArgs[num + 2]) or z; y = tonumber(tArgs[num + 3]) or y
  456. if a ~= x then changedT.new("Length", x) end
  457. if c ~= z then changedT.new("Width", z) end
  458. if b ~= y then changedT.new("Height", y) end
  459. elseif not (tArgs["-default"] or restoreFoundSwitch) then
  460. print("What dimensions?")
  461. print("")
  462.  
  463. term.write("Length? ")
  464. x = math.floor(math.abs(tonumber(io.read()) or x))
  465. term.write("Width? ")
  466. z = math.floor(math.abs(tonumber(io.read()) or z))
  467. term.write("Height? ")
  468. y = math.floor(math.abs(tonumber(io.read()) or y))
  469. changedT.new("Length",x); changedT.new("Width",z); changedT.new("Height",y)
  470. end
  471.  
  472.  
  473. addParam("flatBedrock","Go to bedrock", "boolean")
  474. addParam("invert", "Inverted","boolean", true, not flatBedrock, "inverted")
  475. addParam("startDown","Start Down","number 1-256", nil, not flatBedrock)
  476. addParam("left","Left Quarry","boolean", nil, nil, "goLeftNotRight")
  477.  
  478. addParam("chest", "Chest Drop Side", "side front", nil, nil, "dropSide")
  479. addParam("enderChest","Ender Chest Enabled","boolean special", nil, nil, "enderChestEnabled")
  480. addParam("enderChest", "Ender Chest Slot", "number 1-16", nil, nil, "enderChestSlot")
  481. newSpecialSlot("enderChest",enderChestEnabled and enderChestSlot or 0)
  482. addParam("fuelChest","Fuel Chest Enabled","boolean special", nil, nil, "fuelChestEnabled")
  483. addParam("fuelChest", "Fuel Chest Slot", "number 1-16", nil, nil, "fuelChestSlot")
  484. newSpecialSlot("fuelChest", fuelChestEnabled and fuelChestSlot or 0)
  485.  
  486. addParam("rednet", "Rednet Enabled","boolean",true, supportsRednet, "rednetEnabled")
  487. addParam("sendChannel", "Rednet Send Channel", "number 1-65535", false, supportsRednet, "channels.send")
  488. addParam("receiveChannel","Rednet Receive Channel", "number 1-65535", false, supportsRednet, "channels.receive")
  489. addParam("fingerprint","Sending Fingerprint", "string", false, supportsRednet, "channels.fingerprint")
  490. addParam("legacyRednet","Legacy Rednet","boolean", false, supportsRednet)
  491.  
  492. if addParam("quad", "Quad Rotor Enabled","boolean",nil, rednetEnabled, "quadEnabled") then
  493. gpsEnabled = true
  494. end
  495. addParam("quadTimeout","Quad Rotor Timeout","number 1-1000000", nil, quadEnabled)
  496.  
  497. addParam("gps", "GPS Location Services", "force", nil, (not restoreFoundSwitch) and supportsRednet and not quadEnabled, "gpsEnabled" )
  498. if gpsEnabled and not restoreFoundSwitch then
  499. gpsStartPos = {gps.locate(gpsTimeout)}
  500. gpsEnabled = #gpsStartPos > 0
  501. if quadEnabled and not gpsEnabled then
  502. error("You have no GPS network. You may not use Quad Rotors",0)
  503. end
  504. end
  505.  
  506. addParam("uniqueExtras","Unique Items", "number 0-15")
  507. addParam("doRefuel", "Refuel from Inventory","boolean", nil, checkFuel() ~= math.huge)
  508. addParam("doCheckFuel", "Check Fuel", "boolean", nil, checkFuel() ~= math.huge)
  509. excessFuelAmount = excessFuelAmount or math.huge
  510. addParam("maxFuel", "Max Fuel", "number 1-999999999", nil, checkFuel() ~= math.huge, "excessFuelAmount")
  511. addParam("fuelMultiplier", "Fuel Multiplier", "float 1-9001", nil, checkFuel() ~= math.huge)
  512. paramAlias("fuelMultiplier","fuelRequestMultiplier")
  513. paramAlias("fuelMultiplier","overFuel")
  514.  
  515. addParam("logging", "Logging", "boolean")
  516. addParam("logFolder", "Log Folder", "string")
  517. addParam("logExtension","Log Extension", "string")
  518.  
  519. addParam("startY", "Start Y","number 1-256")
  520. addParam("maxTries","Tries Before Bedrock", "number 1-9001")
  521.  
  522. addParam("keepOpen", "Slots to Keep Open", "number 1-15")
  523. addParam("careAboutResources", "Care About Resources","boolean")
  524. addParam("preciseTotals","Precise Totals","boolean", nil, turtle.getItemDetail ~= nil)
  525. if preciseTotals and not restoreFoundSwitch then
  526. exactTotals = {}
  527. end
  528.  
  529. addParam("autoResume", "Auto Resume", "boolean", nil, doBackup)
  530. paramAlias("autoResume","autoRestart")
  531. addParam("startupRename", "Startup Rename","string", nil, autoResume)
  532. addParam("startupName", "Startup File", "string", nil, autoResume)
  533.  
  534. addParam("oreQuarry", "Ore Quarry", "boolean" )
  535. if oreQuarry and not turtle.inspect then
  536. oldOreQuarry = true
  537. oreQuarry = false
  538. end
  539.  
  540. addParam("oldOreQuarry", "Old Ore Quarry", "boolean")
  541. addParam("dumpCompareItems", "Dump Compare Items", "boolean", nil, oldOreQuarry)
  542. addParam("extraDropItems", "", "force", nil, oldOreQuarry)
  543. paramAlias("extraDropItems","extraDumpItems")
  544.  
  545. addParam("blacklist","Ore Blacklist", "string", nil, oreQuarry, "oreQuarryBlacklistName")
  546. paramAlias("blacklist","blacklistFile")
  547.  
  548.  
  549.  
  550.  
  551. if flatBedrock then
  552. inverted = false
  553. end
  554.  
  555.  
  556. local function doAutoResumeStuff()
  557. if fs.exists(startupName) then
  558. if fs.exists(startupRename) then fs.delete(startupRename) end
  559. fs.move(startupName, startupRename)
  560. end
  561. local file = fs.open(startupName,"w")
  562. file.writeLine(
  563. [[
  564.  
  565.  
  566. print("Now Resuming Quarry")
  567. print("Press any key to quit. You have 5 seconds.")
  568. function deleteStuff()
  569. fs.delete("]]..startupName..[[")
  570. if fs.exists("]]..startupRename..[[") then
  571. fs.move("]]..startupRename.."\",\""..startupName..[[")
  572. end
  573. end
  574. local event
  575. if fs.exists("]]..saveFile..[[") then
  576. for i=5,1,-1 do
  577. print(i)
  578. os.startTimer(1)
  579. event = os.pullEvent()
  580. if event == "key" then break end
  581. end
  582. if event == "timer" then
  583. os.run({},"]]..shell.getRunningProgram()..[[","-resume")
  584. else
  585.  
  586. deleteStuff()
  587. end
  588. else
  589. print("Never mind, no save file found")
  590. deleteStuff()
  591. end
  592. ]])
  593. file.close()
  594. end
  595. if autoResume and not restoreFoundSwitch then
  596. doAutoResumeStuff()
  597. end
  598.  
  599. local blacklist = { "minecraft:air", "minecraft:bedrock", "minecraft:cobblestone", "minecraft:dirt", "minecraft:ice", "minecraft:ladder", "minecraft:netherrack", "minecraft:sand", "minecraft:sandstone",
  600. "minecraft:snow", "minecraft:snow_layer", "minecraft:stone", "minecraft:gravel", "minecraft:grass", "minecraft:torch" }
  601. for a,b in pairs(blacklist) do
  602. blacklist[b], blacklist[b] = true, nil
  603. end
  604. if fs.exists(oreQuarryBlacklistName) then
  605. local file = fs.open(oreQuarryBlacklistName, "r")
  606. blacklist = {}
  607. for a in file:readAll():gmatch("[^,]+") do
  608. blacklist[a:match("%S+:%S+")] = true
  609. end
  610. file:close()
  611. end
  612.  
  613.  
  614. if tArgs["-manualpos"] then
  615. local a = tArgs["-manualpos"]
  616. 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
  617. changedT.new("xPos",xPos); changedT.new("zPos",zPos); changedT.new("yPos",yPos); changedT.new("facing",facing)
  618. restoreFoundSwitch = true
  619. for i=0,4 do tArgs[a+i] = "" end
  620. end
  621. if addParam("atChest", "Is at Chest", "force") then
  622. local neededLayer = math.floor((yPos+1)/3)*3-1
  623. if neededLayer > 2 and neededLayer%3 ~= 2 then
  624. print("Last known pos was not in proper layer, restarting quarry")
  625. sleep(4)
  626. neededLayer = 2
  627. end
  628. if ((neededLayer-2)/3) % 2 == 1 then neededLayer = neededLayer - 3 end
  629. xPos, zPos, yPos, facing, rowCheck, layersDone = 0,1,1, 0, true, math.ceil(neededLayer/3)
  630. doAutoResumeStuff()
  631. events = {{"goto",1,1,neededLayer, 0}}
  632. end
  633.  
  634.  
  635. local function saveProgress(extras)
  636. exclusions = { modem = true, }
  637. if doBackup then
  638. local toWrite = ""
  639. for a,b in pairs(getfenv(1)) do
  640. if not exclusions[a] then
  641.  
  642. if type(b) == "string" then b = "\""..b.."\"" end
  643. if type(b) == "table" then b = textutils.serialize(b) end
  644. if type(b) ~= "function" then
  645. toWrite = toWrite..a.." = "..tostring(b).."\n"
  646. end
  647. end
  648. end
  649. toWrite = toWrite.."doCheckFuel = false\n"
  650. local file
  651. repeat
  652. file = fs.open(saveFile,"w")
  653. until file
  654. file.write(toWrite)
  655. if type(extras) == "table" then
  656. for a, b in pairs(extras) do
  657. file.write(a.." = "..tostring(b).."\n")
  658. end
  659. end
  660. if checkFuel() ~= math.huge then
  661. file.write("fuelLevel = "..tostring(checkFuel()).."\n")
  662. end
  663. file.close()
  664. end
  665. end
  666.  
  667. local area = x*z
  668. local volume = x*y*z
  669. local lastHeight = y%3
  670. layers = math.ceil(y/3)
  671. local yMult = layers
  672. local moveVolume = (area * yMult)
  673.  
  674. do
  675. local changeYFuel = 2*(y + startDown)
  676. local dropOffSupplies = 2*(x + z + y + startDown)
  677. local frequency = math.ceil(((moveVolume/(64*(15-uniqueExtras) + uniqueExtras)) ) )
  678.  
  679. if enderChestEnabled then frequency = 0 end
  680. neededFuel = moveVolume + changeYFuel + (frequency * dropOffSupplies) + ((x + z) * layers)
  681. neededFuel = neededFuel + fuelTable[fuelSafety]
  682. end
  683.  
  684. if neededFuel > checkFuelLimit() and doCheckFuel then
  685. if not (doRefuel or fuelChestEnabled) then
  686. screen()
  687. print("Turtle cannot hold enough fuel\n")
  688. print("Options: \n1. Select a smaller size \n2. Enable Mid-Run Refueling (RECOMMENDED) \n3. Turn fuel checking off (only if fuel chest) \n4. Do nothing")
  689. local _, key = os.pullEvent("char")
  690. if key == "1" then
  691. screen(); print("Okay"); error("",0)
  692. elseif key == "3" then
  693. doCheckFuel = false
  694. elseif key == "4" then
  695.  
  696. else
  697. doRefuel = true
  698. end
  699. end
  700. neededFuel = checkFuelLimit()-checkFuel()-1
  701. end
  702.  
  703.  
  704.  
  705. local hasRefueled
  706. if doCheckFuel and checkFuel() < neededFuel then
  707. neededFuel = math.min(math.floor(neededFuel * fuelMultiplier), checkFuelLimit()-checkFuel()-1)
  708. hasRefueled = true
  709. print("Not enough fuel")
  710. print("Current: ",checkFuel()," Needed: ",neededFuel)
  711. print("Starting SmartFuel...")
  712. sleep(2)
  713. term.clear()
  714. local oneFuel, neededFuelItems = 0,0
  715. local currSlot = 0
  716. local function output(text, x, y)
  717. local currX, currY = term.getCursorPos()
  718. term.setCursorPos(x,y)
  719. term.clearLine()
  720. term.write(text)
  721. term.setCursorPos(currX,currY)
  722. end
  723. local function roundTo(num, target)
  724. if num >= target then return target elseif num < 0 then return 0 else return num end
  725. end
  726. local function updateScreen()
  727. output("Welcome to SmartFuel! Now Refueling...", 1,1)
  728. output("Fuel Request Multiplier: "..tostring(fuelMultiplier).."x",1,2)
  729. output("Currently taking fuel from slot "..currSlot,1,3)
  730. output("Current single fuel: "..tostring(oneFuel or 0),1,4)
  731. output("Current estimate of needed fuel: ",1,4)
  732. output("Single Items: "..math.ceil(neededFuelItems),4,6)
  733. output("Stacks: "..math.ceil(neededFuelItems / 64),4,7)
  734. output("Needed Fuel: "..tostring(neededFuel),1,12)
  735. output("Current Fuel: "..tostring(checkFuel()),1,13)
  736. end
  737. while checkFuel() < neededFuel do
  738. currSlot = currSlot + 1
  739. select(currSlot)
  740. if currSlot ~= 1 and not turtle.refuel(0) then
  741. currSlot = 1; select(currSlot)
  742. end
  743. updateScreen()
  744. while turtle.getItemCount(currSlot) == 0 do
  745. sleep(1.5)
  746. end
  747. repeat
  748. local previous = checkFuel()
  749. turtle.refuel(1)
  750. oneFuel = checkFuel() - previous
  751. updateScreen()
  752. until (oneFuel or 0) > 0
  753. neededFuelItems = math.ceil((neededFuel - checkFuel()) / oneFuel)
  754. turtle.refuel(roundTo(neededFuelItems, 64))
  755. if turtle.getItemCount(roundTo(currSlot + 1, inventoryMax)) == 0 then
  756. currSlot = 0
  757. end
  758. neededFuelItems = math.ceil((neededFuel - checkFuel()) / oneFuel)
  759. end
  760. select(1)
  761. end
  762.  
  763. function promptSpecialSlot(specialSlot, name)
  764. while turtle.getItemCount(specialSlots[specialSlot]) ~= 1 do
  765. screen(1,1)
  766. print("You have decided to use a ",name,"!")
  767. print("Please place one ",name," in slot ",specialSlots[specialSlot])
  768. sleep(1)
  769. end
  770. print(name," in slot ",specialSlots[specialSlot], " checks out")
  771. end
  772. function checkSpecialSlot(specialSlot, name)
  773. if restoreFoundSwitch and turtle.getItemCount(specialSlots[specialSlot]) == 0 then
  774. select(specialSlots[specialSlot])
  775. turtle.dig()
  776. select(1)
  777. end
  778. promptSpecialSlot(specialSlot, name)
  779. allowedItems[specialSlots[specialSlot]] = 1
  780. sleep(2)
  781. end
  782. if enderChestEnabled then
  783. checkSpecialSlot("enderChest","Ender Chest")
  784. end
  785. if fuelChestEnabled then
  786. checkSpecialSlot("fuelChest","Fuel Chest")
  787. end
  788.  
  789.  
  790.  
  791. if oldOreQuarry then
  792. if not restoreFoundSwitch then
  793. local counter = 0
  794. for i=1, inventoryMax do if turtle.getItemCount(i) > 0 and i ~= specialSlots.enderChest then counter = counter+1 end end
  795.  
  796. screen(1,1)
  797. print("You have selected an Ore Quarry!")
  798. if counter == 0 or hasRefueled then
  799. print("Please place your compare blocks in the first slots\n")
  800.  
  801. print("Press Enter when done")
  802. repeat until ({os.pullEvent("key")})[2] == 28
  803. else
  804. print("Registering slots as compare slots")
  805. sleep(1)
  806. end
  807. for i=1, inventoryMax do
  808. if turtle.getItemCount(i) > 0 then
  809. if i ~= specialSlots.enderChest then
  810. table.insert(compareSlots, i)
  811. allowedItems[i] = 1
  812. dumpSlots[i] = true
  813. end
  814. end
  815. end
  816. if extraDropItems then
  817. screen(1,1)
  818. print("Put in extra drop items now\n")
  819. print("Press Enter when done")
  820. repeat until ({os.pullEvent("key")})[2] == 28
  821. for i=1,inventoryMax do
  822. if not dumpSlots[i] and turtle.getItemCount(i) > 0 then
  823. dumpSlots[i] = true
  824. allowedItems[i] = 1
  825. end
  826. end
  827. end
  828.  
  829. if #compareSlots >= inventoryMax-keepOpen then screen(1,1); error("You have more quarry compare items than keep open slots, the turtle will continuously come back to start. Please fix.",0) end
  830. end
  831. local counter = 0
  832. for a, b in pairs(compareSlots) do if turtle.getItemCount(b) > 0 then counter = counter + 1 end end
  833. if counter == 0 then
  834. screen(1,1)
  835. print("You have an ore quarry without any compare slots. Continue? y/n")
  836. if ({os.pullEvent("char")})[2] ~= "y" then error("",0) end
  837. end
  838. elseif not oreQuarry then
  839. dumpCompareItems = false
  840. if specialSlots.enderChest == 1 then
  841. dumpSlots[2] = true
  842. else
  843. dumpSlots[1] = true
  844. end
  845. end
  846.  
  847.  
  848. function newMessageID()
  849. return math.random(1,2000000000)
  850. end
  851. function sendMessage(send, receive, message)
  852. if legacyRednet then
  853. if type(message) == "table" then message = textutils.serialize(message) end
  854. return modem.transmit(send, receive, message)
  855. end
  856. return modem.transmit(send , receive, {fingerprint = channels.fingerprint, id = newMessageID(), message = message})
  857. end
  858. if rednetEnabled then
  859. screen(1,1)
  860. print("Rednet is Enabled")
  861. print("The Channel to open is "..channels.send)
  862. if peripheral.find then
  863. modem = peripheral.find("modem")
  864. else
  865. modem = peripheral.wrap("right")
  866. end
  867. modem.open(channels.receive)
  868. local i = 0
  869. repeat
  870. local id = os.startTimer(3)
  871. i=i+1
  872. print("Sending Initial Message "..i)
  873. sendMessage(channels.send, channels.receive, channels.message)
  874. local message = {}
  875. repeat
  876. local event, idCheck, channel,_,locMessage, distance = os.pullEvent()
  877. if locMessage then message = locMessage end
  878. if legacyRednet then
  879. message = {message = message}
  880. end
  881. until (event == "timer" and idCheck == id) or (event == "modem_message" and channel == channels.receive and type(message) == "table")
  882. until message.message == channels.confirm
  883. connected = true
  884. print("Connection Confirmed!")
  885. sleep(1.5)
  886. end
  887. function biometrics(isAtBedrock, requestQuad)
  888. if not rednetEnabled then return end
  889. local toSend = { label = os.getComputerLabel() or "No Label", id = os.getComputerID(),
  890. percent = percent, zPos = relzPos, xPos = relxPos, yPos = yPos,
  891. layersDone = layersDone, x = x, z = z, layers = layers,
  892. openSlots = getNumOpenSlots(), mined = mined, moved = moved,
  893. chestFull = chestFull, isAtChest = (xPos == 0 and yPos == 1 and zPos == 1),
  894. isGoingToNextLayer = (gotoDest == "layerStart"), foundBedrock = foundBedrock,
  895. fuel = checkFuel(), volume = volume, status = statusString,
  896. }
  897. if requestQuad and isInPath then
  898. if not gps.locate(gpsTimeout) then
  899. print("\nOH NOES! Trying to reach quadrotor, but can't get GPS position!")
  900. sleep(1)
  901. else
  902. toSend.firstPos = gpsStartPos
  903. toSend.secondPos = gpsSecondPos
  904. toSend.emergencyLocation = {gps.locate(gpsTimeout)}
  905. end
  906. end
  907. sendMessage(channels.send, channels.receive, toSend)
  908. id = os.startTimer(0.1)
  909. local event, received
  910. repeat
  911. local locEvent, idCheck, confirm, _, locMessage, distance = os.pullEvent()
  912. event, received = locEvent, locMessage or {message = ""}
  913. if legacyRednet and type(received) == "string" then
  914. received = {message = received}
  915. end
  916. until (event == "timer" and idCheck == id) or (event == "modem_message" and confirm == channels.receive and type(received) == "table")
  917. if event == "modem_message" then connected = true else connected = false end
  918. local message = received.message:lower()
  919. if message == "stop" or message == "quit" or message == "kill" then error("Rednet said to stop...",0) end
  920. if message == "return" then
  921. endingProcedure()
  922. error('Rednet said go back to start...',0)
  923. end
  924. if message == "drop" then
  925. dropOff()
  926. end
  927. if message == "pause" then
  928. print("\nTurtle is paused. Send 'resume' or press any character to resume")
  929. statusString = "Paused"
  930. repeat
  931. sleep(1)
  932. sendMessage(channels.send, channels.receive, toSend)
  933. local event, idCheck, confirm, _, message, distance = os.pullEvent()
  934. until (event == "modem_message" and confirm == channels.receive and (message.message == "resume" or message.message == "unpause" or message.message == "pause")) or (event == "char")
  935. statusString = nil
  936. end
  937. if message == "refuel" then
  938. print("\nEngaging in emergency refueling")
  939. emergencyRefuel()
  940. end
  941.  
  942. end
  943.  
  944. screen(1,1)
  945. print("Your selected settings:")
  946. if #changedT == 0 then
  947. print("Completely Default")
  948. else
  949. for i=1, #changedT do
  950. print(changedT[i][1],": ",changedT[i][2])
  951. end
  952. end
  953. print("\nStarting in 3"); sleep(1); print("2"); sleep(1); print("1"); sleep(1.5)
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960. function eventAddAt(pos, ...)
  961. return table.insert(events,pos, {...}) or true
  962. end
  963. function eventAdd(...)
  964. return eventAddAt(1, ...)
  965. end
  966. function eventGet(pos)
  967. return events[tonumber(pos) or #events]
  968. end
  969. function eventPop(pos)
  970. return table.remove(events,tonumber(pos) or #events) or false
  971. end
  972. function eventRun(value, ...)
  973. local argsList = {...}
  974. if type(value) == "string" then
  975. if value:sub(-1) ~= ")" then
  976. value = value .. "("
  977. for a, b in pairs(argsList) do
  978. local toAppend
  979. if type(b) == "table" then toAppend = textutils.serialize(b)
  980. elseif type(b) == "string" then toAppend = "\""..tostring(b).."\""
  981. else toAppend = tostring(b) end
  982. value = value .. (toAppend or "true") .. ", "
  983. end
  984. if value:sub(-1) ~= "(" then
  985. value = value:sub(1,-3)..""
  986. end
  987. value = value .. ")"
  988. end
  989.  
  990. local func = loadstring(value)
  991. setfenv(func, getfenv(1))
  992. return func()
  993. end
  994. end
  995. function eventClear(pos)
  996. if pos then events[pos] = nil else events = {} end
  997. end
  998. function runAllEvents()
  999. while #events > 0 do
  1000. local toRun = eventGet()
  1001.  
  1002. eventRun(unpack(toRun))
  1003. eventPop()
  1004. end
  1005. end
  1006.  
  1007.  
  1008. function display()
  1009. screen(1,1)
  1010. print("Total Blocks Mined: "..mined)
  1011. print("Current Fuel Level: "..checkFuel())
  1012. print("Cobble: "..totals.cobble)
  1013. print("Usable Fuel: "..totals.fuel)
  1014. print("Other: "..totals.other)
  1015. if rednetEnabled then
  1016. print("")
  1017. print("Sent Stop Message")
  1018. if legacyRednet then
  1019. print("Sent Legacy Stop")
  1020. sendMessage(channels.send, channels.receive, "stop")
  1021. end
  1022. local finalTable = {mined = mined, cobble = totals.cobble, fuelblocks = totals.fuel,
  1023. other = totals.other, fuel = checkFuel(), isDone = true }
  1024. sendMessage(channels.send,channels.receive, finalTable)
  1025. modem.close(channels.receive)
  1026. end
  1027. if doBackup then
  1028. fs.delete(saveFile)
  1029. if autoResume then
  1030. fs.delete(startupName)
  1031. if fs.exists(startupRename) then
  1032. fs.move(startupRename, startupName)
  1033. end
  1034. end
  1035. end
  1036. end
  1037. function updateDisplay()
  1038. screen(1,1)
  1039. print("Blocks Mined")
  1040. print(mined)
  1041. print("Percent Complete")
  1042. print(percent.."%")
  1043. print("Fuel")
  1044. print(checkFuel())
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054. if rednetEnabled then
  1055. screenLine(1,7)
  1056. print("Connected: "..tostring(connected))
  1057. end
  1058. end
  1059.  
  1060. local function pad(str, length, side)
  1061. toRet = ""
  1062. if side == "right" then
  1063. toRet = str
  1064. end
  1065. for i=1, length do
  1066. toRet = toRet.." "
  1067. end
  1068. if side == "left" then
  1069. toRet = toRet..str
  1070. end
  1071. return toRet
  1072. end
  1073. function logMiningRun(textExtension, extras)
  1074. if not logging then return end
  1075. local number, name = 0
  1076. if not fs.isDir(logFolder) then
  1077. fs.delete(logFolder)
  1078. fs.makeDir(logFolder)
  1079. end
  1080. repeat
  1081. number = number + 1
  1082. name = logFolder.."/Quarry_Log_"..tostring(number)..(textExtension or "")
  1083. until not fs.exists(name)
  1084. local handle = fs.open(name,"w")
  1085. local function write(...)
  1086. for a, b in ipairs({...}) do
  1087. handle.write(tostring(b))
  1088. end
  1089. handle.write("\n")
  1090. end
  1091. local function boolToText(bool) if bool then return "Yes" else return "No" end end
  1092. write("Welcome to the Quarry Logs!")
  1093. write("Entry Number: ",number)
  1094. write("Quarry Version: ",VERSION)
  1095. write("Dimensions (X Z Y): ",x," ",z," ", y)
  1096. write("Blocks Mined: ", mined)
  1097. write(" Cobble: ", totals.cobble)
  1098. write(" Usable Fuel: ", totals.fuel)
  1099. write(" Other: ",totals.other)
  1100. write("Total Fuel Used: ", (originalFuel or (neededFuel + checkFuel()))- checkFuel())
  1101. write("Expected Fuel Use: ", neededFuel)
  1102. write("Days to complete mining run: ",os.day()-originalDay)
  1103. write("Day Started: ", originalDay)
  1104. write("Number of times resumed: ", numResumed)
  1105. write("Was an ore quarry? ",boolToText(oreQuarry or oldOreQuarry))
  1106. write("Was inverted? ",boolToText(invert))
  1107. write("Was using rednet? ",boolToText(rednetEnabled))
  1108. write("Chest was on the ",dropSide," side")
  1109. if startDown > 0 then write("Started ",startDown," blocks down") end
  1110. if exactTotals then
  1111. write("\n==DETAILED TOTALS==")
  1112. for a,b in pairs(exactTotals) do
  1113. write(pad(a, 15, "right"),":",pad(tostring(b),({term.getSize()})[1]-15-1, "left"))
  1114. end
  1115. end
  1116. handle.close()
  1117. end
  1118.  
  1119. function isFull(slots)
  1120. slots = slots or inventoryMax
  1121. local numUsed = 0
  1122. sleep(0)
  1123. for i=1, inventoryMax do
  1124. if turtle.getItemCount(i) > 0 then numUsed = numUsed + 1 end
  1125. end
  1126. if numUsed > slots then
  1127. return true
  1128. end
  1129. return false
  1130. end
  1131. function countUsedSlots()
  1132. local toRet, toRetTab = 0, {}
  1133. for i=1, inventoryMax do
  1134. local a = turtle.getItemCount(i)
  1135. if a > 0 then toRet = toRet + 1 end
  1136. table.insert(toRetTab, a)
  1137. end
  1138. return toRet, toRetTab
  1139. end
  1140. function getSlotsTable()
  1141. local _, toRet = countUsedSlots()
  1142. return toRet
  1143. end
  1144. function getChangedSlots(tab1, tab2)
  1145. local toRet = {}
  1146. for i=1, math.min(#tab1, #tab2) do
  1147. diff = math.abs(tab2[i]-tab1[i])
  1148. if diff > 0 then
  1149. table.insert(toRet, {i, diff})
  1150. end
  1151. end
  1152. return toRet
  1153. end
  1154. function getFirstChanged(tab1, tab2)
  1155. local a = getChangedSlots(tab1,tab2)
  1156. return (a[1] or {"none"})[1]
  1157. end
  1158.  
  1159. function getRep(which, list)
  1160. for a,b in pairs(list) do
  1161. if b == which then return a end
  1162. end
  1163. return false
  1164. end
  1165. function assignTypes(types, count)
  1166. types, count = types or {1}, count or 1
  1167. for i=1, inventoryMax do
  1168. if turtle.getItemCount(i) > 0 and not specialSlots[i] then
  1169. select(i)
  1170. for k=1, count do
  1171. if turtle.compareTo(getRep(k, types)) then types[i] = k end
  1172. end
  1173. if not types[i] then
  1174. count = count + 1
  1175. types[i] = count
  1176. end
  1177. if oreQuarry then
  1178. if blacklist[turtle.getItemDetail().name] then
  1179. dumpSlots[i] = true
  1180. else
  1181. dumpSlots[i] = false
  1182. end
  1183. end
  1184. end
  1185. end
  1186. select(1)
  1187. return types, count
  1188. end
  1189. function getTableOfType(which, list)
  1190. local toRet = {}
  1191. for a, b in pairs(list) do
  1192. if b == which then
  1193. table.insert(toRet, a)
  1194. end
  1195. end
  1196. return toRet
  1197. end
  1198.  
  1199.  
  1200. if not restoreFoundSwitch then
  1201. if oldOreQuarry then
  1202. initialTypes, initialCount = assignTypes()
  1203. else
  1204. initialTypes, initialCount = {1}, 1
  1205. end
  1206. end
  1207.  
  1208. function count(add)
  1209. local mod = -1
  1210. if add then mod = 1 end
  1211. if add == false then mod = 0 end
  1212. slot = {}
  1213. for i=1, inventoryMax do
  1214. slot[i] = {}
  1215. slot[i][2] = turtle.getItemCount(i)
  1216. end
  1217.  
  1218. local function iterate(toSet , rawTypes, set)
  1219. for _, a in pairs(getTableOfType(toSet, rawTypes)) do
  1220. slot[a][1] = set
  1221. end
  1222. end
  1223.  
  1224.  
  1225. local rawTypes, numTypes = assignTypes(copyTable(initialTypes), initialCount)
  1226.  
  1227. for i=1, numTypes do
  1228. if (select(getRep(i, rawTypes)) or true) and turtle.refuel(0) then
  1229. iterate(i, rawTypes, 2)
  1230. elseif dumpSlots[getRep(i,(oreQuarry and rawTypes) or initialTypes)] then
  1231. iterate(i, rawTypes, 1)
  1232. else
  1233. iterate(i, rawTypes, 3)
  1234. end
  1235. end
  1236.  
  1237. for i=1,inventoryMax do
  1238. if not specialSlots[i] then
  1239. if exactTotals then
  1240. local data = turtle.getItemDetail()
  1241. exactTotals[data.name] = (exactTotals[data.name] or 0) + data.count
  1242. end
  1243. if slot[i][1] == 1 then totals.cobble = totals.cobble + (slot[i][2] * mod)
  1244. elseif slot[i][1] == 2 then totals.fuel = totals.fuel + (slot[i][2] * mod)
  1245. elseif slot[i][1] == 3 then totals.other = totals.other + (slot[i][2] * mod) end
  1246. end
  1247. end
  1248.  
  1249. select(1)
  1250. end
  1251.  
  1252.  
  1253. function dig(doAdd, mineFunc, inspectFunc)
  1254. if doAdd == nil then doAdd = true end
  1255. mineFunc = mineFunc or turtle.dig
  1256. local function retTab(tab) if type(tab) == "table" then return tab end end
  1257.  
  1258. if not oreQuarry or not inspectFunc or not blacklist[(retTab(({inspectFunc()})[2]) or {name = "none"}).name] then
  1259. if mineFunc() then
  1260. if doAdd then
  1261. mined = mined + 1
  1262. end
  1263. return true
  1264. else
  1265. return false
  1266. end
  1267. end
  1268. return true
  1269. end
  1270.  
  1271.  
  1272. function emergencyRefuel()
  1273. local continueEvac = true
  1274. if fuelChestEnabled then
  1275. if not fuelChestPhase then
  1276. fuelChestPhase = 0
  1277. fuelChestProperFacing = facing
  1278. end
  1279. if fuelChestPhase == 0 then
  1280. turnTo(coterminal(fuelChestProperFacing+2))
  1281. dig(false)
  1282. fuelChestPhase = 1
  1283. saveProgress()
  1284. end
  1285. if fuelChestPhase == 1 then
  1286. select(specialSlots.fuelChest)
  1287. turtle.place()
  1288. fuelChestPhase = 2
  1289. saveProgress()
  1290. end
  1291. if fuelChestPhase == 2 then
  1292. if not enderRefuel() then
  1293. select(specialSlots.fuelChest)
  1294. turtle.drop()
  1295. end
  1296. fuelChestPhase = 3
  1297. saveProgress()
  1298. end
  1299. if fuelChestPhase == 3 then
  1300. select(specialSlots.fuelChest)
  1301. dig(false)
  1302. select(1)
  1303. fuelChestPhase = 4
  1304. saveProgress()
  1305. end
  1306. if fuelChestPhase == 4 then
  1307. turnTo(fuelChestProperFacing)
  1308. fuelChestProperFacing = nil
  1309. fuelChestPhase = nil
  1310. continueEvac = false
  1311. end
  1312. elseif quadEnabled then
  1313. screen()
  1314. print("Attempting an emergency Quad Rotor refuel")
  1315. print("The turtle will soon send a message, then wait ",quadTimeout," seconds before moving on")
  1316. print("Press any key to break timer")
  1317. biometrics(nil, true)
  1318. local timer, counter, counterID, event, id = os.startTimer(quadTimeout), 0, os.startTimer(1)
  1319. local startInventory = getSlotsTable()
  1320. repeat
  1321. if id == counterID then counter = counter + 1; counterID = os.startTimer(1) end
  1322. screenLine(1,6)
  1323. print("Seconds elapsed: ",counter)
  1324. event, id = os.pullEvent()
  1325. until (event == "timer" and id == timer) or event == "key" or event == "turtle_inventory"
  1326. if event == "turtle_inventory" then
  1327. local slot = getFirstChanged(startInventory, getSlotsTable())
  1328. select(slot)
  1329. local initialFuel = checkFuel()
  1330. midRunRefuel(slot)
  1331. if checkFuel() > initialFuel then
  1332. print("Fuel delivered! Evac aborted")
  1333. continueEvac = false
  1334. else
  1335. print("What did you send the turtle? Not fuel >:(")
  1336. print("Continuing evac")
  1337. end
  1338. sleep(1)
  1339. end
  1340. elseif doRefuel then
  1341. screen()
  1342. print("Attempting an emergency refuel")
  1343. print("Fuel Level: ",checkFuel())
  1344. print("Distance Back: ",(xPos+zPos+yPos+1))
  1345. print("Categorizing Items")
  1346. count(false)
  1347. local fuelSwitch, initialFuel = false, checkFuel()
  1348. print("Going through available fuel slots")
  1349. for i=1, inventoryMax do
  1350. if fuelSwitch then break end
  1351. if turtle.getItemCount(i) > 0 and slot[i][1] == 2 then
  1352. select(i)
  1353. fuelSwitch = midRunRefuel(i)
  1354. end
  1355. end
  1356. select(1)
  1357. print("Done fueling")
  1358. if checkFuel() > initialFuel then
  1359. continueEvac = false
  1360. print("Evac Aborted")
  1361. else
  1362. print("Evac is a go, returning to base")
  1363. sleep(1.5)
  1364. end
  1365. end
  1366. return continueEvac
  1367. end
  1368.  
  1369.  
  1370. function digUp(doAdd, ignoreInspect)
  1371. return dig(doAdd, turtle.digUp, (not ignoreInspect and turtle.inspectUp) or nil)
  1372. end
  1373. function digDown(doAdd, ignoreInspect)
  1374. return dig(doAdd, turtle.digDown, (not ignoreInspect and turtle.inspectDown) or nil)
  1375. end
  1376. if inverted then
  1377. digUp, digDown = digDown, digUp
  1378. end
  1379.  
  1380. function smartDig(doDigUp, doDigDown)
  1381. if inverted then doDigUp, doDigDown = doDigDown, doDigUp end
  1382. local blockAbove, blockBelow = doDigUp and turtle.detectUp(), doDigDown and turtle.detectDown()
  1383. local index = 1
  1384. for i=1, #compareSlots do
  1385. if not (blockAbove or blockBelow) then break end
  1386. index = i
  1387. select(compareSlots[i])
  1388. if blockAbove and turtle.compareUp() then blockAbove = false end
  1389. if blockBelow and turtle.compareDown() then blockBelow = false end
  1390. end
  1391. table.insert(compareSlots, 1, table.remove(compareSlots, index))
  1392. if blockAbove then dig(true, turtle.digUp) end
  1393. if blockBelow then dig(true, turtle.digDown) end
  1394. end
  1395.  
  1396. function relxCalc()
  1397. if layersDone % 2 == 1 then
  1398. relzPos = zPos
  1399. else
  1400. relzPos = (z-zPos) + 1
  1401. end
  1402. if relzPos % 2 == 1 then
  1403. relxPos = xPos
  1404. else
  1405. relxPos = (x-xPos)+1
  1406. end
  1407. if layersDone % 2 == 0 and z % 2 == 1 then
  1408. relxPos = (x-relxPos)+1
  1409. end
  1410. end
  1411. function horizontalMove(movement, posAdd, doAdd)
  1412. if doAdd == nil then doAdd = true end
  1413. if movement() then
  1414. if doAdd then
  1415. moved = moved + 1
  1416. end
  1417. if facing == 0 then
  1418. xPos = xPos + 1
  1419. elseif facing == 1 then
  1420. zPos = zPos + 1
  1421. elseif facing == 2 then
  1422. xPos = xPos - 1
  1423. elseif facing == 3 then
  1424. zPos = zPos - 1
  1425. else
  1426. error("Function forward, facing should be 0 - 3, got "..tostring(facing),2)
  1427. end
  1428. relxCalc()
  1429. return true
  1430. end
  1431. return false
  1432. end
  1433. function forward(doAdd)
  1434. return horizontalMove(turtle.forward, 1, doAdd)
  1435. end
  1436. function back(doAdd)
  1437. return horizontalMove(turtle.back, -1, doAdd)
  1438. end
  1439. function verticalMove(moveFunc, yDiff, digFunc, attackFunc)
  1440. local count = 0
  1441. while not moveFunc() do
  1442. if not digFunc(true, true) then
  1443. attackFunc()
  1444. sleep(0.5)
  1445. count = count + 1
  1446. if count > maxTries and yPos > (startY-7) then bedrock() end
  1447. end
  1448. end
  1449. yPos = yDiff + yPos
  1450. saveProgress()
  1451. biometrics()
  1452. return true
  1453. end
  1454. function up()
  1455. verticalMove(inverted and turtle.down or turtle.up, -1, digUp, attackUp)
  1456. end
  1457. function down()
  1458. verticalMove(inverted and turtle.up or turtle.down, 1, digDown, attackDown)
  1459. end
  1460.  
  1461.  
  1462. function right(num)
  1463. num = num or 1
  1464. for i=1, num do
  1465. facing = coterminal(facing+1)
  1466. saveProgress()
  1467. if not goLeftNotRight then turtle.turnRight()
  1468. else turtle.turnLeft() end
  1469. end
  1470. end
  1471. function left(num)
  1472. num = num or 1
  1473. for i=1, num do
  1474. facing = coterminal(facing-1)
  1475. saveProgress()
  1476. if not goLeftNotRight then turtle.turnLeft()
  1477. else turtle.turnRight() end
  1478. end
  1479. end
  1480.  
  1481. function attack(doAdd, func)
  1482. doAdd = doAdd or true
  1483. func = func or turtle.attack
  1484. if func() then
  1485. if doAdd then
  1486. attacked = attacked + 1
  1487. end
  1488. return true
  1489. end
  1490. return false
  1491. end
  1492. function attackUp(doAdd)
  1493. if inverted then
  1494. return attack(doAdd, turtle.attackDown)
  1495. else
  1496. return attack(doAdd, turtle.attackUp)
  1497. end
  1498. end
  1499. function attackDown(doAdd)
  1500. if inverted then
  1501. return attack(doAdd, turtle.attackUp)
  1502. else
  1503. return attack(doAdd, turtle.attackDown)
  1504. end
  1505. end
  1506.  
  1507. function detect(func)
  1508. func = func or turtle.detect
  1509. return func()
  1510. end
  1511. function detectUp(ignoreInvert)
  1512. if inverted and not ignoreInvert then return detect(turtle.detectDown)
  1513. else return detect(turtle.detectUp) end
  1514. end
  1515. function detectDown(ignoreInvert)
  1516. if inverted and not ignoreInvert then return detect(turtle.detectUp)
  1517. else return detect(turtle.detectDown) end
  1518. end
  1519.  
  1520.  
  1521.  
  1522. function mine(doDigDown, doDigUp, outOfPath,doCheckInv)
  1523. if doCheckInv == nil then doCheckInv = true end
  1524. if doDigDown == nil then doDigDown = true end
  1525. if doDigUp == nil then doDigUp = true end
  1526. if outOfPath == nil then outOfPath = false end
  1527. isInPath = (not outOfPath)
  1528. if not outOfPath and (checkFuel() <= xPos + zPos + yPos + 5) then
  1529. local continueEvac = false
  1530. if xPos ~= 0 then
  1531. continueEvac = emergencyRefuel()
  1532. end
  1533. if continueEvac then
  1534. eventClear()
  1535. local currPos = yPos
  1536. endingProcedure()
  1537. print("Turtle ran low on fuel so was brought back to start for you :)\n\nTo resume where you left off, use '-startDown "..tostring(currPos-1).."' when you start")
  1538. error("",0)
  1539. end
  1540. end
  1541. local count = 0
  1542. if not outOfPath then dig() end
  1543. while not forward(not outOfPath) do
  1544. sleep(0)
  1545. count = count + 1
  1546. if not dig() then
  1547. attack()
  1548. end
  1549. if count > 10 then
  1550. attack()
  1551. sleep(0.2)
  1552. end
  1553. if count > maxTries then
  1554. if checkFuel() == 0 then
  1555. saveProgress({doCheckFuel = true, doRefuel = true})
  1556. os.reboot()
  1557. elseif yPos > (startY-7) and turtle.detect() then
  1558. bedrock()
  1559. else
  1560. sleep(1)
  1561. end
  1562. end
  1563. end
  1564. checkSanity()
  1565. saveProgress(tab)
  1566.  
  1567. if not oldOreQuarry then
  1568. if doDigUp then
  1569. sleep(0)
  1570. if not digUp(true) and detectUp() then
  1571. if not attackUp() then
  1572. if yPos > (startY-7) then bedrock() end
  1573. end
  1574. end
  1575. end
  1576. if doDigDown then
  1577. digDown(true)
  1578. end
  1579. else
  1580. smartDig(doDigUp,doDigDown)
  1581. end
  1582. percent = math.ceil(moved/moveVolume*100)
  1583. updateDisplay()
  1584. if doCheckInv and careAboutResources then
  1585. if isFull(inventoryMax-keepOpen) then
  1586. if not ((oreQuarry or oldOreQuarry) and dumpCompareItems) then
  1587. dropOff()
  1588. else
  1589. local currInv = getSlotsTable()
  1590. drop(nil, false, true)
  1591. if #getChangedSlots(currInv, getSlotsTable()) <= 2 then
  1592. dropOff()
  1593. end
  1594. end
  1595. end
  1596. end
  1597. biometrics()
  1598. end
  1599.  
  1600. function checkSanity()
  1601. if not isInPath then
  1602. return true
  1603. end
  1604. if not (facing == 0 or facing == 2) and #events == 0 then
  1605. turnTo(0)
  1606. rowCheck = true
  1607. end
  1608. if xPos < 0 or xPos > x or zPos < 0 or zPos > z or yPos < 0 then
  1609. saveProgress()
  1610. print("I have gone outside boundaries, attempting to fix (maybe)")
  1611. if xPos > x then goto(x, zPos, yPos, 2) end
  1612. if xPos < 0 then goto(1, zPos, yPos, 0) end
  1613. if zPos > z then goto(xPos, z, yPos, 3) end
  1614. if zPos < 0 then goto(xPos, 1, yPos, 1) end
  1615. relxCalc()
  1616. eventClear()
  1617. end
  1618. end
  1619.  
  1620. local function fromBoolean(input)
  1621. if input then return 1 end
  1622. return 0
  1623. end
  1624. local function multBoolean(first,second)
  1625. return (fromBoolean(first) * fromBoolean(second)) == 1
  1626. end
  1627. function coterminal(num, limit)
  1628. limit = limit or 4
  1629. return math.abs((limit*fromBoolean(num < 0))-(math.abs(num)%limit))
  1630. end
  1631. if tArgs["-manualpos"] then
  1632. facing = coterminal(facing)
  1633. if facing == 0 then rowCheck = true elseif facing == 2 then rowCheck = false end
  1634. relxCalc()
  1635. end
  1636.  
  1637.  
  1638. function turnTo(num)
  1639. num = num or facing
  1640. num = coterminal(num)
  1641. local turnRight = true
  1642. if facing-num == 1 or facing-num == -3 then turnRight = false end
  1643. while facing ~= num do
  1644. if turnRight then
  1645. right()
  1646. else
  1647. left()
  1648. end
  1649. end
  1650. end
  1651. function goto(x,z,y, toFace, destination, updateStatus)
  1652.  
  1653. x = x or 1; y = y or 1; z = z or 1; toFace = toFace or facing
  1654. gotoDest = destination or ""
  1655. statusString = "Going somewhere"
  1656.  
  1657. if yPos > y then
  1658. while yPos~=y do up() end
  1659. end
  1660. if zPos > z then
  1661. turnTo(3)
  1662. elseif zPos < z then
  1663. turnTo(1)
  1664. end
  1665. while zPos ~= z do mine(false,false,true,false) end
  1666. if xPos > x then
  1667. turnTo(2)
  1668. elseif xPos < x then
  1669. turnTo(0)
  1670. end
  1671. while xPos ~= x do mine(false,false,true,false) end
  1672. if yPos < y then
  1673. while yPos~=y do down() end
  1674. end
  1675. turnTo(toFace)
  1676. saveProgress()
  1677. gotoDest = ""
  1678. statusString = nil
  1679. end
  1680. function getNumOpenSlots()
  1681. local toRet = 0
  1682. for i=1, inventoryMax do
  1683. if turtle.getItemCount(i) == 0 then
  1684. toRet = toRet + 1
  1685. end
  1686. end
  1687. return toRet
  1688. end
  1689.  
  1690.  
  1691. local function waitDrop(slot, allowed, whereDrop)
  1692. allowed = allowed or 0
  1693. while turtle.getItemCount(slot) > allowed do
  1694. local tries = 1
  1695. while not whereDrop(turtle.getItemCount(slot)-allowed) do
  1696. screen(1,1)
  1697. print("Chest Full, Try "..tries)
  1698. chestFull = true
  1699. biometrics()
  1700. tries = tries + 1
  1701. sleep(2)
  1702. end
  1703. chestFull = false
  1704. end
  1705. end
  1706.  
  1707. function midRunRefuel(i, allowed)
  1708. allowed = allowed or allowedItems[i]
  1709. local numToRefuel = turtle.getItemCount(i)-allowed
  1710. if checkFuel() >= checkFuelLimit() then return true end
  1711. local firstCheck = checkFuel()
  1712. if numToRefuel > 0 then turtle.refuel(1) else return false end
  1713. local singleFuel
  1714. if checkFuel() - firstCheck > 0 then singleFuel = checkFuel() - firstCheck else singleFuel = math.huge end
  1715.  
  1716. turtle.refuel(math.min(numToRefuel-1, math.ceil((checkFuelLimit()-checkFuel()) / singleFuel)))
  1717. if checkFuel() >= checkFuelLimit() then return true end
  1718. return false
  1719. end
  1720.  
  1721. function enderRefuel()
  1722. local slot
  1723. for a,b in ipairs(getSlotsTable()) do
  1724. if b == 0 then slot = a; break end
  1725. end
  1726. if not slot then return false end
  1727. select(slot)
  1728. repeat
  1729. print("Required Fuel: ",excessFuelAmount)
  1730. print("Current Fuel: ",checkFuel())
  1731. local tries = 0
  1732. while not turtle.suck() do
  1733. sleep(1)
  1734. statusString = "No Fuel in Ender Chest"
  1735. biometrics()
  1736. print(statusString,". Try: ",tries)
  1737. tries = tries + 1
  1738. end
  1739. statusString = nil
  1740. until midRunRefuel(slot, 0)
  1741. if not turtle.drop() then turtle.dropDown() end
  1742. return true
  1743. end
  1744.  
  1745.  
  1746. function drop(side, final, compareDump)
  1747. side = sides[side] or "front"
  1748. local dropFunc, detectFunc, dropFacing = turtle.drop, turtle.detect, facing+2
  1749. if side == "top" then dropFunc, detectFunc = turtle.dropUp, turtle.detectUp end
  1750. if side == "bottom" then dropFunc, detectFunc = turtle.dropDown, turtle.detectDown end
  1751. if side == "right" then turnTo(1); dropFacing = 0 end
  1752. if side == "left" then turnTo(3); dropFacing = 0 end
  1753. local properFacing = facing
  1754.  
  1755. count(true)
  1756.  
  1757. while not compareDump and not detectFunc() do
  1758. if final then return end
  1759. chestFull = true
  1760. biometrics()
  1761. screen(1,1)
  1762. print("Waiting for chest placement on ",side," side (when facing quarry)")
  1763. sleep(2)
  1764. end
  1765. chestFull = false
  1766.  
  1767. local fuelSwitch = false
  1768. for i=1,inventoryMax do
  1769.  
  1770. if turtle.getItemCount(i) > 0 then
  1771. if slot[i][1] == 1 and dumpCompareItems then turnTo(dropFacing)
  1772. else turnTo(properFacing)
  1773. end
  1774. select(i)
  1775. if doRefuel and slot[i][1] == 2 then
  1776. if not fuelSwitch then
  1777. fuelSwitch = midRunRefuel(i)
  1778. end
  1779. elseif not compareDump or (compareDump and slot[i][1] == 1) then
  1780. waitDrop(i, allowedItems[i], dropFunc)
  1781. end
  1782. end
  1783. end
  1784.  
  1785. if oldOreQuarry or compareDump then count(nil) end
  1786. resetDumpSlots()
  1787.  
  1788. select(1)
  1789.  
  1790. end
  1791.  
  1792. function dropOff()
  1793. local currX,currZ,currY,currFacing = xPos, zPos, yPos, facing
  1794. if careAboutResources then
  1795. if not enderChestEnabled then
  1796. eventAdd("goto", 1,1,currY,2)
  1797. eventAdd("goto(0,1,1,2)")
  1798. eventAdd("drop", dropSide,false)
  1799. eventAdd("turnTo(0)")
  1800. eventAdd("mine",false,false,true,false)
  1801. eventAdd("goto(1,1,1, 0)")
  1802. eventAdd("goto", 1, 1, currY, 0)
  1803. eventAdd("goto", currX,currZ,currY,currFacing)
  1804. else
  1805. if turtle.getItemCount(specialSlots.enderChest) ~= 1 then eventAdd("promptSpecialSlot('enderChest','Ender Chest')") end
  1806. eventAdd("turnTo",currFacing-2)
  1807. eventAdd("dig",false)
  1808. eventAdd("select",specialSlots.enderChest)
  1809. eventAdd("turtle.place")
  1810. eventAdd("drop","front",false)
  1811. eventAdd("turnTo",currFacing-2)
  1812. eventAdd("select", specialSlots.enderChest)
  1813. eventAdd("dig",false)
  1814. eventAdd("turnTo",currFacing)
  1815. eventAdd("select(1)")
  1816. end
  1817. runAllEvents()
  1818. numDropOffs = numDropOffs + 1
  1819. end
  1820. return true
  1821. end
  1822. function endingProcedure()
  1823. eventAdd("goto",1,1,yPos,2,"quarryStart")
  1824. eventAdd("goto",0,1,1,2, "quarryStart")
  1825. runAllEvents()
  1826.  
  1827. if enderChestEnabled then
  1828. if dropSide == "right" then eventAdd("turnTo(1)") end
  1829. if dropSide == "left" then eventAdd("turnTo(3)") end
  1830. eventAdd("dig(false)")
  1831. eventAdd("select",specialSlots.enderChest)
  1832. eventAdd("turtle.place")
  1833. eventAdd("select(1)")
  1834. end
  1835. eventAdd("drop",dropSide, true)
  1836. eventAdd("turnTo(0)")
  1837.  
  1838.  
  1839. eventAdd("display")
  1840.  
  1841. eventAdd("logMiningRun",logExtension)
  1842. toQuit = true
  1843. runAllEvents()
  1844. end
  1845. function bedrock()
  1846. foundBedrock = true
  1847. if rednetEnabled then biometrics() end
  1848. if checkFuel() == 0 then error("No Fuel",0) end
  1849. local origin = {x = xPos, y = yPos, z = zPos}
  1850. print("Bedrock Detected")
  1851. if turtle.detectUp() and not turtle.digUp() then
  1852. print("Block Above")
  1853. turnTo(facing+2)
  1854. repeat
  1855. if not forward(false) then
  1856. if not attck() then
  1857. if not dig() then
  1858. turnTo(facing+1)
  1859. end
  1860. end
  1861. end
  1862. until not turtle.detectUp() or turtle.digUp()
  1863. end
  1864. up()
  1865. up()
  1866. eventClear()
  1867. endingProcedure()
  1868. print("\nFound bedrock at these coordinates: ")
  1869. print(origin.x," Was position in row\n",origin.z," Was row in layer\n",origin.y," Blocks down from start")
  1870. error("",0)
  1871. end
  1872.  
  1873. function endOfRowTurn(startZ, wasFacing, mineFunctionTable)
  1874. local halfFacing = ((layersDone % 2 == 1) and 1) or 3
  1875. local toFace = coterminal(wasFacing + 2)
  1876. if zPos == startZ then
  1877. if facing ~= halfFacing then turnTo(halfFacing) end
  1878. mine(unpack(mineFunctionTable or {}))
  1879. end
  1880. if facing ~= toFace then
  1881. turnTo(toFace)
  1882. end
  1883. end
  1884.  
  1885.  
  1886.  
  1887.  
  1888. runAllEvents()
  1889. if toQuit then error("",0) end
  1890.  
  1891. local doDigDown, doDigUp = (lastHeight ~= 1), (lastHeight == 0)
  1892. if not restoreFoundSwitch then
  1893.  
  1894. if not isMiningTurtle then
  1895. local a, b = turtle.dig()
  1896. if a then
  1897. mined = mined + 1
  1898. isMiningTurtle = true
  1899. elseif b == "Nothing to dig with" or b == "No tool to dig with" then
  1900. print("This is not a mining turtle. To make a mining turtle, craft me together with a diamond pickaxe")
  1901. error("",0)
  1902. end
  1903. end
  1904. mine(false,false,true)
  1905. if gpsEnabled and not restoreFoundSwitch then
  1906. gpsSecondPos = {gps.locate(gpsTimeout)}
  1907. end
  1908. for i = 1, startDown do
  1909. eventAdd("down")
  1910. end
  1911. runAllEvents()
  1912. if flatBedrock then
  1913. while (detectDown() and digDown(false, true)) or not detectDown() do
  1914. down()
  1915. startDown = startDown + 1
  1916. end
  1917. startDown = startDown - y + 1
  1918. for i=1, y-2 do
  1919. up()
  1920. end
  1921. elseif not(y == 1 or y == 2) then
  1922. down()
  1923. end
  1924. else
  1925. if not(layersDone == layers and not doDigDown) then digDown() end
  1926. if not(layersDone == layers and not doDigUp) then digUp() end
  1927. end
  1928.  
  1929. select(1)
  1930. while layersDone <= layers do
  1931. local lastLayer = layersDone == layers
  1932. local secondToLastLayer = (layersDone + 1) == layers
  1933. moved = moved + 1
  1934. if not(layersDone == layers and not doDigDown) then digDown() end
  1935. if not restoreFoundSwitch and layersDone % 2 == 1 then rowCheck = true end
  1936. relxCalc()
  1937. while relzPos <= z do
  1938. while relxPos < x do
  1939. mine(not lastLayer or (doDigDown and lastLayer), not lastLayer or (doDigUp and lastLayer))
  1940. end
  1941. if relzPos ~= z then
  1942. local func
  1943. if rowCheck == true then
  1944. func = "right"; rowCheck = false; else func = false; rowCheck = true end
  1945. eventAdd("endOfRowTurn", zPos, facing , {not lastLayer or (doDigDown and lastLayer), not lastLayer or (doDigUp and lastLayer)})
  1946. runAllEvents()
  1947. else break
  1948. end
  1949. end
  1950. if layersDone % 2 == 0 then
  1951. eventAdd("goto",1,1,yPos,0, "layerStart")
  1952. else
  1953. eventAdd("turnTo",coterminal(facing-2))
  1954. end
  1955. if not lastLayer then
  1956. for i=1, 2+fromBoolean(not(lastHeight~=0 and secondToLastLayer)) do eventAdd("down()") end
  1957. end
  1958. eventAdd("relxCalc")
  1959. layersDone = layersDone + 1
  1960. restoreFoundSwitch = false
  1961. runAllEvents()
  1962. end
  1963.  
  1964. endingProcedure()
Add Comment
Please, Sign In to add comment