-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyStrategy.cs
333 lines (284 loc) · 14 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
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using Aicup2020.Model;
using Action = Aicup2020.Model.Action;
#endregion
namespace Aicup2020
{
public class MyStrategy
{
private PlayerView View { get; set; }
private DebugInterface Debug { get; set; }
private World Around { get; }
private Dictionary<int, EntityAction> actions;
private readonly List<int[]> positionsToBuildAround5;
private readonly List<int[]> positionsToBuildAround3;
private readonly List<int[]> positionsToBuildAround2;
public MyStrategy()
{
Around = new World();
positionsToBuildAround5 = new List<int[]>
{
new[] {-1, -1},
new[] {0, -1},
new[] {1, -1},
new[] {2, -1},
new[] {3, -1},
new[] {4, -1},
new[] {5, -1},
new[] {-1, 0},
new[] {-1, 1},
new[] {-1, 2},
new[] {-1, 3},
new[] {-1, 4},
new[] {-1, 5},
new[] {0, 5},
new[] {1, 5},
new[] {2, 5},
new[] {3, 5},
new[] {4, 5},
new[] {5, 5},
new[] {5, 4},
new[] {5, 3},
new[] {5, 2},
new[] {5, 1},
new[] {5, 0}
};
positionsToBuildAround3 = new List<int[]>
{
new[] {-1, -1},
new[] {0, -1},
new[] {1, -1},
new[] {2, -1},
new[] {3, -1},
new[] {3, 0},
new[] {3, 1},
new[] {3, 2},
new[] {3, 3},
new[] {2, 3},
new[] {1, 3},
new[] {0, 3},
new[] {-1, 3},
new[] {-1, 2},
new[] {-1, 1},
new[] {-1, 0}
};
positionsToBuildAround2 = new List<int[]>
{
new[] {-1, -1},
new[] {-1, 0},
new[] {-1, 1},
new[] {-1, 2},
new[] {0, 2},
new[] {1, 2},
new[] {2, 2},
new[] {2, 1},
new[] {2, 0},
new[] {2, -1},
new[] {1, -1},
new[] {0, -1},
};
}
public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
{
actions = new Dictionary<int, EntityAction>();
View = playerView;
Debug = debugInterface;
Around.Scan(View);
CommandBuildingsWorkers();
CommandBuildingsRange();
CommandUnitsWorkers();
CommandUnitsRanged();
CommandUnitsMelee();
CommandUnitsTurrets();
return new Action(actions);
}
#region Buildings
private void CommandBuildingsWorkers()
{
var workerUnitsCount = Around.PopulationProvide * Around.unitsRatio[0];
foreach (var builderBuilding in Around.MyBuildingsWorkers)
{
var needBuildBuilders = Around.MyUnitsWorkers.Count() < workerUnitsCount
&& Around.Me.Resource >= Around.WorkerUnitCost;
actions.Add(builderBuilding.Id,
needBuildBuilders
? new EntityAction(null, new BuildAction(EntityType.BuilderUnit, GetRndPositionAround(builderBuilding)), null, null)
: new EntityAction(null, null, null, null));
}
}
private void CommandBuildingsRange()
{
var rangeUnitsCount = Around.PopulationProvide * Around.unitsRatio[1];
foreach (var rangeBuilding in Around.MyBuildingsRanged)
{
var needBuildRanged = Around.MyUnitsRanged.Count() < rangeUnitsCount
&& Around.Me.Resource >= Around.RangedUnitCost;
actions.Add(rangeBuilding.Id,
needBuildRanged
? new EntityAction(null, new BuildAction(EntityType.RangedUnit, GetRndPositionAround(rangeBuilding)), null, null)
: new EntityAction(null, null, null, null));
}
}
private Vec2Int GetRndPositionAround(Entity building) // recursively delete
{
var buildingSize = View.EntityProperties.Single(ep => ep.Key == building.EntityType).Value.Size;
List<int[]> targetCollection;
if (buildingSize == 5)
{
targetCollection = positionsToBuildAround5;
}
else if (buildingSize == 3)
{
targetCollection = positionsToBuildAround3;
}
else
{
targetCollection = positionsToBuildAround2;
}
var rndPositionToBuild = targetCollection.ElementAt(new Random().Next(targetCollection.Count));
var positionToBuild = new Vec2Int(building.Position.X + rndPositionToBuild.ElementAt(0),
building.Position.Y + rndPositionToBuild.ElementAt(1));
// if (recursivelyTillFind)
// {
// return Around.MyUnits.Any(my => my.Position.X == positionToBuild.X &&
// my.Position.Y == positionToBuild.Y)
// ? GetRndPositionAround(building)
// : positionToBuild;
// }
return positionToBuild;
}
#endregion
#region Units
private void CommandUnitsWorkers()
{
foreach (var builderUnit in Around.MyUnitsWorkers) // All goes get spice milange because SPICE MUST FLOW
{
var nearestNotBusySpice = Around.GetNearestNotBusySpice(builderUnit.Position);
var moveAction = new MoveAction(nearestNotBusySpice.Position, true, false);
var attackAction = new AttackAction(nearestNotBusySpice.Id, null);
var action = new EntityAction(moveAction, null, attackAction, null);
actions.Add(builderUnit.Id, action);
}
if (Around.RepairNeeds)
{
foreach (var brokenBuilding in Around.MyBuildingsBroken)
{
var nearestBuilder = Around.GetNearestWorker(brokenBuilding.Position);
actions.Remove(nearestBuilder.Id);
var moveAction = new MoveAction(brokenBuilding.Position, true, false);
var repairAction = new RepairAction(brokenBuilding.Id);
actions.Add(nearestBuilder.Id, new EntityAction(moveAction, null, null, repairAction));
// var hasBigDamage = brokenBuilding.Health <= View.EntityProperties.Single(ep => ep.Key == brokenBuilding.EntityType).Value.MaxHealth / 2;
// if (hasBigDamage)
// {
// var nearestBuilder2 = Around.GetNearestWorker(brokenBuilding.Position);
// actions.Remove(nearestBuilder2.Id);
//
// moveAction = new MoveAction(brokenBuilding.Position, true, false);
// repairAction = new RepairAction(brokenBuilding.Id);
// actions.Add(nearestBuilder2.Id, new EntityAction(moveAction, null, null, repairAction));
// }
}
}
if (Around.NeedBuildBuildingWorkers)
{
SendWorkerToBuild(EntityType.BuilderBase);
}
else if (Around.NeedBuildBuildingRanged)
{
SendWorkerToBuild(EntityType.RangedBase);
}
else if (Around.NeedBuildHouse)
{
SendWorkerToBuild(EntityType.House);
}
}
private void SendWorkerToBuild(EntityType type)
{
var buildingSize = View.EntityProperties.Single(ep => ep.Key == type).Value.Size;
var workersWhoCanBuildRightHereRightNow = Around.MyUnitsWorkers.Where(w => w.GetMappingAround(buildingSize).HasPlaceToBuildAround(Around, buildingSize)).ToList();
if (workersWhoCanBuildRightHereRightNow.Any()) // once can build right here right now
{
var workerWhoCanBuild = workersWhoCanBuildRightHereRightNow.First();
var mappingAround = workerWhoCanBuild.GetMappingAround(buildingSize);
var pointToBuild = mappingAround.GetFirstPointToBuildAround(workerWhoCanBuild, Around, buildingSize);
// else // no who can build right here right now, need to find nearest place
// {
// pointToBuild = Around.FreePoints.GetFirstPointToBuildNear(Around, buildingSize);
// workerWhoCanBuild = Around.GetNearestEntityOfType(pointToBuild, PlayerType.My, EntityType.BuilderUnit);
// }
actions.Remove(workerWhoCanBuild.Id);
actions.Add(workerWhoCanBuild.Id, new EntityAction(null, new BuildAction(type, pointToBuild), null, null));
}
}
private void CommandUnitsRanged()
{
foreach (var rangedUnit in Around.MyUnitsRanged)
{
var moveAction = new MoveAction();
var attackAction = new AttackAction();
var action = new EntityAction();
if (Around.Behavior == BehaviorType.Aggressive)
{
var nearestEnemy = Around.GetNearestEntity(rangedUnit.Position, PlayerType.Enemy);
moveAction = new MoveAction(nearestEnemy.Position, true, false);
attackAction = new AttackAction(nearestEnemy.Id, null);
}
else
{
// var topBuilding = Around.MyBuildings
// .OrderByDescending(b => b.Position.X + b.Position.Y)
// .FirstOrDefault();
// moveAction = new MoveAction(GetRndPositionAround(topBuilding, false), true, false);
moveAction = new MoveAction(rangedUnit.Position, true, false);
}
action = new EntityAction(moveAction, null, attackAction, null);
actions.Add(rangedUnit.Id, action);
}
}
private void CommandUnitsMelee()
{
foreach (var meleeUnit in Around.MyUnitsMelees)
{
var moveAction = new MoveAction();
var attackAction = new AttackAction();
var action = new EntityAction();
if (Around.Behavior == BehaviorType.Aggressive)
{
var nearestEnemy = Around.GetNearestEntity(meleeUnit.Position, PlayerType.Enemy);
moveAction = new MoveAction(nearestEnemy.Position, true, false);
attackAction = new AttackAction(nearestEnemy.Id, null);
}
else
{
// var topBuilding = Around.MyBuildings
// .OrderByDescending(b => b.Position.X + b.Position.Y)
// .FirstOrDefault();
// moveAction = new MoveAction(GetRndPositionAround(topBuilding, false), true, false);
moveAction = new MoveAction(meleeUnit.Position, true, false);
}
action = new EntityAction(moveAction, null, attackAction, null);
actions.Add(meleeUnit.Id, action);
}
}
private void CommandUnitsTurrets()
{
foreach (var turretUnit in Around.MyUnitsTurrets)
{
var nearestEnemy = Around.GetNearestEntity(turretUnit.Position, PlayerType.Enemy);
var attackAction = new AttackAction(nearestEnemy.Id, null);
var action = new EntityAction(null, null, attackAction, null);
actions.Add(turretUnit.Id, action);
}
}
#endregion
public void DebugUpdate(PlayerView playerView, DebugInterface debugInterface)
{
debugInterface.Send(new DebugCommand.Clear());
debugInterface.GetState();
}
}
}