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];
53 bool has_feature_memory;
55 bool has_feature_egt_nalimov;
56 bool has_feature_egt_gaviota;
61 bool new_hack; // "new" is a C++ keyword
84 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;
88 static state_t State[1];
93 static void comp_move (int move);
94 static void move_step (int move);
95 static void board_update ();
98 static void no_mess (int move);
100 static void search_update ();
101 static void search_clear ();
102 static void update_remaining_time();
103 static int report_best_score();
104 static bool kibitz_throttle (bool searching);
105 static void start_protected_command();
106 static void end_protected_command();
108 static bool active ();
109 static bool ponder ();
110 static bool ponder_ok (int ponder_move);
112 static void stop_search ();
114 static void send_board (int extra_move);
115 static void send_pv ();
116 static void send_info ();
118 static void send_xboard_options ();
120 static void learn (int result);
127 void xboard2uci_init() {
136 State->computer[White] = FALSE;
137 State->computer[Black] = TRUE;
139 State->exp_move = MoveNone;
140 State->hint_move = MoveNone;
141 State->resign_nb = 0;
142 my_timer_reset(State->timer);
144 // yes there are engines that do not have the "Hash" option....
145 XB->has_feature_memory= (option_find(Uci->option,"Hash")!=NULL);
146 XB->has_feature_smp = (uci_thread_option(Uci)!=NULL);
147 // TODO: support for other types of table bases
148 // This is a quick hack.
149 XB->has_feature_egt_nalimov = (option_find(Uci->option,"NalimovPath")!=NULL);
150 XB->has_feature_egt_gaviota = (option_find(Uci->option,"GaviotaTbPath")!=NULL);
152 XB->computer = FALSE;
154 my_string_set(&XB->name,"<empty>");
167 XB->time_limit = FALSE;
170 XB->depth_limit = FALSE;
174 XB->opp_time = 300.0;
179 // xboard2uci_gui_step()
181 void xboard2uci_gui_step(char string[]) {
184 char move_string[256];
189 } else if (match(string,"accepted *")) {
193 } else if (match(string,"analyze")) {
195 State->computer[White] = FALSE;
196 State->computer[Black] = FALSE;
199 XB->new_hack = FALSE;
205 } else if (match(string,"bk")) {
207 if (option_get_bool(Option,"Book")) {
208 game_get_board(Game,board);
212 } else if (match(string,"black")) {
214 if (colour_is_black(game_turn(Game))) {
216 State->computer[White] = TRUE;
217 State->computer[Black] = FALSE;
225 } else if (match(string,"computer")) {
229 } else if (match(string,"draw")) {
230 if(option_find(Uci->option,"UCI_DrawOffers")){
231 my_log("POLYGLOT draw from XB received");
232 uci_send_option(Uci,"DrawOffer","%s","draw");}
233 } else if (match(string,"easy")) {
239 } else if (match(string,"edit")) {
243 gui_send(GUI,"Error (unknown command): %s",string);
245 } else if (match(string,"exit")) {
247 State->computer[White] = FALSE;
248 State->computer[Black] = FALSE;
254 } else if (match(string,"force")) {
256 State->computer[White] = FALSE;
257 State->computer[Black] = FALSE;
261 } else if (match(string,"go")) {
263 State->computer[game_turn(Game)] = TRUE;
264 State->computer[colour_opp(game_turn(Game))] = FALSE;
266 XB->new_hack = FALSE;
272 } else if (match(string,"hard")) {
278 } else if (match(string,"hint")) {
281 game_get_board(Game,board);
282 if (option_get_bool(Option,"Book")) {
284 move = book_move(board,FALSE);
286 if(move==MoveNone && State->hint_move!=MoveNone){
287 move=State->hint_move;
290 if (move != MoveNone && move_is_legal(move,board)) {
291 move_to_san(move,board,move_string,256);
292 gui_send(GUI,"Hint: %s",move_string);
295 } else if (match(string,"ics *")) {
299 } else if (match(string,"level * *:* *")) {
301 XB->mps = atoi(Star[0]);
302 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
303 XB->inc = ((double)atoi(Star[3]));
305 } else if (match(string,"level * * *")) {
307 XB->mps = atoi(Star[0]);
308 XB->base = ((double)atoi(Star[1])) * 60.0;
309 XB->inc = ((double)atoi(Star[2]));
311 } else if (match(string,"name *")) {
313 my_string_set(&XB->name,Star[0]);
315 } else if (match(string,"new")) {
317 uci_send_isready_sync(Uci);
318 my_log("POLYGLOT NEW GAME\n");
320 option_set(Option,"Chess960","false");
325 State->computer[White] = FALSE;
326 State->computer[Black] = FALSE;
328 State->computer[White] = FALSE;
329 State->computer[Black] = TRUE;
335 XB->depth_limit = FALSE;
338 XB->computer = FALSE;
339 my_string_set(&XB->name,"<empty>");
344 uci_send_ucinewgame(Uci);
346 } else if (match(string,"nopost")) {
350 } else if (match(string,"otim *")) {
352 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
353 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
355 } else if (match(string,"pause")) {
359 gui_send(GUI,"Error (unknown command): %s",string);
361 } else if (match(string,"ping *")) {
363 // HACK; TODO: answer only after an engine move
366 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
367 XB->ping = atoi(Star[0]);
368 uci_send_isready_sync(Uci);
370 ASSERT(XB->ping==-1);
371 gui_send(GUI,"pong %s",Star[0]);
373 } else if (match(string,"nps *")) {
375 // fake WB play-by-nodes mode
376 XB->node_rate = atoi(Star[0]);
377 } else if (match(string,"playother")) {
379 State->computer[game_turn(Game)] = FALSE;
380 State->computer[colour_opp(game_turn(Game))] = TRUE;
382 XB->new_hack = FALSE;
388 } else if (match(string,"post")) {
392 } else if (match(string,"protover *")) {
393 XB->proto_ver = atoi(Star[0]);
394 ASSERT(XB->proto_ver>=2);
395 send_xboard_options();
397 } else if (match(string,"quit")) {
398 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
400 } else if (match(string,"random")) {
404 } else if (match(string,"rating * *")) {
408 } else if (match(string,"remove")) {
410 if (game_pos(Game) >= 2) {
412 game_goto(Game,game_pos(Game)-2);
414 ASSERT(!XB->new_hack);
415 XB->new_hack = FALSE; // HACK?
422 } else if (match(string,"rejected *")) {
426 } else if (match(string,"reset")) { // protover 3?
430 gui_send(GUI,"Error (unknown command): %s",string);
433 || match(string,"result * {*}")
434 || match(string,"result * {* }")
435 || match(string,"result * { *}")
436 || match(string,"result * { * }")) {
438 my_log("POLYGLOT GAME END\n");
446 if (FALSE && option_get_bool(Option,"Book") &&
447 option_get_bool(Option,"BookLearn")) {
450 } else if (my_string_equal(Star[0],"1-0")) {
452 } else if (my_string_equal(Star[0],"0-1")) {
454 } else if (my_string_equal(Star[0],"1/2-1/2")) {
458 } else if (match(string,"resume")) {
462 gui_send(GUI,"Error (unknown command): %s",string);
464 } else if (match(string,"option *=*") ||
465 match(string,"option * =*") ||
466 match(string,"option *= *") ||
467 match(string,"option * = *")
471 if(match(name, "Polyglot *")){
472 char *pg_name=Star[0];
473 polyglot_set_option(pg_name,value);
475 option_t *opt=option_find(Uci->option,name);
477 if(my_string_case_equal(opt->type,"check")){
478 value=my_string_equal(value,"1")?"true":"false";
480 start_protected_command();
481 uci_send_option(Uci, name, "%s", value);
482 end_protected_command();
484 gui_send(GUI,"Error (unknown option): %s",name);
487 } else if (match(string,"option *")){
489 if(match(name, "Polyglot *")){
490 char *pg_name=Star[0];
491 polyglot_set_option(pg_name,"<empty>");
493 start_protected_command();
495 if(!uci_send_option(Uci, name, "%s", "<empty>")){
496 gui_send(GUI,"Error (unknown option): %s",name);
498 end_protected_command();
500 } else if (XB->has_feature_smp && match(string,"cores *")){
501 int cores=atoi(Star[0]);
503 // updating the number of cores
504 my_log("POLYGLOT setting the number of cores to %d\n",cores);
505 start_protected_command();
506 uci_set_threads(Uci,cores);
507 end_protected_command();
510 gui_send(GUI,"Error (unknown command): %s",string);
512 } else if (match(string,"egtpath * *")){
515 if(my_string_empty(path)){
517 gui_send(GUI,"Error (unknown command): %s",string);
519 if(my_string_case_equal(type,"nalimov") && XB->has_feature_egt_nalimov){
520 // updating NalimovPath
521 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
522 start_protected_command();
523 uci_send_option(Uci,"NalimovPath","%s",path);
524 end_protected_command();
525 }else if(my_string_case_equal(type,"gaviota") && XB->has_feature_egt_gaviota){
526 // updating GaviotaPath
527 my_log("POLYGLOT setting the Gaviota path to %s\n",path);
528 start_protected_command();
529 uci_send_option(Uci,"GaviotaTbPath","%s",path);
530 end_protected_command();
533 gui_send(GUI,"Error (unsupported table base format): %s",string);
536 } else if (XB->has_feature_memory && match(string,"memory *")){
537 int memory = atoi(Star[0]);
541 // updating the available memory
543 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
544 if(XB->has_feature_egt_nalimov && (opt=option_find(Uci->option,"NalimovCache"))){
545 egt_cache=atoi(opt->value);
546 }else if(XB->has_feature_egt_gaviota &&
547 (opt=option_find(Uci->option,"GaviotaTbCache"))){
548 egt_cache=atoi(opt->value);
552 my_log("POLYGLOT EGTB Cache is %dMb\n",egt_cache);
553 real_memory=memory-egt_cache;
555 start_protected_command();
556 uci_send_option(Uci,"Hash", "%d", real_memory);
557 end_protected_command();
561 gui_send(GUI,"Error (unknown command): %s",string);
564 } else if (match(string,"sd *")) {
566 XB->depth_limit = TRUE;
567 XB->depth_max = atoi(Star[0]);
569 } else if (match(string,"setboard *")) {
571 my_log("POLYGLOT FEN %s\n",Star[0]);
573 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
575 State->computer[White] = FALSE;
576 State->computer[Black] = FALSE;
578 XB->new_hack = TRUE; // HACK?
584 } else if (match(string,"st *")) {
586 XB->time_limit = TRUE;
587 XB->time_max = ((double)atoi(Star[0]));
589 } else if (match(string,"time *")) {
591 XB->my_time = ((double)atoi(Star[0])) / 100.0;
592 if (XB->my_time < 0.0) XB->my_time = 0.0;
594 } else if (match(string,"undo")) {
596 if (game_pos(Game) >= 1) {
598 game_goto(Game,game_pos(Game)-1);
600 ASSERT(!XB->new_hack);
601 XB->new_hack = FALSE; // HACK?
608 } else if (match(string,"usermove *")) {
610 game_get_board(Game,board);
611 move = move_from_san(Star[0],board);
613 if (move != MoveNone && move_is_legal(move,board)) {
615 XB->new_hack = FALSE;
624 gui_send(GUI,"Illegal move: %s",Star[0]);
627 } else if (match(string,"variant *")) {
629 if (my_string_equal(Star[0],"fischerandom")) {
630 option_set(Option,"Chess960","true");
632 option_set(Option,"Chess960","false");
635 } else if (match(string,"white")) {
637 if (colour_is_white(game_turn(Game))) {
639 State->computer[White] = FALSE;
640 State->computer[Black] = TRUE;
648 } else if (match(string,"xboard")) {
652 } else if (match(string,".")) { // analyse info
654 if (State->state == ANALYSE) {
655 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
657 ASSERT(Uci->searching);
658 ASSERT(Uci->pending_nb>=1);
660 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
661 move_to_san(Uci->root_move,Uci->board,move_string,256);
662 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);
664 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
668 } else if (match(string,"?")) { // move now
670 if (State->state == THINK) {
672 ASSERT(Uci->searching);
673 ASSERT(Uci->pending_nb>=1);
675 // HACK: just send "stop" to the engine
677 if (Uci->searching) {
678 my_log("POLYGLOT STOP SEARCH\n");
679 engine_send(Engine,"stop");
683 } else { // unknown command, maybe a move?
685 game_get_board(Game,board);
686 move = move_from_san(string,board);
688 if (move != MoveNone && move_is_legal(move,board)) {
690 XB->new_hack = FALSE;
697 } else if (move != MoveNone) {
699 gui_send(GUI,"Illegal move: %s",string);
703 gui_send(GUI,"Error (unknown command): %s",string);
709 // xboard2uci_engine_step()
711 void xboard2uci_engine_step(char string[]) {
715 event = uci_parse(Uci,string);
719 if ((event & EVENT_READY) != 0) {
721 // the engine is now ready
725 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
728 if (!DelayPong && XB->ping >= 0) {
729 gui_send(GUI,"pong %d",XB->ping);
734 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
736 // the engine is playing a move
738 // MEGA HACK: estimate remaining time because XBoard won't send it!
740 my_timer_stop(State->timer);
742 XB->my_time -= my_timer_elapsed_real(State->timer);
743 XB->my_time += XB->inc;
744 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
746 if (XB->my_time < 0.0) XB->my_time = 0.0;
748 // make sure to remember the ponder move
750 State->hint_move=Uci->ponder_move;
752 // play the engine move
754 comp_move(Uci->best_move);
758 if ((event & EVENT_PV) != 0) {
760 // the engine has sent a new PV
764 if ((event & EVENT_INFO) != 0) {
766 // the engine has sent info
770 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
771 my_log("POYGLOT draw offer/resign from engine\n");
772 if(option_find(Uci->option,"UCI_DrawOffers")){
773 if(event & EVENT_DRAW)
774 gui_send(GUI,"offer draw");
776 gui_send(GUI,"resign");
779 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){
780 game_get_board(Game,board);
781 if(board->turn==White){
782 gui_send(GUI,"0-1 {polyglot: resign"
783 " (illegal engine move by white: %s)}",Uci->bestmove);
785 gui_send(GUI,"1-0 {polyglot: resign"
786 " (illegal engine move by black: %s)}",Uci->bestmove);
794 // format_xboard_option_line
796 void format_xboard_option_line(char * option_line, option_t *opt){
798 char option_string[StringSize];
800 strcpy(option_line,"");
801 // buffer overflow alert
802 strcat(option_line,"feature option=\"");
804 strcat(option_line,"Polyglot ");
806 sprintf(option_string,"%s",opt->name);
807 strcat(option_line,option_string);
808 sprintf(option_string," -%s",opt->type);
809 strcat(option_line,option_string);
810 if(!IS_BUTTON(opt->type) && strcmp(opt->type,"combo")){
811 if(strcmp(opt->type,"check")){
812 sprintf(option_string," %s",opt->value);
814 sprintf(option_string," %d",
815 my_string_case_equal(opt->value,"true")||
816 my_string_equal(opt->value,"1")
819 strcat(option_line,option_string);
821 if(IS_SPIN(opt->type)){
822 sprintf(option_string," %s",opt->min);
823 strcat(option_line,option_string);
825 if(IS_SPIN(opt->type)){
826 sprintf(option_string," %s",opt->max);
827 strcat(option_line,option_string);
829 for(j=0;j<opt->var_nb;j++){
830 if(!strcmp(opt->var[j],opt->value)){
831 sprintf(option_string," *%s",opt->var[j]);
833 sprintf(option_string," %s",opt->var[j]);
835 strcat(option_line,option_string);
836 if(j!=opt->var_nb-1){
837 strcat(option_line," ///");
840 strcat(option_line,"\"");
841 if(option_get_bool(Option,"WbWorkArounds") &&
842 (tmp=strstr(option_line,"Draw"))){
844 my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",
849 // send_xboard_options()
851 static void send_xboard_options(){
853 char egtfeature[StringSize];
856 gui_send(GUI,"feature done=0");
858 gui_send(GUI,"feature analyze=1");
859 gui_send(GUI,"feature colors=0");
860 gui_send(GUI,"feature draw=1");
861 gui_send(GUI,"feature ics=1");
862 gui_send(GUI,"feature myname=\"%s\"",
863 option_get_string(Option,"EngineName"));
864 gui_send(GUI,"feature name=1");
865 gui_send(GUI,"feature pause=0");
866 gui_send(GUI,"feature ping=1");
867 gui_send(GUI,"feature playother=1");
868 gui_send(GUI,"feature sigint=1");
869 gui_send(GUI,"feature reuse=1");
870 gui_send(GUI,"feature san=0");
871 gui_send(GUI,"feature setboard=1");
872 gui_send(GUI,"feature sigint=0");
873 gui_send(GUI,"feature sigterm=0");
874 gui_send(GUI,"feature time=1");
875 gui_send(GUI,"feature usermove=1");
876 gui_send(GUI,"feature nps=1");
877 if (XB->has_feature_memory){
878 gui_send(GUI,"feature memory=1");
880 gui_send(GUI,"feature memory=0");
882 if (XB->has_feature_smp){
883 gui_send(GUI,"feature smp=1");
885 gui_send(GUI,"feature smp=0");
888 strncat(egtfeature,"feature egt=\"",StringSize);
889 if (XB->has_feature_egt_nalimov){
891 strncat(egtfeature,"nalimov",StringSize-strlen(egtfeature));
893 if (XB->has_feature_egt_gaviota){
895 strncat(egtfeature,",",StringSize-strlen(egtfeature));
897 strncat(egtfeature,"gaviota",StringSize-strlen(egtfeature));
899 strncat(egtfeature,"\"",StringSize-strlen(egtfeature));
900 egtfeature[StringSize-1]='\0';
901 gui_send(GUI,egtfeature);
903 if (option_find(Uci->option,"UCI_Chess960")) {
904 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
906 gui_send(GUI,"feature variants=\"normal\"");
909 xboard2uci_send_options();
912 void xboard2uci_send_options(){
913 char option_line[StringSize]="";
917 option_start_iter(Uci->option);
918 while((opt=option_next(Uci->option))){
919 if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;
920 if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;
921 if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;
922 if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;
923 if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;
924 if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;
925 if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;
926 if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;
927 if(my_string_case_equal(opt->name,"Ponder")) continue;
928 if(my_string_case_equal(opt->name,"Hash")) continue;
929 if(my_string_case_equal(opt->name,"NalimovPath")) continue;
930 if(my_string_case_equal(opt->name,"GaviotaTbPath")) continue;
931 if((name=uci_thread_option(Uci))!=NULL &&
932 my_string_case_equal(opt->name,name)) continue;
933 format_xboard_option_line(option_line,opt);
935 gui_send(GUI,"%s",option_line);
939 option_start_iter(Option);
940 while((opt=option_next(Option))){
941 if(opt->mode &XBOARD){
942 format_xboard_option_line(option_line,opt);
943 gui_send(GUI,"%s",option_line);
946 gui_send(GUI,"feature done=1");
950 // report_best_score()
952 static int report_best_score(){
953 if(!option_get_bool(Option,"ScoreWhite") ||
954 colour_is_white(Uci->board->turn)){
955 return Uci->best_score;
957 return -Uci->best_score;
963 static void comp_move(int move) {
968 ASSERT(move_is_ok(move));
970 ASSERT(State->state==THINK);
971 ASSERT(!XB->analyse);
973 if(option_get_bool(Option,"RepeatPV"))
974 send_pv(); // to update time and nodes
978 game_get_board(Game,board);
980 if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
981 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
983 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
986 gui_send(GUI,"move %s",string);
990 if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {
992 if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {
995 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
997 if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
998 my_log("POLYGLOT *** RESIGN ***\n");
999 gui_send(GUI,"resign");
1004 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
1005 State->resign_nb = 0;
1017 static void move_step(int move) {
1020 char move_string[256];
1022 ASSERT(move_is_ok(move));
1026 game_get_board(Game,board);
1028 if (move != MoveNone && move_is_legal(move,board)) {
1030 move_to_san(move,board,move_string,256);
1031 my_log("POLYGLOT MOVE %s\n",move_string);
1035 move_to_can(move,board,move_string,256);
1036 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
1039 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
1044 game_add_move(Game,move);
1050 static void board_update() {
1054 ASSERT(!XB->result);
1056 switch (game_status(Game)) {
1060 gui_send(GUI,"1-0 {White mates}");
1063 gui_send(GUI,"0-1 {Black mates}");
1066 gui_send(GUI,"1/2-1/2 {Stalemate}");
1069 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
1072 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
1074 case DRAW_REPETITION:
1075 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
1085 static void mess() {
1087 // clear state variables
1089 State->resign_nb = 0;
1090 State->exp_move = MoveNone;
1091 my_timer_reset(State->timer);
1093 // abort a possible search
1097 // calculate the new state
1100 } else if (!active()) {
1101 State->state = WAIT;
1102 my_log("POLYGLOT WAIT\n");
1103 } else if (XB->analyse) {
1104 State->state = ANALYSE;
1105 my_log("POLYGLOT ANALYSE\n");
1106 } else if (State->computer[game_turn(Game)]) {
1107 State->state = THINK;
1108 my_log("POLYGLOT THINK\n");
1110 State->state = WAIT;
1111 my_log("POLYGLOT WAIT\n");
1119 static void no_mess(int move) {
1121 ASSERT(move_is_ok(move));
1123 // just received a move, calculate the new state
1127 } else if (!active()) {
1129 stop_search(); // abort a possible search
1131 State->state = WAIT;
1132 State->exp_move = MoveNone;
1134 my_log("POLYGLOT WAIT\n");
1136 } else if (State->state == WAIT) {
1138 ASSERT(State->computer[game_turn(Game)]);
1139 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1140 ASSERT(!XB->analyse);
1142 my_log("POLYGLOT WAIT -> THINK\n");
1144 State->state = THINK;
1145 State->exp_move = MoveNone;
1147 } else if (State->state == THINK) {
1149 ASSERT(!State->computer[game_turn(Game)]);
1150 ASSERT(State->computer[colour_opp(game_turn(Game))]);
1151 ASSERT(!XB->analyse);
1153 if (ponder() && ponder_ok(Uci->ponder_move)) {
1155 my_log("POLYGLOT THINK -> PONDER\n");
1157 State->state = PONDER;
1158 State->exp_move = Uci->ponder_move;
1162 my_log("POLYGLOT THINK -> WAIT\n");
1164 State->state = WAIT;
1165 State->exp_move = MoveNone;
1168 } else if (State->state == PONDER) {
1170 ASSERT(State->computer[game_turn(Game)]);
1171 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1172 ASSERT(!XB->analyse);
1174 if (move == State->exp_move && Uci->searching) {
1176 ASSERT(Uci->searching);
1177 ASSERT(Uci->pending_nb>=1);
1179 my_timer_start(State->timer);//also resets
1181 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
1182 engine_send(Engine,"ponderhit");
1184 State->state = THINK;
1185 State->exp_move = MoveNone;
1187 send_pv(); // update display
1189 return; // do not launch a new search
1193 my_log("POLYGLOT PONDER -> THINK (miss)\n");
1197 State->state = THINK;
1198 State->exp_move = MoveNone;
1201 } else if (State->state == ANALYSE) {
1203 ASSERT(XB->analyse);
1205 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
1217 // start_protected_command()
1219 static void start_protected_command(){
1223 static void end_protected_command(){
1224 if(Uci->ready){ // not init faze
1225 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
1227 update_remaining_time();
1228 search_update(); // relaunch search if necessary
1231 // update_remaining_time()
1233 static void update_remaining_time(){
1235 if(State->timer->running){
1236 my_timer_stop(State->timer);
1237 reduce = my_timer_elapsed_real(State->timer);
1238 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
1239 XB->my_time -= reduce;
1240 if(XB->my_time<0.0){
1249 static void search_update() {
1255 ASSERT(!Uci->searching);
1260 // launch a new search if needed
1264 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
1266 // [VdB] moved up as we need the move number
1268 game_get_board(Game,Uci->board);
1272 if (State->state == THINK &&
1273 option_get_bool(Option,"Book") &&
1274 Uci->board->move_nb<option_get_int(Option,"BookDepth")
1278 move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));
1280 if (move != MoveNone && move_is_legal(move,Uci->board)) {
1282 my_log("POLYGLOT *BOOK MOVE*\n");
1284 search_clear(); // clears Uci->ponder_move
1285 Uci->best_move = move;
1287 board_copy(board,Uci->board);
1288 move_do(board,move);
1289 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
1291 Uci->best_pv[0] = Uci->best_move;
1292 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
1293 Uci->best_pv[2] = MoveNone;
1295 comp_move(Uci->best_move);
1303 my_log("POLYGLOT START SEARCH\n");
1307 uci_send_option(Uci,"UCI_Chess960","%s",
1308 option_get_bool(Option,"Chess960")?"true":"false");
1310 if (option_get_int(Option,"UCIVersion") >= 2) {
1311 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
1312 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
1315 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
1319 move = (State->state == PONDER) ? State->exp_move : MoveNone;
1320 send_board(move); // updates Uci->board global variable
1324 if (State->state == THINK || State->state == PONDER) {
1326 engine_send_queue(Engine,"go");
1328 if (XB->time_limit) {
1330 // fixed time per move
1332 if(XB->node_rate > 0){
1333 engine_send_queue(Engine,
1335 XB->time_max*((double)XB->node_rate));
1337 double computed_time;
1339 st_fudge=(double) option_get_int(Option,"STFudge");
1340 my_log("POLYGLOT Giving engine %.0fmsec extra time.\n",st_fudge);
1341 computed_time=XB->time_max*1000.0-st_fudge;
1342 if(computed_time< 1.0){
1345 engine_send_queue(Engine,
1354 if(XB->node_rate > 0) {
1358 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1360 time = XB->my_time / move_nb;
1364 if(time > XB->my_time){
1367 engine_send_queue(Engine,
1369 time*XB->node_rate);
1372 if (colour_is_white(Uci->board->turn)) {
1373 engine_send_queue(Engine,
1374 " wtime %.0f btime %.0f",
1375 XB->my_time*1000.0,XB->opp_time*1000.0);
1377 engine_send_queue(Engine,
1378 " wtime %.0f btime %.0f",
1379 XB->opp_time*1000.0,XB->my_time*1000.0);
1382 if (XB->inc != 0.0){
1383 engine_send_queue(Engine,
1384 " winc %.0f binc %.0f",
1385 XB->inc*1000.0,XB->inc*1000.0);
1389 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1390 ASSERT(move_nb>=1&&move_nb<=XB->mps);
1392 engine_send_queue(Engine," movestogo %d",move_nb);
1396 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
1398 if (State->state == PONDER) engine_send_queue(Engine," ponder");
1400 engine_send(Engine,""); // newline
1402 } else if (State->state == ANALYSE) {
1404 engine_send(Engine,"go infinite");
1413 ASSERT(!Uci->searching);
1417 Uci->searching = TRUE;
1424 static void search_clear() {
1430 my_timer_start(State->timer);//also resets
1435 static bool active() {
1439 if (game_status(Game) != PLAYING) return FALSE; // game ended
1443 if (XB->analyse) return TRUE; // analysing
1444 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
1445 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
1447 return TRUE; // playing
1452 static bool ponder() {
1454 return XB->ponder && (option_get_bool(Option,"CanPonder") ||
1455 option_find(Uci->option,"Ponder"));
1459 static bool ponder_ok(int move) {
1463 ASSERT(move==MoveNone||move_is_ok(move));
1465 // legal ponder move?
1467 if (move == MoveNone) return FALSE;
1469 game_get_board(Game,board);
1470 if (!move_is_legal(move,board)) return FALSE;
1472 // UCI-legal resulting position?
1474 game_add_move(Game,move);
1476 game_get_board(Game,board);
1477 status = game_status(Game);
1479 game_rem_move(Game);
1481 if (status != PLAYING) return FALSE; // game ended
1483 if (option_get_bool(Option,"Book") && is_in_book(board)) {
1492 static void stop_search() {
1494 if (Uci->searching) {
1496 ASSERT(Uci->searching);
1497 ASSERT(Uci->pending_nb>=1);
1499 my_log("POLYGLOT STOP SEARCH\n");
1502 engine_send(Engine,"stop");
1503 Uci->searching = FALSE;
1506 if (option_get_bool(Option,"SyncStop")) {
1507 uci_send_stop_sync(Uci);
1516 static void send_board(int extra_move) {
1525 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
1527 ASSERT(!Uci->searching);
1531 game_get_board(Game,Uci->board);
1532 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
1534 board_to_fen(Uci->board,fen,256);
1535 my_log("POLYGLOT FEN %s\n",fen);
1537 ASSERT(board_can_play(Uci->board));
1542 end = game_pos(Game);
1547 game_get_board_ex(Game,board,start);
1548 board_to_fen(board,string,256);
1550 engine_send_queue(Engine,"position");
1552 if (my_string_equal(string,StartFen)) {
1553 engine_send_queue(Engine," startpos");
1555 engine_send_queue(Engine," fen %s",string);
1560 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
1562 for (pos = start; pos < end; pos++) { // game moves
1564 move = game_move(Game,pos);
1566 move_to_can(move,board,string,256);
1567 engine_send_queue(Engine," %s",string);
1569 move_do(board,move);
1572 if (extra_move != MoveNone) { // move to ponder on
1573 move_to_can(extra_move,board,string,256);
1574 engine_send_queue(Engine," %s",string);
1579 engine_send(Engine,""); // newline
1584 static void send_info() {
1586 if(option_get_bool(Option,"WbWorkArounds2")){
1587 // Silly bug in some versions of WinBoard.
1588 // depth <=1 clears the engine output window.
1589 // Why shouldn't an engine be allowed to send info at depth 1?
1594 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
1595 0,0.0,U64(0),Uci->info);
1600 static void send_pv() {
1602 char pv_string[StringSize];
1605 char move_string[StringSize];
1607 ASSERT(State->state!=WAIT);
1609 if (Uci->best_depth == 0) return;
1611 // xboard search information
1615 if (State->state == THINK || State->state == ANALYSE) {
1617 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1619 if(Uci->depth==-1) //hack to clear the engine output window
1620 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);
1622 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);
1624 } else if (State->state == PONDER &&
1625 option_get_bool(Option,"ShowPonder")) {
1627 game_get_board(Game,board);
1628 move = State->exp_move;
1630 if (move != MoveNone && move_is_legal(move,board)) {
1631 move_to_san(move,board,move_string,256);
1632 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1633 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);
1640 if ((Uci->searching &&
1641 option_get_bool(Option,"KibitzPV") &&
1642 Uci->time >= option_get_double(Option,"KibitzDelay"))
1643 || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {
1645 if (State->state == THINK || State->state == ANALYSE) {
1647 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1648 if(kibitz_throttle(Uci->searching)){
1649 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);
1651 } else if (State->state == PONDER) {
1653 game_get_board(Game,board);
1654 move = State->exp_move;
1656 if (move != MoveNone && move_is_legal(move,board)) {
1657 move_to_san(move,board,move_string,256);
1658 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1659 if(kibitz_throttle(Uci->searching)){
1660 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);
1667 // kibitz_throttle()
1669 static bool kibitz_throttle(bool searching){
1671 static time_t lastKibitzMove=0;
1672 static time_t lastKibitzPV=0;
1673 curr_time = time(NULL);
1674 if(searching){ // KibitzPV
1676 (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
1677 lastKibitzPV=curr_time;
1680 }else{ // KibitzMove
1682 (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
1683 lastKibitzPV=curr_time;
1684 lastKibitzMove=curr_time;
1693 static void learn(int result) {
1699 ASSERT(result>=-1&&result<=+1);
1702 ASSERT(State->computer[White]||State->computer[Black]);
1709 } else if (State->computer[White]) {
1711 } else if (State->computer[Black]) {
1715 my_fatal("learn(): unknown side\n");
1719 } else if (result > 0) {
1720 my_log("POLYGLOT *LEARN WIN*\n");
1721 } else if (result < 0) {
1722 my_log("POLYGLOT *LEARN LOSS*\n");
1724 my_log("POLYGLOT *LEARN DRAW*\n");
1729 for (; pos < Game->size; pos += 2) {
1731 game_get_board_ex(Game,board,pos);
1732 move = game_move(Game,pos);
1734 book_learn_move(board,move,result);
1740 // end of xboard2uci.c