--Created by makeo. All rights reserved. --DO NOT EDIT local FUEL_SLOT = 1 local CHEST_FUEL_SLOT = 1 local ENDER_CHEST_SLOT = 2 local DIG_SLOT = 3 --save movments function move(move, detect, dig, attack) if needsPitstop() then doPitstop() end local first = true while not move() do if detect() then turtle.select(DIG_SLOT) dig() else attack() end if not first then sleep(0.5) end first = false end end function moveForward() move(turtle.forward, turtle.detect, turtle.dig, turtle.attack) end function moveBack() if not turtle.back() then turn180() moveForward() turn180() end end function moveUp() move(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp) end function moveDown() move(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown) end function turn180() turtle.turnLeft() turtle.turnLeft() end --pitstop function doPitstop() if turtle.getItemDetail(ENDER_CHEST_SLOT) == nil then if peripheral.getType("bottom") ~= "ender_chest" then error("No ender chest found.") end else if turtle.detectDown() then turtle.select(DIG_SLOT) turtle.digDown() end turtle.select(ENDER_CHEST_SLOT) turtle.placeDown() sleep(0.5) end if (not hasSpace()) or getMovingHome() then for i = 3, 16, 1 do while turtle.getItemCount(i) > 0 do turtle.select(i) turtle.dropDown() sleep(0.5) if not moved then sleep(2) end end end end turtle.select(ENDER_CHEST_SLOT) turtle.digDown() end function needsPitstop() return (not hasSpace()) or turtle.getFuelLevel() < 5 end function hasSpace() local count = 0 for i = 3, 16, 1 do if turtle.getItemCount(i) == 0 then count = count + 1 end end return count > 1 end function readNumber(minVal) local num = -1 repeat if num ~= -1 then print("Text is not a number.") end num = tonumber(read()) if num ~= nil and num < minVal then print("Number is out of bound.") num = nil end until num ~= nil return num end --ore stuff function isOre() local success, data = turtle.inspect() return checkOre(success, data) end function isOreUp() local success, data = turtle.inspectUp() return checkOre(success, data) end function isOreDown() local success, data = turtle.inspectDown() return checkOre(success, data) end function checkOre(success, data) if success then local name = data["name"] if name ~= nil then --Alternative name bypass if name == "Forestry:resources" or name == "TConstruct:SearedBrick" or name == "denseores:block0" or name == "rftools:dimensionalShardBlock" or name == "harvestcraft:salt" then return true end local index = string.find(name, ":") if index ~= nil then local part = string.lower(string.sub(name, index)) local isOre = string.find(part, "ore") return isOre ~= nil end end end return false end --vein stuff function mineVein() while true do if isOre() then veinMoveForward() elseif isOreUp() then veinMoveUp() elseif isOreDown() then veinMoveDown() else local success = false for i = 1, 3, 1 do veinMoveLeft() if isOre() then veinMoveForward() success = true break end end if not success then if not popVeinMovment() then return end end end sleep(0.5) end end function veinMoveForward() moveForward() pushToVeinStack(1) end function veinMoveUp() moveUp() pushToVeinStack(2) end function veinMoveDown() moveDown() pushToVeinStack(3) end function veinMoveLeft() turtle.turnLeft() pushToVeinStack(4) end function veinMoveRight() turtle.turnRight() pushToVeinStack(5) end function veinMoveBack() moveBack() pushToVeinStack(6) end function popVeinMovment() local direction = getFromVeinStack() if direction == nil then return false end if direction == 1 then moveBack() elseif direction == 2 then moveDown() elseif direction == 3 then moveUp() elseif direction == 4 then turtle.turnRight() removeLastVeinStack() return popVeinMovment() elseif direction == 5 then turtle.turnLeft() removeLastVeinStack() return popVeinMovment() elseif direction == 6 then moveForward() end removeLastVeinStack() return true; end function pushToVeinStack(direction) local stack = getVeinStack() if stack == nil then stack = {} end stack[#stack + 1] = direction stack = optimizeVeinStack(stack) saveVeinStack(stack, #stack) end function getFromVeinStack() local data = getVeinStack() if data ~= nil then return data[#data] end return nil end function optimizeVeinStack(data) local lastAction = 0 local actionCount = 0 local i = 1 while i <= #data do --turn right and then left is somewhat useless if (data[i] == 4 and lastAction == 5) or (data[i] == 5 and lastAction == 4) then data = moveArrayContent(data, i + 1, 2) if #data < 1 then break end i = 1 lastAction = 0 actionCount = 0 end if data[i] ~= lastAction then lastAction = 0 actionCount = 0 end if data[i] == 4 then lastAction = 4 actionCount = actionCount + 1 elseif data[i] == 5 then lastAction = 5 actionCount = actionCount + 1 end if actionCount == 3 then local newAction = 4 if lastAction == 4 then newAction = 5 end data = moveArrayContent(data, i + 1, 2) data[i - 2] = newAction i = 0 lastAction = 0 actionCount = 0 end i = i + 1 end return data end function moveArrayContent(array, startIndex, amount) local size = #array for i = startIndex, size + amount, 1 do array[i - amount] = array[i] array[i] = nil end return array end function removeLastVeinStack() local data = getVeinStack() saveVeinStack(data, #data - 1) end function saveVeinStack(data, length) if data ~= nil then local dataLeng = #data for i = length + 1, dataLeng, 1 do data[i] = nil end end if #data < 1 then data = nil end setVeinStack(data) end function getVeinStack() return getVariable("veinStack") end function setVeinStack(value) setVariable("veinStack", value) end function getVariable(name) local vars = getVariables() if vars ~= nil then return vars[name] end return nil end function setVariable(name, value) local vars = getVariables() if vars == nil then vars = {} end vars[name] = value local handle = fs.open("Vars.dat", "w") handle.writeLine(textutils.serialize(vars)) handle.close() end function getVariables() local handle = fs.open("Vars.dat", "r") if handle ~= nil then local raw = handle.readAll() handle.close() if raw ~= nil then return textutils.unserialize(raw) end end return nil end function getStripLength() return getVariable("stripLength") end function setStripLength(value) setVariable("stripLength", value) end function getStripPos() return getVariable("stripPos") end function setStripPos(value) setVariable("stripPos", value) end function getStripOnGround() return getVariable("stripGround") end function setStripOnGround(value) return setVariable("stripGround", value) end function getStripHasDug() return getVariable("stripHasDug") end function setStripHasDug(value) return setVariable("stripHasDug", value) end function getStripHasFinished() return getVariable("stripHasFinished") end function setStripHasFinished(value) return setVariable("stripHasFinished", value) end function digStrip(gotoStart) local length = getStripLength() if not getStripHasFinished() then while getStripPos() <= length or (not getStripHasDug()) do mineVein() if getStripHasDug() then moveForward() setStripPos(getStripPos() + 1) setStripHasDug(false) else if getStripOnGround() then moveUp() setStripOnGround(false) else moveDown() setStripOnGround(true) end setStripHasDug(true) end end setStripHasFinished(true) end mineVein() if not getStripOnGround() then moveDown() setStripOnGround(true) mineVein() end if gotoStart then while getStripPos() > 1 do moveBack() setStripPos(getStripPos() - 1) sleep(0.5) end end end function clearStripParams() setStripLength(0) setStripPos(1) setStripOnGround(true) setStripHasDug(false) setStripHasFinished(false) end function getIsPreparing() return getVariable("isPreparing") end function setIsPreparing(value) return setVariable("isPreparing", value) end function getSpacing() return getVariable("spacing") end function setSpacing(value) return setVariable("spacing", value) end function getDepth() return getVariable("depth") end function setDepth(value) return setVariable("depth", value) end function getStripCount() return getVariable("stripCount") end function setStripCount(value) return setVariable("stripCount", value) end function getRemainingStripCount() return getVariable("remainStripCount") end function setRemainingStripCount(value) return setVariable("remainStripCount", value) end function getMovingHome() return getVariable("movHome") end function setMovingHome(value) return setVariable("movHome", value) end function prepareNextStrip() if not getIsPreparing() then turtle.turnRight() setIsPreparing(true) clearStripParams() end setStripLength(getSpacing() + 1) digStrip(false) clearStripParams() turtle.turnLeft() setIsPreparing(false) setStripHasFinished(false) end function goHome() if not getMovingHome() then turtle.turnLeft() setMovingHome(true) setStripPos(1) end local length = ((getStripCount() - 1) * (getSpacing() + 1)) while getStripPos() <= length do moveForward() setStripPos(getStripPos() + 1) end doPitstop() turtle.turnRight() fs.delete("Vars.dat") print("All strips cleared.") error() end function initVars() clearStripParams() setIsPreparing(false) setMovingHome(false) print("Enter the spacing between the strips.") setSpacing(readNumber(0)) print("Enter the depth of each strip.") setDepth(readNumber(1)) print("Enter the amount of strips.") local stripCount = readNumber(1) setStripCount(stripCount) setRemainingStripCount(stripCount) end if getSpacing() == nil then initVars() end while true do if getMovingHome() then goHome() end if getIsPreparing() then prepareNextStrip() elseif not getStripHasFinished() then setStripLength(getDepth()) digStrip(true) clearStripParams() local remStrips = getRemainingStripCount() - 1 setRemainingStripCount(remStrips) if remStrips < 1 then goHome() end prepareNextStrip() end end