diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-07-04 22:34:17 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-07-04 22:34:17 +0530 |
| commit | f17df9538b1c3f58f12b738083b747ac332b8d71 (patch) | |
| tree | 4df4254a726cd009a26eb3d9cd7e6d28b1a9c9a5 | |
| parent | 8535106616ccdc9f52c29ce7f46555e613c38b6e (diff) | |
the setoption now changes state
i can now finally start working on how the non existant engine will
interact with uci
| -rw-r--r-- | src/uci.c | 67 | ||||
| -rw-r--r-- | src/uci/response.c | 63 | ||||
| -rw-r--r-- | src/uci/state.c | 34 | ||||
| -rw-r--r-- | src/uci/state.h | 32 |
4 files changed, 161 insertions, 35 deletions
@@ -40,20 +40,65 @@ int main() { strncpy(state.name, "Gacrux", 32); strncpy(state.author, "Aargh Rai <aarghrai.com>", 32); int k = 0; - state.option_settings[k++] = option_setting_spin("Threads", 1, 5, 1); - state.option_settings[k++] = option_setting_spin("Hash", 0, 512, 256); - state.option_settings[k++] = option_setting_button("Clear Hash"); - state.option_settings[k++] = option_setting_check("UCI_ShowCurrLine", false); - state.option_settings[k++] = option_setting_check("UCI_ShowRefutations", false); - state.option_settings[k++] = option_setting_check("UCI_LimitStrength", false); - state.option_settings[k++] = option_setting_spin("UCI_Elo", 100, 3500, 800); - state.option_settings[k++] = option_setting_check("UCI_AnalyseMode", false); - state.option_settings[k++] = option_setting_string("UCI_Opponent", "<empty>"); - state.option_settings[k++] = option_setting_string("UCI_EngineAbout", "Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux"); - state.option_settings[k++] = option_setting_string("UCI_SetPositionValue", "<empty>"); + state.option_settings[k++] = option_setting_spin( + "Threads", + 1, 5, 1, + &state.threads + ); + state.option_settings[k++] = option_setting_spin( + "Hash", + 0, 512, 256, + &state.hash + ); + state.option_settings[k++] = option_setting_button( + "Clear Hash", + &state.clear_hash + ); + state.option_settings[k++] = option_setting_check( + "UCI_ShowCurrLine", + false, + &state.uci_showcurrline + ); + state.option_settings[k++] = option_setting_check( + "UCI_ShowRefutations", + false, + &state.uci_showrefutations + ); + state.option_settings[k++] = option_setting_check( + "UCI_LimitStrength", + false, + &state.uci_limitstrength + ); + state.option_settings[k++] = option_setting_spin( + "UCI_Elo", + 100, 3500, 800, + &state.uci_elo + ); + state.option_settings[k++] = option_setting_check( + "UCI_AnalyseMode", + false, + &state.uci_analysemode + ); + state.option_settings[k++] = option_setting_string( + "UCI_Opponent", + "<empty>", + &state.uci_opponent + ); + state.option_settings[k++] = option_setting_string( + "UCI_EngineAbout", + "Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux", + &state.uci_engineabout + ); + state.option_settings[k++] = option_setting_string( + "UCI_SetPositionValue", + "<empty>", + &state.uci_setpositionvalue + ); + ucicmd cmd = ucicmd_init(); size_t n; char buf[1024]; + while (true) { cmd.empty = true; cmd.args_count = 0; 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 <string.h> #include <assert.h> +#include <stdlib.h> #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) { diff --git a/src/uci/state.c b/src/uci/state.c index 848c6a9..f64b5cf 100644 --- a/src/uci/state.c +++ b/src/uci/state.c @@ -1,10 +1,12 @@ +#include <string.h> #include "state.h" option_setting_t option_setting_combo( char* option_name, char** combinations, int combinations_count, - int default_value + int default_index, + combo_t *combo ) { option_setting_t output = {0}; memcpy(output.option_name, option_name, 32); @@ -12,8 +14,10 @@ option_setting_t option_setting_combo( output.data.combo = (option_combo_setting_t) { combinations, combinations_count, - default_value + default_index }; + output.value.combo = combo; + *combo = default_index; return output; } @@ -21,40 +25,56 @@ option_setting_t option_setting_spin( const char* option_name, int min, int max, - int default_value + int default_value, + spin_t *spin ) { option_setting_t output = {0}; memcpy(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; + *spin = default_value; return output; } option_setting_t option_setting_check( const char* option_name, - bool default_value + bool default_value, + check_t *check ) { option_setting_t output = {0}; memcpy(output.option_name, option_name, 32); output.type = OPTION_CHECK; output.data.check_default = default_value; + output.value.check = check; + *check = default_value; return output; } option_setting_t option_setting_string( const char* option_name, - const char* default_value + const char* default_value, + str_t *string ) { option_setting_t output = {0}; - memcpy(output.option_name, option_name, 32); + strncpy(output.option_name, option_name, 32); output.type = OPTION_STRING; output.data.string_default = default_value; + output.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) { +option_setting_t option_setting_button( + const char* option_name, + button_t *button +) { option_setting_t output = {0}; memcpy(output.option_name, option_name, 32); output.type = OPTION_BUTTON; + output.value.button = button; + *button = false; return output; } diff --git a/src/uci/state.h b/src/uci/state.h index 98b42dd..fd24b7e 100644 --- a/src/uci/state.h +++ b/src/uci/state.h @@ -8,10 +8,7 @@ typedef struct { typedef bool check_t; typedef int spin_t; -typedef struct { - str_t* combinations; - int chosen_option; -} combo_t; +typedef int combo_t; // the button isn't actually a value thing // it's more like an event. // so we parse a button type, we set this to true, handle it @@ -38,35 +35,50 @@ typedef struct { bool check_default; const char* string_default; } data; + union { + combo_t *combo; + spin_t *spin; + check_t *check; + str_t *string; + button_t *button; + } value; } option_setting_t; option_setting_t option_setting_combo( char* option_name, char** combinations, int combinations_count, - int default_index + int default_index, + combo_t *combo ); option_setting_t option_setting_spin( const char* option_name, int min, int max, - int default_value + int default_value, + spin_t *spin ); option_setting_t option_setting_check( const char* option_name, - bool default_value + bool default_value, + check_t *check ); option_setting_t option_setting_string( const char* option_name, - const char* default_value + const char* default_value, + str_t *string +); +option_setting_t option_setting_button( + const char* option_name, + button_t *button ); -option_setting_t option_setting_button(const char* option_name); typedef struct { char name[32]; char author[32]; + bool debug; - option_setting_t option_settings[10]; + option_setting_t option_settings[11]; // options spin_t threads; |
