Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. x, y = ...
  2.  
  3. direction = 1
  4. xPos = 0
  5. yPos = 0
  6.  
  7. function turnLeft(x)
  8. for i=1, x do
  9. turtle.turnLeft()
  10.  
  11. if direction == 1 then
  12. direction = 4
  13. else
  14. direction = direction - 1
  15. end
  16. end
  17. end
  18.  
  19. function turnRight(x)
  20. for i=1, x do
  21. turtle.turnRight()
  22.  
  23. if direction == 4 then
  24. direction = 1
  25. else
  26. direction = direction + 1
  27. end
  28. end
  29. end
  30.  
  31. function setDirection(x)
  32. delta = x - xPos
  33.  
  34. if x < 0 then
  35. turnLeft(-x)
  36. else
  37. turnRight(x)
  38. end
  39. end
  40.  
  41. function forward(x)
  42. for i=1, x do
  43. turtle.forward()
  44.  
  45. if direction == 1 then
  46. yPos = yPos + 1
  47. elseif direction == 2 then
  48. xPos = xPos + 1
  49. elseif direction == 3 then
  50. yPos = yPos - 1
  51. else
  52. xPos = xPos - 1
  53. end
  54. end
  55. end
  56.  
  57. function back(x)
  58. for i=1, x do
  59. turtle.back()
  60.  
  61. if direction == 1 then
  62. yPos = yPos - 1
  63. elseif direction == 2 then
  64. xPos = xPos - 1
  65. elseif direction == 3 then
  66. yPos = yPos + 1
  67. else
  68. xPos = xPos + 1
  69. end
  70. end
  71. end
  72.  
  73. function navigate(x, y)
  74. dx = x - xPos
  75. dy = y - yPos
  76.  
  77. if dx < 0 then
  78. setDirection(4)
  79. forward(-dx)
  80. else
  81. setDirection(2)
  82. forward(dx)
  83. end
  84.  
  85. if dy < 0 then
  86. setDirection(3)
  87. forward(-dy)
  88. else
  89. setDirection(1)
  90. forward(dy)
  91. end
  92.  
  93. print xPos + ", " + yPos
  94. end
  95.  
  96. navigate(x, y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement