Example #1
0
		public void ItemContainerSubtractionTest()
		{
			// Percentage amount in the ItemStacks
			float p1 = 0.8f;
			float p2 = 0.6f;
			float p3 = p1 + p2 - 1;

			// Stack sizes
			int q1 = (int)(Common.ItemStackSize * p1);
			int q2 = (int)(Common.ItemStackSize * p2);
			int q3 = (int)(Common.ItemStackSize * p3);

			// ItemStacks
			ItemStack i1 = common.GetItemStack(q1);
			ItemStack i2 = common.GetItemStack(q2);
			ItemStack i3 = common.GetItemStack(q3);

			// Container
			Container container = new Container(3);
			
			// Add ItemStacks to the Container
			container.Add(i1, i2);

			// Remove ItemStack from the Container
			container.Remove(i3);

			// Check that the right amount is in the first ItemStack
			Assert.AreEqual(Common.ItemStackSize - q3, container.Get(0).Amount);

			// Check that the right amount is in the second ItemStack
			Assert.AreEqual(q3, container.Get(1).Amount);

			// Check that the right amount is returned
			Assert.AreEqual(Common.ItemStackSize, container.Count(common.GetItem().GetType()));
		}
Example #2
0
		/// <summary>
		/// Internal CraftRecipe.
		/// Performs the actual crafting of the recipe.
		/// </summary>
		/// <param name="recipe">CraftingRecipe</param>
		/// <param name="container">Input container</param>
		/// <param name="simulate">If true; does not subtract the ingredients from the container</param>
		/// <returns></returns>
		private ItemStack[] PerformCraftRecipe(CraftingRecipe recipe, Container container = null, bool simulate = false)
		{
			if (!simulate)
			{
				container.Remove(ItemStack.CloneMultiple(true, recipe.RecipeIngredients));
			}

			return ItemStack.GetPersistantMultiple(recipe.Output);
		}
Example #3
0
		/// <summary>
		/// Craft the first found recipe for the input.
		/// </summary>
		/// <param name="container">Force input container; if null, uses input</param>
		/// <param name="simulate">If true; does not subtract the ingredients from the container</param>
		/// <returns>Result of recipe; if no result, null</returns>
		public ItemStack[] CraftRecipe(Container container = null, bool simulate = false)
		{
			if (container == null)
			{
				container = input;
			}

			CraftingRecipe recipe = GetFirstRecipe(container);

			if (recipe == null) return null;

			return PerformCraftRecipe(recipe, container, simulate);
		}
		public void CorrectShapedRecipeDifferentPlacementTest()
		{
			var stick = FrameworkRegistry.GetItem("Stick");
			var stone = FrameworkRegistry.GetItem("Stone");

			var shapedRecipe = new ShapedTestingRecipe();
			
			var craftingField = new Container(16) { Width = 4 };
			craftingField.Add(1, new ItemStack(stone));
			craftingField.Add(5, new ItemStack(stick));
			craftingField.Add(9, new ItemStack(stick, 12));

			Assert.IsTrue(shapedRecipe.CheckRecipe(craftingField));
		}
Example #5
0
		/// <summary>
		/// Craft a specific CraftingRecipe.
		/// Will check if the CraftingRecipe is allowed on this Crafter.
		/// </summary>
		/// <param name="recipe">CraftingRecipe</param>
		/// <param name="container">Force input container; if null, uses input</param>
		/// <param name="simulate">If true; does not subtract the ingredients from the container</param>
		/// <returns></returns>
		public ItemStack[] CraftRecipe(CraftingRecipe recipe, Container container = null, bool simulate = false)
		{
			if (container == null)
			{
				container = input;
			}

			var recipes = GetRecipes();
			if (!recipes.Contains(recipe))
			{
				return null;
			}

			return PerformCraftRecipe(recipe, container, simulate);
		}
Example #6
0
		public virtual bool CheckRecipe(Container input)
		{
			if (input.GetAllItemStacks().Length != RecipeIngredients.Count(x => x != null)) return false;
			
			var clonedItemStacks = ItemStack.CloneMultiple(true, input.GetAllItemStacks()).ToList();
			for (int i = 0; i < RecipeIngredients.Length; i++)
			{
				var select = clonedItemStacks.Where(x => x.Item.GetType() == recipeIngredients[i].Item.GetType())
					.Where(x => x.Amount >= recipeIngredients[i].Amount)
					.OrderBy(x => x.Amount)
					.FirstOrDefault();

				if (select == null) return false;

				clonedItemStacks.Remove(select);
			}
			return clonedItemStacks.Count == 0;
		}
		public void IncorrectShapelessAmount()
		{
			var stick = FrameworkRegistry.GetItem("Stick");
			var stone = FrameworkRegistry.GetItem("Stone");

			var stoneStack = new ItemStack(stone, 2);
			var stoneStack2 = new ItemStack(stone, 2);
			var stickStack = new ItemStack(stick, 12);

			var recipe = new TestSpadeRecipe();

			var craftingField = new Container(16) { Width = 4 };
			craftingField.Add(0, stoneStack);
			craftingField.Add(4, stickStack);
			craftingField.Add(12, stoneStack2);

			Assert.IsFalse(recipe.CheckRecipe(craftingField));
		}
		public void IncorrectInputItemsPlacementTest()
		{
			var stick = FrameworkRegistry.GetItem("Stick");
			var stone = FrameworkRegistry.GetItem("Stone");

			var shapedRecipe = new ShapedTestingRecipe();

			var craftingField = new Container(16) { Width = 4 };
			craftingField.Add(1, new ItemStack(stone));
			craftingField.Add(6, new ItemStack(stone));
			craftingField.Add(10, new ItemStack(stick,12));
			craftingField.Add(14, new ItemStack(stick));

			Assert.IsFalse(shapedRecipe.CheckRecipe(craftingField));

			craftingField.Remove(1);
			var testItem = new UnitTestItem();
			craftingField.Add(1, new ItemStack(testItem));

			Assert.IsFalse(shapedRecipe.CheckRecipe(craftingField));
		}
