Esempio n. 1
0
        /// <summary>
        /// Gets the number of items that can be merged from the specified cluster into the current.
        /// </summary>
        /// <param name="other">The cluster to merge from.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="other"/> is <see langword="null"/>.
        /// </exception>
        /// <returns>
        /// <see langword="true"/> if the cluster is compatible and there is remaining capacity to contain more items; otherwise, <see langword="false"/>.
        /// </returns>
        public bool CanMergeWith(ItemCluster <TItemInfo> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            if (!Prototype.Equals(other.Prototype))
            {
                return(false);
            }

            if (Quantity == ClusterCapacity)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Attempts to merge the specified ItemCluster with the current.
        /// </summary>
        /// <param name="other">The ItemCluster to merge.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="other"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown if <paramref name="other"/> is for a different item prototype than the current instance.
        /// </exception>
        /// <returns>the number of items that were carried over to the current cluster.</returns>
        public int MergeWith(ItemCluster <TItemInfo> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            // Note: This is actually not quite necessary,
            // since Prototypes are immutable and only supplied from the cache,
            // we could go with just identity check.
            if (!Prototype.Equals(other.Prototype))
            {
                throw new ArgumentException(ModelStrings.DifferentItemClusterPrototype, nameof(other));
            }

            int freeSpace         = ClusterCapacity - Quantity;
            int availableQuantity = Math.Min(freeSpace, other.Quantity);

            Quantity       += availableQuantity;
            other.Quantity -= availableQuantity;

            return(availableQuantity);
        }