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