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
217 game_get_board(Game,board);
218 gen_legal_moves(move_list,board);
219 for(i=0; i<list_size(move_list); i++){
220 move_list->value[i] = 0;
221 move_to_san(move_list->move[i],board,move_string,256);
222 printf(" %6s\n",move_string);
224 // this is necessary by the xboard protocol
228 } else if (match(string,"kill *") || match(string,"option Polyglot exclude move=*")) { // [HGM]
232 game_get_board(Game,board);
233 if(!list_size(move_list)) {
234 gen_legal_moves(move_list,board);
235 for(i=0; i<list_size(move_list); i++){
236 move_list->value[i] = 0;
239 move = move_from_san(Star[0],board);
241 for(i=0; i<list_size(move_list); i++){
242 if(move_list->move[i] == move) move_list->value[i] = 1;
247 } else if (match(string,"black")) {
249 if (colour_is_black(game_turn(Game))) {
251 State->computer[White] = TRUE;
252 State->computer[Black] = FALSE;
260 } else if (match(string,"computer")) {
264 } else if (match(string,"draw")) {
265 if(option_find(Uci->option,"UCI_DrawOffers")){
266 my_log("POLYGLOT draw from XB received");
267 uci_send_option(Uci,"DrawOffer","%s","draw");}
268 else if (option_get_bool(Option,"HandleDraws") && Uci->root_move_nb > 20) { // [HGM] PG draw handling
269 my_log("POLYGLOT draw from XB received");
270 if (Uci->best_score <= -option_get_int(Option,"ContemptScore"))
271 gui_send(GUI,"offer draw");}
272 } else if (match(string,"easy")) {
278 } else if (match(string,"edit")) {
282 gui_send(GUI,"Error (unknown command): %s",string);
284 } else if (match(string,"exit")) {
286 State->computer[White] = FALSE;
287 State->computer[Black] = FALSE;
293 } else if (match(string,"force")) {
295 State->computer[White] = FALSE;
296 State->computer[Black] = FALSE;
300 } else if (match(string,"go")) {
302 State->computer[game_turn(Game)] = TRUE;
303 State->computer[colour_opp(game_turn(Game))] = FALSE;
305 XB->new_hack = FALSE;
311 } else if (match(string,"hard")) {
317 } else if (match(string,"hint")) {
320 game_get_board(Game,board);
321 if (option_get_bool(Option,"Book")) {
323 move = book_move(board,FALSE);
325 if(move==MoveNone && State->hint_move!=MoveNone){
326 move=State->hint_move;
329 if (move != MoveNone && move_is_legal(move,board)) {
330 move_to_san(move,board,move_string,256);
331 gui_send(GUI,"Hint: %s",move_string);
334 } else if (match(string,"ics *")) {
338 } else if (match(string,"level * *:* *")) {
340 XB->mps = atoi(Star[0]);
341 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
342 XB->inc = ((double)atoi(Star[3]));
344 } else if (match(string,"level * * *")) {
346 XB->mps = atoi(Star[0]);
347 XB->base = ((double)atoi(Star[1])) * 60.0;
348 XB->inc = ((double)atoi(Star[2]));
350 } else if (match(string,"name *")) {
352 my_string_set(&XB->name,Star[0]);
354 } else if (match(string,"new")) {
356 uci_send_isready_sync(Uci);
357 my_log("POLYGLOT NEW GAME\n");
359 option_set(Option,"Chess960","false");
364 State->computer[White] = FALSE;
365 State->computer[Black] = FALSE;
367 State->computer[White] = FALSE;
368 State->computer[Black] = TRUE;
369 State->playedAllMoves[White] = TRUE; // [HGM]
370 State->playedAllMoves[Black] = TRUE;
376 XB->depth_limit = FALSE;
379 XB->computer = FALSE;
380 my_string_set(&XB->name,"<empty>");
385 uci_send_ucinewgame(Uci);
387 } else if (match(string,"nopost")) {
391 } else if (match(string,"otim *")) {
393 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
394 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
396 } else if (match(string,"pause")) {
400 gui_send(GUI,"Error (unknown command): %s",string);
402 } else if (match(string,"ping *")) {
404 // HACK; TODO: answer only after an engine move
407 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
408 XB->ping = atoi(Star[0]);
409 uci_send_isready_sync(Uci);
411 ASSERT(XB->ping==-1);
412 gui_send(GUI,"pong %s",Star[0]);
414 } else if (match(string,"nps *")) {
416 // fake WB play-by-nodes mode
417 XB->node_rate = atoi(Star[0]);
418 } else if (match(string,"playother")) {
420 State->computer[game_turn(Game)] = FALSE;
421 State->computer[colour_opp(game_turn(Game))] = TRUE;
423 XB->new_hack = FALSE;
429 } else if (match(string,"post")) {
433 } else if (match(string,"protover *")) {
434 XB->proto_ver = atoi(Star[0]);
435 ASSERT(XB->proto_ver>=2);
436 send_xboard_options();
438 } else if (match(string,"quit")) {
439 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
441 } else if (match(string,"random")) {
445 } else if (match(string,"rating * *")) {
449 } else if (match(string,"remove")) {
451 if (game_pos(Game) >= 2) {
453 game_goto(Game,game_pos(Game)-2);
455 ASSERT(!XB->new_hack);
456 XB->new_hack = FALSE; // HACK?
463 } else if (match(string,"rejected *")) {
467 } else if (match(string,"reset")) { // protover 3?
471 gui_send(GUI,"Error (unknown command): %s",string);
474 || match(string,"result * {*}")
475 || match(string,"result * {* }")
476 || match(string,"result * { *}")
477 || match(string,"result * { * }")) {
479 my_log("POLYGLOT GAME END\n");
487 if (option_get_bool(Option,"Book") &&
488 option_get_bool(Option,"BookLearn")) {
491 } else if (my_string_equal(Star[0],"1-0")) {
493 } else if (my_string_equal(Star[0],"0-1")) {
495 } else if (my_string_equal(Star[0],"1/2-1/2")) {
499 } else if (match(string,"resume")) {
503 gui_send(GUI,"Error (unknown command): %s",string);
505 } else if (match(string,"option *=*") ||
506 match(string,"option * =*") ||
507 match(string,"option *= *") ||
508 match(string,"option * = *")
512 if(match(name, "Polyglot *")){
513 char *pg_name=Star[0];
514 polyglot_set_option(pg_name,value);
516 option_t *opt=option_find(Uci->option,name);
518 if(my_string_case_equal(opt->type,"check")){
519 value=my_string_equal(value,"1")?"true":"false";
521 start_protected_command();
522 uci_send_option(Uci, name, "%s", value);
523 end_protected_command();
525 gui_send(GUI,"Error (unknown option): %s",name);
528 } else if (match(string,"option *")){
530 if(match(name, "Polyglot *")){
531 char *pg_name=Star[0];
532 polyglot_set_option(pg_name,"<empty>");
534 start_protected_command();
536 if(!uci_send_option(Uci, name, "%s", "<empty>")){
537 gui_send(GUI,"Error (unknown option): %s",name);
539 end_protected_command();
541 } else if (XB->has_feature_smp && match(string,"cores *")){
542 int cores=atoi(Star[0]);
544 // updating the number of cores
545 my_log("POLYGLOT setting the number of cores to %d\n",cores);
546 start_protected_command();
547 uci_set_threads(Uci,cores);
548 end_protected_command();
551 gui_send(GUI,"Error (unknown command): %s",string);
553 } else if (match(string,"egtpath * *")){
556 if(my_string_empty(path)){
558 gui_send(GUI,"Error (unknown command): %s",string);
560 if(my_string_case_equal(type,"nalimov") && XB->has_feature_egt_nalimov){
561 // updating NalimovPath
562 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
563 start_protected_command();
564 uci_send_option(Uci,"NalimovPath","%s",path);
565 end_protected_command();
566 }else if(my_string_case_equal(type,"gaviota") && XB->has_feature_egt_gaviota){
567 // updating GaviotaPath
568 my_log("POLYGLOT setting the Gaviota path to %s\n",path);
569 start_protected_command();
570 uci_send_option(Uci,"GaviotaTbPath","%s",path);
571 end_protected_command();
574 gui_send(GUI,"Error (unsupported table base format): %s",string);
577 } else if (XB->has_feature_memory && match(string,"memory *")){
578 int memory = atoi(Star[0]);
582 // updating the available memory
584 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
585 if(XB->has_feature_egt_nalimov && (opt=option_find(Uci->option,"NalimovCache"))){
586 egt_cache=atoi(opt->value);
587 }else if(XB->has_feature_egt_gaviota &&
588 (opt=option_find(Uci->option,"GaviotaTbCache"))){
589 egt_cache=atoi(opt->value);
593 my_log("POLYGLOT EGTB Cache is %dMb\n",egt_cache);
594 real_memory=memory-egt_cache;
596 start_protected_command();
597 uci_send_option(Uci,"Hash", "%d", real_memory);
598 end_protected_command();
602 gui_send(GUI,"Error (unknown command): %s",string);
605 } else if (match(string,"sd *")) {
607 XB->depth_limit = TRUE;
608 XB->depth_max = atoi(Star[0]);
610 } else if (match(string,"setboard *")) {
612 my_log("POLYGLOT FEN %s\n",Star[0]);
614 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
616 State->computer[White] = FALSE;
617 State->computer[Black] = FALSE;
619 XB->new_hack = TRUE; // HACK?
625 } else if (match(string,"st *")) {
627 XB->time_limit = TRUE;
628 XB->time_max = ((double)atoi(Star[0]));
630 } else if (match(string,"time *")) {
632 XB->my_time = ((double)atoi(Star[0])) / 100.0;
633 if (XB->my_time < 0.0) XB->my_time = 0.0;
635 } else if (match(string,"undo")) {
637 move_list->size = 0; // [HGM] clear all exclude moves
639 if (game_pos(Game) >= 1) {
641 game_goto(Game,game_pos(Game)-1);
643 ASSERT(!XB->new_hack);
644 XB->new_hack = FALSE; // HACK?
651 } else if (match(string,"usermove *")) {
653 move_list->size = 0; // [HGM] clear all exclude moves
655 game_get_board(Game,board);
656 move = move_from_san(Star[0],board);
658 if (move != MoveNone && move_is_legal(move,board)) {
660 XB->new_hack = FALSE;
664 // [HGM] externally supplied move means we did not fully play the current stm
665 State->playedAllMoves[colour_is_white(game_turn(Game)) ? White : Black] = FALSE;
672 gui_send(GUI,"Illegal move: %s",Star[0]);
675 } else if (match(string,"variant *")) {
677 if (my_string_equal(Star[0],"fischerandom")) {
678 option_set(Option,"Chess960","true");
680 option_set(Option,"Chess960","false");
683 } else if (match(string,"white")) {
685 if (colour_is_white(game_turn(Game))) {
687 State->computer[White] = FALSE;
688 State->computer[Black] = TRUE;
696 } else if (match(string,"xboard")) {
700 } else if (match(string,".")) { // analyse info
702 if (State->state == ANALYSE) {
703 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
705 ASSERT(Uci->searching);
706 ASSERT(Uci->pending_nb>=1);
708 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
709 move_to_san(Uci->root_move,Uci->board,move_string,256);
710 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);
712 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
716 } else if (match(string,"?")) { // move now
718 if (State->state == THINK) {
720 ASSERT(Uci->searching);
721 ASSERT(Uci->pending_nb>=1);
723 // HACK: just send "stop" to the engine
725 if (Uci->searching) {
726 my_log("POLYGLOT STOP SEARCH\n");
727 engine_send(Engine,"stop");
731 } else { // unknown command, maybe a move?
733 game_get_board(Game,board);
734 move = move_from_san(string,board);
736 if (move != MoveNone && move_is_legal(move,board)) {
738 XB->new_hack = FALSE;
745 } else if (move != MoveNone) {
747 gui_send(GUI,"Illegal move: %s",string);
751 gui_send(GUI,"Error (unknown command): %s",string);
757 // xboard2uci_engine_step()
759 void xboard2uci_engine_step(char string[]) {
763 event = uci_parse(Uci,string);
767 if ((event & EVENT_READY) != 0) {
769 // the engine is now ready
773 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
776 if (!DelayPong && XB->ping >= 0) {
777 gui_send(GUI,"pong %d",XB->ping);
782 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
784 // the engine is playing a move
786 // MEGA HACK: estimate remaining time because XBoard won't send it!
788 my_timer_stop(State->timer);
790 XB->my_time -= my_timer_elapsed_real(State->timer);
791 XB->my_time += XB->inc;
792 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
794 if (XB->my_time < 0.0) XB->my_time = 0.0;
796 // make sure to remember the ponder move
798 State->hint_move=Uci->ponder_move;
800 // play the engine move
802 comp_move(Uci->best_move);
806 if ((event & EVENT_PV) != 0) {
808 // the engine has sent a new PV
812 if ((event & EVENT_INFO) != 0) {
814 // the engine has sent info
818 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
819 my_log("POYGLOT draw offer/resign from engine\n");
820 if(option_find(Uci->option,"UCI_DrawOffers")){
821 if(event & EVENT_DRAW)
822 gui_send(GUI,"offer draw");
824 gui_send(GUI,"resign");
827 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){
828 game_get_board(Game,board);
829 if(board->turn==White){
830 gui_send(GUI,"0-1 {polyglot: resign"
831 " (illegal engine move by white: %s)}",Uci->bestmove);
833 gui_send(GUI,"1-0 {polyglot: resign"
834 " (illegal engine move by black: %s)}",Uci->bestmove);
842 // format_xboard_option_line
844 void format_xboard_option_line(char * option_line, option_t *opt){
846 char option_string[StringSize];
848 strcpy(option_line,"");
849 // buffer overflow alert
850 strcat(option_line,"feature option=\"");
852 strcat(option_line,"Polyglot ");
854 sprintf(option_string,"%s",opt->name);
855 strcat(option_line,option_string);
856 sprintf(option_string," -%s",opt->type);
857 strcat(option_line,option_string);
858 if(!IS_BUTTON(opt->type) && strcmp(opt->type,"combo")){
859 if(strcmp(opt->type,"check")){
860 sprintf(option_string," %s",opt->value);
862 sprintf(option_string," %d",
863 my_string_case_equal(opt->value,"true")||
864 my_string_equal(opt->value,"1")
867 strcat(option_line,option_string);
869 if(IS_SPIN(opt->type)){
870 sprintf(option_string," %s",opt->min);
871 strcat(option_line,option_string);
873 if(IS_SPIN(opt->type)){
874 sprintf(option_string," %s",opt->max);
875 strcat(option_line,option_string);
877 for(j=0;j<opt->var_nb;j++){
878 if(!strcmp(opt->var[j],opt->value)){
879 sprintf(option_string," *%s",opt->var[j]);
881 sprintf(option_string," %s",opt->var[j]);
883 strcat(option_line,option_string);
884 if(j!=opt->var_nb-1){
885 strcat(option_line," ///");
888 strcat(option_line,"\"");
889 if(option_get_bool(Option,"WbWorkArounds") &&
890 (tmp=strstr(option_line,"Draw"))){
892 my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",
897 // disarm() // [HGM] cleanse a string of offending double-quotes
899 static char*disarm(const char *s){
903 q = buf + strlen(buf) - 1;
904 while(*q == '"') *q-- = '\0'; // strip trailing quotes
905 while(*p == '"') p++; // strip leading quotes
906 while((q = strchr(p, '"'))) *q = '\''; // replace internal quotes
910 // send_xboard_options()
912 static void send_xboard_options(){
914 char egtfeature[StringSize];
917 gui_send(GUI,"feature done=0");
919 gui_send(GUI,"feature analyze=1");
920 gui_send(GUI,"feature exclude=1");
921 gui_send(GUI,"feature colors=0");
922 gui_send(GUI,"feature draw=1");
923 gui_send(GUI,"feature ics=1");
924 gui_send(GUI,"feature myname=\"%s\"",
925 disarm(option_get_string(Option,"EngineName")));
926 gui_send(GUI,"feature name=1");
927 gui_send(GUI,"feature pause=0");
928 gui_send(GUI,"feature ping=1");
929 gui_send(GUI,"feature playother=1");
930 gui_send(GUI,"feature sigint=1");
931 gui_send(GUI,"feature reuse=1");
932 gui_send(GUI,"feature san=0");
933 gui_send(GUI,"feature setboard=1");
934 gui_send(GUI,"feature sigint=0");
935 gui_send(GUI,"feature sigterm=0");
936 gui_send(GUI,"feature time=1");
937 gui_send(GUI,"feature usermove=1");
938 gui_send(GUI,"feature nps=1");
939 if (XB->has_feature_memory){
940 gui_send(GUI,"feature memory=1");
942 gui_send(GUI,"feature memory=0");
944 if (XB->has_feature_smp){
945 gui_send(GUI,"feature smp=1");
947 gui_send(GUI,"feature smp=0");
950 strncat(egtfeature,"feature egt=\"",StringSize);
951 if (XB->has_feature_egt_nalimov){
953 strncat(egtfeature,"nalimov",StringSize-strlen(egtfeature));
955 if (XB->has_feature_egt_gaviota){
957 strncat(egtfeature,",",StringSize-strlen(egtfeature));
959 strncat(egtfeature,"gaviota",StringSize-strlen(egtfeature));
961 strncat(egtfeature,"\"",StringSize-strlen(egtfeature));
962 egtfeature[StringSize-1]='\0';
963 gui_send(GUI,egtfeature);
965 if (option_find(Uci->option,"UCI_Chess960")) {
966 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
968 gui_send(GUI,"feature variants=\"normal\"");
971 xboard2uci_send_options();
974 void xboard2uci_send_options(){
975 char option_line[StringSize]="";
979 option_start_iter(Uci->option);
980 while((opt=option_next(Uci->option))){
981 if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;
982 if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;
983 if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;
984 if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;
985 if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;
986 if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;
987 if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;
988 if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;
989 if(my_string_case_equal(opt->name,"Ponder")) continue;
990 if(my_string_case_equal(opt->name,"Hash")) continue;
991 if(my_string_case_equal(opt->name,"NalimovPath")) continue;
992 if(my_string_case_equal(opt->name,"GaviotaTbPath")) continue;
993 if((name=uci_thread_option(Uci))!=NULL &&
994 my_string_case_equal(opt->name,name)) continue;
995 format_xboard_option_line(option_line,opt);
997 gui_send(GUI,"%s",option_line);
1001 gui_send(GUI,"feature option=\"Polyglot exclude move -string \"");
1003 option_start_iter(Option);
1004 while((opt=option_next(Option))){
1005 if(opt->mode &XBOARD){
1006 format_xboard_option_line(option_line,opt);
1007 gui_send(GUI,"%s",option_line);
1010 gui_send(GUI,"feature done=1");
1014 // report_best_score()
1016 static int report_best_score(){
1017 if(!option_get_bool(Option,"ScoreWhite") ||
1018 colour_is_white(Uci->board->turn)){
1019 return Uci->best_score;
1021 return -Uci->best_score;
1027 static void comp_move(int move) {
1032 ASSERT(move_is_ok(move));
1034 ASSERT(State->state==THINK);
1035 ASSERT(!XB->analyse);
1037 if(option_get_bool(Option,"RepeatPV"))
1038 send_pv(); // to update time and nodes
1042 game_get_board(Game,board);
1044 if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
1045 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
1047 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
1050 gui_send(GUI,"move %s",string);
1054 if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {
1056 if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {
1059 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
1061 if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
1062 if (!option_get_bool(Option,"QueenNeverResigns") || !board_has_queen(board, board->turn)) { // [HGM] suppress resignig with Queen
1063 my_log("POLYGLOT *** RESIGN ***\n");
1064 gui_send(GUI,"resign");
1070 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
1071 State->resign_nb = 0;
1083 static void move_step(int move) {
1086 char move_string[256];
1088 ASSERT(move_is_ok(move));
1092 game_get_board(Game,board);
1094 if (move != MoveNone && move_is_legal(move,board)) {
1096 move_to_san(move,board,move_string,256);
1097 my_log("POLYGLOT MOVE %s\n",move_string);
1101 move_to_can(move,board,move_string,256);
1102 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
1105 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
1110 game_add_move(Game,move);
1116 static void board_update() {
1120 ASSERT(!XB->result);
1122 switch (game_status(Game)) {
1126 gui_send(GUI,"1-0 {White mates}");
1129 gui_send(GUI,"0-1 {Black mates}");
1132 gui_send(GUI,"1/2-1/2 {Stalemate}");
1135 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
1138 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
1140 case DRAW_REPETITION:
1141 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
1151 static void mess() {
1153 // clear state variables
1155 State->resign_nb = 0;
1156 State->exp_move = MoveNone;
1157 my_timer_reset(State->timer);
1159 // abort a possible search
1163 // calculate the new state
1166 } else if (!active()) {
1167 State->state = WAIT;
1168 my_log("POLYGLOT WAIT\n");
1169 } else if (XB->analyse) {
1170 State->state = ANALYSE;
1171 my_log("POLYGLOT ANALYSE\n");
1172 } else if (State->computer[game_turn(Game)]) {
1173 State->state = THINK;
1174 my_log("POLYGLOT THINK\n");
1176 State->state = WAIT;
1177 my_log("POLYGLOT WAIT\n");
1185 static void no_mess(int move) {
1187 ASSERT(move_is_ok(move));
1189 // just received a move, calculate the new state
1193 } else if (!active()) {
1195 stop_search(); // abort a possible search
1197 State->state = WAIT;
1198 State->exp_move = MoveNone;
1200 my_log("POLYGLOT WAIT\n");
1202 } else if (State->state == WAIT) {
1204 ASSERT(State->computer[game_turn(Game)]);
1205 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1206 ASSERT(!XB->analyse);
1208 my_log("POLYGLOT WAIT -> THINK\n");
1210 State->state = THINK;
1211 State->exp_move = MoveNone;
1213 } else if (State->state == THINK) {
1215 ASSERT(!State->computer[game_turn(Game)]);
1216 ASSERT(State->computer[colour_opp(game_turn(Game))]);
1217 ASSERT(!XB->analyse);
1219 if (ponder() && ponder_ok(Uci->ponder_move)) {
1221 my_log("POLYGLOT THINK -> PONDER\n");
1223 State->state = PONDER;
1224 State->exp_move = Uci->ponder_move;
1228 my_log("POLYGLOT THINK -> WAIT\n");
1230 State->state = WAIT;
1231 State->exp_move = MoveNone;
1234 } else if (State->state == PONDER) {
1236 ASSERT(State->computer[game_turn(Game)]);
1237 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
1238 ASSERT(!XB->analyse);
1240 if (move == State->exp_move && Uci->searching) {
1242 ASSERT(Uci->searching);
1243 ASSERT(Uci->pending_nb>=1);
1245 my_timer_start(State->timer);//also resets
1247 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
1248 engine_send(Engine,"ponderhit");
1250 State->state = THINK;
1251 State->exp_move = MoveNone;
1253 send_pv(); // update display
1255 return; // do not launch a new search
1259 my_log("POLYGLOT PONDER -> THINK (miss)\n");
1263 State->state = THINK;
1264 State->exp_move = MoveNone;
1267 } else if (State->state == ANALYSE) {
1269 ASSERT(XB->analyse);
1271 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
1283 // start_protected_command()
1285 static void start_protected_command(){
1289 static void end_protected_command(){
1290 if(Uci->ready){ // not init faze
1291 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
1293 update_remaining_time();
1294 search_update(); // relaunch search if necessary
1297 // update_remaining_time()
1299 static void update_remaining_time(){
1301 if(State->timer->running){
1302 my_timer_stop(State->timer);
1303 reduce = my_timer_elapsed_real(State->timer);
1304 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
1305 XB->my_time -= reduce;
1306 if(XB->my_time<0.0){
1315 static void search_update() {
1321 ASSERT(!Uci->searching);
1326 // launch a new search if needed
1330 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
1332 // [VdB] moved up as we need the move number
1334 game_get_board(Game,Uci->board);
1338 if (State->state == THINK &&
1339 option_get_bool(Option,"Book") &&
1340 Uci->board->move_nb<option_get_int(Option,"BookDepth")
1344 move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));
1346 if (move != MoveNone && move_is_legal(move,Uci->board)) {
1348 my_log("POLYGLOT *BOOK MOVE*\n");
1350 search_clear(); // clears Uci->ponder_move
1351 Uci->best_move = move;
1353 board_copy(board,Uci->board);
1354 move_do(board,move);
1355 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
1357 Uci->best_pv[0] = Uci->best_move;
1358 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
1359 Uci->best_pv[2] = MoveNone;
1361 comp_move(Uci->best_move);
1369 my_log("POLYGLOT START SEARCH\n");
1373 uci_send_option(Uci,"UCI_Chess960","%s",
1374 option_get_bool(Option,"Chess960")?"true":"false");
1376 if (option_get_int(Option,"UCIVersion") >= 2) {
1377 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
1378 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
1381 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
1385 move = (State->state == PONDER) ? State->exp_move : MoveNone;
1386 send_board(move); // updates Uci->board global variable
1390 if (State->state == THINK || State->state == PONDER) {
1392 engine_send_queue(Engine,"go");
1394 if (XB->time_limit) {
1396 // fixed time per move
1398 if(XB->node_rate > 0){
1399 engine_send_queue(Engine,
1401 XB->time_max*((double)XB->node_rate));
1403 double computed_time;
1405 st_fudge=(double) option_get_int(Option,"STFudge");
1406 my_log("POLYGLOT Giving engine %.0fmsec extra time.\n",st_fudge);
1407 computed_time=XB->time_max*1000.0-st_fudge;
1408 if(computed_time< 1.0){
1411 engine_send_queue(Engine,
1420 if(XB->node_rate > 0) {
1424 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1426 time = XB->my_time / move_nb;
1430 if(time > XB->my_time){
1433 engine_send_queue(Engine,
1435 time*XB->node_rate);
1438 if (colour_is_white(Uci->board->turn)) {
1439 engine_send_queue(Engine,
1440 " wtime %.0f btime %.0f",
1441 XB->my_time*1000.0,XB->opp_time*1000.0);
1443 engine_send_queue(Engine,
1444 " wtime %.0f btime %.0f",
1445 XB->opp_time*1000.0,XB->my_time*1000.0);
1448 if (XB->inc != 0.0){
1449 engine_send_queue(Engine,
1450 " winc %.0f binc %.0f",
1451 XB->inc*1000.0,XB->inc*1000.0);
1455 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
1456 ASSERT(move_nb>=1&&move_nb<=XB->mps);
1458 engine_send_queue(Engine," movestogo %d",move_nb);
1462 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
1464 if (State->state == PONDER) engine_send_queue(Engine," ponder");
1466 engine_send(Engine,""); // newline
1468 } else if (State->state == ANALYSE) {
1470 char move_string[256];
1472 engine_send_queue(Engine,"go infinite");
1474 if(list_size(move_list)) {
1476 game_get_board(Game,board);
1477 engine_send_queue(Engine," searchmoves");
1478 for(i=0; i<list_size(move_list); i++) {
1479 if(!move_list->value[i]) {
1480 move_to_can(move_list->move[i],board,move_string,256);
1481 engine_send_queue(Engine," %s",move_string);
1485 engine_send(Engine,""); // newline
1494 ASSERT(!Uci->searching);
1498 Uci->searching = TRUE;
1505 static void search_clear() {
1511 my_timer_start(State->timer);//also resets
1516 static bool active() {
1520 if (game_status(Game) != PLAYING) return FALSE; // game ended
1524 if (XB->analyse) return TRUE; // analysing
1525 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
1526 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
1528 return TRUE; // playing
1533 static bool ponder() {
1535 return XB->ponder && (option_get_bool(Option,"CanPonder") ||
1536 option_find(Uci->option,"Ponder"));
1540 static bool ponder_ok(int move) {
1544 ASSERT(move==MoveNone||move_is_ok(move));
1546 // legal ponder move?
1548 if (move == MoveNone) return FALSE;
1550 game_get_board(Game,board);
1551 if (!move_is_legal(move,board)) return FALSE;
1553 // UCI-legal resulting position?
1555 game_add_move(Game,move);
1557 game_get_board(Game,board);
1558 status = game_status(Game);
1560 game_rem_move(Game);
1562 if (status != PLAYING) return FALSE; // game ended
1564 if (option_get_bool(Option,"Book") && is_in_book(board)) {
1573 static void stop_search() {
1575 if (Uci->searching) {
1577 ASSERT(Uci->searching);
1578 ASSERT(Uci->pending_nb>=1);
1580 my_log("POLYGLOT STOP SEARCH\n");
1583 engine_send(Engine,"stop");
1584 Uci->searching = FALSE;
1587 if (option_get_bool(Option,"SyncStop")) {
1588 uci_send_stop_sync(Uci);
1597 static void send_board(int extra_move) {
1606 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
1608 ASSERT(!Uci->searching);
1612 game_get_board(Game,Uci->board);
1613 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
1615 board_to_fen(Uci->board,fen,256);
1616 my_log("POLYGLOT FEN %s\n",fen);
1618 ASSERT(board_can_play(Uci->board));
1623 end = game_pos(Game);
1628 game_get_board_ex(Game,board,start);
1629 board_to_fen(board,string,256);
1631 engine_send_queue(Engine,"position");
1633 if (my_string_equal(string,StartFen)) {
1634 engine_send_queue(Engine," startpos");
1636 engine_send_queue(Engine," fen %s",string);
1641 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
1643 for (pos = start; pos < end; pos++) { // game moves
1645 move = game_move(Game,pos);
1647 move_to_can(move,board,string,256);
1648 engine_send_queue(Engine," %s",string);
1650 move_do(board,move);
1653 if (extra_move != MoveNone) { // move to ponder on
1654 move_to_can(extra_move,board,string,256);
1655 engine_send_queue(Engine," %s",string);
1660 engine_send(Engine,""); // newline
1665 static void send_info() {
1667 if(option_get_bool(Option,"WbWorkArounds2")){
1668 // Silly bug in some versions of WinBoard.
1669 // depth <=1 clears the engine output window.
1670 // Why shouldn't an engine be allowed to send info at depth 1?
1675 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
1676 0,0.0,U64(0),Uci->info);
1681 static void send_pv() {
1683 char pv_string[StringSize];
1686 char move_string[StringSize];
1688 ASSERT(State->state!=WAIT);
1690 if (Uci->best_depth == 0) return;
1692 // xboard search information
1696 if (State->state == THINK || State->state == ANALYSE) {
1698 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1700 if(Uci->depth==-1) //hack to clear the engine output window
1701 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);
1703 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);
1705 } else if (State->state == PONDER &&
1706 option_get_bool(Option,"ShowPonder")) {
1708 game_get_board(Game,board);
1709 move = State->exp_move;
1711 if (move != MoveNone && move_is_legal(move,board)) {
1712 move_to_san(move,board,move_string,256);
1713 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1714 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);
1721 if ((Uci->searching &&
1722 option_get_bool(Option,"KibitzPV") &&
1723 Uci->time >= option_get_double(Option,"KibitzDelay"))
1724 || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {
1726 if (State->state == THINK || State->state == ANALYSE) {
1728 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1729 if(kibitz_throttle(Uci->searching)){
1730 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);
1732 } else if (State->state == PONDER) {
1734 game_get_board(Game,board);
1735 move = State->exp_move;
1737 if (move != MoveNone && move_is_legal(move,board)) {
1738 move_to_san(move,board,move_string,256);
1739 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
1740 if(kibitz_throttle(Uci->searching)){
1741 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);
1748 // kibitz_throttle()
1750 static bool kibitz_throttle(bool searching){
1752 static time_t lastKibitzMove=0;
1753 static time_t lastKibitzPV=0;
1754 curr_time = time(NULL);
1755 if(searching){ // KibitzPV
1757 (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
1758 lastKibitzPV=curr_time;
1761 }else{ // KibitzMove
1763 (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
1764 lastKibitzPV=curr_time;
1765 lastKibitzMove=curr_time;
1774 static void learn(int result) {
1780 ASSERT(result>=-1&&result<=+1);
1783 // ASSERT(State->computer[White]||State->computer[Black]);
1789 // [HGM] does not account for the hypothetical possibility we played both sides!
1790 if (State->playedAllMoves[White]) {
1792 } else if (State->playedAllMoves[Black]) {
1796 return; // [HGM] if we did not play all moves for some side, do not learn, but don't make a fuss!
1800 } else if (result > 0) {
1801 my_log("POLYGLOT *LEARN WIN*\n");
1802 } else if (result < 0) {
1803 my_log("POLYGLOT *LEARN LOSS*\n");
1805 my_log("POLYGLOT *LEARN DRAW*\n");
1810 for (; pos < Game->size; pos += 2) {
1812 game_get_board_ex(Game,board,pos);
1813 move = game_move(Game,pos);
1815 book_learn_move(board,move,result);
1821 // end of xboard2uci.c