Advertisement
Vodka51200

affichemonbase

Jul 9th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.65 KB | None | 0 0
  1. --[[
  2.     Base Monitor 1.3
  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.2
  13.     Changed the way ME systems are processed as it was very slow.
  14.     Added filtering so that certain names of items can be excluded.
  15.     This was again a problem on ME systems with a lot of items in them.
  16.     Changes since 1.1
  17.     Fixed a bug with tanks. Used to check if getInventoryName existed to determine if the
  18.     peripheral is a tank or not. Now using the getType function instead.
  19.     Changed the string.match to string.find
  20.     http://youtu.be/KT-2hKjUpGA
  21. --]]
  22. -- Below are values you can change
  23. local isMain = true;
  24. --local id = os.computerID();
  25. local id = "Main Base";
  26.  
  27. local VersionInfo = "Base Monitor 1.3";
  28.  
  29. local titleTextColor = colors.blue;
  30. local titleBackColor = colors.white;
  31. local tankTextColor = colors.black;
  32. local tankBackColor = colors.lime;
  33. local chestTextColor = colors.white;
  34. local chestBackColor = colors.purple;
  35. local cacheTextColor = colors.white;
  36. local cacheBackColor = colors.cyan;
  37. local powerTextColor = colors.black;
  38. local powerBackColor = colors.orange;
  39.  
  40. --[[
  41.     Add or remove values here to exclude items from being displayed.
  42.     It is case insensitive and it will look for matches.
  43.     So an item called "Nether Brick" will not be displayed if any of the following is set
  44.     "Brick","brick","rick","ck"
  45.     This also means that of you set a filter call "iron" then it will remove all items containing iron.
  46.     it will not show Iron Ore, Iron Block, Iron Helmet, etc.
  47. --]]
  48. local filterexclude = {"Ore","Brick","Flesh","Ingot","Pipe","Nugget","Brain","Steel","Drac","Thaum","Sapling","Seed","Cable","Helmet","Chestplat","Boots","Pants","Legging","Glass","Conduit","duct","Stone","Cent","Dollar","Lamp","wood","Crys","config","honey","qua","energy","cell","carpent","fruit","obsid","paper","frog","avoc","crim","nut","berry","raw","cooked","mush","broc","door","card","garden","block","clay","cucum","tea","egg","lettuce","disc","sword","table","tomato","mango","durian","corn","cherry","book","apple","sprout","light","sugar","slime","plate","gate","bar","soul","frame","spawner","fluid","eye","armor","snow","leather","bean","bone","shard","log","tree","and","oats","end","ton","iron"};
  49.  
  50. local NameLen = 18;
  51. local mainChannel = 2;
  52.  
  53. -- Above are values you can change
  54.  
  55. print("Starting "..VersionInfo);
  56. local peripherals = peripheral.getNames();
  57. local mon;
  58. local wmod;
  59. local x,y;
  60. local CurColumn = 0;
  61. local MaxColumn;
  62. local ColumnWidth;
  63. local CurLine = 2;
  64. local ContentData = {};
  65.  
  66. function padString (sText, iLen)
  67.     local iTextLen = string.len(sText);
  68.     -- Too short, pad
  69.     if (iTextLen < iLen) then
  70.         local iDiff = iLen - iTextLen;
  71.         return(sText..string.rep(" ",iDiff));
  72.     end
  73.     -- Too long, trim
  74.     if (iTextLen > iLen) then
  75.         return(string.sub(sText,1,iLen));
  76.     end
  77.     -- Exact length
  78.     return(sText);
  79. end
  80.  
  81. function prepmonitor(objectmon)
  82.     mon = peripheral.wrap(objectmon);
  83.     if (mon.isColor() == false) then
  84.         titleTextColor = colors.black;
  85.         titleBackColor = colors.white;
  86.         tankTextColor = colors.black;
  87.         tankBackColor = colors.white;
  88.         chestTextColor = colors.black;
  89.         chestBackColor = colors.white;
  90.         cacheTextColor = colors.black;
  91.         cacheBackColor = colors.white;
  92.         powerTextColor = colors.black;
  93.         powerBackColor = colors.white;
  94.     end
  95. end
  96.  
  97. function updateTable(strSource,strName,strAmount,strMax,strLegend)
  98.     for l,filter in pairs(filterexclude) do
  99.         if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  100.             return;
  101.         end
  102.     end
  103.     if (ContentData[strSource] == nil) then
  104.         ContentData[strSource] = {};
  105.     end
  106.     ContentData[strSource]["displayname"] = strName;
  107.     if (ContentData[strSource]["count"] == nil) then
  108.         ContentData[strSource]["count"] = strAmount;
  109.     else
  110.         ContentData[strSource]["count"] = ContentData[strSource]["count"] + strAmount;
  111.     end
  112.    
  113.     ContentData[strSource]["max"] = strMax;
  114.     ContentData[strSource]["legend"] = strLegend;
  115. end
  116.  
  117. function printmon(strName,strAmount,strMax,strLegend)
  118.     local textColor;
  119.     local backColor;
  120.     if (strLegend == "#") then
  121.         textColor = chestTextColor;
  122.         backColor = chestBackColor;
  123.         strLegend = "";
  124.     end
  125.     if (strLegend == "+") then
  126.         textColor = tankTextColor;
  127.         backColor = tankBackColor;
  128.         strLegend = "";
  129.     end
  130.     if (strLegend == "*") then
  131.         textColor = powerTextColor;
  132.         backColor = powerBackColor;
  133.         strLegend = "";
  134.     end
  135.     if (strLegend == "$") then
  136.         textColor = cacheTextColor;
  137.         backColor = cacheBackColor;
  138.         strLegend = "";
  139.     end
  140.     local line = string.format("%s  %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
  141.     if (strAmount >= 1000000) then
  142.         line = string.format("%s  %3iM%s",padString(strName,NameLen),math.floor(strAmount/1000000),padString(strLegend,1));
  143.     elseif (strAmount >= 1000) then
  144.         line = string.format("%s  %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1));
  145.     end
  146.  
  147.     local ColPadding = 0;
  148.     if (CurColumn > 0) then
  149.         ColPadding = 1;
  150.     end
  151.     local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
  152.  if (CurColumn == 0) then
  153.     --  print("CurX:"..CurX);
  154.  end
  155.     mon.setCursorPos(CurX,CurLine);
  156.     local percent = strAmount / strMax * 100;
  157.     mon.setBackgroundColor(backColor);
  158.     if (strMax == 0) then
  159.         percent = 0;
  160.     end
  161.     local barlength = math.floor(percent / 100 * (string.len(line)-1));
  162.  --if (CurColumn == 0) then
  163.    barlength = barlength + 1;
  164.  --end
  165.  
  166.     if (string.len(line) > barlength) then
  167.         local msg = string.sub(line,1,barlength);
  168.         mon.setTextColor(textColor);
  169.         mon.write(msg);
  170.         --[[if (percent == 0) then
  171.             mon.setBackgroundColor();
  172.         else
  173.             mon.setBackgroundColor(colors.black);
  174.         end--]]
  175.         mon.setBackgroundColor(colors.black);
  176.         mon.setTextColor(backColor);
  177.         mon.write(string.sub(line,barlength+1,-2))
  178.     else
  179.         local spaces = barlength - string.len(line);
  180.         mon.write(line);
  181.         mon.write(string.rep(" ",spaces));
  182.     end
  183.  
  184.     mon.setTextColor(colors.white);
  185.     CurColumn = CurColumn + 1;
  186.     if (CurColumn > MaxColumn) then
  187.         CurColumn = 0;
  188.         CurLine = CurLine + 1;
  189.     end
  190.     return true;
  191. end
  192.  
  193. -- Find a monitor
  194. function findMonitor()
  195.     for i,name in pairs(peripherals) do
  196.         for j,method in pairs(peripheral.getMethods(name)) do
  197.             if (method == 'getCursorPos') then
  198.                 prepmonitor(name);
  199.             end
  200.         end
  201.     end
  202. end
  203.  
  204. -- Find a wireless modem
  205. function findWirelessModem()
  206.     local foundWireless = false;
  207.     for i,name in pairs(peripherals) do
  208.         for j,method in pairs(peripheral.getMethods(name)) do
  209.             if (method == 'isWireless') then
  210.                 wmod = peripheral.wrap(name);
  211.                 if (wmod.isWireless()) then
  212.                     wmod.closeAll();
  213.                     foundWireless = true;
  214.                     break;
  215.                 else
  216.                     wmod = {};
  217.                 end
  218.             end
  219.         end
  220.         if (foundWireless) then
  221.             break;
  222.         end
  223.     end
  224. end
  225.  
  226. function collectLocalInfo()
  227.     ContentData = {};
  228.     for i,name in pairs(peripherals) do
  229.         local isTank = false;
  230.         local isCache = false;
  231.         local isChest = false;
  232.         local isCell = false;
  233.         local isMESystem = false;
  234.         local displayNames = {};
  235.         for j,method in pairs(peripheral.getMethods(name)) do
  236.             if (method == 'getTankInfo') then  
  237.                 isTank = true;
  238.                 if ( (string.find(peripheral.getType(name),"tank") == -1) and (string.find(peripheral.getType(name),"drum") == -1)) then
  239.                     isTank = false;
  240.                     isCell = false;
  241.                     isChest = false;
  242.                     isMESystem = true;
  243.                 end
  244.                 break;
  245.             end
  246.             if (method == 'getStoredItems') then
  247.                 isCache = true;
  248.                 isChest = false;
  249.                 break;
  250.             end
  251.             if (method == 'getMaxEnergyStored') then
  252.                 isCell = true;
  253.                 isChest = false;
  254.                 if (string.match(peripheral.call(name,"getInventoryName"),"cache") == "cache") then
  255.                     isCell = false;
  256.                     isChest = false;
  257.                     isCache = true;
  258.                 end
  259.                 if (string.match(peripheral.call(name,"getInventoryName"),"Chest") == "Chest") then
  260.                     isCell = false;
  261.                     isChest = false;
  262.                     isMESystem = true;
  263.                 end
  264.                 break;
  265.             end
  266.             if ((method == 'getStackInSlot') and (not isCache)) then
  267.                 isChest = true;
  268.                 if (string.match(peripheral.call(name,"getInventoryName"),"Drive") == "Drive") then
  269.                     isChest = false;
  270.                     isMESystem = true;
  271.                 end
  272.             end
  273.         end
  274.         if (isTank) then
  275.             print("Processing Tank");
  276.             local p = peripheral.wrap(name);
  277.             local iteminfo = p.getTankInfo();
  278.             local maxItems = iteminfo[1].capacity;
  279.             maxItems = math.floor(maxItems/1000);
  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.             updateTable(id.."-"..name,displayname,amount,maxItems,"+");
  288.         end
  289.         if (isCache) then
  290.             print("Processing Cache");
  291.             local p = peripheral.wrap(name);
  292.             local iteminfo = p.getStoredItems();
  293.             if (iteminfo) then
  294.                 local maxItems = p.getMaxStoredItems();
  295.                 local displayname = iteminfo.display_name;
  296.                 updateTable(id.."-"..name,displayname,iteminfo.qty,maxItems,"$");
  297.             end
  298.         end
  299.         if (isCell) then
  300.             print("Processing Cell");
  301.             local p = peripheral.wrap(name);
  302.             local energy = p.getEnergyStored();
  303.             local maxEnergy = p.getMaxEnergyStored();
  304.             local percent = (energy/maxEnergy*100);
  305.             updateTable(id.."-"..name,"Power Cell",energy,maxEnergy,"*");
  306.         end
  307.         if (isChest) then
  308.             print("Processing Chest");
  309.             local p = peripheral.wrap(name);
  310.             local chestSize = p.getInventorySize();
  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.."-"..name.."_"..k,key,val,0,"#");
  329.             end
  330.         end
  331.         if (isMESystem) then
  332.             print("Processing ME System");
  333.             local p = peripheral.wrap(name);
  334.             local itemarray = p.getAvailableItems();
  335.             for j=1,table.getn(itemarray),1 do
  336.                 if (itemarray[j].size > 0) then
  337.                     local fingerprint = itemarray[j].fingerprint;
  338.                     if ( (displayNames[fingerprint.id] == nil) or (displayNames[fingerprint.id] == fingerprint.id)) then
  339.                         local ItemDetail = p.getItemDetail(fingerprint);
  340.                         if (ItemDetail ~= nil) then
  341.                             displayNames[fingerprint.id] = ItemDetail.basic().display_name;
  342.                         else
  343.                             displayNames[fingerprint.id] = fingerprint.id;
  344.                         end
  345.                     end
  346.                     updateTable(id.."-"..name.."_"..fingerprint.id,displayNames[fingerprint.id],itemarray[j].size,0,"#");
  347.                 end
  348.             end
  349.         end
  350.         print("Done");
  351.     end
  352. end
  353.  
  354. function updateMonitor()
  355.     x,y = mon.getSize();
  356.     ColumnWidth = NameLen + 7;
  357.     MaxColumn = math.floor(x / (ColumnWidth))-1;
  358.     mon.setBackgroundColor(colors.black);
  359.     mon.clear();
  360.     CurColumn = 0;
  361.     CurLine = 1;
  362.     mon.setCursorPos(1,1);
  363.     mon.setTextColor(colors.white);
  364.     mon.setTextScale(0.5);
  365.     mon.write(VersionInfo);
  366.     local sorted = {};
  367.     for n in pairs(ContentData) do
  368.         table.insert(sorted, n);
  369.     end
  370.     table.sort(sorted);
  371.     local name = "";
  372.     for i,source in ipairs(sorted) do
  373.         local curname = string.sub(source,1,string.find(source,"-")-1);
  374.         if (name ~= curname) then
  375.             name = curname;
  376.             CurColumn = 0;
  377.             mon.setTextColor(titleTextColor);
  378.             mon.setBackgroundColor(titleBackColor);
  379.             mon.setCursorPos(1,CurLine+1);
  380.             mon.write(padString("Contents for "..name,x-1));
  381.             CurLine = CurLine + 2;
  382.         end
  383.         displayname = ContentData[source]["displayname"];
  384.         count = ContentData[source]["count"];
  385.         max = ContentData[source]["max"];
  386.         legend = ContentData[source]["legend"];
  387.         printmon(displayname,count,max,legend);
  388.     end
  389. end
  390. -- This is the main section of the script
  391.  
  392. findMonitor();
  393. findWirelessModem();
  394.  
  395. if (isMain == true) then
  396.     if (type(wmod.isWireless) == "function") then
  397.         wmod.open(mainChannel);
  398.     else
  399.         print("You don't have a wireless modem, and this is set as the main computer");
  400.     end
  401. end
  402.  
  403. ContentData = {};
  404.  
  405. local timerUpdate = os.startTimer(60);
  406. local updateCount = 0;
  407. local wirelessEventCount = 0;
  408. -- Perform Initial Collection and Update the Monitor
  409. collectLocalInfo();
  410. updateMonitor();
  411.  
  412. while true do  
  413.     local event, param1, param2, param3, param4, param5 = os.pullEvent();
  414.     print("Received event:"..event);
  415.     if (event == "timer") then
  416.         if (param1 == timerUpdate) then
  417.             updateCount = updateCount + 1;
  418.             if (updateCount >= 3) then
  419.                 updateCount = 0;
  420.                 ContentData = {};
  421.             end
  422.             collectLocalInfo();
  423.             if (wmod) then
  424.                 if (isMain == false) then
  425.                     wmod.transmit(mainChannel,1,ContentData);
  426.                 end
  427.             end
  428.             if (updateCount == 2) then
  429.                 updateMonitor();
  430.             end
  431.             wirelessEventCount = 0;
  432.             timerUpdate = os.startTimer(5);
  433.         end
  434.     end
  435.     if (event == "modem_message") then
  436.         if (isMain == true) then
  437.             wirelessEventCount = wirelessEventCount + 1;
  438.             for source,data in pairs(param4) do
  439.                 if (ContentData[source] == nil) then
  440.                     ContentData[source] = {};
  441.                 end
  442.                 ContentData[source]["displayname"] = param4[source]["displayname"];
  443.                 ContentData[source]["count"] = param4[source]["count"];
  444.                 ContentData[source]["max"] = param4[source]["max"];
  445.                 ContentData[source]["legend"] = param4[source]["legend"];
  446.             end
  447.             if (wirelessEventCount >= 10) then
  448.                 timerUpdate = os.startTimer(1);
  449.             end
  450.         end
  451.     end
  452.     if (event == "monitor_touch") or (event == "monitor_resize") then
  453.         print("Updating the Monitor");
  454.         updateMonitor();
  455.     end
  456.     if (event == "peripheral") or (event == "peripheral_detach") then
  457.         print("Updating the peripheral list");
  458.         peripherals = peripheral.getNames();
  459.     end
  460. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement