14 #include "move_do.h"
\r
15 #include "move_legal.h"
\r
23 static const bool UseDebug = FALSE;
\r
25 #define StringSize ((int)4096)
\r
31 // Hopefully the following confusion is temporary
\r
32 // Normally we should check for the engine name but this is a hack anyway
\r
33 // Some of there where provided by Marc Lacrosse
\r
35 const char * thread_options[]={
\r
36 "number of threads", // toga
\r
37 "number threads", // Deep Learning Toga
\r
38 "threads", // glaurung, zappa, cyclone, grapefruit,
\r
39 // Deep Shredder, Deep Junior, bright
\r
40 "core threads", // HIARCS
\r
41 "max cpus", // rybka
\r
42 "cpus", // Deep Sjeng, Fruit2.3.5
\r
43 "maxthreads", // Naum
\r
49 static bool uci_is_ok (const uci_t * uci);
\r
51 static int parse_bestmove (uci_t * uci, const char string[]);
\r
52 static void parse_id (uci_t * uci, const char string[]);
\r
53 static int parse_info (uci_t * uci, const char string[]);
\r
54 static void parse_option (uci_t * uci, const char string[]);
\r
55 static void parse_score (uci_t * uci, const char string[]);
\r
57 static int mate_score (int dist);
\r
61 // uci_set_threads()
\r
63 void uci_set_threads(uci_t * uci, int n) {
\r
64 const char *thread_option=uci_thread_option(uci);
\r
67 uci_send_option(uci,thread_option,"%d",n);
\r
72 const char * uci_thread_option(uci_t * uci){
\r
73 const char **p = thread_options;
\r
74 const char *thread_option;
\r
76 while((thread_option = *(p++))){
\r
77 if((opt=option_find(uci->option,thread_option))){
\r
87 static bool uci_is_ok(const uci_t * uci) {
\r
89 if (uci == NULL) return FALSE;
\r
90 if (uci->engine == NULL) return FALSE;
\r
91 if (!option_is_ok(uci->option)) return FALSE;
\r
97 void uci_open(uci_t * uci, engine_t * engine) {
\r
99 char string[StringSize];
\r
103 ASSERT(engine!=NULL);
\r
107 uci->engine = engine;
\r
110 my_string_set(&uci->name,"<empty>");
\r
111 uci->author = NULL;
\r
112 my_string_set(&uci->author,"<empty>");
\r
113 option_init(uci->option);
\r
116 uci->searching = 0;
\r
117 uci->pending_nb = 0;
\r
118 uci->multipv_mode = FALSE;
\r
119 board_start(uci->board);
\r
122 // send "uci" and wait for "uciok"
\r
124 engine_send(uci->engine,"uci");
\r
127 engine_get(uci->engine,string);
\r
128 event = uci_parse(uci,string);
\r
129 } while (!engine_eof(Engine) && (event & EVENT_UCI) == 0);
\r
134 void uci_close(uci_t * uci) {
\r
136 ASSERT(uci_is_ok(uci));
\r
137 engine_close(uci->engine);
\r
138 uci->engine = NULL;
\r
139 my_string_clear(&uci->name);
\r
140 my_string_clear(&uci->author);
\r
142 option_clear(uci->option);
\r
147 void uci_clear(uci_t * uci) {
\r
149 ASSERT(uci_is_ok(uci));
\r
151 ASSERT(!uci->searching);
\r
153 uci->best_move = MoveNone;
\r
154 uci->ponder_move = MoveNone;
\r
158 uci->sel_depth = 0;
\r
159 line_clear(uci->pv);
\r
161 uci->best_score = 0;
\r
162 uci->best_depth = 0;
\r
163 uci->best_sel_depth = 0;
\r
164 line_clear(uci->best_pv);
\r
165 // make the default 1 instead of 0 so that info lines can be recognized by their node number 0
\r
171 line_clear(uci->current_line);
\r
173 uci->root_move = MoveNone;
\r
174 uci->root_move_pos = 0;
\r
175 uci->root_move_nb = board_mobility(uci->board);
\r
178 // uci_send_isready()
\r
180 void uci_send_isready(uci_t * uci) {
\r
184 engine_send(uci->engine,"isready");
\r
188 // uci_send_isready_sync()
\r
190 void uci_send_isready_sync(uci_t * uci) {
\r
192 char string[StringSize];
\r
195 ASSERT(uci_is_ok(uci));
\r
197 // send "isready" and wait for "readyok"
\r
199 uci_send_isready(uci);
\r
202 engine_get(uci->engine,string);
\r
203 event = uci_parse(uci,string);
\r
204 } while (!engine_eof(Engine) && (event & EVENT_READY) == 0);
\r
209 void uci_send_stop(uci_t * uci) {
\r
211 ASSERT(uci_is_ok(uci));
\r
213 ASSERT(uci->searching);
\r
214 ASSERT(uci->pending_nb>=1);
\r
216 engine_send(Engine,"stop");
\r
217 uci->searching = FALSE;
\r
220 // uci_send_stop_sync()
\r
222 void uci_send_stop_sync(uci_t * uci) {
\r
224 char string[StringSize];
\r
227 ASSERT(uci_is_ok(uci));
\r
229 ASSERT(uci->searching);
\r
230 ASSERT(uci->pending_nb>=1);
\r
232 // send "stop" and wait for "bestmove"
\r
234 uci_send_stop(uci);
\r
237 engine_get(uci->engine,string);
\r
238 event = uci_parse(uci,string);
\r
239 } while (!engine_eof(Engine) && (event & EVENT_STOP) == 0);
\r
242 // uci_send_ucinewgame()
\r
244 void uci_send_ucinewgame(uci_t * uci) {
\r
248 if (option_get_int(Option,"UCIVersion") >= 2) {
\r
249 engine_send(uci->engine,"ucinewgame");
\r
253 // uci_send_option()
\r
255 bool uci_send_option(uci_t * uci, const char option[], const char format[], ...) {
\r
257 char value[FormatBufferSize];
\r
261 ASSERT(uci_is_ok(uci));
\r
262 ASSERT(option!=NULL);
\r
263 ASSERT(format!=NULL);
\r
267 CONSTRUCT_ARG_STRING(format,value);
\r
269 if (UseDebug) my_log("POLYGLOT OPTION %s VALUE %s\n",option,value);
\r
271 opt=option_find(uci->option,option);
\r
274 if(!IS_BUTTON(opt)){
\r
275 if(!my_string_equal(opt->value,value)){
\r
276 engine_send(uci->engine,"setoption name %s value %s",
\r
278 my_string_set(&opt->value,value);
\r
280 my_log("POLYGLOT Not sending option \"%s\" since it "
\r
281 "already has the correct value.\n",opt->name);
\r
284 engine_send(uci->engine,"setoption name %s",opt->name);
\r
292 int uci_parse(uci_t * uci, const char string[]) {
\r
296 char command[StringSize];
\r
297 char argument[StringSize];
\r
299 ASSERT(uci_is_ok(uci));
\r
300 ASSERT(string!=NULL);
\r
304 event = EVENT_NONE;
\r
308 parse_open(parse,string);
\r
310 if (parse_get_word(parse,command,StringSize)) {
\r
312 parse_get_string(parse,argument,StringSize);
\r
313 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" ARGUMENT \"%s\"\n",command,argument);
\r
317 } else if (my_string_equal(command,"bestmove")) {
\r
321 ASSERT(uci->pending_nb>0);
\r
323 if (uci->searching && uci->pending_nb == 1) {
\r
327 uci->searching = FALSE;
\r
330 event = parse_bestmove(uci,argument); // updates uci->best_move and uci->ponder_move
\r
336 if (uci->pending_nb > 0) {
\r
338 if (uci->pending_nb == 0) event = EVENT_STOP;
\r
342 } else if (my_string_equal(command,"id")) {
\r
344 parse_id(uci,argument);
\r
346 } else if (my_string_equal(command,"info")) {
\r
348 // search information
\r
350 if (uci->searching && uci->pending_nb == 1) { // current search
\r
351 event = parse_info(uci,argument);
\r
354 } else if (my_string_equal(command,"option")) {
\r
356 parse_option(uci,argument);
\r
358 } else if (my_string_equal(command,"readyok")) {
\r
362 ASSERT(uci->ready_nb>0);
\r
364 if (uci->ready_nb > 0) {
\r
366 if (uci->ready_nb == 0) event = EVENT_READY;
\r
369 } else if (my_string_equal(command,"uciok")) {
\r
375 if (UseDebug) my_log("POLYGLOT unknown command \"%s\"\n",command);
\r
379 parse_close(parse);
\r
384 // parse_bestmove()
\r
386 static int parse_bestmove(uci_t * uci, const char string[]) {
\r
389 char command[StringSize];
\r
390 char option[StringSize];
\r
391 char argument[StringSize];
\r
394 ASSERT(uci_is_ok(uci));
\r
395 ASSERT(string!=NULL);
\r
399 strcpy(command,"bestmove");
\r
401 parse_open(parse,string);
\r
402 parse_add_keyword(parse,"ponder");
\r
406 if (!parse_get_string(parse,argument,StringSize)) {
\r
407 return EVENT_ILLEGAL_MOVE;
\r
408 // my_fatal("parse_bestmove(): missing argument\n");
\r
411 uci->best_move = move_from_can(argument,uci->board);
\r
412 if (uci->best_move == MoveNone) {
\r
413 return EVENT_ILLEGAL_MOVE;
\r
414 // my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
417 if(!move_is_legal(uci->best_move,uci->board)){
\r
418 return EVENT_ILLEGAL_MOVE;
\r
420 ASSERT(uci->best_move!=MoveNone);
\r
421 ASSERT(move_is_legal(uci->best_move,uci->board));
\r
425 while (parse_get_word(parse,option,StringSize)) {
\r
427 parse_get_string(parse,argument,StringSize);
\r
429 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
433 } else if (my_string_equal(option,"ponder")) {
\r
435 ASSERT(!my_string_empty(argument));
\r
437 board_copy(board,uci->board);
\r
438 move_do(board,uci->best_move);
\r
440 uci->ponder_move = move_from_can(argument,board);
\r
441 // if (uci->ponder_move == MoveNone) my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
443 ASSERT(uci->ponder_move!=MoveNone);
\r
444 ASSERT(move_is_legal(uci->ponder_move,board));
\r
448 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
452 parse_close(parse);
\r
459 static void parse_id(uci_t * uci, const char string[]) {
\r
462 char command[StringSize];
\r
463 char option[StringSize];
\r
464 char argument[StringSize];
\r
467 ASSERT(string!=NULL);
\r
471 strcpy(command,"id");
\r
473 parse_open(parse,string);
\r
474 parse_add_keyword(parse,"author");
\r
475 parse_add_keyword(parse,"name");
\r
479 while (parse_get_word(parse,option,StringSize)) {
\r
481 parse_get_string(parse,argument,StringSize);
\r
482 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
485 } else if (my_string_equal(option,"author")) {
\r
486 ASSERT(!my_string_empty(argument));
\r
487 my_string_set(&uci->author,argument);
\r
488 } else if (my_string_equal(option,"name")) {
\r
489 ASSERT(!my_string_empty(argument));
\r
490 my_string_set(&uci->name,argument);
\r
492 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
496 parse_close(parse);
\r
498 if (UseDebug) my_log("POLYGLOT engine name \"%s\" author \"%s\"\n",uci->name,uci->author);
\r
503 static int parse_info(uci_t * uci, const char string[]) {
\r
507 char command[StringSize];
\r
508 char option[StringSize];
\r
509 char argument[StringSize];
\r
516 ASSERT(uci_is_ok(uci));
\r
517 ASSERT(string!=NULL);
\r
521 event = EVENT_NONE;
\r
523 strcpy(command,"info");
\r
525 parse_open(parse,string);
\r
526 parse_add_keyword(parse,"cpuload");
\r
527 parse_add_keyword(parse,"currline");
\r
528 parse_add_keyword(parse,"currmove");
\r
529 parse_add_keyword(parse,"currmovenumber");
\r
530 parse_add_keyword(parse,"depth");
\r
531 parse_add_keyword(parse,"hashfull");
\r
532 parse_add_keyword(parse,"multipv");
\r
533 parse_add_keyword(parse,"nodes");
\r
534 parse_add_keyword(parse,"nps");
\r
535 parse_add_keyword(parse,"pv");
\r
536 parse_add_keyword(parse,"refutation");
\r
537 parse_add_keyword(parse,"score");
\r
538 parse_add_keyword(parse,"seldepth");
\r
539 parse_add_keyword(parse,"string");
\r
540 parse_add_keyword(parse,"tbhits");
\r
541 parse_add_keyword(parse,"time");
\r
545 while (parse_get_word(parse,option,StringSize)) {
\r
547 parse_get_string(parse,argument,StringSize);
\r
549 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
553 } else if (my_string_equal(option,"cpuload")) {
\r
555 ASSERT(!my_string_empty(argument));
\r
557 n = atoi(argument);
\r
560 if (n >= 0) uci->cpu = ((double)n) / 1000.0;
\r
562 } else if (my_string_equal(option,"currline")) {
\r
564 ASSERT(!my_string_empty(argument));
\r
566 line_from_can(uci->current_line,uci->board,argument,LineSize);
\r
568 } else if (my_string_equal(option,"currmove")) {
\r
570 ASSERT(!my_string_empty(argument));
\r
572 uci->root_move = move_from_can(argument,uci->board);
\r
573 ASSERT(uci->root_move!=MoveNone);
\r
575 } else if (my_string_equal(option,"currmovenumber")) {
\r
577 ASSERT(!my_string_empty(argument));
\r
579 n = atoi(argument);
\r
580 ASSERT(n>=1&&n<=uci->root_move_nb);
\r
582 if (n >= 1 && n <= uci->root_move_nb) {
\r
583 uci->root_move_pos = n - 1;
\r
584 ASSERT(uci->root_move_pos>=0&&uci->root_move_pos<uci->root_move_nb);
\r
587 } else if (my_string_equal(option,"depth")) {
\r
589 ASSERT(!my_string_empty(argument));
\r
591 n = atoi(argument);
\r
595 if (n > uci->depth) event |= EVENT_DEPTH;
\r
599 } else if (my_string_equal(option,"hashfull")) {
\r
601 ASSERT(!my_string_empty(argument));
\r
603 n = atoi(argument);
\r
606 if (n >= 0) uci->hash = ((double)n) / 1000.0;
\r
608 } else if (my_string_equal(option,"multipv")) {
\r
610 ASSERT(!my_string_empty(argument));
\r
612 n = atoi(argument);
\r
613 if(Uci->multipv_mode) multipvline=n;
\r
617 } else if (my_string_equal(option,"nodes")) {
\r
619 ASSERT(!my_string_empty(argument));
\r
621 ln = my_atoll(argument);
\r
624 if (ln >= 0) uci->node_nb = ln;
\r
626 } else if (my_string_equal(option,"nps")) {
\r
628 ASSERT(!my_string_empty(argument));
\r
630 n = atoi(argument);
\r
633 if (n >= 0) uci->speed = ((double)n);
\r
635 } else if (my_string_equal(option,"pv")) {
\r
637 ASSERT(!my_string_empty(argument));
\r
639 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
642 } else if (my_string_equal(option,"refutation")) {
\r
644 ASSERT(!my_string_empty(argument));
\r
646 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
648 } else if (my_string_equal(option,"score")) {
\r
650 ASSERT(!my_string_empty(argument));
\r
652 parse_score(uci,argument);
\r
654 } else if (my_string_equal(option,"seldepth")) {
\r
656 ASSERT(!my_string_empty(argument));
\r
658 n = atoi(argument);
\r
661 if (n >= 0) uci->sel_depth = n;
\r
663 } else if (my_string_equal(option,"string")) {
\r
664 if(my_string_case_equal(argument,"DrawOffer")){
\r
665 event |= EVENT_DRAW;
\r
666 }else if(my_string_case_equal(argument,"Resign")){
\r
667 event |= EVENT_RESIGN;
\r
669 snprintf(uci->info,sizeof(uci->info),"%s",argument);
\r
670 uci->info[sizeof(uci->info)-1]='\0';
\r
673 // TODO: argument to EOS
\r
675 ASSERT(!my_string_empty(argument));
\r
677 } else if (my_string_equal(option,"tbhits")) {
\r
679 ASSERT(!my_string_empty(argument));
\r
681 ln = my_atoll(argument);
\r
684 } else if (my_string_equal(option,"time")) {
\r
686 ASSERT(!my_string_empty(argument));
\r
688 n = atoi(argument);
\r
691 if (n >= 0) uci->time = ((double)n) / 1000.0;
\r
695 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
696 // This should probably be protected
\r
697 // by a "WorkAround" option.
\r
698 snprintf(uci->info,sizeof(uci->info),"%s %s",option,argument);
\r
699 uci->info[sizeof(uci->info)-1]='\0';
\r
704 parse_close(parse);
\r
707 //lousy uci,filter out lower depth multipv lines that have been repeated from the engine
\r
708 if(multipvline>1 && uci->depth<uci->best_depth) event &= ~EVENT_PV;
\r
709 if ((event & EVENT_PV) != 0) {
\r
710 uci->best_score = uci->score;
\r
711 uci->best_depth = uci->depth;
\r
712 if(multipvline==1)uci->depth=-1; //HACK ,clears the engine outpout window,see send_pv in adapter.cpp
\r
713 uci->best_sel_depth = uci->sel_depth;
\r
714 line_copy(uci->best_pv,uci->pv);
\r
721 static void parse_option(uci_t * uci, const char string[]) {
\r
725 char command[StringSize];
\r
726 char option[StringSize];
\r
727 char argument[StringSize];
\r
730 ASSERT(string!=NULL);
\r
734 strcpy(command,"option");
\r
736 memset(opt,0,sizeof(option_t));
\r
738 my_string_set(&opt->value,"<empty>");
\r
739 my_string_set(&opt->name,"<empty>");
\r
740 my_string_set(&opt->default_,"<empty>");
\r
741 my_string_set(&opt->max,"<empty>");
\r
742 my_string_set(&opt->min,"<empty>");
\r
743 my_string_set(&opt->type,"<empty>");
\r
747 parse_open(parse,string);
\r
748 parse_add_keyword(parse,"default");
\r
749 parse_add_keyword(parse,"max");
\r
750 parse_add_keyword(parse,"min");
\r
751 parse_add_keyword(parse,"name");
\r
752 parse_add_keyword(parse,"type");
\r
753 parse_add_keyword(parse,"var");
\r
757 while (parse_get_word(parse,option,StringSize)) {
\r
758 parse_get_string(parse,argument,StringSize);
\r
759 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
763 } else if (my_string_equal(option,"default")) {
\r
765 // ASSERT(!my_string_empty(argument)); // HACK for Pepito
\r
767 if (!my_string_empty(argument)) {
\r
768 my_string_set(&opt->default_,argument);
\r
769 my_string_set(&opt->value,argument);
\r
772 } else if (my_string_equal(option,"max")) {
\r
774 ASSERT(!my_string_empty(argument));
\r
775 my_string_set(&opt->max,argument);
\r
777 } else if (my_string_equal(option,"min")) {
\r
779 ASSERT(!my_string_empty(argument));
\r
780 my_string_set(&opt->min,argument);
\r
782 } else if (my_string_equal(option,"name")) {
\r
784 ASSERT(!my_string_empty(argument));
\r
786 if (!my_string_empty(argument)) {
\r
787 my_string_set(&opt->name,argument);
\r
790 } else if (my_string_equal(option,"type")) {
\r
792 ASSERT(!my_string_empty(argument));
\r
793 my_string_set(&opt->type,argument);
\r
795 } else if (my_string_equal(option,"var")) {
\r
797 ASSERT(!my_string_empty(argument));
\r
798 my_string_set(&opt->var[opt->var_nb++],argument);
\r
799 if(opt->var_nb==VarNb) break;
\r
803 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
807 parse_close(parse);
\r
808 option_insert(uci->option,opt);
\r
811 if (UseDebug) my_log("POLYGLOT option name \"%s\" default \"%s\"\n",opt->name,opt->default_);
\r
816 static void parse_score(uci_t * uci, const char string[]) {
\r
819 char command[StringSize];
\r
820 char option[StringSize];
\r
821 char argument[StringSize];
\r
824 ASSERT(uci_is_ok(uci));
\r
825 ASSERT(string!=NULL);
\r
829 strcpy(command,"score");
\r
831 parse_open(parse,string);
\r
832 parse_add_keyword(parse,"cp");
\r
833 parse_add_keyword(parse,"lowerbound");
\r
834 parse_add_keyword(parse,"mate");
\r
835 parse_add_keyword(parse,"upperbound");
\r
839 while (parse_get_word(parse,option,StringSize)) {
\r
841 parse_get_string(parse,argument,StringSize);
\r
843 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
847 } else if (my_string_equal(option,"cp")) {
\r
849 ASSERT(!my_string_empty(argument));
\r
851 n = atoi(argument);
\r
855 } else if (my_string_equal(option,"lowerbound")) {
\r
857 ASSERT(my_string_empty(argument));
\r
859 } else if (my_string_equal(option,"mate")) {
\r
861 ASSERT(!my_string_empty(argument));
\r
863 n = atoi(argument);
\r
866 uci->score = mate_score(n);
\r
868 } else if (my_string_equal(option,"upperbound")) {
\r
870 ASSERT(my_string_empty(argument));
\r
874 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
878 parse_close(parse);
\r
883 static int mate_score(int dist) {
\r
888 } else if (dist > 0) {
\r
889 return +option_get_int(Option,"MateScore") - (+dist) * 2 + 1;
\r
890 } else if (dist < 0) {
\r
891 return -option_get_int(Option,"MateScore") + (-dist) * 2;
\r