Пример #1
0
        private static void TransferItemsInternal(MyInventory src, MyInventory dst, uint srcItemId, bool spawn, int destItemIndex, MyFixedPoint amount)
        {
            Debug.Assert(Sync.IsServer);
            MyFixedPoint remove = amount;

            MyPhysicalInventoryItem srcItem = default(MyPhysicalInventoryItem);
            int srcIndex = -1;
            for (int i = 0; i < src.m_items.Count; ++i)
            {
                if (src.m_items[i].ItemId == srcItemId)
                {
                    srcIndex = i;
                    srcItem = src.m_items[i];
                    break;
                }
            }
            if (srcIndex == -1) return;

            FixTransferAmount(src, dst, srcItem, spawn, ref remove, ref amount);

            if (amount != 0)
            {
                if (src == dst && destItemIndex >= 0 && destItemIndex < dst.m_items.Count && !dst.m_items[destItemIndex].Content.CanStack(srcItem.Content))
                {
                    dst.SwapItems(srcIndex, destItemIndex);
                }
                else
                {
                    dst.AddItemsInternal(amount, srcItem.Content, dst == src && remove == 0 ? srcItemId : (uint?)null, destItemIndex);
                    if (remove != 0)
                        src.RemoveItems(srcItemId, remove);
                }
            }
        }
Пример #2
0
        public static void Transfer(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null, bool spawn = false)
        {
            var itemNullable = src.GetItemByID(srcItemId);
            if (!itemNullable.HasValue)
                return;

            var item = itemNullable.Value;
            if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId()))
                return;
            if (Sync.IsServer)
            {
                var transferAmount = amount ?? item.Amount;

                if (dst == null)
                {
                    src.RemoveItems(srcItemId, amount, true, spawn);
                    return;
                }

                TransferItemsInternal(src, dst, srcItemId, spawn, dstIdx, transferAmount);
            }
        }
Пример #3
0
        public static void TransferItemsInternal(MyInventory src, MyInventory dst, uint itemId, bool spawn, int destItemIndex, MyFixedPoint amount)
        {
            Debug.Assert(Sync.IsServer);
            MyFixedPoint remove = amount;

            var srcItem = src.GetItemByID(itemId);
            if (!srcItem.HasValue) return;

            FixTransferAmount(src, dst, srcItem, spawn, ref remove, ref amount);

            if (amount != 0)
            {
                if (dst.AddItems(amount, srcItem.Value.Content, destItemIndex))
                {
                    if (remove != 0)
                        src.RemoveItems(itemId, remove);
                }
            }
        }
Пример #4
0
        //this is from client only
        public static void TransferByUser(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null)
        {
            // CH: TODO: Remove if date > 15.5.2016 :-) It's only to catch a nullref
            if (dst.Owner == null) MyLog.Default.WriteLine("dst.Owner == null");

            if (src == null)
            {
                return;
            }

            var itemNullable = src.GetItemByID(srcItemId);
            if (!itemNullable.HasValue)
                return;

            var item = itemNullable.Value;
            
            // CH: TODO: Remove if date > 15.5.2016 :-) It's only to catch a nullref
            if (item.Content == null) MyLog.Default.WriteLine("item.Content == null");

            if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId()))
                return;

            var transferAmount = amount ?? item.Amount;

            if (dst == null)
            {
                src.RemoveItems(srcItemId, amount, true, false);
                return;
            }

            //TransferItemsInternal(src, dst, srcItemId, false, dstIdx, transferAmount);

            // CH: TODO: Remove if date > 15.5.2016 :-) It's only to catch a nullref
            for (int i = 0; i < dst.Owner.InventoryCount; i++)
            {
                if (dst.Owner.GetInventory(i) == null) MyLog.Default.WriteLine("dst.Owner.GetInventory(i) == null");
            }

            byte inventoryIndex = 0;
            for (byte i = 0; i < dst.Owner.InventoryCount; i++)
            {
                if (dst.Owner.GetInventory(i).Equals(dst))
                {
                    inventoryIndex = i;
                    break;
                }
            }

            MyMultiplayer.RaiseEvent(src, x => x.InventoryTransferItem_Implementation, transferAmount, srcItemId, dst.Owner.EntityId, inventoryIndex, dstIdx);
        }
Пример #5
0
        //this is from client only
        public static void TransferByUser(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null)
        {
            var itemNullable = src.GetItemByID(srcItemId);
            if (!itemNullable.HasValue)
                return;

            var item = itemNullable.Value;
            if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId()))
                return;

            var transferAmount = amount ?? item.Amount;

            if (dst == null)
            {
                src.RemoveItems(srcItemId, amount, true, false);
                return;
            }

            //TransferItemsInternal(src, dst, srcItemId, false, dstIdx, transferAmount);

            byte inventoryIndex = 0;
            for (byte i = 0; i < dst.Owner.InventoryCount; i++)
            {
                if (dst.Owner.GetInventory(i).Equals(dst))
                {
                    inventoryIndex = i;
                    break;
                }
            }

            MyMultiplayer.RaiseEvent(src, x => x.InventoryTransferItem_Implementation, transferAmount, srcItemId, dst.Owner.EntityId, inventoryIndex, dstIdx);
        }