Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Nuclear control v.02
- local VER = "0.2"
- local OUT_SIDE = "back";
- local IN_SIDE = "top";
- local matchTable = {};
- local peripheralTable = {};
- local MATCH_FILE = "nuc_match.txt";
- local NUCLEAR_PREFIX_NAME = "nuclear_reactor_";
- local REACTOR_POSTFIX_NAMES = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
- 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 };
- local MAX_HEAT = 7330;
- local MIN_HEAT = 7000;
- print("Nuclear control v" .. VER);
- function createPeripheralTable()
- local res = {};
- for i, v in ipairs(REACTOR_POSTFIX_NAMES) do
- res[i] = peripheral.wrap(NUCLEAR_PREFIX_NAME .. v );
- end
- return res;
- end
- function createMatchTable(pTable)
- local res = {};
- local outCnt;
- for i,v in ipairs(bundleColors) do
- outCnt = 0;
- redstone.setBundledOutput(OUT_SIDE, v);
- os.sleep(1);
- for j,y in ipairs(pTable) do
- if y.isActive() then
- outCnt = outCnt + 1;
- matchTable[j] = v;
- end
- end
- if outCnt > 1 then
- error("ERROR: color=" .. i .. " has been found in more 1 machines!");
- end
- end
- return res;
- end
- function loadMatchTable(fileName, pTable)
- local colUns;
- local res = {};
- if fs.exists(fileName) then
- local file = fs.open(fileName, "r");
- for i,v in ipairs(pTable) do
- colUns = file.readLine();
- if colUns ~= nil then
- res[i] = tonumber(colUns);
- end
- end
- file.close();
- end
- return res;
- end
- function saveMatchTable(fileName, mTable)
- local file = fs.open(fileName, "w");
- for i,v in ipairs(mTable) do
- file.writeLine(v);
- end
- file.close();
- end
- function enableAll(bEnable)
- redstone.setOutput(OUT_SIDE, bEnable);
- end
- function checkOverheatAll(pTable)
- for i,v in ipairs(pTable) do
- if (v.getHeat() > MAX_HEAT) then
- return i;
- end
- end
- return nil;
- end
- function showMonitoring(ctx, pTable, bEnable, alarmId)
- local sym = "#";
- local startPos = 10;
- local startHeight = 5;
- local lineHeight = 10;
- local symCol;
- if (bEnable) then
- symCol = 0x00EE00;
- else
- symCol = 0x606060;
- end
- ctx.clear();
- for i,v in ipairs(pTable) do
- if (i ~= alarmId) then
- ctx.addText(startPos, tonumber(i)*lineHeight + startHeight, sym .. " " .. v.getHeat() .. "/" .. v.getEUOutput()*5, symCol);
- else
- ctx.addText(startPos, tonumber(i)*lineHeight + startHeight, sym .. " " .. v.getHeat() .. "/" .. v.getEUOutput()*5, 0xFF0000);
- end
- end
- end
- function createMonitorContext()
- local ctx;
- ctx = peripheral.wrap("left");
- return ctx;
- end
- function startController()
- peripheralTable = createPeripheralTable();
- local alarmId;
- local ctxMon;
- local bEnable = true;
- local id = -1;
- ctxMon = createMonitorContext();
- enableAll(true);
- os.sleep(1);
- while (true) do
- os.sleep(1);
- alarmId = checkOverheatAll(peripheralTable);
- if (alarmId ~= nil) then
- if (bEnable) then
- enableAll(false);
- bEnable = false;
- id = alarmId;
- end
- end
- if (bEnable == false and peripheralTable[id].getHeat() < MIN_HEAT) then
- bEnable = true;
- enableAll(true);
- end
- showMonitoring(ctxMon, peripheralTable, bEnable, alarmId);
- end
- -- error("Not implemented yet");
- end
- local tArgs = {...};
- if #tArgs == 0 or #tArgs > 1 then
- print("Usage: program <control>");
- elseif #tArgs == 1 then
- -- match command is deprecated
- if (tArgs[1] == "match") then
- print("Create match file mode");
- local pTable = createPeripheralTable();
- local mTable = createMatchTable(pTable);
- saveMatchTable(MATCH_FILE, mTable);
- print("It's created successfully");
- elseif (tArgs[1] == "control") then
- while true do
- pcall(startController);
- print("To break press any key");
- os.startTimer(1);
- local event = os.pullEvent();
- if event == "timer" then
- print("Something bug in mods");
- else
- print("Break");
- break;
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement