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

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 2 additions & 4 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 @@ -33,8 +32,7 @@ private static (float Ambient, float Density) CalculateWantedMoveAmounts(Patch s
// Apply patch distance to diminish how much to move (to make ocean bottoms receive less surface
// resources like oxygen)
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this comment now incorrect?

// 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
56 changes: 38 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 oxygenIn = 0;
float oxygenOut = 0;
float co2In = 0;
float co2Out = 0;

foreach (var species in patch.SpeciesInPatch)
{
Expand Down Expand Up @@ -101,11 +100,11 @@ private void ApplyCompoundsAddition()
{
if (input.Key.ID is Compound.Oxygen)
{
oxygenBalance -= input.Value * inputModifier * effectiveSpeed * species.Value;
oxygenIn += input.Value * inputModifier * effectiveSpeed * species.Value;
}
else if (input.Key.ID is Compound.Carbondioxide)
{
co2Balance -= input.Value * inputModifier * effectiveSpeed * species.Value;
co2In += input.Value * inputModifier * effectiveSpeed * species.Value;
}
}

Expand All @@ -114,27 +113,48 @@ private void ApplyCompoundsAddition()
{
if (output.Key.ID is Compound.Oxygen)
{
oxygenBalance += output.Value * outputModifier * effectiveSpeed * species.Value;
oxygenOut += output.Value * outputModifier * effectiveSpeed * species.Value;
}
else if (output.Key.ID is Compound.Carbondioxide)
{
co2Balance += output.Value * outputModifier * effectiveSpeed * species.Value;
co2Out += output.Value * outputModifier * effectiveSpeed * species.Value;
}
}
}
}

// Scale the balances to make the changes less drastic
oxygenBalance *= modifier;
co2Balance *= modifier;
float oxygenTarget;
float co2Target;

patch.Biome.TryGetCompound(Compound.Oxygen, CompoundAmountType.Biome, out var existingOxygen);
patch.Biome.TryGetCompound(Compound.Carbondioxide, CompoundAmountType.Biome, out var existingCo2);

float total = existingOxygen.Ambient + existingCo2.Ambient;

changesToApply.Clear();
if (oxygenOut == 0)
{
if (co2Out == 0)
{
// in the rare event we aren't making either compound, do nothing
Copy link
Member

Choose a reason for hiding this comment

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

But now consuming the compounds also doesn't do anything? I think this is a wrong conclusion to make in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct. If microbes had only bio processes that destroyed O2 or CO2 without producing anything in return, this code doesn't account for that. Right now the PhotosynthesisProductionEffect class is dependent on the special case of conservation of mass (and that CO2 and O2 are only being converted into each other)

Copy link
Member

Choose a reason for hiding this comment

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

The code shouldn't depend on that then as someone will make a mod eventually that breaks the laws of physics and will get confused why it doesn't do things correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alright, I've modified the code to handle matter sinks, and while a matter generator will result in absurd numbers, I've protected against divide-by-zero exceptions.

continue;
}

if (oxygenBalance != 0)
changesToApply[Compound.Oxygen] = oxygenBalance;
oxygenTarget = 0;
co2Target = total;
}
else if (co2Out == 0)
{
oxygenTarget = total;
co2Target = 0;
}
else
{
oxygenTarget = oxygenOut / (oxygenIn + co2In) * total;
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment here explaining the logic? I don't really understand this kind of scaling because ApplyLongTermCompoundChanges already does a relative applying of gases. So higher oxygen output reduces the effect of the co2 adding and vice versa. So to me it looks like this is now doing the same effect twice, which doesn't logically make a ton of sense as ApplyLongTermCompoundChanges needs to be given the absolute change wanted and then it automatically makes gases related to each other and caps it to a 100%.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair. Rather than explain here, I will add the comment and see if the explanation makes sense to you.

co2Target = co2Out / (oxygenIn + co2In) * total;
}

if (co2Balance != 0)
changesToApply[Compound.Carbondioxide] = co2Balance;
changesToApply[Compound.Oxygen] = (oxygenTarget - existingOxygen.Ambient) / 2.0f;
Copy link
Member

Choose a reason for hiding this comment

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

It's more efficient to multiply by 0.5f than to do a float division.

changesToApply[Compound.Carbondioxide] = (co2Target - existingCo2.Ambient) / 2.0f;

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
5 changes: 4 additions & 1 deletion src/general/world_effects/VolcanismEffect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Godot;
using Newtonsoft.Json;

/// <summary>
Expand Down Expand Up @@ -32,6 +33,8 @@ private void ApplyVolcanism()
{
foreach (var patchKeyValue in targetWorld.Map.Patches)
{
GD.Print(patchKeyValue.Value + " volcanism");
Copy link
Member

Choose a reason for hiding this comment

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

This looks like leftover debugging code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I knew I forgot something!


// TODO: make these configured by the biomes.json file
if (patchKeyValue.Value.BiomeType == BiomeType.Vents)
{
Expand Down Expand Up @@ -72,7 +75,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