public override MyFixedPoint ComputeAmountThatFits(MyDefinitionId contentId)
        {
            if (!MyPerGameSettings.ConstrainInventory())
            {
                return(MyFixedPoint.MaxValue);
            }
            float volume, mass;

            if (!GetVolumeAndMass(ref contentId, out volume, out mass))
            {
                return(0);
            }
            var amountThatFitsVolume = MyFixedPoint.Max((MyFixedPoint)((float)m_maxVolume - (float)m_currentVolume) * (1.0f / (float)volume), 0);
            var amountThatFitsMass   = MyFixedPoint.Max((MyFixedPoint)(((float)m_maxMass - (float)m_currentMass) * (1.0f / (float)mass)), 0);
            var amountThatFits       = MyFixedPoint.Min(amountThatFitsVolume, amountThatFitsMass);

            MyPhysicalItemDefinition physicalItemDefinition = null;

            MyDefinitionManager.Static.TryGetPhysicalItemDefinition(contentId, out physicalItemDefinition);
            if (contentId.TypeId == typeof(MyObjectBuilder_CubeBlock) || (physicalItemDefinition != null && physicalItemDefinition.HasIntegralAmounts))
            {
                amountThatFits = MyFixedPoint.Floor(amountThatFits);
            }

            return(amountThatFits);
        }
        public void TakeFloatingObject(MyFloatingObject obj)
        {
            MyFixedPoint amount = obj.Item.Amount;

            if (MyPerGameSettings.ConstrainInventory())
            {
                amount = MyFixedPoint.Min(ComputeAmountThatFits(obj.Item.Content.GetObjectId()), amount);
            }
            if (amount > 0)
            {
                if (Sync.IsServer)
                {
                    if (obj.MarkedForClose)
                    {
                        return;
                    }
                    MyFloatingObjects.RemoveFloatingObject(obj, amount);
                    AddItemsInternal(amount, obj.Item.Content);
                    SyncObject.SendAddItemsAnnounce(this, amount, obj.Item.Content);
                }
                else
                {
                    SyncObject.TakeFloatingObjectRequest(this, obj);
                }
            }
        }
示例#3
0
        public void AddItems(MyFixedPoint amount, MyObjectBuilder_PhysicalObject objectBuilder, int index = -1)
        {
            if (amount == 0)
            {
                return;
            }
            if (!CanItemsBeAdded(amount, objectBuilder.GetObjectId()))
            {
                return;
            }

            if (Sync.IsServer)
            {
                if (MyPerGameSettings.ConstrainInventory())
                {
                    AffectAddBySurvival(ref amount, objectBuilder);
                }
                if (amount == 0)
                {
                    return;
                }
                AddItemsInternal(amount, objectBuilder, index);
                SyncObject.SendAddItemsAnnounce(this, amount, objectBuilder, index);
            }
            else
            {
                SyncObject.SendAddItemsRequest(this, index, amount, objectBuilder);
            }
        }
示例#4
0
        public MyFixedPoint ComputeAmountThatFits(MyDefinitionId contentId)
        {
            if (!MyPerGameSettings.ConstrainInventory())
            {
                return(MyFixedPoint.MaxValue);
            }
            float volume, mass;

            if (!GetVolumeAndMass(ref contentId, out volume, out mass))
            {
                return(0);
            }
            var amountThatFits = (m_maxVolume - m_currentVolume) * (1.0f / volume);

            amountThatFits = MyFixedPoint.Max(amountThatFits, 0); // In case we added more than we can carry using debug keys.

            MyPhysicalItemDefinition physicalItemDefinition = null;

            MyDefinitionManager.Static.TryGetPhysicalItemDefinition(contentId, out physicalItemDefinition);
            if (contentId.TypeId == typeof(MyObjectBuilder_CubeBlock) || (physicalItemDefinition != null && physicalItemDefinition.HasIntegralAmounts))
            {
                amountThatFits = MyFixedPoint.Floor(amountThatFits);
            }

            return(amountThatFits);
        }
示例#5
0
        public MyInventory(MyFixedPoint maxVolume, Vector3 size, MyInventoryFlags flags, IMyInventoryOwner owner)
        {
            m_maxVolume = MyPerGameSettings.ConstrainInventory() ? maxVolume * MySession.Static.InventoryMultiplier : MyFixedPoint.MaxValue;
            m_size      = size;
            m_flags     = flags;
            m_owner     = owner;

            Clear();

            SyncObject = new MySyncInventory();
        }
示例#6
0
        public bool CanItemsBeAdded(MyFixedPoint amount, MyDefinitionId contentId)
        {
            var adapter = MyInventoryItemAdapter.Static;
            adapter.Adapt(contentId);

            if (MyPerGameSettings.ConstrainInventory() && (amount * adapter.Volume + m_currentVolume > MaxVolume) || (amount * adapter.Mass + m_currentMass > m_maxMass))
            {
                return false;
            }

            return CheckConstraint(contentId);
        }
