Advertisement
TechManDylan

turn direction

Feb 2nd, 2023 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. directionToFace = arg[1]
  2. if directionToFace == nil then
  3.   print("Please provide a direction to turn to")
  4.   return
  5. end
  6.  
  7. -- Get the initial location
  8. startX, startY, startZ = gps.locate()
  9.  
  10. function currentLocation()
  11.  
  12.   currentX, currentY, currentZ = gps.locate()
  13.  
  14. end
  15.  
  16. function facingDirection()
  17.  
  18.   turtle.forward()
  19.  
  20.   currentLocation()
  21.  
  22.  
  23.   if currentX > startX then
  24.     currentDirection = "east"
  25.   end
  26.  
  27.   if currentX < startX then
  28.     currentDirection = "west"
  29.   end
  30.  
  31.   if currentZ > startZ then
  32.     currentDirection = "south"
  33.   end
  34.  
  35.   if currentZ < startZ then
  36.     currentDirection = "north"
  37.   end
  38.  
  39.   turtle.back()
  40.   print("Facing: "..currentDirection)
  41. end
  42.  
  43. -- Start turnDirection()
  44.  
  45. function turnDirection(directionToFace)
  46.  
  47.   -- Case east
  48.   if directionToFace == "east" then  
  49.     if currentDirection == "north" then
  50.       turtle.turnRight()
  51.     elseif currentDirection == "east" then
  52.       --Do nothing
  53.     elseif currentDirection == "south" then
  54.       turtle.turnLeft()
  55.     elseif currentDirection == "west" then
  56.       turtle.turnRight()
  57.       turtle.turnRight()
  58.     end
  59.     currentDirection = "east"
  60.   end
  61.  
  62. --Case east
  63.  
  64. --Case west
  65.   if directionToFace == "west" then
  66.     if currentDirection == "north" then
  67.       turtle.turnLeft()
  68.     elseif currentDirection == "east" then
  69.       turtle.turnRight()
  70.       turtle.turnRight()
  71.     elseif currentDirection == "south" then
  72.       turtle.turnRight()
  73.     elseif currentDirection == "west" then
  74.       --Do nothing
  75.     end
  76.     currentDirection = "west"
  77.   end
  78. --Case west
  79.  
  80. --Case north
  81.   if directionToFace == "north" then
  82.     if currentDirection == "north" then
  83.       --Do nothing
  84.     elseif currentDirection == "east" then
  85.       turtle.turnLeft()
  86.     elseif currentDirection == "south" then
  87.       turtle.turnRight()
  88.       turtle.turnRight()
  89.     elseif currentDirection == "west" then
  90.       turtle.turnRight()
  91.     end
  92.     currentDirection = "north"
  93.   end
  94. --Case north
  95.  
  96. --Case south
  97.   if directionToFace == "south" then
  98.     if currentDirection == "north" then
  99.       turtle.turnRight()
  100.       turtle.turnRight()
  101.     elseif currentDirection == "east" then
  102.       turtle.turnRight()
  103.     elseif currentDirection == "south" then
  104.       --Do nothing
  105.     elseif currentDirection == "west" then
  106.       turtle.turnLeft()
  107.     end
  108.     currentDirection = "south"
  109.   end
  110. --Case south
  111. end
  112.  
  113. facingDirection()
  114. turnDirection(directionToFace)
  115.  
  116.  
  117.  
  118. -- End turnDirection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement