Guest User

Turtle Coordinate Tracker

a guest
Mar 19th, 2016
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.53 KB | None | 0 0
  1. print("Where Am I - Coordinates for turtles. Type help() for help.")
  2. print("Also, do NOT use the normal turtle.forward and such functions, as that will mess up the internal coordinates of the turtle.")
  3. if fs.exists("coords") then
  4.   local file = fs.open("coords", "r")
  5.   X = file.readLine()
  6.   Y = file.readLine()
  7.   Z = file.readLine()
  8.   face = file.readLine()
  9.   file.close()
  10.   X = tonumber(X)
  11.   Y = tonumber(Y)
  12.   Z = tonumber(Z)
  13.   face = tonumber(face)
  14.   print("Initialized settings.")
  15. end
  16. if fs.exists("waypoints") then
  17.   local file = fs.open("waypoints", "r")
  18.   waypoints = {}
  19.   local line = file.readLine()
  20.   while line do
  21.     table.insert(waypoints, line)
  22.     line = file.readLine()
  23.   end
  24.   print("Waypoints Initilialized.")
  25. end
  26. function help(page)
  27.   if type(page) == "nil" then
  28.     page = 1
  29.   end
  30.   Page = tonumber(page)
  31.   if type(Page) ~= "number" then
  32.     error("Bad argument: Number expected, got "..page.."!")
  33.   elseif Page > 6 or Page < 1 then
  34.     error("Bad argument: Integer expected between 1 and 4, got "..Page.."!")
  35.   end
  36.   if Page == 1 then
  37.     print("---------------Help menu---------------")
  38.     print("----------Viewing Page 1 of 6----------")
  39.     print("help(page)")
  40.     print("-Shows these help text pages.")
  41.     print("-Perameraters: Takes the page number you whish to view help on.")
  42.     print("turtle.setCoords(x,y,z,dir)")
  43.     print("-Will set the coords of the turtle.")
  44.     print("-Perameraters: Integers for X,Y and Z, and the initial direction the turtle is facing.")
  45.   elseif Page == 2 then
  46.     print("---------------Help Menu---------------")
  47.     print("----------Viewing Page 2 of 6----------")
  48.     print("turtle.getCoords()")
  49.     print("-Prints the current coordinates of the turtle.")
  50.     print("turtle.move(dir, amount)")
  51.     print("-Moves turtle in indicated direction.")
  52.     print("-Perameraters: The direction the turtle should move, and how many blocks that way. Takes 'front', 'down', 'forward', 'back', 'left', and 'right' as arguments.")
  53.   elseif Page == 3 then
  54.     print("---------------Help Menu---------------")
  55.     print("----------Viewing Page 3 of 6----------")
  56.     print("turtle.turn(string direction)")
  57.     print("-Turn the turtle in the specified direction.")
  58.     print("-Perameraters: The direction the turtle should turn. (left, right, or back)")
  59.   elseif Page == 4 then
  60.     print("-----Help Menu-Viewing Page 4 of 6-----")
  61.     print("setWaypoint(X,Y,Z,name,)")
  62.     print("-Sets a named waypoint for the turtle at the indicated location. Also, if specified, will make the turtle go to height. Useful if the waypoint is under something.")
  63.     print("-Perameraters: Takes Integers for X, Y, and Z. Takes a name for the waypoint. Will also take an integer for the height the turtle should reach before going.")
  64.   elseif Page == 5 then
  65.     print("---------------Help Menu---------------")
  66.     print("----------Viewing Page 5 of 6----------")
  67.     print("deleteWaypoint(name)")
  68.     print("-Deletes the specified waypoint.")
  69.     print("-Perameraters: Takes the name of the waypoint that should be deleted.")
  70.     print("listWaypoints()")
  71.     print("Lists all the available waypoints.")
  72.     print("turtle.gotoWaypoint(name)")
  73.   elseif Page == 6 then
  74.     print("---------------Help Menu---------------")
  75.     print("----------Viewing Page 6 of 6----------")
  76.     print("-Has the turtle go to the waypoint.")
  77.     print("-Perameraters: Takes the name of the waypoint the turtle should go to.")
  78.     print("Coordinates are saved as follows: X coord: X, Y coord: Y, Z coord: Z, Direction: face")
  79.   end
  80. end
  81. local function reinitialize()
  82.   file = fs.open("coords", "w")
  83.   file.writeLine(X)
  84.   file.writeLine(Y)
  85.   file.writeLine(Z)
  86.   file.writeLine(face)
  87.   file.close()
  88. end
  89. function turtle.setCoords(x,y,z,dir)
  90.   X = x
  91.   Y = y
  92.   Z = z
  93.   face = string.lower(dir)
  94.   if face == "north" then
  95.     face = 0
  96.   elseif face == "south" then
  97.     face = 2
  98.   elseif face == "east" then
  99.     face = 3
  100.   elseif face == "west" then
  101.     face = 1
  102.   else
  103.     face = 0
  104.   end
  105.   while type(X) ~= "number" do
  106.     print("X is not a number.")
  107.     X = tonumber(read())
  108.   end
  109.   while type(Y) ~= "number" do
  110.     print("Y is not a number.")
  111.     Y = tonumber(read())
  112.   end
  113.   while type(Z) ~= "number" do
  114.     print("Z is not a number.")
  115.     Z = tonumber(read())
  116.   end
  117.   reinitialize()
  118. end
  119. function turtle.getCoords()
  120.   print("X: "..X)
  121.   print("Y: "..Y)
  122.   print("Z: "..Z)
  123.   if face == 0 then
  124.     print("facing: north")
  125.   elseif face == 1 then
  126.     print("facing: west")
  127.   elseif face == 2 then
  128.     print("facing: south")
  129.   else
  130.     print("facing: east")
  131.   end
  132. end
  133. function turtle.turn(dir)
  134.   if dir == "left" then
  135.     turtle.turnLeft()
  136.     face = ((face + 1) + 4) % 4
  137.   elseif dir == "right" then
  138.     turtle.turnRight()
  139.     face = ((face - 1) + 4) % 4
  140.   elseif dir == "back" then
  141.     turtle.turnLeft()
  142.     turtle.turnLeft()
  143.     face = ((face + 2) + 4) % 4
  144.   end
  145.   reinitialize()
  146. end
  147. function turtle.move(dir,a)
  148.   if a == nil then
  149.     a = 1
  150.   end
  151.   if dir == "up" then
  152.     for i=1,a do
  153.       if turtle.up() then
  154.         Y = Y + 1
  155.       end
  156.     end
  157.   elseif dir == "down" then
  158.     for i=1,a do
  159.       if turtle.down() then
  160.         Y = Y - 1
  161.       end
  162.     end
  163.   elseif dir == "forward" then
  164.     for i=1,a do
  165.       if turtle.forward() then
  166.         if face == 0 then
  167.           Z = Z - 1
  168.         elseif face == 2 then
  169.           Z = Z + 1
  170.         elseif face == 1 then
  171.           X = X - 1
  172.         elseif face == 3 then
  173.           X = X + 1
  174.         end
  175.       end
  176.     end
  177.   elseif dir == "back" then
  178.     for i=1,a do
  179.       if turtle.back() then
  180.         if face == 0 then
  181.           Z = Z + 1
  182.         elseif face == 2 then
  183.           Z = Z - 1
  184.         elseif face == 1 then
  185.           X = X + 1
  186.         elseif face == 3 then
  187.           X = X - 1
  188.         end
  189.       end
  190.     end
  191.   elseif dir == "left" then
  192.     for i=1,a do
  193.       if turtle.turnLeft() then
  194.         face = ((face + 1) + 4) % 4
  195.       end
  196.       if turtle.forward() then
  197.         if face == 0 then
  198.           Z = Z - 1
  199.         elseif face == 2 then
  200.           Z = Z + 1
  201.         elseif face == 1 then
  202.           X = X - 1
  203.         elseif face == 3 then
  204.           X = X + 1
  205.         end
  206.       end
  207.       if turtle.turnRight() then
  208.         face = ((face - 1) + 4) % 4
  209.       end
  210.     end
  211.   elseif dir == "right" then
  212.     for i=1,a do
  213.       if turtle.turnRight() then
  214.         face = ((face - 1) + 4) % 4
  215.       end
  216.       if turtle.forward() then
  217.         if face == 0 then
  218.           Z = Z - 1
  219.         elseif face == 2 then
  220.           Z = Z + 1
  221.         elseif face == 1 then
  222.           X = X - 1
  223.         elseif face == 3 then
  224.           X = X + 1
  225.         end
  226.       end
  227.       if turtle.turnLeft() then
  228.         face = ((face + 1) + 4) % 4
  229.       end
  230.     end
  231.   end
  232.   reinitialize()
  233. end
  234. function setWaypoint(x,y,z,name,yLimit)
  235.   if waypoints == nil then
  236.     waypoints = {}
  237.   end
  238.   if yLimit == nil then
  239.     yLimit = 256
  240.   end
  241.   table.insert(waypoints,name)
  242.   local x = tonumber(x)
  243.   local y = tonumber(y)
  244.   local z = tonumber(z)
  245.   local yLimit = tonumber(yLimit)
  246.   while type(x) ~= "number" do
  247.     print("X is not an integer")
  248.     x = tonumber(read())
  249.   end
  250.   while type(y) ~= "number" do
  251.     print("Y is not an integer")
  252.     y = tonumber(read())
  253.   end
  254.   while type(z) ~= "number" do
  255.     print("Z is not an integer")
  256.     z = tonumber(read)
  257.   end
  258.   while type(yLimit) ~= "number" do
  259.     print("The Y Limit is not an integer")
  260.     yLimit = tonumber(read())
  261.   end
  262.   table.insert(waypoints,x)
  263.   table.insert(waypoints,y)
  264.   table.insert(waypoints,z)
  265.   table.insert(waypoints,yLimit)
  266.   file = fs.open("waypoints", "a")
  267.   file.writeLine(name)
  268.   file.writeLine(x)
  269.   file.writeLine(y)
  270.   file.writeLine(z)
  271.   file.writeLine(yLimit)
  272.   file.close()
  273. end
  274. function deleteWaypoint(name)
  275.   for i=#waypoints,1,-1 do
  276.     if waypoints[i] == name then
  277.       table.remove(waypoints, i)
  278.       table.remove(waypoints, i)
  279.       table.remove(waypoints, i)
  280.       table.remove(waypoints, i)
  281.       table.remove(waypoints, i)
  282.       local file = fs.open("waypoints", "w")
  283.       for key,value in pairs(waypoints) do
  284.         file.writeLine(value)
  285.       end
  286.       file.close()
  287.     end
  288.   end
  289. end
  290. function listWaypoints()
  291.   for i=1,#waypoints do
  292.     if type(tonumber(waypoints[i])) == "nil" then
  293.       print(waypoints[i])
  294.     end
  295.   end
  296. end
  297. function turtle.gotoWaypoint(name)
  298.   local turtleGo = false
  299.   for key,value in pairs(waypoints) do
  300.     if value == name then
  301.       gotoX = waypoints[key + 1]
  302.       gotoY = waypoints[key + 2]
  303.       gotoZ = waypoints[key + 3]
  304.       yLimit = waypoints[key + 4]
  305.       gotoX = tonumber(gotoX)
  306.       gotoY = tonumber(gotoY)
  307.       gotoZ = tonumber(gotoZ)
  308.       yLimit = tonumber(yLimit)
  309.       turtleGo = true
  310.     end
  311.   end
  312.   if turtleGo == false then
  313.     error("Bad argument: Expected listed waypoint, got "..name.."!")
  314.   end
  315.   while (((gotoX ~= X) or (gotoY ~= Y)) or (gotoZ ~= Z)) and turtleGo do
  316.     if yLimit > Y then
  317.       local skyDif = yLimit - Y
  318.       turtle.move("up",skyDif)
  319.     elseif yLimit < Y then
  320.       local skyDif = Y - yLimit
  321.       turtle.move("down",skyDif)
  322.     end
  323.     while gotoX ~= X do
  324.       if gotoX > X then
  325.         local Xdif = gotoX - X
  326.         if face == 0 then
  327.           turtle.turn("right")
  328.         elseif face == 1 then
  329.           turtle.turn("back")
  330.         elseif face == 2 then
  331.           turtle.turn("left")
  332.         end
  333.         turtle.move("forward",Xdif)
  334.       else
  335.         local Xdif = X - gotoX
  336.         if face == 0 then
  337.           turtle.turn("left")
  338.         elseif face == 3 then
  339.           turtle.turn("back")
  340.         elseif face == 2 then
  341.           turtle.turn("right")
  342.         end
  343.         turtle.move("forward",Xdif)
  344.       end
  345.     end
  346.     while gotoZ ~= Z do
  347.       if gotoZ > Z then
  348.         local Zdif = gotoZ - Z
  349.         if face == 0 then
  350.           turtle.turn("back")
  351.         elseif face == 1 then
  352.           turtle.turn("left")
  353.         elseif face == 3 then
  354.           turtle.turn("right")
  355.         end
  356.         turtle.move("forward",Zdif)
  357.       else
  358.         local Zdif = Z - gotoZ
  359.         if face == 1 then
  360.           turtle.turn("right")
  361.         elseif face == 2 then
  362.           turtle.turn("back")
  363.         elseif face == 3 then
  364.           turtle.turn("left")
  365.         end
  366.         turtle.move("forward",Zdif)
  367.       end
  368.     end
  369.     while gotoY ~= Y do
  370.       if gotoY < Y then
  371.         local Ydif = Y - gotoY
  372.         turtle.move("down",Ydif)
  373.       elseif gotoY > Y then
  374.         local Ydif = gotoY - Y
  375.         turtle.move("up",Ydif)
  376.       end
  377.     end
  378.   end
  379. end
Advertisement
Add Comment
Please, Sign In to add comment