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
115 // xboard2uci_init()
\r
117 void xboard2uci_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 // xboard2uci_gui_step()
\r
166 void xboard2uci_gui_step(char string[]) {
\r
169 char move_string[256];
\r
174 } else if (match(string,"accepted *")) {
\r
178 } else if (match(string,"analyze")) {
\r
180 State->computer[White] = false;
\r
181 State->computer[Black] = false;
\r
183 XB->analyse = true;
\r
184 XB->new_hack = false;
\r
185 ASSERT(!XB->result);
\r
186 XB->result = false;
\r
190 } else if (match(string,"bk")) {
\r
192 if (option_get_bool("Book")) {
\r
193 game_get_board(Game,board);
\r
197 } else if (match(string,"black")) {
\r
199 if (colour_is_black(game_turn(Game))) {
\r
201 State->computer[White] = true;
\r
202 State->computer[Black] = false;
\r
204 XB->new_hack = true;
\r
205 XB->result = false;
\r
210 } else if (match(string,"computer")) {
\r
212 XB->computer = true;
\r
214 } else if (match(string,"draw")) {
\r
215 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
216 my_log("POLYGLOT draw from XB received");
\r
217 uci_send_option(Uci,"DrawOffer","%s","draw");}
\r
218 } else if (match(string,"easy")) {
\r
220 XB->ponder = false;
\r
224 } else if (match(string,"edit")) {
\r
228 gui_send(GUI,"Error (unknown command): %s",string);
\r
230 } else if (match(string,"exit")) {
\r
232 State->computer[White] = false;
\r
233 State->computer[Black] = false;
\r
235 XB->analyse = false;
\r
239 } else if (match(string,"force")) {
\r
241 State->computer[White] = false;
\r
242 State->computer[Black] = false;
\r
246 } else if (match(string,"go")) {
\r
248 State->computer[game_turn(Game)] = true;
\r
249 State->computer[colour_opp(game_turn(Game))] = false;
\r
251 XB->new_hack = false;
\r
252 ASSERT(!XB->result);
\r
253 XB->result = false;
\r
257 } else if (match(string,"hard")) {
\r
263 } else if (match(string,"hint")) {
\r
265 if (option_get_bool("Book")) {
\r
267 game_get_board(Game,board);
\r
268 move = book_move(board,false);
\r
270 if (move != MoveNone && move_is_legal(move,board)) {
\r
271 move_to_san(move,board,move_string,256);
\r
272 gui_send(GUI,"Hint: %s",move_string);
\r
276 } else if (match(string,"ics *")) {
\r
280 } else if (match(string,"level * *:* *")) {
\r
282 XB->mps = atoi(Star[0]);
\r
283 XB->base = double(atoi(Star[1])) * 60.0 + double(atoi(Star[2]));
\r
284 XB->inc = double(atoi(Star[3]));
\r
286 } else if (match(string,"level * * *")) {
\r
288 XB->mps = atoi(Star[0]);
\r
289 XB->base = double(atoi(Star[1])) * 60.0;
\r
290 XB->inc = double(atoi(Star[2]));
\r
292 } else if (match(string,"name *")) {
\r
294 my_string_set(&XB->name,Star[0]);
\r
296 } else if (match(string,"new")) {
\r
298 uci_send_isready(Uci);
\r
299 my_log("POLYGLOT NEW GAME\n");
\r
301 option_set("Chess960","false");
\r
306 State->computer[White] = false;
\r
307 State->computer[Black] = false;
\r
309 State->computer[White] = false;
\r
310 State->computer[Black] = true;
\r
313 XB->new_hack = true;
\r
314 XB->result = false;
\r
316 XB->depth_limit = false;
\r
318 XB->computer = false;
\r
319 my_string_set(&XB->name,"<empty>");
\r
324 uci_send_ucinewgame(Uci);
\r
326 } else if (match(string,"nopost")) {
\r
330 } else if (match(string,"otim *")) {
\r
332 XB->opp_time = double(atoi(Star[0])) / 100.0;
\r
333 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
\r
335 } else if (match(string,"pause")) {
\r
339 gui_send(GUI,"Error (unknown command): %s",string);
\r
341 } else if (match(string,"ping *")) {
\r
343 // HACK; TODO: answer only after an engine move
\r
346 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
\r
347 XB->ping = atoi(Star[0]);
\r
348 uci_send_isready(Uci);
\r
350 ASSERT(XB->ping==-1);
\r
351 gui_send(GUI,"pong %s",Star[0]);
\r
354 } else if (match(string,"playother")) {
\r
356 State->computer[game_turn(Game)] = false;
\r
357 State->computer[colour_opp(game_turn(Game))] = true;
\r
359 XB->new_hack = false;
\r
360 ASSERT(!XB->result);
\r
361 XB->result = false;
\r
365 } else if (match(string,"post")) {
\r
369 } else if (match(string,"protover *")) {
\r
371 send_xboard_options();
\r
373 } else if (match(string,"quit")) {
\r
374 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
\r
376 } else if (match(string,"random")) {
\r
380 } else if (match(string,"rating * *")) {
\r
384 } else if (match(string,"remove")) {
\r
386 if (game_pos(Game) >= 2) {
\r
388 game_goto(Game,game_pos(Game)-2);
\r
390 ASSERT(!XB->new_hack);
\r
391 XB->new_hack = false; // HACK?
\r
392 XB->result = false;
\r
398 } else if (match(string,"rejected *")) {
\r
402 } else if (match(string,"reset")) { // protover 3?
\r
406 gui_send(GUI,"Error (unknown command): %s",string);
\r
409 || match(string,"result * {*}")
\r
410 || match(string,"result * {* }")
\r
411 || match(string,"result * { *}")
\r
412 || match(string,"result * { * }")) {
\r
414 my_log("POLYGLOT GAME END\n");
\r
422 if (option_get_bool("Book") && option_get_bool("BookLearn")) {
\r
425 } else if (my_string_equal(Star[0],"1-0")) {
\r
427 } else if (my_string_equal(Star[0],"0-1")) {
\r
429 } else if (my_string_equal(Star[0],"1/2-1/2")) {
\r
433 } else if (match(string,"resume")) {
\r
437 gui_send(GUI,"Error (unknown command): %s",string);
\r
439 } else if (match(string,"option *=*")){
\r
440 char *name=Star[0];
\r
441 char *value=Star[1];
\r
442 if(match(name, "Polyglot *")){
\r
443 char *pg_name=Star[0];
\r
444 polyglot_set_option(pg_name,value);
\r
446 start_protected_command();
\r
447 engine_send(Engine,"setoption name %s value %s",name,value);
\r
448 end_protected_command();
\r
450 } else if (match(string,"option *")){
\r
451 char *name=Star[0];
\r
452 start_protected_command();
\r
453 engine_send(Engine,"setoption name %s",name);
\r
454 end_protected_command();
\r
455 } else if (XB->has_feature_smp && match(string,"cores *")){
\r
456 int cores=atoi(Star[0]);
\r
458 // updating the number of cores
\r
459 my_log("POLYGLOT setting the number of cores to %d\n",cores);
\r
460 start_protected_command();
\r
461 uci_set_threads(Uci,cores);
\r
462 end_protected_command();
\r
465 gui_send(GUI,"Error (unknown command): %s",string);
\r
467 } else if (XB->has_feature_egt && match(string,"egtpath * *")){
\r
468 char *type=Star[0];
\r
469 char *path=Star[1];
\r
470 if(!my_string_case_equal(type,"nalimov")){
\r
472 gui_send(GUI,"Error (unsupported table base format): %s",string);
\r
473 }else if(my_string_empty(path)){
\r
475 gui_send(GUI,"Error (unknown command): %s",string);
\r
477 // updating NalimovPath
\r
478 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
\r
479 start_protected_command();
\r
480 uci_send_option(Uci,"NalimovPath","%s",path);
\r
481 end_protected_command();
\r
483 } else if (XB->has_feature_memory && match(string,"memory *")){
\r
484 int memory = atoi(Star[0]);
\r
488 // updating the available memory
\r
489 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
\r
490 if(uci_get_option(Uci,"NalimovCache")>=0){
\r
491 nalimov_cache=atoi(Uci->option[uci_get_option(Uci,"NalimovCache")].value);
\r
495 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);
\r
496 real_memory=memory-nalimov_cache;
\r
498 start_protected_command();
\r
499 uci_send_option(Uci,"Hash", "%d", real_memory);
\r
500 end_protected_command();
\r
504 gui_send(GUI,"Error (unknown command): %s",string);
\r
507 } else if (match(string,"sd *")) {
\r
509 XB->depth_limit = true;
\r
510 XB->depth_max = atoi(Star[0]);
\r
512 } else if (match(string,"setboard *")) {
\r
514 my_log("POLYGLOT FEN %s\n",Star[0]);
\r
516 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
\r
518 State->computer[White] = false;
\r
519 State->computer[Black] = false;
\r
521 XB->new_hack = true; // HACK?
\r
522 XB->result = false;
\r
527 } else if (match(string,"st *")) {
\r
529 XB->time_limit = true;
\r
530 XB->time_max = double(atoi(Star[0]));
\r
532 } else if (match(string,"time *")) {
\r
534 XB->my_time = double(atoi(Star[0])) / 100.0;
\r
535 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
537 } else if (match(string,"undo")) {
\r
539 if (game_pos(Game) >= 1) {
\r
541 game_goto(Game,game_pos(Game)-1);
\r
543 ASSERT(!XB->new_hack);
\r
544 XB->new_hack = false; // HACK?
\r
545 XB->result = false;
\r
551 } else if (match(string,"usermove *")) {
\r
553 game_get_board(Game,board);
\r
554 move = move_from_san(Star[0],board);
\r
556 if (move != MoveNone && move_is_legal(move,board)) {
\r
558 XB->new_hack = false;
\r
559 ASSERT(!XB->result);
\r
560 XB->result = false;
\r
567 gui_send(GUI,"Illegal move: %s",Star[0]);
\r
570 } else if (match(string,"variant *")) {
\r
572 if (my_string_equal(Star[0],"fischerandom")) {
\r
573 option_set("Chess960","true");
\r
575 option_set("Chess960","false");
\r
578 } else if (match(string,"white")) {
\r
580 if (colour_is_white(game_turn(Game))) {
\r
582 State->computer[White] = false;
\r
583 State->computer[Black] = true;
\r
585 XB->new_hack = true;
\r
586 XB->result = false;
\r
591 } else if (match(string,"xboard")) {
\r
595 } else if (match(string,".")) { // analyse info
\r
597 if (State->state == ANALYSE) {
\r
598 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
\r
600 ASSERT(Uci->searching);
\r
601 ASSERT(Uci->pending_nb>=1);
\r
603 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
\r
604 move_to_san(Uci->root_move,Uci->board,move_string,256);
\r
605 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
607 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
\r
611 } else if (match(string,"?")) { // move now
\r
613 if (State->state == THINK) {
\r
615 ASSERT(Uci->searching);
\r
616 ASSERT(Uci->pending_nb>=1);
\r
618 // HACK: just send "stop" to the engine
\r
620 if (Uci->searching) {
\r
621 my_log("POLYGLOT STOP SEARCH\n");
\r
622 engine_send(Engine,"stop");
\r
626 } else { // unknown command, maybe a move?
\r
628 game_get_board(Game,board);
\r
629 move = move_from_san(string,board);
\r
631 if (move != MoveNone && move_is_legal(move,board)) {
\r
633 XB->new_hack = false;
\r
634 ASSERT(!XB->result);
\r
635 XB->result = false;
\r
640 } else if (move != MoveNone) {
\r
642 gui_send(GUI,"Illegal move: %s",string);
\r
646 gui_send(GUI,"Error (unknown command): %s",string);
\r
652 // xboard2uci_engine_step()
\r
654 void xboard2uci_engine_step(char string[]) {
\r
657 event = uci_parse(Uci,string);
\r
661 if ((event & EVENT_READY) != 0) {
\r
663 // the engine is now ready
\r
667 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
\r
670 if (!DelayPong && XB->ping >= 0) {
\r
671 gui_send(GUI,"pong %d",XB->ping);
\r
676 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
\r
678 // the engine is playing a move
\r
680 // MEGA HACK: estimate remaining time because XBoard won't send it!
\r
682 my_timer_stop(State->timer);
\r
684 XB->my_time -= my_timer_elapsed_real(State->timer);
\r
685 XB->my_time += XB->inc;
\r
686 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
\r
688 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
690 // play the engine move
\r
692 comp_move(Uci->best_move);
\r
695 if ((event & EVENT_PV) != 0) {
\r
697 // the engine has sent a new PV
\r
701 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
\r
702 my_log("POYGLOT draw offer/resign from engine\n");
\r
703 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
704 if(event & EVENT_DRAW)
\r
705 gui_send(GUI,"offer draw");
\r
707 gui_send(GUI,"resign");
\r
712 // format_xboard_option_line
\r
714 void format_xboard_option_line(char * option_line, option_t *opt){
\r
716 char option_string[StringSize];
\r
717 strcpy(option_line,"");
\r
718 strcat(option_line,"feature option=\"");
\r
720 strcat(option_line,"Polyglot ");
\r
722 sprintf(option_string,"%s",opt->name);
\r
723 strcat(option_line,option_string);
\r
724 sprintf(option_string," -%s",opt->type);
\r
725 strcat(option_line,option_string);
\r
726 if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){
\r
727 if(strcmp(opt->type,"check")){
\r
728 sprintf(option_string," %s",opt->default_);
\r
730 sprintf(option_string," %d",
\r
731 strcmp(opt->default_,"true")?0:1);
\r
733 strcat(option_line,option_string);
\r
735 if(!strcmp(opt->type,"spin")){
\r
736 sprintf(option_string," %s",opt->min);
\r
737 strcat(option_line,option_string);
\r
739 if(!strcmp(opt->type,"spin")){
\r
740 sprintf(option_string," %s",opt->max);
\r
741 strcat(option_line,option_string);
\r
743 for(j=0;j<opt->var_nb;j++){
\r
744 if(!strcmp(opt->var[j],opt->default_)){
\r
745 sprintf(option_string," *%s",opt->var[j]);
\r
747 sprintf(option_string," %s",opt->var[j]);
\r
749 strcat(option_line,option_string);
\r
750 if(j!=opt->var_nb-1){
\r
751 strcat(option_line," ///");
\r
754 strcat(option_line,"\"");
\r
757 // send_xboard_options
\r
759 static void send_xboard_options(){
\r
761 char option_line[StringSize]="";
\r
762 option_t *p=Option;
\r
764 XB->proto_ver = atoi(Star[0]);
\r
765 ASSERT(XB->proto_ver>=2);
\r
767 gui_send(GUI,"feature done=0");
\r
769 gui_send(GUI,"feature analyze=1");
\r
770 gui_send(GUI,"feature colors=0");
\r
771 gui_send(GUI,"feature draw=1");
\r
772 gui_send(GUI,"feature ics=1");
\r
773 gui_send(GUI,"feature myname=\"%s\"",option_get_string("EngineName"));
\r
774 gui_send(GUI,"feature name=1");
\r
775 gui_send(GUI,"feature pause=0");
\r
776 gui_send(GUI,"feature ping=1");
\r
777 gui_send(GUI,"feature playother=1");
\r
778 gui_send(GUI,"feature reuse=1");
\r
779 gui_send(GUI,"feature san=0");
\r
780 gui_send(GUI,"feature setboard=1");
\r
781 gui_send(GUI,"feature sigint=0");
\r
782 gui_send(GUI,"feature sigterm=0");
\r
783 gui_send(GUI,"feature time=1");
\r
784 gui_send(GUI,"feature usermove=1");
\r
785 if (XB->has_feature_memory){
\r
786 gui_send(GUI,"feature memory=1");
\r
788 gui_send(GUI,"feature memory=0");
\r
790 if (XB->has_feature_smp){
\r
791 gui_send(GUI,"feature smp=1");
\r
793 gui_send(GUI,"feature smp=0");
\r
795 if (XB->has_feature_egt){
\r
796 // TODO: support for other types of table bases
\r
797 gui_send(GUI,"feature egt=\"nalimov\"");
\r
799 gui_send(GUI,"feature egt=\"\"");
\r
802 if (uci_option_exist(Uci,"UCI_Chess960")) {
\r
803 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
\r
805 gui_send(GUI,"feature variants=\"normal\"");
\r
808 for(i=0;i<Uci->option_nb;i++){
\r
809 if(my_string_case_equal(Uci->option[i].name,"UCI_AnalyseMode")) continue;
\r
810 if(my_string_case_equal(Uci->option[i].name,"Ponder")) continue;
\r
811 if(my_string_case_equal(Uci->option[i].name,"Hash")) continue;
\r
812 if(my_string_case_equal(Uci->option[i].name,"NalimovPath")) continue;
\r
813 if((name=uci_thread_option(Uci))!=NULL && my_string_case_equal(Uci->option[i].name,name)) continue;
\r
814 format_xboard_option_line(option_line,Uci->option+i);
\r
816 gui_send(GUI,"%s",option_line);
\r
820 if(p->mode &XBOARD){
\r
821 format_xboard_option_line(option_line,p);
\r
822 gui_send(GUI,"%s",option_line);
\r
826 gui_send(GUI,"feature done=1");
\r
832 static void comp_move(int move) {
\r
837 ASSERT(move_is_ok(move));
\r
839 ASSERT(State->state==THINK);
\r
840 ASSERT(!XB->analyse);
\r
842 if(option_get_bool("RepeatPV")==true)
\r
843 send_pv(); // to update time and nodes
\r
847 game_get_board(Game,board);
\r
849 if (move_is_castle(move,board) && option_get_bool("Chess960")) {
\r
850 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
\r
852 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
\r
855 gui_send(GUI,"move %s",string);
\r
859 if (option_get_bool("Resign") && Uci->root_move_nb > 1) {
\r
861 if (Uci->best_score <= -abs(option_get_int("ResignScore"))) {
\r
863 State->resign_nb++;
\r
864 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
\r
866 if (State->resign_nb >= option_get_int("ResignMoves")) {
\r
867 my_log("POLYGLOT *** RESIGN ***\n");
\r
868 gui_send(GUI,"resign");
\r
873 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
\r
874 State->resign_nb = 0;
\r
886 static void move_step(int move) {
\r
889 char move_string[256];
\r
891 ASSERT(move_is_ok(move));
\r
895 game_get_board(Game,board);
\r
897 if (move != MoveNone && move_is_legal(move,board)) {
\r
899 move_to_san(move,board,move_string,256);
\r
900 my_log("POLYGLOT MOVE %s\n",move_string);
\r
904 move_to_can(move,board,move_string,256);
\r
905 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
\r
908 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
\r
913 game_add_move(Game,move);
\r
919 static void board_update() {
\r
923 ASSERT(!XB->result);
\r
925 switch (game_status(Game)) {
\r
929 gui_send(GUI,"1-0 {White mates}");
\r
932 gui_send(GUI,"0-1 {Black mates}");
\r
935 gui_send(GUI,"1/2-1/2 {Stalemate}");
\r
937 case DRAW_MATERIAL:
\r
938 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
\r
941 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
\r
943 case DRAW_REPETITION:
\r
944 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
\r
954 static void mess() {
\r
956 // clear state variables
\r
958 State->resign_nb = 0;
\r
959 State->exp_move = MoveNone;
\r
960 my_timer_reset(State->timer);
\r
962 // abort a possible search
\r
966 // calculate the new state
\r
969 } else if (!active()) {
\r
970 State->state = WAIT;
\r
971 my_log("POLYGLOT WAIT\n");
\r
972 } else if (XB->analyse) {
\r
973 State->state = ANALYSE;
\r
974 my_log("POLYGLOT ANALYSE\n");
\r
975 } else if (State->computer[game_turn(Game)]) {
\r
976 State->state = THINK;
\r
977 my_log("POLYGLOT THINK\n");
\r
979 State->state = WAIT;
\r
980 my_log("POLYGLOT WAIT\n");
\r
988 static void no_mess(int move) {
\r
990 ASSERT(move_is_ok(move));
\r
992 // just received a move, calculate the new state
\r
996 } else if (!active()) {
\r
998 stop_search(); // abort a possible search
\r
1000 State->state = WAIT;
\r
1001 State->exp_move = MoveNone;
\r
1003 my_log("POLYGLOT WAIT\n");
\r
1005 } else if (State->state == WAIT) {
\r
1007 ASSERT(State->computer[game_turn(Game)]);
\r
1008 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1009 ASSERT(!XB->analyse);
\r
1011 my_log("POLYGLOT WAIT -> THINK\n");
\r
1013 State->state = THINK;
\r
1014 State->exp_move = MoveNone;
\r
1016 } else if (State->state == THINK) {
\r
1018 ASSERT(!State->computer[game_turn(Game)]);
\r
1019 ASSERT(State->computer[colour_opp(game_turn(Game))]);
\r
1020 ASSERT(!XB->analyse);
\r
1022 if (ponder() && ponder_ok(Uci->ponder_move)) {
\r
1024 my_log("POLYGLOT THINK -> PONDER\n");
\r
1026 State->state = PONDER;
\r
1027 State->exp_move = Uci->ponder_move;
\r
1031 my_log("POLYGLOT THINK -> WAIT\n");
\r
1033 State->state = WAIT;
\r
1034 State->exp_move = MoveNone;
\r
1037 } else if (State->state == PONDER) {
\r
1039 ASSERT(State->computer[game_turn(Game)]);
\r
1040 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1041 ASSERT(!XB->analyse);
\r
1043 if (move == State->exp_move && Uci->searching) {
\r
1045 ASSERT(Uci->searching);
\r
1046 ASSERT(Uci->pending_nb>=1);
\r
1048 my_timer_start(State->timer);//also resets
\r
1050 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
\r
1051 engine_send(Engine,"ponderhit");
\r
1053 State->state = THINK;
\r
1054 State->exp_move = MoveNone;
\r
1056 send_pv(); // update display
\r
1058 return; // do not launch a new search
\r
1062 my_log("POLYGLOT PONDER -> THINK (miss)\n");
\r
1066 State->state = THINK;
\r
1067 State->exp_move = MoveNone;
\r
1070 } else if (State->state == ANALYSE) {
\r
1072 ASSERT(XB->analyse);
\r
1074 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
\r
1086 // start_protected_command()
\r
1088 static void start_protected_command(){
\r
1092 static void end_protected_command(){
\r
1093 if(Uci->ready){ // not init faze
\r
1094 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
\r
1096 update_remaining_time();
\r
1097 search_update(); // relaunch search if necessary
\r
1100 // update_remaining_time()
\r
1102 static void update_remaining_time(){
\r
1104 if(State->timer->running){
\r
1105 my_timer_stop(State->timer);
\r
1106 reduce = my_timer_elapsed_real(State->timer);
\r
1107 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
\r
1108 XB->my_time -= reduce;
\r
1109 if(XB->my_time<0.0){
\r
1116 // search_update()
\r
1118 static void search_update() {
\r
1124 ASSERT(!Uci->searching);
\r
1129 // launch a new search if needed
\r
1133 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
\r
1137 if (State->state == THINK && option_get_bool("Book")) {
\r
1139 game_get_board(Game,Uci->board);
\r
1141 move = book_move(Uci->board,option_get_bool("BookRandom"));
\r
1143 if (move != MoveNone && move_is_legal(move,Uci->board)) {
\r
1145 my_log("POLYGLOT *BOOK MOVE*\n");
\r
1147 search_clear(); // clears Uci->ponder_move
\r
1148 Uci->best_move = move;
\r
1150 board_copy(board,Uci->board);
\r
1151 move_do(board,move);
\r
1152 Uci->ponder_move = book_move(board,false); // expected move = best book move
\r
1154 Uci->best_pv[0] = Uci->best_move;
\r
1155 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
\r
1156 Uci->best_pv[2] = MoveNone;
\r
1158 comp_move(Uci->best_move);
\r
1166 my_log("POLYGLOT START SEARCH\n");
\r
1170 uci_send_option(Uci,"UCI_Chess960","%s",option_get_bool("Chess960")?"true":"false");
\r
1172 if (option_get_int("UCIVersion") >= 2) {
\r
1173 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
\r
1174 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
\r
1177 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
\r
1181 move = (State->state == PONDER) ? State->exp_move : MoveNone;
\r
1182 send_board(move); // updates Uci->board global variable
\r
1186 if (State->state == THINK || State->state == PONDER) {
\r
1188 engine_send_queue(Engine,"go");
\r
1190 if (XB->time_limit) {
\r
1192 // fixed time per move
\r
1194 engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);
\r
1200 if (colour_is_white(Uci->board->turn)) {
\r
1201 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);
\r
1203 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);
\r
1206 if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);
\r
1208 if (XB->mps != 0) {
\r
1210 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
\r
1211 ASSERT(move_nb>=1&&move_nb<=XB->mps);
\r
1213 engine_send_queue(Engine," movestogo %d",move_nb);
\r
1217 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
\r
1219 if (State->state == PONDER) engine_send_queue(Engine," ponder");
\r
1221 engine_send(Engine,""); // newline
\r
1223 } else if (State->state == ANALYSE) {
\r
1225 engine_send(Engine,"go infinite");
\r
1232 // init search info
\r
1234 ASSERT(!Uci->searching);
\r
1238 Uci->searching = true;
\r
1239 Uci->pending_nb++;
\r
1245 static void search_clear() {
\r
1251 my_timer_start(State->timer);//also resets
\r
1256 static bool active() {
\r
1260 if (game_status(Game) != PLAYING) return false; // game ended
\r
1264 if (XB->analyse) return true; // analysing
\r
1265 if (!State->computer[White] && !State->computer[Black]) return false; // force mode
\r
1266 if (XB->new_hack || XB->result) return false; // unstarted or ended game
\r
1268 return true; // playing
\r
1273 static bool ponder() {
\r
1275 return XB->ponder && (option_get_bool("CanPonder") || uci_option_exist(Uci,"Ponder"));
\r
1279 static bool ponder_ok(int move) {
\r
1283 ASSERT(move==MoveNone||move_is_ok(move));
\r
1285 // legal ponder move?
\r
1287 if (move == MoveNone) return false;
\r
1289 game_get_board(Game,board);
\r
1290 if (!move_is_legal(move,board)) return false;
\r
1292 // UCI-legal resulting position?
\r
1294 game_add_move(Game,move);
\r
1296 game_get_board(Game,board);
\r
1297 status = game_status(Game);
\r
1299 game_rem_move(Game);
\r
1301 if (status != PLAYING) return false; // game ended
\r
1303 if (option_get_bool("Book") && is_in_book(board)) {
\r
1312 static void stop_search() {
\r
1314 if (Uci->searching) {
\r
1316 ASSERT(Uci->searching);
\r
1317 ASSERT(Uci->pending_nb>=1);
\r
1319 my_log("POLYGLOT STOP SEARCH\n");
\r
1322 engine_send(Engine,"stop");
\r
1323 Uci->searching = false;
\r
1326 if (option_get_bool("SyncStop")) {
\r
1327 uci_send_stop_sync(Uci);
\r
1329 uci_send_stop(Uci);
\r
1336 static void send_board(int extra_move) {
\r
1345 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
\r
1347 ASSERT(!Uci->searching);
\r
1351 game_get_board(Game,Uci->board);
\r
1352 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
\r
1354 board_to_fen(Uci->board,fen,256);
\r
1355 my_log("POLYGLOT FEN %s\n",fen);
\r
1357 ASSERT(board_can_play(Uci->board));
\r
1362 end = game_pos(Game);
\r
1363 ASSERT(end>=start);
\r
1367 game_get_board(Game,board,start);
\r
1368 board_to_fen(board,string,256);
\r
1370 engine_send_queue(Engine,"position");
\r
1372 if (my_string_equal(string,StartFen)) {
\r
1373 engine_send_queue(Engine," startpos");
\r
1375 engine_send_queue(Engine," fen %s",string);
\r
1380 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
\r
1382 for (pos = start; pos < end; pos++) { // game moves
\r
1384 move = game_move(Game,pos);
\r
1386 move_to_can(move,board,string,256);
\r
1387 engine_send_queue(Engine," %s",string);
\r
1389 move_do(board,move);
\r
1392 if (extra_move != MoveNone) { // move to ponder on
\r
1393 move_to_can(extra_move,board,string,256);
\r
1394 engine_send_queue(Engine," %s",string);
\r
1399 engine_send(Engine,""); // newline
\r
1404 static void send_pv() {
\r
1406 char pv_string[StringSize];
\r
1409 char move_string[StringSize];
\r
1411 ASSERT(State->state!=WAIT);
\r
1413 if (Uci->best_depth == 0) return;
\r
1415 // xboard search information
\r
1419 if (State->state == THINK || State->state == ANALYSE) {
\r
1421 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1423 if(Uci->depth==-1) //hack to clear the engine output window
\r
1424 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,Uci->best_score,Uci->time*100.0,Uci->node_nb);
\r
1426 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
1428 } else if (State->state == PONDER && option_get_bool("ShowPonder")) {
\r
1430 game_get_board(Game,board);
\r
1431 move = State->exp_move;
\r
1433 if (move != MoveNone && move_is_legal(move,board)) {
\r
1434 move_to_san(move,board,move_string,256);
\r
1435 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1436 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
1443 if ((Uci->searching && option_get_bool("KibitzPV") && Uci->time >= option_get_double("KibitzDelay"))
\r
1444 || (!Uci->searching && option_get_bool("KibitzMove"))) {
\r
1446 if (State->state == THINK || State->state == ANALYSE) {
\r
1448 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1449 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
1451 } else if (State->state == PONDER) {
\r
1453 game_get_board(Game,board);
\r
1454 move = State->exp_move;
\r
1456 if (move != MoveNone && move_is_legal(move,board)) {
\r
1457 move_to_san(move,board,move_string,256);
\r
1458 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1459 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
1467 static void learn(int result) {
\r
1473 ASSERT(result>=-1&&result<=+1);
\r
1475 ASSERT(XB->result);
\r
1476 ASSERT(State->computer[White]||State->computer[Black]);
\r
1483 } else if (State->computer[White]) {
\r
1485 } else if (State->computer[Black]) {
\r
1489 my_fatal("learn(): unknown side\n");
\r
1493 } else if (result > 0) {
\r
1494 my_log("POLYGLOT *LEARN WIN*\n");
\r
1495 } else if (result < 0) {
\r
1496 my_log("POLYGLOT *LEARN LOSS*\n");
\r
1498 my_log("POLYGLOT *LEARN DRAW*\n");
\r
1503 for (; pos < Game->size; pos += 2) {
\r
1505 game_get_board(Game,board,pos);
\r
1506 move = game_move(Game,pos);
\r
1508 book_learn_move(board,move,result);
\r
1514 // end of adapter.cpp
\r