ref: f450cdcbeaa4fcf1c2b688e830d0bcc4a1083014
parent: 4f8829009e8c26e6a878261e0bc4c7e7617ef6b6
author: zamfofex <zamfofex@twdb.moe>
date: Fri Oct 27 11:45:47 EDT 2023
fix and improve time management
--- a/main.c
+++ b/main.c
@@ -15,7 +15,7 @@
char *arg;
struct moonfish_move move;
char name[6];
- int time;
+ long int time, wtime, btime, *xtime;
if (argc > 1)
{@@ -53,24 +53,48 @@
if (!strcmp(arg, "go"))
{- if (ctx->chess.white) arg = strstr(arg, " wtime ");
- else arg = strstr(arg, " btime ");
+ wtime = -1;
+ btime = -1;
- if (arg == NULL)
+ for (;;)
{- time = 120000;
- }
- else
- {- if (sscanf(arg + 6, "%d", &time) != 1)
+ arg = strtok(NULL, "\r\n\t ");
+ if (arg == NULL) break;
+ if (!strcmp(arg, "wtime") || !strcmp(arg, "btime"))
{- free(ctx);
- fprintf(stderr, "%s: malformed 'go' command\n", argv[0]);
- return 1;
+ if (!strcmp(arg, "wtime")) xtime = &wtime;
+ else xtime = &btime;
+
+ arg = strtok(NULL, "\r\n\t ");
+
+ if (arg == NULL || sscanf(arg, "%ld", xtime) != 1 || *xtime < 0)
+ {+ free(ctx);
+ fprintf(stderr, "%s: malformed 'go' command\n", argv[0]);
+ return 1;
+ }
+
+ break;
}
}
- moonfish_best_move(ctx, &move, time / 1000);
+ if (wtime < 0 && btime < 0)
+ {+ wtime = 3600000;
+ btime = 3600000;
+ }
+
+ if (wtime < 0) wtime = btime;
+ if (btime < 0) btime = wtime;
+
+ if (!ctx->chess.white)
+ {+ time = wtime;
+ wtime = btime;
+ btime = time;
+ }
+
+ moonfish_best_move(ctx, &move, wtime / 1000, btime / 1000);
moonfish_to_uci(name, &move, ctx->chess.white);
printf("bestmove %s\n", name);}
--- a/minify.sh
+++ b/minify.sh
@@ -55,6 +55,9 @@
# remove duplicate lines (for '#include')
awk '!x[$0]++' |
+# replace 'moonfish_' prefix with 'M'
+sed 's/\bmoonfish_/M/g' |
+
# store the result into a file
tee moonfish.c |
--- a/moonfish.h
+++ b/moonfish.h
@@ -64,7 +64,7 @@
void moonfish_play(struct moonfish_chess *chess, struct moonfish_move *move);
void moonfish_unplay(struct moonfish_chess *chess, struct moonfish_move *move);
-int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *move, int time);
+int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *move, long int our_time, long int their_time);
void moonfish_from_uci(struct moonfish_chess *chess, struct moonfish_move *move, char *name);
void moonfish_to_uci(char *name, struct moonfish_move *move, int white);
--- a/search.c
+++ b/search.c
@@ -176,23 +176,38 @@
#endif
-int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *best_move, int d)
+int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *best_move, long int our_time, long int their_time)
{- time_t t, t0, t1;
+ time_t t, d;
int i;
int score;
+ int base;
- d /= 4;
- t0 = time(NULL);
+ d = our_time - their_time;
+ if (d < 0) d = 0;
+ d += our_time / 8;
- for (i = 0 ; i < 8 ; i++)
+ t = 0;
+ base = 3;
+
+ while (t < 2)
{+ base++;
t = time(NULL);
- score = moonfish_best_move_depth(ctx, best_move, i);
-
- t1 = time(NULL);
- if ((t1 - t) * 32 > d - (t1 - t0)) break;
+ score = moonfish_best_move_depth(ctx, best_move, base);
+ t = time(NULL) - t + 2;
}
- return score;
+ i = base;
+
+ for (;;)
+ {+ t *= 32;
+ if (t > d) break;
+ i++;
+ if (i >= 8) break;
+ }
+
+ if (i == base) return score;
+ return moonfish_best_move_depth(ctx, best_move, i);
}
--- a/tools/lichess.c
+++ b/tools/lichess.c
@@ -771,7 +771,7 @@
speed = cJSON_GetObjectItem(challenge, "speed");
if (!cJSON_IsString(speed)) moonfish_json_error(argv0);
- if (!strcmp(speed->valuestring, "correspondence"))
+ if (!strcmp(speed->valuestring, "classical") || !strcmp(speed->valuestring, "correspondence"))
{snprintf(line, sizeof line, "POST /api/challenge/%s/decline", id->valuestring);
if (moonfish_basic_request(argv0, name, port, token, line, "", "reason=tooSlow"))
--
⑨