diff --git a/simulation_parameters/Constants.cs b/simulation_parameters/Constants.cs index e3238d16af..18e6b837f8 100644 --- a/simulation_parameters/Constants.cs +++ b/simulation_parameters/Constants.cs @@ -1188,8 +1188,7 @@ 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 = 1; - public const float COMPOUND_DIFFUSE_BASE_DISTANCE = 1; + public const float COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT = 0.8f; // Volcanism co2 production configuration public const float VOLCANISM_VENTS_CO2_STRENGTH = 0.15f; diff --git a/src/auto-evo/AutoEvoGlobalCache.cs b/src/auto-evo/AutoEvoGlobalCache.cs index d4efff6e6a..b4a2c79bc2 100644 --- a/src/auto-evo/AutoEvoGlobalCache.cs +++ b/src/auto-evo/AutoEvoGlobalCache.cs @@ -55,7 +55,7 @@ public AutoEvoGlobalCache(WorldGenerationSettings worldSettings) SunlightConversionEfficiencyPressure = new CompoundConversionEfficiencyPressure(Compound.Sunlight, Compound.Glucose, 1.0f); - SunlightCompoundPressure = new EnvironmentalCompoundPressure(Compound.Sunlight, Compound.Glucose, 20000, 1.0f); + SunlightCompoundPressure = new EnvironmentalCompoundPressure(Compound.Sunlight, Compound.Glucose, 400000, 1.0f); TemperatureConversionEfficiencyPressure = new CompoundConversionEfficiencyPressure(Compound.Temperature, Compound.ATP, 1.0f); diff --git a/src/general/world_effects/CompoundDiffusionEffect.cs b/src/general/world_effects/CompoundDiffusionEffect.cs index b4ef79b040..b1e1b42d05 100644 --- a/src/general/world_effects/CompoundDiffusionEffect.cs +++ b/src/general/world_effects/CompoundDiffusionEffect.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Godot; using Newtonsoft.Json; @@ -27,14 +26,10 @@ public void OnTimePassed(double elapsed, double totalTimePassed) HandlePatchCompoundDiffusion(); } - private static (float Ambient, float Density) CalculateWantedMoveAmounts(Patch sourcePatch, Patch adjacent, + private static (float Ambient, float Density) CalculateWantedMoveAmounts(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])); + float moveModifier = Constants.COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT; adjacent.Biome.TryGetCompound(compound.Key, CompoundAmountType.Biome, out var destinationAmount); @@ -74,7 +69,7 @@ private void HandlePatchCompoundDiffusion() foreach (var adjacent in patch.Value.Adjacent) { - var (ambient, density) = CalculateWantedMoveAmounts(patch.Value, adjacent, compound); + var (ambient, density) = CalculateWantedMoveAmounts(adjacent, compound); // If there's nothing really to move, then skip (or if negative as those moves are added by the // other patch) @@ -95,7 +90,7 @@ private void HandlePatchCompoundDiffusion() foreach (var adjacent in patch.Value.Adjacent) { - var (ambient, density) = CalculateWantedMoveAmounts(patch.Value, adjacent, compound); + var (ambient, density) = CalculateWantedMoveAmounts(adjacent, compound); if (ambient < MathUtils.EPSILON && density < MathUtils.EPSILON) continue; diff --git a/src/general/world_effects/PhotosynthesisProductionEffect.cs b/src/general/world_effects/PhotosynthesisProductionEffect.cs index 3c50a06d93..91a517ba88 100644 --- a/src/general/world_effects/PhotosynthesisProductionEffect.cs +++ b/src/general/world_effects/PhotosynthesisProductionEffect.cs @@ -29,12 +29,9 @@ public void OnTimePassed(double elapsed, double totalTimePassed) private void ApplyCompoundsAddition() { // These affect the final balance - var outputModifier = 1.5f; + var outputModifier = 1.0f; var inputModifier = 1.0f; - // This affects how fast the conditions change, but also the final balance somewhat - var modifier = 0.00015f; - List microbeProcesses = []; var cloudSizes = new Dictionary(); @@ -49,8 +46,10 @@ private void ApplyCompoundsAddition() if (patch.SpeciesInPatch.Count < 1) continue; - float oxygenBalance = 0; - float co2Balance = 0; + float oxygenConsumed = 0; + float oxygenProduced = 0; + float co2Consumed = 0; + float co2Produced = 0; foreach (var species in patch.SpeciesInPatch) { @@ -73,6 +72,7 @@ private void ApplyCompoundsAddition() microbeProcesses.Clear(); ProcessSystem.ComputeActiveProcessList(microbeSpecies.Organelles, ref microbeProcesses); + // Iterate over each process and determine compounds produced and consumed foreach (var process in microbeProcesses) { if (process.Process.InternalName == "protein_respiration") @@ -101,11 +101,11 @@ private void ApplyCompoundsAddition() { if (input.Key.ID is Compound.Oxygen) { - oxygenBalance -= input.Value * inputModifier * effectiveSpeed * species.Value; + oxygenConsumed += input.Value * inputModifier * effectiveSpeed * species.Value; } else if (input.Key.ID is Compound.Carbondioxide) { - co2Balance -= input.Value * inputModifier * effectiveSpeed * species.Value; + co2Consumed += input.Value * inputModifier * effectiveSpeed * species.Value; } } @@ -114,27 +114,57 @@ private void ApplyCompoundsAddition() { if (output.Key.ID is Compound.Oxygen) { - oxygenBalance += output.Value * outputModifier * effectiveSpeed * species.Value; + oxygenProduced += output.Value * outputModifier * effectiveSpeed * species.Value; } else if (output.Key.ID is Compound.Carbondioxide) { - co2Balance += output.Value * outputModifier * effectiveSpeed * species.Value; + co2Produced += output.Value * outputModifier * effectiveSpeed * species.Value; } } } } - // Scale the balances to make the changes less drastic - oxygenBalance *= modifier; - co2Balance *= modifier; + patch.Biome.TryGetCompound(Compound.Oxygen, CompoundAmountType.Biome, out var existingOxygen); + patch.Biome.TryGetCompound(Compound.Carbondioxide, CompoundAmountType.Biome, out var existingCo2); + + // Unless something else changes it, compound values stay the same + float oxygenTarget = existingOxygen.Ambient; + float co2Target = existingCo2.Ambient; + + float total = existingOxygen.Ambient + existingCo2.Ambient; + + // Special (but common) case where zero oxygen is being produced + if (oxygenProduced == 0 && oxygenConsumed > 0) + { + oxygenTarget = 0.0f; + + if (co2Produced > 0) + { + co2Target = total; + } + } + + // Special (but common) case where zero carbon dioxide is being produced + if (co2Produced == 0 && co2Consumed > 0) + { + co2Target = 0.0f; - changesToApply.Clear(); + if (oxygenProduced > 0) + { + oxygenTarget = total; + } + } - if (oxygenBalance != 0) - changesToApply[Compound.Oxygen] = oxygenBalance; + // if both compounds are being produced, calculate an aproximate steady state value + if (oxygenProduced > 0 && co2Produced > 0) + { + // Calculate long-term equilibrium balances based on production and consumption ratio + oxygenTarget = oxygenProduced / (oxygenConsumed + co2Consumed + MathUtils.EPSILON) * total; + co2Target = co2Produced / (oxygenConsumed + co2Consumed + MathUtils.EPSILON) * total; + } - if (co2Balance != 0) - changesToApply[Compound.Carbondioxide] = co2Balance; + changesToApply[Compound.Oxygen] = (oxygenTarget - existingOxygen.Ambient) * 0.5f; + changesToApply[Compound.Carbondioxide] = (co2Target - existingCo2.Ambient) * 0.5f; if (changesToApply.Count > 0) patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, changesToApply, cloudSizes);