Guest User

Quarry 3.0.6

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