Пример #1
0
        public CharacterInventory(XElement element, Character character)
            : base(character, element.GetAttributeString("slots", "").Split(',').Count())
        {
            this.character = character;
            IsEquipped     = new bool[capacity];
            SlotTypes      = new InvSlotType[capacity];

            AccessibleWhenAlive = element.GetAttributeBool("accessiblewhenalive", true);

            string[] slotTypeNames = element.GetAttributeString("slots", "").Split(',');
            System.Diagnostics.Debug.Assert(slotTypeNames.Length == capacity);

            for (int i = 0; i < capacity; i++)
            {
                InvSlotType parsedSlotType = InvSlotType.Any;
                slotTypeNames[i] = slotTypeNames[i].Trim();
                if (!Enum.TryParse(slotTypeNames[i], out parsedSlotType))
                {
                    DebugConsole.ThrowError("Error in the inventory config of \"" + character.SpeciesName + "\" - " + slotTypeNames[i] + " is not a valid inventory slot type.");
                }
                SlotTypes[i] = parsedSlotType;
                switch (SlotTypes[i])
                {
                //case InvSlotType.Head:
                //case InvSlotType.OuterClothes:
                case InvSlotType.LeftHand:
                case InvSlotType.RightHand:
                    hideEmptySlot[i] = true;
                    break;
                }
            }

            InitProjSpecific(element);

#if CLIENT
            //clients don't create items until the server says so
            if (GameMain.Client != null)
            {
                return;
            }
#endif

            foreach (XElement subElement in element.Elements())
            {
                if (!subElement.Name.ToString().Equals("item", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                string     itemIdentifier = subElement.GetAttributeString("identifier", "");
                ItemPrefab itemPrefab     = MapEntityPrefab.Find(null, itemIdentifier) as ItemPrefab;
                if (itemPrefab == null)
                {
                    DebugConsole.ThrowError("Error in character inventory \"" + character.SpeciesName + "\" - item \"" + itemIdentifier + "\" not found.");
                    continue;
                }

                Entity.Spawner?.AddToSpawnQueue(itemPrefab, this);
            }
        }
Пример #2
0
        public Pickable(Item item, XElement element)
            : base(item, element)
        {
            allowedSlots = new List <InvSlotType>();

            string slotString = element.GetAttributeString("slots", "Any");

            string[] slotCombinations = slotString.Split(',');
            foreach (string slotCombination in slotCombinations)
            {
                string[]    slots       = slotCombination.Split('+');
                InvSlotType allowedSlot = InvSlotType.None;
                foreach (string slot in slots)
                {
                    if (slot.ToLowerInvariant() == "bothhands")
                    {
                        allowedSlot = InvSlotType.LeftHand | InvSlotType.RightHand;
                    }
                    else
                    {
                        allowedSlot = allowedSlot | (InvSlotType)Enum.Parse(typeof(InvSlotType), slot.Trim());
                    }
                }
                allowedSlots.Add(allowedSlot);
            }

            canBePicked = true;
        }
Пример #3
0
 public bool IsInLimbSlot(Item item, InvSlotType limbSlot)
 {
     for (int i = 0; i < Items.Length; i++)
     {
         if (Items[i] == item && limbSlots[i] == limbSlot)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #4
0
 public int FindLimbSlot(InvSlotType limbSlot)
 {
     for (int i = 0; i < Items.Length; i++)
     {
         if (limbSlots[i] == limbSlot)
         {
             return(i);
         }
     }
     return(-1);
 }
Пример #5
0
 public Item GetItemInLimbSlot(InvSlotType limbSlot)
 {
     for (int i = 0; i < slots.Length; i++)
     {
         if (SlotTypes[i] == limbSlot)
         {
             return(slots[i].FirstOrDefault());
         }
     }
     return(null);
 }
 public bool IsInLimbSlot(Item item, InvSlotType limbSlot)
 {
     for (int i = 0; i < slots.Length; i++)
     {
         if (SlotTypes[i] == limbSlot && slots[i].Contains(item))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #7
0
        public bool IsInLimbSlot(Item item, InvSlotType limbSlot)
        {
            if (limbSlot == (InvSlotType.LeftHand | InvSlotType.RightHand))
            {
                int rightHandSlot = FindLimbSlot(InvSlotType.RightHand);
                int leftHandSlot  = FindLimbSlot(InvSlotType.LeftHand);
                if (rightHandSlot > -1 && slots[rightHandSlot].Contains(item) &&
                    leftHandSlot > -1 && slots[leftHandSlot].Contains(item))
                {
                    return(true);
                }
            }

            for (int i = 0; i < slots.Length; i++)
            {
                if (SlotTypes[i] == limbSlot && slots[i].Contains(item))
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #8
0
 public bool CanBeAutoMovedToCorrectSlots(Item item)
 {
     if (item == null)
     {
         return(false);
     }
     foreach (var allowedSlot in item.AllowedSlots)
     {
         InvSlotType slotsFree = InvSlotType.None;
         for (int i = 0; i < slots.Length; i++)
         {
             if (allowedSlot.HasFlag(SlotTypes[i]) && slots[i].Empty())
             {
                 slotsFree |= SlotTypes[i];
             }
         }
         if (allowedSlot == slotsFree)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #9
0
        public override bool TryPutItem(Item item, int index, bool allowSwapping, Character user, bool createNetworkEvent = true)
        {
            //there's already an item in the slot
            if (Items[index] != null)
            {
                if (Items[index] == item)
                {
                    return(false);
                }

                bool combined = false;
                if (Items[index].Combine(item))
                {
                    System.Diagnostics.Debug.Assert(Items[index] != null);

                    Inventory otherInventory = Items[index].ParentInventory;
                    if (otherInventory != null && otherInventory.Owner != null)
                    {
                    }

                    combined = true;
                }
                //if moving the item between slots in the same inventory
                else if (item.ParentInventory == this && allowSwapping)
                {
                    int currentIndex = Array.IndexOf(Items, item);

                    Item existingItem = Items[index];

                    Items[currentIndex] = null;
                    Items[index]        = null;
                    //if the item in the slot can be moved to the slot of the moved item
                    if (TryPutItem(existingItem, currentIndex, false, user, createNetworkEvent) &&
                        TryPutItem(item, index, false, user, createNetworkEvent))
                    {
                    }
                    else
                    {
                        Items[currentIndex] = null;
                        Items[index]        = null;

                        //swapping the items failed -> move them back to where they were
                        TryPutItem(item, currentIndex, false, user, createNetworkEvent);
                        TryPutItem(existingItem, index, false, user, createNetworkEvent);
                    }
                }

                return(combined);
            }

            if (limbSlots[index] == InvSlotType.Any)
            {
                if (!item.AllowedSlots.Contains(InvSlotType.Any))
                {
                    return(false);
                }
                if (Items[index] != null)
                {
                    return(Items[index] == item);
                }

                PutItem(item, index, user, true, createNetworkEvent);
                return(true);
            }

            InvSlotType placeToSlots = InvSlotType.None;

            bool slotsFree = true;
            List <InvSlotType> allowedSlots = item.AllowedSlots;

            foreach (InvSlotType allowedSlot in allowedSlots)
            {
                if (!allowedSlot.HasFlag(limbSlots[index]))
                {
                    continue;
                }

                for (int i = 0; i < capacity; i++)
                {
                    if (allowedSlot.HasFlag(limbSlots[i]) && Items[i] != null && Items[i] != item)
                    {
                        slotsFree = false;
                        break;
                    }

                    placeToSlots = allowedSlot;
                }
            }

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

            return(TryPutItem(item, user, new List <InvSlotType>()
            {
                placeToSlots
            }, createNetworkEvent));
        }
Пример #10
0
        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));
        }
Пример #11
0
        public override bool TryPutItem(Item item, int index, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true)
        {
            if (index < 0 || index >= Items.Length)
            {
                string errorMsg = "CharacterInventory.TryPutItem failed: index was out of range(" + index + ").\n" + Environment.StackTrace;
                GameAnalyticsManager.AddErrorEventOnce("CharacterInventory.TryPutItem:IndexOutOfRange", GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg);
                return(false);
            }

            //there's already an item in the slot
            if (Items[index] != null)
            {
                if (Items[index] == item)
                {
                    return(false);
                }

                return(base.TryPutItem(item, index, allowSwapping, allowCombine, user, createNetworkEvent));
            }

            if (SlotTypes[index] == InvSlotType.Any)
            {
                if (!item.AllowedSlots.Contains(InvSlotType.Any))
                {
                    return(false);
                }
                if (Items[index] != null)
                {
                    return(Items[index] == item);
                }

                PutItem(item, index, user, true, createNetworkEvent);
                return(true);
            }

            InvSlotType placeToSlots = InvSlotType.None;

            bool slotsFree = true;
            List <InvSlotType> allowedSlots = item.AllowedSlots;

            foreach (InvSlotType allowedSlot in allowedSlots)
            {
                if (!allowedSlot.HasFlag(SlotTypes[index]))
                {
                    continue;
                }

                for (int i = 0; i < capacity; i++)
                {
                    if (allowedSlot.HasFlag(SlotTypes[i]) && Items[i] != null && Items[i] != item)
                    {
                        slotsFree = false;
                        break;
                    }

                    placeToSlots = allowedSlot;
                }
            }

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

            return(TryPutItem(item, user, new List <InvSlotType>()
            {
                placeToSlots
            }, createNetworkEvent));
        }
Пример #12
0
 public void AddToSpawnQueue(ItemPrefab itemPrefab, Inventory inventory, float?condition = null, int?quality = null, Action <Item> onSpawned = null, bool spawnIfInventoryFull = true, bool ignoreLimbSlots = false, InvSlotType slot = InvSlotType.None)
 {
     if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient)
     {
         return;
     }
     if (itemPrefab == null)
     {
         string errorMsg = "Attempted to add a null item to entity spawn queue.\n" + Environment.StackTrace.CleanupStackTrace();
         DebugConsole.ThrowError(errorMsg);
         GameAnalyticsManager.AddErrorEventOnce("EntitySpawner.AddToSpawnQueue3:ItemPrefabNull", GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg);
         return;
     }
     spawnQueue.Enqueue(new ItemSpawnInfo(itemPrefab, inventory, onSpawned, condition, quality)
     {
         SpawnIfInventoryFull = spawnIfInventoryFull,
         IgnoreLimbSlots      = ignoreLimbSlots,
         Slot = slot
     });
 }