void Process_greenhouse(Greenhouse g, EnvironmentAnalyzer env, VesselAnalyzer va) { // skip disabled greenhouses if (!g.active) return; // shortcut to resources SimulatedResource ec = Resource("ElectricCharge"); SimulatedResource res = Resource(g.crop_resource); // calculate natural and artificial lighting double natural = env.solar_flux; double artificial = Math.Max(g.light_tolerance - natural, 0.0); // if lamps are on and artificial lighting is required if (artificial > 0.0) { // consume ec for the lamps ec.Consume(g.ec_rate * (artificial / g.light_tolerance), "greenhouse"); } // execute recipe SimulatedRecipe recipe = new SimulatedRecipe(g.part, "greenhouse"); foreach (ModuleResource input in g.resHandler.inputResources) { // WasteAtmosphere is primary combined input if (g.WACO2 && input.name == "WasteAtmosphere") recipe.Input(input.name, env.breathable ? 0.0 : input.rate, "CarbonDioxide"); // CarbonDioxide is secondary combined input else if (g.WACO2 && input.name == "CarbonDioxide") recipe.Input(input.name, env.breathable ? 0.0 : input.rate, ""); // if atmosphere is breathable disable WasteAtmosphere / CO2 else if (!g.WACO2 && (input.name == "CarbonDioxide" || input.name == "WasteAtmosphere")) recipe.Input(input.name, env.breathable ? 0.0 : input.rate, ""); else recipe.Input(input.name, input.rate); } foreach (ModuleResource output in g.resHandler.outputResources) { // if atmosphere is breathable disable Oxygen if (output.name == "Oxygen") recipe.Output(output.name, env.breathable ? 0.0 : output.rate, true); else recipe.Output(output.name, output.rate, true); } recipes.Add(recipe); // determine environment conditions bool lighting = natural + artificial >= g.light_tolerance; bool pressure = va.pressurized || g.pressure_tolerance <= double.Epsilon; bool radiation = (env.landed ? env.surface_rad : env.magnetopause_rad) * (1.0 - va.shielding) < g.radiation_tolerance; // if all conditions apply // note: we are assuming the inputs are satisfied, we can't really do otherwise here if (lighting && pressure && radiation) { // produce food res.Produce(g.crop_size * g.crop_rate, "greenhouse"); // add harvest info res.harvests.Add(Lib.BuildString(g.crop_size.ToString("F0"), " in ", Lib.HumanReadableDuration(1.0 / g.crop_rate))); } }
// execute the recipe public bool Execute(ResourceSimulator sim) { // determine worst input ratio double worst_input = left; if (outputs.Count > 0) { for (int i = 0; i < inputs.Count; ++i) { Resource_recipe.Entry e = inputs[i]; SimulatedResourceView res = sim.Resource(e.name).GetSimulatedResourceView(loaded_part); // handle combined inputs if (e.combined != null) { // is combined resource the primary if (e.combined != "") { Resource_recipe.Entry sec_e = inputs.Find(x => x.name.Contains(e.combined)); SimulatedResourceView sec = sim.Resource(sec_e.name).GetSimulatedResourceView(loaded_part); double pri_worst = Lib.Clamp(res.amount * e.inv_quantity, 0.0, worst_input); if (pri_worst > 0.0) { worst_input = pri_worst; } else { worst_input = Lib.Clamp(sec.amount * sec_e.inv_quantity, 0.0, worst_input); } } } else { worst_input = Lib.Clamp(res.amount * e.inv_quantity, 0.0, worst_input); } } } // determine worst output ratio double worst_output = left; if (inputs.Count > 0) { for (int i = 0; i < outputs.Count; ++i) { Resource_recipe.Entry e = outputs[i]; if (!e.dump) // ignore outputs that can dump overboard { SimulatedResourceView res = sim.Resource(e.name).GetSimulatedResourceView(loaded_part); worst_output = Lib.Clamp((res.capacity - res.amount) * e.inv_quantity, 0.0, worst_output); } } } // determine worst-io double worst_io = Math.Min(worst_input, worst_output); // consume inputs for (int i = 0; i < inputs.Count; ++i) { Resource_recipe.Entry e = inputs[i]; SimulatedResource res = sim.Resource(e.name); // handle combined inputs if (e.combined != null) { // is combined resource the primary if (e.combined != "") { Resource_recipe.Entry sec_e = inputs.Find(x => x.name.Contains(e.combined)); SimulatedResourceView sec = sim.Resource(sec_e.name).GetSimulatedResourceView(loaded_part); double need = (e.quantity * worst_io) + (sec_e.quantity * worst_io); // do we have enough primary to satisfy needs, if so don't consume secondary if (res.amount >= need) { res.Consume(need, name); } // consume primary if any available and secondary else { need -= res.amount; res.Consume(res.amount, name); sec.Consume(need, name); } } } else { res.Consume(e.quantity * worst_io, name); } } // produce outputs for (int i = 0; i < outputs.Count; ++i) { Resource_recipe.Entry e = outputs[i]; SimulatedResourceView res = sim.Resource(e.name).GetSimulatedResourceView(loaded_part); res.Produce(e.quantity * worst_io, name); } // update amount left to execute left -= worst_io; // the recipe was executed, at least partially return(worst_io > double.Epsilon); }