summaryrefslogtreecommitdiff
path: root/src/uci.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/uci.c')
-rw-r--r--src/uci.c196
1 files changed, 117 insertions, 79 deletions
diff --git a/src/uci.c b/src/uci.c
index 6a2c412..cc16954 100644
--- a/src/uci.c
+++ b/src/uci.c
@@ -1,18 +1,17 @@
-#include <stdio.h>
#include <string.h>
-#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
+#include <stdio.h>
+#include <stdatomic.h>
-#include <stdlib.h>
-#include "state.h"
-#include "command.h"
-#include "response.h"
-#include "uci.h"
-#include "ipc.h"
+#include "uci/internals/command.h"
+#include "uci/internals/response.h"
+#include "uci/state.h"
+#include "ipc/state.h"
+#include "ipc/uci.h"
-void load_from_message(ucicmd* cmd, const char* message) {
+void load_from_message(uci_cmd_t* cmd, const char* message) {
char token[MAX_TOKEN_SIZE];
int i = 0, k = 0;
@@ -27,79 +26,18 @@ void load_from_message(ucicmd* cmd, const char* message) {
}
i++;
token[k] = 0;
- ucicmd_add(cmd, token);
+ uci_cmd_add(cmd, token);
k = 0;
}
token[k] = 0;
- ucicmd_add(cmd, token);
+ uci_cmd_add(cmd, token);
}
-void *uci(void *_com) {
- comms *com = (comms*)_com;
+void *uci(ipc_state_t *ipc_state) {
// https://stackoverflow.com/a/41559081
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
- com->state = (uci_state) {0};
- strncpy(com->state.name, "Gacrux", 32);
- strncpy(com->state.author, "Aargh Rai <aarghrai.com>", 32);
- int k = 0;
- com->state.option_settings[k++] = option_setting_spin(
- "Threads",
- 1, 5, 1,
- &com->state.threads
- );
- com->state.option_settings[k++] = option_setting_spin(
- "Hash",
- 0, 512, 256,
- &com->state.hash
- );
- com->state.option_settings[k++] = option_setting_button(
- "Clear Hash",
- &com->state.clear_hash
- );
- com->state.option_settings[k++] = option_setting_check(
- "UCI_ShowCurrLine",
- false,
- &com->state.uci_showcurrline
- );
- com->state.option_settings[k++] = option_setting_check(
- "UCI_ShowRefutations",
- false,
- &com->state.uci_showrefutations
- );
- com->state.option_settings[k++] = option_setting_check(
- "UCI_LimitStrength",
- false,
- &com->state.uci_limitstrength
- );
- com->state.option_settings[k++] = option_setting_spin(
- "UCI_Elo",
- 100, 3500, 800,
- &com->state.uci_elo
- );
- com->state.option_settings[k++] = option_setting_check(
- "UCI_AnalyseMode",
- false,
- &com->state.uci_analysemode
- );
- com->state.option_settings[k++] = option_setting_string(
- "UCI_Opponent",
- "<empty>",
- &com->state.uci_opponent
- );
- com->state.option_settings[k++] = option_setting_string(
- "UCI_EngineAbout",
- "Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux",
- &com->state.uci_engineabout
- );
- com->state.option_settings[k++] = option_setting_string(
- "UCI_SetPositionValue",
- "<empty>",
- &com->state.uci_setpositionvalue
- );
- atomic_store(&com->uci_state_initialized, 1);
-
- ucicmd cmd = ucicmd_init();
+ uci_cmd_t cmd = uci_cmd_init();
size_t n;
char buf[1024];
@@ -110,11 +48,111 @@ void *uci(void *_com) {
if (n > 0) {
load_from_message(&cmd, buf);
}
- handle_uci(
- &com->state,
- com->engine_messages,
- cmd
- );
+ handle_uci(&ipc_state->uci_state, cmd);
+ handle_ipc(ipc_state);
+ }
+}
+
+int k =0;
+void handle_ipc(ipc_state_t *state) {
+ int signal = state->uci_state.signal;
+ assert(signal < UCI_SIGNAL_COUNT);
+ if (signal == UCI_SIGNAL_NONE) return;
+ if (signal == UCI_SIGNAL_QUIT) {
+ return;
+ }
+ if (signal == UCI_SIGNAL_GO) {
+ // some init stuff
+ ipc_state_set_event(state, EVENT_ENGINE_GO);
+ state->uci_state.signal = UCI_SIGNAL_GO_LOOP;
+ }
+ if (
+ signal == UCI_SIGNAL_GO_LOOP &&
+ ipc_state_is_event(state, EVENT_UCI_RUNNING)
+ ) {
+ bool ended = false;
+ engine_messages_t engine_messages = state->engine_messages;
+ int engine_messages_count = engine_messages.count;
+ for (int i = 0; i < engine_messages_count; i++) {
+ engine_message_handler_t *handler = engine_messages.data[i];
+ if (!engine_message_handler_can_pop(handler)) continue;
+ engine_message_t *message = engine_message_handler_pop(handler);
+
+ printf(
+ "info depth %d seldepth %d multipv %d ",
+ message->depth,
+ message->seldepth,
+ message->multipv
+ );
+ if (message->mate) {
+ printf("mate %d ", message->mate);
+ } else {
+ printf("score cp %d ", message->score_cp);
+ }
+ if (message->node_limit) {
+ printf("upperbound ");
+ }
+ printf(
+ "nodes %d nps %d hashfull %d tbhits %d time %d pv",
+ message->nodes,
+ message->nps,
+ message->hashfull,
+ message->tbhits,
+ message->time
+ );
+
+ for (int j = 0; j < message->pv.count; j++) {
+ ipc_move_t move = message->pv.data[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 (message->best_move.from != 0 && message->best_move.to != 0) {
+ ended = true;
+ printf(
+ "bestmove %c%c%c%c",
+ (message->best_move.from / 8) + 'a',
+ (message->best_move.from % 8) + '1',
+ (message->best_move.to / 8) + 'a',
+ (message->best_move.to % 8) + '1'
+ );
+ if (message->ponder.from != 0 && message->ponder.to != 0) {
+ printf(
+ " ponder %c%c%c%c",
+ (message->ponder.from / 8) + 'a',
+ (message->ponder.from % 8) + '1',
+ (message->ponder.to / 8) + 'a',
+ (message->ponder.to % 8) + '1'
+ );
+ }
+ printf("\n");
+ }
+
+ engine_message_deinit(message);
+ if (ended) {
+ state->uci_state.signal = UCI_SIGNAL_NONE;
+ state->uci_state.current_state = UCI_STATE_IDLE;
+ break;
+ }
+ }
+
+ if (ended) {
+ ipc_state_set_event(state, EVENT_ENGINE_STOP);
+ } else {
+ ipc_state_set_event(state, EVENT_ENGINE_RUNNING);
+ }
+
+ return;
+ }
+ if (signal == UCI_SIGNAL_STOP) {
+ printf("uci stopping\n");
+ return;
}
}