For TD_GAMES (no time control), set worst_playouts = desired_playouts.
[pachi.git] / gtp.c
blob8acedfe773a3f1714ebce365016b4476cf073d61
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 void
19 gtp_prefix(char prefix, int id)
21 if (id >= 0)
22 printf("%c%d ", prefix, id);
23 else
24 printf("%c ", prefix);
27 void
28 gtp_flush(void)
30 putchar('\n');
31 fflush(stdout);
34 void
35 gtp_output(char prefix, int id, va_list params)
37 gtp_prefix(prefix, id);
38 char *s;
39 while ((s = va_arg(params, char *))) {
40 fputs(s, stdout);
42 putchar('\n');
43 gtp_flush();
46 void
47 gtp_reply(int id, ...)
49 va_list params;
50 va_start(params, id);
51 gtp_output('=', id, params);
52 va_end(params);
55 void
56 gtp_error(int id, ...)
58 va_list params;
59 va_start(params, id);
60 gtp_output('?', id, params);
61 va_end(params);
65 /* XXX: THIS IS TOTALLY INSECURE!!!!
66 * Even basic input checking is missing. */
68 void
69 gtp_parse(struct board *board, struct engine *engine, struct time_info *ti, char *buf)
71 #define next_tok(to_) \
72 to_ = next; \
73 next = next + strcspn(next, " \t\r\n"); \
74 if (*next) { \
75 *next = 0; next++; \
76 next += strspn(next, " \t\r\n"); \
79 if (strchr(buf, '#'))
80 *strchr(buf, '#') = 0;
82 char *cmd, *next = buf;
83 next_tok(cmd);
85 int id = -1;
86 if (isdigit(*cmd)) {
87 id = atoi(cmd);
88 next_tok(cmd);
91 if (!*cmd)
92 return;
94 if (!strcasecmp(cmd, "protocol_version")) {
95 gtp_reply(id, "2", NULL);
97 } else if (!strcasecmp(cmd, "name")) {
98 /* KGS hack */
99 gtp_reply(id, "Pachi ", engine->name, NULL);
101 } else if (!strcasecmp(cmd, "version")) {
102 gtp_reply(id, PACHI_VERSION, ": ", engine->comment, NULL);
104 /* TODO: known_command */
106 } else if (!strcasecmp(cmd, "list_commands")) {
107 gtp_reply(id, "protocol_version\n"
108 "name\n"
109 "version\n"
110 "list_commands\n"
111 "quit\n"
112 "boardsize\n"
113 "clear_board\n"
114 "komi\n"
115 "play\n"
116 "genmove\n"
117 "kgs-genmove_cleanup\n"
118 "set_free_handicap\n"
119 "place_free_handicap\n"
120 "final_status_list\n"
121 "kgs-chat\n"
122 "time_left\n"
123 "time_settings\n"
124 "kgs-time_settings", NULL);
126 } else if (!strcasecmp(cmd, "quit")) {
127 gtp_reply(id, NULL);
128 exit(0);
130 } else if (!strcasecmp(cmd, "boardsize")) {
131 char *arg;
132 next_tok(arg);
133 board_resize(board, atoi(arg));
135 gtp_reply(id, NULL);
137 } else if (!strcasecmp(cmd, "clear_board")) {
138 board_clear(board);
139 engine_reset = true;
140 if (DEBUGL(1))
141 board_print(board, stderr);
142 gtp_reply(id, NULL);
144 } else if (!strcasecmp(cmd, "komi")) {
145 char *arg;
146 next_tok(arg);
147 sscanf(arg, "%f", &board->komi);
149 if (DEBUGL(1))
150 board_print(board, stderr);
151 gtp_reply(id, NULL);
153 } else if (!strcasecmp(cmd, "play")) {
154 struct move m;
156 char *arg;
157 next_tok(arg);
158 m.color = str2stone(arg);
159 next_tok(arg);
160 coord_t *c = str2coord(arg, board_size(board));
161 m.coord = *c; coord_done(c);
162 char *reply = NULL;
164 if (DEBUGL(1))
165 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
167 // This is where kgs starts our timer, not at coming genmove!
168 time_start_timer(&ti[stone_other(m.color)]);
170 if (engine->notify_play)
171 reply = engine->notify_play(engine, board, &m);
172 if (board_play(board, &m) < 0) {
173 if (DEBUGL(0)) {
174 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
175 board_print(board, stderr);
177 gtp_error(id, "illegal move", NULL);
178 } else {
179 if (DEBUGL(1))
180 board_print(board, stderr);
181 gtp_reply(id, reply, NULL);
184 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
185 char *arg;
186 next_tok(arg);
187 enum stone color = str2stone(arg);
188 time_prepare_move(&ti[color], board);
190 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
191 struct move m = { *c, color };
192 board_play(board, &m);
193 char *str = coord2str(*c, board);
194 if (DEBUGL(1))
195 fprintf(stderr, "playing move %s\n", str);
196 if (DEBUGL(1)) {
197 board_print_custom(board, stderr, engine->printhook);
199 gtp_reply(id, str, NULL);
200 free(str); coord_done(c);
202 } else if (!strcasecmp(cmd, "set_free_handicap")) {
203 struct move m;
204 m.color = S_BLACK;
206 char *arg;
207 next_tok(arg);
208 do {
209 coord_t *c = str2coord(arg, board_size(board));
210 m.coord = *c; coord_done(c);
211 if (DEBUGL(1))
212 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
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 gtp_error(id, "illegal move", NULL);
219 board->handicap++;
220 next_tok(arg);
221 } while (*arg);
222 if (DEBUGL(1))
223 board_print(board, stderr);
224 gtp_reply(id, NULL);
226 /* TODO: Engine should choose free handicap; however, it tends to take
227 * overly long to think it all out, and unless it's clever its
228 * handicap stones won't be of much help. ;-) */
229 } else if (!strcasecmp(cmd, "place_free_handicap")
230 || !strcasecmp(cmd, "fixed_handicap")) {
231 char *arg;
232 next_tok(arg);
233 int stones = atoi(arg);
235 gtp_prefix('=', id);
236 board_handicap(board, stones, stdout);
237 if (DEBUGL(1))
238 board_print(board, stderr);
239 putchar('\n');
240 gtp_flush();
242 } else if (!strcasecmp(cmd, "final_score")) {
243 struct move_queue q = { .moves = 0 };
244 if (engine->dead_group_list)
245 engine->dead_group_list(engine, board, &q);
246 float score = board_official_score(board, &q);
247 char str[64];
248 if (DEBUGL(1))
249 fprintf(stderr, "counted score %.1f\n", score);
250 if (score == 0) {
251 gtp_reply(id, "0", NULL);
252 } else if (score > 0) {
253 snprintf(str, 64, "W+%.1f", score);
254 gtp_reply(id, str, NULL);
255 } else {
256 snprintf(str, 64, "B+%.1f", -score);
257 gtp_reply(id, str, NULL);
260 /* XXX: This is a huge hack. */
261 } else if (!strcasecmp(cmd, "final_status_list")) {
262 char *arg;
263 next_tok(arg);
264 struct move_queue q = { .moves = 0 };
265 if (engine->dead_group_list)
266 engine->dead_group_list(engine, board, &q);
267 /* else we return empty list - i.e. engine not supporting
268 * this assumes all stones alive at the game end. */
269 if (!strcasecmp(arg, "dead")) {
270 gtp_prefix('=', id);
271 for (int i = 0; i < q.moves; i++) {
272 foreach_in_group(board, q.move[i]) {
273 printf("%s ", coord2sstr(c, board));
274 } foreach_in_group_end;
275 putchar('\n');
277 if (!q.moves)
278 putchar('\n');
279 gtp_flush();
280 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
281 gtp_prefix('=', id);
282 bool printed_group = false;
283 foreach_point(board) { // foreach_group, effectively
284 group_t g = group_at(board, c);
285 if (!g || g != c) continue;
287 for (int i = 0; i < q.moves; i++) {
288 if (q.move[i] == g)
289 goto next_group;
291 foreach_in_group(board, g) {
292 printf("%s ", coord2sstr(c, board));
293 } foreach_in_group_end;
294 putchar('\n');
295 printed_group = true;
296 next_group:;
297 } foreach_point_end;
298 if (!printed_group)
299 putchar('\n');
300 gtp_flush();
301 } else {
302 gtp_error(id, "illegal status specifier", NULL);
305 /* Custom commands for handling UCT opening book */
306 } else if (!strcasecmp(cmd, "uct_genbook")) {
307 /* Board must be initialized properly, as if for genmove;
308 * makes sense only as 'uct_genbook b'. */
309 char *arg;
310 next_tok(arg);
311 enum stone color = str2stone(arg);
312 if (uct_genbook(engine, board, &ti[color], color))
313 gtp_reply(id, NULL);
314 else
315 gtp_error(id, "error generating book", NULL);
317 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
318 char *arg;
319 next_tok(arg);
320 enum stone color = str2stone(arg);
321 uct_dumpbook(engine, board, color);
322 gtp_reply(id, NULL);
324 } else if (!strcasecmp(cmd, "kgs-chat")) {
325 char *loc;
326 next_tok(loc);
327 char *src;
328 next_tok(src);
329 char *msg;
330 next_tok(msg);
331 char *reply = NULL;
332 if (engine->chat)
333 reply = engine->chat(engine, board, msg);
334 if (reply)
335 gtp_reply(id, reply, NULL);
336 else
337 gtp_error(id, "unknown chat command", NULL);
339 } else if (!strcasecmp(cmd, "time_left")) {
340 char *arg;
341 next_tok(arg);
342 enum stone color = str2stone(arg);
343 next_tok(arg);
344 int time = atoi(arg);
345 next_tok(arg);
346 int stones = atoi(arg);
347 time_left(&ti[color], time, stones);
349 gtp_reply(id, NULL);
351 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
352 char *time_system = "canadian";
353 char *arg;
354 int main_time = -1, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
355 if (!strcasecmp(cmd, "kgs-time_settings")) {
356 next_tok(time_system);
357 if (!strcasecmp(time_system, "none")) {
358 // time > 0, stones 0: convention for unlimited
359 byoyomi_time = 1;
360 main_time = 0;
361 } else if (!strcasecmp(time_system, "absolute")) {
362 next_tok(arg);
363 main_time = atoi(arg);
364 } else if (!strcasecmp(time_system, "byoyomi")) {
365 next_tok(arg);
366 main_time = atoi(arg);
367 next_tok(arg);
368 byoyomi_time = atoi(arg);
369 byoyomi_stones = 1;
370 next_tok(arg);
371 byoyomi_periods = atoi(arg);
374 if (main_time < 0) { // canadian time system
375 next_tok(arg);
376 main_time = atoi(arg);
377 next_tok(arg);
378 byoyomi_time = atoi(arg);
379 next_tok(arg);
380 byoyomi_stones = atoi(arg);
382 if (DEBUGL(1))
383 fprintf(stderr, "time_settings %d %d %d %d\n",
384 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
385 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
386 ti[S_WHITE] = ti[S_BLACK];
388 gtp_reply(id, NULL);
390 } else {
391 gtp_error(id, "unknown command", NULL);
394 #undef next_tok