Extend legality testing to drop moves
[xboard.git] / backend.c
index 8a2a8d8..12cef35 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -131,9 +131,16 @@ extern int gettimeofday(struct timeval *, struct timezone *);
 #ifdef ENABLE_NLS 
 # define _(s) gettext (s) 
 # define N_(s) gettext_noop (s) 
+# define T_(s) gettext(s)
 #else 
-# define _(s) (s) 
-# define N_(s) s 
+# ifdef WIN32
+#   define _(s) T_(s)
+#   define N_(s) s
+# else
+#   define _(s) (s) 
+#   define N_(s) s 
+#   define T_(s) s
+# endif
 #endif 
 
 
@@ -714,8 +721,8 @@ InitBackEnd1()
     /* [AS] Adjudication threshold */
     adjudicateLossThreshold = appData.adjudicateLossThreshold;
     
-    first.which = "first";
-    second.which = "second";
+    first.which = _("first");
+    second.which = _("second");
     first.maybeThinking = second.maybeThinking = FALSE;
     first.pr = second.pr = NoProc;
     first.isr = second.isr = NULL;
@@ -888,7 +895,7 @@ InitBackEnd1()
       case VariantGothic:     /* [HGM] should work */
       case VariantCapablanca: /* [HGM] should work */
       case VariantCourier:    /* [HGM] initial forced moves not implemented */
-      case VariantShogi:      /* [HGM] drops not tested for legality */
+      case VariantShogi:      /* [HGM] could still mate with pawn drop */
       case VariantKnightmate: /* [HGM] should work */
       case VariantCylinder:   /* [HGM] untested */
       case VariantFalcon:     /* [HGM] untested */
@@ -4568,6 +4575,10 @@ SendMoveToICS(moveType, fromX, fromY, toX, toY)
       /* POP Fabien */
        sprintf(user_move, "o-o-o\n");
        break;
+      case WhiteNonPromotion:
+      case BlackNonPromotion:
+        sprintf(user_move, "%c%c%c%c=\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
+        break;
       case WhitePromotionQueen:
       case BlackPromotionQueen:
       case WhitePromotionRook:
@@ -4796,6 +4807,8 @@ ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
       case BlackPromotionKnight:
       case WhitePromotionKing:
       case BlackPromotionKing:
+      case WhiteNonPromotion:
+      case BlackNonPromotion:
       case NormalMove:
       case WhiteCapturesEnPassant:
       case BlackCapturesEnPassant:
@@ -5828,11 +5841,10 @@ int lastLoadGameUseList = FALSE;
 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
 ChessMove lastLoadGameStart = (ChessMove) 0;
 
-ChessMove
-UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
+void
+UserMoveEvent(fromX, fromY, toX, toY, promoChar)
      int fromX, fromY, toX, toY;
      int promoChar;
-     Boolean captureOwn;
 {
     ChessMove moveType;
     ChessSquare pdown, pup;
@@ -5857,13 +5869,13 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
       case IcsIdle:
        /* We switched into a game mode where moves are not accepted,
            perhaps while the mouse button was down. */
-        return ImpossibleMove;
+        return;
 
       case MachinePlaysWhite:
        /* User is moving for Black */
        if (WhiteOnMove(currentMove)) {
            DisplayMoveError(_("It is White's turn"));
-            return ImpossibleMove;
+            return;
        }
        break;
 
@@ -5871,7 +5883,7 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
        /* User is moving for White */
        if (!WhiteOnMove(currentMove)) {
            DisplayMoveError(_("It is Black's turn"));
-            return ImpossibleMove;
+            return;
        }
        break;
 
@@ -5885,13 +5897,13 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
            /* User is moving for Black */
            if (WhiteOnMove(currentMove)) {
                DisplayMoveError(_("It is White's turn"));
-                return ImpossibleMove;
+                return;
            }
        } else {
            /* User is moving for White */
            if (!WhiteOnMove(currentMove)) {
                DisplayMoveError(_("It is Black's turn"));
-                return ImpossibleMove;
+                return;
            }
        }
        break;
@@ -5913,7 +5925,7 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
                            "fromY %d, toX %d, toY %d\n",
                            fromX, fromY, toX, toY);
            }
-            return ImpossibleMove;
+            return;
        }
        break;
 
