Advertisement
Guest User

Untitled

a guest
May 4th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1.  
  2. --------------------------------------------------------------------------------------------------------------------
  3. --> GLOBALS
  4. --------------------------------------------------------------------------------------------------------------------
  5.  
  6. -- Position we reside in, relative to time of execution
  7. local pos = vector.new(0, 0, 0);
  8.  
  9. -- Direction we're facing
  10. DIRECTION_FORWARD = 0x0;
  11. DIRECTION_LEFT = 0x1;
  12. DIRECTION_BACK = 0x2;
  13. DIRECTION_RIGHT = 0x3;
  14. local dir = DIRECTION_FORWARD;
  15.  
  16. --------------------------------------------------------------------------------------------------------------------
  17. --> TURTLE ABSTRACTION FUNCTIONS
  18. --------------------------------------------------------------------------------------------------------------------
  19.  
  20. ---
  21. --- Rotates in dir, x times. x will default to one.
  22. --- This updates the orientation of the turtle.
  23. ---
  24. local function rotate(d, x)
  25. if (not x) then
  26. x = 1;
  27. end
  28.  
  29. for i = 1, x do
  30. if (d == DIRECTION_LEFT) then
  31. turtle.turnLeft();
  32.  
  33. -- Increase with clamp
  34. if (dir == 3) then
  35. dir = 0;
  36. else
  37. dir = dir + 1;
  38. end
  39. else
  40. turtle.turnRight();
  41.  
  42. -- Decrease with clamp
  43. if (dir == 0) then
  44. dir = 3;
  45. else
  46. dir = dir - 1;
  47. end
  48. end
  49. end
  50. end
  51.  
  52. -- Helper functions for above
  53. local function left(x) rotate(DIRECTION_LEFT, x); end
  54. local function right(x) rotate(DIRECTION_RIGHT, x); end
  55.  
  56. ---
  57. --- Changes the angle of a turtle to match desired using
  58. --- as little rotations possible
  59. ---
  60. local function setAngle(desired)
  61. local min = math.min(dir, desired);
  62. local max = math.max(dir, desired);
  63. local diff = max - min;
  64.  
  65. -- 180 spins / right - forward
  66. if (diff == 2) then
  67. return left(2);
  68. elseif (diff == 3) then
  69. return right();
  70. end
  71.  
  72. -- Should always be one right/left change
  73. if (desired > dir) then
  74. left();
  75. elseif (desired < dir) then
  76. right();
  77. end
  78. end
  79.  
  80. ---
  81. --- Moves x blocks fowards caving one block above the turtle
  82. --- and one infront of it. Updates coords.
  83. ---
  84. local function move(x)
  85. for i = 1, x do
  86.  
  87. -- Forward can fail, dig until we move
  88. -- forwards succesfully
  89. repeat
  90. turtle.dig();
  91. turtle.digUp();
  92. until turtle.forward();
  93.  
  94. -- Update coords
  95. if (dir == DIRECTION_FORWARD) then
  96. pos.y = pos.y + 1;
  97. elseif (dir == DIRECTION_LEFT) then
  98. pos.x = pos.x - 1;
  99. elseif (dir == DIRECTION_BACK) then
  100. pos.y = pos.y - 1;
  101. elseif (dir == DIRECTION_RIGHT) then
  102. pos.x = pos.x + 1;
  103. end
  104.  
  105. end
  106. end
  107.  
  108. ---
  109. --- Goes to a vector.
  110. ---
  111. local function goto(x, y, preserve)
  112. if (pos.x ~= x) then
  113. if (pos.x > x) then
  114. setAngle(DIRECTION_LEFT);
  115. else
  116. setAngle(DIRECTION_RIGHT);
  117. end
  118.  
  119. move(math.abs(pos.x - x));
  120. end
  121.  
  122. if (pos.y ~= y) then
  123. if (pos.y > y) then
  124. setAngle(DIRECTION_BACK);
  125. else
  126. setAngle(DIRECTION_FORWARD);
  127. end
  128.  
  129. move(math.abs(pos.y - y));
  130. end
  131. end
  132.  
  133. ---
  134. --- Returns the turtle so that it's facing the block that it started on
  135. ---
  136. local function goHome()
  137. goto(0, 0, true);
  138. end
  139.  
  140. --[[
  141. ---
  142. --
  143. ---
  144. local function dump()
  145.  
  146. -- Cache selected index
  147. local selected = ttl("getSelectedSlot");
  148.  
  149. -- Dump items
  150. for i = 1, 16 do
  151. if (ttl("select", i)) then
  152. local count = ttl("getItemCount");
  153.  
  154. if (count > 0) then
  155. ttl("drop", count - 1);
  156. end
  157. end
  158. end
  159.  
  160. -- Restore selected index
  161. ttl("select", selected);
  162. end
  163.  
  164. ---
  165. --- Refuel from chest infront of the turtle
  166. ---
  167. function refuel()
  168.  
  169. local limit = ttl("getFuelLimit");
  170. local current = ttl("getFuelLevel");
  171. local need = limit - current;
  172. local count = math.ceil(need / 1000);
  173. local selected = ttl("getSelectedSlot");
  174.  
  175. if (count > 64) then
  176. count = 64;
  177. end
  178.  
  179. -- Attempt to aqquire fuel
  180. if (not ttl("suck", count)) then
  181. error("Could not refuel from storage");
  182. end
  183.  
  184. -- Find fuel and refuel from it
  185. for i = 1, 16 do
  186. if (ttl("select", i)) then
  187. local count = ttl("getItemCount");
  188. if (ttl("refuel", count - 1)) then
  189. break;
  190. end
  191. end
  192. end
  193.  
  194. ttl("select", selected);
  195. end]]--
  196.  
  197. local arg = { ... };
  198. local x = tonumber(arg[ 1 ]);
  199. local y = tonumber(arg[ 2 ]);
  200. goto(x, y);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement