diff --git a/EvoNet/Configuration/GameConfig.cs b/EvoNet/Configuration/GameConfig.cs index a53e9b3..83ab6b4 100644 --- a/EvoNet/Configuration/GameConfig.cs +++ b/EvoNet/Configuration/GameConfig.cs @@ -18,6 +18,8 @@ public class GameConfig public float ScaleFactor { get; set; } + public long GraphCount { get; set; } + public static GameConfig DefaultConfig { @@ -30,6 +32,7 @@ public static GameConfig DefaultConfig config.MoveRightKeys = new List { Keys.D, Keys.Right }; config.MovementSensitivity = 100.0f; config.ScaleFactor = 0.1f; + config.GraphCount = 10000; return config; } } @@ -42,12 +45,19 @@ public static GameConfig LoadConfigOrDefault() Deserializer yamlDeserializer = new Deserializer(); GameConfig deserialized = yamlDeserializer.Deserialize(sourceText); CheckLoadedConfig(deserialized); + SerializerBuilder builder = new SerializerBuilder(); + builder.EmitDefaults(); + Serializer yamlSerializer = builder.Build(); + string serialized = yamlSerializer.Serialize(deserialized); + File.WriteAllText("Config.cfg", serialized); return deserialized; } else { // Write out default config if there is no config for easier adjustment - Serializer yamlSerializer = new Serializer(); + SerializerBuilder builder = new SerializerBuilder(); + builder.EmitDefaults(); + Serializer yamlSerializer = builder.Build(); string serialized = yamlSerializer.Serialize(DefaultConfig); File.WriteAllText("Config.cfg", serialized); return DefaultConfig; @@ -56,6 +66,10 @@ public static GameConfig LoadConfigOrDefault() public static void CheckLoadedConfig(GameConfig config) { + if (config.GraphCount == 0) + { + config.GraphCount = 10000; + } } } } diff --git a/EvoNet/Controls/GraphControl.cs b/EvoNet/Controls/GraphControl.cs index b2c44a9..896a1a9 100644 --- a/EvoNet/Controls/GraphControl.cs +++ b/EvoNet/Controls/GraphControl.cs @@ -111,7 +111,7 @@ private void CreateCache(IGraphValueList graph) GraphCache cache; int CurrentVertexIndex = 0; int graphCount = graph.Count; - int startIndex = Math.Max(graph.Count - 10000, 0); + int startIndex = 0; int elementCount = graphCount - startIndex; if (elementCount <= 0) { @@ -124,7 +124,7 @@ private void CreateCache(IGraphValueList graph) decimal maxX = graph[startIndex].DisplayPosition; decimal minX = graph[startIndex].DisplayPosition; - for(int elementIndex = startIndex; elementIndex < graphCount; elementIndex++) + for (int elementIndex = startIndex; elementIndex < graphCount; elementIndex++) { maxY = Math.Max(maxY, graph[elementIndex].DisplayValue); minY = Math.Min(minY, graph[elementIndex].DisplayValue); @@ -152,8 +152,8 @@ private void CreateCache(IGraphValueList graph) upperPoint.Position = new Vector2(-1, GetRelativeY(graph.ElementAt(0).DisplayValue)); lowerPoint.Position = new Vector2(-1, -1); - VertexPosition2[] vertexAreaBufferData = new VertexPosition2[((graphCount - startIndex)+1) * 2]; - VertexPosition2[] vertexLineBufferData = new VertexPosition2[((graphCount - startIndex)+1) * 2]; + VertexPosition2[] vertexAreaBufferData = new VertexPosition2[((graphCount - startIndex) + 1) * 2]; + VertexPosition2[] vertexLineBufferData = new VertexPosition2[((graphCount - startIndex) + 1) * 2]; int[] indexBufferData = new int[((graphCount - startIndex)) * 6]; int CurrentIndexBufferIndex = 0; @@ -232,14 +232,18 @@ private void CreateCache(IGraphValueList graph) cache.NumElements = graphCount; GraphCache oldCache; - if (graphCaches.TryGetValue(graph, out oldCache)) + lock (this) { - graphCaches.Remove(graph); - oldCache.IndexAreaBuffer?.Dispose(); - oldCache.VertexAreaBuffer?.Dispose(); - oldCache.VertexLineBuffer?.Dispose(); + if (graphCaches.TryGetValue(graph, out oldCache)) + { + graphCaches.Remove(graph); + oldCache.IndexAreaBuffer?.Dispose(); + oldCache.VertexAreaBuffer?.Dispose(); + oldCache.VertexLineBuffer?.Dispose(); + } + graphCaches[graph] = cache; } - graphCaches[graph] = cache; + } private void DrawGraph(IGraphValueList graph) @@ -252,13 +256,13 @@ private void DrawGraph(IGraphValueList graph) GraphCache cache; bool needsRedraw = true; - if (graphCaches.TryGetValue(graph, out cache)) - { - if (cache.NumElements == graph.Count) - { - needsRedraw = false; - } - } + //if (graphCaches.TryGetValue(graph, out cache)) + //{ + // if (cache.NumElements == graph.Count) + // { + // needsRedraw = false; + // } + //} if (needsRedraw) @@ -276,15 +280,17 @@ private void DrawGraph(IGraphValueList graph) }; ThreadPool.QueueUserWorkItem(worker); } - if (graphCaches.TryGetValue(graph, out cache)) + lock (this) { - DrawCache(graph, ref cache); + if (graphCaches.TryGetValue(graph, out cache)) + { + DrawCache(graph, ref cache); + } } } protected override void Draw(GameTime gameTime) { - //GraphicsDevice.RasterizerState.MultiSampleAntiAlias = true; PresentationParameters pp = GraphicsDevice.PresentationParameters; if (pp.MultiSampleCount < 4) diff --git a/EvoNet/Forms/MainForm.cs b/EvoNet/Forms/MainForm.cs index fcfd064..5ea6f41 100644 --- a/EvoNet/Forms/MainForm.cs +++ b/EvoNet/Forms/MainForm.cs @@ -79,6 +79,14 @@ private CreatureManager CreatureManager private void timer1_Tick(object sender, EventArgs e) { + if (lastFoodIndex == 0) + { + lastFoodIndex = (int)Math.Max(0, TileMap.FoodRecord.Count - evoSimControl1.gameConfiguration.GraphCount); + } + if (lastCreatureIndex == 0) + { + lastCreatureIndex = (int)Math.Max(0, CreatureManager.AliveCreaturesRecord.Count - evoSimControl1.gameConfiguration.GraphCount); + } while (lastFoodIndex < TileMap.FoodRecord.Count) { fictionalDateForFood += TimeSpan.FromSeconds(TileMap.FixedUpdateTime); @@ -93,6 +101,14 @@ private void timer1_Tick(object sender, EventArgs e) lastCreatureIndex++; numberCreaturesAlive.Add(new GraphTimeDoubleValue(fictionalDateForCreatures, Value)); } + if (foodValueList.Count > evoSimControl1.gameConfiguration.GraphCount) + { + foodValueList.RemoveRange(0, (int)(foodValueList.Count - evoSimControl1.gameConfiguration.GraphCount)); + } + if (numberCreaturesAlive.Count > evoSimControl1.gameConfiguration.GraphCount) + { + numberCreaturesAlive.RemoveRange(0, (int)(numberCreaturesAlive.Count - evoSimControl1.gameConfiguration.GraphCount)); + } //FoodGraph.Refresh(); } diff --git a/EvoSim/Objects/Feeler.cs b/EvoSim/Objects/Feeler.cs index 88a9449..fa2811f 100644 --- a/EvoSim/Objects/Feeler.cs +++ b/EvoSim/Objects/Feeler.cs @@ -35,6 +35,7 @@ public Vector2 FeelerPos private WorkingNeuron outFeelerAngle = new WorkingNeuron(-1); private WorkingNeuron outAttack = new WorkingNeuron(-1); + [NonSerialized] private Creature feelerCreature = null; private Creature owner = null; diff --git a/EvoSim/Tasks/MergeCreatureArraysTask.cs b/EvoSim/Tasks/MergeCreatureArraysTask.cs index 752de1f..cabdd23 100644 --- a/EvoSim/Tasks/MergeCreatureArraysTask.cs +++ b/EvoSim/Tasks/MergeCreatureArraysTask.cs @@ -29,8 +29,8 @@ protected override void Run(float time) sim.CreatureManager.numberOfDeaths += creatureTask.CreaturesToKill.Count; foreach (Creature creature in creatureTask.CreaturesToKill) { - if (sim.SimulationConfiguration.UseGraveyard && - creature.Generation > 1 || creature.Children.Count > 1) + if (sim.SimulationConfiguration.UseGraveyard && sim.SimulationConfiguration.DoRuntimeSave && + (creature.Generation > 1 || creature.Children.Count > 1)) { sim.CreatureManager.Graveyard.Add(creature); } diff --git a/Graph/GraphValueList.cs b/Graph/GraphValueList.cs index 377f11b..5cd3a53 100644 --- a/Graph/GraphValueList.cs +++ b/Graph/GraphValueList.cs @@ -87,6 +87,11 @@ public void RemoveAt(int index) values.RemoveAt(index); } + public void RemoveRange(int index, int count) + { + values.RemoveRange(index, count); + } + public IGraphValue this[int index] { get