Advertisement
SolidSnake96AS

Branch Mining - Return Home

Nov 1st, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. local file, str, x, y, net, receiverId, enderchest, percentage, phase, phaseColor, fuelFound, enderSlot;
  2.  
  3. enderSlot=15;
  4.  
  5. local function sendData() --Send the data to the receiver
  6.     if (net==0)
  7.     then
  8.         return
  9.     end
  10.    
  11.     print(percentage);
  12.     print(phase);
  13.     print(phaseColor);
  14.     print(turtle);
  15.     print(fuelFound);
  16.    
  17.     msg=""..percentage..","..phase..","..phaseColor..","..turtle.getFuelLevel()..","..fuelFound;
  18.     rednet.send(receiverId, msg);
  19. end
  20.  
  21. local function dropAll(ender) --Drop all item in the inventory
  22.     local slot, numb, prevSlot, prevPhase;
  23.    
  24.     prevSlot=turtle.getSelectedSlot();
  25.    
  26.     if (not ender)
  27.     then
  28.         numb=14;
  29.     else
  30.         numb=13;
  31.     end
  32.    
  33.     for slot=1,numb
  34.     do
  35.         turtle.select(slot);
  36.         phaseColor=colors.yellow;
  37.         phase="Droppping Item";
  38.         sendData();
  39.        
  40.         if (ender)
  41.         then
  42.             prevPhase=phase;
  43.             while (not turtle.drop())
  44.             do
  45.                 if (turtle.getItemCount(slot)==0)
  46.                 then
  47.                     break
  48.                 end
  49.                
  50.                 sleep(2);
  51.                 phaseColor=colors.red;
  52.                 phase="EnderChest full!";
  53.                 sendData();
  54.                 phase=prevPhase;
  55.             end
  56.         else
  57.             prevPhase=phase;
  58.             while (not turtle.drop())
  59.             do
  60.                 if (turtle.getItemCount(slot)==0)
  61.                 then
  62.                     break
  63.                 end
  64.                
  65.                 sleep(2);
  66.                 phaseColor=colors.red;
  67.                 phase="Home chest full!";
  68.                 sendData();
  69.                 phase=prevPhase;
  70.             end
  71.         end
  72.     end
  73.    
  74.     phaseColor=colors.white;
  75.     turtle.select(prevSlot);
  76. end
  77.  
  78. local function returnHome() --return home to do different tasks
  79.     local x, y, dir, prevSlot;
  80.    
  81.     x=advMovAPI.getX();
  82.     y=advMovAPI.getY();
  83.     dir=advMovAPI.getDir();
  84.    
  85.     if (advMovAPI.getX()>0)
  86.     then
  87.         if (advMovAPI.getDir()==0)
  88.         then
  89.             advMovAPI.turnL();
  90.             advMovAPI.turnL();
  91.         elseif (advMovAPI.getDir()==1)
  92.         then
  93.             advMovAPI.turnL();
  94.         elseif (advMovAPI.getDir()==3)
  95.         then
  96.             advMovAPI.turnR();
  97.         end
  98.     elseif (advMovAPI.getX()<0)
  99.     then
  100.         if (advMovAPI.getDir()==2)
  101.         then
  102.             advMovAPI.turnL();
  103.             advMovAPI.turnL();
  104.         elseif (advMovAPI.getDir()==1)
  105.         then
  106.             advMovAPI.turnR();
  107.         elseif (advMovAPI.getDir()==3)
  108.         then
  109.             advMovAPI.turnL();
  110.         end
  111.     end
  112.    
  113.     while(advMovAPI.getX()~=0)
  114.     do
  115.         advMovAPI.forward(true);
  116.         sendData();
  117.     end
  118.    
  119.     if (advMovAPI.getY()>0)
  120.     then
  121.         if (advMovAPI.getDir()==1)
  122.         then
  123.             advMovAPI.turnL();
  124.             advMovAPI.turnL();
  125.         elseif (advMovAPI.getDir()==2)
  126.         then
  127.             advMovAPI.turnL();
  128.         elseif (advMovAPI.getDir()==0)
  129.         then
  130.             advMovAPI.turnR();
  131.         end
  132.     elseif (advMovAPI.getY()<0)--This is useless...
  133.     then
  134.         if (advMovAPI.getDir()==3)
  135.         then
  136.             advMovAPI.turnL();
  137.             advMovAPI.turnL();
  138.         elseif (advMovAPI.getDir()==2)
  139.         then
  140.             advMovAPI.turnR();
  141.         elseif (advMovAPI.getDir()==0)
  142.         then
  143.             advMovAPI.turnL();
  144.         end
  145.     end
  146.    
  147.     while(advMovAPI.getY()~=0)
  148.     do
  149.         advMovAPI.forward(true);
  150.         sendData();
  151.     end
  152.    
  153.     if (advMovAPI.getDir()==0)
  154.     then
  155.         advMovAPI.turnL();
  156.     elseif (advMovAPI.getDir()==2)
  157.     then
  158.         advMovAPI.turnR();
  159.     elseif (advMovAPI.getDir()==3)
  160.     then
  161.         advMovAPI.turnR();
  162.         advMovAPI.turnR();
  163.     end
  164.    
  165.     if (enderchest==0)
  166.     then
  167.         advMovAPI.turnL();
  168.         dropAll(false);
  169.         advMovAPI.turnR();
  170.     else
  171.         prevSlot=turtle.getSelectedSlot();
  172.         turtle.select(enderSlot);
  173.        
  174.         while(not turtle.place())
  175.         do
  176.             turtle.dig();
  177.             turtle.attack();
  178.         end
  179.        
  180.         dropAll(true);
  181.         turtle.dig();
  182.         turtle.select(prevSlot);
  183.     end
  184. end
  185.  
  186. local function getElement(data, pos) --Parse a string (with format *,*,*[,...]) and return the elemnt "pos"
  187.     local count, i, returnValue;
  188.    
  189.     count=0;
  190.    
  191.     while(count<=pos)
  192.     do
  193.         i=1;
  194.         while(string.char(string.byte(data, i))~="," and i<=#data)
  195.         do
  196.             i=i+1;
  197.         end
  198.        
  199.         returnValue=string.sub(data, 0, i-1);
  200.         data=string.sub(data, i+1);
  201.         count=count+1;
  202.     end
  203.    
  204.     return returnValue
  205. end
  206.  
  207. ---------------------------------------------------------
  208.  
  209. if (not fs.exists("SolidAPI/advMovAPI"))
  210. then
  211.     printError("Could not find the API advMovAPI.");
  212.     print("Trying to download it from pastebin.");
  213.    
  214.     shell.run("pastebin", "get", "4XhYtBvC", "SolidAPI/advMovAPI");
  215.    
  216.     if (not fs.exists("SolidAPI/advMovAPI"))
  217.     then
  218.         error("There was an error while downloading the API.")
  219.         return;
  220.     end
  221. end
  222.  
  223. if (not os.loadAPI("SolidAPI/advMovAPI"))
  224. then
  225.     fs.delete("SolidAPI/advMovAPI");
  226.     error("There was an error while loading the API. Deleting the API...")
  227.     return;
  228. end
  229.  
  230. if (fs.exists("BMStatus"))
  231. then
  232.     file=fs.open("BMStatus", "r");
  233.  
  234.     str=file.readAll();
  235.     print(str);
  236.  
  237.     x=tonumber(getElement(str, 0));
  238.     y=tonumber(getElement(str, 1));
  239.     enderchest=tonumber(getElement(str, 3));
  240.     net=tonumber(getElement(str, 4));
  241.     receiverId=tonumber(getElement(str, 5));
  242.     fuelFound=tonumber(getElement(str, 6));
  243.     percentage=tonumber(getElement(str, 7));
  244.    
  245.     phaseColor=colors.red;
  246.     phase="Server restarted!";
  247.  
  248.     if(net==1)
  249.     then
  250.         rednet.open("right");
  251.     end
  252.  
  253.     advMovAPI.setCoords(x, y, 0);
  254.     advMovAPI.setDir(tonumber(getElement(str, 2)));
  255.  
  256.     file.close();
  257.    
  258.     sendData();
  259.    
  260.     if (x==0 and y==0)
  261.     then
  262.         phaseColor=colors.yellow;
  263.         phase="StandBy";
  264.         sendData();
  265.         return;
  266.     end
  267.    
  268.     returnHome();
  269.  
  270.  
  271.     phaseColor=colors.red;
  272.     phase="Not completed!";
  273.     sendData();
  274. end
  275.  
  276. fs.delete("BMStatus");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement