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