From f17df9538b1c3f58f12b738083b747ac332b8d71 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Sat, 4 Jul 2026 22:34:17 +0530 Subject: the setoption now changes state i can now finally start working on how the non existant engine will interact with uci --- src/uci/response.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 7 deletions(-) (limited to 'src/uci/response.c') diff --git a/src/uci/response.c b/src/uci/response.c index 1f32f55..9b3118b 100644 --- a/src/uci/response.c +++ b/src/uci/response.c @@ -1,13 +1,47 @@ #include #include +#include #include "response.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; + } + printf("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" +// 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 @@ -44,7 +78,6 @@ enum { STATE_HALT, }; int current_state = STATE_INITIAL; -int debug = false; void todo() { assert(0); @@ -84,8 +117,24 @@ void handle_initial(uci_state *state, ucicmd cmd) { void handle_idle(uci_state *state, ucicmd cmd) { if (cmd.empty) return; if (strcmp(cmd.root, "debug") == 0) { - debug = strcmp(ucicmd_get_arg(cmd, 0), "on") == 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) { } else if (strcmp(cmd.root, "position") == 0) { } else if (strcmp(cmd.root, "stop") == 0) { -- cgit v1.2.3