private bool empty(IMyTerminalBlock cube)
        {
            List <MyInventoryItem> li  = new List <MyInventoryItem>();
            IMyInventory           src = cube.GetInventory();

            src.GetItems(li);
            foreach (MyInventoryItem item in li)
            {
                //Echo("Trying to move "+item.Amount.ToIntSafe()+" of "+item.Type.SubtypeId+" from "+cube.CustomName);
                IMyCargoContainer box = getTargetToFill();
                //Echo("Checking "+box.CustomName);
                IMyInventory tgt = box.GetInventory();
                if (tryMove(src, tgt, item))
                {
                    Echo("Moved " + item.Amount.ToIntSafe() + " of " + item.Type.SubtypeId + " from " + cube.CustomName + " to " + box.CustomName);
                    currentPreferredTarget = box;
                    return(true);
                }
                else
                {
                    currentPreferredTarget = null;
                }
            }
            return(false);
        }
        /// <summary>
        /// Removes components which are in the tool's cargo from pulling list.
        /// </summary>
        Dictionary <string, int> Reduce(Dictionary <string, int> ComponentList)
        {
            Dictionary <string, int> Reduced = ComponentList;

            VRage.Game.ModAPI.Ingame.IMyInventory MyInventory = ToolCargo;
            var           InventoryList = MyInventory.GetItems();
            List <string> ToRemove      = new List <string>();

            foreach (var Item in InventoryList)
            {
                if (ComponentList.ContainsKey(Item.Content.SubtypeName))
                {
                    Reduced[Item.Content.SubtypeName] -= (int)Item.Amount;
                    if (Reduced[Item.Content.SubtypeName] <= 0)
                    {
                        ToRemove.Add(Item.Content.SubtypeName);
                    }
                }
            }

            Reduced.RemoveAll(ToRemove);

            return(Reduced);
        }
