Example #1
0
        /// <summary>
        /// Calculates whether or not all the items can be moved to a destination
        /// and how many must be left at the source if the number exceeds the
        /// maximum number of items a destination can accept
        /// </summary>
        /// <param name="removeItem"></param>
        /// <param name="removeNum"></param>
        /// <param name="removeSource"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        private int CalculateTakeBack(T removeItem, int removeNum, IDragContainer <T> removeSource, IDragContainer <T> destination)
        {
            var takebacknum = 0;

            //Get Maximum number of the item the destination can hold
            var destinationMaxAccept = destination.MaxAcceptable(removeItem);

            //Debug.Log("CT ::" + destinationMaxAccept + " RN::" + removeNum);

            //Check if more of an item is being moved to the destination than can be accepted
            if (destinationMaxAccept < removeNum)
            {
                //Calculate how much must be given back to the source because the number being moved
                //Is greater then the destination will accept
                takebacknum = removeNum - destinationMaxAccept;

                //Get maximum number of the item the source can hold
                var sourceTakebackAccept = removeSource.MaxAcceptable(removeItem);

                //Make sure the source can accept the number of items not being transfered.
                //if not Abort and reset
                if (sourceTakebackAccept < takebacknum)
                {
                    return(0);
                }
            }

            return(takebacknum);
        }
Example #2
0
        private void AttemptSwap(IDragContainer <T> destination, IDragContainer <T> source)
        {
            //Get the items and the number of the items at the destination and source
            int removedSourceNumber      = source.GetNumber();
            T   removedSourceItem        = source.GetItem();
            int removedDestinationNumber = destination.GetNumber();
            T   removedDestinationItem   = destination.GetItem();

            //Remove the items from the source and destination
            source.RemoveItems(removedSourceNumber);
            destination.RemoveItems(removedDestinationNumber);

            //Calculates the amount of items we need to take back
            int sourceTakeBackNumber      = CalculateTakeBack(removedSourceItem, removedSourceNumber, source, destination);
            int destinationTakeBackNumber = CalculateTakeBack(removedDestinationItem, removedDestinationNumber, destination, source);

            //if we need to do takebacks then add the items to the source
            if (sourceTakeBackNumber > 0)
            {
                source.AddItems(removedSourceItem, sourceTakeBackNumber);
                removedSourceNumber -= sourceTakeBackNumber;
            }
            //if we need to do takeabacks then add the items to the destination
            if (destinationTakeBackNumber > 0)
            {
                destination.AddItems(removedDestinationItem, destinationTakeBackNumber);
                removedDestinationNumber -= destinationTakeBackNumber;
            }

            //Abandon this if we cannot swap
            if (source.MaxAcceptable(removedDestinationItem) < removedDestinationNumber ||
                destination.MaxAcceptable(removedSourceItem) < removedSourceNumber ||
                removedSourceNumber == 0)
            {
                //Add the items back to the slots
                if (removedDestinationNumber > 0)
                {
                    destination.AddItems(removedDestinationItem, removedDestinationNumber);
                }
                if (removedSourceNumber > 0)
                {
                    source.AddItems(removedSourceItem, removedSourceNumber);
                }
                return;
            }

            // Do swaps
            if (removedDestinationNumber > 0)
            {
                source.AddItems(removedDestinationItem, removedDestinationNumber);
            }
            if (removedSourceNumber > 0)
            {
                destination.AddItems(removedSourceItem, removedSourceNumber);
            }
        }
        private void AttemptSwap(IDragContainer <T> destination, IDragContainer <T> source)
        {
            // Provisionally remove item from both sides.
            var removedSourceNumber      = source.GetNumber();
            var removedSourceItem        = source.GetItem();
            var removedSourceState       = source.GetModifiers();
            var removedDestinationNumber = destination.GetNumber();
            var removedDestinationItem   = destination.GetItem();
            var removedDestinationState  = destination.GetModifiers();

            source.RemoveItems(removedSourceNumber);
            destination.RemoveItems(removedDestinationNumber);

            var sourceTakeBackNumber      = CalculateTakeBack(removedSourceItem, removedSourceNumber, source, destination);
            var destinationTakeBackNumber = CalculateTakeBack(removedDestinationItem, removedDestinationNumber, destination, source);

            // Do take backs (if needed)
            if (sourceTakeBackNumber > 0)
            {
                source.AddItems(removedSourceItem, sourceTakeBackNumber, removedSourceState);
                removedSourceNumber -= sourceTakeBackNumber;
            }
            if (destinationTakeBackNumber > 0)
            {
                destination.AddItems(removedDestinationItem, destinationTakeBackNumber, removedSourceState);
                removedDestinationNumber -= destinationTakeBackNumber;
            }

            // Abort if we can't do a successful swap
            if (source.MaxAcceptable(removedDestinationItem) < removedDestinationNumber ||
                destination.MaxAcceptable(removedSourceItem) < removedSourceNumber)
            {
                destination.AddItems(removedDestinationItem, removedDestinationNumber, removedDestinationState);
                source.AddItems(removedSourceItem, removedSourceNumber, removedSourceState);
                return;
            }

            // If stack exists in inventory, send both items to destination to stack source and return destination
            if (destination.HasStack(removedSourceItem))
            {
                destination.AddItems(removedSourceItem, removedSourceNumber, removedSourceState);
                destination.AddItems(removedDestinationItem, removedDestinationNumber, removedDestinationState);
                return;
            }

            // Do swaps
            if (removedDestinationNumber > 0)
            {
                source.AddItems(removedDestinationItem, removedDestinationNumber, removedDestinationState);
            }
            if (removedSourceNumber > 0)
            {
                destination.AddItems(removedSourceItem, removedSourceNumber, removedSourceState);
            }
        }
Example #4
0
        private void AttemptSwap(IDragContainer <T> destination, IDragContainer <T> source)
        {
            // Provisionally remove item from both sides.
            var removedSourceNumber      = source.GetNumber();
            var removedSourceItem        = source.GetItem();
            var removedDestinationNumber = destination.GetNumber();
            var removedDestinationItem   = destination.GetItem();

            source.RemoveItems(removedSourceNumber);
            destination.RemoveItems(removedDestinationNumber);

            var sourceTakeBackNumber      = CalculateTakeBack(removedSourceItem, removedSourceNumber, source, destination);
            var destinationTakeBackNumber = CalculateTakeBack(removedDestinationItem, removedDestinationNumber, destination, source);

            // Do take backs (if needed)
            if (sourceTakeBackNumber > 0)
            {
                source.AddItems(removedSourceItem, sourceTakeBackNumber);
                removedSourceNumber -= sourceTakeBackNumber;
            }
            if (destinationTakeBackNumber > 0)
            {
                destination.AddItems(removedDestinationItem, destinationTakeBackNumber);
                removedDestinationNumber -= destinationTakeBackNumber;
            }

            // Abort if we can't do a successful swap
            if (source.MaxAcceptable(removedDestinationItem) < removedDestinationNumber ||
                destination.MaxAcceptable(removedSourceItem) < removedSourceNumber ||
                removedSourceNumber == 0)
            {
                if (removedDestinationNumber > 0)
                {
                    destination.AddItems(removedDestinationItem, removedDestinationNumber);
                }
                if (removedSourceNumber > 0)
                {
                    source.AddItems(removedSourceItem, removedSourceNumber);
                }
                return;
            }

            // Do swaps
            if (removedDestinationNumber > 0)
            {
                source.AddItems(removedDestinationItem, removedDestinationNumber);
            }
            if (removedSourceNumber > 0)
            {
                destination.AddItems(removedSourceItem, removedSourceNumber);
            }
        }
        private void AttemptSwap(IDragContainer <T> destination, IDragContainer <T> source)
        {
            var removedSourceNumber      = source.GetNumber();
            var removedSourceItem        = source.GetItem();
            var removedDestinationNumber = destination.GetNumber();
            var removedDestinationItem   = destination.GetItem();

            source.RemoveItems(removedSourceNumber);
            destination.RemoveItems(removedDestinationNumber);

            var sourceTakeBackNumber      = CalculateTakeBack(removedSourceItem, removedSourceNumber, source, destination);
            var destinationTakeBackNumber = CalculateTakeBack(removedDestinationItem, removedDestinationNumber, destination, source);

            if (sourceTakeBackNumber > 0)
            {
                source.AddItems(removedSourceItem, sourceTakeBackNumber);
                removedSourceNumber -= sourceTakeBackNumber;
            }
            if (destinationTakeBackNumber > 0)
            {
                destination.AddItems(removedDestinationItem, destinationTakeBackNumber);
                removedDestinationNumber -= destinationTakeBackNumber;
            }

            if (source.MaxAcceptable(removedDestinationItem) < removedDestinationNumber ||
                destination.MaxAcceptable(removedSourceItem) < removedSourceNumber ||
                removedSourceNumber == 0)
            {
                if (removedDestinationNumber > 0)
                {
                    destination.AddItems(removedDestinationItem, removedDestinationNumber);
                }
                if (removedSourceNumber > 0)
                {
                    source.AddItems(removedSourceItem, removedSourceNumber);
                }
                return;
            }

            if (removedDestinationNumber > 0)
            {
                source.AddItems(removedDestinationItem, removedDestinationNumber);
            }
            if (removedSourceNumber > 0)
            {
                destination.AddItems(removedSourceItem, removedSourceNumber);
            }
        }
        private int CalculateTakeBack(T removedItem, int removedNumber, IDragContainer <T> removeSource, IDragContainer <T> destination)
        {
            var takeBackNumber           = 0;
            var destinationMaxAcceptable = destination.MaxAcceptable(removedItem);

            if (destinationMaxAcceptable < removedNumber)
            {
                takeBackNumber = removedNumber - destinationMaxAcceptable;

                var sourceTakeBackAcceptable = removeSource.MaxAcceptable(removedItem);

                if (sourceTakeBackAcceptable < takeBackNumber)
                {
                    return(0);
                }
            }
            return(takeBackNumber);
        }
Example #7
0
        /// <summary>
        /// Methods attempts to swap the items in the destination and source container.
        /// </summary>
        /// <param name="destinationContainer"></param>
        /// <param name="sourceContainer"></param>
        private void AttemptSwap(IDragContainer <T> destinationContainer, IDragContainer <T> sourceContainer)
        {
            //Debug.Log("In SWAP");
            //Get the item and its number for both the source and destination containers
            //This is so we know what to swap
            var removedSourceNum      = sourceContainer.GetNumber();
            var removedSourceItem     = sourceContainer.GetItem();
            var removeDestinationNum  = destinationContainer.GetNumber();
            var removeDestinationItem = destinationContainer.GetItem();

            //Debug.Log("S#:: " + removedSourceNum + " D#::" + removeDestinationNum);

            //Perform a series of checks to make sure there are items to swap.
            if (removeDestinationNum == 0 || removedSourceNum == 0)
            {
                return;                                                 ///Added to account either the dest or source not having any items.
            }
            if (destinationContainer.MaxAcceptable(removedSourceItem) < 1)
            {
                return;                                                            ///No need to swap if destination cannot accept source item
            }
            if (sourceContainer.MaxAcceptable(removeDestinationItem) < 1)
            {
                return;                                                           ///No need to swap if source cannot accept destination item
            }
            //Zero out item counts from source and destination
            m_source.RemoveItems(removedSourceNum);
            destinationContainer.RemoveItems(removeDestinationNum);

            var sourceTakebackNum      = CalculateTakeBack(removedSourceItem, removedSourceNum, sourceContainer, destinationContainer);
            var destinationTakebackNum = CalculateTakeBack(removeDestinationItem, removeDestinationNum, destinationContainer, sourceContainer);

            //Do takebacks if numbers being moved are exceeding container maximums
            if (sourceTakebackNum > 0)
            {
                //Put takeback number of source items back into container.
                sourceContainer.AddItems(removedSourceItem, sourceTakebackNum);
                removedSourceNum -= sourceTakebackNum;
            }
            if (destinationTakebackNum > 0)
            {
                //Debug.Log(" D2#::" + destinationTakebackNum);
                //Put takeback number of destination items back into container.
                destinationContainer.AddItems(removeDestinationItem, destinationTakebackNum);
                removeDestinationNum -= destinationTakebackNum;
            }

            //Debug.Log(sourceContainer.MaxAcceptable(removeDestinationItem) +"::" + removeDestinationNum + "||"+ destinationContainer.MaxAcceptable(removedSourceItem) +"::"+ removedSourceNum);
            //Abort if we can't do the swap
            if (sourceContainer.MaxAcceptable(removeDestinationItem) < removeDestinationNum || destinationContainer.MaxAcceptable(removedSourceItem) < removedSourceNum)
            {
                //Debug.Log(" D3#::" + removeDestinationNum);
                //reset everything back to the way it was
                destinationContainer.AddItems(removeDestinationItem, removeDestinationNum);
                sourceContainer.AddItems(removedSourceItem, removedSourceNum);
                return;
            }

            //Do the swap
            if (removeDestinationNum > 0)
            {
                sourceContainer.AddItems(removeDestinationItem, removeDestinationNum);
            }

            if (removedSourceNum > 0)
            {
                destinationContainer.AddItems(removedSourceItem, removedSourceNum);
            }
        }