diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-07-13 21:58:18 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-07-13 21:58:18 +0530 |
| commit | f8c0dc54e36cc201c657aa12e5a22029b092e1bc (patch) | |
| tree | a0dd1a42c9681be98a7508c128db0dacacefc2e7 /src/uci | |
| parent | 2e8b78ab0ef01eaacd8094ff839ac93b4451e9c3 (diff) | |
restructed all the files + uci done now
also broke the symbol analyser in the process, i didn't help much, i had
to pull out the pen & paper, but it made a cool graph never the less
multithreading was a pain! but it got it in the end
fixed the structs naming convention, if it's used outside by other
files, it's typedeffed, otherwise it's not
fixed the recursive imports issued i had in go_args*, where i was
casting it to void* in uci to make it work
now response.c doesn't deal with ipc, really happy with that change
now every single thing has its own responsibility
Diffstat (limited to 'src/uci')
| -rw-r--r-- | src/uci/command.c | 52 | ||||
| -rw-r--r-- | src/uci/response.c | 333 | ||||
| -rw-r--r-- | src/uci/state.c | 223 |
3 files changed, 284 insertions, 324 deletions
diff --git a/src/uci/command.c b/src/uci/command.c index 02d6e0f..c64fee2 100644 --- a/src/uci/command.c +++ b/src/uci/command.c @@ -1,10 +1,10 @@ #include <assert.h> #include <string.h> #include <stdlib.h> -#include "command.h" +#include "uci/internals/command.h" -ucicmd ucicmd_init() { - ucicmd output = {0}; +uci_cmd_t uci_cmd_init() { + uci_cmd_t output = {0}; output.empty = true; output.args_capacity = 32; @@ -37,16 +37,16 @@ bool is_valid_cmd(const char *token) { (strcmp(token, "option") == 0); } -void ucicmd_add(ucicmd* cmd, const char* token) { +void uci_cmd_add(uci_cmd_t* cmd, const char* token) { if (!cmd->empty) { - ucicmd_append_arg(cmd, token); + uci_cmd_append_arg(cmd, token); return; } if (is_valid_cmd(token) == 0) return; - ucicmd_set_root(cmd, token); + uci_cmd_set_root(cmd, token); } -void ucicmd_set_root(ucicmd* cmd, const char* token) { +void uci_cmd_set_root(uci_cmd_t* cmd, const char* token) { cmd->empty = false; int i = 0; for (; token[i] != 0; i++) { @@ -56,7 +56,7 @@ void ucicmd_set_root(ucicmd* cmd, const char* token) { if (i < MAX_TOKEN_SIZE) cmd->root[i] = 0; } -void ucicmd_append_arg(ucicmd* cmd, const char* token) { +void uci_cmd_append_arg(uci_cmd_t* cmd, const char* token) { if (cmd->args_count + 1 > cmd->args_capacity) { cmd->args_capacity += 32; // does realloc set all zeros, like calloc??? @@ -66,7 +66,7 @@ void ucicmd_append_arg(ucicmd* cmd, const char* token) { sizeof(char) * MAX_TOKEN_SIZE * cmd->args_capacity ); memset( - ucicmd_get_arg(*cmd, cmd->args_capacity - 32), + uci_cmd_get_arg(*cmd, cmd->args_capacity - 32), 0, 32 * MAX_TOKEN_SIZE ); @@ -81,11 +81,11 @@ void ucicmd_append_arg(ucicmd* cmd, const char* token) { if (i < MAX_TOKEN_SIZE) cmd->args[shift + i] = 0; } -char* ucicmd_get_arg(ucicmd cmd, int i) { +char* uci_cmd_get_arg(uci_cmd_t cmd, int i) { return cmd.args + i * MAX_TOKEN_SIZE; } -void ucicmd_deinit(ucicmd cmd) { +void uci_cmd_deinit(uci_cmd_t cmd) { free(cmd.args); } @@ -94,31 +94,31 @@ void ucicmd_deinit(ucicmd cmd) { #include <stdbool.h> #include <string.h> -bool test_ucicmd() { - ucicmd cmd = ucicmd_init(); - ucicmd_set_root(&cmd, "root"); - ucicmd_append_arg(&cmd, "arg1"); - ucicmd_append_arg(&cmd, "arg2"); - ucicmd_append_arg(&cmd, "arg3"); +bool test_uci_cmd_t() { + uci_cmd_t cmd = uci_cmd_init(); + uci_cmd_set_root(&cmd, "root"); + uci_cmd_append_arg(&cmd, "arg1"); + uci_cmd_append_arg(&cmd, "arg2"); + uci_cmd_append_arg(&cmd, "arg3"); if (strncmp(cmd.root, "root", MAX_TOKEN_SIZE) != 0) return false; - if (strncmp(ucicmd_get_arg(cmd, 0), "arg1", MAX_TOKEN_SIZE) != 0) + if (strncmp(uci_cmd_get_arg(cmd, 0), "arg1", MAX_TOKEN_SIZE) != 0) return false; - if (strncmp(ucicmd_get_arg(cmd, 1), "arg2", MAX_TOKEN_SIZE) != 0) + if (strncmp(uci_cmd_get_arg(cmd, 1), "arg2", MAX_TOKEN_SIZE) != 0) return false; - if (strncmp(ucicmd_get_arg(cmd, 2), "arg3", MAX_TOKEN_SIZE) != 0) + if (strncmp(uci_cmd_get_arg(cmd, 2), "arg3", MAX_TOKEN_SIZE) != 0) return false; return true; } -bool test_invalid_cmd_ucicmd() { - ucicmd cmd = ucicmd_init(); - ucicmd_add(&cmd, "john"); - ucicmd_add(&cmd, "debug"); - ucicmd_add(&cmd, "on"); +bool test_invalid_cmd_uci_cmd_t() { + uci_cmd_t cmd = uci_cmd_t_init(); + uci_cmd_add(&cmd, "john"); + uci_cmd_add(&cmd, "debug"); + uci_cmd_add(&cmd, "on"); if (strncmp(cmd.root, "debug", MAX_TOKEN_SIZE) != 0) return false; - if (strncmp(ucicmd_get_arg(cmd, 0), "on", MAX_TOKEN_SIZE) != 0) + if (strncmp(uci_cmd_t_get_arg(cmd, 0), "on", MAX_TOKEN_SIZE) != 0) return false; return true; } diff --git a/src/uci/response.c b/src/uci/response.c index 7ebe49c..da46cb7 100644 --- a/src/uci/response.c +++ b/src/uci/response.c @@ -2,113 +2,30 @@ #include <assert.h> #include <stdlib.h> -#include "response.h" -#include "ipc.h" +#include "uci/internals/response.h" +#include "uci/state.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) { +void handle_uci(uci_state_t *state, uci_cmd_t cmd) { + if (state->current_state == UCI_STATE_INITIAL) { return handle_initial(state, cmd); } - if (current_state == STATE_IDLE) { + if (state->current_state == UCI_STATE_IDLE) { return handle_idle(state, cmd); } - if (current_state == STATE_SYNC) { + if (state->current_state == UCI_STATE_SYNC) { return handle_sync(state, cmd); } - if (current_state == STATE_PING) { + if (state->current_state == UCI_STATE_PING) { return handle_ping(state, cmd); } - if (current_state == STATE_ACTIVE) { - return handle_active(state, engine_message, cmd); + if (state->current_state == UCI_STATE_ACTIVE) { + return handle_active(state, cmd); } return handle_halt(state, cmd); } -void handle_initial(uci_state *state, ucicmd cmd) { +void handle_initial(uci_state_t *state, uci_cmd_t 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); @@ -119,22 +36,20 @@ void handle_initial(uci_state *state, ucicmd cmd) { } // --- printf("uciok\n"); - current_state = STATE_IDLE; + state->current_state = UCI_STATE_IDLE; } -void handle_idle( - uci_state *state, - ucicmd cmd) { +void handle_idle(uci_state_t *state, uci_cmd_t cmd) { if (cmd.empty) return; if (strcmp(cmd.root, "debug") == 0) { - state->debug = strcmp(ucicmd_get_arg(cmd, 0), "on") == 0; + state->debug = strcmp(uci_cmd_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); + char *arg = uci_cmd_get_arg(cmd, i); if (strcmp(arg, "value") == 0) { *(--cursor) = 0; cursor = buffer; @@ -146,26 +61,18 @@ void handle_idle( *(--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); + char* mode = uci_cmd_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; - } + assert(fen.failed == 0); state->position = fen.position; } else if (strcmp(mode, "fen") == 0) { - struct fen_load fen = load_fen(ucicmd_get_arg(cmd, k++)); + struct fen_load fen = load_fen(uci_cmd_get_arg(cmd, k++)); if (fen.failed) { printf("info invalid fen\n"); return; @@ -174,29 +81,21 @@ void handle_idle( } 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; + assert(strcmp(uci_cmd_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; + 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') - }; + char* move = uci_cmd_get_arg(cmd, k); + add_ipc_move( + state->moves, + (ipc_move_t) { + (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) { @@ -209,179 +108,101 @@ void handle_idle( free(setting.value.string->data); } - atomic_store(&state->quit, 1); + state->signal = UCI_SIGNAL_QUIT; } else if (strcmp(cmd.root, "isready") == 0) { - current_state = STATE_SYNC; + state->current_state = UCI_STATE_SYNC; } else if (strcmp(cmd.root, "go") == 0) { - state->go_args = malloc(sizeof(struct go_args)); - struct go_args *info = state->go_args; + go_args_t info = (go_args_t) {0}; for (int i = 0; i < cmd.args_count; i++) { - if (strcmp(ucicmd_get_arg(cmd, i), "searchmoves") == 0) { - info->searchmoves = comm_moves_init(); + if (strcmp(uci_cmd_get_arg(cmd, i), "searchmoves") == 0) { + ipc_moves_init(&info.searchmoves); for (i++; i < cmd.args_count; i++) { - char* move_str = ucicmd_get_arg(cmd, i); + char* move_str = uci_cmd_get_arg(cmd, i); if (move_str[1] < '0' || move_str[1] > '9') break; - add_comm_move(&info->searchmoves, (struct uci_move) { + add_ipc_move(&info.searchmoves, (ipc_move_t) { (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)); + // it was harsh on my eyes, that's why i did this formatting + } else if (strcmp(uci_cmd_get_arg(cmd, i), "ponder") == 0) { + info.ponder = true; + } else if (strcmp(uci_cmd_get_arg(cmd, i), "wtime") == 0) { + info.wtime = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "btime") == 0) { + info.btime = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "winc") == 0) { + info.winc = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "binc") == 0) { + info.binc = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "movestogo") == 0) { + info.movestogo = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "depth") == 0) { + info.depth = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "nodes") == 0) { + info.nodes = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "mate") == 0) { + info.mate = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "movetime") == 0) { + info.movetime = atoi(uci_cmd_get_arg(cmd, ++i)); + } else if (strcmp(uci_cmd_get_arg(cmd, i), "infinite") == 0) { + info.infinite = true; + } else if (strcmp(uci_cmd_get_arg(cmd, i), "perft") == 0) { + info.perft = atoi(uci_cmd_get_arg(cmd, ++i)); } } - atomic_store(&state->go, 1); - current_state = STATE_ACTIVE; + memcpy(&state->go_args, &info, sizeof(info)); + state->signal = UCI_SIGNAL_GO; + state->current_state = UCI_STATE_ACTIVE; } else { printf("info violation\n"); } } -void handle_sync(uci_state *state, ucicmd cmd) { +void handle_sync(uci_state_t *state, uci_cmd_t cmd) { // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 5 SECONDS // --- printf("readyok\n"); - current_state = STATE_IDLE; + state->current_state = UCI_STATE_IDLE; } -void handle_ping(uci_state *state, ucicmd cmd) { +void handle_ping(uci_state_t *state, uci_cmd_t cmd) { // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS // if (TODO) { // printf("bestmove\n"); - // current_state = STATE_IDLE; + // current_state = UCI_STATE_IDLE; // } // --- printf("readyok\n"); - current_state = STATE_ACTIVE; + state->current_state = UCI_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; - } - } +void handle_active(uci_state_t *state, uci_cmd_t cmd) { if (cmd.empty) { return; } if (strcmp(cmd.root, "isready") == 0) { - current_state = STATE_PING; + state->current_state = UCI_STATE_PING; } else if (strcmp(cmd.root, "stop") == 0) { - current_state = STATE_HALT; + state->current_state = UCI_STATE_HALT; } else { printf("info violation\n"); } } -void handle_halt(uci_state *state, ucicmd cmd) { +void handle_halt(uci_state_t *state, uci_cmd_t cmd) { // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS - - atomic_store(&state->stop, 1); + + state->signal = UCI_SIGNAL_STOP; // --- - current_state = STATE_IDLE; + state->current_state = UCI_STATE_IDLE; } diff --git a/src/uci/state.c b/src/uci/state.c index 56e0b04..ac043ed 100644 --- a/src/uci/state.c +++ b/src/uci/state.c @@ -1,80 +1,219 @@ #include <string.h> -#include "state.h" +#include "uci/state.h" -option_setting_t option_setting_combo( - char* option_name, - char** combinations, +void option_setting_combo( + option_setting_t *target, + char *option_name, + char **combinations, int combinations_count, int default_index, combo_t *combo ) { - option_setting_t output = {0}; - strncpy(output.option_name, option_name, 32); - output.type = OPTION_COMBO; - output.data.combo = (option_combo_setting_t) { + strncpy(target->option_name, option_name, 32); + target->type = OPTION_COMBO; + target->data.combo = (option_combo_setting_t) { combinations, combinations_count, default_index }; - output.value.combo = combo; + target->value.combo = combo; *combo = default_index; - return output; } -option_setting_t option_setting_spin( - const char* option_name, +void option_setting_spin( + option_setting_t *target, + const char *option_name, int min, int max, int default_value, spin_t *spin ) { - option_setting_t output = {0}; - strncpy(output.option_name, option_name, 32); - output.type = OPTION_SPIN; - output.data.spin = (option_spin_setting_t) { min, max, default_value }; - output.value.spin = spin; + strncpy(target->option_name, option_name, 32); + target->type = OPTION_SPIN; + target->data.spin = (option_spin_setting_t) { min, max, default_value }; + target->value.spin = spin; *spin = default_value; - return output; } -option_setting_t option_setting_check( - const char* option_name, +void option_setting_check( + option_setting_t *target, + const char *option_name, bool default_value, check_t *check ) { - option_setting_t output = {0}; - strncpy(output.option_name, option_name, 32); - output.type = OPTION_CHECK; - output.data.check_default = default_value; - output.value.check = check; + strncpy(target->option_name, option_name, 32); + target->type = OPTION_CHECK; + target->data.check_default = default_value; + target->value.check = check; *check = default_value; - return output; } -option_setting_t option_setting_string( - const char* option_name, - const char* default_value, +void option_setting_string( + option_setting_t *target, + const char *option_name, + const char *default_value, str_t *string ) { - option_setting_t output = {0}; - strncpy(output.option_name, option_name, 32); - output.type = OPTION_STRING; - output.data.string_default = default_value; - output.value.string = string; + strncpy(target->option_name, option_name, 32); + target->type = OPTION_STRING; + target->data.string_default = default_value; + target->value.string = string; string->length = strlen(default_value); string->data = malloc(string->length); memcpy(string->data, default_value, string->length); - return output; } -option_setting_t option_setting_button( - const char* option_name, +void option_setting_button( + option_setting_t *target, + const char *option_name, button_t *button ) { - option_setting_t output = {0}; - strncpy(output.option_name, option_name, 32); - output.type = OPTION_BUTTON; - output.value.button = button; + strncpy(target->option_name, option_name, 32); + target->type = OPTION_BUTTON; + target->value.button = button; *button = false; - return output; } + +// 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); } +} + +void uci_state_init(uci_state_t *state) { + state->signal = UCI_SIGNAL_NONE; + strncpy(state->name, "Gacrux", 32); + strncpy(state->author, "Aargh Rai <aarghrai.com>", 32); + int k = 0; + option_setting_spin( + state->option_settings + k++, + "Threads", + 1, 5, 1, + &state->threads + ); + option_setting_spin( + state->option_settings + k++, + "Hash", + 0, 512, 256, + &state->hash + ); + option_setting_button( + state->option_settings + k++, + "Clear Hash", + &state->clear_hash + ); + option_setting_check( + state->option_settings + k++, + "UCI_ShowCurrLine", + false, + &state->uci_showcurrline + ); + option_setting_check( + state->option_settings + k++, + "UCI_ShowRefutations", + false, + &state->uci_showrefutations + ); + option_setting_check( + state->option_settings + k++, + "UCI_LimitStrength", + false, + &state->uci_limitstrength + ); + option_setting_spin( + state->option_settings + k++, + "UCI_Elo", + 100, 3500, 800, + &state->uci_elo + ); + option_setting_check( + state->option_settings + k++, + "UCI_AnalyseMode", + false, + &state->uci_analysemode + ); + option_setting_string( + state->option_settings + k++, + "UCI_Opponent", + "<empty>", + &state->uci_opponent + ); + option_setting_string( + state->option_settings + k++, + "UCI_EngineAbout", + "Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux", + &state->uci_engineabout + ); + option_setting_string( + state->option_settings + k++, + "UCI_SetPositionValue", + "<empty>", + &state->uci_setpositionvalue + ); +} + +void uci_state_deinit(uci_state_t *state) {} + +void apply_option(uci_state_t *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); } + + memcpy(state->option_settings + i, &setting, sizeof(setting)); + return; + } + if (state->debug) { + printf("info invalid option\n"); + } +} + |