Exemple #3
0
        public void Main()           //called each cycle
        {
            tick++;
            if (tick % 4 != 0)
            {
                return;
            }
            counts.Clear();
            long capacity       = 0;
            long used           = 0;
            long totalItems     = 0;
            long capacityOver   = 0;
            long usedOver       = 0;
            long totalItemsOver = 0;

            List <string> nonfull = new List <string>();

            foreach (IMyEntity io in containers)
            {
                IMyInventory inv = io.GetInventory(0);
                if (io is IMyProductionBlock)
                {
                    inv = ((IMyProductionBlock)io).InputInventory;
                }
                int cap     = inv.MaxVolume.ToIntSafe();
                int usedVol = inv.CurrentVolume.ToIntSafe();
                if (cap != usedVol)
                {
                    string name = io.Name;
                    if (io is IMyTerminalBlock)
                    {
                        name = ((IMyTerminalBlock)io).CustomName;
                    }
                    nonfull.Add(name);
                }
                capacity += cap;
                used     += usedVol;
                List <MyInventoryItem> li = new List <MyInventoryItem>();
                inv.GetItems(li);
                foreach (MyInventoryItem ii in li)
                {
                    int    amt  = 0;
                    string type = ii.Type.ToString();
                    counts.TryGetValue(type, out amt);
                    int has = ii.Amount.ToIntSafe();
                    amt         += has;
                    counts[type] = amt;
                    totalItems  += has;
                }
            }

            if (!TREAT_OVERFLOW_AS_MAIN && used >= capacity)
            {
                foreach (IMyEntity io in overflow)
                {
                    IMyInventory inv = io.GetInventory(0);                     //do not increment capacity, so fill fraction goes over 100%
                    capacityOver += inv.MaxVolume.ToIntSafe();
                    used         += inv.CurrentVolume.ToIntSafe();
                    usedOver     += inv.CurrentVolume.ToIntSafe();
                    List <MyInventoryItem> li = new List <MyInventoryItem>();
                    inv.GetItems(li);
                    foreach (MyInventoryItem ii in li)
                    {
                        int    amt  = 0;
                        string type = ii.Type.ToString();
                        counts.TryGetValue(type, out amt);
                        int has = ii.Amount.ToIntSafe();
                        amt            += has;
                        counts[type]    = amt;
                        totalItems     += has;
                        totalItemsOver += has;
                    }
                }
            }

            double frac = used / (double)capacity;
            Color  c    = getDisplayColor(frac);

            foreach (IMyTextPanel scr in displays)
            {
                scr.WriteText("");
                scr.BackgroundColor = c;
                scr.ContentType     = ContentType.TEXT_AND_IMAGE;
                scr.FontColor       = new VRageMath.Color(255, 255, 255, 255);

                scr.WriteText("Cargo is " + Math.Round(frac * 100, 1) + " % full.", true);
                if (!TREAT_OVERFLOW_AS_MAIN && frac > 1)
                {
                    scr.WriteText("Cargo is overflowing!\n");
                    double frac2 = usedOver / (double)capacityOver;
                    scr.WriteText("Overflow is " + Math.Round(frac2 * 100, 1) + " % full.\n", true);
                }
                if (nonfull.Count > 0 && frac > 0.8)
                {
                    scr.WriteText("Space remains in: " + string.Join(", ", nonfull), true);
                }
                if (SHOW_ITEM_BREAKDOWN)
                {
                    scr.WriteText(" Contents:\n", true);
                    List <KeyValuePair <string, int> > entries = new List <KeyValuePair <string, int> >();
                    foreach (var entry in counts)
                    {
                        entries.Add(entry);
                    }
                    entries.Sort((e1, e2) => - e1.Value.CompareTo(e2.Value));
                    foreach (var entry in entries)
                    {
                        int prev = 0;
                        countsLast.TryGetValue(entry.Key, out prev);
                        string delta = "   (No change)";
                        if (prev < entry.Value)
                        {
                            delta = "   (+" + (entry.Value - prev) + " units)";
                        }
                        else if (prev > entry.Value)
                        {
                            delta = "   (-" + (prev - entry.Value) + " units)";
                        }
                        if (countsLast.Count == 0)
                        {
                            delta = "";
                        }
                        scr.WriteText(localize(entry.Key) + " x " + entry.Value + " (" + Math.Round(entry.Value * 100F / totalItems, 1) + " %)" + delta, true);
                        scr.WriteText("\n", true);
                    }
                }
                else
                {
                    scr.WriteText("\n", true);
                }
            }

            if (SHOW_ITEM_BREAKDOWN)
            {
                countsLast = new Dictionary <string, int>(counts);
            }
        }
        public void Main()           //called each cycle
        {
            itemCounts.Clear();
            fractions.Clear();

            foreach (IMyEntity io in containers)
            {
                IMyInventory           inv = io.GetInventory();
                List <MyInventoryItem> li  = new List <MyInventoryItem>();
                inv.GetItems(li);
                foreach (MyInventoryItem ii in li)
                {
                    TurretType ammo = isAmmoItem(ii);
                    if (ammo != TurretType.UNKNOWN)
                    {
                        locale[ammo] = ii.Type.SubtypeId;
                        //Echo(ii.Type.ToString()+" > "+ammo);
                        if (currentTurrets.Contains(ammo))
                        {
                            int amt = 0;
                            itemCounts.TryGetValue(ammo, out amt);
                            int has = ii.Amount.ToIntSafe();
                            amt += has;
                            itemCounts[ammo] = amt;
                            //Echo(ammo+" > "+amt);
                        }
                    }
                }
            }

            foreach (Display scr in ammoDisplays)
            {
                scr.prepare();
            }

            foreach (Display scr in turretDisplays)
            {
                scr.prepare();
            }

            foreach (TurretType type in itemCounts.Keys)
            {
                fractions[type] = Math.Min(1F, itemCounts[type] * roundValues[type] / (float)baseValues[type]);
                Echo(type + " > " + itemCounts[type] + " > " + fractions[type]);
                showStatus(type);
            }

            foreach (Display scr in ammoDisplays)
            {
                scr.write("");
            }

            foreach (IMyLargeTurretBase tur in turrets)
            {
                IMyInventory inv = tur.GetInventory();
                float        f   = inv.CurrentVolume == 0 ? 0 : inv.CurrentVolume.RawValue / (float)inv.MaxVolume.RawValue;
                if (tur is IMyLargeInteriorTurret)
                {
                    f *= (float)inv.MaxVolume.RawValue / 0.01F;
                }
                if (f <= 0.01)
                {
                    //Echo(tur.CustomName+" > "+inv.CurrentVolume+"/"+inv.MaxVolume);
                    foreach (Display scr in turretDisplays)
                    {
                        scr.setColor(Color.Red);
                        scr.write("Turret " + tur.CustomName + " is empty!");
                    }
                }
                else if (f <= 0.25)
                {
                    //Echo(tur.CustomName+" > "+inv.CurrentVolume+"/"+inv.MaxVolume);
                    foreach (Display scr in turretDisplays)
                    {
                        scr.setColor(Color.Yellow);
                        scr.write("Turret " + tur.CustomName + " is low!");
                    }
                }
            }
        }