Repository navigation
Expand file tree
/
Copy pathStarterBot.cs
More file actions
340 lines (290 loc) · 12.1 KB
/
Copy pathStarterBot.cs
File metadata and controls
340 lines (290 loc) · 12.1 KB
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
#:package CommandLineParser@2.9.1
#:property Nullable=disable
#:property PublishAot=false
using System.Net.Http.Json;
using CommandLine;
// A starter bot for Badger Brawl (NetRTS).
//
// Every tick, the server sends us everything we can see (the "state"), we decide what to do,
// and we send back a list of commands. Change the Decide method to make your bot smarter!
//
// Run it with: dotnet run StarterBot.cs --player-name Frank (play a house bot)
// or: dotnet run StarterBot.cs --player-name Frank --create-match our-game (start a match for a classmate)
// or: dotnet run StarterBot.cs --player-name Ann --join-match our-game (join it, by name or by match id)
// or: dotnet run StarterBot.cs --player-name Ann --join-match season-week3 (league matches too: claim your
// bot with Discord on the website first; play starts when the admin starts the heats)
// Add --tick-ms 250 when starting a match to make it run faster (milliseconds per tick; 1000 = one tick a second).
// Every option and its default value is in the Options class at the bottom of this file.
var options = Parser.Default.ParseArguments<Options>(args).Value;
if (options == null)
{
return;
}
AskForNameIfNeeded(options);
var http = new HttpClient { BaseAddress = new Uri(options.Server), Timeout = TimeSpan.FromMinutes(1) };
if (!await SignUp(options.PlayerName))
{
return;
}
var match = await GetIntoMatch(options);
if (match == null)
{
return;
}
var state = await WaitForStart(match);
await Play(match, state);
await PrintResult(match);
// ---------------------------------------------------------------------------------------------
// Step 5: play. Asking for state?waitForTick=N waits until the server has played tick N.
async Task Play(Match match, State state)
{
var matchUrl = "/api/v1/matches/" + match.MatchId;
while (state.Status != "Completed")
{
var commands = Decide(state);
if (commands.Count > 0)
{
await http.PostAsJsonAsync(matchUrl + "/commands", new { commands });
}
Console.WriteLine($"Tick {state.Tick}: {state.You.Resources} ore, {MyUnits(state).Count} units");
state = await http.GetFromJsonAsync<State>(matchUrl + "/state?waitForTick=" + (state.Tick + 1));
}
}
// ---------------------------------------------------------------------------------------------
// Your strategy goes here. Look at the state, return a list of commands.
// Every command, unit and building is described in the bot guide:
// https://github.com/SnowSE/NetRTS/blob/main/docs/bot-guide.md
List<object> Decide(State s)
{
var commands = new List<object>();
var ore = s.You.Resources; // what we can still spend this tick
if (MyBuilding(s, "CommandCenter") == null)
{
return commands; // we lost our headquarters :(
}
var gather = GatherOre(s);
if (gather != null)
{
commands.Add(gather);
}
var worker = TrainWorker(s, ore);
if (worker != null)
{
commands.Add(worker);
ore -= 50;
}
var barracks = BuildBarracks(s, ore);
if (barracks != null)
{
commands.Add(barracks);
ore -= 150;
}
var soldier = TrainSoldier(s, ore);
if (soldier != null)
{
commands.Add(soldier);
ore -= 100;
}
var attack = Attack(s);
if (attack != null)
{
commands.Add(attack);
}
return commands;
}
// 1. Idle workers dig ore at the deposit closest to our headquarters.
object GatherOre(State s)
{
var hq = MyBuilding(s, "CommandCenter");
var closest = s.Resources.OrderBy(r => Math.Abs(r.X - hq.X) + Math.Abs(r.Y - hq.Y)).FirstOrDefault();
if (closest == null)
{
return null;
}
return new { type = "Gather", units = "idle", unitType = "Worker", targetId = closest.Id };
}
// 2. Train workers until we have 8 (a Worker costs 50 ore).
object TrainWorker(State s, int ore)
{
var hq = MyBuilding(s, "CommandCenter");
if (MyUnits(s, "Worker").Count >= 8 || hq.Production.Count > 0 || ore < 50)
{
return null;
}
return new { type = "Produce", buildingId = hq.Id, unitType = "Worker" };
}
// 3. Then build one Barracks (150 ore), 4 tiles from the headquarters toward the middle of the map.
object BuildBarracks(State s, int ore)
{
var workers = MyUnits(s, "Worker");
if (workers.Count < 8 || MyBuilding(s, "Barracks") != null || ore < 150)
{
return null;
}
var hq = MyBuilding(s, "CommandCenter");
var x = hq.X < s.MapWidth / 2 ? hq.X + 4 : hq.X - 4;
var y = hq.Y < s.MapHeight / 2 ? hq.Y + 4 : hq.Y - 4;
return new { type = "Build", unitIds = new[] { workers[0].Id }, buildingType = "Barracks", x, y };
}
// 4. The Barracks trains soldiers non-stop (a Soldier costs 100 ore).
object TrainSoldier(State s, int ore)
{
var barracks = MyBuilding(s, "Barracks");
if (barracks == null || !barracks.Completed || barracks.Production.Count > 0 || ore < 100)
{
return null;
}
return new { type = "Produce", buildingId = barracks.Id, unitType = "Soldier" };
}
// 5. Once we have 5 soldiers, send every idle soldier to attack the enemy's starting corner.
object Attack(State s)
{
var soldiers = MyUnits(s, "Soldier");
if (soldiers.Count < 5)
{
return null;
}
var enemy = s.Players
.Where(p => p.Slot != s.You.Slot && !p.Eliminated)
.MinBy(p => Math.Max(Math.Abs(p.StartPosition.X - soldiers[0].X), Math.Abs(p.StartPosition.Y - soldiers[0].Y)));
if (enemy == null)
{
return null;
}
return new { type = "Attack", units = "idle", unitType = "Soldier", x = enemy.StartPosition.X, y = enemy.StartPosition.Y };
}
// Helpers: our own units (of one type, or all of them) and our first building of a type.
List<Unit> MyUnits(State s, string type = null) =>
s.Units.Where(u => u.Owner == s.You.Slot && (type == null || u.Type == type)).ToList();
Building MyBuilding(State s, string type) =>
s.Buildings.FirstOrDefault(b => b.Owner == s.You.Slot && b.Type == type);
// ---------------------------------------------------------------------------------------------
// Setting up: the name, signing up, getting into a match and seeing who won.
// If nobody picked a name (on the command line or in the Options class), ask for one.
void AskForNameIfNeeded(Options options)
{
if (options.PlayerName == "change-me")
{
Console.WriteLine("Tip: add --player-name YourName (or change the player-name default in StarterBot.cs) and you won't be asked again.");
Console.Write("What is your bot's name? ");
options.PlayerName = Console.ReadLine().Trim();
}
}
// Step 2: sign up once. The server gives us a secret API key, which we save in a file and reuse.
async Task<bool> SignUp(string playerName)
{
var keyFile = "apikey-" + playerName + ".txt";
if (!File.Exists(keyFile))
{
var response = await http.PostAsJsonAsync("/api/v1/players", new { name = playerName });
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("Could not register: " + await response.Content.ReadAsStringAsync());
return false;
}
var player = await response.Content.ReadFromJsonAsync<Player>();
File.WriteAllText(keyFile, player.ApiKey);
}
http.DefaultRequestHeaders.Add("Authorization", "Bearer " + File.ReadAllText(keyFile).Trim());
return true;
}
// Step 3: get into a match: create a named one, join one, or play a house bot.
async Task<Match> GetIntoMatch(Options options)
{
HttpResponseMessage response;
if (options.CreateMatch != null)
{
response = await http.PostAsJsonAsync("/api/v1/matches",
new { name = options.CreateMatch, settings = new { tickIntervalMs = options.TickMs } });
}
else if (options.JoinMatch != null)
{
var matchId = await FindMatchId(options.JoinMatch);
if (matchId == null)
{
Console.WriteLine($"No match named '{options.JoinMatch}' is waiting for players.");
return null;
}
response = await http.PostAsync("/api/v1/matches/" + matchId + "/join", null);
}
else
{
response = await http.PostAsJsonAsync("/api/v1/matches",
new { houseBots = new[] { options.Opponent }, settings = new { tickIntervalMs = options.TickMs } });
}
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("Could not get into a match: " + await response.Content.ReadAsStringAsync());
return null;
}
var match = await response.Content.ReadFromJsonAsync<Match>();
Console.WriteLine($"In match {match.Name ?? match.MatchId}. Watch it at {options.Server}/#/match/{match.Name ?? match.MatchId}");
return match;
}
// Match ids look like 1c042d43-d650-4381-a5b1-c41933e82069. Anything else is a name, so look it up.
async Task<string> FindMatchId(string idOrName)
{
if (Guid.TryParse(idOrName, out _))
{
return idOrName;
}
var waiting = await http.GetFromJsonAsync<List<Match>>("/api/v1/matches?status=Waiting");
var named = waiting.FirstOrDefault(m => string.Equals(m.Name, idOrName, StringComparison.OrdinalIgnoreCase));
return named?.MatchId;
}
// Step 4: wait for every seat to fill. The server holds each request for up to 30 seconds, then
// answers with an error if the match still hasn't started, so we just ask again.
async Task<State> WaitForStart(Match match)
{
while (true)
{
var reply = await http.GetAsync("/api/v1/matches/" + match.MatchId + "/state?waitForTick=0");
if (reply.IsSuccessStatusCode)
{
return await reply.Content.ReadFromJsonAsync<State>();
}
Console.WriteLine("Waiting for the match to start...");
}
}
// Step 6: who won?
async Task PrintResult(Match match)
{
var result = await http.GetFromJsonAsync<Result>("/api/v1/matches/" + match.MatchId + "/result");
foreach (var p in result.Players)
{
Console.WriteLine($"{p.Name}: {p.Score.Total} points" + (p.Winner ? " <-- WINNER" : ""));
}
}
// ---------------------------------------------------------------------------------------------
// The command-line options. Each [Option] names the switch to type, like --player-name Frank,
// and its Default is what you get when you leave it out. Change a Default to change the setting.
class Options
{
[Option("player-name", Default = "change-me", HelpText = "Your bot's name: 3-32 letters, digits, - or _. It must not be taken already.")]
public string PlayerName { get; set; }
[Option("opponent", Default = "sitter", HelpText = "The house bot to play: sitter (easiest), economist, balanced or rusher (hardest).")]
public string Opponent { get; set; }
[Option("tick-ms", Default = 1000, HelpText = "Milliseconds per tick for a match you start (100-10000; 1000 = one tick a second).")]
public int TickMs { get; set; }
[Option("server", Default = "https://NetRTS.snowse.io", HelpText = "The Badger Brawl server to play on.")]
public string Server { get; set; }
[Option("create-match", SetName = "create", HelpText = "Start a match with this name for a classmate to join.")]
public string CreateMatch { get; set; }
[Option("join-match", SetName = "join", HelpText = "Join a waiting match, by its name or its id.")]
public string JoinMatch { get; set; }
}
// The shapes of the JSON the server sends us. We only list the fields this bot uses;
// the bot guide lists them all, so add more here when you need them.
record Player(string ApiKey);
record Match(string MatchId, string Name);
record State(int Tick, string Status, int MapWidth, int MapHeight, Me You,
List<Unit> Units, List<Building> Buildings, List<Ore> Resources, List<PlayerInfo> Players);
record Me(int Slot, int Resources);
record Unit(int Id, int Owner, string Type, int X, int Y, string Activity);
record Building(int Id, int Owner, string Type, int X, int Y, bool Completed, List<object> Production);
record Ore(int Id, int X, int Y);
record PlayerInfo(int Slot, string Name, bool Eliminated, Spot StartPosition);
record Spot(int X, int Y);
record Result(List<ResultPlayer> Players);
record ResultPlayer(string Name, bool Winner, Score Score);
record Score(int Total);