private static void ProcessApiModule(Vessel v, ProtoPartSnapshot p, ProtoPartModuleSnapshot m, Part part_prefab, PartModule module_prefab, VesselResources resources, Dictionary <string, double> availableResources, List <KeyValuePair <string, double> > resourceChangeRequests, double elapsed_s) { resourceChangeRequests.Clear(); try { string title = BackgroundDelegate.Instance(module_prefab).invoke(v, p, m, module_prefab, part_prefab, availableResources, resourceChangeRequests, elapsed_s); foreach (var cr in resourceChangeRequests) { if (cr.Value > 0) { resources.Produce(v, cr.Key, cr.Value * elapsed_s, ResourceBroker.GetOrCreate(title)); } else if (cr.Value < 0) { resources.Consume(v, cr.Key, -cr.Value * elapsed_s, ResourceBroker.GetOrCreate(title)); } } } catch (Exception ex) { Lib.Log("BackgroundUpdate in PartModule " + module_prefab.moduleName + " excepted: " + ex.Message + "\n" + ex.ToString()); } }
public static void ProcessResources(Vessel v, List <KeyValuePair <string, double> > resources, string title) { ResourceBroker broker = ResourceBroker.GetOrCreate(title); foreach (var p in resources) { if (p.Value < 0) { ResourceCache.Consume(v, p.Key, -p.Value, broker); } else { ResourceCache.Produce(v, p.Key, p.Value, broker); } } }
public void UpdateResourceBrokers(Dictionary <ResourceBroker, double> brokersResAmount, Dictionary <ResourceBroker, double> ruleBrokersRate, double unsupportedBrokersRate, double elapsedSeconds) { ResourceBrokers.Clear(); foreach (KeyValuePair <ResourceBroker, double> p in ruleBrokersRate) { ResourceBroker broker = ResourceBroker.GetOrCreate(p.Key.Id + "Avg", p.Key.Category, Lib.BuildString(p.Key.Title, " (", Local.Generic_AVERAGE, ")")); ResourceBrokers.Add(new ResourceBrokerRate(broker, p.Value)); } foreach (KeyValuePair <ResourceBroker, double> p in brokersResAmount) { ResourceBrokers.Add(new ResourceBrokerRate(p.Key, p.Value / elapsedSeconds)); } if (unsupportedBrokersRate != 0.0) { ResourceBrokers.Add(new ResourceBrokerRate(ResourceBroker.Generic, unsupportedBrokersRate)); } }
public Process(ConfigNode node) { name = Lib.ConfigValue(node, "name", string.Empty); title = Lib.ConfigValue(node, "title", name); broker = ResourceBroker.GetOrCreate(name, ResourceBroker.BrokerCategory.Converter, title); modifiers = Lib.Tokenize(Lib.ConfigValue(node, "modifier", string.Empty), ','); // check that name is specified if (name.Length == 0) { throw new Exception("skipping unnamed process"); } // fix for https://github.com/JadeOfMaar/RationalResources/issues/25 bool skip_resources_validity_check = Lib.ConfigValue(node, "skip_resources_validity_check", false); inputs = new Dictionary <string, double>(); foreach (string input in node.GetValues("input")) { // get parameters List <string> tok = Lib.Tokenize(input, '@'); if (tok.Count != 2) { throw new Exception("malformed input on process " + name); } string input_res = tok[0]; double input_rate = Lib.Parse.ToDouble(tok[1]); // check that resource is specified if (input_res.Length == 0) { throw new Exception("skipping resource-less process " + name); } // check that resource exist if (skip_resources_validity_check || Lib.GetDefinition(input_res) == null) { throw new Exception("resource " + input_res + " doesn't exist for process " + name); } // record input inputs[input_res] = input_rate; } outputs = new Dictionary <string, double>(); foreach (string output in node.GetValues("output")) { // get parameters List <string> tok = Lib.Tokenize(output, '@'); if (tok.Count != 2) { throw new Exception("malformed output on process " + name); } string output_res = tok[0]; double output_rate = Lib.Parse.ToDouble(tok[1]); // check that resource is specified if (output_res.Length == 0) { throw new Exception("skipping resource-less process " + name); } // check that resource exist if (skip_resources_validity_check || Lib.GetDefinition(output_res) == null) { throw new Exception("resource " + output_res + " doesn't exist for process " + name); } // record output outputs[output_res] = output_rate; } cures = new Dictionary <string, double>(); foreach (string output in node.GetValues("cures")) { // get parameters List <string> tok = Lib.Tokenize(output, '@'); if (tok.Count != 2) { throw new Exception("malformed cure on process " + name); } string cure = tok[0]; double cure_rate = Lib.Parse.ToDouble(tok[1]); // check that resource is specified if (cure.Length == 0) { throw new Exception("skipping resource-less process " + name); } // record cure cures[cure] = cure_rate; } // parse dump specs dump = new DumpSpecs(Lib.ConfigValue(node, "dump", "false"), Lib.ConfigValue(node, "dump_valve", "false")); }
/// <summary> /// Call ResourceUpdate on all part modules that have that method /// </summary> public void ResourceUpdate(VesselResources resources, double elapsed_s) { // only do this for loaded vessels. unloaded vessels will be handled in Background.cs if (!Vessel.loaded) { return; } if (resourceUpdateDelegates == null) { resourceUpdateDelegates = new List <ResourceUpdateDelegate>(); foreach (var part in Vessel.parts) { foreach (var module in part.Modules) { if (!module.isEnabled) { continue; } var resourceUpdateDelegate = ResourceUpdateDelegate.Instance(module); if (resourceUpdateDelegate != null) { resourceUpdateDelegates.Add(resourceUpdateDelegate); } } } } if (resourceUpdateDelegates.Count == 0) { return; } List <ResourceInfo> allResources = resources.GetAllResources(Vessel); // there might be some performance to be gained by caching the list of all resource Dictionary <string, double> availableResources = new Dictionary <string, double>(); foreach (var ri in allResources) { availableResources[ri.ResourceName] = ri.Amount; } List <KeyValuePair <string, double> > resourceChangeRequests = new List <KeyValuePair <string, double> >(); foreach (var resourceUpdateDelegate in resourceUpdateDelegates) { resourceChangeRequests.Clear(); string title = resourceUpdateDelegate.invoke(availableResources, resourceChangeRequests); ResourceBroker broker = ResourceBroker.GetOrCreate(title); foreach (var rc in resourceChangeRequests) { if (rc.Value > 0) { resources.Produce(Vessel, rc.Key, rc.Value * elapsed_s, broker); } if (rc.Value < 0) { resources.Consume(Vessel, rc.Key, -rc.Value * elapsed_s, broker); } } } }
public Rule(ConfigNode node) { name = Lib.ConfigValue(node, "name", string.Empty); title = Lib.ConfigValue(node, "title", name); input = Lib.ConfigValue(node, "input", string.Empty); output = Lib.ConfigValue(node, "output", string.Empty); interval = Lib.ConfigValue(node, "interval", 0.0); rate = Lib.ConfigValue(node, "rate", 0.0); ratio = Lib.ConfigValue(node, "ratio", 0.0); degeneration = Lib.ConfigValue(node, "degeneration", 0.0); variance = Lib.ConfigValue(node, "variance", 0.0); individuality = Lib.ConfigValue(node, "individuality", 0.0); modifiers = Lib.Tokenize(Lib.ConfigValue(node, "modifier", string.Empty), ','); breakdown = Lib.ConfigValue(node, "breakdown", false); lifetime = Lib.ConfigValue(node, "lifetime", false); warning_threshold = Lib.ConfigValue(node, "warning_threshold", 0.33); danger_threshold = Lib.ConfigValue(node, "danger_threshold", 0.66); fatal_threshold = Lib.ConfigValue(node, "fatal_threshold", 1.0); warning_message = Lib.ConfigValue(node, "warning_message", string.Empty); danger_message = Lib.ConfigValue(node, "danger_message", string.Empty); fatal_message = Lib.ConfigValue(node, "fatal_message", string.Empty); relax_message = Lib.ConfigValue(node, "relax_message", string.Empty); broker = ResourceBroker.GetOrCreate(name, ResourceBroker.BrokerCategory.Kerbal, title); if (warning_message.Length > 0 && warning_message[0] == '#') { Lib.Log("Broken translation: " + warning_message, Lib.LogLevel.Warning); } if (danger_message.Length > 0 && danger_message[0] == '#') { Lib.Log("Broken translation: " + danger_message, Lib.LogLevel.Warning); } if (fatal_message.Length > 0 && fatal_message[0] == '#') { Lib.Log("Broken translation: " + fatal_message, Lib.LogLevel.Warning); } if (relax_message.Length > 0 && relax_message[0] == '#') { Lib.Log("Broken translation: " + relax_message, Lib.LogLevel.Warning); } // check that name is specified if (name.Length == 0) { throw new Exception("skipping unnamed rule"); } // check that degeneration is not zero if (degeneration <= double.Epsilon) { throw new Exception("skipping zero degeneration rule"); } // check that resources exist if (input.Length > 0 && Lib.GetDefinition(input) == null) { throw new Exception("resource '" + input + "' doesn't exist"); } if (output.Length > 0 && Lib.GetDefinition(output) == null) { throw new Exception("resource '" + output + "' doesn't exist"); } // calculate ratio of input vs output resource if (input.Length > 0 && output.Length > 0 && ratio <= double.Epsilon) { var input_density = Lib.GetDefinition(input).density; var output_density = Lib.GetDefinition(output).density; ratio = Math.Min(input_density, output_density) > double.Epsilon ? input_density / output_density : 1.0; } }
public static void ProduceResource(Vessel v, string resource_name, double quantity, string title) { ResourceCache.Produce(v, resource_name, quantity, ResourceBroker.GetOrCreate(title)); }