summaryrefslogtreecommitdiff
path: root/src/uci/response.c
blob: 7ebe49cd0e31f42b01c4f60a55df8fb76aa281f3 (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include "response.h"
#include "ipc.h"
#include "fen.h"
#include "bitboard.h"

void apply_option(uci_state *state, char *name, char *buffer) {
  int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
  for (int i = 0; i < settings_count; i++) {
    option_setting_t setting = state->option_settings[i];

    if (strcmp(setting.option_name, name)) continue;
    if (OPTION_SPIN == setting.type) {
      option_spin_setting_t data = setting.data.spin;
      int num = atoi(buffer);
      if (num < data.min) return;
      if (num > data.max) return;
      *setting.value.spin = num;
    } else if (OPTION_COMBO == setting.type) {
      option_combo_setting_t data = setting.data.combo;
      for (int j = 0; j < data.count; j++) {
        if (strcmp(data.combination[i], buffer)) continue;
        *setting.value.combo = j;
      }
    } else if (OPTION_CHECK == setting.type) {
      *setting.value.check = strcmp(buffer, "true") == 0;
    } else if (OPTION_STRING == setting.type) {
      free(setting.value.string->data);
      setting.value.string->length = strlen(buffer);
      setting.value.string->data = malloc(setting.value.string->length);
      memcpy(setting.value.string->data, buffer, setting.value.string->length);
    } else if (OPTION_BUTTON == setting.type) {
      *setting.value.button = true;
    } else { assert(0); }
    return;
  }
  if (state->debug) {
    printf("debug: invalid option\n");
  }
}

// examples from uci_min.txt
// option name Nullmove type check default true\n
// option name Selectivity type spin default 2 min 0 max 4\n
// option name Style type combo default Normal var Solid var Normal var Risky\n
// option name NalimovPath type string default c:\\n
// option name Clear Hash type button\n
void print_setting(option_setting_t setting) {
  printf("option name %s type ", setting.option_name);
  // accidently did the yoda style if statement, i am keeping it
  if (OPTION_SPIN == setting.type) {
    option_spin_setting_t data = setting.data.spin;
    printf(
      "spin default %d min %d max %d\n",
      data.default_value,
      data.min,
      data.max
    );
  } else if (OPTION_COMBO == setting.type) {
    option_combo_setting_t data = setting.data.combo;
    printf("combo default %s", data.combination[data.default_index]);
    for (int i = 0; i < data.count; i++) {
      printf(" var %s", data.combination[i]);
    }
    printf("\n");
  } else if (OPTION_CHECK == setting.type) {
    printf("check default %d\n", setting.data.check_default);
  } else if (OPTION_STRING == setting.type) {
    printf("string default %s\n", setting.data.string_default);
  } else if (OPTION_BUTTON == setting.type) {
    printf("button\n");
  } else { assert(0); }
}

enum {
  STATE_INITIAL,
  STATE_IDLE,
  STATE_SYNC,
  STATE_PING,
  STATE_ACTIVE,
  STATE_HALT,
};
int current_state = STATE_INITIAL;

void handle_uci(
  uci_state *state,
  engine_messages *engine_message,
  ucicmd cmd
) {
  if (current_state == STATE_INITIAL) {
    return handle_initial(state, cmd);
  }
  if (current_state == STATE_IDLE) {
    return handle_idle(state, cmd);
  }
  if (current_state == STATE_SYNC) {
    return handle_sync(state, cmd);
  }
  if (current_state == STATE_PING) {
    return handle_ping(state, cmd);
  }
  if (current_state == STATE_ACTIVE) {
    return handle_active(state, engine_message, cmd);
  }
  return handle_halt(state, cmd);
}

void handle_initial(uci_state *state, ucicmd cmd) {
  if (cmd.empty || strcmp(cmd.root, "uci") != 0) return;
  // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 5 SECONDS
  printf("id name %s\n", state->name);
  printf("id author %s\n", state->author);
  int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
  for (int i = 0; i < settings_count; i++) {
    print_setting(state->option_settings[i]);
  }
  // ---
  printf("uciok\n");
  current_state = STATE_IDLE;
}

void handle_idle(
  uci_state *state,
  ucicmd cmd) {
  if (cmd.empty) return;
  if (strcmp(cmd.root, "debug") == 0) {
    state->debug = strcmp(ucicmd_get_arg(cmd, 0), "on") == 0;
  } else if (strcmp(cmd.root, "setoption") == 0) {
    char buffer[2048] = {0};
    char name[64] = {0};
    char *cursor = name;
    for (int i = 1; i < cmd.args_count; i++) {
      assert((long)cursor - (long)buffer < 2048);
      char *arg = ucicmd_get_arg(cmd, i);
      if (strcmp(arg, "value") == 0) {
        *(--cursor) = 0;
        cursor = buffer;
        continue;
      }
      cursor = stpncpy(cursor, arg, MAX_TOKEN_SIZE);
      *(cursor++) = ' ';
    }
    *(--cursor) = 0;
    apply_option(state, name, buffer);
  } else if (strcmp(cmd.root, "ucinewgame") == 0) {
    if (state->moves != NULL) {
      free(state->moves);
      state->moves = NULL;
      state->moves_count = 0;
    }
  } else if (strcmp(cmd.root, "position") == 0) {
    int k = 0;
    char* mode = ucicmd_get_arg(cmd, 0);
    if (strcmp(mode, "startpos") == 0) {
      struct fen_load fen = load_fen(
        "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
      );
      if (fen.failed) {
        printf("info invalid fen\n");
        return;
      }
      state->position = fen.position;
    }
    else if (strcmp(mode, "fen") == 0) {
      struct fen_load fen = load_fen(ucicmd_get_arg(cmd, k++));
      if (fen.failed) {
        printf("info invalid fen\n");
        return;
      }
      state->position = fen.position;
    } else { assert(0); }

    if (cmd.args_count >= k) return;
    assert(strcmp(ucicmd_get_arg(cmd, k++), "moves") == 0);
    assert(state->moves_count <= cmd.args_count - k);
    int old_move_count = state->moves_count;
    state->moves_count = cmd.args_count - k;
    if (state->moves == NULL) {
      state->moves = malloc(
        state->moves_count * sizeof(struct uci_move)
      );
    } else {
      state->moves = realloc(
        state->moves,
        state->moves_count * sizeof(struct uci_move)
      );
    }
    int l = 0;
    k += old_move_count;
    l += old_move_count;
    for (; k < cmd.args_count; k++) {
      char* move = ucicmd_get_arg(cmd, k);
      state->moves[l++] = (struct uci_move) {
        (move[0] - 'a') * 8 + (move[1] - '1'),
        (move[2] - 'a') * 8 + (move[3] - '1')
      };
    }
  } else if (strcmp(cmd.root, "stop") == 0) {
  } else if (strcmp(cmd.root, "quit") == 0) {
    free(cmd.args);

    int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
    for (int i = 0; i < settings_count; i++) {
      option_setting_t setting = state->option_settings[i];
      if (OPTION_STRING != setting.type) continue;
      free(setting.value.string->data);
    }

    atomic_store(&state->quit, 1);
  } else if (strcmp(cmd.root, "isready") == 0) {
    current_state = STATE_SYNC;
  } else if (strcmp(cmd.root, "go") == 0) {
    state->go_args = malloc(sizeof(struct go_args));
    struct go_args *info = state->go_args;
    for (int i = 0; i < cmd.args_count; i++) {
      if (strcmp(ucicmd_get_arg(cmd, i), "searchmoves") == 0) {
        info->searchmoves = comm_moves_init();
        for (i++; i < cmd.args_count; i++) {
          char* move_str = ucicmd_get_arg(cmd, i);
          if (move_str[1] < '0' || move_str[1] > '9') break;

          add_comm_move(&info->searchmoves, (struct uci_move) {
            (move_str[0] - 'a') * 8 + (move_str[1] - '1'),
            (move_str[2] - 'a') * 8 + (move_str[3] - '1')
          });
        }
        i--;
      } else if (strcmp(ucicmd_get_arg(cmd, i), "ponder") == 0) {
        info->ponder = true;
      } else if (strcmp(ucicmd_get_arg(cmd, i), "wtime") == 0) {
        info->wtime = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "btime") == 0) {
        info->btime = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "winc") == 0) {
        info->winc = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "binc") == 0) {
        info->binc = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "movestogo") == 0) {
        info->movestogo = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "depth") == 0) {
        info->depth = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "nodes") == 0) {
        info->nodes = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "mate") == 0) {
        info->mate = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "movetime") == 0) {
        info->movetime = atoi(ucicmd_get_arg(cmd, ++i));
      } else if (strcmp(ucicmd_get_arg(cmd, i), "infinite") == 0) {
        info->infinite = true;
      } else if (strcmp(ucicmd_get_arg(cmd, i), "perft") == 0) {
        info->perft = atoi(ucicmd_get_arg(cmd, ++i));
      }
    }

    atomic_store(&state->go, 1);
    current_state = STATE_ACTIVE;
  } else {
    printf("info violation\n");
  }
}

void handle_sync(uci_state *state, ucicmd cmd) {
  // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 5 SECONDS
 


  // ---
  printf("readyok\n");
  current_state = STATE_IDLE;
}

void handle_ping(uci_state *state, ucicmd cmd) {
  // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS
  
  // if (TODO) {
  //   printf("bestmove\n");
  //   current_state = STATE_IDLE;
  // }
  
  // ---
  printf("readyok\n");
  current_state = STATE_ACTIVE;
}

void handle_active(
  uci_state *state,
  engine_messages* engine_messages,
  ucicmd cmd
) {
  while (atomic_load(&state->go_ready_receive) == 0);

  bool ended = false;
  for (int i = 0; i < engine_messages->count; i++) {
    struct engine_message *old = engine_messages->data[i];
    if (old == NULL) continue;
    if (!old->ready) continue;
    old->ready = 0;

    printf(
      "info depth %d seldepth %d multipv %d ",
      old->depth,
      old->seldepth,
      old->multipv
    );
    if (old->mate) {
      printf("mate %d ", old->mate);
    } else {
      printf("score cp %d ", old->score_cp);
    }
    if (old->node_limit) {
      printf("upperbound ");
    }
    printf(
      "nodes %d nps %d hashfull %d tbhits %d time %d pv",
      old->nodes,
      old->nps,
      old->hashfull,
      old->tbhits,
      old->time
    );

    for (int j = 0; j < old->pv.count; j++) {
      struct uci_move move = old->pv.moves[i];
      printf(
        " %c%c%c%c",
        (move.from / 8) + 'a',
        (move.from % 8) + '1',
        (move.to / 8) + 'a',
        (move.to % 8) + '1'
      );
    }
    printf("\n");

    if (old->best_move.from != 0 && old->best_move.to != 0) {
      ended = true;
      printf(
        "bestmove %c%c%c%c",
        (old->best_move.from / 8) + 'a',
        (old->best_move.from % 8) + '1',
        (old->best_move.to / 8) + 'a',
        (old->best_move.to % 8) + '1'
      );
      if (old->ponder.from != 0 && old->ponder.to != 0) {
        printf(
          " ponder %c%c%c%c",
          (old->ponder.from / 8) + 'a',
          (old->ponder.from % 8) + '1',
          (old->ponder.to / 8) + 'a',
          (old->ponder.to % 8) + '1'
        );
      }
      printf("\n");
    }

    engine_messages->data[i] = old->next;
    free(old->pv.moves);
    free(old);

    if (ended) {
      printf("ENDED\n");
      atomic_store(&state->cleanup, 1);
      current_state = STATE_IDLE;
    }
  }
  if (cmd.empty) {
    return;
  }
  if (strcmp(cmd.root, "isready") == 0) {
    current_state = STATE_PING;
  } else if (strcmp(cmd.root, "stop") == 0) {
    current_state = STATE_HALT;
  } else {
    printf("info violation\n");
  }
}

void handle_halt(uci_state *state, ucicmd cmd) {
  // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS
 
  atomic_store(&state->stop, 1);
  
  // ---
  current_state = STATE_IDLE;
}