#region Copyright /* -------------------------------------------------------------------------------- This source file is part of Xenocide by Project Xenocide Team For the latest info on Xenocide, see http://www.projectxenocide.com/ This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. -------------------------------------------------------------------------------- */ /* * @file TransactionLineItem.cs * @date Created: 2007/09/24 * @author File creator: David Teviotdale * @author Credits: none */ #endregion #region Using Statements using System; using System.Collections.Generic; using System.Text; using CeGui; using ProjectXenocide.Utils; using ProjectXenocide.Model.Geoscape; using ProjectXenocide.Model.Geoscape.Outposts; using ProjectXenocide.Model.StaticData.Items; #endregion namespace ProjectXenocide.UI.Screens { /// /// A line in a sale or transfer operation /// public class TransactionLineItem { /// /// Constructor (used for transfering) /// /// Item to build line item for /// inventory item will be taken from /// inventory item will be put into [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "will throw if item or source is null")] public TransactionLineItem(Item item, OutpostInventory source, OutpostInventory destination) { this.item = item; this.sourceCount = source.NumberInInventory(item.ItemInfo); this.maxMovable = (item.ItemInfo.IsUnique) ? 1 : sourceCount; if (null != destination) { destinationCount = destination.NumberInInventory(item.ItemInfo); } } /// /// Constructor (used for selling) /// /// Item to build line item for /// inventory item will be taken from public TransactionLineItem(Item item, OutpostInventory source) : this(item, source, null) { } /// /// Remove the items specifed by this lineItem from the inventory /// /// Inventory to remove items from /// to put items into, if supplied. (can be null) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "will throw if inventory or shipment is null")] public void RemoveItems(OutpostInventory inventory, Shipment shipment) { // If item is unique, then we just delete the item. // Otherwise, we need to remove and delete an instance of each item. // This is because clips may contain different amounts of ammo. if (item.ItemInfo.IsUnique && (1 == numMoving)) { if (null != shipment) { shipment.Add(item); item.OnTransfer(); } else { item.OnSell(); } inventory.Remove(item); } else { for (int i = 0; i < numMoving; ++i) { Item temp = item.ItemInfo.FromOutpost(inventory); inventory.Remove(temp); if (null != shipment) { shipment.Add(temp); } } } } /// /// Check if can fit everything on the list into the outpost's inventory /// /// It's not very efficient, but it runs in response to user input, so doesn't need to be /// /// /// inventory to put everything /// the items to try and put in the inventory /// true if list fits into the inventory public static bool CanFit(OutpostInventory inventory, IEnumerable list) { bool canFit = true; // first put everything into the base foreach (TransactionLineItem line in list) { for (int i = 0; i < line.NumMoving; ++i) { if (!inventory.CanFit(line.Item)) { canFit = false; } inventory.AllocateSpace(line.Item); } } // now take them all out again foreach (TransactionLineItem line in list) { for (int i = 0; i < line.NumMoving; ++i) { inventory.ReleaseSpace(line.Item); } } return canFit; } #region Fields /// /// Number of these items in the source inventory /// public int SourceCount { get { return sourceCount; } } /// /// Number of these items in the destination inventory /// public int DestinationCount { get { return destinationCount; } } /// /// Maximum number of items that can be in this line item /// Not the same as number in source inventory, as unique items must /// have their own lines. E.g. If there's 2 Gryphon in a base, then /// each Gryphon must have it's own line item /// public int MaxMovable { get { return maxMovable; } } /// /// Number of these items the player is selling /// public int NumMoving { get { return numMoving; } set { numMoving = value; } } /// /// Income from selling numSelling of these items /// public int Value { get { return numMoving * SellPrice; } } /// /// Cost to ship the item. /// Items cost 1% of their buy or sell price, whichever is greater /// public int ShippingCost { get { return numMoving * Math.Max(item.ItemInfo.BuyPrice, item.ItemInfo.SellPrice) / 100; } } /// /// Name of item, to show to user /// public String Name { get { return item.Name; } } /// /// Income we would get for selling item /// public int SellPrice { get { return item.ItemInfo.SellPrice; } } /// /// "Item" information for this line /// public ItemInfo Item { get { return item.ItemInfo; } } /// /// Type of item /// private Item item; /// /// Number of these items in the source inventory /// private int sourceCount; /// /// Number of these items in the destination inventory /// private int destinationCount; /// /// Maximum number of items that can be in this line item /// Not the same as number in source inventory, as unique items must /// have their own lines. E.g. If there's 2 Gryphon in a base, then /// each Gryphon must have it's own line item /// private int maxMovable; /// /// Number of these items the player is selling/transfering /// private int numMoving; #endregion Fields } }