22 #include "move_do.h"
\r
23 #include "move_legal.h"
\r
28 #include "uci2uci.h"
\r
33 #define StringSize 4096
\r
37 static const bool UseDebug = FALSE;
\r
38 static const bool DelayPong = FALSE;
\r
44 bool computer[ColourNb];
\r
47 my_timer_t timer[1];
\r
51 bool has_feature_memory;
\r
52 bool has_feature_smp;
\r
53 bool has_feature_egt;
\r
58 bool new_hack; // "new" is a C++ keyword
\r
79 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;
\r
83 static state_t State[1];
\r
88 static void comp_move (int move);
\r
89 static void move_step (int move);
\r
90 static void board_update ();
\r
92 static void mess ();
\r
93 static void no_mess (int move);
\r
95 static void search_update ();
\r
96 static void search_clear ();
\r
97 static void update_remaining_time();
\r
98 static int report_best_score();
\r
99 static bool kibitz_throttle (bool searching);
\r
100 static void start_protected_command();
\r
101 static void end_protected_command();
\r
103 static bool active ();
\r
104 static bool ponder ();
\r
105 static bool ponder_ok (int ponder_move);
\r
107 static void stop_search ();
\r
109 static void send_board (int extra_move);
\r
110 static void send_pv ();
\r
112 static void send_xboard_options ();
\r
114 static void learn (int result);
\r
119 // xboard2uci_init()
\r
121 void xboard2uci_init() {
\r
128 State->state = WAIT;
\r
130 State->computer[White] = FALSE;
\r
131 State->computer[Black] = TRUE;
\r
133 State->exp_move = MoveNone;
\r
134 State->resign_nb = 0;
\r
135 my_timer_reset(State->timer);
\r
137 // yes there are engines that do not have the "Hash" option....
\r
138 XB->has_feature_memory= uci_option_exist(Uci,"Hash");
\r
139 XB->has_feature_smp = uci_thread_option_exist(Uci);
\r
140 // TODO: support for other types of table bases
\r
141 XB->has_feature_egt = uci_option_exist(Uci,"NalimovPath");
\r
142 XB->analyse = FALSE;
\r
143 XB->computer = FALSE;
\r
145 my_string_set(&XB->name,"<empty>");
\r
147 XB->new_hack = TRUE;
\r
149 XB->ponder = FALSE;
\r
152 XB->result = FALSE;
\r
158 XB->time_limit = FALSE;
\r
159 XB->time_max = 5.0;
\r
161 XB->depth_limit = FALSE;
\r
162 XB->depth_max = 127;
\r
164 XB->my_time = 300.0;
\r
165 XB->opp_time = 300.0;
\r
168 // xboard2uci_gui_step()
\r
170 void xboard2uci_gui_step(char string[]) {
\r
173 char move_string[256];
\r
178 } else if (match(string,"accepted *")) {
\r
182 } else if (match(string,"analyze")) {
\r
184 State->computer[White] = FALSE;
\r
185 State->computer[Black] = FALSE;
\r
187 XB->analyse = TRUE;
\r
188 XB->new_hack = FALSE;
\r
189 ASSERT(!XB->result);
\r
190 XB->result = FALSE;
\r
194 } else if (match(string,"bk")) {
\r
196 if (option_get_bool("Book")) {
\r
197 game_get_board(Game,board);
\r
201 } else if (match(string,"black")) {
\r
203 if (colour_is_black(game_turn(Game))) {
\r
205 State->computer[White] = TRUE;
\r
206 State->computer[Black] = FALSE;
\r
208 XB->new_hack = TRUE;
\r
209 XB->result = FALSE;
\r
214 } else if (match(string,"computer")) {
\r
216 XB->computer = TRUE;
\r
218 } else if (match(string,"draw")) {
\r
219 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
220 my_log("POLYGLOT draw from XB received");
\r
221 uci_send_option(Uci,"DrawOffer","%s","draw");}
\r
222 } else if (match(string,"easy")) {
\r
224 XB->ponder = FALSE;
\r
228 } else if (match(string,"edit")) {
\r
232 gui_send(GUI,"Error (unknown command): %s",string);
\r
234 } else if (match(string,"exit")) {
\r
236 State->computer[White] = FALSE;
\r
237 State->computer[Black] = FALSE;
\r
239 XB->analyse = FALSE;
\r
243 } else if (match(string,"force")) {
\r
245 State->computer[White] = FALSE;
\r
246 State->computer[Black] = FALSE;
\r
250 } else if (match(string,"go")) {
\r
252 State->computer[game_turn(Game)] = TRUE;
\r
253 State->computer[colour_opp(game_turn(Game))] = FALSE;
\r
255 XB->new_hack = FALSE;
\r
256 ASSERT(!XB->result);
\r
257 XB->result = FALSE;
\r
261 } else if (match(string,"hard")) {
\r
267 } else if (match(string,"hint")) {
\r
269 if (option_get_bool("Book")) {
\r
271 game_get_board(Game,board);
\r
272 move = book_move(board,FALSE);
\r
274 if (move != MoveNone && move_is_legal(move,board)) {
\r
275 move_to_san(move,board,move_string,256);
\r
276 gui_send(GUI,"Hint: %s",move_string);
\r
280 } else if (match(string,"ics *")) {
\r
284 } else if (match(string,"level * *:* *")) {
\r
286 XB->mps = atoi(Star[0]);
\r
287 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
\r
288 XB->inc = ((double)atoi(Star[3]));
\r
290 } else if (match(string,"level * * *")) {
\r
292 XB->mps = atoi(Star[0]);
\r
293 XB->base = ((double)atoi(Star[1])) * 60.0;
\r
294 XB->inc = ((double)atoi(Star[2]));
\r
296 } else if (match(string,"name *")) {
\r
298 my_string_set(&XB->name,Star[0]);
\r
300 } else if (match(string,"new")) {
\r
302 uci_send_isready(Uci);
\r
303 my_log("POLYGLOT NEW GAME\n");
\r
305 option_set("Chess960","FALSE");
\r
310 State->computer[White] = FALSE;
\r
311 State->computer[Black] = FALSE;
\r
313 State->computer[White] = FALSE;
\r
314 State->computer[Black] = TRUE;
\r
317 XB->new_hack = TRUE;
\r
318 XB->result = FALSE;
\r
320 XB->depth_limit = FALSE;
\r
322 XB->computer = FALSE;
\r
323 my_string_set(&XB->name,"<empty>");
\r
328 uci_send_ucinewgame(Uci);
\r
330 } else if (match(string,"nopost")) {
\r
334 } else if (match(string,"otim *")) {
\r
336 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
\r
337 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
\r
339 } else if (match(string,"pause")) {
\r
343 gui_send(GUI,"Error (unknown command): %s",string);
\r
345 } else if (match(string,"ping *")) {
\r
347 // HACK; TODO: answer only after an engine move
\r
350 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
\r
351 XB->ping = atoi(Star[0]);
\r
352 uci_send_isready(Uci);
\r
354 ASSERT(XB->ping==-1);
\r
355 gui_send(GUI,"pong %s",Star[0]);
\r
358 } else if (match(string,"playother")) {
\r
360 State->computer[game_turn(Game)] = FALSE;
\r
361 State->computer[colour_opp(game_turn(Game))] = TRUE;
\r
363 XB->new_hack = FALSE;
\r
364 ASSERT(!XB->result);
\r
365 XB->result = FALSE;
\r
369 } else if (match(string,"post")) {
\r
373 } else if (match(string,"protover *")) {
\r
375 send_xboard_options();
\r
377 } else if (match(string,"quit")) {
\r
378 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
\r
380 } else if (match(string,"random")) {
\r
384 } else if (match(string,"rating * *")) {
\r
388 } else if (match(string,"remove")) {
\r
390 if (game_pos(Game) >= 2) {
\r
392 game_goto(Game,game_pos(Game)-2);
\r
394 ASSERT(!XB->new_hack);
\r
395 XB->new_hack = FALSE; // HACK?
\r
396 XB->result = FALSE;
\r
402 } else if (match(string,"rejected *")) {
\r
406 } else if (match(string,"reset")) { // protover 3?
\r
410 gui_send(GUI,"Error (unknown command): %s",string);
\r
413 || match(string,"result * {*}")
\r
414 || match(string,"result * {* }")
\r
415 || match(string,"result * { *}")
\r
416 || match(string,"result * { * }")) {
\r
418 my_log("POLYGLOT GAME END\n");
\r
426 if (option_get_bool("Book") && option_get_bool("BookLearn")) {
\r
429 } else if (my_string_equal(Star[0],"1-0")) {
\r
431 } else if (my_string_equal(Star[0],"0-1")) {
\r
433 } else if (my_string_equal(Star[0],"1/2-1/2")) {
\r
437 } else if (match(string,"resume")) {
\r
441 gui_send(GUI,"Error (unknown command): %s",string);
\r
443 } else if (match(string,"option *=*")){
\r
444 char *name=Star[0];
\r
445 char *value=Star[1];
\r
446 if(match(name, "Polyglot *")){
\r
447 char *pg_name=Star[0];
\r
448 polyglot_set_option(pg_name,value);
\r
450 start_protected_command();
\r
451 engine_send(Engine,"setoption name %s value %s",name,value);
\r
452 end_protected_command();
\r
454 } else if (match(string,"option *")){
\r
455 char *name=Star[0];
\r
456 start_protected_command();
\r
457 engine_send(Engine,"setoption name %s",name);
\r
458 end_protected_command();
\r
459 } else if (XB->has_feature_smp && match(string,"cores *")){
\r
460 int cores=atoi(Star[0]);
\r
462 // updating the number of cores
\r
463 my_log("POLYGLOT setting the number of cores to %d\n",cores);
\r
464 start_protected_command();
\r
465 uci_set_threads(Uci,cores);
\r
466 end_protected_command();
\r
469 gui_send(GUI,"Error (unknown command): %s",string);
\r
471 } else if (XB->has_feature_egt && match(string,"egtpath * *")){
\r
472 char *type=Star[0];
\r
473 char *path=Star[1];
\r
474 if(!my_string_case_equal(type,"nalimov")){
\r
476 gui_send(GUI,"Error (unsupported table base format): %s",string);
\r
477 }else if(my_string_empty(path)){
\r
479 gui_send(GUI,"Error (unknown command): %s",string);
\r
481 // updating NalimovPath
\r
482 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
\r
483 start_protected_command();
\r
484 uci_send_option(Uci,"NalimovPath","%s",path);
\r
485 end_protected_command();
\r
487 } else if (XB->has_feature_memory && match(string,"memory *")){
\r
488 int memory = atoi(Star[0]);
\r
492 // updating the available memory
\r
493 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
\r
494 if(uci_get_option(Uci,"NalimovCache")>=0){
\r
495 nalimov_cache=atoi(Uci->option[uci_get_option(Uci,"NalimovCache")].value);
\r
499 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);
\r
500 real_memory=memory-nalimov_cache;
\r
502 start_protected_command();
\r
503 uci_send_option(Uci,"Hash", "%d", real_memory);
\r
504 end_protected_command();
\r
508 gui_send(GUI,"Error (unknown command): %s",string);
\r
511 } else if (match(string,"sd *")) {
\r
513 XB->depth_limit = TRUE;
\r
514 XB->depth_max = atoi(Star[0]);
\r
516 } else if (match(string,"setboard *")) {
\r
518 my_log("POLYGLOT FEN %s\n",Star[0]);
\r
520 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
\r
522 State->computer[White] = FALSE;
\r
523 State->computer[Black] = FALSE;
\r
525 XB->new_hack = TRUE; // HACK?
\r
526 XB->result = FALSE;
\r
531 } else if (match(string,"st *")) {
\r
533 XB->time_limit = TRUE;
\r
534 XB->time_max = ((double)atoi(Star[0]));
\r
536 } else if (match(string,"time *")) {
\r
538 XB->my_time = ((double)atoi(Star[0])) / 100.0;
\r
539 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
541 } else if (match(string,"undo")) {
\r
543 if (game_pos(Game) >= 1) {
\r
545 game_goto(Game,game_pos(Game)-1);
\r
547 ASSERT(!XB->new_hack);
\r
548 XB->new_hack = FALSE; // HACK?
\r
549 XB->result = FALSE;
\r
555 } else if (match(string,"usermove *")) {
\r
557 game_get_board(Game,board);
\r
558 move = move_from_san(Star[0],board);
\r
560 if (move != MoveNone && move_is_legal(move,board)) {
\r
562 XB->new_hack = FALSE;
\r
563 ASSERT(!XB->result);
\r
564 XB->result = FALSE;
\r
571 gui_send(GUI,"Illegal move: %s",Star[0]);
\r
574 } else if (match(string,"variant *")) {
\r
576 if (my_string_equal(Star[0],"fischerandom")) {
\r
577 option_set("Chess960","TRUE");
\r
579 option_set("Chess960","FALSE");
\r
582 } else if (match(string,"white")) {
\r
584 if (colour_is_white(game_turn(Game))) {
\r
586 State->computer[White] = FALSE;
\r
587 State->computer[Black] = TRUE;
\r
589 XB->new_hack = TRUE;
\r
590 XB->result = FALSE;
\r
595 } else if (match(string,"xboard")) {
\r
599 } else if (match(string,".")) { // analyse info
\r
601 if (State->state == ANALYSE) {
\r
602 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
\r
604 ASSERT(Uci->searching);
\r
605 ASSERT(Uci->pending_nb>=1);
\r
607 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
\r
608 move_to_san(Uci->root_move,Uci->board,move_string,256);
\r
609 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);
\r
611 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
\r
615 } else if (match(string,"?")) { // move now
\r
617 if (State->state == THINK) {
\r
619 ASSERT(Uci->searching);
\r
620 ASSERT(Uci->pending_nb>=1);
\r
622 // HACK: just send "stop" to the engine
\r
624 if (Uci->searching) {
\r
625 my_log("POLYGLOT STOP SEARCH\n");
\r
626 engine_send(Engine,"stop");
\r
630 } else { // unknown command, maybe a move?
\r
632 game_get_board(Game,board);
\r
633 move = move_from_san(string,board);
\r
635 if (move != MoveNone && move_is_legal(move,board)) {
\r
637 XB->new_hack = FALSE;
\r
638 ASSERT(!XB->result);
\r
639 XB->result = FALSE;
\r
644 } else if (move != MoveNone) {
\r
646 gui_send(GUI,"Illegal move: %s",string);
\r
650 gui_send(GUI,"Error (unknown command): %s",string);
\r
656 // xboard2uci_engine_step()
\r
658 void xboard2uci_engine_step(char string[]) {
\r
661 event = uci_parse(Uci,string);
\r
665 if ((event & EVENT_READY) != 0) {
\r
667 // the engine is now ready
\r
671 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
\r
674 if (!DelayPong && XB->ping >= 0) {
\r
675 gui_send(GUI,"pong %d",XB->ping);
\r
680 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
\r
682 // the engine is playing a move
\r
684 // MEGA HACK: estimate remaining time because XBoard won't send it!
\r
686 my_timer_stop(State->timer);
\r
688 XB->my_time -= my_timer_elapsed_real(State->timer);
\r
689 XB->my_time += XB->inc;
\r
690 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
\r
692 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
694 // play the engine move
\r
696 comp_move(Uci->best_move);
\r
699 if ((event & EVENT_PV) != 0) {
\r
701 // the engine has sent a new PV
\r
705 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
\r
706 my_log("POYGLOT draw offer/resign from engine\n");
\r
707 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
708 if(event & EVENT_DRAW)
\r
709 gui_send(GUI,"offer draw");
\r
711 gui_send(GUI,"resign");
\r
716 // format_xboard_option_line
\r
718 void format_xboard_option_line(char * option_line, option_t *opt){
\r
720 char option_string[StringSize];
\r
721 strcpy(option_line,"");
\r
722 strcat(option_line,"feature option=\"");
\r
724 strcat(option_line,"Polyglot ");
\r
726 sprintf(option_string,"%s",opt->name);
\r
727 strcat(option_line,option_string);
\r
728 sprintf(option_string," -%s",opt->type);
\r
729 strcat(option_line,option_string);
\r
730 if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){
\r
731 if(strcmp(opt->type,"check")){
\r
732 sprintf(option_string," %s",opt->default_);
\r
734 sprintf(option_string," %d",
\r
735 strcmp(opt->default_,"TRUE")?0:1);
\r
737 strcat(option_line,option_string);
\r
739 if(!strcmp(opt->type,"spin")){
\r
740 sprintf(option_string," %s",opt->min);
\r
741 strcat(option_line,option_string);
\r
743 if(!strcmp(opt->type,"spin")){
\r
744 sprintf(option_string," %s",opt->max);
\r
745 strcat(option_line,option_string);
\r
747 for(j=0;j<opt->var_nb;j++){
\r
748 if(!strcmp(opt->var[j],opt->default_)){
\r
749 sprintf(option_string," *%s",opt->var[j]);
\r
751 sprintf(option_string," %s",opt->var[j]);
\r
753 strcat(option_line,option_string);
\r
754 if(j!=opt->var_nb-1){
\r
755 strcat(option_line," ///");
\r
758 strcat(option_line,"\"");
\r
761 // send_xboard_options
\r
763 static void send_xboard_options(){
\r
765 char option_line[StringSize]="";
\r
766 option_t *p=Option;
\r
768 XB->proto_ver = atoi(Star[0]);
\r
769 ASSERT(XB->proto_ver>=2);
\r
771 gui_send(GUI,"feature done=0");
\r
773 gui_send(GUI,"feature analyze=1");
\r
774 gui_send(GUI,"feature colors=0");
\r
775 gui_send(GUI,"feature draw=1");
\r
776 gui_send(GUI,"feature ics=1");
\r
777 gui_send(GUI,"feature myname=\"%s\"",option_get_string("EngineName"));
\r
778 gui_send(GUI,"feature name=1");
\r
779 gui_send(GUI,"feature pause=0");
\r
780 gui_send(GUI,"feature ping=1");
\r
781 gui_send(GUI,"feature playother=1");
\r
782 gui_send(GUI,"feature reuse=1");
\r
783 gui_send(GUI,"feature san=0");
\r
784 gui_send(GUI,"feature setboard=1");
\r
785 gui_send(GUI,"feature sigint=0");
\r
786 gui_send(GUI,"feature sigterm=0");
\r
787 gui_send(GUI,"feature time=1");
\r
788 gui_send(GUI,"feature usermove=1");
\r
789 if (XB->has_feature_memory){
\r
790 gui_send(GUI,"feature memory=1");
\r
792 gui_send(GUI,"feature memory=0");
\r
794 if (XB->has_feature_smp){
\r
795 gui_send(GUI,"feature smp=1");
\r
797 gui_send(GUI,"feature smp=0");
\r
799 if (XB->has_feature_egt){
\r
800 // TODO: support for other types of table bases
\r
801 gui_send(GUI,"feature egt=\"nalimov\"");
\r
803 gui_send(GUI,"feature egt=\"\"");
\r
806 if (uci_option_exist(Uci,"UCI_Chess960")) {
\r
807 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
\r
809 gui_send(GUI,"feature variants=\"normal\"");
\r
812 for(i=0;i<Uci->option_nb;i++){
\r
813 if(my_string_case_equal(Uci->option[i].name,"UCI_AnalyseMode")) continue;
\r
814 if(my_string_case_equal(Uci->option[i].name,"Ponder")) continue;
\r
815 if(my_string_case_equal(Uci->option[i].name,"Hash")) continue;
\r
816 if(my_string_case_equal(Uci->option[i].name,"NalimovPath")) continue;
\r
817 if((name=uci_thread_option(Uci))!=NULL && my_string_case_equal(Uci->option[i].name,name)) continue;
\r
818 format_xboard_option_line(option_line,Uci->option+i);
\r
820 gui_send(GUI,"%s",option_line);
\r
824 if(p->mode &XBOARD){
\r
825 format_xboard_option_line(option_line,p);
\r
826 gui_send(GUI,"%s",option_line);
\r
830 gui_send(GUI,"feature done=1");
\r
834 // report_best_score()
\r
836 static int report_best_score(){
\r
837 if(!option_get_bool("ScoreWhite") || colour_is_white(Uci->board->turn)){
\r
838 return Uci->best_score;
\r
840 return -Uci->best_score;
\r
846 static void comp_move(int move) {
\r
851 ASSERT(move_is_ok(move));
\r
853 ASSERT(State->state==THINK);
\r
854 ASSERT(!XB->analyse);
\r
856 if(option_get_bool("RepeatPV"))
\r
857 send_pv(); // to update time and nodes
\r
861 game_get_board(Game,board);
\r
863 if (move_is_castle(move,board) && option_get_bool("Chess960")) {
\r
864 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
\r
866 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
\r
869 gui_send(GUI,"move %s",string);
\r
873 if (option_get_bool("Resign") && Uci->root_move_nb > 1) {
\r
875 if (Uci->best_score <= -abs(option_get_int("ResignScore"))) {
\r
877 State->resign_nb++;
\r
878 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
\r
880 if (State->resign_nb >= option_get_int("ResignMoves")) {
\r
881 my_log("POLYGLOT *** RESIGN ***\n");
\r
882 gui_send(GUI,"resign");
\r
887 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
\r
888 State->resign_nb = 0;
\r
900 static void move_step(int move) {
\r
903 char move_string[256];
\r
905 ASSERT(move_is_ok(move));
\r
909 game_get_board(Game,board);
\r
911 if (move != MoveNone && move_is_legal(move,board)) {
\r
913 move_to_san(move,board,move_string,256);
\r
914 my_log("POLYGLOT MOVE %s\n",move_string);
\r
918 move_to_can(move,board,move_string,256);
\r
919 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
\r
922 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
\r
927 game_add_move(Game,move);
\r
933 static void board_update() {
\r
937 ASSERT(!XB->result);
\r
939 switch (game_status(Game)) {
\r
943 gui_send(GUI,"1-0 {White mates}");
\r
946 gui_send(GUI,"0-1 {Black mates}");
\r
949 gui_send(GUI,"1/2-1/2 {Stalemate}");
\r
951 case DRAW_MATERIAL:
\r
952 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
\r
955 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
\r
957 case DRAW_REPETITION:
\r
958 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
\r
968 static void mess() {
\r
970 // clear state variables
\r
972 State->resign_nb = 0;
\r
973 State->exp_move = MoveNone;
\r
974 my_timer_reset(State->timer);
\r
976 // abort a possible search
\r
980 // calculate the new state
\r
983 } else if (!active()) {
\r
984 State->state = WAIT;
\r
985 my_log("POLYGLOT WAIT\n");
\r
986 } else if (XB->analyse) {
\r
987 State->state = ANALYSE;
\r
988 my_log("POLYGLOT ANALYSE\n");
\r
989 } else if (State->computer[game_turn(Game)]) {
\r
990 State->state = THINK;
\r
991 my_log("POLYGLOT THINK\n");
\r
993 State->state = WAIT;
\r
994 my_log("POLYGLOT WAIT\n");
\r
1002 static void no_mess(int move) {
\r
1004 ASSERT(move_is_ok(move));
\r
1006 // just received a move, calculate the new state
\r
1010 } else if (!active()) {
\r
1012 stop_search(); // abort a possible search
\r
1014 State->state = WAIT;
\r
1015 State->exp_move = MoveNone;
\r
1017 my_log("POLYGLOT WAIT\n");
\r
1019 } else if (State->state == WAIT) {
\r
1021 ASSERT(State->computer[game_turn(Game)]);
\r
1022 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1023 ASSERT(!XB->analyse);
\r
1025 my_log("POLYGLOT WAIT -> THINK\n");
\r
1027 State->state = THINK;
\r
1028 State->exp_move = MoveNone;
\r
1030 } else if (State->state == THINK) {
\r
1032 ASSERT(!State->computer[game_turn(Game)]);
\r
1033 ASSERT(State->computer[colour_opp(game_turn(Game))]);
\r
1034 ASSERT(!XB->analyse);
\r
1036 if (ponder() && ponder_ok(Uci->ponder_move)) {
\r
1038 my_log("POLYGLOT THINK -> PONDER\n");
\r
1040 State->state = PONDER;
\r
1041 State->exp_move = Uci->ponder_move;
\r
1045 my_log("POLYGLOT THINK -> WAIT\n");
\r
1047 State->state = WAIT;
\r
1048 State->exp_move = MoveNone;
\r
1051 } else if (State->state == PONDER) {
\r
1053 ASSERT(State->computer[game_turn(Game)]);
\r
1054 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1055 ASSERT(!XB->analyse);
\r
1057 if (move == State->exp_move && Uci->searching) {
\r
1059 ASSERT(Uci->searching);
\r
1060 ASSERT(Uci->pending_nb>=1);
\r
1062 my_timer_start(State->timer);//also resets
\r
1064 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
\r
1065 engine_send(Engine,"ponderhit");
\r
1067 State->state = THINK;
\r
1068 State->exp_move = MoveNone;
\r
1070 send_pv(); // update display
\r
1072 return; // do not launch a new search
\r
1076 my_log("POLYGLOT PONDER -> THINK (miss)\n");
\r
1080 State->state = THINK;
\r
1081 State->exp_move = MoveNone;
\r
1084 } else if (State->state == ANALYSE) {
\r
1086 ASSERT(XB->analyse);
\r
1088 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
\r
1100 // start_protected_command()
\r
1102 static void start_protected_command(){
\r
1106 static void end_protected_command(){
\r
1107 if(Uci->ready){ // not init faze
\r
1108 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
\r
1110 update_remaining_time();
\r
1111 search_update(); // relaunch search if necessary
\r
1114 // update_remaining_time()
\r
1116 static void update_remaining_time(){
\r
1118 if(State->timer->running){
\r
1119 my_timer_stop(State->timer);
\r
1120 reduce = my_timer_elapsed_real(State->timer);
\r
1121 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
\r
1122 XB->my_time -= reduce;
\r
1123 if(XB->my_time<0.0){
\r
1130 // search_update()
\r
1132 static void search_update() {
\r
1138 ASSERT(!Uci->searching);
\r
1143 // launch a new search if needed
\r
1147 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
\r
1151 if (State->state == THINK && option_get_bool("Book")) {
\r
1153 game_get_board(Game,Uci->board);
\r
1155 move = book_move(Uci->board,option_get_bool("BookRandom"));
\r
1157 if (move != MoveNone && move_is_legal(move,Uci->board)) {
\r
1159 my_log("POLYGLOT *BOOK MOVE*\n");
\r
1161 search_clear(); // clears Uci->ponder_move
\r
1162 Uci->best_move = move;
\r
1164 board_copy(board,Uci->board);
\r
1165 move_do(board,move);
\r
1166 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
\r
1168 Uci->best_pv[0] = Uci->best_move;
\r
1169 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
\r
1170 Uci->best_pv[2] = MoveNone;
\r
1172 comp_move(Uci->best_move);
\r
1180 my_log("POLYGLOT START SEARCH\n");
\r
1184 uci_send_option(Uci,"UCI_Chess960","%s",option_get_bool("Chess960")?"TRUE":"FALSE");
\r
1186 if (option_get_int("UCIVersion") >= 2) {
\r
1187 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
\r
1188 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"TRUE":"FALSE");
\r
1191 uci_send_option(Uci,"Ponder","%s",ponder()?"TRUE":"FALSE");
\r
1195 move = (State->state == PONDER) ? State->exp_move : MoveNone;
\r
1196 send_board(move); // updates Uci->board global variable
\r
1200 if (State->state == THINK || State->state == PONDER) {
\r
1202 engine_send_queue(Engine,"go");
\r
1204 if (XB->time_limit) {
\r
1206 // fixed time per move
\r
1208 engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);
\r
1214 if (colour_is_white(Uci->board->turn)) {
\r
1215 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);
\r
1217 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);
\r
1220 if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);
\r
1222 if (XB->mps != 0) {
\r
1224 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
\r
1225 ASSERT(move_nb>=1&&move_nb<=XB->mps);
\r
1227 engine_send_queue(Engine," movestogo %d",move_nb);
\r
1231 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
\r
1233 if (State->state == PONDER) engine_send_queue(Engine," ponder");
\r
1235 engine_send(Engine,""); // newline
\r
1237 } else if (State->state == ANALYSE) {
\r
1239 engine_send(Engine,"go infinite");
\r
1246 // init search info
\r
1248 ASSERT(!Uci->searching);
\r
1252 Uci->searching = TRUE;
\r
1253 Uci->pending_nb++;
\r
1259 static void search_clear() {
\r
1265 my_timer_start(State->timer);//also resets
\r
1270 static bool active() {
\r
1274 if (game_status(Game) != PLAYING) return FALSE; // game ended
\r
1278 if (XB->analyse) return TRUE; // analysing
\r
1279 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
\r
1280 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
\r
1282 return TRUE; // playing
\r
1287 static bool ponder() {
\r
1289 return XB->ponder && (option_get_bool("CanPonder") || uci_option_exist(Uci,"Ponder"));
\r
1293 static bool ponder_ok(int move) {
\r
1297 ASSERT(move==MoveNone||move_is_ok(move));
\r
1299 // legal ponder move?
\r
1301 if (move == MoveNone) return FALSE;
\r
1303 game_get_board(Game,board);
\r
1304 if (!move_is_legal(move,board)) return FALSE;
\r
1306 // UCI-legal resulting position?
\r
1308 game_add_move(Game,move);
\r
1310 game_get_board(Game,board);
\r
1311 status = game_status(Game);
\r
1313 game_rem_move(Game);
\r
1315 if (status != PLAYING) return FALSE; // game ended
\r
1317 if (option_get_bool("Book") && is_in_book(board)) {
\r
1326 static void stop_search() {
\r
1328 if (Uci->searching) {
\r
1330 ASSERT(Uci->searching);
\r
1331 ASSERT(Uci->pending_nb>=1);
\r
1333 my_log("POLYGLOT STOP SEARCH\n");
\r
1336 engine_send(Engine,"stop");
\r
1337 Uci->searching = FALSE;
\r
1340 if (option_get_bool("SyncStop")) {
\r
1341 uci_send_stop_sync(Uci);
\r
1343 uci_send_stop(Uci);
\r
1350 static void send_board(int extra_move) {
\r
1359 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
\r
1361 ASSERT(!Uci->searching);
\r
1365 game_get_board(Game,Uci->board);
\r
1366 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
\r
1368 board_to_fen(Uci->board,fen,256);
\r
1369 my_log("POLYGLOT FEN %s\n",fen);
\r
1371 ASSERT(board_can_play(Uci->board));
\r
1376 end = game_pos(Game);
\r
1377 ASSERT(end>=start);
\r
1381 game_get_board_ex(Game,board,start);
\r
1382 board_to_fen(board,string,256);
\r
1384 engine_send_queue(Engine,"position");
\r
1386 if (my_string_equal(string,StartFen)) {
\r
1387 engine_send_queue(Engine," startpos");
\r
1389 engine_send_queue(Engine," fen %s",string);
\r
1394 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
\r
1396 for (pos = start; pos < end; pos++) { // game moves
\r
1398 move = game_move(Game,pos);
\r
1400 move_to_can(move,board,string,256);
\r
1401 engine_send_queue(Engine," %s",string);
\r
1403 move_do(board,move);
\r
1406 if (extra_move != MoveNone) { // move to ponder on
\r
1407 move_to_can(extra_move,board,string,256);
\r
1408 engine_send_queue(Engine," %s",string);
\r
1413 engine_send(Engine,""); // newline
\r
1418 static void send_pv() {
\r
1420 char pv_string[StringSize];
\r
1423 char move_string[StringSize];
\r
1425 ASSERT(State->state!=WAIT);
\r
1427 if (Uci->best_depth == 0) return;
\r
1429 // xboard search information
\r
1433 if (State->state == THINK || State->state == ANALYSE) {
\r
1435 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1437 if(Uci->depth==-1) //hack to clear the engine output window
\r
1438 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);
\r
1440 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);
\r
1442 } else if (State->state == PONDER && option_get_bool("ShowPonder")) {
\r
1444 game_get_board(Game,board);
\r
1445 move = State->exp_move;
\r
1447 if (move != MoveNone && move_is_legal(move,board)) {
\r
1448 move_to_san(move,board,move_string,256);
\r
1449 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1450 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);
\r
1457 if ((Uci->searching && option_get_bool("KibitzPV") && Uci->time >= option_get_double("KibitzDelay"))
\r
1458 || (!Uci->searching && option_get_bool("KibitzMove"))) {
\r
1460 if (State->state == THINK || State->state == ANALYSE) {
\r
1462 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1463 if(kibitz_throttle(Uci->searching)){
\r
1464 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"%s\"",option_get_string("KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,pv_string);
\r
1466 } else if (State->state == PONDER) {
\r
1468 game_get_board(Game,board);
\r
1469 move = State->exp_move;
\r
1471 if (move != MoveNone && move_is_legal(move,board)) {
\r
1472 move_to_san(move,board,move_string,256);
\r
1473 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1474 if(kibitz_throttle(Uci->searching)){
\r
1475 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"(%s) %s\"",option_get_string("KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,move_string,pv_string);
\r
1482 // kibitz_throttle()
\r
1484 static bool kibitz_throttle(bool searching){
\r
1486 static time_t lastKibitzMove=0;
\r
1487 static time_t lastKibitzPV=0;
\r
1488 curr_time = time(NULL);
\r
1489 if(searching){ // KibitzPV
\r
1490 if(curr_time >= (option_get_int("KibitzInterval") + lastKibitzPV)){
\r
1491 lastKibitzPV=curr_time;
\r
1494 }else{ // KibitzMove
\r
1495 if(curr_time >= (option_get_int("KibitzInterval") + lastKibitzMove)){
\r
1496 lastKibitzPV=curr_time;
\r
1497 lastKibitzMove=curr_time;
\r
1506 static void learn(int result) {
\r
1512 ASSERT(result>=-1&&result<=+1);
\r
1514 ASSERT(XB->result);
\r
1515 ASSERT(State->computer[White]||State->computer[Black]);
\r
1522 } else if (State->computer[White]) {
\r
1524 } else if (State->computer[Black]) {
\r
1528 my_fatal("learn(): unknown side\n");
\r
1532 } else if (result > 0) {
\r
1533 my_log("POLYGLOT *LEARN WIN*\n");
\r
1534 } else if (result < 0) {
\r
1535 my_log("POLYGLOT *LEARN LOSS*\n");
\r
1537 my_log("POLYGLOT *LEARN DRAW*\n");
\r
1542 for (; pos < Game->size; pos += 2) {
\r
1544 game_get_board_ex(Game,board,pos);
\r
1545 move = game_move(Game,pos);
\r
1547 book_learn_move(board,move,result);
\r
1553 // end of adapter.cpp
\r