Support the "kgs-rules" gtp command.
[pachi.git] / gtp.c
blobb820373e753e2d558ec5d7cce515e59dbf374b64
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>
8 #include <unistd.h>
10 #include "board.h"
11 #include "debug.h"
12 #include "engine.h"
13 #include "gtp.h"
14 #include "mq.h"
15 #include "uct/uct.h"
16 #include "version.h"
17 #include "timeinfo.h"
19 #define NO_REPLY (-2)
21 /* Sleep 5 seconds after a game ends to give time to kill the program. */
22 #define GAME_OVER_SLEEP 5
24 void
25 gtp_prefix(char prefix, int id)
27 if (id == NO_REPLY) return;
28 if (id >= 0)
29 printf("%c%d ", prefix, id);
30 else
31 printf("%c ", prefix);
34 void
35 gtp_flush(void)
37 putchar('\n');
38 fflush(stdout);
41 void
42 gtp_output(char prefix, int id, va_list params)
44 if (id == NO_REPLY) return;
45 gtp_prefix(prefix, id);
46 char *s;
47 while ((s = va_arg(params, char *))) {
48 fputs(s, stdout);
50 putchar('\n');
51 gtp_flush();
54 void
55 gtp_reply(int id, ...)
57 va_list params;
58 va_start(params, id);
59 gtp_output('=', id, params);
60 va_end(params);
63 void
64 gtp_error(int id, ...)
66 va_list params;
67 va_start(params, id);
68 gtp_output('?', id, params);
69 va_end(params);
73 /* XXX: THIS IS TOTALLY INSECURE!!!!
74 * Even basic input checking is missing. */
76 enum parse_code
77 gtp_parse(struct board *board, struct engine *engine, struct time_info *ti, char *buf)
79 #define next_tok(to_) \
80 to_ = next; \
81 next = next + strcspn(next, " \t\r\n"); \
82 if (*next) { \
83 *next = 0; next++; \
84 next += strspn(next, " \t\r\n"); \
87 if (strchr(buf, '#'))
88 *strchr(buf, '#') = 0;
90 char *cmd, *next = buf;
91 next_tok(cmd);
93 int id = -1;
94 if (isdigit(*cmd)) {
95 id = atoi(cmd);
96 next_tok(cmd);
99 if (!*cmd)
100 return P_OK;
102 if (!strcasecmp(cmd, "protocol_version")) {
103 gtp_reply(id, "2", NULL);
104 return P_OK;
106 } else if (!strcasecmp(cmd, "name")) {
107 /* KGS hack */
108 gtp_reply(id, "Pachi ", engine->name, NULL);
109 return P_OK;
111 } else if (!strcasecmp(cmd, "version")) {
112 gtp_reply(id, PACHI_VERSION, ": ", engine->comment, NULL);
113 return P_OK;
115 /* TODO: known_command */
117 } else if (!strcasecmp(cmd, "list_commands")) {
118 /* The internal command pachi-genmoves is not exported,
119 * it should only be used between master and slaves of
120 * the distributed engine. */
121 gtp_reply(id, "protocol_version\n"
122 "name\n"
123 "version\n"
124 "list_commands\n"
125 "quit\n"
126 "boardsize\n"
127 "clear_board\n"
128 "kgs-game_over\n"
129 "komi\n"
130 "kgs-rules\n"
131 "play\n"
132 "genmove\n"
133 "kgs-genmove_cleanup\n"
134 "set_free_handicap\n"
135 "place_free_handicap\n"
136 "final_status_list\n"
137 "kgs-chat\n"
138 "time_left\n"
139 "time_settings\n"
140 "kgs-time_settings", NULL);
141 return P_OK;
144 if (engine->notify) {
145 char *reply;
146 enum parse_code c = engine->notify(engine, board, id, cmd, next, &reply);
147 if (c == P_NOREPLY) {
148 id = NO_REPLY;
149 } else if (c == P_DONE_OK) {
150 gtp_reply(id, reply, NULL);
151 return P_OK;
152 } else if (c == P_DONE_ERROR) {
153 gtp_error(id, reply, NULL);
154 /* This is an internal error for the engine, but
155 * it is still OK from main's point of view. */
156 return P_OK;
157 } else if (c != P_OK) {
158 return c;
162 if (!strcasecmp(cmd, "quit")) {
163 gtp_reply(id, NULL);
164 exit(0);
166 } else if (!strcasecmp(cmd, "boardsize")) {
167 char *arg;
168 next_tok(arg);
169 board_resize(board, atoi(arg));
171 gtp_reply(id, NULL);
173 } else if (!strcasecmp(cmd, "clear_board") || !strcasecmp(cmd, "kgs-game_over")) {
174 board_clear(board);
175 if (DEBUGL(1))
176 board_print(board, stderr);
177 if (!strcasecmp(cmd, "kgs-game_over")) {
178 if (DEBUGL(0))
179 fprintf(stderr, "game is over\n");
180 /* Sleep before replying, so that kgs doesn't
181 * start another game immediately. */
182 sleep(GAME_OVER_SLEEP);
184 gtp_reply(id, NULL);
185 return P_ENGINE_RESET;
187 } else if (!strcasecmp(cmd, "komi")) {
188 char *arg;
189 next_tok(arg);
190 sscanf(arg, "%f", &board->komi);
192 if (DEBUGL(1))
193 board_print(board, stderr);
194 gtp_reply(id, NULL);
196 } else if (!strcasecmp(cmd, "kgs-rules")) {
197 char *arg;
198 next_tok(arg);
199 if (!strcasecmp(arg, "japanese")) {
200 board->rules = RULES_JAPANESE;
201 } else if (!strcasecmp(arg, "chinese")) {
202 board->rules = RULES_CHINESE;
203 } else if (!strcasecmp(arg, "aga")) {
204 board->rules = RULES_AGA;
205 } else if (!strcasecmp(arg, "new_zealand")) {
206 board->rules = RULES_NEW_ZEALAND;
207 } else {
208 gtp_error(id, "unknown rules", NULL);
209 return P_OK;
211 gtp_reply(id, NULL);
213 } else if (!strcasecmp(cmd, "play")) {
214 struct move m;
216 char *arg;
217 next_tok(arg);
218 m.color = str2stone(arg);
219 next_tok(arg);
220 coord_t *c = str2coord(arg, board_size(board));
221 m.coord = *c; coord_done(c);
222 char *reply = NULL;
224 if (DEBUGL(1))
225 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
227 // This is where kgs starts the timer, not at genmove!
228 time_start_timer(&ti[stone_other(m.color)]);
230 if (engine->notify_play)
231 reply = engine->notify_play(engine, board, &m);
232 if (board_play(board, &m) < 0) {
233 if (DEBUGL(0)) {
234 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
235 board_print(board, stderr);
237 gtp_error(id, "illegal move", NULL);
238 } else {
239 if (DEBUGL(1))
240 board_print(board, stderr);
241 gtp_reply(id, reply, NULL);
244 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
245 char *arg;
246 next_tok(arg);
247 enum stone color = str2stone(arg);
249 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
250 struct move m = { *c, color };
251 board_play(board, &m);
252 char *str = coord2str(*c, board);
253 if (DEBUGL(1))
254 fprintf(stderr, "playing move %s\n", str);
255 if (DEBUGL(1)) {
256 board_print_custom(board, stderr, engine->printhook);
258 gtp_reply(id, str, NULL);
259 free(str); coord_done(c);
261 /* Account for spent time. If our GTP peer keeps our clock, this will
262 * be overriden by next time_left GTP command properly. */
263 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
264 * should be absolutely rare situation and we will just spend a little
265 * less time than we could on next few moves.) */
266 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
267 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
269 } else if (!strcasecmp(cmd, "pachi-genmoves") || !strcasecmp(cmd, "pachi-genmoves_cleanup")) {
270 char *arg;
271 next_tok(arg);
272 enum stone color = str2stone(arg);
273 struct time_info *myti = &ti[color];
274 /* Get correct time from master. Keep this code in sync
275 * with distributed_genmove(). */
276 if (myti->dim == TD_WALLTIME &&
277 sscanf(next, "%lf %lf %d %d", &myti->len.t.main_time,
278 &myti->len.t.byoyomi_time, &myti->len.t.byoyomi_periods,
279 &myti->len.t.byoyomi_stones) != 4) {
280 gtp_error(id, "incorrect time info", NULL);
281 return P_OK;
284 char *reply = engine->genmoves(engine, board, myti, color, !strcasecmp(cmd, "pachi-genmoves_cleanup"));
285 if (DEBUGL(2))
286 fprintf(stderr, "proposing moves %s\n", reply);
287 if (DEBUGL(1)) {
288 board_print_custom(board, stderr, engine->printhook);
290 gtp_reply(id, reply, NULL);
292 } else if (!strcasecmp(cmd, "set_free_handicap")) {
293 struct move m;
294 m.color = S_BLACK;
296 char *arg;
297 next_tok(arg);
298 do {
299 coord_t *c = str2coord(arg, board_size(board));
300 m.coord = *c; coord_done(c);
301 if (DEBUGL(1))
302 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
304 if (board_play(board, &m) < 0) {
305 if (DEBUGL(0))
306 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
307 gtp_error(id, "illegal move", NULL);
309 board->handicap++;
310 next_tok(arg);
311 } while (*arg);
312 if (DEBUGL(1))
313 board_print(board, stderr);
314 gtp_reply(id, NULL);
316 /* TODO: Engine should choose free handicap; however, it tends to take
317 * overly long to think it all out, and unless it's clever its
318 * handicap stones won't be of much help. ;-) */
319 } else if (!strcasecmp(cmd, "place_free_handicap")
320 || !strcasecmp(cmd, "fixed_handicap")) {
321 char *arg;
322 next_tok(arg);
323 int stones = atoi(arg);
325 gtp_prefix('=', id);
326 board_handicap(board, stones, id == NO_REPLY ? NULL : stdout);
327 if (DEBUGL(1))
328 board_print(board, stderr);
329 if (id == NO_REPLY) return P_OK;
330 putchar('\n');
331 gtp_flush();
333 } else if (!strcasecmp(cmd, "final_score")) {
334 struct move_queue q = { .moves = 0 };
335 if (engine->dead_group_list)
336 engine->dead_group_list(engine, board, &q);
337 float score = board_official_score(board, &q);
338 char str[64];
339 if (DEBUGL(1))
340 fprintf(stderr, "counted score %.1f\n", score);
341 if (score == 0) {
342 gtp_reply(id, "0", NULL);
343 } else if (score > 0) {
344 snprintf(str, 64, "W+%.1f", score);
345 gtp_reply(id, str, NULL);
346 } else {
347 snprintf(str, 64, "B+%.1f", -score);
348 gtp_reply(id, str, NULL);
351 /* XXX: This is a huge hack. */
352 } else if (!strcasecmp(cmd, "final_status_list")) {
353 if (id == NO_REPLY) return P_OK;
354 char *arg;
355 next_tok(arg);
356 struct move_queue q = { .moves = 0 };
357 if (engine->dead_group_list)
358 engine->dead_group_list(engine, board, &q);
359 /* else we return empty list - i.e. engine not supporting
360 * this assumes all stones alive at the game end. */
361 if (!strcasecmp(arg, "dead")) {
362 gtp_prefix('=', id);
363 for (int i = 0; i < q.moves; i++) {
364 foreach_in_group(board, q.move[i]) {
365 printf("%s ", coord2sstr(c, board));
366 } foreach_in_group_end;
367 putchar('\n');
369 if (!q.moves)
370 putchar('\n');
371 gtp_flush();
372 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
373 gtp_prefix('=', id);
374 bool printed_group = false;
375 foreach_point(board) { // foreach_group, effectively
376 group_t g = group_at(board, c);
377 if (!g || g != c) continue;
379 for (int i = 0; i < q.moves; i++) {
380 if (q.move[i] == g)
381 goto next_group;
383 foreach_in_group(board, g) {
384 printf("%s ", coord2sstr(c, board));
385 } foreach_in_group_end;
386 putchar('\n');
387 printed_group = true;
388 next_group:;
389 } foreach_point_end;
390 if (!printed_group)
391 putchar('\n');
392 gtp_flush();
393 } else {
394 gtp_error(id, "illegal status specifier", NULL);
397 /* Custom commands for handling UCT opening book */
398 } else if (!strcasecmp(cmd, "uct_genbook")) {
399 /* Board must be initialized properly, as if for genmove;
400 * makes sense only as 'uct_genbook b'. */
401 char *arg;
402 next_tok(arg);
403 enum stone color = str2stone(arg);
404 if (uct_genbook(engine, board, &ti[color], color))
405 gtp_reply(id, NULL);
406 else
407 gtp_error(id, "error generating book", NULL);
409 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
410 char *arg;
411 next_tok(arg);
412 enum stone color = str2stone(arg);
413 uct_dumpbook(engine, board, color);
414 gtp_reply(id, NULL);
416 } else if (!strcasecmp(cmd, "kgs-chat")) {
417 char *loc;
418 next_tok(loc);
419 char *src;
420 next_tok(src);
421 char *msg;
422 next_tok(msg);
423 char *reply = NULL;
424 if (engine->chat)
425 reply = engine->chat(engine, board, msg);
426 if (reply)
427 gtp_reply(id, reply, NULL);
428 else
429 gtp_error(id, "unknown chat command", NULL);
431 } else if (!strcasecmp(cmd, "time_left")) {
432 char *arg;
433 next_tok(arg);
434 enum stone color = str2stone(arg);
435 next_tok(arg);
436 int time = atoi(arg);
437 next_tok(arg);
438 int stones = atoi(arg);
439 if (!ti[color].ignore_gtp) {
440 time_left(&ti[color], time, stones);
441 } else {
442 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
445 gtp_reply(id, NULL);
447 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
448 char *time_system;
449 char *arg;
450 if (!strcasecmp(cmd, "kgs-time_settings")) {
451 next_tok(time_system);
452 } else {
453 time_system = "canadian";
456 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
457 if (!strcasecmp(time_system, "none")) {
458 main_time = -1;
459 } else if (!strcasecmp(time_system, "absolute")) {
460 next_tok(arg);
461 main_time = atoi(arg);
462 } else if (!strcasecmp(time_system, "byoyomi")) {
463 next_tok(arg);
464 main_time = atoi(arg);
465 next_tok(arg);
466 byoyomi_time = atoi(arg);
467 next_tok(arg);
468 byoyomi_periods = atoi(arg);
469 } else if (!strcasecmp(time_system, "canadian")) {
470 next_tok(arg);
471 main_time = atoi(arg);
472 next_tok(arg);
473 byoyomi_time = atoi(arg);
474 next_tok(arg);
475 byoyomi_stones = atoi(arg);
478 if (DEBUGL(1))
479 fprintf(stderr, "time_settings %d %d/%d*%d\n",
480 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
481 if (!ti[S_BLACK].ignore_gtp) {
482 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
483 ti[S_WHITE] = ti[S_BLACK];
484 } else {
485 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
488 gtp_reply(id, NULL);
490 } else {
491 gtp_error(id, "unknown command", NULL);
492 return P_UNKNOWN_COMMAND;
494 return P_OK;
496 #undef next_tok