Beispiel #1
0
        public void Clear(bool sync = true)
        {
            if (sync == false)
            {
                m_items.Clear();
                return;
            }

            MyInventoryItem[] items = new MyInventoryItem[m_items.Count];
            m_items.CopyTo(items);
            foreach (var it in items)
            {
                RemoveItems(it.ItemId);
            }
        }
        private static void TransferOrRemove(MyInventory src, MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false)
        {
            //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item.");
            if (src.ContainItems(amount, contentId, flags))
            {
                bool         transferAll     = !amount.HasValue;
                MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value;

                int i = 0;
                while (i < src.m_items.Count)
                {
                    if (!transferAll && remainingAmount == 0)
                    {
                        break;
                    }

                    MyInventoryItem item = src.m_items[i];

                    //TODO(AF) Remove oxygen specific code from inventory.
                    //Will be fixed once MyInventory will support Entities.
                    var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject;
                    if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f)
                    {
                        i++;
                        continue;
                    }
                    // End of oxygen specific code


                    if (item.Content.GetObjectId() != contentId)
                    {
                        i++;
                        continue;
                    }

                    if (transferAll || remainingAmount >= item.Amount)
                    {
                        remainingAmount -= item.Amount;
                        Transfer(src, dst, item.ItemId, -1, spawn: spawn);
                    }
                    else
                    {
                        Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn);
                        remainingAmount = 0;
                    }
                }
            }
        }
Beispiel #3
0
        public bool RemoveItemsInternal(uint itemId, MyFixedPoint amount, bool sendEvent = true)
        {
            bool found = false;

            for (int i = 0; i < m_items.Count; i++)
            {
                if (m_items[i].ItemId == itemId)
                {
                    MyInventoryItem item = m_items[i];
                    amount       = MathHelper.Clamp(amount, 0, m_items[i].Amount);
                    item.Amount -= amount;
                    if (item.Amount == 0 || amount == 0)
                    {
                        m_items.RemoveAt(i);
                    }
                    else
                    {
                        m_items[i] = item;
                    }

                    found = true;
                    break;
                }
            }
            if (!found)
            {
                Debug.Assert(!found, "Item is missing in inventory. Can't remove.");
                return(false);
            }

            RefreshVolumeAndMass();

            if (sendEvent && ContentsChanged != null)
            {
                ContentsChanged(this);
            }
            return(true);
        }
        public void Clear(bool sync = true)
        {
            if (sync == false)
            {
                m_items.Clear();
                return;
            }

            MyInventoryItem[] items = new MyInventoryItem[m_items.Count];
            m_items.CopyTo(items);
            foreach (var it in items)
            {
                RemoveItems(it.ItemId);
            }
        }
        public void AddItemsInternal(MyFixedPoint amount, MyObjectBuilder_PhysicalObject objectBuilder, int index = -1, uint? itemId = null)
        {
            Debug.Assert(amount > 0, "Adding 0 amount of item.");

            var newItem = new MyInventoryItem() { Amount = amount, Content = objectBuilder };

            if (index >= 0 && index < m_items.Count)
            {
                if (m_items[index].Content.CanStack(objectBuilder))
                {
                    newItem.Amount += m_items[index].Amount;
                    newItem.ItemId = m_items[index].ItemId;
                    m_items[index] = newItem;
                }
                else
                {
                    newItem.ItemId = NextItemID;
                    m_items.Insert(index, newItem);
                }
            }
            else
            {
                bool add = true;
                bool canStackWithItself = newItem.Content.CanStack(newItem.Content);
                if (index < 0 && canStackWithItself)
                {
                    int? itemPos = FindFirstStackablePosition(objectBuilder);
                    if (itemPos.HasValue)
                    {
                        newItem.ItemId = m_items[itemPos.Value].ItemId;
                        newItem.Amount += m_items[itemPos.Value].Amount;
                        m_items[itemPos.Value] = newItem;
                        add = false;
                    }
                }
                if (add)
                {
                    if (canStackWithItself)
                    {
                        newItem.ItemId = itemId.HasValue ? itemId.Value : NextItemID;
                        m_items.Add(newItem);
                    }
                    else
                    {
                        var targetAmount = newItem.Amount;
                        newItem.Amount = 1;
                        for (MyFixedPoint addedAmount = 0; addedAmount < targetAmount; addedAmount += 1)
                        {
                            newItem.ItemId = itemId.HasValue ? itemId.Value : NextItemID;
                            itemId = null; // so we use NextItemID next time
                            m_items.Add(newItem);
                            newItem.Content = newItem.Content.Clone() as MyObjectBuilder_PhysicalObject;
                            Debug.Assert(newItem.Content != null);
                        }
                    }
                }
            }

            RefreshVolumeAndMass();

            VerifyIntegrity();

            if (ContentsChanged != null)
                ContentsChanged(this);
        }
