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