Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if turtle.getItemDetail(1) == nil then
- print ("Put fuel in the first slot of the turtle and run again!\n")
- end
- local data = turtle.getItemDetail(1) -- fuel slot 1
- local fuelType = data.name
- local args = {...}
- --local maxDistance = tonumber(args[1]) or 30 -- straight line
- local distance = 0
- local path = {}
- local target = "minecraft:sugar_cane"
- local skipped = 0
- local sleepTime = 60 -- in seconds
- -- resting and current Position
- local rPosition = { x = tonumber(args[1]), y = tonumber(args[2]), z = tonumber(args[3]) }
- local cPosition = {}
- cPosition.x, cPosition.y, cPosition.z = gps.locate()
- -- back to start?
- if rPosition.x ~= cPosition.x or
- rPosition.y ~= cPosition.y or
- rPosition.z ~= cPosition.z then
- -- go to resting Position first
- -- move turtle above sugar canes
- turtle.up()
- turtle.up()
- cPosition.x, cPosition.y, cPosition.z = gps.locate()
- print ("I am at:\nx: " .. cPosition.x .. "\ny: " .. cPosition.y + 2 .. "\nz: " .. cPosition.z .. "\n")
- print ("I want to be at:\nx: " .. rPosition.x .. "\ny: " .. rPosition.y .. "\nz: " .. rPosition.z .. "\n")
- turtle.forward()
- local tmp = {}
- tmp.x, tmp.y, tmp.z = gps.locate()
- --[[
- print (
- math.abs(math.floor(tmp.x) - math.floor(rPosition.x)) < math.abs(math.floor(cPosition.x) - math.floor(rPosition.x)),
- math.abs(math.floor(tmp.y) - math.floor(rPosition.y)) < math.abs(math.floor(cPosition.y) - math.floor(rPosition.y)),
- math.abs(math.floor(tmp.z) - math.floor(rPosition.z)) < math.abs(math.floor(cPosition.z) - math.floor(rPosition.z))
- )
- ]]
- while not (math.abs(math.floor(tmp.z) - math.floor(rPosition.z)) < math.abs(math.floor(cPosition.z) - math.floor(rPosition.z))) and rPosition.z ~= tmp.z do
- turtle.back()
- turtle.turnRight()
- turtle.forward()
- tmp.x, tmp.y, tmp.z = gps.locate()
- end
- while math.abs(math.floor(tmp.z) - math.floor(rPosition.z)) < math.abs(math.floor(cPosition.z) - math.floor(rPosition.z))
- and rPosition.z ~= tmp.z do
- turtle.forward()
- tmp.x, tmp.y, tmp.z = gps.locate()
- end
- print ("Reached z position\n")
- turtle.forward()
- while not (math.abs(math.floor(tmp.x) - math.floor(rPosition.x)) < math.abs(math.floor(cPosition.x) - math.floor(rPosition.x))) and rPosition.x ~= tmp.x do
- turtle.back()
- turtle.turnRight()
- turtle.forward()
- tmp.x, tmp.y, tmp.z = gps.locate()
- end
- while math.abs(math.floor(tmp.x) - math.floor(rPosition.x)) < math.abs(math.floor(cPosition.x) - math.floor(rPosition.x))
- and rPosition.x ~= tmp.x do
- turtle.forward()
- tmp.x, tmp.y, tmp.z = gps.locate()
- end
- print ("Reached x position\n")
- while tmp.y ~= rPosition.y do
- turtle.down()
- tmp.x, tmp.y, tmp.z = gps.locate()
- end
- print ("Reached y position\n")
- turtle.turnRight()
- turtle.turnRight()
- end
- local _, tmpI = turtle.inspect()
- while tmpI.name ~= target do
- print ("Turning to " .. target .. "\n")
- turtle.turnRight()
- _, tmpI = turtle.inspect()
- end
- function harvest ()
- local _, data = turtle.inspect()
- if data.name == target then
- turtle.dig()
- turtle.suckUp()
- turtle.suck()
- turtle.suckDown()
- skipped = 0
- --print ("harvest true\n")
- return true
- end
- skipped = skipped + 1
- --print ("harvest false\n")
- return false
- end
- function checkFuel ()
- if turtle.getFuelLevel() <= #path + 10 then
- print ("no fuel\n")
- for i = 1, 16, 1 do
- if turtle.getItemDetail(i).name == fuelType then
- turtle.select(i)
- turtle.refuel()
- print ("refuel\n")
- return true
- end
- end
- print ("out of fuel\n")
- return false
- end
- return true
- end
- function run ()
- if not checkFuel() then return end
- local cond = false
- turtle.up()
- turtle.up()
- local _, data = turtle.inspect()
- if data.name == target then cond = true end
- turtle.down()
- while cond and skipped < 6 do -- ignore max distance for now
- print ("skipped: " .. skipped .. "\ndistance: " .. distance .. "\n")
- -- refuel
- if not checkFuel() then return end
- if harvest() then
- if turtle.forward() then
- distance = distance + 1
- table.insert(path, "forward")
- end
- else
- --print ("check left\n")
- turtle.turnLeft()
- if harvest() then
- if turtle.forward() then
- table.insert(path, "left")
- end
- else
- --print ("check right\n")
- turtle.turnRight()
- turtle.turnRight()
- if harvest() then
- if turtle.forward() then
- table.insert(path, "right")
- end
- else
- turtle.turnLeft()
- if turtle.forward() then
- distance = distance + 1
- table.insert(path, "forward")
- end
- end
- end
- end
- end
- print ("Moving back!\nDistance: " .. distance .. "\nPath Length: " .. #path .. "\n")
- turtle.turnRight()
- turtle.turnRight()
- for i = #path, 1, -1 do
- while not turtle.forward() do harvest() end
- if path[i] == "forward" then
- -- just forward
- elseif path[i] == "left" then
- turtle.turnRight()
- elseif path[i] == "right" then
- turtle.turnLeft()
- end
- end
- turtle.down()
- turtle.turnRight()
- turtle.turnRight()
- print ("wait " .. sleepTime .. "s\n")
- sleep(sleepTime)
- path = {}
- skipped = 0
- distance = 0
- print ("restart\n")
- run()
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement