Exemplo n.º 1
0
        public void Step()
        {
            bool ventActive = false;

            if (deviceActive)
            {
                PipeObject  input  = connectedPipe;
                AtmosObject output = GetComponentInParent <TileObject>().atmos;

                if (input != null || input.GetTotalMoles() > 0)
                {
                    AtmosContainer inputContainer = input.GetAtmosContainer();


                    if (mode == OperatingMode.External)
                    {
                        // If the output pressure is acceptable
                        if (output.GetPressure() <= TargetPressure - 1f)
                        {
                            ventActive = true;
                            float totalMoles = input.GetTotalMoles();

                            // Calculate necessary moles to transfer using PV=nRT
                            float pressureDifference = TargetPressure - output.GetPressure();
                            float transferMoles      = pressureDifference * 1000 * output.GetAtmosContainer().Volume / (output.GetAtmosContainer().GetTemperature() * Gas.gasConstant);

                            // We can not transfer more moles than the machinery allows
                            transferMoles = Mathf.Min(Gas.maxMoleTransfer, transferMoles);

                            // We can't transfer more moles than there are
                            if (transferMoles > totalMoles)
                            {
                                transferMoles = totalMoles;
                            }

                            for (int i = 0; i < Gas.numOfGases; i++)
                            {
                                // Divide the moles according to their percentage
                                float molePerGas = (inputContainer.GetGas(i) / totalMoles) * transferMoles;
                                if (inputContainer.GetGas(i) > 0f)
                                {
                                    input.RemoveGas(i, molePerGas);
                                    output.AddGas(i, molePerGas);
                                }
                            }
                        }
                    }

                    else if (mode == OperatingMode.Internal)
                    {
                        // If the output pressure is acceptable
                        if (input.GetPressure() >= TargetPressure + 1f)
                        {
                            ventActive = true;
                            float totalMoles = input.GetTotalMoles();

                            // Calculate necessary moles to transfer using PV=nRT
                            float pressureDifference = input.GetPressure() - TargetPressure;
                            float transferMoles      = pressureDifference * 1000 * input.GetAtmosContainer().Volume / (input.GetAtmosContainer().GetTemperature() * Gas.gasConstant);

                            // We can not transfer more moles than the machinery allows
                            transferMoles = Mathf.Min(Gas.maxMoleTransfer, transferMoles);

                            // We can't transfer more moles than there are in the input
                            if (transferMoles > totalMoles)
                            {
                                transferMoles = totalMoles;
                            }

                            for (int i = 0; i < Gas.numOfGases; i++)
                            {
                                // Divide the moles according to their percentage
                                float molePerGas = (inputContainer.GetGas(i) / totalMoles) * transferMoles;
                                if (inputContainer.GetGas(i) > 0f)
                                {
                                    input.RemoveGas(i, molePerGas);
                                    output.AddGas(i, molePerGas);
                                }
                            }
                        }
                    }
                }
            }

            // Update the animator
            anim.SetBool("ventActive", ventActive);
            anim.SetBool("deviceActive", deviceActive);
        }
Exemplo n.º 2
0
        public void Step()
        {
            int  numOfTiles  = 0;
            bool scrubActive = false;

            switch (range)
            {
            case Range.Normal:
                numOfTiles = 1;
                break;

            case Range.Extended:
                numOfTiles = 5;
                break;
            }
            if (deviceActive)
            {
                // We loop 1 or 5 times based on the range setting
                for (int i = 0; i < numOfTiles; i++)
                {
                    if (i == 0)
                    {
                        if (input == null)
                        {
                            input = GetComponentInParent <TileObject>().atmos;
                        }
                    }
                    else
                    {
                        input = atmosNeighbours[i - 1];
                    }

                    PipeObject output = connectedPipe;

                    if (input == null || input.GetTotalMoles() == 0 || output == null || mode == OperatingMode.Off)
                    {
                        return;
                    }

                    AtmosContainer inputContainer  = input.GetAtmosContainer();
                    AtmosContainer outputContainer = output.GetAtmosContainer();



                    // If the output pressure is acceptable
                    if (output.GetPressure() <= TargetPressure - 1f)
                    {
                        float totalMoles = input.GetTotalMoles();

                        // Calculate necessary moles to transfer using PV=nRT
                        float pressureDifference = _targetPressure - output.GetPressure();
                        float transferMoles      = pressureDifference * 1000 * output.volume / (output.GetAtmosContainer().GetTemperature() * Gas.gasConstant);

                        // We can not transfer more moles than the machinery allows
                        transferMoles = Mathf.Min(Gas.maxMoleTransfer, transferMoles);

                        // We don't transfer tiny amounts
                        transferMoles = Mathf.Max(transferMoles, Gas.minMoleTransfer);

                        // We can't transfer more moles than there are in the input
                        if (transferMoles > totalMoles)
                        {
                            transferMoles = totalMoles;
                        }

                        for (int j = 0; j < atmosGasses.Length; j++)
                        {
                            if (mode == OperatingMode.Siphoning)
                            {
                                scrubActive = true;

                                // Divide the moles according to their percentage
                                float molePerGas = (inputContainer.GetGas(atmosGasses[j]) / input.GetTotalMoles()) * transferMoles;
                                if (inputContainer.GetGas(atmosGasses[j]) > 0f)
                                {
                                    input.RemoveGas(atmosGasses[j], molePerGas);
                                    output.AddGas(atmosGasses[j], molePerGas);
                                }
                            }

                            // If scrubbing, remove only filtered gas
                            if (mode == OperatingMode.Scrubbing && IsFiltered(atmosGasses[j]))
                            {
                                if (inputContainer.GetGas(atmosGasses[j]) > 0f)
                                {
                                    scrubActive = true;

                                    // To avoid leaving a small amount of a certain gas, we apply the min threshold again
                                    float molePerGas = Mathf.Min(transferMoles, inputContainer.GetGas(atmosGasses[j]));

                                    input.RemoveGas(atmosGasses[j], molePerGas);
                                    output.AddGas(atmosGasses[j], molePerGas);
                                }
                            }
                        }
                    }
                }
            }

            // Update the animator
            anim.SetBool("scrubActive", scrubActive);
            anim.SetBool("deviceActive", deviceActive);
        }