Advertisement
Guest User

test

a guest
Aug 21st, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. -- Nuclear control v.02
  2. local VER = "0.2"
  3. local OUT_SIDE = "back";
  4. local IN_SIDE = "top";
  5. local matchTable = {};
  6. local peripheralTable = {};
  7. local MATCH_FILE = "nuc_match.txt";
  8. local NUCLEAR_PREFIX_NAME = "nuclear_reactor_";
  9. local REACTOR_POSTFIX_NAMES = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
  10. local bundleColors = {colors.white, colors.orange,  colors.magenta,     colors.magenta,     colors.lightBlue,   colors.yellow, colors.lime, colors.pink,    colors.gray,    colors.lightGray,   colors.cyan,    colors.purple,  colors.blue,    colors.brown, colors.green, colors.red, colors.black    };
  11. local MAX_HEAT = 7330;
  12. local MIN_HEAT = 7000;
  13. print("Nuclear control v" .. VER);
  14.  
  15. function createPeripheralTable()
  16.     local res = {};
  17.     for i, v in ipairs(REACTOR_POSTFIX_NAMES) do
  18.         res[i] = peripheral.wrap(NUCLEAR_PREFIX_NAME .. v );
  19.     end
  20.     return res;
  21. end
  22.  
  23. function createMatchTable(pTable)
  24.     local res = {};
  25.     local outCnt;
  26.  
  27.     for i,v in ipairs(bundleColors) do
  28.         outCnt = 0;
  29.         redstone.setBundledOutput(OUT_SIDE, v);
  30.         os.sleep(1);
  31.         for j,y in ipairs(pTable) do
  32.             if y.isActive() then
  33.                 outCnt = outCnt + 1;
  34.                 matchTable[j] = v;
  35.             end
  36.         end
  37.         if outCnt > 1 then
  38.             error("ERROR: color=" .. i .. " has been found in more 1 machines!");
  39.         end
  40.  
  41.     end
  42.  
  43.     return res;
  44. end
  45.  
  46. function loadMatchTable(fileName, pTable)
  47.     local colUns;
  48.     local res = {};
  49.     if fs.exists(fileName) then
  50.         local file = fs.open(fileName, "r");
  51.         for i,v in ipairs(pTable) do
  52.             colUns = file.readLine();
  53.             if colUns ~= nil then
  54.                 res[i] = tonumber(colUns);
  55.             end
  56.         end
  57.         file.close();
  58.     end
  59.     return res;
  60. end
  61.  
  62. function saveMatchTable(fileName, mTable)
  63.     local file = fs.open(fileName, "w");
  64.     for i,v in ipairs(mTable) do
  65.         file.writeLine(v);
  66.     end
  67.     file.close();
  68. end
  69.  
  70. function enableAll(bEnable)
  71.     redstone.setOutput(OUT_SIDE, bEnable);
  72. end
  73.  
  74. function checkOverheatAll(pTable)
  75.     for i,v in ipairs(pTable) do
  76.         if (v.getHeat() > MAX_HEAT) then
  77.             return i;
  78.         end
  79.     end
  80.     return nil;
  81. end
  82.  
  83.  
  84. function showMonitoring(ctx, pTable, bEnable, alarmId)
  85.     local sym = "#";
  86.     local startPos = 10;
  87.     local startHeight = 5;
  88.     local lineHeight = 10;
  89.     local symCol;
  90.     if (bEnable) then
  91.         symCol = 0x00EE00;
  92.     else
  93.         symCol = 0x606060;
  94.     end
  95.  
  96.     ctx.clear();
  97.     for i,v in ipairs(pTable) do
  98.         if (i ~= alarmId) then
  99.             ctx.addText(startPos, tonumber(i)*lineHeight + startHeight, sym .. " " .. v.getHeat() .. "/" .. v.getEUOutput()*5,   symCol);
  100.         else
  101.             ctx.addText(startPos, tonumber(i)*lineHeight + startHeight, sym .. " " .. v.getHeat() .. "/" .. v.getEUOutput()*5,   0xFF0000);
  102.         end
  103.     end
  104. end
  105.  
  106. function createMonitorContext()
  107.     local ctx;
  108.     ctx = peripheral.wrap("left");
  109.  
  110.     return ctx;
  111. end
  112.  
  113. function startController()
  114.     peripheralTable = createPeripheralTable();
  115.     local alarmId;
  116.     local ctxMon;
  117.     local bEnable = true;
  118.  local id = -1;
  119.     ctxMon = createMonitorContext();
  120.     enableAll(true);
  121.     os.sleep(1);
  122.     while (true) do
  123.         os.sleep(1);
  124.         alarmId = checkOverheatAll(peripheralTable);
  125.         if (alarmId ~= nil) then
  126.             if (bEnable) then
  127.                 enableAll(false);
  128.                 bEnable = false;
  129.     id = alarmId;
  130.             end
  131.         end
  132.         if (bEnable == false and peripheralTable[id].getHeat() < MIN_HEAT) then
  133.             bEnable = true;
  134.             enableAll(true);
  135.         end
  136.         showMonitoring(ctxMon, peripheralTable, bEnable, alarmId);
  137.  
  138.     end
  139. --  error("Not implemented yet");
  140. end
  141.  
  142. local tArgs = {...};
  143. if #tArgs == 0 or #tArgs > 1 then
  144.         print("Usage: program <control>");
  145. elseif #tArgs == 1 then
  146.     -- match command is deprecated
  147.     if (tArgs[1] == "match") then
  148.         print("Create match file mode");
  149.         local pTable = createPeripheralTable();
  150.         local mTable = createMatchTable(pTable);
  151.         saveMatchTable(MATCH_FILE, mTable);
  152.         print("It's created successfully");
  153.     elseif (tArgs[1] == "control") then
  154.   while true do
  155.         pcall(startController);
  156.     print("To break press any key");
  157.     os.startTimer(1);
  158.     local event = os.pullEvent();
  159.     if event == "timer" then
  160.       print("Something bug in mods");
  161.     else
  162.       print("Break");
  163.       break;
  164.     end
  165.    
  166.   end
  167.  
  168.     end
  169.  
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement