Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Base Monitor 1.0
- This script allows you to monitor caches, tanks, barrels and chests
- Monitoring chests requires an open peripheral proxy, the same goes for
- buildcraft tanks.
- You can build one of more computers and set one of them as the main server by
- setting isMain to true. This will let it receive information from other computers.
- For the main server to receive info from other computers you need to attach a
- wireless modem to each computer.
- Peripherals can be attached to any side you like, the script will automatically
- work out where the peripherals are.
- http://youtu.be/KT-2hKjUpGA
- --]]
- -- Below are values you can change
- local isMain = false;
- --local id = os.computerID();
- local id = "The Base";
- local titleTextColor = colors.blue;
- local titleBackColor = colors.white;
- local tankTextColor = colors.black;
- local tankBackColor = colors.lime;
- local chestTextColor = colors.white;
- local chestBackColor = colors.purple;
- local cacheTextColor = colors.white;
- local cacheBackColor = colors.cyan;
- local powerTextColor = colors.black;
- local powerBackColor = colors.orange;
- local NameLen = 21;
- local mainChannel = 2;
- -- Above are values you can change
- local peripherals = peripheral.getNames();
- local mon;
- local wmod;
- local x,y;
- local CurColumn = 0;
- local MaxColumn;
- local ColumnWidth;
- local CurLine = 2;
- local ContentData = {};
- function padString (sText, iLen)
- local iTextLen = string.len(sText);
- -- Too short, pad
- if (iTextLen < iLen) then
- local iDiff = iLen - iTextLen;
- return(sText..string.rep(" ",iDiff));
- end
- -- Too long, trim
- if (iTextLen > iLen) then
- return(string.sub(sText,1,iLen));
- end
- -- Exact length
- return(sText);
- end
- function prepmonitor(objectmon)
- mon = peripheral.wrap(objectmon);
- if (mon.isColor() == false) then
- titleTextColor = colors.black;
- titleBackColor = colors.white;
- tankTextColor = colors.black;
- tankBackColor = colors.white;
- chestTextColor = colors.black;
- chestBackColor = colors.white;
- cacheTextColor = colors.black;
- cacheBackColor = colors.white;
- powerTextColor = colors.black;
- powerBackColor = colors.white;
- end
- end
- function updateTable(strSource,strName,strAmount,strMax,strLegend)
- ContentData[strSource] = {};
- ContentData[strSource]["displayname"] = strName;
- ContentData[strSource]["count"] = strAmount;
- ContentData[strSource]["max"] = strMax;
- ContentData[strSource]["legend"] = strLegend;
- end
- function printmon(strName,strAmount,strMax,strLegend)
- local textColor;
- local backColor;
- if (strLegend == "#") then
- textColor = chestTextColor;
- backColor = chestBackColor;
- strLegend = "";
- end
- if (strLegend == "+") then
- textColor = tankTextColor;
- backColor = tankBackColor;
- strLegend = "";
- end
- if (strLegend == "*") then
- textColor = powerTextColor;
- backColor = powerBackColor;
- strLegend = "";
- end
- if (strLegend == "$") then
- textColor = cacheTextColor;
- backColor = cacheBackColor;
- strLegend = "";
- end
- local line = string.format("%s %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
- if (strAmount >= 1000000) then
- line = string.format("%s %3iM%s",padString(strName,NameLen),math.floor(strAmount/1000000),padString(strLegend,1));
- elseif (strAmount >= 1000) then
- line = string.format("%s %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1));
- end
- local ColPadding = 0;
- if (CurColumn > 0) then
- ColPadding = 1;
- end
- local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
- --print("CurX:"..CurX);
- mon.setCursorPos(CurX,CurLine);
- local percent = strAmount / strMax * 100;
- mon.setBackgroundColor(backColor);
- if (strMax == 0) then
- percent = 0;
- end
- local barlength = math.floor(percent / 100 * (string.len(line)-1));
- if (string.len(line) > barlength) then
- local msg = string.sub(line,1,barlength);
- mon.setTextColor(textColor);
- mon.write(msg);
- --[[if (percent == 0) then
- mon.setBackgroundColor();
- else
- mon.setBackgroundColor(colors.black);
- end--]]
- mon.setBackgroundColor(colors.black);
- mon.setTextColor(backColor);
- mon.write(string.sub(line,barlength+1,-2))
- else
- local spaces = barlength - string.len(line);
- mon.write(line);
- mon.write(string.rep(" ",spaces));
- end
- mon.setTextColor(colors.white);
- CurColumn = CurColumn + 1;
- if (CurColumn > MaxColumn) then
- CurColumn = 0;
- CurLine = CurLine + 1;
- end
- return true;
- end
- -- Find a monitor
- function findMonitor()
- for i,name in pairs(peripherals) do
- for j,method in pairs(peripheral.getMethods(name)) do
- if (method == 'getCursorPos') then
- prepmonitor(name);
- end
- end
- end
- end
- -- Find a wireless modem
- function findWirelessModem()
- local foundWireless = false;
- for i,name in pairs(peripherals) do
- for j,method in pairs(peripheral.getMethods(name)) do
- if (method == 'isWireless') then
- wmod = peripheral.wrap(name);
- if (wmod.isWireless()) then
- wmod.closeAll();
- foundWireless = true;
- break;
- else
- wmod = {};
- end
- end
- end
- if (foundWireless) then
- break;
- end
- end
- end
- function collectLocalInfo()
- for i,name in pairs(peripherals) do
- local isTank = false;
- local isCache = false;
- local isChest = false;
- local isCell = false;
- for j,method in pairs(peripheral.getMethods(name)) do
- if (method == 'getTankInfo') then
- isTank = true;
- break;
- end
- if (method == 'getStoredItems') then
- isCache = true;
- isChest = false;
- break;
- end
- if (method == 'getMaxEnergyStored') then
- isCell = true;
- isChest = false;
- if (string.match(peripheral.call(name,"getInventoryName"),"cache") == "cache") then
- isCell = false;
- isChest = false;
- isCache = true;
- end
- break;
- end
- if ((method == 'getStackInSlot') and (not isCache)) then
- isChest = true;
- end
- end
- if (isTank) then
- local p = peripheral.wrap(name);
- local iteminfo = p.getTankInfo();
- local maxItems = iteminfo[1].capacity;
- maxItems = math.floor(maxItems/1000);
- local displayname = "Empty";
- local amount = 0;
- if (iteminfo[1].contents) then
- displayname = iteminfo[1].contents.rawName;
- amount = iteminfo[1].contents.amount;
- amount = math.floor(amount/1000);
- end
- --printmon(displayname,amount,maxItems,"+");
- updateTable(id.."-"..name,displayname,amount,maxItems,"+");
- end
- if (isCache) then
- local p = peripheral.wrap(name);
- local iteminfo = p.getStoredItems();
- if (iteminfo) then
- local maxItems = p.getMaxStoredItems();
- local displayname = iteminfo.display_name;
- --print(name..",".."cache,"..displayname..","..iteminfo.qty..","..maxItems);
- --printmon(displayname,iteminfo.qty,maxItems,"#");
- updateTable(id.."-"..name,displayname,iteminfo.qty,maxItems,"$");
- end
- end
- if (isCell) then
- local p = peripheral.wrap(name);
- local energy = p.getEnergyStored();
- local maxEnergy = p.getMaxEnergyStored();
- local percent = (energy/maxEnergy*100);
- --printmon("Power Cell",energy,maxEnergy,"*");
- updateTable(id.."-"..name,"Power Cell",energy,maxEnergy,"*");
- end
- if (isChest) then
- --print("is a Chest");
- local p = peripheral.wrap(name);
- local chestSize = p.getInventorySize();
- local items = {};
- for j=1,chestSize,1 do
- local iteminfo = p.getStackInSlot(j);
- if (iteminfo) then
- displayname = iteminfo.display_name;
- if (displayname) then
- if (not items[displayname]) then
- items[displayname] = iteminfo.qty;
- else
- items[displayname] = items[displayname] + iteminfo.qty;
- end
- end
- end
- end
- local k = 0;
- for key,val in pairs(items) do
- k = k + 1;
- --printmon(key,val,0,"#");
- updateTable(id.."-"..name.."_"..k,key,val,0,"#");
- end
- end
- end
- end
- function updateMonitor()
- x,y = mon.getSize();
- ColumnWidth = NameLen + 7;
- MaxColumn = math.floor(x / (ColumnWidth))-1;
- mon.setBackgroundColor(colors.black);
- mon.clear();
- CurColumn = 0;
- CurLine = 1;
- mon.setCursorPos(1,1);
- mon.setTextColor(colors.white);
- mon.setTextScale(0.5);
- mon.write("Base Monitor 1.0");
- local sorted = {};
- for n in pairs(ContentData) do
- table.insert(sorted, n);
- end
- table.sort(sorted);
- local name = "";
- for i,source in ipairs(sorted) do
- local curname = string.sub(string.match(source,"^.*-"),1,-2);
- if (name ~= curname) then
- name = curname;
- CurColumn = 0;
- mon.setTextColor(titleTextColor);
- mon.setBackgroundColor(titleBackColor);
- mon.setCursorPos(1,CurLine+1);
- mon.write(padString("Contents for "..name,x-1));
- CurLine = CurLine + 2;
- end
- displayname = ContentData[source]["displayname"];
- count = ContentData[source]["count"];
- max = ContentData[source]["max"];
- legend = ContentData[source]["legend"];
- printmon(displayname,count,max,legend);
- end
- end
- -- This is the main section of the script
- findMonitor();
- findWirelessModem();
- if (isMain == true) then
- if (wmod) then
- wmod.open(mainChannel);
- else
- print("You don't have a wireless modem, and this is set as the main computer");
- end
- end
- ContentData = {};
- local timerUpdate = os.startTimer(10);
- local updateCount = 0;
- local wirelessEventCount = 0;
- -- Perform Initial Collection and Update the Monitor
- collectLocalInfo();
- updateMonitor();
- while true do
- local event, param1, param2, param3, param4, param5 = os.pullEvent();
- print("Received event:"..event);
- if (event == "timer") then
- if (param1 == timerUpdate) then
- updateCount = updateCount + 1;
- if (updateCount >= 3) then
- updateCount = 0;
- ContentData = {};
- end
- collectLocalInfo();
- if (wmod) then
- if (isMain == false) then
- wmod.transmit(mainChannel,1,ContentData);
- end
- end
- if (updateCount == 2) then
- updateMonitor();
- end
- wirelessEventCount = 0;
- timerUpdate = os.startTimer(5);
- end
- end
- if (event == "modem_message") then
- if (isMain == true) then
- wirelessEventCount = wirelessEventCount + 1;
- for source,data in pairs(param4) do
- ContentData[source] = {};
- ContentData[source]["displayname"] = param4[source]["displayname"];
- ContentData[source]["count"] = param4[source]["count"];
- ContentData[source]["max"] = param4[source]["max"];
- ContentData[source]["legend"] = param4[source]["legend"];
- end
- if (wirelessEventCount >= 10) then
- timerUpdate = os.startTimer(1);
- end
- end
- end
- if (event == "monitor_touch") or (event == "monitor_resize") then
- print("Updating the Monitor");
- updateMonitor();
- end
- if (event == "peripheral") or (event == "peripheral_detach") then
- print("Updating the peripheral list");
- peripherals = peripheral.getNames();
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement