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; }
113 procedure( tree_t * restrict ptree )
116 if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
120 if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
124 if ( usi_mode != usi_off ) { return proce_usi( ptree ); }
127 return proce_cui( ptree );
130 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
131 int move_list[1024], move_ptr;
133 int all_moves[MAX_LEGAL_MOVES];
136 #define IF(X) else if(!strcmp(command, X))
138 int myTime, hisTime, movesPerSession, inc, plyNr;
140 int root_pos[nsquare];
143 bonanza_piece( char p )
146 if(p >= 'a') p += 'A' - 'a';
147 switch(p) { // encode piece
148 case 'P': piece = pawn; break;
149 case 'L': piece = lance; break;
150 case 'N': piece = knight; break;
151 case 'S': piece = silver; break;
152 case 'G': piece = gold; break;
153 case 'B': piece = bishop; break;
154 case 'R': piece = rook; break;
155 case 'K': piece = king; break;
163 static char fen[128];
165 char *q = fen, *start = p;
166 strncpy( fen, p+9 , 127);
167 strcpy( p, "new SU" ); p += 6;
168 for ( r = 1; r <= 9; r++) {
169 for ( f = 0; *q; q++ ) {
171 if( *q == '+' ) promoted = promote, q++;
172 if( isdigit( *q ) ) f += atoi( q ); else
173 if( isalpha( *q ) ) {
174 int piece = bonanza_piece( *q );
175 if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
176 gote = !gote; // switch color
177 *p++ = ( gote ? '-' : '+' );
179 sprintf( p, "%d%d%s", 9-f, r, astr_table_piece[piece + promoted] );
181 } else { q++; break; }
184 while( q[-1] == ' ' ) q++;
185 if( q[-1] == '[' ) { // holdings
186 while( isalpha( *q ) ) {
187 int piece = bonanza_piece( *q );
188 if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
189 gote = !gote; // switch color
190 *p++ = ( gote ? '-' : '+' );
192 sprintf( p, "00%s", astr_table_piece[piece] );
197 while( *q == ' ' ) q++;
199 sprintf(p, " %c", *q == 'w' ? '+' : '-' ); // side to move
203 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
205 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
207 if(fromY == '@') { // drop (contains all info needed to convert it)
208 piece = bonanza_piece( fromX );
209 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
210 } else { // board move (need to figure out moved piece)
211 int from = ('9' - fromY)*9 + (fromX - 'a');
212 int flag = (promo == '+' ? FLAG_PROMO : 0);
213 piece = abs( BOARD[from] ); // this only works when no search in progress!
214 printf("# piece from board: %d\n", piece);fflush(stdout);
215 if( game_status & flag_thinking ) {
216 int i, to = ('9' - toY)*9 + (toX - 'a');
217 piece = 0; // kludge to force illegal CSA move
218 for( i = 0; i < root_nmove; i++ ) { // determine the piece from the move list
219 int move = root_move_list[i].move;
220 if( I2To(move) != to ) continue;
221 if( I2From(move) != from ) continue;
222 if( (move & FLAG_PROMO) != flag ) continue;
223 piece = I2PieceMove( move ); // we found the move; take the piece from it
226 } else if( game_status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
227 printf("# piece corrected to %d\n", piece);fflush(stdout);
228 if( promo == '+') piece += promote;
229 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
234 update_exclude_list( tree_t * restrict ptree, int add, unsigned int move )
235 { // [HGM] exclude: manage list of excluded moves
237 if( moves_ignore[0] == MOVE_NA ) { // nothing is excluded yet; make a copy of root move list;
238 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
239 all_moves[i] = MOVE_NA;
241 if ( move == MOVE_NA ) { // all moves
242 if( add ) { // copy entire list of legal moves
243 for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
244 } else moves_ignore[0] = MOVE_NA; // clear list
245 } else { // single move
246 for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
247 if( add ) { // we must add the move
248 if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
249 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
250 moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
251 } else { // we must delete the move
252 if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
253 while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
260 { // set white and black times from own and opponent time.
262 if(movesPerSession <= 0) moves = 35; else {
264 while(moves <= 0) moves += movesPerSession;
266 time_limit = (myTime-inc-30)/(moves+2) + inc;
267 time_max_limit = 3*time_limit;
268 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
269 time_limit *= 10; // msec
270 time_max_limit *= 10;
271 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
275 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
276 { // [HGM] added to make Bonanza.exe a native WinBoard engine
278 static char forceMode = 0;
279 sscanf(line + strlen(command) + 1, "%d", &value);
280 Out("# command = '%s'\n", line);
284 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
285 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
287 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
288 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
290 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
291 IF("easy") { strcpy(line, "ponder off"); return 0; }
292 IF("hard") { strcpy(line, "ponder on"); return 0; }
295 IF("time") { sscanf(line+5, "%d", &myTime); }
296 IF("otim") { sscanf(line+5, "%d", &hisTime); }
297 IF("force") { forceMode = 1; }
298 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
299 IF("memory") { sprintf(line, "hash %d", value); return 0; }
300 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
301 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
303 IF("quit") { return 0; }
304 IF("analyze") { return 0; }
305 IF("exit") { return 0; }
306 IF("variant") { /* ignore, since it must be Shogi */; }
307 IF("setboard") { forceMode = 1; plyNr = 0; read_fen( line ); return 0; }
309 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
310 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
312 IF("level") { int min, sec; float fsec=0.;
313 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
314 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
315 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
317 IF("usermove") { char buf[20];
318 xboard_to_CSA( ptree, line+9, buf );
319 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
324 IF("undo") { return 0; }
326 IF("ping") { Out("pong %d\n", value); }
327 IF("exclude") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
328 IF("include") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
333 static int CONV proce_cui( tree_t * restrict ptree )
338 token = strtok_r( str_cmdline, str_delimiters, &last );
340 if ( token == NULL || *token == '#' ) { return 1; }
344 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
345 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
346 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
348 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
351 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
352 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
353 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
354 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
355 if ( ! strcmp( token, "include" ) ) { return cmd_inex( ptree , 0, &last); } // [HGM] exclude
356 if ( ! strcmp( token, "exclude" ) ) { return cmd_inex( ptree , 1, &last); } // [HGM] exclude
358 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
360 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
361 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
362 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
363 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
364 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
365 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
366 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
367 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
368 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
369 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
370 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
371 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
372 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
373 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
374 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
375 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
376 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
377 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
378 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
379 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
380 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
382 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
383 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
386 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
389 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
391 #if defined(DFPN_CLIENT)
392 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
396 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
398 #if ! defined(NO_STDOUT)
399 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
401 #if ! defined(MINIMUM)
402 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
405 str_error = str_bad_cmdline;
411 static int CONV proce_csalan( tree_t * restrict ptree )
416 token = strtok_r( str_cmdline, str_delimiters, &last );
418 if ( token == NULL ) { return 1; }
419 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
424 token = strtok_r( NULL, str_delimiters, &last );
425 if ( token == NULL || *token != 'T' )
427 str_error = str_bad_cmdline;
431 l = strtol( token+1, &ptr, 0 );
432 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
434 str_error = str_bad_cmdline;
438 adjust_time( (unsigned int)l, client_turn );
439 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
442 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
444 return cmd_usrmove( ptree, token+1, &last );
446 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
447 if ( ! strcmp( token, "#WIN" )
448 || ! strcmp( token, "#LOSE" )
449 || ! strcmp( token, "#DRAW" )
450 || ! strcmp( token, "#CHUDAN" ) )
452 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
454 game_status |= flag_suspend;
458 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
459 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
463 if ( client_ngame == client_max_game ) { return cmd_quit(); }
465 return client_next_game( ptree, client_str_addr, (int)client_port );
474 static int CONV proce_mnj( tree_t * restrict ptree )
480 token = strtok_r( str_cmdline, str_delimiters, &last );
481 if ( token == NULL ) { return 1; }
483 if ( ! strcmp( token, "new" ) )
485 iret = cmd_suspend();
486 if ( iret != 1 ) { return iret; }
489 iret = cmd_new( ptree, &last );
490 if ( iret < 0 ) { return iret; }
492 moves_ignore[0] = MOVE_NA;
493 return analyze( ptree );
495 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
496 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
497 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
498 if ( ! strcmp( token, "retract" ) )
502 const char *str = strtok_r( NULL, str_delimiters, &last );
505 str_error = str_bad_cmdline;
508 l = strtol( str, &ptr, 0 );
509 if ( ptr == str || (long)NUM_UNMAKE < l )
511 str_error = str_bad_cmdline;
515 return cmd_mnjmove( ptree, &last, (int)l );
517 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
519 str_error = str_bad_cmdline;
525 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
534 token = strtok_r( NULL, str_delimiters, lasts );
537 str_error = str_bad_cmdline;
540 lid = strtol( token, &ptr, 0 );
541 if ( ptr == token || lid == LONG_MAX || lid < 1 )
543 str_error = str_bad_cmdline;
547 AbortDifficultCommand;
549 for ( i = 0; ; i += 1 )
551 token = strtok_r( NULL, str_delimiters, lasts );
552 if ( token == NULL ) { break; }
554 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
556 moves_ignore[i] = move;
560 str_error = str_bad_cmdline;
563 mnj_posi_id = (int)lid;
564 moves_ignore[i] = MOVE_NA;
566 return analyze( ptree );
571 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
573 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
574 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
580 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
582 str_error = str_bad_cmdline;
586 lid = strtol( str2, &ptr, 0 );
587 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
589 str_error = str_bad_cmdline;
593 AbortDifficultCommand;
597 iret = unmake_move_root( ptree );
598 if ( iret < 0 ) { return iret; }
603 iret = interpret_CSA_move( ptree, &move, str1 );
604 if ( iret < 0 ) { return iret; }
606 iret = get_elapsed( &time_turn_start );
607 if ( iret < 0 ) { return iret; }
609 mnj_posi_id = (int)lid;
611 iret = make_move_root( ptree, move, ( flag_time | flag_rep
612 | flag_detect_hang ) );
613 if ( iret < 0 ) { return iret; }
615 # if ! defined(NO_STDOUT)
616 iret = out_board( ptree, stdout, 0, 0 );
617 if ( iret < 0 ) { return iret; }
620 moves_ignore[0] = MOVE_NA;
621 return analyze( ptree );
627 do_analyze( tree_t * restrict ptree )
628 { // [HGM] analyze: do a ponder search on the current position
630 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
631 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
633 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
634 for( iret = 0; iret < nsquare; iret++ ) root_pos[iret] = BOARD[iret];
636 game_status |= flag_pondering;
637 iret = iterate( ptree );
638 game_status &= ~flag_pondering;
644 cmd_undo( tree_t * restrict ptree )
645 { // [HGM] undo: restart the game, and feed all moves except the last
646 int i, last = move_ptr;
647 char *p = start_data;
648 if( move_ptr <= 0 ) {
649 str_error = "undo past start of game ignored";
653 AbortDifficultCommand;
656 cmd_new( ptree, &p );
657 for(i=0; i<last; i++) {
658 make_move_root( ptree, move_list[i], 0);
661 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
662 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
669 cmd_analyze( tree_t * restrict ptree )
670 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
671 AbortDifficultCommand;
674 return do_analyze( ptree );
679 cmd_inex( tree_t * restrict ptree, int inex, char **last )
680 { // [HGM] analyze: switch off analysis mode
681 const char *str = strtok_r( NULL, str_delimiters, last );
685 AbortDifficultCommand;
687 if( ! strcmp( str, "all" ) ) {
690 iret = interpret_CSA_move( ptree, &move, str );
691 if ( iret < 0 ) { return iret; }
694 update_exclude_list( ptree, inex, move );
696 if ( ! analyze_mode ) return 1;
697 return do_analyze( ptree );
703 { // [HGM] analyze: switch off analysis mode
704 if ( !analyze_mode ) {
705 str_error = "was not analyzing";
709 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
711 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
718 static int CONV proce_usi( tree_t * restrict ptree )
724 token = strtok_r( str_cmdline, str_delimiters, &lasts );
725 if ( token == NULL ) { return 1; }
727 if ( ! strcmp( token, "usi" ) )
729 USIOut( "id name %s\n", str_myname );
730 USIOut( "id author Kunihito Hoki\n" );
735 if ( ! strcmp( token, "isready" ) )
737 USIOut( "readyok\n", str_myname );
741 if ( ! strcmp( token, "echo" ) )
743 USIOut( "%s\n", lasts );
747 if ( ! strcmp( token, "ignore_moves" ) )
749 return usi_ignore( ptree, &lasts );
752 if ( ! strcmp( token, "genmove_probability" ) )
754 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
755 return usi_root_list( ptree );
758 if ( ! strcmp( token, "go" ) )
760 iret = usi_go( ptree, &lasts );
761 moves_ignore[0] = MOVE_NA;
765 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
766 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
767 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
769 str_error = str_bad_cmdline;
775 usi_ignore( tree_t * restrict ptree, char **lasts )
782 AbortDifficultCommand;
784 for ( i = 0; ; i += 1 )
786 token = strtok_r( NULL, str_delimiters, lasts );
787 if ( token == NULL ) { break; }
789 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
790 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
792 moves_ignore[i] = move;
795 moves_ignore[i] = MOVE_NA;
802 usi_go( tree_t * restrict ptree, char **lasts )
809 AbortDifficultCommand;
811 if ( game_status & mask_game_end )
813 str_error = str_game_ended;
817 token = strtok_r( NULL, str_delimiters, lasts );
819 if ( ! strcmp( token, "book" ) )
821 AbortDifficultCommand;
822 if ( usi_book( ptree ) < 0 ) { return -1; }
828 if ( ! strcmp( token, "infinite" ) )
830 usi_byoyomi = UINT_MAX;
831 depth_limit = PLY_MAX;
832 node_limit = UINT64_MAX;
833 sec_limit_depth = UINT_MAX;
835 else if ( ! strcmp( token, "byoyomi" ) )
837 token = strtok_r( NULL, str_delimiters, lasts );
840 str_error = str_bad_cmdline;
844 l = strtol( token, &ptr, 0 );
845 if ( ptr == token || l > UINT_MAX || l < 1 )
847 str_error = str_bad_cmdline;
851 usi_byoyomi = (unsigned int)l;
852 depth_limit = PLY_MAX;
853 node_limit = UINT64_MAX;
854 sec_limit_depth = UINT_MAX;
857 str_error = str_bad_cmdline;
862 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
864 iret = com_turn_start( ptree, 0 );
866 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
875 usi_posi( tree_t * restrict ptree, char **lasts )
881 AbortDifficultCommand;
883 moves_ignore[0] = MOVE_NA;
885 token = strtok_r( NULL, str_delimiters, lasts );
886 if ( strcmp( token, "startpos" ) )
888 str_error = str_bad_cmdline;
892 if ( ini_game( ptree, &min_posi_no_handicap,
893 flag_history, NULL, NULL ) < 0 ) { return -1; }
895 token = strtok_r( NULL, str_delimiters, lasts );
896 if ( token == NULL ) { return 1; }
898 if ( strcmp( token, "moves" ) )
900 str_error = str_bad_cmdline;
906 token = strtok_r( NULL, str_delimiters, lasts );
907 if ( token == NULL ) { break; }
909 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
910 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
911 if ( make_move_root( ptree, move, ( flag_history | flag_time
913 | flag_detect_hang ) ) < 0 )
919 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
926 static int CONV cmd_move_now( void )
928 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
935 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
943 if ( game_status & mask_game_end )
945 str_error = str_game_ended;
949 if ( game_status & flag_thinking )
951 str_error = str_busy_think;
955 str = strtok_r( NULL, str_delimiters, lasts );
956 if ( str == NULL ) { lelapsed = 0; }
960 str_error = str_bad_cmdline;
964 lelapsed = strtol( str, &ptr, 0 );
965 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
967 str_error = str_bad_cmdline;
972 if ( game_status & ( flag_pondering | flag_puzzling ) )
976 for ( i = 0; i < ponder_nmove; i++ )
978 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
983 if ( i == ponder_nmove )
986 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
989 #if defined(CSASHOGI)
990 AbortDifficultCommand;
992 str_error = str_illegal_move;
997 if ( ( game_status & flag_puzzling )
998 || strcmp( str_move, str_CSA_move(ponder_move) ) )
1000 ponder_move = MOVE_PONDER_FAILED;
1001 game_status |= flag_quit_ponder;
1005 iret = update_time( Flip(root_turn) );
1006 if ( iret < 0 ) { return iret; }
1009 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
1012 out_CSA( ptree, &record_game, ponder_move );
1014 game_status &= ~flag_pondering;
1015 game_status |= flag_thinking;
1019 set_search_limit_time( root_turn );
1021 OutCsaShogi( "info ponder end\n" );
1023 str = str_time_symple( time_turn_start - time_start );
1024 Out( " %6s MOVE PREDICTION HIT\n"
1025 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
1030 iret = interpret_CSA_move( ptree, &move, str_move );
1031 if ( iret < 0 ) { return iret; }
1032 move_evasion_pchk = 0;
1033 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
1034 | flag_detect_hang ) );
1038 #if defined(CSA_LAN)
1039 if ( sckt_csa != SCKT_NULL )
1041 if ( move_evasion_pchk )
1043 str = str_CSA_move( move_evasion_pchk );
1044 iret = sckt_out( sckt_csa, "%c%s\n",
1045 ach_turn[Flip(root_turn)], str );
1046 if ( iret < 0 ) { return iret; }
1048 return cmd_suspend();
1052 if ( move_evasion_pchk )
1054 str = str_CSA_move( move_evasion_pchk );
1055 #if defined(CSASHOGI)
1056 OutCsaShogi( "move%s\n", str );
1057 return cmd_suspend();
1059 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
1060 ach_turn[Flip(root_turn)], str );
1061 str_error = str_message;
1069 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
1070 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
1072 #if defined(CSA_LAN)
1073 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
1075 iret = sckt_out( sckt_csa, "%%TORYO\n" );
1076 if ( iret < 0 ) { return iret; }
1080 if ( ! ( game_status & mask_game_end ) )
1082 iret = com_turn_start( ptree, 0 );
1083 if ( iret < 0 ) { return iret; }
1090 static int CONV cmd_beep( char **lasts )
1092 const char *str = strtok_r( NULL, str_delimiters, lasts );
1095 str_error = str_bad_cmdline;
1099 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
1100 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
1102 str_error = str_bad_cmdline;
1110 static int CONV cmd_peek( char **lasts )
1112 const char *str = strtok_r( NULL, str_delimiters, lasts );
1116 str_error = str_bad_cmdline;
1120 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
1121 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
1123 str_error = str_bad_cmdline;
1131 static int CONV cmd_stdout( char **lasts )
1133 const char *str = strtok_r( NULL, str_delimiters, lasts );
1137 str_error = str_bad_cmdline;
1141 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1142 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1144 str_error = str_bad_cmdline;
1152 static int CONV cmd_newlog( char **lasts )
1154 const char *str = strtok_r( NULL, str_delimiters, lasts );
1158 str_error = str_bad_cmdline;
1162 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1163 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1165 str_error = str_bad_cmdline;
1173 static int CONV cmd_ponder( char **lasts )
1175 const char *str = strtok_r( NULL, str_delimiters, lasts );
1179 str_error = str_bad_cmdline;
1183 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1184 else if ( ! strcmp( str, str_off ) )
1186 if ( game_status & ( flag_pondering | flag_puzzling ) )
1188 game_status |= flag_quit_ponder;
1190 game_status |= flag_noponder;
1193 str_error = str_bad_cmdline;
1201 #if ! defined(NO_STDOUT)
1202 static int CONV cmd_stress( char **lasts )
1204 const char *str = strtok_r( NULL, str_delimiters, lasts );
1208 str_error = str_bad_cmdline;
1212 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1213 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1215 str_error = str_bad_cmdline;
1225 #if defined(MINIMUM)
1226 cmd_book( char **lasts )
1228 cmd_book( tree_t * restrict ptree, char **lasts )
1231 const char *str = strtok_r( NULL, str_delimiters, lasts );
1236 str_error = str_bad_cmdline;
1239 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1240 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1241 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1242 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1243 #if ! defined(MINIMUM)
1244 else if ( ! strcmp( str, "create" ) )
1246 AbortDifficultCommand;
1248 iret = book_create( ptree );
1249 if ( iret < 0 ) { return iret; }
1251 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1253 if ( iret < 0 ) { return iret; }
1255 iret = get_elapsed( &time_turn_start );
1259 str_error = str_bad_cmdline;
1267 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1269 const char *str = strtok_r( NULL, str_delimiters, lasts );
1276 l = strtol( str, &ptr, 0 );
1279 str_error = str_bad_cmdline;
1282 if ( l == 1 ) { game_status &= ~flag_reverse; }
1283 else if ( l == 2 ) { game_status |= flag_reverse; }
1285 str_error = str_bad_cmdline;
1291 iret = out_board( ptree, stdout, 0, 0 );
1292 if ( iret < 0 ) { return iret; }
1293 #if ! defined(NO_LOGGING)
1294 iret = out_board( ptree, pf_log, 0, 0 );
1295 if ( iret < 0 ) { return iret; }
1303 static int CONV cmd_ping( void )
1305 OutCsaShogi( "pong\n" );
1311 static int CONV cmd_hash( char **lasts )
1313 const char *str = strtok_r( NULL, str_delimiters, lasts );
1319 str_error = str_bad_cmdline;
1323 l = strtol( str, &ptr, 0 );
1324 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1326 str_error = str_bad_cmdline;
1330 AbortDifficultCommand;
1332 log2_ntrans_table = (int)l;
1333 memory_free( (void *)ptrans_table_orig );
1334 return ini_trans_table();
1338 static int CONV cmd_limit( char **lasts )
1340 const char *str = strtok_r( NULL, str_delimiters, lasts );
1346 str_error = str_bad_cmdline;
1350 AbortDifficultCommand;
1352 if ( ! strcmp( str, "depth" ) )
1354 str = strtok_r( NULL, str_delimiters, lasts );
1357 str_error = str_bad_cmdline;
1360 l1 = strtol( str, &ptr, 0 );
1361 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1363 str_error = str_bad_cmdline;
1366 sec_limit_up = UINT_MAX;
1367 node_limit = UINT64_MAX;
1368 depth_limit = (int)l1;
1370 else if ( ! strcmp( str, "nodes" ) )
1372 str = strtok_r( NULL, str_delimiters, lasts );
1375 str_error = str_bad_cmdline;
1378 l1 = strtol( str, &ptr, 0 );
1379 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1381 str_error = str_bad_cmdline;
1384 sec_limit_up = UINT_MAX;
1385 depth_limit = PLY_MAX;
1386 node_limit = (uint64_t)l1;
1388 else if ( ! strcmp( str, "time" ) )
1390 str = strtok_r( NULL, str_delimiters, lasts );
1393 str_error = str_bad_cmdline;
1397 if ( ! strcmp( str, "extendable" ) )
1399 game_status |= flag_time_extendable;
1401 else if ( ! strcmp( str, "strict" ) )
1403 game_status &= ~flag_time_extendable;
1406 l1 = strtol( str, &ptr, 0 );
1407 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1409 str_error = str_bad_cmdline;
1413 str = strtok_r( NULL, str_delimiters, lasts );
1416 str_error = str_bad_cmdline;
1419 l2 = strtol( str, &ptr, 0 );
1420 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1422 str_error = str_bad_cmdline;
1426 str = strtok_r( NULL, str_delimiters, lasts );
1427 if ( ! str ) { l3 = -1; }
1429 l3 = strtol( str, &ptr, 0 );
1430 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1432 str_error = str_bad_cmdline;
1437 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1439 depth_limit = PLY_MAX;
1440 node_limit = UINT64_MAX;
1441 sec_limit = (unsigned int)l1 * 60U;
1442 sec_limit_up = (unsigned int)l2;
1443 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1444 else { sec_limit_depth = (unsigned int)l3; }
1448 str_error = str_bad_cmdline;
1457 cmd_read( tree_t * restrict ptree, char **lasts )
1459 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1460 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1461 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1462 const char *str_tmp;
1463 FILE *pf_src, *pf_dest;
1464 char str_file[SIZE_FILENAME];
1470 flag = flag_history | flag_rep | flag_detect_hang;
1476 str_error = str_bad_cmdline;
1482 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1483 else if ( strcmp( str2, "nil" ) )
1485 str_error = str_bad_cmdline;
1492 l = strtol( str3, &ptr, 0 );
1493 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1495 str_error = str_bad_cmdline;
1498 moves = (unsigned int)l - 1U;
1501 AbortDifficultCommand;
1503 if ( ! strcmp( str1, "." ) )
1505 str_tmp = "game.cs_";
1507 #if defined(NO_LOGGING)
1508 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1510 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1511 str_dir_logs, record_num );
1513 pf_dest = file_open( str_tmp, "w" );
1514 if ( pf_dest == NULL ) { return -2; }
1516 pf_src = file_open( str_file, "r" );
1517 if ( pf_src == NULL )
1519 file_close( pf_dest );
1523 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1525 iret = file_close( pf_src );
1528 file_close( pf_dest );
1532 iret = file_close( pf_dest );
1533 if ( iret < 0 ) { return iret; }
1539 iret = read_record( ptree, str1, moves, flag );
1540 if ( iret < 0 ) { return iret; }
1542 iret = get_elapsed( &time_turn_start );
1543 if ( iret < 0 ) { return iret; }
1545 if ( str_tmp && remove( str_tmp ) )
1547 out_warning( "remove() failed." );
1555 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1557 const char *str = strtok_r( NULL, str_delimiters, lasts );
1561 if ( str == NULL || *str == 'T' )
1563 AbortDifficultCommand;
1565 if ( game_status & mask_game_end ) { return 1; }
1567 game_status |= flag_resigned;
1568 update_time( root_turn );
1569 out_CSA( ptree, &record_game, MOVE_RESIGN );
1572 l = strtol( str, &ptr, 0 );
1573 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1575 str_error = str_bad_cmdline;
1578 resign_threshold = (int)l;
1585 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1587 const char *str = strtok_r( NULL, str_delimiters, lasts );
1593 if ( game_status & mask_game_end )
1595 str_error = str_game_ended;
1599 AbortDifficultCommand;
1603 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1605 str_error = str_bad_cmdline;
1609 iret = get_elapsed( &time_turn_start );
1610 if ( iret < 0 ) { return iret; }
1612 return com_turn_start( ptree, 0 );
1615 l = strtol( str, &ptr, 0 );
1616 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1618 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1620 str_error = str_bad_cmdline;
1624 for ( i = 0; i < l; i += 1 )
1626 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1628 iret = get_elapsed( &time_turn_start );
1629 if ( iret < 0 ) { return iret; }
1631 iret = com_turn_start( ptree, 0 );
1632 if ( iret < 0 ) { return iret; }
1639 iret = interpret_CSA_move( ptree, &move, str );
1640 if ( iret < 0 ) { return iret; }
1642 iret = get_elapsed( &time_turn_start );
1643 if ( iret < 0 ) { return iret; }
1645 iret = make_move_root( ptree, move,
1646 ( flag_history | flag_time | flag_rep
1647 | flag_detect_hang ) );
1648 if ( iret < 0 ) { return iret; }
1650 str = strtok_r( NULL, str_delimiters, lasts );
1652 } while ( str != NULL );
1654 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1655 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1661 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1663 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1664 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1665 const min_posi_t *pmp;
1666 min_posi_t min_posi;
1669 AbortDifficultCommand;
1671 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1672 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1676 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1677 memset( &min_posi.asquare, empty, nsquare );
1678 min_posi.hand_black = min_posi.hand_white = 0;
1679 iret = read_board_rep1( str1, &min_posi );
1680 if ( iret < 0 ) { return iret; }
1684 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1685 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1687 str_error = str_bad_cmdline;
1691 else { min_posi.turn_to_move = black; }
1695 else { pmp = &min_posi_no_handicap; }
1697 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1698 if ( iret < 0 ) { return iret; }
1700 return get_elapsed( &time_turn_start );
1704 static int CONV cmd_outmove( tree_t * restrict ptree )
1706 const char *str_move;
1708 unsigned int move_list[ MAX_LEGAL_MOVES ];
1711 AbortDifficultCommand;
1713 if ( game_status & mask_game_end )
1715 Out( "NO LEGAL MOVE\n" );
1716 DFPNOut( "NO LEGAL MOVE\n" );
1720 n = gen_legal_moves( ptree, move_list, 0 );
1723 for ( c = i = 0; i < n; i += 1 )
1725 str_move = str_CSA_move(move_list[i]);
1727 if ( i && ( i % 10 ) == 0 )
1729 Out( "%s\n", buffer );
1730 DFPNOut( "%s ", buffer );
1731 memcpy( buffer, str_move, 6 );
1737 memcpy( buffer + c + 1, str_move, 6 );
1741 memcpy( buffer + c, str_move, 6 );
1746 Out( "%s\n", buffer );
1747 DFPNOut( "%s\n", buffer );
1753 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1755 const char *str = strtok_r( NULL, str_delimiters, lasts );
1758 unsigned int nposition;
1764 AbortDifficultCommand;
1769 if ( str != NULL && ! strcmp( str, "mate" ) )
1772 str = strtok_r( NULL, str_delimiters, lasts );
1778 l = strtol( str, &ptr, 0 );
1779 if ( ptr == str || l == LONG_MAX || l < 1 )
1781 str_error = str_bad_cmdline;
1784 nposition = (unsigned int)l;
1786 else { nposition = UINT_MAX; }
1789 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1790 if ( iret < 0 ) { return iret; }
1793 iret = is_mate ? solve_mate_problems( ptree, nposition )
1794 : solve_problems( ptree, nposition );
1796 iret = solve_problems( ptree, nposition );
1801 record_close( &record_problems );
1805 iret = record_close( &record_problems );
1806 if ( iret < 0 ) { return iret; }
1808 return get_elapsed( &time_turn_start );
1812 static int CONV cmd_quit( void )
1814 game_status |= flag_quit;
1819 static int CONV cmd_suspend( void )
1821 if ( game_status & ( flag_pondering | flag_puzzling ) )
1823 game_status |= flag_quit_ponder;
1827 game_status |= flag_suspend;
1832 static int CONV cmd_time( char **lasts )
1834 const char *str = strtok_r( NULL, str_delimiters, lasts );
1839 str_error = str_bad_cmdline;
1842 else if ( ! strcmp( str, "response" ) )
1845 str = strtok_r( NULL, str_delimiters, lasts );
1848 str_error = str_bad_cmdline;
1851 l = strtol( str, &ptr, 0 );
1852 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1854 str_error = str_bad_cmdline;
1857 time_response = (unsigned int)l;
1860 else if ( ! strcmp( str, "remain" ) )
1864 str = strtok_r( NULL, str_delimiters, lasts );
1867 str_error = str_bad_cmdline;
1870 l1 = strtol( str, &ptr, 0 );
1871 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1873 str_error = str_bad_cmdline;
1877 str = strtok_r( NULL, str_delimiters, lasts );
1880 str_error = str_bad_cmdline;
1883 l2 = strtol( str, &ptr, 0 );
1884 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1886 str_error = str_bad_cmdline;
1890 if ( sec_limit_up == UINT_MAX )
1892 str_error = str_bad_cmdline;
1896 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1899 str_error = str_bad_cmdline;
1904 #if !defined(MINIMUM)
1905 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1906 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1908 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1909 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1910 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1911 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1913 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1914 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1918 unsigned int max_games;
1919 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1923 str_error = str_bad_cmdline;
1926 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1927 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1929 str_error = str_bad_cmdline;
1933 max_games = UINT_MAX;
1934 max_iterations = INT_MAX;
1935 nworker1 = nworker2 = nsteps = 1;
1939 l = strtol( str2, &ptr, 0 );
1940 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1942 str_error = str_bad_cmdline;
1950 l = strtol( str3, &ptr, 0 );
1951 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1953 str_error = str_bad_cmdline;
1956 if ( l > 0 ) { max_games = (unsigned int)l; }
1961 l = strtol( str4, &ptr, 0 );
1962 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1964 str_error = str_bad_cmdline;
1967 if ( l > 0 ) { max_iterations = (int)l; }
1973 l = strtol( str5, &ptr, 0 );
1974 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1976 str_error = str_bad_cmdline;
1984 l = strtol( str6, &ptr, 0 );
1985 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1987 str_error = str_bad_cmdline;
1994 AbortDifficultCommand;
1996 log2_ntrans_table = 12;
1998 memory_free( (void *)ptrans_table_orig );
2000 iret = ini_trans_table();
2001 if ( iret < 0 ) { return iret; }
2003 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
2004 nworker1, nworker2 );
2005 if ( iret < 0 ) { return -1; }
2007 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2008 if ( iret < 0 ) { return -1; }
2010 iret = get_elapsed( &time_turn_start );
2011 if ( iret < 0 ) { return iret; }
2015 #endif /* MINIMUM */
2019 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
2021 const char *str = strtok_r( NULL, str_delimiters, lasts );
2027 str_error = str_bad_cmdline;
2030 else if ( ! strcmp( str, "num" ) )
2032 str = strtok_r( NULL, str_delimiters, lasts );
2035 str_error = str_bad_cmdline;
2038 l = strtol( str, &ptr, 0 );
2039 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
2041 str_error = str_bad_cmdline;
2045 AbortDifficultCommand;
2049 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
2053 else if ( ! strcmp( str, "width" ) )
2055 str = strtok_r( NULL, str_delimiters, lasts );
2058 str_error = str_bad_cmdline;
2061 l = strtol( str, &ptr, 0 );
2062 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
2064 str_error = str_bad_cmdline;
2068 AbortDifficultCommand;
2072 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
2077 str_error = str_bad_cmdline;
2084 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
2086 const char *str = strtok_r( NULL, str_delimiters, lasts );
2090 str_error = str_bad_cmdline;
2093 else if ( ! strcmp( str, "hash" ) )
2098 str = strtok_r( NULL, str_delimiters, lasts );
2101 str_error = str_bad_cmdline;
2104 l = strtol( str, &ptr, 0 );
2105 if ( ptr == str || l == LONG_MAX || l < 1 )
2107 str_error = str_bad_cmdline;
2111 AbortDifficultCommand;
2113 dfpn_hash_log2 = (unsigned int)l;
2114 return dfpn_ini_hash();
2116 else if ( ! strcmp( str, "go" ) )
2118 AbortDifficultCommand;
2120 return dfpn( ptree, root_turn, 1 );
2122 else if ( ! strcmp( str, "connect" ) )
2130 str = strtok_r( NULL, str_delimiters, lasts );
2131 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2132 strncpy( str_addr, str, 255 );
2133 str_addr[255] = '\0';
2135 str = strtok_r( NULL, str_delimiters, lasts );
2136 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2137 l = strtol( str, &ptr, 0 );
2138 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2140 str_error = str_bad_cmdline;
2145 str = strtok_r( NULL, str_delimiters, lasts );
2146 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2147 strncpy( str_id, str, 255 );
2150 AbortDifficultCommand;
2152 dfpn_sckt = sckt_connect( str_addr, port );
2153 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2155 str_buffer_cmdline[0] = '\0';
2156 DFPNOut( "Worker: %s\n", str_id );
2161 str_error = str_bad_cmdline;
2168 static int CONV cmd_thread( char **lasts )
2170 const char *str = strtok_r( NULL, str_delimiters, lasts );
2174 str_error = str_bad_cmdline;
2177 else if ( ! strcmp( str, "num" ) )
2182 str = strtok_r( NULL, str_delimiters, lasts );
2185 str_error = str_bad_cmdline;
2188 l = strtol( str, &ptr, 0 );
2189 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2191 str_error = str_bad_cmdline;
2199 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2206 str_error = str_bad_cmdline;
2212 #if defined(DFPN_CLIENT)
2213 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2219 AbortDifficultCommand;
2221 str = strtok_r( NULL, str_delimiters, lasts );
2222 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2223 strncpy( dfpn_client_str_addr, str, 255 );
2224 dfpn_client_str_addr[255] = '\0';
2226 str = strtok_r( NULL, str_delimiters, lasts );
2227 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2228 dfpn_client_port = strtol( str, &ptr, 0 );
2229 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2230 || dfpn_client_port > USHRT_MAX )
2232 str_error = str_bad_cmdline;
2236 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2238 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2239 if ( iret < 0 ) { return iret; }
2241 if ( dfpn_client_sckt == SCKT_NULL )
2243 str_error = "Check network status.";
2247 return get_elapsed( &time_turn_start );
2252 #if defined(CSA_LAN)
2253 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2259 str = strtok_r( NULL, str_delimiters, lasts );
2260 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2261 strncpy( client_str_addr, str, 255 );
2262 client_str_addr[255] = '\0';
2264 str = strtok_r( NULL, str_delimiters, lasts );
2265 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2266 client_port = strtol( str, &ptr, 0 );
2267 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2268 || client_port > USHRT_MAX )
2270 str_error = str_bad_cmdline;
2274 str = strtok_r( NULL, str_delimiters, lasts );
2275 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2276 strncpy( client_str_id, str, 255 );
2277 client_str_id[255] = '\0';
2279 str = strtok_r( NULL, " \t", lasts );
2280 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2281 strncpy( client_str_pwd, str, 255 );
2282 client_str_pwd[255] = '\0';
2284 str = strtok_r( NULL, str_delimiters, lasts );
2285 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2287 max_games = strtol( str, &ptr, 0 );
2288 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2290 str_error = str_bad_cmdline;
2293 client_max_game = max_games;
2296 AbortDifficultCommand;
2300 return client_next_game( ptree, client_str_addr, (int)client_port );
2304 static int CONV cmd_sendpv( char **lasts )
2306 const char *str = strtok_r( NULL, str_delimiters, lasts );
2310 str_error = str_bad_cmdline;
2314 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2315 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2317 str_error = str_bad_cmdline;
2326 #if defined(MNJ_LAN)
2327 /* mnj sd seed addr port name factor stable_depth */
2328 static int CONV cmd_mnj( char **lasts )
2330 char client_str_addr[256];
2331 char client_str_id[256];
2336 int client_port, sd;
2339 str = strtok_r( NULL, str_delimiters, lasts );
2342 str_error = str_bad_cmdline;
2345 l = strtol( str, &ptr, 0 );
2346 if ( ptr == str || l == LONG_MAX || l < 0 )
2348 str_error = str_bad_cmdline;
2354 str = strtok_r( NULL, str_delimiters, lasts );
2357 str_error = str_bad_cmdline;
2360 l = strtol( str, &ptr, 0 );
2361 if ( ptr == str || l == LONG_MAX || l < 0 )
2363 str_error = str_bad_cmdline;
2366 seed = (unsigned int)l;
2369 str = strtok_r( NULL, str_delimiters, lasts );
2370 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2371 strncpy( client_str_addr, str, 255 );
2372 client_str_addr[255] = '\0';
2375 str = strtok_r( NULL, str_delimiters, lasts );
2376 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2377 l = strtol( str, &ptr, 0 );
2378 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2380 str_error = str_bad_cmdline;
2383 client_port = (int)l;
2386 str = strtok_r( NULL, str_delimiters, lasts );
2387 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2388 strncpy( client_str_id, str, 255 );
2389 client_str_id[255] = '\0';
2391 str = strtok_r( NULL, str_delimiters, lasts );
2392 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2393 factor = strtod( str, &ptr );
2394 if ( ptr == str || factor < 0.0 )
2396 str_error = str_bad_cmdline;
2400 str = strtok_r( NULL, str_delimiters, lasts );
2401 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2403 l = strtol( str, &ptr, 0 );
2404 if ( ptr == str || l == LONG_MAX )
2406 str_error = str_bad_cmdline;
2410 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2411 else { mnj_depth_stable = (int)l; }
2413 AbortDifficultCommand;
2415 resign_threshold = 65535;
2416 game_status |= ( flag_noponder | flag_noprompt );
2417 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2419 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2420 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2422 str_buffer_cmdline[0] = '\0';
2424 Out( "Sending my name %s", client_str_id );
2425 MnjOut( "%s %g final%s\n", client_str_id, factor,
2426 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2428 return cmd_suspend();