Castle with nearest rather than corner piece
[xboard.git] / backend.c
index f969aba..670eb4b 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -5141,10 +5141,17 @@ SendMoveToProgram (int moveNum, ChessProgramState *cps)
        else SendToProgram(moveList[moveNum], cps);
       } else
       if(moveList[moveNum][4] == ';') { // [HGM] lion: move is double-step over intermediate square
-         snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", moveList[moveNum][0], moveList[moveNum][1] - '0', // convert to two moves
-                                              moveList[moveNum][5], moveList[moveNum][6] - '0',
-                                              moveList[moveNum][5], moveList[moveNum][6] - '0',
-                                              moveList[moveNum][2], moveList[moveNum][3] - '0');
+       char *m = moveList[moveNum];
+       if((boards[moveNum][m[6]-ONE][m[5]-AAA] < BlackPawn) == (boards[moveNum][m[1]-ONE][m[0]-AAA] < BlackPawn)) // move is kludge to indicate castling
+         snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", m[0], m[1] - '0', // convert to two moves
+                                              m[2], m[3] - '0',
+                                              m[5], m[6] - '0',
+                                              m[2] + (m[0] > m[5] ? 1 : -1), m[3] - '0');
+       else
+         snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", m[0], m[1] - '0', // convert to two moves
+                                              m[5], m[6] - '0',
+                                              m[5], m[6] - '0',
+                                              m[2], m[3] - '0');
          SendToProgram(buf, cps);
       } else
       if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
@@ -5520,6 +5527,7 @@ ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fro
       case BlackASideCastleFR:
       /* End of code added by Tord */
       case IllegalMove:                /* bug or odd chess variant */
+       if(currentMoveString[1] == '@') goto drop; // illegal drop
         *fromX = currentMoveString[0] - AAA;
         *fromY = currentMoveString[1] - ONE;
         *toX = currentMoveString[2] - AAA;
@@ -5543,6 +5551,7 @@ ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fro
 
       case WhiteDrop:
       case BlackDrop:
+      drop:
        *fromX = *moveType == WhiteDrop ?
          (int) CharToPiece(ToUpper(currentMoveString[0])) :
          (int) CharToPiece(ToLower(currentMoveString[0]));
@@ -7495,7 +7504,7 @@ LeftClick (ClickType clickType, int xPix, int yPix)
            return;
        }
     }
-
+printf("to click %d,%d\n",x,y);
     /* fromX != -1 */
     if (clickType == Press && gameMode != EditPosition) {
        ChessSquare fromP;
@@ -7558,6 +7567,12 @@ LeftClick (ClickType clickType, int xPix, int yPix)
        // ignore clicks on holdings
        if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
     }
+printf("A type=%d\n",clickType);
+
+    if(x == fromX && y == fromY && gameMode == EditPosition && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
+       gatingPiece = boards[currentMove][fromY][fromX]; // prepare to copy rather than move
+       return;
+    }
 
     if (clickType == Release && x == fromX && y == fromY && killX < 0) {
        DragPieceEnd(xPix, yPix); dragging = 0;
@@ -7591,7 +7606,7 @@ LeftClick (ClickType clickType, int xPix, int yPix)
     }
 
     clearFlag = 0;
-
+printf("B\n");
     if(gameMode != EditPosition && !appData.testLegality && !legal[y][x] &&
        fromX >= BOARD_LEFT && fromX < BOARD_RGHT && (x != killX || y != killY) && !sweepSelecting) {
        if(dragging) DragPieceEnd(xPix, yPix), dragging = 0;
@@ -7599,7 +7614,7 @@ LeftClick (ClickType clickType, int xPix, int yPix)
        DrawPosition(TRUE, NULL);
        return; // ignore to-click
     }
