CoderJohn

turtle_gps

Nov 13th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --once program is added refer to the gps.functions only by there name, you don't have to add gps.
  2. --to use in your program add(make sure program is called turtle_gps)
  3. --[[
  4. if(not shell.run("turtle_gps"))then
  5.   term.clear(); term.setCursorPos(1,1);
  6.   print("ERROR: You need turtle_gps to run this program!");
  7.   return false;
  8. end
  9. ]]
  10.  
  11. local gps = {};
  12. local position = {0,0,0};
  13. local orientation = 0;
  14.   --0 = x+
  15.   --1 = z+
  16.   --2 = x-
  17.   --3 = z-
  18. local dir_moves = {
  19.   {1,0},
  20.   {0,1},
  21.   {-1,0},
  22.   {0,-1},
  23.   {1,0}
  24. };
  25.  
  26. gps.setCoordinate = function(x,y,z)
  27.   position[1] = x;
  28.   position[2] = y;
  29.   position[3] = z;
  30. end
  31.  
  32. gps.requestCoordinate = function()
  33.   term.clear(); term.setCursorPos(1,1);
  34.   local x,y,z;
  35.   print("Enter turtle coordinates!");
  36.   term.write("X: "); x=tonumber(read());
  37.  
  38.   term.setCursorPos(1,3);
  39.   term.write("Y: "); y=tonumber(read());
  40.   term.setCursorPos(1,4);
  41.   term.write("Z: "); z=tonumber(read());
  42.   position[1] = x;
  43.   position[2] = y;
  44.   position[3] = z;
  45.   print("If turtle is not facing x+ then it will have inaccurate gps readings!");
  46. end
  47.  
  48. gps.getPosition = function()
  49.   return unpack(position);
  50. end
  51.  
  52. gps.getDirection = function()
  53.   return orientation;
  54. end
  55.  
  56. gps.getOrientation = gps.getDirection;
  57.  
  58. gps.moveVertical = function(id)
  59.   id = id or 1;
  60.   local moved;
  61.   if(id == 1)then
  62.     moved = turtle.up();
  63.     if(not moved)then return false; end
  64.     position[2] = position[2] + 1;
  65.   else
  66.     moved = turtle.down();
  67.     if(not moved)then return false; end
  68.     position[2] = position[2] - 1;
  69.   end
  70.   return true;
  71. end
  72.  
  73. gps.forward = function(text)
  74.   local moved = turtle.forward();
  75.   if(not moved)then return false; end
  76.   local dir = dir_moves[orientation%4+1];
  77.   position[1] = position[1] + dir[1];
  78.   position[3] = position[3] + dir[2];
  79.   return true;
  80. end
  81.  
  82. gps.turnLeft = function()
  83.   turtle.turnLeft();
  84.   orientation = (orientation - 1) % 4;
  85. end
  86.  
  87. gps.turnRight = function()
  88.   turtle.turnRight();
  89.   orientation = (orientation + 1) % 4;
  90. end
  91.  
  92. gps.setOrientation = function(o)
  93.   o = o % 4;
  94.   if(orientation==o)then return; end
  95.   local x;
  96.   if( (orientation-1) % 4 == o)then
  97.     x = gps.turnLeft;
  98.   else
  99.     x = gps.turnRight;
  100.   end
  101.   while(orientation~=o)do
  102.     x();
  103.   end
  104.   if(orientation~=o)then print("[GPS] ERROR! Orientation mismatch!"); return false; end
  105.   return true;
  106. end
  107.  
  108. gps.loadAPI = function()
  109.   local env = getfenv();
  110.   for i,v in pairs(gps)do
  111.     env[i] = v;
  112.   end
  113. end
  114. gps.setCoordinate = gps.requestCoordinate;
  115.  
  116. gps.loadAPI();
Advertisement
Add Comment
Please, Sign In to add comment