X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=gnushogi%2Fcommondsp.c;h=2d4fed577ec9bde7876c1d286caa66cce646b6f0;hb=da2a684903fc7a96d47b43dc2feeecde53bd1aa6;hp=4c6dfa42a90782d39588d7d7e77b84b9081e24bc;hpb=21a60b5aae737b538480c4d8beee575b3d9a8e35;p=gnushogi.git diff --git a/gnushogi/commondsp.c b/gnushogi/commondsp.c index 4c6dfa4..2d4fed5 100644 --- a/gnushogi/commondsp.c +++ b/gnushogi/commondsp.c @@ -1600,8 +1600,6 @@ ReadFEN(char *fen) } -/* FIXME! This is truly the function from hell! */ - /* * Process the user's command. If easy mode is OFF (the computer is thinking * on opponents time) and the program is out of book, then make the 'hint' @@ -1610,27 +1608,16 @@ ReadFEN(char *fen) * the hint move, then set Sdepth to zero. */ -int -InputCommand(char *command, int root) +static char ponderString[20]; + +void +PonderOnHintMove(void) { #ifdef QUIETBACKGROUND short have_shown_prompt = false; #endif - short ok, done, is_move = false; unsigned short mv; - char s[200], sx[200], s2[200]; - static char backlog[200], ponderString[20]; - - ok = flag.quit = done = false; - player = opponent; - -#if ttblsz - if (TTadd > ttbllimit) - ZeroTTable(); -#endif - while ((hint > 0) && !flag.easy && !flag.force && !command && !backlog[0] && root) - { /* * A hint move for the player is available. Compute a move for the * opponent in background mode assuming that the hint move will be @@ -1693,76 +1680,24 @@ InputCommand(char *command, int root) ElapsedTime(COMPUTE_AND_INIT_MODE); GameList[GameCnt-1].time = (short) (et + 50)/100; /* FIXME: this is wrong */ - if(TCflag && TimeControl.moves[computer] == 0) - SetTimeControl(); /* add time for next session */ + RenewTimeControl(computer, 0); /* add time for next session */ } Sdepth = 0; } time0 = ft; /* Restore reference time for the player. */ ponderString[0] = '\0'; - /* on a ponder miss or other command, loop terminates because of backlog */ - } - - while(!(ok || flag.quit || done)) - { - player = opponent; - - if (flag.analyze && !command && !backlog[0] && root) { - SelectMove(opponent, BACKGROUND_MODE); - } - -#ifdef QUIETBACKGROUND - if (!have_shown_prompt) - { -#endif /* QUIETBACKGROUND */ - - dsp->ShowPrompt(); - -#ifdef QUIETBACKGROUND - } - - have_shown_prompt = false; -#endif /* QUIETBACKGROUND */ - - if (!command && backlog[0]) command = backlog; /* pick up backlogged command */ - - if (command == NULL) { - int eof = dsp->GetString(sx); - if (eof) - dsp->ExitShogi(); - } else { - strcpy(sx, command); - backlog[0]= '\0'; /* make sure no backlog is left */ - command = NULL; - } - - /* extract first word */ - if (sscanf(sx, "%s %s", s, s2) < 1) - continue; - - if (!root && (strcmp(s, "usermove") == 0) - && (strcmp(s2, ponderString) == 0)) - { /* ponder hit; switch to normal search */ - background = false; - hint = 0; - if (TCflag) - { /* account opponent time and moves */ - TimeControl.clock[opponent] -= et; - timeopp[oppptr] = et; - --TimeControl.moves[opponent]; - if(TimeControl.moves[computer] == 0) SetTimeControl(); - } - SetResponseTime(computer); - strcpy(ponderString, "hit"); - return false; /* no search abort */ - } +} - if (!root && strcmp(s, ".") && strcmp(s, "time") && strcmp(s, "otim")) - { /* during search most commands can only be done after abort */ - strcpy(backlog, sx); /* backlog the command */ - return true; /* and order search abort */ - } +/* + * Recognize the command s from input line sx, and perform the action it specifies. + * Returns whether the command could cause it to be out turn to move. + */ +int +ParseAndExecuteCommand(char *s, char *sx) +{ + short ok; + unsigned short mv; if (strcmp(s, "bd") == 0) /* bd -- display board */ { @@ -2200,33 +2135,134 @@ InputCommand(char *command, int root) flag.mate = true; } - else + else if (XSHOGI) { - is_move = true; - } + /* add remaining time in milliseconds for xshogi */ + printf("%d. %s %ld\n", + ++mycnt2, s, TimeControl.clock[player] * 10); + } } Sdepth = 0; } + + return ok; +} + +/* + * Read commands from input, and execute them, until it becomes our turn to move. + * When called during a background search (root = false) it just backlogs the + * input command without executing it, and returns immediately. Unless the command + * was the move on which the search was pondering. In that case we turn the ongoing + * search into a foreground search. To judge this, it is also necessary to process + * the 'time' and 'otim' commands that preceed the move. The '.' command is also + * always processed, to prevent it from aborting an analysis search. + * The time spent waiting for input can be filled with background searches for + * pondering or analysis. (In !root mode input is guaranteed to be pending already!) + */ +int +InputCommand(int root) +{ +#ifdef QUIETBACKGROUND + short have_shown_prompt = false; +#endif + short ok; + char s[200], sx[200], s2[200]; + static char backlog[200]; + + ok = flag.quit = false; + player = opponent; + +#if ttblsz + /* CHECKME: should this also be done in the following while loop? */ + if (TTadd > ttbllimit) + ZeroTTable(); +#endif + + while ((hint > 0) && !flag.easy && !flag.force && !backlog[0] && root) + { + /* + * A hint move for the player is available. Compute a move for the + * opponent in background mode assuming that the hint move will be + * selected by the player. + * Terminate this search on input, which will then be saved in backlog[]. + * Unless the input was the hint move ('ponder hit'). Then that move will + * be played (after the search times out) in addition to the hint. There + * will then be no backlog, and we start pondering on the new hint move. + */ + + PonderOnHintMove(); + } + + while(!(ok || flag.quit)) + { /* process input commands until our it becomes our turn to move */ + player = opponent; + + /* in analysis mode we do a background search while waiting for input */ + if (flag.analyze && !backlog[0] && root) { + SelectMove(opponent, BACKGROUND_MODE); + } + +#ifdef QUIETBACKGROUND + if (!have_shown_prompt) + { +#endif /* QUIETBACKGROUND */ + + dsp->ShowPrompt(); + +#ifdef QUIETBACKGROUND + } + + have_shown_prompt = false; +#endif /* QUIETBACKGROUND */ + + if (!backlog[0]) { /* read new input line */ + int eof = dsp->GetString(sx); + if (eof) + dsp->ExitShogi(); + } else { /* or use backlogged input line */ + strcpy(sx, backlog); + backlog[0]= '\0'; /* make sure no backlog is left */ + } + + /* extract first word */ + if (sscanf(sx, "%s %s", s, s2) < 1) + continue; + + if (!root && (strcmp(s, "usermove") == 0) + && (strcmp(s2, ponderString) == 0)) + { /* ponder hit; switch to normal search */ + background = false; + hint = 0; + if (TCflag) + { /* account opponent time and moves */ + TimeControl.clock[opponent] -= et; + timeopp[oppptr] = et; + if (--TimeControl.moves[opponent] == 0) + TimeControl.moves[opponent] = TCmoves; /* assumes uni-TC! */ + } + SetResponseTime(computer); + strcpy(ponderString, "hit"); + return false; /* no search abort */ + } + + if (!root && strcmp(s, ".") && strcmp(s, "time") && strcmp(s, "otim")) + { /* during search most commands can only be done after abort */ + strcpy(backlog, sx); /* backlog the command */ + return true; /* and order search abort */ + } + + ok = ParseAndExecuteCommand(s, sx); /* returns whether turn changed */ } ElapsedTime(COMPUTE_AND_INIT_MODE); + /* kludge alert: change the side we play to prevent starting a search */ if (flag.force) { computer = opponent; opponent = computer ^ 1; } - if (XSHOGI) - { - /* add remaining time in milliseconds for xshogi */ - if (is_move) - { - printf("%d. %s %ld\n", - ++mycnt2, s, TimeControl.clock[player] * 10); - } - } - return true; }