Exemplo n.º 1
0
        public void AddItem(Item i, int numItems)
        {
            int recievedCount = numItems;

            if (!bag.IsInInventory(i.ID) && bag.Count == bag.Size)
            {
                ChatBot.WriteLineToChat("/w " + name + " Inventory is full.");
            }
            else
            {
                // Create a list of the incoming items?
                // say you are adding 500 potions each at a stack of 99
                // Make 5 full stacks of potions and a partial stack of 5 potions.
                // Then add the full stacks to the inventory (if inventory size allows) and look for a partial stack in the inventory to combine the partial stacks

                //single items will be changed into multiple in stacks.
                i.Count = 1;
                int loopCounter = numItems / i.StackSize;
                if (numItems % i.StackSize != 0)
                {
                    loopCounter = (i.Count / i.StackSize) + 1;
                }


                if (i is Potion)
                {
                    Potion tempItem = (Potion)i;
                    if (bag.IsInInventory(i.ID))
                    {
                        // Item is in the inventory already.  We need to make sure the item count is not at max.  If its at max just make a new item.
                        List <Item> items = bag.GetItemsByID(i.ID);
                        for (int j = 0; j < items.Count; j++)
                        {
                            // i.stacksize - items[j].count = amount to refill

                            if (items[j].Count < tempItem.StackSize)
                            {
                                int fillAmount = tempItem.StackSize - items[j].Count;
                                // If the fill amount is larger than the item count being added, just add the new items count.
                                // else fill the stack and continue pulling items
                                if (fillAmount > numItems)
                                {
                                    // Adding items to a stack so it doesn't add to bagsize
                                    items[j].Count += numItems;
                                    break;
                                }
                                else
                                {
                                    //Fill the partial item stack
                                    items[j].Count = tempItem.StackSize;
                                    numItems      -= fillAmount;

                                    //divide the rest into stacks and add them to the inventory
                                    for (int k = 0; k < loopCounter; k++)
                                    {
                                        if (bag.Count == bag.Size)
                                        {
                                            break;
                                        }
                                        Item temp = tempItem.Copy();
                                        temp.Count = temp.StackSize;
                                        bag.AddItem(temp);
                                        numItems -= temp.StackSize;
                                    }

                                    //if there are any items left make a new partial item stack
                                    if (numItems != 0)
                                    {
                                        if (bag.Count == bag.Size)
                                        {
                                            break;
                                        }
                                        Item temp = tempItem.Copy();
                                        temp.Count = numItems;
                                        bag.AddItem(temp);
                                    }
                                    break;
                                }
                            }
                        }
                        if (numItems > 0)
                        {
                            if (i.StackSize > numItems)
                            {
                                Item temp = tempItem.Copy();
                                temp.Count = numItems;
                                bag.AddItem(temp);
                            }
                            else
                            {
                                for (int k = 0; k < loopCounter; k++)
                                {
                                    if (bag.Count == bag.Size)
                                    {
                                        break;
                                    }
                                    Item temp = tempItem.Copy();
                                    temp.Count = temp.StackSize;
                                    bag.AddItem(temp);
                                    numItems -= temp.StackSize;
                                }

                                if (numItems > 0)
                                {
                                    Item temp = tempItem.Copy();
                                    temp.Count = numItems;
                                    bag.AddItem(temp);
                                }
                            }
                        }
                    }

                    //IF the item is not in the inventory
                    else
                    {
                        //divide the rest into stacks and add them to the inventory
                        if (numItems >= tempItem.StackSize)
                        {
                            for (int j = 0; j < loopCounter; j++)
                            {
                                if (bag.Count == bag.Size)
                                {
                                    break;
                                }
                                Item temp = tempItem.Copy();
                                temp.Count = temp.StackSize;
                                bag.AddItem(temp);
                                numItems -= temp.StackSize;
                            }
                        }
                        //if there are any items left make a new partial item stack
                        if (numItems != 0)
                        {
                            if (bag.Count != bag.Size)
                            {
                                Item temp = tempItem.Copy();
                                temp.Count = numItems;
                                bag.AddItem(temp);
                            }
                        }
                    }
                }
                else if (i is Weapon)
                {
                    Weapon tempItem = (Weapon)i;
                    if (bag.IsInInventory(i.ID))
                    {
                        // Item is in the inventory already.  We need to make sure the item count is not at max.  If its at max just make a new item.
                        List <Item> items = bag.GetItemsByID(i.ID);
                        for (int j = 0; j < items.Count; j++)
                        {
                            // i.stacksize - items[j].count = amount to refill

                            if (items[j].Count < tempItem.StackSize)
                            {
                                int fillAmount = tempItem.StackSize - items[j].Count;
                                // If the fill amount is larger than the item count being added, just add the new items count.
                                // else fill the stack and continue pulling items
                                if (fillAmount > numItems)
                                {
                                    // Adding items to a stack so it doesn't add to bagsize
                                    items[j].Count += numItems;
                                    break;
                                }
                                else
                                {
                                    //Fill the partial item stack
                                    items[j].Count = tempItem.StackSize;
                                    numItems      -= fillAmount;

                                    //divide the rest into stacks and add them to the inventory
                                    for (int k = 0; k < loopCounter; k++)
                                    {
                                        if (bag.Count == bag.Size)
                                        {
                                            break;
                                        }
                                        Item temp = tempItem.Copy();
                                        temp.Count = temp.StackSize;
                                        bag.AddItem(temp);
                                        numItems -= temp.StackSize;
                                    }

                                    //if there are any items left make a new partial item stack
                                    if (numItems != 0)
                                    {
                                        if (bag.Count == bag.Size)
                                        {
                                            break;
                                        }
                                        Item temp = tempItem.Copy();
                                        temp.Count = numItems;
                                        bag.AddItem(temp);
                                    }
                                    break;
                                }
                            }
                        }
                        if (numItems > 0)
                        {
                            if (i.StackSize > numItems)
                            {
                                Item temp = tempItem.Copy();
                                temp.Count = numItems;
                                bag.AddItem(temp);
                            }
                            else
                            {
                                for (int k = 0; k < loopCounter; k++)
                                {
                                    if (bag.Count == bag.Size)
                                    {
                                        break;
                                    }
                                    Item temp = tempItem.Copy();
                                    temp.Count = temp.StackSize;
                                    bag.AddItem(temp);
                                    numItems -= temp.StackSize;
                                }

                                if (numItems > 0)
                                {
                                    Item temp = tempItem.Copy();
                                    temp.Count = numItems;
                                    bag.AddItem(temp);
                                }
                            }
                        }
                    }

                    //IF the item is not in the inventory
                    else
                    {
                        //divide the rest into stacks and add them to the inventory
                        if (numItems >= tempItem.StackSize)
                        {
                            for (int j = 0; j < loopCounter; j++)
                            {
                                if (bag.Count == bag.Size)
                                {
                                    break;
                                }
                                Item temp = tempItem.Copy();
                                temp.Count = temp.StackSize;
                                bag.AddItem(temp);
                                numItems -= temp.StackSize;
                            }
                        }
                        //if there are any items left make a new partial item stack
                        if (numItems != 0)
                        {
                            if (bag.Count != bag.Size)
                            {
                                Item temp = tempItem.Copy();
                                temp.Count = numItems;
                                bag.AddItem(temp);
                            }
                        }
                    }
                }
                else if (i is Armor)
                {
                    Armor tempItem = (Armor)i;
                    if (bag.IsInInventory(i.ID))
                    {
                        // Item is in the inventory already.  We need to make sure the item count is not at max.  If its at max just make a new item.
                        List <Item> items = bag.GetItemsByID(i.ID);
                        for (int j = 0; j < items.Count; j++)
                        {
                            // i.stacksize - items[j].count = amount to refill

                            if (items[j].Count < tempItem.StackSize)
                            {
                                int fillAmount = tempItem.StackSize - items[j].Count;
                                // If the fill amount is larger than the item count being added, just add the new items count.
                                // else fill the stack and continue pulling items
                                if (fillAmount > numItems)
                                {
                                    // Adding items to a stack so it doesn't add to bagsize
                                    items[j].Count += numItems;
                                    break;
                                }
                                else
                                {
                                    //Fill the partial item stack
                                    items[j].Count = tempItem.StackSize;
                                    numItems      -= fillAmount;

                                    //divide the rest into stacks and add them to the inventory
                                    for (int k = 0; k < loopCounter; k++)
                                    {
                                        if (bag.Count == bag.Size)
                                        {
                                            break;
                                        }
                                        Item temp = tempItem.Copy();
                                        temp.Count = temp.StackSize;
                                        bag.AddItem(temp);
                                        numItems -= temp.StackSize;
                                    }

                                    //if there are any items left make a new partial item stack
                                    if (numItems != 0)
                                    {
                                        if (bag.Count == bag.Size)
                                        {
                                            break;
                                        }
                                        Item temp = tempItem.Copy();
                                        temp.Count = numItems;
                                        bag.AddItem(temp);
                                    }
                                    break;
                                }
                            }
                        }
                        if (numItems > 0)
                        {
                            if (i.StackSize > numItems)
                            {
                                Item temp = tempItem.Copy();
                                temp.Count = numItems;
                                bag.AddItem(temp);
                            }
                            else
                            {
                                for (int k = 0; k < loopCounter; k++)
                                {
                                    if (bag.Count == bag.Size)
                                    {
                                        break;
                                    }
                                    Item temp = tempItem.Copy();
                                    temp.Count = temp.StackSize;
                                    bag.AddItem(temp);
                                    numItems -= temp.StackSize;
                                }

                                if (numItems > 0)
                                {
                                    Item temp = tempItem.Copy();
                                    temp.Count = numItems;
                                    bag.AddItem(temp);
                                }
                            }
                        }
                    }

                    //IF the item is not in the inventory
                    else
                    {
                        //divide the rest into stacks and add them to the inventory
                        if (numItems >= tempItem.StackSize)
                        {
                            for (int j = 0; j < loopCounter; j++)
                            {
                                if (bag.Count == bag.Size)
                                {
                                    break;
                                }
                                Item temp = tempItem.Copy();
                                temp.Count = temp.StackSize;
                                bag.AddItem(temp);
                                numItems -= temp.StackSize;
                            }
                        }
                        //if there are any items left make a new partial item stack
                        if (numItems != 0)
                        {
                            if (bag.Count != bag.Size)
                            {
                                Item temp = tempItem.Copy();
                                temp.Count = numItems;
                                bag.AddItem(temp);
                            }
                        }
                    }
                }
                if (recievedCount > 1)
                {
                    ChatBot.WriteLineToChat("/w " + name + " Received " + recievedCount + " " + i.Name + "s");
                }
                else
                {
                    ChatBot.WriteLineToChat("/w " + name + " Received " + recievedCount + " " + i.Name);
                }
                if (i is Weapon)
                {
                    Weapon temp = (Weapon)i;
                    dps += temp.attack * numItems;
                }
                else if (i is Armor)
                {
                    Armor temp = (Armor)i;
                    defense += temp.defense * numItems;
                }
            }
        }