diff --git a/simulation_parameters/Constants.cs b/simulation_parameters/Constants.cs index e3238d16af7..8d305ff8f2b 100644 --- a/simulation_parameters/Constants.cs +++ b/simulation_parameters/Constants.cs @@ -1188,6 +1188,9 @@ public static class Constants public const float GLUCOSE_MIN = 0.0f; // Tweak variable for how fast compounds diffuse between patches + public const float COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT_SIMPLE = 0.8f; + + // More complex square root distance movement calculation variables: public const float COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT = 1; public const float COMPOUND_DIFFUSE_BASE_DISTANCE = 1; diff --git a/src/general/world_effects/CompoundDiffusionEffect.cs b/src/general/world_effects/CompoundDiffusionEffect.cs index b4ef79b0402..0428d25dd45 100644 --- a/src/general/world_effects/CompoundDiffusionEffect.cs +++ b/src/general/world_effects/CompoundDiffusionEffect.cs @@ -4,8 +4,9 @@ using Newtonsoft.Json; /// -/// An effect diffusing specially marked compounds between patches (and also takes ocean depth into account) in -/// contrast to +/// An effect diffusing specially marked compounds between patches (and also takes ocean depth into account). This +/// operates on specific compounds as it causes a bit of a mess and unintended effects if all compounds are always +/// allowed to move. /// [JSONDynamicTypeAllowed] public class CompoundDiffusionEffect : IWorldEffect @@ -18,6 +19,11 @@ public CompoundDiffusionEffect(GameWorld targetWorld) this.targetWorld = targetWorld; } + /// + /// If true this uses a more complex move modifier formula based on the square root distance between patches + /// + public bool UseDistanceMoveModifier { get; set; } + public void OnRegisterToWorld() { } @@ -27,27 +33,6 @@ public void OnTimePassed(double elapsed, double totalTimePassed) HandlePatchCompoundDiffusion(); } - private static (float Ambient, float Density) CalculateWantedMoveAmounts(Patch sourcePatch, Patch adjacent, - KeyValuePair compound) - { - // Apply patch distance to diminish how much to move (to make ocean bottoms receive less surface - // resources like oxygen) - // TODO: improve the formula here as sqrt isn't the best - float moveModifier = Constants.COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT / - MathF.Sqrt(Constants.COMPOUND_DIFFUSE_BASE_DISTANCE + Math.Abs(sourcePatch.Depth[0] - adjacent.Depth[0])); - - adjacent.Biome.TryGetCompound(compound.Key, CompoundAmountType.Biome, - out var destinationAmount); - - // Calculate compound amounts to move - // At most half of the surplus can move as otherwise the source patch may end up with fewer compounds than - // the destination - float ambient = (compound.Value.Ambient - destinationAmount.Ambient) * 0.5f; - - float density = (compound.Value.Density - destinationAmount.Density) * 0.5f; - return (ambient * moveModifier, density * moveModifier); - } - private void HandlePatchCompoundDiffusion() { var simulationParameters = SimulationParameters.Instance; @@ -160,6 +145,40 @@ private void HandlePatchCompoundDiffusion() } } + private (float Ambient, float Density) CalculateWantedMoveAmounts(Patch sourcePatch, Patch adjacent, + KeyValuePair compound) + { + // Apply patch distance to diminish how much to move (to make ocean bottoms receive less surface + // resources like oxygen) + + float moveModifier; + if (UseDistanceMoveModifier) + { + // TODO: improve the formula here as sqrt isn't the best + moveModifier = Constants.COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT / + MathF.Sqrt( + Constants.COMPOUND_DIFFUSE_BASE_DISTANCE + Math.Abs(sourcePatch.Depth[0] - adjacent.Depth[0])); + } + else + { + // TODO: as this is basically just a constraint on how many patches away something is, should cases where + // patches are "skipped" in a vertical stack be divided by the number of skipped patches here to have the + // same end result? + moveModifier = Constants.COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT_SIMPLE; + } + + adjacent.Biome.TryGetCompound(compound.Key, CompoundAmountType.Biome, + out var destinationAmount); + + // Calculate compound amounts to move + // At most half of the surplus can move as otherwise the source patch may end up with fewer compounds than + // the destination + float ambient = (compound.Value.Ambient - destinationAmount.Ambient) * 0.5f; + + float density = (compound.Value.Density - destinationAmount.Density) * 0.5f; + return (ambient * moveModifier, density * moveModifier); + } + private void AddMove(Compound compound, Patch patch, BiomeCompoundProperties amount, Dictionary> result) { diff --git a/src/general/world_effects/PhotosynthesisProductionEffect.cs b/src/general/world_effects/PhotosynthesisProductionEffect.cs index 3c50a06d931..065d31ac3d4 100644 --- a/src/general/world_effects/PhotosynthesisProductionEffect.cs +++ b/src/general/world_effects/PhotosynthesisProductionEffect.cs @@ -4,7 +4,7 @@ /// /// Creates oxygen based on photosynthesizers (and removes carbon). And does the vice versa for oxygen consumption -/// to balance things out. This is kind of a simplified version of +/// to balance things out. /// [JSONDynamicTypeAllowed] public class PhotosynthesisProductionEffect : IWorldEffect