Advertisement
SolidSnake96AS

Advance Movement API 2.0

Sep 2nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. --[[
  2.     The coordinate system has center to the turtle
  3.     and is rotated relatively to the turtle
  4. ]]--
  5.  
  6. --Local variables
  7. local x, y, z, dir, waitTime;
  8.  
  9. --Global variables
  10. L=0;
  11. U=1;
  12. R=2;
  13. D=3;
  14.  
  15. dir=1;
  16. x=0;
  17. y=0;
  18. z=0;
  19. waitTime=30;
  20.  
  21. --Functions
  22.  
  23. local function updateCoords(incrementValue)
  24.    
  25.     if (dir==0)
  26.     then
  27.         x=x+incrementValue;
  28.     elseif (dir==1)
  29.     then
  30.         y=y+incrementValue;
  31.     elseif (dir==2)
  32.     then
  33.         x=x-incrementValue;
  34.     elseif (dir==3)
  35.     then
  36.         y=y-incrementValue;
  37.     end
  38. end
  39.  
  40. function f(gravelSensor) --Go forward
  41.     local i;
  42.    
  43.     i=0;
  44.    
  45.     while (true)
  46.     do
  47.         if (turtle.forward())
  48.         then
  49.             updateCoords(1);
  50.            
  51.             break;
  52.         end
  53.        
  54.         if (gravelSensor)
  55.         then
  56.             if (i>waitTime)--Maybe there is gravel or a mob!
  57.             then
  58.                 turtle.dig();
  59.                 turtle.attack();
  60.             end
  61.            
  62.             i=i+1;
  63.         end
  64.     end
  65. end
  66.  
  67. function b() --Go back
  68.     while (true)
  69.     do
  70.         if (turtle.back())
  71.         then
  72.             updateCoords(-1);
  73.  
  74.             break;
  75.         end
  76.     end
  77. end
  78.  
  79. function u(gravelSensor) --Go up
  80.     local i=0;
  81.  
  82.     while (true)
  83.     do
  84.         if (turtle.up())
  85.         then
  86.             z=z+1;
  87.             break;
  88.         end
  89.  
  90.         if (gravelSensor)
  91.         then
  92.             if (i>waitTime) --Maybe there is gravel or a mob!
  93.             then
  94.                 turtle.digUp();
  95.                 turtle.attackUp();
  96.             end
  97.            
  98.             i=i+1;
  99.         end
  100.     end
  101. end
  102.  
  103. function d(gravelSensor) --Go down
  104.     local i=0;
  105.  
  106.     while (true)
  107.     do
  108.         if (turtle.down())
  109.         then
  110.             z=z-1;
  111.             break;
  112.         end
  113.  
  114.         if (gravelSensor)
  115.         then
  116.             if (i>waitTime) --Maybe there is gravel or a mob!
  117.             then
  118.                 turtle.digDown();
  119.                 turtle.attackDown();
  120.             end
  121.        
  122.             i=i+1;
  123.         end
  124.     end
  125. end
  126.  
  127. function r() --Turn right
  128.     turtle.turnRight();
  129.    
  130.     if (dir==0)
  131.     then
  132.         dir=3;
  133.     else
  134.         dir=dir-1;
  135.     end
  136. end
  137.  
  138. function l() --Turn left
  139.     turtle.turnLeft();
  140.    
  141.     if (dir==3)
  142.     then
  143.         dir=0;
  144.     else
  145.         dir=dir+1;
  146.     end
  147. end
  148.  
  149. function digL() --Dig to the left
  150.     turtle.turnLeft();
  151.     turtle.dig();
  152.     turtle.turnRight();
  153. end
  154.  
  155. function digR() --Dig to the right
  156.     turtle.turnRight();
  157.     turtle.dig();
  158.     turtle.turnLeft();
  159. end
  160.  
  161. function pointTo(_dir)
  162.     while(getDir()>_dir)
  163.     do
  164.         r();
  165.     end
  166.    
  167.     while(getDir()<_dir)
  168.     do
  169.         l();
  170.     end
  171. end
  172.  
  173. function getDir() --Get the direction of the turtle (0=left, 1=up, 2=right, 3=down)
  174.     return dir;
  175. end
  176.  
  177. function setDir(_dir) --Set the direction of the turtle
  178.     dir=_dir;
  179. end
  180.  
  181. function setPos(_x, _y, _z) --Set the coordinates
  182.     x=_x;
  183.     y=_y;
  184.     z=_z;
  185. end
  186.  
  187. function pos()
  188.     return {x, y, z};
  189. end
  190.  
  191. function getX()
  192.     return x;
  193. end
  194.  
  195. function getY()
  196.     return y;
  197. end
  198.  
  199. function getZ()
  200.     return z;
  201. end
  202.  
  203.  
  204. function setWaitTime(_waitTime)
  205.     waitTime=_waitTime;
  206. end
  207.  
  208. function getWaitTime()
  209.     return waitTime;
  210. end
  211.  
  212. --Aliasses
  213. forward=f;
  214. back=b;
  215. turnLeft=l;
  216. turnL=l;
  217. turnRight=r;
  218. turnR=r;
  219. up=u;
  220. down=d;
  221. getPos=pos;
  222. LEFT=L;
  223. UP=U;
  224. RIGHT=R;
  225. DOWN=D;
  226.  
  227. --Original script by SolidSnake96AS.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement