12 #include <sys/select.h>
\r
13 #include <sys/types.h> // Mac OS X needs this one
\r
17 #include "adapter.h"
\r
28 #include "move_do.h"
\r
29 #include "move_legal.h"
\r
40 static const bool UseDebug = false;
\r
41 static const bool DelayPong = false;
\r
43 static const int StringSize = 4096;
\r
49 bool computer[ColourNb];
\r
52 my_timer_t timer[1];
\r
56 bool has_feature_memory;
\r
57 bool has_feature_smp;
\r
58 bool has_feature_egt;
\r
63 bool new_hack; // "new" is a C++ keyword
\r
84 enum dummy_state_t { WAIT, THINK, PONDER, ANALYSE };
\r
88 static state_t State[1];
\r
94 static void xboard_step (char string[]);
\r
95 static void engine_step (char string[]);
\r
97 static void comp_move (int move);
\r
98 static void move_step (int move);
\r
99 static void board_update ();
\r
101 static void mess ();
\r
102 static void no_mess (int move);
\r
104 static void search_update ();
\r
105 static void search_clear ();
\r
106 static void update_remaining_time();
\r
107 static void start_protected_command();
\r
108 static void end_protected_command();
\r
110 static bool active ();
\r
111 static bool ponder ();
\r
112 static bool ponder_ok (int ponder_move);
\r
114 static void stop_search ();
\r
116 static void send_board (int extra_move);
\r
117 static void send_pv ();
\r
119 static void send_xboard_options ();
\r
121 static void learn (int result);
\r
123 static void adapter_step ();
\r
129 void adapter_loop() {
\r
137 State->state = WAIT;
\r
139 State->computer[White] = false;
\r
140 State->computer[Black] = true;
\r
142 State->exp_move = MoveNone;
\r
143 State->resign_nb = 0;
\r
144 my_timer_reset(State->timer);
\r
146 // yes there are engines that do not have the "Hash" option....
\r
147 XB->has_feature_memory= uci_option_exist(Uci,"Hash");
\r
148 XB->has_feature_smp = uci_thread_option_exist(Uci);
\r
149 // TODO: support for other types of table bases
\r
150 XB->has_feature_egt = uci_option_exist(Uci,"NalimovPath");
\r
151 XB->analyse = false;
\r
152 XB->computer = false;
\r
154 my_string_set(&XB->name,"<empty>");
\r
156 XB->new_hack = true;
\r
158 XB->ponder = false;
\r
161 XB->result = false;
\r
167 XB->time_limit = false;
\r
168 XB->time_max = 5.0;
\r
170 XB->depth_limit = false;
\r
171 XB->depth_max = 127;
\r
173 XB->my_time = 300.0;
\r
174 XB->opp_time = 300.0;
\r
175 while (true) adapter_step();
\r
181 static void adapter_step(){ // polling!
\r
183 char string[StringSize];
\r
184 if(option_get_bool("UCI")){
\r
185 xin=gui_get_non_blocking(GUI,string,StringSize);
\r
186 if(xin) uci_gui_step(string);
\r
187 ein=engine_get_non_blocking(Engine,string,StringSize);
\r
188 if(ein) uci_engine_step(string);
\r
190 xin=gui_get_non_blocking(GUI,string,StringSize);
\r
191 if(xin) xboard_step(string);
\r
192 ein=engine_get_non_blocking(Engine,string,StringSize);
\r
193 if(ein) engine_step(string);
\r
195 if(xin==false && ein==false) Idle();//nobody wants me,lets have a beauty nap
\r
198 static void adapter_step() {
\r
203 char string[StringSize];
\r
205 // process buffered lines
\r
207 while (io_line_ready(GUI->io)){
\r
208 gui_get(GUI,string,StringSize);
\r
209 if(option_get_bool("UCI")){
\r
210 uci_gui_step(string);
\r
212 xboard_step(string); // process available xboard lines
\r
215 while (io_line_ready(Engine->io)){
\r
216 engine_get(Engine,string,StringSize);
\r
217 if(option_get_bool("UCI")){
\r
218 uci_engine_step(string); // process available engine lines
\r
220 engine_step(string);
\r
227 fd_max = -1; // HACK
\r
229 // add xboard input
\r
231 ASSERT(GUI->io->in_fd>=0);
\r
233 FD_SET(GUI->io->in_fd,set);
\r
234 if (GUI->io->in_fd > fd_max) fd_max = GUI->io->in_fd;
\r
236 // add engine input
\r
238 ASSERT(Engine->io->in_fd>=0);
\r
240 FD_SET(Engine->io->in_fd,set);
\r
241 if (Engine->io->in_fd > fd_max) fd_max = Engine->io->in_fd;
\r
243 // wait for something to read (no timeout)
\r
247 val = select(fd_max+1,set,NULL,NULL,NULL);
\r
248 if (val == -1 && errno != EINTR) my_fatal("adapter_step(): select(): %s\n",strerror(errno));
\r
251 if (FD_ISSET(GUI->io->in_fd,set)) io_get_update(GUI->io); // read some xboard input
\r
252 if (FD_ISSET(Engine->io->in_fd,set)) io_get_update(Engine->io); // read some engine input
\r
257 static void xboard_step(char string[]) {
\r
260 char move_string[256];
\r
262 static bool firsttime=true;
\r
265 if((match(string,"uci"))){
\r
266 my_log("POLYGLOT *** Switching to UCI mode ***\n");
\r
267 send_uci_options();
\r
268 option_set("UCI","true");
\r
271 //uci_send_isready(Uci); // In UCI mode this done by the GUI
\r
272 //Grrr...Toga can fixes the number of threads after "isready"
\r
273 //So we delay "isready"
\r
279 } else if (match(string,"accepted *")) {
\r
283 } else if (match(string,"analyze")) {
\r
285 State->computer[White] = false;
\r
286 State->computer[Black] = false;
\r
288 XB->analyse = true;
\r
289 XB->new_hack = false;
\r
290 ASSERT(!XB->result);
\r
291 XB->result = false;
\r
295 } else if (match(string,"bk")) {
\r
297 if (option_get_bool("Book")) {
\r
298 game_get_board(Game,board);
\r
302 } else if (match(string,"black")) {
\r
304 if (colour_is_black(game_turn(Game))) {
\r
306 State->computer[White] = true;
\r
307 State->computer[Black] = false;
\r
309 XB->new_hack = true;
\r
310 XB->result = false;
\r
315 } else if (match(string,"computer")) {
\r
317 XB->computer = true;
\r
319 } else if (match(string,"draw")) {
\r
320 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
321 my_log("POLYGLOT draw from XB received");
\r
322 uci_send_option(Uci,"DrawOffer","%s","draw");}
\r
323 } else if (match(string,"easy")) {
\r
325 XB->ponder = false;
\r
329 } else if (match(string,"edit")) {
\r
333 gui_send(GUI,"Error (unknown command): %s",string);
\r
335 } else if (match(string,"exit")) {
\r
337 State->computer[White] = false;
\r
338 State->computer[Black] = false;
\r
340 XB->analyse = false;
\r
344 } else if (match(string,"force")) {
\r
346 State->computer[White] = false;
\r
347 State->computer[Black] = false;
\r
351 } else if (match(string,"go")) {
\r
353 State->computer[game_turn(Game)] = true;
\r
354 State->computer[colour_opp(game_turn(Game))] = false;
\r
356 XB->new_hack = false;
\r
357 ASSERT(!XB->result);
\r
358 XB->result = false;
\r
362 } else if (match(string,"hard")) {
\r
368 } else if (match(string,"hint")) {
\r
370 if (option_get_bool("Book")) {
\r
372 game_get_board(Game,board);
\r
373 move = book_move(board,false);
\r
375 if (move != MoveNone && move_is_legal(move,board)) {
\r
376 move_to_san(move,board,move_string,256);
\r
377 gui_send(GUI,"Hint: %s",move_string);
\r
381 } else if (match(string,"ics *")) {
\r
385 } else if (match(string,"level * *:* *")) {
\r
387 XB->mps = atoi(Star[0]);
\r
388 XB->base = double(atoi(Star[1])) * 60.0 + double(atoi(Star[2]));
\r
389 XB->inc = double(atoi(Star[3]));
\r
391 } else if (match(string,"level * * *")) {
\r
393 XB->mps = atoi(Star[0]);
\r
394 XB->base = double(atoi(Star[1])) * 60.0;
\r
395 XB->inc = double(atoi(Star[2]));
\r
397 } else if (match(string,"name *")) {
\r
399 my_string_set(&XB->name,Star[0]);
\r
401 } else if (match(string,"new")) {
\r
403 uci_send_isready(Uci);
\r
404 my_log("POLYGLOT NEW GAME\n");
\r
406 option_set("Chess960","false");
\r
411 State->computer[White] = false;
\r
412 State->computer[Black] = false;
\r
414 State->computer[White] = false;
\r
415 State->computer[Black] = true;
\r
418 XB->new_hack = true;
\r
419 XB->result = false;
\r
421 XB->depth_limit = false;
\r
423 XB->computer = false;
\r
424 my_string_set(&XB->name,"<empty>");
\r
429 uci_send_ucinewgame(Uci);
\r
431 } else if (match(string,"nopost")) {
\r
435 } else if (match(string,"otim *")) {
\r
437 XB->opp_time = double(atoi(Star[0])) / 100.0;
\r
438 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
\r
440 } else if (match(string,"pause")) {
\r
444 gui_send(GUI,"Error (unknown command): %s",string);
\r
446 } else if (match(string,"ping *")) {
\r
448 // HACK; TODO: answer only after an engine move
\r
451 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
\r
452 XB->ping = atoi(Star[0]);
\r
453 uci_send_isready(Uci);
\r
455 ASSERT(XB->ping==-1);
\r
456 gui_send(GUI,"pong %s",Star[0]);
\r
459 } else if (match(string,"playother")) {
\r
461 State->computer[game_turn(Game)] = false;
\r
462 State->computer[colour_opp(game_turn(Game))] = true;
\r
464 XB->new_hack = false;
\r
465 ASSERT(!XB->result);
\r
466 XB->result = false;
\r
470 } else if (match(string,"post")) {
\r
474 } else if (match(string,"protover *")) {
\r
476 send_xboard_options();
\r
478 } else if (match(string,"quit")) {
\r
479 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
\r
481 } else if (match(string,"random")) {
\r
485 } else if (match(string,"rating * *")) {
\r
489 } else if (match(string,"remove")) {
\r
491 if (game_pos(Game) >= 2) {
\r
493 game_goto(Game,game_pos(Game)-2);
\r
495 ASSERT(!XB->new_hack);
\r
496 XB->new_hack = false; // HACK?
\r
497 XB->result = false;
\r
503 } else if (match(string,"rejected *")) {
\r
507 } else if (match(string,"reset")) { // protover 3?
\r
511 gui_send(GUI,"Error (unknown command): %s",string);
\r
514 || match(string,"result * {*}")
\r
515 || match(string,"result * {* }")
\r
516 || match(string,"result * { *}")
\r
517 || match(string,"result * { * }")) {
\r
519 my_log("POLYGLOT GAME END\n");
\r
527 if (option_get_bool("Book") && option_get_bool("BookLearn")) {
\r
530 } else if (my_string_equal(Star[0],"1-0")) {
\r
532 } else if (my_string_equal(Star[0],"0-1")) {
\r
534 } else if (my_string_equal(Star[0],"1/2-1/2")) {
\r
538 } else if (match(string,"resume")) {
\r
542 gui_send(GUI,"Error (unknown command): %s",string);
\r
544 } else if (match(string,"option *=*")){
\r
545 char *name=Star[0];
\r
546 char *value=Star[1];
\r
547 if(match(name, "Polyglot *")){
\r
548 char *pg_name=Star[0];
\r
549 polyglot_set_option(pg_name,value);
\r
551 start_protected_command();
\r
552 engine_send(Engine,"setoption name %s value %s",name,value);
\r
553 end_protected_command();
\r
555 } else if (match(string,"option *")){
\r
556 char *name=Star[0];
\r
557 start_protected_command();
\r
558 engine_send(Engine,"setoption name %s",name);
\r
559 end_protected_command();
\r
560 } else if (XB->has_feature_smp && match(string,"cores *")){
\r
561 int cores=atoi(Star[0]);
\r
563 // updating the number of cores
\r
564 my_log("POLYGLOT setting the number of cores to %d\n",cores);
\r
565 start_protected_command();
\r
566 uci_set_threads(Uci,cores);
\r
567 end_protected_command();
\r
570 gui_send(GUI,"Error (unknown command): %s",string);
\r
572 } else if (XB->has_feature_egt && match(string,"egtpath * *")){
\r
573 char *type=Star[0];
\r
574 char *path=Star[1];
\r
575 if(!my_string_case_equal(Star[0],"nalimov")){
\r
577 gui_send(GUI,"Error (unsupported table base format): %s",string);
\r
578 }else if(my_string_empty(path)){
\r
580 gui_send(GUI,"Error (unknown command): %s",string);
\r
582 // updating NalimovPath
\r
583 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
\r
584 start_protected_command();
\r
585 uci_send_option(Uci,"NalimovPath","%s",path);
\r
586 end_protected_command();
\r
588 } else if (XB->has_feature_memory && match(string,"memory *")){
\r
589 int memory = atoi(Star[0]);
\r
593 // updating the available memory
\r
594 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
\r
595 if(uci_get_option(Uci,"NalimovCache")>=0){
\r
596 nalimov_cache=atoi(Uci->option[uci_get_option(Uci,"NalimovCache")].value);
\r
600 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);
\r
601 real_memory=memory-nalimov_cache;
\r
603 start_protected_command();
\r
604 uci_send_option(Uci,"Hash", "%d", real_memory);
\r
605 end_protected_command();
\r
609 gui_send(GUI,"Error (unknown command): %s",string);
\r
612 } else if (match(string,"sd *")) {
\r
614 XB->depth_limit = true;
\r
615 XB->depth_max = atoi(Star[0]);
\r
617 } else if (match(string,"setboard *")) {
\r
619 my_log("POLYGLOT FEN %s\n",Star[0]);
\r
621 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
\r
623 State->computer[White] = false;
\r
624 State->computer[Black] = false;
\r
626 XB->new_hack = true; // HACK?
\r
627 XB->result = false;
\r
632 } else if (match(string,"st *")) {
\r
634 XB->time_limit = true;
\r
635 XB->time_max = double(atoi(Star[0]));
\r
637 } else if (match(string,"time *")) {
\r
639 XB->my_time = double(atoi(Star[0])) / 100.0;
\r
640 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
642 } else if (match(string,"undo")) {
\r
644 if (game_pos(Game) >= 1) {
\r
646 game_goto(Game,game_pos(Game)-1);
\r
648 ASSERT(!XB->new_hack);
\r
649 XB->new_hack = false; // HACK?
\r
650 XB->result = false;
\r
656 } else if (match(string,"usermove *")) {
\r
658 game_get_board(Game,board);
\r
659 move = move_from_san(Star[0],board);
\r
661 if (move != MoveNone && move_is_legal(move,board)) {
\r
663 XB->new_hack = false;
\r
664 ASSERT(!XB->result);
\r
665 XB->result = false;
\r
672 gui_send(GUI,"Illegal move: %s",Star[0]);
\r
675 } else if (match(string,"variant *")) {
\r
677 if (my_string_equal(Star[0],"fischerandom")) {
\r
678 option_set("Chess960","true");
\r
680 option_set("Chess960","false");
\r
683 } else if (match(string,"white")) {
\r
685 if (colour_is_white(game_turn(Game))) {
\r
687 State->computer[White] = false;
\r
688 State->computer[Black] = true;
\r
690 XB->new_hack = true;
\r
691 XB->result = false;
\r
696 } else if (match(string,"xboard")) {
\r
700 } else if (match(string,".")) { // analyse info
\r
702 if (State->state == ANALYSE) {
\r
703 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
\r
705 ASSERT(Uci->searching);
\r
706 ASSERT(Uci->pending_nb>=1);
\r
708 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
\r
709 move_to_san(Uci->root_move,Uci->board,move_string,256);
\r
710 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d %s",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,Uci->root_move_nb-(Uci->root_move_pos+1),Uci->root_move_nb,move_string);
\r
712 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
\r
716 } else if (match(string,"?")) { // move now
\r
718 if (State->state == THINK) {
\r
720 ASSERT(Uci->searching);
\r
721 ASSERT(Uci->pending_nb>=1);
\r
723 // HACK: just send "stop" to the engine
\r
725 if (Uci->searching) {
\r
726 my_log("POLYGLOT STOP SEARCH\n");
\r
727 engine_send(Engine,"stop");
\r
731 } else { // unknown command, maybe a move?
\r
733 game_get_board(Game,board);
\r
734 move = move_from_san(string,board);
\r
736 if (move != MoveNone && move_is_legal(move,board)) {
\r
738 XB->new_hack = false;
\r
739 ASSERT(!XB->result);
\r
740 XB->result = false;
\r
745 } else if (move != MoveNone) {
\r
747 gui_send(GUI,"Illegal move: %s",string);
\r
751 gui_send(GUI,"Error (unknown command): %s",string);
\r
759 static void engine_step(char string[]) {
\r
762 event = uci_parse(Uci,string);
\r
766 if ((event & EVENT_READY) != 0) {
\r
768 // the engine is now ready
\r
772 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
\r
775 if (!DelayPong && XB->ping >= 0) {
\r
776 gui_send(GUI,"pong %d",XB->ping);
\r
781 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
\r
783 // the engine is playing a move
\r
785 // MEGA HACK: estimate remaining time because XBoard won't send it!
\r
787 my_timer_stop(State->timer);
\r
789 XB->my_time -= my_timer_elapsed_real(State->timer);
\r
790 XB->my_time += XB->inc;
\r
791 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
\r
793 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
795 // play the engine move
\r
797 comp_move(Uci->best_move);
\r
800 if ((event & EVENT_PV) != 0) {
\r
802 // the engine has sent a new PV
\r
806 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
\r
807 my_log("POYGLOT draw offer/resign from engine\n");
\r
808 if(uci_option_exist(Uci,"UCI_DrawOffers")){
\r
809 if(event & EVENT_DRAW)
\r
810 gui_send(GUI,"offer draw");
\r
812 gui_send(GUI,"resign");
\r
817 // format_xboard_option_line
\r
819 void format_xboard_option_line(char * option_line, option_t *opt){
\r
821 char option_string[StringSize];
\r
822 strcpy(option_line,"");
\r
823 strcat(option_line,"feature option=\"");
\r
825 strcat(option_line,"Polyglot ");
\r
827 sprintf(option_string,"%s",opt->name);
\r
828 strcat(option_line,option_string);
\r
829 sprintf(option_string," -%s",opt->type);
\r
830 strcat(option_line,option_string);
\r
831 if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){
\r
832 if(strcmp(opt->type,"check")){
\r
833 sprintf(option_string," %s",opt->default_);
\r
835 sprintf(option_string," %d",
\r
836 strcmp(opt->default_,"true")?0:1);
\r
838 strcat(option_line,option_string);
\r
840 if(!strcmp(opt->type,"spin")){
\r
841 sprintf(option_string," %s",opt->min);
\r
842 strcat(option_line,option_string);
\r
844 if(!strcmp(opt->type,"spin")){
\r
845 sprintf(option_string," %s",opt->max);
\r
846 strcat(option_line,option_string);
\r
848 for(j=0;j<opt->var_nb;j++){
\r
849 if(!strcmp(opt->var[j],opt->default_)){
\r
850 sprintf(option_string," *%s",opt->var[j]);
\r
852 sprintf(option_string," %s",opt->var[j]);
\r
854 strcat(option_line,option_string);
\r
855 if(j!=opt->var_nb-1){
\r
856 strcat(option_line," ///");
\r
859 strcat(option_line,"\"");
\r
862 // send_xboard_options
\r
864 static void send_xboard_options(){
\r
866 char option_line[StringSize]="";
\r
867 option_t *p=Option;
\r
869 XB->proto_ver = atoi(Star[0]);
\r
870 ASSERT(XB->proto_ver>=2);
\r
872 gui_send(GUI,"feature done=0");
\r
874 gui_send(GUI,"feature analyze=1");
\r
875 gui_send(GUI,"feature colors=0");
\r
876 gui_send(GUI,"feature draw=1");
\r
877 gui_send(GUI,"feature ics=1");
\r
878 gui_send(GUI,"feature myname=\"%s\"",option_get_string("EngineName"));
\r
879 gui_send(GUI,"feature name=1");
\r
880 gui_send(GUI,"feature pause=0");
\r
881 gui_send(GUI,"feature ping=1");
\r
882 gui_send(GUI,"feature playother=1");
\r
883 gui_send(GUI,"feature reuse=1");
\r
884 gui_send(GUI,"feature san=0");
\r
885 gui_send(GUI,"feature setboard=1");
\r
886 gui_send(GUI,"feature sigint=0");
\r
887 gui_send(GUI,"feature sigterm=0");
\r
888 gui_send(GUI,"feature time=1");
\r
889 gui_send(GUI,"feature usermove=1");
\r
890 if (XB->has_feature_memory){
\r
891 gui_send(GUI,"feature memory=1");
\r
893 gui_send(GUI,"feature memory=0");
\r
895 if (XB->has_feature_smp){
\r
896 gui_send(GUI,"feature smp=1");
\r
898 gui_send(GUI,"feature smp=0");
\r
900 if (XB->has_feature_egt){
\r
901 // TODO: support for other types of table bases
\r
902 gui_send(GUI,"feature egt=\"nalimov\"");
\r
904 gui_send(GUI,"feature egt=\"\"");
\r
907 if (uci_option_exist(Uci,"UCI_Chess960")) {
\r
908 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
\r
910 gui_send(GUI,"feature variants=\"normal\"");
\r
913 for(i=0;i<Uci->option_nb;i++){
\r
914 if(!strcmp(Uci->option[i].name,PolyglotBookFile)) continue;
\r
915 if(my_string_case_equal(Uci->option[i].name,"UCI_AnalyseMode")) continue;
\r
916 if(my_string_case_equal(Uci->option[i].name,"Ponder")) continue;
\r
917 if(my_string_case_equal(Uci->option[i].name,"Hash")) continue;
\r
918 if(my_string_case_equal(Uci->option[i].name,"NalimovPath")) continue;
\r
919 if((name=uci_thread_option(Uci))!=NULL && my_string_case_equal(Uci->option[i].name,name)) continue;
\r
920 format_xboard_option_line(option_line,Uci->option+i);
\r
922 gui_send(GUI,"%s",option_line);
\r
926 if(p->mode &XBOARD){
\r
927 format_xboard_option_line(option_line,p);
\r
928 gui_send(GUI,"%s",option_line);
\r
932 gui_send(GUI,"feature done=1");
\r
938 static void comp_move(int move) {
\r
943 ASSERT(move_is_ok(move));
\r
945 ASSERT(State->state==THINK);
\r
946 ASSERT(!XB->analyse);
\r
948 if(option_get_bool("RepeatPV")==true)
\r
949 send_pv(); // to update time and nodes
\r
953 game_get_board(Game,board);
\r
955 if (move_is_castle(move,board) && option_get_bool("Chess960")) {
\r
956 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
\r
958 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
\r
961 gui_send(GUI,"move %s",string);
\r
965 if (option_get_bool("Resign") && Uci->root_move_nb > 1) {
\r
967 if (Uci->best_score <= -abs(option_get_int("ResignScore"))) {
\r
969 State->resign_nb++;
\r
970 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
\r
972 if (State->resign_nb >= option_get_int("ResignMoves")) {
\r
973 my_log("POLYGLOT *** RESIGN ***\n");
\r
974 gui_send(GUI,"resign");
\r
979 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
\r
980 State->resign_nb = 0;
\r
992 static void move_step(int move) {
\r
995 char move_string[256];
\r
997 ASSERT(move_is_ok(move));
\r
1001 game_get_board(Game,board);
\r
1003 if (move != MoveNone && move_is_legal(move,board)) {
\r
1005 move_to_san(move,board,move_string,256);
\r
1006 my_log("POLYGLOT MOVE %s\n",move_string);
\r
1010 move_to_can(move,board,move_string,256);
\r
1011 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
\r
1012 board_disp(board);
\r
1014 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
\r
1019 game_add_move(Game,move);
\r
1025 static void board_update() {
\r
1027 // handle game end
\r
1029 ASSERT(!XB->result);
\r
1031 switch (game_status(Game)) {
\r
1035 gui_send(GUI,"1-0 {White mates}");
\r
1038 gui_send(GUI,"0-1 {Black mates}");
\r
1041 gui_send(GUI,"1/2-1/2 {Stalemate}");
\r
1043 case DRAW_MATERIAL:
\r
1044 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
\r
1047 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
\r
1049 case DRAW_REPETITION:
\r
1050 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
\r
1060 static void mess() {
\r
1062 // clear state variables
\r
1064 State->resign_nb = 0;
\r
1065 State->exp_move = MoveNone;
\r
1066 my_timer_reset(State->timer);
\r
1068 // abort a possible search
\r
1072 // calculate the new state
\r
1075 } else if (!active()) {
\r
1076 State->state = WAIT;
\r
1077 my_log("POLYGLOT WAIT\n");
\r
1078 } else if (XB->analyse) {
\r
1079 State->state = ANALYSE;
\r
1080 my_log("POLYGLOT ANALYSE\n");
\r
1081 } else if (State->computer[game_turn(Game)]) {
\r
1082 State->state = THINK;
\r
1083 my_log("POLYGLOT THINK\n");
\r
1085 State->state = WAIT;
\r
1086 my_log("POLYGLOT WAIT\n");
\r
1094 static void no_mess(int move) {
\r
1096 ASSERT(move_is_ok(move));
\r
1098 // just received a move, calculate the new state
\r
1102 } else if (!active()) {
\r
1104 stop_search(); // abort a possible search
\r
1106 State->state = WAIT;
\r
1107 State->exp_move = MoveNone;
\r
1109 my_log("POLYGLOT WAIT\n");
\r
1111 } else if (State->state == WAIT) {
\r
1113 ASSERT(State->computer[game_turn(Game)]);
\r
1114 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1115 ASSERT(!XB->analyse);
\r
1117 my_log("POLYGLOT WAIT -> THINK\n");
\r
1119 State->state = THINK;
\r
1120 State->exp_move = MoveNone;
\r
1122 } else if (State->state == THINK) {
\r
1124 ASSERT(!State->computer[game_turn(Game)]);
\r
1125 ASSERT(State->computer[colour_opp(game_turn(Game))]);
\r
1126 ASSERT(!XB->analyse);
\r
1128 if (ponder() && ponder_ok(Uci->ponder_move)) {
\r
1130 my_log("POLYGLOT THINK -> PONDER\n");
\r
1132 State->state = PONDER;
\r
1133 State->exp_move = Uci->ponder_move;
\r
1137 my_log("POLYGLOT THINK -> WAIT\n");
\r
1139 State->state = WAIT;
\r
1140 State->exp_move = MoveNone;
\r
1143 } else if (State->state == PONDER) {
\r
1145 ASSERT(State->computer[game_turn(Game)]);
\r
1146 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1147 ASSERT(!XB->analyse);
\r
1149 if (move == State->exp_move && Uci->searching) {
\r
1151 ASSERT(Uci->searching);
\r
1152 ASSERT(Uci->pending_nb>=1);
\r
1154 my_timer_start(State->timer);//also resets
\r
1156 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
\r
1157 engine_send(Engine,"ponderhit");
\r
1159 State->state = THINK;
\r
1160 State->exp_move = MoveNone;
\r
1162 send_pv(); // update display
\r
1164 return; // do not launch a new search
\r
1168 my_log("POLYGLOT PONDER -> THINK (miss)\n");
\r
1172 State->state = THINK;
\r
1173 State->exp_move = MoveNone;
\r
1176 } else if (State->state == ANALYSE) {
\r
1178 ASSERT(XB->analyse);
\r
1180 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
\r
1192 // start_protected_command()
\r
1194 static void start_protected_command(){
\r
1198 static void end_protected_command(){
\r
1199 if(Uci->ready){ // not init faze
\r
1200 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
\r
1202 update_remaining_time();
\r
1203 search_update(); // relaunch search if necessary
\r
1206 // update_remaining_time()
\r
1208 static void update_remaining_time(){
\r
1210 if(State->timer->running){
\r
1211 my_timer_stop(State->timer);
\r
1212 reduce = my_timer_elapsed_real(State->timer);
\r
1213 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
\r
1214 XB->my_time -= reduce;
\r
1215 if(XB->my_time<0.0){
\r
1222 // search_update()
\r
1224 static void search_update() {
\r
1229 int nalimov_cache;
\r
1232 ASSERT(!Uci->searching);
\r
1237 // launch a new search if needed
\r
1241 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
\r
1245 if (State->state == THINK && option_get_bool("Book")) {
\r
1247 game_get_board(Game,Uci->board);
\r
1249 move = book_move(Uci->board,option_get_bool("BookRandom"));
\r
1251 if (move != MoveNone && move_is_legal(move,Uci->board)) {
\r
1253 my_log("POLYGLOT *BOOK MOVE*\n");
\r
1255 search_clear(); // clears Uci->ponder_move
\r
1256 Uci->best_move = move;
\r
1258 board_copy(board,Uci->board);
\r
1259 move_do(board,move);
\r
1260 Uci->ponder_move = book_move(board,false); // expected move = best book move
\r
1262 Uci->best_pv[0] = Uci->best_move;
\r
1263 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
\r
1264 Uci->best_pv[2] = MoveNone;
\r
1266 comp_move(Uci->best_move);
\r
1274 my_log("POLYGLOT START SEARCH\n");
\r
1278 uci_send_option(Uci,"UCI_Chess960","%s",option_get_bool("Chess960")?"true":"false");
\r
1280 if (option_get_int("UCIVersion") >= 2) {
\r
1281 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
\r
1282 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
\r
1285 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
\r
1289 move = (State->state == PONDER) ? State->exp_move : MoveNone;
\r
1290 send_board(move); // updates Uci->board global variable
\r
1294 if (State->state == THINK || State->state == PONDER) {
\r
1296 engine_send_queue(Engine,"go");
\r
1298 if (XB->time_limit) {
\r
1300 // fixed time per move
\r
1302 engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);
\r
1308 if (colour_is_white(Uci->board->turn)) {
\r
1309 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);
\r
1311 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);
\r
1314 if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);
\r
1316 if (XB->mps != 0) {
\r
1318 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
\r
1319 ASSERT(move_nb>=1&&move_nb<=XB->mps);
\r
1321 engine_send_queue(Engine," movestogo %d",move_nb);
\r
1325 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
\r
1327 if (State->state == PONDER) engine_send_queue(Engine," ponder");
\r
1329 engine_send(Engine,""); // newline
\r
1331 } else if (State->state == ANALYSE) {
\r
1333 engine_send(Engine,"go infinite");
\r
1340 // init search info
\r
1342 ASSERT(!Uci->searching);
\r
1346 Uci->searching = true;
\r
1347 Uci->pending_nb++;
\r
1353 static void search_clear() {
\r
1359 my_timer_start(State->timer);//also resets
\r
1364 static bool active() {
\r
1368 if (game_status(Game) != PLAYING) return false; // game ended
\r
1372 if (XB->analyse) return true; // analysing
\r
1373 if (!State->computer[White] && !State->computer[Black]) return false; // force mode
\r
1374 if (XB->new_hack || XB->result) return false; // unstarted or ended game
\r
1376 return true; // playing
\r
1381 static bool ponder() {
\r
1383 return XB->ponder && (option_get_bool("CanPonder") || uci_option_exist(Uci,"Ponder"));
\r
1387 static bool ponder_ok(int move) {
\r
1391 ASSERT(move==MoveNone||move_is_ok(move));
\r
1393 // legal ponder move?
\r
1395 if (move == MoveNone) return false;
\r
1397 game_get_board(Game,board);
\r
1398 if (!move_is_legal(move,board)) return false;
\r
1400 // UCI-legal resulting position?
\r
1402 game_add_move(Game,move);
\r
1404 game_get_board(Game,board);
\r
1405 status = game_status(Game);
\r
1407 game_rem_move(Game);
\r
1409 if (status != PLAYING) return false; // game ended
\r
1411 if (option_get_bool("Book") && is_in_book(board)) {
\r
1420 static void stop_search() {
\r
1422 if (Uci->searching) {
\r
1424 ASSERT(Uci->searching);
\r
1425 ASSERT(Uci->pending_nb>=1);
\r
1427 my_log("POLYGLOT STOP SEARCH\n");
\r
1430 engine_send(Engine,"stop");
\r
1431 Uci->searching = false;
\r
1434 if (option_get_bool("SyncStop")) {
\r
1435 uci_send_stop_sync(Uci);
\r
1437 uci_send_stop(Uci);
\r
1444 static void send_board(int extra_move) {
\r
1453 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
\r
1455 ASSERT(!Uci->searching);
\r
1459 game_get_board(Game,Uci->board);
\r
1460 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
\r
1462 board_to_fen(Uci->board,fen,256);
\r
1463 my_log("POLYGLOT FEN %s\n",fen);
\r
1465 ASSERT(board_can_play(Uci->board));
\r
1470 end = game_pos(Game);
\r
1471 ASSERT(end>=start);
\r
1475 game_get_board(Game,board,start);
\r
1476 board_to_fen(board,string,256);
\r
1478 engine_send_queue(Engine,"position");
\r
1480 if (my_string_equal(string,StartFen)) {
\r
1481 engine_send_queue(Engine," startpos");
\r
1483 engine_send_queue(Engine," fen %s",string);
\r
1488 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
\r
1490 for (pos = start; pos < end; pos++) { // game moves
\r
1492 move = game_move(Game,pos);
\r
1494 move_to_can(move,board,string,256);
\r
1495 engine_send_queue(Engine," %s",string);
\r
1497 move_do(board,move);
\r
1500 if (extra_move != MoveNone) { // move to ponder on
\r
1501 move_to_can(extra_move,board,string,256);
\r
1502 engine_send_queue(Engine," %s",string);
\r
1507 engine_send(Engine,""); // newline
\r
1512 static void send_pv() {
\r
1514 char pv_string[StringSize];
\r
1517 char move_string[StringSize];
\r
1519 ASSERT(State->state!=WAIT);
\r
1521 if (Uci->best_depth == 0) return;
\r
1523 // xboard search information
\r
1527 if (State->state == THINK || State->state == ANALYSE) {
\r
1529 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1531 if(Uci->depth==-1) //hack to clear the engine output window
\r
1532 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,Uci->best_score,Uci->time*100.0,Uci->node_nb);
\r
1534 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
1536 } else if (State->state == PONDER && option_get_bool("ShowPonder")) {
\r
1538 game_get_board(Game,board);
\r
1539 move = State->exp_move;
\r
1541 if (move != MoveNone && move_is_legal(move,board)) {
\r
1542 move_to_san(move,board,move_string,256);
\r
1543 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1544 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
1551 if ((Uci->searching && option_get_bool("KibitzPV") && Uci->time >= option_get_double("KibitzDelay"))
\r
1552 || (!Uci->searching && option_get_bool("KibitzMove"))) {
\r
1554 if (State->state == THINK || State->state == ANALYSE) {
\r
1556 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1557 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
1559 } else if (State->state == PONDER) {
\r
1561 game_get_board(Game,board);
\r
1562 move = State->exp_move;
\r
1564 if (move != MoveNone && move_is_legal(move,board)) {
\r
1565 move_to_san(move,board,move_string,256);
\r
1566 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1567 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
1575 static void learn(int result) {
\r
1581 ASSERT(result>=-1&&result<=+1);
\r
1583 ASSERT(XB->result);
\r
1584 ASSERT(State->computer[White]||State->computer[Black]);
\r
1591 } else if (State->computer[White]) {
\r
1593 } else if (State->computer[Black]) {
\r
1597 my_fatal("learn(): unknown side\n");
\r
1601 } else if (result > 0) {
\r
1602 my_log("POLYGLOT *LEARN WIN*\n");
\r
1603 } else if (result < 0) {
\r
1604 my_log("POLYGLOT *LEARN LOSS*\n");
\r
1606 my_log("POLYGLOT *LEARN DRAW*\n");
\r
1611 for (; pos < Game->size; pos += 2) {
\r
1613 game_get_board(Game,board,pos);
\r
1614 move = game_move(Game,pos);
\r
1616 book_learn_move(board,move,result);
\r
1622 // end of adapter.cpp
\r