@@ -5934,7 +5946,7 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
                            "fromY %d, toX %d, toY %d\n",
                            fromX, fromY, toX, toY);
            }
-            return ImpossibleMove;
+            return;
        }
        break;
 
@@ -5946,7 +5958,8 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
           click-click move is possible */
        if (toX == -2 || toY == -2) {
            boards[0][fromY][fromX] = EmptySquare;
-           return AmbiguousMove;
+           DrawPosition(FALSE, boards[currentMove]);
+           return;
        } else if (toX >= 0 && toY >= 0) {
            boards[0][toY][toX] = boards[0][fromY][fromX];
            if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
@@ -5962,28 +5975,27 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
                }
            } else
            boards[0][fromY][fromX] = EmptySquare;
-           return AmbiguousMove;
+           DrawPosition(FALSE, boards[currentMove]);
+           return;
        }
-        return ImpossibleMove;
+        return;
     }
 
-    if(toX < 0 || toY < 0) return ImpossibleMove;
+    if(toX < 0 || toY < 0) return;
     pdown = boards[currentMove][fromY][fromX];
     pup = boards[currentMove][toY][toX];
 
-    /* [HGM] If move started in holdings, it means a drop */
+    /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
     if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) { 
-         if( pup != EmptySquare ) return ImpossibleMove;
-         if(appData.testLegality) {
-             /* it would be more logical if LegalityTest() also figured out
-              * which drops are legal. For now we forbid pawns on back rank.
-              * Shogi is on its own here...
-              */
-             if( (pdown == WhitePawn || pdown == BlackPawn) &&
-                 (toY == 0 || toY == BOARD_HEIGHT -1 ) )
-                 return(ImpossibleMove); /* no pawn drops on 1st/8th */
-         }
-         return WhiteDrop; /* Not needed to specify white or black yet */
+         if( pup != EmptySquare ) return;
+         moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
+          if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n", 
+               moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
+          // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
+          if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
+          fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
+          while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++; 
+         fromY = DROP_RANK;
     }
 
     /* [HGM] always test for legality, to get promotion info */
@@ -5993,18 +6005,11 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
     if (appData.testLegality) {
        if (moveType == IllegalMove || moveType == ImpossibleMove) {
            DisplayMoveError(_("Illegal move"));
-            return ImpossibleMove;
+            return;
        }
     }
 
-    return moveType;
-    /* [HGM] <popupFix> in stead of calling FinishMove directly, this
-       function is made into one that returns an OK move type if FinishMove
-       should be called. This to give the calling driver routine the
-       opportunity to finish the userMove input with a promotion popup,
-       without bothering the user with this for invalid or illegal moves */
-
-/*    FinishMove(moveType, fromX, fromY, toX, toY, promoChar); */
+    FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
 }
 
 /* Common tail of UserMoveEvent and DropMenuEvent */
@@ -6031,18 +6036,6 @@ FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
     if(moveType == NormalMove && promoChar)
         moveType = PromoCharToMoveType(WhiteOnMove(currentMove), promoChar);
 
-    /* [HGM] convert drag-and-drop piece drops to standard form */
-    if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ){
-         moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
-          if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n", 
-               moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
-          // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
-          if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
-          fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
-          while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++; 
-         fromY = DROP_RANK;
-    }
-
     /* [HGM] <popupFix> The following if has been moved here from
        UserMoveEvent(). Because it seemed to belong here (why not allow
        piece drops in training games?), and because it can only be
@@ -6200,28 +6193,6 @@ FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
 }
 
 void
-UserMoveEvent(fromX, fromY, toX, toY, promoChar)
-     int fromX, fromY, toX, toY;
-     int promoChar;
-{
-    /* [HGM] This routine was added to allow calling of its two logical
-       parts from other modules in the old way. Before, UserMoveEvent()
-       automatically called FinishMove() if the move was OK, and returned
-       otherwise. I separated the two, in order to make it possible to
-       slip a promotion popup in between. But that it always needs two
-       calls, to the first part, (now called UserMoveTest() ), and to
-       FinishMove if the first part succeeded. Calls that do not need
-       to do anything in between, can call this routine the old way. 
-    */
-    ChessMove moveType = UserMoveTest(fromX, fromY, toX, toY, promoChar, FALSE);
-if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", moveType, promoChar);
-    if(moveType == AmbiguousMove)
-       DrawPosition(FALSE, boards[currentMove]);
-    else if(moveType != ImpossibleMove && moveType != Comment)
-        FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
-}
-
-void
 Mark(board, flags, kind, rf, ff, rt, ft, closure)
      Board board;
      int flags;
