Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local programParam = { ... }
- local startLoc = { 0, 0, 0, 1 }
- local lastGoodLoc = { 0, 0, 0, 1 }
- local currentLoc = { 0, 0, 0, 1 }
- local hitBedrock = false
- local doneMining = false
- local rtnToStart = false
- local needFuel, returnAndUnload, travelTo, turnRight, turnLeft, unloadInv
- -- Save current location for later use
- local function saveLoc()
- local newSaveLoc = {};
- newSaveLoc[1] = currentLoc[1]
- newSaveLoc[2] = currentLoc[2]
- newSaveLoc[3] = currentLoc[3]
- newSaveLoc[4] = currentLoc[4]
- return newSaveLoc
- end
- -- When out of fuel, go back to start and wait for fuel
- local function goWaitForFuel()
- lastGoodLoc = saveLoc()
- rtnToStart = true
- travelTo(startLoc)
- turnRight()
- turnRight()
- unloadInv()
- turnLeft()
- turnLeft()
- print("NEED MOAR FUELS!")
- while needFuel() do
- os.pullEvent("turtle_inventory")
- end
- rtnToStart = false
- travelTo(lastGoodLoc)
- end
- -- determine if fuel is needed
- function needFuel()
- local fuelReq
- if rtnToStart then
- -- If traveling to starting location, calculate needed fuel by:
- fuelReq = (math.abs(currentLoc[1]) + math.abs(currentLoc[2]) + math.abs(currentLoc[3]) + 2)
- else
- -- If traveling to last saved location, calculate needed fuel by:
- fuelReq = (math.abs(lastGoodLoc[1]) + math.abs(lastGoodLoc[2]) + math.abs(lastGoodLoc[3]) + 2)
- end
- local fuelNeeded = false
- -- Check for unlimted fuel
- if turtle.getFuelLevel() == "unlimited" then
- return false
- else
- if fuelReq > turtle.getFuelLevel() then
- fuelNeeded = true
- for i = 1, 16 do
- if fuelNeeded then
- -- Select one inventory slot at a time, then try and burn it.
- -- If burn successful, repeat until enough fuel has been used.
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(1) then
- if turtle.getItemCount(i) > 0 then
- while fuelReq > turtle.getFuelLevel() and turtle.refuel(1) do
- end
- end
- if fuelReq < turtle.getFuelLevel() then
- turtle.select(1)
- fuelNeeded = false
- break
- end
- end
- end
- end
- end
- turtle.select(1)
- end
- -- Return result
- if fuelNeeded then
- return true
- else
- return false
- end
- end
- end
- -- Check inventory for free space
- local function checkInvSpace()
- local invFull = true
- local totalItemCount = 0
- for i = 1, 16 do
- -- Look for empty inventory spaces
- local itemCount = turtle.getItemCount(i)
- if itemCount == 0 then
- invFull = false
- end
- totalItemCount = totalItemCount + itemCount
- end
- -- Return result
- if invFull then
- print("Inventory full.")
- return false
- else
- return true
- end
- end
- -- Update x, y, movement
- local function updateLoc()
- if currentLoc[4] == 1 then
- currentLoc[2] = currentLoc[2] + 1
- elseif currentLoc[4] == 2 then
- currentLoc[1] = currentLoc[1] + 1
- elseif currentLoc[4] == 3 then
- currentLoc[2] = currentLoc[2] - 1
- else
- currentLoc[1] = currentLoc[1] - 1
- end
- end
- -- Forward mining movement
- local function moveForward()
- if needFuel() then
- goWaitForFuel()
- end
- while not turtle.forward() do
- turtle.attack()
- turtle.dig()
- if not checkInvSpace() and not rtnToStart then
- returnAndUnload()
- end
- sleep(0.5)
- end
- local checkSucc, data = turtle.inspectDown()
- if data.name == "minecraft:bedrock" then
- hitBedrock = true
- end
- local checkSucc, data = turtle.inspect()
- if data.name == "minecraft:bedrock" then
- hitBedrock = true
- turtle.up()
- currentLoc[3] = currentLoc[3] + 1
- end
- -- Update location for movement
- updateLoc()
- end
- -- Upward mining movement
- local function moveUp()
- if needFuel() then
- goWaitForFuel()
- end
- while not turtle.up() do
- turtle.attackUp()
- turtle.digUp()
- if not checkInvSpace() and not rtnToStart then
- returnAndUnload()
- end
- sleep(0.5)
- end
- -- Update location for movement
- currentLoc[3] = currentLoc[3] + 1
- end
- -- Downward mining movement
- local function moveDown()
- if needFuel() then
- goWaitForFuel()
- end
- while not turtle.down() do
- turtle.attackDown()
- turtle.digDown()
- if not checkInvSpace() and not rtnToStart then
- returnAndUnload()
- end
- sleep(0.5)
- end
- -- Update location for movement
- currentLoc[3] = currentLoc[3] - 1
- end
- -- Rotate turtle counterclockwise
- function turnLeft()
- if turtle.turnLeft() then
- currentLoc[4] = currentLoc[4] - 1
- if currentLoc[4] < 1 then
- currentLoc[4] = 4
- end
- else
- print("Error! Unable to turn left.")
- end
- end
- -- Rotate turtle clockwise
- function turnRight()
- if turtle.turnRight() then
- currentLoc[4] = currentLoc[4] + 1
- if currentLoc[4] > 4 then
- currentLoc[4] = 1
- end
- else
- print("Error! Unable to turn right.")
- end
- end
- -- Unload inventory
- function unloadInv()
- -- If mining is complete, unload everything
- if doneMining then
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- else
- -- Unload only what can't be used as fuel
- for i = 1, 16 do
- turtle.select(i)
- if not turtle.refuel(1) then
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- end
- -- Check if at start
- local function isAtStart()
- return currentLoc[1] == startLoc[1] and currentLoc[2] ==startLoc[2] and currentLoc[3] == startLoc[3] and currentLoc[4] == startLoc[4]
- end
- -- Travel to a coordinate
- function travelTo(locArrayIn)
- -- Below end location
- if currentLoc[3] < locArrayIn[3] then
- while currentLoc[3] ~= locArrayIn[3] do
- moveUp()
- end
- end
- -- If left of end location
- if currentLoc[1] < locArrayIn[1] then
- while currentLoc[4] ~= 2 do
- turnRight()
- end
- while currentLoc[1] ~= locArrayIn[1] do
- moveForward()
- end
- end
- -- If right of end location
- if currentLoc[1] > locArrayIn[1] then
- while currentLoc[4] ~= 4 do
- turnLeft()
- end
- while currentLoc[1] ~= locArrayIn[1] do
- moveForward()
- end
- end
- -- If behind end location
- if currentLoc[2] < locArrayIn[2] then
- while currentLoc[4] ~= 1 do
- turnRight()
- end
- while currentLoc[2] ~= locArrayIn[2] do
- moveForward()
- end
- end
- -- If in front of end location
- if currentLoc[2] > locArrayIn[2] then
- while currentLoc[4] ~= 3 do
- turnLeft()
- end
- while currentLoc[2] ~= locArrayIn[2] do
- moveForward()
- end
- end
- -- Above end location
- if currentLoc[3] > locArrayIn[3] then
- while currentLoc[3] ~= locArrayIn[3] do
- moveDown()
- end
- end
- -- Orient turtle to correct location
- while currentLoc[4] ~= locArrayIn[4] do
- turnRight()
- end
- end
- function returnAndUnload()
- lastGoodLoc = saveLoc()
- rtnToStart = true
- travelTo(startLoc)
- turnRight()
- turnRight()
- unloadInv()
- rtnToStart = false
- if not doneMining then
- turnLeft()
- turnLeft()
- travelTo(lastGoodLoc)
- end
- end
- -- Start digging that hole!
- --
- while true do
- -- Dig trench for however long required
- for i = 1, (tonumber(programParam[1]) - 1) do
- moveForward()
- end
- -- Finish the row
- -- See if dig is done
- if hitBedrock then
- doneMining = true
- returnAndUnload()
- print("Job's done.")
- break
- else
- -- If not, turn around, go down, and continue
- turnRight()
- turnRight()
- moveDown()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment