22 #include "move_do.h"
\r
23 #include "move_legal.h"
\r
28 #include "uci2uci.h"
\r
30 #include "xboard2uci.h"
\r
34 #define StringSize 4096
\r
38 static const bool UseDebug = FALSE;
\r
39 static const bool DelayPong = FALSE;
\r
45 bool computer[ColourNb];
\r
48 my_timer_t timer[1];
\r
52 bool has_feature_memory;
\r
53 bool has_feature_smp;
\r
54 bool has_feature_egt;
\r
59 bool new_hack; // "new" is a C++ keyword
\r
80 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;
\r
84 static state_t State[1];
\r
89 static void comp_move (int move);
\r
90 static void move_step (int move);
\r
91 static void board_update ();
\r
93 static void mess ();
\r
94 static void no_mess (int move);
\r
96 static void search_update ();
\r
97 static void search_clear ();
\r
98 static void update_remaining_time();
\r
99 static int report_best_score();
\r
100 static bool kibitz_throttle (bool searching);
\r
101 static void start_protected_command();
\r
102 static void end_protected_command();
\r
104 static bool active ();
\r
105 static bool ponder ();
\r
106 static bool ponder_ok (int ponder_move);
\r
108 static void stop_search ();
\r
110 static void send_board (int extra_move);
\r
111 static void send_pv ();
\r
112 static void send_info ();
\r
114 static void send_xboard_options ();
\r
116 static void learn (int result);
\r
121 // xboard2uci_init()
\r
123 void xboard2uci_init() {
\r
130 State->state = WAIT;
\r
132 State->computer[White] = FALSE;
\r
133 State->computer[Black] = TRUE;
\r
135 State->exp_move = MoveNone;
\r
136 State->resign_nb = 0;
\r
137 my_timer_reset(State->timer);
\r
139 // yes there are engines that do not have the "Hash" option....
\r
140 XB->has_feature_memory= (option_find(Uci->option,"Hash")!=NULL);
\r
141 XB->has_feature_smp = (uci_thread_option(Uci)!=NULL);
\r
142 // TODO: support for other types of table bases
\r
143 XB->has_feature_egt = (option_find(Uci->option,"NalimovPath")!=NULL);
\r
144 XB->analyse = FALSE;
\r
145 XB->computer = FALSE;
\r
147 my_string_set(&XB->name,"<empty>");
\r
149 XB->new_hack = TRUE;
\r
151 XB->ponder = FALSE;
\r
154 XB->result = FALSE;
\r
160 XB->time_limit = FALSE;
\r
161 XB->time_max = 5.0;
\r
163 XB->depth_limit = FALSE;
\r
164 XB->depth_max = 127;
\r
166 XB->my_time = 300.0;
\r
167 XB->opp_time = 300.0;
\r
170 // xboard2uci_gui_step()
\r
172 void xboard2uci_gui_step(char string[]) {
\r
175 char move_string[256];
\r
180 } else if (match(string,"accepted *")) {
\r
184 } else if (match(string,"analyze")) {
\r
186 State->computer[White] = FALSE;
\r
187 State->computer[Black] = FALSE;
\r
189 XB->analyse = TRUE;
\r
190 XB->new_hack = FALSE;
\r
191 ASSERT(!XB->result);
\r
192 XB->result = FALSE;
\r
196 } else if (match(string,"bk")) {
\r
198 if (option_get_bool(Option,"Book")) {
\r
199 game_get_board(Game,board);
\r
203 } else if (match(string,"black")) {
\r
205 if (colour_is_black(game_turn(Game))) {
\r
207 State->computer[White] = TRUE;
\r
208 State->computer[Black] = FALSE;
\r
210 XB->new_hack = TRUE;
\r
211 XB->result = FALSE;
\r
216 } else if (match(string,"computer")) {
\r
218 XB->computer = TRUE;
\r
220 } else if (match(string,"draw")) {
\r
221 if(option_find(Uci->option,"UCI_DrawOffers")){
\r
222 my_log("POLYGLOT draw from XB received");
\r
223 uci_send_option(Uci,"DrawOffer","%s","draw");}
\r
224 } else if (match(string,"easy")) {
\r
226 XB->ponder = FALSE;
\r
230 } else if (match(string,"edit")) {
\r
234 gui_send(GUI,"Error (unknown command): %s",string);
\r
236 } else if (match(string,"exit")) {
\r
238 State->computer[White] = FALSE;
\r
239 State->computer[Black] = FALSE;
\r
241 XB->analyse = FALSE;
\r
245 } else if (match(string,"force")) {
\r
247 State->computer[White] = FALSE;
\r
248 State->computer[Black] = FALSE;
\r
252 } else if (match(string,"go")) {
\r
254 State->computer[game_turn(Game)] = TRUE;
\r
255 State->computer[colour_opp(game_turn(Game))] = FALSE;
\r
257 XB->new_hack = FALSE;
\r
258 ASSERT(!XB->result);
\r
259 XB->result = FALSE;
\r
263 } else if (match(string,"hard")) {
\r
269 } else if (match(string,"hint")) {
\r
271 if (option_get_bool(Option,"Book")) {
\r
273 game_get_board(Game,board);
\r
274 move = book_move(board,FALSE);
\r
276 if (move != MoveNone && move_is_legal(move,board)) {
\r
277 move_to_san(move,board,move_string,256);
\r
278 gui_send(GUI,"Hint: %s",move_string);
\r
282 } else if (match(string,"ics *")) {
\r
286 } else if (match(string,"level * *:* *")) {
\r
288 XB->mps = atoi(Star[0]);
\r
289 XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));
\r
290 XB->inc = ((double)atoi(Star[3]));
\r
292 } else if (match(string,"level * * *")) {
\r
294 XB->mps = atoi(Star[0]);
\r
295 XB->base = ((double)atoi(Star[1])) * 60.0;
\r
296 XB->inc = ((double)atoi(Star[2]));
\r
298 } else if (match(string,"name *")) {
\r
300 my_string_set(&XB->name,Star[0]);
\r
302 } else if (match(string,"new")) {
\r
304 uci_send_isready(Uci);
\r
305 my_log("POLYGLOT NEW GAME\n");
\r
307 option_set(Option,"Chess960","false");
\r
312 State->computer[White] = FALSE;
\r
313 State->computer[Black] = FALSE;
\r
315 State->computer[White] = FALSE;
\r
316 State->computer[Black] = TRUE;
\r
319 XB->new_hack = TRUE;
\r
320 XB->result = FALSE;
\r
322 XB->depth_limit = FALSE;
\r
324 XB->computer = FALSE;
\r
325 my_string_set(&XB->name,"<empty>");
\r
330 uci_send_ucinewgame(Uci);
\r
332 } else if (match(string,"nopost")) {
\r
336 } else if (match(string,"otim *")) {
\r
338 XB->opp_time = ((double)atoi(Star[0])) / 100.0;
\r
339 if (XB->opp_time < 0.0) XB->opp_time = 0.0;
\r
341 } else if (match(string,"pause")) {
\r
345 gui_send(GUI,"Error (unknown command): %s",string);
\r
347 } else if (match(string,"ping *")) {
\r
349 // HACK; TODO: answer only after an engine move
\r
352 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping
\r
353 XB->ping = atoi(Star[0]);
\r
354 uci_send_isready(Uci);
\r
356 ASSERT(XB->ping==-1);
\r
357 gui_send(GUI,"pong %s",Star[0]);
\r
360 } else if (match(string,"playother")) {
\r
362 State->computer[game_turn(Game)] = FALSE;
\r
363 State->computer[colour_opp(game_turn(Game))] = TRUE;
\r
365 XB->new_hack = FALSE;
\r
366 ASSERT(!XB->result);
\r
367 XB->result = FALSE;
\r
371 } else if (match(string,"post")) {
\r
375 } else if (match(string,"protover *")) {
\r
376 XB->proto_ver = atoi(Star[0]);
\r
377 ASSERT(XB->proto_ver>=2);
\r
378 send_xboard_options();
\r
380 } else if (match(string,"quit")) {
\r
381 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
\r
383 } else if (match(string,"random")) {
\r
387 } else if (match(string,"rating * *")) {
\r
391 } else if (match(string,"remove")) {
\r
393 if (game_pos(Game) >= 2) {
\r
395 game_goto(Game,game_pos(Game)-2);
\r
397 ASSERT(!XB->new_hack);
\r
398 XB->new_hack = FALSE; // HACK?
\r
399 XB->result = FALSE;
\r
405 } else if (match(string,"rejected *")) {
\r
409 } else if (match(string,"reset")) { // protover 3?
\r
413 gui_send(GUI,"Error (unknown command): %s",string);
\r
416 || match(string,"result * {*}")
\r
417 || match(string,"result * {* }")
\r
418 || match(string,"result * { *}")
\r
419 || match(string,"result * { * }")) {
\r
421 my_log("POLYGLOT GAME END\n");
\r
429 if (option_get_bool(Option,"Book") &&
\r
430 option_get_bool(Option,"BookLearn")) {
\r
433 } else if (my_string_equal(Star[0],"1-0")) {
\r
435 } else if (my_string_equal(Star[0],"0-1")) {
\r
437 } else if (my_string_equal(Star[0],"1/2-1/2")) {
\r
441 } else if (match(string,"resume")) {
\r
445 gui_send(GUI,"Error (unknown command): %s",string);
\r
447 } else if (match(string,"option *=*") ||
\r
448 match(string,"option * =*") ||
\r
449 match(string,"option *= *") ||
\r
450 match(string,"option * = *")
\r
452 char *name=Star[0];
\r
453 char *value=Star[1];
\r
454 if(match(name, "Polyglot *")){
\r
455 char *pg_name=Star[0];
\r
456 polyglot_set_option(pg_name,value);
\r
458 option_t *opt=option_find(Uci->option,name);
\r
460 if(my_string_case_equal(opt->type,"check")){
\r
461 value=my_string_equal(value,"1")?"true":"false";
\r
463 start_protected_command();
\r
464 uci_send_option(Uci, name, "%s", value);
\r
465 end_protected_command();
\r
467 gui_send(GUI,"Error (unknown option): %s",name);
\r
470 } else if (match(string,"option *")){
\r
471 char *name=Star[0];
\r
472 if(match(name, "Polyglot *")){
\r
473 char *pg_name=Star[0];
\r
474 polyglot_set_option(pg_name,"<empty>");
\r
476 start_protected_command();
\r
477 // value is ignored
\r
478 if(!uci_send_option(Uci, name, "%s", "<empty>")){
\r
479 gui_send(GUI,"Error (unknown option): %s",name);
\r
481 end_protected_command();
\r
483 } else if (XB->has_feature_smp && match(string,"cores *")){
\r
484 int cores=atoi(Star[0]);
\r
486 // updating the number of cores
\r
487 my_log("POLYGLOT setting the number of cores to %d\n",cores);
\r
488 start_protected_command();
\r
489 uci_set_threads(Uci,cores);
\r
490 end_protected_command();
\r
493 gui_send(GUI,"Error (unknown command): %s",string);
\r
495 } else if (XB->has_feature_egt && match(string,"egtpath * *")){
\r
496 char *type=Star[0];
\r
497 char *path=Star[1];
\r
498 if(!my_string_case_equal(type,"nalimov")){
\r
500 gui_send(GUI,"Error (unsupported table base format): %s",string);
\r
501 }else if(my_string_empty(path)){
\r
503 gui_send(GUI,"Error (unknown command): %s",string);
\r
505 // updating NalimovPath
\r
506 my_log("POLYGLOT setting the Nalimov path to %s\n",path);
\r
507 start_protected_command();
\r
508 uci_send_option(Uci,"NalimovPath","%s",path);
\r
509 end_protected_command();
\r
511 } else if (XB->has_feature_memory && match(string,"memory *")){
\r
512 int memory = atoi(Star[0]);
\r
516 // updating the available memory
\r
518 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);
\r
519 if((opt=option_find(Uci->option,"NalimovCache"))){
\r
520 nalimov_cache=atoi(opt->value);
\r
524 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);
\r
525 real_memory=memory-nalimov_cache;
\r
527 start_protected_command();
\r
528 uci_send_option(Uci,"Hash", "%d", real_memory);
\r
529 end_protected_command();
\r
533 gui_send(GUI,"Error (unknown command): %s",string);
\r
536 } else if (match(string,"sd *")) {
\r
538 XB->depth_limit = TRUE;
\r
539 XB->depth_max = atoi(Star[0]);
\r
541 } else if (match(string,"setboard *")) {
\r
543 my_log("POLYGLOT FEN %s\n",Star[0]);
\r
545 if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);
\r
547 State->computer[White] = FALSE;
\r
548 State->computer[Black] = FALSE;
\r
550 XB->new_hack = TRUE; // HACK?
\r
551 XB->result = FALSE;
\r
556 } else if (match(string,"st *")) {
\r
558 XB->time_limit = TRUE;
\r
559 XB->time_max = ((double)atoi(Star[0]));
\r
561 } else if (match(string,"time *")) {
\r
563 XB->my_time = ((double)atoi(Star[0])) / 100.0;
\r
564 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
566 } else if (match(string,"undo")) {
\r
568 if (game_pos(Game) >= 1) {
\r
570 game_goto(Game,game_pos(Game)-1);
\r
572 ASSERT(!XB->new_hack);
\r
573 XB->new_hack = FALSE; // HACK?
\r
574 XB->result = FALSE;
\r
580 } else if (match(string,"usermove *")) {
\r
582 game_get_board(Game,board);
\r
583 move = move_from_san(Star[0],board);
\r
585 if (move != MoveNone && move_is_legal(move,board)) {
\r
587 XB->new_hack = FALSE;
\r
588 ASSERT(!XB->result);
\r
589 XB->result = FALSE;
\r
596 gui_send(GUI,"Illegal move: %s",Star[0]);
\r
599 } else if (match(string,"variant *")) {
\r
601 if (my_string_equal(Star[0],"fischerandom")) {
\r
602 option_set(Option,"Chess960","true");
\r
604 option_set(Option,"Chess960","false");
\r
607 } else if (match(string,"white")) {
\r
609 if (colour_is_white(game_turn(Game))) {
\r
611 State->computer[White] = FALSE;
\r
612 State->computer[Black] = TRUE;
\r
614 XB->new_hack = TRUE;
\r
615 XB->result = FALSE;
\r
620 } else if (match(string,"xboard")) {
\r
624 } else if (match(string,".")) { // analyse info
\r
626 if (State->state == ANALYSE) {
\r
627 int depth=Uci->best_depth;//HACK: don't clear engine-output window...
\r
629 ASSERT(Uci->searching);
\r
630 ASSERT(Uci->pending_nb>=1);
\r
632 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {
\r
633 move_to_san(Uci->root_move,Uci->board,move_string,256);
\r
634 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
636 gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK
\r
640 } else if (match(string,"?")) { // move now
\r
642 if (State->state == THINK) {
\r
644 ASSERT(Uci->searching);
\r
645 ASSERT(Uci->pending_nb>=1);
\r
647 // HACK: just send "stop" to the engine
\r
649 if (Uci->searching) {
\r
650 my_log("POLYGLOT STOP SEARCH\n");
\r
651 engine_send(Engine,"stop");
\r
655 } else { // unknown command, maybe a move?
\r
657 game_get_board(Game,board);
\r
658 move = move_from_san(string,board);
\r
660 if (move != MoveNone && move_is_legal(move,board)) {
\r
662 XB->new_hack = FALSE;
\r
663 ASSERT(!XB->result);
\r
664 XB->result = FALSE;
\r
669 } else if (move != MoveNone) {
\r
671 gui_send(GUI,"Illegal move: %s",string);
\r
675 gui_send(GUI,"Error (unknown command): %s",string);
\r
681 // xboard2uci_engine_step()
\r
683 void xboard2uci_engine_step(char string[]) {
\r
687 event = uci_parse(Uci,string);
\r
691 if ((event & EVENT_READY) != 0) {
\r
693 // the engine is now ready
\r
697 // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");
\r
700 if (!DelayPong && XB->ping >= 0) {
\r
701 gui_send(GUI,"pong %d",XB->ping);
\r
706 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {
\r
708 // the engine is playing a move
\r
710 // MEGA HACK: estimate remaining time because XBoard won't send it!
\r
712 my_timer_stop(State->timer);
\r
714 XB->my_time -= my_timer_elapsed_real(State->timer);
\r
715 XB->my_time += XB->inc;
\r
716 if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;
\r
718 if (XB->my_time < 0.0) XB->my_time = 0.0;
\r
720 // play the engine move
\r
722 comp_move(Uci->best_move);
\r
725 if ((event & EVENT_PV) != 0) {
\r
727 // the engine has sent a new PV
\r
731 if ((event & EVENT_INFO) != 0) {
\r
733 // the engine has sent info
\r
737 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){
\r
738 my_log("POYGLOT draw offer/resign from engine\n");
\r
739 if(option_find(Uci->option,"UCI_DrawOffers")){
\r
740 if(event & EVENT_DRAW)
\r
741 gui_send(GUI,"offer draw");
\r
743 gui_send(GUI,"resign");
\r
746 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){
\r
747 game_get_board(Game,board);
\r
748 if(board->turn==White){
\r
749 gui_send(GUI,"0-1 {polyglot: resign"
\r
750 " (illegal engine move white)}");
\r
752 gui_send(GUI,"1-0 {polyglot: resign"
\r
753 " (illegal engine move black)}");
\r
761 // format_xboard_option_line
\r
763 void format_xboard_option_line(char * option_line, option_t *opt){
\r
765 char option_string[StringSize];
\r
767 strcpy(option_line,"");
\r
768 // buffer overflow alert
\r
769 strcat(option_line,"feature option=\"");
\r
771 strcat(option_line,"Polyglot ");
\r
773 sprintf(option_string,"%s",opt->name);
\r
774 strcat(option_line,option_string);
\r
775 sprintf(option_string," -%s",opt->type);
\r
776 strcat(option_line,option_string);
\r
777 if(!IS_BUTTON(opt) && strcmp(opt->type,"combo")){
\r
778 if(strcmp(opt->type,"check")){
\r
779 sprintf(option_string," %s",opt->value);
\r
781 sprintf(option_string," %d",
\r
782 my_string_case_equal(opt->value,"true")||
\r
783 my_string_equal(opt->value,"1")
\r
786 strcat(option_line,option_string);
\r
789 sprintf(option_string," %s",opt->min);
\r
790 strcat(option_line,option_string);
\r
793 sprintf(option_string," %s",opt->max);
\r
794 strcat(option_line,option_string);
\r
796 for(j=0;j<opt->var_nb;j++){
\r
797 if(!strcmp(opt->var[j],opt->value)){
\r
798 sprintf(option_string," *%s",opt->var[j]);
\r
800 sprintf(option_string," %s",opt->var[j]);
\r
802 strcat(option_line,option_string);
\r
803 if(j!=opt->var_nb-1){
\r
804 strcat(option_line," ///");
\r
807 strcat(option_line,"\"");
\r
808 if(option_get_bool(Option,"WbWorkArounds") &&
\r
809 (tmp=strstr(option_line,"Draw"))){
\r
811 my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",
\r
816 // send_xboard_options()
\r
818 static void send_xboard_options(){
\r
821 gui_send(GUI,"feature done=0");
\r
823 gui_send(GUI,"feature analyze=1");
\r
824 gui_send(GUI,"feature colors=0");
\r
825 gui_send(GUI,"feature draw=1");
\r
826 gui_send(GUI,"feature ics=1");
\r
827 gui_send(GUI,"feature myname=\"%s\"",
\r
828 option_get_string(Option,"EngineName"));
\r
829 gui_send(GUI,"feature name=1");
\r
830 gui_send(GUI,"feature pause=0");
\r
831 gui_send(GUI,"feature ping=1");
\r
832 gui_send(GUI,"feature playother=1");
\r
833 gui_send(GUI,"feature reuse=1");
\r
834 gui_send(GUI,"feature san=0");
\r
835 gui_send(GUI,"feature setboard=1");
\r
836 gui_send(GUI,"feature sigint=0");
\r
837 gui_send(GUI,"feature sigterm=0");
\r
838 gui_send(GUI,"feature time=1");
\r
839 gui_send(GUI,"feature usermove=1");
\r
840 if (XB->has_feature_memory){
\r
841 gui_send(GUI,"feature memory=1");
\r
843 gui_send(GUI,"feature memory=0");
\r
845 if (XB->has_feature_smp){
\r
846 gui_send(GUI,"feature smp=1");
\r
848 gui_send(GUI,"feature smp=0");
\r
850 if (XB->has_feature_egt){
\r
851 // TODO: support for other types of table bases
\r
852 gui_send(GUI,"feature egt=\"nalimov\"");
\r
854 gui_send(GUI,"feature egt=\"\"");
\r
857 if (option_find(Uci->option,"UCI_Chess960")) {
\r
858 gui_send(GUI,"feature variants=\"normal,fischerandom\"");
\r
860 gui_send(GUI,"feature variants=\"normal\"");
\r
863 xboard2uci_send_options();
\r
866 void xboard2uci_send_options(){
\r
867 char option_line[StringSize]="";
\r
871 option_start_iter(Uci->option);
\r
872 while((opt=option_next(Uci->option))){
\r
873 if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;
\r
874 if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;
\r
875 if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;
\r
876 if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;
\r
877 if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;
\r
878 if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;
\r
879 if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;
\r
880 if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;
\r
881 if(my_string_case_equal(opt->name,"Ponder")) continue;
\r
882 if(my_string_case_equal(opt->name,"Hash")) continue;
\r
883 if(my_string_case_equal(opt->name,"NalimovPath")) continue;
\r
884 if((name=uci_thread_option(Uci))!=NULL &&
\r
885 my_string_case_equal(opt->name,name)) continue;
\r
886 format_xboard_option_line(option_line,opt);
\r
888 gui_send(GUI,"%s",option_line);
\r
892 option_start_iter(Option);
\r
893 while((opt=option_next(Option))){
\r
894 if(opt->mode &XBOARD){
\r
895 format_xboard_option_line(option_line,opt);
\r
896 gui_send(GUI,"%s",option_line);
\r
899 gui_send(GUI,"feature done=1");
\r
903 // report_best_score()
\r
905 static int report_best_score(){
\r
906 if(!option_get_bool(Option,"ScoreWhite") ||
\r
907 colour_is_white(Uci->board->turn)){
\r
908 return Uci->best_score;
\r
910 return -Uci->best_score;
\r
916 static void comp_move(int move) {
\r
921 ASSERT(move_is_ok(move));
\r
923 ASSERT(State->state==THINK);
\r
924 ASSERT(!XB->analyse);
\r
926 if(option_get_bool(Option,"RepeatPV"))
\r
927 send_pv(); // to update time and nodes
\r
931 game_get_board(Game,board);
\r
933 if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
\r
934 if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
\r
936 if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
\r
939 gui_send(GUI,"move %s",string);
\r
943 if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {
\r
945 if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {
\r
947 State->resign_nb++;
\r
948 my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
\r
950 if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
\r
951 my_log("POLYGLOT *** RESIGN ***\n");
\r
952 gui_send(GUI,"resign");
\r
957 if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
\r
958 State->resign_nb = 0;
\r
970 static void move_step(int move) {
\r
973 char move_string[256];
\r
975 ASSERT(move_is_ok(move));
\r
979 game_get_board(Game,board);
\r
981 if (move != MoveNone && move_is_legal(move,board)) {
\r
983 move_to_san(move,board,move_string,256);
\r
984 my_log("POLYGLOT MOVE %s\n",move_string);
\r
988 move_to_can(move,board,move_string,256);
\r
989 my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);
\r
992 my_fatal("move_step(): illegal move \"%s\"\n",move_string);
\r
997 game_add_move(Game,move);
\r
1003 static void board_update() {
\r
1005 // handle game end
\r
1007 ASSERT(!XB->result);
\r
1009 switch (game_status(Game)) {
\r
1013 gui_send(GUI,"1-0 {White mates}");
\r
1016 gui_send(GUI,"0-1 {Black mates}");
\r
1019 gui_send(GUI,"1/2-1/2 {Stalemate}");
\r
1021 case DRAW_MATERIAL:
\r
1022 gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");
\r
1025 gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");
\r
1027 case DRAW_REPETITION:
\r
1028 gui_send(GUI,"1/2-1/2 {Draw by repetition}");
\r
1038 static void mess() {
\r
1040 // clear state variables
\r
1042 State->resign_nb = 0;
\r
1043 State->exp_move = MoveNone;
\r
1044 my_timer_reset(State->timer);
\r
1046 // abort a possible search
\r
1050 // calculate the new state
\r
1053 } else if (!active()) {
\r
1054 State->state = WAIT;
\r
1055 my_log("POLYGLOT WAIT\n");
\r
1056 } else if (XB->analyse) {
\r
1057 State->state = ANALYSE;
\r
1058 my_log("POLYGLOT ANALYSE\n");
\r
1059 } else if (State->computer[game_turn(Game)]) {
\r
1060 State->state = THINK;
\r
1061 my_log("POLYGLOT THINK\n");
\r
1063 State->state = WAIT;
\r
1064 my_log("POLYGLOT WAIT\n");
\r
1072 static void no_mess(int move) {
\r
1074 ASSERT(move_is_ok(move));
\r
1076 // just received a move, calculate the new state
\r
1080 } else if (!active()) {
\r
1082 stop_search(); // abort a possible search
\r
1084 State->state = WAIT;
\r
1085 State->exp_move = MoveNone;
\r
1087 my_log("POLYGLOT WAIT\n");
\r
1089 } else if (State->state == WAIT) {
\r
1091 ASSERT(State->computer[game_turn(Game)]);
\r
1092 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1093 ASSERT(!XB->analyse);
\r
1095 my_log("POLYGLOT WAIT -> THINK\n");
\r
1097 State->state = THINK;
\r
1098 State->exp_move = MoveNone;
\r
1100 } else if (State->state == THINK) {
\r
1102 ASSERT(!State->computer[game_turn(Game)]);
\r
1103 ASSERT(State->computer[colour_opp(game_turn(Game))]);
\r
1104 ASSERT(!XB->analyse);
\r
1106 if (ponder() && ponder_ok(Uci->ponder_move)) {
\r
1108 my_log("POLYGLOT THINK -> PONDER\n");
\r
1110 State->state = PONDER;
\r
1111 State->exp_move = Uci->ponder_move;
\r
1115 my_log("POLYGLOT THINK -> WAIT\n");
\r
1117 State->state = WAIT;
\r
1118 State->exp_move = MoveNone;
\r
1121 } else if (State->state == PONDER) {
\r
1123 ASSERT(State->computer[game_turn(Game)]);
\r
1124 ASSERT(!State->computer[colour_opp(game_turn(Game))]);
\r
1125 ASSERT(!XB->analyse);
\r
1127 if (move == State->exp_move && Uci->searching) {
\r
1129 ASSERT(Uci->searching);
\r
1130 ASSERT(Uci->pending_nb>=1);
\r
1132 my_timer_start(State->timer);//also resets
\r
1134 my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");
\r
1135 engine_send(Engine,"ponderhit");
\r
1137 State->state = THINK;
\r
1138 State->exp_move = MoveNone;
\r
1140 send_pv(); // update display
\r
1142 return; // do not launch a new search
\r
1146 my_log("POLYGLOT PONDER -> THINK (miss)\n");
\r
1150 State->state = THINK;
\r
1151 State->exp_move = MoveNone;
\r
1154 } else if (State->state == ANALYSE) {
\r
1156 ASSERT(XB->analyse);
\r
1158 my_log("POLYGLOT ANALYSE -> ANALYSE\n");
\r
1170 // start_protected_command()
\r
1172 static void start_protected_command(){
\r
1176 static void end_protected_command(){
\r
1177 if(Uci->ready){ // not init faze
\r
1178 uci_send_isready_sync(Uci); // gobble up spurious "bestmove"
\r
1180 update_remaining_time();
\r
1181 search_update(); // relaunch search if necessary
\r
1184 // update_remaining_time()
\r
1186 static void update_remaining_time(){
\r
1188 if(State->timer->running){
\r
1189 my_timer_stop(State->timer);
\r
1190 reduce = my_timer_elapsed_real(State->timer);
\r
1191 my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);
\r
1192 XB->my_time -= reduce;
\r
1193 if(XB->my_time<0.0){
\r
1200 // search_update()
\r
1202 static void search_update() {
\r
1208 ASSERT(!Uci->searching);
\r
1213 // launch a new search if needed
\r
1217 if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {
\r
1219 // [VdB] moved up as we need the move number
\r
1221 game_get_board(Game,Uci->board);
\r
1225 if (State->state == THINK &&
\r
1226 option_get_bool(Option,"Book") &&
\r
1227 Uci->board->move_nb<option_get_int(Option,"BookDepth")
\r
1231 move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));
\r
1233 if (move != MoveNone && move_is_legal(move,Uci->board)) {
\r
1235 my_log("POLYGLOT *BOOK MOVE*\n");
\r
1237 search_clear(); // clears Uci->ponder_move
\r
1238 Uci->best_move = move;
\r
1240 board_copy(board,Uci->board);
\r
1241 move_do(board,move);
\r
1242 Uci->ponder_move = book_move(board,FALSE); // expected move = best book move
\r
1244 Uci->best_pv[0] = Uci->best_move;
\r
1245 Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
\r
1246 Uci->best_pv[2] = MoveNone;
\r
1248 comp_move(Uci->best_move);
\r
1256 my_log("POLYGLOT START SEARCH\n");
\r
1260 uci_send_option(Uci,"UCI_Chess960","%s",
\r
1261 option_get_bool(Option,"Chess960")?"true":"false");
\r
1263 if (option_get_int(Option,"UCIVersion") >= 2) {
\r
1264 uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
\r
1265 uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
\r
1268 uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");
\r
1272 move = (State->state == PONDER) ? State->exp_move : MoveNone;
\r
1273 send_board(move); // updates Uci->board global variable
\r
1277 if (State->state == THINK || State->state == PONDER) {
\r
1279 engine_send_queue(Engine,"go");
\r
1281 if (XB->time_limit) {
\r
1283 // fixed time per move
\r
1285 engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);
\r
1291 if (colour_is_white(Uci->board->turn)) {
\r
1292 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);
\r
1294 engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);
\r
1297 if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);
\r
1299 if (XB->mps != 0) {
\r
1301 move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
\r
1302 ASSERT(move_nb>=1&&move_nb<=XB->mps);
\r
1304 engine_send_queue(Engine," movestogo %d",move_nb);
\r
1308 if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);
\r
1310 if (State->state == PONDER) engine_send_queue(Engine," ponder");
\r
1312 engine_send(Engine,""); // newline
\r
1314 } else if (State->state == ANALYSE) {
\r
1316 engine_send(Engine,"go infinite");
\r
1323 // init search info
\r
1325 ASSERT(!Uci->searching);
\r
1329 Uci->searching = TRUE;
\r
1330 Uci->pending_nb++;
\r
1336 static void search_clear() {
\r
1342 my_timer_start(State->timer);//also resets
\r
1347 static bool active() {
\r
1351 if (game_status(Game) != PLAYING) return FALSE; // game ended
\r
1355 if (XB->analyse) return TRUE; // analysing
\r
1356 if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode
\r
1357 if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game
\r
1359 return TRUE; // playing
\r
1364 static bool ponder() {
\r
1366 return XB->ponder && (option_get_bool(Option,"CanPonder") ||
\r
1367 option_find(Uci->option,"Ponder"));
\r
1371 static bool ponder_ok(int move) {
\r
1375 ASSERT(move==MoveNone||move_is_ok(move));
\r
1377 // legal ponder move?
\r
1379 if (move == MoveNone) return FALSE;
\r
1381 game_get_board(Game,board);
\r
1382 if (!move_is_legal(move,board)) return FALSE;
\r
1384 // UCI-legal resulting position?
\r
1386 game_add_move(Game,move);
\r
1388 game_get_board(Game,board);
\r
1389 status = game_status(Game);
\r
1391 game_rem_move(Game);
\r
1393 if (status != PLAYING) return FALSE; // game ended
\r
1395 if (option_get_bool(Option,"Book") && is_in_book(board)) {
\r
1404 static void stop_search() {
\r
1406 if (Uci->searching) {
\r
1408 ASSERT(Uci->searching);
\r
1409 ASSERT(Uci->pending_nb>=1);
\r
1411 my_log("POLYGLOT STOP SEARCH\n");
\r
1414 engine_send(Engine,"stop");
\r
1415 Uci->searching = FALSE;
\r
1418 if (option_get_bool(Option,"SyncStop")) {
\r
1419 uci_send_stop_sync(Uci);
\r
1421 uci_send_stop(Uci);
\r
1428 static void send_board(int extra_move) {
\r
1437 ASSERT(extra_move==MoveNone||move_is_ok(extra_move));
\r
1439 ASSERT(!Uci->searching);
\r
1443 game_get_board(Game,Uci->board);
\r
1444 if (extra_move != MoveNone) move_do(Uci->board,extra_move);
\r
1446 board_to_fen(Uci->board,fen,256);
\r
1447 my_log("POLYGLOT FEN %s\n",fen);
\r
1449 ASSERT(board_can_play(Uci->board));
\r
1454 end = game_pos(Game);
\r
1455 ASSERT(end>=start);
\r
1459 game_get_board_ex(Game,board,start);
\r
1460 board_to_fen(board,string,256);
\r
1462 engine_send_queue(Engine,"position");
\r
1464 if (my_string_equal(string,StartFen)) {
\r
1465 engine_send_queue(Engine," startpos");
\r
1467 engine_send_queue(Engine," fen %s",string);
\r
1472 if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");
\r
1474 for (pos = start; pos < end; pos++) { // game moves
\r
1476 move = game_move(Game,pos);
\r
1478 move_to_can(move,board,string,256);
\r
1479 engine_send_queue(Engine," %s",string);
\r
1481 move_do(board,move);
\r
1484 if (extra_move != MoveNone) { // move to ponder on
\r
1485 move_to_can(extra_move,board,string,256);
\r
1486 engine_send_queue(Engine," %s",string);
\r
1491 engine_send(Engine,""); // newline
\r
1496 static void send_info() {
\r
1498 if(option_get_bool(Option,"WbWorkArounds2")){
\r
1499 // Silly bug in some versions of WinBoard.
\r
1500 // depth <=1 clears the engine output window.
\r
1501 // Why shouldn't an engine be allowed to send info at depth 1?
\r
1506 gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
\r
1507 0,0,0.0,0,Uci->info);
\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,report_best_score(),Uci->time*100.0,Uci->node_nb);
\r
1534 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
1536 } else if (State->state == PONDER &&
\r
1537 option_get_bool(Option,"ShowPonder")) {
\r
1539 game_get_board(Game,board);
\r
1540 move = State->exp_move;
\r
1542 if (move != MoveNone && move_is_legal(move,board)) {
\r
1543 move_to_san(move,board,move_string,256);
\r
1544 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1545 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
1552 if ((Uci->searching &&
\r
1553 option_get_bool(Option,"KibitzPV") &&
\r
1554 Uci->time >= option_get_double(Option,"KibitzDelay"))
\r
1555 || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {
\r
1557 if (State->state == THINK || State->state == ANALYSE) {
\r
1559 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1560 if(kibitz_throttle(Uci->searching)){
\r
1561 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
1563 } else if (State->state == PONDER) {
\r
1565 game_get_board(Game,board);
\r
1566 move = State->exp_move;
\r
1568 if (move != MoveNone && move_is_legal(move,board)) {
\r
1569 move_to_san(move,board,move_string,256);
\r
1570 line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);
\r
1571 if(kibitz_throttle(Uci->searching)){
\r
1572 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
1579 // kibitz_throttle()
\r
1581 static bool kibitz_throttle(bool searching){
\r
1583 static time_t lastKibitzMove=0;
\r
1584 static time_t lastKibitzPV=0;
\r
1585 curr_time = time(NULL);
\r
1586 if(searching){ // KibitzPV
\r
1588 (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
\r
1589 lastKibitzPV=curr_time;
\r
1592 }else{ // KibitzMove
\r
1594 (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
\r
1595 lastKibitzPV=curr_time;
\r
1596 lastKibitzMove=curr_time;
\r
1605 static void learn(int result) {
\r
1611 ASSERT(result>=-1&&result<=+1);
\r
1613 ASSERT(XB->result);
\r
1614 ASSERT(State->computer[White]||State->computer[Black]);
\r
1621 } else if (State->computer[White]) {
\r
1623 } else if (State->computer[Black]) {
\r
1627 my_fatal("learn(): unknown side\n");
\r
1631 } else if (result > 0) {
\r
1632 my_log("POLYGLOT *LEARN WIN*\n");
\r
1633 } else if (result < 0) {
\r
1634 my_log("POLYGLOT *LEARN LOSS*\n");
\r
1636 my_log("POLYGLOT *LEARN DRAW*\n");
\r
1641 for (; pos < Game->size; pos += 2) {
\r
1643 game_get_board_ex(Game,board,pos);
\r
1644 move = game_move(Game,pos);
\r
1646 book_learn_move(board,move,result);
\r
1652 // end of xboard2uci.c
\r