Beispiel #6
0
        private static void TransferOrRemove(MyInventory src, MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false)
        {
            //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item.");
            if (src.ContainItems(amount, contentId, flags))
            {
                bool         transferAll     = !amount.HasValue;
                MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value;

                //TODO(AF) Remove oxygen specific code from inventory.
                //Will be fixed once MyInventory will support Entities.
                // If the requested item is an oxygen container, do a preliminary loop to pull any non-full items first.
                if (contentId.TypeId == typeof(MyObjectBuilder_OxygenContainerObject))
                {
                    int k = 0;
                    while (k < src.m_items.Count)
                    {
                        if (!transferAll && remainingAmount == 0)
                        {
                            break;
                        }

                        MyInventoryItem item = src.m_items[k];

                        // Skip full oxygen bottles in this loop.  They will not be skipped in the next one.
                        var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject;
                        if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f)
                        {
                            k++;
                            continue;
                        }

                        if (item.Content.GetObjectId() != contentId)
                        {
                            k++;
                            continue;
                        }

                        if (transferAll || remainingAmount >= item.Amount)
                        {
                            remainingAmount -= item.Amount;
                            Transfer(src, dst, item.ItemId, -1, spawn: spawn);
                        }
                        else
                        {
                            Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn);
                            remainingAmount = 0;
                        }
                    }
                }
                // End of oxygen specific code

                int i = 0;
                while (i < src.m_items.Count)
                {
                    if (!transferAll && remainingAmount == 0)
                    {
                        break;
                    }

                    MyInventoryItem item = src.m_items[i];

                    if (item.Content.GetObjectId() != contentId)
                    {
                        i++;
                        continue;
                    }

                    if (transferAll || remainingAmount >= item.Amount)
                    {
                        remainingAmount -= item.Amount;
                        Transfer(src, dst, item.ItemId, -1, spawn: spawn);
                    }
                    else
                    {
                        Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn);
                        remainingAmount = 0;
                    }
                }
            }
        }