Example #9
0
		public void ItemContainerValidatorDeniedTest()
		{
			ItemStack i1 = new ItemStack(FrameworkRegistry.GetItem("Spade"), false, true);
			Container container = new Container(1);

			container.Validator += (int index, ItemStack itemStack, System.ComponentModel.CancelEventArgs args) =>
			{
				args.Cancel = true;
			};

			container.Add(i1);

			Assert.AreEqual(null, container.Get(0));
		}
Example #10
0
		public void ItemContainerEmptyTest()
		{
			ItemStack i1 = new ItemStack(FrameworkRegistry.GetItem("Spade"), false, true);
			Container container = new Container(1);
			Assert.AreEqual(1, container.Items.Length);
			
			container.Add(i1);

			Assert.AreNotEqual(null, container.Get(0));

            container.Get(0).Amount = 0;

			Assert.AreEqual(null, container.Get(0));
		}
Example #11
0
		/// <summary>
		/// Get the first found recipe for the input.
		/// </summary>
		/// <param name="container">Force input container; if null, uses input</param>
		/// <returns>The first found recipe; if no recipe found, null</returns>
		public CraftingRecipe GetFirstRecipe(Container container = null)
		{
			if (container == null)
			{
				container = input;
			}

			var recipes = GetRecipes();

			return recipes.FirstOrDefault(recipe => recipe.CheckRecipe(container));
		}
		public void IncorrectShapedRecipeAmountsTest()
		{
			var stick = FrameworkRegistry.GetItem("Stick");
			var stone = FrameworkRegistry.GetItem("Stone");

			var shapedRecipe = new ShapedTestingRecipe();

			var craftingField = new Container(16) { Width = 4 };
			craftingField.Add(6, new ItemStack(stone));
			craftingField.Add(10, new ItemStack(stick));
			craftingField.Add(14, new ItemStack(stick, 11));

			Assert.IsFalse(shapedRecipe.CheckRecipe(craftingField));
		}
		/// <summary>
		/// Checks the recipe shape against the layout in the crafting layout
		/// </summary>
		/// <param name="input">The crafting grid to check against</param>
		/// <returns></returns>
		public override bool CheckRecipe(Container input)
		{
			// If the recipe is wider than the crafting field, it will fail
			if (input.Width < width) return false;

			// If the amount of item stacks on the recipe and the crafting list aren't the same the recipe will fail.
			if (input.GetAllItemStacks().Length != RecipeIngredients.Count(x => x != null)) return false;

			// Setup a shortcut as we use it a lot
			var inputStacks = input.ItemStacks;

			// Get the first item in the recipe, and the first item in the
			// crafting window, this makes it easier to match them up against each other
			int inputFirstItemIndex = inputStacks.IndexOf(x => x != null);
			int recipeFirstItemIndex = RecipeIngredients.IndexOf(x => x != null);

			// Match the recipe indexes to the crafters indexes
			int inputCurrentPosition = inputFirstItemIndex - recipeFirstItemIndex;

			// Gets the start column and end column of the recipe in comparison to
			// the crafting grid
			int inputColumnStartIndex = inputCurrentPosition % input.Width;
			int inputColumnEndIndex = inputColumnStartIndex + width - 1;

			// Ensure the recipe doesn't extend outside the crafting grid
			if (inputColumnEndIndex >= input.Width) return false;

			// Loop through the ingredients
			for (int i = 0, j = RecipeIngredients.Length; i < j; i++)
			{
				// Ensure the current recipe position is not beyond the length of the crafting grid
				if (inputCurrentPosition >= inputStacks.Length) return false;

				// The current ingredient
				var ingredient = RecipeIngredients[i];

				// If the ingredient is null and the ItemStack at the current position in the
				// input isn't, or vice versa the recipe will fail
				if ((ingredient != null && inputStacks[inputCurrentPosition] == null) ||
					(ingredient == null && inputStacks[inputCurrentPosition] != null))
				{
					return false;
				}

				if (ingredient != null)
				{
					// If the item type doesn't match or there isn't enough items on the current position
					// the recipe will fail
					if (ingredient.Item.GetType() != inputStacks[inputCurrentPosition].Item.GetType())
					{
						return false;
					}
					if (ingredient.Amount > inputStacks[inputCurrentPosition].Amount)
					{
						return false;
					}
				}

				// If we are at the edge of the recipe jump to the next row of the recipe
				if (inputCurrentPosition % input.Width == inputColumnEndIndex)
				{
					inputCurrentPosition += input.Width - width;
				}

				inputCurrentPosition++;
			}

			return true;
		}