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 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
145 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
147 if(fromY == '@') { // drop (contains all info needed to convert it)
148 if(fromX >= 'a') fromX += 'A' - 'a';
149 switch(fromX) { // encode piece
150 case 'P': piece = pawn; break;
151 case 'L': piece = lance; break;
152 case 'N': piece = knight; break;
153 case 'S': piece = silver; break;
154 case 'G': piece = gold; break;
155 case 'B': piece = bishop; break;
156 case 'R': piece = rook; break;
158 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
159 } else { // board move (need to figure out moved piece)
160 int from = ('9' - fromY)*9 + (fromX - 'a');
161 int flag = (promo == '+' ? FLAG_PROMO : 0);
162 piece = abs( BOARD[from] ); // this only works when no search in progress!
163 printf("# piece from board: %d\n", piece);fflush(stdout);
164 if( game_status & flag_thinking ) {
165 int i, to = ('9' - toY)*9 + (toX - 'a');
166 piece = 0; // kludge to force illegal CSA move
167 for( i = 0; i < root_nmove; i++ ) { // determine the piece from the move list
168 int move = root_move_list[i].move;
169 if( I2To(move) != to ) continue;
170 if( I2From(move) != from ) continue;
171 if( (move & FLAG_PROMO) != flag ) continue;
172 piece = I2PieceMove( move ); // we found the move; take the piece from it
175 } else if( game_status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
176 printf("# piece corrected to %d\n", piece);fflush(stdout);
177 if( promo == '+') piece += promote;
178 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
183 update_exclude_list( tree_t * restrict ptree, int add, unsigned int move )
184 { // [HGM] exclude: manage list of excluded moves
186 if( moves_ignore[0] == MOVE_NA ) { // nothing is excluded yet; make a copy of root move list;
187 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
188 all_moves[i] = MOVE_NA;
190 if ( move == MOVE_NA ) { // all moves
191 if( add ) { // copy entire list of legal moves
192 for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
193 } else moves_ignore[0] = MOVE_NA; // clear list
194 } else { // single move
195 for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
196 if( add ) { // we must add the move
197 if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
198 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
199 moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
200 } else { // we must delete the move
201 if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
202 while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
209 { // set white and black times from own and opponent time.
211 if(movesPerSession <= 0) moves = 35; else {
213 while(moves <= 0) moves += movesPerSession;
215 time_limit = (myTime-inc-30)/(moves+2) + inc;
216 time_max_limit = 3*time_limit;
217 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
218 time_limit *= 10; // msec
219 time_max_limit *= 10;
220 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
224 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
225 { // [HGM] added to make Bonanza.exe a native WinBoard engine
227 static char forceMode = 0;
228 sscanf(line + strlen(command) + 1, "%d", &value);
229 Out("# command = '%s'\n", line);
233 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
234 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
236 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
237 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
239 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
240 IF("easy") { strcpy(line, "ponder off"); return 0; }
241 IF("hard") { strcpy(line, "ponder on"); return 0; }
244 IF("time") { sscanf(line+5, "%d", &myTime); }
245 IF("otim") { sscanf(line+5, "%d", &hisTime); }
246 IF("force") { forceMode = 1; }
247 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
248 IF("memory") { sprintf(line, "hash %d", value); return 0; }
249 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
250 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
252 IF("quit") { return 0; }
253 IF("analyze") { return 0; }
254 IF("exit") { return 0; }
255 IF("variant") { /* ignore, since it must be Shogi */; }
258 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
259 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
261 IF("level") { int min, sec; float fsec=0.;
262 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
263 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
264 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
266 IF("usermove") { char buf[20];
267 xboard_to_CSA( ptree, line+9, buf );
268 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
273 IF("undo") { return 0; }
275 IF("ping") { Out("pong %d\n", value); }
276 IF("exclude") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
277 IF("include") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
282 static int CONV proce_cui( tree_t * restrict ptree )
287 token = strtok_r( str_cmdline, str_delimiters, &last );
289 if ( token == NULL || *token == '#' ) { return 1; }
293 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
294 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
295 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
297 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
300 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
301 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
302 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
303 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
304 if ( ! strcmp( token, "include" ) ) { return cmd_inex( ptree , 0, &last); } // [HGM] exclude
305 if ( ! strcmp( token, "exclude" ) ) { return cmd_inex( ptree , 1, &last); } // [HGM] exclude
307 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
309 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
310 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
311 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
312 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
313 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
314 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
315 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
316 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
317 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
318 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
319 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
320 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
321 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
322 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
323 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
324 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
325 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
326 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
327 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
328 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
329 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
331 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
332 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
335 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
338 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
340 #if defined(DFPN_CLIENT)
341 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
345 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
347 #if ! defined(NO_STDOUT)
348 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
350 #if ! defined(MINIMUM)
351 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
354 str_error = str_bad_cmdline;
360 static int CONV proce_csalan( tree_t * restrict ptree )
365 token = strtok_r( str_cmdline, str_delimiters, &last );
367 if ( token == NULL ) { return 1; }
368 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
373 token = strtok_r( NULL, str_delimiters, &last );
374 if ( token == NULL || *token != 'T' )
376 str_error = str_bad_cmdline;
380 l = strtol( token+1, &ptr, 0 );
381 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
383 str_error = str_bad_cmdline;
387 adjust_time( (unsigned int)l, client_turn );
388 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
391 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
393 return cmd_usrmove( ptree, token+1, &last );
395 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
396 if ( ! strcmp( token, "#WIN" )
397 || ! strcmp( token, "#LOSE" )
398 || ! strcmp( token, "#DRAW" )
399 || ! strcmp( token, "#CHUDAN" ) )
401 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
403 game_status |= flag_suspend;
407 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
408 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
412 if ( client_ngame == client_max_game ) { return cmd_quit(); }
414 return client_next_game( ptree, client_str_addr, (int)client_port );
423 static int CONV proce_mnj( tree_t * restrict ptree )
429 token = strtok_r( str_cmdline, str_delimiters, &last );
430 if ( token == NULL ) { return 1; }
432 if ( ! strcmp( token, "new" ) )
434 iret = cmd_suspend();
435 if ( iret != 1 ) { return iret; }
438 iret = cmd_new( ptree, &last );
439 if ( iret < 0 ) { return iret; }
441 moves_ignore[0] = MOVE_NA;
442 return analyze( ptree );
444 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
445 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
446 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
447 if ( ! strcmp( token, "retract" ) )
451 const char *str = strtok_r( NULL, str_delimiters, &last );
454 str_error = str_bad_cmdline;
457 l = strtol( str, &ptr, 0 );
458 if ( ptr == str || (long)NUM_UNMAKE < l )
460 str_error = str_bad_cmdline;
464 return cmd_mnjmove( ptree, &last, (int)l );
466 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
468 str_error = str_bad_cmdline;
474 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
483 token = strtok_r( NULL, str_delimiters, lasts );
486 str_error = str_bad_cmdline;
489 lid = strtol( token, &ptr, 0 );
490 if ( ptr == token || lid == LONG_MAX || lid < 1 )
492 str_error = str_bad_cmdline;
496 AbortDifficultCommand;
498 for ( i = 0; ; i += 1 )
500 token = strtok_r( NULL, str_delimiters, lasts );
501 if ( token == NULL ) { break; }
503 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
505 moves_ignore[i] = move;
509 str_error = str_bad_cmdline;
512 mnj_posi_id = (int)lid;
513 moves_ignore[i] = MOVE_NA;
515 return analyze( ptree );
520 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
522 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
523 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
529 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
531 str_error = str_bad_cmdline;
535 lid = strtol( str2, &ptr, 0 );
536 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
538 str_error = str_bad_cmdline;
542 AbortDifficultCommand;
546 iret = unmake_move_root( ptree );
547 if ( iret < 0 ) { return iret; }
552 iret = interpret_CSA_move( ptree, &move, str1 );
553 if ( iret < 0 ) { return iret; }
555 iret = get_elapsed( &time_turn_start );
556 if ( iret < 0 ) { return iret; }
558 mnj_posi_id = (int)lid;
560 iret = make_move_root( ptree, move, ( flag_time | flag_rep
561 | flag_detect_hang ) );
562 if ( iret < 0 ) { return iret; }
564 # if ! defined(NO_STDOUT)
565 iret = out_board( ptree, stdout, 0, 0 );
566 if ( iret < 0 ) { return iret; }
569 moves_ignore[0] = MOVE_NA;
570 return analyze( ptree );
576 do_analyze( tree_t * restrict ptree )
577 { // [HGM] analyze: do a ponder search on the current position
579 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
580 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
582 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
583 for( iret = 0; iret < nsquare; iret++ ) root_pos[iret] = BOARD[iret];
585 game_status |= flag_pondering;
586 iret = iterate( ptree );
587 game_status &= ~flag_pondering;
593 cmd_undo( tree_t * restrict ptree )
594 { // [HGM] undo: restart the game, and feed all moves except the last
595 int i, last = move_ptr;
596 char *p = start_data;
597 if( move_ptr <= 0 ) {
598 str_error = "undo past start of game ignored";
602 AbortDifficultCommand;
605 cmd_new( ptree, &p );
606 for(i=0; i<last; i++) {
607 make_move_root( ptree, move_list[i], 0);
610 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
611 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
618 cmd_analyze( tree_t * restrict ptree )
619 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
620 AbortDifficultCommand;
623 return do_analyze( ptree );
628 cmd_inex( tree_t * restrict ptree, int inex, char **last )
629 { // [HGM] analyze: switch off analysis mode
630 const char *str = strtok_r( NULL, str_delimiters, last );
634 AbortDifficultCommand;
636 if( ! strcmp( str, "all" ) ) {
639 iret = interpret_CSA_move( ptree, &move, str );
640 if ( iret < 0 ) { return iret; }
643 update_exclude_list( ptree, inex, move );
645 if ( ! analyze_mode ) return 1;
646 return do_analyze( ptree );
652 { // [HGM] analyze: switch off analysis mode
653 if ( !analyze_mode ) {
654 str_error = "was not analyzing";
658 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
660 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
667 static int CONV proce_usi( tree_t * restrict ptree )
673 token = strtok_r( str_cmdline, str_delimiters, &lasts );
674 if ( token == NULL ) { return 1; }
676 if ( ! strcmp( token, "usi" ) )
678 USIOut( "id name %s\n", str_myname );
679 USIOut( "id author Kunihito Hoki\n" );
684 if ( ! strcmp( token, "isready" ) )
686 USIOut( "readyok\n", str_myname );
690 if ( ! strcmp( token, "echo" ) )
692 USIOut( "%s\n", lasts );
696 if ( ! strcmp( token, "ignore_moves" ) )
698 return usi_ignore( ptree, &lasts );
701 if ( ! strcmp( token, "genmove_probability" ) )
703 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
704 return usi_root_list( ptree );
707 if ( ! strcmp( token, "go" ) )
709 iret = usi_go( ptree, &lasts );
710 moves_ignore[0] = MOVE_NA;
714 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
715 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
716 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
718 str_error = str_bad_cmdline;
724 usi_ignore( tree_t * restrict ptree, char **lasts )
731 AbortDifficultCommand;
733 for ( i = 0; ; i += 1 )
735 token = strtok_r( NULL, str_delimiters, lasts );
736 if ( token == NULL ) { break; }
738 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
739 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
741 moves_ignore[i] = move;
744 moves_ignore[i] = MOVE_NA;
751 usi_go( tree_t * restrict ptree, char **lasts )
758 AbortDifficultCommand;
760 if ( game_status & mask_game_end )
762 str_error = str_game_ended;
766 token = strtok_r( NULL, str_delimiters, lasts );
768 if ( ! strcmp( token, "book" ) )
770 AbortDifficultCommand;
771 if ( usi_book( ptree ) < 0 ) { return -1; }
777 if ( ! strcmp( token, "infinite" ) )
779 usi_byoyomi = UINT_MAX;
780 depth_limit = PLY_MAX;
781 node_limit = UINT64_MAX;
782 sec_limit_depth = UINT_MAX;
784 else if ( ! strcmp( token, "byoyomi" ) )
786 token = strtok_r( NULL, str_delimiters, lasts );
789 str_error = str_bad_cmdline;
793 l = strtol( token, &ptr, 0 );
794 if ( ptr == token || l > UINT_MAX || l < 1 )
796 str_error = str_bad_cmdline;
800 usi_byoyomi = (unsigned int)l;
801 depth_limit = PLY_MAX;
802 node_limit = UINT64_MAX;
803 sec_limit_depth = UINT_MAX;
806 str_error = str_bad_cmdline;
811 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
813 iret = com_turn_start( ptree, 0 );
815 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
824 usi_posi( tree_t * restrict ptree, char **lasts )
830 AbortDifficultCommand;
832 moves_ignore[0] = MOVE_NA;
834 token = strtok_r( NULL, str_delimiters, lasts );
835 if ( strcmp( token, "startpos" ) )
837 str_error = str_bad_cmdline;
841 if ( ini_game( ptree, &min_posi_no_handicap,
842 flag_history, NULL, NULL ) < 0 ) { return -1; }
844 token = strtok_r( NULL, str_delimiters, lasts );
845 if ( token == NULL ) { return 1; }
847 if ( strcmp( token, "moves" ) )
849 str_error = str_bad_cmdline;
855 token = strtok_r( NULL, str_delimiters, lasts );
856 if ( token == NULL ) { break; }
858 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
859 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
860 if ( make_move_root( ptree, move, ( flag_history | flag_time
862 | flag_detect_hang ) ) < 0 )
868 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
875 static int CONV cmd_move_now( void )
877 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
884 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
892 if ( game_status & mask_game_end )
894 str_error = str_game_ended;
898 if ( game_status & flag_thinking )
900 str_error = str_busy_think;
904 str = strtok_r( NULL, str_delimiters, lasts );
905 if ( str == NULL ) { lelapsed = 0; }
909 str_error = str_bad_cmdline;
913 lelapsed = strtol( str, &ptr, 0 );
914 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
916 str_error = str_bad_cmdline;
921 if ( game_status & ( flag_pondering | flag_puzzling ) )
925 for ( i = 0; i < ponder_nmove; i++ )
927 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
932 if ( i == ponder_nmove )
935 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
938 #if defined(CSASHOGI)
939 AbortDifficultCommand;
941 str_error = str_illegal_move;
946 if ( ( game_status & flag_puzzling )
947 || strcmp( str_move, str_CSA_move(ponder_move) ) )
949 ponder_move = MOVE_PONDER_FAILED;
950 game_status |= flag_quit_ponder;
954 iret = update_time( Flip(root_turn) );
955 if ( iret < 0 ) { return iret; }
958 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
961 out_CSA( ptree, &record_game, ponder_move );
963 game_status &= ~flag_pondering;
964 game_status |= flag_thinking;
968 set_search_limit_time( root_turn );
970 OutCsaShogi( "info ponder end\n" );
972 str = str_time_symple( time_turn_start - time_start );
973 Out( " %6s MOVE PREDICTION HIT\n"
974 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
979 iret = interpret_CSA_move( ptree, &move, str_move );
980 if ( iret < 0 ) { return iret; }
981 move_evasion_pchk = 0;
982 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
983 | flag_detect_hang ) );
988 if ( sckt_csa != SCKT_NULL )
990 if ( move_evasion_pchk )
992 str = str_CSA_move( move_evasion_pchk );
993 iret = sckt_out( sckt_csa, "%c%s\n",
994 ach_turn[Flip(root_turn)], str );
995 if ( iret < 0 ) { return iret; }
997 return cmd_suspend();
1001 if ( move_evasion_pchk )
1003 str = str_CSA_move( move_evasion_pchk );
1004 #if defined(CSASHOGI)
1005 OutCsaShogi( "move%s\n", str );
1006 return cmd_suspend();
1008 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
1009 ach_turn[Flip(root_turn)], str );
1010 str_error = str_message;
1018 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
1019 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
1021 #if defined(CSA_LAN)
1022 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
1024 iret = sckt_out( sckt_csa, "%%TORYO\n" );
1025 if ( iret < 0 ) { return iret; }
1029 if ( ! ( game_status & mask_game_end ) )
1031 iret = com_turn_start( ptree, 0 );
1032 if ( iret < 0 ) { return iret; }
1039 static int CONV cmd_beep( char **lasts )
1041 const char *str = strtok_r( NULL, str_delimiters, lasts );
1044 str_error = str_bad_cmdline;
1048 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
1049 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
1051 str_error = str_bad_cmdline;
1059 static int CONV cmd_peek( char **lasts )
1061 const char *str = strtok_r( NULL, str_delimiters, lasts );
1065 str_error = str_bad_cmdline;
1069 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
1070 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
1072 str_error = str_bad_cmdline;
1080 static int CONV cmd_stdout( char **lasts )
1082 const char *str = strtok_r( NULL, str_delimiters, lasts );
1086 str_error = str_bad_cmdline;
1090 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1091 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1093 str_error = str_bad_cmdline;
1101 static int CONV cmd_newlog( char **lasts )
1103 const char *str = strtok_r( NULL, str_delimiters, lasts );
1107 str_error = str_bad_cmdline;
1111 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1112 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1114 str_error = str_bad_cmdline;
1122 static int CONV cmd_ponder( char **lasts )
1124 const char *str = strtok_r( NULL, str_delimiters, lasts );
1128 str_error = str_bad_cmdline;
1132 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1133 else if ( ! strcmp( str, str_off ) )
1135 if ( game_status & ( flag_pondering | flag_puzzling ) )
1137 game_status |= flag_quit_ponder;
1139 game_status |= flag_noponder;
1142 str_error = str_bad_cmdline;
1150 #if ! defined(NO_STDOUT)
1151 static int CONV cmd_stress( char **lasts )
1153 const char *str = strtok_r( NULL, str_delimiters, lasts );
1157 str_error = str_bad_cmdline;
1161 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1162 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1164 str_error = str_bad_cmdline;
1174 #if defined(MINIMUM)
1175 cmd_book( char **lasts )
1177 cmd_book( tree_t * restrict ptree, char **lasts )
1180 const char *str = strtok_r( NULL, str_delimiters, lasts );
1185 str_error = str_bad_cmdline;
1188 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1189 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1190 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1191 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1192 #if ! defined(MINIMUM)
1193 else if ( ! strcmp( str, "create" ) )
1195 AbortDifficultCommand;
1197 iret = book_create( ptree );
1198 if ( iret < 0 ) { return iret; }
1200 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1202 if ( iret < 0 ) { return iret; }
1204 iret = get_elapsed( &time_turn_start );
1208 str_error = str_bad_cmdline;
1216 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1218 const char *str = strtok_r( NULL, str_delimiters, lasts );
1225 l = strtol( str, &ptr, 0 );
1228 str_error = str_bad_cmdline;
1231 if ( l == 1 ) { game_status &= ~flag_reverse; }
1232 else if ( l == 2 ) { game_status |= flag_reverse; }
1234 str_error = str_bad_cmdline;
1240 iret = out_board( ptree, stdout, 0, 0 );
1241 if ( iret < 0 ) { return iret; }
1242 #if ! defined(NO_LOGGING)
1243 iret = out_board( ptree, pf_log, 0, 0 );
1244 if ( iret < 0 ) { return iret; }
1252 static int CONV cmd_ping( void )
1254 OutCsaShogi( "pong\n" );
1260 static int CONV cmd_hash( char **lasts )
1262 const char *str = strtok_r( NULL, str_delimiters, lasts );
1268 str_error = str_bad_cmdline;
1272 l = strtol( str, &ptr, 0 );
1273 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1275 str_error = str_bad_cmdline;
1279 AbortDifficultCommand;
1281 log2_ntrans_table = (int)l;
1282 memory_free( (void *)ptrans_table_orig );
1283 return ini_trans_table();
1287 static int CONV cmd_limit( char **lasts )
1289 const char *str = strtok_r( NULL, str_delimiters, lasts );
1295 str_error = str_bad_cmdline;
1299 AbortDifficultCommand;
1301 if ( ! strcmp( str, "depth" ) )
1303 str = strtok_r( NULL, str_delimiters, lasts );
1306 str_error = str_bad_cmdline;
1309 l1 = strtol( str, &ptr, 0 );
1310 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1312 str_error = str_bad_cmdline;
1315 sec_limit_up = UINT_MAX;
1316 node_limit = UINT64_MAX;
1317 depth_limit = (int)l1;
1319 else if ( ! strcmp( str, "nodes" ) )
1321 str = strtok_r( NULL, str_delimiters, lasts );
1324 str_error = str_bad_cmdline;
1327 l1 = strtol( str, &ptr, 0 );
1328 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1330 str_error = str_bad_cmdline;
1333 sec_limit_up = UINT_MAX;
1334 depth_limit = PLY_MAX;
1335 node_limit = (uint64_t)l1;
1337 else if ( ! strcmp( str, "time" ) )
1339 str = strtok_r( NULL, str_delimiters, lasts );
1342 str_error = str_bad_cmdline;
1346 if ( ! strcmp( str, "extendable" ) )
1348 game_status |= flag_time_extendable;
1350 else if ( ! strcmp( str, "strict" ) )
1352 game_status &= ~flag_time_extendable;
1355 l1 = strtol( str, &ptr, 0 );
1356 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1358 str_error = str_bad_cmdline;
1362 str = strtok_r( NULL, str_delimiters, lasts );
1365 str_error = str_bad_cmdline;
1368 l2 = strtol( str, &ptr, 0 );
1369 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1371 str_error = str_bad_cmdline;
1375 str = strtok_r( NULL, str_delimiters, lasts );
1376 if ( ! str ) { l3 = -1; }
1378 l3 = strtol( str, &ptr, 0 );
1379 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1381 str_error = str_bad_cmdline;
1386 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1388 depth_limit = PLY_MAX;
1389 node_limit = UINT64_MAX;
1390 sec_limit = (unsigned int)l1 * 60U;
1391 sec_limit_up = (unsigned int)l2;
1392 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1393 else { sec_limit_depth = (unsigned int)l3; }
1397 str_error = str_bad_cmdline;
1406 cmd_read( tree_t * restrict ptree, char **lasts )
1408 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1409 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1410 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1411 const char *str_tmp;
1412 FILE *pf_src, *pf_dest;
1413 char str_file[SIZE_FILENAME];
1419 flag = flag_history | flag_rep | flag_detect_hang;
1425 str_error = str_bad_cmdline;
1431 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1432 else if ( strcmp( str2, "nil" ) )
1434 str_error = str_bad_cmdline;
1441 l = strtol( str3, &ptr, 0 );
1442 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1444 str_error = str_bad_cmdline;
1447 moves = (unsigned int)l - 1U;
1450 AbortDifficultCommand;
1452 if ( ! strcmp( str1, "." ) )
1454 str_tmp = "game.cs_";
1456 #if defined(NO_LOGGING)
1457 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1459 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1460 str_dir_logs, record_num );
1462 pf_dest = file_open( str_tmp, "w" );
1463 if ( pf_dest == NULL ) { return -2; }
1465 pf_src = file_open( str_file, "r" );
1466 if ( pf_src == NULL )
1468 file_close( pf_dest );
1472 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1474 iret = file_close( pf_src );
1477 file_close( pf_dest );
1481 iret = file_close( pf_dest );
1482 if ( iret < 0 ) { return iret; }
1488 iret = read_record( ptree, str1, moves, flag );
1489 if ( iret < 0 ) { return iret; }
1491 iret = get_elapsed( &time_turn_start );
1492 if ( iret < 0 ) { return iret; }
1494 if ( str_tmp && remove( str_tmp ) )
1496 out_warning( "remove() failed." );
1504 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1506 const char *str = strtok_r( NULL, str_delimiters, lasts );
1510 if ( str == NULL || *str == 'T' )
1512 AbortDifficultCommand;
1514 if ( game_status & mask_game_end ) { return 1; }
1516 game_status |= flag_resigned;
1517 update_time( root_turn );
1518 out_CSA( ptree, &record_game, MOVE_RESIGN );
1521 l = strtol( str, &ptr, 0 );
1522 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1524 str_error = str_bad_cmdline;
1527 resign_threshold = (int)l;
1534 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1536 const char *str = strtok_r( NULL, str_delimiters, lasts );
1542 if ( game_status & mask_game_end )
1544 str_error = str_game_ended;
1548 AbortDifficultCommand;
1552 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1554 str_error = str_bad_cmdline;
1558 iret = get_elapsed( &time_turn_start );
1559 if ( iret < 0 ) { return iret; }
1561 return com_turn_start( ptree, 0 );
1564 l = strtol( str, &ptr, 0 );
1565 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1567 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1569 str_error = str_bad_cmdline;
1573 for ( i = 0; i < l; i += 1 )
1575 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1577 iret = get_elapsed( &time_turn_start );
1578 if ( iret < 0 ) { return iret; }
1580 iret = com_turn_start( ptree, 0 );
1581 if ( iret < 0 ) { return iret; }
1588 iret = interpret_CSA_move( ptree, &move, str );
1589 if ( iret < 0 ) { return iret; }
1591 iret = get_elapsed( &time_turn_start );
1592 if ( iret < 0 ) { return iret; }
1594 iret = make_move_root( ptree, move,
1595 ( flag_history | flag_time | flag_rep
1596 | flag_detect_hang ) );
1597 if ( iret < 0 ) { return iret; }
1599 str = strtok_r( NULL, str_delimiters, lasts );
1601 } while ( str != NULL );
1603 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1604 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1610 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1612 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1613 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1614 const min_posi_t *pmp;
1615 min_posi_t min_posi;
1618 AbortDifficultCommand;
1620 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1621 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1625 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1626 memset( &min_posi.asquare, empty, nsquare );
1627 min_posi.hand_black = min_posi.hand_white = 0;
1628 iret = read_board_rep1( str1, &min_posi );
1629 if ( iret < 0 ) { return iret; }
1633 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1634 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1636 str_error = str_bad_cmdline;
1640 else { min_posi.turn_to_move = black; }
1644 else { pmp = &min_posi_no_handicap; }
1646 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1647 if ( iret < 0 ) { return iret; }
1649 return get_elapsed( &time_turn_start );
1653 static int CONV cmd_outmove( tree_t * restrict ptree )
1655 const char *str_move;
1657 unsigned int move_list[ MAX_LEGAL_MOVES ];
1660 AbortDifficultCommand;
1662 if ( game_status & mask_game_end )
1664 Out( "NO LEGAL MOVE\n" );
1665 DFPNOut( "NO LEGAL MOVE\n" );
1669 n = gen_legal_moves( ptree, move_list, 0 );
1672 for ( c = i = 0; i < n; i += 1 )
1674 str_move = str_CSA_move(move_list[i]);
1676 if ( i && ( i % 10 ) == 0 )
1678 Out( "%s\n", buffer );
1679 DFPNOut( "%s ", buffer );
1680 memcpy( buffer, str_move, 6 );
1686 memcpy( buffer + c + 1, str_move, 6 );
1690 memcpy( buffer + c, str_move, 6 );
1695 Out( "%s\n", buffer );
1696 DFPNOut( "%s\n", buffer );
1702 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1704 const char *str = strtok_r( NULL, str_delimiters, lasts );
1707 unsigned int nposition;
1713 AbortDifficultCommand;
1718 if ( str != NULL && ! strcmp( str, "mate" ) )
1721 str = strtok_r( NULL, str_delimiters, lasts );
1727 l = strtol( str, &ptr, 0 );
1728 if ( ptr == str || l == LONG_MAX || l < 1 )
1730 str_error = str_bad_cmdline;
1733 nposition = (unsigned int)l;
1735 else { nposition = UINT_MAX; }
1738 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1739 if ( iret < 0 ) { return iret; }
1742 iret = is_mate ? solve_mate_problems( ptree, nposition )
1743 : solve_problems( ptree, nposition );
1745 iret = solve_problems( ptree, nposition );
1750 record_close( &record_problems );
1754 iret = record_close( &record_problems );
1755 if ( iret < 0 ) { return iret; }
1757 return get_elapsed( &time_turn_start );
1761 static int CONV cmd_quit( void )
1763 game_status |= flag_quit;
1768 static int CONV cmd_suspend( void )
1770 if ( game_status & ( flag_pondering | flag_puzzling ) )
1772 game_status |= flag_quit_ponder;
1776 game_status |= flag_suspend;
1781 static int CONV cmd_time( char **lasts )
1783 const char *str = strtok_r( NULL, str_delimiters, lasts );
1788 str_error = str_bad_cmdline;
1791 else if ( ! strcmp( str, "response" ) )
1794 str = strtok_r( NULL, str_delimiters, lasts );
1797 str_error = str_bad_cmdline;
1800 l = strtol( str, &ptr, 0 );
1801 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1803 str_error = str_bad_cmdline;
1806 time_response = (unsigned int)l;
1809 else if ( ! strcmp( str, "remain" ) )
1813 str = strtok_r( NULL, str_delimiters, lasts );
1816 str_error = str_bad_cmdline;
1819 l1 = strtol( str, &ptr, 0 );
1820 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1822 str_error = str_bad_cmdline;
1826 str = strtok_r( NULL, str_delimiters, lasts );
1829 str_error = str_bad_cmdline;
1832 l2 = strtol( str, &ptr, 0 );
1833 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1835 str_error = str_bad_cmdline;
1839 if ( sec_limit_up == UINT_MAX )
1841 str_error = str_bad_cmdline;
1845 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1848 str_error = str_bad_cmdline;
1853 #if !defined(MINIMUM)
1854 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1855 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1857 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1858 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1859 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1860 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1862 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1863 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1867 unsigned int max_games;
1868 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1872 str_error = str_bad_cmdline;
1875 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1876 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1878 str_error = str_bad_cmdline;
1882 max_games = UINT_MAX;
1883 max_iterations = INT_MAX;
1884 nworker1 = nworker2 = nsteps = 1;
1888 l = strtol( str2, &ptr, 0 );
1889 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1891 str_error = str_bad_cmdline;
1899 l = strtol( str3, &ptr, 0 );
1900 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1902 str_error = str_bad_cmdline;
1905 if ( l > 0 ) { max_games = (unsigned int)l; }
1910 l = strtol( str4, &ptr, 0 );
1911 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1913 str_error = str_bad_cmdline;
1916 if ( l > 0 ) { max_iterations = (int)l; }
1922 l = strtol( str5, &ptr, 0 );
1923 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1925 str_error = str_bad_cmdline;
1933 l = strtol( str6, &ptr, 0 );
1934 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1936 str_error = str_bad_cmdline;
1943 AbortDifficultCommand;
1945 log2_ntrans_table = 12;
1947 memory_free( (void *)ptrans_table_orig );
1949 iret = ini_trans_table();
1950 if ( iret < 0 ) { return iret; }
1952 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1953 nworker1, nworker2 );
1954 if ( iret < 0 ) { return -1; }
1956 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1957 if ( iret < 0 ) { return -1; }
1959 iret = get_elapsed( &time_turn_start );
1960 if ( iret < 0 ) { return iret; }
1964 #endif /* MINIMUM */
1968 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
1970 const char *str = strtok_r( NULL, str_delimiters, lasts );
1976 str_error = str_bad_cmdline;
1979 else if ( ! strcmp( str, "num" ) )
1981 str = strtok_r( NULL, str_delimiters, lasts );
1984 str_error = str_bad_cmdline;
1987 l = strtol( str, &ptr, 0 );
1988 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1990 str_error = str_bad_cmdline;
1994 AbortDifficultCommand;
1998 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
2002 else if ( ! strcmp( str, "width" ) )
2004 str = strtok_r( NULL, str_delimiters, lasts );
2007 str_error = str_bad_cmdline;
2010 l = strtol( str, &ptr, 0 );
2011 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
2013 str_error = str_bad_cmdline;
2017 AbortDifficultCommand;
2021 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
2026 str_error = str_bad_cmdline;
2033 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
2035 const char *str = strtok_r( NULL, str_delimiters, lasts );
2039 str_error = str_bad_cmdline;
2042 else if ( ! strcmp( str, "hash" ) )
2047 str = strtok_r( NULL, str_delimiters, lasts );
2050 str_error = str_bad_cmdline;
2053 l = strtol( str, &ptr, 0 );
2054 if ( ptr == str || l == LONG_MAX || l < 1 )
2056 str_error = str_bad_cmdline;
2060 AbortDifficultCommand;
2062 dfpn_hash_log2 = (unsigned int)l;
2063 return dfpn_ini_hash();
2065 else if ( ! strcmp( str, "go" ) )
2067 AbortDifficultCommand;
2069 return dfpn( ptree, root_turn, 1 );
2071 else if ( ! strcmp( str, "connect" ) )
2079 str = strtok_r( NULL, str_delimiters, lasts );
2080 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2081 strncpy( str_addr, str, 255 );
2082 str_addr[255] = '\0';
2084 str = strtok_r( NULL, str_delimiters, lasts );
2085 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2086 l = strtol( str, &ptr, 0 );
2087 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2089 str_error = str_bad_cmdline;
2094 str = strtok_r( NULL, str_delimiters, lasts );
2095 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2096 strncpy( str_id, str, 255 );
2099 AbortDifficultCommand;
2101 dfpn_sckt = sckt_connect( str_addr, port );
2102 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2104 str_buffer_cmdline[0] = '\0';
2105 DFPNOut( "Worker: %s\n", str_id );
2110 str_error = str_bad_cmdline;
2117 static int CONV cmd_thread( char **lasts )
2119 const char *str = strtok_r( NULL, str_delimiters, lasts );
2123 str_error = str_bad_cmdline;
2126 else if ( ! strcmp( str, "num" ) )
2131 str = strtok_r( NULL, str_delimiters, lasts );
2134 str_error = str_bad_cmdline;
2137 l = strtol( str, &ptr, 0 );
2138 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2140 str_error = str_bad_cmdline;
2148 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2155 str_error = str_bad_cmdline;
2161 #if defined(DFPN_CLIENT)
2162 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2168 AbortDifficultCommand;
2170 str = strtok_r( NULL, str_delimiters, lasts );
2171 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2172 strncpy( dfpn_client_str_addr, str, 255 );
2173 dfpn_client_str_addr[255] = '\0';
2175 str = strtok_r( NULL, str_delimiters, lasts );
2176 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2177 dfpn_client_port = strtol( str, &ptr, 0 );
2178 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2179 || dfpn_client_port > USHRT_MAX )
2181 str_error = str_bad_cmdline;
2185 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2187 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2188 if ( iret < 0 ) { return iret; }
2190 if ( dfpn_client_sckt == SCKT_NULL )
2192 str_error = "Check network status.";
2196 return get_elapsed( &time_turn_start );
2201 #if defined(CSA_LAN)
2202 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2208 str = strtok_r( NULL, str_delimiters, lasts );
2209 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2210 strncpy( client_str_addr, str, 255 );
2211 client_str_addr[255] = '\0';
2213 str = strtok_r( NULL, str_delimiters, lasts );
2214 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2215 client_port = strtol( str, &ptr, 0 );
2216 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2217 || client_port > USHRT_MAX )
2219 str_error = str_bad_cmdline;
2223 str = strtok_r( NULL, str_delimiters, lasts );
2224 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2225 strncpy( client_str_id, str, 255 );
2226 client_str_id[255] = '\0';
2228 str = strtok_r( NULL, " \t", lasts );
2229 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2230 strncpy( client_str_pwd, str, 255 );
2231 client_str_pwd[255] = '\0';
2233 str = strtok_r( NULL, str_delimiters, lasts );
2234 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2236 max_games = strtol( str, &ptr, 0 );
2237 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2239 str_error = str_bad_cmdline;
2242 client_max_game = max_games;
2245 AbortDifficultCommand;
2249 return client_next_game( ptree, client_str_addr, (int)client_port );
2253 static int CONV cmd_sendpv( char **lasts )
2255 const char *str = strtok_r( NULL, str_delimiters, lasts );
2259 str_error = str_bad_cmdline;
2263 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2264 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2266 str_error = str_bad_cmdline;
2275 #if defined(MNJ_LAN)
2276 /* mnj sd seed addr port name factor stable_depth */
2277 static int CONV cmd_mnj( char **lasts )
2279 char client_str_addr[256];
2280 char client_str_id[256];
2285 int client_port, sd;
2288 str = strtok_r( NULL, str_delimiters, lasts );
2291 str_error = str_bad_cmdline;
2294 l = strtol( str, &ptr, 0 );
2295 if ( ptr == str || l == LONG_MAX || l < 0 )
2297 str_error = str_bad_cmdline;
2303 str = strtok_r( NULL, str_delimiters, lasts );
2306 str_error = str_bad_cmdline;
2309 l = strtol( str, &ptr, 0 );
2310 if ( ptr == str || l == LONG_MAX || l < 0 )
2312 str_error = str_bad_cmdline;
2315 seed = (unsigned int)l;
2318 str = strtok_r( NULL, str_delimiters, lasts );
2319 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2320 strncpy( client_str_addr, str, 255 );
2321 client_str_addr[255] = '\0';
2324 str = strtok_r( NULL, str_delimiters, lasts );
2325 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2326 l = strtol( str, &ptr, 0 );
2327 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2329 str_error = str_bad_cmdline;
2332 client_port = (int)l;
2335 str = strtok_r( NULL, str_delimiters, lasts );
2336 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2337 strncpy( client_str_id, str, 255 );
2338 client_str_id[255] = '\0';
2340 str = strtok_r( NULL, str_delimiters, lasts );
2341 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2342 factor = strtod( str, &ptr );
2343 if ( ptr == str || factor < 0.0 )
2345 str_error = str_bad_cmdline;
2349 str = strtok_r( NULL, str_delimiters, lasts );
2350 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2352 l = strtol( str, &ptr, 0 );
2353 if ( ptr == str || l == LONG_MAX )
2355 str_error = str_bad_cmdline;
2359 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2360 else { mnj_depth_stable = (int)l; }
2362 AbortDifficultCommand;
2364 resign_threshold = 65535;
2365 game_status |= ( flag_noponder | flag_noprompt );
2366 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2368 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2369 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2371 str_buffer_cmdline[0] = '\0';
2373 Out( "Sending my name %s", client_str_id );
2374 MnjOut( "%s %g final%s\n", client_str_id, factor,
2375 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2377 return cmd_suspend();