Advertisement
Oualembo

azarahapplied

Apr 9th, 2021
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.39 KB | None | 0 0
  1. --[[
  2.     Base Monitor 1.7
  3.         This script allows you to monitor caches, tanks, barrels and chests
  4.         Monitoring chests requires an open peripheral proxy, the same goes for
  5.         buildcraft tanks.
  6.         You can build one of more computers and set one of them as the main server by
  7.         setting isMain to true. This will let it receive information from other computers.
  8.         For the main server to receive info from other computers you need to attach a
  9.         wireless modem to each computer.
  10.         Peripherals can be attached to any side you like, the script will automatically
  11.         work out where the peripherals are.
  12.     Changes since 1.6
  13.         Made significant changed to how peripherals are checked and processed.
  14.         Redesigned how the contents is processed and stored so that there is a timestamp.
  15.         The timestamp allows remote computer information to be processed correctly.
  16.         Each computer must have a unique id set, if the same id is used for two systems,
  17.         then the last one to update the table will win and you won't see all the content.
  18.         Added ItemsFullPercentAt and FluidFullPercentAt to set what level is considered full.
  19.     Changes since 1.5
  20.         Added a workaround to ignore BuildCraft pipes if attached
  21.     Changes since 1.4
  22.         Added support for PeripheralPlusOne on Minecraft 1.12.2
  23.         Currently only supports ME Systems via the ME Bridge
  24.     Changes since 1.3
  25.         Changed filterexclude to blacklist and added whitelist
  26.     Changes since 1.2
  27.         Changed the way ME systems are processed as it was very slow.
  28.         Added filtering so that certain names of items can be excluded.
  29.         This was again a problem on ME systems with a lot of items in them.
  30.     Changes since 1.1
  31.         Fixed a bug with tanks. Used to check if getInventoryName existed to determine if the
  32.         peripheral is a tank or not. Now using the getType function instead.
  33.         Changed the string.match to string.find
  34.     http://youtu.be/KT-2hKjUpGA
  35. --]]
  36. -- Below are values you can change
  37. local isMain = true;
  38. local id = "Main Base";
  39. local UpdateIntervalSeconds = 60;
  40. local ItemsFullPercentAt = 256000;
  41. local FluidFullPercentAt = 64000;
  42.  
  43. local VersionInfo = "Azarah stworzyl to dzielo";
  44.  
  45. local titleTextColor = colors.blue;
  46. local titleBackColor = colors.white;
  47. local tankTextColor = colors.black;
  48. local tankBackColor = colors.lime;
  49. local chestTextColor = colors.white;
  50. local chestBackColor = colors.purple;
  51. local cacheTextColor = colors.white;
  52. local cacheBackColor = colors.cyan;
  53. local powerTextColor = colors.black;
  54. local powerBackColor = colors.orange;
  55.  
  56. --[[
  57.     Add or remove values here to exclude items from being displayed.
  58.     It is case insensitive and it will look for matches.
  59.     So an item called "Nether Brick" will not be displayed if any of the following is set
  60.     "Brick","brick","rick","ck"
  61.     This also means that of you set a filter call "iron" then it will remove all items containing iron.
  62.     it will not show Iron Ore, Iron Block, Iron Helmet, etc.
  63. --]]
  64. local blacklist = {"Flesh","Nugget","Brain","Sapling","Seed","egg","eye"};
  65. --[[
  66.     Add or remove values here to only show items matching these names.
  67.     It is case insensitive and it will look for matches.
  68.     So an item called "Nether Brick" will be displayed if any of the following is set
  69.     "Brick","brick","rick","ck"
  70.     This also means that of you set a filter called "iron" then it will show all items containing iron.
  71.     it will show show Iron Ore, Iron Block, Iron Helmet, etc.
  72.     To disable the whitelist set it to local whitelist = "";
  73.     To enable the whitelist set it to local whitelist = {"Diamond","Gold"};
  74. --]]
  75. local whitelist = "";
  76.  
  77. local NameLen = 18;
  78. local mainChannel = 2;
  79.  
  80. -- Above are values you can change
  81.  
  82. print("Starting "..VersionInfo);
  83. local peripherals = peripheral.getNames();
  84. local mon;
  85. local wmod;
  86. local x,y;
  87. local CurColumn = 0;
  88. local MaxColumn;
  89. local ColumnWidth;
  90. local CurLine = 2;
  91. local ContentData = {};
  92.  
  93. function padString (sText, iLen)
  94.     local iTextLen = string.len(sText);
  95.     -- Too short, pad
  96.     if (iTextLen < iLen) then
  97.         local iDiff = iLen - iTextLen;
  98.         return(sText..string.rep(" ",iDiff));
  99.     end
  100.     -- Too long, trim
  101.     if (iTextLen > iLen) then
  102.         return(string.sub(sText,1,iLen));
  103.     end
  104.     -- Exact length
  105.     return(sText);
  106. end
  107.  
  108. function prepmonitor(objectmon)
  109.     mon = peripheral.wrap(objectmon);
  110.     if (mon.isColor() == false) then
  111.         titleTextColor = colors.black;
  112.         titleBackColor = colors.white;
  113.         tankTextColor = colors.black;
  114.         tankBackColor = colors.white;
  115.         chestTextColor = colors.black;
  116.         chestBackColor = colors.white;
  117.         cacheTextColor = colors.black;
  118.         cacheBackColor = colors.white;
  119.         powerTextColor = colors.black;
  120.         powerBackColor = colors.white;
  121.     end
  122. end
  123.  
  124. function updateTable(strSource,strName,strAmount,timestamp,strLegend)
  125.     local isWhitelisted = true;
  126.     if (type(whitelist) == "table") then
  127.         isWhitelisted = false;
  128.         for l,filter in pairs(whitelist) do
  129.             if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  130.                 isWhitelisted = true;
  131.             end
  132.         end
  133.     end
  134.     if (isWhitelisted == false) then
  135.         return;
  136.     end
  137.     if (type(blacklist) == "table") then
  138.         for l,filter in pairs(blacklist) do
  139.             if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  140.                 return;
  141.             end
  142.         end
  143.     end
  144.     if (ContentData[strSource] == nil) then
  145.         ContentData[strSource] = {};
  146.     end
  147.     if (ContentData[strSource][timestamp] == nil) then
  148.         ContentData[strSource][timestamp] = {};
  149.     end
  150.     if (ContentData[strSource][timestamp][strName] == nil) then
  151.         ContentData[strSource][timestamp][strName] = {};
  152.     end
  153.     if (ContentData[strSource][timestamp][strName]["count"] == nil) then
  154.         ContentData[strSource][timestamp][strName]["count"] = strAmount;
  155.     else
  156.         ContentData[strSource][timestamp][strName]["count"] = ContentData[strSource][timestamp][strName]["count"] + strAmount;
  157.     end
  158.     ContentData[strSource][timestamp][strName]["legend"] = strLegend;
  159. end
  160.  
  161. function printmon(strName,strAmount,strMax,strLegend)
  162.     local textColor;
  163.     local backColor;
  164.     local FullPercentAt = ItemsFullPercentAt;
  165.     if (strLegend == "#") then
  166.         textColor = chestTextColor;
  167.         backColor = chestBackColor;
  168.         strLegend = "";
  169.     end
  170.     if (strLegend == "+") then
  171.         textColor = tankTextColor;
  172.         backColor = tankBackColor;
  173.         FullPercentAt = FluidFullPercentAt;
  174.         strLegend = "";
  175.     end
  176.     if (strLegend == "*") then
  177.         textColor = powerTextColor;
  178.         backColor = powerBackColor;
  179.         strLegend = "";
  180.     end
  181.     if (strLegend == "$") then
  182.         textColor = cacheTextColor;
  183.         backColor = cacheBackColor;
  184.         strLegend = "";
  185.     end
  186.     local line = string.format("%s  %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
  187.     if (strAmount >= 1000000) then
  188.         line = string.format("%s  %3iM%s",padString(strName,NameLen),math.floor(strAmount/1000000),padString(strLegend,1));
  189.     elseif (strAmount >= 1000) then
  190.         line = string.format("%s  %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1));
  191.     end
  192.  
  193.     local ColPadding = 0;
  194.     if (CurColumn > 0) then
  195.         ColPadding = 1;
  196.     end
  197.     local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
  198.     if (CurColumn == 0) then
  199.         --  print("CurX:"..CurX);
  200.     end
  201.     mon.setCursorPos(CurX,CurLine);
  202.     --local percent = strAmount / strMax * 100;
  203.     mon.setBackgroundColor(backColor);
  204.     local percent = strAmount / FullPercentAt * 100;
  205.     if (percent > 100) then percent = 100; end
  206.     local barlength = math.floor(percent / 100 * (string.len(line)-1));
  207.     --if (CurColumn == 0) then
  208.     --  barlength = barlength + 1;
  209.     --end
  210.  
  211.     if (string.len(line) > barlength) then
  212.         local msg = string.sub(line,1,barlength);
  213.         mon.setTextColor(textColor);
  214.         mon.write(msg);
  215.         --[[if (percent == 0) then
  216.             mon.setBackgroundColor();
  217.         else
  218.             mon.setBackgroundColor(colors.black);
  219.         end--]]
  220.         mon.setBackgroundColor(colors.black);
  221.         mon.setTextColor(backColor);
  222.         mon.write(string.sub(line,barlength+1,-2))
  223.     else
  224.         local spaces = barlength - string.len(line);
  225.         mon.write(line);
  226.         mon.write(string.rep(" ",spaces));
  227.     end
  228.  
  229.     mon.setTextColor(colors.white);
  230.     CurColumn = CurColumn + 1;
  231.     if (CurColumn > MaxColumn) then
  232.         CurColumn = 0;
  233.         CurLine = CurLine + 1;
  234.     end
  235.     return true;
  236. end
  237.  
  238. -- Find a monitor
  239. function findMonitor()
  240.     for i,name in pairs(peripherals) do
  241.         for j,method in pairs(peripheral.getMethods(name)) do
  242.             if (method == 'getCursorPos') then
  243.                 prepmonitor(name);
  244.             end
  245.         end
  246.     end
  247. end
  248.  
  249. -- Find a wireless modem
  250. function findWirelessModem()
  251.     local foundWireless = false;
  252.     for i,name in pairs(peripherals) do
  253.         for j,method in pairs(peripheral.getMethods(name)) do
  254.             if (method == 'isWireless') then
  255.                 wmod = peripheral.wrap(name);
  256.                 if (wmod.isWireless()) then
  257.                     wmod.closeAll();
  258.                     foundWireless = true;
  259.                     break;
  260.                 else
  261.                     wmod = {};
  262.                 end
  263.             end
  264.         end
  265.         if (foundWireless) then
  266.             break;
  267.         end
  268.     end
  269. end
  270.  
  271. function collectLocalInfo()
  272.     local timestamp = os.clock();
  273.     for i,name in pairs(peripherals) do
  274.         local p = peripheral.wrap(name);
  275.         local displayNames = {};
  276.         if (p.getTankInfo ~= nil) then
  277.             print("Processing Tank");
  278.             local iteminfo = p.getTankInfo();
  279.             if (iteminfo ~= nil) then
  280.                 local displayname = "Empty";
  281.                 local amount = 0;
  282.                 if (iteminfo[1].contents) then
  283.                     displayname = iteminfo[1].contents.rawName;
  284.                     amount = iteminfo[1].contents.amount;
  285.                     amount = math.floor(amount/1000);
  286.                 end
  287.                 if (amount ~= 0) then
  288.                     updateTable(id,displayname,amount,timestamp,"+");
  289.                 end
  290.             end
  291.         end
  292.         if (p.getStoredItems ~= nil) then
  293.             print("Processing Cache");
  294.             local iteminfo = p.getStoredItems();
  295.             if (iteminfo) then
  296.                 local displayname = iteminfo.display_name;
  297.                 updateTable(id,displayname,iteminfo.qty,timestamp,"$");
  298.             end
  299.         end
  300.         if (p.getEnergyStored ~= nil) then
  301.             print("Processing Energy");
  302.             local energy = p.getEnergyStored();
  303.             if (energy ~= nil) then
  304.                 updateTable(id,"Energy",energy,timestamp,"*");
  305.             end
  306.         end
  307.         if (p.getInventorySize ~= nil) then
  308.             print("Processing Chest");
  309.             local chestSize = p.getInventorySize();
  310.             if (chestSize ~= nil) then
  311.                 local items = {};
  312.                 for j=1,chestSize,1 do
  313.                     local iteminfo = p.getStackInSlot(j);
  314.                     if (iteminfo) then
  315.                         displayname = iteminfo.display_name;
  316.                         if (displayname) then
  317.                             if (not items[displayname]) then
  318.                                 items[displayname] = iteminfo.qty;
  319.                             else
  320.                                 items[displayname] = items[displayname] + iteminfo.qty;
  321.                             end
  322.                         end
  323.                     end
  324.                 end
  325.                 local k = 0;
  326.                 for key,val in pairs(items) do
  327.                     k = k + 1;
  328.                     updateTable(id,key,val,timestamp,"#");
  329.                 end
  330.             end
  331.         end
  332.         if (p.getAvailableItems ~= nil) then
  333.             print("Processing ME System");
  334.             local itemarray = p.getAvailableItems();
  335.             if (itemarray ~= nil) then
  336.                 for j=1,table.getn(itemarray),1 do
  337.                     if (itemarray[j].size > 0) then
  338.                         local fingerprint = itemarray[j].fingerprint;
  339.                         if ( (displayNames[fingerprint.id] == nil) or (displayNames[fingerprint.id] == fingerprint.id)) then
  340.                             local ItemDetail = p.getItemDetail(fingerprint);
  341.                             if (ItemDetail ~= nil) then
  342.                                 displayNames[fingerprint.id] = ItemDetail.basic().display_name;
  343.                             else
  344.                                 displayNames[fingerprint.id] = fingerprint.id;
  345.                             end
  346.                         end
  347.                         updateTable(id,displayNames[fingerprint.id],itemarray[j].size,timestamp,"#");
  348.                     end
  349.                 end
  350.             end
  351.         end
  352.         if (p.listItems ~= nil) then
  353.             print("Processing ME Bridge");
  354.             local itemarray = p.listItems();
  355.             if (itemarray ~= nil) then
  356.                 for j=1,table.getn(itemarray),1 do
  357.                     if (itemarray[j].amount > 0) then
  358.                         local fingerprint = itemarray[j].name;
  359.                         local displayName = itemarray[j].displayName;
  360.                         updateTable(id,displayName,itemarray[j].amount,timestamp,"#");
  361.                     end
  362.                 end
  363.             end
  364.         end
  365.         print("Done");
  366.     end
  367. end
  368.  
  369. function updateMonitor()
  370.     x,y = mon.getSize();
  371.     ColumnWidth = NameLen + 7;
  372.     MaxColumn = math.floor(x / (ColumnWidth))-1;
  373.     mon.setBackgroundColor(colors.black);
  374.     mon.clear();
  375.     CurColumn = 0;
  376.     CurLine = 1;
  377.     mon.setCursorPos(1,1);
  378.     mon.setTextColor(colors.white);
  379.     mon.setTextScale(0.5);
  380.     mon.write(VersionInfo);
  381.     -- Sort by Base names
  382.     local sortedSources = {};
  383.     for n in pairs(ContentData) do
  384.         table.insert(sortedSources, n);
  385.     end
  386.     table.sort(sortedSources);
  387.     local name = "";
  388.     for i,source in ipairs(sortedSources) do
  389.         if (name ~= source) then
  390.             name = source;
  391.             CurColumn = 0;
  392.             mon.setTextColor(titleTextColor);
  393.             mon.setBackgroundColor(titleBackColor);
  394.             mon.setCursorPos(1,CurLine+1);
  395.             mon.write(padString("Contents for "..name,x-1));
  396.             CurLine = CurLine + 2;
  397.         end
  398.         sortedTimestamps = {};
  399.         for timestamp in pairs(ContentData[source]) do
  400.             table.insert(sortedTimestamps,timestamp);
  401.         end
  402.         table.sort(sortedTimestamps);
  403.         latest = nil;
  404.         for j=table.maxn(sortedTimestamps),1,-1 do
  405.             if (latest == nil) then
  406.                 latest = sortedTimestamps[j];
  407.             else
  408.                 ContentData[source][sortedTimestamps[j]] = nil;
  409.             end
  410.         end
  411.         for itemname in pairs(ContentData[source][latest]) do
  412.             displayname = itemname;
  413.             count = ContentData[source][latest][itemname]["count"];
  414.             legend = ContentData[source][latest][itemname]["legend"];
  415.             max = 0;
  416.             printmon(displayname,count,max,legend);
  417.         end
  418.     end
  419. end
  420. -- This is the main section of the script
  421.  
  422. findMonitor();
  423. findWirelessModem();
  424.  
  425. if (isMain == true) then
  426.     if (type(wmod.isWireless) == "function") then
  427.         wmod.open(mainChannel);
  428.     else
  429.         print("You don't have a wireless modem, and this is set as the main computer");
  430.     end
  431. end
  432.  
  433. ContentData = {};
  434.  
  435. local timerUpdate = os.startTimer(UpdateIntervalSeconds);
  436. local updateCount = 0;
  437. local wirelessEventCount = 0;
  438. -- Perform Initial Collection and Update the Monitor
  439. collectLocalInfo();
  440. updateMonitor();
  441.  
  442. -- Main program loop
  443. while true do  
  444.     local event, param1, param2, param3, param4, param5 = os.pullEvent();
  445.     print("Received event:"..event);
  446.     if (event == "timer") then
  447.         if (param1 == timerUpdate) then
  448.             collectLocalInfo();
  449.             if (wmod) then
  450.                 if (isMain == false) then
  451.                     wmod.transmit(mainChannel,1,ContentData);
  452.                 end
  453.             end
  454.             updateMonitor();
  455.             wirelessEventCount = 0;
  456.             timerUpdate = os.startTimer(UpdateIntervalSeconds);
  457.         end
  458.     end
  459.     if (event == "modem_message") then
  460.         if (isMain == true) then
  461.             wirelessEventCount = wirelessEventCount + 1;
  462.             for source,data in pairs(param4) do
  463.                 if (ContentData[source] == nil) then
  464.                     ContentData[source] = {};
  465.                 end
  466.                 ContentData[source] = param4[source];
  467.             end
  468.             if (wirelessEventCount >= 10) then
  469.                 timerUpdate = os.startTimer(1);
  470.             end
  471.         end
  472.     end
  473.     if (event == "monitor_touch") or (event == "monitor_resize") then
  474.         print("Updating the Monitor");
  475.         updateMonitor();
  476.     end
  477.     if (event == "peripheral") or (event == "peripheral_detach") then
  478.         print("Updating the peripheral list");
  479.         peripherals = peripheral.getNames();
  480.     end
  481. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement