Skip to content

Commit

Permalink
Food chain related miche data saving and loading (#5554)
Browse files Browse the repository at this point in the history
* Made saving/loading miche trees work

* Made food chain show initially after load
  • Loading branch information
hhyyrylainen authored Oct 1, 2024
1 parent 1e19865 commit 2ed87d7
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/auto-evo/Miche.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

/// <summary>
/// A node for the Miche Tree
Expand All @@ -15,10 +16,24 @@
/// For a fuller explanation see docs/auto_evo.md
/// </para>
/// </remarks>
/// <remarks>
/// <para>
/// This is partially saved to be able to display info about the auto-evo run in the editor after loading a save.
/// This has to be a reference to work with the parent miche reference. This uses Thrive serializer as the fields
/// can already refer back to this object.
/// </para>
/// </remarks>
[JsonObject(IsReference = true)]
[UseThriveSerializer]
public class Miche
{
[JsonProperty]
public readonly SelectionPressure Pressure;

[JsonProperty]
public readonly List<Miche> Children = new();

[JsonProperty]
public Miche? Parent;

/// <summary>
Expand All @@ -29,6 +44,7 @@ public class Miche
/// Occupant should always be null if this Miche is not a leaf node (children is not empty).
/// </para>
/// </remarks>
[JsonProperty]
public Species? Occupant;

public Miche(SelectionPressure pressure)
Expand Down
6 changes: 6 additions & 0 deletions src/auto-evo/RunResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class RunResults
// afterwards when loading from a save
private readonly List<PossibleSpecies> modifiedSpecies = new();

/// <summary>
/// Miche tree generated for this auto-evo run. This is partially saved (some subobjects are excluded) so that
/// food chain tab information can be displayed after loading a save.
/// </summary>
[JsonProperty]
[JsonConverter(typeof(DictionaryWithJSONKeysConverter<Patch, Miche>))]
private readonly Dictionary<Patch, Miche> micheByPatch = new();

public enum NewSpeciesType
Expand Down
2 changes: 2 additions & 0 deletions src/auto-evo/mutation_strategy/IMutationStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public interface IMutationStrategy<T>
where T : Species
{
[JsonIgnore]
public bool Repeatable { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace AutoEvo;

using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class AvoidPredationSelectionPressure : SelectionPressure
{
[JsonProperty]
public readonly Species Predator;

// Needed for translation extraction
Expand Down Expand Up @@ -29,6 +33,7 @@ public AvoidPredationSelectionPressure(Species predator, float weight) : base(we
Predator = predator;
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
12 changes: 12 additions & 0 deletions src/auto-evo/selection_pressure/ChunkCompoundPressure.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace AutoEvo;

using System;
using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class ChunkCompoundPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -12,18 +14,28 @@ public class ChunkCompoundPressure : SelectionPressure

private readonly CompoundDefinition atp = SimulationParameters.GetCompound(Compound.ATP);

[JsonProperty]
private readonly string chunkType;

[JsonProperty]
private readonly LocalizedString readableName;

private readonly CompoundDefinition compound;

// Needed for saving to work
[JsonProperty(nameof(compound))]
private readonly Compound compoundRaw;

public ChunkCompoundPressure(string chunkType, LocalizedString readableName, Compound compound, float weight) :
base(weight, [])
{
compoundRaw = compound;
this.compound = SimulationParameters.GetCompound(compound);
this.chunkType = chunkType;
this.readableName = readableName;
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
7 changes: 7 additions & 0 deletions src/auto-evo/selection_pressure/CompoundCloudPressure.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace AutoEvo;

using System;
using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class CompoundCloudPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -10,8 +12,12 @@ public class CompoundCloudPressure : SelectionPressure

// ReSharper restore ArrangeObjectCreationWhenTypeEvident

[JsonProperty]
private readonly Compound compound;

private readonly CompoundDefinition compoundDefinition;

[JsonProperty]
private readonly bool isDayNightCycleEnabled;

public CompoundCloudPressure(Compound compound, bool isDayNightCycleEnabled, float weight) :
Expand All @@ -29,6 +35,7 @@ public CompoundCloudPressure(Compound compound, bool isDayNightCycleEnabled, flo
this.isDayNightCycleEnabled = isDayNightCycleEnabled;
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
namespace AutoEvo;

using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class CompoundConversionEfficiencyPressure : SelectionPressure
{
[JsonIgnore]
public readonly CompoundDefinition FromCompound;

[JsonIgnore]
public readonly CompoundDefinition ToCompound;

// Needed for translation extraction
Expand All @@ -11,16 +17,27 @@ public class CompoundConversionEfficiencyPressure : SelectionPressure

// ReSharper restore ArrangeObjectCreationWhenTypeEvident

// These two are needed purely for saving to work
[JsonProperty]
private readonly Compound compound;

[JsonProperty]
private readonly Compound outCompound;

public CompoundConversionEfficiencyPressure(Compound compound, Compound outCompound, float weight) :
base(weight, [
AddOrganelleAnywhere.ThatConvertBetweenCompounds(compound, outCompound),
RemoveOrganelle.ThatCreateCompound(outCompound),
])
{
this.compound = compound;
this.outCompound = outCompound;
FromCompound = SimulationParameters.GetCompound(compound);
ToCompound = SimulationParameters.GetCompound(outCompound);
}
[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
15 changes: 15 additions & 0 deletions src/auto-evo/selection_pressure/EnvironmentalCompoundPressure.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace AutoEvo;

using System;
using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class EnvironmentalCompoundPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -14,14 +16,26 @@ public class EnvironmentalCompoundPressure : SelectionPressure

private readonly CompoundDefinition createdCompound;
private readonly CompoundDefinition compound;

[JsonProperty]
private readonly float energyMultiplier;

// These two are needed purely for saving to work
[JsonProperty(nameof(compound))]
private readonly Compound compoundRaw;

[JsonProperty(nameof(createdCompound))]
private readonly Compound createdCompoundRaw;

public EnvironmentalCompoundPressure(Compound compound, Compound createdCompound, float energyMultiplier,
float weight) :
base(weight, [
AddOrganelleAnywhere.ThatUseCompound(compound),
])
{
compoundRaw = compound;
createdCompoundRaw = createdCompound;

this.compound = SimulationParameters.GetCompound(compound);

if (this.compound.IsCloud)
Expand All @@ -36,6 +50,7 @@ public EnvironmentalCompoundPressure(Compound compound, Compound createdCompound
this.energyMultiplier = energyMultiplier;
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
8 changes: 8 additions & 0 deletions src/auto-evo/selection_pressure/MaintainCompoundPressure.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace AutoEvo;

using System;
using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class MaintainCompoundPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -12,14 +14,20 @@ public class MaintainCompoundPressure : SelectionPressure

private readonly CompoundDefinition compound;

// Needed for saving to work
[JsonProperty(nameof(compound))]
private readonly Compound compoundRaw;

public MaintainCompoundPressure(Compound compound, float weight) : base(weight, [
AddOrganelleAnywhere.ThatCreateCompound(compound),
RemoveOrganelle.ThatUseCompound(compound),
])
{
compoundRaw = compound;
this.compound = SimulationParameters.GetCompound(compound);
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
4 changes: 4 additions & 0 deletions src/auto-evo/selection_pressure/MetabolicStabilityPressure.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace AutoEvo;

using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class MetabolicStabilityPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -15,6 +18,7 @@ public MetabolicStabilityPressure(float weight) : base(weight, [
{
}
[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
4 changes: 4 additions & 0 deletions src/auto-evo/selection_pressure/NoOpPressure.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace AutoEvo;

using Newtonsoft.Json;

/// <summary>
/// This pressure does nothing, but is used as a placeholder node in the Miche Tree
/// </summary>
[JSONDynamicTypeAllowed]
public class NoOpPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -15,6 +18,7 @@ public NoOpPressure() : base(1, [])
{
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace AutoEvo;

using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class PredationEffectivenessPressure : SelectionPressure
{
[JsonProperty]
public readonly Species Prey;

// Needed for translation extraction
Expand All @@ -28,6 +32,7 @@ public PredationEffectivenessPressure(Species prey, float weight) :
Prey = prey;
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
4 changes: 4 additions & 0 deletions src/auto-evo/selection_pressure/PredatorRoot.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace AutoEvo;

using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class PredatorRoot : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -17,6 +20,7 @@ public PredatorRoot(float weight) : base(weight, [
{
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
4 changes: 4 additions & 0 deletions src/auto-evo/selection_pressure/RootPressure.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace AutoEvo;

using Newtonsoft.Json;

[JSONDynamicTypeAllowed]
public class RootPressure : SelectionPressure
{
// Needed for translation extraction
Expand All @@ -16,6 +19,7 @@ public RootPressure() : base(1, [
{
}

[JsonIgnore]
public override LocalizedString Name => NameString;

public override float Score(Species species, Patch patch, SimulationCache cache)
Expand Down
Loading

0 comments on commit 2ed87d7

Please sign in to comment.