Skip to content

Commit

Permalink
Prepare Virtual World scene for car and neural network integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Selinux24 committed Aug 2, 2024
1 parent 1c13b14 commit 21e450c
Show file tree
Hide file tree
Showing 58 changed files with 232 additions and 231 deletions.
10 changes: 10 additions & 0 deletions Samples/05 AISamples/Common/Agents/AgentControlTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace AISamples.Common.Agents
{
enum AgentControlTypes
{
Player,
Dummy,
AI,
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@

namespace AISamples.SceneCWRSelfDrivingCar
namespace AISamples.Common.Agents
{
class CarControls
class AgentControls
{
public const int InputCount = 4;

private readonly CarControlTypes controlType;
private readonly AgentControlTypes controlType;

public bool Forward { get; set; } = false;
public bool Reverse { get; set; } = false;
public bool Left { get; set; } = false;
public bool Right { get; set; } = false;


public CarControls(CarControlTypes controlType)
public AgentControls(AgentControlTypes controlType)
{
this.controlType = controlType;

Expand Down Expand Up @@ -48,11 +47,11 @@ public void Reset()

switch (controlType)
{
case CarControlTypes.Dummy:
case AgentControlTypes.Dummy:
Forward = true;
break;
case CarControlTypes.Player:
case CarControlTypes.AI:
case AgentControlTypes.Player:
case AgentControlTypes.AI:
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Engine;
using SharpDX;

namespace AISamples.SceneCWRSelfDrivingCar
namespace AISamples.Common.Agents
{
class CarFollower(float distance, float height) : IFollower
class AgentFollower(float distance, float height) : IFollower
{
private readonly float distance = distance;
private readonly float height = height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Linq;

namespace AISamples.SceneCWRSelfDrivingCar
namespace AISamples.Common.Agents
{
class Car
{
Expand All @@ -23,18 +23,18 @@ class Car
private readonly float rotationSpeed = 0.02f;
private Vector2 direction = Vector2.Zero;

public CarControls Controls { get; }
public AgentControls Controls { get; }
public bool Forward => !Damaged && Controls.Forward;
public bool Reverse => !Damaged && Controls.Reverse;
public bool Left => !Damaged && Controls.Left;
public bool Right => !Damaged && Controls.Right;

public CarControlTypes ControlType { get; }
public AgentControlTypes ControlType { get; }
public Sensor Sensor { get; }
public NeuralNetwork Brain { get; }
public bool Damaged { get; private set; } = false;

public Car(float width, float height, float depth, CarControlTypes controlType, float maxSpeed = 3f, float maxReverseSpeed = 1.5f)
public Car(float width, float height, float depth, AgentControlTypes controlType, float maxSpeed = 3f, float maxReverseSpeed = 1.5f)
{
box = new(new(width * -0.5f, 0, depth * -0.5f), new(width * 0.5f, height, depth * 0.5f));
ControlType = controlType;
Expand All @@ -43,10 +43,10 @@ public Car(float width, float height, float depth, CarControlTypes controlType,

Controls = new(controlType);

if (controlType != CarControlTypes.Dummy)
if (controlType != AgentControlTypes.Dummy)
{
Sensor = new(this, 5, 50, MathUtil.PiOverTwo);
Brain = new([Sensor.GetRayCount(), 6, CarControls.InputCount]);
Brain = new([Sensor.GetRayCount(), 6, AgentControls.InputCount]);
}
}

Expand All @@ -73,7 +73,7 @@ public Segment[] GetPolygon()
return segments;
}

public void Update(IGameTime gameTime, Road road, Car[] traffic, bool damageTraffic)
public void Update(IGameTime gameTime, Segment[] roadBorders, Car[] traffic, bool damageTraffic)
{
float time = gameTime.ElapsedSeconds;

Expand All @@ -82,15 +82,15 @@ public void Update(IGameTime gameTime, Road road, Car[] traffic, bool damageTraf

if (!Damaged)
{
Damaged = AssessDamage(road, traffic, damageTraffic);
Damaged = AssessDamage(roadBorders, traffic, damageTraffic);
}

if (Sensor == null)
{
return;
}

Sensor.Update(road, traffic);
Sensor.Update(roadBorders, traffic);

if (Brain == null)
{
Expand Down Expand Up @@ -144,10 +144,8 @@ private void Move(float time)
x += direction.X * speed;
y += direction.Y * speed;
}
private bool AssessDamage(Road road, Car[] traffic, bool damageTraffic)
private bool AssessDamage(Segment[] roadBorders, Car[] traffic, bool damageTraffic)
{
var roadBorders = road.GetBorders();

for (int i = 0; i < roadBorders.Length; i++)
{
if (Utils.Segment2DIntersectsPoly2D(roadBorders[i], points))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using SharpDX;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AISamples.SceneCWRVirtualWorld
namespace AISamples.Common
{
class Graph
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using Engine.Common;
using SharpDX;
using System.Collections.Generic;
using System.Linq;

namespace AISamples.SceneCWRVirtualWorld.Items
namespace AISamples.Common.Items
{
class Building(Polygon polygon, float height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.Common.Persistence;
using Engine;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Items
namespace AISamples.Common.Items
{
class Tree(Vector3 position, float radius, float height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Engine;
using AISamples.Common.Persistence;
using Engine;
using SharpDX;

namespace AISamples.SceneCWRSelfDrivingCar
namespace AISamples.Common
{
class Level
{
Expand Down Expand Up @@ -152,12 +153,4 @@ public void Mutate(float amount)
}
}
}

class LevelFile
{
public float[] Inputs { get; set; }
public float[] Outputs { get; set; }
public float[] Biases { get; set; }
public float[][] Weights { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Crossing(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using Engine.Common;
using SharpDX;
using System.Collections.Generic;
using System.Linq;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Light(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height, true)
{
Expand Down Expand Up @@ -43,19 +42,19 @@ protected override IEnumerable<VertexPositionTexture> CreateMarking(float height
{
var support = GetSupport();
var border = GetBorder();
var p = border.P2 + (border.Direction * border.Length);
var p = border.P2 + border.Direction * border.Length;

var dir = new Vector3(support.Direction.X, 0, support.Direction.Y);
const float baseH = 10f;
const float baseR = 0.5f;
var baseCenter = new Vector3(p.X, height + (baseH * 0.5f), p.Y);
var baseCenter = new Vector3(p.X, height + baseH * 0.5f, p.Y);
const float topH = 6f;
const float topR = 1f;
var topCenter = new Vector3(p.X, height + baseH + (topH * 0.5f), p.Y);
var topCenter = new Vector3(p.X, height + baseH + topH * 0.5f, p.Y);
const float sphR = 0.5f;
var sphCenter1 = new Vector3(p.X, height + baseH + (topH * 0.8f), p.Y) + (dir * topR);
var sphCenter2 = new Vector3(p.X, height + baseH + (topH * 0.5f), p.Y) + (dir * topR);
var sphCenter3 = new Vector3(p.X, height + baseH + (topH * 0.2f), p.Y) + (dir * topR);
var sphCenter1 = new Vector3(p.X, height + baseH + topH * 0.8f, p.Y) + dir * topR;
var sphCenter2 = new Vector3(p.X, height + baseH + topH * 0.5f, p.Y) + dir * topR;
var sphCenter3 = new Vector3(p.X, height + baseH + topH * 0.2f, p.Y) + dir * topR;

var baseCyl = GeometryUtil.CreateCylinder(Topology.TriangleList, baseCenter, baseR, baseH, 16);
var topCyl = GeometryUtil.CreateCylinder(Topology.TriangleList, topCenter, topR, topH, 16);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using Engine.Common;
Expand All @@ -8,7 +8,7 @@
using System.Collections.Generic;
using System.Linq;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
abstract class Marking
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Parking(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.Common.Persistence;
using Engine;
using Engine.BuiltIn.Primitives;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Start(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Stop(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.Common.Persistence;
using Engine;
using Engine.BuiltIn.Primitives;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Target(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using AISamples.Common;
using AISamples.SceneCWRVirtualWorld.Content;
using AISamples.SceneCWRVirtualWorld.Primitives;
using AISamples.Common.Persistence;
using AISamples.Common.Primitives;
using Engine;
using Engine.BuiltIn.Primitives;
using SharpDX;

namespace AISamples.SceneCWRVirtualWorld.Markings
namespace AISamples.Common.Markings
{
class Yield(Vector2 position, Vector2 direction, float width, float height) : Marking(position, direction, width, height)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Engine;
using AISamples.Common.Persistence;
using Engine;
using System;
using System.IO;
using System.Linq;

namespace AISamples.SceneCWRSelfDrivingCar
namespace AISamples.Common
{
class NeuralNetwork
{
Expand Down Expand Up @@ -70,7 +71,7 @@ public void Save(string fileName)
Outputs = [.. outputs]
};

SerializationHelper.SerializeJsonToFile(file, fileName);
file.SerializeJsonToFile(fileName);
}
public void Load(string fileName)
{
Expand All @@ -96,10 +97,4 @@ public void Mutate(float amount)
}
}
}

class NeuralNetworkFile
{
public LevelFile[] Levels { get; set; }
public float[] Outputs { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

namespace AISamples.SceneCWRVirtualWorld.Content
namespace AISamples.Common.Persistence
{
struct BuildingFile
{
Expand Down
Loading

0 comments on commit 21e450c

Please sign in to comment.