Esempio n. 1
0
        public override void OnFixedUpdate()
        {
            if (scoopIsEnabled)
            {
                string atmospheric_resource_name = ORSX_AtmosphericResourceHandler.getAtmosphericResourceName(vessel.mainBody.flightGlobalsIndex, currentresource);
                if (atmospheric_resource_name != null)
                {
                    //Common
                    double resourcedensity = PartResourceLibrary.Instance.GetDefinition(atmospheric_resource_name).density;
                    double respcent        = ORSX_AtmosphericResourceHandler.getAtmosphericResourceContent(vessel.mainBody.flightGlobalsIndex, currentresource);

                    double airdensity = part.vessel.atmDensity / 1000;


                    double powerrequirements = scoopair / 0.15f * ecRequirement;
                    double airspeed          = part.vessel.srf_velocity.magnitude + 40.0;
                    double air = airspeed * airdensity * scoopair / resourcedensity;


                    if (respcent > 0 && vessel.altitude <= ORSX_Helper.getMaxAtmosphericAltitude(vessel.mainBody))
                    {
                        double scoopedAtm    = air * respcent;
                        float  powerreceived = (float)Math.Max(part.RequestResource("ElectricCharge", powerrequirements * TimeWarp.fixedDeltaTime), 0);
                        float  powerpcnt     = (float)(powerreceived / powerrequirements / TimeWarp.fixedDeltaTime);
                        resflowf = (float)ORSX_Helper.fixedRequestResource(part, atmospheric_resource_name, -scoopedAtm * powerpcnt * TimeWarp.fixedDeltaTime);

                        resflowf = -resflowf / TimeWarp.fixedDeltaTime;
                    }
                }
            }
        }
Esempio n. 2
0
        public double getLatLongAbundanceValue(double lat, double lng)
        {
            lat = ORSX_Helper.ToLatitude(lat);
            lng = ORSX_Helper.ToLongitude(lng);

            double len_x    = map.width;
            double len_y    = map.height;
            double origin_x = map.width / 2.0;
            double origin_y = map.height / 2.0;

            double map_x = (lng * len_x / 2 / 180 + origin_x);
            double map_y = (lat * len_y / 2 / 90 + origin_y);

            int pix_x = (int)Math.Round(map_x);
            int pix_y = (int)Math.Round(map_y);

            return(getPixelAbundanceValue(pix_x, pix_y));
        }
        public override void OnFixedUpdate()
        {
            if (IsEnabled)
            {
                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;
                }

                /** RAILS **/
                if (Time.timeSinceLevelLoad < 1.0f || !FlightGlobals.ready)
                {
                    return;
                }

                if (lastUpdateTime == 0.0f)
                {
                    // Just started running
                    lastUpdateTime = Planetarium.GetUniversalTime();
                    return;
                }

                double deltaTime = Math.Min(Planetarium.GetUniversalTime() - lastUpdateTime, Utilities.MaxDeltaTime);
                lastUpdateTime += deltaTime;
                /** RAILS **/


                double electrical_power_provided = 0;
                double desiredPower = power_requirements * TimeWarp.fixedDeltaTime;
                double maxPower     = power_requirements * Math.Max(Utilities.ElectricityMaxDeltaTime, TimeWarp.fixedDeltaTime);

                var powerRequested = Math.Min(desiredPower, maxPower);


                electrical_power_provided = part.RequestResource(resourceToUse, powerRequested);
                if (power_requirements > 0)
                {
                    electrical_power_ratio = electrical_power_provided / desiredPower;
                }
                else
                {
                    if (power_requirements < 0)
                    {
                        IsEnabled = false;
                        return;
                    }
                    else
                    {
                        electrical_power_ratio = 1;
                    }
                }
                double resource_abundance = 0;
                if (vessel.Landed)
                {
                    ORSX_PlanetaryResourcePixel current_resource_abundance_pixel = ORSX_PlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(vessel.mainBody.flightGlobalsIndex, resourceName, vessel.latitude, vessel.longitude);
                    resource_abundance = current_resource_abundance_pixel.getAmount();
                }
                else if (vessel.Splashed)
                {
                    resource_abundance = ORSX_OceanicResourceHandler.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 = -ORSX_Helper.fixedRequestResource(part, resourceName, -extraction_rate / resource_density * deltaTime) / deltaTime;
                }
                else
                {
                    IsEnabled = false;
                }
            }
        }
        public override void OnFixedUpdate()
        {
            if (CollectorIsEnabled)
            {
                string atmospheric_resource_name = ORSX_AtmosphericResourceHandler.getAtmosphericResourceName(vessel.mainBody.flightGlobalsIndex, currentresource);
                if (atmospheric_resource_name != null)
                {
                    //range is 10% of the atmosphere
                    var range = (ORSX_Helper.getMaxAtmosphericAltitude(vessel.mainBody) * 1.1);
                    range += altitudeBonus;
                    double resourcedensity = PartResourceLibrary.Instance.GetDefinition(atmospheric_resource_name).density;
                    double respcent = ORSX_AtmosphericResourceHandler.getAtmosphericResourceContent(vessel.mainBody.flightGlobalsIndex, currentresource);

                    //If we're in the narrow band of the upper atmosphere
                    if (vessel.altitude <= range 
                        && respcent > 0 
                        && vessel.altitude >= ORSX_Helper.getMaxAtmosphericAltitude(vessel.mainBody))
                    {
                        print("[ORSX] PASS ");
                        /** RAILS **/
                        if (Time.timeSinceLevelLoad < 1.0f || !FlightGlobals.ready)
                        {
                            return;
                        }

                        if (lastUpdateTime == 0.0f)
                        {
                            // Just started running
                            lastUpdateTime = Planetarium.GetUniversalTime();
                            return;
                        }

                        double deltaTime = Math.Min(Planetarium.GetUniversalTime() - lastUpdateTime, Utilities.MaxDeltaTime);
                        lastUpdateTime += deltaTime;
                        /** RAILS **/


                        double powerrequirements = particleRate/0.15f*ecRequirement;
                        double desiredPower = powerrequirements * TimeWarp.fixedDeltaTime;
                        double maxPower = powerrequirements * Math.Max(Utilities.ElectricityMaxDeltaTime, TimeWarp.fixedDeltaTime);
                        var powerRequested = Math.Min(desiredPower, maxPower);                        


                        double particles = particleRate/resourcedensity;
                        double CollectedParticles = particles*respcent;
                        float powerreceived =
                            (float)
                                Math.Max(
                                    part.RequestResource("ElectricCharge", powerRequested),
                                    0);
                        float powerpcnt = (float)(powerreceived / desiredPower);
                        resflowf =
                            (float)
                                ORSX_Helper.fixedRequestResource(part, atmospheric_resource_name,
                                    -CollectedParticles*powerpcnt*deltaTime);

                        resflowf = -resflowf/(float)deltaTime;
                    }
                }
            }
        }
Esempio n. 5
0
        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 = my_part.GetConnectedResources(resource_name).ToList();
            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;
            }


            //Prioritise supplying stock ElectricCharge resource
            if (String.Equals(resource_name, FNRESOURCE_MEGAJOULES) && stored_stable_supply > 0)
            {
                List <PartResource> electric_charge_resources = my_part.GetConnectedResources("ElectricCharge").ToList();
                double stock_electric_charge_needed           = 0;
                foreach (PartResource partresource in electric_charge_resources)
                {
                    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 <ORSX_ResourceSuppliable, double> > power_draw_items = power_draws.ToList();

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

            // check engines
            foreach (var power_kvp in power_draw_items)
            {
                ORSX_ResourceSuppliable 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, resource_name);
                }
            }
            // check others
            foreach (var power_kvp in power_draw_items)
            {
                ORSX_ResourceSuppliable 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, resource_name);
                }
            }
            // check radiators
            foreach (var power_kvp in power_draw_items)
            {
                ORSX_ResourceSuppliable 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, 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);
            ORSX_Helper.fixedRequestResource(my_part, resource_name, internl_power_extract);
            powersupply   = 0;
            stable_supply = 0;
            power_supplies.Clear();
            power_draws.Clear();
        }
Esempio n. 6
0
 public static double ImprovedRequestResource(this Part part, String resourcename, double resource_amount)
 {
     return(ORSX_Helper.fixedRequestResource(part, resourcename, resource_amount));
 }