-
Notifications
You must be signed in to change notification settings - Fork 43
Fueled Thrusters #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ArtisticRoomba
wants to merge
16
commits into
EphemeralSpace:master
from
ArtisticRoomba:fueled-thrusters
Closed
Fueled Thrusters #22
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b952e32
Initial commit for system logic
ArtisticRoomba 082fa2e
Fix thrusters not setting benefits
ArtisticRoomba 6a8cf03
Begin initial refactor
ArtisticRoomba 3ec6d90
ModifyThrustContribution helper method
ArtisticRoomba dd3b623
Thruster enable/disable and atomized helper methods
ArtisticRoomba 52232a7
Implement most events and their methods
ArtisticRoomba dd612ac
ExaminedEvent
ArtisticRoomba df5c052
Add Atmos method
ArtisticRoomba 509b4ae
Entity<T> nullability shenanigans
ArtisticRoomba 3888ec7
Complete system replace
ArtisticRoomba cefb473
Complete system replacement
ArtisticRoomba 6aa251c
Fix div/0 errors
ArtisticRoomba 866c32e
Split nonresolve that could cause problems
ArtisticRoomba 35de797
Properly pass in angle to ModifyThrustContribution in TryDisableThruster
ArtisticRoomba d986739
Merge remote-tracking branch 'upstream/master' into fueled-thrusters
ArtisticRoomba f52c1dc
cleanup unused imports
ArtisticRoomba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
Content.Server/Atmos/EntitySystems/AtmoshpereSystem.EsHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| using Content.Shared.Atmos; | ||
|
|
||
| namespace Content.Server.Atmos.EntitySystems; | ||
|
|
||
| public sealed partial class AtmosphereSystem | ||
| { | ||
| /// <summary> | ||
| /// Compares two <see cref="GasMixture"/>s and returns a similarity score between 0 and 1, | ||
| /// based entirely on the percent compositions of the <see cref="GasMixture"/>s. | ||
| /// </summary> | ||
| /// <param name="a">The first <see cref="GasMixture"/> to compare.</param> | ||
| /// <param name="b">The second <see cref="GasMixture"/> to compare.</param> | ||
| /// <returns>A float between 0 and 1 based on how similar the gas mixtures are, | ||
| /// based on percent compositions. This method will also return zero if one compared GasMixture is empty.</returns> | ||
| public float GetGasMixtureSimilarity(GasMixture? a, GasMixture? b) | ||
| { | ||
| // Naive. | ||
| if (a == null || b == null) | ||
| return 0f; | ||
|
|
||
| var aArr = a.Moles; | ||
| var bArr = b.Moles; | ||
| var len = aArr.Length; | ||
|
|
||
| var totalA = NumericsHelpers.HorizontalAdd(aArr); | ||
| var totalB = NumericsHelpers.HorizontalAdd(bArr); | ||
|
|
||
| // If both mixtures are empty, consider them identical. | ||
| if (totalA <= 0f && totalB <= 0f) | ||
| return 1f; | ||
|
|
||
| float numerator = 0f, denominator = Math.Max(totalA, totalB); | ||
|
|
||
| // Normalize each gas mole value by dividing it by its total moles | ||
| for (var i = 0; i < len; i++) | ||
| { | ||
| // Protect against Div/0 for each normalization | ||
| var normalizedA = totalA > 0f ? aArr[i] / totalA : 0f; | ||
| var normalizedB = totalB > 0f ? bArr[i] / totalB : 0f; | ||
|
|
||
| numerator += Math.Min(normalizedA, normalizedB); | ||
| } | ||
|
|
||
| // Fall back to 1 if no valid denominator is present | ||
| // Abs comparison because platform dependent FPU behavior | ||
| return Math.Abs(denominator) < 0.001f ? 1f : numerator; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the percentage (0-1) of each gas in the mixture. | ||
| /// </summary> | ||
| public static float[] GetGasPercentages(GasMixture mixture) | ||
| { | ||
| var total = mixture.TotalMoles; | ||
| var result = new float[mixture.Moles.Length]; | ||
|
|
||
| if (total <= 0f) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| for (var i = 0; i < mixture.Moles.Length; i++) | ||
| { | ||
| result[i] = mixture.Moles[i] / total; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true if all gases in the target mixture are present in the source mixture above a threshold percentage. | ||
| /// </summary> | ||
| public static bool HasGasesAboveThreshold(GasMixture target, GasMixture source, float threshold = 0.01f) | ||
| { | ||
| var targetPerc = GetGasPercentages(target); | ||
| var sourcePerc = GetGasPercentages(source); | ||
|
|
||
| // Check if each gas in the target mixture is present in the source mixture above the threshold. | ||
| // If one doesn't match, we return false. | ||
| for (var i = 0; i < targetPerc.Length; i++) | ||
| { | ||
| if (targetPerc[i] > 0f && sourcePerc[i] < targetPerc[i] - threshold) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the minimum ratio of source/target for all gases present in the target mixture. | ||
| /// </summary> | ||
| public static float GetPurityRatio(GasMixture target, GasMixture source) | ||
| { | ||
| var targetPerc = GetGasPercentages(target); | ||
| var sourcePerc = GetGasPercentages(source); | ||
| var min = 1f; | ||
| for (var i = 0; i < targetPerc.Length; i++) | ||
| { | ||
| if (targetPerc[i] > 0f) | ||
| { | ||
| if (sourcePerc[i] <= 0f) | ||
| return 0f; | ||
| min = Math.Min(min, sourcePerc[i] / targetPerc[i]); | ||
| } | ||
| } | ||
| return Math.Clamp(min, 0f, 1f); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true if any gas in the target mixture is present in the source mixture above a threshold. | ||
| /// </summary> | ||
| public static bool HasAnyRequiredGas(GasMixture target, GasMixture source, float threshold = 0.01f) | ||
| { | ||
| var targetPerc = GetGasPercentages(target); | ||
| var sourcePerc = GetGasPercentages(source); | ||
|
|
||
| for (var i = 0; i < targetPerc.Length; i++) | ||
| { | ||
| if (targetPerc[i] > 0f && sourcePerc[i] > threshold) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit/suggestion: mathematical similarity feels really arbitrary for thrusters, maybe just handle the gasses uniquely instead (esp as this can enable fun interactions like the thrusters oxidizing themselves to death if you put O2 in there and allowing the use of N2O for boosting
which makes no sense they're ION thrusters and do not combust the plasma but it'd be funny)