X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=backend.c;h=c34724e5ef514395e048a34f528aa1e505129a98;hb=797e76a7d465fb38fafa878fe62abb768213ba1d;hp=b8cfa92f2ceacc4fbf810305c9e9b49ecc4c22ef;hpb=ea750683ac62717dd7346de17b5ae072622ff92a;p=xboard.git diff --git a/backend.c b/backend.c index b8cfa92..c34724e 100644 --- a/backend.c +++ b/backend.c @@ -2,8 +2,10 @@ * backend.c -- Common back end for X and Windows NT versions of * XBoard $Id: backend.c,v 2.6 2003/11/28 09:37:36 mann Exp $ * - * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts. - * Enhancements Copyright 1992-2001 Free Software Foundation, Inc. + * Copyright 1991 by Digital Equipment Corporation, Maynard, + * Massachusetts. Enhancements Copyright + * 1992-2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software + * Foundation, Inc. * * The following terms apply to Digital Equipment Corporation's copyright * interest in XBoard: @@ -27,25 +29,25 @@ * SOFTWARE. * ------------------------------------------------------------------------ * - * The following terms apply to the enhanced version of XBoard distributed - * by the Free Software Foundation: + * The following terms apply to the enhanced version of XBoard + * distributed by the Free Software Foundation: * ------------------------------------------------------------------------ - * This program is free software; you can redistribute it and/or modify + * + * GNU XBoard is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * GNU XBoard is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * ------------------------------------------------------------------------ + * along with this program. If not, see http://www.gnu.org/licenses/. * * - * See the file ChangeLog for a revision history. */ + *------------------------------------------------------------------------ + ** See the file ChangeLog for a revision history. */ /* [AS] Also useful here for debugging */ #ifdef WIN32 @@ -68,6 +70,7 @@ #include #include #include +#include #if STDC_HEADERS # include @@ -208,6 +211,10 @@ void ParseFeatures P((char* args, ChessProgramState *cps)); void InitBackEnd3 P((void)); void FeatureDone P((ChessProgramState* cps, int val)); void InitChessProgram P((ChessProgramState *cps, int setup)); +void OutputKibitz(int window, char *text); +int PerpetualChase(int first, int last); +int EngineOutputIsUp(); +void InitDrawingSizes(int x, int y); #ifdef WIN32 extern void ConsoleCreate(); @@ -226,8 +233,7 @@ extern char installDir[MSG_SIZ]; extern int tinyLayout, smallLayout; ChessProgramStats programStats; static int exiting = 0; /* [HGM] moved to top */ -static int setboardSpoiledMachineBlack = 0, errorExitFlag = 0; -extern int startedFromPositionFile; +static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/; int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */ char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */ int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */ @@ -288,6 +294,8 @@ static char * safeStrCpy( char * dst, const char * src, size_t count ) return dst; } +#if 0 +//[HGM] for future use? Conditioned out for now to suppress warning. static char * safeStrCat( char * dst, const char * src, size_t count ) { size_t dst_len; @@ -304,6 +312,7 @@ static char * safeStrCat( char * dst, const char * src, size_t count ) return dst; } +#endif /* Some compiler can't cast u64 to double * This function do the job for us: @@ -342,6 +351,8 @@ PosFlags(index) flags &= ~F_ALL_CASTLE_OK; case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC! flags |= F_IGNORE_CHECK; + case VariantLosers: + flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist break; case VariantAtomic: flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE; @@ -608,6 +619,7 @@ InitBackEnd1() ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options GetTimeMark(&programStartTime); + srand(programStartTime.ms); // [HGM] book: makes sure random is unpredictabe to msec level ClearProgramStats(); programStats.ok_to_send = 1; @@ -737,6 +749,8 @@ InitBackEnd1() first.useFEN960 = FALSE; second.useFEN960 = FALSE; first.useOOCastle = TRUE; second.useOOCastle = TRUE; /* End of new features added by Tord. */ + first.fenOverride = appData.fenOverride1; + second.fenOverride = appData.fenOverride2; /* [HGM] time odds: set factor for each machine */ first.timeOdds = appData.firstTimeOdds; @@ -759,6 +773,10 @@ InitBackEnd1() first.debug = second.debug = FALSE; first.supportsNPS = second.supportsNPS = UNKNOWN; + /* [HGM] options */ + first.optionSettings = appData.firstOptions; + second.optionSettings = appData.secondOptions; + first.scoreIsAbsolute = appData.firstScoreIsAbsolute; /* [AS] */ second.scoreIsAbsolute = appData.secondScoreIsAbsolute; /* [AS] */ first.isUCI = appData.firstIsUCI; /* [AS] */ @@ -1937,7 +1955,7 @@ VariantSwitch(Board board, VariantClass newVariant) { int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j; int oldCurrentMove = currentMove, oldForwardMostMove = forwardMostMove, oldBackwardMostMove = backwardMostMove; - Board tempBoard; int saveCastling[BOARD_SIZE], saveEP; +// Board tempBoard; int saveCastling[BOARD_SIZE], saveEP; startedFromPositionFile = FALSE; if(gameInfo.variant == newVariant) return; @@ -2353,10 +2371,10 @@ read_from_ics(isr, closure, data, count, error) (StrStr(star_match[0], gameInfo.white) == star_match[0] || StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent suppressKibitz = TRUE; - if((StrStr(star_match[0], gameInfo.white) == star_match[0]) - && (gameMode == IcsPlayingWhite) || - (StrStr(star_match[0], gameInfo.black) == star_match[0]) - && (gameMode == IcsPlayingBlack) ) // opponent kibitz + if((StrStr(star_match[0], gameInfo.white) == star_match[0] + && (gameMode == IcsPlayingWhite)) || + (StrStr(star_match[0], gameInfo.black) == star_match[0] + && (gameMode == IcsPlayingBlack)) ) // opponent kibitz started = STARTED_CHATTER; // own kibitz we simply discard else { started = STARTED_COMMENT; // make sure it will be collected in parse[] @@ -2846,7 +2864,7 @@ read_from_ics(isr, closure, data, count, error) if(bookHit) { // [HGM] book: simulate book reply static char bookMove[MSG_SIZ]; // a bit generous? - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -3876,7 +3894,7 @@ ParseBoard12(string) if(bookHit) { // [HGM] book: simulate book reply static char bookMove[MSG_SIZ]; // a bit generous? - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -4096,7 +4114,7 @@ ProcessICSInitScript(f) void AlphaRank(char *move, int n) { - char *p = move, c; int x, y; +// char *p = move, c; int x, y; if (appData.debugMode) { fprintf(debugFP, "alphaRank(%s,%d)\n", move, n); @@ -4240,106 +4258,7 @@ ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar) } } -/* [AS] FRC game initialization */ -static int FindEmptySquare( Board board, int n ) -{ - int i = 0; - - while( 1 ) { - while( board[0][i] != EmptySquare ) i++; - if( n == 0 ) - break; - n--; - i++; - } - - return i; -} - -#if 0 -static void ShuffleFRC( Board board ) -{ - int i; - - srand( time(0) ); - - for( i=0; i<8; i++ ) { - board[0][i] = EmptySquare; - } - - board[0][(rand() % 4)*2 ] = WhiteBishop; /* On dark square */ - board[0][(rand() % 4)*2+1] = WhiteBishop; /* On lite square */ - board[0][FindEmptySquare(board, rand() % 6)] = WhiteQueen; - board[0][FindEmptySquare(board, rand() % 5)] = WhiteKnight; - board[0][FindEmptySquare(board, rand() % 4)] = WhiteKnight; - board[0][ i=FindEmptySquare(board, 0) ] = WhiteRook; - initialRights[1] = initialRights[4] = - castlingRights[0][1] = castlingRights[0][4] = i; - board[0][ i=FindEmptySquare(board, 0) ] = WhiteKing; - initialRights[2] = initialRights[5] = - castlingRights[0][2] = castlingRights[0][5] = i; - board[0][ i=FindEmptySquare(board, 0) ] = WhiteRook; - initialRights[0] = initialRights[3] = - castlingRights[0][0] = castlingRights[0][3] = i; - - for( i=BOARD_LEFT; i> 8 ^ random() << 8; if(seed<0) seed = -seed; @@ -4369,9 +4287,9 @@ int put(Board board, int pieceType, int rank, int n, int shade) int i; for(i=BOARD_LEFT; i (int) WhitePawn; p--) { if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue; @@ -4495,8 +4413,6 @@ void SetUpShuffle(Board board, int number) if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize } -#endif - int SetCharTable( char *table, const char * map ) /* [HGM] moved here from winboard.c because of its general usefulness */ /* Basically a safe strcpy that uses the last character as King */ @@ -4614,11 +4530,6 @@ InitPosition(redraw) break; case VariantTwoKings: pieces = twoKingsArray; - nrCastlingRights = 8; /* add rights for second King */ - castlingRights[0][6] = initialRights[2] = 5; - castlingRights[0][7] = initialRights[5] = 5; - castlingRank[6] = 0; - castlingRank[7] = BOARD_HEIGHT-1; break; case VariantCapaRandom: shuffleOpenings = TRUE; @@ -4639,10 +4550,10 @@ InitPosition(redraw) nrCastlingRights = 6; castlingRights[0][0] = initialRights[0] = BOARD_RGHT-1; castlingRights[0][1] = initialRights[1] = BOARD_LEFT; - castlingRights[0][2] = initialRights[2] = BOARD_WIDTH-1>>1; + castlingRights[0][2] = initialRights[2] =(BOARD_WIDTH-1)>>1; castlingRights[0][3] = initialRights[3] = BOARD_RGHT-1; castlingRights[0][4] = initialRights[4] = BOARD_LEFT; - castlingRights[0][5] = initialRights[5] = BOARD_WIDTH-1>>1; + castlingRights[0][5] = initialRights[5] =(BOARD_WIDTH-1)>>1; break; case VariantFalcon: pieces = FalconArray; @@ -4784,8 +4695,8 @@ InitPosition(redraw) if(gameInfo.variant == VariantSuper) Prelude(initialPosition); if(gameInfo.variant == VariantGreat) { // promotion commoners - initialPosition[PieceToNumber(WhiteMan)][BOARD_RGHT-1] = WhiteMan; - initialPosition[PieceToNumber(WhiteMan)][BOARD_RGHT-2] = 9; + initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan; + initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9; initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan; initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9; } @@ -4844,7 +4755,7 @@ SendBoard(cps, moveNum) char message[MSG_SIZ]; if (cps->useSetboard) { - char* fen = PositionToFEN(moveNum, cps->useFEN960); + char* fen = PositionToFEN(moveNum, cps->fenOverride); sprintf(message, "setboard %s\n", fen); SendToProgram(message, cps); free(fen); @@ -5436,7 +5347,7 @@ if(appData.debugMode) fprintf(debugFP, "moveType 2 = %d, promochar = %x\n", move if(bookHit) { // [HGM] book: simulate book reply static char bookMove[MSG_SIZ]; // a bit generous? - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -5469,7 +5380,7 @@ if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", move void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats ) { - char * hint = lastHint; +// char * hint = lastHint; FrontEndProgramStats stats; stats.which = cps == &first ? 0 : 1; @@ -5663,8 +5574,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h sprintf(buf1, _("Illegal move \"%s\" from %s machine"), machineMove, cps->which); DisplayError(buf1, 0); - sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d%c", - machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0); + sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d", + machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType); if (gameMode == TwoMachinesPlay) { GameEnds(machineWhite ? BlackWins : WhiteWins, buf1, GE_XBOARD); @@ -5709,6 +5620,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h toX--; currentMoveString[2]--; break; + default: ; // nothing to do, but suppresses warning of pedantic compilers } } hintRequested = FALSE; @@ -5731,8 +5643,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h programStats.depth, programStats.score / 100., programStats.time / 100., - (double) programStats.nodes, - programStats.nodes / (10*abs(programStats.time) + 1.), + u64ToDouble(programStats.nodes), + u64ToDouble(programStats.nodes) / (10*abs(programStats.time) + 1.), programStats.movelist); SendToICS(buf); } @@ -5801,8 +5713,13 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h if(appData.checkMates) { SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */ ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/ - GameEnds( GameIsDrawn, "Xboard adjudication: Stalemate", - GE_XBOARD ); + if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantSuicide + || gameInfo.variant == VariantGiveaway) // [HGM] losers: + GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, // stalemated side wins! + "Xboard adjudication: Stalemate", GE_XBOARD ); + else + GameEnds( GameIsDrawn, "Xboard adjudication: Stalemate", GE_XBOARD ); + return; } break; case MT_CHECKMATE: @@ -5810,9 +5727,10 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h if(appData.checkMates) { SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */ ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/ - GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins, - "Xboard adjudication: Checkmate", - GE_XBOARD ); + GameEnds( WhiteOnMove(forwardMostMove) != (gameInfo.variant == VariantLosers) // [HGM] losers: + ? BlackWins : WhiteWins, // reverse the result ( A!=1 is !A for a boolean) + "Xboard adjudication: Checkmate", GE_XBOARD ); + return; } break; } @@ -5820,8 +5738,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h if( appData.testLegality ) { /* [HGM] Some more adjudications for obstinate engines */ int NrWN=0, NrBN=0, NrWB=0, NrBB=0, NrWR=0, NrBR=0, - NrWQ=0, NrBQ=0, NrW=0, bishopsColor = 0, - NrPieces=0, NrPawns=0, PawnAdvance=0, i, j, k; + NrWQ=0, NrBQ=0, NrW=0, NrK=0, bishopsColor = 0, + NrPieces=0, NrPawns=0, PawnAdvance=0, i, j; static int moveCount = 6; /* First absolutely insufficient mating material. Count what is on board. */ @@ -5831,6 +5749,9 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h switch((int) p) { /* count B,N,R and other of each side */ + case WhiteKing: + case BlackKing: + NrK++; break; // [HGM] atomic: count Kings case WhiteKnight: NrWN++; break; case WhiteBishop: @@ -5867,7 +5788,47 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h } } - if( NrPieces == 2 || gameInfo.variant != VariantXiangqi && + if(gameInfo.variant == VariantAtomic && NrK < 2) { + // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal + epStatus[forwardMostMove] = EP_CHECKMATE; // make claimable as if stm is checkmated + if(appData.checkMates) { + SendMoveToProgram(forwardMostMove-1, cps->other); // make sure opponent gets move + ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/ + GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins, + "Xboard adjudication: King destroyed", GE_XBOARD ); + return; + } + } + + /* Bare King in Shatranj (loses) or Losers (wins) */ + if( NrW == 1 || NrPieces - NrW == 1) { + if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first) + epStatus[forwardMostMove] = EP_STALEMATE; // kludge to make position claimable as win + if(appData.checkMates) { + SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */ + ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/ + GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, + "Xboard adjudication: Bare king", GE_XBOARD ); + return; + } + } else + if( gameInfo.variant == VariantShatranj && --bare < 0) + { /* bare King */ + epStatus[forwardMostMove] = EP_CHECKMATE; // make claimable as win for stm + if(appData.checkMates) { + /* but only adjudicate if adjudication enabled */ + SendMoveToProgram(forwardMostMove-1, cps->other); // make sure opponent gets move + ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/ + GameEnds( NrW > 1 ? WhiteWins : NrPieces - NrW > 1 ? BlackWins : GameIsDrawn, + "Xboard adjudication: Bare king", GE_XBOARD ); + return; + } + } + } else bare = 1; + + + if( NrPieces == 2 || gameInfo.variant != VariantXiangqi && + gameInfo.variant != VariantShatranj && // [HGM] baring will remain possible (NrPieces == 3 && NrWN+NrBN+NrWB+NrBB == 1 || NrPieces == NrBB+NrWB+2 && bishopsColor != 3)) // [HGM] all Bishops (Ferz!) same color { /* KBK, KNK, KK of KBKB with like Bishops */ @@ -5885,20 +5846,6 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h } } - /* Shatranj baring rule */ - if( gameInfo.variant == VariantShatranj && (NrW == 1 || NrPieces - NrW == 1) ) - { /* bare King */ - - if(--bare < 0 && appData.checkMates) { - /* but only adjudicate them if adjudication enabled */ - SendMoveToProgram(forwardMostMove-1, cps->other); /* make sure opponent gets to see move */ - ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/ - GameEnds( NrW > 1 ? WhiteWins : NrPieces - NrW > 1 ? BlackWins : GameIsDrawn, - "Xboard adjudication: Bare king", GE_XBOARD ); - return; - } - } else bare = 1; - /* Then some trivial draws (only adjudicate, cannot be claimed) */ if(NrPieces == 4 && ( NrWR == 1 && NrBR == 1 /* KRKR */ @@ -5990,6 +5937,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h EP_NONE, castlingRights[m-1]) != MT_CHECK) hisPerpetual = 0; // the opponent did not always check } + if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n", + ourPerpetual, hisPerpetual); if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, "Xboard adjudication: perpetual checking", GE_XBOARD ); @@ -5997,8 +5946,19 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h } if(hisPerpetual && !ourPerpetual) // he is checking us, but did not repeat yet break; // (or we would have caught him before). Abort repetition-checking loop. - // if neither of us is checking all the time, or both are, it is draw - // (illegal-chase forfeits not implemented yet!) + // Now check for perpetual chases + if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase + hisPerpetual = PerpetualChase(k, forwardMostMove); + ourPerpetual = PerpetualChase(k+1, forwardMostMove); + if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit + GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, + "Xboard adjudication: perpetual chasing", GE_XBOARD ); + return; + } + if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet + break; // Abort repetition-checking loop. + } + // if neither of us is checking or chasing all the time, or both are, it is draw } GameEnds( GameIsDrawn, "Xboard adjudication: repetition draw", GE_XBOARD ); return; @@ -6107,7 +6067,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h strcat(bookMove, bookHit); message = bookMove; cps = cps->other; - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -6641,8 +6601,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */ int ticklen; - if(cps->nps == 0) ticklen = 10*time; // use engine reported time - else ticklen = (1000. * nodes) / cps->nps; // convert node count to time + if(cps->nps == 0) ticklen = 10*time; // use engine reported time + else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time if(WhiteOnMove(forwardMostMove)) whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen; else blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen; @@ -6801,7 +6761,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h else { buf1[0] = NULLCHAR; - if (sscanf(message, "%d%c %d %d %lu %[^\n]\n", + if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n", &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) { ChessProgramStats cpstats; @@ -7046,7 +7006,7 @@ ApplyMove(fromX, fromY, toX, toY, promoChar, board) /* [HGM] compute & store e.p. status and castling rights for new position */ /* if we are updating a board for which those exist (i.e. in boards[]) */ if((p = ((int)board - (int)boards[0])/((int)boards[1]-(int)boards[0])) < MAX_MOVES && p > 0) - { int i, j; + { int i; if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A; oldEP = epStatus[p-1]; @@ -7331,15 +7291,15 @@ MakeMove(fromX, fromY, toX, toY, promoChar) int fromX, fromY, toX, toY; int promoChar; { - forwardMostMove++; +// forwardMostMove++; // [HGM] bare: moved downstream - if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting */ + if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */ int timeLeft; static int lastLoadFlag=0; int king, piece; - piece = boards[forwardMostMove-1][fromY][fromX]; + piece = boards[forwardMostMove][fromY][fromX]; king = piece < (int) BlackPawn ? WhiteKing : BlackKing; if(gameInfo.variant == VariantKnightmate) king += (int) WhiteUnicorn - (int) WhiteKing; - if(forwardMostMove == 1) { + if(forwardMostMove == 0) { if(blackPlaysFirst) fprintf(serverMoves, "%s;", second.tidy); fprintf(serverMoves, "%s;", first.tidy); @@ -7357,9 +7317,9 @@ MakeMove(fromX, fromY, toX, toY, promoChar) fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY); } // e.p. suffix - if( (boards[forwardMostMove-1][fromY][fromX] == WhitePawn || - boards[forwardMostMove-1][fromY][fromX] == BlackPawn ) && - boards[forwardMostMove-1][toY][toX] == EmptySquare + if( (boards[forwardMostMove][fromY][fromX] == WhitePawn || + boards[forwardMostMove][fromY][fromX] == BlackPawn ) && + boards[forwardMostMove][toY][toX] == EmptySquare && fromX != toX ) fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY); // promotion suffix @@ -7367,28 +7327,29 @@ MakeMove(fromX, fromY, toX, toY, promoChar) fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY); if(!loadFlag) { fprintf(serverMoves, "/%d/%d", - pvInfoList[forwardMostMove-1].depth, pvInfoList[forwardMostMove-1].score); - if(forwardMostMove & 1) timeLeft = whiteTimeRemaining/1000; - else timeLeft = blackTimeRemaining/1000; + pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score); + if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000; + else timeLeft = blackTimeRemaining/1000; fprintf(serverMoves, "/%d", timeLeft); } fflush(serverMoves); } - if (forwardMostMove >= MAX_MOVES) { + if (forwardMostMove+1 >= MAX_MOVES) { DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"), 0, 1); return; } SwitchClocks(); - timeRemaining[0][forwardMostMove] = whiteTimeRemaining; - timeRemaining[1][forwardMostMove] = blackTimeRemaining; - if (commentList[forwardMostMove] != NULL) { - free(commentList[forwardMostMove]); - commentList[forwardMostMove] = NULL; - } - CopyBoard(boards[forwardMostMove], boards[forwardMostMove - 1]); - ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove]); + timeRemaining[0][forwardMostMove+1] = whiteTimeRemaining; + timeRemaining[1][forwardMostMove+1] = blackTimeRemaining; + if (commentList[forwardMostMove+1] != NULL) { + free(commentList[forwardMostMove+1]); + commentList[forwardMostMove+1] = NULL; + } + CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]); + ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]); + forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board gameInfo.result = GameUnfinished; if (gameInfo.resultDetails != NULL) { free(gameInfo.resultDetails); @@ -7770,20 +7731,35 @@ GameEnds(result, resultDetails, whosays) if(gameMode == TwoMachinesPlay && appData.testClaims) { if(appData.testLegality && whosays >= GE_ENGINE1 ) { char claimer; + ChessMove trueResult = (ChessMove) -1; claimer = whosays == GE_ENGINE1 ? /* color of claimer */ first.twoMachinesColor[0] : second.twoMachinesColor[0] ; - if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && - (result == WhiteWins && claimer == 'w' || - result == BlackWins && claimer == 'b' ) ) { - if (appData.debugMode) { - fprintf(debugFP, "result=%d sp=%d move=%d\n", - result, epStatus[forwardMostMove], forwardMostMove); + + // [HGM] losers: because the logic is becoming a bit hairy, determine true result first + if(epStatus[forwardMostMove] == EP_CHECKMATE) { + /* [HGM] verify: engine mate claims accepted if they were flagged */ + trueResult = WhiteOnMove(forwardMostMove) != (gameInfo.variant == VariantLosers) + ? BlackWins : WhiteWins; // [HGM] losers: reverse the result in VariantLosers! + } else + if(epStatus[forwardMostMove] == EP_STALEMATE) { + trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE + if(gameInfo.variant == VariantGiveaway || gameInfo.variant == VariantSuicide || + gameInfo.variant == VariantLosers) // [HGM] losers: in giveaway variants stalemate wins + trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; } - /* [HGM] verify: engine mate claims accepted if they were flagged */ - if(epStatus[forwardMostMove] != EP_CHECKMATE && - result != (WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins)) { + + // now verify win claims, but not in drop games, as we don't understand those yet + if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper + || gameInfo.variant == VariantGreat) && + (result == WhiteWins && claimer == 'w' || + result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win + if (appData.debugMode) { + fprintf(debugFP, "result=%d sp=%d move=%d\n", + result, epStatus[forwardMostMove], forwardMostMove); + } + if(result != trueResult) { sprintf(buf, "False win claim: '%s'", resultDetails); result = claimer == 'w' ? BlackWins : WhiteWins; resultDetails = buf; @@ -7803,7 +7779,9 @@ GameEnds(result, resultDetails, whosays) } /* [HGM] bare: don't allow bare King to win */ if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) - && result != GameIsDrawn) + && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway + && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course... + && result != GameIsDrawn) { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn); for(j=BOARD_LEFT; j 0 || startedFromSetupPosition) { - char *fen = PositionToFEN(backwardMostMove, 1); + char *fen = PositionToFEN(backwardMostMove, NULL); fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen); fprintf(f, "\n{--------------\n"); PrintPosition(f, backwardMostMove); @@ -9605,12 +9583,6 @@ SaveGamePGN(f) #else seconds = (pvInfoList[i].time + 5)/10; // [HGM] PVtime: use engine time #endif - if (appData.debugMode,0) { - fprintf(debugFP, "times = %d %d %d %d, seconds=%d\n", - timeRemaining[0][i+1], timeRemaining[0][i], - timeRemaining[1][i+1], timeRemaining[1][i], seconds - ); - } if( seconds <= 0) buf[0] = 0; else if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else { @@ -9787,7 +9759,7 @@ SavePosition(f, dummy, dummy2) PrintPosition(f, currentMove); fprintf(f, "--------------]\n"); } else { - fen = PositionToFEN(currentMove, 1); + fen = PositionToFEN(currentMove, NULL); fprintf(f, "%s\n", fen); free(fen); } @@ -10423,7 +10395,7 @@ MachineWhiteEvent() if(bookHit) { // [HGM] book: simulate book reply static char bookMove[MSG_SIZ]; // a bit generous? - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -10498,7 +10470,7 @@ MachineBlackEvent() if(bookHit) { // [HGM] book: simulate book reply static char bookMove[MSG_SIZ]; // a bit generous? - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -10644,7 +10616,7 @@ TwoMachinesEvent P((void)) if(bookHit) { // [HGM] book: simulate book reply static char bookMove[MSG_SIZ]; // a bit generous? - programStats.depth = programStats.nodes = programStats.time = + programStats.nodes = programStats.depth = programStats.time = programStats.score = programStats.got_only_move = 0; sprintf(programStats.movelist, "%s (xbook)", bookHit); @@ -12377,6 +12349,15 @@ ParseOption(Option *opt, ChessProgramState *cps) opt->type = SaveButton; } else return FALSE; *p = 0; // terminate option name + // now look if the command-line options define a setting for this engine option. + if(cps->optionSettings && cps->optionSettings[0]) + p = strstr(cps->optionSettings, opt->name); else p = NULL; + if(p && (p == cps->optionSettings || p[-1] == ',')) { + sprintf(buf, "option %s", p); + if(p = strstr(buf, ",")) *p = 0; + strcat(buf, "\n"); + SendToProgram(buf, cps); + } return TRUE; } @@ -12584,6 +12565,8 @@ DisplayMove(moveNumber) char res[MSG_SIZ]; char cpThinkOutput[MSG_SIZ]; + if(appData.noGUI) return; // [HGM] fast: suppress display of moves + if (moveNumber == forwardMostMove - 1 || gameMode == AnalyzeMode || gameMode == AnalyzeFile) { @@ -12621,7 +12604,7 @@ DisplayMove(moveNumber) } else { res[0] = NULLCHAR; } - + if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) { DisplayMessage(res, cpThinkOutput); } else { @@ -12869,6 +12852,11 @@ DisplayBothClocks() you have neither ftime nor gettimeofday. */ +/* VS 2008 requires the #include outside of the function */ +#if !HAVE_GETTIMEOFDAY && HAVE_FTIME +#include +#endif + /* Get the current time as a TimeMark */ void GetTimeMark(tm) @@ -12886,7 +12874,7 @@ GetTimeMark(tm) #else /*!HAVE_GETTIMEOFDAY*/ #if HAVE_FTIME -#include +// include / moved to just above start of function struct timeb timeB; ftime(&timeB); @@ -13307,9 +13295,9 @@ PGNDate() char * -PositionToFEN(move, useFEN960) +PositionToFEN(move, overrideCastling) int move; - int useFEN960; + char *overrideCastling; { int i, j, fromX, fromY, toX, toY; int whiteToPlay; @@ -13384,6 +13372,9 @@ PositionToFEN(move, useFEN960) *p++ = whiteToPlay ? 'w' : 'b'; *p++ = ' '; + if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines + while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; + } else { if(nrCastlingRights) { q = p; if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) { @@ -13441,6 +13432,7 @@ PositionToFEN(move, useFEN960) } *p++ = ' '; } + } /* [HGM] find reversible plies */ { int i = 0, j=move;