Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- layer = vortex and vortex.layer or 100
- function locate()
- local x,y,z = gps.locate()
- if not x then
- error("Can't get location",0)
- end
- if X and (x ~= X or y ~= Y or z ~= Z) then
- error("GPS desync",0)
- end return x,y,z
- end
- local function zeroAssert(suc,err)
- if not suc then
- error(err,2)
- end
- end
- local function gotoLayer(layer)
- local x,y,z = locate()
- if y < layer then
- for i=1,layer-y do
- zeroAssert(turtle.up())
- end
- else
- for i=1,y-layer do
- zeroAssert(turtle.down())
- end
- end
- end
- if vortex then
- vortex.rotate(0)
- else
- local ok
- for i=1,4 do
- if turtle.forward() then
- turtle.turnLeft()
- turtle.turnLeft()
- ok = true break
- else
- turtle.turnLeft()
- end
- end
- if not ok then
- error("Can't determine rotation",0)
- end
- local pos = {locate()}
- turtle.forward()
- for k,v in pairs{locate()} do
- pos[k] = v - pos[k]
- end
- if pos[1] == 0 then
- if pos[3] == -1 then
- -- Facing north
- else
- -- Facing south
- turtle.turnLeft()
- turtle.turnLeft()
- end
- elseif pos[1] == 1 then
- -- Facing east
- turtle.turnLeft()
- else
- -- Facing west
- turtle.turnRight()
- end
- end
- ro = vortex and vortex.ro or 0
- function rotate(n)
- ro,n = ro%4,n%4
- if ro < 0 then
- ro = ro + 4
- end
- if math.abs(ro-n-4) <
- math.abs(ro-n) then
- ro = ro - 4
- end
- while ro < n do
- turtle.turnLeft()
- end
- while ro > n do
- turtle.turnRight()
- end ro = (ro + 4)%4
- end
- local old = vortex and vortex.old or {
- turnLeft = turtle.turnLeft;
- turnRight = turtle.turnRight;
- forward = turtle.forward;
- back = turtle.back;
- down = turtle.down;
- up = turtle.up;
- }
- boundsFunc = vortex and vortex.boundsFunc or function(x,y,z)
- if x > 1111 then return false end
- if x < 1073 then return false end
- if z > 1703 then return false end
- if z < 1649 then return false end
- if z < 1665 and x > 1086 then
- return false
- end return y < 101 and y > 76
- end
- X,Y,Z = gps.locate()
- function turtle.turnLeft()
- old.turnLeft() ro = (ro + 1)%4
- end
- function turtle.turnRight()
- old.turnRight() ro = (ro + 3)%4
- end
- function turtle.forward()
- local x,z = X,Z
- if ro == 0 then
- z = z - 1
- elseif ro == 1 then
- x = x - 1
- elseif ro == 2 then
- z = z + 1
- elseif ro == 3 then
- x = x + 1
- end
- if not boundsFunc(x,Y,z) then
- return false,"Outside bounds"
- elseif not old.forward() then
- return false,"Can't move forward"
- end X,Z = x,z return true
- end
- function turtle.back()
- local x,z = X,Z
- if ro == 0 then
- z = z + 1
- elseif ro == 1 then
- x = x + 1
- elseif ro == 2 then
- z = z - 1
- elseif ro == 3 then
- x = x - 1
- end
- if not boundsFunc(x,Y,z) then
- return false,"Outside bounds"
- elseif not old.back() then
- return false,"Can't move back"
- end X,Z = x,z return true
- end
- function turtle.up()
- if not boundsFunc(X,Y+1,Z) then
- return false,"Outside bounds"
- elseif not old.up() then
- return false,"Can't move up"
- end Y = Y + 1 return true
- end
- function turtle.down()
- if not boundsFunc(X,Y-1,Z) then
- return false,"Outside bounds"
- elseif not old.down() then
- return false,"Can't move down"
- end Y = Y - 1 return true
- end
- local function succeedOrUp(f)
- local s,e = f()
- while not s do
- if e == "Outside bounds" then
- error("Has to move outside bounds",0)
- end zeroAssert(turtle.up())
- s,e = f()
- end
- end
- function goto(X,Y,Z,l)
- --gotoLayer(l or Y)
- local x,y,z = vortex.locate()
- x,y,z = x-X,y-Y,z-Z
- rotate(z < 0 and 2 or 0)
- for i=1,math.abs(z) do
- succeedOrUp(turtle.forward)
- end
- rotate(x > 0 and 1 or -1)
- for i=1,math.abs(x) do
- succeedOrUp(turtle.forward)
- end gotoLayer(Y)
- end
- function goto_old(X,Y,Z,l)
- gotoLayer(l == true and Y or l or layer)
- local x,y,z = vortex.locate()
- x,y,z = x-X,y-Y,z-Z
- rotate(z < 0 and 2 or 0)
- for i=1,math.abs(z) do
- zeroAssert(turtle.forward())
- end
- rotate(x > 0 and 1 or -1)
- for i=1,math.abs(x) do
- zeroAssert(turtle.forward())
- end
- if y > 0 then
- for i=1,y do
- zeroAssert(turtle.down())
- end
- else
- for i=1,-y do
- zeroAssert(turtle.up())
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement