Exemplo n.º 1
0
        // ReSharper disable once MemberCanBeMadeStatic.Local
        private void UninstallToInventory(MyInventoryBase inv, MyDefinitionId id, ref int amount)
        {
            var toAdd = Math.Min(amount, inv.ComputeAmountThatFits(id));

            // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
            if (inv.AddItems(id, toAdd))
            {
                amount = toAdd;
            }
            else
            {
                amount = 0;
            }
        }
Exemplo n.º 2
0
        public void MoveUnneededItemsFromConstructionStockpile(MyInventoryBase toInventory)
        {
            if (m_stockpile == null) return;

            Debug.Assert(toInventory != null);
            if (toInventory == null) return;

            m_tmpItemList.Clear();
            AcquireUnneededStockpileItems(m_tmpItemList);
            m_stockpile.ClearSyncList();

            foreach (var item in m_tmpItemList)
            {
                var amount = (int)toInventory.ComputeAmountThatFits(item.Content.GetId());
                amount = Math.Min(amount, item.Amount);
                toInventory.AddItems(amount, item.Content);
                m_stockpile.RemoveItems(amount, item.Content);
            }
            CubeGrid.SyncObject.SendStockpileChanged(this, m_stockpile.GetSyncList());
            m_stockpile.ClearSyncList();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Moves items with the given flags from the construction inventory to the character.
        /// If the flags are None, all items are moved.
        /// </summary>
        public void MoveItemsFromConstructionStockpile(MyInventoryBase toInventory, MyItemFlags flags = MyItemFlags.None)
        {
            if (m_stockpile == null) return;

            Debug.Assert(toInventory != null);
            if (toInventory == null) return;

            m_tmpItemList.Clear();
            foreach (var item in m_stockpile.GetItems())
            {
                if (flags == MyItemFlags.None || (item.Content.Flags & flags) != 0)
                    m_tmpItemList.Add(item);
            }
            m_stockpile.ClearSyncList();
            foreach (var item in m_tmpItemList)
            {
                var amount = (int)toInventory.ComputeAmountThatFits(item.Content.GetId());
                amount = Math.Min(amount, item.Amount);
                toInventory.AddItems(amount, item.Content);
                m_stockpile.RemoveItems(amount, item.Content);
            }
            CubeGrid.SyncObject.SendStockpileChanged(this, m_stockpile.GetSyncList());
            m_stockpile.ClearSyncList();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Moves items with the given flags from the construction inventory to the character.
        /// If the flags are None, all items are moved.
        /// </summary>
        public void MoveItemsFromConstructionStockpile(MyInventoryBase toInventory, MyItemFlags flags = MyItemFlags.None)
        {
            if (m_stockpile == null) return;

            Debug.Assert(toInventory != null);
            if (toInventory == null) return;

            m_tmpItemList.Clear();
            foreach (var item in m_stockpile.GetItems())
            {
                if (flags == MyItemFlags.None || (item.Content.Flags & flags) != 0)
                    m_tmpItemList.Add(item);
            }
            m_stockpile.ClearSyncList();
            foreach (var item in m_tmpItemList)
            {
                // If the item is just some component that is represented by another components, use the first
                // ME Example: ScrapWoodComponent has representation as ScrapWood or ScrapWoodBranches
                MyComponentSubstitutionDefinition substitution;
                if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(item.Content.GetId(), out substitution))
                {
                    Debug.Assert(substitution.ProvidingComponents.Count > 0, "Invalid component substitution definition for: " + item.Content.GetId().ToString());
                    MyDefinitionId componentId = item.Content.GetId();
                    int componentAmount = (int)item.Amount;
                    MyObjectBuilder_Base itemBuilder = item.Content;
                    if (substitution.ProvidingComponents.Count > 0)
                    {
                        componentId = substitution.ProvidingComponents.First().Key;
                        componentAmount = componentAmount * substitution.ProvidingComponents.First().Value;
                        itemBuilder = MyObjectBuilderSerializer.CreateNewObject(componentId);
                    }
                    var amount = (int)toInventory.ComputeAmountThatFits(componentId);
                    amount = Math.Min(amount, componentAmount);                   
                    toInventory.AddItems(amount, itemBuilder);
                    var removedAmount = amount;
                    if (substitution.ProvidingComponents.Count > 0)
                    {
                        removedAmount = removedAmount / substitution.ProvidingComponents.First().Value;
                    }
                    m_stockpile.RemoveItems(amount, item.Content);
                }
                else
                {
                    var amount = (int)toInventory.ComputeAmountThatFits(item.Content.GetId());
                    amount = Math.Min(amount, item.Amount);
                    toInventory.AddItems(amount, item.Content);
                    m_stockpile.RemoveItems(amount, item.Content);
                }
            }
            CubeGrid.SyncObject.SendStockpileChanged(this, m_stockpile.GetSyncList());
            m_stockpile.ClearSyncList();
        }