9 /* unacceptable when the program is thinking, or quit pondering */
10 #define AbortDifficultCommand \
11 if ( game_status & flag_thinking ) \
13 str_error = str_busy_think; \
16 else if ( game_status & ( flag_pondering | flag_puzzling ) ) \
18 game_status |= flag_quit_ponder; \
23 # define CmdBook(x,y) cmd_book(y);
24 static int CONV cmd_book( char **lasts );
26 # define CmdBook(x,y) cmd_book(x,y);
27 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts );
28 static int CONV cmd_book( tree_t * restrict ptree, char **lasts );
31 #if ! defined(NO_STDOUT)
32 static int CONV cmd_stress( char **lasts );
36 static int CONV proce_csalan( tree_t * restrict ptree );
37 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts );
38 static int CONV cmd_sendpv( char **lasts );
42 static int CONV proce_mnj( tree_t * restrict ptree );
43 static int CONV cmd_mnjignore( tree_t *restrict ptree, char **lasts );
44 static int CONV cmd_mnj( char **lasts );
45 static int CONV cmd_mnjmove( tree_t * restrict ptree, char **lasts,
50 static int CONV proce_usi( tree_t * restrict ptree );
51 static int CONV usi_posi( tree_t * restrict ptree, char **lasts );
52 static int CONV usi_go( tree_t * restrict ptree, char **lasts );
53 static int CONV usi_ignore( tree_t * restrict ptree, char **lasts );
57 static int CONV cmd_thread( char **lasts );
61 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts );
65 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts );
68 #if defined(DFPN_CLIENT)
69 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts );
72 static int CONV proce_cui( tree_t * restrict ptree );
73 static int CONV cmd_usrmove( tree_t * restrict ptree, const char *str_move,
75 static int CONV cmd_outmove( tree_t * restrict ptree );
76 static int CONV cmd_move_now( void );
77 static int CONV cmd_ponder( char **lasts );
78 static int CONV cmd_limit( char **lasts );
79 static int CONV cmd_quit( void );
80 static int CONV cmd_beep( char **lasts );
81 static int CONV cmd_peek( char **lasts );
82 static int CONV cmd_stdout( char **lasts );
83 static int CONV cmd_newlog( char **lasts );
84 static int CONV cmd_hash( char **lasts );
85 static int CONV cmd_ping( void );
86 static int CONV cmd_suspend( void );
87 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts );
88 static int CONV cmd_display( tree_t * restrict ptree, char **lasts );
89 static int CONV cmd_move( tree_t * restrict ptree, char **lasts );
90 static int CONV cmd_new( tree_t * restrict ptree, char **lasts );
91 static int CONV cmd_read( tree_t * restrict ptree, char **lasts );
92 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts );
93 static int CONV cmd_undo( tree_t * restrict ptree ); // [HGM] undo
94 static int CONV cmd_time( char **lasts );
95 static int CONV cmd_undo( tree_t * restrict ptree ); // [HGM] undo
96 static int CONV cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
97 static int CONV cmd_inex( tree_t * restrict ptree, int inex, char **last ); // [HGM] exclude
98 static int CONV cmd_exit( void );
101 int CONV is_move( const char *str )
103 if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
104 && isdigit( (int)str[2] ) && isdigit( (int)str[3] )
105 && isupper( (int)str[4] ) && isupper( (int)str[5] )
106 && str[6] == '\0' ) { return 1; }
114 strtok_r( char *s, const char *t, char **next)
118 if( ! s ) return NULL;
119 while( *s && strchr(t, *s) ) s++;
120 if( ! *s ) return NULL;
122 while( *s && ! (p = strchr(t, *s)) ) s++;
124 if( ! *s ) *next = NULL;
131 procedure( tree_t * restrict ptree )
134 if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
138 if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
142 if ( usi_mode != usi_off ) { return proce_usi( ptree ); }
145 return proce_cui( ptree );
148 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
149 int move_list[1024], move_ptr;
151 int all_moves[MAX_LEGAL_MOVES];
154 #define IF(X) else if(!strcmp(command, X))
156 int myTime, hisTime, movesPerSession, inc, plyNr;
158 int root_pos[nsquare];
161 bonanza_piece( char p )
164 if(p >= 'a') p += 'A' - 'a';
165 switch(p) { // encode piece
166 case 'P': piece = pawn; break;
167 case 'L': piece = lance; break;
168 case 'N': piece = knight; break;
169 case 'S': piece = silver; break;
170 case 'G': piece = gold; break;
171 case 'B': piece = bishop; break;
172 case 'R': piece = rook; break;
173 case 'K': piece = king; break;
181 static char fen[128];
183 char *q = fen, *start = p;
184 strncpy( fen, p+9 , 127);
185 strcpy( p, "new SU" ); p += 6;
186 for ( r = 1; r <= 9; r++) {
187 for ( f = 0; *q; q++ ) {
189 if( *q == '+' ) promoted = promote, q++;
190 if( isdigit( *q ) ) f += atoi( q ); else
191 if( isalpha( *q ) ) {
192 int piece = bonanza_piece( *q );
193 if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
194 gote = !gote; // switch color
195 *p++ = ( gote ? '-' : '+' );
197 sprintf( p, "%d%d%s", 9-f, r, astr_table_piece[piece + promoted] );
199 } else { q++; break; }
202 while( q[-1] == ' ' ) q++;
203 if( q[-1] == '[' ) { // holdings
204 while( isalpha( *q ) ) {
205 int piece = bonanza_piece( *q );
206 if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
207 gote = !gote; // switch color
208 *p++ = ( gote ? '-' : '+' );
210 sprintf( p, "00%s", astr_table_piece[piece] );
215 while( *q == ' ' ) q++;
217 sprintf(p, " %c", *q == 'w' ? '+' : '-' ); // side to move
221 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
223 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
225 if(fromY == '@') { // drop (contains all info needed to convert it)
226 piece = bonanza_piece( fromX );
227 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
228 } else { // board move (need to figure out moved piece)
229 int from = ('9' - fromY)*9 + (fromX - 'a');
230 int flag = (promo == '+' ? FLAG_PROMO : 0);
231 piece = abs( BOARD[from] ); // this only works when no search in progress!
232 printf("# piece from board: %d\n", piece);fflush(stdout);
233 if( game_status & flag_thinking ) {
234 int i, to = ('9' - toY)*9 + (toX - 'a');
235 piece = 0; // kludge to force illegal CSA move
236 for( i = 0; i < root_nmove; i++ ) { // determine the piece from the move list
237 int move = root_move_list[i].move;
238 if( I2To(move) != to ) continue;
239 if( I2From(move) != from ) continue;
240 if( (move & FLAG_PROMO) != flag ) continue;
241 piece = I2PieceMove( move ); // we found the move; take the piece from it
244 } else if( game_status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
245 printf("# piece corrected to %d\n", piece);fflush(stdout);
246 if( promo == '+') piece += promote;
247 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
252 update_exclude_list( tree_t * restrict ptree, int add, unsigned int move )
253 { // [HGM] exclude: manage list of excluded moves
255 if( moves_ignore[0] == MOVE_NA ) { // nothing is excluded yet; make a copy of root move list;
256 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
257 all_moves[i] = MOVE_NA;
259 if ( move == MOVE_NA ) { // all moves
260 if( add ) { // copy entire list of legal moves
261 for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
262 } else moves_ignore[0] = MOVE_NA; // clear list
263 } else { // single move
264 for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
265 if( add ) { // we must add the move
266 if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
267 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
268 moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
269 } else { // we must delete the move
270 if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
271 while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
278 { // set white and black times from own and opponent time.
280 if(movesPerSession <= 0) moves = 35; else {
282 while(moves <= 0) moves += movesPerSession;
284 time_limit = (myTime-inc-30)/(moves+2) + inc;
285 time_max_limit = 3*time_limit;
286 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
287 time_limit *= 10; // msec
288 time_max_limit *= 10;
289 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
293 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
294 { // [HGM] added to make Bonanza.exe a native WinBoard engine
296 static char forceMode = 0;
297 sscanf(line + strlen(command) + 1, "%d", &value);
298 Out("# command = '%s'\n", line);
302 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
303 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
305 Out("feature option=\"Resign threshold -spin 1000 0 2000\"\n");
306 Out("feature option=\"Create Learn File -button\"\n");
307 Out("feature option=\"Learn -check 1\"\n");
308 Out("feature option=\"Use book -check 1\"\n");
309 Out("feature option=\"Book -combo wide /// narrow\"\n");
310 Out("feature option=\"\"\n");
311 Out("feature option=\"\"\n");
312 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
313 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
315 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
316 IF("easy") { strcpy(line, "ponder off"); return 0; }
317 IF("hard") { strcpy(line, "ponder on"); return 0; }
320 IF("time") { sscanf(line+5, "%d", &myTime); }
321 IF("otim") { sscanf(line+5, "%d", &hisTime); }
322 IF("force") { forceMode = 1; }
323 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
324 IF("memory") { sprintf(line, "hash %d", value); return 0; }
325 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
326 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
328 IF("quit") { return 0; }
329 IF("analyze") { return 0; }
330 IF("exit") { return 0; }
331 IF("variant") { /* ignore, since it must be Shogi */; }
332 IF("setboard") { forceMode = 1; plyNr = 0; read_fen( line ); return 0; }
335 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
336 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
337 if(sscanf(line+7, "Resign threshold=%d", &value) == 1) { sprintf(line, "resign %d", value); return 0; }
338 if(sscanf(line+7, "Learn=%d", &value) == 1) { sprintf(line, "hash learn %s", value ? "on" : "off"); return 0; }
339 if(sscanf(line+7, "Create Learn File") == 1) { sprintf(line, "hash learn create"); return 0; }
340 if(sscanf(line+7, "Use book=%d", &value) == 1) { sprintf(line, "book %s", value ? "on" : "off"); return 0; }
341 if(sscanf(line+7, "Book=%s", buf) == 1) { sprintf(line, "book %s", buf); return 0; }
343 IF("level") { int min, sec; float fsec=0.;
344 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
345 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
346 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
348 IF("usermove") { char buf[20];
349 xboard_to_CSA( ptree, line+9, buf );
350 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
355 IF("undo") { return 0; }
357 IF("ping") { Out("pong %d\n", value); }
358 IF("exclude") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
359 IF("include") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
364 static int CONV proce_cui( tree_t * restrict ptree )
369 token = strtok_r( str_cmdline, str_delimiters, &last );
371 if ( token == NULL || *token == '#' ) { return 1; }
375 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
376 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
377 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
379 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
382 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
383 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
384 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
385 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
386 if ( ! strcmp( token, "include" ) ) { return cmd_inex( ptree , 0, &last); } // [HGM] exclude
387 if ( ! strcmp( token, "exclude" ) ) { return cmd_inex( ptree , 1, &last); } // [HGM] exclude
389 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
391 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
392 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
393 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
394 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
395 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
396 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
397 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
398 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
399 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
400 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
401 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
402 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
403 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
404 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
405 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
406 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
407 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
408 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
409 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
410 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
411 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
413 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
414 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
417 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
420 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
422 #if defined(DFPN_CLIENT)
423 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
427 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
429 #if ! defined(NO_STDOUT)
430 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
432 #if ! defined(MINIMUM)
433 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
436 str_error = str_bad_cmdline;
442 static int CONV proce_csalan( tree_t * restrict ptree )
447 token = strtok_r( str_cmdline, str_delimiters, &last );
449 if ( token == NULL ) { return 1; }
450 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
455 token = strtok_r( NULL, str_delimiters, &last );
456 if ( token == NULL || *token != 'T' )
458 str_error = str_bad_cmdline;
462 l = strtol( token+1, &ptr, 0 );
463 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
465 str_error = str_bad_cmdline;
469 adjust_time( (unsigned int)l, client_turn );
470 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
473 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
475 return cmd_usrmove( ptree, token+1, &last );
477 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
478 if ( ! strcmp( token, "#WIN" )
479 || ! strcmp( token, "#LOSE" )
480 || ! strcmp( token, "#DRAW" )
481 || ! strcmp( token, "#CHUDAN" ) )
483 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
485 game_status |= flag_suspend;
489 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
490 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
494 if ( client_ngame == client_max_game ) { return cmd_quit(); }
496 return client_next_game( ptree, client_str_addr, (int)client_port );
505 static int CONV proce_mnj( tree_t * restrict ptree )
511 token = strtok_r( str_cmdline, str_delimiters, &last );
512 if ( token == NULL ) { return 1; }
514 if ( ! strcmp( token, "new" ) )
516 iret = cmd_suspend();
517 if ( iret != 1 ) { return iret; }
520 iret = cmd_new( ptree, &last );
521 if ( iret < 0 ) { return iret; }
523 moves_ignore[0] = MOVE_NA;
524 return analyze( ptree );
526 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
527 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
528 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
529 if ( ! strcmp( token, "retract" ) )
533 const char *str = strtok_r( NULL, str_delimiters, &last );
536 str_error = str_bad_cmdline;
539 l = strtol( str, &ptr, 0 );
540 if ( ptr == str || (long)NUM_UNMAKE < l )
542 str_error = str_bad_cmdline;
546 return cmd_mnjmove( ptree, &last, (int)l );
548 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
550 str_error = str_bad_cmdline;
556 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
565 token = strtok_r( NULL, str_delimiters, lasts );
568 str_error = str_bad_cmdline;
571 lid = strtol( token, &ptr, 0 );
572 if ( ptr == token || lid == LONG_MAX || lid < 1 )
574 str_error = str_bad_cmdline;
578 AbortDifficultCommand;
580 for ( i = 0; ; i += 1 )
582 token = strtok_r( NULL, str_delimiters, lasts );
583 if ( token == NULL ) { break; }
585 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
587 moves_ignore[i] = move;
591 str_error = str_bad_cmdline;
594 mnj_posi_id = (int)lid;
595 moves_ignore[i] = MOVE_NA;
597 return analyze( ptree );
602 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
604 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
605 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
611 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
613 str_error = str_bad_cmdline;
617 lid = strtol( str2, &ptr, 0 );
618 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
620 str_error = str_bad_cmdline;
624 AbortDifficultCommand;
628 iret = unmake_move_root( ptree );
629 if ( iret < 0 ) { return iret; }
634 iret = interpret_CSA_move( ptree, &move, str1 );
635 if ( iret < 0 ) { return iret; }
637 iret = get_elapsed( &time_turn_start );
638 if ( iret < 0 ) { return iret; }
640 mnj_posi_id = (int)lid;
642 iret = make_move_root( ptree, move, ( flag_time | flag_rep
643 | flag_detect_hang ) );
644 if ( iret < 0 ) { return iret; }
646 # if ! defined(NO_STDOUT)
647 iret = out_board( ptree, stdout, 0, 0 );
648 if ( iret < 0 ) { return iret; }
651 moves_ignore[0] = MOVE_NA;
652 return analyze( ptree );
658 do_analyze( tree_t * restrict ptree )
659 { // [HGM] analyze: do a ponder search on the current position
661 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
662 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
664 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
665 for( iret = 0; iret < nsquare; iret++ ) root_pos[iret] = BOARD[iret];
667 game_status |= flag_pondering;
668 iret = iterate( ptree );
669 game_status &= ~flag_pondering;
675 cmd_undo( tree_t * restrict ptree )
676 { // [HGM] undo: restart the game, and feed all moves except the last
677 int i, last = move_ptr;
678 char *p = start_data;
679 if( move_ptr <= 0 ) {
680 str_error = "undo past start of game ignored";
684 AbortDifficultCommand;
687 cmd_new( ptree, &p );
688 for(i=0; i<last; i++) {
689 make_move_root( ptree, move_list[i], 0);
692 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
693 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
700 cmd_analyze( tree_t * restrict ptree )
701 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
702 AbortDifficultCommand;
705 return do_analyze( ptree );
710 cmd_inex( tree_t * restrict ptree, int inex, char **last )
711 { // [HGM] analyze: switch off analysis mode
712 const char *str = strtok_r( NULL, str_delimiters, last );
716 AbortDifficultCommand;
718 if( ! strcmp( str, "all" ) ) {
721 iret = interpret_CSA_move( ptree, &move, str );
722 if ( iret < 0 ) { return iret; }
725 update_exclude_list( ptree, inex, move );
727 if ( ! analyze_mode ) return 1;
728 return do_analyze( ptree );
734 { // [HGM] analyze: switch off analysis mode
735 if ( !analyze_mode ) {
736 str_error = "was not analyzing";
740 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
742 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
749 static int CONV proce_usi( tree_t * restrict ptree )
755 token = strtok_r( str_cmdline, str_delimiters, &lasts );
756 if ( token == NULL ) { return 1; }
758 if ( ! strcmp( token, "usi" ) )
760 USIOut( "id name %s\n", str_myname );
761 USIOut( "id author Kunihito Hoki\n" );
766 if ( ! strcmp( token, "isready" ) )
768 USIOut( "readyok\n", str_myname );
772 if ( ! strcmp( token, "echo" ) )
774 USIOut( "%s\n", lasts );
778 if ( ! strcmp( token, "ignore_moves" ) )
780 return usi_ignore( ptree, &lasts );
783 if ( ! strcmp( token, "genmove_probability" ) )
785 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
786 return usi_root_list( ptree );
789 if ( ! strcmp( token, "go" ) )
791 iret = usi_go( ptree, &lasts );
792 moves_ignore[0] = MOVE_NA;
796 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
797 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
798 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
800 str_error = str_bad_cmdline;
806 usi_ignore( tree_t * restrict ptree, char **lasts )
813 AbortDifficultCommand;
815 for ( i = 0; ; i += 1 )
817 token = strtok_r( NULL, str_delimiters, lasts );
818 if ( token == NULL ) { break; }
820 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
821 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
823 moves_ignore[i] = move;
826 moves_ignore[i] = MOVE_NA;
833 usi_go( tree_t * restrict ptree, char **lasts )
840 AbortDifficultCommand;
842 if ( game_status & mask_game_end )
844 str_error = str_game_ended;
848 token = strtok_r( NULL, str_delimiters, lasts );
850 if ( ! strcmp( token, "book" ) )
852 AbortDifficultCommand;
853 if ( usi_book( ptree ) < 0 ) { return -1; }
859 if ( ! strcmp( token, "infinite" ) )
861 usi_byoyomi = UINT_MAX;
862 depth_limit = PLY_MAX;
863 node_limit = UINT64_MAX;
864 sec_limit_depth = UINT_MAX;
866 else if ( ! strcmp( token, "byoyomi" ) )
868 token = strtok_r( NULL, str_delimiters, lasts );
871 str_error = str_bad_cmdline;
875 l = strtol( token, &ptr, 0 );
876 if ( ptr == token || l > UINT_MAX || l < 1 )
878 str_error = str_bad_cmdline;
882 usi_byoyomi = (unsigned int)l;
883 depth_limit = PLY_MAX;
884 node_limit = UINT64_MAX;
885 sec_limit_depth = UINT_MAX;
888 str_error = str_bad_cmdline;
893 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
895 iret = com_turn_start( ptree, 0 );
897 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
906 usi_posi( tree_t * restrict ptree, char **lasts )
912 AbortDifficultCommand;
914 moves_ignore[0] = MOVE_NA;
916 token = strtok_r( NULL, str_delimiters, lasts );
917 if ( strcmp( token, "startpos" ) )
919 str_error = str_bad_cmdline;
923 if ( ini_game( ptree, &min_posi_no_handicap,
924 flag_history, NULL, NULL ) < 0 ) { return -1; }
926 token = strtok_r( NULL, str_delimiters, lasts );
927 if ( token == NULL ) { return 1; }
929 if ( strcmp( token, "moves" ) )
931 str_error = str_bad_cmdline;
937 token = strtok_r( NULL, str_delimiters, lasts );
938 if ( token == NULL ) { break; }
940 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
941 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
942 if ( make_move_root( ptree, move, ( flag_history | flag_time
944 | flag_detect_hang ) ) < 0 )
950 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
957 static int CONV cmd_move_now( void )
959 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
966 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
974 if ( game_status & mask_game_end )
976 str_error = str_game_ended;
980 if ( game_status & flag_thinking )
982 str_error = str_busy_think;
986 str = strtok_r( NULL, str_delimiters, lasts );
987 if ( str == NULL ) { lelapsed = 0; }
991 str_error = str_bad_cmdline;
995 lelapsed = strtol( str, &ptr, 0 );
996 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
998 str_error = str_bad_cmdline;
1003 if ( game_status & ( flag_pondering | flag_puzzling ) )
1007 for ( i = 0; i < ponder_nmove; i++ )
1009 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
1014 if ( i == ponder_nmove )
1016 #if defined(CSA_LAN)
1017 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
1020 #if defined(CSASHOGI)
1021 AbortDifficultCommand;
1023 str_error = str_illegal_move;
1028 if ( ( game_status & flag_puzzling )
1029 || strcmp( str_move, str_CSA_move(ponder_move) ) )
1031 ponder_move = MOVE_PONDER_FAILED;
1032 game_status |= flag_quit_ponder;
1036 iret = update_time( Flip(root_turn) );
1037 if ( iret < 0 ) { return iret; }
1040 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
1043 out_CSA( ptree, &record_game, ponder_move );
1045 game_status &= ~flag_pondering;
1046 game_status |= flag_thinking;
1050 set_search_limit_time( root_turn );
1052 OutCsaShogi( "info ponder end\n" );
1054 str = str_time_symple( time_turn_start - time_start );
1055 Out( " %6s MOVE PREDICTION HIT\n"
1056 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
1061 iret = interpret_CSA_move( ptree, &move, str_move );
1062 if ( iret < 0 ) { return iret; }
1063 move_evasion_pchk = 0;
1064 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
1065 | flag_detect_hang ) );
1069 #if defined(CSA_LAN)
1070 if ( sckt_csa != SCKT_NULL )
1072 if ( move_evasion_pchk )
1074 str = str_CSA_move( move_evasion_pchk );
1075 iret = sckt_out( sckt_csa, "%c%s\n",
1076 ach_turn[Flip(root_turn)], str );
1077 if ( iret < 0 ) { return iret; }
1079 return cmd_suspend();
1083 if ( move_evasion_pchk )
1085 str = str_CSA_move( move_evasion_pchk );
1086 #if defined(CSASHOGI)
1087 OutCsaShogi( "move%s\n", str );
1088 return cmd_suspend();
1090 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
1091 ach_turn[Flip(root_turn)], str );
1092 str_error = str_message;
1100 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
1101 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
1103 #if defined(CSA_LAN)
1104 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
1106 iret = sckt_out( sckt_csa, "%%TORYO\n" );
1107 if ( iret < 0 ) { return iret; }
1111 if ( ! ( game_status & mask_game_end ) )
1113 iret = com_turn_start( ptree, 0 );
1114 if ( iret < 0 ) { return iret; }
1121 static int CONV cmd_beep( char **lasts )
1123 const char *str = strtok_r( NULL, str_delimiters, lasts );
1126 str_error = str_bad_cmdline;
1130 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
1131 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
1133 str_error = str_bad_cmdline;
1141 static int CONV cmd_peek( char **lasts )
1143 const char *str = strtok_r( NULL, str_delimiters, lasts );
1147 str_error = str_bad_cmdline;
1151 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
1152 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
1154 str_error = str_bad_cmdline;
1162 static int CONV cmd_stdout( char **lasts )
1164 const char *str = strtok_r( NULL, str_delimiters, lasts );
1168 str_error = str_bad_cmdline;
1172 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1173 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1175 str_error = str_bad_cmdline;
1183 static int CONV cmd_newlog( char **lasts )
1185 const char *str = strtok_r( NULL, str_delimiters, lasts );
1189 str_error = str_bad_cmdline;
1193 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1194 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1196 str_error = str_bad_cmdline;
1204 static int CONV cmd_ponder( char **lasts )
1206 const char *str = strtok_r( NULL, str_delimiters, lasts );
1210 str_error = str_bad_cmdline;
1214 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1215 else if ( ! strcmp( str, str_off ) )
1217 if ( game_status & ( flag_pondering | flag_puzzling ) )
1219 game_status |= flag_quit_ponder;
1221 game_status |= flag_noponder;
1224 str_error = str_bad_cmdline;
1232 #if ! defined(NO_STDOUT)
1233 static int CONV cmd_stress( char **lasts )
1235 const char *str = strtok_r( NULL, str_delimiters, lasts );
1239 str_error = str_bad_cmdline;
1243 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1244 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1246 str_error = str_bad_cmdline;
1256 #if defined(MINIMUM)
1257 cmd_book( char **lasts )
1259 cmd_book( tree_t * restrict ptree, char **lasts )
1262 const char *str = strtok_r( NULL, str_delimiters, lasts );
1267 str_error = str_bad_cmdline;
1270 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1271 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1272 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1273 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1274 #if ! defined(MINIMUM)
1275 else if ( ! strcmp( str, "create" ) )
1277 AbortDifficultCommand;
1279 iret = book_create( ptree );
1280 if ( iret < 0 ) { return iret; }
1282 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1284 if ( iret < 0 ) { return iret; }
1286 iret = get_elapsed( &time_turn_start );
1290 str_error = str_bad_cmdline;
1298 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1300 const char *str = strtok_r( NULL, str_delimiters, lasts );
1307 l = strtol( str, &ptr, 0 );
1310 str_error = str_bad_cmdline;
1313 if ( l == 1 ) { game_status &= ~flag_reverse; }
1314 else if ( l == 2 ) { game_status |= flag_reverse; }
1316 str_error = str_bad_cmdline;
1322 iret = out_board( ptree, stdout, 0, 0 );
1323 if ( iret < 0 ) { return iret; }
1324 #if ! defined(NO_LOGGING)
1325 iret = out_board( ptree, pf_log, 0, 0 );
1326 if ( iret < 0 ) { return iret; }
1334 static int CONV cmd_ping( void )
1336 OutCsaShogi( "pong\n" );
1342 static int CONV cmd_hash( char **lasts )
1344 const char *str = strtok_r( NULL, str_delimiters, lasts );
1350 str_error = str_bad_cmdline;
1354 l = strtol( str, &ptr, 0 );
1355 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1357 str_error = str_bad_cmdline;
1361 AbortDifficultCommand;
1363 log2_ntrans_table = (int)l;
1364 memory_free( (void *)ptrans_table_orig );
1365 return ini_trans_table();
1369 static int CONV cmd_limit( char **lasts )
1371 const char *str = strtok_r( NULL, str_delimiters, lasts );
1377 str_error = str_bad_cmdline;
1381 AbortDifficultCommand;
1383 if ( ! strcmp( str, "depth" ) )
1385 str = strtok_r( NULL, str_delimiters, lasts );
1388 str_error = str_bad_cmdline;
1391 l1 = strtol( str, &ptr, 0 );
1392 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1394 str_error = str_bad_cmdline;
1397 sec_limit_up = UINT_MAX;
1398 node_limit = UINT64_MAX;
1399 depth_limit = (int)l1;
1401 else if ( ! strcmp( str, "nodes" ) )
1403 str = strtok_r( NULL, str_delimiters, lasts );
1406 str_error = str_bad_cmdline;
1409 l1 = strtol( str, &ptr, 0 );
1410 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1412 str_error = str_bad_cmdline;
1415 sec_limit_up = UINT_MAX;
1416 depth_limit = PLY_MAX;
1417 node_limit = (uint64_t)l1;
1419 else if ( ! strcmp( str, "time" ) )
1421 str = strtok_r( NULL, str_delimiters, lasts );
1424 str_error = str_bad_cmdline;
1428 if ( ! strcmp( str, "extendable" ) )
1430 game_status |= flag_time_extendable;
1432 else if ( ! strcmp( str, "strict" ) )
1434 game_status &= ~flag_time_extendable;
1437 l1 = strtol( str, &ptr, 0 );
1438 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1440 str_error = str_bad_cmdline;
1444 str = strtok_r( NULL, str_delimiters, lasts );
1447 str_error = str_bad_cmdline;
1450 l2 = strtol( str, &ptr, 0 );
1451 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1453 str_error = str_bad_cmdline;
1457 str = strtok_r( NULL, str_delimiters, lasts );
1458 if ( ! str ) { l3 = -1; }
1460 l3 = strtol( str, &ptr, 0 );
1461 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1463 str_error = str_bad_cmdline;
1468 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1470 depth_limit = PLY_MAX;
1471 node_limit = UINT64_MAX;
1472 sec_limit = (unsigned int)l1 * 60U;
1473 sec_limit_up = (unsigned int)l2;
1474 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1475 else { sec_limit_depth = (unsigned int)l3; }
1479 str_error = str_bad_cmdline;
1488 cmd_read( tree_t * restrict ptree, char **lasts )
1490 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1491 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1492 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1493 const char *str_tmp;
1494 FILE *pf_src, *pf_dest;
1495 char str_file[SIZE_FILENAME];
1501 flag = flag_history | flag_rep | flag_detect_hang;
1507 str_error = str_bad_cmdline;
1513 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1514 else if ( strcmp( str2, "nil" ) )
1516 str_error = str_bad_cmdline;
1523 l = strtol( str3, &ptr, 0 );
1524 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1526 str_error = str_bad_cmdline;
1529 moves = (unsigned int)l - 1U;
1532 AbortDifficultCommand;
1534 if ( ! strcmp( str1, "." ) )
1536 str_tmp = "game.cs_";
1538 #if defined(NO_LOGGING)
1539 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1541 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1542 str_dir_logs, record_num );
1544 pf_dest = file_open( str_tmp, "w" );
1545 if ( pf_dest == NULL ) { return -2; }
1547 pf_src = file_open( str_file, "r" );
1548 if ( pf_src == NULL )
1550 file_close( pf_dest );
1554 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1556 iret = file_close( pf_src );
1559 file_close( pf_dest );
1563 iret = file_close( pf_dest );
1564 if ( iret < 0 ) { return iret; }
1570 iret = read_record( ptree, str1, moves, flag );
1571 if ( iret < 0 ) { return iret; }
1573 iret = get_elapsed( &time_turn_start );
1574 if ( iret < 0 ) { return iret; }
1576 if ( str_tmp && remove( str_tmp ) )
1578 out_warning( "remove() failed." );
1586 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1588 const char *str = strtok_r( NULL, str_delimiters, lasts );
1592 if ( str == NULL || *str == 'T' )
1594 AbortDifficultCommand;
1596 if ( game_status & mask_game_end ) { return 1; }
1598 game_status |= flag_resigned;
1599 update_time( root_turn );
1600 out_CSA( ptree, &record_game, MOVE_RESIGN );
1603 l = strtol( str, &ptr, 0 );
1604 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1606 str_error = str_bad_cmdline;
1609 resign_threshold = (int)l;
1616 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1618 const char *str = strtok_r( NULL, str_delimiters, lasts );
1624 if ( game_status & mask_game_end )
1626 str_error = str_game_ended;
1630 AbortDifficultCommand;
1634 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1636 str_error = str_bad_cmdline;
1640 iret = get_elapsed( &time_turn_start );
1641 if ( iret < 0 ) { return iret; }
1643 return com_turn_start( ptree, 0 );
1646 l = strtol( str, &ptr, 0 );
1647 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1649 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1651 str_error = str_bad_cmdline;
1655 for ( i = 0; i < l; i += 1 )
1657 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1659 iret = get_elapsed( &time_turn_start );
1660 if ( iret < 0 ) { return iret; }
1662 iret = com_turn_start( ptree, 0 );
1663 if ( iret < 0 ) { return iret; }
1670 iret = interpret_CSA_move( ptree, &move, str );
1671 if ( iret < 0 ) { return iret; }
1673 iret = get_elapsed( &time_turn_start );
1674 if ( iret < 0 ) { return iret; }
1676 iret = make_move_root( ptree, move,
1677 ( flag_history | flag_time | flag_rep
1678 | flag_detect_hang ) );
1679 if ( iret < 0 ) { return iret; }
1681 str = strtok_r( NULL, str_delimiters, lasts );
1683 } while ( str != NULL );
1685 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1686 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1692 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1694 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1695 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1696 const min_posi_t *pmp;
1697 min_posi_t min_posi;
1700 AbortDifficultCommand;
1702 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1703 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1707 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1708 memset( &min_posi.asquare, empty, nsquare );
1709 min_posi.hand_black = min_posi.hand_white = 0;
1710 iret = read_board_rep1( str1, &min_posi );
1711 if ( iret < 0 ) { return iret; }
1715 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1716 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1718 str_error = str_bad_cmdline;
1722 else { min_posi.turn_to_move = black; }
1726 else { pmp = &min_posi_no_handicap; }
1728 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1729 if ( iret < 0 ) { return iret; }
1731 return get_elapsed( &time_turn_start );
1735 static int CONV cmd_outmove( tree_t * restrict ptree )
1737 const char *str_move;
1739 unsigned int move_list[ MAX_LEGAL_MOVES ];
1742 AbortDifficultCommand;
1744 if ( game_status & mask_game_end )
1746 Out( "NO LEGAL MOVE\n" );
1747 DFPNOut( "NO LEGAL MOVE\n" );
1751 n = gen_legal_moves( ptree, move_list, 0 );
1754 for ( c = i = 0; i < n; i += 1 )
1756 str_move = str_CSA_move(move_list[i]);
1758 if ( i && ( i % 10 ) == 0 )
1760 Out( "%s\n", buffer );
1761 DFPNOut( "%s ", buffer );
1762 memcpy( buffer, str_move, 6 );
1768 memcpy( buffer + c + 1, str_move, 6 );
1772 memcpy( buffer + c, str_move, 6 );
1777 Out( "%s\n", buffer );
1778 DFPNOut( "%s\n", buffer );
1784 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1786 const char *str = strtok_r( NULL, str_delimiters, lasts );
1789 unsigned int nposition;
1795 AbortDifficultCommand;
1800 if ( str != NULL && ! strcmp( str, "mate" ) )
1803 str = strtok_r( NULL, str_delimiters, lasts );
1809 l = strtol( str, &ptr, 0 );
1810 if ( ptr == str || l == LONG_MAX || l < 1 )
1812 str_error = str_bad_cmdline;
1815 nposition = (unsigned int)l;
1817 else { nposition = UINT_MAX; }
1820 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1821 if ( iret < 0 ) { return iret; }
1824 iret = is_mate ? solve_mate_problems( ptree, nposition )
1825 : solve_problems( ptree, nposition );
1827 iret = solve_problems( ptree, nposition );
1832 record_close( &record_problems );
1836 iret = record_close( &record_problems );
1837 if ( iret < 0 ) { return iret; }
1839 return get_elapsed( &time_turn_start );
1843 static int CONV cmd_quit( void )
1845 game_status |= flag_quit;
1850 static int CONV cmd_suspend( void )
1852 if ( game_status & ( flag_pondering | flag_puzzling ) )
1854 game_status |= flag_quit_ponder;
1858 game_status |= flag_suspend;
1863 static int CONV cmd_time( char **lasts )
1865 const char *str = strtok_r( NULL, str_delimiters, lasts );
1870 str_error = str_bad_cmdline;
1873 else if ( ! strcmp( str, "response" ) )
1876 str = strtok_r( NULL, str_delimiters, lasts );
1879 str_error = str_bad_cmdline;
1882 l = strtol( str, &ptr, 0 );
1883 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1885 str_error = str_bad_cmdline;
1888 time_response = (unsigned int)l;
1891 else if ( ! strcmp( str, "remain" ) )
1895 str = strtok_r( NULL, str_delimiters, lasts );
1898 str_error = str_bad_cmdline;
1901 l1 = strtol( str, &ptr, 0 );
1902 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1904 str_error = str_bad_cmdline;
1908 str = strtok_r( NULL, str_delimiters, lasts );
1911 str_error = str_bad_cmdline;
1914 l2 = strtol( str, &ptr, 0 );
1915 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1917 str_error = str_bad_cmdline;
1921 if ( sec_limit_up == UINT_MAX )
1923 str_error = str_bad_cmdline;
1927 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1930 str_error = str_bad_cmdline;
1935 #if !defined(MINIMUM)
1936 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1937 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1939 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1940 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1941 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1942 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1944 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1945 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1949 unsigned int max_games;
1950 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1954 str_error = str_bad_cmdline;
1957 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1958 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1960 str_error = str_bad_cmdline;
1964 max_games = UINT_MAX;
1965 max_iterations = INT_MAX;
1966 nworker1 = nworker2 = nsteps = 1;
1970 l = strtol( str2, &ptr, 0 );
1971 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1973 str_error = str_bad_cmdline;
1981 l = strtol( str3, &ptr, 0 );
1982 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1984 str_error = str_bad_cmdline;
1987 if ( l > 0 ) { max_games = (unsigned int)l; }
1992 l = strtol( str4, &ptr, 0 );
1993 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1995 str_error = str_bad_cmdline;
1998 if ( l > 0 ) { max_iterations = (int)l; }
2004 l = strtol( str5, &ptr, 0 );
2005 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
2007 str_error = str_bad_cmdline;
2015 l = strtol( str6, &ptr, 0 );
2016 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
2018 str_error = str_bad_cmdline;
2025 AbortDifficultCommand;
2027 log2_ntrans_table = 12;
2029 memory_free( (void *)ptrans_table_orig );
2031 iret = ini_trans_table();
2032 if ( iret < 0 ) { return iret; }
2034 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
2035 nworker1, nworker2 );
2036 if ( iret < 0 ) { return -1; }
2038 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2039 if ( iret < 0 ) { return -1; }
2041 iret = get_elapsed( &time_turn_start );
2042 if ( iret < 0 ) { return iret; }
2046 #endif /* MINIMUM */
2050 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
2052 const char *str = strtok_r( NULL, str_delimiters, lasts );
2058 str_error = str_bad_cmdline;
2061 else if ( ! strcmp( str, "num" ) )
2063 str = strtok_r( NULL, str_delimiters, lasts );
2066 str_error = str_bad_cmdline;
2069 l = strtol( str, &ptr, 0 );
2070 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
2072 str_error = str_bad_cmdline;
2076 AbortDifficultCommand;
2080 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
2084 else if ( ! strcmp( str, "width" ) )
2086 str = strtok_r( NULL, str_delimiters, lasts );
2089 str_error = str_bad_cmdline;
2092 l = strtol( str, &ptr, 0 );
2093 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
2095 str_error = str_bad_cmdline;
2099 AbortDifficultCommand;
2103 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
2108 str_error = str_bad_cmdline;
2115 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
2117 const char *str = strtok_r( NULL, str_delimiters, lasts );
2121 str_error = str_bad_cmdline;
2124 else if ( ! strcmp( str, "hash" ) )
2129 str = strtok_r( NULL, str_delimiters, lasts );
2132 str_error = str_bad_cmdline;
2135 l = strtol( str, &ptr, 0 );
2136 if ( ptr == str || l == LONG_MAX || l < 1 )
2138 str_error = str_bad_cmdline;
2142 AbortDifficultCommand;
2144 dfpn_hash_log2 = (unsigned int)l;
2145 return dfpn_ini_hash();
2147 else if ( ! strcmp( str, "go" ) )
2149 AbortDifficultCommand;
2151 return dfpn( ptree, root_turn, 1 );
2153 else if ( ! strcmp( str, "connect" ) )
2161 str = strtok_r( NULL, str_delimiters, lasts );
2162 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2163 strncpy( str_addr, str, 255 );
2164 str_addr[255] = '\0';
2166 str = strtok_r( NULL, str_delimiters, lasts );
2167 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2168 l = strtol( str, &ptr, 0 );
2169 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2171 str_error = str_bad_cmdline;
2176 str = strtok_r( NULL, str_delimiters, lasts );
2177 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2178 strncpy( str_id, str, 255 );
2181 AbortDifficultCommand;
2183 dfpn_sckt = sckt_connect( str_addr, port );
2184 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2186 str_buffer_cmdline[0] = '\0';
2187 DFPNOut( "Worker: %s\n", str_id );
2192 str_error = str_bad_cmdline;
2199 static int CONV cmd_thread( char **lasts )
2201 const char *str = strtok_r( NULL, str_delimiters, lasts );
2205 str_error = str_bad_cmdline;
2208 else if ( ! strcmp( str, "num" ) )
2213 str = strtok_r( NULL, str_delimiters, lasts );
2216 str_error = str_bad_cmdline;
2219 l = strtol( str, &ptr, 0 );
2220 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2222 str_error = str_bad_cmdline;
2230 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2237 str_error = str_bad_cmdline;
2243 #if defined(DFPN_CLIENT)
2244 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2250 AbortDifficultCommand;
2252 str = strtok_r( NULL, str_delimiters, lasts );
2253 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2254 strncpy( dfpn_client_str_addr, str, 255 );
2255 dfpn_client_str_addr[255] = '\0';
2257 str = strtok_r( NULL, str_delimiters, lasts );
2258 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2259 dfpn_client_port = strtol( str, &ptr, 0 );
2260 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2261 || dfpn_client_port > USHRT_MAX )
2263 str_error = str_bad_cmdline;
2267 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2269 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2270 if ( iret < 0 ) { return iret; }
2272 if ( dfpn_client_sckt == SCKT_NULL )
2274 str_error = "Check network status.";
2278 return get_elapsed( &time_turn_start );
2283 #if defined(CSA_LAN)
2284 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2290 str = strtok_r( NULL, str_delimiters, lasts );
2291 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2292 strncpy( client_str_addr, str, 255 );
2293 client_str_addr[255] = '\0';
2295 str = strtok_r( NULL, str_delimiters, lasts );
2296 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2297 client_port = strtol( str, &ptr, 0 );
2298 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2299 || client_port > USHRT_MAX )
2301 str_error = str_bad_cmdline;
2305 str = strtok_r( NULL, str_delimiters, lasts );
2306 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2307 strncpy( client_str_id, str, 255 );
2308 client_str_id[255] = '\0';
2310 str = strtok_r( NULL, " \t", lasts );
2311 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2312 strncpy( client_str_pwd, str, 255 );
2313 client_str_pwd[255] = '\0';
2315 str = strtok_r( NULL, str_delimiters, lasts );
2316 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2318 max_games = strtol( str, &ptr, 0 );
2319 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2321 str_error = str_bad_cmdline;
2324 client_max_game = max_games;
2327 AbortDifficultCommand;
2331 return client_next_game( ptree, client_str_addr, (int)client_port );
2335 static int CONV cmd_sendpv( char **lasts )
2337 const char *str = strtok_r( NULL, str_delimiters, lasts );
2341 str_error = str_bad_cmdline;
2345 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2346 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2348 str_error = str_bad_cmdline;
2357 #if defined(MNJ_LAN)
2358 /* mnj sd seed addr port name factor stable_depth */
2359 static int CONV cmd_mnj( char **lasts )
2361 char client_str_addr[256];
2362 char client_str_id[256];
2367 int client_port, sd;
2370 str = strtok_r( NULL, str_delimiters, lasts );
2373 str_error = str_bad_cmdline;
2376 l = strtol( str, &ptr, 0 );
2377 if ( ptr == str || l == LONG_MAX || l < 0 )
2379 str_error = str_bad_cmdline;
2385 str = strtok_r( NULL, str_delimiters, lasts );
2388 str_error = str_bad_cmdline;
2391 l = strtol( str, &ptr, 0 );
2392 if ( ptr == str || l == LONG_MAX || l < 0 )
2394 str_error = str_bad_cmdline;
2397 seed = (unsigned int)l;
2400 str = strtok_r( NULL, str_delimiters, lasts );
2401 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2402 strncpy( client_str_addr, str, 255 );
2403 client_str_addr[255] = '\0';
2406 str = strtok_r( NULL, str_delimiters, lasts );
2407 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2408 l = strtol( str, &ptr, 0 );
2409 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2411 str_error = str_bad_cmdline;
2414 client_port = (int)l;
2417 str = strtok_r( NULL, str_delimiters, lasts );
2418 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2419 strncpy( client_str_id, str, 255 );
2420 client_str_id[255] = '\0';
2422 str = strtok_r( NULL, str_delimiters, lasts );
2423 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2424 factor = strtod( str, &ptr );
2425 if ( ptr == str || factor < 0.0 )
2427 str_error = str_bad_cmdline;
2431 str = strtok_r( NULL, str_delimiters, lasts );
2432 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2434 l = strtol( str, &ptr, 0 );
2435 if ( ptr == str || l == LONG_MAX )
2437 str_error = str_bad_cmdline;
2441 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2442 else { mnj_depth_stable = (int)l; }
2444 AbortDifficultCommand;
2446 resign_threshold = 65535;
2447 game_status |= ( flag_noponder | flag_noprompt );
2448 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2450 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2451 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2453 str_buffer_cmdline[0] = '\0';
2455 Out( "Sending my name %s", client_str_id );
2456 MnjOut( "%s %g final%s\n", client_str_id, factor,
2457 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2459 return cmd_suspend();