Advertisement
gravitowl

easydig

Dec 2nd, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.12 KB | None | 0 0
  1. local args = {...};
  2.  
  3. local digDirection = {
  4.     front = turtle.dig,
  5.     up = turtle.digUp,
  6.     down = turtle.digDown
  7. }
  8.  
  9. local inspectDirection = {
  10.     front = turtle.inspect,
  11.     up = turtle.inspectUp,
  12.     down = turtle.inspectDown
  13. }
  14.  
  15. local Direction = {
  16.     front = "front",
  17.     up = "up",
  18.     down = "down"
  19. }
  20.  
  21. local StartPosition = {
  22.     top = "top",
  23.     bottom = "bottom"
  24. }
  25.  
  26. local fluids = {
  27.     "minecraft:water",
  28.     "minecraft:lava"
  29. }
  30.  
  31. function table:contains(value, recursive)
  32.     recursive = recursive or false
  33.     for key, table_value in pairs(self) do
  34.         if recursive and type(table_value) == "table" then
  35.             local contains = table.contains(table_value, value);
  36.             if contains then return true end
  37.         end
  38.  
  39.         if table_value == value then
  40.             return true;
  41.         end
  42.     end
  43.  
  44.     return false;
  45. end
  46.  
  47. local function printHelp()
  48.     print("USAGE: dig <width> <height> <depth> [startPosition (top | bottom)]");
  49.  
  50.     print("DESCRIPTION:");
  51.     print(" - <width> The width of the area to dig.");
  52.     print(" - <height> The height of the area to dig.");
  53.     print(" - <depth> The depth of the area to dig.");
  54.     print(" - [startPosition] Optional. Specificy wether this turtle is at the top or bottom of the defined area, defaults to top.");
  55. end
  56.  
  57. local function calculateFuelUsage(width, height, depth)
  58.     local totalFuelUsage = 0;
  59.    
  60.     local area = width * depth;
  61.     local volume = area * height;
  62.     totalFuelUsage = totalFuelUsage + volume;
  63.  
  64.     if area%2 ~= 0 then
  65.         totalFuelUsage = totalFuelUsage + height;
  66.         totalFuelUsage = totalFuelUsage + width;
  67.     end
  68.  
  69.     totalFuelUsage = totalFuelUsage + depth;
  70.  
  71.     return totalFuelUsage;
  72. end
  73.  
  74. local function tryRefuelling(requiredFuelAmount)
  75.     local oldSelectedSlot = turtle.getSelectedSlot();
  76.  
  77.     for i=1, 16 do
  78.         turtle.select(i);
  79.         turtle.refuel();
  80.         if turtle.getFuelLevel() >= requiredFuelAmount then
  81.             turtle.select(oldSelectedSlot);
  82.             return;
  83.         end
  84.     end
  85.  
  86.     turtle.select(oldSelectedSlot);
  87. end
  88.  
  89. local function forceDig(direction)
  90.     if not Direction[direction] then return false end
  91.  
  92.     local hasBlock, data = inspectDirection[direction]();
  93.  
  94.     while hasBlock do
  95.         digDirection[direction]();
  96.  
  97.         hasBlock, data = inspectDirection[direction]();
  98.  
  99.         if hasBlock and table.contains(fluids, data.name) then
  100.             return true;
  101.         end
  102.     end
  103.  
  104.     return true;
  105. end
  106.  
  107. local function dig(width, height, depth, startPosition)
  108.     local positionString = "bottom";
  109.     if startPosition == StartPosition.top then
  110.         positionString = "top";
  111.     end
  112.     print("Alright, digging an area of size " .. width .. ", " .. height .. ", " .. depth .. " starting at the " .. positionString .. ".");
  113.  
  114.     local totalColumnCounter = 0;
  115.  
  116.     if startPosition == StartPosition.top then
  117.         totalColumnCounter = totalColumnCounter + 1;
  118.     end
  119.  
  120.     for z=0, depth-1 do
  121.         forceDig(Direction.front)
  122.         turtle.forward();
  123.  
  124.         if z%2 == 0 then
  125.             turtle.turnRight();
  126.         else
  127.             turtle.turnLeft();
  128.         end
  129.  
  130.         for x=0, width-1 do            
  131.             for y=0, height-2 do
  132.                 if totalColumnCounter%2 == 0 then
  133.                     forceDig(Direction.up)
  134.                     turtle.up();
  135.                 else
  136.                     forceDig(Direction.down)
  137.                     turtle.down();
  138.                 end
  139.             end
  140.  
  141.             if x < width-1 then
  142.                 forceDig(Direction.front)
  143.                 turtle.forward();
  144.             end
  145.  
  146.             totalColumnCounter = totalColumnCounter + 1;
  147.         end
  148.  
  149.         if z%2 == 0 then
  150.             turtle.turnLeft();
  151.         else
  152.             turtle.turnRight();
  153.         end
  154.     end
  155.    
  156.     print("Finished digging!");
  157. end
  158.  
  159. local function returnHome(width, height, depth, startPosition)
  160.     print("Returning home.");
  161.     local area = width * depth;
  162.  
  163.     if area%2 ~= 0 then
  164.         for i=0, height-1 do
  165.             if startPosition == StartPosition.bottom then
  166.                 turtle.down();
  167.             else
  168.                 turtle.up();
  169.             end
  170.         end
  171.  
  172.         turtle.turnLeft();
  173.         for i = 0, width-1 do
  174.             turtle.forward();
  175.         end
  176.     else
  177.         turtle.turnLeft();
  178.     end
  179.  
  180.     turtle.turnLeft();
  181.     for i = 0, depth-1 do
  182.         turtle.forward();
  183.     end
  184.  
  185.     turtle.turnRight();
  186.     turtle.turnRight();
  187.  
  188.     print("Returned home!");
  189. end
  190.  
  191. local function main()
  192.     if args[1] == "help" then
  193.         printHelp();
  194.         return;
  195.     end
  196.  
  197.     if #args < 3 then
  198.         printHelp();
  199.         return;
  200.     end
  201.  
  202.     local width = tonumber(args[1]);
  203.     local height = tonumber(args[2]);
  204.     local depth = tonumber(args[3]);
  205.  
  206.     if not (width and height and depth) then
  207.         printHelp();
  208.         return;
  209.     end
  210.  
  211.     local startPosition = StartPosition.bottom;
  212.  
  213.     if #args >= 4 then
  214.         local startPositionArg = args[4];
  215.  
  216.         if startPositionArg == "top" then
  217.             startPosition = StartPosition.top;
  218.         elseif startPositionArg == "bottom" then
  219.             startPosition = StartPosition.bottom
  220.         else
  221.             printHelp();
  222.             return;
  223.         end
  224.     end
  225.  
  226.     local requiredFuelAmount = calculateFuelUsage(width, height, depth);
  227.     if requiredFuelAmount > turtle.getFuelLimit() then
  228.         print("More fuel required than limit allows, quitting program.");
  229.         return;
  230.     end
  231.  
  232.     while turtle.getFuelLevel() < requiredFuelAmount do
  233.         print("Not enough fuel, i need " .. requiredFuelAmount .. " but only have " .. turtle.getFuelLevel());
  234.         print("Try refuelling? (y/n)");
  235.         local doRefuel = read();
  236.  
  237.         if doRefuel == "n" then
  238.             print("Not refuelling, quitting program.");
  239.             return;
  240.         elseif doRefuel == "y" then
  241.             tryRefuelling(requiredFuelAmount);
  242.         end
  243.     end
  244.  
  245.     dig(width, height, depth, startPosition);
  246.     returnHome(width, height, depth, startPosition);
  247. end
  248.  
  249. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement