Esempio n. 1
0
        /// <summary>
        /// Gets the entire contents of the stack, setting the base stack to null.
        /// </summary>
        /// <returns></returns>
        public virtual ItemStack TakeOutWhole()
        {
            ItemStack stack = itemstack.Clone();

            itemstack.StackSize = 0;
            itemstack           = null;
            OnItemSlotModified(stack);

            return(stack);
        }
Esempio n. 2
0
        List <MatchedSmeltableStackAlloy> mergeAndCompareStacks(ItemStack[] inputStacks, bool useSmeltedWhereApplicable)
        {
            List <MatchedSmeltableStackAlloy> mergedStacks = new List <MatchedSmeltableStackAlloy>();
            List <MetalAlloyIngredient>       ingredients  = new List <MetalAlloyIngredient>(this.Ingredients);

            for (int i = 0; i < inputStacks.Length; i++)
            {
                if (inputStacks[i] == null)
                {
                    continue;
                }

                ItemStack stack     = inputStacks[i];
                float     stackSize = stack.StackSize;

                if (useSmeltedWhereApplicable && stack.Collectible.CombustibleProps?.SmeltedStack != null)
                {
                    stackSize /= stack.Collectible.CombustibleProps.SmeltedRatio;
                    stack      = stack.Collectible.CombustibleProps.SmeltedStack.ResolvedItemstack;
                }

                bool exists = false;
                for (int j = 0; j < mergedStacks.Count; j++)
                {
                    if (stack.Class == mergedStacks[j].stack.Class && stack.Id == mergedStacks[j].stack.Id)
                    {
                        mergedStacks[j].stackSize += stackSize;
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    MetalAlloyIngredient ingred = getIgrendientFor(stack, ingredients);
                    if (ingred == null)
                    {
                        return(null);
                    }

                    mergedStacks.Add(new MatchedSmeltableStackAlloy()
                    {
                        stack = stack.Clone(), ingred = ingred, stackSize = stackSize
                    });
                }
            }

            if (ingredients.Count > 0)
            {
                return(null);
            }

            return(mergedStacks);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a deep copy of this object
        /// </summary>
        /// <returns></returns>
        public JsonItemStack Clone()
        {
            JsonItemStack stack = new JsonItemStack()
            {
                Code = Code.Clone(),
                ResolvedItemstack = ResolvedItemstack?.Clone(),
                StackSize         = StackSize,
                Type = Type,
            };

            if (Attributes != null)
            {
                stack.Attributes = Attributes.Clone();
            }

            return(stack);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns an itemstack with random quantity as configured via the Quantity field
        /// </summary>
        /// <returns></returns>
        public ItemStack GetNextItemStack(float dropQuantityMultiplier = 1f)
        {
            if (ResolvedItemstack == null)
            {
                return(null);
            }

            float val      = Quantity.nextFloat() * dropQuantityMultiplier;
            int   quantity = (int)val + (((val - (int)val) > random.NextDouble()) ? 1 : 0);

            if (quantity <= 0)
            {
                return(null);
            }

            ItemStack cloned = ResolvedItemstack.Clone();

            cloned.StackSize = quantity;

            return(cloned);
        }
Esempio n. 5
0
        public override bool TryGiveItemStack(ItemStack itemstack)
        {
            if (itemstack == null || itemstack.StackSize == 0)
            {
                return(false);
            }

            ItemSlot dummySlot = new DummySlot(null);

            dummySlot.Itemstack = itemstack.Clone();

            ItemStackMoveOperation op = new ItemStackMoveOperation(World, EnumMouseButton.Left, 0, EnumMergePriority.AutoMerge, itemstack.StackSize);

            if (GearInventory != null)
            {
                WeightedSlot wslot = GearInventory.GetBestSuitedSlot(dummySlot, new List <ItemSlot>());
                if (wslot.weight > 0)
                {
                    dummySlot.TryPutInto(wslot.slot, ref op);
                    itemstack.StackSize -= op.MovedQuantity;
                    WatchedAttributes.MarkAllDirty();
                    return(op.MovedQuantity > 0);
                }
            }

            if (LeftHandItemSlot?.Inventory != null)
            {
                WeightedSlot wslot = LeftHandItemSlot.Inventory.GetBestSuitedSlot(dummySlot, new List <ItemSlot>());
                if (wslot.weight > 0)
                {
                    dummySlot.TryPutInto(wslot.slot, ref op);
                    itemstack.StackSize -= op.MovedQuantity;
                    WatchedAttributes.MarkAllDirty();
                    return(op.MovedQuantity > 0);
                }
            }

            return(false);
        }
        public T CloneTo <T>() where T : CraftingRecipeIngredient, new()
        {
            T stack = new T()
            {
                Code               = Code.Clone(),
                Type               = Type,
                Name               = Name,
                Quantity           = Quantity,
                IsWildCard         = IsWildCard,
                IsTool             = IsTool,
                ToolDurabilityCost = ToolDurabilityCost,
                AllowedVariants    = AllowedVariants == null ? null : (string[])AllowedVariants.Clone(),
                ResolvedItemstack  = ResolvedItemstack?.Clone(),
                ReturnedStack      = ReturnedStack?.Clone()
            };

            if (Attributes != null)
            {
                stack.Attributes = Attributes.Clone();
            }

            return(stack);
        }
Esempio n. 7
0
        private bool MatchesShapeLess(ItemSlot[] suppliedSlots, int gridWidth)
        {
            int gridHeight = suppliedSlots.Length / gridWidth;

            if (gridWidth < Width || gridHeight < Height)
            {
                return(false);
            }

            List <KeyValuePair <ItemStack, CraftingRecipeIngredient> > ingredientStacks = new List <KeyValuePair <ItemStack, CraftingRecipeIngredient> >();
            List <ItemStack> suppliedStacks = new List <ItemStack>();

            // Step 1: Merge all stacks from the supplied slots
            for (int i = 0; i < suppliedSlots.Length; i++)
            {
                if (suppliedSlots[i].Itemstack != null)
                {
                    bool found = false;
                    for (int j = 0; j < suppliedStacks.Count; j++)
                    {
                        if (suppliedStacks[j].Satisfies(suppliedSlots[i].Itemstack))
                        {
                            suppliedStacks[j].StackSize += suppliedSlots[i].Itemstack.StackSize;
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        suppliedStacks.Add(suppliedSlots[i].Itemstack.Clone());
                    }
                }
            }


            // Step 2: Merge all stacks from the recipe reference
            for (int i = 0; i < resolvedIngredients.Length; i++)
            {
                CraftingRecipeIngredient ingredient = resolvedIngredients[i];

                if (ingredient == null)
                {
                    continue;
                }

                if (ingredient.IsWildCard)
                {
                    bool foundw = false;
                    int  j      = 0;
                    for (; !foundw && j < suppliedStacks.Count; j++)
                    {
                        ItemStack inputStack = suppliedStacks[j];

                        foundw =
                            ingredient.Type == inputStack.Class &&
                            WildcardUtil.Match(ingredient.Code, inputStack.Collectible.Code, ingredient.AllowedVariants) &&
                            inputStack.StackSize >= ingredient.Quantity
                        ;

                        foundw &= inputStack.Collectible.MatchesForCrafting(inputStack, this, ingredient);
                    }

                    if (!foundw)
                    {
                        return(false);
                    }
                    suppliedStacks.RemoveAt(j - 1);
                    continue;
                }

                ItemStack stack = ingredient.ResolvedItemstack;
                bool      found = false;
                for (int j = 0; j < ingredientStacks.Count; j++)
                {
                    if (ingredientStacks[j].Key.Equals(world, stack, GlobalConstants.IgnoredStackAttributes))
                    {
                        ingredientStacks[j].Key.StackSize += stack.StackSize;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    ingredientStacks.Add(new KeyValuePair <ItemStack, CraftingRecipeIngredient>(stack.Clone(), ingredient));
                }
            }



            if (ingredientStacks.Count != suppliedStacks.Count)
            {
                return(false);
            }


            bool equals = true;

            for (int i = 0; equals && i < ingredientStacks.Count; i++)
            {
                bool found = false;

                for (int j = 0; !found && j < suppliedStacks.Count; j++)
                {
                    found =
                        ingredientStacks[i].Key.Satisfies(suppliedStacks[j]) &&
                        ingredientStacks[i].Key.StackSize <= suppliedStacks[j].StackSize &&
                        suppliedStacks[j].Collectible.MatchesForCrafting(suppliedStacks[j], this, ingredientStacks[i].Value)
                    ;

                    if (found)
                    {
                        suppliedStacks.RemoveAt(j);
                    }
                }

                equals &= found;
            }

            return(equals);
        }