Hamsta

SE InGame Cago Capacity V2

Jan 14th, 2015
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.01 KB | None | 0 0
  1.         //Begin
  2.         List<String> Groups = new List<String>();
  3.  
  4.         void Main()
  5.         {
  6.             Groups.Add("Lageranzeige Lager 1");
  7.  
  8.             doDisplay();
  9.         }
  10.  
  11.         void doDisplay()
  12.         {
  13.             BMyInventoryCollectionShow show;
  14.             IMyBlockGroup BlockGroup;
  15.             IMyTerminalBlock display;
  16.             for (int i = 0; i < Groups.Count; i++)
  17.             {
  18.                     BlockGroup = getGroup(Groups[i]);
  19.                     if ((BlockGroup as IMyBlockGroup) != null)
  20.                     {
  21.                         display = getDisplayBlock(BlockGroup);
  22.                         if (isDisplayBlock(display))
  23.                         {
  24.                             display.SetCustomName(BlockGroup.Name);
  25.                             show = new BMyInventoryCollectionShow(new BMyInventoryCollection(BlockGroup));
  26.                             display.SetCustomName(show.getGeneratedOutput());
  27.                         }
  28.                     }
  29.             }            
  30.         }
  31.  
  32.  
  33.         public class BMyInventoryInfo
  34.         {
  35.             public double maxVolume = 0;
  36.             public double currentVolume = 0;
  37.             public string customName = "";
  38.  
  39.             public void setMaxVolume(double _maxVolume)
  40.             {
  41.                 maxVolume = _maxVolume;
  42.             }
  43.  
  44.             public double getMaxVolume()
  45.             {
  46.                 return maxVolume;
  47.             }
  48.  
  49.             public void setCurrentVolume(double _currentVolume)
  50.             {
  51.                 currentVolume = _currentVolume;
  52.             }
  53.  
  54.             public double getCurrentVolume()
  55.             {
  56.                 return currentVolume;
  57.             }
  58.  
  59.             public void setCustomName(string _customName)
  60.             {
  61.                 customName = _customName;
  62.             }
  63.  
  64.             public bool isMaxVolumeSet()
  65.             {
  66.                 return this.maxVolume > 0;
  67.             }
  68.  
  69.             public bool isFull(bool approximately = true)
  70.             {
  71.                 if (approximately)
  72.                 {
  73.                     return getFillLevelInPercent() > 99;
  74.                 }
  75.                 else
  76.                 {
  77.                     return getCurrentVolume() == getMaxVolume();
  78.                 }
  79.  
  80.             }
  81.  
  82.             public double getFillLevelInPercent(int decimals = 0){
  83.                 if(isMaxVolumeSet() && getCurrentVolume() > 0){
  84.                     return Math.Round(100 * (getCurrentVolume() / getMaxVolume()), decimals);
  85.                 }
  86.                 return 0;
  87.             }                        
  88.         }
  89.  
  90.         public class BMyInventory
  91.         {
  92.             public BMyInventoryInfo incomingInventory = null;
  93.             public BMyInventoryInfo outgoingInventory = null;
  94.             public IMyTerminalBlock container = null;
  95.  
  96.             public void setContainer(IMyTerminalBlock _container)
  97.             {
  98.                 container = _container;
  99.             }
  100.  
  101.             public IMyTerminalBlock getContainer()
  102.             {
  103.                 return container;
  104.             }
  105.  
  106.             public bool hasContainer()
  107.             {
  108.                 return (container as IMyTerminalBlock) != null;  
  109.             }
  110.  
  111.             public void setIncomingInventory(BMyInventoryInfo _incomingInventory)
  112.             {
  113.                 incomingInventory = _incomingInventory;
  114.             }
  115.  
  116.             public BMyInventoryInfo getIncomingInventory()
  117.             {
  118.                 return incomingInventory;
  119.             }
  120.  
  121.             public bool hasIncomingInventory()
  122.             {
  123.                 return (incomingInventory as BMyInventoryInfo) != null;
  124.             }
  125.  
  126.             public void setOutgoingInventory(BMyInventoryInfo _outgoingInventory)
  127.             {
  128.                 outgoingInventory = _outgoingInventory;
  129.             }
  130.  
  131.             public BMyInventoryInfo getOutgoingInventory()
  132.             {
  133.                 return outgoingInventory;
  134.             }
  135.  
  136.             public bool hasOutgoingInventory()
  137.             {
  138.                 return (outgoingInventory as BMyInventoryInfo) != null;
  139.             }
  140.  
  141.             public int inventoryCount()
  142.             {
  143.                 int count = 0;
  144.                 if (hasIncomingInventory())
  145.                 {
  146.                     count++;
  147.                 }
  148.                 if (hasOutgoingInventory())
  149.                 {
  150.                     count++;
  151.                 }
  152.  
  153.                 return count;
  154.             }
  155.         }
  156.  
  157.         public class BMyInventoryFactory
  158.         {
  159.             public BMyInventory buildInventoryFromBlock(IMyTerminalBlock block)
  160.             {
  161.                 BMyInventory inventoryRecord = new BMyInventory();
  162.                 inventoryRecord.setContainer(block);
  163.                 inventoryRecord.setIncomingInventory(buildInventoryInfo(getIncominginventoryFromBlock(block), block));
  164.                 inventoryRecord.setOutgoingInventory(buildInventoryInfo(getOutgoingInventoryFromBlock(block), block));
  165.                
  166.                 return inventoryRecord;
  167.             }
  168.  
  169.             public IMyInventory getIncominginventoryFromBlock(IMyTerminalBlock block)
  170.             {
  171.                 return getInventoryById(block, 0);
  172.             }
  173.  
  174.             public IMyInventory getOutgoingInventoryFromBlock(IMyTerminalBlock block)
  175.             {
  176.                 return getInventoryById(block, 1);
  177.             }
  178.  
  179.             public IMyInventory getInventoryById(IMyTerminalBlock block, int id)
  180.             {
  181.                 IMyInventoryOwner inventoryOwner = (block as IMyInventoryOwner);
  182.                 if (id > 0 && inventoryOwner.GetInventory(0).Equals(inventoryOwner.GetInventory(id)))
  183.                 {
  184.                     return null;
  185.                 }
  186.                 return inventoryOwner.GetInventory(id);
  187.             }
  188.  
  189.             public BMyInventoryInfo buildInventoryInfo(IMyInventory inventory, IMyTerminalBlock block){
  190.                 BMyInventoryInfo info = new BMyInventoryInfo();
  191.                 info.setCustomName(block.CustomName);
  192.                 info.setCurrentVolume(inventory.CurrentVolume.RawValue);
  193.                 info.setMaxVolume(inventory.MaxVolume.RawValue);
  194.  
  195.                 return info;
  196.             }
  197.  
  198.         }
  199.  
  200.         public class BMyInventoryCollection
  201.         {
  202.             List<BMyInventory> inventories = new List<BMyInventory>();
  203.             BMyInventoryFactory factory = new BMyInventoryFactory();
  204.             String customName = "";
  205.  
  206.             public BMyInventoryCollection(IMyBlockGroup group)
  207.             {
  208.                 setCutomName(group.Name);
  209.                 addContainersFromList(group.Blocks);
  210.             }
  211.  
  212.             public void setCutomName(String _customName)
  213.             {
  214.                 customName = _customName;
  215.             }
  216.  
  217.             public string getCustomName()
  218.             {
  219.                 return customName;
  220.             }
  221.  
  222.             public void addContainersFromList(List<IMyTerminalBlock> list)
  223.             {
  224.                 for (int i = 0; i < list.Count; i++)
  225.                 {
  226.                     addContainerToList(list[i]);
  227.                 }
  228.             }
  229.  
  230.             public void addContainerToList(IMyTerminalBlock block)
  231.             {
  232.                 BMyInventory inventory = factory.buildInventoryFromBlock(block);
  233.                 if (inventory.inventoryCount() > 0)
  234.                 {
  235.                     inventories.Add(inventory);
  236.                 }                
  237.             }
  238.                                
  239.             public List<BMyInventory> getInventories()
  240.             {
  241.                 return inventories;
  242.             }                        
  243.         }
  244.  
  245.         public class BMyInventoryCollectionShow
  246.         {
  247.             public BMyInventoryCollection inventoryCollection;
  248.             public String templateIsFull = " [Full]";
  249.             public String template = "{GroupName}\r\n-------------------------------------{Items}";
  250.             public String itemTemplateSingle = "\r\n{CustomName}: {Percent}% [{curVol}/{maxVol}L]{IsFULL}";
  251.             public String itemTemplateIncoming = "\r\n{CustomName}: [IN] {Percent}% [{curVol}/{maxVol}L]{IsFULL}";
  252.             public String itemTemplateOutgoing = " [OUT] {Percent}% [{curVol}/{maxVol}L]{IsFULL}";
  253.  
  254.             public BMyInventoryCollectionShow(BMyInventoryCollection _inventoryCollection)
  255.             {
  256.                 inventoryCollection = _inventoryCollection;
  257.             }
  258.  
  259.             public void setTemplateIsFull(String _template)
  260.             {
  261.                 templateIsFull = _template;
  262.             }
  263.  
  264.             public String getTemplateIsFull()
  265.             {
  266.                 return templateIsFull;
  267.             }
  268.  
  269.             public void setTemplate(String _template)
  270.             {
  271.                 template = _template;
  272.             }
  273.  
  274.             public void setItemTemplateSingle(String _template)
  275.             {
  276.                 itemTemplateSingle = _template;
  277.             }
  278.  
  279.             public void setItemTemplateIncoming(String _template)
  280.             {
  281.                 itemTemplateIncoming = _template;
  282.             }
  283.  
  284.             public void setItemTemplateOutgoing(String _template)
  285.             {
  286.                 itemTemplateOutgoing = _template;
  287.             }
  288.  
  289.             public String getTemplate()
  290.             {
  291.                 return template;
  292.             }
  293.  
  294.             public String getItemTemplateSingle()
  295.             {
  296.                 return itemTemplateSingle;
  297.             }
  298.  
  299.             public String getItemTemplateIncoming()
  300.             {
  301.                 return itemTemplateIncoming;
  302.             }
  303.  
  304.             public String getItemTemplateOutgoing()
  305.             {
  306.                 return itemTemplateOutgoing;
  307.             }
  308.            
  309.             public String getWellFormedPercent(double _percent)
  310.             {
  311.                 if (_percent < 1 && _percent != 0)
  312.                 {
  313.                     return "< 1";
  314.                 }
  315.                 if (_percent > 99 && _percent < 100)
  316.                 {
  317.                     return "> 99";
  318.                 }
  319.  
  320.                 return String.Format("{0:N0}", _percent);
  321.             }
  322.  
  323.             public String getGeneratedOutput()
  324.             {
  325.                 String items = "", output = "";
  326.                 List<BMyInventory> inventories = inventoryCollection.getInventories();
  327.                 for (int i = 0; i < inventories.Count; i++)
  328.                 {
  329.                     if(inventories[i].inventoryCount() > 0){
  330.                         if (inventories[i].hasOutgoingInventory())
  331.                         {
  332.                             items = items + replace(getItemTemplateIncoming(),
  333.                                 new String[] { "{CustomName}", "{Percent}", "{curVol}", "{maxVol}", "{IsFULL}" },
  334.                                 new String[] {
  335.                                         inventories[i].getContainer().CustomName,
  336.                                         String.Format("{0:N0}", inventories[i].getIncomingInventory().getFillLevelInPercent()),
  337.                                         String.Format("{0:N0}", inventories[i].getIncomingInventory().getCurrentVolume()),
  338.                                         String.Format("{0:N0}", inventories[i].getIncomingInventory().getMaxVolume()),
  339.                                         inventories[i].getIncomingInventory().isFull(true)?getTemplateIsFull():""
  340.                                     }
  341.                                 );
  342.                             items = items + replace(getItemTemplateOutgoing(),
  343.                                 new String[] { "{CustomName}", "{Percent}", "{curVol}", "{maxVol}", "{IsFULL}" },
  344.                                 new String[] {
  345.                                         inventories[i].getContainer().CustomName,
  346.                                         String.Format("{0:N0}", inventories[i].getOutgoingInventory().getFillLevelInPercent()),
  347.                                         String.Format("{0:N0}", inventories[i].getOutgoingInventory().getCurrentVolume()),
  348.                                         String.Format("{0:N0}", inventories[i].getOutgoingInventory().getMaxVolume()),
  349.                                         inventories[i].getOutgoingInventory().isFull(true)?getTemplateIsFull():""
  350.                                     }
  351.                                 );
  352.                         }
  353.                         else
  354.                         {
  355.                             items = items + replace(getItemTemplateSingle(),
  356.                                 new String[] { "{CustomName}", "{Percent}", "{curVol}", "{maxVol}", "{IsFULL}" },
  357.                                 new String[] {
  358.                                         inventories[i].getContainer().CustomName,
  359.                                         String.Format("{0:N0}", inventories[i].getIncomingInventory().getFillLevelInPercent()),
  360.                                         String.Format("{0:N0}", inventories[i].getIncomingInventory().getCurrentVolume()),
  361.                                         String.Format("{0:N0}", inventories[i].getIncomingInventory().getMaxVolume()),
  362.                                         inventories[i].getIncomingInventory().isFull(true)?getTemplateIsFull():""
  363.                                     }
  364.                                 );
  365.                         }
  366.                     }                    
  367.                 }
  368.                 output = replace(getItemTemplateSingle(),
  369.                                 new String[] { "{GroupName}", "{Items}" },
  370.                                 new String[] {
  371.                                         inventoryCollection.getCustomName(),
  372.                                         items
  373.                                     }
  374.                                 );
  375.  
  376.                 return output;
  377.             }
  378.  
  379.             public String replace(String source, String[] oldVal, String[] newVal)
  380.             {
  381.                 if (oldVal.Length == newVal.Length)
  382.                 {
  383.                     for (int i = 0; i < oldVal.Length; i++)
  384.                     {
  385.                         source = source.Replace(oldVal[i], newVal[i]);
  386.                     }
  387.                 }
  388.  
  389.                 return source;
  390.             }
  391.  
  392.         }
  393.            
  394.    
  395.         IMyBlockGroup getGroup(String name)
  396.         {
  397.             List<IMyBlockGroup> blockGroups = GridTerminalSystem.BlockGroups;
  398.             for (int i = 0; i < blockGroups.Count; i++)
  399.             {
  400.                 if (blockGroups[i].Name.IndexOf(name) == 0)
  401.                 {
  402.                     return blockGroups[i];
  403.                 }
  404.             }
  405.             return null;
  406.         }
  407.  
  408.         bool isDisplayBlock(IMyTerminalBlock block)
  409.         {
  410.             return isRadioAntenna(block) || isBeacon(block);
  411.         }
  412.  
  413.         IMyTerminalBlock getDisplayBlock(IMyBlockGroup group)
  414.         {
  415.             IMyTerminalBlock block;
  416.             block = getDisplayAntenna(group);
  417.             if (isRadioAntenna(block))
  418.             {
  419.                 block = getDisplayBeacon(group);
  420.             }
  421.  
  422.             return block;
  423.         }
  424.  
  425.         bool isRadioAntenna(IMyTerminalBlock testBlock)
  426.         {
  427.             return (testBlock as IMyRadioAntenna) != null;
  428.         }
  429.  
  430.         IMyRadioAntenna getDisplayAntenna(IMyBlockGroup group)
  431.         {
  432.             for (int i = 0; i < group.Blocks.Count; i++)
  433.             {
  434.                 if (isRadioAntenna(group.Blocks[i]))
  435.                 {
  436.                     return (group.Blocks[i] as IMyRadioAntenna);
  437.                 }
  438.             }
  439.  
  440.             return null;
  441.         }
  442.  
  443.         bool isBeacon(IMyTerminalBlock testBlock)
  444.         {
  445.             return (testBlock as IMyBeacon) != null;
  446.         }
  447.  
  448.         IMyBeacon getDisplayBeacon(IMyBlockGroup group)
  449.         {
  450.             for (int i = 0; i < group.Blocks.Count; i++)
  451.             {
  452.                 if (isBeacon(group.Blocks[i]))
  453.                 {
  454.                     return (group.Blocks[i] as IMyBeacon);
  455.                 }
  456.             }
  457.  
  458.             return null;
  459.         }
  460.  
  461.         //END
Advertisement
Add Comment
Please, Sign In to add comment