Beispiel #7
0
        public void AddItemsInternal(MyFixedPoint amount, MyObjectBuilder_PhysicalObject objectBuilder, int index = -1, uint?itemId = null)
        {
            Debug.Assert(amount > 0, "Adding 0 amount of item.");

            var newItem = new MyInventoryItem()
            {
                Amount = amount, Content = objectBuilder
            };

            if (index >= 0 && index < m_items.Count)
            {
                if (m_items[index].Content.CanStack(objectBuilder))
                {
                    newItem.Amount += m_items[index].Amount;
                    newItem.ItemId  = m_items[index].ItemId;
                    m_items[index]  = newItem;
                }
                else
                {
                    newItem.ItemId = NextItemID;
                    m_items.Insert(index, newItem);
                }
            }
            else
            {
                bool add = true;
                bool canStackWithItself = newItem.Content.CanStack(newItem.Content);
                if (index < 0 && canStackWithItself)
                {
                    int?itemPos = FindFirstStackablePosition(objectBuilder);
                    if (itemPos.HasValue)
                    {
                        newItem.ItemId         = m_items[itemPos.Value].ItemId;
                        newItem.Amount        += m_items[itemPos.Value].Amount;
                        m_items[itemPos.Value] = newItem;
                        add = false;
                    }
                }
                if (add)
                {
                    if (canStackWithItself)
                    {
                        newItem.ItemId = itemId.HasValue ? itemId.Value : NextItemID;
                        m_items.Add(newItem);
                    }
                    else
                    {
                        var targetAmount = newItem.Amount;
                        newItem.Amount = 1;
                        for (MyFixedPoint addedAmount = 0; addedAmount < targetAmount; addedAmount += 1)
                        {
                            newItem.ItemId = itemId.HasValue ? itemId.Value : NextItemID;
                            itemId         = null; // so we use NextItemID next time
                            m_items.Add(newItem);
                            newItem.Content = newItem.Content.Clone() as MyObjectBuilder_PhysicalObject;
                            Debug.Assert(newItem.Content != null);
                        }
                    }
                }
            }

            RefreshVolumeAndMass();

            VerifyIntegrity();

            if (ContentsChanged != null)
            {
                ContentsChanged(this);
            }
        }
        public static MyGuiControlGrid.Item CreateInventoryGridItem(MyInventoryItem item)
        {
            var definition = MyDefinitionManager.Static.GetPhysicalItemDefinition(item.Content);

            var itemMass = definition.Mass * (double)item.Amount;
            var itemVolume = definition.Volume * 1000 * (double)item.Amount;

            var gridItem = new MyGuiControlGrid.Item(
                icon: definition.Icon,
                userData: item,
                toolTip: new StringBuilder().AppendFormat(MyTexts.GetString(MySpaceTexts.ToolTipTerminalInventory_ItemInfo),
                    definition.DisplayNameText,
                    (itemMass < 0.01) ? "<0.01" : itemMass.ToString(MyInventoryConstants.GUI_DISPLAY_FORMAT, CultureInfo.InvariantCulture),
                    (itemVolume < 0.01) ? "<0.01" : itemVolume.ToString(MyInventoryConstants.GUI_DISPLAY_FORMAT, CultureInfo.InvariantCulture),
                    (item.Content.Flags == MyItemFlags.Damaged ? MyTexts.Get(MySpaceTexts.ItemDamagedDescription) : MyTexts.Get(MySpaceTexts.Blank))).ToString());
            if (MyFakes.SHOW_INVENTORY_ITEM_IDS)
            {
                gridItem.ToolTip.AddToolTip(new StringBuilder().AppendFormat("ItemID: {0}", item.ItemId).ToString());
            }
            FormatItemAmount(item, m_textCache);
            gridItem.AddText(m_textCache, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM);
            m_textCache.Clear();

            if (definition.IconSymbol.HasValue)
                gridItem.AddText(MyTexts.Get(definition.IconSymbol.Value), MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP);
            return gridItem;
        }
        public static void FormatItemAmount(MyInventoryItem item, StringBuilder text)
        {
            try
            {
                var typeId = item.Content.GetType();
                if (typeId == typeof(MyObjectBuilder_Ore) ||
                    typeId == typeof(MyObjectBuilder_Ingot))
                {
                    double amount = (double)item.Amount;

                    if (amount < 0.01)
                        text.Append(amount.ToString("<0.01", CultureInfo.InvariantCulture));
                    else if (item.Amount < 10)
                        text.Append(amount.ToString("0.##", CultureInfo.InvariantCulture));
                    else if (item.Amount < 100)
                        text.Append(amount.ToString("0.#", CultureInfo.InvariantCulture));
                    else if (item.Amount < 1000)
                        text.Append(amount.ToString("0.", CultureInfo.InvariantCulture));
                    else if (item.Amount < 10000)
                        text.Append((amount / 1000.0).ToString("0.##k", CultureInfo.InvariantCulture));
                    else if (item.Amount < 100000)
                        text.Append((amount / 1000.0).ToString("0.#k", CultureInfo.InvariantCulture));
                    else
                        text.Append((amount / 1000.0).ToString("#,##0.k", CultureInfo.InvariantCulture));
                }
                else if (typeId == typeof(MyObjectBuilder_PhysicalGunObject))
                {
                    Debug.Assert(item.Amount == 1, "There should only be one gun in a single slot. This is safe to ignore.");
                }
                else if (typeId == typeof(MyObjectBuilder_OxygenContainerObject))
                {
                    Debug.Assert(item.Amount == 1, "There should only be one oxygen bottle in a single slot. This is safe to ignore.");

                    var oxygenContainer = item.Content as MyObjectBuilder_OxygenContainerObject;
                    text.Append((oxygenContainer.OxygenLevel * 100f).ToString("F0") + "%");
                }
                else
                {
                    int integerPart = (int)item.Amount;
                    var decimalPart = item.Amount - integerPart;
                    if (decimalPart > 0)
                        text.Append('~'); // used for half empty magazines and such
                    text.Append(integerPart.ToString("#,##0.x", CultureInfo.InvariantCulture));
                }
            }
            catch (System.OverflowException)
            {
                text.Append("ERROR");
            }
        }