Merge branch 'dist' into derm
[pachi.git] / gtp.c
blob154d3f081ca21fd3b0c5fb6359e60c88f6e3ade0
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 } else if (c == P_DONE_ERROR) {
146 gtp_error(id, reply, NULL);
147 } else if (c != P_OK) {
148 return c;
152 if (!strcasecmp(cmd, "quit")) {
153 gtp_reply(id, NULL);
154 exit(0);
156 } else if (!strcasecmp(cmd, "boardsize")) {
157 char *arg;
158 next_tok(arg);
159 board_resize(board, atoi(arg));
161 gtp_reply(id, NULL);
163 } else if (!strcasecmp(cmd, "clear_board")) {
164 board_clear(board);
165 if (DEBUGL(1))
166 board_print(board, stderr);
167 gtp_reply(id, NULL);
168 return P_ENGINE_RESET;
170 } else if (!strcasecmp(cmd, "komi")) {
171 char *arg;
172 next_tok(arg);
173 sscanf(arg, "%f", &board->komi);
175 if (DEBUGL(1))
176 board_print(board, stderr);
177 gtp_reply(id, NULL);
179 } else if (!strcasecmp(cmd, "play")) {
180 struct move m;
182 char *arg;
183 next_tok(arg);
184 m.color = str2stone(arg);
185 next_tok(arg);
186 coord_t *c = str2coord(arg, board_size(board));
187 m.coord = *c; coord_done(c);
188 char *reply = NULL;
190 if (DEBUGL(1))
191 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
193 // This is where kgs starts the timer, not at genmove!
194 time_start_timer(&ti[stone_other(m.color)]);
196 if (engine->notify_play)
197 reply = engine->notify_play(engine, board, &m);
198 if (board_play(board, &m) < 0) {
199 if (DEBUGL(0)) {
200 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
201 board_print(board, stderr);
203 gtp_error(id, "illegal move", NULL);
204 } else {
205 if (DEBUGL(1))
206 board_print(board, stderr);
207 gtp_reply(id, reply, NULL);
210 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
211 char *arg;
212 next_tok(arg);
213 enum stone color = str2stone(arg);
215 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
216 struct move m = { *c, color };
217 board_play(board, &m);
218 char *str = coord2str(*c, board);
219 if (DEBUGL(1))
220 fprintf(stderr, "playing move %s\n", str);
221 if (DEBUGL(1)) {
222 board_print_custom(board, stderr, engine->printhook);
224 gtp_reply(id, str, NULL);
225 free(str); coord_done(c);
227 /* Account for spent time. If our GTP peer keeps our clock, this will
228 * be overriden by next time_left GTP command properly. */
229 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
230 * should be absolutely rare situation and we will just spend a little
231 * less time than we could on next few moves.) */
232 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
233 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
235 } else if (!strcasecmp(cmd, "pachi-genmoves") || !strcasecmp(cmd, "pachi-genmoves_cleanup")) {
236 char *arg;
237 next_tok(arg);
238 enum stone color = str2stone(arg);
240 char *reply = engine->genmoves(engine, board, &ti[color], color, !strcasecmp(cmd, "pachi-genmoves_cleanup"));
241 if (DEBUGL(2))
242 fprintf(stderr, "proposing moves %s\n", reply);
243 gtp_reply(id, reply, NULL);
245 /* See "genmove" above about time management. */
246 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
247 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
249 } else if (!strcasecmp(cmd, "set_free_handicap")) {
250 struct move m;
251 m.color = S_BLACK;
253 char *arg;
254 next_tok(arg);
255 do {
256 coord_t *c = str2coord(arg, board_size(board));
257 m.coord = *c; coord_done(c);
258 if (DEBUGL(1))
259 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
261 if (board_play(board, &m) < 0) {
262 if (DEBUGL(0))
263 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
264 gtp_error(id, "illegal move", NULL);
266 board->handicap++;
267 next_tok(arg);
268 } while (*arg);
269 if (DEBUGL(1))
270 board_print(board, stderr);
271 gtp_reply(id, NULL);
273 /* TODO: Engine should choose free handicap; however, it tends to take
274 * overly long to think it all out, and unless it's clever its
275 * handicap stones won't be of much help. ;-) */
276 } else if (!strcasecmp(cmd, "place_free_handicap")
277 || !strcasecmp(cmd, "fixed_handicap")) {
278 char *arg;
279 next_tok(arg);
280 int stones = atoi(arg);
282 gtp_prefix('=', id);
283 board_handicap(board, stones, id == NO_REPLY ? NULL : stdout);
284 if (DEBUGL(1))
285 board_print(board, stderr);
286 if (id == NO_REPLY) return P_OK;
287 putchar('\n');
288 gtp_flush();
290 } else if (!strcasecmp(cmd, "final_score")) {
291 struct move_queue q = { .moves = 0 };
292 if (engine->dead_group_list)
293 engine->dead_group_list(engine, board, &q);
294 float score = board_official_score(board, &q);
295 char str[64];
296 if (DEBUGL(1))
297 fprintf(stderr, "counted score %.1f\n", score);
298 if (score == 0) {
299 gtp_reply(id, "0", NULL);
300 } else if (score > 0) {
301 snprintf(str, 64, "W+%.1f", score);
302 gtp_reply(id, str, NULL);
303 } else {
304 snprintf(str, 64, "B+%.1f", -score);
305 gtp_reply(id, str, NULL);
308 /* XXX: This is a huge hack. */
309 } else if (!strcasecmp(cmd, "final_status_list")) {
310 if (id == NO_REPLY) return P_OK;
311 char *arg;
312 next_tok(arg);
313 struct move_queue q = { .moves = 0 };
314 if (engine->dead_group_list)
315 engine->dead_group_list(engine, board, &q);
316 /* else we return empty list - i.e. engine not supporting
317 * this assumes all stones alive at the game end. */
318 if (!strcasecmp(arg, "dead")) {
319 gtp_prefix('=', id);
320 for (int i = 0; i < q.moves; i++) {
321 foreach_in_group(board, q.move[i]) {
322 printf("%s ", coord2sstr(c, board));
323 } foreach_in_group_end;
324 putchar('\n');
326 if (!q.moves)
327 putchar('\n');
328 gtp_flush();
329 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
330 gtp_prefix('=', id);
331 bool printed_group = false;
332 foreach_point(board) { // foreach_group, effectively
333 group_t g = group_at(board, c);
334 if (!g || g != c) continue;
336 for (int i = 0; i < q.moves; i++) {
337 if (q.move[i] == g)
338 goto next_group;
340 foreach_in_group(board, g) {
341 printf("%s ", coord2sstr(c, board));
342 } foreach_in_group_end;
343 putchar('\n');
344 printed_group = true;
345 next_group:;
346 } foreach_point_end;
347 if (!printed_group)
348 putchar('\n');
349 gtp_flush();
350 } else {
351 gtp_error(id, "illegal status specifier", NULL);
354 /* Custom commands for handling UCT opening book */
355 } else if (!strcasecmp(cmd, "uct_genbook")) {
356 /* Board must be initialized properly, as if for genmove;
357 * makes sense only as 'uct_genbook b'. */
358 char *arg;
359 next_tok(arg);
360 enum stone color = str2stone(arg);
361 if (uct_genbook(engine, board, &ti[color], color))
362 gtp_reply(id, NULL);
363 else
364 gtp_error(id, "error generating book", NULL);
366 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
367 char *arg;
368 next_tok(arg);
369 enum stone color = str2stone(arg);
370 uct_dumpbook(engine, board, color);
371 gtp_reply(id, NULL);
373 } else if (!strcasecmp(cmd, "kgs-chat")) {
374 char *loc;
375 next_tok(loc);
376 char *src;
377 next_tok(src);
378 char *msg;
379 next_tok(msg);
380 char *reply = NULL;
381 if (engine->chat)
382 reply = engine->chat(engine, board, msg);
383 if (reply)
384 gtp_reply(id, reply, NULL);
385 else
386 gtp_error(id, "unknown chat command", NULL);
388 } else if (!strcasecmp(cmd, "time_left")) {
389 char *arg;
390 next_tok(arg);
391 enum stone color = str2stone(arg);
392 next_tok(arg);
393 int time = atoi(arg);
394 next_tok(arg);
395 int stones = atoi(arg);
396 if (!ti[color].ignore_gtp) {
397 time_left(&ti[color], time, stones);
398 } else {
399 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
402 gtp_reply(id, NULL);
404 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
405 char *time_system;
406 char *arg;
407 if (!strcasecmp(cmd, "kgs-time_settings")) {
408 next_tok(time_system);
409 } else {
410 time_system = "canadian";
413 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
414 if (!strcasecmp(time_system, "none")) {
415 main_time = -1;
416 } else if (!strcasecmp(time_system, "absolute")) {
417 next_tok(arg);
418 main_time = atoi(arg);
419 } else if (!strcasecmp(time_system, "byoyomi")) {
420 next_tok(arg);
421 main_time = atoi(arg);
422 next_tok(arg);
423 byoyomi_time = atoi(arg);
424 next_tok(arg);
425 byoyomi_periods = atoi(arg);
426 } else if (!strcasecmp(time_system, "canadian")) {
427 next_tok(arg);
428 main_time = atoi(arg);
429 next_tok(arg);
430 byoyomi_time = atoi(arg);
431 next_tok(arg);
432 byoyomi_stones = atoi(arg);
435 if (DEBUGL(1))
436 fprintf(stderr, "time_settings %d %d/%d*%d\n",
437 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
438 if (!ti[S_BLACK].ignore_gtp) {
439 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
440 ti[S_WHITE] = ti[S_BLACK];
441 } else {
442 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
445 gtp_reply(id, NULL);
447 } else {
448 gtp_error(id, "unknown command", NULL);
449 return P_UNKNOWN_COMMAND;
451 return P_OK;
453 #undef next_tok