Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local positionX=0
- local positionY=0
- local positionZ=0
- right="right"
- left="left"
- up="up"
- down="down"
- forward="forward"
- back="back"
- function doMove(direction,distance)
- -- set defaults
- if (distance==nil) then distance = 1 end
- if (distance==0) then return end
- direction=direction or "none"
- --fix negative distances
- if (distance<0) then
- distance=distance * -1
- if (direction==right) then direction=left
- elseif (direction==left) then direction=right
- elseif (direction==forward) then direction=back
- elseif (direction==back) then direction=forward
- elseif (direction==up) then direction=down
- elseif (direction==down) then direction=up
- end
- end
- -- increase or decrease position with increment
- if ((direction==right) or (direction==up) or (direction==forward)) then
- increment=1
- else
- increment=-1
- end
- --always face forward so that suck works
- --set temporary location to update
- if (direction==right) then
- tmpLocation=positionX
- turtle.turnRight()
- elseif (direction==left) then
- tmpLocation=positionX
- turtle.turnLeft()
- elseif (direction==back) then
- turtle.turnLeft()
- turtle.turnLeft()
- tmpLocation=positionY
- elseif (direction==forward) then
- tmpLocation=positionY
- elseif ((direction==up) or (direction==down)) then
- tmpLocation=positionZ
- else
- print("unknown direction: "..direction)
- return
- end
- --move and update position
- if (direction==up) then
- for d=1,distance do
- turtle.suckUp()
- if turtle.up() then tmpLocation=tmpLocation+increment end
- end
- elseif (direction==down) then
- for d=1,distance do
- turtle.suckDown()
- if turtle.down() then tmpLocation=tmpLocation+increment end
- end
- else
- for d=1,distance do
- turtle.suck()
- if turtle.forward() then tmpLocation=tmpLocation+increment end
- end
- end
- --set the globalposition and face correct direction
- if (direction==right) then
- positionX=tmpLocation
- turtle.turnLeft()
- elseif (direction==left) then
- positionX=tmpLocation
- turtle.turnRight()
- elseif (direction==back) then
- positionY=tmpLocation
- turtle.turnLeft()
- turtle.turnLeft()
- elseif (direction==forward) then
- positionY=tmpLocation
- elseif ((direction==up) or (direction==down)) then
- positionZ=tmpLocation
- end
- end
- print("X: "..positionX..", Y: "..positionY..", Z: "..positionZ)
- print(right)
- doMove(left,-2)
- sleep(2)
- print(left)
- doMove(right,-2)
- sleep(2)
- print(up)
- doMove(down,-2)
- sleep(2)
- print(down)
- doMove(up,-2)
- sleep(2)
- print(forward)
- doMove(back,-2)
- sleep(2)
- print(back)
- doMove(forward,-2)
- print("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement