Ejemplo n.º 1
0
        public MyEntity RemoveItems(uint itemId, MyFixedPoint?amount = null, bool sendEvent = true, bool spawn = false, MatrixD?spawnPos = null)
        {
            var      item    = GetItemByID(itemId);
            var      am      = amount.HasValue ? amount.Value : (item.HasValue? item.Value.Amount : 1);
            MyEntity spawned = null;

            if (Sync.IsServer)
            {
                if (item.HasValue && RemoveItemsInternal(itemId, am, sendEvent))
                {
                    if (spawn)
                    {
                        var owner = Owner as MyEntity;
                        if (!spawnPos.HasValue)
                        {
                            spawnPos = MatrixD.CreateWorld(owner.PositionComp.GetPosition() + owner.PositionComp.WorldMatrix.Forward + owner.PositionComp.WorldMatrix.Up, owner.PositionComp.WorldMatrix.Forward, owner.PositionComp.WorldMatrix.Up);
                        }
                        spawned = item.Value.Spawn(am, spawnPos.Value, owner);
                    }
                    SyncObject.SendRemoveItemsAnnounce(this, am, itemId);
                }
            }
            else
            {
                SyncObject.SendRemoveItemsRequest(this, am, itemId, spawn);
            }
            return(spawned);
        }
Ejemplo n.º 2
0
        public override void ApplyChanges(List<MyComponentChange> changes)
        {
            Debug.Assert(Sync.IsServer);
            if (!Sync.IsServer) return;

            m_tmpItemsToAdd.Clear();

            bool changed = false;
            for (int j = 0; j < changes.Count; ++j)
            {
                var change = changes[j];
                if (change.IsAddition())
                {
                    throw new NotImplementedException();
                }

                for (int i = m_items.Count - 1; i >= 0; i--)
                {
                    var item = m_items[i];
                    MyDefinitionId id = item.Content.GetId();
                    if (id.TypeId == typeof(MyObjectBuilder_BlockItem))
                    {
                        id = MyDefinitionManager.Static.GetComponentId(item.Content.GetObjectId());
                    }

                    if (change.ToRemove != id) continue;

                    MyFixedPoint amount = change.Amount;
                    if (amount > 0)
                    {
                        MyFixedPoint removed = MyFixedPoint.Min(amount, item.Amount);

                        amount -= removed;
                        if (amount == 0)
                        {
                            changes.RemoveAtFast(j);
                            j--;
                        }
                        else
                        {
                            change.Amount = (int)amount;
                            changes[j] = change;
                        }

                        if (item.Amount - removed == 0)
                        {
                            m_items.RemoveAt(i);
                            SyncObject.SendRemoveItemsAnnounce(this, item.Amount, item.ItemId);
                        }
                        else
                        {
                            item.Amount = item.Amount - removed;
                            m_items[i] = item;
                            SyncObject.SendRemoveItemsAnnounce(this, removed, item.ItemId);
                        }

                        if (change.IsChange())
                        {
                            int presentAmount = 0;
                            m_tmpItemsToAdd.TryGetValue(change.ToAdd, out presentAmount);
                            presentAmount += (int)removed;
                            if (presentAmount != 0)
                                m_tmpItemsToAdd[change.ToAdd] = presentAmount;
                        }

                        changed = true;
                    }
                }
            }

            RefreshVolumeAndMass();

            foreach (var addition in m_tmpItemsToAdd)
            {
                MyCubeBlockDefinition blockDef = MyDefinitionManager.Static.GetComponentBlockDefinition(addition.Key);
                Debug.Assert(blockDef != null, "Could not find block definition for adding an inventory item");
                if (blockDef == null) return;

                bool success = AddBlocks(blockDef, addition.Value);
                Debug.Assert(success, "Could not add blocks to the inventory!");

                changed = true;
                RefreshVolumeAndMass();
            }

            if (changed)
            {
                RefreshVolumeAndMass();
                OnContentsChanged();
            }
        }