Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x = 0
- local y = 0
- local z = 0
- local dir = "north"
- function sortFuel()
- local data = turtle.getItemDetail(16)
- if data ~= nil then
- if data.name ~= "minecraft:coal" then
- turtle.select(16)
- sleep(0.25)
- turtle.drop()
- sleep(0.25)
- turtle.select(1)
- sleep(0.25)
- end
- end
- for i = 1,15 do
- data = turtle.getItemDetail(i)
- if data ~= nil then
- if data.name == "minecraft:coal" then
- turtle.select(i)
- turtle.transferTo(16)
- return
- end
- end
- end
- end
- function saveHere()
- local file = fs.open("here","w")
- file.writeLine(tostring(x).."\n"..tostring(y).."\n"..tostring(z).."\n"..dir)
- file.close()
- print("x:"..tostring(x)..", y:"..tostring(y)..", z:"..tostring(z))
- end
- function right()
- if dir == "north" then
- dir = "east"
- elseif dir == "east" then
- dir = "south"
- elseif dir == "south" then
- dir = "west"
- else
- dir = "north"
- end
- turtle.turnRight()
- saveHere()
- end
- function left()
- if dir == "north" then
- dir = "west"
- elseif dir == "west" then
- dir = "south"
- elseif dir == "south" then
- dir = "east"
- else
- dir = "north"
- end
- turtle.turnLeft()
- saveHere()
- end
- function forward()
- if turtle.getFuelLevel() < 500 then
- sortFuel()
- turtle.select(16)
- sleep(0.25)
- while turtle.getItemCount(16) > 2 and turtle.getFuelLevel() < 1000 do
- if turtle.getItemCount(16) > 2 then
- turtle.refuel(1)
- end
- end
- sleep(0.25)
- end
- if dir == "north" then
- z = z - 1
- elseif dir == "south" then
- z = z + 1
- elseif dir == "east" then
- x = x + 1
- else
- x = x - 1
- end
- if turtle.detect() then
- if string.find(block(),"turtle") then
- if math.random(1,2) == 2 then
- down()
- forward()
- up()
- return
- else
- up()
- forward()
- down()
- return
- end
- else
- repeat
- dig()
- sleep(0.5)
- until not turtle.detect()
- end
- end
- repeat
- dig()
- until turtle.forward()
- saveHere()
- end
- function back()
- if dir == "north" then
- z = z + 1
- elseif dir == "south" then
- z = z - 1
- elseif dir == "east" then
- x = x - 1
- else
- x = x + 1
- end
- repeat
- until turtle.back()
- saveHere()
- end
- function down()
- local tries = 250
- repeat
- digDown()
- tries = tries-1
- until turtle.down() or tries < 0
- if not (tries < 0) then
- y = y - 1
- end
- saveHere()
- end
- function up()
- repeat
- digUp()
- until turtle.up()
- y = y+1
- saveHere()
- end
- if fs.exists("here") then
- local file = fs.open("here","r")
- x = tonumber(file.readLine())
- y = tonumber(file.readLine())
- z = tonumber(file.readLine())
- dir = file.readLine()
- file.close()
- else
- local file = fs.open("here","w")
- file.writeLine("0\n0\n0\nnorth")
- file.close()
- end
- function block(o)
- local d,data = turtle.inspect()
- if o ~= nil then
- if o == "up" then
- d,data = turtle.inspectUp()
- else
- d,data = turtle.inspectDown()
- end
- end
- if not d then
- return "air"
- else
- return data.name
- end
- end
- function dig()
- while string.find(block(),"turtle")
- sleep(0.5)
- end
- turtle.dig()
- end
- function digUp()
- repeat
- sleep(0.5)
- until not string.find(block("up"),"turtle")
- turtle.digUp()
- end
- function digDown()
- repeat
- sleep(0.5)
- until not string.find(block("down"),"turtle")
- turtle.digDown()
- end
- function vien()
- if string.find(block(),"ore") then
- dig()
- forward()
- vien()
- back()
- end
- right()
- if string.find(block(),"ore") then
- dig()
- forward()
- vien()
- back()
- end
- right()
- right()
- if string.find(block(),"ore") then
- dig()
- forward()
- vien()
- back()
- end
- right()
- if string.find(block("up"),"ore") then
- digUp()
- up()
- vien()
- down()
- end
- if string.find(block("down"),"ore") then
- digDown()
- down()
- vien()
- up()
- end
- end
- function goHome()
- if x ~= 0 or y ~= 0 or z ~= 0 or dir ~= "north" then
- if x > 0 then
- if dir ~= "west" then
- repeat
- right()
- until dir == "west"
- end
- forward()
- elseif x < 0 then
- if dir ~= "east" then
- repeat
- right()
- until dir == "east"
- end
- forward()
- elseif z > 0 then
- if dir ~= "north" then
- repeat
- right()
- until dir == "north"
- end
- forward()
- elseif z < 0 then
- if dir ~= "south" then
- repeat
- right()
- until dir == "south"
- end
- elseif y > 0 then
- down()
- elseif y < 0 then
- up()
- elseif dir ~= north then
- right()
- end
- goHome()
- else
- print("home!")
- end
- end
- function dump()
- for i = 1,15 do
- turtle.select(i)
- sleep(0.25)
- turtle.drop()
- end
- turtle.select(1)
- end
- function shouldGoHome()
- local dist = math.abs(x)+math.abs(y)+math.abs(z)
- if (dist*2) > turtle.getFuelLevel() or dist > 150 then
- return true
- end
- for i = 1,15 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- goHome()
- right()
- right()
- while turtle.getFuelLevel() > 100 do
- turtle.dig()
- forward()
- left()
- if turtle.detect() then
- while not shouldGoHome() do
- dig()
- forward()
- vien()
- end
- goHome()
- dump()
- right()
- right()
- else
- right()
- dig()
- forward()
- right()
- if turtle.detect() then
- while not shouldGoHome() do
- dig()
- forward()
- vien()
- end
- goHome()
- dump()
- right()
- right()
- else
- left()
- end
- end
- end
- print("no fuel!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement