Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- --------------------------------------------------------------------------
- -- Constants
- -- --------------------------------------------------------------------------
- INPUT = "bottom";
- OUTPUT = "left";
- MB_LOW = 1000;
- MB_CRITICAL = 5000;
- PROCESS_TIME = 30;
- POLL = 1;
- MON = peripheral.wrap("top");
- MON2 = peripheral.wrap("monitor_1");
- Monitors = { MON, MON2 }
- Data = {
- {
- Name = "Redstone",
- Color = colors.red,
- Low = colors.white,
- Critical = colors.orange,
- Item = colors.magenta,
- mbPerItem = 900,
- Output = colors.white,
- },
- {
- Name = "Glowstone",
- Color = colors.yellow,
- Low = colors.lightBlue,
- Critical = colors.yellow,
- Item = colors.lime,
- mbPerItem = 1000,
- Output = colors.orange,
- },
- {
- Name = "Enderium",
- Color = colors.green,
- Low = colors.pink,
- Critical = colors.gray,
- Item = colors.lightGray,
- mbPerItem = 250,
- Output = colors.magenta,
- },
- {
- Name = "Pyrotheum",
- Color = colors.orange,
- Low = colors.cyan,
- Critical = colors.purple,
- Item = colors.blue,
- mbPerItem = 100,
- Output = colors.lightBlue,
- },
- {
- Name = "Cryotheum",
- Color = colors.cyan,
- Low = colors.brown,
- Critical = colors.green,
- Item = colors.red,
- mbPerItem = 100,
- Output = colors.yellow,
- },
- }
- Status = {
- Good = "Good",
- Low = "Low",
- Critical = "Critical",
- }
- MonData = {
- Columns = { 1, 11, 20, 28 };
- Colors = {
- Good = colors.lime;
- Low = colors.orange;
- Critical = colors.red;
- }
- }
- -- --------------------------------------------------------------------------
- -- Variables
- -- --------------------------------------------------------------------------
- Timer = {};
- -- --------------------------------------------------------------------------
- -- Methods
- -- --------------------------------------------------------------------------
- function GetFlag(data, flag)
- return bit.band(data, flag) ~= 0;
- end
- -- --------------------------------------------------------------------------
- function GetNumToSend(mbPerBucket, isCritical)
- local target = MB_LOW;
- if (isCritical) then
- target = MB_CRITICAL;
- end
- local count = target / mbPerBucket;
- return math.ceil(count);
- end
- -- --------------------------------------------------------------------------
- function Pulse(channel, count)
- for i = 1, count do
- rs.setBundledOutput(OUTPUT, channel);
- os.sleep(0.1);
- rs.setBundledOutput(OUTPUT, 0);
- os.sleep(0.1);
- end
- end
- -- --------------------------------------------------------------------------
- function SetDelay(id)
- Timer[id] = os.clock() + PROCESS_TIME;
- end
- -- --------------------------------------------------------------------------
- function MON_Clear()
- for i = 1, #Monitors do
- local m = Monitors[i];
- m.clear();
- m.setTextColor(colors.white);
- end
- end
- -- --------------------------------------------------------------------------
- function MON_Write(text, x, y, color)
- color = color or colors.white;
- x = x or 1;
- y = y or 1;
- for i = 1, #Monitors do
- local m = Monitors[i];
- m.setCursorPos(x, y);
- m.setTextColor(color);
- m.write(text);
- end
- end
- -- --------------------------------------------------------------------------
- function UpdateMonitor(monitorData)
- MON_Clear();
- for i, data in ipairs(monitorData) do
- -- Col 1: Name
- MON_Write(data.Name, MonData.Columns[1], i, data.Color);
- -- Col 2: Status
- MON_Write(data.Status, MonData.Columns[2], i, MonData.Colors[data.Status]);
- -- Col 3: Stock
- local text = "Stocked";
- local color = MonData.Colors.Good;
- if (not data.HasStock) then
- text = "Empty";
- color = MonData.Colors.Critical;
- end
- MON_Write(text, MonData.Columns[3], i, color);
- -- Col 4: Processing Time
- if(data.Time > 0) then
- MON_Write(string.format("%d", data.Time), MonData.Columns[4], i, colors.white);
- end
- end
- end
- -- --------------------------------------------------------------------------
- -- Program
- -- --------------------------------------------------------------------------
- while true do
- local raw = rs.getBundledInput(INPUT);
- local drawNewLine = true;
- local monitorData = {};
- for i, data in ipairs(Data) do
- local isLow = GetFlag(raw, data.Low);
- local isCritical = GetFlag(raw, data.Critical);
- local hasStock = GetFlag(raw, data.Item);
- local time = os.clock();
- local waitTilTime = Timer[data.Name] or 0;
- if (isLow and (time >= waitTilTime))then
- if (drawNewLine) then
- print("\n------------------------------------------------")
- drawNewLine = false;
- end
- if (hasStock) then
- local count = GetNumToSend(data.mbPerItem, isCritical);
- print(string.format("[%d] Sending %d %s", time, count, data.Name));
- Pulse(data.Output, count);
- SetDelay(data.Name);
- else
- print(string.format("[%d] %s is low but Items are not available!", time, data.Name))
- SetDelay(data.Name);
- end
- end
- table.insert(monitorData, {
- Name = data.Name;
- Color = data.Color;
- Status = (isCritical and Status.Critical) or (isLow and Status.Low) or Status.Good;
- HasStock = hasStock;
- Time = waitTilTime - time;
- });
- end
- UpdateMonitor(monitorData);
- os.sleep(POLL);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement