shithub: moonfish

Download patch

ref: 2169d2bd3e0c2292f0bb58e2e12d30c1b859ec70
parent: cf02115762da42294a526181433a2acbe1cbc0df
author: zamfofex <zamfofex@twdb.moe>
date: Thu Nov 28 16:41:12 EST 2024

add support for 'go nodes'

--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@
 These are things that might be resolved eventually.
 
 - the TUIs do not let you underpromote
-- no support for `go depth`, `go infinite`, `go mate`, or `go nodes`
+- no support for `go depth`, `go infinite`, `go mate`
 - no FEN validation (may lead to potential exploits)
 
 download
--- a/main.c
+++ b/main.c
@@ -31,6 +31,7 @@
 	char *arg, *end;
 	char name[6];
 	struct moonfish_info *info;
+	long int node_count;
 	
 	info = data;
 	
@@ -37,6 +38,7 @@
 	our_time = -1;
 	their_time = -1;
 	time = -1;
+	node_count = -1;
 	
 	moonfish_root(info->root, &chess);
 	
@@ -89,11 +91,30 @@
 			
 			continue;
 		}
+		
+		if (!strcmp(arg, "nodes")) {
+			
+			arg = strtok(NULL, "\r\n\t ");
+			if (arg == NULL) {
+				fprintf(stderr, "malformed 'go nodes' command\n");
+				exit(1);
+			}
+			
+			errno = 0;
+			node_count = strtol(arg, &end, 10);
+			if (errno || *end != 0 || node_count < 0) {
+				fprintf(stderr, "malformed 'nodes' in 'go' command\n");
+				exit(1);
+			}
+			
+			continue;
+		}
 	}
 	
 	options.max_time = time;
 	options.our_time = our_time;
 	options.thread_count = info->thread_count;
+	options.node_count = node_count;
 	moonfish_best_move(info->root, &result, &options);
 	moonfish_to_uci(&chess, &result.move, name);
 	
--- a/mini.c
+++ b/mini.c
@@ -35,6 +35,7 @@
 		if (!strncmp(line, "go ", 3)) {
 			sscanf(line, "go wtime %d btime %d", &wtime, &btime);
 			options.max_time = -1;
+			options.node_count = -1;
 			options.our_time = chess.white ? wtime : btime;
 			moonfish_best_move(root, &result, &options);
 			moonfish_to_uci(&chess, &result.move, name);
--- a/moonfish.h
+++ b/moonfish.h
@@ -97,6 +97,7 @@
 struct moonfish_options {
 	long int max_time;
 	long int our_time;
+	long int node_count;
 	int thread_count;
 };
 
--- a/search.c
+++ b/search.c
@@ -59,6 +59,7 @@
 struct moonfish_data {
 	struct moonfish_root *root;
 	long int time, time0;
+	long int node_count;
 };
 
 double moonfish_values[] = {0,0,0,0,103,124,116,101,104,120,104,108,106,118,107,112,122,131,118,123,170,183,170,167,243,249,232,223,0,0,0,0,293,328,339,338,338,342,357,365,338,368,378,391,362,386,397,401,377,389,418,419,367,395,416,424,347,367,394,400,249,342,356,371,373,383,375,379,390,403,404,398,395,405,409,415,395,408,414,426,400,416,423,432,409,419,422,423,383,403,409,407,378,390,384,394,592,607,611,616,586,602,606,606,594,608,604,608,610,619,619,623,631,636,642,645,643,651,655,655,652,655,661,663,649,652,653,654,1181,1168,1172,1190,1178,1189,1199,1195,1187,1197,1203,1200,1191,1208,1209,1214,1211,1213,1226,1231,1217,1224,1240,1240,1197,1189,1233,1238,1214,1227,1239,1243,-21,2,-24,-31,-6,-2,-6,-8,-17,-1,4,8,-12,10,18,23,6,32,34,33,20,44,40,29,5,34,27,16,-50,-1,-5,-10};
@@ -319,6 +320,12 @@
 	moonfish_search(&data->root->node, &data->root->chess, 0x100);
 	while (moonfish_clock() - data->time0 < data->time) {
 		if (data->root->stop) break;
+#ifndef moonfish_mini
+		if (data->root->node.visits + 0x1000 >= data->node_count) {
+			moonfish_search(&data->root->node, &data->root->chess, data->node_count - data->root->node.visits);
+			break;
+		}
+#endif
 		count = data->root->node.count;
 		for (i = 0 ; i < data->root->node.count ; i++) {
 			if (data->root->node.children[i].ignored) count--;
@@ -332,8 +339,7 @@
 
 void moonfish_best_move(struct moonfish_root *root, struct moonfish_result *result, struct moonfish_options *options)
 {
-	static struct moonfish_data data;
-	
+	struct moonfish_data data;
 	struct moonfish_node *best_node;
 	long int time;
 	long int best_visits;
@@ -350,6 +356,8 @@
 	data.root = root;
 	data.time = time;
 	data.time0 = moonfish_clock();
+	if (options->node_count < 0) data.node_count = LONG_MAX;
+	else data.node_count = options->node_count;
 	
 #ifdef moonfish_no_threads
 	
--