version 1.4w10UCIb17
[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,j;
150     option_t *p=Option;
151     const char * name;
152     char option_line[StringSize]="";
153     gui_send(GUI,"id name %s", Uci->name);
154     gui_send(GUI,"id author %s", Uci->author);
155     for(i=0;i<Uci->option_nb;i++){
156         format_uci_option_line(option_line,Uci->option+i);
157          gui_send(GUI,"%s",option_line);
158     }
159     while(p->name){
160         if(p->mode &UCI){
161             format_uci_option_line(option_line,p);
162             gui_send(GUI,"%s",option_line);
163         }
164         p++;
165     }   
166     gui_send(GUI,"uciok");
167 }
168
169 // parse_setoption()
170
171
172
173 static void parse_setoption(const char string[]) {
174     parse_t parse[1];
175     char *name;
176     char *pg_name;
177     char *value;
178     char * string_copy;
179     string_copy=my_strdup(string);
180     if(match(string_copy,"setoption name * value *")){
181         name=Star[0];
182         value=Star[1];
183         if(match(name, "Polyglot *")){
184             pg_name=Star[0];
185             polyglot_set_option(pg_name,value);
186         }else{
187             engine_send(Engine,"%s",string);
188         }
189     }else{
190         engine_send(Engine,"%s",string);
191     }
192     free(string_copy);
193 }
194
195
196 // uci_gui_step()
197
198 void uci_gui_step(char string[]) {
199     int move;
200      if(false){
201      }else if(match(string,"uci")){
202          send_uci_options();
203          return;
204      }else if(match(string,"setoption *")){
205          parse_setoption(string);
206          return;
207      }else if(match(string,"position *")){
208          parse_position(string);
209          Init=false;
210      }else if(match(string,"go *")){
211          if(Init){
212              board_from_fen(UCIboard,StartFen);
213              Init=false;
214          }
215          SavedMove=MoveNone;
216          if(!strstr(string,"infinite")){
217              move=book_move(UCIboard,option_get_bool("BookRandom"));
218              if (move != MoveNone && move_is_legal(move,UCIboard)) {
219                  if(strstr(string,"ponder")){
220                      SavedMove=move;
221                      return;
222                  }else{
223                      send_book_move(move);
224                      return;
225                  }
226              }
227          }
228      }else if(match(string,"ponderhit") || match(string,"stop")){
229          if(SavedMove!=MoveNone){
230                 send_book_move(SavedMove);
231                 SavedMove=MoveNone;
232                 return;
233          }
234      }else if(match(string,"quit")){
235          my_log("POLYGLOT *** \"quit\" from GUI ***\n");
236          quit();
237      }
238      engine_send(Engine,"%s",string);
239 }
240
241 void uci_engine_step(char string[]) {
242     gui_send(GUI,string);
243 }
244
245 // end of uci2uci.cpp