15 #include "move_legal.h"
24 #define StringSize 4096
28 static board_t UCIboard[1];
29 static bool Init=TRUE;
30 static int SavedMove=MoveNone;
34 static void send_uci_options();
39 static void normalize_type(char *dst, const char* src){
40 if(option_get_int(Option,"UCIVersion") <=2){
44 }else if(IS_SPIN(src)){
46 }else if(IS_BUTTON(src)){
58 static void parse_position(const char string[]) {
60 /* This is borrowed from Toga II. This code is quite hacky and will be
61 rewritten using the routines in parse.cpp.
67 char move_string[256];
73 string_copy=my_strdup(string);
75 fen = strstr(string_copy,"fen ");
76 moves = strstr(string_copy,"moves ");
80 if (fen != NULL) { // "fen" present
82 if (moves != NULL) { // "moves" present
84 moves[-1] = '\0'; // dirty, but so is UCI
87 board_from_fen(UCIboard,fen+4); // CHANGE ME
91 // HACK: assumes startpos
93 board_from_fen(UCIboard,StartFen);
98 if (moves != NULL) { // "moves" present
102 while (*ptr != '\0') {
104 while (*ptr == ' ') ptr++;
106 move_string[0] = *ptr++;
107 move_string[1] = *ptr++;
108 move_string[2] = *ptr++;
109 move_string[3] = *ptr++;
111 if (*ptr == '\0' || *ptr == ' ') {
112 move_string[4] = '\0';
114 move_string[4] = *ptr++;
115 move_string[5] = '\0';
117 move = move_from_can(move_string,UCIboard);
119 move_do(UCIboard,move);
129 static void send_book_move(int move){
130 char move_string[256];
131 my_log("POLYGLOT *BOOK MOVE*\n");
132 move_to_can(move,UCIboard,move_string,256);
134 gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0");
135 gui_send(GUI,"bestmove %s",move_string);
138 // format_uci_option_line()
140 static void format_uci_option_line(char * option_line,option_t *opt){
141 char option_string[StringSize];
142 char type[StringSize];
144 strcpy(option_line,"");
145 // buffer overflow alert
146 strcat(option_line,"option name");
148 strcat(option_line," Polyglot");
150 sprintf(option_string," %s",opt->name);
151 strcat(option_line,option_string);
152 normalize_type(type,opt->type);
153 sprintf(option_string," type %s",type);
154 strcat(option_line,option_string);
155 if(!IS_BUTTON(opt->type)){
156 sprintf(option_string," default %s",opt->value);
157 strcat(option_line,option_string);
159 if(IS_SPIN(opt->type)){
160 sprintf(option_string," min %s",opt->min);
161 strcat(option_line,option_string);
163 if(IS_SPIN(opt->type)){
164 sprintf(option_string," max %s",opt->max);
165 strcat(option_line,option_string);
167 for(j=0;j<opt->var_nb;j++){
168 sprintf(option_string," var %s",opt->var[j]);
169 strcat(option_line,option_string);
173 // send_uci_options()
175 static void send_uci_options() {
178 char option_line[StringSize]="";
179 gui_send(GUI,"id name %s", Uci->name);
180 gui_send(GUI,"id author %s", Uci->author);
181 option_start_iter(Uci->option);
182 while((opt=option_next(Uci->option))){
183 format_uci_option_line(option_line,opt);
184 gui_send(GUI,"%s",option_line);
186 option_start_iter(Option);
187 while((opt=option_next(Option))){
189 format_uci_option_line(option_line,opt);
190 gui_send(GUI,"%s",option_line);
193 gui_send(GUI,"uciok");
200 static void parse_setoption(const char string[]) {
205 string_copy=my_strdup(string);
206 if(match(string_copy,"setoption name * value *")){
209 if(match(name, "Polyglot *")){
211 polyglot_set_option(pg_name,value);
213 engine_send(Engine,"%s",string);
216 engine_send(Engine,"%s",string);
222 // uci2uci_gui_step()
224 void uci2uci_gui_step(char string[]) {
227 }else if(match(string,"uci")){
230 }else if(match(string,"setoption *")){
231 parse_setoption(string);
233 }else if(match(string,"position *")){
234 parse_position(string);
236 }else if(match(string,"go *")){
238 board_from_fen(UCIboard,StartFen);
242 if(!strstr(string,"infinite")){
243 move=book_move(UCIboard,option_get_bool(Option,"BookRandom"));
244 if (move != MoveNone && move_is_legal(move,UCIboard)) {
245 if(strstr(string,"ponder")){
249 send_book_move(move);
254 }else if(match(string,"ponderhit") || match(string,"stop")){
255 if(SavedMove!=MoveNone){
256 send_book_move(SavedMove);
260 }else if(match(string,"quit")){
261 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
264 engine_send(Engine,"%s",string);
267 void uci2uci_engine_step(char string[]) {
268 gui_send(GUI,string);
271 // end of uci2uci.cpp