shithub: moonfish

Download patch

ref: 2971724514d8aa536cd02067bd4bc5e12b855ca2
parent: dbd7bdf80e36aaa1e0b28d68c8a2e5a17c5489ea
author: zamfofex <zamfofex@twdb.moe>
date: Wed Oct 11 20:43:03 EDT 2023

separate ‘struct moonfish_chess’ from ‘struct moonfish’

--- a/chess.c
+++ b/chess.c
@@ -1,6 +1,6 @@
 #include "moonfish.h"
 
-static struct moonfish_move *moonfish_create_move(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, unsigned char to)
+static struct moonfish_move *moonfish_create_move(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from, unsigned char to)
 {
 	(*moves)->from = from;
 	(*moves)->to = to;
@@ -11,7 +11,7 @@
 	return (*moves)++;
 }
 
-static char moonfish_delta(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, unsigned char *to, signed char delta)
+static char moonfish_delta(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from, unsigned char *to, signed char delta)
 {
 	*to += delta;
 	if (ctx->board[*to] <= moonfish_our_king) return 0;
@@ -20,7 +20,7 @@
 	return 1;
 }
 
-static void moonfish_slide(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, signed char delta)
+static void moonfish_slide(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from, signed char delta)
 {
 	unsigned char to;
 	to = from;
@@ -27,12 +27,12 @@
 	while (moonfish_delta(ctx, moves, from, &to, delta)) { }
 }
 
-static void moonfish_jump(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, signed char delta)
+static void moonfish_jump(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from, signed char delta)
 {
 	moonfish_delta(ctx, moves, from, &from, delta);
 }
 
-static void moonfish_move_knight(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_move_knight(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	moonfish_jump(ctx, moves, from, 21);
 	moonfish_jump(ctx, moves, from, 19);
@@ -44,7 +44,7 @@
 	moonfish_jump(ctx, moves, from, -12);
 }
 
-static void moonfish_move_bishop(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_move_bishop(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	moonfish_slide(ctx, moves, from, 11);
 	moonfish_slide(ctx, moves, from, 9);
@@ -52,7 +52,7 @@
 	moonfish_slide(ctx, moves, from, -11);
 }
 
-static void moonfish_move_rook(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_move_rook(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	moonfish_slide(ctx, moves, from, 10);
 	moonfish_slide(ctx, moves, from, -10);
@@ -60,13 +60,13 @@
 	moonfish_slide(ctx, moves, from, -1);
 }
 
-static void moonfish_move_queen(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_move_queen(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	moonfish_move_rook(ctx, moves, from);
 	moonfish_move_bishop(ctx, moves, from);
 }
 
-static char moonfish_attacked(struct moonfish *ctx, unsigned char from, unsigned char to)
+static char moonfish_attacked(struct moonfish_chess *ctx, unsigned char from, unsigned char to)
 {
 	int check;
 	ctx->board[from] = moonfish_empty;
@@ -77,7 +77,7 @@
 	return check;
 }
 
-static void moonfish_castle_low(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_castle_low(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	unsigned char to;
 	
@@ -92,7 +92,7 @@
 	moonfish_create_move(ctx, moves, from, from - 2);
 }
 
-static void moonfish_castle_high(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_castle_high(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	unsigned char to;
 	
@@ -107,7 +107,7 @@
 	moonfish_create_move(ctx, moves, from, from + 2);
 }
 
-static void moonfish_move_king(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_move_king(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	moonfish_jump(ctx, moves, from, 1 + 10);
 	moonfish_jump(ctx, moves, from, -1 + 10);
@@ -130,7 +130,7 @@
 	}
 }
 
-static void moonfish_move_pawn(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
+static void moonfish_move_pawn(struct moonfish_chess *ctx, struct moonfish_move **moves, unsigned char from)
 {
 	struct moonfish_move *move;
 	unsigned char promotion;
@@ -168,7 +168,7 @@
 	}
 }
 
-void moonfish_moves(struct moonfish *ctx, struct moonfish_move *moves, unsigned char from)
+void moonfish_moves(struct moonfish_chess *ctx, struct moonfish_move *moves, unsigned char from)
 {
 	unsigned char piece;
 	piece = ctx->board[from];
@@ -181,7 +181,7 @@
 	moves->piece = moonfish_outside;
 }
 
-static void moonfish_swap(struct moonfish *ctx, int i, int j)
+static void moonfish_swap(struct moonfish_chess *ctx, int i, int j)
 {
 	unsigned char piece;
 	piece = ctx->board[i];
@@ -189,7 +189,7 @@
 	ctx->board[j] = piece;
 }
 
-static void moonfish_flip_horizontally(struct moonfish *ctx)
+static void moonfish_flip_horizontally(struct moonfish_chess *ctx)
 {
 	int x, y;
 	
@@ -203,7 +203,7 @@
 	}
 }
 
-static void moonfish_flip_vertically(struct moonfish *ctx)
+static void moonfish_flip_vertically(struct moonfish_chess *ctx)
 {
 	int x, y;
 	
@@ -217,7 +217,7 @@
 	}
 }
 
-static void moonfish_rotate(struct moonfish *ctx)
+static void moonfish_rotate(struct moonfish_chess *ctx)
 {
 	int x, y;
 	
@@ -235,7 +235,7 @@
 	}
 }
 
-void moonfish_play(struct moonfish *ctx, struct moonfish_move *move)
+void moonfish_play(struct moonfish_chess *ctx, struct moonfish_move *move)
 {
 	int x0, x1;
 	
@@ -294,7 +294,7 @@
 	moonfish_rotate(ctx);
 }
 
-void moonfish_unplay(struct moonfish *ctx, struct moonfish_move *move)
+void moonfish_unplay(struct moonfish_chess *ctx, struct moonfish_move *move)
 {
 	int x0, x1;
 	
@@ -315,7 +315,7 @@
 	}
 }
 
-void moonfish_chess(struct moonfish *ctx)
+void moonfish_chess(struct moonfish_chess *ctx)
 {
 	int x, y;
 	
@@ -326,7 +326,7 @@
 	moonfish_fen(ctx, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq");
 }
 
-void moonfish_play_uci(struct moonfish *ctx, char *name)
+void moonfish_play_uci(struct moonfish_chess *ctx, char *name)
 {
 	struct moonfish_move move;
 	int x, y;
@@ -409,7 +409,7 @@
 	}
 }
 
-void moonfish_fen(struct moonfish *ctx, char *fen)
+void moonfish_fen(struct moonfish_chess *ctx, char *fen)
 {
 	int x, y;
 	unsigned char type, color;
@@ -482,7 +482,7 @@
 	}
 }
 
-int moonfish_validate(struct moonfish *ctx)
+int moonfish_validate(struct moonfish_chess *ctx)
 {
 	int x, y;
 	struct moonfish_move moves[32];
@@ -500,7 +500,7 @@
 	return 1;
 }
 
-int moonfish_check(struct moonfish *ctx)
+int moonfish_check(struct moonfish_chess *ctx)
 {
 	int valid;
 	struct moonfish_castle castle;
--- a/main.c
+++ b/main.c
@@ -52,7 +52,7 @@
 		return 1;
 	}
 	
-	if (moonfish_nnue(ctx, file))
+	if (moonfish_nnue(&ctx->nnue, file))
 	{
 		fprintf(stderr, "%s: could not parse network\n", argv[0]);
 		fclose(file);
@@ -76,7 +76,7 @@
 		"        `--    *\n\n"
 	);
 	
-	moonfish_chess(ctx);
+	moonfish_chess(&ctx->chess);
 	
 	for (;;)
 	{
@@ -99,7 +99,7 @@
 		if (!strcmp(arg, "go"))
 		{
 			moonfish_best_move(ctx, &move);
-			moonfish_to_uci(name, &move, ctx->white);
+			moonfish_to_uci(name, &move, ctx->chess.white);
 			printf("bestmove %s\n", name);
 		}
 		else if (!strcmp(arg, "quit"))
@@ -119,7 +119,7 @@
 			if (!strcmp(arg, "fen"))
 			{
 				arg = strtok(NULL, "\r\n");
-				moonfish_fen(ctx, arg);
+				moonfish_fen(&ctx->chess, arg);
 				
 				arg = strstr(arg, "moves");
 				if (arg != NULL)
@@ -136,7 +136,7 @@
 			}
 			else if (!strcmp(arg, "startpos"))
 			{
-				moonfish_chess(ctx);
+				moonfish_chess(&ctx->chess);
 			}
 			else
 			{
@@ -149,7 +149,7 @@
 			if (arg != NULL && !strcmp(arg, "moves"))
 			{
 				while ((arg = strtok(NULL, "\r\n\t ")) != NULL)
-					moonfish_play_uci(ctx, arg);
+					moonfish_play_uci(&ctx->chess, arg);
 			}
 		}
 		else if (!strcmp(arg, "uci"))
--- a/moonfish.h
+++ b/moonfish.h
@@ -40,14 +40,19 @@
 	unsigned int black_oo:1, black_ooo:1;
 };
 
-struct moonfish
+struct moonfish_chess
 {
-	struct moonfish_nnue nnue;
 	unsigned char board[120];
 	unsigned char white;
 	struct moonfish_castle castle;
 };
 
+struct moonfish
+{
+	struct moonfish_nnue nnue;
+	struct moonfish_chess chess;
+};
+
 struct moonfish_move
 {
 	unsigned char from, to;
@@ -57,25 +62,25 @@
 	struct moonfish_castle castle;
 };
 
-int moonfish_nnue(struct moonfish *ctx, FILE *file);
+int moonfish_nnue(struct moonfish_nnue *ctx, FILE *file);
 
-void moonfish_chess(struct moonfish *ctx);
-void moonfish_fen(struct moonfish *ctx, char *fen);
+void moonfish_chess(struct moonfish_chess *ctx);
+void moonfish_fen(struct moonfish_chess *ctx, char *fen);
 
-void moonfish_moves(struct moonfish *ctx, struct moonfish_move *moves, unsigned char from);
+void moonfish_moves(struct moonfish_chess *ctx, struct moonfish_move *moves, unsigned char from);
 
-void moonfish_play(struct moonfish *ctx, struct moonfish_move *move);
-void moonfish_unplay(struct moonfish *ctx, struct moonfish_move *move);
+void moonfish_play(struct moonfish_chess *ctx, struct moonfish_move *move);
+void moonfish_unplay(struct moonfish_chess *ctx, struct moonfish_move *move);
 
 int moonfish_tanh(int value);
 
 int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *move);
 
-void moonfish_play_uci(struct moonfish *ctx, char *name);
+void moonfish_play_uci(struct moonfish_chess *ctx, char *name);
 void moonfish_to_uci(char *name, struct moonfish_move *move, int white);
 
-int moonfish_validate(struct moonfish *ctx);
-int moonfish_check(struct moonfish *ctx);
+int moonfish_validate(struct moonfish_chess *ctx);
+int moonfish_check(struct moonfish_chess *ctx);
 
 extern char *moonfish_network;
 
--- a/nnue.c
+++ b/nnue.c
@@ -15,7 +15,7 @@
 	return 0;
 }
 
-static int moonfish_read_nnue(struct moonfish *ctx, FILE *file)
+static int moonfish_read_nnue(struct moonfish_nnue *ctx, FILE *file)
 {
 	int ch;
 	unsigned int i, j;
@@ -25,23 +25,23 @@
 	if (fgetc(file) != 'L') return 1;
 	if (fgetc(file) != 'u') return 1;
 	if (fgetc(file) != 'n') return 1;
-	if (moonfish_read_array(ctx->nnue.pst0, sizeof ctx->nnue.pst0, file)) return 1;
-	if (moonfish_read_array(ctx->nnue.pst1, sizeof ctx->nnue.pst1, file)) return 1;
-	if (moonfish_read_array(ctx->nnue.pst3, sizeof ctx->nnue.pst3, file)) return 1;
-	if (moonfish_read_array(ctx->nnue.layer1, sizeof ctx->nnue.layer1, file)) return 1;
-	if (moonfish_read_array(ctx->nnue.layer2, sizeof ctx->nnue.layer2, file)) return 1;
+	if (moonfish_read_array(ctx->pst0, sizeof ctx->pst0, file)) return 1;
+	if (moonfish_read_array(ctx->pst1, sizeof ctx->pst1, file)) return 1;
+	if (moonfish_read_array(ctx->pst3, sizeof ctx->pst3, file)) return 1;
+	if (moonfish_read_array(ctx->layer1, sizeof ctx->layer1, file)) return 1;
+	if (moonfish_read_array(ctx->layer2, sizeof ctx->layer2, file)) return 1;
 	
 	for (i = 0 ; i < 4 ; i++)
 	for (j = 0 ; j < 6 * 8 ; j++)
 	{
-		v = ctx->nnue.pst0[i * 6 * 8 + j];
-		ctx->nnue.pst0[i * 6 * 8 + j] = ctx->nnue.pst0[(7 - i) * 6 * 8 + j];
-		ctx->nnue.pst0[(7 - i) * 6 * 8 + j] = v;
+		v = ctx->pst0[i * 6 * 8 + j];
+		ctx->pst0[i * 6 * 8 + j] = ctx->pst0[(7 - i) * 6 * 8 + j];
+		ctx->pst0[(7 - i) * 6 * 8 + j] = v;
 	}
 	
 	ch = fgetc(file);
 	if (ch == EOF) return 1;
-	ctx->nnue.scale = ch;
+	ctx->scale = ch;
 	
 	if (fgetc(file) != EOF) return 1;
 	
@@ -48,7 +48,7 @@
 	return 0;
 }
 
-int moonfish_nnue(struct moonfish *ctx, FILE *file)
+int moonfish_nnue(struct moonfish_nnue *ctx, FILE *file)
 {
 	int p, s, d, o, c, value;
 	
@@ -60,8 +60,8 @@
 	{
 		value = 0;
 		for (d = 0 ; d < 6 ; d++)
-			value += ctx->nnue.pst0[s * 6 + d] * ctx->nnue.pst1[o * 6 * 6 + d * 6 + p];
-		ctx->nnue.values0[p][s][o] = value / 127;
+			value += ctx->pst0[s * 6 + d] * ctx->pst1[o * 6 * 6 + d * 6 + p];
+		ctx->values0[p][s][o] = value / 127;
 	}
 	
 	for (c = 0 ; c < 2 ; c++)
@@ -71,8 +71,8 @@
 	{
 		value = 0;
 		for (d = 0 ; d < 10 ; d++)
-			value += ctx->nnue.values0[p][s][d] * ctx->nnue.pst3[o * 10 * 2 + d * 2 + c];
-		ctx->nnue.values[c][p][s][o] = value / 127;
+			value += ctx->values0[p][s][d] * ctx->pst3[o * 10 * 2 + d * 2 + c];
+		ctx->values[c][p][s][o] = value / 127;
 	}
 	
 	return 0;
--- a/search.c
+++ b/search.c
@@ -15,7 +15,7 @@
 	for (y = 0 ; y < 8 ; y++)
 	for (x = 0 ; x < 8 ; x++)
 	{
-		piece = ctx->board[(x + 1) + (y + 2) * 10];
+		piece = ctx->chess.board[(x + 1) + (y + 2) * 10];
 		if (piece == moonfish_empty) continue;
 		
 		color = (piece >> 4) - 1;
@@ -67,7 +67,7 @@
 	for (y = 0 ; y < 8 ; y++)
 	for (x = 0 ; x < 8 ; x++)
 	{
-		moonfish_moves(ctx, moves, (x + 1) + (y + 2) * 10);
+		moonfish_moves(&ctx->chess, moves, (x + 1) + (y + 2) * 10);
 		
 		for (move = moves ; move->piece != moonfish_outside ; move++)
 		{
@@ -74,9 +74,9 @@
 			if (depth <= 0 && move->captured == moonfish_empty) continue;
 			if (move->captured == moonfish_their_king) return moonfish_omega * (depth + 10);
 			
-			moonfish_play(ctx, move);
+			moonfish_play(&ctx->chess, move);
 			score = -moonfish_search(ctx, -beta, -alpha, depth - 1);
-			moonfish_unplay(ctx, move);
+			moonfish_unplay(&ctx->chess, move);
 			
 			if (score >= beta) return beta;
 			if (score > alpha) alpha = score;
@@ -98,20 +98,20 @@
 	for (y = 0 ; y < 8 ; y++)
 	for (x = 0 ; x < 8 ; x++)
 	{
-		moonfish_moves(ctx, moves, (x + 1) + (y + 2) * 10);
+		moonfish_moves(&ctx->chess, moves, (x + 1) + (y + 2) * 10);
 		
 		for (move = moves ; move->piece != moonfish_outside ; move++)
 		{
-			moonfish_play(ctx, move);
+			moonfish_play(&ctx->chess, move);
 			
-			if (!moonfish_validate(ctx))
+			if (!moonfish_validate(&ctx->chess))
 			{
-				moonfish_unplay(ctx, move);
+				moonfish_unplay(&ctx->chess, move);
 				continue;
 			}
 			
 			score = -moonfish_search(ctx, -10 * moonfish_omega, 10 * moonfish_omega, 3);
-			moonfish_unplay(ctx, move);
+			moonfish_unplay(&ctx->chess, move);
 			
 			if (score > best_score)
 			{
--- a/tools/play.c
+++ b/tools/play.c
@@ -13,7 +13,7 @@
 
 struct moonfish_fancy
 {
-	struct moonfish ctx;
+	struct moonfish_chess chess;
 	int white;
 	int our_time, their_time;
 	long int our_nano, their_nano;
@@ -36,10 +36,10 @@
 	else
 		printf("\x1B[48;5;69m");
 	
-	if (fancy->white == fancy->ctx.white) y = 7 - y;
+	if (fancy->white == fancy->chess.white) y = 7 - y;
 	else x = 7 - x;
 	
-	piece = fancy->ctx.board[(x + 1) + (y + 2) * 10];
+	piece = fancy->chess.board[(x + 1) + (y + 2) * 10];
 	
 	if (piece == moonfish_empty)
 	{
@@ -47,7 +47,7 @@
 		return;
 	}
 	
-	if (!fancy->ctx.white) piece ^= 0x30;
+	if (!fancy->chess.white) piece ^= 0x30;
 	
 	if (piece >> 4 == 1)
 		printf("\x1B[38;5;253m");
@@ -113,7 +113,7 @@
 	delta = timespec.tv_sec - fancy->timespec.tv_sec;
 	if (timespec.tv_nsec - fancy->timespec.tv_nsec > 500000000L) delta++;
 	
-	if (fancy->white == fancy->ctx.white)
+	if (fancy->white == fancy->chess.white)
 		our_time -= delta;
 	else
 		their_time -= delta;
@@ -183,7 +183,7 @@
 	exit(1);
 }
 
-static int moonfish_play_from(struct moonfish *ctx, struct moonfish_move *found, int x0, int y0, int x1, int y1)
+static int moonfish_play_from(struct moonfish_chess *chess, struct moonfish_move *found, int x0, int y0, int x1, int y1)
 {
 	struct moonfish_move moves[32];
 	struct moonfish_move *move;
@@ -192,15 +192,15 @@
 	y0 = 9 - y0;
 	y1 = 9 - y1;
 	
-	moonfish_moves(ctx, moves, x0 + (y0 + 1) * 10);
+	moonfish_moves(chess, moves, x0 + (y0 + 1) * 10);
 	
 	for (move = moves ; move->piece != moonfish_outside ; move++)
 	{
 		if (move->to == x1 + (y1 + 1) * 10)
 		{
-			moonfish_play(ctx, move);
-			valid = moonfish_validate(ctx);
-			moonfish_unplay(ctx, move);
+			moonfish_play(chess, move);
+			valid = moonfish_validate(chess);
+			moonfish_unplay(chess, move);
 			
 			if (valid)
 			{
@@ -228,12 +228,12 @@
 	
 	if (fancy->white)
 	{
-		if (fancy->ctx.white) time = &fancy->their_time, nano = &fancy->their_nano;
+		if (fancy->chess.white) time = &fancy->their_time, nano = &fancy->their_nano;
 		else time = &fancy->our_time, nano = &fancy->our_nano;
 	}
 	else
 	{
-		if (fancy->ctx.white) time = &fancy->our_time, nano = &fancy->our_nano;
+		if (fancy->chess.white) time = &fancy->our_time, nano = &fancy->our_nano;
 		else time = &fancy->their_time, nano = &fancy->their_nano;
 	}
 	
@@ -266,7 +266,7 @@
 	int white_time, black_time;
 	char *arg;
 	
-	if (fancy->white == fancy->ctx.white)
+	if (fancy->white == fancy->chess.white)
 	{
 		white_time = fancy->our_time * 1000 + fancy->our_nano / 1000000;
 		black_time = fancy->their_time * 1000 + fancy->their_nano / 1000000;
@@ -296,7 +296,7 @@
 	strcpy(name, arg);
 	
 	pthread_mutex_lock(fancy->mutex);
-	moonfish_play_uci(&fancy->ctx, arg);
+	moonfish_play_uci(&fancy->chess, arg);
 }
 
 int main(int argc, char **argv)
@@ -475,11 +475,11 @@
 	if (scanf("[%d;%dR", &oy, &ox) != 2) return 1;
 	oy -= 11;
 	
-	moonfish_chess(&fancy->ctx);
+	moonfish_chess(&fancy->chess);
 	
 	pthread_create(&thread, NULL, &moonfish_start, fancy);
 	
-	if (fancy->white != fancy->ctx.white)
+	if (fancy->white != fancy->chess.white)
 	{
 		*name++ = ' ';
 		moonfish_go(fancy, names + 1, name, in, out);
@@ -494,8 +494,8 @@
 	
 	for (ch0 = 0 ; ch0 != EOF ; ch0 = getchar())
 	{
-		if (fancy->white && !fancy->ctx.white) continue;
-		if (!fancy->white && fancy->ctx.white) continue;
+		if (fancy->white && !fancy->chess.white) continue;
+		if (!fancy->white && fancy->chess.white) continue;
 		
 		if (ch0 != 0x1B) continue;
 		ch0 = getchar();
@@ -552,15 +552,15 @@
 			if (x1 == 0) continue;
 			if (y1 == 0) continue;
 			
-			if (moonfish_play_from(&fancy->ctx, &move, fancy->x, fancy->y, x1, y1) == 0)
+			if (moonfish_play_from(&fancy->chess, &move, fancy->x, fancy->y, x1, y1) == 0)
 			{
 				*name++ = ' ';
-				moonfish_to_uci(name, &move, fancy->ctx.white);
+				moonfish_to_uci(name, &move, fancy->chess.white);
 				name += strlen(name);
 				
 				pthread_mutex_lock(fancy->mutex);
 				
-				moonfish_play(&fancy->ctx, &move);
+				moonfish_play(&fancy->chess, &move);
 				moonfish_reset_time(fancy);
 				fancy->x = 0;
 				moonfish_fancy(fancy);
--