@@ -6716,7 +6687,7 @@ Adjudicate(ChessProgramState *cps)
                           SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
                          GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins, 
-                                                       _("Xboard adjudication: King destroyed"), GE_XBOARD );
+                                                       "Xboard adjudication: King destroyed", GE_XBOARD );
                          return 1;
                     }
                }
@@ -6730,7 +6701,7 @@ Adjudicate(ChessProgramState *cps)
                           SendMoveToProgram(forwardMostMove-1, engineOpponent); // 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 );
+                                                       "Xboard adjudication: Bare king", GE_XBOARD );
                          return 1;
                     }
                  } else
@@ -6743,7 +6714,7 @@ Adjudicate(ChessProgramState *cps)
                              SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
                            ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
                            GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn, 
-                                                       _("Xboard adjudication: Bare king"), GE_XBOARD );
+                                                       "Xboard adjudication: Bare king", GE_XBOARD );
                            return 1;
                        }
                  }
@@ -6759,7 +6730,7 @@ Adjudicate(ChessProgramState *cps)
                        if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
                            checkCnt++;
                        if(checkCnt >= 2) {
-                           reason = _("Xboard adjudication: 3rd check");
+                           reason = "Xboard adjudication: 3rd check";
                            boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
                            break;
                        }
@@ -6770,7 +6741,7 @@ Adjudicate(ChessProgramState *cps)
                break;
              case MT_STALEMATE:
              case MT_STAINMATE:
-               reason = _("Xboard adjudication: Stalemate");
+               reason = "Xboard adjudication: Stalemate";
                if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
                    boards[forwardMostMove][EP_STATUS] = EP_STALEMATE;   // default result for stalemate is draw
                    if(gameInfo.variant == VariantLosers  || gameInfo.variant == VariantGiveaway) // [HGM] losers:
@@ -6784,7 +6755,7 @@ Adjudicate(ChessProgramState *cps)
                }
                break;
              case MT_CHECKMATE:
-               reason = _("Xboard adjudication: Checkmate");
+               reason = "Xboard adjudication: Checkmate";
                boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
                break;
            }
@@ -6822,7 +6793,7 @@ Adjudicate(ChessProgramState *cps)
                           SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
                         }
                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
-                         GameEnds( GameIsDrawn, _("Xboard adjudication: Insufficient mating material"), GE_XBOARD );
+                         GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
                          return 1;
                      }
                 }
@@ -6843,7 +6814,7 @@ Adjudicate(ChessProgramState *cps)
                            SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
                          }
                           ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
-                          GameEnds( GameIsDrawn, _("Xboard adjudication: Trivial draw"), GE_XBOARD );
+                          GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
                           return 1;
                      }
                 } else moveCount = 6;
@@ -6891,7 +6862,7 @@ Adjudicate(ChessProgramState *cps)
                             && appData.drawRepeats > 1) {
                              /* adjudicate after user-specified nr of repeats */
                             int result = GameIsDrawn;
-                            char *details = _("XBoard adjudication: repetition draw");
+                            char *details = "XBoard adjudication: repetition draw";
                             if(gameInfo.variant == VariantXiangqi && appData.testLegality) { 
                                // [HGM] xiangqi: check for forbidden perpetuals
                                int m, ourPerpetual = 1, hisPerpetual = 1;
@@ -6905,7 +6876,7 @@ Adjudicate(ChessProgramState *cps)
                                                                        ourPerpetual, hisPerpetual);
                                if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
                                    result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
-                                   details = _("Xboard adjudication: perpetual checking");
+                                   details = "Xboard adjudication: perpetual checking";
                                } else
                                if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
                                    break; // (or we would have caught him before). Abort repetition-checking loop.
@@ -6916,7 +6887,7 @@ Adjudicate(ChessProgramState *cps)
                                    ourPerpetual = PerpetualChase(k+1, forwardMostMove);
                                    if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
                                        result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
-                                       details = _("Xboard adjudication: perpetual chasing");
+                                       details = "Xboard adjudication: perpetual chasing";
                                    } else
                                    if(hisPerpetual && !ourPerpetual)   // he is chasing us, but did not repeat yet
                                        break; // Abort repetition-checking loop.
@@ -6965,7 +6936,7 @@ Adjudicate(ChessProgramState *cps)
                           SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
                         }
                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
-                         GameEnds( GameIsDrawn, _("Xboard adjudication: 50-move rule"), GE_XBOARD );
+                         GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
                          return 1;
                 }
 
@@ -6999,7 +6970,7 @@ Adjudicate(ChessProgramState *cps)
                      SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
                    }
                    ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
-                   GameEnds( GameIsDrawn, _("Xboard adjudication: long game"), GE_XBOARD );
+                   GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
                    return 1;
                }
        return 0;
@@ -7193,9 +7164,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
         /* to make sure an illegal e.p. capture does not slip through,   */
         /* to cause a forfeit on a justified illegal-move complaint      */
         /* of the opponent.                                              */
-        if( gameMode==TwoMachinesPlay && appData.testLegality
-            && fromY != DROP_RANK /* [HGM] temporary; should still add legality test for drops */
-                                                              ) {
+        if( gameMode==TwoMachinesPlay && appData.testLegality ) {
            ChessMove moveType;
            moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
                              fromY, fromX, toY, toX, promoChar);
@@ -7270,7 +7239,7 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
                ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
 
                 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, 
-                    _("Xboard adjudication"), 
+                    "Xboard adjudication", 
                     GE_XBOARD );
 
                 return;
@@ -8163,6 +8132,8 @@ ParseGameHistory(game)
          case BlackPromotionKnight:
          case WhitePromotionKing:
          case BlackPromotionKing:
+         case WhiteNonPromotion:
+         case BlackNonPromotion:
          case NormalMove:
          case WhiteCapturesEnPassant:
          case BlackCapturesEnPassant:
@@ -8309,7 +8280,6 @@ ApplyMove(fromX, fromY, toX, toY, promoChar, board)
 
     /* [HGM] compute & store e.p. status and castling rights for new position */
     /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
-    { int i;
 
       if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
       oldEP = (signed char)board[EP_STATUS];
@@ -8318,6 +8288,16 @@ ApplyMove(fromX, fromY, toX, toY, promoChar, board)
       if( board[toY][toX] != EmptySquare ) 
            board[EP_STATUS] = EP_CAPTURE;  
 
+  /* [HGM] In Shatranj and Courier all promotions are to Ferz */
+  if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
+       && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
+         
+  if (fromY == DROP_RANK) {
+       /* must be first */
+        piece = board[toY][toX] = (ChessSquare) fromX;
+  } else {
+      int i;
+
       if( board[fromY][fromX] == WhitePawn ) {
            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
               board[EP_STATUS] = EP_PAWN_MOVE;
@@ -8349,18 +8329,8 @@ ApplyMove(fromX, fromY, toX, toY, promoChar, board)
              ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
        }
 
-    }
-
-  /* [HGM] In Shatranj and Courier all promotions are to Ferz */
-  if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
-       && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
-         
-  if (fromX == toX && fromY == toY) return;
+     if (fromX == toX && fromY == toY) return;
 
-  if (fromY == DROP_RANK) {
-       /* must be first */
-        piece = board[toY][toX] = (ChessSquare) fromX;
-  } else {
      piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
      king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
      if(gameInfo.variant == VariantKnightmate)
@@ -9639,6 +9609,8 @@ LoadGameOneMove(readAhead)
       case BlackPromotionKnight:
       case WhitePromotionKing:
       case BlackPromotionKing:
+      case WhiteNonPromotion:
+      case BlackNonPromotion:
       case NormalMove:
       case WhiteKingSideCastle:
       case WhiteQueenSideCastle:
@@ -14007,7 +13979,7 @@ DisplayMove(moveNumber)
            sprintf(res, " %s", PGNResult(gameInfo.result));
        } else {
            sprintf(res, " {%s} %s",
-                   gameInfo.resultDetails, PGNResult(gameInfo.result));
+                   T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
        }
     } else {
        res[0] = NULLCHAR;