Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local homeX, homeY, homeZ -- Coordinates of turtle home station
- homeX = 420
- homeY = 65
- homeZ = -104
- local direction
- local timeout = 5 -- The timeout of the gps
- local safeHeight = 35 --The height that is safe to dig at
- function inAllowedBlock(block)
- local allowed = {"minecraft:dirt", "minecraft:cobblestone", "minecraft:stone", "minecraft:sand", "minecraft:sandstone", "minecraft:clay", "minecraft:gravel", "minecraft:yellow_flower", "minecraft:red_flower", "minecraft:granite", "minecraft:wood", "minecraft:double_plant", "minecraft:leaves", "minecraft:moss_stone", "minecraft:grass"}
- for i = 1, #allowed do
- if(allowed[i-1]==block) then return true end
- end
- return false
- end
- function go(dist, dig)
- dig = (dig == true)
- dist = dist or 1
- if(dist <= 0) then return end
- for i = 1, dist do
- while(not turtle.forward()) do
- turtle.attack()
- if(turtle.detect()) then
- local succ, block = turtle.inspect()
- if(isAllowedBlock(block.name) or dig) then
- turtle.dig()
- else
- local x,y,z = gps.locate(timeout, true)
- if(safeHeight<y) then
- goDown(y-safeHeight)
- turtle.dig()
- else
- turtle.dig()
- end
- end
- end
- end
- end
- end
- function goDown(dist, dig)
- dig = true -- Until I find a way to make sure it does go home if it can't go down
- dist = dist or 1
- if(dist<=0) then return end
- for i = 1, dist do
- while(not turtle.down()) do
- turtle.attackDown()
- if(dig) then
- turtle.digDown()
- else
- return false
- end
- end
- end
- end
- function goUp(dist)
- dist = dist or 1
- if(dist<=0)then return end
- for i = 1, dist do
- while(not turtle.up()) do
- turtle.up()
- turtle.digUp()
- end
- end
- end
- function getDirection(dig, times)
- dig = dig ~= false
- times = times or 1
- local loc1 = vector.new(gps.locate(timeout, false))
- if(not turtle.forward()) then
- if(turtle.detect()) then
- local succ, block = turtle.inspect()
- if(isAllowedBlock(block.name) or dig) then
- turtle.dig()
- turtle.forward()
- else
- if(times<4) then
- turnLeft(false)
- local tmp = getDirection(dig, times+1)
- turnRight(false)
- return (tmp+1)%4
- end
- return false
- end
- end
- end
- local loc2 = vector.new(gps.locate(timeout, false))
- heading = loc2-loc1
- if(not turtle.back()) then
- turtle.turnLeft()
- turtle.turnLeft()
- while(not turtle.forward()) do
- turtle.dig()
- turtle.attack()
- end
- turtle.turnRight()
- turtle.turnRight()
- end
- return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))-1
- end
- function turnLeft(followDir)
- followDir = followDir ~= false
- turtle.turnLeft()
- if(followDir) then
- direction = (direction-1)%4
- end
- end
- function turnRight(followDir)
- followDir = followDir ~= false
- turtle.turnRight()
- if(followDir) then
- direction = (direction+1)%4
- end
- end
- function turnTo(dir)
- if(dir >= 0 and dir <= 3) then
- local turns = (dir-direction)%4
- if (turns == 1 or turns == -3) then
- turnRight()
- elseif(turns == 2) then
- turnRight()
- turnRight()
- elseif(turns == -1 or turns == 3) then
- turnLeft()
- elseif(turns == -2) then
- turnLeft()
- turnLeft()
- end
- end
- if(dir ~= direction) then
- print((dir-direction)%4)
- print("Problem in turnTo()")
- end
- end
- function goHome()
- local x,y,z = gps.locate(5, false)
- -- if(y > safeHeight) then
- -- goDown(y-safeHeight)
- --end
- direction = getDirection(y<=safeHeight)
- print("Got direction: "..direction)
- if(x>homeX) then
- turnTo(0)
- elseif(x<homeX) then
- turnTo(2)
- end
- go(math.abs(x-homeX))
- if(z>homeZ) then
- turnTo(1)
- elseif(z<homeZ) then
- turnTo(3)
- end
- go(math.abs(z-homeZ))
- if(y>homeY) then
- goDown(y-homeY)
- elseif(y<homeY) then
- goUp(homeY-y)
- end
- if(vector.new(x,y,z)-vector.new(homeX,homeY,homeZ) == 0) then
- print("Error in going home equation")
- print("Current position: ("..x..","..y..","..z..")")
- end
- end
- os.sleep(3)
- goHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement