/// <summary>transfer per-part resources to the simulator</summary> void Process_part(Part p, string res_name) { SimulatedResourceView res = Resource(res_name).GetSimulatedResourceView(p); res.AddPartResources(p); }
// 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); }