Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t = turtle
- left = 0
- forward = 0
- down = 0
- function distance ()
- return math.sqrt(left^2 + forward^2 + down^2)
- end
- function go (direction)
- if direction == 'l' then
- t.turnLeft()
- st = t.dig()
- if st then st = t.forward(); t.turnRight() end
- if st then left+=1 end
- elseif direction == 'r' then
- t.turnRight()
- st = t.dig()
- if st then st = t.forward(); t.turnLeft() end
- if st then left-=1 end
- elseif direction == 'd' then
- t.digDown()
- st = t.down()
- if st then down+=1 end
- elseif direction == 'u' then
- t.digUp()
- st = t.up()
- if st then down-=1 end
- elseif direction == 'f' then
- t.dig()
- st = t.forward()
- if st then forward+=1 end
- elseif direction == 'b' then
- t.turnLeft()
- t.turnLeft()
- st = t.dig()
- if st then forward-=1 end
- t.turnLeft()
- t.turnLeft()
- end
- return st
- end
- function returnHome ()
- while left != 0 do
- if left > 0 then
- go('r')
- else
- go('l')
- end
- end
- while forward != 0 do
- if forward > 0 then
- go('b')
- else
- go('f')
- end
- end
- while down != 0 do
- if down > 0 then
- go('u')
- else
- go('d')
- end
- end
- end
- downLine = true
- while true do
- if (distance() > 0.8 * turtle.getFuelLevel()) then
- returnHome()
- break
- end
- if (downLine) then
- st = go('d')
- else
- st = go('u')
- end
- if (!st) then
- st = go('f')
- if (!st) then
- print('got stuck')
- returnHome()
- break
- end
- downLine = false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement