Merge branch 'master' into derm
[pachi.git] / gtp.c
blob9e3d947fcaf0211b32e568cb7f81f1763d438a0c
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 void
21 gtp_prefix(char prefix, int id)
23 if (id == NO_REPLY) return;
24 if (id >= 0)
25 printf("%c%d ", prefix, id);
26 else
27 printf("%c ", prefix);
30 void
31 gtp_flush(void)
33 putchar('\n');
34 fflush(stdout);
37 void
38 gtp_output(char prefix, int id, va_list params)
40 if (id == NO_REPLY) return;
41 gtp_prefix(prefix, id);
42 char *s;
43 while ((s = va_arg(params, char *))) {
44 fputs(s, stdout);
46 putchar('\n');
47 gtp_flush();
50 void
51 gtp_reply(int id, ...)
53 va_list params;
54 va_start(params, id);
55 gtp_output('=', id, params);
56 va_end(params);
59 void
60 gtp_error(int id, ...)
62 va_list params;
63 va_start(params, id);
64 gtp_output('?', id, params);
65 va_end(params);
69 /* XXX: THIS IS TOTALLY INSECURE!!!!
70 * Even basic input checking is missing. */
72 enum parse_code
73 gtp_parse(struct board *board, struct engine *engine, struct time_info *ti, char *buf)
75 #define next_tok(to_) \
76 to_ = next; \
77 next = next + strcspn(next, " \t\r\n"); \
78 if (*next) { \
79 *next = 0; next++; \
80 next += strspn(next, " \t\r\n"); \
83 if (strchr(buf, '#'))
84 *strchr(buf, '#') = 0;
86 char *cmd, *next = buf;
87 next_tok(cmd);
89 int id = -1;
90 if (isdigit(*cmd)) {
91 id = atoi(cmd);
92 next_tok(cmd);
95 if (!*cmd)
96 return P_OK;
98 if (!strcasecmp(cmd, "protocol_version")) {
99 gtp_reply(id, "2", NULL);
100 return P_OK;
102 } else if (!strcasecmp(cmd, "name")) {
103 /* KGS hack */
104 gtp_reply(id, "Pachi ", engine->name, NULL);
105 return P_OK;
107 } else if (!strcasecmp(cmd, "version")) {
108 gtp_reply(id, PACHI_VERSION, ": ", engine->comment, NULL);
109 return P_OK;
111 /* TODO: known_command */
113 } else if (!strcasecmp(cmd, "list_commands")) {
114 /* The internal command pachi-genmoves is not exported,
115 * it should only be used between master and slaves of
116 * the distributed engine. */
117 gtp_reply(id, "protocol_version\n"
118 "name\n"
119 "version\n"
120 "list_commands\n"
121 "quit\n"
122 "boardsize\n"
123 "clear_board\n"
124 "komi\n"
125 "play\n"
126 "genmove\n"
127 "kgs-genmove_cleanup\n"
128 "set_free_handicap\n"
129 "place_free_handicap\n"
130 "final_status_list\n"
131 "kgs-chat\n"
132 "time_left\n"
133 "time_settings\n"
134 "kgs-time_settings", NULL);
135 return P_OK;
138 if (engine->notify) {
139 char *reply;
140 enum parse_code c = engine->notify(engine, board, id, cmd, next, &reply);
141 if (c == P_NOREPLY) {
142 id = NO_REPLY;
143 } else if (c == P_DONE_OK) {
144 gtp_reply(id, reply, NULL);
145 return P_OK;
146 } else if (c == P_DONE_ERROR) {
147 gtp_error(id, reply, NULL);
148 /* This is an internal error for the engine, but
149 * it is still OK from main's point of view. */
150 return P_OK;
151 } else if (c != P_OK) {
152 return c;
156 if (!strcasecmp(cmd, "quit")) {
157 gtp_reply(id, NULL);
158 exit(0);
160 } else if (!strcasecmp(cmd, "boardsize")) {
161 char *arg;
162 next_tok(arg);
163 board_resize(board, atoi(arg));
165 gtp_reply(id, NULL);
167 } else if (!strcasecmp(cmd, "clear_board")) {
168 board_clear(board);
169 if (DEBUGL(1))
170 board_print(board, stderr);
171 gtp_reply(id, NULL);
172 return P_ENGINE_RESET;
174 } else if (!strcasecmp(cmd, "komi")) {
175 char *arg;
176 next_tok(arg);
177 sscanf(arg, "%f", &board->komi);
179 if (DEBUGL(1))
180 board_print(board, stderr);
181 gtp_reply(id, NULL);
183 } else if (!strcasecmp(cmd, "play")) {
184 struct move m;
186 char *arg;
187 next_tok(arg);
188 m.color = str2stone(arg);
189 next_tok(arg);
190 coord_t *c = str2coord(arg, board_size(board));
191 m.coord = *c; coord_done(c);
192 char *reply = NULL;
194 if (DEBUGL(1))
195 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
197 // This is where kgs starts the timer, not at genmove!
198 time_start_timer(&ti[stone_other(m.color)]);
200 if (engine->notify_play)
201 reply = engine->notify_play(engine, board, &m);
202 if (board_play(board, &m) < 0) {
203 if (DEBUGL(0)) {
204 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
205 board_print(board, stderr);
207 gtp_error(id, "illegal move", NULL);
208 } else {
209 if (DEBUGL(1))
210 board_print(board, stderr);
211 gtp_reply(id, reply, NULL);
214 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
215 char *arg;
216 next_tok(arg);
217 enum stone color = str2stone(arg);
219 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
220 struct move m = { *c, color };
221 board_play(board, &m);
222 char *str = coord2str(*c, board);
223 if (DEBUGL(1))
224 fprintf(stderr, "playing move %s\n", str);
225 if (DEBUGL(1)) {
226 board_print_custom(board, stderr, engine->printhook);
228 gtp_reply(id, str, NULL);
229 free(str); coord_done(c);
231 /* Account for spent time. If our GTP peer keeps our clock, this will
232 * be overriden by next time_left GTP command properly. */
233 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
234 * should be absolutely rare situation and we will just spend a little
235 * less time than we could on next few moves.) */
236 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
237 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
239 } else if (!strcasecmp(cmd, "pachi-genmoves") || !strcasecmp(cmd, "pachi-genmoves_cleanup")) {
240 char *arg;
241 next_tok(arg);
242 enum stone color = str2stone(arg);
244 char *reply = engine->genmoves(engine, board, &ti[color], color, !strcasecmp(cmd, "pachi-genmoves_cleanup"));
245 if (DEBUGL(2))
246 fprintf(stderr, "proposing moves %s\n", reply);
247 gtp_reply(id, reply, NULL);
249 /* See "genmove" above about time management. */
250 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
251 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
253 } else if (!strcasecmp(cmd, "set_free_handicap")) {
254 struct move m;
255 m.color = S_BLACK;
257 char *arg;
258 next_tok(arg);
259 do {
260 coord_t *c = str2coord(arg, board_size(board));
261 m.coord = *c; coord_done(c);
262 if (DEBUGL(1))
263 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
265 if (board_play(board, &m) < 0) {
266 if (DEBUGL(0))
267 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
268 gtp_error(id, "illegal move", NULL);
270 board->handicap++;
271 next_tok(arg);
272 } while (*arg);
273 if (DEBUGL(1))
274 board_print(board, stderr);
275 gtp_reply(id, NULL);
277 /* TODO: Engine should choose free handicap; however, it tends to take
278 * overly long to think it all out, and unless it's clever its
279 * handicap stones won't be of much help. ;-) */
280 } else if (!strcasecmp(cmd, "place_free_handicap")
281 || !strcasecmp(cmd, "fixed_handicap")) {
282 char *arg;
283 next_tok(arg);
284 int stones = atoi(arg);
286 gtp_prefix('=', id);
287 board_handicap(board, stones, id == NO_REPLY ? NULL : stdout);
288 if (DEBUGL(1))
289 board_print(board, stderr);
290 if (id == NO_REPLY) return P_OK;
291 putchar('\n');
292 gtp_flush();
294 } else if (!strcasecmp(cmd, "final_score")) {
295 struct move_queue q = { .moves = 0 };
296 if (engine->dead_group_list)
297 engine->dead_group_list(engine, board, &q);
298 float score = board_official_score(board, &q);
299 char str[64];
300 if (DEBUGL(1))
301 fprintf(stderr, "counted score %.1f\n", score);
302 if (score == 0) {
303 gtp_reply(id, "0", NULL);
304 } else if (score > 0) {
305 snprintf(str, 64, "W+%.1f", score);
306 gtp_reply(id, str, NULL);
307 } else {
308 snprintf(str, 64, "B+%.1f", -score);
309 gtp_reply(id, str, NULL);
312 /* XXX: This is a huge hack. */
313 } else if (!strcasecmp(cmd, "final_status_list")) {
314 if (id == NO_REPLY) return P_OK;
315 char *arg;
316 next_tok(arg);
317 struct move_queue q = { .moves = 0 };
318 if (engine->dead_group_list)
319 engine->dead_group_list(engine, board, &q);
320 /* else we return empty list - i.e. engine not supporting
321 * this assumes all stones alive at the game end. */
322 if (!strcasecmp(arg, "dead")) {
323 gtp_prefix('=', id);
324 for (int i = 0; i < q.moves; i++) {
325 foreach_in_group(board, q.move[i]) {
326 printf("%s ", coord2sstr(c, board));
327 } foreach_in_group_end;
328 putchar('\n');
330 if (!q.moves)
331 putchar('\n');
332 gtp_flush();
333 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
334 gtp_prefix('=', id);
335 bool printed_group = false;
336 foreach_point(board) { // foreach_group, effectively
337 group_t g = group_at(board, c);
338 if (!g || g != c) continue;
340 for (int i = 0; i < q.moves; i++) {
341 if (q.move[i] == g)
342 goto next_group;
344 foreach_in_group(board, g) {
345 printf("%s ", coord2sstr(c, board));
346 } foreach_in_group_end;
347 putchar('\n');
348 printed_group = true;
349 next_group:;
350 } foreach_point_end;
351 if (!printed_group)
352 putchar('\n');
353 gtp_flush();
354 } else {
355 gtp_error(id, "illegal status specifier", NULL);
358 /* Custom commands for handling UCT opening book */
359 } else if (!strcasecmp(cmd, "uct_genbook")) {
360 /* Board must be initialized properly, as if for genmove;
361 * makes sense only as 'uct_genbook b'. */
362 char *arg;
363 next_tok(arg);
364 enum stone color = str2stone(arg);
365 if (uct_genbook(engine, board, &ti[color], color))
366 gtp_reply(id, NULL);
367 else
368 gtp_error(id, "error generating book", NULL);
370 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
371 char *arg;
372 next_tok(arg);
373 enum stone color = str2stone(arg);
374 uct_dumpbook(engine, board, color);
375 gtp_reply(id, NULL);
377 } else if (!strcasecmp(cmd, "kgs-chat")) {
378 char *loc;
379 next_tok(loc);
380 char *src;
381 next_tok(src);
382 char *msg;
383 next_tok(msg);
384 char *reply = NULL;
385 if (engine->chat)
386 reply = engine->chat(engine, board, msg);
387 if (reply)
388 gtp_reply(id, reply, NULL);
389 else
390 gtp_error(id, "unknown chat command", NULL);
392 } else if (!strcasecmp(cmd, "time_left")) {
393 char *arg;
394 next_tok(arg);
395 enum stone color = str2stone(arg);
396 next_tok(arg);
397 int time = atoi(arg);
398 next_tok(arg);
399 int stones = atoi(arg);
400 if (!ti[color].ignore_gtp) {
401 time_left(&ti[color], time, stones);
402 } else {
403 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
406 gtp_reply(id, NULL);
408 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
409 char *time_system;
410 char *arg;
411 if (!strcasecmp(cmd, "kgs-time_settings")) {
412 next_tok(time_system);
413 } else {
414 time_system = "canadian";
417 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
418 if (!strcasecmp(time_system, "none")) {
419 main_time = -1;
420 } else if (!strcasecmp(time_system, "absolute")) {
421 next_tok(arg);
422 main_time = atoi(arg);
423 } else if (!strcasecmp(time_system, "byoyomi")) {
424 next_tok(arg);
425 main_time = atoi(arg);
426 next_tok(arg);
427 byoyomi_time = atoi(arg);
428 next_tok(arg);
429 byoyomi_periods = atoi(arg);
430 } else if (!strcasecmp(time_system, "canadian")) {
431 next_tok(arg);
432 main_time = atoi(arg);
433 next_tok(arg);
434 byoyomi_time = atoi(arg);
435 next_tok(arg);
436 byoyomi_stones = atoi(arg);
439 if (DEBUGL(1))
440 fprintf(stderr, "time_settings %d %d/%d*%d\n",
441 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
442 if (!ti[S_BLACK].ignore_gtp) {
443 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
444 ti[S_WHITE] = ti[S_BLACK];
445 } else {
446 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
449 gtp_reply(id, NULL);
451 } else {
452 gtp_error(id, "unknown command", NULL);
453 return P_UNKNOWN_COMMAND;
455 return P_OK;
457 #undef next_tok