version 1.4w10UCIb16
[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 // format_uci_option_line()
122
123 static void format_uci_option_line(char * option_line,option_t *opt){
124     char option_string[StringSize];
125     int j;
126     strcpy(option_line,"");
127     strcat(option_line,"option name");
128     if(opt->mode&PG){
129         strcat(option_line," Polyglot");
130     }
131     sprintf(option_string," %s",opt->name);
132     strcat(option_line,option_string);
133     sprintf(option_string," type %s",opt->type);
134     strcat(option_line,option_string);
135     if(strcmp(opt->type,"button")){
136         sprintf(option_string," default %s",opt->default_);
137         strcat(option_line,option_string);
138     }
139     if(!strcmp(opt->type,"spin")){
140         sprintf(option_string," min %s",opt->min);
141         strcat(option_line,option_string);
142     }
143     if(!strcmp(opt->type,"spin")){
144         sprintf(option_string," max %s",opt->max);
145         strcat(option_line,option_string);
146     }
147     for(j=0;j<opt->var_nb;j++){
148         sprintf(option_string," var %s",opt->var[j]);
149         strcat(option_line,option_string);
150     }
151 }
152
153 // send_uci_options()
154
155 void send_uci_options() {
156     int i,j;
157     option_t *p=Option;
158     const char * name;
159     char option_line[StringSize]="";
160     gui_send(GUI,"id name %s", Uci->name);
161     gui_send(GUI,"id author %s", Uci->author);
162     for(i=0;i<Uci->option_nb;i++){
163         format_uci_option_line(option_line,Uci->option+i);
164          gui_send(GUI,"%s",option_line);
165     }
166     while(p->name){
167         if(p->mode &UCI){
168             format_uci_option_line(option_line,p);
169             gui_send(GUI,"%s",option_line);
170         }
171         p++;
172     }   
173     gui_send(GUI,"uciok");
174 }
175
176 // parse_setoption()
177
178
179
180 static void parse_setoption(const char string[]) {
181     parse_t parse[1];
182     char *name;
183     char *pg_name;
184     char *value;
185     char * string_copy;
186     string_copy=my_strdup(string);
187     if(match(string_copy,"setoption name * value *")){
188         name=Star[0];
189         value=Star[1];
190         if(match(name, "Polyglot *")){
191             pg_name=Star[0];
192             polyglot_set_option(pg_name,value);
193         }else{
194             engine_send(Engine,"%s",string);
195         }
196     }else{
197         engine_send(Engine,"%s",string);
198     }
199     free(string_copy);
200 }
201
202
203 // uci_gui_step()
204
205 void uci_gui_step(char string[]) {
206     int move;
207      if(false){
208      }else if(match(string,"uci")){
209          send_uci_options();
210          return;
211      }else if(match(string,"setoption *")){
212          parse_setoption(string);
213          return;
214      }else if(match(string,"position *")){
215          parse_position(string);
216          Init=false;
217      }else if(match(string,"go *")){
218          if(Init){
219              board_from_fen(UCIboard,StartFen);
220              Init=false;
221          }
222          SavedMove=MoveNone;
223          if(!strstr(string,"infinite")){
224              move=book_move(UCIboard,option_get_bool("BookRandom"));
225              if (move != MoveNone && move_is_legal(move,UCIboard)) {
226                  if(strstr(string,"ponder")){
227                      SavedMove=move;
228                      return;
229                  }else{
230                      send_book_move(move);
231                      return;
232                  }
233              }
234          }
235      }else if(match(string,"ponderhit") || match(string,"stop")){
236          if(SavedMove!=MoveNone){
237                 send_book_move(SavedMove);
238                 SavedMove=MoveNone;
239                 return;
240          }
241      }else if(match(string,"quit")){
242          my_log("POLYGLOT *** \"quit\" from GUI ***\n");
243          quit();
244      }
245      engine_send(Engine,"%s",string);
246 }
247
248 void uci_engine_step(char string[]) {
249     gui_send(GUI,string);
250 }
251
252 // end of uci2uci.cpp