From c5399c2312b9da3aa178b9864e99ac5893013052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henri=20Hyyryl=C3=A4inen?= Date: Thu, 12 Dec 2024 15:22:58 +0200 Subject: [PATCH] Added the simpler compound move multiplier calculation from adQuid's PR #5693 --- simulation_parameters/Constants.cs | 3 + .../world_effects/CompoundDiffusionEffect.cs | 60 ++++++++++++------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/simulation_parameters/Constants.cs b/simulation_parameters/Constants.cs index e3238d16af..8d305ff8f2 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 d3b16c8583..0428d25dd4 100644 --- a/src/general/world_effects/CompoundDiffusionEffect.cs +++ b/src/general/world_effects/CompoundDiffusionEffect.cs @@ -19,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() { } @@ -28,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; @@ -161,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) {