summaryrefslogtreecommitdiff
path: root/src/uci
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-05 23:55:45 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-05 23:55:45 +0530
commit1876294f217d7faa2a07dbaaaab82d142ce42803 (patch)
treed67ba4b240f25042063c06ce99370e3be34457ff /src/uci
parentf17df9538b1c3f58f12b738083b747ac332b8d71 (diff)
all in on uci
the old server.c & log.c is gonna be removed soon
Diffstat (limited to 'src/uci')
-rw-r--r--src/uci/response.c67
-rw-r--r--src/uci/state.h9
2 files changed, 62 insertions, 14 deletions
diff --git a/src/uci/response.c b/src/uci/response.c
index 9b3118b..a7e53fd 100644
--- a/src/uci/response.c
+++ b/src/uci/response.c
@@ -1,7 +1,10 @@
#include <string.h>
#include <assert.h>
#include <stdlib.h>
+
#include "response.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);
@@ -33,7 +36,9 @@ void apply_option(uci_state *state, char *name, char *buffer) {
} else { assert(0); }
return;
}
- printf("invalid option\n");
+ if (state->debug) {
+ printf("debug: invalid option\n");
+ }
}
// examples from uci_min.txt
@@ -79,10 +84,6 @@ enum {
};
int current_state = STATE_INITIAL;
-void todo() {
- assert(0);
-}
-
void handle_uci(uci_state *state, ucicmd cmd) {
if (current_state == STATE_INITIAL) {
return handle_initial(state, cmd);
@@ -104,12 +105,14 @@ void handle_uci(uci_state *state, ucicmd 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;
}
@@ -136,35 +139,69 @@ void handle_idle(uci_state *state, ucicmd cmd) {
*(--cursor) = 0;
apply_option(state, name, buffer);
} else if (strcmp(cmd.root, "ucinewgame") == 0) {
+ // clear game state here
} else if (strcmp(cmd.root, "position") == 0) {
+ int k = 0;
+ char* mode = ucicmd_get_arg(cmd, 0);
+ if (strcmp(mode, "startpos") == 0) {
+ state->position = load_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ }
+ else if (strcmp(mode, "fen") == 0) {
+ state->position = load_fen(ucicmd_get_arg(cmd, k++));
+ } else { assert(0); }
+
+ if (cmd.args_count >= k) return;
+ assert(ucicmd_get_arg(cmd, k++), "moves") == 0);
+ struct uci_move *moves = malloc((cmd.args_count - k) * sizeof(struct uci_move));
+ int l = 0;
+ for (; k < cmd.args_count; k++) {
+ char* move = ucicmd_get_arg(cmd, k);
+ moves[l++] = (struct uci_move) {
+ (move[0] - 'a') * 8 + (move[1] - '1'),
+ (move[2] - 'a') * 8 + (move[3] - '1')
+ };
+ }
+ // SENT state->position & struct uci_move to ENGINE
} 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);
+ }
+
exit(0);
} else if (strcmp(cmd.root, "isready") == 0) {
current_state = STATE_SYNC;
} else if (strcmp(cmd.root, "go") == 0) {
current_state = STATE_ACTIVE;
} else {
- printf("Invalid command: %s\n", cmd.root);
+ printf("info violation\n");
}
}
void handle_sync(uci_state *state, ucicmd cmd) {
- // when i come up with some info to give:
- // printf("info\n");
+ // 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) {
- // when i come up with some info to give:
- // printf("info\n");
- //
+ // 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;
}
@@ -179,13 +216,15 @@ void handle_active(uci_state *state, ucicmd cmd) {
} else if (strcmp(cmd.root, "stop") == 0) {
current_state = STATE_HALT;
} else {
- printf("Invalid command: %s\n", cmd.root);
+ printf("info violation\n");
}
}
void handle_halt(uci_state *state, ucicmd cmd) {
- // when i come up with some info to give:
- // printf("info\n");
+ // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS
+
+
+ // ---
printf("bestmove 0000\n"); // TODO: idk get it rn
current_state = STATE_IDLE;
}
diff --git a/src/uci/state.h b/src/uci/state.h
index fd24b7e..cb0d63f 100644
--- a/src/uci/state.h
+++ b/src/uci/state.h
@@ -1,6 +1,8 @@
#ifndef UCI_STATE_H
#define UCI_STATE_H
+#include "engine/fen.h"
+
typedef struct {
char* data;
int length;
@@ -78,6 +80,8 @@ typedef struct {
char author[32];
bool debug;
+ struct fen_load position;
+
option_setting_t option_settings[11];
// options
@@ -100,4 +104,9 @@ typedef struct {
str_t uci_setpositionvalue;
} uci_state;
+struct uci_move {
+ int from;
+ int to;
+};
+
#endif // UCI_STATE_H