function getInput(prompt) if(prompt ~= nil) then print(prompt) end local inputString = read() return inputString end function chkNoObs(digReturn) -- check if no obstruction if(digReturn == "obstruction") then ShouldStopDig = true print("obstruction detected. stopping!") for i = 0, 100 do turtle.turnRight() end return false end return true end Coords = {0,0,0} FacingDirection = "n" Directions = {"n","e","s","w"} function trackTurnRight() for i, v in ipairs(Directions) do if(v == FacingDirection) then print("v "..v) if(i == 4) then FacingDirection = Directions[1] else FacingDirection = Directions[i+1] end end end print("facing "..FacingDirection) return turtle.turnRight() end function trackTurnLeft() for i, v in ipairs(Directions) do if(v == FacingDirection) then print("v "..v) if(i == 1) then FacingDirection = Directions[4] else FacingDirection = Directions[i-1] end end end print("facing "..FacingDirection) return turtle.turnLeft() end function faceDirection(desiredDirection) -- there's a more efficient way to do it but im lazy flagDidTurn = false if(desiredDirection == "n") then flagDidTurn = true if(FacingDirection == "e") then turtle.turnRight() end if(FacingDirection == "w") then turtle.turnLeft() end if(FacingDirection == "s") then turtle.turnRight() turtle.turnRight() end FacingDirection = "n" end if(desiredDirection == "e") then flagDidTurn = true if(FacingDirection == "s") then turtle.turnRight() end if(FacingDirection == "n") then turtle.turnLeft() end if(FacingDirection == "w") then turtle.turnRight() turtle.turnRight() end FacingDirection = "e" end if(desiredDirection == "s") then flagDidTurn = true if(FacingDirection == "w") then turtle.turnRight() end if(FacingDirection == "e") then turtle.turnLeft() end if(FacingDirection == "n") then turtle.turnRight() turtle.turnRight() end FacingDirection = "s" end if(desiredDirection == "w") then flagDidTurn = true if(FacingDirection == "n") then turtle.turnLeft() end if(FacingDirection == "s") then turtle.turnRight() end if(FacingDirection == "e") then turtle.turnRight() turtle.turnRight() end FacingDirection = "w" end if(flagDidTurn == false) then ShouldStopDig = true print("ERROR: face direction command failed!") end end function trackForward() if(FacingDirection == "n") then Coords[1] = Coords[1] + 1 end if(FacingDirection == "s") then Coords[1] = Coords[1] - 1 end if(FacingDirection == "e") then Coords[2] = Coords[2] + 1 end if(FacingDirection == "w") then Coords[2] = Coords[2] - 1 end return turtle.forward() end function trackUp() Coords[3] = Coords[3] + 1 return turtle.up() end function trackDown() Coords[3] = Coords[3] - 1 return turtle.down() end function getNumberParameter(prompt, minValue, maxValue) local returnValue = 0 while true do local shouldLoop = true while shouldLoop do term.clear() if(type(prompt) == "string") then print(prompt) elseif(type(prompt) == "table") then for i, v in ipairs(prompt) do print(v) end else print("ERROR: invalid prompt!! please report this bug.") print(textutils.serialise(prompt)) end returnValue = tonumber(getInput()) -- sanitizing user input; -- making sure the input is a number and -- removing negatives from the result if if(minValue >= 0) then returnValue = math.abs(returnValue) end if (type(returnValue ~= nil) and returnValue >= minValue and returnValue <= maxValue) then shouldLoop = false end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Input " .. returnValue .. "? (yes to continue)") == "yes") then break end end return returnValue end function tryDig(dir) local validDirectionFlag = false local didDig = false local didMove = false if(dir == "up") then validDirectionFlag = true didDig = turtle.digUp() didMove = trackUp() end if(dir == "down") then validDirectionFlag = true didDig = turtle.digDown() didMove = trackDown() end if(dir == "forward" or dir == "front") then validDirectionFlag = true didDig = turtle.dig() didMove = trackForward() end if(dir == "back" or dir == "backward") then validDirectionFlag = true trackTurnRight() trackTurnRight() didDig = turtle.dig() didMove = trackForward() trackTurnRight() trackTurnRight() end if(validDirectionFlag ~= true) then return "invalidDirection" end if(didDig == true or didMove == true) then return "success" end if(didDig == false and didMove == false) then return "obstruction" end end -- initializing certain arrays fuelArray = { {"minecraft:coal", 80}, {"minecraft:charcoal", 80}, {"minecraft:coal_block", 800}, {"quark:charcoal_block", 800}, {"minecraft:lava_bucket", 1000}, {"minecraft:dried_kelp_block", 200}, } FuelBank = {} function setupFuelBank() FuelBank = {} for i = 1, 16 do local toAdd = {i, false} table.insert(FuelBank, toAdd) end end function calculateRequiredFuel(type) if(type == 0) then return (DigX * DigY * DigZ) end if(type == 1) then return (50) end end function checkFuelCount() -- only checks for coal & coal blocks :D local fuelCount = 0 for i = 1, 16 do local checkedItem = turtle.getItemDetail(i) if(checkedItem ~= nil) then for n, v in ipairs(fuelArray) do if(checkedItem.name == v[1]) then fuelCount = fuelCount + (v[2] * checkedItem.count) print("found " .. (v[2] * checkedItem.count) .. " fuel from block " .. checkedItem.name .. " in slot " .. i) FuelBank[i] = {i, true} break end end end print(turtle.getFuelLevel() .. " fuel in turtle out of " .. turtle.getFuelLimit()) print("fuel amount in inventory is " .. fuelCount) return fuelCount end end function smartRefuel(shouldAbort) print("attempting smart refuel...") if(checkFuelCount() == 0) then print("No fuel detected!") if(shouldAbort == true) then InDigLoop = false end return false end local fuelAmount = 0 for n, fuelBankItem in ipairs(FuelBank) do if(fuelBankItem[2] == true) then turtle.select(fuelBankItem[1]) local checkedItem = turtle.getItemDetail() for j, v in ipairs(fuelArray) do if(checkedItem.name == v[1]) then local fuelAmount = v[2] print("found " .. (fuelAmount) .. " fuel from block " .. v[1] .. " in slot " .. fuelBankItem[1]) break end end turtle.refuel() break end end return fuelAmount end -- start of stair program InDigLoop = false DigType = 0 DigX = 1 DigY = 1 DigZ = 1 DigDir = {0,0} ShouldReturn = 0 setupFuelBank() --[[ 0 = dig in a square x is forward, y is width, z is height 1 = stairway x is total distance, y is width --]] -- input dig mode local textPrompt = {"Dig modes:","0 - dig in a cube","1 - dig a stairway (not implemented)","2 - test fuelcheck","What dig mode do you wish to enable?"} DigType = getNumberParameter(textPrompt, 0, 2) --[[ while true do local shouldLoop = true while shouldLoop do term.clear() print("Dig modes:") print("0 - dig in a cube") print("1 - dig a stairway (not implemented)") print("2 - test fuelcheck") DigType = tonumber(getInput("What dig mode do you wish to enable?")) -- sanitizing user input; -- making sure the input is a number and -- removing negatives from the result if so if (type(DigType ~= nil) and DigType >= 0 and DigType < 3 ) then shouldLoop = false DigType = math.abs(DigType) end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Dig mode of " .. DigType .. "? (yes to continue)") == "yes") then break end end --]] if(DigType == 2) then print(checkFuelCount()) read() os.reboot() end -- input dig direction A if(DigType == 0) then local textPrompt = {"Dig modes:","0 - dig to the right","1 - dig to the left","What dig direction do you wish to use?"} DigDir[1] = getNumberParameter(textPrompt, 0, 1) end --[[ while true do local shouldLoop = true while shouldLoop do term.clear() print("Dig Directions") if (DigType == 0) then print("0 - dig to the right") print("1 - dig to the left") end if (DigType == 1) then print("0 - dig down") print("1 - dig up") end DigDir[0] = tonumber(getInput("What dig direction do you wish to use?")) -- sanitizing user input; -- making sure the input is a number and -- removing negatives from the result if so if (type(DigDir[0] ~= nil) and DigDir[0] >= 0 and DigDir[0] < 2 ) then shouldLoop = false DigDir[0] = math.abs(DigDir[0]) end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Dig direction of " .. DigDir[0] .. "? (yes to continue)") == "yes") then break end end --]] -- input dig direction B local textPrompt = {"Dig modes:","0 - dig up","1 - dig down","What dig direction do you wish to use?"} DigDir[2] = getNumberParameter(textPrompt, 0, 1) --[[ while true do local shouldLoop = true while shouldLoop do term.clear() print("Dig Directions") if (DigType == 0) then print("0 - dig up") print("1 - dig down") end if (DigType == 1) then break end DigDir[1] = tonumber(getInput("What dig direction do you wish to use?")) -- sanitizing user input; -- making sure the input is a number and -- removing negatives from the result if so if (type(DigDir[1] ~= nil) and DigDir[1] >= 0 and DigDir[1] < 2 ) then shouldLoop = false DigDir[1] = math.abs(DigDir[1]) end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Dig direction of " .. DigDir[1] .. "? (yes to continue)") == "yes") then break end end --]] -- input dig x local textPrompt = "How many blocks forward?" DigX = getNumberParameter(textPrompt, 1, 10000) --[[ while true do local shouldLoop = true while shouldLoop do term.clear() DigX = tonumber(getInput("How many blocks forward? (INCLUDING the block the turtle is in)")) -- sanitizing user input; -- making sure the input is a number and -- removing negatives from the result if so if (type(DigX ~= nil) and math.abs(DigX) >= 1) then shouldLoop = false DigX = math.abs(DigX) end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Dig forward for " .. DigX .. " blocks? (yes to continue)") == "yes") then break end end --]] -- input dig y local textPrompt = "How many blocks wide?" DigY = getNumberParameter(textPrompt, 1, 10000) --[[ while true do local shouldLoop = true while shouldLoop do term.clear() DigY = tonumber(getInput("How many blocks wide?")) if (type(DigY ~= nil) and math.abs(DigY) >= 1) then shouldLoop = false DigY = math.abs(DigY) end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Dig " .. DigY .. " blocks wide? (yes to continue)") == "yes") then break end end --]] -- input dig z local textPrompt = "How many blocks high?" DigZ = getNumberParameter(textPrompt, 1, 10000) --[[ while true do local shouldLoop = true while shouldLoop do term.clear() if(digType == 1) then break end DigZ = tonumber(getInput("How many blocks high?")) term.clear() if (type(DigZ ~= nil) and math.abs(DigZ) >= 1) then shouldLoop = false DigZ = math.abs(DigZ) end end -- if the player's answer is yes, break from the stair length selection loop if(getInput("Dig " .. DigZ .. " blocks high? (yes to continue)") == "yes") then break end end --]] local textPrompt = {"Return to starting spot after finishing?", "0 - do not return", "1 - return to initial spot"} ShouldReturn = getNumberParameter(textPrompt, 0, 1) term.clear() print("Commencing pre-dig check.") if((turtle.getFuelLevel() + checkFuelCount()) < calculateRequiredFuel(DigType)) then print("Not enough fuel present to dig, aborting. Press enter to continue...") read() os.reboot() end while(turtle.getFuelLevel() < calculateRequiredFuel(DigType)) do smartRefuel(true) end print("fuel is sufficient. commencing dig operation.") ShouldStopDig = false if(DigType == 0) then local digFlipped = false local stopCheck = true local vertiFlipped = false if(DigDir[1] == 1) then digFlipped = true end for z = 1, DigZ do for y = 1, DigY do for x = 1, DigX do if(ShouldStopDig == false) then if((x == DigX) and (y < DigY)) then if(digFlipped == false) then trackTurnRight() chkNoObs(tryDig("forward")) trackTurnRight() digFlipped = true else trackTurnLeft() chkNoObs(tryDig("forward")) trackTurnLeft() digFlipped = false end else if((y == DigY and x == DigX) == false) then if(chkNoObs(tryDig("forward"))) then end end end end end end if(ShouldStopDig == false) then if(z < DigZ) then if(DigDir[2] == 0) then if(chkNoObs(tryDig("up"))) then end else if(chkNoObs(tryDig("down"))) then end end trackTurnLeft() trackTurnLeft() if(vertiFlipped == false) then vertiFlipped = true else vertiFlipped = false end end end end print("attempting return...") if(ShouldReturn) then --[[ for i = 1, DigZ-1 do if(DigDir[2] == 0) then trackDown() else trackUp() end end if(((digFlipped == false and DigDir[1] == 0) or (digFlipped == true and DigDir[1] == 1)) and DigX > 1) then trackTurnRight() trackTurnRight() for i = 1, DigX-1 do trackForward() end end -- --if(DigDir[1] == 1) then --if(digFlipped == true) then digFlipped = false end --if(digFlipped == false) then digFlipped = true end --end --if(digFlipped == true) then --turtle.turnRight() --elseif(digFlipped == false) then --turtle.turnLeft() --end if(DigDir[1] == 0) then trackTurnRight() else trackTurnLeft() end for i = 1, DigY-1 do trackForward() end if(DigDir[1] == 0) then trackTurnRight() else trackTurnLeft() end ]] -- left-right if(Coords[2] > 0) then faceDirection("w") end if(Coords[2] < 0) then faceDirection("e") end while(Coords[2] ~= 0 and ShouldStopDig == false) do if(trackForward() == false) then if(tryDig("forward") == false) then ShouldStopDig = true print("obstruction detected. unable to return to starting location.") end end end -- forward-back return if(Coords[1] > 0) then faceDirection("s") end if(Coords[1] < 0) then faceDirection("n") end while(Coords[1] ~= 0 and ShouldStopDig == false) do if(trackForward() == false) then if(tryDig("forward") == false) then ShouldStopDig = true print("obstruction detected. unable to return to starting location.") end end end -- up-down return while(Coords[3] ~= 0 and ShouldStopDig == false) do if(Coords[3] > 0) then if(trackDown() == false) then if(tryDig("down") == false) then ShouldStopDig = true print("obstruction detected. unable to return to starting location.") end end end if(Coords[3] < 0) then if(trackUp() == false) then if(tryDig("up") == false) then ShouldStopDig = true print("obstruction detected. unable to return to starting location.") end end end end print("returned to starting location") end print("Digging complete!") end if(DigType == 1) then local digFlipped = false local stopCheck = true end