Beispiel #1
0
        /// <summary>
        /// Calculates the minimum battery charge across the circuit, used for manual
        /// generators to schedule the chore.
        /// </summary>
        /// <param name="circuit">The circuit to update.</param>
        /// <returns>true if any battery has charge, or false otherwise.</returns>
        private static bool GetMinimumBatteryCharge(ref CircuitInfo circuit)
        {
            float batteryLevel = 1.0f, charge;
            var   batteries         = circuit.batteries;
            var   inputTransformers = circuit.inputTransformers;
            int   n         = batteries.Count;
            bool  hasCharge = false;

            for (int i = 0; i < n; i++)
            {
                var battery = batteries[i];
                charge = battery.PercentFull;
                if (battery.JoulesAvailable > 0.0f)
                {
                    hasCharge = true;
                }
                if (batteryLevel > charge)
                {
                    batteryLevel = charge;
                }
            }
            n = inputTransformers.Count;
            for (int i = 0; i < n; i++)
            {
                charge = inputTransformers[i].PercentFull;
                if (batteryLevel > charge)
                {
                    batteryLevel = charge;
                }
            }
            circuit.minBatteryPercentFull = batteryLevel;
            return(hasCharge);
        }
Beispiel #2
0
        /// <summary>
        /// Adds electrical circuits if needed to match the number of wire networks.
        /// </summary>
        /// <param name="instance">The circuit manager to initialize.</param>
        /// <param name="electricalSystem">The current electrical network.</param>
        private static void InitNetworks(CircuitManager instance,
                                         UtilityNetworkManager <ElectricalUtilityNetwork, Wire> electricalSystem)
        {
            var networks = electricalSystem.GetNetworks();
            var circuits = instance.circuitInfo;
            int nGroups = (int)Wire.WattageRating.NumRatings, nNetworks = networks.Count;

            while (circuits.Count < nNetworks)
            {
                var newInfo = new CircuitInfo {
                    generators         = new List <Generator>(16),
                    consumers          = new List <IEnergyConsumer>(32),
                    batteries          = new List <Battery>(16),
                    inputTransformers  = new List <Battery>(8),
                    outputTransformers = new List <Generator>(16)
                };
                var wireLinks = new List <WireUtilityNetworkLink> [nGroups];
                for (int i = 0; i < nGroups; i++)
                {
                    wireLinks[i] = new List <WireUtilityNetworkLink>(8);
                }
                newInfo.bridgeGroups = wireLinks;
                circuits.Add(newInfo);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Populates the list of generators that are generating power.
        /// </summary>
        /// <param name="circuit">The circuit to update.</param>
        /// <param name="activeGenerators">The location where the active generators will be stored.</param>
        /// <returns>true if any generator is active, or a power transformer is able to provide
        /// power; or false otherwise.</returns>
        private static bool GetActiveGenerators(ref CircuitInfo circuit,
                                                List <Generator> activeGenerators)
        {
            var  generators         = circuit.generators;
            var  outputTransformers = circuit.outputTransformers;
            int  n = generators.Count;
            bool hasEnergy;

            activeGenerators.Clear();
            // Take from generators first
            for (int i = 0; i < n; i++)
            {
                var generator = generators[i];
                if (generator != null && generator.JoulesAvailable > 0.0f)
                {
                    activeGenerators.Add(generator);
                }
            }
            activeGenerators.Sort(GeneratorChargeComparer.Instance);
            hasEnergy = activeGenerators.Count > 0;
            // Then from transformers that output onto this grid
            n = outputTransformers.Count;
            for (int i = 0; i < n && !hasEnergy; i++)
            {
                var transformer = outputTransformers[i];
                if (transformer != null && transformer.JoulesAvailable > 0.0f)
                {
                    hasEnergy = true;
                }
            }
            return(hasEnergy);
        }
Beispiel #4
0
        /// <summary>
        /// Supplies as many consumers on the circuit as there is energy to do so.
        /// </summary>
        /// <param name="circuit">The circuit to update.</param>
        /// <param name="activeGenerators">The list of generators which are actively producing power.</param>
        /// <returns>The total wattage used for overloading purposes.</returns>
        private static float SupplyConsumers(ref CircuitInfo circuit,
                                             IList <Generator> activeGenerators)
        {
            var consumers = circuit.consumers;
            var batteries = circuit.batteries;
            var batteryStatus = new ConsumerRun(batteries);
            var outputTransformers = circuit.outputTransformers;
            int nc = consumers.Count, nb = batteries.Count, firstGenerator = 0,
                firstTransformer = 0;
            float usage          = 0.0f;

            batteries.Sort(BatteryChargeComparer.Instance);
            for (int i = 0; i < nc; i++)
            {
                var   consumer = consumers[i];
                float energy   = consumer.WattsUsed * UpdateManager.SecondsPerSimTick;
                if (energy > 0.0f)
                {
                    float e0 = energy;
                    energy = DrainFirstAvailable(energy, activeGenerators, ref firstGenerator);
                    if (energy > 0.0f)
                    {
                        energy = DrainFirstAvailable(energy, outputTransformers,
                                                     ref firstTransformer);
                    }
                    if (energy > 0.0f)
                    {
                        energy = batteryStatus.Power(energy);
                    }
                    if (REPORT && energy < e0)
                    {
                        ReportManager.Instance.ReportValue(ReportManager.ReportType.
                                                           EnergyCreated, energy - e0, consumer.Name);
                    }
                    usage += e0 - energy;
                    consumer.SetConnectionStatus(energy == 0.0f ? ConnectionStatus.Powered :
                                                 ConnectionStatus.Unpowered);
                }
                else
                {
                    // Base game had a condition that was always true when reached
                    consumer.SetConnectionStatus(ConnectionStatus.Powered);
                }
            }
            batteryStatus.Finish();
            return(usage / UpdateManager.SecondsPerSimTick);
        }
Beispiel #5
0
        /// <summary>
        /// Fills battery and transformer storage on the circuit.
        /// </summary>
        /// <param name="circuit">The circuit to update.</param>
        /// <returns>The total wattage used for overloading purposes.</returns>
        private static float SupplyStorage(ref CircuitInfo circuit)
        {
            var batteries = circuit.batteries;
            var generators = circuit.generators;
            var inputTransformers = circuit.inputTransformers;
            var outputTransformers = circuit.outputTransformers;
            int firstGenerator = 0, firstTransformer = 0, firstBattery = 0;

            batteries.Sort(BatterySpaceComparer.Instance);
            inputTransformers.Sort(BatterySpaceComparer.Instance);
            generators.Sort(GeneratorChargeComparer.Instance);
            float usage = ChargeDrainFirst(inputTransformers, generators, ref firstGenerator);

            usage += ChargeDrainFirst(inputTransformers, outputTransformers,
                                      ref firstTransformer);
            if (batteries.Count > 0)
            {
                ChargeBatteries(batteries, generators, firstGenerator, ref firstBattery);
                ChargeBatteries(batteries, outputTransformers, firstTransformer,
                                ref firstBattery);
            }
            return(usage / UpdateManager.SecondsPerSimTick);
        }