22 #include "move_do.h"
\r
23 #include "move_legal.h"
\r
28 #include "uci2uci.h"
\r
33 static const bool UseDebug = false;
\r
34 static const bool DelayPong = false;
\r
36 static const int StringSize = 4096;
\r
42 bool computer[ColourNb];
\r
45 my_timer_t timer[1];
\r
49 bool has_feature_memory;
\r
50 bool has_feature_smp;
\r
51 bool has_feature_egt;
\r
56 bool new_hack; // "new" is a C++ keyword
\r
77 enum dummy_state_t { WAIT, THINK, PONDER, ANALYSE };
\r
81 static state_t State[1];
\r
86 static void comp_move (int move);
\r
87 static void move_step (int move);
\r
88 static void board_update ();
\r
90 static void mess ();
\r
91 static void no_mess (int move);
\r
93 static void search_update ();
\r
94 static void search_clear ();
\r
95 static void update_remaining_time();
\r
96 static void start_protected_command();
\r
97 static void end_protected_command();
\r
99 static bool active ();
\r
100 static bool ponder ();
\r
101 static bool ponder_ok (int ponder_move);
\r
103 static void stop_search ();
\r
105 static void send_board (int extra_move);
\r
106 static void send_pv ();
\r
108 static void send_xboard_options ();
\r
110 static void learn (int result);
\r
117 void xboard_init() {
\r
124 State->state = WAIT;
\r
126 State->computer[White] = false;
\r
127 State->computer[Black] = true;
\r
129 State->exp_move = MoveNone;
\r
130 State->resign_nb = 0;
\r
131 my_timer_reset(State->timer);
\r
133 // yes there are engines that do not have the "Hash" option....
\r
134 XB->has_feature_memory= uci_option_exist(Uci,"Hash");
\r
135 XB->has_feature_smp = uci_thread_option_exist(Uci);
\r
136 // TODO: support for other types of table bases
\r
137 XB->has_feature_egt = uci_option_exist(Uci,"NalimovPath");
\r
138 XB->analyse = false;
\r
139 XB->computer = false;
\r
141 my_string_set(&XB->name,"<empty>");
\r
143 XB->new_hack = true;
\r
145 XB->ponder = false;
\r
148 XB->result = false;
\r
154 XB->time_limit = false;
\r
155 XB->time_max = 5.0;
\r
157 XB->depth_limit = false;
\r
158 XB->depth_max = 127;
\r
160 XB->my_time = 300.0;
\r
161 XB->opp_time = 300.0;
\r
164 void xboard_step(char string[]) {
\r
167 char move_string[256];
\r
169 static bool firsttime=true;
\r
172 if((match(string,"uci"))){
\r
173 my_log("POLYGLOT *** Switching to UCI mode ***\n");
\r
174 send_uci_options();
\r
175 option_set("UCI","true");
\r
178 //uci_send_isready(Uci); // In UCI mode this done by the GUI
\r
179 //Grrr...Toga can fixes the number of threads after "isready"
\r
180 //So we delay "isready"
\r
186 } else if (match(string,"accepted *")) {
\r
190 } else if (match(string,"analyze")) {
\r
192 State->computer[White] = false;
\r
193 State->computer[Black] = false;
\r
195 XB->analyse = true;
\r
196 XB->new_hack = false;
\r
197 ASSERT(!XB->result);
\r
198 XB->result = false;
\r
202 } else if (match(string,"bk")) {
\r
204 if (option_get_bool("Book")) {
\r
205 game_get_board(Game,board);
\r
209 } else if (match(string,"black")) {
\r
211 if (colour_is_black(game_turn(Game))) {
\r
213 State->computer[White] = true;
\r
214 State->computer[Black] = false;
\r
216 XB->new_hack = true;
\r
217 XB->result = false;
\r
222 } else if (match(string,"computer")) {
\r
224 XB->computer = true;
\r
226 } else if (match(string,"draw")) {
\r
227 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
228 my_log("POLYGLOT draw from XB received");
\r
229 uci_send_option(Uci,"DrawOffer","%s","draw");}
\r
230 } else if (match(string,"easy")) {
\r
232 XB->ponder = false;
\r
236 } else if (match(string,"edit")) {
\r
240 gui_send(GUI,"Error (unknown command): %s",string);
\r
242 } else if (match(string,"exit")) {
\r
244 State->computer[White] = false;
\r
245 State->computer[Black] = false;
\r
247 XB->analyse = false;
\r
251 } else if (match(string,"force")) {
\r
253 State->computer[White] = false;
\r
254 State->computer[Black] = false;
\r
258 } else if (match(string,"go")) {
\r
260 State->computer[game_turn(Game)] = true;
\r
261 State->computer[colour_opp(game_turn(Game))] = false;
\r
263 XB->new_hack = false;
\r
264 ASSERT(!XB->result);
\r
265 XB->result = false;
\r
269 } else if (match(string,"hard")) {
\r
275 } else if (match(string,"hint")) {
\r
277 if (option_get_bool("Book")) {
\r
279 game_get_board(Game,board);
\r
280 move = book_move(board,false);
\r
282 if (move != MoveNone && move_is_legal(move,board)) {
\r
283 move_to_san(move,board,move_string,256);
\r
284 gui_send(GUI,"Hint: %s",move_string);
\r
288 } else if (match(string,"ics *")) {
\r
292 } else if (match(string,"level * *:* *")) {
\r
294 XB->mps = atoi(Star[0]);
\r
295 XB->base = double(atoi(Star[1])) * 60.0 + double(atoi(Star[2]));
\r
296 XB->inc = double(atoi(Star[3]));
\r
298 } else if (match(string,"level * * *")) {
\r
300 XB->mps = atoi(Star[0]);
\r
301 XB->base = double(atoi(Star[1])) * 60.0;
\r
302 XB->inc = double(atoi(Star[2]));
\r
304 } else if (match(string,"name *")) {
\r
306 my_string_set(&XB->name,Star[0]);
\r
308 } else if (match(string,"new")) {
\r
310 uci_send_isready(Uci);
\r
311 my_log("POLYGLOT NEW GAME\n");
\r
313 option_set("Chess960","false");
\r
318 State->computer[White] = false;
\r
319 State->computer[Black] = false;
\r
321 State->computer[White] = false;
\r
322 State->computer[Black] = true;
\r
325 XB->new_hack = true;
\r
326 XB->result = false;
\r
328 XB->depth_limit = false;
\r
330 XB->computer = false;
\r
331 my_string_set(&XB->name,"<empty>");
\r
336 uci_send_ucinewgame(Uci);
\r
338 } else if (match(string,"nopost")) {
\r
342 } else if (match(string,"otim *")) {
\r
344 XB->opp_time = double(atoi(Star[0])) / 100.0;
\r
345 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
\r
347 } else if (match(string,"pause")) {
\r
351 gui_send(GUI,"Error (unknown command): %s",string);
\r
353 } else if (match(string,"ping *")) {
\r
355 // HACK; TODO: answer only after an engine move
\r
358 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
\r
359 XB->ping = atoi(Star[0]);
\r
360 uci_send_isready(Uci);
\r
362 ASSERT(XB->ping==-1);
\r
363 gui_send(GUI,"pong %s",Star[0]);
\r
366 } else if (match(string,"playother")) {
\r
368 State->computer[game_turn(Game)] = false;
\r
369 State->computer[colour_opp(game_turn(Game))] = true;
\r
371 XB->new_hack = false;
\r
372 ASSERT(!XB->result);
\r
373 XB->result = false;
\r
377 } else if (match(string,"post")) {
\r
381 } else if (match(string,"protover *")) {
\r
383 send_xboard_options();
\r
385 } else if (match(string,"quit")) {
\r
386 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
\r
388 } else if (match(string,"random")) {
\r
392 } else if (match(string,"rating * *")) {
\r
396 } else if (match(string,"remove")) {
\r
398 if (game_pos(Game) >= 2) {
\r
400 game_goto(Game,game_pos(Game)-2);
\r
402 ASSERT(!XB->new_hack);
\r
403 XB->new_hack = false; // HACK?
\r
404 XB->result = false;
\r
410 } else if (match(string,"rejected *")) {
\r
414 } else if (match(string,"reset")) { // protover 3?
\r
418 gui_send(GUI,"Error (unknown command): %s",string);
\r
421 || match(string,"result * {*}")
\r
422 || match(string,"result * {* }")
\r
423 || match(string,"result * { *}")
\r
424 || match(string,"result * { * }")) {
\r
426 my_log("POLYGLOT GAME END\n");
\r
434 if (option_get_bool("Book") && option_get_bool("BookLearn")) {
\r
437 } else if (my_string_equal(Star[0],"1-0")) {
\r
439 } else if (my_string_equal(Star[0],"0-1")) {
\r
441 } else if (my_string_equal(Star[0],"1/2-1/2")) {
\r
445 } else if (match(string,"resume")) {
\r
449 gui_send(GUI,"Error (unknown command): %s",string);
\r
451 } else if (match(string,"option *=*")){
\r
452 char *name=Star[0];
\r
453 char *value=Star[1];
\r
454 if(match(name, "Polyglot *")){
\r
455 char *pg_name=Star[0];
\r
456 polyglot_set_option(pg_name,value);
\r
458 start_protected_command();
\r
459 engine_send(Engine,"setoption name %s value %s",name,value);
\r
460 end_protected_command();
\r
462 } else if (match(string,"option *")){
\r
463 char *name=Star[0];
\r
464 start_protected_command();
\r
465 engine_send(Engine,"setoption name %s",name);
\r
466 end_protected_command();
\r
467 } else if (XB->has_feature_smp && match(string,"cores *")){
\r
468 int cores=atoi(Star[0]);
\r
470 // updating the number of cores
\r
471 my_log("POLYGLOT setting the number of cores to %d\n",cores);
\r
472 start_protected_command();
\r
473 uci_set_threads(Uci,cores);
\r
474 end_protected_command();
\r
477 gui_send(GUI,"Error (unknown command): %s",string);
\r
479 } else if (XB->has_feature_egt && match(string,"egtpath * *")){
\r
480 char *type=Star[0];
\r
481 char *path=Star[1];
\r
482 if(!my_string_case_equal(Star[0],"nalimov")){
\r
484 gui_send(GUI,"Error (unsupported table base format): %s",string);
\r
485 }else if(my_string_empty(path)){
\r
487 gui_send(GUI,"Error (unknown command): %s",string);
\r
489 // updating NalimovPath
\r
490 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
\r
491 start_protected_command();
\r
492 uci_send_option(Uci,"NalimovPath","%s",path);
\r
493 end_protected_command();
\r
495 } else if (XB->has_feature_memory && match(string,"memory *")){
\r
496 int memory = atoi(Star[0]);
\r
500 // updating the available memory
\r
501 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
\r
502 if(uci_get_option(Uci,"NalimovCache")>=0){
\r
503 nalimov_cache=atoi(Uci->option[uci_get_option(Uci,"NalimovCache")].value);
\r
507 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);
\r
508 real_memory=memory-nalimov_cache;
\r
510 start_protected_command();
\r
511 uci_send_option(Uci,"Hash", "%d", real_memory);
\r
512 end_protected_command();
\r
516 gui_send(GUI,"Error (unknown command): %s",string);
\r
519 } else if (match(string,"sd *")) {
\r
521 XB->depth_limit = true;
\r
522 XB->depth_max = atoi(Star[0]);
\r
524 } else if (match(string,"setboard *")) {
\r
526 my_log("POLYGLOT FEN %s\n",Star[0]);
\r
528 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
\r
530 State->computer[White] = false;
\r
531 State->computer[Black] = false;
\r
533 XB->new_hack = true; // HACK?
\r
534 XB->result = false;
\r
539 } else if (match(string,"st *")) {
\r
541 XB->time_limit = true;
\r
542 XB->time_max = double(atoi(Star[0]));
\r
544 } else if (match(string,"time *")) {
\r
546 XB->my_time = double(atoi(Star[0])) / 100.0;
\r
547 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
549 } else if (match(string,"undo")) {
\r
551 if (game_pos(Game) >= 1) {
\r
553 game_goto(Game,game_pos(Game)-1);
\r
555 ASSERT(!XB->new_hack);
\r
556 XB->new_hack = false; // HACK?
\r
557 XB->result = false;
\r
563 } else if (match(string,"usermove *")) {
\r
565 game_get_board(Game,board);
\r
566 move = move_from_san(Star[0],board);
\r
568 if (move != MoveNone && move_is_legal(move,board)) {
\r
570 XB->new_hack = false;
\r
571 ASSERT(!XB->result);
\r
572 XB->result = false;
\r
579 gui_send(GUI,"Illegal move: %s",Star[0]);
\r
582 } else if (match(string,"variant *")) {
\r
584 if (my_string_equal(Star[0],"fischerandom")) {
\r
585 option_set("Chess960","true");
\r
587 option_set("Chess960","false");
\r
590 } else if (match(string,"white")) {
\r
592 if (colour_is_white(game_turn(Game))) {
\r
594 State->computer[White] = false;
\r
595 State->computer[Black] = true;
\r
597 XB->new_hack = true;
\r
598 XB->result = false;
\r
603 } else if (match(string,"xboard")) {
\r
607 } else if (match(string,".")) { // analyse info
\r
609 if (State->state == ANALYSE) {
\r
610 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
\r
612 ASSERT(Uci->searching);
\r
613 ASSERT(Uci->pending_nb>=1);
\r
615 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
\r
616 move_to_san(Uci->root_move,Uci->board,move_string,256);
\r
617 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
619 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
\r
623 } else if (match(string,"?")) { // move now
\r
625 if (State->state == THINK) {
\r
627 ASSERT(Uci->searching);
\r
628 ASSERT(Uci->pending_nb>=1);
\r
630 // HACK: just send "stop" to the engine
\r
632 if (Uci->searching) {
\r
633 my_log("POLYGLOT STOP SEARCH\n");
\r
634 engine_send(Engine,"stop");
\r
638 } else { // unknown command, maybe a move?
\r
640 game_get_board(Game,board);
\r
641 move = move_from_san(string,board);
\r
643 if (move != MoveNone && move_is_legal(move,board)) {
\r
645 XB->new_hack = false;
\r
646 ASSERT(!XB->result);
\r
647 XB->result = false;
\r
652 } else if (move != MoveNone) {
\r
654 gui_send(GUI,"Illegal move: %s",string);
\r
658 gui_send(GUI,"Error (unknown command): %s",string);
\r
666 void engine_step(char string[]) {
\r
669 event = uci_parse(Uci,string);
\r
673 if ((event & EVENT_READY) != 0) {
\r
675 // the engine is now ready
\r
679 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
\r
682 if (!DelayPong && XB->ping >= 0) {
\r
683 gui_send(GUI,"pong %d",XB->ping);
\r
688 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
\r
690 // the engine is playing a move
\r
692 // MEGA HACK: estimate remaining time because XBoard won't send it!
\r
694 my_timer_stop(State->timer);
\r
696 XB->my_time -= my_timer_elapsed_real(State->timer);
\r
697 XB->my_time += XB->inc;
\r
698 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
\r
700 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
702 // play the engine move
\r
704 comp_move(Uci->best_move);
\r
707 if ((event & EVENT_PV) != 0) {
\r
709 // the engine has sent a new PV
\r
713 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
\r
714 my_log("POYGLOT draw offer/resign from engine\n");
\r
715 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
716 if(event & EVENT_DRAW)
\r
717 gui_send(GUI,"offer draw");
\r
719 gui_send(GUI,"resign");
\r
724 // format_xboard_option_line
\r
726 void format_xboard_option_line(char * option_line, option_t *opt){
\r
728 char option_string[StringSize];
\r
729 strcpy(option_line,"");
\r
730 strcat(option_line,"feature option=\"");
\r
732 strcat(option_line,"Polyglot ");
\r
734 sprintf(option_string,"%s",opt->name);
\r
735 strcat(option_line,option_string);
\r
736 sprintf(option_string," -%s",opt->type);
\r
737 strcat(option_line,option_string);
\r
738 if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){
\r
739 if(strcmp(opt->type,"check")){
\r
740 sprintf(option_string," %s",opt->default_);
\r
742 sprintf(option_string," %d",
\r
743 strcmp(opt->default_,"true")?0:1);
\r
745 strcat(option_line,option_string);
\r
747 if(!strcmp(opt->type,"spin")){
\r
748 sprintf(option_string," %s",opt->min);
\r
749 strcat(option_line,option_string);
\r
751 if(!strcmp(opt->type,"spin")){
\r
752 sprintf(option_string," %s",opt->max);
\r
753 strcat(option_line,option_string);
\r
755 for(j=0;j<opt->var_nb;j++){
\r
756 if(!strcmp(opt->var[j],opt->default_)){
\r
757 sprintf(option_string," *%s",opt->var[j]);
\r
759 sprintf(option_string," %s",opt->var[j]);
\r
761 strcat(option_line,option_string);
\r
762 if(j!=opt->var_nb-1){
\r
763 strcat(option_line," ///");
\r
766 strcat(option_line,"\"");
\r
769 // send_xboard_options
\r
771 static void send_xboard_options(){
\r
773 char option_line[StringSize]="";
\r
774 option_t *p=Option;
\r
776 XB->proto_ver = atoi(Star[0]);
\r
777 ASSERT(XB->proto_ver>=2);
\r
779 gui_send(GUI,"feature done=0");
\r
781 gui_send(GUI,"feature analyze=1");
\r
782 gui_send(GUI,"feature colors=0");
\r
783 gui_send(GUI,"feature draw=1");
\r
784 gui_send(GUI,"feature ics=1");
\r
785 gui_send(GUI,"feature myname=\"%s\"",option_get_string("EngineName"));
\r
786 gui_send(GUI,"feature name=1");
\r
787 gui_send(GUI,"feature pause=0");
\r
788 gui_send(GUI,"feature ping=1");
\r
789 gui_send(GUI,"feature playother=1");
\r
790 gui_send(GUI,"feature reuse=1");
\r
791 gui_send(GUI,"feature san=0");
\r
792 gui_send(GUI,"feature setboard=1");
\r
793 gui_send(GUI,"feature sigint=0");
\r
794 gui_send(GUI,"feature sigterm=0");
\r
795 gui_send(GUI,"feature time=1");
\r
796 gui_send(GUI,"feature usermove=1");
\r
797 if (XB->has_feature_memory){
\r
798 gui_send(GUI,"feature memory=1");
\r
800 gui_send(GUI,"feature memory=0");
\r
802 if (XB->has_feature_smp){
\r
803 gui_send(GUI,"feature smp=1");
\r
805 gui_send(GUI,"feature smp=0");
\r
807 if (XB->has_feature_egt){
\r
808 // TODO: support for other types of table bases
\r
809 gui_send(GUI,"feature egt=\"nalimov\"");
\r
811 gui_send(GUI,"feature egt=\"\"");
\r
814 if (uci_option_exist(Uci,"UCI_Chess960")) {
\r
815 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
\r
817 gui_send(GUI,"feature variants=\"normal\"");
\r
820 for(i=0;i<Uci->option_nb;i++){
\r
821 if(my_string_case_equal(Uci->option[i].name,"UCI_AnalyseMode")) continue;
\r
822 if(my_string_case_equal(Uci->option[i].name,"Ponder")) continue;
\r
823 if(my_string_case_equal(Uci->option[i].name,"Hash")) continue;
\r
824 if(my_string_case_equal(Uci->option[i].name,"NalimovPath")) continue;
\r
825 if((name=uci_thread_option(Uci))!=NULL && my_string_case_equal(Uci->option[i].name,name)) continue;
\r
826 format_xboard_option_line(option_line,Uci->option+i);
\r
828 gui_send(GUI,"%s",option_line);
\r
832 if(p->mode &XBOARD){
\r
833 format_xboard_option_line(option_line,p);
\r
834 gui_send(GUI,"%s",option_line);
\r
838 gui_send(GUI,"feature done=1");
\r
844 static void comp_move(int move) {
\r
849 ASSERT(move_is_ok(move));
\r
851 ASSERT(State->state==THINK);
\r
852 ASSERT(!XB->analyse);
\r
854 if(option_get_bool("RepeatPV")==true)
\r
855 send_pv(); // to update time and nodes
\r
859 game_get_board(Game,board);
\r
861 if (move_is_castle(move,board) && option_get_bool("Chess960")) {
\r
862 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
\r
864 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
\r
867 gui_send(GUI,"move %s",string);
\r
871 if (option_get_bool("Resign") && Uci->root_move_nb > 1) {
\r
873 if (Uci->best_score <= -abs(option_get_int("ResignScore"))) {
\r
875 State->resign_nb++;
\r
876 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
\r
878 if (State->resign_nb >= option_get_int("ResignMoves")) {
\r
879 my_log("POLYGLOT *** RESIGN ***\n");
\r
880 gui_send(GUI,"resign");
\r
885 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
\r
886 State->resign_nb = 0;
\r
898 static void move_step(int move) {
\r
901 char move_string[256];
\r
903 ASSERT(move_is_ok(move));
\r
907 game_get_board(Game,board);
\r
909 if (move != MoveNone && move_is_legal(move,board)) {
\r
911 move_to_san(move,board,move_string,256);
\r
912 my_log("POLYGLOT MOVE %s\n",move_string);
\r
916 move_to_can(move,board,move_string,256);
\r
917 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
\r
920 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
\r
925 game_add_move(Game,move);
\r
931 static void board_update() {
\r
935 ASSERT(!XB->result);
\r
937 switch (game_status(Game)) {
\r
941 gui_send(GUI,"1-0 {White mates}");
\r
944 gui_send(GUI,"0-1 {Black mates}");
\r
947 gui_send(GUI,"1/2-1/2 {Stalemate}");
\r
949 case DRAW_MATERIAL:
\r
950 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
\r
953 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
\r
955 case DRAW_REPETITION:
\r
956 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
\r
966 static void mess() {
\r
968 // clear state variables
\r
970 State->resign_nb = 0;
\r
971 State->exp_move = MoveNone;
\r
972 my_timer_reset(State->timer);
\r
974 // abort a possible search
\r
978 // calculate the new state
\r
981 } else if (!active()) {
\r
982 State->state = WAIT;
\r
983 my_log("POLYGLOT WAIT\n");
\r
984 } else if (XB->analyse) {
\r
985 State->state = ANALYSE;
\r
986 my_log("POLYGLOT ANALYSE\n");
\r
987 } else if (State->computer[game_turn(Game)]) {
\r
988 State->state = THINK;
\r
989 my_log("POLYGLOT THINK\n");
\r
991 State->state = WAIT;
\r
992 my_log("POLYGLOT WAIT\n");
\r
1000 static void no_mess(int move) {
\r
1002 ASSERT(move_is_ok(move));
\r
1004 // just received a move, calculate the new state
\r
1008 } else if (!active()) {
\r
1010 stop_search(); // abort a possible search
\r
1012 State->state = WAIT;
\r
1013 State->exp_move = MoveNone;
\r
1015 my_log("POLYGLOT WAIT\n");
\r
1017 } else if (State->state == WAIT) {
\r
1019 ASSERT(State->computer[game_turn(Game)]);
\r
1020 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1021 ASSERT(!XB->analyse);
\r
1023 my_log("POLYGLOT WAIT -> THINK\n");
\r
1025 State->state = THINK;
\r
1026 State->exp_move = MoveNone;
\r
1028 } else if (State->state == THINK) {
\r
1030 ASSERT(!State->computer[game_turn(Game)]);
\r
1031 ASSERT(State->computer[colour_opp(game_turn(Game))]);
\r
1032 ASSERT(!XB->analyse);
\r
1034 if (ponder() && ponder_ok(Uci->ponder_move)) {
\r
1036 my_log("POLYGLOT THINK -> PONDER\n");
\r
1038 State->state = PONDER;
\r
1039 State->exp_move = Uci->ponder_move;
\r
1043 my_log("POLYGLOT THINK -> WAIT\n");
\r
1045 State->state = WAIT;
\r
1046 State->exp_move = MoveNone;
\r
1049 } else if (State->state == PONDER) {
\r
1051 ASSERT(State->computer[game_turn(Game)]);
\r
1052 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1053 ASSERT(!XB->analyse);
\r
1055 if (move == State->exp_move && Uci->searching) {
\r
1057 ASSERT(Uci->searching);
\r
1058 ASSERT(Uci->pending_nb>=1);
\r
1060 my_timer_start(State->timer);//also resets
\r
1062 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
\r
1063 engine_send(Engine,"ponderhit");
\r
1065 State->state = THINK;
\r
1066 State->exp_move = MoveNone;
\r
1068 send_pv(); // update display
\r
1070 return; // do not launch a new search
\r
1074 my_log("POLYGLOT PONDER -> THINK (miss)\n");
\r
1078 State->state = THINK;
\r
1079 State->exp_move = MoveNone;
\r
1082 } else if (State->state == ANALYSE) {
\r
1084 ASSERT(XB->analyse);
\r
1086 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
\r
1098 // start_protected_command()
\r
1100 static void start_protected_command(){
\r
1104 static void end_protected_command(){
\r
1105 if(Uci->ready){ // not init faze
\r
1106 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
\r
1108 update_remaining_time();
\r
1109 search_update(); // relaunch search if necessary
\r
1112 // update_remaining_time()
\r
1114 static void update_remaining_time(){
\r
1116 if(State->timer->running){
\r
1117 my_timer_stop(State->timer);
\r
1118 reduce = my_timer_elapsed_real(State->timer);
\r
1119 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
\r
1120 XB->my_time -= reduce;
\r
1121 if(XB->my_time<0.0){
\r
1128 // search_update()
\r
1130 static void search_update() {
\r
1135 int nalimov_cache;
\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(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,Uci->best_score,Uci->time*100.0,Uci->node_nb);
\r
1440 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,Uci->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,Uci->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 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(Uci->best_score)/100.0,pv_string);
\r
1465 } else if (State->state == PONDER) {
\r
1467 game_get_board(Game,board);
\r
1468 move = State->exp_move;
\r
1470 if (move != MoveNone && move_is_legal(move,board)) {
\r
1471 move_to_san(move,board,move_string,256);
\r
1472 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1473 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(Uci->best_score)/100.0,move_string,pv_string);
\r
1481 static void learn(int result) {
\r
1487 ASSERT(result>=-1&&result<=+1);
\r
1489 ASSERT(XB->result);
\r
1490 ASSERT(State->computer[White]||State->computer[Black]);
\r
1497 } else if (State->computer[White]) {
\r
1499 } else if (State->computer[Black]) {
\r
1503 my_fatal("learn(): unknown side\n");
\r
1507 } else if (result > 0) {
\r
1508 my_log("POLYGLOT *LEARN WIN*\n");
\r
1509 } else if (result < 0) {
\r
1510 my_log("POLYGLOT *LEARN LOSS*\n");
\r
1512 my_log("POLYGLOT *LEARN DRAW*\n");
\r
1517 for (; pos < Game->size; pos += 2) {
\r
1519 game_get_board(Game,board,pos);
\r
1520 move = game_move(Game,pos);
\r
1522 book_learn_move(board,move,result);
\r
1528 // end of adapter.cpp
\r