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
210 // uci_send_isready()
\r
212 void uci_send_isready(uci_t * uci) {
\r
216 engine_send(uci->engine,"isready");
\r
220 // uci_send_isready_sync()
\r
222 void uci_send_isready_sync(uci_t * uci) {
\r
224 char string[StringSize];
\r
227 ASSERT(uci_is_ok(uci));
\r
229 // send "isready" and wait for "readyok"
\r
231 uci_send_isready(uci);
\r
234 engine_get(uci->engine,string);
\r
235 event = uci_parse(uci,string);
\r
236 } while (!engine_eof(Engine) && (event & EVENT_READY) == 0);
\r
241 void uci_send_stop(uci_t * uci) {
\r
243 ASSERT(uci_is_ok(uci));
\r
245 ASSERT(uci->searching);
\r
246 ASSERT(uci->pending_nb>=1);
\r
248 engine_send(Engine,"stop");
\r
249 uci->searching = FALSE;
\r
252 // uci_send_stop_sync()
\r
254 void uci_send_stop_sync(uci_t * uci) {
\r
256 char string[StringSize];
\r
259 ASSERT(uci_is_ok(uci));
\r
261 ASSERT(uci->searching);
\r
262 ASSERT(uci->pending_nb>=1);
\r
264 // send "stop" and wait for "bestmove"
\r
266 uci_send_stop(uci);
\r
269 engine_get(uci->engine,string);
\r
270 event = uci_parse(uci,string);
\r
271 } while (!engine_eof(Engine) && (event & EVENT_STOP) == 0);
\r
274 // uci_send_ucinewgame()
\r
276 void uci_send_ucinewgame(uci_t * uci) {
\r
280 if (option_get_int(Option,"UCIVersion") >= 2) {
\r
281 engine_send(uci->engine,"ucinewgame");
\r
285 // uci_send_option()
\r
287 bool uci_send_option(uci_t * uci, const char option[], const char format[], ...) {
\r
289 char value[FormatBufferSize];
\r
293 ASSERT(uci_is_ok(uci));
\r
294 ASSERT(option!=NULL);
\r
295 ASSERT(format!=NULL);
\r
299 CONSTRUCT_ARG_STRING(format,value);
\r
301 if (UseDebug) my_log("POLYGLOT OPTION %s VALUE %s\n",option,value);
\r
303 opt=option_find(uci->option,option);
\r
306 if(!IS_BUTTON(opt->type)){
\r
307 if(!my_string_equal(opt->value,value)){
\r
308 engine_send(uci->engine,"setoption name %s value %s",
\r
310 my_string_set(&opt->value,value);
\r
312 my_log("POLYGLOT Not sending option \"%s\" since it "
\r
313 "already has the correct value.\n",opt->name);
\r
316 engine_send(uci->engine,"setoption name %s",opt->name);
\r
324 int uci_parse(uci_t * uci, const char string[]) {
\r
328 char command[StringSize];
\r
329 char argument[StringSize];
\r
331 ASSERT(uci_is_ok(uci));
\r
332 ASSERT(string!=NULL);
\r
336 event = EVENT_NONE;
\r
340 parse_open(parse,string);
\r
342 if (parse_get_word(parse,command,StringSize)) {
\r
344 parse_get_string(parse,argument,StringSize);
\r
345 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" ARGUMENT \"%s\"\n",command,argument);
\r
349 } else if (my_string_equal(command,"bestmove")) {
\r
353 ASSERT(uci->pending_nb>0);
\r
355 if (uci->searching && uci->pending_nb == 1) {
\r
359 uci->searching = FALSE;
\r
362 event = parse_bestmove(uci,argument); // updates uci->best_move and uci->ponder_move
\r
368 if (uci->pending_nb > 0) {
\r
370 if (uci->pending_nb == 0) event = EVENT_STOP;
\r
374 } else if (my_string_equal(command,"id")) {
\r
376 parse_id(uci,argument);
\r
378 } else if (my_string_equal(command,"info")) {
\r
380 // search information
\r
382 if (uci->searching && uci->pending_nb == 1) { // current search
\r
383 event = parse_info(uci,argument);
\r
386 } else if (my_string_equal(command,"option")) {
\r
388 parse_option(uci,argument);
\r
390 } else if (my_string_equal(command,"readyok")) {
\r
394 ASSERT(uci->ready_nb>0);
\r
396 if (uci->ready_nb > 0) {
\r
398 if (uci->ready_nb == 0) event = EVENT_READY;
\r
401 } else if (my_string_equal(command,"uciok")) {
\r
407 if (UseDebug) my_log("POLYGLOT unknown command \"%s\"\n",command);
\r
411 parse_close(parse);
\r
416 // parse_bestmove()
\r
418 static int parse_bestmove(uci_t * uci, const char string[]) {
\r
421 char command[StringSize];
\r
422 char option[StringSize];
\r
423 char argument[StringSize];
\r
426 ASSERT(uci_is_ok(uci));
\r
427 ASSERT(string!=NULL);
\r
431 strcpy(command,"bestmove");
\r
433 parse_open(parse,string);
\r
434 parse_add_keyword(parse,"ponder");
\r
438 if (!parse_get_string(parse,argument,StringSize)) {
\r
439 return EVENT_ILLEGAL_MOVE;
\r
440 // my_fatal("parse_bestmove(): missing argument\n");
\r
443 uci->best_move = move_from_can(argument,uci->board);
\r
444 if (uci->best_move == MoveNone) {
\r
445 return EVENT_ILLEGAL_MOVE;
\r
446 // my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
449 if(!move_is_legal(uci->best_move,uci->board)){
\r
450 return EVENT_ILLEGAL_MOVE;
\r
452 ASSERT(uci->best_move!=MoveNone);
\r
453 ASSERT(move_is_legal(uci->best_move,uci->board));
\r
457 while (parse_get_word(parse,option,StringSize)) {
\r
459 parse_get_string(parse,argument,StringSize);
\r
461 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
465 } else if (my_string_equal(option,"ponder")) {
\r
467 ASSERT(!my_string_empty(argument));
\r
469 board_copy(board,uci->board);
\r
470 move_do(board,uci->best_move);
\r
472 uci->ponder_move = move_from_can(argument,board);
\r
473 // if (uci->ponder_move == MoveNone) my_fatal("parse_bestmove(): not a move \"%s\"\n",argument);
\r
475 ASSERT(uci->ponder_move!=MoveNone);
\r
476 ASSERT(move_is_legal(uci->ponder_move,board));
\r
480 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
484 parse_close(parse);
\r
491 static void parse_id(uci_t * uci, const char string[]) {
\r
494 char command[StringSize];
\r
495 char option[StringSize];
\r
496 char argument[StringSize];
\r
499 ASSERT(string!=NULL);
\r
503 strcpy(command,"id");
\r
505 parse_open(parse,string);
\r
506 parse_add_keyword(parse,"author");
\r
507 parse_add_keyword(parse,"name");
\r
511 while (parse_get_word(parse,option,StringSize)) {
\r
513 parse_get_string(parse,argument,StringSize);
\r
514 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
517 } else if (my_string_equal(option,"author")) {
\r
518 ASSERT(!my_string_empty(argument));
\r
519 my_string_set(&uci->author,argument);
\r
520 } else if (my_string_equal(option,"name")) {
\r
521 ASSERT(!my_string_empty(argument));
\r
522 my_string_set(&uci->name,argument);
\r
524 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
528 parse_close(parse);
\r
530 if (UseDebug) my_log("POLYGLOT engine name \"%s\" author \"%s\"\n",uci->name,uci->author);
\r
535 static int parse_info(uci_t * uci, const char string[]) {
\r
539 char command[StringSize];
\r
540 char option[StringSize];
\r
541 char argument[StringSize];
\r
548 ASSERT(uci_is_ok(uci));
\r
549 ASSERT(string!=NULL);
\r
553 event = EVENT_NONE;
\r
555 strcpy(command,"info");
\r
557 parse_open(parse,string);
\r
558 parse_add_keyword(parse,"cpuload");
\r
559 parse_add_keyword(parse,"currline");
\r
560 parse_add_keyword(parse,"currmove");
\r
561 parse_add_keyword(parse,"currmovenumber");
\r
562 parse_add_keyword(parse,"depth");
\r
563 parse_add_keyword(parse,"hashfull");
\r
564 parse_add_keyword(parse,"multipv");
\r
565 parse_add_keyword(parse,"nodes");
\r
566 parse_add_keyword(parse,"nps");
\r
567 parse_add_keyword(parse,"pv");
\r
568 parse_add_keyword(parse,"refutation");
\r
569 parse_add_keyword(parse,"score");
\r
570 parse_add_keyword(parse,"seldepth");
\r
571 parse_add_keyword(parse,"string");
\r
572 parse_add_keyword(parse,"tbhits");
\r
573 parse_add_keyword(parse,"time");
\r
577 while (parse_get_word(parse,option,StringSize)) {
\r
579 parse_get_string(parse,argument,StringSize);
\r
581 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
585 } else if (my_string_equal(option,"cpuload")) {
\r
587 ASSERT(!my_string_empty(argument));
\r
589 n = atoi(argument);
\r
592 if (n >= 0) uci->cpu = ((double)n) / 1000.0;
\r
594 } else if (my_string_equal(option,"currline")) {
\r
596 ASSERT(!my_string_empty(argument));
\r
598 line_from_can(uci->current_line,uci->board,argument,LineSize);
\r
600 } else if (my_string_equal(option,"currmove")) {
\r
602 ASSERT(!my_string_empty(argument));
\r
604 uci->root_move = move_from_can(argument,uci->board);
\r
605 ASSERT(uci->root_move!=MoveNone);
\r
607 } else if (my_string_equal(option,"currmovenumber")) {
\r
609 ASSERT(!my_string_empty(argument));
\r
611 n = atoi(argument);
\r
612 ASSERT(n>=1&&n<=uci->root_move_nb);
\r
614 if (n >= 1 && n <= uci->root_move_nb) {
\r
615 uci->root_move_pos = n - 1;
\r
616 ASSERT(uci->root_move_pos>=0&&uci->root_move_pos<uci->root_move_nb);
\r
619 } else if (my_string_equal(option,"depth")) {
\r
621 ASSERT(!my_string_empty(argument));
\r
623 n = atoi(argument);
\r
627 if (n > uci->depth) event |= EVENT_DEPTH;
\r
631 } else if (my_string_equal(option,"hashfull")) {
\r
633 ASSERT(!my_string_empty(argument));
\r
635 n = atoi(argument);
\r
638 if (n >= 0) uci->hash = ((double)n) / 1000.0;
\r
640 } else if (my_string_equal(option,"multipv")) {
\r
642 ASSERT(!my_string_empty(argument));
\r
644 n = atoi(argument);
\r
649 } else if (my_string_equal(option,"nodes")) {
\r
651 ASSERT(!my_string_empty(argument));
\r
653 ln = my_atoll(argument);
\r
656 if (ln >= 0) uci->node_nb = ln;
\r
658 } else if (my_string_equal(option,"nps")) {
\r
660 ASSERT(!my_string_empty(argument));
\r
662 n = atoi(argument);
\r
665 if (n >= 0) uci->speed = ((double)n);
\r
667 } else if (my_string_equal(option,"pv")) {
\r
669 ASSERT(!my_string_empty(argument));
\r
671 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
674 } else if (my_string_equal(option,"refutation")) {
\r
676 ASSERT(!my_string_empty(argument));
\r
678 line_from_can(uci->pv,uci->board,argument,LineSize);
\r
680 } else if (my_string_equal(option,"score")) {
\r
682 ASSERT(!my_string_empty(argument));
\r
684 parse_score(uci,argument);
\r
686 } else if (my_string_equal(option,"seldepth")) {
\r
688 ASSERT(!my_string_empty(argument));
\r
690 n = atoi(argument);
\r
693 if (n >= 0) uci->sel_depth = n;
\r
695 } else if (my_string_equal(option,"string")) {
\r
696 if(my_string_case_equal(argument,"DrawOffer")){
\r
697 event |= EVENT_DRAW;
\r
698 }else if(my_string_case_equal(argument,"Resign")){
\r
699 event |= EVENT_RESIGN;
\r
701 snprintf(uci->info,sizeof(uci->info),"%s",argument);
\r
702 uci->info[sizeof(uci->info)-1]='\0';
\r
705 // TODO: argument to EOS
\r
707 ASSERT(!my_string_empty(argument));
\r
709 } else if (my_string_equal(option,"tbhits")) {
\r
711 ASSERT(!my_string_empty(argument));
\r
713 ln = my_atoll(argument);
\r
716 } else if (my_string_equal(option,"time")) {
\r
718 ASSERT(!my_string_empty(argument));
\r
720 n = atoi(argument);
\r
723 if (n >= 0) uci->time = ((double)n) / 1000.0;
\r
727 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
728 // This should probably be protected
\r
729 // by a "WorkAround" option.
\r
730 snprintf(uci->info,sizeof(uci->info),"%s %s",option,argument);
\r
731 uci->info[sizeof(uci->info)-1]='\0';
\r
736 parse_close(parse);
\r
740 if ((event & EVENT_PV) != 0) {
\r
741 uci->best_score = uci->score;
\r
742 uci->best_sel_depth = uci->sel_depth;
\r
743 line_copy(uci->best_pv,uci->pv);
\r
745 if(uci->depth < uci->best_depth){
\r
746 // ignore lines of lower depth
\r
747 event &= ~EVENT_PV;
\r
749 if(uci->depth > uci->best_depth) {
\r
750 // clear stack when we start new depth
\r
751 uci->multipvSP = 0;
\r
753 uci->best_depth = uci->depth;
\r
754 if(multipvline >= 1) {
\r
756 for(i=0; i<uci->multipvSP; i++) {
\r
757 if(uci->score == uci->multipvScore[i] && uci->pv[0] == uci->multipvMove[i]) {
\r
758 event &= ~EVENT_PV; // ignore duplicates
\r
761 if(event & EVENT_PV){
\r
762 // line is new, try to add to stack
\r
763 if(uci->multipvSP<MultiPVStackSize){
\r
764 uci->multipvMove[uci->multipvSP] = uci->pv[0];
\r
765 uci->multipvScore[uci->multipvSP] = uci->score;
\r
768 my_fatal("parse_info(): multipv stack overflow.");
\r
780 static void parse_option(uci_t * uci, const char string[]) {
\r
784 char command[StringSize];
\r
785 char option[StringSize];
\r
786 char argument[StringSize];
\r
789 ASSERT(string!=NULL);
\r
793 strcpy(command,"option");
\r
795 memset(opt,0,sizeof(option_t));
\r
797 my_string_set(&opt->value,"<empty>");
\r
798 my_string_set(&opt->name,"<empty>");
\r
799 my_string_set(&opt->default_,"<empty>");
\r
800 my_string_set(&opt->max,"<empty>");
\r
801 my_string_set(&opt->min,"<empty>");
\r
802 my_string_set(&opt->type,"<empty>");
\r
806 parse_open(parse,string);
\r
807 parse_add_keyword(parse,"default");
\r
808 parse_add_keyword(parse,"max");
\r
809 parse_add_keyword(parse,"min");
\r
810 parse_add_keyword(parse,"name");
\r
811 parse_add_keyword(parse,"type");
\r
812 parse_add_keyword(parse,"var");
\r
816 while (parse_get_word(parse,option,StringSize)) {
\r
817 parse_get_string(parse,argument,StringSize);
\r
818 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
822 } else if (my_string_equal(option,"default")) {
\r
824 // ASSERT(!my_string_empty(argument)); // HACK for Pepito
\r
826 if (!my_string_empty(argument)) {
\r
827 my_string_set(&opt->default_,argument);
\r
828 my_string_set(&opt->value,argument);
\r
831 } else if (my_string_equal(option,"max")) {
\r
833 ASSERT(!my_string_empty(argument));
\r
834 my_string_set(&opt->max,argument);
\r
836 } else if (my_string_equal(option,"min")) {
\r
838 ASSERT(!my_string_empty(argument));
\r
839 my_string_set(&opt->min,argument);
\r
841 } else if (my_string_equal(option,"name")) {
\r
843 ASSERT(!my_string_empty(argument));
\r
845 if (!my_string_empty(argument)) {
\r
846 my_string_set(&opt->name,argument);
\r
849 } else if (my_string_equal(option,"type")) {
\r
851 ASSERT(!my_string_empty(argument));
\r
852 my_string_set(&opt->type,argument);
\r
854 } else if (my_string_equal(option,"var")) {
\r
856 ASSERT(!my_string_empty(argument));
\r
857 my_string_set(&opt->var[opt->var_nb++],argument);
\r
858 if(opt->var_nb==VarNb) break;
\r
862 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
866 parse_close(parse);
\r
868 apply_UCI3_heuristics(opt);
\r
869 option_insert(uci->option,opt);
\r
872 if (UseDebug) my_log("POLYGLOT option name \"%s\" default \"%s\"\n",opt->name,opt->default_);
\r
877 static void parse_score(uci_t * uci, const char string[]) {
\r
880 char command[StringSize];
\r
881 char option[StringSize];
\r
882 char argument[StringSize];
\r
885 ASSERT(uci_is_ok(uci));
\r
886 ASSERT(string!=NULL);
\r
890 strcpy(command,"score");
\r
892 parse_open(parse,string);
\r
893 parse_add_keyword(parse,"cp");
\r
894 parse_add_keyword(parse,"lowerbound");
\r
895 parse_add_keyword(parse,"mate");
\r
896 parse_add_keyword(parse,"upperbound");
\r
900 while (parse_get_word(parse,option,StringSize)) {
\r
902 parse_get_string(parse,argument,StringSize);
\r
904 if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" OPTION \"%s\" ARGUMENT \"%s\"\n",command,option,argument);
\r
908 } else if (my_string_equal(option,"cp")) {
\r
910 ASSERT(!my_string_empty(argument));
\r
912 n = atoi(argument);
\r
916 } else if (my_string_equal(option,"lowerbound")) {
\r
918 ASSERT(my_string_empty(argument));
\r
920 } else if (my_string_equal(option,"mate")) {
\r
922 ASSERT(!my_string_empty(argument));
\r
924 n = atoi(argument);
\r
927 uci->score = mate_score(n);
\r
929 } else if (my_string_equal(option,"upperbound")) {
\r
931 ASSERT(my_string_empty(argument));
\r
935 my_log("POLYGLOT unknown option \"%s\" for command \"%s\"\n",option,command);
\r
939 parse_close(parse);
\r
944 static int mate_score(int dist) {
\r
949 } else if (dist > 0) {
\r
950 return +option_get_int(Option,"MateScore") - (+dist) * 2 + 1;
\r
951 } else if (dist < 0) {
\r
952 return -option_get_int(Option,"MateScore") + (-dist) * 2;
\r