Fix XB crash on giving keyboard focus to non-text widget
[xboard.git] / moves.c
diff --git a/moves.c b/moves.c
index 2a017c3..75b9434 100644 (file)
--- a/moves.c
+++ b/moves.c
@@ -5,7 +5,7 @@
  * Massachusetts.
  *
  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
- * 2007, 2008, 2009 Free Software Foundation, Inc.
+ * 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  *
  * Enhancements Copyright 2005 Alessandro Scotti
  *
@@ -260,6 +260,7 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
 {
     int rf, ff;
     int i, j, d, s, fs, rs, rt, ft, m;
+    int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
 
     for (rf = 0; rf < BOARD_HEIGHT; rf++) 
       for (ff = BOARD_LEFT; ff < BOARD_RGHT; ff++) {
@@ -303,7 +304,7 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
               }
               if (rf < BOARD_HEIGHT-1 && board[rf + 1][ff] == EmptySquare) {
                  callback(board, flags,
-                          rf == BOARD_HEIGHT-2 ? WhitePromotionQueen : NormalMove,
+                          rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotionQueen : NormalMove,
                           rf, ff, rf + 1, ff, closure);
              }
              if (rf == 1 && board[2][ff] == EmptySquare &&
@@ -318,7 +319,7 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
                      ((flags & F_KRIEGSPIEL_CAPTURE) ||
                       BlackPiece(board[rf + 1][ff + s]))) {
                      callback(board, flags, 
-                              rf == BOARD_HEIGHT-2 ? WhitePromotionQueen : NormalMove,
+                              rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotionQueen : NormalMove,
                               rf, ff, rf + 1, ff + s, closure);
                  }
                  if (rf == BOARD_HEIGHT-4) {
@@ -353,7 +354,7 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
               }
              if (rf > 0 && board[rf - 1][ff] == EmptySquare) {
                  callback(board, flags, 
-                          rf == 1 ? BlackPromotionQueen : NormalMove,
+                          rf <= promoRank ? BlackPromotionQueen : NormalMove,
                           rf, ff, rf - 1, ff, closure);
              }
              if (rf == BOARD_HEIGHT-2 && board[BOARD_HEIGHT-3][ff] == EmptySquare &&
@@ -368,7 +369,7 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
                      ((flags & F_KRIEGSPIEL_CAPTURE) ||
                       WhitePiece(board[rf - 1][ff + s]))) {
                      callback(board, flags, 
-                              rf == 1 ? BlackPromotionQueen : NormalMove,
+                              rf <= promoRank ? BlackPromotionQueen : NormalMove,
                               rf, ff, rf - 1, ff + s, closure);
                  }
                  if (rf == 3) {
@@ -608,6 +609,8 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
 
             /* Shogi Pawn and Silver General: first the Pawn move,    */
             /* then the General continues like a Ferz                 */
+            case WhiteMan:
+                if(gameInfo.variant != VariantMakruk) goto commoner;
             case SHOGI WhitePawn:
             case SHOGI WhiteFerz:
                   if (rf < BOARD_HEIGHT-1 &&
@@ -617,6 +620,8 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
               if(piece != SHOGI WhitePawn) goto finishSilver;
               break;
 
+            case BlackMan:
+                if(gameInfo.variant != VariantMakruk) goto commoner;
             case SHOGI BlackPawn:
             case SHOGI BlackFerz:
                   if (rf > 0 &&
@@ -644,8 +649,7 @@ void GenPseudoLegal(board, flags, epfile, callback, closure)
            case WhiteSilver:
            case BlackSilver:
                m++; // [HGM] superchess: use for Centaur
-            case WhiteMan:
-            case BlackMan:
+            commoner:
             case SHOGI WhiteKing:
             case SHOGI BlackKing:
            case WhiteKing:
@@ -854,7 +858,7 @@ int GenLegal(board, flags, epfile, castlingRights, callback, closure)
        }
     }
 
-  if(gameInfo.variant == VariantFischeRandom) {
+  if(flags & F_FRC_TYPE_CASTLING) {
 
     /* generate all potential FRC castling moves (KxR), ignoring flags */
     /* [HGM] test if the Rooks we find have castling rights */
@@ -863,10 +867,6 @@ int GenLegal(board, flags, epfile, castlingRights, callback, closure)
     if ((flags & F_WHITE_ON_MOVE) != 0) {
         ff = castlingRights[2]; /* King file if we have any rights */
         if(ff > 0 && board[0][ff] == WhiteKing) {
-    if (appData.debugMode) {
-        fprintf(debugFP, "FRC castling, %d %d %d %d %d %d\n",
-                castlingRights[0],castlingRights[1],ff,castlingRights[3],castlingRights[4],castlingRights[5]);
-    }
             ft = castlingRights[0]; /* Rook file if we have H-side rights */
             left  = ff+1;
             right = BOARD_RGHT-2;
@@ -1034,9 +1034,6 @@ void LegalityTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
 {
     register LegalityTestClosure *cl = (LegalityTestClosure *) closure;
 
-//    if (appData.debugMode) {
-//        fprintf(debugFP, "Legality test: %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
-//    }
     if(board[rt][ft] != EmptySquare || kind==WhiteCapturesEnPassant || kind==BlackCapturesEnPassant)
        cl->captures++; // [HGM] losers: count legal captures
     if (rf == cl->rf && ff == cl->ff && rt == cl->rt && ft == cl->ft)
@@ -1051,11 +1048,6 @@ ChessMove LegalityTest(board, flags, epfile, castlingRights, rf, ff, rt, ft, pro
 {
     LegalityTestClosure cl; ChessSquare piece = board[rf][ff];
     
-    if (appData.debugMode) {
-        int i;
-        for(i=0; i<6; i++) fprintf(debugFP, "%d ", castlingRights[i]);
-        fprintf(debugFP, "Legality test? %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
-    }
     /* [HGM] Lance, Cobra and Falcon are wildcard pieces; consider all their moves legal */
     /* (perhaps we should disallow moves that obviously leave us in check?)              */
     if(piece == WhiteFalcon || piece == BlackFalcon ||
@@ -1160,7 +1152,7 @@ int MateTest(board, flags, epfile, castlingRights)
            else hisPieces++;
        }
     }
-    if(appData.debugMode) fprintf(debugFP, "MateTest: K=%d, my=%d, his=%d\n", nrKing, myPieces, hisPieces);
+
     switch(gameInfo.variant) { // [HGM] losers: extinction wins
        case VariantShatranj:
                if(hisPieces == 1) return myPieces > 1 ? MT_BARE : MT_DRAW;
@@ -1240,11 +1232,7 @@ void Disambiguate(board, flags, epfile, closure)
     closure->count = 0;
     closure->rf = closure->ff = closure->rt = closure->ft = 0;
     closure->kind = ImpossibleMove;
-    if (appData.debugMode) {
-        fprintf(debugFP, "Disambiguate in:  %d(%d,%d)-(%d,%d) = %d (%c)\n",
-                             closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
-                             closure->promoCharIn, closure->promoCharIn >= ' ' ? closure->promoCharIn : '-');
-    }
+
     GenLegal(board, flags, epfile, initialRights, DisambiguateCallback, (VOIDSTAR) closure);
     if (closure->count == 0) {
        /* See if it's an illegal move due to check */
@@ -1253,13 +1241,6 @@ void Disambiguate(board, flags, epfile, closure)
                 (VOIDSTAR) closure);   
        if (closure->count == 0) {
            /* No, it's not even that */
-    if (appData.debugMode) { int i, j;
-       for(i=BOARD_HEIGHT-1; i>=0; i--) {
-               for(j=0; j<BOARD_WIDTH; j++)
-                       fprintf(debugFP, "%3d", (int) board[i][j]);
-               fprintf(debugFP, "\n");
-       }
-    }
            return;
        }
     }
@@ -1323,11 +1304,6 @@ void Disambiguate(board, flags, epfile, closure)
     if(closure->kind == IllegalMove)
     /* [HGM] might be a variant we don't understand, pass on promotion info */
         closure->promoChar = ToLower(closure->promoCharIn);
-    if (appData.debugMode) {
-        fprintf(debugFP, "Disambiguate out: %d(%d,%d)-(%d,%d) = %d (%c)\n",
-        closure->piece,closure->ff,closure->rf,closure->ft,closure->rt,closure->promoChar,
-       closure->promoChar >= ' ' ? closure->promoChar:'-');
-    }
 }
 
 
@@ -1409,8 +1385,6 @@ ChessMove CoordsToAlgebraic(board, flags, epfile,
     piece = board[rf][ff];
     if(PieceToChar(piece)=='~') piece = (ChessSquare)(DEMOTED piece);
 
-  if (appData.debugMode)
-          fprintf(debugFP, "CoordsToAlgebraic, piece=%d (%d,%d)-(%d,%d) %c\n", (int)piece,ff,rf,ft,rt,promoChar >= ' ' ? promoChar : '-');
     switch (piece) {
       case WhitePawn:
       case BlackPawn:
@@ -1441,8 +1415,6 @@ ChessMove CoordsToAlgebraic(board, flags, epfile,
        }
        /* Use promotion suffix style "=Q" */
        *outp = NULLCHAR;
-  if (appData.debugMode)
-          fprintf(debugFP, "movetype=%d, promochar=%d=%c\n", (int)kind, promoChar, promoChar >= ' ' ? promoChar : '-');
         if (promoChar != NULLCHAR) {
             if(gameInfo.variant == VariantShogi) {
                 /* [HGM] ... but not in Shogi! */