Skip to content

Commit

Permalink
Added the simpler compound move multiplier calculation
Browse files Browse the repository at this point in the history
from adQuid's PR #5693
  • Loading branch information
hhyyrylainen committed Dec 12, 2024
1 parent f25f9e5 commit c5399c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
3 changes: 3 additions & 0 deletions simulation_parameters/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
60 changes: 39 additions & 21 deletions src/general/world_effects/CompoundDiffusionEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public CompoundDiffusionEffect(GameWorld targetWorld)
this.targetWorld = targetWorld;
}

/// <summary>
/// If true this uses a more complex move modifier formula based on the square root distance between patches
/// </summary>
public bool UseDistanceMoveModifier { get; set; }

public void OnRegisterToWorld()
{
}
Expand All @@ -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, BiomeCompoundProperties> 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;
Expand Down Expand Up @@ -161,6 +145,40 @@ private void HandlePatchCompoundDiffusion()
}
}

private (float Ambient, float Density) CalculateWantedMoveAmounts(Patch sourcePatch, Patch adjacent,
KeyValuePair<Compound, BiomeCompoundProperties> 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<Patch, Dictionary<Compound, BiomeCompoundProperties>> result)
{
Expand Down

0 comments on commit c5399c2

Please sign in to comment.