15 #include "move_do.h"
\r
16 #include "move_legal.h"
\r
25 static const bool UseDebug = FALSE;
\r
27 #define StringSize ((int)4096)
\r
33 // Hopefully the following confusion is temporary
\r
34 // Normally we should check for the engine name but this is a hack anyway
\r
35 // Some of there where provided by Marc Lacrosse
\r
37 const char * thread_options[]={
\r
38 "number of threads", // toga
\r
39 "number threads", // Deep Learning Toga
\r
40 "threads", // glaurung, zappa, cyclone, grapefruit,
\r
41 // Deep Shredder, Deep Junior, bright
\r
42 "core threads", // HIARCS
\r
43 "max cpus", // rybka
\r
44 "cpus", // Deep Sjeng, Fruit2.3.5
\r
45 "maxthreads", // Naum
\r
51 static bool uci_is_ok (const uci_t * uci);
\r
53 static int parse_bestmove (uci_t * uci, const char string[]);
\r
54 static void parse_id (uci_t * uci, const char string[]);
\r
55 static int parse_info (uci_t * uci, const char string[]);
\r
56 static void parse_option (uci_t * uci, const char string[]);
\r
57 static void parse_score (uci_t * uci, const char string[]);
\r
59 static int mate_score (int dist);
\r
66 static void apply_UCI3_heuristics(option_t *opt){
\r
67 if(option_get_int(Option,"UCIVersion")>2){
\r
70 if(!my_string_equal(opt->type,"string")){
\r
73 if(!strncmp(opt->name,"UCI_",4)){
\r
76 if(my_string_case_contains(opt->name,"file")){
\r
77 my_string_set(&opt->type,"file");
\r
80 if(my_string_case_contains(opt->name,"path")){
\r
81 my_string_set(&opt->type,"path");
\r
86 // uci_set_threads()
\r
88 void uci_set_threads(uci_t * uci, int n) {
\r
89 const char *thread_option=uci_thread_option(uci);
\r
92 uci_send_option(uci,thread_option,"%d",n);
\r
97 const char * uci_thread_option(uci_t * uci){
\r
98 const char **p = thread_options;
\r
99 const char *thread_option;
\r
101 while((thread_option = *(p++))){
\r
102 if((opt=option_find(uci->option,thread_option))){
\r
112 static bool uci_is_ok(const uci_t * uci) {
\r
114 if (uci == NULL) return FALSE;
\r
115 if (uci->engine == NULL) return FALSE;
\r
116 if (!option_is_ok(uci->option)) return FALSE;
\r
122 void uci_open(uci_t * uci, engine_t * engine) {
\r
124 char string[StringSize];
\r
128 ASSERT(engine!=NULL);
\r
132 uci->engine = engine;
\r
135 my_string_set(&uci->name,"<empty>");
\r
136 uci->author = NULL;
\r
137 my_string_set(&uci->author,"<empty>");
\r
138 option_init(uci->option);
\r
141 uci->searching = 0;
\r
142 uci->pending_nb = 0;
\r
143 uci->multipv_mode = FALSE;
\r
144 board_start(uci->board);
\r
147 // send "uci" and wait for "uciok"
\r
149 engine_send(uci->engine,"uci");
\r
152 engine_get(uci->engine,string);
\r
153 // Handle the case that the engine is really a WB engine somewhat gracefully.
\r
154 if((strstr(string,"Illegal") || strstr(string,"Error"))
\r
155 &&strstr(string,"uci")){
\r
156 my_fatal("uci_open(): Not an UCI engine (not found).\n");
\r
158 event = uci_parse(uci,string);
\r
159 } while (!engine_eof(Engine) && (event & EVENT_UCI) == 0);
\r
164 void uci_close(uci_t * uci) {
\r
166 ASSERT(uci_is_ok(uci));
\r
167 engine_close(uci->engine);
\r
168 uci->engine = NULL;
\r
169 my_string_clear(&uci->name);
\r
170 my_string_clear(&uci->author);
\r
172 option_clear(uci->option);
\r
177 void uci_clear(uci_t * uci) {
\r
179 ASSERT(uci_is_ok(uci));
\r
181 ASSERT(!uci->searching);
\r
183 uci->best_move = MoveNone;
\r
184 uci->ponder_move = MoveNone;
\r
188 uci->sel_depth = 0;
\r
189 line_clear(uci->pv);
\r
191 uci->best_score = 0;
\r
192 uci->best_depth = 0;
\r
193 uci->best_sel_depth = 0;
\r
194 line_clear(uci->best_pv);
\r
195 // make the default 1 instead of 0 so that info lines can be recognized by their node number 0
\r
201 line_clear(uci->current_line);
\r
203 uci->root_move = MoveNone;
\r
204 uci->root_move_pos = 0;
\r
205 uci->root_move_nb = board_mobility(uci->board);
\r
208 // uci_send_isready()
\r
210 void uci_send_isready(uci_t * uci) {
\r
214 engine_send(uci->engine,"isready");
\r
218 // uci_send_isready_sync()
\r
220 void uci_send_isready_sync(uci_t * uci) {
\r
222 char string[StringSize];
\r
225 ASSERT(uci_is_ok(uci));
\r
227 // send "isready" and wait for "readyok"
\r
229 uci_send_isready(uci);
\r
232 engine_get(uci->engine,string);
\r
233 event = uci_parse(uci,string);
\r
234 } while (!engine_eof(Engine) && (event & EVENT_READY) == 0);
\r
239 void uci_send_stop(uci_t * uci) {
\r
241 ASSERT(uci_is_ok(uci));
\r
243 ASSERT(uci->searching);
\r
244 ASSERT(uci->pending_nb>=1);
\r
246 engine_send(Engine,"stop");
\r
247 uci->searching = FALSE;
\r
250 // uci_send_stop_sync()
\r
252 void uci_send_stop_sync(uci_t * uci) {
\r
254 char string[StringSize];
\r
257 ASSERT(uci_is_ok(uci));
\r
259 ASSERT(uci->searching);
\r
260 ASSERT(uci->pending_nb>=1);
\r
262 // send "stop" and wait for "bestmove"
\r
264 uci_send_stop(uci);
\r
267 engine_get(uci->engine,string);
\r
268 event = uci_parse(uci,string);
\r
269 } while (!engine_eof(Engine) && (event & EVENT_STOP) == 0);
\r
272 // uci_send_ucinewgame()
\r
274 void uci_send_ucinewgame(uci_t * uci) {
\r
278 if (option_get_int(Option,"UCIVersion") >= 2) {
\r
279 engine_send(uci->engine,"ucinewgame");
\r
283 // uci_send_option()
\r
285 bool uci_send_option(uci_t * uci, const char option[], const char format[], ...) {
\r
287 char value[FormatBufferSize];
\r
291 ASSERT(uci_is_ok(uci));
\r
292 ASSERT(option!=NULL);
\r
293 ASSERT(format!=NULL);
\r
297 CONSTRUCT_ARG_STRING(format,value);
\r
299 if (UseDebug) my_log("POLYGLOT OPTION %s VALUE %s\n",option,value);
\r
301 opt=option_find(uci->option,option);
\r
304 if(!IS_BUTTON(opt->type)){
\r
305 if(!my_string_equal(opt->value,value)){
\r
306 engine_send(uci->engine,"setoption name %s value %s",
\r
308 my_string_set(&opt->value,value);
\r
310 my_log("POLYGLOT Not sending option \"%s\" since it "
\r
311 "already has the correct value.\n",opt->name);
\r
314 engine_send(uci->engine,"setoption name %s",opt->name);
\r
322 int uci_parse(uci_t * uci, const char string[]) {
\r
326 char command[StringSize];
\r
327 char argument[StringSize];
\r
329 ASSERT(uci_is_ok(uci));
\r
330 ASSERT(string!=NULL);
\r
334 event = EVENT_NONE;
\r
338 parse_open(parse,string);
\r
340 if (parse_get_word(parse,command,StringSize)) {
\r
342 parse_get_string(parse,argument,StringSize);
\r
343 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" ARGUMENT \"%s\"\n",command,argument);
\r
347 } else if (my_string_equal(command,"bestmove")) {
\r
351 ASSERT(uci->pending_nb>0);
\r
353 if (uci->searching && uci->pending_nb == 1) {
\r
357 uci->searching = FALSE;
\r
360 event = parse_bestmove(uci,argument); // updates uci->best_move and uci->ponder_move
\r
366 if (uci->pending_nb > 0) {
\r
368 if (uci->pending_nb == 0) event = EVENT_STOP;
\r
372 } else if (my_string_equal(command,"id")) {
\r
374 parse_id(uci,argument);
\r
376 } else if (my_string_equal(command,"info")) {
\r
378 // search information
\r
380 if (uci->searching && uci->pending_nb == 1) { // current search
\r
381 event = parse_info(uci,argument);
\r
384 } else if (my_string_equal(command,"option")) {
\r
386 parse_option(uci,argument);
\r
388 } else if (my_string_equal(command,"readyok")) {
\r
392 ASSERT(uci->ready_nb>0);
\r
394 if (uci->ready_nb > 0) {
\r
396 if (uci->ready_nb == 0) event = EVENT_READY;
\r
399 } else if (my_string_equal(command,"uciok")) {
\r
405 if (UseDebug) my_log("POLYGLOT unknown command \"%s\"\n",command);
\r
409 parse_close(parse);
\r
414 // parse_bestmove()
\r
416 static int parse_bestmove(uci_t * uci, const char string[]) {
\r
419 char command[StringSize];
\r
420 char option[StringSize];
\r
421 char argument[StringSize];
\r
424 ASSERT(uci_is_ok(uci));
\r
425 ASSERT(string!=NULL);
\r
429 strcpy(command,"bestmove");
\r
431 parse_open(parse,string);
\r
432 parse_add_keyword(parse,"ponder");
\r
436 if (!parse_get_string(parse,argument,StringSize)) {
\r
437 return EVENT_ILLEGAL_MOVE;
\r
438 // my_fatal("parse_bestmove(): missing argument\n");
\r
441 uci->best_move = move_from_can(argument,uci->board);
\r
442 if (uci->best_move == MoveNone) {
\r
443 return EVENT_ILLEGAL_MOVE;
\r
444 // my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
447 if(!move_is_legal(uci->best_move,uci->board)){
\r
448 return EVENT_ILLEGAL_MOVE;
\r
450 ASSERT(uci->best_move!=MoveNone);
\r
451 ASSERT(move_is_legal(uci->best_move,uci->board));
\r
455 while (parse_get_word(parse,option,StringSize)) {
\r
457 parse_get_string(parse,argument,StringSize);
\r
459 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
463 } else if (my_string_equal(option,"ponder")) {
\r
465 ASSERT(!my_string_empty(argument));
\r
467 board_copy(board,uci->board);
\r
468 move_do(board,uci->best_move);
\r
470 uci->ponder_move = move_from_can(argument,board);
\r
471 // if (uci->ponder_move == MoveNone) my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
473 ASSERT(uci->ponder_move!=MoveNone);
\r
474 ASSERT(move_is_legal(uci->ponder_move,board));
\r
478 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
482 parse_close(parse);
\r
489 static void parse_id(uci_t * uci, const char string[]) {
\r
492 char command[StringSize];
\r
493 char option[StringSize];
\r
494 char argument[StringSize];
\r
497 ASSERT(string!=NULL);
\r
501 strcpy(command,"id");
\r
503 parse_open(parse,string);
\r
504 parse_add_keyword(parse,"author");
\r
505 parse_add_keyword(parse,"name");
\r
509 while (parse_get_word(parse,option,StringSize)) {
\r
511 parse_get_string(parse,argument,StringSize);
\r
512 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
515 } else if (my_string_equal(option,"author")) {
\r
516 ASSERT(!my_string_empty(argument));
\r
517 my_string_set(&uci->author,argument);
\r
518 } else if (my_string_equal(option,"name")) {
\r
519 ASSERT(!my_string_empty(argument));
\r
520 my_string_set(&uci->name,argument);
\r
522 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
526 parse_close(parse);
\r
528 if (UseDebug) my_log("POLYGLOT engine name \"%s\" author \"%s\"\n",uci->name,uci->author);
\r
533 static int parse_info(uci_t * uci, const char string[]) {
\r
537 char command[StringSize];
\r
538 char option[StringSize];
\r
539 char argument[StringSize];
\r
546 ASSERT(uci_is_ok(uci));
\r
547 ASSERT(string!=NULL);
\r
551 event = EVENT_NONE;
\r
553 strcpy(command,"info");
\r
555 parse_open(parse,string);
\r
556 parse_add_keyword(parse,"cpuload");
\r
557 parse_add_keyword(parse,"currline");
\r
558 parse_add_keyword(parse,"currmove");
\r
559 parse_add_keyword(parse,"currmovenumber");
\r
560 parse_add_keyword(parse,"depth");
\r
561 parse_add_keyword(parse,"hashfull");
\r
562 parse_add_keyword(parse,"multipv");
\r
563 parse_add_keyword(parse,"nodes");
\r
564 parse_add_keyword(parse,"nps");
\r
565 parse_add_keyword(parse,"pv");
\r
566 parse_add_keyword(parse,"refutation");
\r
567 parse_add_keyword(parse,"score");
\r
568 parse_add_keyword(parse,"seldepth");
\r
569 parse_add_keyword(parse,"string");
\r
570 parse_add_keyword(parse,"tbhits");
\r
571 parse_add_keyword(parse,"time");
\r
575 while (parse_get_word(parse,option,StringSize)) {
\r
577 parse_get_string(parse,argument,StringSize);
\r
579 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
583 } else if (my_string_equal(option,"cpuload")) {
\r
585 ASSERT(!my_string_empty(argument));
\r
587 n = atoi(argument);
\r
590 if (n >= 0) uci->cpu = ((double)n) / 1000.0;
\r
592 } else if (my_string_equal(option,"currline")) {
\r
594 ASSERT(!my_string_empty(argument));
\r
596 line_from_can(uci->current_line,uci->board,argument,LineSize);
\r
598 } else if (my_string_equal(option,"currmove")) {
\r
600 ASSERT(!my_string_empty(argument));
\r
602 uci->root_move = move_from_can(argument,uci->board);
\r
603 ASSERT(uci->root_move!=MoveNone);
\r
605 } else if (my_string_equal(option,"currmovenumber")) {
\r
607 ASSERT(!my_string_empty(argument));
\r
609 n = atoi(argument);
\r
610 ASSERT(n>=1&&n<=uci->root_move_nb);
\r
612 if (n >= 1 && n <= uci->root_move_nb) {
\r
613 uci->root_move_pos = n - 1;
\r
614 ASSERT(uci->root_move_pos>=0&&uci->root_move_pos<uci->root_move_nb);
\r
617 } else if (my_string_equal(option,"depth")) {
\r
619 ASSERT(!my_string_empty(argument));
\r
621 n = atoi(argument);
\r
625 if (n > uci->depth) event |= EVENT_DEPTH;
\r
629 } else if (my_string_equal(option,"hashfull")) {
\r
631 ASSERT(!my_string_empty(argument));
\r
633 n = atoi(argument);
\r
636 if (n >= 0) uci->hash = ((double)n) / 1000.0;
\r
638 } else if (my_string_equal(option,"multipv")) {
\r
640 ASSERT(!my_string_empty(argument));
\r
642 n = atoi(argument);
\r
643 if(Uci->multipv_mode) multipvline=n;
\r
647 } else if (my_string_equal(option,"nodes")) {
\r
649 ASSERT(!my_string_empty(argument));
\r
651 ln = my_atoll(argument);
\r
654 if (ln >= 0) uci->node_nb = ln;
\r
656 } else if (my_string_equal(option,"nps")) {
\r
658 ASSERT(!my_string_empty(argument));
\r
660 n = atoi(argument);
\r
663 if (n >= 0) uci->speed = ((double)n);
\r
665 } else if (my_string_equal(option,"pv")) {
\r
667 ASSERT(!my_string_empty(argument));
\r
669 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
672 } else if (my_string_equal(option,"refutation")) {
\r
674 ASSERT(!my_string_empty(argument));
\r
676 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
678 } else if (my_string_equal(option,"score")) {
\r
680 ASSERT(!my_string_empty(argument));
\r
682 parse_score(uci,argument);
\r
684 } else if (my_string_equal(option,"seldepth")) {
\r
686 ASSERT(!my_string_empty(argument));
\r
688 n = atoi(argument);
\r
691 if (n >= 0) uci->sel_depth = n;
\r
693 } else if (my_string_equal(option,"string")) {
\r
694 if(my_string_case_equal(argument,"DrawOffer")){
\r
695 event |= EVENT_DRAW;
\r
696 }else if(my_string_case_equal(argument,"Resign")){
\r
697 event |= EVENT_RESIGN;
\r
699 snprintf(uci->info,sizeof(uci->info),"%s",argument);
\r
700 uci->info[sizeof(uci->info)-1]='\0';
\r
703 // TODO: argument to EOS
\r
705 ASSERT(!my_string_empty(argument));
\r
707 } else if (my_string_equal(option,"tbhits")) {
\r
709 ASSERT(!my_string_empty(argument));
\r
711 ln = my_atoll(argument);
\r
714 } else if (my_string_equal(option,"time")) {
\r
716 ASSERT(!my_string_empty(argument));
\r
718 n = atoi(argument);
\r
721 if (n >= 0) uci->time = ((double)n) / 1000.0;
\r
725 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
726 // This should probably be protected
\r
727 // by a "WorkAround" option.
\r
728 snprintf(uci->info,sizeof(uci->info),"%s %s",option,argument);
\r
729 uci->info[sizeof(uci->info)-1]='\0';
\r
734 parse_close(parse);
\r
737 //lousy uci,filter out lower depth multipv lines that have been repeated from the engine
\r
738 if(multipvline>1 && uci->depth<uci->best_depth) event &= ~EVENT_PV;
\r
739 if ((event & EVENT_PV) != 0) {
\r
740 uci->best_score = uci->score;
\r
741 uci->best_depth = uci->depth;
\r
742 if(multipvline==1)uci->depth=-1; //HACK ,clears the engine outpout window,see send_pv in adapter.cpp
\r
743 uci->best_sel_depth = uci->sel_depth;
\r
744 line_copy(uci->best_pv,uci->pv);
\r
751 static void parse_option(uci_t * uci, const char string[]) {
\r
755 char command[StringSize];
\r
756 char option[StringSize];
\r
757 char argument[StringSize];
\r
760 ASSERT(string!=NULL);
\r
764 strcpy(command,"option");
\r
766 memset(opt,0,sizeof(option_t));
\r
768 my_string_set(&opt->value,"<empty>");
\r
769 my_string_set(&opt->name,"<empty>");
\r
770 my_string_set(&opt->default_,"<empty>");
\r
771 my_string_set(&opt->max,"<empty>");
\r
772 my_string_set(&opt->min,"<empty>");
\r
773 my_string_set(&opt->type,"<empty>");
\r
777 parse_open(parse,string);
\r
778 parse_add_keyword(parse,"default");
\r
779 parse_add_keyword(parse,"max");
\r
780 parse_add_keyword(parse,"min");
\r
781 parse_add_keyword(parse,"name");
\r
782 parse_add_keyword(parse,"type");
\r
783 parse_add_keyword(parse,"var");
\r
787 while (parse_get_word(parse,option,StringSize)) {
\r
788 parse_get_string(parse,argument,StringSize);
\r
789 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
793 } else if (my_string_equal(option,"default")) {
\r
795 // ASSERT(!my_string_empty(argument)); // HACK for Pepito
\r
797 if (!my_string_empty(argument)) {
\r
798 my_string_set(&opt->default_,argument);
\r
799 my_string_set(&opt->value,argument);
\r
802 } else if (my_string_equal(option,"max")) {
\r
804 ASSERT(!my_string_empty(argument));
\r
805 my_string_set(&opt->max,argument);
\r
807 } else if (my_string_equal(option,"min")) {
\r
809 ASSERT(!my_string_empty(argument));
\r
810 my_string_set(&opt->min,argument);
\r
812 } else if (my_string_equal(option,"name")) {
\r
814 ASSERT(!my_string_empty(argument));
\r
816 if (!my_string_empty(argument)) {
\r
817 my_string_set(&opt->name,argument);
\r
820 } else if (my_string_equal(option,"type")) {
\r
822 ASSERT(!my_string_empty(argument));
\r
823 my_string_set(&opt->type,argument);
\r
825 } else if (my_string_equal(option,"var")) {
\r
827 ASSERT(!my_string_empty(argument));
\r
828 my_string_set(&opt->var[opt->var_nb++],argument);
\r
829 if(opt->var_nb==VarNb) break;
\r
833 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
837 parse_close(parse);
\r
839 apply_UCI3_heuristics(opt);
\r
840 option_insert(uci->option,opt);
\r
843 if (UseDebug) my_log("POLYGLOT option name \"%s\" default \"%s\"\n",opt->name,opt->default_);
\r
848 static void parse_score(uci_t * uci, const char string[]) {
\r
851 char command[StringSize];
\r
852 char option[StringSize];
\r
853 char argument[StringSize];
\r
856 ASSERT(uci_is_ok(uci));
\r
857 ASSERT(string!=NULL);
\r
861 strcpy(command,"score");
\r
863 parse_open(parse,string);
\r
864 parse_add_keyword(parse,"cp");
\r
865 parse_add_keyword(parse,"lowerbound");
\r
866 parse_add_keyword(parse,"mate");
\r
867 parse_add_keyword(parse,"upperbound");
\r
871 while (parse_get_word(parse,option,StringSize)) {
\r
873 parse_get_string(parse,argument,StringSize);
\r
875 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
879 } else if (my_string_equal(option,"cp")) {
\r
881 ASSERT(!my_string_empty(argument));
\r
883 n = atoi(argument);
\r
887 } else if (my_string_equal(option,"lowerbound")) {
\r
889 ASSERT(my_string_empty(argument));
\r
891 } else if (my_string_equal(option,"mate")) {
\r
893 ASSERT(!my_string_empty(argument));
\r
895 n = atoi(argument);
\r
898 uci->score = mate_score(n);
\r
900 } else if (my_string_equal(option,"upperbound")) {
\r
902 ASSERT(my_string_empty(argument));
\r
906 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
910 parse_close(parse);
\r
915 static int mate_score(int dist) {
\r
920 } else if (dist > 0) {
\r
921 return +option_get_int(Option,"MateScore") - (+dist) * 2 + 1;
\r
922 } else if (dist < 0) {
\r
923 return -option_get_int(Option,"MateScore") + (-dist) * 2;
\r