public void AddIngredient(Ingredient ingredient)
 {
     for (int i = 0; i < ingredients.Count; i++) {
         if (ingredients[i].name == ingredient.name) {
             ingredients[i].ratio += ingredient.ratio;
             return;
         }
     }
     ingredients.Add (ingredient);
 }
 public Recipe(ConfigNode recipe)
 {
     var resdict = new Dictionary<string,Ingredient>();
     foreach (ConfigNode.Value res in recipe.values) {
         var ingredient = new Ingredient (res);
         if (ingredient.ratio == 0) {
             continue;
         }
         if (resdict.ContainsKey (ingredient.name)) {
             resdict[ingredient.name].ratio += ingredient.ratio;
         } else {
             resdict[ingredient.name] = ingredient;
         }
     }
     ingredients = new List<Ingredient> (resdict.Values);
 }
 IEnumerator LoadPartRecipes()
 {
     //print ("[EL Recipes] LoadPartRecipes");
     var dbase = GameDatabase.Instance;
     var configurls = dbase.GetConfigs("PART");
     var module_recipes = ExRecipeDatabase.module_recipes;
     foreach (var c in configurls) {
         var node = c.config;
         string name = node.GetValue("name");
         if (String.IsNullOrEmpty (name)) {
             print ("[EL Recipes] skipping unnamed PART");
             continue;
         }
         name = name.Replace('_', '.');
         //print("[EL Recipes] " + name);
         if (node.HasNode ("EL_Recipe")) {
             var recipe_node = node.GetNode ("EL_Recipe");
             var recipe = new PartRecipe (recipe_node);
             ExRecipeDatabase.part_recipes[name] = recipe;
         } else {
             var recipe = new PartRecipe ();
             var modules = node.GetNodes ("MODULE");
             for (int i = 0; i < modules.Length; i++) {
                 var mod_name = modules[i].GetValue ("name");
                 if (String.IsNullOrEmpty (mod_name)) {
                     print ("[EL Recipes] skipping unnamed MODULE");
                     continue;
                 }
                 if (module_recipes.ContainsKey (mod_name)) {
                     //print ("[EL Recipes] adding module " + mod_name);
                     var mod_ingredient = new Ingredient (mod_name, 1);
                     recipe.part_recipe.AddIngredient (mod_ingredient);
                 }
             }
             ExRecipeDatabase.part_recipes[name] = recipe;
         }
         yield return null;
     }
     done = true;
 }
		void ProcessIngredient (Ingredient ingredient, BuildResourceSet rd, bool xfer)
		{
			var res = ingredient.name;
			Recipe recipe = null;

			// If the resource is being transfered from a tank (rather than
			// coming from the part body itself), then a transfer recipe will
			// override a recycle recipe
			if (xfer) {
				recipe = ExRecipeDatabase.TransferRecipe (res);
			}
			if (recipe == null) {
				recipe = ExRecipeDatabase.RecycleRecipe (res);
			}

			if (recipe != null) {
				recipe = recipe.Bake (ingredient.ratio);
				foreach (var ing in recipe.ingredients) {
					if (ing.isReal) {
						var br = new BuildResource (ing);
						rd.Add (br);
					}
				}
			} else {
				if (ExRecipeDatabase.ResourceRecipe (res) != null) {
				} else {
					if (ingredient.isReal) {
						var br = new BuildResource (ingredient);
						rd.Add (br);
					}
				}
			}
		}
        public Recipe Bake(double mass)
        {
            double total = 0;
            for (int i = 0; i < ingredients.Count; i++) {
                total += ingredients[i].ratio;
            }

            Recipe bake = new Recipe ();
            for (int i = 0; i < ingredients.Count; i++) {
                var name = ingredients[i].name;
                var ratio = mass * ingredients[i].ratio / total;
                //Debug.Log(String.Format("Bake: {0} {1} {2} {3}", name, ratio, ingredients[i].ratio, total));
                var ingredient = new Ingredient (name, ratio);
                bake.ingredients.Add (ingredient);
            }
            return bake;
        }
		public RecipeResourceContainer (Part part, Ingredient resource)
		{
			recipe_part = part;
			recipe_resource = resource;
		}