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
64 static void apply_UCI3_heuristics(option_t *opt){
\r
65 if(option_get_int(Option,"UCIVersion")>2){
\r
68 if(!my_string_equal(opt->type,"string")){
\r
71 if(!strncmp(opt->name,"UCI_",4)){
\r
74 if(my_string_case_contains(opt->name,"file")){
\r
75 my_string_set(&opt->type,"file");
\r
78 if(my_string_case_contains(opt->name,"path")){
\r
79 my_string_set(&opt->type,"path");
\r
84 // uci_set_threads()
\r
86 void uci_set_threads(uci_t * uci, int n) {
\r
87 const char *thread_option=uci_thread_option(uci);
\r
90 uci_send_option(uci,thread_option,"%d",n);
\r
95 const char * uci_thread_option(uci_t * uci){
\r
96 const char **p = thread_options;
\r
97 const char *thread_option;
\r
99 while((thread_option = *(p++))){
\r
100 if((opt=option_find(uci->option,thread_option))){
\r
110 static bool uci_is_ok(const uci_t * uci) {
\r
112 if (uci == NULL) return FALSE;
\r
113 if (uci->engine == NULL) return FALSE;
\r
114 if (!option_is_ok(uci->option)) return FALSE;
\r
120 void uci_open(uci_t * uci, engine_t * engine) {
\r
122 char string[StringSize];
\r
126 ASSERT(engine!=NULL);
\r
130 uci->engine = engine;
\r
133 my_string_set(&uci->name,"<empty>");
\r
134 uci->author = NULL;
\r
135 my_string_set(&uci->author,"<empty>");
\r
136 option_init(uci->option);
\r
139 uci->searching = 0;
\r
140 uci->pending_nb = 0;
\r
141 uci->multipv_mode = FALSE;
\r
142 board_start(uci->board);
\r
145 // send "uci" and wait for "uciok"
\r
147 engine_send(uci->engine,"uci");
\r
150 engine_get(uci->engine,string);
\r
151 event = uci_parse(uci,string);
\r
152 } while (!engine_eof(Engine) && (event & EVENT_UCI) == 0);
\r
157 void uci_close(uci_t * uci) {
\r
159 ASSERT(uci_is_ok(uci));
\r
160 engine_close(uci->engine);
\r
161 uci->engine = NULL;
\r
162 my_string_clear(&uci->name);
\r
163 my_string_clear(&uci->author);
\r
165 option_clear(uci->option);
\r
170 void uci_clear(uci_t * uci) {
\r
172 ASSERT(uci_is_ok(uci));
\r
174 ASSERT(!uci->searching);
\r
176 uci->best_move = MoveNone;
\r
177 uci->ponder_move = MoveNone;
\r
181 uci->sel_depth = 0;
\r
182 line_clear(uci->pv);
\r
184 uci->best_score = 0;
\r
185 uci->best_depth = 0;
\r
186 uci->best_sel_depth = 0;
\r
187 line_clear(uci->best_pv);
\r
188 // make the default 1 instead of 0 so that info lines can be recognized by their node number 0
\r
194 line_clear(uci->current_line);
\r
196 uci->root_move = MoveNone;
\r
197 uci->root_move_pos = 0;
\r
198 uci->root_move_nb = board_mobility(uci->board);
\r
201 // uci_send_isready()
\r
203 void uci_send_isready(uci_t * uci) {
\r
207 engine_send(uci->engine,"isready");
\r
211 // uci_send_isready_sync()
\r
213 void uci_send_isready_sync(uci_t * uci) {
\r
215 char string[StringSize];
\r
218 ASSERT(uci_is_ok(uci));
\r
220 // send "isready" and wait for "readyok"
\r
222 uci_send_isready(uci);
\r
225 engine_get(uci->engine,string);
\r
226 event = uci_parse(uci,string);
\r
227 } while (!engine_eof(Engine) && (event & EVENT_READY) == 0);
\r
232 void uci_send_stop(uci_t * uci) {
\r
234 ASSERT(uci_is_ok(uci));
\r
236 ASSERT(uci->searching);
\r
237 ASSERT(uci->pending_nb>=1);
\r
239 engine_send(Engine,"stop");
\r
240 uci->searching = FALSE;
\r
243 // uci_send_stop_sync()
\r
245 void uci_send_stop_sync(uci_t * uci) {
\r
247 char string[StringSize];
\r
250 ASSERT(uci_is_ok(uci));
\r
252 ASSERT(uci->searching);
\r
253 ASSERT(uci->pending_nb>=1);
\r
255 // send "stop" and wait for "bestmove"
\r
257 uci_send_stop(uci);
\r
260 engine_get(uci->engine,string);
\r
261 event = uci_parse(uci,string);
\r
262 } while (!engine_eof(Engine) && (event & EVENT_STOP) == 0);
\r
265 // uci_send_ucinewgame()
\r
267 void uci_send_ucinewgame(uci_t * uci) {
\r
271 if (option_get_int(Option,"UCIVersion") >= 2) {
\r
272 engine_send(uci->engine,"ucinewgame");
\r
276 // uci_send_option()
\r
278 bool uci_send_option(uci_t * uci, const char option[], const char format[], ...) {
\r
280 char value[FormatBufferSize];
\r
284 ASSERT(uci_is_ok(uci));
\r
285 ASSERT(option!=NULL);
\r
286 ASSERT(format!=NULL);
\r
290 CONSTRUCT_ARG_STRING(format,value);
\r
292 if (UseDebug) my_log("POLYGLOT OPTION %s VALUE %s\n",option,value);
\r
294 opt=option_find(uci->option,option);
\r
297 if(!IS_BUTTON(opt->type)){
\r
298 if(!my_string_equal(opt->value,value)){
\r
299 engine_send(uci->engine,"setoption name %s value %s",
\r
301 my_string_set(&opt->value,value);
\r
303 my_log("POLYGLOT Not sending option \"%s\" since it "
\r
304 "already has the correct value.\n",opt->name);
\r
307 engine_send(uci->engine,"setoption name %s",opt->name);
\r
315 int uci_parse(uci_t * uci, const char string[]) {
\r
319 char command[StringSize];
\r
320 char argument[StringSize];
\r
322 ASSERT(uci_is_ok(uci));
\r
323 ASSERT(string!=NULL);
\r
327 event = EVENT_NONE;
\r
331 parse_open(parse,string);
\r
333 if (parse_get_word(parse,command,StringSize)) {
\r
335 parse_get_string(parse,argument,StringSize);
\r
336 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" ARGUMENT \"%s\"\n",command,argument);
\r
340 } else if (my_string_equal(command,"bestmove")) {
\r
344 ASSERT(uci->pending_nb>0);
\r
346 if (uci->searching && uci->pending_nb == 1) {
\r
350 uci->searching = FALSE;
\r
353 event = parse_bestmove(uci,argument); // updates uci->best_move and uci->ponder_move
\r
359 if (uci->pending_nb > 0) {
\r
361 if (uci->pending_nb == 0) event = EVENT_STOP;
\r
365 } else if (my_string_equal(command,"id")) {
\r
367 parse_id(uci,argument);
\r
369 } else if (my_string_equal(command,"info")) {
\r
371 // search information
\r
373 if (uci->searching && uci->pending_nb == 1) { // current search
\r
374 event = parse_info(uci,argument);
\r
377 } else if (my_string_equal(command,"option")) {
\r
379 parse_option(uci,argument);
\r
381 } else if (my_string_equal(command,"readyok")) {
\r
385 ASSERT(uci->ready_nb>0);
\r
387 if (uci->ready_nb > 0) {
\r
389 if (uci->ready_nb == 0) event = EVENT_READY;
\r
392 } else if (my_string_equal(command,"uciok")) {
\r
398 if (UseDebug) my_log("POLYGLOT unknown command \"%s\"\n",command);
\r
402 parse_close(parse);
\r
407 // parse_bestmove()
\r
409 static int parse_bestmove(uci_t * uci, const char string[]) {
\r
412 char command[StringSize];
\r
413 char option[StringSize];
\r
414 char argument[StringSize];
\r
417 ASSERT(uci_is_ok(uci));
\r
418 ASSERT(string!=NULL);
\r
422 strcpy(command,"bestmove");
\r
424 parse_open(parse,string);
\r
425 parse_add_keyword(parse,"ponder");
\r
429 if (!parse_get_string(parse,argument,StringSize)) {
\r
430 return EVENT_ILLEGAL_MOVE;
\r
431 // my_fatal("parse_bestmove(): missing argument\n");
\r
434 uci->best_move = move_from_can(argument,uci->board);
\r
435 if (uci->best_move == MoveNone) {
\r
436 return EVENT_ILLEGAL_MOVE;
\r
437 // my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
440 if(!move_is_legal(uci->best_move,uci->board)){
\r
441 return EVENT_ILLEGAL_MOVE;
\r
443 ASSERT(uci->best_move!=MoveNone);
\r
444 ASSERT(move_is_legal(uci->best_move,uci->board));
\r
448 while (parse_get_word(parse,option,StringSize)) {
\r
450 parse_get_string(parse,argument,StringSize);
\r
452 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
456 } else if (my_string_equal(option,"ponder")) {
\r
458 ASSERT(!my_string_empty(argument));
\r
460 board_copy(board,uci->board);
\r
461 move_do(board,uci->best_move);
\r
463 uci->ponder_move = move_from_can(argument,board);
\r
464 // if (uci->ponder_move == MoveNone) my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
466 ASSERT(uci->ponder_move!=MoveNone);
\r
467 ASSERT(move_is_legal(uci->ponder_move,board));
\r
471 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
475 parse_close(parse);
\r
482 static void parse_id(uci_t * uci, const char string[]) {
\r
485 char command[StringSize];
\r
486 char option[StringSize];
\r
487 char argument[StringSize];
\r
490 ASSERT(string!=NULL);
\r
494 strcpy(command,"id");
\r
496 parse_open(parse,string);
\r
497 parse_add_keyword(parse,"author");
\r
498 parse_add_keyword(parse,"name");
\r
502 while (parse_get_word(parse,option,StringSize)) {
\r
504 parse_get_string(parse,argument,StringSize);
\r
505 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
508 } else if (my_string_equal(option,"author")) {
\r
509 ASSERT(!my_string_empty(argument));
\r
510 my_string_set(&uci->author,argument);
\r
511 } else if (my_string_equal(option,"name")) {
\r
512 ASSERT(!my_string_empty(argument));
\r
513 my_string_set(&uci->name,argument);
\r
515 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
519 parse_close(parse);
\r
521 if (UseDebug) my_log("POLYGLOT engine name \"%s\" author \"%s\"\n",uci->name,uci->author);
\r
526 static int parse_info(uci_t * uci, const char string[]) {
\r
530 char command[StringSize];
\r
531 char option[StringSize];
\r
532 char argument[StringSize];
\r
539 ASSERT(uci_is_ok(uci));
\r
540 ASSERT(string!=NULL);
\r
544 event = EVENT_NONE;
\r
546 strcpy(command,"info");
\r
548 parse_open(parse,string);
\r
549 parse_add_keyword(parse,"cpuload");
\r
550 parse_add_keyword(parse,"currline");
\r
551 parse_add_keyword(parse,"currmove");
\r
552 parse_add_keyword(parse,"currmovenumber");
\r
553 parse_add_keyword(parse,"depth");
\r
554 parse_add_keyword(parse,"hashfull");
\r
555 parse_add_keyword(parse,"multipv");
\r
556 parse_add_keyword(parse,"nodes");
\r
557 parse_add_keyword(parse,"nps");
\r
558 parse_add_keyword(parse,"pv");
\r
559 parse_add_keyword(parse,"refutation");
\r
560 parse_add_keyword(parse,"score");
\r
561 parse_add_keyword(parse,"seldepth");
\r
562 parse_add_keyword(parse,"string");
\r
563 parse_add_keyword(parse,"tbhits");
\r
564 parse_add_keyword(parse,"time");
\r
568 while (parse_get_word(parse,option,StringSize)) {
\r
570 parse_get_string(parse,argument,StringSize);
\r
572 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
576 } else if (my_string_equal(option,"cpuload")) {
\r
578 ASSERT(!my_string_empty(argument));
\r
580 n = atoi(argument);
\r
583 if (n >= 0) uci->cpu = ((double)n) / 1000.0;
\r
585 } else if (my_string_equal(option,"currline")) {
\r
587 ASSERT(!my_string_empty(argument));
\r
589 line_from_can(uci->current_line,uci->board,argument,LineSize);
\r
591 } else if (my_string_equal(option,"currmove")) {
\r
593 ASSERT(!my_string_empty(argument));
\r
595 uci->root_move = move_from_can(argument,uci->board);
\r
596 ASSERT(uci->root_move!=MoveNone);
\r
598 } else if (my_string_equal(option,"currmovenumber")) {
\r
600 ASSERT(!my_string_empty(argument));
\r
602 n = atoi(argument);
\r
603 ASSERT(n>=1&&n<=uci->root_move_nb);
\r
605 if (n >= 1 && n <= uci->root_move_nb) {
\r
606 uci->root_move_pos = n - 1;
\r
607 ASSERT(uci->root_move_pos>=0&&uci->root_move_pos<uci->root_move_nb);
\r
610 } else if (my_string_equal(option,"depth")) {
\r
612 ASSERT(!my_string_empty(argument));
\r
614 n = atoi(argument);
\r
618 if (n > uci->depth) event |= EVENT_DEPTH;
\r
622 } else if (my_string_equal(option,"hashfull")) {
\r
624 ASSERT(!my_string_empty(argument));
\r
626 n = atoi(argument);
\r
629 if (n >= 0) uci->hash = ((double)n) / 1000.0;
\r
631 } else if (my_string_equal(option,"multipv")) {
\r
633 ASSERT(!my_string_empty(argument));
\r
635 n = atoi(argument);
\r
636 if(Uci->multipv_mode) multipvline=n;
\r
640 } else if (my_string_equal(option,"nodes")) {
\r
642 ASSERT(!my_string_empty(argument));
\r
644 ln = my_atoll(argument);
\r
647 if (ln >= 0) uci->node_nb = ln;
\r
649 } else if (my_string_equal(option,"nps")) {
\r
651 ASSERT(!my_string_empty(argument));
\r
653 n = atoi(argument);
\r
656 if (n >= 0) uci->speed = ((double)n);
\r
658 } else if (my_string_equal(option,"pv")) {
\r
660 ASSERT(!my_string_empty(argument));
\r
662 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
665 } else if (my_string_equal(option,"refutation")) {
\r
667 ASSERT(!my_string_empty(argument));
\r
669 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
671 } else if (my_string_equal(option,"score")) {
\r
673 ASSERT(!my_string_empty(argument));
\r
675 parse_score(uci,argument);
\r
677 } else if (my_string_equal(option,"seldepth")) {
\r
679 ASSERT(!my_string_empty(argument));
\r
681 n = atoi(argument);
\r
684 if (n >= 0) uci->sel_depth = n;
\r
686 } else if (my_string_equal(option,"string")) {
\r
687 if(my_string_case_equal(argument,"DrawOffer")){
\r
688 event |= EVENT_DRAW;
\r
689 }else if(my_string_case_equal(argument,"Resign")){
\r
690 event |= EVENT_RESIGN;
\r
692 snprintf(uci->info,sizeof(uci->info),"%s",argument);
\r
693 uci->info[sizeof(uci->info)-1]='\0';
\r
696 // TODO: argument to EOS
\r
698 ASSERT(!my_string_empty(argument));
\r
700 } else if (my_string_equal(option,"tbhits")) {
\r
702 ASSERT(!my_string_empty(argument));
\r
704 ln = my_atoll(argument);
\r
707 } else if (my_string_equal(option,"time")) {
\r
709 ASSERT(!my_string_empty(argument));
\r
711 n = atoi(argument);
\r
714 if (n >= 0) uci->time = ((double)n) / 1000.0;
\r
718 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
719 // This should probably be protected
\r
720 // by a "WorkAround" option.
\r
721 snprintf(uci->info,sizeof(uci->info),"%s %s",option,argument);
\r
722 uci->info[sizeof(uci->info)-1]='\0';
\r
727 parse_close(parse);
\r
730 //lousy uci,filter out lower depth multipv lines that have been repeated from the engine
\r
731 if(multipvline>1 && uci->depth<uci->best_depth) event &= ~EVENT_PV;
\r
732 if ((event & EVENT_PV) != 0) {
\r
733 uci->best_score = uci->score;
\r
734 uci->best_depth = uci->depth;
\r
735 if(multipvline==1)uci->depth=-1; //HACK ,clears the engine outpout window,see send_pv in adapter.cpp
\r
736 uci->best_sel_depth = uci->sel_depth;
\r
737 line_copy(uci->best_pv,uci->pv);
\r
744 static void parse_option(uci_t * uci, const char string[]) {
\r
748 char command[StringSize];
\r
749 char option[StringSize];
\r
750 char argument[StringSize];
\r
753 ASSERT(string!=NULL);
\r
757 strcpy(command,"option");
\r
759 memset(opt,0,sizeof(option_t));
\r
761 my_string_set(&opt->value,"<empty>");
\r
762 my_string_set(&opt->name,"<empty>");
\r
763 my_string_set(&opt->default_,"<empty>");
\r
764 my_string_set(&opt->max,"<empty>");
\r
765 my_string_set(&opt->min,"<empty>");
\r
766 my_string_set(&opt->type,"<empty>");
\r
770 parse_open(parse,string);
\r
771 parse_add_keyword(parse,"default");
\r
772 parse_add_keyword(parse,"max");
\r
773 parse_add_keyword(parse,"min");
\r
774 parse_add_keyword(parse,"name");
\r
775 parse_add_keyword(parse,"type");
\r
776 parse_add_keyword(parse,"var");
\r
780 while (parse_get_word(parse,option,StringSize)) {
\r
781 parse_get_string(parse,argument,StringSize);
\r
782 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
786 } else if (my_string_equal(option,"default")) {
\r
788 // ASSERT(!my_string_empty(argument)); // HACK for Pepito
\r
790 if (!my_string_empty(argument)) {
\r
791 my_string_set(&opt->default_,argument);
\r
792 my_string_set(&opt->value,argument);
\r
795 } else if (my_string_equal(option,"max")) {
\r
797 ASSERT(!my_string_empty(argument));
\r
798 my_string_set(&opt->max,argument);
\r
800 } else if (my_string_equal(option,"min")) {
\r
802 ASSERT(!my_string_empty(argument));
\r
803 my_string_set(&opt->min,argument);
\r
805 } else if (my_string_equal(option,"name")) {
\r
807 ASSERT(!my_string_empty(argument));
\r
809 if (!my_string_empty(argument)) {
\r
810 my_string_set(&opt->name,argument);
\r
813 } else if (my_string_equal(option,"type")) {
\r
815 ASSERT(!my_string_empty(argument));
\r
816 my_string_set(&opt->type,argument);
\r
818 } else if (my_string_equal(option,"var")) {
\r
820 ASSERT(!my_string_empty(argument));
\r
821 my_string_set(&opt->var[opt->var_nb++],argument);
\r
822 if(opt->var_nb==VarNb) break;
\r
826 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
830 parse_close(parse);
\r
832 apply_UCI3_heuristics(opt);
\r
833 option_insert(uci->option,opt);
\r
836 if (UseDebug) my_log("POLYGLOT option name \"%s\" default \"%s\"\n",opt->name,opt->default_);
\r
841 static void parse_score(uci_t * uci, const char string[]) {
\r
844 char command[StringSize];
\r
845 char option[StringSize];
\r
846 char argument[StringSize];
\r
849 ASSERT(uci_is_ok(uci));
\r
850 ASSERT(string!=NULL);
\r
854 strcpy(command,"score");
\r
856 parse_open(parse,string);
\r
857 parse_add_keyword(parse,"cp");
\r
858 parse_add_keyword(parse,"lowerbound");
\r
859 parse_add_keyword(parse,"mate");
\r
860 parse_add_keyword(parse,"upperbound");
\r
864 while (parse_get_word(parse,option,StringSize)) {
\r
866 parse_get_string(parse,argument,StringSize);
\r
868 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
872 } else if (my_string_equal(option,"cp")) {
\r
874 ASSERT(!my_string_empty(argument));
\r
876 n = atoi(argument);
\r
880 } else if (my_string_equal(option,"lowerbound")) {
\r
882 ASSERT(my_string_empty(argument));
\r
884 } else if (my_string_equal(option,"mate")) {
\r
886 ASSERT(!my_string_empty(argument));
\r
888 n = atoi(argument);
\r
891 uci->score = mate_score(n);
\r
893 } else if (my_string_equal(option,"upperbound")) {
\r
895 ASSERT(my_string_empty(argument));
\r
899 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
903 parse_close(parse);
\r
908 static int mate_score(int dist) {
\r
913 } else if (dist > 0) {
\r
914 return +option_get_int(Option,"MateScore") - (+dist) * 2 + 1;
\r
915 } else if (dist < 0) {
\r
916 return -option_get_int(Option,"MateScore") + (-dist) * 2;
\r