--rednet.open("right") --GLOBAL VARIABLES-- fuelSlot = 1 stoneSlot = 2 dirtSlot = 3 gravelSlot = 4 cobbleSlot = 5 home = tonumber(0) north = 0 east = 1 south = 2 west = 3 t = tonumber(0) --REDNET FUNCTIONS-- function report(msg) --rednet.open("right") clear() --rednet.broadcast(tostring(msg)) print(msg) end --FILING FUNCTIONS-- function getLoc() if fs.exists("loc") then data = fs.open("loc", "r") x = tonumber(data.readLine()) y = tonumber(data.readLine()) z = tonumber(data.readLine()) h = tonumber(data.readLine()) b = tonumber(data.readLine()) tl = tonumber(data.readLine()) bl = tonumber(data.readLine()) data.close() else x = 0 y = 0 z = 0 h = 0 b = 0 tl = 0 bl = 0 startup() end end function getTempLoc() data = fs.open("tempLoc", "r") tgtx = tonumber(data.readLine()) tgty = tonumber(data.readLine()) tgtz = tonumber(data.readLine()) tgth = tonumber(data.readLine()) data.close() end function dumpLoc() if fs.exists("loc") then fs.delete("loc") end report("Opening LOC data file.") data = fs.open("loc", "w") data.writeLine(tostring(x)) data.writeLine(tostring(y)) data.writeLine(tostring(z)) data.writeLine(tostring(h)) data.writeLine(tostring(b)) data.writeLine(tostring(tl)) data.writeLine(tostring(bl)) data.close() report("New LOC data written to file.") end function dumpTempLoc() report("Opening temporary LOC data file.") data = fs.open("tempLoc", "w") data.writeLine(tostring(x)) data.writeLine(tostring(y)) data.writeLine(tostring(z)) data.writeLine(tostring(h)) data.close() report("New LOC data written to file.") end --MOVEMENT FUNCTIONS-- function refuel(reqFuel) if reqFuel == nil then reqFuel = 80 end fuel = turtle.getFuelLevel() while fuel < reqFuel do if turtle.getItemCount(fuelSlot) == 0 then goHome() goBack() break else turtle.refuel(1) fuel = turtle.getFuelLevel() end end end function fd() getLoc() refuel() if turtle.forward() then if h == 0 then x = x + 1 elseif h == 1 then z = z + 1 elseif h == 2 then x = x - 1 elseif h == 3 then z = z - 1 end report("Moving.") else report("Blocked") end dumpLoc() end function up() getLoc() refuel() if turtle.up() then y = y + 1 report("Moving up.") else report("Blocked") end dumpLoc() end function dn() getLoc() refuel() if turtle.down() then y = y - 1 report("Moving down.") else report("Blocked") end dumpLoc() end function rt() getLoc() report("Turning.") turtle.turnRight() report("Updating data file.") h = (h + 1) % 4 dumpLoc() end function lt() getLoc() turtle.turnLeft() h = (h - 1) % 4 while h < 0 do h = h + 4 end dumpLoc() end function dig() turtle.dig() end function digUp() turtle.digUp() end function digDn() turtle.digDown() end --ADVANCED MOVEMENT FUNCTIONS-- function checkStuck() getLoc() dumpTempLoc() digFd() if x == tarx and z == tarz then face(south) digFd() digFd() digFd() goHome() end end function gofd(dist) if dist == nil then fd() else while dist > 0 do fd() dist = dist - 1 end end end function face(tar) getLoc() while true do if tar == h then break else getLoc() rt() dumpLoc() report("Turning. "..tostring(h)) end end end function findX(pos) getLoc() if x > pos then face(south) while x > pos do fd() getLoc() end elseif x < pos then face(north) while x < pos do fd() getLoc() end else end end function findZ(pos) getLoc() if z > pos then face(west) while z > pos do fd() getLoc() end elseif z < pos then face(east) while z < pos do fd() getLoc() end else end end function findY(height) if y > height then while y > height do dn() getLoc() end elseif y < height then while y < height do up() getLoc() end else end end function moveTo(newx, newy, newz) findY(newx) findX(newz) findZ(newy) end function goHome() getLoc() dumpTempLoc() findZ(tonumber(0)) findX(tonumber(0)) findY(tonumber(0)) face(north) unload() getFuel() end function goBack() getTempLoc() moveTo(tarx, tary, tarz) end function findBedrock() while true do turtle.digDown() if not turtle.down() then break end y = y - 1 report(y) end findY(y + 5) b = y report("Bedrock found!") end --USEFUL FUNCTIONS-- function clear(line, column) if line == nil then line = 1 end if column == nil then column = 1 end term.clear() term.setCursorPos(line, column) end function rest(count) while count > 0 do report("Sleeping... T.R: "..tostring(count).." sec.") sleep(1) count = count - 1 end end function fill(slot) turtle.select(slot) while turtle.getItemCount(slot) < 64 do turtle.suck() end for i = 1, 16 do turtle.select(i) if i == slot then else if turtle.compareTo(slot) then turtle.drop() end end end end function emptyFrom(slot) for i = slot, 16 do turtle.select(i) turtle.drop() end end --JOB SPECIFIC FUNCTIONS-- function unload() face(east) emptyFrom(5) face(north) end function getFuel() face(west) fill(fuelSlot) face(north) end function gravel() turtle.select(gravelSlot) rest(1) if turtle.compare() then while turtle.compare() do report("Encountered Gravel") dig() rest(1) end end end function tossJunk() for slot = 1, 16 do turtle.select(slot) if turtle.getItemCount(slot) == 64 then if slot == cobbleSlot then turtle.drop(63) report("Dropping Junk.") elseif slot == dirtSlot then turtle.drop(63) report("Dropping Junk.") elseif slot == gravelSlot then turtle.drop(63) report("Dropping Junk.") else end end end end function checkFull() turtle.select(16) if turtle.getItemCount(16) > 1 then goHome() goBack() end turtle.select(fuelSlot) if turtle.getItemCount(fuelSlot) < 1 then goHome() goBack() end end function checkDig() tossJunk() checkFull() for slot = stoneSlot, cobbleSlot do turtle.select(slot) if turtle.compare(slot) then if slot == stoneSlot then turtle.select(cobbleSlot) dig() break elseif slot == dirtSlot then turtle.select(dirtSlot) dig() break elseif slot == gravelSlot then gravel() break elseif slot == cobbleSlot then report("Cobble generator found.") goHome() end else end end dig() end function evaluate() junk = 0 for slot = stoneSlot, cobbleSlot do turtle.select(slot) if turtle.compare() then junk = junk + 1 end end if junk == 0 then dig() report("Treasure!") else end end function findOre() report("Scanning...") lt() evaluate() lt() lt() evaluate() lt() end function digFd() checkDig() rest(1) checkDig() fd() end function branch() if h == west then nbl = bl - (2 * bl) while z > nbl do digFd() findOre() end elseif h == east then nbl = bl while z < nbl do digFd() findOre() end end findZ(t) end function trunk() findY(b) while x <= tl do for i = 1, 3 do digFd() end face(west) branch() face(east) branch() face(north) end b = b + 1 dumpLoc() goHome() end --INTERFACE-- function startup() clear() print("Welcome to the BranchMine program! :)") print("please set a few starting variables.") sleep(1) clear() print("How long should the trunk be?") print() write("Trunk Length: ") input = read() tl = tonumber(input) print() print("trunk length set to "..tostring(tl)) sleep(1) clear() print("How long should the branches be?") print() write("Branch Length: ") input = read() bl = tonumber(input) print() print("Branch length set to "..tostring(tl)) sleep(1) clear() print("Thank you for your input.") print("Preparing files for mining operation.") dumpLoc() report("Getting fuel.") getFuel() report("Finding Bedrock layer.") findBedrock() dumpLoc() end --PROGRAM-- getLoc() goHome() while b < -5 do trunk() end