21 gtp_prefix(char prefix
, int id
)
23 if (id
== NO_REPLY
) return;
25 printf("%c%d ", prefix
, id
);
27 printf("%c ", prefix
);
38 gtp_output(char prefix
, int id
, va_list params
)
40 if (id
== NO_REPLY
) return;
41 gtp_prefix(prefix
, id
);
43 while ((s
= va_arg(params
, char *))) {
51 gtp_reply(int id
, ...)
55 gtp_output('=', id
, params
);
60 gtp_error(int id
, ...)
64 gtp_output('?', id
, params
);
69 /* XXX: THIS IS TOTALLY INSECURE!!!!
70 * Even basic input checking is missing. */
73 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
75 #define next_tok(to_) \
77 next = next + strcspn(next, " \t\r\n"); \
80 next += strspn(next, " \t\r\n"); \
84 *strchr(buf
, '#') = 0;
86 char *cmd
, *next
= buf
;
98 if (!strcasecmp(cmd
, "protocol_version")) {
99 gtp_reply(id
, "2", NULL
);
102 } else if (!strcasecmp(cmd
, "name")) {
104 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
107 } else if (!strcasecmp(cmd
, "version")) {
108 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
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"
127 "kgs-genmove_cleanup\n"
128 "set_free_handicap\n"
129 "place_free_handicap\n"
130 "final_status_list\n"
134 "kgs-time_settings", NULL
);
138 if (engine
->notify
) {
140 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
141 if (c
== P_NOREPLY
) {
143 } else if (c
== P_DONE_OK
) {
144 gtp_reply(id
, reply
, NULL
);
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. */
151 } else if (c
!= P_OK
) {
156 if (!strcasecmp(cmd
, "quit")) {
160 } else if (!strcasecmp(cmd
, "boardsize")) {
163 board_resize(board
, atoi(arg
));
167 } else if (!strcasecmp(cmd
, "clear_board")) {
170 board_print(board
, stderr
);
172 return P_ENGINE_RESET
;
174 } else if (!strcasecmp(cmd
, "komi")) {
177 sscanf(arg
, "%f", &board
->komi
);
180 board_print(board
, stderr
);
183 } else if (!strcasecmp(cmd
, "play")) {
188 m
.color
= str2stone(arg
);
190 coord_t
*c
= str2coord(arg
, board_size(board
));
191 m
.coord
= *c
; coord_done(c
);
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) {
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
);
210 board_print(board
, stderr
);
211 gtp_reply(id
, reply
, NULL
);
214 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
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
);
224 fprintf(stderr
, "playing move %s\n", str
);
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")) {
242 enum stone color
= str2stone(arg
);
244 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "pachi-genmoves_cleanup"));
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")) {
260 coord_t
*c
= str2coord(arg
, board_size(board
));
261 m
.coord
= *c
; coord_done(c
);
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) {
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
);
274 board_print(board
, stderr
);
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")) {
284 int stones
= atoi(arg
);
287 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
289 board_print(board
, stderr
);
290 if (id
== NO_REPLY
) return P_OK
;
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
);
301 fprintf(stderr
, "counted score %.1f\n", score
);
303 gtp_reply(id
, "0", NULL
);
304 } else if (score
> 0) {
305 snprintf(str
, 64, "W+%.1f", score
);
306 gtp_reply(id
, str
, NULL
);
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
;
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")) {
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
;
333 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
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
++) {
344 foreach_in_group(board
, g
) {
345 printf("%s ", coord2sstr(c
, board
));
346 } foreach_in_group_end
;
348 printed_group
= true;
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'. */
364 enum stone color
= str2stone(arg
);
365 if (uct_genbook(engine
, board
, &ti
[color
], color
))
368 gtp_error(id
, "error generating book", NULL
);
370 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
373 enum stone color
= str2stone(arg
);
374 uct_dumpbook(engine
, board
, color
);
377 } else if (!strcasecmp(cmd
, "kgs-chat")) {
386 reply
= engine
->chat(engine
, board
, msg
);
388 gtp_reply(id
, reply
, NULL
);
390 gtp_error(id
, "unknown chat command", NULL
);
392 } else if (!strcasecmp(cmd
, "time_left")) {
395 enum stone color
= str2stone(arg
);
397 int time
= atoi(arg
);
399 int stones
= atoi(arg
);
400 if (!ti
[color
].ignore_gtp
) {
401 time_left(&ti
[color
], time
, stones
);
403 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
408 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
411 if (!strcasecmp(cmd
, "kgs-time_settings")) {
412 next_tok(time_system
);
414 time_system
= "canadian";
417 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
418 if (!strcasecmp(time_system
, "none")) {
420 } else if (!strcasecmp(time_system
, "absolute")) {
422 main_time
= atoi(arg
);
423 } else if (!strcasecmp(time_system
, "byoyomi")) {
425 main_time
= atoi(arg
);
427 byoyomi_time
= atoi(arg
);
429 byoyomi_periods
= atoi(arg
);
430 } else if (!strcasecmp(time_system
, "canadian")) {
432 main_time
= atoi(arg
);
434 byoyomi_time
= atoi(arg
);
436 byoyomi_stones
= atoi(arg
);
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
];
446 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
452 gtp_error(id
, "unknown command", NULL
);
453 return P_UNKNOWN_COMMAND
;