daily pastebin goal
22%
SHARE
TWEET

Quarry 3.0.7

a guest Jun 11th, 2013 10,673 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Version 3.0.7
  3. Recent Changes:
  4. 1. Now has session persistence! Just type "quarry -restore"
  5.    and your turtle will pick up where it left off
  6. 2. doRefuel argument will now take fuel from inventory while it is running, instead
  7.    of waiting to come back to the start
  8. 3. Will now wait for its chest to clear if the chest is full
  9. 4. Slight modifications to the refueling system
  10. 5. Added in fuelSafety argument
  11. ]]
  12. --[[ToDo:
  13. 1. Actually get the rednet send back and forth to work
  14. 3. Send basic commands from receiver program e.g. Stop
  15. 4. Improve Session Persistence. See idea on desktop
  16. ]]
  17. --Defining things
  18. _G.civilTable = {}; civilTable = nil; setmetatable(civilTable, {__index = _G}); setfenv(1,civilTable)
  19. -------Defaults for Arguments----------
  20. --Arguments assignable by text
  21. x,y,z = 3,3,3 --These are just in case tonumber fails
  22. inverted = false --False goes from top down, true goes from bottom up [Default false]
  23. rednetEnabled = false --Default rednet on or off  [Default false]
  24. --Arguments assignable by tArgs
  25. dropSide = "front" --Side it will eject to when full or done [Default "front"]
  26. careAboutResources = true --Will not stop mining once inventory full if false [Default true]
  27. doCheckFuel = true --Perform fuel check [Default true]
  28. doRefuel = false --Whenever it comes to start location will attempt to refuel from inventory [Default false]
  29. invCheckFreq = 10 --Will check for inventory full every <-- moved spaces [Default 10]
  30. keepOpen = 1 --How many inventory slots it will attempt to keep open at all times [Default 1]
  31. fuelSafety = "moderate" --How much fuel it will ask for: safe, moderate, and loose [Default moderate]
  32. saveFile = "Civil_Quarry_Restore" --Where it saves restore data [Default "Civil_Quarry_Restore"]
  33. doBackup = true --If it will keep backups for session persistence [Default true]
  34. --Standard number slots for fuel (you shouldn't care)
  35. fuelTable = { --Will add in this amount of fuel to requirement.
  36. safe = 1000,
  37. moderate = 200,
  38. loose = 0 } --Default 1000, 200, 0
  39. --Standard rednet channels
  40. channels = {
  41. send = os.getComputerID()  ,
  42. receive = 10 ,
  43. confirm = "Confirm"
  44. }
  45.  
  46. local help_paragraph = [[
  47. -DEFAULT: This will ignore all arguments and prompts and use defaults
  48. -vanilla: This will ignore all arguments except for dimensions
  49. -dim: [num] [num] [num] This sets the dimensions of the quarry
  50. -invert: [t/f] This sets whether invert is true or false
  51. -rednet: [t/f] This sets whether it will attempt to make a rednet connection
  52. -sendChannel: [num] This sets the channel the turtle will attempt to send on
  53. -receiveChannel: [num] This sets the channel the turtle will attempt to receive on
  54. -doRefuel: Changes whether or not the turtle will refuel itself with coal when fuel is low (opposite of written config)
  55. -doCheckFuel: Changes whether or not the turtle will check its fuel level before running (opposite of written config)
  56. -chest: [chest side] Changes what side turtle will output to
  57. -keepOpen: [num] How many slots of the inventory turtle will try to keep open (Will then auto-empty)
  58. -invCheckFreq: [num] How many blocks before full inventory checks
  59. -saveFile: [word] Changes where the turtle saves its backup to.
  60. -doBackup: [t/f] Whether or not the turtle will backup its current position to a file.
  61. -restore: Turtle will check for a save file. If found will ignore all other arguments.
  62. -fuelSafety: [safe/moderate/loose] How much extra fuel the turtle will request at startup
  63. Examples [1]:
  64.  The minimum amount necessary to start a turtle automatically would be one of these two
  65.  ---------
  66.  quarry -dim 3 3 3 -invert false -rednet false
  67.  ---------
  68.  quarry -dim 3 3 3 -vanilla
  69.  ---------
  70.  or, if you actually wanted a 3x3x3 then
  71.  ---------
  72.  quarry -DEFAULT
  73. Examples [2]:
  74.  If you wanted start a quarry that has
  75.  rednet on channels 500 and 501, outputs
  76.  to a chest below itself, is inverted,
  77.  and has the dimesions 5x5x21, then here
  78.  is what you would type: (Remember that
  79.  order does not matter)
  80.  ----------
  81.  quarry -receiveChannel 501 -sendChannel 500 -invert true -dim 5 5 21 -chest bottom -rednet true
  82. Examples [2] (cont.):
  83.  Now, this may be very long, but it is meant for people who want to make automated quarries with
  84.  the same settings every time (or with variable setting)
  85. Examples [3]:
  86.  If you are playing softcore then you
  87.  can do
  88.  ---------
  89.  quarry -doCheckFuel false
  90.  ---------
  91.  Followed by any other arguments
  92. Tips:
  93.  You don't actually have to type out "false" or "true" if you don't want. It actaully just checks if the first
  94.  letter is "t", so you (usually) don't have to put anything at all. Putting the whole word just helps with clarity
  95. Internal Config:
  96.  At the top of the program, right below the changelog is a written config. Anything not specified by arguments
  97.  will be taken from here. If you have a quarry setup you use a lot, you could just edit the config and then
  98.  type in the following:
  99.  ----------
  100.  quarry -DEFAULT
  101. ]]
  102.  
  103. --Parsing help for display
  104. --[[The way the help table works:
  105. All help indexes are numbered. There is a help[i].title that contains the title,
  106. and the other lines are in help[i][1] - help[i][#help[i] ]
  107. Different lines (e.g. other than first) start with a space.
  108. As of now, the words are not wrapped, fix that later]]
  109. local help = {}
  110. local i = 0
  111. local titlePattern = ".-%:" --Find the beginning of the line, then characters, then a ":"
  112. local textPattern = "%:.+" --Find a ":", then characters until the end of the line
  113. for a in help_paragraph:gmatch("\n?.-\n") do --Matches in between newlines
  114. local current = string.sub(a,1,-2).."" --Concatenate Trick
  115. if string.sub(current,1,1) ~= " " then
  116. i = i + 1
  117. help[i] = {}
  118. help[i].title = string.sub(string.match(current, titlePattern),1,-2)..""
  119. help[i][1] = string.sub(string.match(current,textPattern) or " ",3,-1)
  120. elseif string.sub(current,1,1) == " " then
  121. table.insert(help[i], string.sub(current,2, -1).."")
  122. end
  123. end
  124.  
  125.  
  126. local supportsRednet = (peripheral.wrap("right") ~= nil)
  127.  
  128. local tArgs = {...}
  129. --You don't care about these
  130.       xPos,yPos,zPos,facing,percent,mined,moved,relxPos, rowCheck, connected, isInPath, layersDone
  131.     = 0,   1,   1,   0,     0,      0,    0,    1,       "right",  false,     true,     1
  132.  
  133. local totals = {cobble = 0, fuel = 0, other = 0} -- Total for display (cannot go inside function)
  134. local function count() --Done any time inventory dropped and at end
  135. slot = {}        --1: Cobble 2: Fuel 3:Other
  136. for i=1, 16 do   --[1] is type, [2] is number
  137. slot[i] = {}
  138. slot[i][2] = turtle.getItemCount(i)
  139. end
  140. slot[1][1] = 1   -- = Assumes Cobble/Main
  141. for i=1, 16 do   --Cobble Check
  142. turtle.select(i)
  143. if turtle.compareTo(1)  then
  144. slot[i][1] = 1
  145. totals.cobble = totals.cobble + slot[i][2]
  146. elseif turtle.refuel(0) then
  147. slot[i][1] = 2
  148. totals.fuel = totals.fuel + slot[i][2]
  149. else
  150. slot[i][1] = 3
  151. totals.other = totals.other + slot[i][2]
  152. end
  153. end
  154. turtle.select(1)
  155. end
  156. local function checkFuel()
  157. return turtle.getFuelLevel()
  158. end
  159. local function isFull(slots)
  160. slots = slots or 16
  161. local numUsed = 0
  162. sleep(0.5)
  163. for i=1, slots do
  164. if turtle.getItemCount(i) > 0 then numUsed = numUsed + 1 end
  165. end
  166. if numUsed >= slots then return true end
  167. return false
  168. end
  169.  
  170.  -----------------------------------------------------------------
  171. --Input Phase
  172. local function screen(xPos,yPos)
  173. xPos, yPos = xPos or 1, yPos or 1
  174. term.setCursorPos(xPos,yPos); term.clear(); end
  175. local function screenLine(xPos,yPos)
  176. term.setCursorPos(xPos,yPos); term.clearLine(); end
  177.  
  178. screen(1,1)
  179. print("----- Welcome to Quarry! -----")
  180. print("")
  181.  
  182. local sides = {top = "top", right = "right", left = "left", bottom = "bottom", front = "front"} --Used to whitelist sides
  183. local errorT = {num = "Numbers not recognized", zero = "Variable is out of range", word = "String failed assertion" }
  184. local changedT = {}
  185. changedT.new = function(key, value) changedT[#changedT+1] = {[key] = value} end
  186. changedT.getPair = function(i) for a, b in pairs(changedT[i]) do return a, b end end
  187. local function capitalize(text) return (string.upper(string.sub(text,1,1))..string.sub(text,2,-1)) end
  188. local function assert(condition, message, section) section = section or "[Blank]"; if condition then return condition else error("Error: "..message.."\nin section "..section, 0) end end
  189. local function checkNum(number, section) return assert(tonumber(number),errorT.num, section) end
  190. tArgs.checkStart = function(num) tArgs[tArgs[num]] = num end
  191. for i=1, #tArgs do tArgs.checkStart(i) end
  192.  
  193. if tArgs["help"] or tArgs["-help"] or tArgs["-?"] then
  194. print("You have selected help, press any key to continue"); print("Use arrow keys to naviate, q to quit"); os.pullEvent("key")
  195. local pos = 1
  196. local key = 0
  197. while pos <= #help and key ~= keys.q do
  198. if pos < 1 then pos = 1 end
  199. screen(1,1)
  200. print(help[pos].title)
  201. for a=1, #help[pos] do print(help[pos][a]) end
  202. repeat
  203. _, key = os.pullEvent("key")
  204. until key == 200 or key == 208 or key == keys.q
  205. if key == 200 then pos = pos - 1 end
  206. if key == 208 then pos = pos + 1 end
  207. end
  208. error("",0)
  209. end
  210.  
  211. --Saving
  212. if tArgs["doBackup"] then doBackup = (string.lower(string.sub(tArgs[tArgs["doBackup"]+1],1,1)) ~= "f") end
  213. if tArgs["saveFile"] then saveFile = tArgs[tArgs["saveFile"]+1] or saveFile end
  214.  
  215. local restoreFound = false
  216. if tArgs["-restore"] then
  217. restoreFound = fs.exists(saveFile)
  218. if restoreFound then
  219. os.run(getfenv(1),saveFile)
  220. print("Restore File read successfully. Starting in 3"); sleep(3)
  221. end
  222. end
  223.  
  224. if not (tArgs["-DEFAULT"] or restoreFound) then
  225. local section = "Dimensions"
  226. --Dimesnions
  227. if tArgs["-dim"] then local num = tArgs["-dim"];
  228. x = checkNum(tArgs[num + 1],section); z = checkNum(tArgs[num + 2],section); y = checkNum(tArgs[num + 3],section)
  229. else
  230. print("What dimensions?")
  231. print("")
  232. --This will protect from negatives, letters, and decimals
  233. term.write("Length: ")
  234. x = math.floor(math.abs(tonumber(io.read()) or x))
  235. term.write("Width: ")
  236. z = math.floor(math.abs(tonumber(io.read()) or z))
  237. term.write("Height: ")
  238. y = math.floor(math.abs(tonumber(io.read()) or y))
  239. end
  240. changedT.new("x",x); changedT.new("z",z); changedT.new("y",y)
  241. assert(x~=0, errorT.zero, section); assert(z~=0, errorT.zero, section); assert(y~=0, errorT.zero, section)
  242. assert(not(x == 1 and y == 1 and z == 1) ,"1, 1, 1 dosen't work well at all, try again", section)
  243. if not tArgs["-vanilla"] then
  244. --Invert
  245. if tArgs["-invert"] then
  246. inverted = (string.lower(string.sub(tArgs[tArgs["-invert"]+1] or "",1,1)) == "t") else
  247. term.write("Inverted? ")
  248. inverted = (string.lower(string.sub(io.read(),1,1)) == "y")
  249. end
  250. changedT.new("Inverted", inverted)
  251. --Rednet
  252. if supportsRednet then
  253. if tArgs["-rednet"] then
  254. rednetEnabled = (string.lower(string.sub(tArgs[tArgs["-rednet"]+1] or "",1,1)) == "t")
  255. else term.write("Rednet? "); rednetEnabled = (string.lower(string.sub(io.read(),1,1)) == "y")
  256. end
  257. changedT.new("Rednet Enabled", rednetEnabled)
  258. if tArgs["-sendChannel"] then
  259. channels.send = assert(tonumber(tArgs[tArgs["-sendChannel"]+1]), errorT.num)
  260. assert(channels.send > 0 and channels.send < 65535, errorT.zero)
  261. changedT.new("Send Channel",channels.send) end
  262. if tArgs["-receiveChannel"] then
  263. channels.receive = assert(tonumber(tArgs[tArgs["-receiveChannel"]+1]), errorT.num)
  264. assert(channels.receive > 0 and channels.receive < 65535 and channels.receive ~= channels.send, errorT.zero)
  265. changedT.new("Receive Channel",channels.receive) end
  266. end
  267. --Fuel
  268. if tArgs["-doRefuel"] then doRefuel = not doRefuel; changedT.new("Do Refuel",doRefuel) end
  269. if tArgs["-doCheckFuel"] then
  270. doCheckFuel = (string.lower(string.sub(tArgs[tArgs["-doCheckFuel"]+1] or "",1,1)) == "t"); changedT.new("Do Check Fuel", doCheckFuel) end
  271. if tArgs["-chest"] then
  272. dropSide = sides[tArgs[tArgs["-chest"]+1]] or dropSide; changedT.new("Chest Side",dropSide) end
  273. if tArgs["-fuelSafety"] then local loc = tArgs[tArgs["-fuelSafety"]+1]
  274.   if fuelTable[loc] then
  275.     fuelSafety = loc; changedT.new("Fuel Check Safety", fuelSafety)
  276.   end
  277. end
  278. --Misc
  279. if tArgs["-invCheckFreq"] then
  280. invCheckFreq = math.abs(math.floor(checkNum(tArgs[tArgs["-invCheckFreq"]+1],"Inventory Check Frequency")))
  281. changedT.new("Inventory Check Frequency",invCheckFreq) end
  282. assert(invCheckFreq ~= 0, errorT.zero, "Inventory Check Frequency")
  283. if tArgs["-keepOpen"] then
  284. keepOpen = math.abs(math.floor(checkNum(tArgs[tArgs["-keepOpen"]+1],"Open Slots")))
  285. changedT.new("Slots to keep open", keepOpen) end
  286. assert(keepOpen ~= 0 and keepOpen < 16, errorT.zero, "Open Slots")
  287. if tArgs["-ignoreResources"] then careAboutResources = false; changedT.new("Ignore Resources?", not careAboutResources) end
  288. if tArgs["-saveFile"] then saveFile = tArgs[tArgs["-saveFile"]+1] changedT.new("Save File", saveFile) end
  289. assert(#saveFile >= 2,errorT.word, "Save File")
  290. end; end --First end is for vanilla, second is for DEFAULT
  291.  
  292. local function saveProgress() --Session persistence
  293. local file = fs.open(saveFile,"w")
  294. for a,b in pairs(getfenv(1)) do
  295. if type(b) == "string" then b = "\""..b.."\"" end
  296. if type(b) == "table" and a~="modem" then b = textutils.serialize(b) end
  297. if type(b) ~= "function" then
  298. file.write(a.." = "..tostring(b).."\n")
  299. end
  300. end
  301. file.write("doCheckFuel = false\n")
  302. local a = 1; if facing == 2 then a = -1 end
  303. file.write("xPos = "..xPos+a)
  304. file.close()
  305. end
  306.  
  307. local area = x*z
  308. local volume = x*y*z
  309. local lastHeight = y%3
  310. local dispY = y
  311. y = math.floor(y/3)*3
  312. local yMult = y/3 + math.ceil(lastHeight/2)
  313. local moveVolumeCalc = ((area+x+z)*yMult) + (2 * dispY)
  314. local moveVolume = (area * yMult)
  315.  
  316. --Getting Fuel
  317. if doCheckFuel then --Calculating Needed Fuel
  318. local neededFuel = moveVolume + (math.floor(volume / (64 * 8)) * (x+dispY+z)) --Standard move plus dropping off supplies
  319.                              --How many times come back to start| * If it were at the very far side
  320. neededFuel = neededFuel + fuelTable[fuelSafety]
  321. if neededFuel < 100 then neededFuel = 100; end
  322. if checkFuel() < neededFuel then
  323. screen(1,1)
  324. print("More Fuel Needed")
  325. print("Current Fuel: ",checkFuel()," Needed: ",neededFuel)
  326. print("Place fuel in Bottom Right")
  327. while turtle.getItemCount(16) == 0 do
  328.   sleep(1)
  329. end
  330. turtle.select(16)
  331. while checkFuel() < neededFuel do
  332. if not turtle.refuel(1) then
  333. term.clearLine()
  334. print("Still too little fuel")
  335. term.clearLine()
  336. print("Insert more fuel to resume")
  337. while turtle.getItemCount(16) == 0 do
  338.   sleep(1)
  339. end
  340. end
  341. local x,y = term.getCursorPos()
  342. print(checkFuel().." Fuel")
  343. term.setCursorPos(x,y)
  344. end
  345. print(checkFuel().." Units of Fuel")
  346. sleep(3)
  347. turtle.select(1)
  348. end
  349. end
  350. --Initial Rednet Handshake
  351. if rednetEnabled then
  352. screen(1,1)
  353. print("Rednet is Enabled")
  354. print("The Channel to open is "..channels.send)
  355. modem = peripheral.wrap("right")
  356. modem.open(channels.receive)
  357. local i = 0
  358. repeat
  359. local id = os.startTimer(3)
  360. i=i+1
  361. print("Sending Initial Message "..i)
  362. modem.transmit(channels.send, channels.receive, "{ 'Initial' }")
  363. local message
  364. repeat
  365. local event, idCheck, channel,_,locMessage, distance = os.pullEvent()
  366. message = locMessage
  367. until (event == "timer" and idCheck == id) or (event == "modem_message" and channel == channels.receive and message == channels.confirm)
  368. until message == channels.confirm
  369. connected = true
  370. print("Connection Confirmed!")
  371. sleep(1.5)
  372. end
  373. local function biometrics(sendChannel)
  374. local commands = { Confirm = "Confirm" }
  375. local toSend = { ["x"] = x, ["y"] = (y/3 + math.ceil(lastHeight/2)),
  376.      ["z"] = z,                     --The y calc is weird...
  377.     ["xPos"] = xPos, ["yPos"] = yPos, ["zPos"] = zPos,
  378.     ["percent"] = percent, ["mined" ]= mined,
  379.     ["fuel"] = checkFuel(), ["moved"] = moved,
  380.     ["remainingBlocks"] = (volume-mined), ["ID"] = os.getComputerID(),
  381.     ["isInPath"] = isInPath, --Whether it is going back to start
  382.     ["volume"] = volume, ["area"] = area}
  383. modem.transmit(channels.send, channels.receive, textutils.serialize(toSend))
  384. id = os.startTimer(0.1)
  385. local event, message
  386. repeat
  387. local locEvent, idCheck, confirm, _, locMessage, distance = os.pullEvent()
  388. event, message = locEvent, locMessage
  389. until (event == "timer" and idCheck == id) or (event == "modem_message" and confirm == channels.receive)
  390. if event == "modem_message" then connected = true else connected = false end
  391. --Stuff to do for different commands
  392. end
  393.  
  394. --Showing changes to settings
  395. screen(1,1)
  396. print("Your selected settings:")
  397. if #changedT == 0 then
  398. print("Completely Default")
  399. else
  400. for i=1, #changedT do
  401. local title, value = changedT.getPair(i)
  402. print(capitalize(title)..": ",value)
  403. end
  404. end
  405. print("\nStarting in 3"); sleep(1); print("2"); sleep(1); print("1"); sleep(1.5)
  406.  
  407. ----------------------------------------------------------------
  408. --Mining Phase
  409. local function display()
  410. screen(1,1)
  411. print("Total Blocks Mined: "..mined)
  412. print("Current Fuel Level: "..turtle.getFuelLevel())
  413. print("Cobble: "..totals.cobble)
  414. print("Usable Fuel: "..totals.fuel)
  415. print("Other: "..totals.other)
  416. if rednetEnabled then
  417. print("")
  418. print("Sent Stop Message")
  419. finalTable = {{["Mined: "] = mined}, {["Cobble: "] = totals.cobble}, {["Fuel: "] = totals.fuel},
  420.     {["Other: "] = totals.other}, {["Fuel: "] = checkFuel()} }
  421. modem.transmit(channels.send,channels.receive,"stop")
  422. modem.transmit(channels.send,channels.receive,textutils.serialize(finalTable))
  423. modem.close(channels.receive)
  424. end
  425. end
  426. local function updateDisplay() --Runs in Mine(), display information to the screen in a certain place
  427. screen(1,1)
  428. print("Blocks Mined")
  429. print(mined)
  430. print("Percent Complete")
  431. print(percent.."%")
  432. print("Fuel")
  433. print(checkFuel())
  434. if rednetEnabled then
  435. screenLine(1,7)
  436. print("Connected: "..tostring(connected))
  437. end
  438. end
  439. local function mine(digDown, digUp, outOfPath,doCheckInv) -- Basic Move Forward
  440. if doCheckInv == nil then doCheckInv = true end
  441. if digDown == nil then digDown = true end
  442. if digUp == nil then digUp = true end
  443. if doRefuel and checkFuel() < 100 then
  444.   for i=1, 16 do
  445.   if turtle.getItemCount(i) > 0 then
  446.     turtle.select(i)
  447.     if checkFuel() < 200 + fuelTable[fuelSafety] then
  448.       turtle.refuel()
  449.     end
  450.   end
  451.   end
  452. end
  453. saveProgress()
  454. --Move this to after the move. Then it will have done everything already, don't forget
  455. --to modify saveProgress to reflect the change.
  456. local count = 0
  457. while not turtle.forward() do count = count + 1
  458. if turtle.dig() then mined = mined + 1; else turtle.attack(); end
  459.   if count > 20 then
  460.     if turtle.getFuelLevel() > 0 then
  461.       bedrock() else
  462.     error("No Fuel",0) end end
  463. end
  464. if facing == 0 then xPos = xPos +1
  465. elseif facing == 2 then xPos = xPos-1
  466. elseif facing == 1 then zPos = zPos+1
  467. elseif facing == 3 then zPos = zPos-1; end
  468. if digUp then
  469. local count = 0
  470. while turtle.detectUp() do count = count + 1
  471. if turtle.digUp() then mined = mined + 1; end
  472. if count > 20 then bedrock() end; end
  473. end
  474. if digDown then
  475. if turtle.digDown() then mined = mined + 1; end
  476. end
  477. percent = math.ceil(moved/moveVolume*100)
  478. if rowCheck == "right" then relxPos = xPos else relxPos = (x-xPos)+1 end --Maybe adjust this for out of path
  479. updateDisplay()
  480. if outOfPath then isInPath = false; else isInPath = true; moved = moved + 1; end
  481. if doCheckInv and careAboutResources then
  482. if moved%invCheckFreq == 0 then
  483.  if isFull(16-keepOpen) then dropOff() end
  484. end; end
  485. if rednetEnabled then biometrics() end
  486. end
  487. --Direction: Front = 0, Right = 1, Back = 2, Left = 3
  488. local function facingF(num)
  489. facing = facing + num
  490. if facing > 3 then facing = 0 end
  491. if facing < 0 then facing = 3 end
  492. end
  493.  
  494. if up then local temp1 = up end --Just in case another program uses this
  495. if down then local temp2 = down end
  496. function up(num, sneak)
  497. num = num or 1
  498. sneak = sneak or 1
  499. if inverted and sneak == 1 then
  500.     down(num, -1)
  501. else
  502.     for i=1, num do while not turtle.up() do
  503.         while not turtle.digUp() do
  504.         turtle.attackUp(); sleep(0.5); end
  505.         mined = mined + 1
  506.     end; yPos = yPos - sneak; end
  507. end
  508. end
  509. function down(num, sneak)
  510. num = num or 1
  511. sneak = sneak or 1
  512. if inverted and sneak == 1 then
  513.     up(num, -1)
  514. else
  515.     for i=1, num do local count = 0
  516.         while not turtle.down() do
  517.         count = count + 1
  518.         if not turtle.digDown() then
  519.         turtle.attackDown(); sleep(0.2); end
  520.         mined = mined+1
  521.         if count > 20 then bedrock() end
  522.     end; yPos = yPos + sneak; end
  523. end
  524. end
  525. local function right(num)
  526. num = num or 1
  527. for i=1, num do turtle.turnRight(); facingF(1); end
  528. end
  529. local function left(num)
  530. num = num or 1
  531. for i=1, num do turtle.turnLeft(); facingF(-1) end
  532. end
  533. local function goto(x,z,y, toFace)
  534. x = x or 1; y = y or 1; z = z or 1; toFace = toFace or facing
  535. if yPos > y then while yPos~=y do up() end end
  536.  if zPos > z then
  537.  while facing ~= 3 do left() end
  538.  elseif zPos < z then while facing ~= 1 do left() end
  539.  end
  540.  while zPos ~= z do mine(false,false,true,false) end
  541.   if xPos> x then
  542.   while facing ~= 2 do left() end
  543.   elseif xPos < x then while facing ~= 0 do left() end
  544.   end
  545.   while xPos ~= x do mine(false,false,true,false) end
  546. if yPos < y then while yPos~=y do down() end end
  547. while facing ~= toFace do right() end
  548. saveProgress()
  549. end
  550. local function turnTo(num)
  551. num = num or 1; goto(xPos,zPos,yPos,num)
  552. end
  553. local function drop(side, final, allowSkip)
  554. side = sides[side] or "front"    --The final number means that it will
  555. if final then final = 0 else final = 1 end --drop a whole stack at the end
  556. local allowSkip = allowSkip or (final == 0) --This will allow drop(side,t/f, rednetConnected)
  557. count()
  558. if doRefuel then
  559.   for i=1, 16 do
  560.     if slot[i][1] == 2 then
  561.       turtle.select(i); turtle.refuel()
  562.     end
  563.   end
  564.   turtle.select(1)
  565. end
  566. if side == "right" then turnTo(1) end
  567. if side == "left" then turnTo(3) end
  568. local whereDetect, whereDrop1, whereDropAll
  569. local _1 = slot[1][2] - final --All but one if final, all if not final
  570. if side == "top" then
  571. whereDetect = turtle.detectUp ; whereDrop = turtle.dropUp
  572. elseif side == "bottom" then
  573. whereDetect = turtle.detectDown ; whereDrop = turtle.dropDown
  574. else
  575. whereDetect = turtle.detect; whereDrop = turtle.drop
  576. end
  577. local function waitDrop(val) --This will just drop, but wait if it can't
  578.   val = val or 64
  579.   local try = 1
  580.   while not whereDrop(val) do
  581.     print("Chest Full, Try "..try)
  582.     try = try + 1
  583.     sleep(2)
  584.   end
  585. end
  586. repeat
  587. local detected = whereDetect()
  588. if detected then
  589.   waitDrop(_1)
  590.   for i=2, 16 do
  591.     if turtle.getItemCount(i) > 0 then
  592.       turtle.select(i)
  593.       waitDrop()
  594.     end
  595.   end
  596. elseif not allowSkip then
  597.   print("Waiting for chest placement place a chest to continue")
  598.   while not whereDetect() do
  599.     sleep(1)
  600.   end
  601. end
  602. until detected or allowSkip
  603. if not allowSkip then totals.cobble = totals.cobble - 1 end
  604. turtle.select(1)
  605. turnTo(0)
  606. end
  607. function dropOff() --Not local because called in mine()
  608. local currX,currZ,currY,currFacing = xPos, zPos, yPos, facing
  609. goto(0,1,1,2)
  610. drop(dropSide,false)
  611. mine(false,false,true, false)
  612. goto(1,1,1, 0)
  613. goto(currX,currZ,currY,currFacing)
  614. end
  615. function bedrock()
  616. local origin = {x = xPos, y = yPos, z = zPos}
  617. print("Bedrock Detected")
  618. if turtle.detectUp() then
  619. print("Block Above")
  620. local var
  621. if facing == 0 then var = 2 elseif facing == 2 then var = 0 else error("Was facing left or right on bedrock") end
  622. goto(xPos,zPos,yPos,var)
  623. for i=1, relxPos do mine(true,true); end
  624. end
  625. goto(0,1,1,2)
  626. drop(dropSide, true)
  627. display()
  628. print("\nFound bedrock at these coordinates: ")
  629. print(origin.x," Was position in row\n",origin.z," Was row in layer\n",origin.y," Blocks down from start")
  630. error("",0)
  631. end
  632. -------------------------------------------------------------------------------------
  633. if facing ~= 0 then
  634. if xPos > 1 then turnTo(2) else turnTo(0) end
  635. end
  636. local doDigDown, doDigUp
  637. if not inverted then doDigDown , doDigUp = (lastHeight ~= 1), false
  638. else doDigUp, doDigDown = (lastHeight ~= 1) , false; end --Used in lastHeight
  639. if not restoreFound then mine(false,false, true) else if turtle.digUp() and turtle.digDown() then mined = mined + 2 end end
  640. if y ~= 0 and not restoreFound then down() end
  641. --Mining Loops
  642. turtle.select(1)
  643. while layersDone <= y do -------------Height---------
  644. moved = moved + 1
  645. rowCheck = "right"
  646. if rowCheck == "right" then relxPos = xPos else relxPos = (x-xPos)+1 end
  647. while zPos <= z do -------------Width----------
  648. while relxPos < x do ------------Length---------
  649. mine()
  650. end ---------------Length End-------
  651. if zPos ~= z then
  652. if rowCheck == "right" and zPos ~= z then --Swithcing to next row
  653. rowCheck = "left"; right(); mine(); right()
  654. else
  655. rowCheck = "right"; left(); mine(); left()
  656. end
  657. else break
  658. end
  659. end ---------------Width End--------
  660. goto(1,1,yPos,0)
  661. if yPos+1 ~= y then down(3) end
  662. layersDone = layersDone + 3; end ---------------Height End-------
  663. if lastHeight ~= 0 then ---------LAST ROW--------- (copied from above)
  664. moved = moved + 1
  665. if y ~= 0 then down(2) end --If the basic y == 2 or 1
  666. rowCheck = "right"
  667. if rowCheck == "right" then relxPos = xPos else relxPos = (x-xPos)+1 end
  668. while zPos <= z do -------------Width----------
  669. while relxPos < x do ------------Length---------
  670. mine(doDigDown, doDigUp)
  671. end ---------------Length End-------
  672. if zPos ~= z then
  673. if rowCheck == "right" and zPos ~= z then --Swithcing to next row
  674. rowCheck = "left"; right(); mine(doDigDown, doDigUp); right()
  675. else
  676. rowCheck = "right"; left(); mine(doDigDown, doDigUp); left()
  677. end
  678. else break
  679. end
  680. end ---------------Width End--------
  681. goto(1,1,yPos,0)
  682. end
  683. if not inverted then
  684.     if doDigDown then if turtle.digDown() then mined = mined + 1 end end
  685.   else
  686.     if doDigUp then if turtle.digUp() then mined = mined + 1 end end
  687. end
  688. goto(0,1,1,2)
  689.  
  690. --Output to a chest or sit there
  691. drop(dropSide, true)
  692. --Display was moved above to be used in bedrock function
  693. display()
  694.  
  695. if doBackup then fs.delete(saveFile) end
  696. --The only global variables I had to use
  697. if temp1 then up = temp1 end
  698. if temp2 then down = temp2 end
RAW Paste Data
Top