/// <summary>
        /// If there is room, puts the item in the inventory and returns true, otherwise returns false
        /// </summary>
        public override bool TryPutItem(Item item, Character user, IEnumerable <InvSlotType> allowedSlots = null, bool createNetworkEvent = true, bool ignoreCondition = false)
        {
            if (allowedSlots == null || !allowedSlots.Any())
            {
                return(false);
            }
            if (item == null)
            {
#if DEBUG
                throw new Exception("item null");
#else
                return(false);
#endif
            }
            if (item.Removed)
            {
#if DEBUG
                throw new Exception("Tried to put a removed item (" + item.Name + ") in an inventory");
#else
                DebugConsole.ThrowError("Tried to put a removed item (" + item.Name + ") in an inventory.\n" + Environment.StackTrace.CleanupStackTrace());
                return(false);
#endif
            }

            bool inSuitableSlot = false;
            bool inWrongSlot    = false;
            int  currentSlot    = -1;
            for (int i = 0; i < capacity; i++)
            {
                if (slots[i].Contains(item))
                {
                    currentSlot = i;
                    if (allowedSlots.Any(a => a.HasFlag(SlotTypes[i])))
                    {
                        inSuitableSlot = true;
                    }
                    else if (!allowedSlots.Any(a => a.HasFlag(SlotTypes[i])))
                    {
                        inWrongSlot = true;
                    }
                }
            }
            //all good
            if (inSuitableSlot && !inWrongSlot)
            {
                return(true);
            }

            //try to place the item in a LimbSlot.Any slot if that's allowed
            if (allowedSlots.Contains(InvSlotType.Any) && item.AllowedSlots.Contains(InvSlotType.Any))
            {
                int freeIndex = CheckIfAnySlotAvailable(item, inWrongSlot);
                if (freeIndex > -1)
                {
                    PutItem(item, freeIndex, user, true, createNetworkEvent);
                    item.Unequip(character);
                    return(true);
                }
            }

            int placedInSlot = -1;
            foreach (InvSlotType allowedSlot in allowedSlots)
            {
                if (allowedSlot.HasFlag(InvSlotType.RightHand) && character.AnimController.GetLimb(LimbType.RightHand) == null)
                {
                    continue;
                }
                if (allowedSlot.HasFlag(InvSlotType.LeftHand) && character.AnimController.GetLimb(LimbType.LeftHand) == null)
                {
                    continue;
                }

                //check if all the required slots are free
                bool free = true;
                for (int i = 0; i < capacity; i++)
                {
                    if (allowedSlot.HasFlag(SlotTypes[i]) && item.AllowedSlots.Any(s => s.HasFlag(SlotTypes[i])) && slots[i].Items.Any(it => it != item))
                    {
#if CLIENT
                        if (PersonalSlots.HasFlag(SlotTypes[i]))
                        {
                            hidePersonalSlots = false;
                        }
#endif
                        if (!slots[i].First().AllowedSlots.Contains(InvSlotType.Any) || !TryPutItem(slots[i].FirstOrDefault(), character, new List <InvSlotType> {
                            InvSlotType.Any
                        }, true, ignoreCondition))
                        {
                            free = false;
#if CLIENT
                            for (int j = 0; j < capacity; j++)
                            {
                                if (visualSlots != null && slots[j] == slots[i])
                                {
                                    visualSlots[j].ShowBorderHighlight(GUI.Style.Red, 0.1f, 0.9f);
                                }
                            }
#endif
                        }
                    }
                }

                if (!free)
                {
                    continue;
                }

                for (int i = 0; i < capacity; i++)
                {
                    if (allowedSlot.HasFlag(SlotTypes[i]) && item.GetComponents <Pickable>().Any(p => p.AllowedSlots.Any(s => s.HasFlag(SlotTypes[i]))) && slots[i].Empty())
                    {
#if CLIENT
                        if (PersonalSlots.HasFlag(SlotTypes[i]))
                        {
                            hidePersonalSlots = false;
                        }
#endif
                        bool removeFromOtherSlots = item.ParentInventory != this;
                        if (placedInSlot == -1 && inWrongSlot)
                        {
                            if (!slots[i].HideIfEmpty || SlotTypes[currentSlot] != InvSlotType.Any)
                            {
                                removeFromOtherSlots = true;
                            }
                        }

                        PutItem(item, i, user, removeFromOtherSlots, createNetworkEvent);
                        item.Equip(character);
                        placedInSlot = i;
                    }
                }
                if (placedInSlot > -1)
                {
                    break;
                }
            }

            return(placedInSlot > -1);
        }
        public override bool TryPutItem(Item item, int index, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true, bool ignoreCondition = false)
        {
            if (index < 0 || index >= slots.Length)
            {
                string errorMsg = "CharacterInventory.TryPutItem failed: index was out of range(" + index + ").\n" + Environment.StackTrace.CleanupStackTrace();
                GameAnalyticsManager.AddErrorEventOnce("CharacterInventory.TryPutItem:IndexOutOfRange", GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg);
                return(false);
            }
#if CLIENT
            if (PersonalSlots.HasFlag(SlotTypes[index]))
            {
                hidePersonalSlots = false;
            }
#endif
            //there's already an item in the slot
            if (slots[index].Any())
            {
                if (slots[index].Contains(item))
                {
                    return(false);
                }
                return(base.TryPutItem(item, index, allowSwapping, allowCombine, user, createNetworkEvent, ignoreCondition));
            }

            if (SlotTypes[index] == InvSlotType.Any)
            {
                if (!item.GetComponents <Pickable>().Any(p => p.AllowedSlots.Contains(InvSlotType.Any)))
                {
                    return(false);
                }
                if (slots[index].Any())
                {
                    return(slots[index].Contains(item));
                }
                PutItem(item, index, user, true, createNetworkEvent);
                return(true);
            }

            InvSlotType placeToSlots = InvSlotType.None;

            bool slotsFree = true;
            foreach (Pickable pickable in item.GetComponents <Pickable>())
            {
                foreach (InvSlotType allowedSlot in pickable.AllowedSlots)
                {
                    if (!allowedSlot.HasFlag(SlotTypes[index]))
                    {
                        continue;
                    }
    #if CLIENT
                    if (PersonalSlots.HasFlag(allowedSlot))
                    {
                        hidePersonalSlots = false;
                    }
    #endif
                    for (int i = 0; i < capacity; i++)
                    {
                        if (allowedSlot.HasFlag(SlotTypes[i]) && slots[i].Any() && !slots[i].Contains(item))
                        {
                            slotsFree = false;
                            break;
                        }
                        placeToSlots = allowedSlot;
                    }
                }
            }

            if (!slotsFree)
            {
                return(false);
            }

            return(TryPutItem(item, user, new List <InvSlotType>()
            {
                placeToSlots
            }, createNetworkEvent, ignoreCondition));
        }
