-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.cs
305 lines (238 loc) · 11 KB
/
Bot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System.Numerics;
using Application.Actions;
namespace Application;
public class Bot
{
public const string NAME = "My cool C# bot";
private Dictionary<Debris, Projectile> targetedMeteors = new();
private Debris targetMeteor;
private Debris targetedMeteor;
private GameMessage gameMessage;
/// <summary>
/// This method should be use to initialize some variables you will need throughout the game.
/// </summary>
public Bot()
{
Console.WriteLine("Initializing your super mega bot!");
}
/// <summary>
/// Here is where the magic happens, for now the moves are random. I bet you can do better ;)
/// </summary>
public IEnumerable<Action> GetNextMoves(GameMessage gameMessage)
{
var actions = new List<Action>();
this.gameMessage = gameMessage;
Debris[] targetableMeteors = gameMessage.Debris.ToArray();
Debris[] untargetedTargetableMeteors =
targetableMeteors.Except(targetedMeteors.Keys, new DebrisEqualityComparer()).ToArray();
var ourShip = this.gameMessage.Ships[gameMessage.CurrentTeamId];
var theirShips = this.gameMessage.ShipsPositions.Where(ship => ship.Key != gameMessage.CurrentTeamId).ToList();
try
{
if (gameMessage.CurrentTickNumber == 1)
{
CrewmateManager crewmateManager = new CrewmateManager(ourShip.Crew.ToList());
actions.AddRange(crewmateManager.moveCrewmates(ourShip.Stations.Turrets.ToList<Station>().GetRange(0,4)));
//CrewmateManager crewmateManager2 = new CrewmateManager(ourShip.Crew.ToList().GetRange(3,1));
// List<Station> helm = new List<Station>(ourShip.Stations.Helms.Take(1).ToList());
// actions.AddRange(crewmateManager2.moveCrewmates(helm).Item1.GetActions());
}
if (gameMessage.CurrentTickNumber % 1000 == 0)
{
foreach (var crewmate in ourShip.Crew.ToList().GetRange(0,4))
{
}
}
}
catch (Exception e)
{
MOVECREW(gameMessage, actions);
}
//enlever les cibles qui ont deja ete tirees
foreach (var turret in ourShip.Stations.Turrets.Where(turret => turret.Operator != null))
{
Action orient;
if (turret.TurretType == TurretType.EMP)
{
orient = new TurretLookAtAction(turret.Id,new Vector(theirShips.First().Value.X, theirShips.First().Value.Y)); // changer avant la compet pour le premier ennemi
}
else
{
FindBestTarget(turret,gameMessage.Constants.Ship.Stations.TurretInfos[turret.TurretType],untargetedTargetableMeteors,out (double x,double y ) shotPosition, ourShip.WorldPosition, gameMessage.Constants.Ship.Stations.Shield.ShieldRadius);
if (shotPosition is { x: 0, y: 0 })
{
orient = new TurretLookAtAction(turret.Id,new Vector(theirShips.First().Value.X,theirShips.First().Value.Y));
}
else
{
orient = new TurretLookAtAction(turret.Id,new Vector(shotPosition.x,shotPosition.y));
}
}
if (!gameMessage.Constants.Ship.Stations.TurretInfos[turret.TurretType].Rotatable)
{
actions.Add(new ShipLookAtAction(new Vector(theirShips.First().Value.X,
theirShips.First().Value.Y)));
//actions = actions.Concat(PositionWeaponTowardsFirstEnemy(new Vector(theirShips.First().Value.X, theirShips.First().Value.Y))).ToList();
}
Action shoot = new TurretShootAction(turret.Id);
actions.Add(orient);
actions.Add(shoot);
}
//selectionner la meilleure cible selon lheuristique
return actions;
}
private static void MOVECREW(GameMessage gameMessage, List<Action> actions)
{
var myShip = gameMessage.Ships[gameMessage.CurrentTeamId];
// var otherShipsIds = gameMessage.ShipsPositions.Keys.Where(shipId => shipId != gameMessage.CurrentTeamId)
// .ToList();
//
// // You could find who's not doing anything and try to give them a job?
var idleCrewmates = myShip.Crew
.Where(crewmate => crewmate.CurrentStation == null && crewmate.Destination == null)
.ToList();
foreach (var crewmate in idleCrewmates)
{
var visitableStations = crewmate.DistanceFromStations.Shields
.Concat(crewmate.DistanceFromStations.Turrets)
.Concat(crewmate.DistanceFromStations.Helms)
.Concat(crewmate.DistanceFromStations.Radars)
.ToList();
var stationToMoveTo = visitableStations[Random.Shared.Next(visitableStations.Count)];
actions.Add(new CrewMoveAction(crewmate.Id, stationToMoveTo.StationPosition));
}
}
private void FindBestTarget(TurretStation turretStation, TurretInfo turret,Debris[] targetableMeteors, out (double x, double y ) shotPosition, Vector shipPosition, double shipRadius)
{
var validShots = new List<Shot>();
var bestShots = new List<Shot>();
foreach (var meteor in targetableMeteors)
{
var rocketPosition = turretStation;
for (var currentTick = 0; currentTick < 1000; currentTick++)
{
var nextPosition = PositionAt(meteor, currentTick);
var distancesInCannonTicks = Distance(nextPosition, (rocketPosition.WorldPosition.X, rocketPosition.WorldPosition.Y)) /
turret.RocketSpeed;
if (distancesInCannonTicks < currentTick && isInBounds(nextPosition) && WillCollide(shipPosition, shipRadius, meteor))
{
validShots.Add(new Shot()
{
lostTicks = currentTick - distancesInCannonTicks,
totalTicks = currentTick,
X = nextPosition.x,
Y = nextPosition.y,
target = meteor
});
}
}
if (validShots.Count > 0)
{
bestShots.Add(validShots.MinBy(shot => shot.lostTicks));
}
validShots.Clear();
}
if (bestShots.Count > 0)
{
Shot bestShot = bestShots.OrderBy(shot => Distance( (shot.X, shot.Y ), (gameMessage.Ships[gameMessage.CurrentTeamId].WorldPosition.X,gameMessage.Ships[gameMessage.CurrentTeamId].WorldPosition.Y)) ).ThenByDescending(shot => shot.target.DebrisType)
.First();
targetedMeteor = bestShot.target;
shotPosition = (bestShot.X, bestShot.Y);
}
else
{
targetedMeteor = null;
shotPosition = (0, 0);
}
}
private bool WillCollide(Vector shipPosition, double shipRadius, Debris meteor)
{
double meteorRadius = gameMessage.Constants.DebrisInfos[meteor.DebrisType].Radius;
Vector meteorPosition = meteor.Position;
Vector meteorVelocity = meteor.Velocity;
// Calculate the time of collision using relative velocity
double timeToCollision = MathUtil.Dot(MathUtil.Subtract(shipPosition, meteorPosition), meteorVelocity) / MathUtil.LengthSquared(meteorVelocity);
// Calculate the predicted position of the meteor at the time of collision
Vector predictedMeteorPosition = MathUtil.Add(meteorPosition, MathUtil.Multiply(meteorVelocity, timeToCollision));
// Check if the distance between the ship and predicted meteor position is less than the sum of their radii
double distance = MathUtil.Length(MathUtil.Subtract(predictedMeteorPosition, shipPosition));
double combinedRadii = shipRadius + meteorRadius;
return distance < combinedRadii;
}
private (double x, double y) Velocity((double x, double y) position1, (double x, double y) position2, double t)
{
double vx = (position2.x - position1.x) / t;
double vy = (position2.y - position1.y) / t;
return (vx, vy);
}
private bool isInBounds((double x, double y) position)
{
if (position.x > gameMessage.Constants.World.Width || position.x < 140 || position.y > gameMessage.Constants.World.Height || position.y < 0)
{
return false;
}
return true;
}
private double Distance((double x, double y) position1, (double x, double y) position2)
{
double deltaX = position2.x - position1.x;
double deltaY = position2.y - position1.y;
return Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
}
private (double x, double y) PositionAt(Projectile projectile, int t)
{
return (projectile.Position.X + projectile.Velocity.X * t, projectile.Position.Y + projectile.Velocity.Y * t);
}
private List<Action> PositionWeaponTowardsFirstEnemy(Vector enemyPosition)
{
try
{
var enemyShip = enemyPosition;
var weaponToShootFrom = gameMessage.Ships[gameMessage.CurrentTeamId].Stations.Turrets.Where(turret =>
!gameMessage.Constants.Ship.Stations.TurretInfos[turret.TurretType].Rotatable).ToList().First();
var weaponAngle = weaponToShootFrom.OrientationDegrees;
var ownShipPosition = gameMessage.Ships[gameMessage.CurrentTeamId].WorldPosition;
var actions = new List<Action>();
if (Math.Abs(MathUtil.AngleBetween(MathUtil.Subtract(enemyShip, ownShipPosition),
MathUtil.FromAngleDegrees(weaponAngle))) > 1e-3f)
{
actions.Add(new ShipRotateAction(Math.Abs(MathUtil.AngleBetween(
MathUtil.Subtract(enemyShip, ownShipPosition),
MathUtil.FromAngleDegrees(weaponAngle)))));
}
return actions;
}
catch (Exception e)
{
return new List<Action>();
}
}
class DebrisEqualityComparer : IEqualityComparer<Debris>
{
public bool Equals(Debris? x, Debris? y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x.Id == y.Id;
}
public int GetHashCode(Debris obj)
{
return obj.Id.GetHashCode();
}
}
public struct Shot
{
public Shot()
{
lostTicks = Double.MaxValue;
}
public double totalTicks { get; set; }
public double lostTicks { get; set; }
public double X { get; set; }
public double Y { get; set; }
public Debris target { get; set; }
public override string ToString() => $"({X}, {Y})";
}
}