public long ProduceMaterial(string materialName, long amount, Dictionary <string, long> materialBag) { // Find the recipe. NanoFactoryRecipe recipeMaterial = this.FindRecipe(materialName); // Is this already in the leftover ingredients? NanoFactoryIngredient ingredientOnHand = this.CheckToSeeIfIHaveItAlready(materialName, amount, materialBag); long amountLeftToProduce = (amount - ingredientOnHand.amount); if (amountLeftToProduce == 0) { return(0); // Cost zero ORE! } // If this ingredient is primary, go ahead and make it with ORE. if (IsAPrimaryMaterial(materialName, "ORE")) { long oreCost = 0; long amountOfPrimaryProduced = 0; while (amountLeftToProduce > amountOfPrimaryProduced) { //Program.Assert(recipeMaterial.inputs[0].name == "ORE", "ERROR!"); // Program.Assert(recipeMaterial.inputs.Count == 1, "ERROR!"); oreCost += recipeMaterial.inputs[0].amount; amountOfPrimaryProduced += recipeMaterial.output.amount; } this.AddToCollection(materialName, amountOfPrimaryProduced, materialBag); return(oreCost); } // If we have made it here, we need to produce a non-primary material. long totalOreCost = 0; int howManyTimesDoRunRecipe = (int)Math.Ceiling((((double)amountLeftToProduce) / (double)recipeMaterial.output.amount)); foreach (NanoFactoryIngredient inputIngredient in recipeMaterial.inputs) { long amountPerRun = inputIngredient.amount; long amountNeeded = howManyTimesDoRunRecipe * amountPerRun; totalOreCost += this.ProduceMaterial(inputIngredient.name, amountNeeded, materialBag); this.RemoveFromCollection(inputIngredient.name, amountNeeded, materialBag); } // What did we make? long amountProduced = (howManyTimesDoRunRecipe * recipeMaterial.output.amount); this.AddToCollection(recipeMaterial.output.name, amountProduced, materialBag); //Console.WriteLine("Produced " + amountProduced + " " + materialName); return(totalOreCost); }
public void ParseRecipe(string recipe) { // 1 A, 2 B, 3 C => 2 D string[] parts = recipe.Split("=>"); string partsInput = parts[0]; string partsOutput = parts[1]; // output string[] partsOutput2 = partsOutput.Trim().Split(' '); this.output = new NanoFactoryIngredient(partsOutput2[1], Convert.ToInt32(partsOutput2[0])); // input string[] partsInput2 = partsInput.Split(','); foreach (string partsInput3 in partsInput2) { string[] partsInput4 = partsInput3.Trim().Split(' '); this.inputs.Add(new NanoFactoryIngredient(partsInput4[1], Convert.ToInt32(partsInput4[0]))); } }