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
);
145 } else if (c
== P_DONE_ERROR
) {
146 gtp_error(id
, reply
, NULL
);
147 } else if (c
!= P_OK
) {
152 if (!strcasecmp(cmd
, "quit")) {
156 } else if (!strcasecmp(cmd
, "boardsize")) {
159 board_resize(board
, atoi(arg
));
163 } else if (!strcasecmp(cmd
, "clear_board")) {
166 board_print(board
, stderr
);
168 return P_ENGINE_RESET
;
170 } else if (!strcasecmp(cmd
, "komi")) {
173 sscanf(arg
, "%f", &board
->komi
);
176 board_print(board
, stderr
);
179 } else if (!strcasecmp(cmd
, "play")) {
184 m
.color
= str2stone(arg
);
186 coord_t
*c
= str2coord(arg
, board_size(board
));
187 m
.coord
= *c
; coord_done(c
);
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) {
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
);
206 board_print(board
, stderr
);
207 gtp_reply(id
, reply
, NULL
);
210 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
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
);
220 fprintf(stderr
, "playing move %s\n", str
);
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")) {
238 enum stone color
= str2stone(arg
);
240 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "pachi-genmoves_cleanup"));
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")) {
256 coord_t
*c
= str2coord(arg
, board_size(board
));
257 m
.coord
= *c
; coord_done(c
);
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) {
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
);
270 board_print(board
, stderr
);
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")) {
280 int stones
= atoi(arg
);
283 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
285 board_print(board
, stderr
);
286 if (id
== NO_REPLY
) return P_OK
;
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
);
297 fprintf(stderr
, "counted score %.1f\n", score
);
299 gtp_reply(id
, "0", NULL
);
300 } else if (score
> 0) {
301 snprintf(str
, 64, "W+%.1f", score
);
302 gtp_reply(id
, str
, NULL
);
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
;
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")) {
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
;
329 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
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
++) {
340 foreach_in_group(board
, g
) {
341 printf("%s ", coord2sstr(c
, board
));
342 } foreach_in_group_end
;
344 printed_group
= true;
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'. */
360 enum stone color
= str2stone(arg
);
361 if (uct_genbook(engine
, board
, &ti
[color
], color
))
364 gtp_error(id
, "error generating book", NULL
);
366 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
369 enum stone color
= str2stone(arg
);
370 uct_dumpbook(engine
, board
, color
);
373 } else if (!strcasecmp(cmd
, "kgs-chat")) {
382 reply
= engine
->chat(engine
, board
, msg
);
384 gtp_reply(id
, reply
, NULL
);
386 gtp_error(id
, "unknown chat command", NULL
);
388 } else if (!strcasecmp(cmd
, "time_left")) {
391 enum stone color
= str2stone(arg
);
393 int time
= atoi(arg
);
395 int stones
= atoi(arg
);
396 if (!ti
[color
].ignore_gtp
) {
397 time_left(&ti
[color
], time
, stones
);
399 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
404 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
407 if (!strcasecmp(cmd
, "kgs-time_settings")) {
408 next_tok(time_system
);
410 time_system
= "canadian";
413 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
414 if (!strcasecmp(time_system
, "none")) {
416 } else if (!strcasecmp(time_system
, "absolute")) {
418 main_time
= atoi(arg
);
419 } else if (!strcasecmp(time_system
, "byoyomi")) {
421 main_time
= atoi(arg
);
423 byoyomi_time
= atoi(arg
);
425 byoyomi_periods
= atoi(arg
);
426 } else if (!strcasecmp(time_system
, "canadian")) {
428 main_time
= atoi(arg
);
430 byoyomi_time
= atoi(arg
);
432 byoyomi_stones
= atoi(arg
);
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
];
442 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
448 gtp_error(id
, "unknown command", NULL
);
449 return P_UNKNOWN_COMMAND
;