Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Code by mikebald
- Modify / Take / Use how you like
- --]]
- local position = {forward=0, right=0, down=0, face=1}
- local facing = { "forward", "right", "back", "left" }
- function Debug()
- print( "Forward: " .. position.forward )
- print( "Right: " .. position.right )
- print( "Down: " .. position.down )
- print( "Face: " .. position.face )
- print( "Facing: " .. facing[position.face] )
- end
- function TurnTo( direction )
- while facing[position.face] ~= direction do
- turtle.turnRight()
- position.face = position.face + 1
- if position.face > 4 then position.face = 1 end
- end
- end
- function Forward( amount )
- amount = amount or 1
- for i=1,amount do
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- turtle.attackUp()
- end
- sleep(0.5)
- end
- -- Update Position
- if position.face == 1 then position.forward = position.forward + 1 end
- if position.face == 3 then position.forward = position.forward - 1 end
- if position.face == 2 then position.right = position.right + 1 end
- if position.face == 4 then position.right = position.right - 1 end
- end
- end
- function MoveTo(forward, right, down)
- local forwardMovement = forward - position.forward
- local rightMovement = right - position.right
- local downMovement = down - position.down
- if forwardMovement > 0 then
- TurnTo("forward")
- Forward(forwardMovement)
- end
- if forwardMovement < 0 then
- TurnTo("back")
- Forward(math.abs(forwardMovement))
- end
- if rightMovement > 0 then
- TurnTo("right")
- Forward(rightMovement)
- end
- if rightMovement < 0 then
- TurnTo("left")
- Forward(math.abs(rightMovement))
- end
- if downMovement > 0 then
- Down(downMovement)
- end
- if downMovement < 0 then
- Up(math.abs(downMovement))
- end
- end
- function TurnAround()
- TurnTo( facing[math.fmod(position.face + 2, 4)] )
- end
- function Up( amount )
- amount = amount or 1
- for i=1,amount do
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- sleep(0.5)
- end
- position.down = position.down - 1
- end
- end
- function Down( amount )
- amount = amount or 1
- for i=1,amount do
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- end
- position.down = position.down + 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment