Skip to content

Commit

Permalink
Creature Stats Rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Brotcrunsher committed Oct 27, 2016
1 parent 1b4ac03 commit df85dc8
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 30 deletions.
5 changes: 3 additions & 2 deletions EvoNet/AI/Neuron.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -21,7 +22,7 @@ public virtual void Deserialize(BinaryReader reader)
name = reader.ReadString();
}


public Vector2 DrawPosition { get; set; }

public string GetName()
{
Expand Down
82 changes: 74 additions & 8 deletions EvoNet/AI/NeuronalNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -280,27 +281,51 @@ public void Deserialize(BinaryReader reader)

public void Draw(SpriteBatch spriteBatch, Rectangle rect)
{
CalculateNeuronsRenderPosition(rect);
float yMin = rect.Y + NEURONSIZE / 2;
float yMax = rect.Y + rect.Height - NEURONSIZE / 2;
DrawLayer(spriteBatch, rect.X + NEURONSIZE / 2, yMin, yMax, inputNeurons, new Vector2(-10, -10), true);
DrawLayer(spriteBatch, rect.X + rect.Width / 2, yMin, yMax, hiddenNeurons);
DrawLayer(spriteBatch, rect.X + rect.Width - NEURONSIZE / 2, yMin, yMax, outputNeurons, new Vector2(10, -10));
float strongestConnection = GetStrongestConnection();
DrawLayer(spriteBatch, outputNeurons, strongestConnection, new Vector2(10, -10));
DrawLayer(spriteBatch, hiddenNeurons, strongestConnection);
DrawLayer(spriteBatch, inputNeurons, strongestConnection, new Vector2(-10, -10), true);
}

private void DrawLayer(SpriteBatch spriteBatch, float x, float yMin, float yMax, List<Neuron> layer, Vector2? nameOffset = null, bool writeRight = false) {
private void CalculateNeuronsRenderPosition(Rectangle rect)
{
float yMin = rect.Y + NEURONSIZE / 2;
float yMax = rect.Y + rect.Height - NEURONSIZE / 2;
CalculateNeuronsRederPositionLayer(outputNeurons, rect.X + rect.Width - NEURONSIZE / 2, yMin, yMax);
CalculateNeuronsRederPositionLayer(hiddenNeurons, rect.X + rect.Width / 2, yMin, yMax);
CalculateNeuronsRederPositionLayer(inputNeurons, rect.X + NEURONSIZE / 2, yMin, yMax);
}

private void CalculateNeuronsRederPositionLayer(List<Neuron> layer, float x, float yMin, float yMax)
{
float yDiff = yMax - yMin;
float distanceBetweenNeurons = yDiff / (layer.Count - 1);
float currentY = yMin;
for (int i = 0; i < layer.Count; i++)
{
layer[i].DrawPosition = new Vector2(x, currentY);
currentY += distanceBetweenNeurons;
}
}

private void DrawLayer(SpriteBatch spriteBatch, List<Neuron> layer, float strongestConnection, Vector2? nameOffset = null, bool writeRight = false) {
for(int i = 0; i<layer.Count; i++)
{
DrawNeuron(spriteBatch, x, currentY, layer[i], nameOffset, writeRight);
currentY += distanceBetweenNeurons;
DrawNeuron(spriteBatch, layer[i], strongestConnection, nameOffset, writeRight);
}
}

private void DrawNeuron(SpriteBatch spriteBatch, float x, float y, Neuron n, Vector2? nameOffset = null, bool writeRight = false)
private void DrawNeuron(SpriteBatch spriteBatch, Neuron n, float strongestConnection, Vector2? nameOffset = null, bool writeRight = false)
{
if(n is WorkingNeuron)
{
DrawConnections(spriteBatch, n, strongestConnection);
}
float x = n.DrawPosition.X;
float y = n.DrawPosition.Y;
Color c = Color.Black;
float val = n.GetValue();
if(val < 0)
Expand All @@ -309,7 +334,7 @@ private void DrawNeuron(SpriteBatch spriteBatch, float x, float y, Neuron n, Vec
}
else
{
c = Color.Blue;
c = Color.Green;
}

float valSize = val * NEURONSIZE;
Expand All @@ -327,5 +352,46 @@ private void DrawNeuron(SpriteBatch spriteBatch, float x, float y, Neuron n, Vec
spriteBatch.DrawString(Fonts.FontArial, n.GetName(), pos, Color.White);
}
}

private void DrawConnections(SpriteBatch spriteBatch, Neuron n, float strongestConnection)
{
WorkingNeuron wn = (WorkingNeuron)n;
foreach(Connection c in wn.GetConnections())
{
Color color = Color.Black;
float value = c.GetValue();
float alpha = Math.Abs(value) / strongestConnection;
//TODO
if (value > 0)
{
color = new Color(0f, 1f, 0f, alpha);
}else
{

color = new Color(1f, 0f, 0f, alpha);
}
RenderHelper.DrawLine(spriteBatch, n.DrawPosition.X, n.DrawPosition.Y, c.entryNeuron.DrawPosition.X, c.entryNeuron.DrawPosition.Y, color, 1);
}
}

public float GetStrongestConnection()
{
return Mathf.Max(GetStrongestLayerConnection(hiddenNeurons), GetStrongestLayerConnection(outputNeurons));
}

private float GetStrongestLayerConnection(List<Neuron> layer)
{
float strongestConnection = 0;
foreach (Neuron n in layer)
{
WorkingNeuron wn = (WorkingNeuron)n;
float strongestNeuronConnection = Math.Abs(wn.GetStrongestConnection());
if (strongestNeuronConnection > strongestConnection)
{
strongestConnection = strongestNeuronConnection;
}
}
return strongestConnection;
}
}
}
11 changes: 11 additions & 0 deletions EvoNet/AI/WorkingNeuron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,16 @@ public override Neuron NameCopy()
clone.SetName(GetName());
return clone;
}

public float GetStrongestConnection()
{
float strongest = 0;
foreach(Connection c in connections)
{
float val = Mathf.Abs(c.weight);
if (val > strongest) strongest = val;
}
return strongest;
}
}
}
2 changes: 1 addition & 1 deletion EvoNet/EvoGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override void LoadContent()
Creature.Initialize();


RenderHelper.Ini(spriteBatch, WhiteTexture, WhiteCircleTexture);
RenderHelper.Ini(WhiteTexture, WhiteCircleTexture);
tileMap = TileMap.DeserializeFromFile("tilemap.dat", this);
if (tileMap == null)
{
Expand Down
10 changes: 10 additions & 0 deletions EvoNet/Mathf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,15 @@ public static float Min(List<float> list)
}
return min;
}

public static float Max(float a, float b)
{
return a > b ? a : b;
}

public static float Min(float a, float b)
{
return a > b ? b : a;
}
}
}
28 changes: 24 additions & 4 deletions EvoNet/Objects/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public static Creature oldestCreatureEver
private long id;

private Vector2 pos;
public Vector2 Pos
{
get
{
return pos;
}
}
private float viewAngle;

private float feelerAngle;
Expand Down Expand Up @@ -140,6 +147,13 @@ public NeuronalNetwork Brain


private List<Creature> children = new List<Creature>();
public List<Creature> Children
{
get
{
return children;
}
}

// Temps for deserialization
private long motherId;
Expand Down Expand Up @@ -448,13 +462,19 @@ public void CalculateFeelerPos()
public void Draw()
{
spriteBatch.Begin(transformMatrix: Camera.instanceGameWorld.Matrix);
RenderHelper.DrawLine(pos.X, pos.Y, feelerPos.X, feelerPos.Y, Color.White);
spriteBatch.Draw(bodyTex, new Rectangle((int)pos.X - 27, (int)pos.Y - 27, 54, 54), color_inv);
spriteBatch.Draw(bodyTex, new Rectangle((int)pos.X - 25, (int)pos.Y - 25, 50, 50), color);
spriteBatch.Draw(feelerTex, new Rectangle((int)feelerPos.X - 5, (int)feelerPos.Y - 5, 10, 10), Color.Blue);
DrawCreature(spriteBatch, Vector2.Zero);

spriteBatch.End();
}

public void DrawCreature(SpriteBatch spriteBatch, Vector2 offset)
{
RenderHelper.DrawLine(spriteBatch, pos.X + offset.X, pos.Y + offset.Y, feelerPos.X + offset.X, feelerPos.Y + offset.Y, Color.White);
spriteBatch.Draw(bodyTex, new Rectangle((int)(pos.X + offset.X - 27), (int)(pos.Y + offset.Y - 27), 54, 54), color_inv);
spriteBatch.Draw(bodyTex, new Rectangle((int)(pos.X + offset.X - 25), (int)(pos.Y + offset.Y - 25), 50, 50), color);
spriteBatch.Draw(feelerTex, new Rectangle((int)(feelerPos.X + offset.X - 5), (int)(feelerPos.Y + offset.Y - 5), 10, 10), Color.Blue);
}

public void Serialize(BinaryWriter writer)
{
writer.Write("CreatureBegin");
Expand Down
15 changes: 10 additions & 5 deletions EvoNet/Objects/CreatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public override void Update(GameTime deltaTime)
}
}

Debug.WriteLine(OldestCreatureAlive.Age + " " + OldestCreatureAlive.Energy);

AliveCreaturesRecord.Add(Creatures.Count);

SelectedCreature = OldestCreatureAlive;
Expand Down Expand Up @@ -140,8 +138,6 @@ private void DrawGeneralStats()
else
{
spriteBatch.DrawString(Fonts.FontArial, "Creatures Alive Graph ", new Vector2(20, 180), Color.Red);
GraphRenderer.RenderGraph(spriteBatch, new Rectangle(20, 200, 260, 100), Color.Blue, AliveCreaturesRecord, Fonts.FontArial, true);
spriteBatch.DrawString(Fonts.FontArial, "Creatures Alive Graph ", new Vector2(20, 180), Color.Red);
GraphRenderer.RenderGraph(spriteBatch, new Rectangle(20, 200, 260, 100), Color.Blue, AliveCreaturesRecord, Fonts.FontArial, true);
spriteBatch.DrawString(Fonts.FontArial, "Average Age on Death Graph ", new Vector2(20, 320), Color.Red);
if (AverageAgeOfLastCreaturesAccurate)
Expand All @@ -154,7 +150,16 @@ private void DrawGeneralStats()

if(SelectedCreature != null)
{
SelectedCreature.Brain.Draw(spriteBatch, new Rectangle(950, 0, 200, 200));
Primitives2D.FillRectangle(spriteBatch, new Rectangle(800, 0, 500, 400), AdditionalColors.TRANSPARENTBLACK);

spriteBatch.DrawString(Fonts.FontArial, "Selected Creature: ", new Vector2(820, 50), Color.Red);
spriteBatch.DrawString(Fonts.FontArial, "A: " + SelectedCreature.Age, new Vector2(820, 70), Color.Red);
spriteBatch.DrawString(Fonts.FontArial, "E: " + SelectedCreature.Energy, new Vector2(820, 90), Color.Red);
spriteBatch.DrawString(Fonts.FontArial, "C: " + SelectedCreature.Children.Count, new Vector2(820, 110), Color.Red);
spriteBatch.DrawString(Fonts.FontArial, "G: " + SelectedCreature.Generation, new Vector2(820, 130), Color.Red);
spriteBatch.DrawString(Fonts.FontArial, "S: " + (SelectedCreature.Energy > 100 ? "Alive" : "Dead"), new Vector2(820, 150), Color.Red);
SelectedCreature.DrawCreature(spriteBatch, SelectedCreature.Pos * -1 + new Vector2(1050, 70));
SelectedCreature.Brain.Draw(spriteBatch, new Rectangle(950, 160, 200, 200));
}

spriteBatch.End();
Expand Down
14 changes: 4 additions & 10 deletions EvoNet/Rendering/RenderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,18 @@ namespace EvoNet.Rendering
{
class RenderHelper
{
private static SpriteBatch spriteBatch = null;
private static Texture2D whiteTexture = null;
private static Texture2D whiteCircleTexture = null;

public static void Ini(SpriteBatch spriteBatch, Texture2D whiteTexture, Texture2D whiteCircleTexture)
public static void Ini(Texture2D whiteTexture, Texture2D whiteCircleTexture)
{
RenderHelper.spriteBatch = spriteBatch;
RenderHelper.whiteTexture = whiteTexture;
RenderHelper.whiteCircleTexture = whiteCircleTexture;
}

public static void DrawLine(float x0, float y0, float x1, float y1, Color c)
public static void DrawLine(SpriteBatch spriteBatch, float x0, float y0, float x1, float y1, Color c, int lineWidth = 1)
{
float xDiff = x0 - x1;
float yDiff = y0 - y1;
float angle = (float)Math.Atan2(yDiff, xDiff);
float dist = (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff);

spriteBatch.Draw(whiteTexture, new Rectangle((int)x1, (int)y1, (int)dist, 1), null, c, angle, Vector2.Zero, SpriteEffects.None, 0);
Primitives2D.DrawLine(spriteBatch, x0, y0, x1, y1, c);
}

public static void DrawCircle(SpriteBatch spriteBatch, float x, float y, float radius, Color c)
Expand All @@ -42,5 +35,6 @@ public static void DrawCircle(SpriteBatch spriteBatch, float x, float y, float r
int radiusD = (int)Math.Round(radius * 2);
spriteBatch.Draw(whiteCircleTexture, new Rectangle(xD, yD, radiusD, radiusD), c);
}

}
}

0 comments on commit df85dc8

Please sign in to comment.