Advertisement
SolidSnake96AS

Branch Mining

Sep 13th, 2014
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.03 KB | None | 0 0
  1. local tunnelLength, branchLength, fuelNeeded, numBranch, enderchest, net, home, enderSlot, bucketSlot, receiverId, percentage, phase, phaseColor, fuelFound, homeFuel, startFuel;
  2. local arg={ ... };
  3.  
  4. enderchest=false;
  5. net=false;
  6. home=false;
  7. enderSlot=14;
  8. torchSlot=15
  9. bucketSlot=16;
  10. receiverId=-1;
  11. percentage=0;
  12. fuelFound=0;
  13. homeFuel=0;
  14. startFuel=turtle.getFuelLevel();
  15. phase="";
  16. phaseColor=colors.white;
  17.  
  18. local function setup() --Setup for the program
  19.     local event,char
  20.    
  21.     print("How long the tunnel should be?");
  22.     tunnelLength=tonumber(io.read());
  23.     print("How long a branch should be?");
  24.     branchLength=tonumber(io.read());
  25.    
  26.     if (enderchest)
  27.     then
  28.         if (turtle.getItemCount(enderSlot)==0)
  29.         then
  30.             print("\nPut an EnderChest (from EnderStorage) in slot "..tostring(enderSlot).."!\nPress any key to continue.");
  31.             os.pullEvent("key");
  32.         end
  33.     else
  34.         print("\nPut a large chest to the left of the turtle!\nPress any key to continue.");
  35.         os.pullEvent("key");
  36.     end
  37.    
  38.     if (turtle.getItemCount(bucketSlot)==0)
  39.     then
  40.         print("\nPut an empty bucket in slot "..tostring(bucketSlot).."!\nPress any key to continue.");
  41.         os.pullEvent("key");
  42.     end
  43.    
  44.     if (turtle.getItemCount(torchSlot)==0)
  45.     then
  46.         print("\nPut some torches in slot "..tostring(torchSlot).."!\nPress any key to continue.");
  47.         os.pullEvent("key");
  48.     end
  49.    
  50.     sleep(0.2);
  51.    
  52.     print("\nMain tunnel length: "..tunnelLength.."\nIs this correct? Y/N");
  53.     event,char=os.pullEvent("char");
  54.    
  55.     if(char=="n")
  56.     then
  57.         print("How long the tunnel should be?");
  58.         tunnelLength=tonumber(io.read());
  59.     end
  60.    
  61.     sleep(0.2);
  62.    
  63.     print("\nTunnel branches length: "..branchLength.."\nIs this correct? Y/N");
  64.     event, char=os.pullEvent("char");
  65.    
  66.     if(char=="n")
  67.     then
  68.         print("How long a branch should be?");
  69.         branchLength=tonumber(io.read());
  70.     end
  71. end
  72.  
  73. local function help() --Print the help guide
  74.     print("To use this program the turtle must have a pickaxe on the left and a wireless modem to the right.")
  75.     print("\nArguments:");
  76.     print("\nhelp:\nDisplay this help guide.");
  77.     print("\nstart:\nStart the program.");
  78.     os.pullEvent("key");
  79.     print("\nAdditional arguments:");
  80.     print("\nenderchest:\nWhen the inventory is full, the Turtle places an Ender Chest to empty its inventory, otherwise it returns to home to empty its inventory. (Place a chest to the left of the turtle before running the program)");
  81.     os.pullEvent("key");
  82.     print("\nrednet id:\nSends data over the rednet. Replace \"id\" with the id of the receiver computer. Get the receiver command: pastebin get R89UWx10 BMReceiver");
  83.     os.pullEvent("key");
  84.     print("\nhome:\nMakes the turtle go at the start position after a server restart.");
  85.     os.pullEvent("key");
  86.     print("\n\nExample:\nBranchMine start enderchest rednet 10");
  87.     print("\nThe Turtle will place down the EnderChest (from EnderStorage) to empty its inventory, and the data will be sent to the pc with id 10.")
  88. end
  89.  
  90. local function sendData() --Send the data to the receiver
  91.     if (not net)
  92.     then
  93.         return
  94.     end
  95.    
  96.     percentage=(((startFuel-turtle.getFuelLevel())-homeFuel)*100)/fuelNeeded;
  97.     msg=""..percentage..","..phase..","..phaseColor..","..turtle.getFuelLevel()..","..fuelFound;
  98.     rednet.send(receiverId, msg);
  99. end
  100.  
  101. local function checkFull() --Check if inventory is full
  102.    
  103.     if (not enderchest)
  104.     then
  105.             numb=14;
  106.     else
  107.             numb=13;
  108.     end
  109.    
  110.     for i=1,numb
  111.     do
  112.         if (turtle.getItemCount(i)==0)
  113.         then
  114.             return false;
  115.         end
  116.     end
  117.    
  118.     return true;
  119. end
  120.  
  121. local function updateFile() --Update the file
  122.     local file, str, _net, _enderchest;
  123.    
  124.     if (not home)
  125.     then
  126.         return;
  127.     end
  128.    
  129.     _net=0;
  130.     _enderchest=0;
  131.    
  132.     if (net)
  133.     then
  134.         _net=1;
  135.     end
  136.    
  137.     if (enderchest)
  138.     then
  139.         _enderchest=1;
  140.     end
  141.    
  142.     file=fs.open("BMStatus", "w");
  143.    
  144.     str=advMovAPI.getX()..","..advMovAPI.getY()..","..advMovAPI.getDir()..",".._enderchest..",".._net..","..receiverId..","..fuelFound..","..percentage;
  145.    
  146.     file.write(str);
  147.    
  148.     file.close();
  149. end
  150.  
  151. local function turnL()
  152.     advMovAPI.turnL();
  153.     updateFile();
  154. end
  155.  
  156. local function turnR()
  157.     advMovAPI.turnR();
  158.     updateFile();
  159. end
  160.  
  161.  
  162. local function returnBack(x, y, dir) --Go to coords
  163.    
  164.     while(advMovAPI.getY()~=y)
  165.     do
  166.         advMovAPI.forward(true);
  167.         homeFuel=homeFuel+1;
  168.     end
  169.    
  170.     if (advMovAPI.getX()<x)
  171.     then
  172.         turnR();
  173.     elseif(advMovAPI.getX()>x)
  174.     then
  175.         turnL();
  176.     end
  177.    
  178.     while(advMovAPI.getX()~=x)
  179.     do
  180.         advMovAPI.forward(true);
  181.         homeFuel=homeFuel+1;
  182.     end
  183.    
  184.     while(advMovAPI.getDir()>dir)
  185.     do
  186.         turnR();
  187.     end
  188.    
  189.     while(advMovAPI.getDir()<dir)
  190.     do
  191.         turnL();
  192.     end
  193. end
  194.  
  195. local function dropAll(ender) --Drop all item in the inventory
  196.     local slot, numb, prevSlot, prevPhase;
  197.    
  198.     prevSlot=turtle.getSelectedSlot();
  199.    
  200.     if (not ender)
  201.     then
  202.         numb=14;
  203.     else
  204.         numb=13;
  205.     end
  206.    
  207.     for slot=1,numb
  208.     do
  209.         turtle.select(slot);
  210.         phaseColor=colors.yellow;
  211.         phase="Droppping Item";
  212.         sendData();
  213.        
  214.         if (ender)
  215.         then
  216.             prevPhase=phase;
  217.             while (not turtle.drop())
  218.             do
  219.                 if (turtle.getItemCount(slot)==0)
  220.                 then
  221.                     break;
  222.                 end
  223.                
  224.                 sleep(2);
  225.                 phaseColor=colors.red;
  226.                 phase="EnderChest full!";
  227.                 sendData();
  228.                 phase=prevPhase;
  229.             end
  230.         else
  231.             prevPhase=phase;
  232.             while (not turtle.drop())
  233.             do
  234.                 if (turtle.getItemCount(slot)==0)
  235.                 then
  236.                     break;
  237.                 end
  238.                
  239.                 sleep(2);
  240.                 phaseColor=colors.red;
  241.                 phase="Home chest full!";
  242.                 sendData();
  243.                 phase=prevPhase;
  244.             end
  245.         end
  246.     end
  247.    
  248.     phaseColor=colors.white;
  249.     turtle.select(prevSlot);
  250. end
  251.  
  252. local function returnHome() --return home to do different tasks
  253.     local x, y, dir;
  254.    
  255.     x=advMovAPI.getX();
  256.     y=advMovAPI.getY();
  257.     dir=advMovAPI.getDir();
  258.    
  259.     if (advMovAPI.getX()>0)
  260.     then
  261.         if (advMovAPI.getDir()==0)
  262.         then
  263.             turnL();
  264.             turnL();
  265.         elseif (advMovAPI.getDir()==1)
  266.         then
  267.             turnL();
  268.         elseif (advMovAPI.getDir()==3)
  269.         then
  270.             turnR();
  271.         end
  272.     elseif (advMovAPI.getX()<0)
  273.     then
  274.         if (advMovAPI.getDir()==2)
  275.         then
  276.             turnL();
  277.             turnL();
  278.         elseif (advMovAPI.getDir()==1)
  279.         then
  280.             turnR();
  281.         elseif (advMovAPI.getDir()==3)
  282.         then
  283.             turnL();
  284.         end
  285.     end
  286.    
  287.     while(advMovAPI.getX()~=0)
  288.     do
  289.         advMovAPI.forward(true);
  290.         homeFuel=homeFuel+1;
  291.     end
  292.    
  293.     if (advMovAPI.getY()>0)
  294.     then
  295.         if (advMovAPI.getDir()==1)
  296.         then
  297.             turnL();
  298.             turnL();
  299.         elseif (advMovAPI.getDir()==2)
  300.         then
  301.             turnL();
  302.         elseif (advMovAPI.getDir()==0)
  303.         then
  304.             turnR();
  305.         end
  306.     elseif (advMovAPI.getY()<0)--This is useless...
  307.     then
  308.         if (advMovAPI.getDir()==3)
  309.         then
  310.             turnL();
  311.             turnL();
  312.         elseif (advMovAPI.getDir()==2)
  313.         then
  314.             turnR();
  315.         elseif (advMovAPI.getDir()==0)
  316.         then
  317.             turnL();
  318.         end
  319.     end
  320.    
  321.     while(advMovAPI.getY()~=0)
  322.     do
  323.         advMovAPI.forward(true);
  324.         homeFuel=homeFuel+1;
  325.     end
  326.    
  327.     if (advMovAPI.getDir()==0)
  328.     then
  329.         turnL();
  330.     elseif (advMovAPI.getDir()==2)
  331.     then
  332.         turnR();
  333.     elseif (advMovAPI.getDir()==3)
  334.     then
  335.         turnR();
  336.         turnR();
  337.     end
  338.    
  339.     turnL();
  340.     dropAll(false);
  341.     turnR();
  342.     returnBack(x, y, dir);
  343. end
  344.  
  345. local function pickUp() --Pick up all item from an inventory *unused
  346.     local slot
  347.  
  348.     slot=1;
  349.     print("Taking seeds...");
  350.  
  351.     for slot=1,16
  352.     do
  353.         turtle.select(slot);
  354.         turtle.suck(64);
  355.     end
  356. end
  357.  
  358. local function select() --Select the first not empty slot *unused
  359.     local slot
  360.  
  361.     for slot=1,16
  362.     do
  363.         if (turtle.getItemCount(slot)>0)
  364.         then
  365.             turtle.select(slot);
  366.             return;
  367.         end
  368.     end
  369. end
  370.  
  371. local function placeTorch()
  372.     local prevSlot;
  373.    
  374.     prevSlot=turtle.getSelectedSlot();
  375.     turtle.select(torchSlot);
  376.     turtle.placeUp();
  377.     turtle.select(prevSlot);
  378. end
  379.  
  380. local function checkForLiquid() --Check for lava in front of it
  381.     local prevSlot,full,fuel;
  382.    
  383.     prevSlot=turtle.getSelectedSlot();
  384.     turtle.select(bucketSlot);
  385.    
  386.     if (tonumber(turtle.getFuelLimit())==nil)
  387.     then
  388.         return;
  389.     end
  390.    
  391.     if (turtle.place())
  392.     then
  393.         fuel=turtle.getFuelLevel();
  394.        
  395.         if (turtle.refuel())
  396.         then
  397.             if (not full)
  398.             then
  399.                 startFuel=startFuel+(turtle.getFuelLevel()-fuel);
  400.                 fuelFound=fuelFound+(turtle.getFuelLevel()-fuel);
  401.             end
  402.         else
  403.             turtle.place();
  404.         end
  405.     end
  406.    
  407.     turtle.select(prevSlot);
  408. end
  409.  
  410. local function refresh() --Do different tasks
  411.     local prevSlot;
  412.    
  413.     updateFile();
  414.     sendData();
  415.     checkForLiquid();
  416.    
  417.     if (checkFull())
  418.     then
  419.         if (enderchest)
  420.         then
  421.             prevSlot=turtle.getSelectedSlot();
  422.             turtle.select(enderSlot);
  423.            
  424.             while(not turtle.place())
  425.             do
  426.                 turtle.dig();
  427.                 turtle.attack();
  428.             end
  429.            
  430.             dropAll(true);
  431.             turtle.dig();
  432.             turtle.select(prevSlot);
  433.         else
  434.             returnHome();
  435.         end
  436.     end
  437. end
  438.  
  439. local function digTunnel() --Dig the main tunnel
  440.     phase="Main tunnel";
  441.    
  442.     for i=0,tunnelLength
  443.     do
  444.         if (i<tunnelLength)
  445.         then
  446.             turtle.dig();
  447.         end
  448.        
  449.         turtle.digUp();
  450.        
  451.         if (i<tunnelLength)
  452.         then
  453.             advMovAPI.forward(true);
  454.             refresh();
  455.         end
  456.        
  457.     end
  458.    
  459.     turnR();
  460.     turtle.dig();
  461.     advMovAPI.forward(true);
  462.     turnR();
  463.     refresh();
  464.    
  465.     for i=0,tunnelLength
  466.     do
  467.         if (i<tunnelLength)
  468.         then
  469.             turtle.dig();
  470.         end
  471.        
  472.         turtle.digUp();
  473.        
  474.         if (i<tunnelLength)
  475.         then
  476.             advMovAPI.forward(true);
  477.             refresh();
  478.         end
  479.        
  480.     end
  481.    
  482.     turnR();
  483.     advMovAPI.forward(true);
  484.     turnR();
  485.     refresh();
  486. end
  487.  
  488. local function digBranch() --Dig a branch
  489.     torchTimer=10;
  490.    
  491.     for i=0,branchLength
  492.     do
  493.         if (i<branchLength)
  494.         then
  495.             turtle.dig();
  496.         end
  497.        
  498.         turtle.digUp();
  499.        
  500.         if (i<branchLength)
  501.         then
  502.             advMovAPI.forward(true);
  503.             refresh();
  504.         end
  505.     end
  506.    
  507.     turnL();
  508.     turnL();
  509.    
  510.     while (advMovAPI.getX()~=0)
  511.     do
  512.         if (torchTimer>=11)
  513.         then
  514.             placeTorch();
  515.                
  516.             torchTimer=0;
  517.         end
  518.            
  519.         torchTimer=torchTimer+1;
  520.    
  521.         if (advMovAPI.getX()==2 or advMovAPI.getX()==-1)
  522.         then
  523.             placeTorch();
  524.         end
  525.    
  526.         advMovAPI.forward(true);
  527.         refresh();
  528.     end
  529. end
  530.  
  531. local function digBranches() --Steps to dig branches consecutively
  532.     for i=0,numBranch-1
  533.     do
  534.         phase="Branch number "..tostring(i+1);
  535.         turnL();
  536.         digBranch();
  537.         advMovAPI.forward(true)
  538.         digBranch();
  539.         turnR();
  540.         refresh();
  541.        
  542.         if (i<numBranch-1)
  543.         then
  544.             advMovAPI.forward(true);
  545.             advMovAPI.forward(true);
  546.             advMovAPI.forward(true);
  547.            
  548.         end
  549.         refresh();
  550.     end
  551.    
  552.     while (advMovAPI.getY()>0)
  553.     do
  554.         advMovAPI.back();
  555.         refresh();
  556.     end
  557. end
  558.  
  559. ------------------------------------------------------
  560.  
  561. term.clear();
  562. term.setCursorPos(1, 1);
  563.    
  564. if (#arg>0)
  565. then
  566.     if (arg[1]=="help")
  567.     then
  568.         help();
  569.         return;
  570.     elseif(arg[1]=="start")
  571.     then
  572.         for i=2,#arg
  573.         do
  574.             if (arg[i]=="enderchest")
  575.             then
  576.                 enderchest=true;
  577.             elseif (arg[i]=="rednet")
  578.             then
  579.                 net=true;
  580.                 if (arg[i+1]~=nil)
  581.                 then
  582.                     receiverId=tonumber(arg[i+1]);
  583.                     i=i+2;
  584.                 else
  585.                     printError("You have to specify the id of the computer receiver after the argument \"rednet\".");
  586.                     return;
  587.                 end
  588.                
  589.                 rednet.open("right");
  590.             elseif (arg[i]=="home")
  591.             then
  592.                 home=true;
  593.                
  594.                 if (not fs.exists("startup"))
  595.                 then
  596.                     shell.run("pastebin", "get", "QSG5bWKr", "startup");
  597.                 end
  598.             end
  599.         end
  600.     else
  601.         error("Wrong arguments");
  602.     end
  603. else
  604.     printError("Wrong number of arguments")
  605.     print("Use \"BranchMine help\" to display an help guide.")
  606.     return;
  607. end
  608.  
  609. if (not fs.exists("SolidAPI/advMovAPI"))
  610. then
  611.     printError("Could not find the API advMovAPI.");
  612.     print("Trying to download it from pastebin.\nPress any key to continue.");
  613.    
  614.     os.pullEvent("key");
  615.     term.clear();
  616.     term.setCursorPos(1, 1);
  617.    
  618.     shell.run("pastebin", "get", "4XhYtBvC", "SolidAPI/advMovAPI");
  619.    
  620.     print("Press any key to continue.");
  621.     os.pullEvent("char");
  622.     term.clear();
  623.     term.setCursorPos(1, 1);
  624.    
  625.     if (not fs.exists("SolidAPI/advMovAPI"))
  626.     then
  627.         error("Could not find advMovAPI. An error has occurred while downloading the API.");
  628.     end
  629.    
  630.     if (os.loadAPI("SolidAPI/advMovAPI"))
  631.     then
  632.         advMovAPI.setCoords(0, 0, 0);
  633.         advMovAPI.setDir(1);
  634.     else
  635.         fs.delete("SolidAPI/advMovAPI");
  636.         error("Could not load API \"advMovAPI\". Deleting the API...");
  637.     end
  638. else
  639.     if (os.loadAPI("SolidAPI/advMovAPI"))
  640.     then
  641.         advMovAPI.setCoords(0, 0, 0);
  642.         advMovAPI.setDir(1);
  643.     else
  644.         fs.delete("SolidAPI/advMovAPI");
  645.         error("Could not load API \"advMovAPI\". Deleting the API...");
  646.     end
  647. end
  648.  
  649. term.clear();
  650. term.setCursorPos(1, 1);
  651.  
  652. setup();
  653.  
  654. term.clear();
  655. term.setCursorPos(1, 1);
  656.  
  657. numBranch=math.ceil(tunnelLength/3);
  658. fuelNeeded=(tunnelLength*2+2)+(2*numBranch)+((numBranch-1)*3)+((numBranch-1)*3)+(numBranch*branchLength*4)+2;--ft+2+fb
  659.  
  660. if (turtle.getFuelLevel()<fuelNeeded)
  661. then
  662.     print("The fuel level is too low to run the program.\n");
  663.     print("Needed: "..fuelNeeded);
  664.     print("Current: "..turtle.getFuelLevel());
  665.     print("\nIt is suggested to put more fuel than necessary to prevent an occasionally return to the starting position.");
  666.     return;
  667. end
  668.  
  669. digTunnel();
  670. sendData();
  671. advMovAPI.forward(true);
  672. digBranches();
  673. sendData();
  674.  
  675. if (not enderchest)
  676. then
  677.     turnL();
  678.     dropAll(false);
  679.     turnR();
  680. else
  681.     turtle.select(enderSlot);
  682.  
  683.     while(not turtle.place())
  684.     do
  685.         turtle.dig();
  686.         turtle.attack();
  687.     end
  688.    
  689.     dropAll(true);
  690.     turtle.dig();
  691. end
  692.  
  693. phase="Completed";
  694. phaseColor=colors.lime;
  695. sendData();
  696. fs.delete("BMStatus");
  697.  
  698. --TODO: more data send
  699.  
  700. --Original script by SolidSnake96AS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement