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