23 #include "move_legal.h"
30 #include "xboard2uci.h"
34 #define StringSize 4096
38 static const bool UseDebug = FALSE;
39 static const bool DelayPong = FALSE;
45 bool computer[ColourNb];
46 bool playedAllMoves[ColourNb];
54 bool has_feature_memory;
56 bool has_feature_egt_nalimov;
57 bool has_feature_egt_gaviota;
62 bool new_hack; // "new" is a C++ keyword
85 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;
89 static state_t State[1];
94 static void comp_move (int move);
95 static void move_step (int move);
96 static void board_update ();
99 static void no_mess (int move);
101 static void search_update ();
102 static void search_clear ();
103 static void update_remaining_time();
104 static int report_best_score();
105 static bool kibitz_throttle (bool searching);
106 static void start_protected_command();
107 static void end_protected_command();
109 static bool active ();
110 static bool ponder ();
111 static bool ponder_ok (int ponder_move);
113 static void stop_search ();
115 static void send_board (int extra_move);
116 static void send_pv ();
117 static void send_info ();
119 static void send_xboard_options ();
121 static void learn (int result);
128 void xboard2uci_init() {
137 State->computer[White] = FALSE;
138 State->computer[Black] = TRUE;
140 State->exp_move = MoveNone;
141 State->hint_move = MoveNone;
142 State->resign_nb = 0;
143 my_timer_reset(State->timer);
145 // yes there are engines that do not have the "Hash" option....
146 XB->has_feature_memory= (option_find(Uci->option,"Hash")!=NULL);
147 XB->has_feature_smp = (uci_thread_option(Uci)!=NULL);
148 // TODO: support for other types of table bases
149 // This is a quick hack.
150 XB->has_feature_egt_nalimov = (option_find(Uci->option,"NalimovPath")!=NULL);
151 XB->has_feature_egt_gaviota = (option_find(Uci->option,"GaviotaTbPath")!=NULL);
153 XB->computer = FALSE;
155 my_string_set(&XB->name,"<empty>");
168 XB->time_limit = FALSE;
171 XB->depth_limit = FALSE;
175 XB->opp_time = 300.0;
180 // xboard2uci_gui_step()
182 void xboard2uci_gui_step(char string[]) {
185 char move_string[256];
190 } else if (match(string,"accepted *")) {
194 } else if (match(string,"analyze")) {
196 State->computer[White] = FALSE;
197 State->computer[Black] = FALSE;
200 XB->new_hack = FALSE;
206 } else if (match(string,"bk")) {
208 if (option_get_bool(Option,"Book")) {
209 game_get_board(Game,board);
213 } else if (match(string,"black")) {
215 if (colour_is_black(game_turn(Game))) {
217 State->computer[White] = TRUE;
218 State->computer[Black] = FALSE;
226 } else if (match(string,"computer")) {
230 } else if (match(string,"draw")) {
231 if(option_find(Uci->option,"UCI_DrawOffers")){
232 my_log("POLYGLOT draw from XB received");
233 uci_send_option(Uci,"DrawOffer","%s","draw");}
234 } else if (match(string,"easy")) {
240 } else if (match(string,"edit")) {
244 gui_send(GUI,"Error (unknown command): %s",string);
246 } else if (match(string,"exit")) {
248 State->computer[White] = FALSE;
249 State->computer[Black] = FALSE;
255 } else if (match(string,"force")) {
257 State->computer[White] = FALSE;
258 State->computer[Black] = FALSE;
262 } else if (match(string,"go")) {
264 State->computer[game_turn(Game)] = TRUE;
265 State->computer[colour_opp(game_turn(Game))] = FALSE;
267 XB->new_hack = FALSE;
273 } else if (match(string,"hard")) {
279 } else if (match(string,"hint")) {
282 game_get_board(Game,board);
283 if (option_get_bool(Option,"Book")) {
285 move = book_move(board,FALSE);
287 if(move==MoveNone && State->hint_move!=MoveNone){
288 move=State->hint_move;
291 if (move != MoveNone && move_is_legal(move,board)) {
292 move_to_san(move,board,move_string,256);
293 gui_send(GUI,"Hint: %s",move_string);
296 } else if (match(string,"ics *")) {
300 } else if (match(string,"level * *:* *")) {
302 XB->mps = atoi(Star[0]);
303 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
304 XB->inc = ((double)atoi(Star[3]));
306 } else if (match(string,"level * * *")) {
308 XB->mps = atoi(Star[0]);
309 XB->base = ((double)atoi(Star[1])) * 60.0;
310 XB->inc = ((double)atoi(Star[2]));
312 } else if (match(string,"name *")) {
314 my_string_set(&XB->name,Star[0]);
316 } else if (match(string,"new")) {
318 uci_send_isready_sync(Uci);
319 my_log("POLYGLOT NEW GAME\n");
321 option_set(Option,"Chess960","false");
326 State->computer[White] = FALSE;
327 State->computer[Black] = FALSE;
329 State->computer[White] = FALSE;
330 State->computer[Black] = TRUE;
331 State->playedAllMoves[White] = TRUE; // [HGM]
332 State->playedAllMoves[Black] = TRUE;
338 XB->depth_limit = FALSE;
341 XB->computer = FALSE;
342 my_string_set(&XB->name,"<empty>");
347 uci_send_ucinewgame(Uci);
349 } else if (match(string,"nopost")) {
353 } else if (match(string,"otim *")) {
355 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
356 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
358 } else if (match(string,"pause")) {
362 gui_send(GUI,"Error (unknown command): %s",string);
364 } else if (match(string,"ping *")) {
366 // HACK; TODO: answer only after an engine move
369 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
370 XB->ping = atoi(Star[0]);
371 uci_send_isready_sync(Uci);
373 ASSERT(XB->ping==-1);
374 gui_send(GUI,"pong %s",Star[0]);
376 } else if (match(string,"nps *")) {
378 // fake WB play-by-nodes mode
379 XB->node_rate = atoi(Star[0]);
380 } else if (match(string,"playother")) {
382 State->computer[game_turn(Game)] = FALSE;
383 State->computer[colour_opp(game_turn(Game))] = TRUE;
385 XB->new_hack = FALSE;
391 } else if (match(string,"post")) {
395 } else if (match(string,"protover *")) {
396 XB->proto_ver = atoi(Star[0]);
397 ASSERT(XB->proto_ver>=2);
398 send_xboard_options();
400 } else if (match(string,"quit")) {
401 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
403 } else if (match(string,"random")) {
407 } else if (match(string,"rating * *")) {
411 } else if (match(string,"remove")) {
413 if (game_pos(Game) >= 2) {
415 game_goto(Game,game_pos(Game)-2);
417 ASSERT(!XB->new_hack);
418 XB->new_hack = FALSE; // HACK?
425 } else if (match(string,"rejected *")) {
429 } else if (match(string,"reset")) { // protover 3?
433 gui_send(GUI,"Error (unknown command): %s",string);
436 || match(string,"result * {*}")
437 || match(string,"result * {* }")
438 || match(string,"result * { *}")
439 || match(string,"result * { * }")) {
441 my_log("POLYGLOT GAME END\n");
449 if (option_get_bool(Option,"Book") &&
450 option_get_bool(Option,"BookLearn")) {
453 } else if (my_string_equal(Star[0],"1-0")) {
455 } else if (my_string_equal(Star[0],"0-1")) {
457 } else if (my_string_equal(Star[0],"1/2-1/2")) {
461 } else if (match(string,"resume")) {
465 gui_send(GUI,"Error (unknown command): %s",string);
467 } else if (match(string,"option *=*") ||
468 match(string,"option * =*") ||
469 match(string,"option *= *") ||
470 match(string,"option * = *")
474 if(match(name, "Polyglot *")){
475 char *pg_name=Star[0];
476 polyglot_set_option(pg_name,value);
478 option_t *opt=option_find(Uci->option,name);
480 if(my_string_case_equal(opt->type,"check")){
481 value=my_string_equal(value,"1")?"true":"false";
483 start_protected_command();
484 uci_send_option(Uci, name, "%s", value);
485 end_protected_command();
487 gui_send(GUI,"Error (unknown option): %s",name);
490 } else if (match(string,"option *")){
492 if(match(name, "Polyglot *")){
493 char *pg_name=Star[0];
494 polyglot_set_option(pg_name,"<empty>");
496 start_protected_command();
498 if(!uci_send_option(Uci, name, "%s", "<empty>")){
499 gui_send(GUI,"Error (unknown option): %s",name);
501 end_protected_command();
503 } else if (XB->has_feature_smp && match(string,"cores *")){
504 int cores=atoi(Star[0]);
506 // updating the number of cores
507 my_log("POLYGLOT setting the number of cores to %d\n",cores);
508 start_protected_command();
509 uci_set_threads(Uci,cores);
510 end_protected_command();
513 gui_send(GUI,"Error (unknown command): %s",string);
515 } else if (match(string,"egtpath * *")){
518 if(my_string_empty(path)){
520 gui_send(GUI,"Error (unknown command): %s",string);
522 if(my_string_case_equal(type,"nalimov") && XB->has_feature_egt_nalimov){
523 // updating NalimovPath
524 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
525 start_protected_command();
526 uci_send_option(Uci,"NalimovPath","%s",path);
527 end_protected_command();
528 }else if(my_string_case_equal(type,"gaviota") && XB->has_feature_egt_gaviota){
529 // updating GaviotaPath
530 my_log("POLYGLOT setting the Gaviota path to %s\n",path);
531 start_protected_command();
532 uci_send_option(Uci,"GaviotaTbPath","%s",path);
533 end_protected_command();
536 gui_send(GUI,"Error (unsupported table base format): %s",string);
539 } else if (XB->has_feature_memory && match(string,"memory *")){
540 int memory = atoi(Star[0]);
544 // updating the available memory
546 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
547 if(XB->has_feature_egt_nalimov && (opt=option_find(Uci->option,"NalimovCache"))){
548 egt_cache=atoi(opt->value);
549 }else if(XB->has_feature_egt_gaviota &&
550 (opt=option_find(Uci->option,"GaviotaTbCache"))){
551 egt_cache=atoi(opt->value);
555 my_log("POLYGLOT EGTB Cache is %dMb\n",egt_cache);
556 real_memory=memory-egt_cache;
558 start_protected_command();
559 uci_send_option(Uci,"Hash", "%d", real_memory);
560 end_protected_command();
564 gui_send(GUI,"Error (unknown command): %s",string);
567 } else if (match(string,"sd *")) {
569 XB->depth_limit = TRUE;
570 XB->depth_max = atoi(Star[0]);
572 } else if (match(string,"setboard *")) {
574 my_log("POLYGLOT FEN %s\n",Star[0]);
576 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
578 State->computer[White] = FALSE;
579 State->computer[Black] = FALSE;
581 XB->new_hack = TRUE; // HACK?
587 } else if (match(string,"st *")) {
589 XB->time_limit = TRUE;
590 XB->time_max = ((double)atoi(Star[0]));
592 } else if (match(string,"time *")) {
594 XB->my_time = ((double)atoi(Star[0])) / 100.0;
595 if (XB->my_time < 0.0) XB->my_time = 0.0;
597 } else if (match(string,"undo")) {
599 if (game_pos(Game) >= 1) {
601 game_goto(Game,game_pos(Game)-1);
603 ASSERT(!XB->new_hack);
604 XB->new_hack = FALSE; // HACK?
611 } else if (match(string,"usermove *")) {
613 game_get_board(Game,board);
614 move = move_from_san(Star[0],board);
616 if (move != MoveNone && move_is_legal(move,board)) {
618 XB->new_hack = FALSE;
622 // [HGM] externally supplied move means we did not fully play the current stm
623 State->playedAllMoves[colour_is_white(game_turn(Game)) ? White : Black] = FALSE;
630 gui_send(GUI,"Illegal move: %s",Star[0]);
633 } else if (match(string,"variant *")) {
635 if (my_string_equal(Star[0],"fischerandom")) {
636 option_set(Option,"Chess960","true");
638 option_set(Option,"Chess960","false");
641 } else if (match(string,"white")) {
643 if (colour_is_white(game_turn(Game))) {
645 State->computer[White] = FALSE;
646 State->computer[Black] = TRUE;
654 } else if (match(string,"xboard")) {
658 } else if (match(string,".")) { // analyse info
660 if (State->state == ANALYSE) {
661 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
663 ASSERT(Uci->searching);
664 ASSERT(Uci->pending_nb>=1);
666 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
667 move_to_san(Uci->root_move,Uci->board,move_string,256);
668 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);
670 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
674 } else if (match(string,"?")) { // move now
676 if (State->state == THINK) {
678 ASSERT(Uci->searching);
679 ASSERT(Uci->pending_nb>=1);
681 // HACK: just send "stop" to the engine
683 if (Uci->searching) {
684 my_log("POLYGLOT STOP SEARCH\n");
685 engine_send(Engine,"stop");
689 } else { // unknown command, maybe a move?
691 game_get_board(Game,board);
692 move = move_from_san(string,board);
694 if (move != MoveNone && move_is_legal(move,board)) {
696 XB->new_hack = FALSE;
703 } else if (move != MoveNone) {
705 gui_send(GUI,"Illegal move: %s",string);
709 gui_send(GUI,"Error (unknown command): %s",string);
715 // xboard2uci_engine_step()
717 void xboard2uci_engine_step(char string[]) {
721 event = uci_parse(Uci,string);
725 if ((event & EVENT_READY) != 0) {
727 // the engine is now ready
731 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
734 if (!DelayPong && XB->ping >= 0) {
735 gui_send(GUI,"pong %d",XB->ping);
740 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
742 // the engine is playing a move
744 // MEGA HACK: estimate remaining time because XBoard won't send it!
746 my_timer_stop(State->timer);
748 XB->my_time -= my_timer_elapsed_real(State->timer);
749 XB->my_time += XB->inc;
750 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
752 if (XB->my_time < 0.0) XB->my_time = 0.0;
754 // make sure to remember the ponder move
756 State->hint_move=Uci->ponder_move;
758 // play the engine move
760 comp_move(Uci->best_move);
764 if ((event & EVENT_PV) != 0) {
766 // the engine has sent a new PV
770 if ((event & EVENT_INFO) != 0) {
772 // the engine has sent info
776 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
777 my_log("POYGLOT draw offer/resign from engine\n");
778 if(option_find(Uci->option,"UCI_DrawOffers")){
779 if(event & EVENT_DRAW)
780 gui_send(GUI,"offer draw");
782 gui_send(GUI,"resign");
785 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){
786 game_get_board(Game,board);
787 if(board->turn==White){
788 gui_send(GUI,"0-1 {polyglot: resign"
789 " (illegal engine move by white: %s)}",Uci->bestmove);
791 gui_send(GUI,"1-0 {polyglot: resign"
792 " (illegal engine move by black: %s)}",Uci->bestmove);
800 // format_xboard_option_line
802 void format_xboard_option_line(char * option_line, option_t *opt){
804 char option_string[StringSize];
806 strcpy(option_line,"");
807 // buffer overflow alert
808 strcat(option_line,"feature option=\"");
810 strcat(option_line,"Polyglot ");
812 sprintf(option_string,"%s",opt->name);
813 strcat(option_line,option_string);
814 sprintf(option_string," -%s",opt->type);
815 strcat(option_line,option_string);
816 if(!IS_BUTTON(opt->type) && strcmp(opt->type,"combo")){
817 if(strcmp(opt->type,"check")){
818 sprintf(option_string," %s",opt->value);
820 sprintf(option_string," %d",
821 my_string_case_equal(opt->value,"true")||
822 my_string_equal(opt->value,"1")
825 strcat(option_line,option_string);
827 if(IS_SPIN(opt->type)){
828 sprintf(option_string," %s",opt->min);
829 strcat(option_line,option_string);
831 if(IS_SPIN(opt->type)){
832 sprintf(option_string," %s",opt->max);
833 strcat(option_line,option_string);
835 for(j=0;j<opt->var_nb;j++){
836 if(!strcmp(opt->var[j],opt->value)){
837 sprintf(option_string," *%s",opt->var[j]);
839 sprintf(option_string," %s",opt->var[j]);
841 strcat(option_line,option_string);
842 if(j!=opt->var_nb-1){
843 strcat(option_line," ///");
846 strcat(option_line,"\"");
847 if(option_get_bool(Option,"WbWorkArounds") &&
848 (tmp=strstr(option_line,"Draw"))){
850 my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",
855 // disarm() // [HGM] cleanse a string of offending double-quotes
857 static char*disarm(const char *s){
861 q = buf + strlen(buf) - 1;
862 while(*q == '"') *q-- = '\0'; // strip trailing quotes
863 while(*p == '"') p++; // strip leading quotes
864 while((q = strchr(p, '"'))) *q = '\''; // replace internal quotes
868 // send_xboard_options()
870 static void send_xboard_options(){
872 char egtfeature[StringSize];
875 gui_send(GUI,"feature done=0");
877 gui_send(GUI,"feature analyze=1");
878 gui_send(GUI,"feature colors=0");
879 gui_send(GUI,"feature draw=1");
880 gui_send(GUI,"feature ics=1");
881 gui_send(GUI,"feature myname=\"%s\"",
882 disarm(option_get_string(Option,"EngineName")));
883 gui_send(GUI,"feature name=1");
884 gui_send(GUI,"feature pause=0");
885 gui_send(GUI,"feature ping=1");
886 gui_send(GUI,"feature playother=1");
887 gui_send(GUI,"feature sigint=1");
888 gui_send(GUI,"feature reuse=1");
889 gui_send(GUI,"feature san=0");
890 gui_send(GUI,"feature setboard=1");
891 gui_send(GUI,"feature sigint=0");
892 gui_send(GUI,"feature sigterm=0");
893 gui_send(GUI,"feature time=1");
894 gui_send(GUI,"feature usermove=1");
895 gui_send(GUI,"feature nps=1");
896 if (XB->has_feature_memory){
897 gui_send(GUI,"feature memory=1");
899 gui_send(GUI,"feature memory=0");
901 if (XB->has_feature_smp){
902 gui_send(GUI,"feature smp=1");
904 gui_send(GUI,"feature smp=0");
907 strncat(egtfeature,"feature egt=\"",StringSize);
908 if (XB->has_feature_egt_nalimov){
910 strncat(egtfeature,"nalimov",StringSize-strlen(egtfeature));
912 if (XB->has_feature_egt_gaviota){
914 strncat(egtfeature,",",StringSize-strlen(egtfeature));
916 strncat(egtfeature,"gaviota",StringSize-strlen(egtfeature));
918 strncat(egtfeature,"\"",StringSize-strlen(egtfeature));
919 egtfeature[StringSize-1]='\0';
920 gui_send(GUI,egtfeature);
922 if (option_find(Uci->option,"UCI_Chess960")) {
923 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
925 gui_send(GUI,"feature variants=\"normal\"");
928 xboard2uci_send_options();
931 void xboard2uci_send_options(){
932 char option_line[StringSize]="";
936 option_start_iter(Uci->option);
937 while((opt=option_next(Uci->option))){
938 if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;
939 if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;
940 if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;
941 if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;
942 if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;
943 if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;
944 if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;
945 if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;
946 if(my_string_case_equal(opt->name,"Ponder")) continue;
947 if(my_string_case_equal(opt->name,"Hash")) continue;
948 if(my_string_case_equal(opt->name,"NalimovPath")) continue;
949 if(my_string_case_equal(opt->name,"GaviotaTbPath")) continue;
950 if((name=uci_thread_option(Uci))!=NULL &&
951 my_string_case_equal(opt->name,name)) continue;
952 format_xboard_option_line(option_line,opt);
954 gui_send(GUI,"%s",option_line);
958 option_start_iter(Option);
959 while((opt=option_next(Option))){
960 if(opt->mode &XBOARD){
961 format_xboard_option_line(option_line,opt);
962 gui_send(GUI,"%s",option_line);
965 gui_send(GUI,"feature done=1");
969 // report_best_score()
971 static int report_best_score(){
972 if(!option_get_bool(Option,"ScoreWhite") ||
973 colour_is_white(Uci->board->turn)){
974 return Uci->best_score;
976 return -Uci->best_score;
982 static void comp_move(int move) {
987 ASSERT(move_is_ok(move));
989 ASSERT(State->state==THINK);
990 ASSERT(!XB->analyse);
992 if(option_get_bool(Option,"RepeatPV"))
993 send_pv(); // to update time and nodes
997 game_get_board(Game,board);
999 if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
1000 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
1002 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
1005 gui_send(GUI,"move %s",string);
1009 if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {
1011 if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {
1014 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
1016 if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
1017 if (!option_get_bool(Option,"QueenNeverResigns") || !board_has_queen(board, board->turn)) { // [HGM] suppress resignig with Queen
1018 my_log("POLYGLOT *** RESIGN ***\n");
1019 gui_send(GUI,"resign");
1025 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
1026 State->resign_nb = 0;
1038 static void move_step(int move) {
1041 char move_string[256];
1043 ASSERT(move_is_ok(move));
1047 game_get_board(Game,board);
1049 if (move != MoveNone && move_is_legal(move,board)) {
1051 move_to_san(move,board,move_string,256);
1052 my_log("POLYGLOT MOVE %s\n",move_string);
1056 move_to_can(move,board,move_string,256);
1057 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
1060 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
1065 game_add_move(Game,move);
1071 static void board_update() {
1075 ASSERT(!XB->result);
1077 switch (game_status(Game)) {
1081 gui_send(GUI,"1-0 {White mates}");
1084 gui_send(GUI,"0-1 {Black mates}");
1087 gui_send(GUI,"1/2-1/2 {Stalemate}");
1090 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
1093 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
1095 case DRAW_REPETITION:
1096 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
1106 static void mess() {
1108 // clear state variables
1110 State->resign_nb = 0;
1111 State->exp_move = MoveNone;
1112 my_timer_reset(State->timer);
1114 // abort a possible search
1118 // calculate the new state
1121 } else if (!active()) {
1122 State->state = WAIT;
1123 my_log("POLYGLOT WAIT\n");
1124 } else if (XB->analyse) {
1125 State->state = ANALYSE;
1126 my_log("POLYGLOT ANALYSE\n");
1127 } else if (State->computer[game_turn(Game)]) {
1128 State->state = THINK;
1129 my_log("POLYGLOT THINK\n");
1131 State->state = WAIT;
1132 my_log("POLYGLOT WAIT\n");
1140 static void no_mess(int move) {
1142 ASSERT(move_is_ok(move));
1144 // just received a move, calculate the new state
1148 } else if (!active()) {
1150 stop_search(); // abort a possible search
1152 State->state = WAIT;
1153 State->exp_move = MoveNone;
1155 my_log("POLYGLOT WAIT\n");
1157 } else if (State->state == WAIT) {
1159 ASSERT(State->computer[game_turn(Game)]);
1160 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1161 ASSERT(!XB->analyse);
1163 my_log("POLYGLOT WAIT -> THINK\n");
1165 State->state = THINK;
1166 State->exp_move = MoveNone;
1168 } else if (State->state == THINK) {
1170 ASSERT(!State->computer[game_turn(Game)]);
1171 ASSERT(State->computer[colour_opp(game_turn(Game))]);
1172 ASSERT(!XB->analyse);
1174 if (ponder() && ponder_ok(Uci->ponder_move)) {
1176 my_log("POLYGLOT THINK -> PONDER\n");
1178 State->state = PONDER;
1179 State->exp_move = Uci->ponder_move;
1183 my_log("POLYGLOT THINK -> WAIT\n");
1185 State->state = WAIT;
1186 State->exp_move = MoveNone;
1189 } else if (State->state == PONDER) {
1191 ASSERT(State->computer[game_turn(Game)]);
1192 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1193 ASSERT(!XB->analyse);
1195 if (move == State->exp_move && Uci->searching) {
1197 ASSERT(Uci->searching);
1198 ASSERT(Uci->pending_nb>=1);
1200 my_timer_start(State->timer);//also resets
1202 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
1203 engine_send(Engine,"ponderhit");
1205 State->state = THINK;
1206 State->exp_move = MoveNone;
1208 send_pv(); // update display
1210 return; // do not launch a new search
1214 my_log("POLYGLOT PONDER -> THINK (miss)\n");
1218 State->state = THINK;
1219 State->exp_move = MoveNone;
1222 } else if (State->state == ANALYSE) {
1224 ASSERT(XB->analyse);
1226 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
1238 // start_protected_command()
1240 static void start_protected_command(){
1244 static void end_protected_command(){
1245 if(Uci->ready){ // not init faze
1246 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
1248 update_remaining_time();
1249 search_update(); // relaunch search if necessary
1252 // update_remaining_time()
1254 static void update_remaining_time(){
1256 if(State->timer->running){
1257 my_timer_stop(State->timer);
1258 reduce = my_timer_elapsed_real(State->timer);
1259 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
1260 XB->my_time -= reduce;
1261 if(XB->my_time<0.0){
1270 static void search_update() {
1276 ASSERT(!Uci->searching);
1281 // launch a new search if needed
1285 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
1287 // [VdB] moved up as we need the move number
1289 game_get_board(Game,Uci->board);
1293 if (State->state == THINK &&
1294 option_get_bool(Option,"Book") &&
1295 Uci->board->move_nb<option_get_int(Option,"BookDepth")
1299 move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));
1301 if (move != MoveNone && move_is_legal(move,Uci->board)) {
1303 my_log("POLYGLOT *BOOK MOVE*\n");
1305 search_clear(); // clears Uci->ponder_move
1306 Uci->best_move = move;
1308 board_copy(board,Uci->board);
1309 move_do(board,move);
1310 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
1312 Uci->best_pv[0] = Uci->best_move;
1313 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
1314 Uci->best_pv[2] = MoveNone;
1316 comp_move(Uci->best_move);
1324 my_log("POLYGLOT START SEARCH\n");
1328 uci_send_option(Uci,"UCI_Chess960","%s",
1329 option_get_bool(Option,"Chess960")?"true":"false");
1331 if (option_get_int(Option,"UCIVersion") >= 2) {
1332 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
1333 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
1336 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
1340 move = (State->state == PONDER) ? State->exp_move : MoveNone;
1341 send_board(move); // updates Uci->board global variable
1345 if (State->state == THINK || State->state == PONDER) {
1347 engine_send_queue(Engine,"go");
1349 if (XB->time_limit) {
1351 // fixed time per move
1353 if(XB->node_rate > 0){
1354 engine_send_queue(Engine,
1356 XB->time_max*((double)XB->node_rate));
1358 double computed_time;
1360 st_fudge=(double) option_get_int(Option,"STFudge");
1361 my_log("POLYGLOT Giving engine %.0fmsec extra time.\n",st_fudge);
1362 computed_time=XB->time_max*1000.0-st_fudge;
1363 if(computed_time< 1.0){
1366 engine_send_queue(Engine,
1375 if(XB->node_rate > 0) {
1379 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1381 time = XB->my_time / move_nb;
1385 if(time > XB->my_time){
1388 engine_send_queue(Engine,
1390 time*XB->node_rate);
1393 if (colour_is_white(Uci->board->turn)) {
1394 engine_send_queue(Engine,
1395 " wtime %.0f btime %.0f",
1396 XB->my_time*1000.0,XB->opp_time*1000.0);
1398 engine_send_queue(Engine,
1399 " wtime %.0f btime %.0f",
1400 XB->opp_time*1000.0,XB->my_time*1000.0);
1403 if (XB->inc != 0.0){
1404 engine_send_queue(Engine,
1405 " winc %.0f binc %.0f",
1406 XB->inc*1000.0,XB->inc*1000.0);
1410 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1411 ASSERT(move_nb>=1&&move_nb<=XB->mps);
1413 engine_send_queue(Engine," movestogo %d",move_nb);
1417 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
1419 if (State->state == PONDER) engine_send_queue(Engine," ponder");
1421 engine_send(Engine,""); // newline
1423 } else if (State->state == ANALYSE) {
1425 engine_send(Engine,"go infinite");
1434 ASSERT(!Uci->searching);
1438 Uci->searching = TRUE;
1445 static void search_clear() {
1451 my_timer_start(State->timer);//also resets
1456 static bool active() {
1460 if (game_status(Game) != PLAYING) return FALSE; // game ended
1464 if (XB->analyse) return TRUE; // analysing
1465 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
1466 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
1468 return TRUE; // playing
1473 static bool ponder() {
1475 return XB->ponder && (option_get_bool(Option,"CanPonder") ||
1476 option_find(Uci->option,"Ponder"));
1480 static bool ponder_ok(int move) {
1484 ASSERT(move==MoveNone||move_is_ok(move));
1486 // legal ponder move?
1488 if (move == MoveNone) return FALSE;
1490 game_get_board(Game,board);
1491 if (!move_is_legal(move,board)) return FALSE;
1493 // UCI-legal resulting position?
1495 game_add_move(Game,move);
1497 game_get_board(Game,board);
1498 status = game_status(Game);
1500 game_rem_move(Game);
1502 if (status != PLAYING) return FALSE; // game ended
1504 if (option_get_bool(Option,"Book") && is_in_book(board)) {
1513 static void stop_search() {
1515 if (Uci->searching) {
1517 ASSERT(Uci->searching);
1518 ASSERT(Uci->pending_nb>=1);
1520 my_log("POLYGLOT STOP SEARCH\n");
1523 engine_send(Engine,"stop");
1524 Uci->searching = FALSE;
1527 if (option_get_bool(Option,"SyncStop")) {
1528 uci_send_stop_sync(Uci);
1537 static void send_board(int extra_move) {
1546 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
1548 ASSERT(!Uci->searching);
1552 game_get_board(Game,Uci->board);
1553 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
1555 board_to_fen(Uci->board,fen,256);
1556 my_log("POLYGLOT FEN %s\n",fen);
1558 ASSERT(board_can_play(Uci->board));
1563 end = game_pos(Game);
1568 game_get_board_ex(Game,board,start);
1569 board_to_fen(board,string,256);
1571 engine_send_queue(Engine,"position");
1573 if (my_string_equal(string,StartFen)) {
1574 engine_send_queue(Engine," startpos");
1576 engine_send_queue(Engine," fen %s",string);
1581 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
1583 for (pos = start; pos < end; pos++) { // game moves
1585 move = game_move(Game,pos);
1587 move_to_can(move,board,string,256);
1588 engine_send_queue(Engine," %s",string);
1590 move_do(board,move);
1593 if (extra_move != MoveNone) { // move to ponder on
1594 move_to_can(extra_move,board,string,256);
1595 engine_send_queue(Engine," %s",string);
1600 engine_send(Engine,""); // newline
1605 static void send_info() {
1607 if(option_get_bool(Option,"WbWorkArounds2")){
1608 // Silly bug in some versions of WinBoard.
1609 // depth <=1 clears the engine output window.
1610 // Why shouldn't an engine be allowed to send info at depth 1?
1615 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
1616 0,0.0,U64(0),Uci->info);
1621 static void send_pv() {
1623 char pv_string[StringSize];
1626 char move_string[StringSize];
1628 ASSERT(State->state!=WAIT);
1630 if (Uci->best_depth == 0) return;
1632 // xboard search information
1636 if (State->state == THINK || State->state == ANALYSE) {
1638 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1640 if(Uci->depth==-1) //hack to clear the engine output window
1641 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);
1643 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);
1645 } else if (State->state == PONDER &&
1646 option_get_bool(Option,"ShowPonder")) {
1648 game_get_board(Game,board);
1649 move = State->exp_move;
1651 if (move != MoveNone && move_is_legal(move,board)) {
1652 move_to_san(move,board,move_string,256);
1653 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1654 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);
1661 if ((Uci->searching &&
1662 option_get_bool(Option,"KibitzPV") &&
1663 Uci->time >= option_get_double(Option,"KibitzDelay"))
1664 || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {
1666 if (State->state == THINK || State->state == ANALYSE) {
1668 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1669 if(kibitz_throttle(Uci->searching)){
1670 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);
1672 } else if (State->state == PONDER) {
1674 game_get_board(Game,board);
1675 move = State->exp_move;
1677 if (move != MoveNone && move_is_legal(move,board)) {
1678 move_to_san(move,board,move_string,256);
1679 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1680 if(kibitz_throttle(Uci->searching)){
1681 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);
1688 // kibitz_throttle()
1690 static bool kibitz_throttle(bool searching){
1692 static time_t lastKibitzMove=0;
1693 static time_t lastKibitzPV=0;
1694 curr_time = time(NULL);
1695 if(searching){ // KibitzPV
1697 (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
1698 lastKibitzPV=curr_time;
1701 }else{ // KibitzMove
1703 (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
1704 lastKibitzPV=curr_time;
1705 lastKibitzMove=curr_time;
1714 static void learn(int result) {
1720 ASSERT(result>=-1&&result<=+1);
1723 // ASSERT(State->computer[White]||State->computer[Black]);
1729 // [HGM] does not account for the hypothetical possibility we played both sides!
1730 if (State->playedAllMoves[White]) {
1732 } else if (State->playedAllMoves[Black]) {
1736 return; // [HGM] if we did not play all moves for some side, do not learn, but don't make a fuss!
1740 } else if (result > 0) {
1741 my_log("POLYGLOT *LEARN WIN*\n");
1742 } else if (result < 0) {
1743 my_log("POLYGLOT *LEARN LOSS*\n");
1745 my_log("POLYGLOT *LEARN DRAW*\n");
1750 for (; pos < Game->size; pos += 2) {
1752 game_get_board_ex(Game,board,pos);
1753 move = game_move(Game,pos);
1755 book_learn_move(board,move,result);
1761 // end of xboard2uci.c