Advertisement
SolidSnake96AS

Branch Mining - Data Receiver

Sep 14th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local i, width, height, percentage, phase, phaseColor, senderId, distance, protocol, fuelLevel, fuelFound, tries, timeout, marquee, ypos;
  2. local arg={...};
  3.  
  4. local function help()
  5.     print("To use this program put a wireless modem on the left side of the computer and a 3x2 (3 width, 2 height) display to the right.");
  6. end
  7.  
  8. local function setBoot()
  9.     file=fs.open("startup", "w");
  10.     file.write("shell.run(\"BMReceiver\", \"start\");");
  11.     file.close();
  12. end
  13.  
  14. local function resetMonitor()
  15.     t.setTextScale(1);
  16.     t.setBackgroundColor(colors.black);
  17.     t.setCursorPos(1, 1);
  18.     t.setTextColor(colors.white);
  19.     width, height=t.getSize();
  20. end
  21.  
  22. local function getElement(data, pos) --Parse a string (with format *,*,*,[...]) and return the elemnt "pos"
  23.     local count, i, returnValue;
  24.    
  25.     count=0;
  26.    
  27.     while(count<=pos)
  28.     do
  29.         i=1;
  30.         while(string.char(string.byte(data, i))~="," and i<=#data)
  31.         do
  32.             i=i+1;
  33.         end
  34.        
  35.         returnValue=string.sub(data, 0, i-1);
  36.         data=string.sub(data, i+1);
  37.         count=count+1;
  38.     end
  39.    
  40.     return returnValue
  41. end
  42.  
  43. if (#arg==0)
  44. then
  45.     printError("You have to specify an argument.");
  46.     print("\nArguments:");
  47.     print("help: Display an help guide.");
  48.     print("id: Get the id of this computer to use with rednet.");
  49.     print("start: Start the program.");
  50.     print("setboot: This will cause the program to start automatically when the computer turns on.");
  51.     return;
  52. elseif (#arg>1)
  53. then
  54.     printError("Wrong number of argument");
  55.     return;
  56. else
  57.     if (arg[1]=="id")
  58.     then
  59.         print("Receiver id: "..os.getComputerID());
  60.         return;
  61.     elseif (arg[1]=="help")
  62.     then
  63.         help();
  64.         return;
  65.     elseif (arg[1]=="setboot")
  66.     then
  67.         setBoot();
  68.         return;
  69.     elseif (arg[1]~="start")
  70.     then
  71.         printError("Wrong argument");
  72.         return;
  73.     end
  74. end
  75.  
  76. rednet.open("left");
  77. t=peripheral.wrap("right");
  78.  
  79. phaseColor=colors.white;
  80.  
  81. term.clear();
  82. term.setCursorPos(1, 1);
  83.  
  84. t.clear();
  85. resetMonitor();
  86.  
  87. tries=5;
  88. marquee=0;
  89. ypos=0;
  90.  
  91. while(true)
  92. do
  93.     if (tries>=5)
  94.     then
  95.         timeout=0.1;
  96.     else
  97.         timeout=20;
  98.     end
  99.    
  100.     senderId, msg, distance, protocol=rednet.receive(timeout);
  101.     t.clear();
  102.    
  103.     if (msg~=nil)
  104.     then
  105.         percentage=tostring(math.floor(tonumber(getElement(msg, 0))));
  106.         phase=getElement(msg, 1);
  107.         phaseColor=tonumber(getElement(msg, 2));
  108.         fuelLevel=getElement(msg, 3);
  109.         fuelFound=getElement(msg, 4);
  110.         tries=0;
  111.        
  112.         term.clear();
  113.         term.setCursorPos(1, 1);
  114.     else
  115.         if (tries<5)
  116.         then
  117.             tries=tries+1;
  118.             print("Timeout... try number "..tries.." out of 5");
  119.         end
  120.     end
  121.    
  122.     if (tries<5)
  123.     then
  124.         --t.setTextScale(2);
  125.         t.setTextColor(colors.lime);
  126.         t.setCursorPos(width/2-string.len("Branch Mining")/2+1, 1);
  127.         t.write("Branch Mining");
  128.        
  129.         resetMonitor();
  130.        
  131.         t.setCursorPos(1, 3);
  132.         t.write("Phase: ");
  133.         t.setTextColor(phaseColor);
  134.         t.setCursorPos(string.len("Phase: ")+1, 3);
  135.         t.write(phase);
  136.         resetMonitor();
  137.         t.setCursorPos(1, 4);
  138.         t.write("Fuel level: "..fuelLevel);
  139.         t.setCursorPos(1, 5);
  140.         t.write("Fuel found: "..fuelFound);
  141.        
  142.         t.setCursorPos(width/2-string.len("Progress")/2+2, 7);
  143.         t.write("Progress");
  144.        
  145.         for i=1,width-8
  146.         do
  147.             if (i<=(percentage/(100/(width-8))))
  148.             then
  149.                 t.setBackgroundColor(colors.lime);
  150.             else
  151.                 t.setBackgroundColor(colors.red);
  152.             end
  153.            
  154.             t.setCursorPos((width/2-(width-8)/2)+i, 9);
  155.             t.write(" ");
  156.         end
  157.        
  158.         t.setBackgroundColor(colors.black);
  159.         t.setCursorPos((width/2)-(string.len(percentage.."%")/2)+2, 8);
  160.         t.write(percentage.."%");
  161.        
  162.         t.setTextColor(colors.gray);
  163.         t.setCursorPos(width-string.len("by SolidSnake96AS")+1, height);
  164.         t.write("by SolidSnake96AS");
  165.         resetMonitor();
  166.     else
  167.         marquee=marquee+1.5;
  168.  
  169.         if (marquee>25)
  170.         then
  171.             marquee=-25;
  172.             ypos=math.random(0,4);
  173.         end
  174.    
  175.         resetMonitor();
  176.         t.setTextColor(colors.gray);
  177.         t.setCursorPos(width-string.len("by SolidSnake96AS")+1, height);
  178.         t.write("by SolidSnake96AS");
  179.        
  180.         t.setTextColor(colors.lime);
  181.         t.setCursorPos(width/2-string.len("Branch Mining Script")/2+1, 2);
  182.         t.write("Branch Mining Script");
  183.        
  184.         t.setTextColor(colors.yellow);
  185.         t.setCursorPos(width/2-string.len("StandBy")/2+1, 3);
  186.         t.write("StandBy");
  187.         resetMonitor();
  188.        
  189.         t.setTextColor(colors.red);
  190.         t.setCursorPos(width/2-string.len(" __  _  _  ___ _   _ ")/2+1+marquee, 4+ypos);
  191.         t.write(" __  _  _  ___ _   _ ");
  192.         t.setTextColor(colors.orange);
  193.         t.setCursorPos(width/2-string.len("/ _|| \\| || o ) \\_/ |")/2+1+marquee, 5+ypos);
  194.         t.write("/ _|| \\| || o ) \\_/ |");
  195.         t.setTextColor(colors.lime);
  196.         t.setCursorPos(width/2-string.len("\\_ \\| \\\\ || o \\ \\_/ |")/2+1+marquee, 6+ypos);
  197.         t.write("\\_ \\| \\\\ || o \\ \\_/ |");
  198.         t.setTextColor(colors.cyan);
  199.         t.setCursorPos(width/2-string.len("|__/|_|\\_||___/_| |_|")/2+1+marquee, 7+ypos);
  200.         t.write("|__/|_|\\_||___/_| |_|");
  201.        
  202.         --[[
  203.              __  _  _  ___ _   _
  204.             / _|| \| || o ) \_/ |
  205.             \_ \| \\ || o \ \_/ |
  206.             |__/|_|\_||___/_| |_|
  207.         ]]--
  208.     end
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement