source clean up. Fix compiler warning, removed unused variables, etc.
[xboard.git] / backend.c
index 5ae5525..910d1d8 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -70,6 +70,7 @@
 #include <sys/types.h>\r
 #include <sys/stat.h>\r
 #include <math.h>\r
+#include <ctype.h>\r
 \r
 #if STDC_HEADERS\r
 # include <stdlib.h>\r
@@ -210,6 +211,10 @@ void ParseFeatures P((char* args, ChessProgramState *cps));
 void InitBackEnd3 P((void));\r
 void FeatureDone P((ChessProgramState* cps, int val));\r
 void InitChessProgram P((ChessProgramState *cps, int setup));\r
+void OutputKibitz(int window, char *text);\r
+int PerpetualChase(int first, int last);\r
+int EngineOutputIsUp();\r
+void InitDrawingSizes(int x, int y);\r
 \r
 #ifdef WIN32\r
        extern void ConsoleCreate();\r
@@ -228,8 +233,7 @@ extern char installDir[MSG_SIZ];
 extern int tinyLayout, smallLayout;\r
 ChessProgramStats programStats;\r
 static int exiting = 0; /* [HGM] moved to top */\r
-static int setboardSpoiledMachineBlack = 0, errorExitFlag = 0;\r
-extern int startedFromPositionFile;\r
+static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;\r
 int startedFromPositionFile = FALSE; Board filePosition;       /* [HGM] loadPos */\r
 char endingGame = 0;    /* [HGM] crash: flag to prevent recursion of GameEnds() */\r
 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS     */\r
@@ -290,6 +294,8 @@ static char * safeStrCpy( char * dst, const char * src, size_t count )
     return dst;\r
 }\r
 \r
+#if 0\r
+//[HGM] for future use? Conditioned out for now to suppress warning.\r
 static char * safeStrCat( char * dst, const char * src, size_t count )\r
 {\r
     size_t  dst_len;\r
@@ -306,6 +312,7 @@ static char * safeStrCat( char * dst, const char * src, size_t count )
 \r
     return dst;\r
 }\r
+#endif\r
 \r
 /* Some compiler can't cast u64 to double\r
  * This function do the job for us:\r
@@ -1943,7 +1950,7 @@ VariantSwitch(Board board, VariantClass newVariant)
 {\r
    int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;\r
    int oldCurrentMove = currentMove, oldForwardMostMove = forwardMostMove, oldBackwardMostMove = backwardMostMove;\r
-   Board tempBoard; int saveCastling[BOARD_SIZE], saveEP;\r
+//   Board tempBoard; int saveCastling[BOARD_SIZE], saveEP;\r
 \r
    startedFromPositionFile = FALSE;\r
    if(gameInfo.variant == newVariant) return;\r
@@ -2359,10 +2366,10 @@ read_from_ics(isr, closure, data, count, error)
                   (StrStr(star_match[0], gameInfo.white) == star_match[0] || \r
                    StrStr(star_match[0], gameInfo.black) == star_match[0]   )) { // kibitz of self or opponent\r
                        suppressKibitz = TRUE;\r
-                       if((StrStr(star_match[0], gameInfo.white) == star_match[0])\r
-                               && (gameMode == IcsPlayingWhite) ||\r
-                          (StrStr(star_match[0], gameInfo.black) == star_match[0])\r
-                               && (gameMode == IcsPlayingBlack)   ) // opponent kibitz\r
+                       if((StrStr(star_match[0], gameInfo.white) == star_match[0]\r
+                               && (gameMode == IcsPlayingWhite)) ||\r
+                          (StrStr(star_match[0], gameInfo.black) == star_match[0]\r
+                               && (gameMode == IcsPlayingBlack))   ) // opponent kibitz\r
                            started = STARTED_CHATTER; // own kibitz we simply discard\r
                        else {\r
                            started = STARTED_COMMENT; // make sure it will be collected in parse[]\r
@@ -2852,7 +2859,7 @@ read_from_ics(isr, closure, data, count, error)
                if(bookHit) { // [HGM] book: simulate book reply\r
                    static char bookMove[MSG_SIZ]; // a bit generous?\r
 \r
-                   programStats.depth = programStats.nodes = programStats.time = \r
+                   programStats.nodes = programStats.depth = programStats.time = \r
                    programStats.score = programStats.got_only_move = 0;\r
                    sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -3882,7 +3889,7 @@ ParseBoard12(string)
     if(bookHit) { // [HGM] book: simulate book reply\r
        static char bookMove[MSG_SIZ]; // a bit generous?\r
 \r
-       programStats.depth = programStats.nodes = programStats.time = \r
+       programStats.nodes = programStats.depth = programStats.time = \r
        programStats.score = programStats.got_only_move = 0;\r
        sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -4102,7 +4109,7 @@ ProcessICSInitScript(f)
 void\r
 AlphaRank(char *move, int n)\r
 {\r
-    char *p = move, c; int x, y;\r
+//    char *p = move, c; int x, y;\r
 \r
     if (appData.debugMode) {\r
         fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);\r
@@ -4246,6 +4253,7 @@ ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
     }\r
 }\r
 \r
+#if 0\r
 /* [AS] FRC game initialization */\r
 static int FindEmptySquare( Board board, int n )\r
 {\r
@@ -4262,7 +4270,6 @@ static int FindEmptySquare( Board board, int n )
     return i;\r
 }\r
 \r
-#if 0\r
 static void ShuffleFRC( Board board )\r
 {\r
     int i;\r
@@ -4354,7 +4361,7 @@ static void SetupFRC( Board board, int pos_index )
 \r
 int squaresLeft[4];\r
 int piecesLeft[(int)BlackPawn];\r
-long long int seed, nrOfShuffles;\r
+u64 seed, nrOfShuffles;\r
 \r
 void GetPositionNumber()\r
 {      // sets global variable seed\r
@@ -4375,9 +4382,9 @@ int put(Board board, int pieceType, int rank, int n, int shade)
        int i;\r
 \r
        for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {\r
-               if( ((i-BOARD_LEFT)&1)+1 & shade && board[rank][i] == EmptySquare && n-- == 0) {\r
+               if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {\r
                        board[rank][i] = (ChessSquare) pieceType;\r
-                       squaresLeft[(i-BOARD_LEFT&1) + 1]--;\r
+                       squaresLeft[((i-BOARD_LEFT)&1) + 1]--;\r
                        squaresLeft[ANY]--;\r
                        piecesLeft[pieceType]--; \r
                        return i;\r
@@ -4444,7 +4451,7 @@ void SetUpShuffle(Board board, int number)
            // in variants with super-numerary Kings and Rooks, we leave these for the shuffle\r
        }\r
 \r
-       if((BOARD_RGHT-BOARD_LEFT & 1) == 0)\r
+       if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)\r
            // only for even boards make effort to put pairs of colorbound pieces on opposite colors\r
            for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {\r
                if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;\r
@@ -4645,10 +4652,10 @@ InitPosition(redraw)
       nrCastlingRights = 6;\r
         castlingRights[0][0] = initialRights[0] = BOARD_RGHT-1;\r
         castlingRights[0][1] = initialRights[1] = BOARD_LEFT;\r
-        castlingRights[0][2] = initialRights[2] = BOARD_WIDTH-1>>1;\r
+        castlingRights[0][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;\r
         castlingRights[0][3] = initialRights[3] = BOARD_RGHT-1;\r
         castlingRights[0][4] = initialRights[4] = BOARD_LEFT;\r
-        castlingRights[0][5] = initialRights[5] = BOARD_WIDTH-1>>1;\r
+        castlingRights[0][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;\r
       break;\r
     case VariantFalcon:\r
       pieces = FalconArray;\r
@@ -5442,7 +5449,7 @@ if(appData.debugMode) fprintf(debugFP, "moveType 2 = %d, promochar = %x\n", move
   if(bookHit) { // [HGM] book: simulate book reply\r
        static char bookMove[MSG_SIZ]; // a bit generous?\r
 \r
-       programStats.depth = programStats.nodes = programStats.time = \r
+       programStats.nodes = programStats.depth = programStats.time = \r
        programStats.score = programStats.got_only_move = 0;\r
        sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -5475,7 +5482,7 @@ if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", move
 \r
 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )\r
 {\r
-    char * hint = lastHint;\r
+//    char * hint = lastHint;\r
     FrontEndProgramStats stats;\r
 \r
     stats.which = cps == &first ? 0 : 1;\r
@@ -5669,8 +5676,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
             sprintf(buf1, _("Illegal move \"%s\" from %s machine"),\r
                    machineMove, cps->which);\r
            DisplayError(buf1, 0);\r
-            sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d%c",\r
-                    machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);\r
+            sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",\r
+                    machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);\r
            if (gameMode == TwoMachinesPlay) {\r
              GameEnds(machineWhite ? BlackWins : WhiteWins,\r
                        buf1, GE_XBOARD);\r
@@ -5715,6 +5722,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
                toX--;\r
                currentMoveString[2]--;\r
                break;\r
+            default: ; // nothing to do, but suppresses warning of pedantic compilers\r
            }\r
         }\r
        hintRequested = FALSE;\r
@@ -5737,8 +5745,8 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
                        programStats.depth,\r
                        programStats.score / 100.,\r
                        programStats.time / 100.,\r
-                       (double) programStats.nodes,\r
-                       programStats.nodes / (10*abs(programStats.time) + 1.),\r
+                       u64ToDouble(programStats.nodes),\r
+                       u64ToDouble(programStats.nodes) / (10*abs(programStats.time) + 1.),\r
                        programStats.movelist);\r
                SendToICS(buf);\r
          }\r
@@ -5827,7 +5835,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
            {   /* [HGM] Some more adjudications for obstinate engines */\r
                int NrWN=0, NrBN=0, NrWB=0, NrBB=0, NrWR=0, NrBR=0,\r
                     NrWQ=0, NrBQ=0, NrW=0, bishopsColor = 0,\r
-                    NrPieces=0, NrPawns=0, PawnAdvance=0, i, j, k;\r
+                    NrPieces=0, NrPawns=0, PawnAdvance=0, i, j;\r
                static int moveCount = 6;\r
 \r
                 /* First absolutely insufficient mating material. Count what is on board. */\r
@@ -6126,7 +6134,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
                strcat(bookMove, bookHit);\r
                message = bookMove;\r
                cps = cps->other;\r
-               programStats.depth = programStats.nodes = programStats.time = \r
+               programStats.nodes = programStats.depth = programStats.time = \r
                programStats.score = programStats.got_only_move = 0;\r
                sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -6660,8 +6668,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 */\r
                        int ticklen;\r
 \r
-                       if(cps->nps == 0) ticklen = 10*time;       // use engine reported time\r
-                       else ticklen = (1000. * nodes) / cps->nps; // convert node count to time\r
+                       if(cps->nps == 0) ticklen = 10*time;                    // use engine reported time\r
+                       else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time\r
                        if(WhiteOnMove(forwardMostMove)) \r
                             whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;\r
                        else blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;\r
@@ -6820,7 +6828,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
         else {\r
            buf1[0] = NULLCHAR;\r
 \r
-           if (sscanf(message, "%d%c %d %d %lu %[^\n]\n",\r
+           if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",\r
                       &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) \r
             {\r
                 ChessProgramStats cpstats;\r
@@ -7065,7 +7073,7 @@ ApplyMove(fromX, fromY, toX, toY, promoChar, board)
     /* [HGM] compute & store e.p. status and castling rights for new position */\r
     /* if we are updating a board for which those exist (i.e. in boards[])    */\r
     if((p = ((int)board - (int)boards[0])/((int)boards[1]-(int)boards[0])) < MAX_MOVES && p > 0)\r
-    { int i, j;\r
+    { int i;\r
 \r
       if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;\r
       oldEP = epStatus[p-1];\r
@@ -9516,7 +9524,7 @@ SaveGamePGN(f)
 {\r
     int i, offset, linelen, newblock;\r
     time_t tm;\r
-    char *movetext;\r
+//    char *movetext;\r
     char numtext[32];\r
     int movelen, numlen, blank;\r
     char move_buffer[100]; /* [AS] Buffer for move+PV info */\r
@@ -9625,12 +9633,6 @@ SaveGamePGN(f)
 #else\r
             seconds = (pvInfoList[i].time + 5)/10; // [HGM] PVtime: use engine time\r
 #endif\r
-    if (appData.debugMode,0) {\r
-        fprintf(debugFP, "times = %d %d %d %d, seconds=%d\n",\r
-                timeRemaining[0][i+1], timeRemaining[0][i],\r
-                     timeRemaining[1][i+1], timeRemaining[1][i], seconds\r
-        );\r
-    }\r
 \r
             if( seconds <= 0) buf[0] = 0; else\r
             if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else {\r
@@ -10443,7 +10445,7 @@ MachineWhiteEvent()
     if(bookHit) { // [HGM] book: simulate book reply\r
        static char bookMove[MSG_SIZ]; // a bit generous?\r
 \r
-       programStats.depth = programStats.nodes = programStats.time = \r
+       programStats.nodes = programStats.depth = programStats.time = \r
        programStats.score = programStats.got_only_move = 0;\r
        sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -10518,7 +10520,7 @@ MachineBlackEvent()
     if(bookHit) { // [HGM] book: simulate book reply\r
        static char bookMove[MSG_SIZ]; // a bit generous?\r
 \r
-       programStats.depth = programStats.nodes = programStats.time = \r
+       programStats.nodes = programStats.depth = programStats.time = \r
        programStats.score = programStats.got_only_move = 0;\r
        sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -10664,7 +10666,7 @@ TwoMachinesEvent P((void))
     if(bookHit) { // [HGM] book: simulate book reply\r
        static char bookMove[MSG_SIZ]; // a bit generous?\r
 \r
-       programStats.depth = programStats.nodes = programStats.time = \r
+       programStats.nodes = programStats.depth = programStats.time = \r
        programStats.score = programStats.got_only_move = 0;\r
        sprintf(programStats.movelist, "%s (xbook)", bookHit);\r
 \r
@@ -12921,7 +12923,7 @@ GetTimeMark(tm)
 #else /*!HAVE_GETTIMEOFDAY*/\r
 #if HAVE_FTIME\r
 \r
-#include <sys/timeb.h>\r
+// include <sys/timeb.h> / moved to just above start of function\r
     struct timeb timeB;\r
 \r
     ftime(&timeB);\r