Check in polyglot-1.4w10UCIb15
[polyglot.git] / uci2uci.cpp
1 // uci2uci.cpp
2
3 // includes
4
5 #include <cstring>
6 #include <cstdlib>
7
8 #include "util.h"
9 #include "adapter.h"
10 #include "board.h"
11 #include "engine.h"
12 #include "fen.h"
13 #include "gui.h"
14 #include "move.h"
15 #include "move_do.h"
16 #include "move_legal.h"
17 #include "parse.h"
18 #include "option.h"
19 #include "book.h"
20 #include "main.h"
21 #include "uci.h"
22
23 // constants
24
25 static const int StringSize = 4096;
26
27 // variables 
28
29 static board_t UCIboard[1];
30 static bool Init=true;
31 static int SavedMove=MoveNone;
32
33 // defines
34
35 #ifdef _WIN32
36 #define strcasecmp lstrcmpi
37 #endif
38
39 // parse_position()
40
41 static void parse_position(const char string[]) {
42
43 /* This is borrowed from Toga II. This code is quite hacky and will be
44    rewritten using the routines in parse.cpp.
45 */
46                                                    
47    const char * fen;
48    char * moves;
49    const char * ptr;
50    char move_string[256];
51    int move;
52    char * string_copy;
53
54    // init
55
56    string_copy=my_strdup(string);
57    
58    fen = strstr(string_copy,"fen ");
59    moves = strstr(string_copy,"moves ");
60
61    // start position
62
63    if (fen != NULL) { // "fen" present
64
65       if (moves != NULL) { // "moves" present
66          ASSERT(moves>fen);
67          moves[-1] = '\0'; // dirty, but so is UCI
68       }
69
70       board_from_fen(UCIboard,fen+4); // CHANGE ME
71
72    } else {
73
74       // HACK: assumes startpos
75
76       board_from_fen(UCIboard,StartFen);
77    }
78
79    // moves
80
81    if (moves != NULL) { // "moves" present
82
83       ptr = moves + 6;
84
85       while (*ptr != '\0') {
86
87          while (*ptr == ' ') ptr++;
88
89          move_string[0] = *ptr++;
90          move_string[1] = *ptr++;
91          move_string[2] = *ptr++;
92          move_string[3] = *ptr++;
93
94          if (*ptr == '\0' || *ptr == ' ') {
95             move_string[4] = '\0';
96          } else { // promote
97             move_string[4] = *ptr++;
98             move_string[5] = '\0';
99          }
100          move = move_from_can(move_string,UCIboard);
101
102          move_do(UCIboard,move);
103
104       }
105    }
106    free(string_copy);
107 }
108
109
110 // send_book_move()
111
112 static void send_book_move(int move){
113     char move_string[256];
114     my_log("POLYGLOT *BOOK MOVE*\n");
115     move_to_can(move,UCIboard,move_string,256);
116         // bogus info lines
117     gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0");
118     gui_send(GUI,"bestmove %s",move_string);
119 }
120
121 // send_uci_options()
122
123 void send_uci_options() {
124     int i;
125     char option_line[StringSize]="";
126     char option_string[StringSize]="";
127     gui_send(GUI,"id name %s", Uci->name);
128     gui_send(GUI,"id author %s", Uci->author);
129     for(i=0;i<Uci->option_nb;i++){
130         strcat(option_line,"option");
131         if(strcmp(Uci->option[i].name,"<empty>")){
132             sprintf(option_string," name %s",Uci->option[i].name);
133             strcat(option_line,option_string);
134         }
135         if(strcmp(Uci->option[i].type,"<empty>")){
136             sprintf(option_string," type %s",Uci->option[i].type);
137             strcat(option_line,option_string);
138         }
139         if(strcmp(Uci->option[i].value,"<empty>")){
140             sprintf(option_string," default %s",Uci->option[i].value);
141             strcat(option_line,option_string);
142         }
143         if(strcmp(Uci->option[i].min,"<empty>")){
144             sprintf(option_string," min %s",Uci->option[i].min);
145             strcat(option_line,option_string);
146         }
147         if(strcmp(Uci->option[i].max,"<empty>")){
148             sprintf(option_string," max %s",Uci->option[i].max);
149             strcat(option_line,option_string);
150         }
151         if(strcmp(Uci->option[i].var,"<empty>")){
152             sprintf(option_string," var %s",Uci->option[i].var);
153             strcat(option_line,option_string);
154         }
155         gui_send(GUI,"%s",option_line);
156         strcpy(option_line,"");
157     }
158     gui_send(GUI,"uciok");
159 }
160
161 // parse_setoption()
162
163
164
165 static void parse_setoption(const char string[]) {
166     parse_t parse[1];
167     char option[StringSize];
168     char name[StringSize];
169     char value[StringSize];
170     char * string_copy;
171     string_copy=my_strdup(string);
172     parse_open(parse,string_copy);
173     parse_add_keyword(parse,"setoption");
174     parse_add_keyword(parse,"name");
175     parse_add_keyword(parse,"value");
176     parse_get_word(parse,option,StringSize);
177     ASSERT(my_string_equal(option,"setoption"));
178     if(parse_get_word(parse,option,StringSize)){
179         if(my_string_equal(option,"name")){
180             parse_get_string(parse,name,StringSize);
181             if(parse_get_word(parse,option,StringSize)){
182                 if(my_string_equal(option,"value")){
183                     parse_get_string(parse,value,StringSize);
184                     if(my_string_case_equal(name,PolyglotBookFile)){
185                         my_log("POLYGLOT *** SETTING BOOK ***\n");
186                         my_log("POLYGLOT BOOK \"%s\"\n",value);
187                         book_close();
188                         book_clear();
189                         book_open(value);
190                         if(!book_is_open()){
191                             my_log("POLYGLOT Unable to open book \"%s\"\n",value);
192                         }
193                     }else{
194                         engine_send(Engine,"%s",string);       
195                     }
196                 }
197             }
198         }
199     }
200     parse_close(parse);
201     free(string_copy);
202 }
203
204
205 // uci_gui_step()
206
207 void uci_gui_step(char string[]) {
208     int move;
209      if(false){
210      }else if(match(string,"uci")){
211          send_uci_options();
212          return;
213      }else if(match(string,"setoption *")){
214          parse_setoption(string);
215          return;
216      }else if(match(string,"position *")){
217          parse_position(string);
218          Init=false;
219      }else if(match(string,"go *")){
220          if(Init){
221              board_from_fen(UCIboard,StartFen);
222              Init=false;
223          }
224          SavedMove=MoveNone;
225          if(!strstr(string,"infinite")){
226              move=book_move(UCIboard,option_get_bool("BookRandom"));
227              if (move != MoveNone && move_is_legal(move,UCIboard)) {
228                  if(strstr(string,"ponder")){
229                      SavedMove=move;
230                      return;
231                  }else{
232                      send_book_move(move);
233                      return;
234                  }
235              }
236          }
237      }else if(match(string,"ponderhit") || match(string,"stop")){
238          if(SavedMove!=MoveNone){
239                 send_book_move(SavedMove);
240                 SavedMove=MoveNone;
241                 return;
242          }
243      }else if(match(string,"quit")){
244          my_log("POLYGLOT *** \"quit\" from GUI ***\n");
245          quit();
246      }
247      engine_send(Engine,"%s",string);
248 }
249
250 void uci_engine_step(char string[]) {
251     gui_send(GUI,string);
252 }
253
254 // end of uci2uci.cpp