Rusettsten

TurtleTurn

Nov 29th, 2020 (edited)
499
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- A Basic Turn API for DanetOS3
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4.     print("TurtleTurn ERROR: Incorrect Usage. TurtleTurn <direction>")
  5.     return
  6. end
  7.  
  8. function readCurDirection()
  9.     local fileData = io.open("danetos3/direction.danet", "r")
  10.     io.input(fileData)
  11.     curDirection = io.read()
  12.     io.close(fileData)
  13.     return(curDirection)
  14. end
  15.  
  16. function writeNewDirection(direction)
  17.     local fileData = io.open("danetos3/direction.danet", "w")
  18.     io.output(fileData)
  19.     io.write(direction)
  20.     io.close(fileData)
  21. end
  22.  
  23.  
  24. function turnLeft() -- Turn that direction and then calculate the new facing direction
  25.     turtle.turnLeft()
  26.     curDirection = readCurDirection()
  27.     if curDirection == "north" then
  28.         writeNewDirection("west")
  29.     elseif curDirection == "west" then
  30.         writeNewDirection("south")
  31.     elseif curDirection == "south" then
  32.         writeNewDirection("east")
  33.     elseif curDirection == "east" then
  34.         writeNewDirection("north")
  35.     end
  36. end
  37.  
  38. function turnRight()
  39.     turtle.turnRight()
  40.     curDirection = readCurDirection()
  41.     if curDirection == "north" then
  42.         writeNewDirection("east")
  43.     elseif curDirection == "east" then
  44.         writeNewDirection("south")
  45.     elseif curDirection == "south" then
  46.         writeNewDirection("west")
  47.     elseif curDirection == "west" then
  48.         writeNewDirection("north")
  49.     end
  50. end
  51.  
  52. if tArgs[1] == "left" then
  53.     turnLeft()
  54. elseif tArgs[1] == "right" then
  55.     turnRight()
  56. else
  57.     print("TurtleTurn ERROR: Incorrect args. Must be left or right.")
  58. end
RAW Paste Data