22 #include "move_do.h"
\r
23 #include "move_legal.h"
\r
28 #include "uci2uci.h"
\r
33 #define StringSize 4096
\r
37 static const bool UseDebug = FALSE;
\r
38 static const bool DelayPong = FALSE;
\r
44 bool computer[ColourNb];
\r
47 my_timer_t timer[1];
\r
51 bool has_feature_memory;
\r
52 bool has_feature_smp;
\r
53 bool has_feature_egt;
\r
58 bool new_hack; // "new" is a C++ keyword
\r
79 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;
\r
83 static state_t State[1];
\r
88 static void comp_move (int move);
\r
89 static void move_step (int move);
\r
90 static void board_update ();
\r
92 static void mess ();
\r
93 static void no_mess (int move);
\r
95 static void search_update ();
\r
96 static void search_clear ();
\r
97 static void update_remaining_time();
\r
98 static int report_best_score();
\r
99 static bool kibitz_throttle (bool searching);
\r
100 static void start_protected_command();
\r
101 static void end_protected_command();
\r
103 static bool active ();
\r
104 static bool ponder ();
\r
105 static bool ponder_ok (int ponder_move);
\r
107 static void stop_search ();
\r
109 static void send_board (int extra_move);
\r
110 static void send_pv ();
\r
111 static void send_info ();
\r
113 static void send_xboard_options ();
\r
115 static void learn (int result);
\r
120 // xboard2uci_init()
\r
122 void xboard2uci_init() {
\r
129 State->state = WAIT;
\r
131 State->computer[White] = FALSE;
\r
132 State->computer[Black] = TRUE;
\r
134 State->exp_move = MoveNone;
\r
135 State->resign_nb = 0;
\r
136 my_timer_reset(State->timer);
\r
138 // yes there are engines that do not have the "Hash" option....
\r
139 XB->has_feature_memory= (option_find(Uci->option,"Hash")!=NULL);
\r
140 XB->has_feature_smp = (uci_thread_option(Uci)!=NULL);
\r
141 // TODO: support for other types of table bases
\r
142 XB->has_feature_egt = (option_find(Uci->option,"NalimovPath")!=NULL);
\r
143 XB->analyse = FALSE;
\r
144 XB->computer = FALSE;
\r
146 my_string_set(&XB->name,"<empty>");
\r
148 XB->new_hack = TRUE;
\r
150 XB->ponder = FALSE;
\r
153 XB->result = FALSE;
\r
159 XB->time_limit = FALSE;
\r
160 XB->time_max = 5.0;
\r
162 XB->depth_limit = FALSE;
\r
163 XB->depth_max = 127;
\r
165 XB->my_time = 300.0;
\r
166 XB->opp_time = 300.0;
\r
169 // xboard2uci_gui_step()
\r
171 void xboard2uci_gui_step(char string[]) {
\r
174 char move_string[256];
\r
179 } else if (match(string,"accepted *")) {
\r
183 } else if (match(string,"analyze")) {
\r
185 State->computer[White] = FALSE;
\r
186 State->computer[Black] = FALSE;
\r
188 XB->analyse = TRUE;
\r
189 XB->new_hack = FALSE;
\r
190 ASSERT(!XB->result);
\r
191 XB->result = FALSE;
\r
195 } else if (match(string,"bk")) {
\r
197 if (option_get_bool(Option,"Book")) {
\r
198 game_get_board(Game,board);
\r
202 } else if (match(string,"black")) {
\r
204 if (colour_is_black(game_turn(Game))) {
\r
206 State->computer[White] = TRUE;
\r
207 State->computer[Black] = FALSE;
\r
209 XB->new_hack = TRUE;
\r
210 XB->result = FALSE;
\r
215 } else if (match(string,"computer")) {
\r
217 XB->computer = TRUE;
\r
219 } else if (match(string,"draw")) {
\r
220 if(option_find(Uci->option,"UCI_DrawOffers")){
\r
221 my_log("POLYGLOT draw from XB received");
\r
222 uci_send_option(Uci,"DrawOffer","%s","draw");}
\r
223 } else if (match(string,"easy")) {
\r
225 XB->ponder = FALSE;
\r
229 } else if (match(string,"edit")) {
\r
233 gui_send(GUI,"Error (unknown command): %s",string);
\r
235 } else if (match(string,"exit")) {
\r
237 State->computer[White] = FALSE;
\r
238 State->computer[Black] = FALSE;
\r
240 XB->analyse = FALSE;
\r
244 } else if (match(string,"force")) {
\r
246 State->computer[White] = FALSE;
\r
247 State->computer[Black] = FALSE;
\r
251 } else if (match(string,"go")) {
\r
253 State->computer[game_turn(Game)] = TRUE;
\r
254 State->computer[colour_opp(game_turn(Game))] = FALSE;
\r
256 XB->new_hack = FALSE;
\r
257 ASSERT(!XB->result);
\r
258 XB->result = FALSE;
\r
262 } else if (match(string,"hard")) {
\r
268 } else if (match(string,"hint")) {
\r
270 if (option_get_bool(Option,"Book")) {
\r
272 game_get_board(Game,board);
\r
273 move = book_move(board,FALSE);
\r
275 if (move != MoveNone && move_is_legal(move,board)) {
\r
276 move_to_san(move,board,move_string,256);
\r
277 gui_send(GUI,"Hint: %s",move_string);
\r
281 } else if (match(string,"ics *")) {
\r
285 } else if (match(string,"level * *:* *")) {
\r
287 XB->mps = atoi(Star[0]);
\r
288 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
\r
289 XB->inc = ((double)atoi(Star[3]));
\r
291 } else if (match(string,"level * * *")) {
\r
293 XB->mps = atoi(Star[0]);
\r
294 XB->base = ((double)atoi(Star[1])) * 60.0;
\r
295 XB->inc = ((double)atoi(Star[2]));
\r
297 } else if (match(string,"name *")) {
\r
299 my_string_set(&XB->name,Star[0]);
\r
301 } else if (match(string,"new")) {
\r
303 uci_send_isready(Uci);
\r
304 my_log("POLYGLOT NEW GAME\n");
\r
306 option_set(Option,"Chess960","false");
\r
311 State->computer[White] = FALSE;
\r
312 State->computer[Black] = FALSE;
\r
314 State->computer[White] = FALSE;
\r
315 State->computer[Black] = TRUE;
\r
318 XB->new_hack = TRUE;
\r
319 XB->result = FALSE;
\r
321 XB->depth_limit = FALSE;
\r
323 XB->computer = FALSE;
\r
324 my_string_set(&XB->name,"<empty>");
\r
329 uci_send_ucinewgame(Uci);
\r
331 } else if (match(string,"nopost")) {
\r
335 } else if (match(string,"otim *")) {
\r
337 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
\r
338 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
\r
340 } else if (match(string,"pause")) {
\r
344 gui_send(GUI,"Error (unknown command): %s",string);
\r
346 } else if (match(string,"ping *")) {
\r
348 // HACK; TODO: answer only after an engine move
\r
351 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
\r
352 XB->ping = atoi(Star[0]);
\r
353 uci_send_isready(Uci);
\r
355 ASSERT(XB->ping==-1);
\r
356 gui_send(GUI,"pong %s",Star[0]);
\r
359 } else if (match(string,"playother")) {
\r
361 State->computer[game_turn(Game)] = FALSE;
\r
362 State->computer[colour_opp(game_turn(Game))] = TRUE;
\r
364 XB->new_hack = FALSE;
\r
365 ASSERT(!XB->result);
\r
366 XB->result = FALSE;
\r
370 } else if (match(string,"post")) {
\r
374 } else if (match(string,"protover *")) {
\r
375 XB->proto_ver = atoi(Star[0]);
\r
376 ASSERT(XB->proto_ver>=2);
\r
377 send_xboard_options();
\r
379 } else if (match(string,"quit")) {
\r
380 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
\r
382 } else if (match(string,"random")) {
\r
386 } else if (match(string,"rating * *")) {
\r
390 } else if (match(string,"remove")) {
\r
392 if (game_pos(Game) >= 2) {
\r
394 game_goto(Game,game_pos(Game)-2);
\r
396 ASSERT(!XB->new_hack);
\r
397 XB->new_hack = FALSE; // HACK?
\r
398 XB->result = FALSE;
\r
404 } else if (match(string,"rejected *")) {
\r
408 } else if (match(string,"reset")) { // protover 3?
\r
412 gui_send(GUI,"Error (unknown command): %s",string);
\r
415 || match(string,"result * {*}")
\r
416 || match(string,"result * {* }")
\r
417 || match(string,"result * { *}")
\r
418 || match(string,"result * { * }")) {
\r
420 my_log("POLYGLOT GAME END\n");
\r
428 if (option_get_bool(Option,"Book") &&
\r
429 option_get_bool(Option,"BookLearn")) {
\r
432 } else if (my_string_equal(Star[0],"1-0")) {
\r
434 } else if (my_string_equal(Star[0],"0-1")) {
\r
436 } else if (my_string_equal(Star[0],"1/2-1/2")) {
\r
440 } else if (match(string,"resume")) {
\r
444 gui_send(GUI,"Error (unknown command): %s",string);
\r
446 } else if (match(string,"option *=*") ||
\r
447 match(string,"option * =*") ||
\r
448 match(string,"option *= *") ||
\r
449 match(string,"option * = *")
\r
451 char *name=Star[0];
\r
452 char *value=Star[1];
\r
453 if(match(name, "Polyglot *")){
\r
454 char *pg_name=Star[0];
\r
455 polyglot_set_option(pg_name,value);
\r
457 option_t *opt=option_find(Uci->option,name);
\r
459 if(my_string_case_equal(opt->type,"check")){
\r
460 value=my_string_equal(value,"1")?"true":"false";
\r
462 start_protected_command();
\r
463 uci_send_option(Uci, name, "%s", value);
\r
464 end_protected_command();
\r
466 gui_send(GUI,"Error (unknown option): %s",name);
\r
469 } else if (match(string,"option *")){
\r
470 char *name=Star[0];
\r
471 start_protected_command();
\r
472 // value is ignored
\r
473 if(!uci_send_option(Uci, name, "%s", "<empty>")){
\r
474 gui_send(GUI,"Error (unknown option): %s",name);
\r
476 end_protected_command();
\r
477 } else if (XB->has_feature_smp && match(string,"cores *")){
\r
478 int cores=atoi(Star[0]);
\r
480 // updating the number of cores
\r
481 my_log("POLYGLOT setting the number of cores to %d\n",cores);
\r
482 start_protected_command();
\r
483 uci_set_threads(Uci,cores);
\r
484 end_protected_command();
\r
487 gui_send(GUI,"Error (unknown command): %s",string);
\r
489 } else if (XB->has_feature_egt && match(string,"egtpath * *")){
\r
490 char *type=Star[0];
\r
491 char *path=Star[1];
\r
492 if(!my_string_case_equal(type,"nalimov")){
\r
494 gui_send(GUI,"Error (unsupported table base format): %s",string);
\r
495 }else if(my_string_empty(path)){
\r
497 gui_send(GUI,"Error (unknown command): %s",string);
\r
499 // updating NalimovPath
\r
500 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
\r
501 start_protected_command();
\r
502 uci_send_option(Uci,"NalimovPath","%s",path);
\r
503 end_protected_command();
\r
505 } else if (XB->has_feature_memory && match(string,"memory *")){
\r
506 int memory = atoi(Star[0]);
\r
510 // updating the available memory
\r
512 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
\r
513 if((opt=option_find(Uci->option,"NalimovCache"))){
\r
514 nalimov_cache=atoi(opt->value);
\r
518 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);
\r
519 real_memory=memory-nalimov_cache;
\r
521 start_protected_command();
\r
522 uci_send_option(Uci,"Hash", "%d", real_memory);
\r
523 end_protected_command();
\r
527 gui_send(GUI,"Error (unknown command): %s",string);
\r
530 } else if (match(string,"sd *")) {
\r
532 XB->depth_limit = TRUE;
\r
533 XB->depth_max = atoi(Star[0]);
\r
535 } else if (match(string,"setboard *")) {
\r
537 my_log("POLYGLOT FEN %s\n",Star[0]);
\r
539 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
\r
541 State->computer[White] = FALSE;
\r
542 State->computer[Black] = FALSE;
\r
544 XB->new_hack = TRUE; // HACK?
\r
545 XB->result = FALSE;
\r
550 } else if (match(string,"st *")) {
\r
552 XB->time_limit = TRUE;
\r
553 XB->time_max = ((double)atoi(Star[0]));
\r
555 } else if (match(string,"time *")) {
\r
557 XB->my_time = ((double)atoi(Star[0])) / 100.0;
\r
558 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
560 } else if (match(string,"undo")) {
\r
562 if (game_pos(Game) >= 1) {
\r
564 game_goto(Game,game_pos(Game)-1);
\r
566 ASSERT(!XB->new_hack);
\r
567 XB->new_hack = FALSE; // HACK?
\r
568 XB->result = FALSE;
\r
574 } else if (match(string,"usermove *")) {
\r
576 game_get_board(Game,board);
\r
577 move = move_from_san(Star[0],board);
\r
579 if (move != MoveNone && move_is_legal(move,board)) {
\r
581 XB->new_hack = FALSE;
\r
582 ASSERT(!XB->result);
\r
583 XB->result = FALSE;
\r
590 gui_send(GUI,"Illegal move: %s",Star[0]);
\r
593 } else if (match(string,"variant *")) {
\r
595 if (my_string_equal(Star[0],"fischerandom")) {
\r
596 option_set(Option,"Chess960","true");
\r
598 option_set(Option,"Chess960","false");
\r
601 } else if (match(string,"white")) {
\r
603 if (colour_is_white(game_turn(Game))) {
\r
605 State->computer[White] = FALSE;
\r
606 State->computer[Black] = TRUE;
\r
608 XB->new_hack = TRUE;
\r
609 XB->result = FALSE;
\r
614 } else if (match(string,"xboard")) {
\r
618 } else if (match(string,".")) { // analyse info
\r
620 if (State->state == ANALYSE) {
\r
621 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
\r
623 ASSERT(Uci->searching);
\r
624 ASSERT(Uci->pending_nb>=1);
\r
626 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
\r
627 move_to_san(Uci->root_move,Uci->board,move_string,256);
\r
628 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
630 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
\r
634 } else if (match(string,"?")) { // move now
\r
636 if (State->state == THINK) {
\r
638 ASSERT(Uci->searching);
\r
639 ASSERT(Uci->pending_nb>=1);
\r
641 // HACK: just send "stop" to the engine
\r
643 if (Uci->searching) {
\r
644 my_log("POLYGLOT STOP SEARCH\n");
\r
645 engine_send(Engine,"stop");
\r
649 } else { // unknown command, maybe a move?
\r
651 game_get_board(Game,board);
\r
652 move = move_from_san(string,board);
\r
654 if (move != MoveNone && move_is_legal(move,board)) {
\r
656 XB->new_hack = FALSE;
\r
657 ASSERT(!XB->result);
\r
658 XB->result = FALSE;
\r
663 } else if (move != MoveNone) {
\r
665 gui_send(GUI,"Illegal move: %s",string);
\r
669 gui_send(GUI,"Error (unknown command): %s",string);
\r
675 // xboard2uci_engine_step()
\r
677 void xboard2uci_engine_step(char string[]) {
\r
681 event = uci_parse(Uci,string);
\r
685 if ((event & EVENT_READY) != 0) {
\r
687 // the engine is now ready
\r
691 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
\r
694 if (!DelayPong && XB->ping >= 0) {
\r
695 gui_send(GUI,"pong %d",XB->ping);
\r
700 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
\r
702 // the engine is playing a move
\r
704 // MEGA HACK: estimate remaining time because XBoard won't send it!
\r
706 my_timer_stop(State->timer);
\r
708 XB->my_time -= my_timer_elapsed_real(State->timer);
\r
709 XB->my_time += XB->inc;
\r
710 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
\r
712 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
714 // play the engine move
\r
716 comp_move(Uci->best_move);
\r
719 if ((event & EVENT_PV) != 0) {
\r
721 // the engine has sent a new PV
\r
725 if ((event & EVENT_INFO) != 0) {
\r
727 // the engine has sent info
\r
731 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
\r
732 my_log("POYGLOT draw offer/resign from engine\n");
\r
733 if(option_find(Uci->option,"UCI_DrawOffers")){
\r
734 if(event & EVENT_DRAW)
\r
735 gui_send(GUI,"offer draw");
\r
737 gui_send(GUI,"resign");
\r
740 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){
\r
741 game_get_board(Game,board);
\r
742 if(board->turn==White){
\r
743 gui_send(GUI,"0-1 {polyglot: resign"
\r
744 " (illegal engine move white)}");
\r
746 gui_send(GUI,"1-0 {polyglot: resign"
\r
747 " (illegal engine move black)}");
\r
755 // format_xboard_option_line
\r
757 void format_xboard_option_line(char * option_line, option_t *opt){
\r
759 char option_string[StringSize];
\r
761 strcpy(option_line,"");
\r
762 // buffer overflow alert
\r
763 strcat(option_line,"feature option=\"");
\r
765 strcat(option_line,"Polyglot ");
\r
767 sprintf(option_string,"%s",opt->name);
\r
768 strcat(option_line,option_string);
\r
769 sprintf(option_string," -%s",opt->type);
\r
770 strcat(option_line,option_string);
\r
771 if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){
\r
772 if(strcmp(opt->type,"check")){
\r
773 sprintf(option_string," %s",opt->default_);
\r
775 sprintf(option_string," %d",
\r
776 my_string_case_equal(opt->default_,"true")?1:0);
\r
778 strcat(option_line,option_string);
\r
780 if(!strcmp(opt->type,"spin")){
\r
781 sprintf(option_string," %s",opt->min);
\r
782 strcat(option_line,option_string);
\r
784 if(!strcmp(opt->type,"spin")){
\r
785 sprintf(option_string," %s",opt->max);
\r
786 strcat(option_line,option_string);
\r
788 for(j=0;j<opt->var_nb;j++){
\r
789 if(!strcmp(opt->var[j],opt->default_)){
\r
790 sprintf(option_string," *%s",opt->var[j]);
\r
792 sprintf(option_string," %s",opt->var[j]);
\r
794 strcat(option_line,option_string);
\r
795 if(j!=opt->var_nb-1){
\r
796 strcat(option_line," ///");
\r
799 strcat(option_line,"\"");
\r
800 if(option_get_bool(Option,"WbWorkArounds") &&
\r
801 (tmp=strstr(option_line,"Draw"))){
\r
803 my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",
\r
808 // send_xboard_options()
\r
810 static void send_xboard_options(){
\r
812 char option_line[StringSize]="";
\r
816 gui_send(GUI,"feature done=0");
\r
818 gui_send(GUI,"feature analyze=1");
\r
819 gui_send(GUI,"feature colors=0");
\r
820 gui_send(GUI,"feature draw=1");
\r
821 gui_send(GUI,"feature ics=1");
\r
822 gui_send(GUI,"feature myname=\"%s\"",
\r
823 option_get_string(Option,"EngineName"));
\r
824 gui_send(GUI,"feature name=1");
\r
825 gui_send(GUI,"feature pause=0");
\r
826 gui_send(GUI,"feature ping=1");
\r
827 gui_send(GUI,"feature playother=1");
\r
828 gui_send(GUI,"feature reuse=1");
\r
829 gui_send(GUI,"feature san=0");
\r
830 gui_send(GUI,"feature setboard=1");
\r
831 gui_send(GUI,"feature sigint=0");
\r
832 gui_send(GUI,"feature sigterm=0");
\r
833 gui_send(GUI,"feature time=1");
\r
834 gui_send(GUI,"feature usermove=1");
\r
835 if (XB->has_feature_memory){
\r
836 gui_send(GUI,"feature memory=1");
\r
838 gui_send(GUI,"feature memory=0");
\r
840 if (XB->has_feature_smp){
\r
841 gui_send(GUI,"feature smp=1");
\r
843 gui_send(GUI,"feature smp=0");
\r
845 if (XB->has_feature_egt){
\r
846 // TODO: support for other types of table bases
\r
847 gui_send(GUI,"feature egt=\"nalimov\"");
\r
849 gui_send(GUI,"feature egt=\"\"");
\r
852 if (option_find(Uci->option,"UCI_Chess960")) {
\r
853 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
\r
855 gui_send(GUI,"feature variants=\"normal\"");
\r
858 option_start_iter(Uci->option);
\r
859 while((opt=option_next(Uci->option))){
\r
860 if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;
\r
861 if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;
\r
862 if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;
\r
863 if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;
\r
864 if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;
\r
865 if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;
\r
866 if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;
\r
867 if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;
\r
868 if(my_string_case_equal(opt->name,"Ponder")) continue;
\r
869 if(my_string_case_equal(opt->name,"Hash")) continue;
\r
870 if(my_string_case_equal(opt->name,"NalimovPath")) continue;
\r
871 if((name=uci_thread_option(Uci))!=NULL &&
\r
872 my_string_case_equal(opt->name,name)) continue;
\r
874 format_xboard_option_line(option_line,opt);
\r
876 gui_send(GUI,"%s",option_line);
\r
880 option_start_iter(Option);
\r
881 while((opt=option_next(Option))){
\r
882 if(opt->mode &XBOARD){
\r
883 format_xboard_option_line(option_line,opt);
\r
884 gui_send(GUI,"%s",option_line);
\r
887 gui_send(GUI,"feature done=1");
\r
891 // report_best_score()
\r
893 static int report_best_score(){
\r
894 if(!option_get_bool(Option,"ScoreWhite") ||
\r
895 colour_is_white(Uci->board->turn)){
\r
896 return Uci->best_score;
\r
898 return -Uci->best_score;
\r
904 static void comp_move(int move) {
\r
909 ASSERT(move_is_ok(move));
\r
911 ASSERT(State->state==THINK);
\r
912 ASSERT(!XB->analyse);
\r
914 if(option_get_bool(Option,"RepeatPV"))
\r
915 send_pv(); // to update time and nodes
\r
919 game_get_board(Game,board);
\r
921 if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
\r
922 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
\r
924 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
\r
927 gui_send(GUI,"move %s",string);
\r
931 if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {
\r
933 if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {
\r
935 State->resign_nb++;
\r
936 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
\r
938 if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
\r
939 my_log("POLYGLOT *** RESIGN ***\n");
\r
940 gui_send(GUI,"resign");
\r
945 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
\r
946 State->resign_nb = 0;
\r
958 static void move_step(int move) {
\r
961 char move_string[256];
\r
963 ASSERT(move_is_ok(move));
\r
967 game_get_board(Game,board);
\r
969 if (move != MoveNone && move_is_legal(move,board)) {
\r
971 move_to_san(move,board,move_string,256);
\r
972 my_log("POLYGLOT MOVE %s\n",move_string);
\r
976 move_to_can(move,board,move_string,256);
\r
977 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
\r
980 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
\r
985 game_add_move(Game,move);
\r
991 static void board_update() {
\r
995 ASSERT(!XB->result);
\r
997 switch (game_status(Game)) {
\r
1001 gui_send(GUI,"1-0 {White mates}");
\r
1004 gui_send(GUI,"0-1 {Black mates}");
\r
1007 gui_send(GUI,"1/2-1/2 {Stalemate}");
\r
1009 case DRAW_MATERIAL:
\r
1010 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
\r
1013 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
\r
1015 case DRAW_REPETITION:
\r
1016 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
\r
1026 static void mess() {
\r
1028 // clear state variables
\r
1030 State->resign_nb = 0;
\r
1031 State->exp_move = MoveNone;
\r
1032 my_timer_reset(State->timer);
\r
1034 // abort a possible search
\r
1038 // calculate the new state
\r
1041 } else if (!active()) {
\r
1042 State->state = WAIT;
\r
1043 my_log("POLYGLOT WAIT\n");
\r
1044 } else if (XB->analyse) {
\r
1045 State->state = ANALYSE;
\r
1046 my_log("POLYGLOT ANALYSE\n");
\r
1047 } else if (State->computer[game_turn(Game)]) {
\r
1048 State->state = THINK;
\r
1049 my_log("POLYGLOT THINK\n");
\r
1051 State->state = WAIT;
\r
1052 my_log("POLYGLOT WAIT\n");
\r
1060 static void no_mess(int move) {
\r
1062 ASSERT(move_is_ok(move));
\r
1064 // just received a move, calculate the new state
\r
1068 } else if (!active()) {
\r
1070 stop_search(); // abort a possible search
\r
1072 State->state = WAIT;
\r
1073 State->exp_move = MoveNone;
\r
1075 my_log("POLYGLOT WAIT\n");
\r
1077 } else if (State->state == WAIT) {
\r
1079 ASSERT(State->computer[game_turn(Game)]);
\r
1080 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1081 ASSERT(!XB->analyse);
\r
1083 my_log("POLYGLOT WAIT -> THINK\n");
\r
1085 State->state = THINK;
\r
1086 State->exp_move = MoveNone;
\r
1088 } else if (State->state == THINK) {
\r
1090 ASSERT(!State->computer[game_turn(Game)]);
\r
1091 ASSERT(State->computer[colour_opp(game_turn(Game))]);
\r
1092 ASSERT(!XB->analyse);
\r
1094 if (ponder() && ponder_ok(Uci->ponder_move)) {
\r
1096 my_log("POLYGLOT THINK -> PONDER\n");
\r
1098 State->state = PONDER;
\r
1099 State->exp_move = Uci->ponder_move;
\r
1103 my_log("POLYGLOT THINK -> WAIT\n");
\r
1105 State->state = WAIT;
\r
1106 State->exp_move = MoveNone;
\r
1109 } else if (State->state == PONDER) {
\r
1111 ASSERT(State->computer[game_turn(Game)]);
\r
1112 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1113 ASSERT(!XB->analyse);
\r
1115 if (move == State->exp_move && Uci->searching) {
\r
1117 ASSERT(Uci->searching);
\r
1118 ASSERT(Uci->pending_nb>=1);
\r
1120 my_timer_start(State->timer);//also resets
\r
1122 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
\r
1123 engine_send(Engine,"ponderhit");
\r
1125 State->state = THINK;
\r
1126 State->exp_move = MoveNone;
\r
1128 send_pv(); // update display
\r
1130 return; // do not launch a new search
\r
1134 my_log("POLYGLOT PONDER -> THINK (miss)\n");
\r
1138 State->state = THINK;
\r
1139 State->exp_move = MoveNone;
\r
1142 } else if (State->state == ANALYSE) {
\r
1144 ASSERT(XB->analyse);
\r
1146 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
\r
1158 // start_protected_command()
\r
1160 static void start_protected_command(){
\r
1164 static void end_protected_command(){
\r
1165 if(Uci->ready){ // not init faze
\r
1166 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
\r
1168 update_remaining_time();
\r
1169 search_update(); // relaunch search if necessary
\r
1172 // update_remaining_time()
\r
1174 static void update_remaining_time(){
\r
1176 if(State->timer->running){
\r
1177 my_timer_stop(State->timer);
\r
1178 reduce = my_timer_elapsed_real(State->timer);
\r
1179 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
\r
1180 XB->my_time -= reduce;
\r
1181 if(XB->my_time<0.0){
\r
1188 // search_update()
\r
1190 static void search_update() {
\r
1196 ASSERT(!Uci->searching);
\r
1201 // launch a new search if needed
\r
1205 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
\r
1207 // [VdB] moved up as we need the move number
\r
1209 game_get_board(Game,Uci->board);
\r
1213 if (State->state == THINK &&
\r
1214 option_get_bool(Option,"Book") &&
\r
1215 Uci->board->move_nb<option_get_int(Option,"BookDepth")
\r
1219 move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));
\r
1221 if (move != MoveNone && move_is_legal(move,Uci->board)) {
\r
1223 my_log("POLYGLOT *BOOK MOVE*\n");
\r
1225 search_clear(); // clears Uci->ponder_move
\r
1226 Uci->best_move = move;
\r
1228 board_copy(board,Uci->board);
\r
1229 move_do(board,move);
\r
1230 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
\r
1232 Uci->best_pv[0] = Uci->best_move;
\r
1233 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
\r
1234 Uci->best_pv[2] = MoveNone;
\r
1236 comp_move(Uci->best_move);
\r
1244 my_log("POLYGLOT START SEARCH\n");
\r
1248 uci_send_option(Uci,"UCI_Chess960","%s",
\r
1249 option_get_bool(Option,"Chess960")?"true":"false");
\r
1251 if (option_get_int(Option,"UCIVersion") >= 2) {
\r
1252 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
\r
1253 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
\r
1256 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
\r
1260 move = (State->state == PONDER) ? State->exp_move : MoveNone;
\r
1261 send_board(move); // updates Uci->board global variable
\r
1265 if (State->state == THINK || State->state == PONDER) {
\r
1267 engine_send_queue(Engine,"go");
\r
1269 if (XB->time_limit) {
\r
1271 // fixed time per move
\r
1273 engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);
\r
1279 if (colour_is_white(Uci->board->turn)) {
\r
1280 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);
\r
1282 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);
\r
1285 if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);
\r
1287 if (XB->mps != 0) {
\r
1289 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
\r
1290 ASSERT(move_nb>=1&&move_nb<=XB->mps);
\r
1292 engine_send_queue(Engine," movestogo %d",move_nb);
\r
1296 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
\r
1298 if (State->state == PONDER) engine_send_queue(Engine," ponder");
\r
1300 engine_send(Engine,""); // newline
\r
1302 } else if (State->state == ANALYSE) {
\r
1304 engine_send(Engine,"go infinite");
\r
1311 // init search info
\r
1313 ASSERT(!Uci->searching);
\r
1317 Uci->searching = TRUE;
\r
1318 Uci->pending_nb++;
\r
1324 static void search_clear() {
\r
1330 my_timer_start(State->timer);//also resets
\r
1335 static bool active() {
\r
1339 if (game_status(Game) != PLAYING) return FALSE; // game ended
\r
1343 if (XB->analyse) return TRUE; // analysing
\r
1344 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
\r
1345 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
\r
1347 return TRUE; // playing
\r
1352 static bool ponder() {
\r
1354 return XB->ponder && (option_get_bool(Option,"CanPonder") ||
\r
1355 option_find(Uci->option,"Ponder"));
\r
1359 static bool ponder_ok(int move) {
\r
1363 ASSERT(move==MoveNone||move_is_ok(move));
\r
1365 // legal ponder move?
\r
1367 if (move == MoveNone) return FALSE;
\r
1369 game_get_board(Game,board);
\r
1370 if (!move_is_legal(move,board)) return FALSE;
\r
1372 // UCI-legal resulting position?
\r
1374 game_add_move(Game,move);
\r
1376 game_get_board(Game,board);
\r
1377 status = game_status(Game);
\r
1379 game_rem_move(Game);
\r
1381 if (status != PLAYING) return FALSE; // game ended
\r
1383 if (option_get_bool(Option,"Book") && is_in_book(board)) {
\r
1392 static void stop_search() {
\r
1394 if (Uci->searching) {
\r
1396 ASSERT(Uci->searching);
\r
1397 ASSERT(Uci->pending_nb>=1);
\r
1399 my_log("POLYGLOT STOP SEARCH\n");
\r
1402 engine_send(Engine,"stop");
\r
1403 Uci->searching = FALSE;
\r
1406 if (option_get_bool(Option,"SyncStop")) {
\r
1407 uci_send_stop_sync(Uci);
\r
1409 uci_send_stop(Uci);
\r
1416 static void send_board(int extra_move) {
\r
1425 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
\r
1427 ASSERT(!Uci->searching);
\r
1431 game_get_board(Game,Uci->board);
\r
1432 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
\r
1434 board_to_fen(Uci->board,fen,256);
\r
1435 my_log("POLYGLOT FEN %s\n",fen);
\r
1437 ASSERT(board_can_play(Uci->board));
\r
1442 end = game_pos(Game);
\r
1443 ASSERT(end>=start);
\r
1447 game_get_board_ex(Game,board,start);
\r
1448 board_to_fen(board,string,256);
\r
1450 engine_send_queue(Engine,"position");
\r
1452 if (my_string_equal(string,StartFen)) {
\r
1453 engine_send_queue(Engine," startpos");
\r
1455 engine_send_queue(Engine," fen %s",string);
\r
1460 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
\r
1462 for (pos = start; pos < end; pos++) { // game moves
\r
1464 move = game_move(Game,pos);
\r
1466 move_to_can(move,board,string,256);
\r
1467 engine_send_queue(Engine," %s",string);
\r
1469 move_do(board,move);
\r
1472 if (extra_move != MoveNone) { // move to ponder on
\r
1473 move_to_can(extra_move,board,string,256);
\r
1474 engine_send_queue(Engine," %s",string);
\r
1479 engine_send(Engine,""); // newline
\r
1484 static void send_info() {
\r
1486 if(option_get_bool(Option,"WbWorkArounds2")){
\r
1487 // Silly bug in some versions of WinBoard.
\r
1488 // depth <=1 clears the engine output window.
\r
1489 // Why shouldn't an engine be allowed to send info at depth 1?
\r
1494 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
\r
1495 0,0,0.0,0,Uci->info);
\r
1500 static void send_pv() {
\r
1502 char pv_string[StringSize];
\r
1505 char move_string[StringSize];
\r
1507 ASSERT(State->state!=WAIT);
\r
1509 if (Uci->best_depth == 0) return;
\r
1511 // xboard search information
\r
1515 if (State->state == THINK || State->state == ANALYSE) {
\r
1517 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1519 if(Uci->depth==-1) //hack to clear the engine output window
\r
1520 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);
\r
1522 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);
\r
1524 } else if (State->state == PONDER &&
\r
1525 option_get_bool(Option,"ShowPonder")) {
\r
1527 game_get_board(Game,board);
\r
1528 move = State->exp_move;
\r
1530 if (move != MoveNone && move_is_legal(move,board)) {
\r
1531 move_to_san(move,board,move_string,256);
\r
1532 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1533 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" (%s) %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,move_string,pv_string);
\r
1540 if ((Uci->searching &&
\r
1541 option_get_bool(Option,"KibitzPV") &&
\r
1542 Uci->time >= option_get_double(Option,"KibitzDelay"))
\r
1543 || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {
\r
1545 if (State->state == THINK || State->state == ANALYSE) {
\r
1547 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1548 if(kibitz_throttle(Uci->searching)){
\r
1549 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"%s\"",option_get_string(Option,"KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,pv_string);
\r
1551 } else if (State->state == PONDER) {
\r
1553 game_get_board(Game,board);
\r
1554 move = State->exp_move;
\r
1556 if (move != MoveNone && move_is_legal(move,board)) {
\r
1557 move_to_san(move,board,move_string,256);
\r
1558 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1559 if(kibitz_throttle(Uci->searching)){
\r
1560 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"(%s) %s\"",option_get_string(Option,"KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,move_string,pv_string);
\r
1567 // kibitz_throttle()
\r
1569 static bool kibitz_throttle(bool searching){
\r
1571 static time_t lastKibitzMove=0;
\r
1572 static time_t lastKibitzPV=0;
\r
1573 curr_time = time(NULL);
\r
1574 if(searching){ // KibitzPV
\r
1576 (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
\r
1577 lastKibitzPV=curr_time;
\r
1580 }else{ // KibitzMove
\r
1582 (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
\r
1583 lastKibitzPV=curr_time;
\r
1584 lastKibitzMove=curr_time;
\r
1593 static void learn(int result) {
\r
1599 ASSERT(result>=-1&&result<=+1);
\r
1601 ASSERT(XB->result);
\r
1602 ASSERT(State->computer[White]||State->computer[Black]);
\r
1609 } else if (State->computer[White]) {
\r
1611 } else if (State->computer[Black]) {
\r
1615 my_fatal("learn(): unknown side\n");
\r
1619 } else if (result > 0) {
\r
1620 my_log("POLYGLOT *LEARN WIN*\n");
\r
1621 } else if (result < 0) {
\r
1622 my_log("POLYGLOT *LEARN LOSS*\n");
\r
1624 my_log("POLYGLOT *LEARN DRAW*\n");
\r
1629 for (; pos < Game->size; pos += 2) {
\r
1631 game_get_board_ex(Game,board,pos);
\r
1632 move = game_move(Game,pos);
\r
1634 book_learn_move(board,move,result);
\r
1640 // end of xboard2uci.c
\r