-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyStrategy.cs
466 lines (404 loc) · 17.9 KB
/
MyStrategy.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
using Com.CodeGame.CodeWizards2016.DevKit.CSharpCgdk.Model;
using System.Linq;
using System.Collections.Generic;
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Com.CodeGame.CodeWizards2016.DevKit.CSharpCgdk {
public sealed class MyStrategy : IStrategy {
Wizard me;
World world;
Game game;
Move move;
ClusterController clController = new ClusterController();
static Vector home;
static Vector mordor;
Faction OppositeFaction;
// static Vector firstAssault;
public void Move(Wizard me, World world, Game game, Move move) {
this.me = me;
this.world = world;
this.move = move;
this.game = game;
if(home.IsEmpty) {
home = me.Faction == Faction.Academy ? new Vector(600, 3390) : new Vector(3390, 600);
mordor = me.Faction == Faction.Renegades ? new Vector(600, 3390) : new Vector(3390, 600);
OppositeFaction = me.Faction == Faction.Academy ? Faction.Renegades : Faction.Academy;
}
AI.MakeMove(me, world, game, move);
return;
UpdateMap();
if(me.Life < me.MaxLife * 0.5) {
LivingUnit runFrom = FindDanger(); //TODO make separate logic block with loacl pathfinding to escape
if(runFrom != null && runFrom.GetDistanceTo(me) <= me.VisionRange ) {
if(me.GetDistanceTo(home.X, home.Y) <= me.VisionRange)
TryToGo(mordor.X, mordor.Y);
else
TryToGo(home.X, home.Y);
move.Action = ActionType.Staff;
//TryToGo(runFrom.X, runFrom.Y);
return;
}
}
//attack
LivingUnit archEnemy = FindArchEnemy();
if(archEnemy != null) {
Fight(archEnemy);
return;
}
MakeMove();
// FollowMinions();
}
bool attackNeutrals = false;
private void MakeMove() {
var bouns = world.Bonuses.OrderBy(b => b.GetDistanceTo(me)).ToList();
if(bouns.Count > 0) {
TryToGo(bouns[0].X, bouns[0].Y);
return;
}
var objects = new List<CircularUnit>();
objects.AddRange(world.Wizards.Where(w => !w.IsMe && w.Faction == OppositeFaction));
objects.AddRange(world.Buildings.Where(b => b.Faction == OppositeFaction));
objects.AddRange(world.Minions.Where(b => b.Faction != Faction.Neutral ));
clController.Update(objects);
if(clController.Clusters.Count > 0) {
var goals = clController.Clusters.OrderBy(c => c.Value(me)).ToList();
TryToGo(goals);
return;
}
TryToGo(2000, 2000);
}
//int
private void TryToGo(List<Cluster> goals) {
for(int i = goals.Count - 1; i >= 0; i--) {
var path = grid.GetPath(me.X, me.Y, goals[i].Position.X, goals[i].Position.Y);
if(path != null && path.Count > 1) {
Goal(true, path[1].X, path[1].Y);
return;
}
}
Goal(true, goals[0].Position.X, goals[0].Position.Y);
}
class Cluster {
const int COLORSPACE = 0xFF * 0xFF * 0xFF;
const int ALPHA = 0xFF << 24;
public List<CircularUnit> units = new List<CircularUnit>();
public Cluster(CircularUnit obj1) {
units.Add(obj1);
UpdatePosition();
//penColor = Color.FromArgb(rnd.Next(COLORSPACE) + ALPHA);
}
public Vector Position { get; internal set; }
//public Color penColor { get; internal set; }
//static Random rnd = new Random();
public static int Radius = 200;
public object Value(Wizard me) {
try {
if(units.Count < 3)
return 0d;
double d = me.GetDistanceTo(Position.X, Position.Y);
return d <= me.VisionRange * 2 ? 40000/d : units.Count;
}
catch(Exception e) {
}
return 100 / Position.DistanceTo(me.X, me.Y);
}
internal void AddUnit(CircularUnit obj1) {
units.Add(obj1);
UpdatePosition();
}
void UpdatePosition() {
Position = new Vector(units.Sum(u => u.X) / units.Count, units.Sum(u => u.Y) / units.Count);
}
internal bool IsOwnerOf(CircularUnit obj) {
foreach(var unit in units) {
if(unit.GetDistanceTo(obj) <= Radius)
return true;
}
return Position.DistanceTo(obj.X, obj.Y) <= Radius;
}
}
class ClusterController {
public List<Cluster> Clusters { get; set; } = new List<Cluster>();
internal void Update(List<CircularUnit> objects) {
Clusters = new List<Cluster>();
foreach(var obj1 in objects) {
Cluster owner = Clusters.Find(c => c.IsOwnerOf(obj1));
if(owner != null)
owner.AddUnit(obj1);
else {
Clusters.Add(new Cluster(obj1));
}
}
// if(Clusters.Count>0)
// Draw();
}
//public void Draw() {
// Pen pen = new Pen(Color.Red, 5);
// Bitmap bmp = new Bitmap(4000, 4000);
// Graphics gr = Graphics.FromImage(bmp);
// foreach(var cluster in Clusters) {
// // gr.DrawEllipse(pen, (float)(cluster.Position.X - cluster.Radius), (float)(cluster.Position.Y - cluster.Radius), (float)cluster.Radius*2f, (float)cluster.Radius*2f);
// foreach(var unit in cluster.units) {
// Brush b = new SolidBrush(Color.White);
// if(unit is Wizard) {
// if(unit.Faction == Faction.Academy)
// b = new SolidBrush(Color.Green);
// else
// b = new SolidBrush(Color.Red);
// }
// if(unit is Minion) {
// if(unit.Faction == Faction.Academy)
// b = new SolidBrush(Color.Lime);
// else
// b = new SolidBrush(Color.Cyan);
// }
// gr.FillEllipse(b, (float)(unit.X - unit.Radius), (float)(unit.Y - unit.Radius), (float)unit.Radius * 2f, (float)unit.Radius * 2f);
// gr.DrawEllipse(new Pen( cluster.penColor, 10), (float)(unit.X - unit.Radius), (float)(unit.Y - unit.Radius), (float)unit.Radius * 2f, (float)unit.Radius * 2f);
// }
// }
// bmp.Save("clusters.png", ImageFormat.Png);
//}
}
private void Fight(LivingUnit archEnemy) {
double dist = me.GetDistanceTo(archEnemy);
if(dist > me.CastRange * 0.9) {
Goal(true, archEnemy.X, archEnemy.Y);
return;
}
else
if(dist < me.CastRange * 0.7) {
Goal(false, archEnemy.X, archEnemy.Y);
}
Attack(archEnemy);
}
private void UpdateMap() {
var objects = new List<CircularUnit>();
objects.AddRange(world.Buildings);
objects.AddRange(world.Trees);
if(grid == null)
grid = new Grid(objects);
else
grid.Reveal(objects);
}
private void TryToGo(double x, double y) {
if(grid != null) {
var path = grid.GetPath(me.X, me.Y, x, y);
if(path != null && path.Count > 1) {
Goal(true, path[1].X, path[1].Y);
return;
}
}
Goal(true, x, y);
}
Grid grid;
bool BeatACreeps() {
attackNeutrals = true;
List<Minion> objects = world.Minions.Where(b => b.Faction == OppositeFaction || (attackNeutrals && b.Faction == Faction.Neutral)).ToList();
if(objects.Count != 0) {
var neutrals = GetNearEnemyUnits(3000);
if(neutrals != null) {
var neu = neutrals.OrderBy(n => me.GetDistanceTo(n)).Last();
TryToGo(neu.X, neu.Y);
return true;
}
}
return false;
}
void Attack(LivingUnit archEnemy) {
double angle = me.GetAngleTo(archEnemy);
var dist = archEnemy.GetDistanceTo(me);
if(Math.Abs(angle) > 0.01)
move.Turn = angle;
//move.Speed = 0;
move.Action = ActionType.MagicMissile;
move.MinCastDistance = dist - archEnemy.Radius * 1.1;
move.MaxCastDistance = dist + archEnemy.Radius * 1.1;
//if(strafe == 30) {
// strafeSpeed = -1;
//}
//if(strafe == -30) {
// strafeSpeed = 1;
//}
//move.StrafeSpeed = strafeSpeed * game.WizardStrafeSpeed;
//strafe += strafeSpeed;
}
LivingUnit GetFave() {
try {
return world.Minions.OrderBy(m => m.GetDistanceTo(me)).First();
}
catch (Exception e){
}
return null;
}
LivingUnit FindDanger() {
List<LivingUnit> danger = GetNearEnemyUnits(me.VisionRange);
if(danger != null && danger.Count > 0)
return danger[0];
return null;
//TODO calc danger units in order and local path to escape
//if(danger != null && (me.Life < me.MaxLife * 0.5 && me.GetDistanceTo(danger.X, danger.Y) < me.CastRange ))
// return danger;
//return null;
}
List<LivingUnit> GetNearEnemyUnits(double range) {
try {
List<LivingUnit> list = new List<LivingUnit>();
list.AddRange(world.Minions);
list.AddRange(world.Wizards);
list.AddRange(world.Buildings);
var all = list.Where(
w => w.Faction == OppositeFaction ||
(w.Faction == Faction.Neutral && (w.Life < w.MaxLife || attackNeutrals)));
if(attackNeutrals && all.FirstOrDefault(u => u.Faction == OppositeFaction) != null)
attackNeutrals = false;
return all.Where(u => u.GetDistanceTo(me.X, me.Y) <=range).ToList();
}
catch(Exception e) {
}
return null;
}
LivingUnit FindArchEnemy() {
var enemies = GetNearEnemyUnits(me.VisionRange);
if(enemies == null)
return null;
double bestValue = double.MinValue;
LivingUnit result = null;
foreach(var en in enemies) {
double HPfactor = 1.0 - (double)en.Life / en.MaxLife;
var dist = en.GetDistanceTo(me);
double distFactor = dist >= me.VisionRange ? -10 : dist >= en.Radius + me.Radius ? 100 / dist : dist * 100;
double typeFactor = GetTypeFactor(en);
double value = (HPfactor + typeFactor + distFactor) / 3.0;
if(value > bestValue && value > 0) {
bestValue = value;
result = en;
}
}
return result;
}
double GetTypeFactor(LivingUnit en) {
return 1;
if(en is Minion) return 0.6;
if(en is Wizard) return 1;
if(en is Building) {
if((en as Building).Type == BuildingType.FactionBase) return 0.8;
if((en as Building).Type == BuildingType.GuardianTower) return 0.6;
}
return 0;
}
Vector CalcFaveNearPoint(Unit goalUnit, double angleTo, double distTo) {
double angleTotal = goalUnit.Angle + angleTo;
return new Vector(goalUnit.X - Math.Cos(angleTotal) * distTo, goalUnit.Y - Math.Sin(angleTotal) * distTo);
}
void Goal(bool fwd, double x, double y) {
if(WalkAround())
return;
move.Turn = me.GetAngleTo(x, y);
move.Speed = fwd ? game.WizardForwardSpeed : -game.WizardBackwardSpeed;
//if(!fwd) move.Action = ActionType.MagicMissile;
}
bool WalkAround() {
List<CircularUnit> blocks = new List<CircularUnit>();
blocks.AddRange(world.Buildings);
blocks.AddRange(world.Trees);
blocks.AddRange(world.Minions);
blocks.AddRange(world.Wizards);
try {
CircularUnit obj = blocks.Where(b => b.Id != me.Id).OrderBy(u => u.GetDistanceTo(me)).First();
double minDist = me.Radius + obj.Radius + 50;
double angle = me.GetAngleTo(obj.X, obj.Y);
double dist = obj.GetDistanceTo(me);
if(Math.Abs(angle) <= Math.PI / 2 && dist <= minDist) {
if(wallAroundcounter == 30)
wallArounddir = -1;
if(wallAroundcounter == 0)
wallArounddir = 1;
move.Speed = game.WizardForwardSpeed * wallArounddir;
wallAroundcounter += wallArounddir;
move.Turn = -angle;
return true;
}
}
catch(Exception e) {
};
// wallAroundcounter = 0;
return false;
}
int wallAroundcounter = 0;
int wallArounddir = 1;
}
//public static class CpWalker {
// public static CheckpointList points;
// static CpWalker() {
// InitializeMap();
// }
// public static Vector NextPointToGo(double x0, double y0, double x1, double y1) {
// Vector end = new Vector(x1, y1);
// Vector start = new Vector(x0, y0);
// var around = points.list.Where(p => p.Value.Position.DistanceTo(start) < Checkpoint.Radius).ToList();
// //in some point
// if(around != null && around.Count > 0) {
// var nextPoints = around.First().Value.Next;
// int currentPoint = around.First().Key;
// double min = double.MaxValue;
// int resultIndex = -1;
// foreach(int index in nextPoints) {
// double dist = points[index].Position.DistanceTo(end);
// if(dist < min) {
// min = dist;
// resultIndex = index;
// }
// }
// return points[resultIndex].Position;
// }
// //not in some point
// var closest = points.list.OrderBy(p => p.Value.Position.DistanceTo(start)).Take(2);
// var goTo = closest.OrderBy(p => p.Value.Position.DistanceTo(end)).First();
// return goTo.Value.Position;
// }
// static void InitializeMap() {
// points = new CheckpointList();
// points.Add(0, 185, 3330, 1, 3);
// points.Add(1, 630, 3390, 1, 2, 4);
// points.Add(2, 650, 3830, 1, 5);
// points.Add(3, 190, 2670, 0, 6);
// points.Add(4, 1080, 2980, 1, 7);
// points.Add(5, 1390, 3820, 2, 8);
// points.Add(6, 170, 1650, 3, 9);
// points.Add(7, 1545, 2435, 4, 10);
// points.Add(8, 2280, 3820, 5, 11);
// points.Add(9, 275, 275, 6, 10, 12);
// points.Add(10, 2000, 2000, 7, 9, 13, 11);
// points.Add(11, 3600, 3600, 8, 10, 14);
// points.Add(12, 1675, 233, 9, 15);
// points.Add(13, 2529, 1457, 10, 16);
// points.Add(14, 3840, 2350, 11, 17);
// points.Add(15, 2650, 170, 12, 18);
// points.Add(16, 2960, 980, 13, 19);
// points.Add(17, 2777, 1313, 14, 20);
// points.Add(18, 3300, 177, 15, 19);
// points.Add(19, 3385, 561, 16, 18, 20);
// points.Add(20, 3785, 600, 17, 19);
// }
//}
//public class CheckpointList {
// public Dictionary<int, Checkpoint> list = new Dictionary<int, Checkpoint>();
// internal void Add(int id, double x, double y, params int[] next) {
// list[id] = new Checkpoint(x, y, next);
// }
// public Checkpoint this[int i] {
// get { return list[i]; }
// // set { InnerList[i] = value; }
// }
//}
//public class Checkpoint {
// public Vector Position;
// public const int Radius = 50;
// public List<int> Next = new List<int>();
// public Checkpoint(double x, double y, int[] next) {
// Position = new Vector(x, y);
// Next.AddRange(next);
// }
//}
}