uct_search() result cannot change: Check against worst.time instead of desired.time
[pachi.git] / gtp.c
blob918bf08b592d96a98fc1b0839eefa1296f8ee8a4
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);
189 coord_t *c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
190 struct move m = { *c, color };
191 board_play(board, &m);
192 char *str = coord2str(*c, board);
193 if (DEBUGL(1))
194 fprintf(stderr, "playing move %s\n", str);
195 if (DEBUGL(1)) {
196 board_print_custom(board, stderr, engine->printhook);
198 gtp_reply(id, str, NULL);
199 free(str); coord_done(c);
201 /* Account for spent time. If our GTP peer keeps our clock, this will
202 * be overriden by next time_left GTP command properly. */
203 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
204 * should be absolutely rare situation and we will just spend a little
205 * less time than we could on next few moves.) */
206 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
207 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start);
209 } else if (!strcasecmp(cmd, "set_free_handicap")) {
210 struct move m;
211 m.color = S_BLACK;
213 char *arg;
214 next_tok(arg);
215 do {
216 coord_t *c = str2coord(arg, board_size(board));
217 m.coord = *c; coord_done(c);
218 if (DEBUGL(1))
219 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
221 if (board_play(board, &m) < 0) {
222 if (DEBUGL(0))
223 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
224 gtp_error(id, "illegal move", NULL);
226 board->handicap++;
227 next_tok(arg);
228 } while (*arg);
229 if (DEBUGL(1))
230 board_print(board, stderr);
231 gtp_reply(id, NULL);
233 /* TODO: Engine should choose free handicap; however, it tends to take
234 * overly long to think it all out, and unless it's clever its
235 * handicap stones won't be of much help. ;-) */
236 } else if (!strcasecmp(cmd, "place_free_handicap")
237 || !strcasecmp(cmd, "fixed_handicap")) {
238 char *arg;
239 next_tok(arg);
240 int stones = atoi(arg);
242 gtp_prefix('=', id);
243 board_handicap(board, stones, stdout);
244 if (DEBUGL(1))
245 board_print(board, stderr);
246 putchar('\n');
247 gtp_flush();
249 } else if (!strcasecmp(cmd, "final_score")) {
250 struct move_queue q = { .moves = 0 };
251 if (engine->dead_group_list)
252 engine->dead_group_list(engine, board, &q);
253 float score = board_official_score(board, &q);
254 char str[64];
255 if (DEBUGL(1))
256 fprintf(stderr, "counted score %.1f\n", score);
257 if (score == 0) {
258 gtp_reply(id, "0", NULL);
259 } else if (score > 0) {
260 snprintf(str, 64, "W+%.1f", score);
261 gtp_reply(id, str, NULL);
262 } else {
263 snprintf(str, 64, "B+%.1f", -score);
264 gtp_reply(id, str, NULL);
267 /* XXX: This is a huge hack. */
268 } else if (!strcasecmp(cmd, "final_status_list")) {
269 char *arg;
270 next_tok(arg);
271 struct move_queue q = { .moves = 0 };
272 if (engine->dead_group_list)
273 engine->dead_group_list(engine, board, &q);
274 /* else we return empty list - i.e. engine not supporting
275 * this assumes all stones alive at the game end. */
276 if (!strcasecmp(arg, "dead")) {
277 gtp_prefix('=', id);
278 for (int i = 0; i < q.moves; i++) {
279 foreach_in_group(board, q.move[i]) {
280 printf("%s ", coord2sstr(c, board));
281 } foreach_in_group_end;
282 putchar('\n');
284 if (!q.moves)
285 putchar('\n');
286 gtp_flush();
287 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
288 gtp_prefix('=', id);
289 bool printed_group = false;
290 foreach_point(board) { // foreach_group, effectively
291 group_t g = group_at(board, c);
292 if (!g || g != c) continue;
294 for (int i = 0; i < q.moves; i++) {
295 if (q.move[i] == g)
296 goto next_group;
298 foreach_in_group(board, g) {
299 printf("%s ", coord2sstr(c, board));
300 } foreach_in_group_end;
301 putchar('\n');
302 printed_group = true;
303 next_group:;
304 } foreach_point_end;
305 if (!printed_group)
306 putchar('\n');
307 gtp_flush();
308 } else {
309 gtp_error(id, "illegal status specifier", NULL);
312 /* Custom commands for handling UCT opening book */
313 } else if (!strcasecmp(cmd, "uct_genbook")) {
314 /* Board must be initialized properly, as if for genmove;
315 * makes sense only as 'uct_genbook b'. */
316 char *arg;
317 next_tok(arg);
318 enum stone color = str2stone(arg);
319 if (uct_genbook(engine, board, &ti[color], color))
320 gtp_reply(id, NULL);
321 else
322 gtp_error(id, "error generating book", NULL);
324 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
325 char *arg;
326 next_tok(arg);
327 enum stone color = str2stone(arg);
328 uct_dumpbook(engine, board, color);
329 gtp_reply(id, NULL);
331 } else if (!strcasecmp(cmd, "kgs-chat")) {
332 char *loc;
333 next_tok(loc);
334 char *src;
335 next_tok(src);
336 char *msg;
337 next_tok(msg);
338 char *reply = NULL;
339 if (engine->chat)
340 reply = engine->chat(engine, board, msg);
341 if (reply)
342 gtp_reply(id, reply, NULL);
343 else
344 gtp_error(id, "unknown chat command", NULL);
346 } else if (!strcasecmp(cmd, "time_left")) {
347 char *arg;
348 next_tok(arg);
349 enum stone color = str2stone(arg);
350 next_tok(arg);
351 int time = atoi(arg);
352 next_tok(arg);
353 int stones = atoi(arg);
354 if (!ti[color].ignore_gtp) {
355 time_left(&ti[color], time, stones);
356 } else {
357 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
360 gtp_reply(id, NULL);
362 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
363 char *time_system;
364 char *arg;
365 if (!strcasecmp(cmd, "kgs-time_settings")) {
366 next_tok(time_system);
367 } else {
368 time_system = "canadian";
371 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
372 if (!strcasecmp(time_system, "none")) {
373 main_time = -1;
374 } else if (!strcasecmp(time_system, "absolute")) {
375 next_tok(arg);
376 main_time = atoi(arg);
377 } else if (!strcasecmp(time_system, "byoyomi")) {
378 next_tok(arg);
379 main_time = atoi(arg);
380 next_tok(arg);
381 byoyomi_time = atoi(arg);
382 next_tok(arg);
383 byoyomi_periods = atoi(arg);
384 } else if (!strcasecmp(time_system, "canadian")) {
385 next_tok(arg);
386 main_time = atoi(arg);
387 next_tok(arg);
388 byoyomi_time = atoi(arg);
389 next_tok(arg);
390 byoyomi_stones = atoi(arg);
393 if (DEBUGL(1))
394 fprintf(stderr, "time_settings %d %d/%d*%d\n",
395 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
396 if (!ti[S_BLACK].ignore_gtp) {
397 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
398 ti[S_WHITE] = ti[S_BLACK];
399 } else {
400 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
403 gtp_reply(id, NULL);
405 } else {
406 gtp_error(id, "unknown command", NULL);
409 #undef next_tok