Skip to content
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

Oxygen and Carbon Dioxide Rebalance #5693

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions simulation_parameters/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,8 +1164,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;
Expand Down
2 changes: 1 addition & 1 deletion src/auto-evo/AutoEvoGlobalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 5 additions & 10 deletions src/general/world_effects/CompoundDiffusionEffect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Godot;
using Newtonsoft.Json;

Expand Down Expand Up @@ -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, 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]));
float moveModifier = Constants.COMPOUND_DIFFUSE_BASE_MOVE_AMOUNT;
hhyyrylainen marked this conversation as resolved.
Show resolved Hide resolved

adjacent.Biome.TryGetCompound(compound.Key, CompoundAmountType.Biome,
out var destinationAmount);
Expand Down Expand Up @@ -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)
Expand All @@ -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;

Expand Down
66 changes: 48 additions & 18 deletions src/general/world_effects/PhotosynthesisProductionEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TweakedProcess> microbeProcesses = [];

var cloudSizes = new Dictionary<Compound, float>();
Expand All @@ -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)
{
Expand All @@ -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")
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this make the game even more harsh? Wasn't the goal of this PR to make the changes less extreme? I think whatever trace oxygen there is should be allowed to be slowly consumed over time instead of immediately forcing it to zero...


if (co2Produced > 0)
{
co2Target = total;
}
}

// Special (but common) case where zero carbon dioxide is being produced
if (co2Produced == 0 && co2Consumed > 0)
{
co2Target = 0.0f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is flat out wrong, there's the volcanism effect that generates co2 so this would override that impact. So even if my previous comment doesn't absolutely need to be addressed, this does have to.


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// if both compounds are being produced, calculate an aproximate steady state value
// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if no longer works correctly after your changes because even if the change to apply is less than EPSILON the value is added to the dictionary so this check no longer works. Either it needs to be reworked that small values (so close to target) are not processed or this needs to loop here to check if any of the changes are above EPSILON. I think the first approach will be easier to do and clearer to understand here.

patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, changesToApply, cloudSizes);
Expand Down
2 changes: 1 addition & 1 deletion src/general/world_effects/VolcanismEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void ProduceCO2(Patch patch, float co2Strength, float threshold)
return;

// TODO: should this clamp or not?
addedCo2[Compound.Carbondioxide] = Math.Clamp(amount.Ambient + co2Strength, 0, threshold);
addedCo2[Compound.Carbondioxide] = Math.Clamp(co2Strength, 0, threshold);
hhyyrylainen marked this conversation as resolved.
Show resolved Hide resolved

patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, addedCo2, cloudSizesDummy);
}
Expand Down