Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. t = turtle
  2. left = 0
  3. forward = 0
  4. down = 0
  5.  
  6. function distance ()
  7. return math.sqrt(left^2 + forward^2 + down^2)
  8. end
  9.  
  10. function go (direction)
  11. if direction == 'l' then
  12. t.turnLeft()
  13. st = t.dig()
  14. if st then st = t.forward(); t.turnRight() end
  15. if st then left=left+1 end
  16. elseif direction == 'r' then
  17. t.turnRight()
  18. st = t.dig()
  19. if st then st = t.forward(); t.turnLeft() end
  20. if st then left=left-1 end
  21. elseif direction == 'd' then
  22. t.digDown()
  23. st = t.down()
  24. if st then down=down+1 end
  25. elseif direction == 'u' then
  26. t.digUp()
  27. st = t.up()
  28. if st then down=down-1 end
  29. elseif direction == 'f' then
  30. t.dig()
  31. st = t.forward()
  32. if st then forward=forward+1 end
  33. elseif direction == 'b' then
  34. t.turnLeft()
  35. t.turnLeft()
  36. st = t.dig()
  37. if st then forward=forward-1 end
  38. t.turnLeft()
  39. t.turnLeft()
  40. end
  41. return st
  42. end
  43.  
  44. function returnHome ()
  45. while left ~= 0 do
  46. if left > 0 then
  47. go('r')
  48. else
  49. go('l')
  50. end
  51. end
  52. while forward ~= 0 do
  53. if forward > 0 then
  54. go('b')
  55. else
  56. go('f')
  57. end
  58. end
  59. while down ~= 0 do
  60. if down > 0 then
  61. go('u')
  62. else
  63. go('d')
  64. end
  65. end
  66. end
  67.  
  68.  
  69. downLine = true
  70. while true do
  71. if (distance() > 0.8 * turtle.getFuelLevel()) then
  72. returnHome()
  73. break
  74. end
  75. if (downLine) then
  76. st = go('d')
  77. else
  78. st = go('u')
  79. end
  80. if (~st) then
  81. st = go('f')
  82. if (~st) then
  83. print('got stuck')
  84. returnHome()
  85. break
  86. end
  87. downLine = false
  88. end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement