Exemple #1
0
        public override void OnFixedUpdate()
        {
            if (IsEnabled)
            {
                double power_requirements = powerConsumption;
                double extraction_time    = extractionRatePerTon;
                if (vessel.altitude > ORSHelper.getMaxAtmosphericAltitude(vessel.mainBody))
                {
                    IsEnabled = false;
                    return;
                }

                double electrical_power_provided = 0;
                if (resourceManaged)
                {
                    electrical_power_provided = consumeFNResource(power_requirements * TimeWarp.fixedDeltaTime, resourceToUse);
                }
                else
                {
                    electrical_power_provided = part.RequestResource(resourceToUse, power_requirements * TimeWarp.fixedDeltaTime);
                }

                if (power_requirements > 0)
                {
                    electrical_power_ratio = electrical_power_provided / TimeWarp.fixedDeltaTime / power_requirements;
                }
                else
                {
                    if (power_requirements < 0)
                    {
                        IsEnabled = false;
                        return;
                    }
                    else
                    {
                        electrical_power_ratio = 1;
                    }
                }
                double resource_abundance = ORSAtmosphericResourceHandler.getAtmosphericResourceContent(vessel.mainBody.flightGlobalsIndex, resourceName);
                double extraction_rate    = resource_abundance * extraction_time * electrical_power_ratio * part.vessel.atmDensity;
                if (resource_abundance > 0)
                {
                    double resource_density = PartResourceLibrary.Instance.GetDefinition(resourceName).density;
                    //extraction_rate_d = -part.RequestResource(resourceName, -extraction_rate / resource_density * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;
                    extraction_rate_d = -ORSHelper.fixedRequestResource(part, resourceName, -extraction_rate / resource_density * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;
                }
                else
                {
                    IsEnabled = false;
                }
            }
        }
Exemple #2
0
 public static double ImprovedRequestResource(this Part part, String resourcename, double resource_amount)
 {
     return(ORSHelper.fixedRequestResource(part, resourcename, resource_amount));
 }
        public void update()
        {
            stored_supply          = powersupply;
            stored_stable_supply   = stable_supply;
            stored_resource_demand = current_resource_demand;
            double stored_current_demand        = current_resource_demand;
            double stored_current_hp_demand     = high_priority_resource_demand;
            double stored_current_charge_demand = charge_resource_demand;

            stored_charge_demand = charge_resource_demand;

            current_resource_demand       = 0;
            high_priority_resource_demand = 0;
            charge_resource_demand        = 0;

            //Debug.Log ("Early:" + powersupply);

            //stored power
            List <PartResource> partresources = new List <PartResource>();

            my_part.GetConnectedResources(PartResourceLibrary.Instance.GetDefinition(resource_name).id, PartResourceLibrary.Instance.GetDefinition(resource_name).resourceFlowMode, partresources);
            double currentmegajoules = 0;
            double maxmegajoules     = 0;

            foreach (PartResource partresource in partresources)
            {
                currentmegajoules += partresource.amount;
                maxmegajoules     += partresource.maxAmount;
            }
            if (maxmegajoules > 0)
            {
                resource_bar_ratio = currentmegajoules / maxmegajoules;
            }
            else
            {
                resource_bar_ratio = 0;
            }
            double missingmegajoules = maxmegajoules - currentmegajoules;

            powersupply += currentmegajoules;
            //Debug.Log ("Current:" + currentmegajoules);

            double demand_supply_ratio = 0;
            double high_priority_demand_supply_ratio = 0;

            if (high_priority_resource_demand > 0)
            {
                high_priority_demand_supply_ratio = Math.Min((powersupply - stored_current_charge_demand) / stored_current_hp_demand, 1.0);
            }
            else
            {
                high_priority_demand_supply_ratio = 1.0;
            }

            if (stored_current_demand > 0)
            {
                demand_supply_ratio = Math.Min((powersupply - stored_current_charge_demand - stored_current_hp_demand) / stored_current_demand, 1.0);
            }
            else
            {
                demand_supply_ratio = 1.0;
            }

            //Debug.Log ("Late:" + powersupply);

            //Prioritise supplying stock ElectricCharge resource
            if (String.Equals(this.resource_name, ORSResourceManager.FNRESOURCE_MEGAJOULES) && stored_stable_supply > 0)
            {
                List <PartResource> partresources2 = new List <PartResource>();
                my_part.GetConnectedResources(PartResourceLibrary.Instance.GetDefinition("ElectricCharge").id, PartResourceLibrary.Instance.GetDefinition("ElectricCharge").resourceFlowMode, partresources2);
                double stock_electric_charge_needed = 0;
                foreach (PartResource partresource in partresources2)
                {
                    stock_electric_charge_needed += partresource.maxAmount - partresource.amount;
                }
                double power_supplied = Math.Min(powersupply * 1000 * TimeWarp.fixedDeltaTime, stock_electric_charge_needed);
                if (stock_electric_charge_needed > 0)
                {
                    current_resource_demand += stock_electric_charge_needed / 1000.0 / TimeWarp.fixedDeltaTime;
                    charge_resource_demand  += stock_electric_charge_needed / 1000.0 / TimeWarp.fixedDeltaTime;
                }
                if (power_supplied > 0)
                {
                    powersupply += my_part.RequestResource("ElectricCharge", -power_supplied) / 1000 / TimeWarp.fixedDeltaTime;
                }
            }

            //sort by power draw
            //var power_draw_items = from pair in power_draws orderby pair.Value ascending select pair;
            List <KeyValuePair <ORSResourceSuppliable, double> > power_draw_items = power_draws.ToList();

            power_draw_items.Sort(delegate(KeyValuePair <ORSResourceSuppliable, double> firstPair, KeyValuePair <ORSResourceSuppliable, double> nextPair) { return(firstPair.Value.CompareTo(nextPair.Value)); });
            power_draw_list_archive = power_draw_items.ToList();
            power_draw_list_archive.Reverse();

            // check engines
            foreach (KeyValuePair <ORSResourceSuppliable, double> power_kvp in power_draw_items)
            {
                ORSResourceSuppliable ms = power_kvp.Key;

                if (ms.getPowerPriority() == 1)
                {
                    double power = power_kvp.Value;
                    current_resource_demand       += power;
                    high_priority_resource_demand += power;
                    if (flow_type == FNRESOURCE_FLOWTYPE_EVEN)
                    {
                        power = power * high_priority_demand_supply_ratio;
                    }
                    double power_supplied = Math.Max(Math.Min(powersupply, power), 0.0);
                    //Debug.Log (power + ", " + powersupply + "::: " + power_supplied);
                    powersupply -= power_supplied;
                    //notify of supply
                    ms.receiveFNResource(power_supplied, this.resource_name);
                }
            }
            // check others
            foreach (KeyValuePair <ORSResourceSuppliable, double> power_kvp in power_draw_items)
            {
                ORSResourceSuppliable ms = power_kvp.Key;

                if (ms.getPowerPriority() == 2)
                {
                    double power = power_kvp.Value;
                    current_resource_demand += power;
                    if (flow_type == FNRESOURCE_FLOWTYPE_EVEN)
                    {
                        power = power * demand_supply_ratio;
                    }
                    double power_supplied = Math.Max(Math.Min(powersupply, power), 0.0);
                    powersupply -= power_supplied;

                    //notify of supply
                    ms.receiveFNResource(power_supplied, this.resource_name);
                }
            }
            // check radiators
            foreach (KeyValuePair <ORSResourceSuppliable, double> power_kvp in power_draw_items)
            {
                ORSResourceSuppliable ms = power_kvp.Key;
                if (ms.getPowerPriority() == 3)
                {
                    double power = power_kvp.Value;
                    current_resource_demand += power;
                    if (flow_type == FNRESOURCE_FLOWTYPE_EVEN)
                    {
                        power = power * demand_supply_ratio;
                    }
                    double power_supplied = Math.Max(Math.Min(powersupply, power), 0.0);
                    powersupply -= power_supplied;

                    //notify of supply
                    ms.receiveFNResource(power_supplied, this.resource_name);
                }
            }


            powersupply -= Math.Max(currentmegajoules, 0.0);

            internl_power_extract = -powersupply * TimeWarp.fixedDeltaTime;

            pluginSpecificImpl();

            if (internl_power_extract > 0)
            {
                internl_power_extract = Math.Min(internl_power_extract, currentmegajoules);
            }
            else if (internl_power_extract < 0)
            {
                internl_power_extract = Math.Max(internl_power_extract, -missingmegajoules);
            }

            //my_part.RequestResource(this.resource_name, internl_power_extract);
            ORSHelper.fixedRequestResource(my_part, this.resource_name, internl_power_extract);
            powersupply   = 0;
            stable_supply = 0;
            power_supplies.Clear();
            power_draws.Clear();
        }
Exemple #4
0
 public void FixedUpdate()
 {
     if (IsEnabled && HighLogic.LoadedSceneIsFlight)
     {
         double power_requirements = 0;
         double extraction_time    = 0;
         if (vessel.Landed)
         {
             power_requirements = powerConsumptionLand;
             extraction_time    = extractionRateLandPerTon;
         }
         else if (vessel.Splashed)
         {
             power_requirements = powerConsumptionOcean;
             extraction_time    = extractionRateOceanPerTon;
         }
         else
         {
             IsEnabled = false;
             return;
         }
         double electrical_power_provided = 0;
         if (resourceManaged)
         {
             electrical_power_provided = consumeFNResource(power_requirements * TimeWarp.fixedDeltaTime, resourceToUse);
         }
         else
         {
             electrical_power_provided = part.RequestResource(resourceToUse, power_requirements * TimeWarp.fixedDeltaTime);
         }
         if (power_requirements > 0)
         {
             electrical_power_ratio = electrical_power_provided / TimeWarp.fixedDeltaTime / power_requirements;
         }
         else
         {
             if (power_requirements < 0)
             {
                 IsEnabled = false;
                 return;
             }
             else
             {
                 electrical_power_ratio = 1;
             }
         }
         double resource_abundance = 0;
         if (vessel.Landed)
         {
             ORSPlanetaryResourcePixel current_resource_abundance_pixel = ORSPlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(vessel.mainBody.flightGlobalsIndex, resourceName, vessel.latitude, vessel.longitude);
             resource_abundance = current_resource_abundance_pixel.getAmount();
         }
         else if (vessel.Splashed)
         {
             resource_abundance = ORSOceanicResourceHandler.getOceanicResourceContent(vessel.mainBody.flightGlobalsIndex, resourceName);
         }
         double extraction_rate = resource_abundance * extraction_time * electrical_power_ratio;
         if (resource_abundance > 0)
         {
             double resource_density = PartResourceLibrary.Instance.GetDefinition(resourceName).density;
             //extraction_rate_d = -part.RequestResource(resourceName, -extraction_rate / resource_density * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;
             extraction_rate_d = -ORSHelper.fixedRequestResource(part, resourceName, -extraction_rate / resource_density * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;
         }
         else
         {
             IsEnabled = false;
         }
     }
 }