Machuga14

Computercraft Turtle Self-GPS

Apr 29th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.20 KB | None | 0 0
  1. -- Turtle Self-tracking System created by Latias1290.
  2. -- Original code stolen from:http://www.computercraft.info/wiki/Turtle_GPS_self-tracker_expansion_(tutorial) (04/29/2016)
  3. -- Original code has been modified to be more useful. (supports loading and saving)
  4.  
  5. local xPos, yPos, zPos = nil
  6. face = 1 -- 1 is west, 2 is south, 3 is east, 0 is north
  7. cal = false
  8. local fileLoc = "MatthewC/appdata/turtleLoc" -- The name of the file.
  9. local debug = false -- Stores whether or not we are in debug mode (if true, print our location after every move)
  10. local slowPrint = false -- Stores whether to use slowprint or fast print when printing location
  11.  
  12. function setDebug(val)
  13.   debug = val
  14. end
  15.  
  16. function getDebug(val)
  17.   return debug
  18. end
  19.  
  20. function setSlowPrint(val)
  21.   slowPrint = val
  22. end
  23.  
  24. function getSlowPrint()
  25.   return slowPrint
  26. end
  27.  
  28. -- Converts a string direction into a face EG N or NORTH (case insensitive) gets converted to 0
  29. function convertStringToFace(f)
  30.   if string.upper(f) == "N" or string.upper(f) == "NORTH" then
  31.     return 0
  32.   elseif string.upper(f) == "W" or string.upper(f) == "WEST" then
  33.     return 1
  34.   elseif string.upper(f) == "S" or string.upper(f) == "SOUTH" then
  35.     return 2
  36.   elseif string.upper(f) == "E" or string.upper(f) == "EAST" then
  37.     return 3
  38.   else
  39.     return nil
  40.   end
  41. end
  42.  
  43. -- Converts an int into a direction EG 0 : North, 1: West, 2: South, 3: East
  44. function convertFaceToString(f)
  45.   if f == 0 then
  46.     return "N"
  47.   elseif f == 1 then
  48.     return "W"
  49.   elseif f == 2 then
  50.     return "S"
  51.   elseif f == 3 then
  52.     return "E"
  53.   else
  54.     return nil
  55.   end
  56. end
  57.  
  58. function getLocationAsString()
  59.   if not cal then
  60.     return "Location: Not Calibrated!"
  61.   end
  62.  
  63.   return "Location: x="..xPos..", y="..yPos..", z="..zPos..", Direction="..convertFaceToString(face)
  64. end
  65.  
  66. function printLocation()
  67.   if slowPrint then
  68.     textutils.slowPrint(getLocationAsString())
  69.   else
  70.     print(getLocationAsString())
  71.   end
  72.   return printVal
  73. end
  74.  
  75. function saveLocation() -- save the location of the turtle, for persistence.
  76.   if not cal then
  77.     return false
  78.   end
  79.  
  80.   local h = fs.open(fileLoc, "w")
  81.   h.writeLine(xPos)
  82.   h.writeLine(yPos)
  83.   h.writeLine(zPos)
  84.   h.writeLine(face)
  85.   h.close()
  86.   return true
  87. end
  88.  
  89. function loadLocation() -- Load the location of the turtle, for persistence.
  90.   local h = fs.open(fileLoc, "r")
  91.   if not h then
  92.     return false
  93.   end
  94.  
  95.   local h = fs.open(fileLoc, "r")
  96.   xPos = tonumber(h.readLine())
  97.   yPos = tonumber(h.readLine())
  98.   zPos = tonumber(h.readLine())
  99.   face = tonumber(h.readLine())
  100.   cal = true
  101.   h.close()
  102.   return true
  103. end
  104.  
  105. function setLocation(face) -- get gps using other computers
  106.   xPos, yPos, zPos = gps.locate()
  107.   face = convertStringToFace(direction)
  108.   cal = true
  109.   saveLocation()
  110. end
  111.  
  112. function manSetLocation(x, y, z, direction) -- manually set location
  113.   xPos = x
  114.   yPos = y
  115.   zPos = z
  116.   face = convertStringToFace(direction)
  117.   cal = true
  118.   saveLocation()
  119. end
  120.  
  121. function getLocation() -- return the location
  122.   if not xPos then
  123.     return nil
  124.   else
  125.     return xPos, yPos, zPos, convertFaceToString(face)
  126.   end
  127. end
  128.  
  129. function isCalibrated() -- Return whether or not the api is calibrated
  130.   return cal
  131. end
  132.  
  133. function turnLeft() -- turn left
  134.   if not turtle.turnLeft() then
  135.     return false
  136.   end
  137.  
  138.   if face == 0 then
  139.     face = 1
  140.   elseif face == 1 then
  141.     face = 2
  142.   elseif face == 2 then
  143.     face = 3
  144.   elseif face == 3 then
  145.     face = 0
  146.   end
  147.  
  148.   saveLocation()
  149.   return true
  150. end
  151.  
  152. function turnRight() -- turn right
  153.   if not turtle.turnRight() then
  154.     return false
  155.   end
  156.  
  157.   if face == 0 then
  158.     face = 3
  159.   elseif face == 1 then
  160.     face = 0
  161.   elseif face == 2 then
  162.     face = 1
  163.   elseif face == 3 then
  164.     face = 2
  165.  end
  166.  
  167.   saveLocation()
  168.   return true
  169. end
  170.  
  171.  
  172. function turnToOrientation(direction) -- Turn to a specified N, E, S or W direction. Returns true if successful, or false it not successul.
  173.   inFace = convertStringToFace(direction)
  174.  
  175.   if not inFace then
  176.     return false
  177.   end
  178.  
  179.   -- Special case where we are already facing the correct direction
  180.   if inFace == face then
  181.     return true
  182.   end
  183.  
  184.   -- Special cases where we just turn once.
  185.   -- Cases for turning left
  186.   if (inFace == 0 and face == 3) or (inFace == 3 and face == 2) or (inFace == 2 and face == 1) or (inFace == 1 and face == 0) then
  187.     return turnLeft()
  188.   -- Cases for turning right
  189.   elseif (inFace == 0 and face == 1) or (inFace == 1 and face == 2) or (inFace == 2 and face == 3) or (inFace == 3 and face == 0) then
  190.     return turnRight()
  191.   end
  192.  
  193.   -- If we get to this point, we are guaranteed to need to turn exactly twice, so lets do so.
  194.   turnSuccess = turnLeft()
  195.  
  196.   if not turnSuccess then
  197.     return false
  198.   end
  199.  
  200.   return turnLeft()
  201. end
  202.  
  203. function forward() -- go forward
  204.   if not turtle.forward() then
  205.     return false
  206.   end
  207.  
  208.   if cal == true then
  209.     if face == 0 then
  210.       zPos = zPos - 1
  211.     elseif face == 1 then
  212.       xPos = xPos - 1
  213.     elseif face == 2 then
  214.       zPos = zPos + 1
  215.     elseif face == 3 then
  216.       xPos = xPos + 1
  217.     end
  218.     else
  219.     print("Not Calibrated.")
  220.   end
  221.  
  222.   saveLocation()
  223.   printLocation()
  224.   return true
  225. end
  226.  
  227. function back() -- go back
  228.   if not turtle.back() then
  229.     return false
  230.   end
  231.  
  232.   if cal == true then
  233.     if face == 0 then
  234.       zPos = zPos + 1
  235.     elseif face == 1 then
  236.       xPos = xPos + 1
  237.     elseif face == 2 then
  238.       zPos = zPos - 1
  239.     elseif face == 3 then
  240.       xPos = xPos - 1
  241.     end
  242.   else
  243.     print("Not Calibrated.")
  244.   end
  245.  
  246.   saveLocation()
  247.   printLocation()
  248.   return true
  249. end
  250.  
  251. function up() -- go up
  252.   if not turtle.up() then
  253.     return false
  254.   end
  255.  
  256.   if cal == true then
  257.     yPos = yPos + 1
  258.   else
  259.     print("Not Calibrated.")
  260.   end
  261.  
  262.   saveLocation()
  263.   printLocation()
  264.   return true
  265. end
  266.  
  267. function down() -- go down
  268.   if not turtle.down() then
  269.     return false
  270.   end
  271.  
  272.   if cal == true then
  273.     yPos = yPos - 1
  274.   else
  275.     print("Not Calibrated.")
  276.   end
  277.  
  278.   saveLocation()
  279.   printLocation()
  280.   return true
  281. end
  282.  
  283. function jump() -- perform a jump. useless? yup!
  284.   turtle.up()
  285.   turtle.down()
  286. end
  287.  
  288. loadLocation() -- Try to load the location of the turtle, if it's already cached.
Add Comment
Please, Sign In to add comment