os.loadAPI("/apis/storage") os.loadAPI("/apis/position") local db = storage position.hook() local t = turtle function invFull () for i = 2, 16 do if 0 == t.getItemCount(i) then return false end end return true end function face (dir) local F = position.get.F; ({ function () end, t.turnLeft, function () t.turnRight() t.turnRight() end, t.turnRight })[(F() + 4 - dir) % 4 + 1]() end local function goto (axis, N) local get = position.get if get[axis]() == N then return end local f if "Y" == axis then f = ({ [1] = t.up, [2] = t.down }) [get[axis]() < N and 1 or 2] else face(({ ["X"] = {3, 1}, ["Z"] = {0, 2} }) [axis][get[axis]() < N and 1 or 2]) f = t.forward end repeat f() until get[axis]() == N end function goHome () print("going home") goto("Y", 0) goto("X", 0) goto("Z", 0) end local bookmark = {} for i, v in ipairs({ "X", "Y", "Z", "F" }) do bookmark[v] = ( db.get("bookmark/"..v) or 0 ) - 0 end local function resume () print("resuming") local get = position.get goto("Z", bookmark.Z) goto("X", bookmark.X) goto("Y", bookmark.Y) face(bookmark.F) db.clear("bookmark") end local function pause () db.create("bookmark") for _, axis in ipairs({ "X", "Y", "Z", "F" }) do bookmark[axis] = position.get[axis]() db.set("bookmark/"..axis, bookmark[axis]) end end function emptyInv () for i = 2, 16 do if 0 < t.getItemCount(i) then t.select(i) if not t.dropUp() then print("dropoff full, waiting...") repeat sleep(1) until t.dropUp() end end end t.select(1) end function refuel () local fuelLevel = t.getFuelLevel() local fuelValue, softLimit for i = 2, 16 do t.select(i) if t.refuel(1) then fuelValue = t.getFuelLevel() - fuelLevel softLimit = t.getFuelLimit() - fuelValue repeat t.refuel(1) until t.getFuelLevel() > softLimit or 0 == t.getItemCount() if t.getFuelLevel() > softLimit then break end end end t.select(1) if softLimit and t.getFuelLevel() > softLimit then print("Refueling complete. Please take any leftover fuel back.") return true end return false end function sane_p2 (op) goHome() emptyInv() if 0 == t.getItemCount(1) then print("need torches") repeat sleep(1) until 0 < t.getItemCount(1) end if 400 > t.getFuelLevel() then print("need fuel") repeat sleep(1) until refuel() end storage.clear("autohome") resume() t.forward() end function sane (op) if 0 == t.getItemCount(1) or invFull() then storage.set("autohome", true) t.back() pause() sane_p2() end return (turtle.sane or turtle)[op]() end function dig () return sane("dig") end function digUp () return sane("digUp") end function digDown () return sane("digDown") end function hook () if t.sane then return false end t.sane = {} for _, f in ipairs({ "dig", "digUp", "digDown" }) do t.sane[f] = t[f] t[f] = function () return sane(f) end end end if storage.get("autohome") then sane_p2() end if storage.get("bookmark") then resume() end --]]