Exemple #3
0
        /// <summary>
        /// If there is room, puts the item in the inventory and returns true, otherwise returns false
        /// </summary>
        public override bool TryPutItem(Item item, Character user, List <InvSlotType> allowedSlots = null, bool createNetworkEvent = true)
        {
            if (allowedSlots == null || !allowedSlots.Any())
            {
                return(false);
            }

            bool inSuitableSlot = false;
            bool inWrongSlot    = false;
            int  currentSlot    = -1;

            for (int i = 0; i < capacity; i++)
            {
                if (Items[i] == item)
                {
                    currentSlot = i;
                    if (allowedSlots.Any(a => a.HasFlag(SlotTypes[i])))
                    {
                        inSuitableSlot = true;
                    }
                    else if (!allowedSlots.Any(a => a.HasFlag(SlotTypes[i])))
                    {
                        inWrongSlot = true;
                    }
                }
            }
            //all good
            if (inSuitableSlot && !inWrongSlot)
            {
                return(true);
            }

            //try to place the item in a LimbSlot.Any slot if that's allowed
            if (allowedSlots.Contains(InvSlotType.Any) && item.AllowedSlots.Contains(InvSlotType.Any))
            {
                int freeIndex = CheckIfAnySlotAvailable(item, inWrongSlot);
                if (freeIndex > -1)
                {
                    PutItem(item, freeIndex, user, true, createNetworkEvent);
                    item.Unequip(character);
                    return(true);
                }
            }

            int placedInSlot = -1;

            foreach (InvSlotType allowedSlot in allowedSlots)
            {
                //check if all the required slots are free
                bool free = true;
                for (int i = 0; i < capacity; i++)
                {
                    if (allowedSlot.HasFlag(SlotTypes[i]) && item.AllowedSlots.Any(s => s.HasFlag(SlotTypes[i])) && Items[i] != null && Items[i] != item)
                    {
#if CLIENT
                        if (PersonalSlots.HasFlag(SlotTypes[i]))
                        {
                            hidePersonalSlots = false;
                        }
#endif
                        if (!Items[i].AllowedSlots.Contains(InvSlotType.Any) || !TryPutItem(Items[i], character, new List <InvSlotType> {
                            InvSlotType.Any
                        }, true))
                        {
                            free = false;
#if CLIENT
                            for (int j = 0; j < capacity; j++)
                            {
                                if (slots != null && Items[j] == Items[i])
                                {
                                    slots[j].ShowBorderHighlight(GUI.Style.Red, 0.1f, 0.9f);
                                }
                            }
#endif
                        }
                    }
                }

                if (!free)
                {
                    continue;
                }

                for (int i = 0; i < capacity; i++)
                {
                    if (allowedSlot.HasFlag(SlotTypes[i]) && item.AllowedSlots.Any(s => s.HasFlag(SlotTypes[i])) && Items[i] == null)
                    {
#if CLIENT
                        if (PersonalSlots.HasFlag(SlotTypes[i]))
                        {
                            hidePersonalSlots = false;
                        }
#endif
                        bool removeFromOtherSlots = item.ParentInventory != this;
                        if (placedInSlot == -1 && inWrongSlot)
                        {
                            if (!hideEmptySlot[i] || SlotTypes[currentSlot] != InvSlotType.Any)
                            {
                                removeFromOtherSlots = true;
                            }
                        }

                        PutItem(item, i, user, removeFromOtherSlots, createNetworkEvent);
                        item.Equip(character);
                        placedInSlot = i;
                    }
                }
                if (placedInSlot > -1)
                {
                    break;
                }
            }

            return(placedInSlot > -1);
        }