Skip to content

Commit ab58a3d

Browse files
committed
Feature: Aim asteroids towards the ship when in pursuit mode.
1 parent af0c294 commit ab58a3d

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

G33kShell.Desktop/Console/Screensavers/AI/AiBrainBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
//
1010
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND.
1111
using System;
12-
using System.Linq;
1312
using CSharp.Core.AI;
1413
using CSharp.Core.Extensions;
1514
using Newtonsoft.Json;
15+
// ReSharper disable once RedundantUsingDirective
16+
using System.Linq;
1617

1718
namespace G33kShell.Desktop.Console.Screensavers.AI;
1819

G33kShell.Desktop/Console/Screensavers/Asteroids/Asteroid.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Asteroid
1313
private readonly Random m_rand;
1414
private readonly int m_arenaWidth;
1515
private readonly int m_arenaHeight;
16-
private readonly float m_direction;
16+
private float m_direction;
1717
private readonly Vector2[] m_offsets;
1818
private static readonly int[] Radii = [2, 4, MaxRadius];
1919
private static readonly int[] SizeMetrics = [1, 2, 4];
@@ -131,4 +131,10 @@ public float DistanceTo(Vector2 shipPosition)
131131

132132
return MathF.Sqrt(minDist);
133133
}
134+
135+
public void AimTowards(Vector2 target)
136+
{
137+
var direction = target - Position;
138+
m_direction = MathF.Atan2(direction.Y, direction.X);
139+
}
134140
}

G33kShell.Desktop/Console/Screensavers/Asteroids/Game.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public override double Rating
4040
if (m_thrustTicks == 0)
4141
return 0.0; // Penalize non-thrusters.
4242

43-
var rating = 1.0 + Score * Score * HitRatio * Math.Min(m_gameTicks, 20 * 60) * TurnEquality * 0.00001;
43+
var rating = 1.0 +
44+
Score * Score *
45+
HitRatio *
46+
Math.Min(m_gameTicks, 20 * 60) *
47+
TurnEquality
48+
* 0.00001;
4449
return rating * rating;
4550
}
4651
}
@@ -91,20 +96,23 @@ public override Game ResetGame()
9196
m_thrustTicks = 0;
9297
m_ticksSinceScore = 0;
9398

94-
EnsureMinimumAsteroidCount();
99+
EnsureMinimumAsteroidCount(true);
95100

96101
return this;
97102
}
98103

99-
private void EnsureMinimumAsteroidCount()
104+
private void EnsureMinimumAsteroidCount(bool aimForShip)
100105
{
101106
while (Asteroids.FastSum(o => o.SizeMetric) < 12)
102107
{
103108
var pos = new Vector2(GameRand.NextFloat() * ArenaWidth, GameRand.NextFloat() * ArenaHeight);
104109
if (Vector2.Distance(pos, Ship.Position) < 25)
105110
continue; // Too close to the ship.
106111

107-
Asteroids.Add(new Asteroid(pos, 0.1f, GameRand, ArenaWidth, ArenaHeight));
112+
var asteroid = new Asteroid(pos, 0.1f, GameRand, ArenaWidth, ArenaHeight);
113+
if (aimForShip)
114+
asteroid.AimTowards(Ship.Position);
115+
Asteroids.Add(asteroid);
108116
}
109117
}
110118

@@ -117,7 +125,7 @@ public override void Tick()
117125
m_ticksSinceScore++;
118126

119127
// Spawn asteroids.
120-
EnsureMinimumAsteroidCount();
128+
EnsureMinimumAsteroidCount(false);
121129

122130
// Move ship.
123131
Ship.Move();
@@ -150,7 +158,7 @@ public override void Tick()
150158
// Check for bullet/asteroid collisions.
151159
if (Bullets.Count > 0)
152160
{
153-
var bulletsToRemove = new List<Bullet>();
161+
List<Bullet> bulletsToRemove = null;
154162
for (var index = 0; index < Bullets.Count; index++)
155163
{
156164
var bullet = Bullets[index];
@@ -169,14 +177,18 @@ public override void Tick()
169177
continue; // Bullet not hitting anything.
170178

171179
// Bullet hit an asteroid.
180+
bulletsToRemove ??= new List<Bullet>();
172181
bulletsToRemove.Add(bullet);
173182
hitAsteroid.Explode(Asteroids);
174183
Score++;
175184
m_ticksSinceScore = 0;
176185
}
177186

178-
for (var i = 0; i < bulletsToRemove.Count; i++)
179-
Bullets.Remove(bulletsToRemove[i]);
187+
if (bulletsToRemove != null)
188+
{
189+
for (var i = 0; i < bulletsToRemove.Count; i++)
190+
Bullets.Remove(bulletsToRemove[i]);
191+
}
180192
}
181193

182194
// Check for ship/asteroid collisions.

G33kShell.Desktop/Console/Screensavers/Asteroids/GameState.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void FillInputVector(double[] inputVector)
3939
inputVector[0] = 1.0;
4040

4141
// Find nearest asteroid.
42-
var asteroid = m_asteroids.Count == 0 ? null : m_asteroids.FastFindMin(o => Vector2.DistanceSquared(o.Position, m_ship.Position));
42+
var asteroid = m_asteroids.Count == 0 ? null : m_asteroids.FastFindMin(o => GetDistanceToAsteroid(o, m_ship));
4343
if (asteroid != null)
4444
{
4545
var relativePos = asteroid.Position - m_ship.Position;
@@ -59,4 +59,7 @@ public void FillInputVector(double[] inputVector)
5959
inputVector[4] = relativeVelocity.Y.Clamp(-1.0f, 1.0f);
6060
inputVector[5] = (m_ship.Theta / Math.Tau).Clamp(-1.0f, 1.0f);
6161
}
62+
63+
private static float GetDistanceToAsteroid(Asteroid asteroid, Ship ship) =>
64+
Vector2.DistanceSquared(asteroid.Position, ship.Position);
6265
}

0 commit comments

Comments
 (0)