Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --once program is added refer to the gps.functions only by there name, you don't have to add gps.
- --to use in your program add(make sure program is called turtle_gps)
- --[[
- if(not shell.run("turtle_gps"))then
- term.clear(); term.setCursorPos(1,1);
- print("ERROR: You need turtle_gps to run this program!");
- return false;
- end
- ]]
- local gps = {};
- local position = {0,0,0};
- local orientation = 0;
- --0 = x+
- --1 = z+
- --2 = x-
- --3 = z-
- local dir_moves = {
- {1,0},
- {0,1},
- {-1,0},
- {0,-1},
- {1,0}
- };
- gps.setCoordinate = function(x,y,z)
- position[1] = x;
- position[2] = y;
- position[3] = z;
- end
- gps.requestCoordinate = function()
- term.clear(); term.setCursorPos(1,1);
- local x,y,z;
- print("Enter turtle coordinates!");
- term.write("X: "); x=tonumber(read());
- term.setCursorPos(1,3);
- term.write("Y: "); y=tonumber(read());
- term.setCursorPos(1,4);
- term.write("Z: "); z=tonumber(read());
- position[1] = x;
- position[2] = y;
- position[3] = z;
- print("If turtle is not facing x+ then it will have inaccurate gps readings!");
- end
- gps.getPosition = function()
- return unpack(position);
- end
- gps.getDirection = function()
- return orientation;
- end
- gps.getOrientation = gps.getDirection;
- gps.moveVertical = function(id)
- id = id or 1;
- local moved;
- if(id == 1)then
- moved = turtle.up();
- if(not moved)then return false; end
- position[2] = position[2] + 1;
- else
- moved = turtle.down();
- if(not moved)then return false; end
- position[2] = position[2] - 1;
- end
- return true;
- end
- gps.forward = function(text)
- local moved = turtle.forward();
- if(not moved)then return false; end
- local dir = dir_moves[orientation%4+1];
- position[1] = position[1] + dir[1];
- position[3] = position[3] + dir[2];
- return true;
- end
- gps.turnLeft = function()
- turtle.turnLeft();
- orientation = (orientation - 1) % 4;
- end
- gps.turnRight = function()
- turtle.turnRight();
- orientation = (orientation + 1) % 4;
- end
- gps.setOrientation = function(o)
- o = o % 4;
- if(orientation==o)then return; end
- local x;
- if( (orientation-1) % 4 == o)then
- x = gps.turnLeft;
- else
- x = gps.turnRight;
- end
- while(orientation~=o)do
- x();
- end
- if(orientation~=o)then print("[GPS] ERROR! Orientation mismatch!"); return false; end
- return true;
- end
- gps.loadAPI = function()
- local env = getfenv();
- for i,v in pairs(gps)do
- env[i] = v;
- end
- end
- gps.setCoordinate = gps.requestCoordinate;
- gps.loadAPI();
Advertisement
Add Comment
Please, Sign In to add comment