-
+printf("(%d,%d)-(%d,%d) %d %d\n",fromX,fromY,toX,toY,x,y);
     /* we now have a different from- and (possibly off-board) to-square */
     /* Completed move */
     if(!sweepSelecting) {
@@ -8652,12 +8667,13 @@ FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book h
            safeStrCpy(firstLeg, machineMove, 20); // just remember it for processing when second leg arrives
            return;
        } else if(firstLeg[0]) { // there was a previous leg;
-           // only support case where same piece makes two step (and don't even test that!)
+           // only support case where same piece makes two step
            char buf[20], *p = machineMove+1, *q = buf+1, f;
            safeStrCpy(buf, machineMove, 20);
            while(isdigit(*q)) q++; // find start of to-square
            safeStrCpy(machineMove, firstLeg, 20);
-           while(isdigit(*p)) p++;
+           while(isdigit(*p)) p++; // to-square of first leg (which is now copied to machineMove)
+           if(*p == *buf)          // if first-leg to not equal to second-leg from first leg says unmodified (assume it ia King move of castling)
            safeStrCpy(p, q, 20); // glue to-square of second leg to from-square of first, to process over-all move
            sscanf(buf, "%c%d", &f, &killY); killX = f - AAA; killY -= ONE - '0'; // pass intermediate square to MakeMove in global
            firstLeg[0] = NULLCHAR;
@@ -9995,8 +10011,8 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
      if(gameInfo.variant == VariantKnightmate)
          king += (int) WhiteUnicorn - (int) WhiteKing;
 
-    if(piece != WhiteKing && piece != BlackKing && pieceDesc[piece] && killX >= 0 && strchr(pieceDesc[piece], 'O') // Betza castling-enabled
-       && (piece < BlackPawn ? killed < BlackPawn : killed >= BlackPawn)) {    // and captures own
+    if(pieceDesc[piece] && killX >= 0 && strchr(pieceDesc[piece], 'O') // Betza castling-enabled
+       && (piece < BlackPawn ? killed < BlackPawn : killed >= BlackPawn)) {    // and tramples own
        board[toY][toX] = piece; board[fromY][fromX] = EmptySquare;
        board[toY][toX + (killX < fromX ? 1 : -1)] = killed;
         board[EP_STATUS] = EP_NONE; // capture was fake!
@@ -10028,19 +10044,19 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
     } else if (board[fromY][fromX] == king
         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
         && toY == fromY && toX > fromX+1) {
+       for(rookX=fromX+1; board[toY][rookX] == EmptySquare && rookX < BOARD_RGHT-1; rookX++); // castle with nearest piece
+        board[fromY][toX-1] = board[fromY][rookX];
+        board[fromY][rookX] = EmptySquare;
        board[fromY][fromX] = EmptySquare;
         board[toY][toX] = king;
-       for(rookX=BOARD_RGHT-1; board[toY][rookX] == DarkSquare && rookX > toX + 1; rookX--);
-        board[toY][toX-1] = board[fromY][rookX];
-        board[fromY][rookX] = EmptySquare;
     } else if (board[fromY][fromX] == king
         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
                && toY == fromY && toX < fromX-1) {
+       for(rookX=fromX-1; board[toY][rookX] == EmptySquare && rookX > 0; rookX--); // castle with nearest piece
+        board[fromY][toX+1] = board[fromY][rookX];
+        board[fromY][rookX] = EmptySquare;
        board[fromY][fromX] = EmptySquare;
         board[toY][toX] = king;
-       for(rookX=BOARD_LEFT; board[toY][rookX] == DarkSquare && rookX < toX - 1; rookX++);
-        board[toY][toX+1] = board[fromY][rookX];
-        board[fromY][rookX] = EmptySquare;
     } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
                 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu)
                && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
@@ -10079,19 +10095,19 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
     } else if (board[fromY][fromX] == king
         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
                && toY == fromY && toX > fromX+1) {
+       for(rookX=toX+1; board[toY][rookX] == EmptySquare && rookX < BOARD_RGHT - 1; rookX++);
+        board[fromY][toX-1] = board[fromY][rookX];
+        board[fromY][rookX] = EmptySquare;
        board[fromY][fromX] = EmptySquare;
         board[toY][toX] = king;
-       for(rookX=BOARD_RGHT-1; board[toY][rookX] == DarkSquare && rookX > toX + 1; rookX--);
-        board[toY][toX-1] = board[fromY][rookX];
-        board[fromY][rookX] = EmptySquare;
     } else if (board[fromY][fromX] == king
         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
                && toY == fromY && toX < fromX-1) {
+       for(rookX=toX-1; board[toY][rookX] == EmptySquare && rookX > 0; rookX--);
+        board[fromY][toX+1] = board[fromY][rookX];
+        board[fromY][rookX] = EmptySquare;
        board[fromY][fromX] = EmptySquare;
         board[toY][toX] = king;
-       for(rookX=BOARD_LEFT; board[toY][rookX] == DarkSquare && rookX < toX - 1; rookX++);
-        board[toY][toX+1] = board[fromY][rookX];
-        board[fromY][rookX] = EmptySquare;
     } else if (fromY == 7 && fromX == 3
               && board[fromY][fromX] == BlackKing
               && toY == 7 && toX == 5) {