zzgo: always reset the default time for a new game.
[pachi/derm.git] / gtp.c
blob643177c78a54e4811e227044e77d48a555dd732c
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 "play\n"
131 "genmove\n"
132 "kgs-genmove_cleanup\n"
133 "set_free_handicap\n"
134 "place_free_handicap\n"
135 "final_status_list\n"
136 "kgs-chat\n"
137 "time_left\n"
138 "time_settings\n"
139 "kgs-time_settings", NULL);
140 return P_OK;
143 if (engine->notify) {
144 char *reply;
145 enum parse_code c = engine->notify(engine, board, id, cmd, next, &reply);
146 if (c == P_NOREPLY) {
147 id = NO_REPLY;
148 } else if (c == P_DONE_OK) {
149 gtp_reply(id, reply, NULL);
150 return P_OK;
151 } else if (c == P_DONE_ERROR) {
152 gtp_error(id, reply, NULL);
153 /* This is an internal error for the engine, but
154 * it is still OK from main's point of view. */
155 return P_OK;
156 } else if (c != P_OK) {
157 return c;
161 if (!strcasecmp(cmd, "quit")) {
162 gtp_reply(id, NULL);
163 exit(0);
165 } else if (!strcasecmp(cmd, "boardsize")) {
166 char *arg;
167 next_tok(arg);
168 board_resize(board, atoi(arg));
170 gtp_reply(id, NULL);
172 } else if (!strcasecmp(cmd, "clear_board") || !strcasecmp(cmd, "kgs-game_over")) {
173 board_clear(board);
174 if (DEBUGL(1))
175 board_print(board, stderr);
176 if (!strcasecmp(cmd, "kgs-game_over")) {
177 if (DEBUGL(0))
178 fprintf(stderr, "game is over\n");
179 /* Sleep before replying, so that kgs doesn't
180 * start another game immediately. */
181 sleep(GAME_OVER_SLEEP);
183 gtp_reply(id, NULL);
184 return P_ENGINE_RESET;
186 } else if (!strcasecmp(cmd, "komi")) {
187 char *arg;
188 next_tok(arg);
189 sscanf(arg, "%f", &board->komi);
191 if (DEBUGL(1))
192 board_print(board, stderr);
193 gtp_reply(id, NULL);
195 } else if (!strcasecmp(cmd, "play")) {
196 struct move m;
198 char *arg;
199 next_tok(arg);
200 m.color = str2stone(arg);
201 next_tok(arg);
202 coord_t *c = str2coord(arg, board_size(board));
203 m.coord = *c; coord_done(c);
204 char *reply = NULL;
206 if (DEBUGL(1))
207 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
209 // This is where kgs starts the timer, not at genmove!
210 time_start_timer(&ti[stone_other(m.color)]);
212 if (engine->notify_play)
213 reply = engine->notify_play(engine, board, &m);
214 if (board_play(board, &m) < 0) {
215 if (DEBUGL(0)) {
216 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
217 board_print(board, stderr);
219 gtp_error(id, "illegal move", NULL);
220 } else {
221 if (DEBUGL(1))
222 board_print(board, stderr);
223 gtp_reply(id, reply, NULL);
226 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
227 char *arg;
228 next_tok(arg);
229 enum stone color = str2stone(arg);
231 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
232 struct move m = { *c, color };
233 board_play(board, &m);
234 char *str = coord2str(*c, board);
235 if (DEBUGL(1))
236 fprintf(stderr, "playing move %s\n", str);
237 if (DEBUGL(1)) {
238 board_print_custom(board, stderr, engine->printhook);
240 gtp_reply(id, str, NULL);
241 free(str); coord_done(c);
243 /* Account for spent time. If our GTP peer keeps our clock, this will
244 * be overriden by next time_left GTP command properly. */
245 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
246 * should be absolutely rare situation and we will just spend a little
247 * less time than we could on next few moves.) */
248 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
249 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
251 } else if (!strcasecmp(cmd, "pachi-genmoves") || !strcasecmp(cmd, "pachi-genmoves_cleanup")) {
252 char *arg;
253 next_tok(arg);
254 enum stone color = str2stone(arg);
255 struct time_info *myti = &ti[color];
256 /* Get correct time from master. Keep this code in sync
257 * with distributed_genmove(). */
258 if (myti->dim == TD_WALLTIME &&
259 sscanf(next, "%lf %lf %d %d", &myti->len.t.main_time,
260 &myti->len.t.byoyomi_time, &myti->len.t.byoyomi_periods,
261 &myti->len.t.byoyomi_stones) != 4) {
262 gtp_error(id, "incorrect time info", NULL);
263 return P_OK;
266 char *reply = engine->genmoves(engine, board, myti, color, !strcasecmp(cmd, "pachi-genmoves_cleanup"));
267 if (DEBUGL(2))
268 fprintf(stderr, "proposing moves %s\n", reply);
269 if (DEBUGL(1)) {
270 board_print_custom(board, stderr, engine->printhook);
272 gtp_reply(id, reply, NULL);
274 } else if (!strcasecmp(cmd, "set_free_handicap")) {
275 struct move m;
276 m.color = S_BLACK;
278 char *arg;
279 next_tok(arg);
280 do {
281 coord_t *c = str2coord(arg, board_size(board));
282 m.coord = *c; coord_done(c);
283 if (DEBUGL(1))
284 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
286 if (board_play(board, &m) < 0) {
287 if (DEBUGL(0))
288 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
289 gtp_error(id, "illegal move", NULL);
291 board->handicap++;
292 next_tok(arg);
293 } while (*arg);
294 if (DEBUGL(1))
295 board_print(board, stderr);
296 gtp_reply(id, NULL);
298 /* TODO: Engine should choose free handicap; however, it tends to take
299 * overly long to think it all out, and unless it's clever its
300 * handicap stones won't be of much help. ;-) */
301 } else if (!strcasecmp(cmd, "place_free_handicap")
302 || !strcasecmp(cmd, "fixed_handicap")) {
303 char *arg;
304 next_tok(arg);
305 int stones = atoi(arg);
307 gtp_prefix('=', id);
308 board_handicap(board, stones, id == NO_REPLY ? NULL : stdout);
309 if (DEBUGL(1))
310 board_print(board, stderr);
311 if (id == NO_REPLY) return P_OK;
312 putchar('\n');
313 gtp_flush();
315 } else if (!strcasecmp(cmd, "final_score")) {
316 struct move_queue q = { .moves = 0 };
317 if (engine->dead_group_list)
318 engine->dead_group_list(engine, board, &q);
319 float score = board_official_score(board, &q);
320 char str[64];
321 if (DEBUGL(1))
322 fprintf(stderr, "counted score %.1f\n", score);
323 if (score == 0) {
324 gtp_reply(id, "0", NULL);
325 } else if (score > 0) {
326 snprintf(str, 64, "W+%.1f", score);
327 gtp_reply(id, str, NULL);
328 } else {
329 snprintf(str, 64, "B+%.1f", -score);
330 gtp_reply(id, str, NULL);
333 /* XXX: This is a huge hack. */
334 } else if (!strcasecmp(cmd, "final_status_list")) {
335 if (id == NO_REPLY) return P_OK;
336 char *arg;
337 next_tok(arg);
338 struct move_queue q = { .moves = 0 };
339 if (engine->dead_group_list)
340 engine->dead_group_list(engine, board, &q);
341 /* else we return empty list - i.e. engine not supporting
342 * this assumes all stones alive at the game end. */
343 if (!strcasecmp(arg, "dead")) {
344 gtp_prefix('=', id);
345 for (int i = 0; i < q.moves; i++) {
346 foreach_in_group(board, q.move[i]) {
347 printf("%s ", coord2sstr(c, board));
348 } foreach_in_group_end;
349 putchar('\n');
351 if (!q.moves)
352 putchar('\n');
353 gtp_flush();
354 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
355 gtp_prefix('=', id);
356 bool printed_group = false;
357 foreach_point(board) { // foreach_group, effectively
358 group_t g = group_at(board, c);
359 if (!g || g != c) continue;
361 for (int i = 0; i < q.moves; i++) {
362 if (q.move[i] == g)
363 goto next_group;
365 foreach_in_group(board, g) {
366 printf("%s ", coord2sstr(c, board));
367 } foreach_in_group_end;
368 putchar('\n');
369 printed_group = true;
370 next_group:;
371 } foreach_point_end;
372 if (!printed_group)
373 putchar('\n');
374 gtp_flush();
375 } else {
376 gtp_error(id, "illegal status specifier", NULL);
379 /* Custom commands for handling UCT opening book */
380 } else if (!strcasecmp(cmd, "uct_genbook")) {
381 /* Board must be initialized properly, as if for genmove;
382 * makes sense only as 'uct_genbook b'. */
383 char *arg;
384 next_tok(arg);
385 enum stone color = str2stone(arg);
386 if (uct_genbook(engine, board, &ti[color], color))
387 gtp_reply(id, NULL);
388 else
389 gtp_error(id, "error generating book", NULL);
391 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
392 char *arg;
393 next_tok(arg);
394 enum stone color = str2stone(arg);
395 uct_dumpbook(engine, board, color);
396 gtp_reply(id, NULL);
398 } else if (!strcasecmp(cmd, "kgs-chat")) {
399 char *loc;
400 next_tok(loc);
401 char *src;
402 next_tok(src);
403 char *msg;
404 next_tok(msg);
405 char *reply = NULL;
406 if (engine->chat)
407 reply = engine->chat(engine, board, msg);
408 if (reply)
409 gtp_reply(id, reply, NULL);
410 else
411 gtp_error(id, "unknown chat command", NULL);
413 } else if (!strcasecmp(cmd, "time_left")) {
414 char *arg;
415 next_tok(arg);
416 enum stone color = str2stone(arg);
417 next_tok(arg);
418 int time = atoi(arg);
419 next_tok(arg);
420 int stones = atoi(arg);
421 if (!ti[color].ignore_gtp) {
422 time_left(&ti[color], time, stones);
423 } else {
424 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
427 gtp_reply(id, NULL);
429 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
430 char *time_system;
431 char *arg;
432 if (!strcasecmp(cmd, "kgs-time_settings")) {
433 next_tok(time_system);
434 } else {
435 time_system = "canadian";
438 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
439 if (!strcasecmp(time_system, "none")) {
440 main_time = -1;
441 } else if (!strcasecmp(time_system, "absolute")) {
442 next_tok(arg);
443 main_time = atoi(arg);
444 } else if (!strcasecmp(time_system, "byoyomi")) {
445 next_tok(arg);
446 main_time = atoi(arg);
447 next_tok(arg);
448 byoyomi_time = atoi(arg);
449 next_tok(arg);
450 byoyomi_periods = atoi(arg);
451 } else if (!strcasecmp(time_system, "canadian")) {
452 next_tok(arg);
453 main_time = atoi(arg);
454 next_tok(arg);
455 byoyomi_time = atoi(arg);
456 next_tok(arg);
457 byoyomi_stones = atoi(arg);
460 if (DEBUGL(1))
461 fprintf(stderr, "time_settings %d %d/%d*%d\n",
462 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
463 if (!ti[S_BLACK].ignore_gtp) {
464 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
465 ti[S_WHITE] = ti[S_BLACK];
466 } else {
467 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
470 gtp_reply(id, NULL);
472 } else {
473 gtp_error(id, "unknown command", NULL);
474 return P_UNKNOWN_COMMAND;
476 return P_OK;
478 #undef next_tok