Beispiel #1
0
        private void TakeCommodities(Container c, Type type, ref int amount)
        {
            if (c == null)
            {
                return;
            }

            Item[]      items  = c.FindItemsByType(typeof(CommodityDeed));
            List <Item> toSell = new List <Item>();

            foreach (Item item in items)
            {
                CommodityDeed commodityDeed = item as CommodityDeed;

                if (commodityDeed != null && commodityDeed.Commodity != null && commodityDeed.Commodity.GetType() == type)
                {
                    Item commodity = commodityDeed.Commodity;

                    if (commodity.Amount <= amount)
                    {
                        toSell.Add(item);
                        amount -= commodity.Amount;
                    }
                    else
                    {
                        CommodityDeed newDeed = new CommodityDeed();
                        Item          newItem = Loot.Construct(type);
                        newItem.Amount = amount;
                        newDeed.SetCommodity(newItem);

                        commodity.Amount -= amount;
                        commodityDeed.InvalidateProperties();
                        toSell.Add(newDeed);
                        amount = 0;
                    }
                }
            }

            foreach (Item item in toSell)
            {
                AddInventory(null, item);
            }
        }
            /// <summary>
            /// Core procedure of the donation system for stackable items.
            /// </summary>
            /// <param name="m">The player whose backpack the items will be taken</param>
            /// <param name="type">The type of the donation item.</param>
            /// <param name="ratio">Number of points per unit of the item.</param>
            /// <param name="amount">How much is he going to donate.</param>
            private void Donate(Mobile m, Type type, double ratio, int amount)
            {
                if (amount <= 0)
                {
                    // That is not a valid donation quantity.
                    m.SendLocalizedMessage(1073181);

                    return;
                }

                int sum    = GetTotalAmountByType(m, type);
                int points = (int)(amount * ratio);

                if (sum < amount)
                {
                    // You do not have enough to make a donation of that magnitude!
                    m.SendLocalizedMessage(1073182);
                }
                else if (points <= 0)
                {
                    // Your donation is too small to award any points.  Try giving a larger donation.
                    m.SendLocalizedMessage(1073166);
                }
                else
                {
                    List <Item> packItems = new List <Item>(m.Backpack.Items);

                    for (int i = 0; amount > 0 && i < packItems.Count; i++)
                    {
                        Item item = packItems[i];

                        CommodityDeed deed = null;

                        if (item is CommodityDeed)
                        {
                            deed = item as CommodityDeed;
                            item = deed.Commodity;
                        }

                        if (item != null && item.GetType() == type)
                        {
                            if (amount >= item.Amount)
                            {
                                amount -= item.Amount;
                                item.Delete();

                                if (deed != null)
                                {
                                    deed.Delete();
                                }
                            }
                            else
                            {
                                item.Amount -= amount;
                                amount       = 0;

                                if (deed != null)
                                {
                                    deed.InvalidateProperties();
                                }
                            }
                        }
                    }

                    m_Collection.Award(m, points);
                }
            }