static void parse_option() { const char * file_name; FILE * file; char line[256]; char * name, * value; file_name = option_get_string("OptionFile"); file = fopen(file_name,"r"); if (file == NULL) my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno)); // PolyGlot options (assumed first) while (true) { if (!my_file_read_line(file,line,256)) { my_fatal("parse_option(): missing [Engine] section\n"); } if (my_string_case_equal(line,"[engine]")) break; if (parse_line(line,&name,&value)) option_set(name,value); } if (option_get_bool("Log")) { my_log_open(option_get_string("LogFile")); } my_log("POLYGLOT %s *** START ***\n",Version); my_log("POLYGLOT INI file \"%s\"\n",file_name); engine_open(Engine); Init = true; uci_open(Uci,Engine); while (my_file_read_line(file,line,256)) { if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line); if (parse_line(line,&name,&value)) { uci_send_option(Uci,name,"%s",value); //to get a decent display in winboard_x we need to now if an engine really is doing multipv analysis // "multipv 1" in the pv is meaningless,f.i. toga sends that all the time //therefore check if MultiPV is set to a decent value in the polyglot ini file if(my_string_case_equal(name,"MultiPV") && atoi(value)>1) Uci->multipv_mode=true; } } uci_send_isready(Uci); fclose(file); if (my_string_equal(option_get_string("EngineName"),"<empty>")) { option_set("EngineName",Uci->name); } }
void uci_send_option(uci_t * uci, const char option[], const char format[], ...) { va_list arg_list; char value[StringSize]; int i; option_t * opt; ASSERT(uci_is_ok(uci)); ASSERT(option!=NULL); ASSERT(format!=NULL); // format va_start(arg_list,format); vsprintf(value,format,arg_list); va_end(arg_list); if (UseDebug) my_log("POLYGLOT OPTION %s VALUE %s\n",option,value); // scan options for (i = 0; i < uci->option_nb; i++) { opt = &uci->option[i]; if (my_string_case_equal(opt->name,option) && !my_string_equal(opt->default_,value)) { engine_send(uci->engine,"setoption name %s value %s",opt->name,value); my_string_set(&opt->default_,value); break; } } }
bool option_get_bool(const char var[]) { const char * val; val = option_get(var); if (false) { } else if (my_string_case_equal(val,"true") || my_string_case_equal(val,"yes") || my_string_equal(val,"1")) { return true; } else if (my_string_case_equal(val,"false") || my_string_case_equal(val,"no") || my_string_equal(val,"0")) { return false; } ASSERT(false); return false; }
bool option_get_bool(option_list_t *option, const char name[]) { const char * value; value = option_get(option,name); if (FALSE) { } else if (my_string_case_equal(value,"true") || my_string_case_equal(value,"yes") || my_string_equal(value,"1")) { return TRUE; } else if (my_string_case_equal(value,"false") || my_string_case_equal(value,"no") || my_string_equal(value,"0")) { return FALSE; } ASSERT(FALSE); return FALSE; }
int uci_get_option(uci_t * uci, const char * name){ int i; for(i=0;i<Uci->option_nb;i++){ if(my_string_case_equal(Uci->option[i].name,name)){ return i; } } return -1; }
static option_t * option_find(const char var[]) { option_t * opt; ASSERT(var!=NULL); for (opt = &Option[0]; opt->var != NULL; opt++) { if (my_string_case_equal(opt->var,var)) return opt; } return NULL; }
void format_xboard_option_line(char * option_line, option_t *opt){ int j; char option_string[StringSize]; char *tmp; strcpy(option_line,""); // buffer overflow alert strcat(option_line,"feature option=\""); if(opt->mode&PG){ strcat(option_line,"Polyglot "); } sprintf(option_string,"%s",opt->name); strcat(option_line,option_string); sprintf(option_string," -%s",opt->type); strcat(option_line,option_string); if(!IS_BUTTON(opt->type) && strcmp(opt->type,"combo")){ if(strcmp(opt->type,"check")){ sprintf(option_string," %s",opt->value); }else{ sprintf(option_string," %d", my_string_case_equal(opt->value,"true")|| my_string_equal(opt->value,"1") ?1:0); } strcat(option_line,option_string); } if(IS_SPIN(opt->type)){ sprintf(option_string," %s",opt->min); strcat(option_line,option_string); } if(IS_SPIN(opt->type)){ sprintf(option_string," %s",opt->max); strcat(option_line,option_string); } for(j=0;j<opt->var_nb;j++){ if(!strcmp(opt->var[j],opt->value)){ sprintf(option_string," *%s",opt->var[j]); }else{ sprintf(option_string," %s",opt->var[j]); } strcat(option_line,option_string); if(j!=opt->var_nb-1){ strcat(option_line," ///"); } } strcat(option_line,"\""); if(option_get_bool(Option,"WbWorkArounds") && (tmp=strstr(option_line,"Draw"))){ *tmp='d'; my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n", opt->name); } }
bool option_set_default(option_list_t *option, const char name[], const char value[]) { option_t * opt; ASSERT(name!=NULL); ASSERT(value!=NULL); opt = option_find(option,name); if (opt == NULL) return FALSE; if(my_string_case_equal(opt->type,"check")){ value=(my_string_equal(value,"1")|| my_string_case_equal(value,"true"))?"true":"false"; } my_string_set(&opt->default_,value); if (UseDebug) my_log("POLYGLOT OPTION DEFAULT SET \"%s\" -> \"%s\"\n",opt->name,opt->default_); return TRUE; }
option_t * option_find(option_list_t *option, const char name[]) { option_t * opt; int i; ASSERT(name!=NULL); for (i=0; i<option->option_nb; i++){ opt=option->options+i; if (my_string_case_equal(opt->name,name)){ return opt; } } return NULL; }
bool uci_option_exist(uci_t * uci, const char option[]) { int i; option_t * opt; ASSERT(uci_is_ok(uci)); ASSERT(option!=NULL); // scan options for (i = 0; i < uci->option_nb; i++) { opt = &uci->option[i]; if (my_string_case_equal(opt->name,option)) return true; } return false; }
void xboard2uci_send_options(){ char option_line[StringSize]=""; const char * name; option_t *opt; option_start_iter(Uci->option); while((opt=option_next(Uci->option))){ if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue; if(my_string_case_equal(opt->name,"UCI_Opponent")) continue; if(my_string_case_equal(opt->name,"UCI_3Check")) continue; if(my_string_case_equal(opt->name,"UCI_Chess960")) continue; if(my_string_case_equal(opt->name,"UCI_Atomic")) continue; if(my_string_case_equal(opt->name,"UCI_Horde")) continue; if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue; if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue; if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue; if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue; if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue; if(my_string_case_equal(opt->name,"Ponder")) continue; if(my_string_case_equal(opt->name,"Hash")) continue; if(my_string_case_equal(opt->name,"NalimovPath")) continue; if(my_string_case_equal(opt->name,"GaviotaTbPath")) continue; if((name=uci_thread_option(Uci))!=NULL && my_string_case_equal(opt->name,name)) continue; format_xboard_option_line(option_line,opt); gui_send(GUI,"%s",option_line); } option_start_iter(Option); while((opt=option_next(Option))){ if(opt->mode &XBOARD){ format_xboard_option_line(option_line,opt); gui_send(GUI,"%s",option_line); } } gui_send(GUI,"feature done=1"); }
void xboard2uci_gui_step(char string[]) { int move; char move_string[256]; board_t board[1]; if (FALSE) { } else if (match(string,"accepted *")) { // ignore } else if (match(string,"analyze")) { State->computer[White] = FALSE; State->computer[Black] = FALSE; XB->analyse = TRUE; XB->new_hack = FALSE; ASSERT(!XB->result); XB->result = FALSE; mess(); } else if (match(string,"bk")) { if (option_get_bool(Option,"Book")) { game_get_board(Game,board); book_disp(board); } } else if (match(string,"black")) { if (colour_is_black(game_turn(Game))) { State->computer[White] = TRUE; State->computer[Black] = FALSE; XB->new_hack = TRUE; XB->result = FALSE; mess(); } } else if (match(string,"computer")) { XB->computer = TRUE; } else if (match(string,"draw")) { if(option_find(Uci->option,"UCI_DrawOffers")){ my_log("POLYGLOT draw from XB received"); uci_send_option(Uci,"DrawOffer","%s","draw");} } else if (match(string,"easy")) { XB->ponder = FALSE; mess(); } else if (match(string,"edit")) { // refuse gui_send(GUI,"Error (unknown command): %s",string); } else if (match(string,"exit")) { State->computer[White] = FALSE; State->computer[Black] = FALSE; XB->analyse = FALSE; mess(); } else if (match(string,"force")) { State->computer[White] = FALSE; State->computer[Black] = FALSE; mess(); } else if (match(string,"go")) { State->computer[game_turn(Game)] = TRUE; State->computer[colour_opp(game_turn(Game))] = FALSE; XB->new_hack = FALSE; ASSERT(!XB->result); XB->result = FALSE; mess(); } else if (match(string,"hard")) { XB->ponder = TRUE; mess(); } else if (match(string,"hint")) { move=MoveNone; game_get_board(Game,board); if (option_get_bool(Option,"Book")) { move = book_move(board,FALSE); } if(move==MoveNone && State->hint_move!=MoveNone){ move=State->hint_move; } if (move != MoveNone && move_is_legal(move,board)) { move_to_san(move,board,move_string,256); gui_send(GUI,"Hint: %s",move_string); } } else if (match(string,"ics *")) { XB->ics = TRUE; } else if (match(string,"level * *:* *")) { XB->mps = atoi(Star[0]); XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2])); XB->inc = ((double)atoi(Star[3])); } else if (match(string,"level * * *")) { XB->mps = atoi(Star[0]); XB->base = ((double)atoi(Star[1])) * 60.0; XB->inc = ((double)atoi(Star[2])); } else if (match(string,"name *")) { my_string_set(&XB->name,Star[0]); } else if (match(string,"new")) { uci_send_isready_sync(Uci); my_log("POLYGLOT NEW GAME\n"); option_set(Option,"3Check","false"); option_set(Option,"Chess960","false"); option_set(Option,"Atomic","false"); option_set(Option,"Horde","false"); game_clear(Game); if (XB->analyse) { State->computer[White] = FALSE; State->computer[Black] = FALSE; } else { State->computer[White] = FALSE; State->computer[Black] = TRUE; } XB->new_hack = TRUE; XB->result = FALSE; XB->depth_limit = FALSE; XB->node_rate=-1; XB->computer = FALSE; my_string_set(&XB->name,"<empty>"); board_update(); mess(); uci_send_ucinewgame(Uci); } else if (match(string,"nopost")) { XB->post = FALSE; } else if (match(string,"otim *")) { XB->opp_time = ((double)atoi(Star[0])) / 100.0; if (XB->opp_time < 0.0) XB->opp_time = 0.0; } else if (match(string,"pause")) { // refuse gui_send(GUI,"Error (unknown command): %s",string); } else if (match(string,"ping *")) { // HACK; TODO: answer only after an engine move if (DelayPong) { if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping XB->ping = atoi(Star[0]); uci_send_isready_sync(Uci); } else { ASSERT(XB->ping==-1); gui_send(GUI,"pong %s",Star[0]); } } else if (match(string,"nps *")) { // fake WB play-by-nodes mode XB->node_rate = atoi(Star[0]); } else if (match(string,"playother")) { State->computer[game_turn(Game)] = FALSE; State->computer[colour_opp(game_turn(Game))] = TRUE; XB->new_hack = FALSE; ASSERT(!XB->result); XB->result = FALSE; mess(); } else if (match(string,"post")) { XB->post = TRUE; } else if (match(string,"protover *")) { XB->proto_ver = atoi(Star[0]); ASSERT(XB->proto_ver>=2); send_xboard_options(); } else if (match(string,"quit")) { my_log("POLYGLOT *** \"quit\" from GUI ***\n"); quit(); } else if (match(string,"random")) { // ignore } else if (match(string,"rating * *")) { // ignore } else if (match(string,"remove")) { if (game_pos(Game) >= 2) { game_goto(Game,game_pos(Game)-2); ASSERT(!XB->new_hack); XB->new_hack = FALSE; // HACK? XB->result = FALSE; board_update(); mess(); } } else if (match(string,"rejected *")) { // ignore } else if (match(string,"reset")) { // protover 3? // refuse gui_send(GUI,"Error (unknown command): %s",string); } else if (FALSE || match(string,"result * {*}") || match(string,"result * {* }") || match(string,"result * { *}") || match(string,"result * { * }")) { my_log("POLYGLOT GAME END\n"); XB->result = TRUE; mess(); // book learning if (FALSE && option_get_bool(Option,"Book") && option_get_bool(Option,"BookLearn")) { if (FALSE) { } else if (my_string_equal(Star[0],"1-0")) { learn(+1); } else if (my_string_equal(Star[0],"0-1")) { learn(-1); } else if (my_string_equal(Star[0],"1/2-1/2")) { learn(0); } } } else if (match(string,"resume")) { // refuse gui_send(GUI,"Error (unknown command): %s",string); } else if (match(string,"option *=*") || match(string,"option * =*") || match(string,"option *= *") || match(string,"option * = *") ){ char *name=Star[0]; char *value=Star[1]; if(match(name, "Polyglot *")){ char *pg_name=Star[0]; polyglot_set_option(pg_name,value); }else{ option_t *opt=option_find(Uci->option,name); if(opt){ if(my_string_case_equal(opt->type,"check")){ value=my_string_equal(value,"1")?"true":"false"; } start_protected_command(); uci_send_option(Uci, name, "%s", value); end_protected_command(); }else{ gui_send(GUI,"Error (unknown option): %s",name); } } } else if (match(string,"option *")){ char *name=Star[0]; if(match(name, "Polyglot *")){ char *pg_name=Star[0]; polyglot_set_option(pg_name,"<empty>"); }else{ start_protected_command(); // value is ignored if(!uci_send_option(Uci, name, "%s", "<empty>")){ gui_send(GUI,"Error (unknown option): %s",name); }; end_protected_command(); } } else if (XB->has_feature_smp && match(string,"cores *")){ int cores=atoi(Star[0]); if(cores>=1){ // updating the number of cores my_log("POLYGLOT setting the number of cores to %d\n",cores); start_protected_command(); uci_set_threads(Uci,cores); end_protected_command(); } else{ // refuse gui_send(GUI,"Error (unknown command): %s",string); } } else if (match(string,"egtpath * *")){ char *type=Star[0]; char *path=Star[1]; if(my_string_empty(path)){ // refuse gui_send(GUI,"Error (unknown command): %s",string); }else{ if(my_string_case_equal(type,"nalimov") && XB->has_feature_egt_nalimov){ // updating NalimovPath my_log("POLYGLOT setting the Nalimov path to %s\n",path); start_protected_command(); uci_send_option(Uci,"NalimovPath","%s",path); end_protected_command(); }else if(my_string_case_equal(type,"gaviota") && XB->has_feature_egt_gaviota){ // updating GaviotaPath my_log("POLYGLOT setting the Gaviota path to %s\n",path); start_protected_command(); uci_send_option(Uci,"GaviotaTbPath","%s",path); end_protected_command(); }else{ // refuse gui_send(GUI,"Error (unsupported table base format): %s",string); } } } else if (XB->has_feature_memory && match(string,"memory *")){ int memory = atoi(Star[0]); int egt_cache; int real_memory; if(memory>=1){ // updating the available memory option_t *opt; my_log("POLYGLOT setting the amount of memory to %dMb\n",memory); if(XB->has_feature_egt_nalimov && (opt=option_find(Uci->option,"NalimovCache"))){ egt_cache=atoi(opt->value); }else if(XB->has_feature_egt_gaviota && (opt=option_find(Uci->option,"GaviotaTbCache"))){ egt_cache=atoi(opt->value); }else{ egt_cache=0; } my_log("POLYGLOT EGTB Cache is %dMb\n",egt_cache); real_memory=memory-egt_cache; if(real_memory>0){ start_protected_command(); uci_send_option(Uci,"Hash", "%d", real_memory); end_protected_command(); } }else{ // refuse gui_send(GUI,"Error (unknown command): %s",string); } } else if (match(string,"sd *")) { XB->depth_limit = TRUE; XB->depth_max = atoi(Star[0]); } else if (match(string,"setboard *")) { my_log("POLYGLOT FEN %s\n",Star[0]); if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]); State->computer[White] = FALSE; State->computer[Black] = FALSE; XB->new_hack = TRUE; // HACK? XB->result = FALSE; board_update(); mess(); } else if (match(string,"st *")) { XB->time_limit = TRUE; XB->time_max = ((double)atoi(Star[0])); } else if (match(string,"time *")) { XB->my_time = ((double)atoi(Star[0])) / 100.0; if (XB->my_time < 0.0) XB->my_time = 0.0; } else if (match(string,"undo")) { if (game_pos(Game) >= 1) { game_goto(Game,game_pos(Game)-1); ASSERT(!XB->new_hack); XB->new_hack = FALSE; // HACK? XB->result = FALSE; board_update(); mess(); } } else if (match(string,"usermove *")) { game_get_board(Game,board); move = move_from_san(Star[0],board); if (move != MoveNone && move_is_legal(move,board)) { XB->new_hack = FALSE; ASSERT(!XB->result); XB->result = FALSE; move_step(move); no_mess(move); } else { gui_send(GUI,"Illegal move: %s",Star[0]); } } else if (match(string,"variant *")) { if (my_string_equal(Star[0],"3check")) { option_set(Option,"3Check","true"); } else { option_set(Option,"3Check","false"); } if (my_string_equal(Star[0],"fischerandom")) { option_set(Option,"Chess960","true"); } else { option_set(Option,"Chess960","false"); } if (my_string_equal(Star[0],"atomic")) { option_set(Option,"Atomic","true"); } else { option_set(Option,"Atomic","false"); } if (my_string_equal(Star[0],"horde")) { option_set(Option,"Horde","true"); game_init(Game,StartFenHorde); //gui_send(GUI,"setup %s",StartFenHorde); } else { option_set(Option,"Horde","false"); } } else if (match(string,"white")) { if (colour_is_white(game_turn(Game))) { State->computer[White] = FALSE; State->computer[Black] = TRUE; XB->new_hack = TRUE; XB->result = FALSE; mess(); } } else if (match(string,"xboard")) { // ignore } else if (match(string,".")) { // analyse info if (State->state == ANALYSE) { int depth=Uci->best_depth;//HACK: don't clear engine-output window... ASSERT(Uci->searching); ASSERT(Uci->pending_nb>=1); if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) { move_to_san(Uci->root_move,Uci->board,move_string,256); 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); } else { gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK } } } else if (match(string,"?")) { // move now if (State->state == THINK) { ASSERT(Uci->searching); ASSERT(Uci->pending_nb>=1); // HACK: just send "stop" to the engine if (Uci->searching) { my_log("POLYGLOT STOP SEARCH\n"); engine_send(Engine,"stop"); } } } else { // unknown command, maybe a move? game_get_board(Game,board); move = move_from_san(string,board); if (move != MoveNone && move_is_legal(move,board)) { XB->new_hack = FALSE; ASSERT(!XB->result); XB->result = FALSE; move_step(move); no_mess(move); } else if (move != MoveNone) { gui_send(GUI,"Illegal move: %s",string); } else { gui_send(GUI,"Error (unknown command): %s",string); } } return; }
static void parse_option() { const char * file_name; FILE * file; char line[256]; char * name, * value; file_name = option_get_string("OptionFile"); file = fopen(file_name,"r"); if (file == NULL) my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno)); // PolyGlot options (assumed first) //read the ini file,and store the name/value pairs while (true) { if (!my_file_read_line(file,line,sizeof(line))) { my_fatal("parse_option(): missing [Engine] section\n"); } if (my_string_case_equal(line,"[engine]")) break; if (parse_line(line,&name,&value)) option_set(name,value); } if (option_get_bool("Log")) { my_log_open(option_get_string("LogFile"));} while (my_file_read_line(file,line,sizeof(line))) { if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line); if (parse_line(line,&name,&value)) { uci_option_store(name,value); } } fclose(file); //read the optional global.ini file if(!option_get_bool("NoGlobals")){ file=fopen("globals.ini","r"); //override settings,if any if(file!=NULL){ while (true) { if (!my_file_read_line(file,line,sizeof(line))) { my_fatal("parse_option(): missing [Engine] section\n"); } if (my_string_case_equal(line,"[engine]")) break; if (parse_line(line,&name,&value)){ if(!my_string_case_equal(name,"LogFile")) option_set(name,value); } } if (option_get_bool("Log")) { my_log_open(option_get_string("LogFile")); } else my_log_close(); //close it while (my_file_read_line(file,line,sizeof(line))) { if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line); if (parse_line(line,&name,&value)) { uci_option_store(name,value); } } fclose(file); } } my_log("POLYGLOT %s *** START ***\n",Version); my_log("POLYGLOT INI file \"%s\"\n",file_name); //do the dump: engine_open(Engine); Init = true; uci_open(Uci,Engine); uci_option_t *next; init_uci_list(&next); while(next!=NULL){ if(next->var==NULL) break; uci_send_option(Uci,next->var,"%s",next->val); if(my_string_case_equal(next->var,"MultiPV") && atoi(next->val)>1) Uci->multipv_mode=true; next=next->next; } uci_send_isready(Uci); if (my_string_equal(option_get_string("EngineName"),"<empty>")) { option_set("EngineName",Uci->name); } }