Support the kgs-game_over command.
[pachi/ann.git] / gtp.c
blob5cfb2c9e3f64b34bed97d8beabe5a301c959d78a
1 #define DEBUG
2 #include <assert.h>
3 #include <ctype.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include "board.h"
10 #include "debug.h"
11 #include "engine.h"
12 #include "gtp.h"
13 #include "mq.h"
14 #include "uct/uct.h"
15 #include "version.h"
16 #include "timeinfo.h"
18 #define NO_REPLY (-2)
20 /* Sleep 5 seconds after a game ends to give time to kill the program. */
21 #define GAME_OVER_SLEEP 5
23 void
24 gtp_prefix(char prefix, int id)
26 if (id == NO_REPLY) return;
27 if (id >= 0)
28 printf("%c%d ", prefix, id);
29 else
30 printf("%c ", prefix);
33 void
34 gtp_flush(void)
36 putchar('\n');
37 fflush(stdout);
40 void
41 gtp_output(char prefix, int id, va_list params)
43 if (id == NO_REPLY) return;
44 gtp_prefix(prefix, id);
45 char *s;
46 while ((s = va_arg(params, char *))) {
47 fputs(s, stdout);
49 putchar('\n');
50 gtp_flush();
53 void
54 gtp_reply(int id, ...)
56 va_list params;
57 va_start(params, id);
58 gtp_output('=', id, params);
59 va_end(params);
62 void
63 gtp_error(int id, ...)
65 va_list params;
66 va_start(params, id);
67 gtp_output('?', id, params);
68 va_end(params);
72 /* XXX: THIS IS TOTALLY INSECURE!!!!
73 * Even basic input checking is missing. */
75 enum parse_code
76 gtp_parse(struct board *board, struct engine *engine, struct time_info *ti, char *buf)
78 #define next_tok(to_) \
79 to_ = next; \
80 next = next + strcspn(next, " \t\r\n"); \
81 if (*next) { \
82 *next = 0; next++; \
83 next += strspn(next, " \t\r\n"); \
86 if (strchr(buf, '#'))
87 *strchr(buf, '#') = 0;
89 char *cmd, *next = buf;
90 next_tok(cmd);
92 int id = -1;
93 if (isdigit(*cmd)) {
94 id = atoi(cmd);
95 next_tok(cmd);
98 if (!*cmd)
99 return P_OK;
101 if (!strcasecmp(cmd, "protocol_version")) {
102 gtp_reply(id, "2", NULL);
103 return P_OK;
105 } else if (!strcasecmp(cmd, "name")) {
106 /* KGS hack */
107 gtp_reply(id, "Pachi ", engine->name, NULL);
108 return P_OK;
110 } else if (!strcasecmp(cmd, "version")) {
111 gtp_reply(id, PACHI_VERSION, ": ", engine->comment, NULL);
112 return P_OK;
114 /* TODO: known_command */
116 } else if (!strcasecmp(cmd, "list_commands")) {
117 /* The internal command pachi-genmoves is not exported,
118 * it should only be used between master and slaves of
119 * the distributed engine. */
120 gtp_reply(id, "protocol_version\n"
121 "name\n"
122 "version\n"
123 "list_commands\n"
124 "quit\n"
125 "boardsize\n"
126 "clear_board\n"
127 "kgs-game_over\n"
128 "komi\n"
129 "play\n"
130 "genmove\n"
131 "kgs-genmove_cleanup\n"
132 "set_free_handicap\n"
133 "place_free_handicap\n"
134 "final_status_list\n"
135 "kgs-chat\n"
136 "time_left\n"
137 "time_settings\n"
138 "kgs-time_settings", NULL);
139 return P_OK;
142 if (engine->notify) {
143 char *reply;
144 enum parse_code c = engine->notify(engine, board, id, cmd, next, &reply);
145 if (c == P_NOREPLY) {
146 id = NO_REPLY;
147 } else if (c == P_DONE_OK) {
148 gtp_reply(id, reply, NULL);
149 return P_OK;
150 } else if (c == P_DONE_ERROR) {
151 gtp_error(id, reply, NULL);
152 /* This is an internal error for the engine, but
153 * it is still OK from main's point of view. */
154 return P_OK;
155 } else if (c != P_OK) {
156 return c;
160 if (!strcasecmp(cmd, "quit")) {
161 gtp_reply(id, NULL);
162 exit(0);
164 } else if (!strcasecmp(cmd, "boardsize")) {
165 char *arg;
166 next_tok(arg);
167 board_resize(board, atoi(arg));
169 gtp_reply(id, NULL);
171 } else if (!strcasecmp(cmd, "clear_board") || !strcasecmp(cmd, "kgs-game_over")) {
172 board_clear(board);
173 if (DEBUGL(1))
174 board_print(board, stderr);
175 if (!strcasecmp(cmd, "kgs-game_over")) {
176 if (DEBUGL(0))
177 fprintf(stderr, "game is over\n");
178 /* Sleep before replying, so that kgs doesn't
179 * start another game immediately. */
180 sleep(GAME_OVER_SLEEP);
182 gtp_reply(id, NULL);
183 return P_ENGINE_RESET;
185 } else if (!strcasecmp(cmd, "komi")) {
186 char *arg;
187 next_tok(arg);
188 sscanf(arg, "%f", &board->komi);
190 if (DEBUGL(1))
191 board_print(board, stderr);
192 gtp_reply(id, NULL);
194 } else if (!strcasecmp(cmd, "play")) {
195 struct move m;
197 char *arg;
198 next_tok(arg);
199 m.color = str2stone(arg);
200 next_tok(arg);
201 coord_t *c = str2coord(arg, board_size(board));
202 m.coord = *c; coord_done(c);
203 char *reply = NULL;
205 if (DEBUGL(1))
206 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
208 // This is where kgs starts the timer, not at genmove!
209 time_start_timer(&ti[stone_other(m.color)]);
211 if (engine->notify_play)
212 reply = engine->notify_play(engine, board, &m);
213 if (board_play(board, &m) < 0) {
214 if (DEBUGL(0)) {
215 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
216 board_print(board, stderr);
218 gtp_error(id, "illegal move", NULL);
219 } else {
220 if (DEBUGL(1))
221 board_print(board, stderr);
222 gtp_reply(id, reply, NULL);
225 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
226 char *arg;
227 next_tok(arg);
228 enum stone color = str2stone(arg);
230 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
231 struct move m = { *c, color };
232 board_play(board, &m);
233 char *str = coord2str(*c, board);
234 if (DEBUGL(1))
235 fprintf(stderr, "playing move %s\n", str);
236 if (DEBUGL(1)) {
237 board_print_custom(board, stderr, engine->printhook);
239 gtp_reply(id, str, NULL);
240 free(str); coord_done(c);
242 /* Account for spent time. If our GTP peer keeps our clock, this will
243 * be overriden by next time_left GTP command properly. */
244 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
245 * should be absolutely rare situation and we will just spend a little
246 * less time than we could on next few moves.) */
247 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
248 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
250 } else if (!strcasecmp(cmd, "pachi-genmoves") || !strcasecmp(cmd, "pachi-genmoves_cleanup")) {
251 char *arg;
252 next_tok(arg);
253 enum stone color = str2stone(arg);
255 char *reply = engine->genmoves(engine, board, &ti[color], color, !strcasecmp(cmd, "pachi-genmoves_cleanup"));
256 if (DEBUGL(2))
257 fprintf(stderr, "proposing moves %s\n", reply);
258 gtp_reply(id, reply, NULL);
260 /* See "genmove" above about time management. */
261 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
262 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
264 } else if (!strcasecmp(cmd, "set_free_handicap")) {
265 struct move m;
266 m.color = S_BLACK;
268 char *arg;
269 next_tok(arg);
270 do {
271 coord_t *c = str2coord(arg, board_size(board));
272 m.coord = *c; coord_done(c);
273 if (DEBUGL(1))
274 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
276 if (board_play(board, &m) < 0) {
277 if (DEBUGL(0))
278 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
279 gtp_error(id, "illegal move", NULL);
281 board->handicap++;
282 next_tok(arg);
283 } while (*arg);
284 if (DEBUGL(1))
285 board_print(board, stderr);
286 gtp_reply(id, NULL);
288 /* TODO: Engine should choose free handicap; however, it tends to take
289 * overly long to think it all out, and unless it's clever its
290 * handicap stones won't be of much help. ;-) */
291 } else if (!strcasecmp(cmd, "place_free_handicap")
292 || !strcasecmp(cmd, "fixed_handicap")) {
293 char *arg;
294 next_tok(arg);
295 int stones = atoi(arg);
297 gtp_prefix('=', id);
298 board_handicap(board, stones, id == NO_REPLY ? NULL : stdout);
299 if (DEBUGL(1))
300 board_print(board, stderr);
301 if (id == NO_REPLY) return P_OK;
302 putchar('\n');
303 gtp_flush();
305 } else if (!strcasecmp(cmd, "final_score")) {
306 struct move_queue q = { .moves = 0 };
307 if (engine->dead_group_list)
308 engine->dead_group_list(engine, board, &q);
309 float score = board_official_score(board, &q);
310 char str[64];
311 if (DEBUGL(1))
312 fprintf(stderr, "counted score %.1f\n", score);
313 if (score == 0) {
314 gtp_reply(id, "0", NULL);
315 } else if (score > 0) {
316 snprintf(str, 64, "W+%.1f", score);
317 gtp_reply(id, str, NULL);
318 } else {
319 snprintf(str, 64, "B+%.1f", -score);
320 gtp_reply(id, str, NULL);
323 /* XXX: This is a huge hack. */
324 } else if (!strcasecmp(cmd, "final_status_list")) {
325 if (id == NO_REPLY) return P_OK;
326 char *arg;
327 next_tok(arg);
328 struct move_queue q = { .moves = 0 };
329 if (engine->dead_group_list)
330 engine->dead_group_list(engine, board, &q);
331 /* else we return empty list - i.e. engine not supporting
332 * this assumes all stones alive at the game end. */
333 if (!strcasecmp(arg, "dead")) {
334 gtp_prefix('=', id);
335 for (int i = 0; i < q.moves; i++) {
336 foreach_in_group(board, q.move[i]) {
337 printf("%s ", coord2sstr(c, board));
338 } foreach_in_group_end;
339 putchar('\n');
341 if (!q.moves)
342 putchar('\n');
343 gtp_flush();
344 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
345 gtp_prefix('=', id);
346 bool printed_group = false;
347 foreach_point(board) { // foreach_group, effectively
348 group_t g = group_at(board, c);
349 if (!g || g != c) continue;
351 for (int i = 0; i < q.moves; i++) {
352 if (q.move[i] == g)
353 goto next_group;
355 foreach_in_group(board, g) {
356 printf("%s ", coord2sstr(c, board));
357 } foreach_in_group_end;
358 putchar('\n');
359 printed_group = true;
360 next_group:;
361 } foreach_point_end;
362 if (!printed_group)
363 putchar('\n');
364 gtp_flush();
365 } else {
366 gtp_error(id, "illegal status specifier", NULL);
369 /* Custom commands for handling UCT opening book */
370 } else if (!strcasecmp(cmd, "uct_genbook")) {
371 /* Board must be initialized properly, as if for genmove;
372 * makes sense only as 'uct_genbook b'. */
373 char *arg;
374 next_tok(arg);
375 enum stone color = str2stone(arg);
376 if (uct_genbook(engine, board, &ti[color], color))
377 gtp_reply(id, NULL);
378 else
379 gtp_error(id, "error generating book", NULL);
381 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
382 char *arg;
383 next_tok(arg);
384 enum stone color = str2stone(arg);
385 uct_dumpbook(engine, board, color);
386 gtp_reply(id, NULL);
388 } else if (!strcasecmp(cmd, "kgs-chat")) {
389 char *loc;
390 next_tok(loc);
391 char *src;
392 next_tok(src);
393 char *msg;
394 next_tok(msg);
395 char *reply = NULL;
396 if (engine->chat)
397 reply = engine->chat(engine, board, msg);
398 if (reply)
399 gtp_reply(id, reply, NULL);
400 else
401 gtp_error(id, "unknown chat command", NULL);
403 } else if (!strcasecmp(cmd, "time_left")) {
404 char *arg;
405 next_tok(arg);
406 enum stone color = str2stone(arg);
407 next_tok(arg);
408 int time = atoi(arg);
409 next_tok(arg);
410 int stones = atoi(arg);
411 if (!ti[color].ignore_gtp) {
412 time_left(&ti[color], time, stones);
413 } else {
414 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
417 gtp_reply(id, NULL);
419 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
420 char *time_system;
421 char *arg;
422 if (!strcasecmp(cmd, "kgs-time_settings")) {
423 next_tok(time_system);
424 } else {
425 time_system = "canadian";
428 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
429 if (!strcasecmp(time_system, "none")) {
430 main_time = -1;
431 } else if (!strcasecmp(time_system, "absolute")) {
432 next_tok(arg);
433 main_time = atoi(arg);
434 } else if (!strcasecmp(time_system, "byoyomi")) {
435 next_tok(arg);
436 main_time = atoi(arg);
437 next_tok(arg);
438 byoyomi_time = atoi(arg);
439 next_tok(arg);
440 byoyomi_periods = atoi(arg);
441 } else if (!strcasecmp(time_system, "canadian")) {
442 next_tok(arg);
443 main_time = atoi(arg);
444 next_tok(arg);
445 byoyomi_time = atoi(arg);
446 next_tok(arg);
447 byoyomi_stones = atoi(arg);
450 if (DEBUGL(1))
451 fprintf(stderr, "time_settings %d %d/%d*%d\n",
452 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
453 if (!ti[S_BLACK].ignore_gtp) {
454 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
455 ti[S_WHITE] = ti[S_BLACK];
456 } else {
457 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
460 gtp_reply(id, NULL);
462 } else {
463 gtp_error(id, "unknown command", NULL);
464 return P_UNKNOWN_COMMAND;
466 return P_OK;
468 #undef next_tok