21 /* Sleep 5 seconds after a game ends to give time to kill the program. */
22 #define GAME_OVER_SLEEP 5
25 gtp_prefix(char prefix
, int id
)
27 if (id
== NO_REPLY
) return;
29 printf("%c%d ", prefix
, id
);
31 printf("%c ", prefix
);
42 gtp_output(char prefix
, int id
, va_list params
)
44 if (id
== NO_REPLY
) return;
45 gtp_prefix(prefix
, id
);
47 while ((s
= va_arg(params
, char *))) {
55 gtp_reply(int id
, ...)
59 gtp_output('=', id
, params
);
64 gtp_error(int id
, ...)
68 gtp_output('?', id
, params
);
72 /* List of known gtp commands. The internal command pachi-genmoves is not exported,
73 * it should only be used between master and slaves of the distributed engine. */
74 static char *known_commands
=
87 "kgs-genmove_cleanup\n"
89 "place_free_handicap\n"
97 /* Return true if cmd is a valid gtp command. */
99 gtp_is_valid(char *cmd
)
101 if (!cmd
|| !*cmd
) return false;
102 char *s
= strcasestr(known_commands
, cmd
);
103 if (!s
) return false;
105 int len
= strlen(cmd
);
106 return s
[len
] == '\0' || s
[len
] == '\n';
109 /* XXX: THIS IS TOTALLY INSECURE!!!!
110 * Even basic input checking is missing. */
113 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
115 #define next_tok(to_) \
117 next = next + strcspn(next, " \t\r\n"); \
120 next += strspn(next, " \t\r\n"); \
123 if (strchr(buf
, '#'))
124 *strchr(buf
, '#') = 0;
126 char *cmd
, *next
= buf
;
138 if (!strcasecmp(cmd
, "protocol_version")) {
139 gtp_reply(id
, "2", NULL
);
142 } else if (!strcasecmp(cmd
, "name")) {
144 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
147 } else if (!strcasecmp(cmd
, "version")) {
148 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
151 } else if (!strcasecmp(cmd
, "list_commands")) {
152 gtp_reply(id
, known_commands
, NULL
);
156 if (engine
->notify
) {
158 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
159 if (c
== P_NOREPLY
) {
161 } else if (c
== P_DONE_OK
) {
162 gtp_reply(id
, reply
, NULL
);
164 } else if (c
== P_DONE_ERROR
) {
165 gtp_error(id
, reply
, NULL
);
166 /* This is an internal error for the engine, but
167 * it is still OK from main's point of view. */
169 } else if (c
!= P_OK
) {
174 if (!strcasecmp(cmd
, "quit")) {
178 } else if (!strcasecmp(cmd
, "boardsize")) {
181 board_resize(board
, atoi(arg
));
185 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
188 board_print(board
, stderr
);
189 if (!strcasecmp(cmd
, "kgs-game_over")) {
191 fprintf(stderr
, "game is over\n");
192 /* Sleep before replying, so that kgs doesn't
193 * start another game immediately. */
194 sleep(GAME_OVER_SLEEP
);
197 return P_ENGINE_RESET
;
199 } else if (!strcasecmp(cmd
, "komi")) {
202 sscanf(arg
, "%f", &board
->komi
);
205 board_print(board
, stderr
);
208 } else if (!strcasecmp(cmd
, "kgs-rules")) {
211 if (!strcasecmp(arg
, "japanese")) {
212 board
->rules
= RULES_JAPANESE
;
213 } else if (!strcasecmp(arg
, "chinese")) {
214 board
->rules
= RULES_CHINESE
;
215 } else if (!strcasecmp(arg
, "aga")) {
216 board
->rules
= RULES_AGA
;
217 } else if (!strcasecmp(arg
, "new_zealand")) {
218 board
->rules
= RULES_NEW_ZEALAND
;
220 gtp_error(id
, "unknown rules", NULL
);
225 } else if (!strcasecmp(cmd
, "play")) {
230 m
.color
= str2stone(arg
);
232 coord_t
*c
= str2coord(arg
, board_size(board
));
233 m
.coord
= *c
; coord_done(c
);
237 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
239 // This is where kgs starts the timer, not at genmove!
240 time_start_timer(&ti
[stone_other(m
.color
)]);
242 if (engine
->notify_play
)
243 reply
= engine
->notify_play(engine
, board
, &m
);
244 if (board_play(board
, &m
) < 0) {
246 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
247 board_print(board
, stderr
);
249 gtp_error(id
, "illegal move", NULL
);
252 board_print_custom(board
, stderr
, engine
->printhook
);
253 gtp_reply(id
, reply
, NULL
);
256 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
259 enum stone color
= str2stone(arg
);
261 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
262 struct move m
= { *c
, color
};
263 board_play(board
, &m
);
264 char *str
= coord2str(*c
, board
);
266 fprintf(stderr
, "playing move %s\n", str
);
268 board_print_custom(board
, stderr
, engine
->printhook
);
270 gtp_reply(id
, str
, NULL
);
271 free(str
); coord_done(c
);
273 /* Account for spent time. If our GTP peer keeps our clock, this will
274 * be overriden by next time_left GTP command properly. */
275 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
276 * should be absolutely rare situation and we will just spend a little
277 * less time than we could on next few moves.) */
278 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
279 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
281 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
284 enum stone color
= str2stone(arg
);
288 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
289 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
290 &stats
, &stats_size
);
292 gtp_error(id
, "genmoves error", NULL
);
296 fprintf(stderr
, "proposing moves %s\n", reply
);
298 board_print_custom(board
, stderr
, engine
->printhook
);
299 gtp_reply(id
, reply
, NULL
);
300 if (stats_size
> 0) {
301 double start
= time_now();
302 fwrite(stats
, 1, stats_size
, stdout
);
305 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
306 stats_size
, (time_now() - start
)*1000);
309 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
316 coord_t
*c
= str2coord(arg
, board_size(board
));
317 m
.coord
= *c
; coord_done(c
);
319 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
321 if (board_play(board
, &m
) < 0) {
323 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
324 gtp_error(id
, "illegal move", NULL
);
330 board_print(board
, stderr
);
333 /* TODO: Engine should choose free handicap; however, it tends to take
334 * overly long to think it all out, and unless it's clever its
335 * handicap stones won't be of much help. ;-) */
336 } else if (!strcasecmp(cmd
, "place_free_handicap")
337 || !strcasecmp(cmd
, "fixed_handicap")) {
340 int stones
= atoi(arg
);
343 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
345 board_print(board
, stderr
);
346 if (id
== NO_REPLY
) return P_OK
;
350 } else if (!strcasecmp(cmd
, "final_score")) {
351 struct move_queue q
= { .moves
= 0 };
352 if (engine
->dead_group_list
)
353 engine
->dead_group_list(engine
, board
, &q
);
354 float score
= board_official_score(board
, &q
);
357 fprintf(stderr
, "counted score %.1f\n", score
);
359 gtp_reply(id
, "0", NULL
);
360 } else if (score
> 0) {
361 snprintf(str
, 64, "W+%.1f", score
);
362 gtp_reply(id
, str
, NULL
);
364 snprintf(str
, 64, "B+%.1f", -score
);
365 gtp_reply(id
, str
, NULL
);
368 /* XXX: This is a huge hack. */
369 } else if (!strcasecmp(cmd
, "final_status_list")) {
370 if (id
== NO_REPLY
) return P_OK
;
373 struct move_queue q
= { .moves
= 0 };
374 if (engine
->dead_group_list
)
375 engine
->dead_group_list(engine
, board
, &q
);
376 /* else we return empty list - i.e. engine not supporting
377 * this assumes all stones alive at the game end. */
378 if (!strcasecmp(arg
, "dead")) {
380 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
381 foreach_in_group(board
, q
.move
[i
]) {
382 printf("%s ", coord2sstr(c
, board
));
383 } foreach_in_group_end
;
389 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
391 bool printed_group
= false;
392 foreach_point(board
) { // foreach_group, effectively
393 group_t g
= group_at(board
, c
);
394 if (!g
|| g
!= c
) continue;
396 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
400 foreach_in_group(board
, g
) {
401 printf("%s ", coord2sstr(c
, board
));
402 } foreach_in_group_end
;
404 printed_group
= true;
411 gtp_error(id
, "illegal status specifier", NULL
);
414 /* Custom commands for handling UCT opening book */
415 } else if (!strcasecmp(cmd
, "uct_genbook")) {
416 /* Board must be initialized properly, as if for genmove;
417 * makes sense only as 'uct_genbook b'. */
420 enum stone color
= str2stone(arg
);
421 if (uct_genbook(engine
, board
, &ti
[color
], color
))
424 gtp_error(id
, "error generating book", NULL
);
426 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
429 enum stone color
= str2stone(arg
);
430 uct_dumpbook(engine
, board
, color
);
433 } else if (!strcasecmp(cmd
, "kgs-chat")) {
442 reply
= engine
->chat(engine
, board
, msg
);
444 gtp_reply(id
, reply
, NULL
);
446 gtp_error(id
, "unknown chat command", NULL
);
448 } else if (!strcasecmp(cmd
, "time_left")) {
451 enum stone color
= str2stone(arg
);
453 int time
= atoi(arg
);
455 int stones
= atoi(arg
);
456 if (!ti
[color
].ignore_gtp
) {
457 time_left(&ti
[color
], time
, stones
);
459 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
464 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
467 if (!strcasecmp(cmd
, "kgs-time_settings")) {
468 next_tok(time_system
);
470 time_system
= "canadian";
473 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
474 if (!strcasecmp(time_system
, "none")) {
476 } else if (!strcasecmp(time_system
, "absolute")) {
478 main_time
= atoi(arg
);
479 } else if (!strcasecmp(time_system
, "byoyomi")) {
481 main_time
= atoi(arg
);
483 byoyomi_time
= atoi(arg
);
485 byoyomi_periods
= atoi(arg
);
486 } else if (!strcasecmp(time_system
, "canadian")) {
488 main_time
= atoi(arg
);
490 byoyomi_time
= atoi(arg
);
492 byoyomi_stones
= atoi(arg
);
496 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
497 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
498 if (!ti
[S_BLACK
].ignore_gtp
) {
499 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
500 ti
[S_WHITE
] = ti
[S_BLACK
];
502 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
508 gtp_error(id
, "unknown command", NULL
);
509 return P_UNKNOWN_COMMAND
;