Guest User

LavaLakeFarm

a guest
May 21st, 2014
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. local tArgs = { ... };
  2. local MAX_DIST = 20;
  3. local MAX_FUEL = 20000;
  4.  
  5. -- The different orientations (DOWN is +Y and UP is -Y)
  6. local NORTH = 0; -- -Z
  7. local EAST = 1; -- +X
  8. local SOUTH = 2; -- +Z
  9. local WEST = 3; -- -X
  10.  
  11. -- The different directions
  12. local UP = 4;
  13. local DOWN = 5;
  14. local FORWARD = 6;
  15. local RIGHT = 7;
  16. local BACK = 8;
  17. local LEFT = 9;
  18.  
  19.  
  20. -- The starting position and orientation
  21. local orientation = NORTH;
  22.  
  23. -- Directions to get back to start
  24. local returnStack = {};
  25.  
  26.  
  27.  
  28. -- --------------------------------- --
  29. -- STACK FUNCTIONS                   --
  30. -- --------------------------------- --
  31.  
  32. --
  33. --  Put a new value on the stack
  34. --
  35. --  @param stack - The stack to add the value to
  36. --  @param val - The value to push onto the stack.
  37. --
  38. function push(stack, val)
  39.   if (stack["length"] == nil) then
  40.     stack["length"] = 1;
  41.   else
  42.     stack["length"] = stack["length"] + 1;
  43.   end
  44.  
  45.   stack[stack["length"]] = val;
  46. end
  47.  
  48. --
  49. --  Pops the next value off the top of the stack.
  50. --
  51. --  @param stack - The stack to take the value from.
  52. --  @return Returns the value from the top of the stack or nil if there are no items.
  53. --
  54. function pop(stack)
  55.   if (stack["length"] == nil or stack["length"] == 0) then
  56.     return nil;
  57.   else
  58.     local val = stack[stack["length"]];
  59.     stack["length"] = stack["length"] - 1;
  60.     return val;
  61.   end
  62. end
  63.  
  64. --
  65. --  Finds the size of a stack.
  66. --
  67. --  @param stack - The stack to find the size of.
  68. --  @return Returns the number of items on the provided stack.
  69. --
  70. function size(stack)
  71.   if (stack["length"] == nil) then
  72.     return 0;
  73.   else
  74.     return stack["length"];
  75.   end
  76. end
  77.  
  78. -- --------------------------------- --
  79. -- MOVEMENT FUNCTIONS                --
  80. -- --------------------------------- --
  81.  
  82. --
  83. --  Used to move the turtle in the specified direction. If something is blocking it, the turtle
  84. --  will continue to attack and attempt to move until it completes. If moving BACK, the turtle
  85. --  will not attack between attempting to move. Regardless of where it moves, the turtle will
  86. --  end with the same orientation that it started with
  87. --
  88. --  @param dir - The direction or orientation to move the turtle
  89. --
  90. function move(dir)
  91.   if (dir == FORWARD) then
  92.     while (not(turtle.forward())) do turtle.attack(); end;
  93.   elseif (dir == BACK) then
  94.     while (not(turtle.back())) do os.sleep(.1); end;
  95.   elseif (dir == UP) then
  96.     while (not(turtle.up())) do  turtle.attackUp(); end;
  97.   elseif (dir == DOWN) then
  98.     while (not(turtle.down())) do turtle.attackDown(); end;
  99.   elseif (dir == RIGHT) then
  100.     turn(RIGHT);
  101.     move(FORWARD);
  102.     turn(LEFT);
  103.   elseif (dir == LEFT) then
  104.     turn(LEFT);
  105.     move(FORWARD);
  106.     turn(RIGHT);
  107.   elseif (dir == NORTH or dir == EAST or dir == SOUTH or dir == WEST) then
  108.     local oldOrient = orientation;
  109.     turn(dir);
  110.     move(FORWARD);
  111.     turn(oldOrient);
  112.   end
  113. end
  114.  
  115. --
  116. --  Used to turn the turtle the specified direction or to the orientation provided
  117. --
  118. --  @param dir - The direction or orientation to turn the turtle
  119. --
  120. function turn(dir)
  121.   if (dir == RIGHT) then
  122.     turtle.turnRight();
  123.     orientation = (orientation + 1) % 4;
  124.    
  125.   elseif (dir == LEFT) then
  126.     turtle.turnLeft();
  127.     orientation = (orientation + 3) % 4; -- mathematically, a left turn is the same as 3 right turns
  128.    
  129.   elseif (dir == NORTH or dir == EAST or dir == SOUTH or dir == WEST) then
  130.     local numLeftTurns = ((4 + orientation) - dir) % 4;
  131.    
  132.     if (numLeftTurns == 3) then -- it's shorter to do just one right turn for this case
  133.       turtle.turnRight();
  134.     else
  135.       for i=1,numLeftTurns do turtle.turnLeft(); end -- perform left turns to new orientation
  136.     end
  137.     orientation = dir;
  138.    
  139.   end
  140. end
  141.  
  142.  
  143.  
  144. -- --------------------------------- --
  145. -- UTILITY FUNCTIONS                 --
  146. -- --------------------------------- --
  147.  
  148. --
  149. --  Used to collect lava from a specific direction. This function does not maintain the original
  150. --  turtle orientation. The turtle will be facing whichever direction it was told to collected from.
  151. --
  152. --  @param dir - The direction or orientation to collect from
  153. --  @return Will return true if lava was actually collected and used.
  154. --
  155. function collectFuel(dir)
  156.  
  157.   -- attempt to pick up lava from the specified location
  158.   if (dir == FORWARD) then
  159.     turtle.place();
  160.   elseif (dir == UP) then
  161.     turtle.placeUp();
  162.   elseif (dir == DOWN) then
  163.     turtle.placeDown();
  164.   elseif (dir == NORTH or dir == EAST or dir == SOUTH or dir == WEST) then
  165.     turn(dir);
  166.     turtle.place();
  167.   else
  168.     return false;
  169.   end
  170.  
  171.   -- check if lava was actually collected
  172.   if (not(turtle.compareTo(2))) then
  173.     return false;
  174.   end
  175.  
  176.   turtle.refuel();
  177.   return true;
  178. end
  179.  
  180.  
  181.  
  182.  
  183.  
  184. -- --------------------------------- --
  185. -- STARTING THE PROGRAM...           --
  186. -- --------------------------------- --
  187. term.clear();
  188. term.setCursorPos(1,1);
  189. turtle.select(1);
  190.  
  191. -- Check if they specified a different maximum travel distance
  192. if (#tArgs == 1) then
  193.   MAX_DIST = tonumber(tArgs[1]);
  194. end
  195.  
  196. MAX_FUEL = turtle.getFuelLimit();
  197.  
  198. -- Do an initial fuel check and end the program if it is below the required level
  199. if (MAX_FUEL == "unlimited") then
  200.   print("Turtles are not configured to require fuel on this server. Ending program to avoid wasting lava.");
  201.   return;
  202. elseif (turtle.getFuelLevel()<2) then
  203.   print("You must have a fuel level of at least 2 before running this program.");
  204.   return;
  205. end
  206.  
  207. -- Make sure the turtle is labeled
  208. if (os.getComputerLabel() == nil) then
  209.   print("This turtle has not yet been labeled. Setting turtle name to Murtle so the fuel won't be lost");
  210.   os.setComputerLabel("Murtle");
  211. end
  212.  
  213. -- Make sure there are 2 buckets in the first slot
  214. if (turtle.getItemCount(1) ~= 2 and turtle.getItemSpace(1) ~= 14) then
  215.   print("Please place 2 empty buckets in slot 1.");
  216.   while (turtle.getItemCount(1) ~= 2 and turtle.getItemSpace(1) ~= 14) do os.sleep(.1); end
  217. end
  218.  
  219. -- Make sure there is nothing in the second slot
  220. if (turtle.getItemCount(2) ~= 0) then
  221.   print("Please remove any items from slot 2.");
  222.   while (turtle.getItemCount(2) ~= 0) do os.sleep(.1); end
  223. end
  224.  
  225. -- Check to make sure we started directly above lava and collect one bucket of lava to use for comparison
  226. turtle.placeDown();
  227. if (turtle.getItemCount(1) ~= 1 and turtle.getItemCount(2) ~= 1) then
  228.   print("The turtle must be one block directly above lava before starting this program.");
  229.   return;
  230. end
  231.  
  232.  
  233. -- take an initial step down to where we collected the first bucket of lava from
  234. move(DOWN);
  235. push(returnStack,UP);
  236.  
  237. -- The main collection logic starts here
  238. -- TODO: Because the move() function preserves orientation, there may be an optimization that  
  239. --        prevents the need to check all the sides again after you backtrack.
  240. while (size(returnStack) > 0) do
  241.  
  242.   local lavaCollected = false;
  243.    
  244.   -- still room for more fuel and not too far away yet
  245.   if (((MAX_FUEL - 1000) > turtle.getFuelLevel()) and size(returnStack)<MAX_DIST) then
  246.  
  247.     -- check if there is lava below us and move down if there is
  248.     if ( collectFuel(DOWN) ) then
  249.       move(DOWN);
  250.       push(returnStack,UP);
  251.       lavaCollected = true;
  252.     else
  253.       -- check all around us for lava and move there if you find some
  254.       for i=1,4 do
  255.         if ( collectFuel(FORWARD) ) then
  256.           move(FORWARD);
  257.           push(returnStack, ((orientation+2) % 4));
  258.           lavaCollected = true;
  259.           break;
  260.         else
  261.           turn(RIGHT);
  262.         end
  263.       end
  264.      
  265.       -- if we haven't found any lava around us yet, then check above us
  266.       if (not(lavaCollected)) then
  267.         if ( collectFuel(UP) ) then
  268.           move(UP);
  269.           push(returnStack, DOWN);
  270.           lavaCollected = true;
  271.         end
  272.       end
  273.     end
  274.   end
  275.  
  276.   -- if there's no more lava around our current position, let's backtrack
  277.   if (not(lavaCollected)) then
  278.     move( pop(returnStack) );
  279.   end
  280. end
  281.  
  282. -- Use the one bucket of lava in the second slot
  283. turtle.select(2);
  284. turtle.refuel();
  285. turtle.transferTo(1);
  286.  
  287. print("The final fuel level is: " .. turtle.getFuelLevel());
Advertisement
Add Comment
Please, Sign In to add comment