23 #include "move_legal.h"
31 #include "xboard2uci.h"
35 #define StringSize 4096
39 static const bool UseDebug = FALSE;
40 static const bool DelayPong = FALSE;
46 bool computer[ColourNb];
47 bool playedAllMoves[ColourNb];
55 bool has_feature_memory;
57 bool has_feature_egt_nalimov;
58 bool has_feature_egt_gaviota;
63 bool new_hack; // "new" is a C++ keyword
86 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;
90 static state_t State[1];
95 static void comp_move (int move);
96 static void move_step (int move);
97 static void board_update ();
100 static void no_mess (int move);
102 static void search_update ();
103 static void search_clear ();
104 static void update_remaining_time();
105 static int report_best_score();
106 static bool kibitz_throttle (bool searching);
107 static void start_protected_command();
108 static void end_protected_command();
110 static bool active ();
111 static bool ponder ();
112 static bool ponder_ok (int ponder_move);
114 static void stop_search ();
116 static void send_board (int extra_move);
117 static void send_pv ();
118 static void send_info ();
120 static void send_xboard_options ();
122 static void learn (int result);
129 void xboard2uci_init() {
138 State->computer[White] = FALSE;
139 State->computer[Black] = TRUE;
141 State->exp_move = MoveNone;
142 State->hint_move = MoveNone;
143 State->resign_nb = 0;
144 my_timer_reset(State->timer);
146 // yes there are engines that do not have the "Hash" option....
147 XB->has_feature_memory= (option_find(Uci->option,"Hash")!=NULL);
148 XB->has_feature_smp = (uci_thread_option(Uci)!=NULL);
149 // TODO: support for other types of table bases
150 // This is a quick hack.
151 XB->has_feature_egt_nalimov = (option_find(Uci->option,"NalimovPath")!=NULL);
152 XB->has_feature_egt_gaviota = (option_find(Uci->option,"GaviotaTbPath")!=NULL);
154 XB->computer = FALSE;
156 my_string_set(&XB->name,"<empty>");
169 XB->time_limit = FALSE;
172 XB->depth_limit = FALSE;
176 XB->opp_time = 300.0;
182 static list_t move_list[1];
184 // xboard2uci_gui_step()
186 void xboard2uci_gui_step(char string[]) {
189 char move_string[256];
194 } else if (match(string,"accepted *")) {
198 } else if (match(string,"analyze")) {
200 State->computer[White] = FALSE;
201 State->computer[Black] = FALSE;
204 XB->new_hack = FALSE;
210 } else if (match(string,"bk")) {
212 if (option_get_bool(Option,"Book")) {
213 game_get_board(Game,board);
215 } else { // [HGM] without book, print all legal moves
216 int i, gen=!list_size(move_list);
217 game_get_board(Game,board);
218 if(gen) gen_legal_moves(move_list,board);
219 for(i=0; i<list_size(move_list); i++){
220 if(gen) move_list->value[i] = 0;
221 move_to_san(move_list->move[i],board,move_string,256);
222 printf(" %s%6s\n", move_list->value[i]? "* " : "", move_string);
224 // this is necessary by the xboard protocol
228 } else if (match(string,"exclude *") || match(string,"option Polyglot exclude move=*")) { // [HGM]
230 int i, all = !strcmp(Star[0], "all"), change=FALSE, cnt=0;
231 game_get_board(Game,board);
232 if(!list_size(move_list)) {
233 gen_legal_moves(move_list,board);
234 for(i=0; i<list_size(move_list); i++){
235 move_list->value[i] = 0;
238 move = move_from_san(Star[0],board);
240 for(i=0; i<list_size(move_list); i++){
241 if(all || move_list->move[i] == move)
242 change |= !move_list->value[i], move_list->value[i] = 1;
243 cnt += !move_list->value[i];
245 if(change && cnt) mess(); // do not relay to engine if no change or no moves left
247 } else if (match(string,"include *")) { // [HGM]
249 int i, all = !strcmp(Star[0], "all"), change = FALSE;
250 game_get_board(Game,board);
251 move = move_from_san(Star[0],board);
253 for(i=0; i<list_size(move_list); i++){
254 if(all || move_list->move[i] == move)
255 change |= move_list->value[i], move_list->value[i] = 0;
259 } else if (match(string,"black")) {
261 if (colour_is_black(game_turn(Game))) {
263 State->computer[White] = TRUE;
264 State->computer[Black] = FALSE;
272 } else if (match(string,"computer")) {
276 } else if (match(string,"draw")) {
277 if(option_find(Uci->option,"UCI_DrawOffers")){
278 my_log("POLYGLOT draw from XB received");
279 uci_send_option(Uci,"DrawOffer","%s","draw");}
280 else if (option_get_bool(Option,"HandleDraws") && Uci->root_move_nb > 20) { // [HGM] PG draw handling
281 my_log("POLYGLOT draw from XB received");
282 if (Uci->best_score <= -option_get_int(Option,"ContemptScore"))
283 gui_send(GUI,"offer draw");}
284 } else if (match(string,"easy")) {
290 } else if (match(string,"edit")) {
294 gui_send(GUI,"Error (unknown command): %s",string);
296 } else if (match(string,"exit")) {
298 State->computer[White] = FALSE;
299 State->computer[Black] = FALSE;
305 } else if (match(string,"force")) {
307 State->computer[White] = FALSE;
308 State->computer[Black] = FALSE;
312 } else if (match(string,"go")) {
314 State->computer[game_turn(Game)] = TRUE;
315 State->computer[colour_opp(game_turn(Game))] = FALSE;
317 XB->new_hack = FALSE;
323 } else if (match(string,"hard")) {
329 } else if (match(string,"hint")) {
332 game_get_board(Game,board);
333 if (option_get_bool(Option,"Book")) {
335 move = book_move(board,FALSE);
337 if(move==MoveNone && State->hint_move!=MoveNone){
338 move=State->hint_move;
341 if (move != MoveNone && move_is_legal(move,board)) {
342 move_to_san(move,board,move_string,256);
343 gui_send(GUI,"Hint: %s",move_string);
346 } else if (match(string,"ics *")) {
350 } else if (match(string,"level * *:* *")) {
352 XB->mps = atoi(Star[0]);
353 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
354 XB->inc = ((double)atoi(Star[3]));
356 } else if (match(string,"level * * *")) {
358 XB->mps = atoi(Star[0]);
359 XB->base = ((double)atoi(Star[1])) * 60.0;
360 XB->inc = ((double)atoi(Star[2]));
362 } else if (match(string,"name *")) {
364 my_string_set(&XB->name,Star[0]);
366 } else if (match(string,"new")) {
368 uci_send_isready_sync(Uci);
369 my_log("POLYGLOT NEW GAME\n");
371 option_set(Option,"Chess960","false");
375 move_list->size = 0; // [HGM] clear all exclude moves
378 State->computer[White] = FALSE;
379 State->computer[Black] = FALSE;
381 State->computer[White] = FALSE;
382 State->computer[Black] = TRUE;
383 State->playedAllMoves[White] = TRUE; // [HGM]
384 State->playedAllMoves[Black] = TRUE;
390 XB->depth_limit = FALSE;
393 XB->computer = FALSE;
394 my_string_set(&XB->name,"<empty>");
399 uci_send_ucinewgame(Uci);
401 } else if (match(string,"nopost")) {
405 } else if (match(string,"otim *")) {
407 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
408 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
410 } else if (match(string,"pause")) {
414 gui_send(GUI,"Error (unknown command): %s",string);
416 } else if (match(string,"ping *")) {
418 // HACK; TODO: answer only after an engine move
421 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
422 XB->ping = atoi(Star[0]);
423 uci_send_isready_sync(Uci);
425 ASSERT(XB->ping==-1);
426 gui_send(GUI,"pong %s",Star[0]);
428 } else if (match(string,"nps *")) {
430 // fake WB play-by-nodes mode
431 XB->node_rate = atoi(Star[0]);
432 } else if (match(string,"playother")) {
434 State->computer[game_turn(Game)] = FALSE;
435 State->computer[colour_opp(game_turn(Game))] = TRUE;
437 XB->new_hack = FALSE;
443 } else if (match(string,"post")) {
447 } else if (match(string,"protover *")) {
448 XB->proto_ver = atoi(Star[0]);
449 ASSERT(XB->proto_ver>=2);
450 send_xboard_options();
452 } else if (match(string,"quit")) {
453 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
455 } else if (match(string,"random")) {
459 } else if (match(string,"rating * *")) {
463 } else if (match(string,"remove")) {
465 if (game_pos(Game) >= 2) {
467 game_goto(Game,game_pos(Game)-2);
469 ASSERT(!XB->new_hack);
470 XB->new_hack = FALSE; // HACK?
477 } else if (match(string,"rejected *")) {
481 } else if (match(string,"reset")) { // protover 3?
485 gui_send(GUI,"Error (unknown command): %s",string);
488 || match(string,"result * {*}")
489 || match(string,"result * {* }")
490 || match(string,"result * { *}")
491 || match(string,"result * { * }")) {
493 my_log("POLYGLOT GAME END\n");
501 if (option_get_bool(Option,"Book") &&
502 option_get_bool(Option,"BookLearn")) {
505 } else if (my_string_equal(Star[0],"1-0")) {
507 } else if (my_string_equal(Star[0],"0-1")) {
509 } else if (my_string_equal(Star[0],"1/2-1/2")) {
513 } else if (match(string,"resume")) {
517 gui_send(GUI,"Error (unknown command): %s",string);
519 } else if (match(string,"option *=*") ||
520 match(string,"option * =*") ||
521 match(string,"option *= *") ||
522 match(string,"option * = *")
526 if(match(name, "Polyglot *")){
527 char *pg_name=Star[0];
528 polyglot_set_option(pg_name,value);
530 option_t *opt=option_find(Uci->option,name);
532 if(my_string_case_equal(opt->type,"check")){
533 value=my_string_equal(value,"1")?"true":"false";
535 start_protected_command();
536 uci_send_option(Uci, name, "%s", value);
537 end_protected_command();
539 gui_send(GUI,"Error (unknown option): %s",name);
542 } else if (match(string,"option *")){
544 if(match(name, "Polyglot *")){
545 char *pg_name=Star[0];
546 polyglot_set_option(pg_name,"<empty>");
548 start_protected_command();
550 if(!uci_send_option(Uci, name, "%s", "<empty>")){
551 gui_send(GUI,"Error (unknown option): %s",name);
553 end_protected_command();
555 } else if (XB->has_feature_smp && match(string,"cores *")){
556 int cores=atoi(Star[0]);
558 // updating the number of cores
559 my_log("POLYGLOT setting the number of cores to %d\n",cores);
560 start_protected_command();
561 uci_set_threads(Uci,cores);
562 end_protected_command();
565 gui_send(GUI,"Error (unknown command): %s",string);
567 } else if (match(string,"egtpath * *")){
570 if(my_string_empty(path)){
572 gui_send(GUI,"Error (unknown command): %s",string);
574 if(my_string_case_equal(type,"nalimov") && XB->has_feature_egt_nalimov){
575 // updating NalimovPath
576 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
577 start_protected_command();
578 uci_send_option(Uci,"NalimovPath","%s",path);
579 end_protected_command();
580 }else if(my_string_case_equal(type,"gaviota") && XB->has_feature_egt_gaviota){
581 // updating GaviotaPath
582 my_log("POLYGLOT setting the Gaviota path to %s\n",path);
583 start_protected_command();
584 uci_send_option(Uci,"GaviotaTbPath","%s",path);
585 end_protected_command();
588 gui_send(GUI,"Error (unsupported table base format): %s",string);
591 } else if (XB->has_feature_memory && match(string,"memory *")){
592 int memory = atoi(Star[0]);
596 // updating the available memory
598 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
599 if(XB->has_feature_egt_nalimov && (opt=option_find(Uci->option,"NalimovCache"))){
600 egt_cache=atoi(opt->value);
601 }else if(XB->has_feature_egt_gaviota &&
602 (opt=option_find(Uci->option,"GaviotaTbCache"))){
603 egt_cache=atoi(opt->value);
607 my_log("POLYGLOT EGTB Cache is %dMb\n",egt_cache);
608 real_memory=memory-egt_cache;
610 start_protected_command();
611 uci_send_option(Uci,"Hash", "%d", real_memory);
612 end_protected_command();
616 gui_send(GUI,"Error (unknown command): %s",string);
619 } else if (match(string,"sd *")) {
621 XB->depth_limit = TRUE;
622 XB->depth_max = atoi(Star[0]);
624 } else if (match(string,"setboard *")) {
626 my_log("POLYGLOT FEN %s\n",Star[0]);
628 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
630 move_list->size = 0; // [HGM] clear all exclude moves
632 State->computer[White] = FALSE;
633 State->computer[Black] = FALSE;
635 XB->new_hack = TRUE; // HACK?
641 } else if (match(string,"st *")) {
643 XB->time_limit = TRUE;
644 XB->time_max = ((double)atoi(Star[0]));
646 } else if (match(string,"time *")) {
648 XB->my_time = ((double)atoi(Star[0])) / 100.0;
649 if (XB->my_time < 0.0) XB->my_time = 0.0;
651 } else if (match(string,"undo")) {
653 move_list->size = 0; // [HGM] clear all exclude moves
655 if (game_pos(Game) >= 1) {
657 game_goto(Game,game_pos(Game)-1);
659 ASSERT(!XB->new_hack);
660 XB->new_hack = FALSE; // HACK?
667 } else if (match(string,"usermove *")) {
669 move_list->size = 0; // [HGM] clear all exclude moves
671 game_get_board(Game,board);
672 move = move_from_san(Star[0],board);
674 if (move != MoveNone && move_is_legal(move,board)) {
676 XB->new_hack = FALSE;
680 // [HGM] externally supplied move means we did not fully play the current stm
681 State->playedAllMoves[colour_is_white(game_turn(Game)) ? White : Black] = FALSE;
688 gui_send(GUI,"Illegal move: %s",Star[0]);
691 } else if (match(string,"variant *")) {
693 if (my_string_equal(Star[0],"fischerandom")) {
694 option_set(Option,"Chess960","true");
696 option_set(Option,"Chess960","false");
699 } else if (match(string,"white")) {
701 if (colour_is_white(game_turn(Game))) {
703 State->computer[White] = FALSE;
704 State->computer[Black] = TRUE;
712 } else if (match(string,"xboard")) {
716 } else if (match(string,".")) { // analyse info
718 if (State->state == ANALYSE) {
719 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
721 ASSERT(Uci->searching);
722 ASSERT(Uci->pending_nb>=1);
724 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
725 move_to_san(Uci->root_move,Uci->board,move_string,256);
726 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d %s",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,Uci->root_move_nb-(Uci->root_move_pos+1),Uci->root_move_nb,move_string);
728 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
732 } else if (match(string,"?")) { // move now
734 if (State->state == THINK) {
736 ASSERT(Uci->searching);
737 ASSERT(Uci->pending_nb>=1);
739 // HACK: just send "stop" to the engine
741 if (Uci->searching) {
742 my_log("POLYGLOT STOP SEARCH\n");
743 engine_send(Engine,"stop");
747 } else { // unknown command, maybe a move?
749 game_get_board(Game,board);
750 move = move_from_san(string,board);
752 if (move != MoveNone && move_is_legal(move,board)) {
754 XB->new_hack = FALSE;
761 } else if (move != MoveNone) {
763 gui_send(GUI,"Illegal move: %s",string);
767 gui_send(GUI,"Error (unknown command): %s",string);
773 // xboard2uci_engine_step()
775 void xboard2uci_engine_step(char string[]) {
779 event = uci_parse(Uci,string);
783 if ((event & EVENT_READY) != 0) {
785 // the engine is now ready
789 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
792 if (!DelayPong && XB->ping >= 0) {
793 gui_send(GUI,"pong %d",XB->ping);
798 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
800 // the engine is playing a move
802 // MEGA HACK: estimate remaining time because XBoard won't send it!
804 my_timer_stop(State->timer);
806 XB->my_time -= my_timer_elapsed_real(State->timer);
807 XB->my_time += XB->inc;
808 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
810 if (XB->my_time < 0.0) XB->my_time = 0.0;
812 // make sure to remember the ponder move
814 State->hint_move=Uci->ponder_move;
816 // play the engine move
818 comp_move(Uci->best_move);
822 if ((event & EVENT_PV) != 0) {
824 // the engine has sent a new PV
828 if ((event & EVENT_INFO) != 0) {
830 // the engine has sent info
834 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
835 my_log("POYGLOT draw offer/resign from engine\n");
836 if(option_find(Uci->option,"UCI_DrawOffers")){
837 if(event & EVENT_DRAW)
838 gui_send(GUI,"offer draw");
840 gui_send(GUI,"resign");
843 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){
844 game_get_board(Game,board);
845 if(board->turn==White){
846 gui_send(GUI,"0-1 {polyglot: resign"
847 " (illegal engine move by white: %s)}",Uci->bestmove);
849 gui_send(GUI,"1-0 {polyglot: resign"
850 " (illegal engine move by black: %s)}",Uci->bestmove);
858 // format_xboard_option_line
860 void format_xboard_option_line(char * option_line, option_t *opt){
862 char option_string[StringSize];
864 strcpy(option_line,"");
865 // buffer overflow alert
866 strcat(option_line,"feature option=\"");
868 strcat(option_line,"Polyglot ");
870 sprintf(option_string,"%s",opt->name);
871 strcat(option_line,option_string);
872 sprintf(option_string," -%s",opt->type);
873 strcat(option_line,option_string);
874 if(!IS_BUTTON(opt->type) && strcmp(opt->type,"combo")){
875 if(strcmp(opt->type,"check")){
876 sprintf(option_string," %s",opt->value);
878 sprintf(option_string," %d",
879 my_string_case_equal(opt->value,"true")||
880 my_string_equal(opt->value,"1")
883 strcat(option_line,option_string);
885 if(IS_SPIN(opt->type)){
886 sprintf(option_string," %s",opt->min);
887 strcat(option_line,option_string);
889 if(IS_SPIN(opt->type)){
890 sprintf(option_string," %s",opt->max);
891 strcat(option_line,option_string);
893 for(j=0;j<opt->var_nb;j++){
894 if(!strcmp(opt->var[j],opt->value)){
895 sprintf(option_string," *%s",opt->var[j]);
897 sprintf(option_string," %s",opt->var[j]);
899 strcat(option_line,option_string);
900 if(j!=opt->var_nb-1){
901 strcat(option_line," ///");
904 strcat(option_line,"\"");
905 if(option_get_bool(Option,"WbWorkArounds") &&
906 (tmp=strstr(option_line,"Draw"))){
908 my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",
913 // disarm() // [HGM] cleanse a string of offending double-quotes
915 static char*disarm(const char *s){
919 q = buf + strlen(buf) - 1;
920 while(*q == '"') *q-- = '\0'; // strip trailing quotes
921 while(*p == '"') p++; // strip leading quotes
922 while((q = strchr(p, '"'))) *q = '\''; // replace internal quotes
926 // send_xboard_options()
928 static void send_xboard_options(){
930 char egtfeature[StringSize];
933 gui_send(GUI,"feature done=0");
935 gui_send(GUI,"feature analyze=1");
936 gui_send(GUI,"feature exclude=1");
937 gui_send(GUI,"feature colors=0");
938 gui_send(GUI,"feature draw=1");
939 gui_send(GUI,"feature ics=1");
940 gui_send(GUI,"feature myname=\"%s\"",
941 disarm(option_get_string(Option,"EngineName")));
942 gui_send(GUI,"feature name=1");
943 gui_send(GUI,"feature pause=0");
944 gui_send(GUI,"feature ping=1");
945 gui_send(GUI,"feature playother=1");
946 gui_send(GUI,"feature sigint=1");
947 gui_send(GUI,"feature reuse=1");
948 gui_send(GUI,"feature san=0");
949 gui_send(GUI,"feature setboard=1");
950 gui_send(GUI,"feature sigint=0");
951 gui_send(GUI,"feature sigterm=0");
952 gui_send(GUI,"feature time=1");
953 gui_send(GUI,"feature usermove=1");
954 gui_send(GUI,"feature nps=1");
955 if (XB->has_feature_memory){
956 gui_send(GUI,"feature memory=1");
958 gui_send(GUI,"feature memory=0");
960 if (XB->has_feature_smp){
961 gui_send(GUI,"feature smp=1");
963 gui_send(GUI,"feature smp=0");
966 strncat(egtfeature,"feature egt=\"",StringSize);
967 if (XB->has_feature_egt_nalimov){
969 strncat(egtfeature,"nalimov",StringSize-strlen(egtfeature));
971 if (XB->has_feature_egt_gaviota){
973 strncat(egtfeature,",",StringSize-strlen(egtfeature));
975 strncat(egtfeature,"gaviota",StringSize-strlen(egtfeature));
977 strncat(egtfeature,"\"",StringSize-strlen(egtfeature));
978 egtfeature[StringSize-1]='\0';
979 gui_send(GUI,egtfeature);
981 if (option_find(Uci->option,"UCI_Chess960")) {
982 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
984 gui_send(GUI,"feature variants=\"normal\"");
987 xboard2uci_send_options();
990 void xboard2uci_send_options(){
991 char option_line[StringSize]="";
995 option_start_iter(Uci->option);
996 while((opt=option_next(Uci->option))){
997 if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;
998 if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;
999 if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;
1000 if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;
1001 if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;
1002 if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;
1003 if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;
1004 if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;
1005 if(my_string_case_equal(opt->name,"Ponder")) continue;
1006 if(my_string_case_equal(opt->name,"Hash")) continue;
1007 if(my_string_case_equal(opt->name,"NalimovPath")) continue;
1008 if(my_string_case_equal(opt->name,"GaviotaTbPath")) continue;
1009 if((name=uci_thread_option(Uci))!=NULL &&
1010 my_string_case_equal(opt->name,name)) continue;
1011 format_xboard_option_line(option_line,opt);
1013 gui_send(GUI,"%s",option_line);
1017 gui_send(GUI,"feature option=\"Polyglot exclude move -string \"");
1019 option_start_iter(Option);
1020 while((opt=option_next(Option))){
1021 if(opt->mode &XBOARD){
1022 format_xboard_option_line(option_line,opt);
1023 gui_send(GUI,"%s",option_line);
1026 gui_send(GUI,"feature done=1");
1030 // report_best_score()
1032 static int report_best_score(){
1033 if(!option_get_bool(Option,"ScoreWhite") ||
1034 colour_is_white(Uci->board->turn)){
1035 return Uci->best_score;
1037 return -Uci->best_score;
1043 static void comp_move(int move) {
1048 ASSERT(move_is_ok(move));
1050 ASSERT(State->state==THINK);
1051 ASSERT(!XB->analyse);
1053 if(option_get_bool(Option,"RepeatPV"))
1054 send_pv(); // to update time and nodes
1058 game_get_board(Game,board);
1060 if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
1061 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
1063 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
1066 gui_send(GUI,"move %s",string);
1070 if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {
1072 if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {
1075 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
1077 if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
1078 if (!option_get_bool(Option,"QueenNeverResigns") || !board_has_queen(board, board->turn)) { // [HGM] suppress resignig with Queen
1079 my_log("POLYGLOT *** RESIGN ***\n");
1080 gui_send(GUI,"resign");
1086 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
1087 State->resign_nb = 0;
1099 static void move_step(int move) {
1102 char move_string[256];
1104 ASSERT(move_is_ok(move));
1108 game_get_board(Game,board);
1110 if (move != MoveNone && move_is_legal(move,board)) {
1112 move_to_san(move,board,move_string,256);
1113 my_log("POLYGLOT MOVE %s\n",move_string);
1117 move_to_can(move,board,move_string,256);
1118 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
1121 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
1126 game_add_move(Game,move);
1132 static void board_update() {
1136 ASSERT(!XB->result);
1138 switch (game_status(Game)) {
1142 gui_send(GUI,"1-0 {White mates}");
1145 gui_send(GUI,"0-1 {Black mates}");
1148 gui_send(GUI,"1/2-1/2 {Stalemate}");
1151 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
1154 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
1156 case DRAW_REPETITION:
1157 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
1167 static void mess() {
1169 // clear state variables
1171 State->resign_nb = 0;
1172 State->exp_move = MoveNone;
1173 my_timer_reset(State->timer);
1175 // abort a possible search
1179 // calculate the new state
1182 } else if (!active()) {
1183 State->state = WAIT;
1184 my_log("POLYGLOT WAIT\n");
1185 } else if (XB->analyse) {
1186 State->state = ANALYSE;
1187 my_log("POLYGLOT ANALYSE\n");
1188 } else if (State->computer[game_turn(Game)]) {
1189 State->state = THINK;
1190 my_log("POLYGLOT THINK\n");
1192 State->state = WAIT;
1193 my_log("POLYGLOT WAIT\n");
1201 static void no_mess(int move) {
1203 ASSERT(move_is_ok(move));
1205 // just received a move, calculate the new state
1209 } else if (!active()) {
1211 stop_search(); // abort a possible search
1213 State->state = WAIT;
1214 State->exp_move = MoveNone;
1216 my_log("POLYGLOT WAIT\n");
1218 } else if (State->state == WAIT) {
1220 ASSERT(State->computer[game_turn(Game)]);
1221 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1222 ASSERT(!XB->analyse);
1224 my_log("POLYGLOT WAIT -> THINK\n");
1226 State->state = THINK;
1227 State->exp_move = MoveNone;
1229 } else if (State->state == THINK) {
1231 ASSERT(!State->computer[game_turn(Game)]);
1232 ASSERT(State->computer[colour_opp(game_turn(Game))]);
1233 ASSERT(!XB->analyse);
1235 if (ponder() && ponder_ok(Uci->ponder_move)) {
1237 my_log("POLYGLOT THINK -> PONDER\n");
1239 State->state = PONDER;
1240 State->exp_move = Uci->ponder_move;
1244 my_log("POLYGLOT THINK -> WAIT\n");
1246 State->state = WAIT;
1247 State->exp_move = MoveNone;
1250 } else if (State->state == PONDER) {
1252 ASSERT(State->computer[game_turn(Game)]);
1253 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1254 ASSERT(!XB->analyse);
1256 if (move == State->exp_move && Uci->searching) {
1258 ASSERT(Uci->searching);
1259 ASSERT(Uci->pending_nb>=1);
1261 my_timer_start(State->timer);//also resets
1263 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
1264 engine_send(Engine,"ponderhit");
1266 State->state = THINK;
1267 State->exp_move = MoveNone;
1269 send_pv(); // update display
1271 return; // do not launch a new search
1275 my_log("POLYGLOT PONDER -> THINK (miss)\n");
1279 State->state = THINK;
1280 State->exp_move = MoveNone;
1283 } else if (State->state == ANALYSE) {
1285 ASSERT(XB->analyse);
1287 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
1299 // start_protected_command()
1301 static void start_protected_command(){
1305 static void end_protected_command(){
1306 if(Uci->ready){ // not init faze
1307 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
1309 update_remaining_time();
1310 search_update(); // relaunch search if necessary
1313 // update_remaining_time()
1315 static void update_remaining_time(){
1317 if(State->timer->running){
1318 my_timer_stop(State->timer);
1319 reduce = my_timer_elapsed_real(State->timer);
1320 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
1321 XB->my_time -= reduce;
1322 if(XB->my_time<0.0){
1331 static void search_update() {
1337 ASSERT(!Uci->searching);
1342 // launch a new search if needed
1346 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
1348 // [VdB] moved up as we need the move number
1350 game_get_board(Game,Uci->board);
1354 if (State->state == THINK &&
1355 option_get_bool(Option,"Book") &&
1356 Uci->board->move_nb<option_get_int(Option,"BookDepth")
1360 move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));
1362 if (move != MoveNone && move_is_legal(move,Uci->board)) {
1364 my_log("POLYGLOT *BOOK MOVE*\n");
1366 search_clear(); // clears Uci->ponder_move
1367 Uci->best_move = move;
1369 board_copy(board,Uci->board);
1370 move_do(board,move);
1371 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
1373 Uci->best_pv[0] = Uci->best_move;
1374 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
1375 Uci->best_pv[2] = MoveNone;
1377 comp_move(Uci->best_move);
1385 my_log("POLYGLOT START SEARCH\n");
1389 uci_send_option(Uci,"UCI_Chess960","%s",
1390 option_get_bool(Option,"Chess960")?"true":"false");
1392 if (option_get_int(Option,"UCIVersion") >= 2) {
1393 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
1394 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
1397 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
1401 move = (State->state == PONDER) ? State->exp_move : MoveNone;
1402 send_board(move); // updates Uci->board global variable
1406 if (State->state == THINK || State->state == PONDER) {
1408 engine_send_queue(Engine,"go");
1410 if (XB->time_limit) {
1412 // fixed time per move
1414 if(XB->node_rate > 0){
1415 engine_send_queue(Engine,
1417 XB->time_max*((double)XB->node_rate));
1419 double computed_time;
1421 st_fudge=(double) option_get_int(Option,"STFudge");
1422 my_log("POLYGLOT Giving engine %.0fmsec extra time.\n",st_fudge);
1423 computed_time=XB->time_max*1000.0-st_fudge;
1424 if(computed_time< 1.0){
1427 engine_send_queue(Engine,
1436 if(XB->node_rate > 0) {
1440 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1442 time = XB->my_time / move_nb;
1446 if(time > XB->my_time){
1449 engine_send_queue(Engine,
1451 time*XB->node_rate);
1454 if (colour_is_white(Uci->board->turn)) {
1455 engine_send_queue(Engine,
1456 " wtime %.0f btime %.0f",
1457 XB->my_time*1000.0,XB->opp_time*1000.0);
1459 engine_send_queue(Engine,
1460 " wtime %.0f btime %.0f",
1461 XB->opp_time*1000.0,XB->my_time*1000.0);
1464 if (XB->inc != 0.0){
1465 engine_send_queue(Engine,
1466 " winc %.0f binc %.0f",
1467 XB->inc*1000.0,XB->inc*1000.0);
1471 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1472 ASSERT(move_nb>=1&&move_nb<=XB->mps);
1474 engine_send_queue(Engine," movestogo %d",move_nb);
1478 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
1480 if (State->state == PONDER) engine_send_queue(Engine," ponder");
1482 engine_send(Engine,""); // newline
1484 } else if (State->state == ANALYSE) {
1486 char move_string[256];
1488 engine_send_queue(Engine,"go infinite");
1490 if(list_size(move_list)) {
1492 game_get_board(Game,board);
1493 engine_send_queue(Engine," searchmoves");
1494 for(i=0; i<list_size(move_list); i++) {
1495 if(!move_list->value[i]) {
1496 move_to_can(move_list->move[i],board,move_string,256);
1497 engine_send_queue(Engine," %s",move_string);
1501 engine_send(Engine,""); // newline
1510 ASSERT(!Uci->searching);
1514 Uci->searching = TRUE;
1521 static void search_clear() {
1527 my_timer_start(State->timer);//also resets
1532 static bool active() {
1536 if (game_status(Game) != PLAYING) return FALSE; // game ended
1540 if (XB->analyse) return TRUE; // analysing
1541 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
1542 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
1544 return TRUE; // playing
1549 static bool ponder() {
1551 return XB->ponder && (option_get_bool(Option,"CanPonder") ||
1552 option_find(Uci->option,"Ponder"));
1556 static bool ponder_ok(int move) {
1560 ASSERT(move==MoveNone||move_is_ok(move));
1562 // legal ponder move?
1564 if (move == MoveNone) return FALSE;
1566 game_get_board(Game,board);
1567 if (!move_is_legal(move,board)) return FALSE;
1569 // UCI-legal resulting position?
1571 game_add_move(Game,move);
1573 game_get_board(Game,board);
1574 status = game_status(Game);
1576 game_rem_move(Game);
1578 if (status != PLAYING) return FALSE; // game ended
1580 if (option_get_bool(Option,"Book") && is_in_book(board)) {
1589 static void stop_search() {
1591 if (Uci->searching) {
1593 ASSERT(Uci->searching);
1594 ASSERT(Uci->pending_nb>=1);
1596 my_log("POLYGLOT STOP SEARCH\n");
1599 engine_send(Engine,"stop");
1600 Uci->searching = FALSE;
1603 if (option_get_bool(Option,"SyncStop")) {
1604 uci_send_stop_sync(Uci);
1613 static void send_board(int extra_move) {
1622 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
1624 ASSERT(!Uci->searching);
1628 game_get_board(Game,Uci->board);
1629 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
1631 board_to_fen(Uci->board,fen,256);
1632 my_log("POLYGLOT FEN %s\n",fen);
1634 ASSERT(board_can_play(Uci->board));
1639 end = game_pos(Game);
1644 game_get_board_ex(Game,board,start);
1645 board_to_fen(board,string,256);
1647 engine_send_queue(Engine,"position");
1649 if (my_string_equal(string,StartFen)) {
1650 engine_send_queue(Engine," startpos");
1652 engine_send_queue(Engine," fen %s",string);
1657 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
1659 for (pos = start; pos < end; pos++) { // game moves
1661 move = game_move(Game,pos);
1663 move_to_can(move,board,string,256);
1664 engine_send_queue(Engine," %s",string);
1666 move_do(board,move);
1669 if (extra_move != MoveNone) { // move to ponder on
1670 move_to_can(extra_move,board,string,256);
1671 engine_send_queue(Engine," %s",string);
1676 engine_send(Engine,""); // newline
1681 static void send_info() {
1683 if(option_get_bool(Option,"WbWorkArounds2")){
1684 // Silly bug in some versions of WinBoard.
1685 // depth <=1 clears the engine output window.
1686 // Why shouldn't an engine be allowed to send info at depth 1?
1691 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
1692 0,0.0,U64(0),Uci->info);
1697 static void send_pv() {
1699 char pv_string[StringSize];
1702 char move_string[StringSize];
1704 ASSERT(State->state!=WAIT);
1706 if (Uci->best_depth == 0) return;
1708 // xboard search information
1712 if (State->state == THINK || State->state == ANALYSE) {
1714 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1716 if(Uci->depth==-1) //hack to clear the engine output window
1717 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);
1719 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);
1721 } else if (State->state == PONDER &&
1722 option_get_bool(Option,"ShowPonder")) {
1724 game_get_board(Game,board);
1725 move = State->exp_move;
1727 if (move != MoveNone && move_is_legal(move,board)) {
1728 move_to_san(move,board,move_string,256);
1729 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1730 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" (%s) %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,move_string,pv_string);
1737 if ((Uci->searching &&
1738 option_get_bool(Option,"KibitzPV") &&
1739 Uci->time >= option_get_double(Option,"KibitzDelay"))
1740 || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {
1742 if (State->state == THINK || State->state == ANALYSE) {
1744 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1745 if(kibitz_throttle(Uci->searching)){
1746 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"%s\"",option_get_string(Option,"KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,pv_string);
1748 } else if (State->state == PONDER) {
1750 game_get_board(Game,board);
1751 move = State->exp_move;
1753 if (move != MoveNone && move_is_legal(move,board)) {
1754 move_to_san(move,board,move_string,256);
1755 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1756 if(kibitz_throttle(Uci->searching)){
1757 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"(%s) %s\"",option_get_string(Option,"KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,move_string,pv_string);
1764 // kibitz_throttle()
1766 static bool kibitz_throttle(bool searching){
1768 static time_t lastKibitzMove=0;
1769 static time_t lastKibitzPV=0;
1770 curr_time = time(NULL);
1771 if(searching){ // KibitzPV
1773 (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
1774 lastKibitzPV=curr_time;
1777 }else{ // KibitzMove
1779 (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
1780 lastKibitzPV=curr_time;
1781 lastKibitzMove=curr_time;
1790 static void learn(int result) {
1796 ASSERT(result>=-1&&result<=+1);
1799 // ASSERT(State->computer[White]||State->computer[Black]);
1805 // [HGM] does not account for the hypothetical possibility we played both sides!
1806 if (State->playedAllMoves[White]) {
1808 } else if (State->playedAllMoves[Black]) {
1812 return; // [HGM] if we did not play all moves for some side, do not learn, but don't make a fuss!
1816 } else if (result > 0) {
1817 my_log("POLYGLOT *LEARN WIN*\n");
1818 } else if (result < 0) {
1819 my_log("POLYGLOT *LEARN LOSS*\n");
1821 my_log("POLYGLOT *LEARN DRAW*\n");
1826 for (; pos < Game->size; pos += 2) {
1828 game_get_board_ex(Game,board,pos);
1829 move = game_move(Game,pos);
1831 book_learn_move(board,move,result);
1837 // end of xboard2uci.c