Advertisement
eonsv

ProtLib

Nov 2nd, 2012
2,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.60 KB | None | 0 0
  1. --Protactin/Eonsv's move and dig API.
  2. --Used for all my programs.
  3. --v1.4
  4.  
  5. --Coordinates used in the functions of this API.
  6. --All coordinates are integer values.
  7. local x;
  8. local y;
  9. local z;
  10. local rot;
  11.  
  12. --Get function for the coordinate x.
  13. function getX()
  14.    return x;
  15. end;
  16.  
  17. --Set function for the coordinate x.
  18. function setX(newX)
  19.    x = newX;
  20.    x = x - x%1;
  21. end;
  22.  
  23. --Get function for the coordinate y.
  24. function getY()
  25.    return y;
  26. end;
  27.  
  28. --Set function for the coordinate y.
  29. function setY(newY)
  30.    y = newY;
  31.    y = y - y%1;
  32. end;
  33.  
  34. --Get function for the coordinate z.
  35. function getZ()
  36.    return z;
  37. end;
  38.  
  39. --Set function for the coordinate z.
  40. function setZ(newZ)
  41.    z = newZ;
  42.    z = z - z%1;
  43. end;
  44.  
  45. --Get function for the coordinate rot.
  46. function getRot()
  47.    return rot;
  48. end;
  49.  
  50. --Set function for the coordinate rot.
  51. function setRot(newRot)
  52.    rot = newRot;
  53.    rot = rot - rot%1;
  54. end;
  55.  
  56. --Selects the first empty slot 3-16.
  57. function selectFirstEmpty()
  58.    for i = 3, 16 do
  59.       if(turtle.getItemCount(i) == 0) then
  60.          turtle.select(i);
  61.          return i;
  62.       end;
  63.    end;
  64.    return 0;
  65. end;
  66.  
  67. --Fixes the rot variable to rot = 0, 1, 2, 3
  68. local function fixRot()
  69.    rot = rot - rot%1;
  70.    while(rot < 0) do
  71.       rot = rot + 4;
  72.    end;
  73.    while(rot > 3) do
  74.       rot = rot - 4;
  75.    end;
  76.    return rot;
  77. end;
  78.  
  79. --Update the coordinates x, y.
  80. local function newPos()
  81.    fixRot();
  82.    if(rot == 0) then
  83.       x = x + 1;
  84.    elseif(rot == 1) then
  85.       y = y + 1;
  86.    elseif(rot == 2) then
  87.       x = x - 1;
  88.    else
  89.       y = y - 1;
  90.    end;
  91. end;
  92.  
  93. --Initializes the possition. x = a; y = b; z = c and the rotation rot = r.
  94. function setPos(newX, newY, newZ, newRot)
  95.    if(newX == nil) then
  96.       x = 0;
  97.    else
  98.       x = newX;
  99.    end;
  100.    if(newY == nil) then
  101.       y = 0;
  102.    else
  103.       y = newY;
  104.    end;
  105.    if(newZ == nil) then
  106.       z = 0;
  107.    else
  108.       z = newZ;
  109.    end;
  110.    if(newRot == nil) then
  111.       rot = 0;
  112.    else
  113.       rot = newRot;
  114.    end;
  115. end;
  116.  
  117. --Rotates in positive direction(dir > 0) or negative direction (dir < 0)
  118. --Returns the current rotation rot
  119. function rotate(dir)
  120.    fixRot();
  121.    if(dir >= 1) then
  122.       rot = rot + 1;
  123.       turtle.turnLeft();
  124.       dir = dir - 1;
  125.    elseif(dir <= -1) then
  126.       rot = rot - 1;
  127.       turtle.turnRight();
  128.       dir = dir + 1;
  129.    end;
  130.    fixRot();
  131.    if(dir < 0 or dir > 0) then
  132.       rotate(dir);
  133.    end;
  134.    return rot;
  135. end;
  136.  
  137. --Tries to move forward, if unable to then attacks and digs untill it is clear,
  138. --or untill it has tried N times since last successfull attack or dig.
  139. --s seconds between each try.
  140. --Returns true if it is able to move forward, false if not.
  141. function forward(N, s)
  142.    if(N == nil) then
  143.       N = 5;
  144.    end;
  145.    if(s == nil) then
  146.       s = 0.5;
  147.    end;
  148.    local n = 0;
  149.    while(not turtle.forward()) do
  150.       n = n + 1;
  151.       if(turtle.attack()) then
  152.          n = 0;
  153.       elseif(turtle.dig()) then
  154.          n = 0;
  155.       end;
  156.       if(n == N) then
  157.          return false;
  158.       end;
  159.       os.sleep(s);
  160.    end;
  161.    newPos();
  162.    return true;
  163. end;
  164.  
  165. --Tries to move up, if unable to then attacks and digs untill it is clear,
  166. --or untill it has tried N times since last successfull attack or dig.
  167. --s seconds between each try.
  168. --Returns true if it is able to move up, false if not.
  169. function up(N, s)
  170.    if(N == nil) then
  171.       N = 5;
  172.    end;
  173.    if(s == nil) then
  174.       s = 0.5;
  175.    end;
  176.    local n = 0;
  177.    while(not turtle.up()) do
  178.       n = n + 1;
  179.       if(turtle.attackUp()) then
  180.          n = 0;
  181.       elseif(turtle.digUp()) then
  182.          n = 0;
  183.       end;
  184.       os.sleep(s);
  185.       if(n == N) then
  186.          return false;
  187.       end;
  188.    end;
  189.    z = z + 1;
  190.    return true;
  191. end;
  192.  
  193. --Tries to move down, if unable to then attacks and digs untill it is clear,
  194. --or untill it has tried N times since last successfull attack or dig.
  195. --s seconds between each try.
  196. --Returns true if it is able to move down, false if not.
  197. function down(N, s)
  198.    if(N == nil) then
  199.       N = 5;
  200.    end;
  201.    if(s == nil) then
  202.       s = 0.5;
  203.    end;
  204.    local n = 0;
  205.    while(not turtle.down()) do
  206.       n = n + 1;
  207.       if(turtle.attackDown()) then
  208.          n = 0;
  209.       elseif(turtle.digDown()) then
  210.          n = 0;
  211.       end;
  212.       if(n == N) then
  213.          return false;
  214.       end;
  215.       os.sleep(s);
  216.    end;
  217.    z = z - 1;
  218.    return true;
  219. end;
  220.  
  221. --Tries to move backwards, if unable to then attacks and digs untill it is clear,
  222. --or untill it has tried N times since last successfull attack or dig.
  223. --s seconds between each try.
  224. --Returns true if it is able to move backwards, false if not.
  225. function back(N, s)
  226.    local try;
  227.    if(not turtle.back()) then
  228.       rotate(2);
  229.       try = forward(N, s);
  230.       rotate(2);
  231.       return try;
  232.    else
  233.       rot = rot + 2;
  234.       newPos();
  235.       rot = rot + 2;
  236.       fixRot();
  237.       return true;
  238.    end;
  239. end;
  240.  
  241. --Digs untill clear, or untill the turtle have tried N times.
  242. --s seconds between each try.
  243. --Returns true if successfull, false if not
  244. function dig(N, s)
  245.    if(N == nil) then
  246.       N = 5;
  247.    end;
  248.    if(s == nil) then
  249.       s = 0.5;
  250.    end;
  251.    local n = 0;
  252.    while(turtle.detect()) do
  253.       if(turtle.dig()) then
  254.          n = 0;
  255.       else
  256.          n = n + 1;
  257.       end;
  258.       if(n == N) then
  259.          return false;
  260.       end;
  261.       os.sleep(s);
  262.    end;
  263.    return true;
  264. end;
  265.  
  266. --Digs up untill clear, or untill the turtle have tried N times.
  267. --s seconds between each try.
  268. --Returns true if successfull, false if not
  269. function digUp(N, s)
  270.    if(N == nil) then
  271.       N = 5;
  272.    end;
  273.    if(s == nil) then
  274.       s = 0.5;
  275.    end;
  276.    local n = 0;
  277.    while(turtle.detectUp()) do
  278.       if(turtle.digUp()) then
  279.          n = 0;
  280.       else
  281.          n = n + 1;
  282.       end;
  283.       if(n == N) then
  284.          return false;
  285.       end;
  286.       os.sleep(s);
  287.    end;
  288.    return true;
  289. end;
  290.  
  291. --Digs down untill clear, or untill the turtle have tried N times.
  292. --s seconds between each try.
  293. --Returns true if successfull, false if not
  294. function digDown(N, s)
  295.    if(N == nil) then
  296.       N = 5;
  297.    end;
  298.    if(s == nil) then
  299.       s = 0.5;
  300.    end;
  301.    local n = 0;
  302.    while(turtle.detectDown()) do
  303.       if(turtle.digDown()) then
  304.          n = 0;
  305.       else
  306.          n = n + 1;
  307.       end;
  308.       if(n == N) then
  309.          return false;
  310.       end;
  311.       os.sleep(s);
  312.    end;
  313.    return true;
  314. end;
  315.  
  316. --Rotates to r, rotating the least number of times needed.
  317. --Returns number of times rotated |dr| and the direction rotated dr/|dr|.
  318. function rotateTo(r)
  319.    fixRot();
  320.    local dr = r - rot;
  321.    if(dr == 0) then
  322.       return 0;
  323.    elseif(dr < -2) then
  324.       rotate(1);
  325.       return 1;
  326.    elseif(dr > 2) then
  327.       rotate(-1);
  328.       return -1;
  329.    else
  330.       rotate(dr);
  331.       return dr;
  332.    end;
  333. end;
  334.  
  335. --Moves to the coordinates xe, ye, ze. Attacks and digs if something is in it's path.
  336. --Returns true if successfull. False if stuck.
  337. function moveTo(xe, ye, ze)
  338.    local try = true;
  339.    if(x < xe) then
  340.       rotateTo(0);
  341.       while(x < xe) do
  342.          try = forward();
  343.          if(not try) then
  344.             return false;
  345.          end;
  346.       end;
  347.    elseif(x > xe) then
  348.       rotateTo(2);
  349.       while(x > xe) do
  350.          try = forward();
  351.          if(not try) then
  352.             return false;
  353.          end;
  354.       end;
  355.    end;
  356.    if(y < ye) then
  357.       rotateTo(1);
  358.       while(y < ye) do
  359.          try = forward();
  360.          if(not try) then
  361.             return false;
  362.          end;
  363.       end;
  364.    elseif(y > ye) then
  365.       rotateTo(3);
  366.       while(y > ye) do
  367.          try = forward();
  368.          if(not try) then
  369.             return false;
  370.          end;
  371.       end;
  372.    end;
  373.    if(z < ze) then
  374.       while(z < ze) do
  375.          try = up();
  376.          if(not try) then
  377.             return false;
  378.          end;
  379.       end;
  380.    elseif(z > ze) then
  381.       while(z > ze) do
  382.          try = down();
  383.          if(not try) then
  384.             return false;
  385.          end;
  386.       end;
  387.    end;
  388.    return true;
  389. end;
  390.  
  391. --Moves dx, dy, dz blocks in the x, y, z direction.
  392. --Returns true if sucsessfull, false if not.
  393. function moveRel(dx, dy, dz)
  394.    return moveTo(getX() + dx, getY() + dy, getZ() + dz);
  395. end;
  396.  
  397. --Commonly used printing functions
  398.  
  399. --Clears the screen, and sets the cursor at the top left of the screen.
  400. function clear()
  401.     term.clear();
  402.     term.setCursorPos(1, 1);
  403. end;
  404.  
  405. --Sleeps time seconds, while displaying the message text and counting down.
  406. function sleep(text, time)
  407.     for i = 0, time - 1 do
  408.         clear();
  409.         print(text);
  410.         print('Countdown: '..time - i..'s.');
  411.         os.sleep(1);
  412.     end;
  413.     clear();
  414. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement