Advertisement
Xlonix

Reactor code

Feb 24th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local reactors = {};
  2. local energyCell = 1;
  3. local mon = 1;
  4. local reactorSel = 1;
  5. local monSel = 1;
  6. local w = 1;
  7. local h = 1;
  8. local clicked = "none";
  9. local run = false;
  10. local cell;
  11.  
  12. function getReactors()
  13.   i = {};
  14.   j = 1;
  15.   for k=1, #peripheral.getNames() do
  16.     if (string.find(peripheral.getNames()[k], "Reactor") ~= nil) then
  17.       table.insert(i, j, peripheral.wrap(peripheral.getNames()[k]));
  18.       j = j+1;
  19.     end
  20.   end
  21.   return i;
  22. end    
  23.  
  24. function numberOf(item)
  25.   i = 0;
  26.   for j=1, #peripheral.getNames() do
  27.     if (string.find(peripheral.getNames()[j], item) ~= nil) then
  28.       i = i+1;
  29.     end
  30.   end
  31.   return i;
  32. end
  33.  
  34. function getMonitors()
  35.   i = {};
  36.   j = 1;
  37.   for k=1, #peripheral.getNames() do
  38.     if (string.find(peripheral.getNames()[k], "monitor") ~= nil) then
  39.       table.insert(i, j, peripheral.wrap(peripheral.getNames()[k]));
  40.       j = j+1;
  41.     end
  42.   end
  43.   return i;
  44. end
  45.  
  46. function getCell()
  47.   for j=1, #peripheral.getNames() do
  48.     if (string.find(peripheral.getNames()[j], "thermalexpansion_cell") ~= nil) then
  49.       return peripheral.wrap(peripheral.getNames()[j]);
  50.     end
  51.   end
  52. end
  53.  
  54. function setup()
  55.   term.clear();
  56.   term.setCursorPos(1,1);
  57.   reactors = getReactors();
  58.   if (#reactors > 0) then
  59.     write(numberOf("monitor") .. " monitors detected.\nSelect 1-" .. numberOf("monitor") .. "\n>");
  60.     monSel = tonumber(read());
  61.     if (monSel < numberOf("monitor")) or (monSel > numberOf("monitor")) then
  62.       write("Invalid monitor!\n");
  63.     else
  64.       mon = getMonitors()[monSel];
  65.       term.redirect(mon);
  66.       w, h = term.getSize();
  67.       button.newButton("last", 3,2,3,2);
  68.       button.newButton("next", 7,2,7,2);
  69.       button.newButton("exit", w,h,w,h);
  70.       button.newButton("power", 9,2,13,2);
  71.       cell = getCell();
  72.       run = true;
  73.     end
  74.   else
  75.     write("No reactors detected!\n");
  76.   end
  77. end
  78.  
  79. function main()
  80.   if (clicked == "last") then
  81.     if (reactorSel > 0) then
  82.       reactorSel = reactorSel-1;
  83.     end
  84.   elseif (clicked == "next") then
  85.     if(reactorSel < numberOf("Reactor")) then
  86.       reactorSel = reactorSel+1;
  87.     end
  88.   elseif (clicked == "exit") then
  89.     term.setBackgroundColor(colors.black);
  90.     term.clear();
  91.     run = false;
  92.     return;
  93.   elseif (clicked == "power") then
  94.     if(reactors[reactorSel].getActive()) then
  95.       reactors[reactorSel].setActive(false);
  96.     else
  97.       reactors[reactorSel].setActive(true);
  98.     end
  99.   end
  100.  
  101.   paintutils.drawFilledBox(1, 1, w, h, colors.black);
  102.  
  103.   current = reactors[reactorSel];
  104.  
  105.   term.setCursorPos(3,2);
  106.  
  107.   term.setBackgroundColor(colors.blue);
  108.   term.setTextColor(colors.black);
  109.   term.write("<");
  110.  
  111.   term.setBackgroundColor(colors.black);
  112.   term.setTextColor(colors.white);
  113.   term.write(" " .. reactorSel .. " ");
  114.  
  115.   term.setBackgroundColor(colors.blue);
  116.   term.setTextColor(colors.black);
  117.   term.write(">");
  118.  
  119.  
  120.   term.setCursorPos(9,2);
  121.   if(current.getActive()) then
  122.     term.setBackgroundColor(colors.red);
  123.        term.setTextColor(colors.black);
  124.     term.write("STOP ");
  125.   else
  126.     term.setBackgroundColor(colors.green);
  127.     term.setTextColor(colors.black);
  128.     term.write("START");
  129.   end
  130.  
  131.   term.setCursorPos(1, h-2);
  132.   term.setTextColor(colors.white);
  133.   term.setBackgroundColor(colors.black);
  134.   term.write("Energy Cell Storage");
  135.  
  136.   paintutils.drawFilledBox(1, h-1, w*(cell.getEnergyStored()/cell.getMaxEnergyStored()), h, colors.red);
  137.  
  138.   paintutils.drawFilledBox(2, (h*(1/4))*(current.getFuelAmount()/current.getFuelAmountMax()), 4, h-4, colors.yellow);
  139.  
  140.   term.setCursorPos(w,h);
  141.   term.setBackgroundColor(colors.red);
  142.   term.setTextColor(colors.black);
  143.   term.write("X");
  144.  
  145.   os.startTimer(5);
  146.   os.pullEventRaw("timer");
  147. end
  148.  
  149. setup();
  150.  
  151. while run do
  152.   parallel.waitForAny(button.monDetect, main);
  153.   clicked = button.getLastClicked();
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement