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); }