Advertisement
TechManDylan

facedirection

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