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 cmd_book( char **lasts );
26 # define CmdBook(x,y) cmd_book(x,y);
27 static int cmd_learn( tree_t * restrict ptree, char **lasts );
28 static int cmd_book( tree_t * restrict ptree, char **lasts );
31 #if ! defined(NO_STDOUT)
32 static int cmd_stress( char **lasts );
35 #if defined(DEKUNOBOU)
36 static int cmd_dek( char **lasts );
40 static int proce_csalan( tree_t * restrict ptree );
41 static int cmd_connect( tree_t * restrict ptree, char **lasts );
45 static int proce_mnj( tree_t * restrict ptree );
46 static int cmd_mnj( tree_t * restrict ptree, char **lasts );
47 static int cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter );
51 static int cmd_thread( char **lasts );
55 static int cmd_mpv( tree_t * restrict ptree, char **lasts );
58 static int proce_cui( tree_t * restrict ptree );
59 static int cmd_usrmove( tree_t * restrict ptree, const char *str_move,
61 static int cmd_move_now( void );
62 static int cmd_ponder( char **lasts );
63 static int cmd_limit( char **lasts );
64 static int cmd_quit( void );
65 static int cmd_beep( char **lasts );
66 static int cmd_peek( char **lasts );
67 static int cmd_hash( char **lasts );
68 static int cmd_ping( void );
69 static int cmd_suspend( void );
70 static int cmd_problem( tree_t * restrict ptree, char **lasts );
71 static int cmd_display( tree_t * restrict ptree, char **lasts );
72 static int cmd_move( tree_t * restrict ptree, char **lasts );
73 static int cmd_new( tree_t * restrict ptree, char **lasts );
74 static int cmd_read( tree_t * restrict ptree, char **lasts );
75 static int cmd_resign( tree_t * restrict ptree, char **lasts );
76 static int cmd_time( char **lasts );
77 static int cmd_undo( tree_t * restrict ptree ); // [HGM] undo
78 static int cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
79 static int cmd_exit( void );
80 static int is_move( const char *str );
84 procedure( tree_t * restrict ptree )
87 if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
90 if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
93 return proce_cui( ptree );
96 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
97 int move_list[1024], move_ptr;
99 int exclude_list[MAX_LEGAL_MOVES], all_moves[MAX_LEGAL_MOVES];
102 #define IF(X) else if(!strcmp(command, X))
104 int myTime, hisTime, movesPerSession, inc, plyNr;
108 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
110 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
111 int piece=0, bonanza_move=0;
112 if(fromY == '@') { // drop (contains all info needed to convert it)
113 if(fromX >= 'a') fromX += 'A' - 'a';
114 switch(fromX) { // encode piece
115 case 'P': piece = pawn; break;
116 case 'L': piece = lance; break;
117 case 'N': piece = knight; break;
118 case 'S': piece = silver; break;
119 case 'G': piece = gold; break;
120 case 'B': piece = bishop; break;
121 case 'R': piece = rook; break;
123 bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
124 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
125 } else { // board move (need to figure out moved piece)
126 int from = ('9' - fromY)*9 + (fromX - 'a');
127 int flag = (promo == '+' ? FLAG_PROMO : 0);
128 piece = abs( BOARD[from] ); // this only works when not searching!
129 printf("# piece from board: %d\n", piece);fflush(stdout);
130 if( status & ( flag_pondering | flag_puzzling | flag_thinking ) ) {
131 int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | flag_thinking));
132 piece = 0; // kludge to force illegal CSA move
133 for( i = 0; l ? all_moves[i] : i < root_nmove; i++ ) { // determine the piece from the move list
134 int move = (l ? all_moves[i] : root_move_list[i].move);
135 if( I2To(move) != to ) continue;
136 if( I2From(move) != from ) continue;
137 if( (move & FLAG_PROMO) != flag ) continue;
138 piece = I2PieceMove( move ); // we found the move; take the piece from it
142 printf("# piece corrected to %d\n", piece);fflush(stdout);
144 if( promo ) piece += promote;
145 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
151 update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
152 { // [HGM] exclude: manage list of excluded moves
155 if( ! exclude_list[0] ) { // nothing is excluded yet; make a copy of root move list;
156 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
159 if ( ! strcmp(move_str, "all") ) { // all moves
160 if( add ) { // copy entire list of legal moves
161 for( i = 0; i < root_nmove; i++ ) exclude_list[i] = all_moves[i];
162 } else exclude_list[0] = 0; // clear list
163 } else { // single move
164 int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
165 for( i = 0; exclude_list[i]; i++ ) if( move == exclude_list[i] ) break;
166 if( add ) { // we must add the move
167 if( exclude_list[i] ) return; // but it was already in list
168 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
169 exclude_list[i] = move; exclude_list[i+1] = 0; // append move
170 } else { // we must delete the move
171 if( exclude_list[i] == 0 ) return; // but it was not there
172 while( (exclude_list[i] = exclude_list[i+1]) ) i++; // squeeze it out
179 { // set white and black times from own and opponent time.
181 if(movesPerSession <= 0) moves = 35; else {
183 while(moves <= 0) moves += movesPerSession;
185 time_limit = (myTime-inc-30)/(moves+2) + inc;
186 time_max_limit = 3*time_limit;
187 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
188 time_limit *= 10; // msec
189 time_max_limit *= 10;
190 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
194 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
195 { // [HGM] added to make Bonanza.exe a native WinBoard engine
197 static char forceMode = 0;
198 sscanf(line + strlen(command) + 1, "%d", &value);
199 Out("# command = '%s'\n", line);
203 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
204 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
206 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
207 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
209 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
210 IF("easy") { strcpy(line, "ponder off"); return 0; }
211 IF("hard") { strcpy(line, "ponder on"); return 0; }
214 IF("time") { sscanf(line+5, "%d", &myTime); }
215 IF("otim") { sscanf(line+5, "%d", &hisTime); }
216 IF("force") { forceMode = 1; }
217 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
219 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
220 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
222 IF("quit") { return 0; }
223 IF("analyze") { return 0; }
224 IF("exit") { return 0; }
225 IF("variant") { /* ignore, since it must be Shogi */; }
228 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
229 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
231 IF("level") { int min, sec; float fsec=0.;
232 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
233 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
234 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
236 IF("usermove") { char buf[20];
237 xboard_to_CSA( ptree, line+9, buf, game_status );
238 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
243 IF("undo") { return 0; }
245 IF("ping") { Out("pong %d\n", value); }
246 IF("exclude") { update_exclude_list( ptree, 1, line+8 ); strcpy(line, "analyze"); return 0; }
247 IF("include") { update_exclude_list( ptree, 0, line+8 ); strcpy(line, "analyze"); return 0; }
253 proce_cui( tree_t * restrict ptree )
258 token = strtok_r( str_cmdline, str_delimiters, &last );
260 if ( token == NULL || *token == '#' ) { return 1; }
264 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
265 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
266 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
268 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
271 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
272 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
273 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
274 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
276 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
278 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
279 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
280 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
281 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
282 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
283 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
284 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
285 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
286 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
287 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
288 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
289 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
290 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
291 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
292 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
293 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
294 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
295 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
297 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
300 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( ptree, &last ); }
302 #if defined(DEKUNOBOU)
303 if ( ! strcmp( token, "dekunobou" ) ) { return cmd_dek( &last ); }
306 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
308 #if ! defined(NO_STDOUT)
309 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
311 #if ! defined(MINIMUM)
312 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
315 str_error = str_bad_cmdline;
322 proce_csalan( tree_t * restrict ptree )
327 token = strtok_r( str_cmdline, str_delimiters, &last );
329 if ( token == NULL ) { return 1; }
330 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
335 token = strtok_r( NULL, str_delimiters, &last );
336 if ( token == NULL || *token != 'T' )
338 str_error = str_bad_cmdline;
342 l = strtol( token+1, &ptr, 0 );
343 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
345 str_error = str_bad_cmdline;
349 adjust_time( (unsigned int)l, client_turn );
350 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
353 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
355 return cmd_usrmove( ptree, token+1, &last );
357 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
358 if ( ! strcmp( token, "#WIN" )
359 || ! strcmp( token, "#LOSE" )
360 || ! strcmp( token, "#DRAW" )
361 || ! strcmp( token, "#CHUDAN" ) )
363 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
365 game_status |= flag_suspend;
371 if ( client_ngame == client_max_game ) { return cmd_quit(); }
373 return client_next_game( ptree, client_str_addr, (int)client_port );
383 proce_mnj( tree_t * restrict ptree )
389 token = strtok_r( str_cmdline, str_delimiters, &last );
390 if ( token == NULL ) { return 1; }
392 if ( ! strcmp( token, "new" ) )
394 iret = cmd_suspend();
395 if ( iret != 1 ) { return iret; }
398 iret = cmd_new( ptree, &last );
399 if ( iret < 0 ) { return iret; }
401 return analyze( ptree );
403 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
404 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
405 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
407 str_error = str_bad_cmdline;
413 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter )
415 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
416 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
422 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
424 str_error = str_bad_cmdline;
428 lid = strtol( str2, &ptr, 0 );
429 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
431 str_error = str_bad_cmdline;
435 AbortDifficultCommand;
437 if ( is_alter ) { unmake_move_root( ptree, mnj_move_last ); };
439 iret = interpret_CSA_move( ptree, &move, str1 );
440 if ( iret < 0 ) { return iret; }
442 iret = get_elapsed( &time_turn_start );
443 if ( iret < 0 ) { return iret; }
445 mnj_posi_id = (int)lid;
446 mnj_move_last = move;
448 iret = make_move_root( ptree, move, ( flag_history | flag_time | flag_rep
450 | flag_rejections ) );
451 if ( iret < 0 ) { return iret; }
453 # if ! defined(NO_STDOUT)
454 iret = out_board( ptree, stdout, 0, 0 );
455 if ( iret < 0 ) { return iret; }
458 return analyze( ptree );
464 do_analyze( tree_t * restrict ptree )
465 { // [HGM] analyze: do a ponder search on the current position
467 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
468 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
470 Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
472 game_status |= flag_pondering;
473 iret = iterate( ptree, flag_refer_rest );
474 game_status &= ~flag_pondering;
480 cmd_undo( tree_t * restrict ptree )
481 { // [HGM] undo: restart the game, and feed all moves except the last
482 int i, last = move_ptr;
483 char *p = start_data;
484 if( move_ptr <= 0 ) {
485 str_error = "undo past start of game ignored";
489 AbortDifficultCommand;
492 cmd_new( ptree, &p );
493 for(i=0; i<last; i++) {
494 make_move_root( ptree, move_list[i], 0);
497 exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
498 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
505 cmd_analyze( tree_t * restrict ptree )
506 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
507 AbortDifficultCommand;
510 return do_analyze( ptree );
516 { // [HGM] analyze: switch off analysis mode
517 if ( !analyze_mode ) {
518 str_error = "was not analyzing";
522 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
524 exclude_list[0] = 0; // [HGM] exclude: exclude list cleared after analysis
531 is_move( const char *str )
533 if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
534 && isdigit( (int)str[2] ) && isdigit( (int)str[3] )
535 && isupper( (int)str[4] ) && isupper( (int)str[5] )
536 && str[6] == '\0' ) { return 1; }
545 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
552 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
560 if ( game_status & mask_game_end )
562 str_error = str_game_ended;
566 if ( game_status & flag_thinking )
568 str_error = str_busy_think;
572 str = strtok_r( NULL, str_delimiters, lasts );
573 if ( str == NULL ) { lelapsed = 0; }
577 str_error = str_bad_cmdline;
581 lelapsed = strtol( str, &ptr, 0 );
582 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
584 str_error = str_bad_cmdline;
589 if ( game_status & ( flag_pondering | flag_puzzling ) )
593 for ( i = 0; i < ponder_nmove; i++ )
595 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
600 if ( i == ponder_nmove )
603 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
606 #if defined(DEKUNOBOU)
607 if ( dek_ngame ) { AbortDifficultCommand; }
610 #if defined(CSASHOGI)
611 AbortDifficultCommand;
613 str_error = str_illegal_move;
618 if ( ( game_status & flag_puzzling )
619 || strcmp( str_move, str_CSA_move(ponder_move) ) )
621 ponder_move = MOVE_PONDER_FAILED;
622 game_status |= flag_quit_ponder;
626 iret = renovate_time( Flip(root_turn) );
627 if ( iret < 0 ) { return iret; }
630 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
633 history_book_learn[ record_game.moves ].move_played = ponder_move;
634 history_book_learn[ record_game.moves ].hand_played
635 = ptree->rep_hand_list[ root_nrep-1 ];
636 history_book_learn[ record_game.moves ].key_played
637 = (unsigned int)ptree->rep_board_list[ root_nrep-1 ];
639 out_CSA( ptree, &record_game, ponder_move );
641 game_status &= ~flag_pondering;
642 game_status |= flag_thinking;
647 set_search_limit_time( root_turn );
649 OutCsaShogi( "info ponder end\n" );
651 str = str_time_symple( time_turn_start - time_start );
652 Out( " %6s MOVE PREDICTION HIT\n"
653 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
658 iret = interpret_CSA_move( ptree, &move, str_move );
659 if ( iret < 0 ) { return iret; }
660 move_evasion_pchk = 0;
661 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
663 | flag_detect_hang ) );
668 if ( sckt_csa != SCKT_NULL )
670 if ( move_evasion_pchk )
672 str = str_CSA_move( move_evasion_pchk );
673 iret = sckt_out( sckt_csa, "%c%s\n",
674 ach_turn[Flip(root_turn)], str );
675 if ( iret < 0 ) { return iret; }
677 return cmd_suspend();
681 #if defined(DEKUNOBOU)
684 if ( move_evasion_pchk )
687 OutDek( "%%TORYO\n" );
689 return cmd_suspend();
693 if ( move_evasion_pchk )
695 str = str_CSA_move( move_evasion_pchk );
696 #if defined(CSASHOGI)
697 OutCsaShogi( "move%s\n", str );
698 return cmd_suspend();
700 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
701 ach_turn[Flip(root_turn)], str );
702 str_error = str_message;
710 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
711 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
714 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
716 iret = sckt_out( sckt_csa, "%%TORYO\n" );
717 if ( iret < 0 ) { return iret; }
721 #if defined(DEKUNOBOU)
722 if ( dek_ngame && ( game_status & flag_drawn ) ) { OutDek( "%%TORYO\n" ); }
725 if ( ! ( game_status & mask_game_end ) )
727 iret = com_turn_start( ptree, 0 );
728 if ( iret < 0 ) { return iret; }
736 cmd_beep( char **lasts )
738 const char *str = strtok_r( NULL, str_delimiters, lasts );
741 str_error = str_bad_cmdline;
745 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
746 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
748 str_error = str_bad_cmdline;
757 cmd_peek( char **lasts )
759 const char *str = strtok_r( NULL, str_delimiters, lasts );
763 str_error = str_bad_cmdline;
767 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
768 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
770 str_error = str_bad_cmdline;
779 cmd_ponder( char **lasts )
781 const char *str = strtok_r( NULL, str_delimiters, lasts );
785 str_error = str_bad_cmdline;
789 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
790 else if ( ! strcmp( str, str_off ) )
792 if ( game_status & ( flag_pondering | flag_puzzling ) )
794 game_status |= flag_quit_ponder;
796 game_status |= flag_noponder;
799 str_error = str_bad_cmdline;
807 #if ! defined(NO_STDOUT)
809 cmd_stress( char **lasts )
811 const char *str = strtok_r( NULL, str_delimiters, lasts );
815 str_error = str_bad_cmdline;
819 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
820 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
822 str_error = str_bad_cmdline;
833 cmd_book( char **lasts )
835 cmd_book( tree_t * restrict ptree, char **lasts )
838 const char *str = strtok_r( NULL, str_delimiters, lasts );
843 str_error = str_bad_cmdline;
846 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
847 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
848 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
849 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
850 #if ! defined(MINIMUM)
851 else if ( ! strcmp( str, "create" ) )
853 AbortDifficultCommand;
855 iret = book_create( ptree );
856 if ( iret < 0 ) { return iret; }
858 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
860 if ( iret < 0 ) { return iret; }
862 iret = get_elapsed( &time_turn_start );
866 str_error = str_bad_cmdline;
875 cmd_display( tree_t * restrict ptree, char **lasts )
877 const char *str = strtok_r( NULL, str_delimiters, lasts );
884 l = strtol( str, &ptr, 0 );
887 str_error = str_bad_cmdline;
890 if ( l == 1 ) { game_status &= ~flag_reverse; }
891 else if ( l == 2 ) { game_status |= flag_reverse; }
893 str_error = str_bad_cmdline;
899 iret = out_board( ptree, stdout, 0, 0 );
900 if ( iret < 0 ) { return iret; }
901 #if ! defined(NO_LOGGING)
902 iret = out_board( ptree, pf_log, 0, 0 );
903 if ( iret < 0 ) { return iret; }
914 OutCsaShogi( "pong\n" );
921 cmd_hash( char **lasts )
923 const char *str = strtok_r( NULL, str_delimiters, lasts );
929 str_error = str_bad_cmdline;
933 if ( ! strcmp( str, "learn" ) )
935 str = strtok_r( NULL, str_delimiters, lasts );
936 if ( str != NULL && ! strcmp( str, str_on ) )
938 return hash_learn_on();
940 else if ( str != NULL && ! strcmp( str, str_off ) )
942 return hash_learn_off();
944 #if ! defined(MINIMUM)
945 else if ( str != NULL && ! strcmp( str, "create" ) )
947 return hash_learn_create();
951 str_error = str_bad_cmdline;
956 l = strtol( str, &ptr, 0 );
957 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
959 str_error = str_bad_cmdline;
963 AbortDifficultCommand;
965 log2_ntrans_table = (int)l;
966 memory_free( (void *)ptrans_table_orig );
967 return ini_trans_table();
972 cmd_limit( char **lasts )
974 const char *str = strtok_r( NULL, str_delimiters, lasts );
980 str_error = str_bad_cmdline;
984 AbortDifficultCommand;
986 if ( ! strcmp( str, "depth" ) )
988 str = strtok_r( NULL, str_delimiters, lasts );
991 str_error = str_bad_cmdline;
994 l1 = strtol( str, &ptr, 0 );
995 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
997 str_error = str_bad_cmdline;
1000 sec_limit_up = UINT_MAX;
1001 node_limit = UINT64_MAX;
1002 depth_limit = (int)l1;
1004 else if ( ! strcmp( str, "nodes" ) )
1006 str = strtok_r( NULL, str_delimiters, lasts );
1009 str_error = str_bad_cmdline;
1012 l1 = strtol( str, &ptr, 0 );
1013 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1015 str_error = str_bad_cmdline;
1018 sec_limit_up = UINT_MAX;
1019 depth_limit = PLY_MAX;
1020 node_limit = (uint64_t)l1;
1022 else if ( ! strcmp( str, "time" ) )
1024 str = strtok_r( NULL, str_delimiters, lasts );
1027 str_error = str_bad_cmdline;
1031 if ( ! strcmp( str, "extendable" ) )
1033 game_status |= flag_time_extendable;
1035 else if ( ! strcmp( str, "strict" ) )
1037 game_status &= ~flag_time_extendable;
1040 l1 = strtol( str, &ptr, 0 );
1041 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1043 str_error = str_bad_cmdline;
1047 str = strtok_r( NULL, str_delimiters, lasts );
1050 str_error = str_bad_cmdline;
1053 l2 = strtol( str, &ptr, 0 );
1054 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1056 str_error = str_bad_cmdline;
1060 str = strtok_r( NULL, str_delimiters, lasts );
1061 if ( ! str ) { l3 = -1; }
1063 l3 = strtol( str, &ptr, 0 );
1064 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1066 str_error = str_bad_cmdline;
1071 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1073 depth_limit = PLY_MAX;
1074 node_limit = UINT64_MAX;
1075 sec_limit = (unsigned int)l1 * 60U;
1076 sec_limit_up = (unsigned int)l2;
1077 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1078 else { sec_limit_depth = (unsigned int)l3; }
1082 str_error = str_bad_cmdline;
1091 cmd_read( tree_t * restrict ptree, char **lasts )
1093 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1094 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1095 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1096 const char *str_tmp;
1097 FILE *pf_src, *pf_dest;
1098 char str_file[SIZE_FILENAME];
1104 flag = flag_history | flag_rep | flag_detect_hang | flag_rejections;
1110 str_error = str_bad_cmdline;
1116 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1117 else if ( strcmp( str2, "nil" ) )
1119 str_error = str_bad_cmdline;
1126 l = strtol( str3, &ptr, 0 );
1127 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1129 str_error = str_bad_cmdline;
1132 moves = (unsigned int)l - 1U;
1135 AbortDifficultCommand;
1137 if ( ! strcmp( str1, "." ) )
1139 str_tmp = "game.cs_";
1141 #if defined(NO_LOGGING)
1142 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1144 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1145 str_dir_logs, irecord_game );
1147 pf_dest = file_open( str_tmp, "w" );
1148 if ( pf_dest == NULL ) { return -2; }
1150 pf_src = file_open( str_file, "r" );
1151 if ( pf_src == NULL )
1153 file_close( pf_dest );
1157 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1159 iret = file_close( pf_src );
1162 file_close( pf_dest );
1166 iret = file_close( pf_dest );
1167 if ( iret < 0 ) { return iret; }
1173 iret = read_record( ptree, str1, moves, flag );
1174 if ( iret < 0 ) { return iret; }
1176 iret = get_elapsed( &time_turn_start );
1177 if ( iret < 0 ) { return iret; }
1179 if ( str_tmp && remove( str_tmp ) )
1181 out_warning( "remove() failed." );
1190 cmd_resign( tree_t * restrict ptree, char **lasts )
1192 const char *str = strtok_r( NULL, str_delimiters, lasts );
1196 if ( str == NULL || *str == 'T' )
1198 AbortDifficultCommand;
1200 if ( game_status & mask_game_end ) { return 1; }
1202 #if defined(DEKUNOBOU)
1203 if ( dek_ngame && record_game.moves < 2 )
1205 str_error = "ignore resignation";
1210 game_status |= flag_resigned;
1211 renovate_time( root_turn );
1212 out_CSA( ptree, &record_game, MOVE_RESIGN );
1215 l = strtol( str, &ptr, 0 );
1216 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1218 str_error = str_bad_cmdline;
1221 resign_threshold = (int)l;
1229 cmd_move( tree_t * restrict ptree, char **lasts )
1231 const char *str = strtok_r( NULL, str_delimiters, lasts );
1235 if ( game_status & mask_game_end )
1237 str_error = str_game_ended;
1241 AbortDifficultCommand;
1245 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1247 str_error = str_bad_cmdline;
1251 iret = get_elapsed( &time_turn_start );
1252 if ( iret < 0 ) { return iret; }
1254 iret = com_turn_start( ptree, 0 );
1255 if ( iret < 0 ) { return iret; }
1257 else if ( ! strcmp( str, "restraint" ) )
1259 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1261 str_error = str_bad_cmdline;
1265 iret = get_elapsed( &time_turn_start );
1266 if ( iret < 0 ) { return iret; }
1268 iret = com_turn_start( ptree, flag_refer_rest );
1269 if ( iret < 0 ) { return iret; }
1273 iret = interpret_CSA_move( ptree, &move, str );
1274 if ( iret < 0 ) { return iret; }
1276 iret = get_elapsed( &time_turn_start );
1277 if ( iret < 0 ) { return iret; }
1279 #if defined(MNJ_LAN)
1280 if ( sckt_mnj != SCKT_NULL )
1282 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1285 if ( str2 ) { l = strtol( str2, &ptr, 0 ); }
1286 if ( ! str2 || ptr == str || l == LONG_MAX || l < 1 )
1288 str_error = str_bad_cmdline;
1291 mnj_posi_id = (int)l;
1292 mnj_move_last = move;
1296 iret = make_move_root( ptree, move, ( flag_history | flag_time | flag_rep
1298 | flag_rejections ) );
1299 if ( iret < 0 ) { return iret; }
1301 exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
1302 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1310 cmd_new( tree_t * restrict ptree, char **lasts )
1312 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1313 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1314 const min_posi_t *pmp;
1315 min_posi_t min_posi;
1318 AbortDifficultCommand;
1320 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1321 exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
1325 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1326 memset( &min_posi.asquare, empty, nsquare );
1327 min_posi.hand_black = min_posi.hand_white = 0;
1328 iret = read_board_rep1( str1, &min_posi );
1329 if ( iret < 0 ) { return iret; }
1333 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1334 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1336 str_error = str_bad_cmdline;
1340 else { min_posi.turn_to_move = black; }
1344 else { pmp = &min_posi_no_handicap; }
1346 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1347 if ( iret < 0 ) { return iret; }
1349 return get_elapsed( &time_turn_start );
1354 cmd_problem( tree_t * restrict ptree, char **lasts )
1356 const char *str = strtok_r( NULL, str_delimiters, lasts );
1359 unsigned int nposition;
1364 l = strtol( str, &ptr, 0 );
1365 if ( ptr == str || l == LONG_MAX || l < 1 )
1367 str_error = str_bad_cmdline;
1370 nposition = (unsigned int)l;
1372 else { nposition = UINT_MAX; }
1374 AbortDifficultCommand;
1376 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1377 if ( iret < 0 ) { return iret; }
1379 iret = solve_problems( ptree, nposition );
1382 record_close( &record_problems );
1386 iret = record_close( &record_problems );
1387 if ( iret < 0 ) { return iret; }
1389 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1390 if ( iret < 0 ) { return iret; }
1392 return get_elapsed( &time_turn_start );
1399 game_status |= flag_quit;
1407 if ( game_status & ( flag_pondering | flag_puzzling ) )
1409 game_status |= flag_quit_ponder;
1413 game_status |= flag_suspend;
1419 cmd_time( char **lasts )
1421 const char *str = strtok_r( NULL, str_delimiters, lasts );
1426 str_error = str_bad_cmdline;
1429 else if ( ! strcmp( str, "response" ) )
1432 str = strtok_r( NULL, str_delimiters, lasts );
1435 str_error = str_bad_cmdline;
1438 l = strtol( str, &ptr, 0 );
1439 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1441 str_error = str_bad_cmdline;
1444 time_response = (unsigned int)l;
1447 else if ( ! strcmp( str, "remain" ) )
1451 str = strtok_r( NULL, str_delimiters, lasts );
1454 str_error = str_bad_cmdline;
1457 l1 = strtol( str, &ptr, 0 );
1458 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1460 str_error = str_bad_cmdline;
1464 str = strtok_r( NULL, str_delimiters, lasts );
1467 str_error = str_bad_cmdline;
1470 l2 = strtol( str, &ptr, 0 );
1471 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1473 str_error = str_bad_cmdline;
1477 if ( sec_limit_up == UINT_MAX )
1479 str_error = str_bad_cmdline;
1483 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1486 str_error = str_bad_cmdline;
1491 #if !defined(MINIMUM)
1492 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1494 cmd_learn( tree_t * restrict ptree, char **lasts )
1496 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1497 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1498 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1499 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1501 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1502 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1506 unsigned int max_games;
1507 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1511 str_error = str_bad_cmdline;
1514 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1515 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1517 str_error = str_bad_cmdline;
1521 max_games = UINT_MAX;
1522 max_iterations = INT_MAX;
1523 nworker1 = nworker2 = nsteps = 1;
1527 l = strtol( str2, &ptr, 0 );
1528 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1530 str_error = str_bad_cmdline;
1538 l = strtol( str3, &ptr, 0 );
1539 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1541 str_error = str_bad_cmdline;
1544 if ( l > 0 ) { max_games = (unsigned int)l; }
1549 l = strtol( str4, &ptr, 0 );
1550 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1552 str_error = str_bad_cmdline;
1555 if ( l > 0 ) { max_iterations = (int)l; }
1561 l = strtol( str5, &ptr, 0 );
1562 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1564 str_error = str_bad_cmdline;
1572 l = strtol( str6, &ptr, 0 );
1573 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1575 str_error = str_bad_cmdline;
1582 AbortDifficultCommand;
1584 log2_ntrans_table = 12;
1586 memory_free( (void *)ptrans_table_orig );
1588 iret = ini_trans_table();
1589 if ( iret < 0 ) { return iret; }
1591 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1592 nworker1, nworker2 );
1593 if ( iret < 0 ) { return -1; }
1595 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1596 if ( iret < 0 ) { return -1; }
1598 iret = get_elapsed( &time_turn_start );
1599 if ( iret < 0 ) { return iret; }
1603 #endif /* MINIMUM */
1608 cmd_mpv( tree_t * restrict ptree, char **lasts )
1610 const char *str = strtok_r( NULL, str_delimiters, lasts );
1616 str_error = str_bad_cmdline;
1619 else if ( ! strcmp( str, "num" ) )
1621 str = strtok_r( NULL, str_delimiters, lasts );
1624 str_error = str_bad_cmdline;
1627 l = strtol( str, &ptr, 0 );
1628 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1630 str_error = str_bad_cmdline;
1634 AbortDifficultCommand;
1638 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
1642 else if ( ! strcmp( str, "width" ) )
1644 str = strtok_r( NULL, str_delimiters, lasts );
1647 str_error = str_bad_cmdline;
1650 l = strtol( str, &ptr, 0 );
1651 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1653 str_error = str_bad_cmdline;
1657 AbortDifficultCommand;
1661 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
1666 str_error = str_bad_cmdline;
1674 cmd_thread( char **lasts )
1676 const char *str = strtok_r( NULL, str_delimiters, lasts );
1680 str_error = str_bad_cmdline;
1683 else if ( ! strcmp( str, "num" ) )
1688 str = strtok_r( NULL, str_delimiters, lasts );
1691 str_error = str_bad_cmdline;
1694 l = strtol( str, &ptr, 0 );
1695 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
1697 str_error = str_bad_cmdline;
1705 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
1712 str_error = str_bad_cmdline;
1718 #if defined(CSA_LAN)
1720 cmd_connect( tree_t * restrict ptree, char **lasts )
1726 str = strtok_r( NULL, str_delimiters, lasts );
1727 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
1728 strncpy( client_str_addr, str, 255 );
1729 client_str_addr[255] = '\0';
1731 str = strtok_r( NULL, str_delimiters, lasts );
1732 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
1733 client_port = strtol( str, &ptr, 0 );
1734 if ( ptr == str || client_port == LONG_MAX || client_port < 0
1735 || client_port > USHRT_MAX )
1737 str_error = str_bad_cmdline;
1741 str = strtok_r( NULL, str_delimiters, lasts );
1742 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
1743 strncpy( client_str_id, str, 255 );
1744 client_str_id[255] = '\0';
1746 str = strtok_r( NULL, " \t", lasts );
1747 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
1748 strncpy( client_str_pwd, str, 255 );
1749 client_str_pwd[255] = '\0';
1751 str = strtok_r( NULL, str_delimiters, lasts );
1752 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
1754 max_games = strtol( str, &ptr, 0 );
1755 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
1757 str_error = str_bad_cmdline;
1760 client_max_game = max_games;
1763 AbortDifficultCommand;
1767 return client_next_game( ptree, client_str_addr, (int)client_port );
1772 #if defined(MNJ_LAN)
1774 cmd_mnj( tree_t * restrict ptree, char **lasts )
1776 char client_str_addr[256];
1777 char client_str_id[256];
1785 str = strtok_r( NULL, str_delimiters, lasts );
1788 str_error = str_bad_cmdline;
1791 l = strtol( str, &ptr, 0 );
1792 if ( ptr == str || l == LONG_MAX || l < 0 )
1794 str_error = str_bad_cmdline;
1800 str = strtok_r( NULL, str_delimiters, lasts );
1803 str_error = str_bad_cmdline;
1806 l = strtol( str, &ptr, 0 );
1807 if ( ptr == str || l == LONG_MAX || l < 0 )
1809 str_error = str_bad_cmdline;
1812 seed = (unsigned int)l;
1815 str = strtok_r( NULL, str_delimiters, lasts );
1816 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
1817 strncpy( client_str_addr, str, 255 );
1818 client_str_addr[255] = '\0';
1821 str = strtok_r( NULL, str_delimiters, lasts );
1822 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
1823 l = strtol( str, &ptr, 0 );
1824 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
1826 str_error = str_bad_cmdline;
1829 client_port = (int)l;
1832 str = strtok_r( NULL, str_delimiters, lasts );
1833 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
1834 strncpy( client_str_id, str, 255 );
1835 client_str_id[255] = '\0';
1837 AbortDifficultCommand;
1839 resign_threshold = 65535;
1840 game_status |= ( flag_noponder | flag_noprompt );
1841 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
1843 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
1844 if ( sckt_mnj == SCKT_NULL ) { return -2; }
1846 str_buffer_cmdline[0] = '\0';
1848 Out( "Sending my name %s", client_str_id );
1849 sckt_out( sckt_mnj, "%s\n", client_str_id );
1851 return analyze( ptree );
1856 #if defined(DEKUNOBOU)
1859 cmd_dek( char **lasts )
1861 const char *str = strtok_r( NULL, str_delimiters, lasts );
1868 str_error = str_bad_cmdline;
1871 strncpy( str_message, str, SIZE_MESSAGE-1 );
1872 str_message[SIZE_MESSAGE-1] = '\0';
1873 dek_ul_addr = inet_addr( str );
1875 str = strtok_r( NULL, str_delimiters, lasts );
1878 str_error = str_bad_cmdline;
1881 l1 = strtol( str, &ptr, 0 );
1882 if ( ptr == str || l1 == LONG_MAX || l1 < 0 || l1 > USHRT_MAX )
1884 str_error = str_bad_cmdline;
1888 str = strtok_r( NULL, str_delimiters, lasts );
1891 str_error = str_bad_cmdline;
1894 l2 = strtol( str, &ptr, 0 );
1895 if ( ptr == str || l2 == LONG_MAX || l2 < 0 || l2 > USHRT_MAX )
1897 str_error = str_bad_cmdline;
1901 AbortDifficultCommand;
1903 iret = dek_start( str_message, (int)l1, (int)l2 );
1904 if ( iret < 0 ) { return iret; }
1906 Out( "\n- in communication with Dekunobou...\n" );
1908 str_buffer_cmdline[0] = '\0';
1913 game_status |= flag_resigned;