File: uci2uci.c

package info (click to toggle)
polyglot 2.0.1%2Bgit20140926-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,228 kB
  • ctags: 1,163
  • sloc: ansic: 10,416; sh: 994; makefile: 18
file content (272 lines) | stat: -rw-r--r-- 6,367 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// uci2uci.c

// includes

#include <string.h>
#include <stdlib.h>

#include "util.h"
#include "board.h"
#include "engine.h"
#include "fen.h"
#include "gui.h"
#include "move.h"
#include "move_do.h"
#include "move_legal.h"
#include "parse.h"
#include "option.h"
#include "book.h"
#include "main.h"
#include "uci.h"

// defines

#define StringSize 4096

// variables 

static board_t UCIboard[1];
static bool Init=TRUE;
static int SavedMove=MoveNone;

// prototypes

static void send_uci_options();


// normalize_type()

static void normalize_type(char *dst, const char* src){
  if(option_get_int(Option,"UCIVersion") <=2){
    if(IS_STRING(src)){
      strcpy(dst,"string");
      return;
    }else if(IS_SPIN(src)){
      strcpy(dst,"spin");
    }else if(IS_BUTTON(src)){
      strcpy(dst,"button");
    }else{
      strcpy(dst,src);
    }
  }else{
    strcpy(dst,src);
  }
}

// parse_position()

static void parse_position(const char string[]) {

/* This is borrowed from Toga II. This code is quite hacky and will be
   rewritten using the routines in parse.cpp.
*/
                                                   
   const char * fen;
   char * moves;
   const char * ptr;
   char move_string[256];
   int move;
   char * string_copy;

   // init

   string_copy=my_strdup(string);
   
   fen = strstr(string_copy,"fen ");
   moves = strstr(string_copy,"moves ");

   // start position

   if (fen != NULL) { // "fen" present

      if (moves != NULL) { // "moves" present
         ASSERT(moves>fen);
         moves[-1] = '\0'; // dirty, but so is UCI
      }

      board_from_fen(UCIboard,fen+4); // CHANGE ME

   } else {

      // HACK: assumes startpos

      board_from_fen(UCIboard,StartFen);
   }

   // moves

   if (moves != NULL) { // "moves" present

      ptr = moves + 6;

      while (*ptr != '\0') {

         while (*ptr == ' ') ptr++;

         move_string[0] = *ptr++;
         move_string[1] = *ptr++;
         move_string[2] = *ptr++;
         move_string[3] = *ptr++;

         if (*ptr == '\0' || *ptr == ' ') {
            move_string[4] = '\0';
         } else { // promote
            move_string[4] = *ptr++;
            move_string[5] = '\0';
         }
         move = move_from_can(move_string,UCIboard);

         move_do(UCIboard,move);

      }
   }
   free(string_copy);
}


// send_book_move()

static void send_book_move(int move){
    char move_string[256];
    my_log("POLYGLOT *BOOK MOVE*\n");
    move_to_can(move,UCIboard,move_string,256);
        // bogus info lines
    gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0");
    gui_send(GUI,"bestmove %s",move_string);
}

// format_uci_option_line()

static void format_uci_option_line(char * option_line,option_t *opt){
    char option_string[StringSize];
    char type[StringSize];
    int j;
    strcpy(option_line,"");
        // buffer overflow alert
    strcat(option_line,"option name");
    if(opt->mode&PG){
        strcat(option_line," Polyglot");
    }
    sprintf(option_string," %s",opt->name);
    strcat(option_line,option_string);
    normalize_type(type,opt->type);
    sprintf(option_string," type %s",type);
    strcat(option_line,option_string);
    if(!IS_BUTTON(opt->type)){
        sprintf(option_string," default %s",opt->value);
        strcat(option_line,option_string);
    }
    if(IS_SPIN(opt->type)){
        sprintf(option_string," min %s",opt->min);
        strcat(option_line,option_string);
    }
    if(IS_SPIN(opt->type)){
        sprintf(option_string," max %s",opt->max);
        strcat(option_line,option_string);
    }
    for(j=0;j<opt->var_nb;j++){
        sprintf(option_string," var %s",opt->var[j]);
        strcat(option_line,option_string);
    }
}

// send_uci_options()

static void send_uci_options() {

    option_t * opt;
    char option_line[StringSize]="";
    gui_send(GUI,"id name %s", Uci->name);
    gui_send(GUI,"id author %s", Uci->author);
    option_start_iter(Uci->option);
    while((opt=option_next(Uci->option))){
        format_uci_option_line(option_line,opt);
        gui_send(GUI,"%s",option_line);
    }
    option_start_iter(Option);
    while((opt=option_next(Option))){
        if(opt->mode &UCI){
            format_uci_option_line(option_line,opt);
            gui_send(GUI,"%s",option_line);
        }
    }   
    gui_send(GUI,"uciok");
}

// parse_setoption()



static void parse_setoption(const char string[]) {
    char *name;
    char *pg_name;
    char *value;
    char * string_copy;
    string_copy=my_strdup(string);
    if(match(string_copy,"setoption name * value *")){
        name=Star[0];
        value=Star[1];
        if(match(name, "Polyglot *")){
            pg_name=Star[0];
            polyglot_set_option(pg_name,value);
        }else{
            engine_send(Engine,"%s",string);
        }
    }else{
        engine_send(Engine,"%s",string);
    }
    free(string_copy);
}


// uci2uci_gui_step()

void uci2uci_gui_step(char string[]) {
    int move;
     if(FALSE){
     }else if(match(string,"uci")){
         send_uci_options();
         return;
     }else if(match(string,"setoption *")){
         parse_setoption(string);
         return;
     }else if(match(string,"position *")){
         parse_position(string);
         Init=FALSE;
     }else if(match(string,"go *")){
         if(Init){
             board_from_fen(UCIboard,StartFen);
             Init=FALSE;
         }
         SavedMove=MoveNone;
         if(!strstr(string,"infinite") 
	    && UCIboard->move_nb<option_get_int(Option,"BookDepth")){
             move=book_move(UCIboard,option_get_bool(Option,"BookRandom"));
             if (move != MoveNone && move_is_legal(move,UCIboard)) {
                 if(strstr(string,"ponder")){
                     SavedMove=move;
                     return;
                 }else{
                     send_book_move(move);
                     return;
                 }
             }
         }
     }else if(match(string,"ponderhit") || match(string,"stop")){
         if(SavedMove!=MoveNone){
         	send_book_move(SavedMove);
         	SavedMove=MoveNone;
         	return;
         }
     }else if(match(string,"quit")){
         my_log("POLYGLOT *** \"quit\" from GUI ***\n");
         quit();
     }
     engine_send(Engine,"%s",string);
}

void uci2uci_engine_step(char string[]) {
    gui_send(GUI,string);
}

// end of uci2uci.cpp