Skip to content

Commit

Permalink
Use patch snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk26g committed Mar 7, 2025
1 parent 14ea6b5 commit 8799c13
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
83 changes: 51 additions & 32 deletions src/general/world_effects/patch_events/GlobalGlaciationEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ public class GlobalGlaciationEvent : IWorldEffect
private readonly XoShiRo256starstar random;

[JsonProperty]
private readonly Dictionary<int, Dictionary<Compound, float>> previousEnvironmentalChanges = new();

[JsonProperty]
private readonly Dictionary<int, string> previousBackground = new();

[JsonProperty]
private readonly Dictionary<int, Color> previousLightColour = new();
private readonly List<int> modifiedPatchesIds = new();

[JsonProperty]
private bool hasEventAlreadyHappened;
Expand All @@ -35,6 +29,9 @@ public class GlobalGlaciationEvent : IWorldEffect
[JsonProperty]
private int generationsLeft = -1;

[JsonProperty]
private int eventDuration = 0;

[JsonProperty]
private GameWorld targetWorld;

Expand Down Expand Up @@ -89,10 +86,13 @@ private void MarkPatches(double totalTimePassed)

private void TryToTriggerEvent(double totalTimePassed)
{
// if (totalTimePassed <= 200_000_000)
// return;
if (!AreConditionsMet())
return;

generationsLeft = random.Next(Constants.GLOBAL_GLACIATION_MIN_DURATION, Constants.GLOBAL_GLACIATION_MAX_DURATION);
eventDuration = random.Next(Constants.GLOBAL_GLACIATION_MIN_DURATION, Constants.GLOBAL_GLACIATION_MAX_DURATION);
generationsLeft = eventDuration;

foreach (var (index, patch) in targetWorld.Map.Patches)
{
Expand Down Expand Up @@ -128,22 +128,22 @@ private bool AreConditionsMet()

private void ChangePatchProperties(int index, Patch patch, double totalTimePassed)
{
AdjustBackground(index, patch);
AdjustEnvironment(index, patch);
modifiedPatchesIds.Add(index);
AdjustBackground(patch);
AdjustEnvironment(patch);
AddIceChunks(patch);
LogEvent(patch, totalTimePassed);
}

private void AdjustBackground(int index, Patch patch)
private void AdjustBackground(Patch patch)
{
previousBackground.Add(index, patch.BiomeTemplate.Background);
previousLightColour.Add(index, patch.BiomeTemplate.Sunlight.Colour);

patch.BiomeTemplate.Background = Background;
patch.BiomeTemplate.Sunlight.Colour = new Color(0.8f, 0.9f, 1, 1);
patch.CurrentSnapshot.Background = Background;
patch.BiomeTemplate.Sunlight.Colour = new Color(0.8f, 0.8f, 1, 1);
patch.CurrentSnapshot.LightColour = new Color(0.8f, 0.8f, 1, 1);
}

private void AdjustEnvironment(int index, Patch patch)
private void AdjustEnvironment(Patch patch)
{
var currentTemperature = patch.Biome.ChangeableCompounds[Compound.Temperature];
var currentSunlight = patch.Biome.ChangeableCompounds[Compound.Sunlight];
Expand All @@ -156,9 +156,8 @@ private void AdjustEnvironment(int index, Patch patch)
[Compound.Temperature] = currentTemperature.Ambient,
[Compound.Sunlight] = currentSunlight.Ambient,
};
previousEnvironmentalChanges.Add(index, changes);

patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, changes, null);
patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, changes, new Dictionary<Compound, float>());
}

/// <summary>
Expand Down Expand Up @@ -191,29 +190,49 @@ private void LogEvent(Patch patch, double totalTimePassed)

private void FinishEvent()
{
foreach (var index in previousEnvironmentalChanges.Keys)
hasEventAlreadyHappened = true;
foreach (var index in modifiedPatchesIds)
{
if (!targetWorld.Map.Patches.TryGetValue(index, out var patch))
continue;

var biomeBackground = previousBackground[index];
var biomeLightColour = previousLightColour[index];
var changes = previousEnvironmentalChanges[index];

// Reverse the changes in sunlight and temperature values
foreach (var changeIndex in changes.Keys)
{
changes[changeIndex] = -changes[changeIndex];
}

patch.BiomeTemplate.Background = biomeBackground;
patch.BiomeTemplate.Sunlight.Colour = biomeLightColour;
patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, changes, new Dictionary<Compound, float>());
PatchSnapshot patchSnapshot = patch.History[eventDuration];

ResetBackground(patch, patchSnapshot);
ResetEnvironment(patch, patchSnapshot);
RemoveChunks(patch);
}
}

private void ResetBackground(Patch patch, PatchSnapshot patchSnapshot)
{
var biomeBackground = patchSnapshot.Background;
var biomeLightColour = patchSnapshot.LightColour;
patch.BiomeTemplate.Background = biomeBackground;
patch.BiomeTemplate.Sunlight.Colour = biomeLightColour;
patch.CurrentSnapshot.Background = biomeBackground;
patch.CurrentSnapshot.LightColour = biomeLightColour;
}

private void ResetEnvironment(Patch patch, PatchSnapshot patchSnapshot)
{
var currentTemperature = patch.Biome.ChangeableCompounds[Compound.Temperature];
var currentSunlight = patch.Biome.ChangeableCompounds[Compound.Sunlight];
var previousTemperature = patchSnapshot.Biome.ChangeableCompounds[Compound.Temperature];
var previousSunlight = patchSnapshot.Biome.ChangeableCompounds[Compound.Sunlight];

currentTemperature.Ambient = previousTemperature.Ambient - currentTemperature.Ambient;
currentSunlight.Ambient = 0.5f - currentSunlight.Ambient - previousSunlight.Ambient;

var changes = new Dictionary<Compound, float>
{
[Compound.Temperature] = currentTemperature.Ambient,
[Compound.Sunlight] = currentSunlight.Ambient,
};

patch.Biome.ApplyLongTermCompoundChanges(patch.BiomeTemplate, changes, new Dictionary<Compound, float>());
}

private void RemoveChunks(Patch patch)
{
foreach (var configuration in IceChunksConfigurations)
Expand Down
13 changes: 10 additions & 3 deletions src/microbe_stage/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public Patch(LocalizedString name, int id, Biome biomeTemplate, BiomeType biomeT
ID = id;
BiomeTemplate = biomeTemplate;
BiomeType = biomeType;
currentSnapshot = new PatchSnapshot((BiomeConditions)biomeTemplate.Conditions.Clone());
currentSnapshot = new PatchSnapshot((BiomeConditions)biomeTemplate.Conditions.Clone(),
biomeTemplate.Sunlight.Colour, biomeTemplate.Background);
Region = region;
}

Expand All @@ -68,6 +69,8 @@ public Patch(LocalizedString name, int id, Biome biomeTemplate, PatchSnapshot cu
Name = name;
ID = id;
BiomeTemplate = biomeTemplate;
BiomeTemplate.Background = currentSnapshot.Background;
BiomeTemplate.Sunlight.Colour = currentSnapshot.LightColour;
this.currentSnapshot = currentSnapshot;
}

Expand Down Expand Up @@ -648,12 +651,16 @@ public class PatchSnapshot : ICloneable
public Dictionary<Species, SpeciesInfo> RecordedSpeciesInfo = new();

public BiomeConditions Biome;
public Color LightColour;
public string Background;

public List<GameEventDescription> EventsLog = new();

public PatchSnapshot(BiomeConditions biome)
public PatchSnapshot(BiomeConditions biome, Color lightColour, string background)
{
Biome = biome;
Background = background;
LightColour = lightColour;
}

public void ReplaceSpecies(Species old, Species newSpecies)
Expand All @@ -676,7 +683,7 @@ public void ReplaceSpecies(Species old, Species newSpecies)
public object Clone()
{
// We only do a shallow copy of RecordedSpeciesInfo here as SpeciesInfo objects are never modified.
var result = new PatchSnapshot((BiomeConditions)Biome.Clone())
var result = new PatchSnapshot((BiomeConditions)Biome.Clone(), new Color(LightColour), Background)
{
TimePeriod = TimePeriod,
SpeciesInPatch = new Dictionary<Species, long>(SpeciesInPatch),
Expand Down
1 change: 1 addition & 0 deletions src/microbe_stage/editor/MicrobeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ protected override void ElapseEditorEntryTime()
{
// TODO: select which units will be used for the master elapsed time counter
CurrentGame.GameWorld.OnTimePassed(1);
cellEditorTab.UpdateBackgroundImage(CurrentPatch.BiomeTemplate);
}

protected override GameProperties StartNewGameForEditor()
Expand Down
1 change: 1 addition & 0 deletions src/multicellular_stage/editor/MulticellularEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ protected override void ElapseEditorEntryTime()
{
// TODO: select which units will be used for the master elapsed time counter
CurrentGame.GameWorld.OnTimePassed(1);
cellEditorTab.UpdateBackgroundImage(CurrentPatch.BiomeTemplate);
}

protected override GameProperties StartNewGameForEditor()
Expand Down

1 comment on commit 8799c13

@hhyyrylainen
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 it going to be a bit of a nightmare to have the background stored as a string in the history? That will blow up save compatibility any time that is changed.

I thought that maybe it'd be more reasonable to store the patch archetype in the history (by internal name) rather than the data directly that references game assets. This way the system is much more bulletproof against save upgrade problems.

Please sign in to comment.