示例#7
0
        public MyFixedPoint ComputeAmountThatFits(MyBlueprintDefinitionBase blueprint)
        {
            if (!MyPerGameSettings.ConstrainInventory())
                return MyFixedPoint.MaxValue;

            var amountThatFits = (MaxVolume - m_currentVolume) * (1.0f / blueprint.OutputVolume);
            amountThatFits = MyFixedPoint.Max(amountThatFits, 0); // In case we added more than we can carry using debug keys.

            if (blueprint.Atomic)
                amountThatFits = MyFixedPoint.Floor(amountThatFits);

            return amountThatFits;
        }
        public bool CanItemsBeAdded(MyFixedPoint amount, MyDefinitionId contentId)
        {
            float volume, mass;

            if (!GetVolumeAndMass(ref contentId, out volume, out mass))
            {
                return(false);
            }

            if (MyPerGameSettings.ConstrainInventory() && amount * volume + m_currentVolume > m_maxVolume)
            {
                return(false);
            }

            return(CheckConstraint(contentId));
        }
        public override bool AddItems(MyFixedPoint amount, MyObjectBuilder_Base objectBuilder, int index = -1)
        {
            Debug.Assert(objectBuilder is MyObjectBuilder_PhysicalObject, "This type of inventory can't add other types than PhysicalObjects!");
            MyObjectBuilder_PhysicalObject physicalObjectBuilder = objectBuilder as MyObjectBuilder_PhysicalObject;

            if (physicalObjectBuilder == null)
            {
                return(false);
            }
            if (amount == 0)
            {
                return(false);
            }
            if (!CanItemsBeAdded(amount, physicalObjectBuilder.GetObjectId()))
            {
                return(false);
            }

            if (Sync.IsServer)
            {
                if (MyPerGameSettings.ConstrainInventory())
                {
                    AffectAddBySurvival(ref amount, physicalObjectBuilder);
                }
                if (amount == 0)
                {
                    return(false);
                }
                AddItemsInternal(amount, physicalObjectBuilder, index);
                SyncObject.SendAddItemsAnnounce(this, amount, physicalObjectBuilder, index);
            }
            else
            {
                SyncObject.SendAddItemsRequest(this, index, amount, physicalObjectBuilder);
            }
            return(true);
        }
示例#10
0
        public void Init(MyObjectBuilder_Inventory objectBuilder)
        {
            Clear();

            if (objectBuilder == null)
            {
                return;
            }

            if (objectBuilder.Mass.HasValue)
            {
                m_maxMass = objectBuilder.Mass.Value;
            }
            if (objectBuilder.Volume.HasValue)
            {
                MyFixedPoint savedValue = objectBuilder.Volume.Value;
                if (savedValue != MyFixedPoint.MaxValue || MyPerGameSettings.ConstrainInventory() == false)
                {
                    m_maxVolume = savedValue;
                }
            }
            if (objectBuilder.Size.HasValue)
            {
                m_size = objectBuilder.Size.Value;
            }
            if (objectBuilder.InventoryFlags.HasValue)
            {
                m_flags = objectBuilder.InventoryFlags.Value;
            }

            if (!Sync.IsServer)
            {
                m_nextItemID = objectBuilder.nextItemId;
            }
            else
            {
                m_nextItemID = 0;
            }

            int i = 0;

            foreach (var item in objectBuilder.Items)
            {
                if (item.Amount <= 0)
                {
                    // Remove all items with 0 amount when loading inventory.
                    // Should only solve backward problems with saves. 0 amount items should not be created.
                    Debug.Fail(string.Format("Removing item with invalid amount: {0}x '{1}'. This is safe to ignore.", item.Amount, item.PhysicalContent.GetObjectId()));
                    continue;
                }

                if (!MyInventoryItemAdapter.Static.TryAdapt(item.PhysicalContent.GetObjectId()))
                {
                    Debug.Assert(false, "Invalid inventory item: " + item.PhysicalContent.GetObjectId().ToString() + " Not adding it!");
                    continue;
                }

                var contentId = item.PhysicalContent.GetObjectId();

                if (!CanItemsBeAdded(item.Amount, contentId))
                {
                    continue;
                }

                var canStackWithItself = item.PhysicalContent.CanStack(item.PhysicalContent);
                if (!canStackWithItself)
                {
                    MyFixedPoint added = 0;
                    while (added < item.Amount)
                    {
                        AddItemsInternal(1, item.PhysicalContent, i, itemId: (Sync.IsServer) ? null : (uint?)item.ItemId);
                        added += 1;
                        ++i;
                    }
                }
                else
                {
                    if (Sync.IsServer)
                    {
                        AddItemsInternal(item.Amount, item.PhysicalContent, i);
                    }
                    else
                    {
                        //Building from information recieved from server - dont send msg about adding this
                        AddItemsInternal(item.Amount, item.PhysicalContent, i, itemId: item.ItemId);
                    }
                }
                i++;
            }
            VerifyIntegrity();
        }