Make deferral default in Shogi promotions
[xboard.git] / backend.c
index 6bd4418..5c7ebad 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -449,6 +449,7 @@ int adjudicateLossPlies = 6;
 char white_holding[64], black_holding[64];
 TimeMark lastNodeCountTime;
 long lastNodeCount=0;
+int shiftKey; // [HGM] set by mouse handler
 
 int have_sent_ICS_logon = 0;
 int sending_ICS_login    = 0;
@@ -1083,7 +1084,6 @@ ParseTimeControl(tc, ti, mps)
   long tc2;
   char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
   int min, sec=0;
-  int len;
 
   if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
   if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
@@ -4377,7 +4377,7 @@ ParseBoard12(string)
            strcat(parseList[moveNum - 1], " ");
            strcat(parseList[moveNum - 1], elapsed_time);
            /* currentMoveString is set as a side-effect of ParseOneMove */
-           if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '+';
+           if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
            safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
            strcat(moveList[moveNum - 1], "\n");
 
@@ -4804,7 +4804,7 @@ CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
                     AAA + ff, ONE + rf, AAA + ft, ONE + rt);
        } else {
            sprintf(move, "%c%c%c%c%c\n",
-                    AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
+                    AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar == '^' ? '+' : promoChar);
        }
     }
 }
@@ -4884,10 +4884,16 @@ ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
      int *fromX, *fromY, *toX, *toY;
      char *promoChar;
 {
+    char moveCopy[20], *p = moveCopy;
+    strncpy(moveCopy, move, 20); // make a copy of move to preprocess it
+    if(gameInfo.variant == VariantShogi) {
+        while(*p && *p != ' ') p++;
+        if(p[-1] == '+') p[-1] = '^'; // in Shogi '+' is promotion, distinguish from check
+    }
     if (appData.debugMode) {
-        fprintf(debugFP, "move to parse: %s\n", move);
+        fprintf(debugFP, "move to parse: %s\n", moveCopy);
     }
-    *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
+    *moveType = yylexstr(moveNum, moveCopy, yy_textstr, sizeof yy_textstr);
 
     switch (*moveType) {
       case WhitePromotion:
@@ -5694,14 +5700,14 @@ HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
            if(toY == 0 && piece == BlackPawn ||
               toY == 0 && piece == BlackQueen ||
               toY <= 1 && piece == BlackKnight) {
-               *promoChoice = '+';
+               *promoChoice = '^';
                return FALSE;
            }
        } else {
            if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
               toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
               toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
-               *promoChoice = '+';
+               *promoChoice = '^';
                return FALSE;
            }
        }
@@ -5721,6 +5727,11 @@ HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
        *promoChoice = PieceToChar(BlackFerz);  // no choice
        return FALSE;
     }
+    // no sense asking what we must promote to if it is going to explode...
+    if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
+       *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
+       return FALSE;
+    }
     if(autoQueen) { // predetermined
        if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantLosers)
             *promoChoice = PieceToChar(BlackKing); // in Suicide Q is the last thing we want
@@ -5733,7 +5744,7 @@ HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
              gameMode == IcsPlayingBlack &&  WhiteOnMove(currentMove);
     if(appData.testLegality && !premove) {
        moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
-                       fromY, fromX, toY, toX, NULLCHAR);
+                       fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '^' : NULLCHAR);
        if(moveType != WhitePromotion && moveType  != BlackPromotion)
            return FALSE;
     }
@@ -5875,6 +5886,8 @@ OnlyMove(int *x, int *y, Boolean captures) {
       case IcsPlayingBlack:
        if(WhiteOnMove(currentMove)) return FALSE;
        break;
+      case EditGame:
+        break;
       default:
        return FALSE;
     }
@@ -6161,7 +6174,8 @@ FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
      the previous line in Analysis Mode */
   if ((gameMode == AnalyzeMode || gameMode == EditGame)
                                && currentMove < forwardMostMove) {
-    PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
+    if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
+    else forwardMostMove = currentMove;
   }
 
   /* If we need the chess program but it's dead, restart it */
@@ -6311,6 +6325,20 @@ MarkTargetSquares(int clear)
   DrawPosition(TRUE, NULL);
 }
 
+int
+Explode(Board board, int fromX, int fromY, int toX, int toY)
+{
+    if(gameInfo.variant == VariantAtomic &&
+       (board[toY][toX] != EmptySquare ||                     // capture?
+        toX != fromX && (board[fromY][fromX] == WhitePawn ||  // e.p. ?
+                         board[fromY][fromX] == BlackPawn   )
+      )) {
+        AnimateAtomicCapture(board, fromX, fromY, toX, toY);
+        return TRUE;
+    }
+    return FALSE;
+}
+
 void LeftClick(ClickType clickType, int xPix, int yPix)
 {
     int x, y;
@@ -6521,9 +6549,13 @@ void LeftClick(ClickType clickType, int xPix, int yPix)
        }
        PromotionPopUp();
     } else {
+       int oldMove = currentMove;
        UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
        if (!appData.highlightLastMove || gotPremove) ClearHighlights();
        if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
+       if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
+          Explode(boards[currentMove-1], fromX, fromY, toX, toY))
+           DrawPosition(TRUE, boards[currentMove]);
        fromX = fromY = -1;
     }
     appData.animate = saveAnimate;
@@ -8618,7 +8650,7 @@ ApplyMove(fromX, fromY, toX, toY, promoChar, board)
        board[toY][toX] = EmptySquare;
       }
     }
-    if(promoChar == '+') {
+    if(promoChar == '^') {
         /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite orinary Pawn promotion) */
         board[toY][toX] = (ChessSquare) (PROMOTED piece);
     } else if(!appData.testLegality) { // without legality testing, unconditionally believe promoChar
@@ -13574,10 +13606,10 @@ SendTimeControl(cps, mps, tc, inc, sd, st)
        /* Note old gnuchess bug -- minutes:seconds used to not work.
           Fixed in later versions, but still avoid :seconds
           when seconds is 0. */
-       snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000);
+       snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
       } else {
        snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
-                seconds, inc/1000);
+                seconds, inc/1000.);
       }
     }
     SendToProgram(buf, cps);