Advertisement
eonsv

dig

Dec 25th, 2012
4,503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.56 KB | None | 0 0
  1. ----------------------------------------------------------
  2. --                      Name: dig                       --
  3. ----------------------------------------------------------
  4. --                   Version: 0.2.17                    --
  5. ----------------------------------------------------------
  6. --                    Author: Eonsv                     --
  7. ----------------------------------------------------------
  8. --The program clears an area of size x, y of all ores by--
  9. --making the turtle to dig in the continous pattern     --
  10. --. X . . . . X . . .                                   --
  11. --. . . . X . . . . X                                   --
  12. --. . X . . . . X . .                                   --
  13. --X . . . . X . . . .                                   --
  14. --. . . X . . . . X .                                   --
  15. --. X . . . . X . . .                                   --
  16. --. . . . X . . . . X                                   --
  17. --. . X . . . . X . .                                   --
  18. --X . . . . X . . . .                                   --
  19. --. . . X . . . . X .                                   --
  20. --until hitting bedrock, and checking walls for ores.   --
  21. --It will dispose items to an enderchest when reaching  --
  22. --full inventory. Theres options for automatic fueling, --
  23. --filling in the holes, and configurable number of      --
  24. --blocks to avoid.                                      --
  25. ----------------------------------------------------------
  26.  
  27. ------------
  28. --INCLUDES--
  29. ------------
  30.  
  31. --Tests wether a file with the filename name exists. Returns true if it does and false if not
  32. function fileExists(name)
  33.    local f = io.open(name,'r');
  34.    if (f ~= nil) then
  35.         f:close();
  36.         return true;
  37.     else
  38.         return false;
  39.     end;
  40. end;
  41.  
  42. --Downloads and installs the ProtLib API.
  43. if(not fileExists('ProtLib')) then
  44.     shell.run('pastebin', 'get', 's8GSFZrU', 'ProtLib');
  45. end;
  46. os.loadAPI('ProtLib');
  47.  
  48. --Downloads the startup program
  49. if(not fileExists('startup')) then
  50.     shell.run('pastebin', 'get', 'CDCELxfj', 'startup');
  51. end;
  52.  
  53. -------------
  54. --VARIABLES--
  55. -------------
  56. --Commandline arguments
  57. local arg = {...};
  58.  
  59. if(arg[1] == 'inventory') then
  60.     print('Slot 1: Enderchest for automatic item disposal.');
  61.     print('Slot 2: Fuel for automatic refueling.');
  62.     print('Slot 3: Materials to be used for filling in holes.');
  63.     print('Slot 4-15: Blocks to ignore.');
  64.     print('Slot 16: Empty.');
  65.     do return end;
  66. end;
  67.  
  68. --If wrong number of arguments
  69. for i = 1, 3 do
  70.     if(arg[i] == nil) then
  71.         print('Usage: dig <length> <width> <[left]/[right]> <y coordinate(optional)>');
  72.         print('The y coordinate is only used when prefueling is used.');
  73.         print('For how to setup inventory, use: dig <inventory>');
  74.         do return end;
  75.     end;
  76. end;
  77.  
  78. --current x, y, z position, x and y size of the quarry, going right or left.
  79. local x, y, z, X, Y, lr;
  80. --Pattern matrix, size of pattern, current state, rebooting.
  81. local A0, N, state, reboot;
  82. --Automatic disposal, number of blocks to avoid, automatic fueling, slot number of first and last block to avoid.
  83. local automatic, avoid, autofuel, favoid, lavoid;
  84. --The z coordinate of top of the hole, wether to plug the hole or not
  85. local top, plug;
  86. --Slot number of the materials to plug the hole with, slot number of the fuel
  87. local plugSlot, fuelSlot;
  88.  
  89. --Initialzing the dig pattern. Makes an array of size 5x5 with indices 0 to 4.
  90. N = 5;
  91. A0 = {  {0, 1, 0, 0, 0},
  92.         {0, 0, 0, 0, 1},
  93.         {0, 0, 1, 0, 0},
  94.         {1, 0, 0, 0, 0},
  95.         {0, 0, 0, 1, 0}};
  96.  
  97. A0[0] = {};
  98. for i = 0, 4 do
  99.     for j = 0, 4 do
  100.         A0[i][j] = A0[i + 1][j + 1];
  101.     end;
  102. end;
  103. A0[5] = nil;
  104. for i = 0, 4 do
  105.     A0[i][5] = nil;
  106. end
  107.  
  108. --X size
  109. X = tonumber(arg[1]);
  110.  
  111. --Y size
  112. Y = tonumber(arg[2]);
  113.  
  114. --Starting to move to the left or right depending on the third input
  115. if(arg[3] == 'left' or arg[3] == 'l' or arg[3] == 'L') then
  116.     lr = 1;
  117. elseif(arg[3] == 'right' or arg[3] == 'r' or arg[3] == 'R') then
  118.     lr = -1;
  119. else
  120.     lr = tonumber(arg[3]);
  121.     if(lr > 0) then
  122.         lr = 1;
  123.     else
  124.         lr = -1;
  125.     end;
  126. end;
  127.  
  128. --Test to check if wether the program has rebooted
  129. if(arg[5] == nil) then
  130.     x = 0;
  131.     y = 0;
  132.     z = 0;
  133.     state = 0;
  134.     ProtLib.setPos();
  135.     reboot = false;
  136.     top = 1;
  137. else
  138.     x = tonumber(arg[4]);
  139.     y = tonumber(arg[5]);
  140.     z = tonumber(arg[6]);
  141.     state = tonumber(arg[7]);
  142.     ProtLib.setPos(x, y, z, tonumber(arg[8]));
  143.     reboot = true;
  144.     automatic = (arg[9] == '1');
  145.     avoid = tonumber(arg[10]);
  146.     autofuel = (arg[11] == '1');
  147.     top = tonumber(arg[12]);
  148.     plug = (arg[13] == '1');
  149.     if(avoid == 0) then
  150.         favoid = 0;
  151.         lavoid = 4;
  152.         if(not automatic) then
  153.             lavoid = lavoid - 1;
  154.         end;
  155.         if(not autofuel) then
  156.             lavoid = lavoid - 1;
  157.         end;
  158.         if(not plug) then
  159.             lavoid = lavoid - 1;
  160.         end;
  161.     else
  162.         favoid = 4;
  163.         if(not automatic) then
  164.             favoid = favoid - 1;
  165.         end;
  166.         if(not autofuel) then
  167.             favoid = favoid - 1;
  168.         end;
  169.         if(not plug) then
  170.             favoid = favoid - 1;
  171.         end;
  172.         lavoid = favoid + avoid - 1;
  173.     end;
  174.     fuelSlot = 2;
  175.     if(not automatic) then
  176.         fuelSlot = fuelSlot - 1;
  177.     end;
  178.     plugSlot = 3;
  179.     if(not automatic) then
  180.         plugSlot = plugSlot - 1;
  181.     end;
  182.     if(not autofuel) then
  183.         plugSlot = plugSlot - 1;
  184.     end;
  185. end;
  186.  
  187. -------------
  188. --FUNCTIONS--
  189. -------------
  190. --Finds fuel and places it in the fuel slot
  191. local function findFuel()
  192.     if(not autofuel) then
  193.         return true;
  194.     end;
  195.     --Finds fuel in inventory and puts it in the fuel slot
  196.     turtle.select(fuelSlot);
  197.     if(turtle.getItemSpace(fuelSlot) > 0) then
  198.         for i = lavoid + 1, 16 do
  199.             --Stops if fuel slot is full.
  200.             if(turtle.getItemSpace(fuelSlot) == 0) then
  201.                 break;
  202.             end;
  203.             if(turtle.compareTo(i)) then
  204.                 turtle.select(i);
  205.                 turtle.transferTo(fuelSlot);
  206.                 turtle.select(fuelSlot);
  207.             end;
  208.         end;
  209.     end;
  210.     turtle.select(lavoid + 1);
  211. end;
  212.  
  213. --Saves current progress to file
  214. function save(dr)
  215.     if(dr == nil) then
  216.         dr = 0;
  217.     end;
  218.     local str = '';
  219.     str = str..X..'\n';
  220.     str = str..Y..'\n';
  221.     str = str..lr..'\n';
  222.     str = str..x..'\n';
  223.     str = str..y..'\n';
  224.     str = str..z..'\n';
  225.     str = str..state..'\n';
  226.     str = str..ProtLib.getRot() + dr..'\n';
  227.     if(automatic) then
  228.         str = str..'1'..'\n';
  229.     else
  230.         str = str..'0'..'\n';
  231.     end;
  232.     str = str..avoid..'\n';
  233.     if(autofuel) then
  234.         str = str..'1'..'\n';
  235.     else
  236.         str = str..'0'..'\n';
  237.     end;
  238.     str = str..top..'\n';
  239.     if(plug) then
  240.         str = str..'1'..'\n';
  241.     else
  242.         str = str..'0'..'\n';
  243.     end;
  244.     local file = io.open('save', 'w');
  245.     file:write(str);
  246.     file:close();
  247. end;
  248.  
  249. --Clears the inventory by placing the enderchest from slot 1 above the turtle,
  250. --and disposing all mined items to this one.
  251. local function dump()
  252.     findFuel();
  253.     --Drops all unwanted blocks
  254.     if(favoid > 0) then
  255.         for i = favoid, lavoid do
  256.             turtle.select(i);
  257.             for j = lavoid + 1, 16 do
  258.                 if(turtle.compareTo(j)) then
  259.                     turtle.select(j);
  260.                     turtle.dropDown();
  261.                     turtle.select(i);
  262.                 end;
  263.             end;
  264.         end;
  265.         turtle.select(lavoid + 1);
  266.     end;
  267.     --Not automated
  268.     if(not automatic) then
  269.         local filled = 0;
  270.         for i = lavoid + 1, 16 do
  271.             if(turtle.getItemCount(i) > 0) then
  272.                 filled = filled + 1;
  273.             end;
  274.         end;
  275.         while(filled > 0) do
  276.             sleep('Empty slot '..favoid..' to '..lavoid, 10);
  277.             filled = 0;
  278.             for i = lavoid + 1, 16 do
  279.                 if(turtle.getItemCount(i) > 0) then
  280.                     filled = filled + 1;
  281.                 end;
  282.             end;
  283.         end;
  284.     else
  285.         --Clears space above the turtle for placing a block.
  286.         if(turtle.detectUp()) then
  287.             ProtLib.digUp();
  288.         end;
  289.         --Places the enderchest
  290.         turtle.select(1);
  291.         while(not turtle.placeUp()) do
  292.             turtle.attackUp();
  293.         end;
  294.         --Dumps items to the chest
  295.         for i = lavoid + 1, 16 do
  296.             if(turtle.getItemCount(i) > 0) then
  297.                 turtle.select(i);
  298.                 --Displays an error message if the chest is full
  299.                 while(not turtle.dropUp()) do
  300.                     sleep('Unable to dispose items to the chest.', 10);
  301.                 end;
  302.             end;
  303.         end;
  304.         --Picks up the enderchest
  305.         turtle.select(1);
  306.         ProtLib.digUp();
  307.         --Selects first empty slot
  308.         turtle.select(lavoid + 1);
  309.     end;
  310. end;
  311.  
  312. --Tests if the inventory is full, and dumps items if it is.
  313. local function dispose()
  314.     local empty = 0;
  315.     --Calculates number of empty slots
  316.     for i = lavoid + 1, 16 do
  317.         if(turtle.getItemCount(i) == 0) then
  318.             empty = empty + 1;
  319.         end;
  320.     end;
  321.     --Dumps items if the inventory is full
  322.     if(empty == 0) then
  323.         dump();
  324.         clear();
  325.         print('Disposal of inventory complete.');
  326.     end;
  327. end;
  328.  
  329. --Refuels until the fuel level is at or above fuel.
  330. local function refuel(fuel)
  331.     if(turtle.getFuelLevel() >= fuel) then
  332.         return true;
  333.     end;
  334.     if(not autofuel) then
  335.         while(turtle.getFuelLevel() < fuel) do
  336.             sleep('Error: The y coordinate input was incorrect.\nInsert a stack of fuel in slot 16.', 30);
  337.             turtle.select(16);
  338.             turtle.refuel();
  339.             turtle.select(lavoid + 1);
  340.         end;
  341.         return true;
  342.     end;
  343.     --Refuling until the fuel level has reached the input, and leaving 1 fuel item in the fuel slot.
  344.     turtle.select(fuelSlot);
  345.     if(turtle.getItemCount(fuelSlot) > 1) then
  346.         turtle.refuel(1);
  347.     end;
  348.     while(turtle.getFuelLevel() < fuel) do
  349.         findFuel();
  350.         if(turtle.getItemCount(fuelSlot) > 1) then
  351.             turtle.refuel(1);
  352.         end;
  353.         while(turtle.getFuelLevel() < fuel and turtle.getItemCount(fuelSlot) < 2) do
  354.             sleep('Insert more fuel.', 10);
  355.         end;
  356.     end;
  357.     findFuel();
  358.     turtle.select(lavoid + 1);
  359. end;
  360.  
  361. --Moves to the position x + dx, y + dy, z + dz, where x, y, z is the current position.
  362. --It will toss away unwanted blocks.
  363. local function move(dx, dy, dz)
  364.     if(automatic) then
  365.         while(turtle.getItemCount(1) == 0) do
  366.             sleep('Something went terribly wrong, and I lost the chest. Place it in slot 1.', 10);
  367.         end;
  368.     end;
  369.     if(turtle.getItemCount(16) > 0) then
  370.         dispose();
  371.     end;
  372.     x = x + dx*dx;
  373.     y = y + dy*dy;
  374.     z = z + dz;
  375.     save();
  376.     turtle.select(16);
  377.     test = ProtLib.moveRel(dx, dy, dz);
  378.     --Filling up filler blocks
  379.     if(plug and turtle.compareTo(plugSlot) and turtle.getItemSpace(plugSlot) > 0) then
  380.         turtle.transferTo(plugSlot);
  381.     end;
  382.     --Disposing of unwanted blocks
  383.     if(turtle.getItemCount(16) > 0 and favoid > 0) then
  384.         for i = favoid, lavoid do
  385.             if(turtle.compareTo(i)) then
  386.                 turtle.dropDown();
  387.                 break;
  388.             end;
  389.         end;
  390.     end;
  391.     --Cleaning up inventory
  392.     if(turtle.getItemCount(16) > 0) then
  393.         for i = lavoid + 1, 15 do
  394.             if(turtle.compareTo(i)) then
  395.                 turtle.transferTo(i);
  396.                 if(turtle.getItemCount(16) == 0) then
  397.                     break;
  398.                 end;
  399.             elseif(turtle.getItemCount(i) == 0) then
  400.                 turtle.transferTo(i);
  401.                 break;
  402.             end;
  403.         end;
  404.     end;
  405.     turtle.select(lavoid + 1);
  406.     --The turtle will never be ordered to move into bedrock,
  407.     --therefore it's forced to move until it's able to.
  408.     if(not test) then
  409.         move(dx, dy, dz);
  410.     end;
  411. end;
  412.  
  413. --Digs the block below. Returns true if successful, and false if the block is bedrock.
  414. --It will toss away unwanted blocks
  415. local function digDownTest()
  416.     local test;
  417.     if(turtle.getItemCount(16) > 0) then
  418.         dispose();
  419.     end;
  420.     turtle.select(16);
  421.     test = ProtLib.digDown();
  422.     --Filling up filler blocks
  423.     if(plug and turtle.compareTo(plugSlot) and turtle.getItemSpace(plugSlot) > 0) then
  424.         turtle.transferTo(plugSlot);
  425.     end;
  426.     --Disposing of unwanted blocks
  427.     if(turtle.getItemCount(16) > 0 and favoid > 0) then
  428.         for i = favoid, lavoid do
  429.             if(turtle.compareTo(i)) then
  430.                 turtle.dropDown();
  431.                 break;
  432.             end;
  433.         end;
  434.     end;
  435.     --Cleaning up inventory
  436.     if(turtle.getItemCount(16) > 0) then
  437.         for i = lavoid + 1, 15 do
  438.             if(turtle.compareTo(i)) then
  439.                 turtle.transferTo(i);
  440.                 if(turtle.getItemCount(16) == 0) then
  441.                     break;
  442.                 end;
  443.             elseif(turtle.getItemCount(i) == 0) then
  444.                 turtle.transferTo(i);
  445.                 break;
  446.             end;
  447.         end;
  448.     end;
  449.     turtle.select(lavoid + 1);
  450.     dispose();
  451.     return test;
  452. end;
  453.  
  454. --Rotates relative to the current rotation.
  455. rotate = ProtLib.rotate;
  456.  
  457. --Clears the screen, and sets the cursor at the top left of the screen.
  458. function clear()
  459.     term.clear();
  460.     term.setCursorPos(1, 1);
  461. end;
  462.  
  463. --Sleeps time seconds, while displaying the message text and counting down.
  464. function sleep(text, time)
  465.     for i = 0, time - 1 do
  466.         clear();
  467.         print(text);
  468.         print('Countdown: '..time - i..'s.');
  469.         os.sleep(1);
  470.     end;
  471.     clear();
  472. end;
  473.  
  474. --Cleans up the inventory by placeing all empty slots at the bottom of the inventory.
  475. function cleanUp()
  476.     if(not automatic) then
  477.         if(not autofuel) then
  478.             turtle.select(3);
  479.             turtle.transferTo(1);
  480.         else
  481.             turtle.select(2);
  482.             turtle.transferTo(1);
  483.             turtle.select(3);
  484.             turtle.transferTo(2);
  485.         end;
  486.     elseif(not autofuel) then
  487.         turtle.select(3);
  488.         turtle.transferTo(2);
  489.     end;
  490.     if(favoid > 0) then
  491.         for i = favoid, 15 do
  492.             if(turtle.getItemCount(i) == 0) then
  493.                 for j = 16, i + 1, -1 do
  494.                     if(turtle.getItemCount(j) > 0) then
  495.                         turtle.select(j);
  496.                         turtle.transferTo(i);
  497.                         break;
  498.                     end;
  499.                 end;
  500.             end;
  501.         end;
  502.     end;
  503.    
  504.     turtle.select(lavoid + 1);
  505. end;
  506.  
  507. --Initializes the turtle by determining automatic fueling, disposing of items,
  508. --number of blocks to avoid and what type of blocks to fill in holes with.
  509. function init()
  510.     while(turtle.getItemCount(1) > 1) do
  511.         sleep('Wrong number of items in slot 1. Needs to be 1 or 0.', 5);
  512.     end;
  513.    
  514.     automatic = (turtle.getItemCount(1) == 1);
  515.     autofuel = (turtle.getItemCount(2) > 0 and arg[4] == nil)
  516.     plug = (turtle.getItemCount(3) > 0);
  517.    
  518.     if(not autofuel and turtle.getItemCount(2) > 0) then
  519.         turtle.select(2);
  520.         turtle.refuel();
  521.         turtle.drop();
  522.         turtle.select(1);
  523.     end;
  524.    
  525.     while(autofuel and turtle.getItemSpace(2) > 0) do
  526.         sleep('Insert a whole stack of fuel in slot 2.', 5);
  527.     end;
  528.     while(plug and turtle.getItemSpace(3) > 0) do
  529.         sleep('Insert a whole stack of blocks in slot 3.', 5);
  530.     end;
  531.    
  532.     for i = 4, 15 do
  533.         while(turtle.getItemCount(i) > 1) do
  534.             sleep('Wrong number of items in slot '..i..'.Needs to be 1 or 0.', 5);
  535.         end;
  536.     end;
  537.    
  538.     while(turtle.getItemCount(16) > 0) do
  539.         sleep('Slot 16 needs to be empty.', 5);
  540.     end;
  541.    
  542.     avoid = 0;
  543.     for i = 4, 15 do
  544.         if(turtle.getItemCount(i) > 0) then
  545.             avoid = avoid + 1;
  546.         end;
  547.     end;
  548.    
  549.     --If there is no blocks to avoid
  550.     if(avoid == 0) then
  551.         favoid = 0;
  552.         lavoid = 4;
  553.         if(not automatic) then
  554.             lavoid = lavoid - 1;
  555.         end;
  556.         if(not autofuel) then
  557.             lavoid = lavoid - 1;
  558.         end;
  559.         if(not plug) then
  560.             lavoid = lavoid - 1;
  561.         end;
  562.     else
  563.         --Places the first block to avoid in the first empty slot.
  564.         favoid = 4;
  565.         if(not autofuel) then
  566.             favoid = favoid - 1;
  567.         end;
  568.         if(not automatic) then
  569.             favoid = favoid - 1;
  570.         end;
  571.         if(not plug) then
  572.             favoid = favoid - 1;
  573.         end;
  574.         lavoid = favoid + avoid - 1;
  575.     end;
  576.     fuelSlot = 2;
  577.     if(not automatic) then
  578.         fuelSlot = fuelSlot - 1;
  579.     end;
  580.     plugSlot = 3;
  581.     if(not automatic) then
  582.         plugSlot = plugSlot - 1;
  583.     end;
  584.     if(not autofuel) then
  585.         plugSlot = plugSlot - 1;
  586.     end;
  587.    
  588.     cleanUp();
  589.    
  590.     if(not autofuel) then
  591.         local fuel = X*Y - 1;
  592.         local ycoord;
  593.         clear();
  594.         ycoord = tonumber(arg[4]);
  595.         if(ycoord == nil) then
  596.             print('Enter y coordinate:');
  597.             ycoord = read();
  598.         end;
  599.         local holes = 0;
  600.         for i = 0, X - 1 do
  601.             for j = 0, Y - 1 do
  602.                 holes = holes + A0[i%N][j%N];
  603.             end;
  604.         end;
  605.        
  606.         fuel = fuel + 2*holes*(ycoord - 2);
  607.         while(turtle.getFuelLevel() < fuel) do
  608.             sleep('Refueling:', 10);
  609.             for i = lavoid + 1, 16 do
  610.                 turtle.select(i);
  611.                 turtle.refuel();
  612.             end;
  613.         end;
  614.         turtle.select(lavoid + 1);
  615.     end;
  616.     --Info output
  617.     local str1, str2, str3;
  618.     if(automatic) then
  619.         str1 = 'automatic disposal of inventory';
  620.     else
  621.         str1 = 'manual disposal of inventory';
  622.     end;
  623.     if(autofuel) then
  624.         str2 = 'automatic refueling';
  625.     else
  626.         str2 = 'fueling at start';
  627.     end;
  628.     if(plug) then
  629.         str3 = 'fill holes';
  630.     else
  631.         str3 = 'leave holes empty';
  632.     end;
  633.     sleep('Using '..str1..' and '..str2..'. Avoiding '..avoid..' number of blocks. Will '..str3..'.', 5);
  634. end;
  635.  
  636. --Moves up until reaching initial height
  637. function moveUp()
  638.     local function fillInHole()
  639.         if(z == top - 1) then
  640.             turtle.select(plugSlot);
  641.             while(turtle.getItemCount(plugSlot) < 2) do
  642.                 sleep('Insert more fill blocks.', 10);
  643.             end;
  644.             --Places a block
  645.             if(not turtle.detect()) then
  646.                 while(not turtle.place()) do
  647.                     turtle.attack();
  648.                 end;
  649.             end;
  650.             for i = 1, 3 do
  651.                 save(1);
  652.                 rotate(1);
  653.                 if(turtle.detect()) then
  654.                     break;
  655.                 end;
  656.                 while(turtle.getItemCount(plugSlot) < 2) do
  657.                     sleep('Insert more fill blocks.', 10);
  658.                 end;
  659.                 --Places a block
  660.                 while(not turtle.place()) do
  661.                     turtle.attack();
  662.                 end;
  663.             end;
  664.             turtle.select(lavoid + 1);
  665.         end;
  666.         if(top == z and not turtle.detectDown()) then
  667.             turtle.select(plugSlot);
  668.             while(turtle.getItemCount(plugSlot) < 2) do
  669.                 sleep('Insert more fill blocks.', 10);
  670.             end;
  671.             while(not turtle.placeDown()) do
  672.                 turtle.attackUp();
  673.                 turtle.attackDown();
  674.             end;
  675.             turtle.select(lavoid + 1);
  676.         end;
  677.     end;
  678.    
  679.     if(plug) then
  680.         fillInHole();
  681.     end;
  682.     while(z < 0) do
  683.         refuel(1);
  684.         move(0, 0, 1);
  685.         if(plug) then
  686.             fillInHole();
  687.         end;
  688.     end;
  689.     turtle.select(lavoid + 1);
  690. end;
  691.  
  692. --Digs the block in front until it's clear. It will not dig blocks to avoid.
  693. function dig()
  694.     test = true;
  695.     if(favoid > 0) then
  696.         for i = favoid, lavoid do
  697.             turtle.select(i);
  698.             if(turtle.compare()) then
  699.                 test = false;
  700.                 break;
  701.             end;
  702.         end;
  703.     end;
  704.     turtle.select(lavoid + 1);
  705.     if(test) then
  706.         ProtLib.dig();
  707.         dispose();
  708.     end;
  709. end;
  710.  
  711. --Digging down until bedrock, clearing the walls for ores, and then moves back up.
  712. function digDown()
  713.     top = 1;
  714.     local function digAround()
  715.         for i = 0, 2 do
  716.             dig();
  717.             save(1);
  718.             rotate(1);
  719.         end;
  720.         dig();
  721.     end;
  722.    
  723.     state = 1;
  724.     if(z ~= 0) then
  725.         digAround();
  726.     end;
  727.    
  728.     while(true) do
  729.         if(top > 0 and turtle.detectDown()) then
  730.             top = z;
  731.         end;
  732.         if(not digDownTest()) then
  733.             break;
  734.         end;
  735.         refuel(1);
  736.         move(0, 0, -1);
  737.         digAround();
  738.     end;
  739.     state = 0;
  740.     save();
  741.     moveUp();
  742. end;
  743.  
  744. --Moves to the next point in the grid
  745. function moveNext()
  746.     refuel(1);
  747.     if(y < Y - 1) then
  748.         move(0, lr, 0)
  749.     elseif(x < X - 1) then
  750.         y = 0;
  751.         lr = -lr;
  752.         move(1, 0, 0)
  753.     end;
  754.     local p = (x*Y + y)/(X*Y);
  755.     p = math.floor(p*1000 + 0.5)/10;
  756.     clear();
  757.     print(p..'% complete.');
  758. end;
  759.  
  760. function moveTest()
  761.     if(y < Y - 1 or x < X - 1) then
  762.         return true;
  763.     else
  764.         return false;
  765.     end;
  766. end;
  767.  
  768. --------
  769. --MAIN--
  770. --------
  771. if(not reboot) then
  772.     init();
  773. else
  774.     --If disposing items while rebooting
  775.     if(turtle.getItemCount(1) == 0 and automatic) then
  776.         turtle.select(1);
  777.         ProtLib.digUp();
  778.         turtle.select(lavoid + 1);
  779.         dispose();
  780.         while(turtle.getItemCount(1) == 0) do
  781.             sleep('Something went terribly wrong, and i lost the chest. Place it in slot 1.', 10);
  782.         end;
  783.     end;
  784.     --Resuming the current action.
  785.     --State 1: diging down, state 0: moving up
  786.     if(state == 1) then
  787.         digDown();
  788.     else
  789.         moveUp();
  790.     end;
  791. end;
  792.  
  793. local posx, posy;
  794. while(moveTest()) do
  795.     --Calculating current location in the grid
  796.     if(lr == -1) then
  797.         posx = x;
  798.         posy = y;
  799.     else
  800.         posx = x;
  801.         posy = (Y - 1) - y;
  802.     end;
  803.     --If hitting a hole, and if it's not getting out of the hole after a reboot,
  804.     --then it will dig the hole.
  805.     if(A0[posx%N][posy%N] == 1 and not reboot) then
  806.         digDown();
  807.     end;
  808.     reboot = false;
  809.     moveNext();
  810. end;
  811.  
  812. if(lr == -1) then
  813.     posx = X - 1;
  814.     posy = Y - 1;
  815. else
  816.     posx = X - 1;
  817.     posy = 0;
  818. end;
  819. if(A0[posx%N][posy%N] == 1) then
  820.     digDown();
  821. end;
  822. ProtLib.rotateTo(0);
  823. --Dumping all items to the enderchest
  824. if(automatic) then
  825.     if(turtle.detectUp()) then
  826.         ProtLib.digUp();
  827.     end;
  828.     turtle.select(1);
  829.     turtle.placeUp();
  830.     for i = lavoid + 1, 16 do
  831.         if(turtle.getItemCount(i) > 0) then
  832.             turtle.select(i);
  833.             turtle.dropUp();
  834.         end;
  835.     end;
  836.     turtle.select(1);
  837.     ProtLib.digUp();
  838.     turtle.select(lavoid + 1);
  839. end;
  840. --Deletes the startup program
  841. shell.run('delete', 'startup');
  842. clear();
  843. print('Digging accomplished.');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement