update year in copyright info
[xboard.git] / backend.c
index c364fe2..3fa521b 100644 (file)
--- a/backend.c
+++ b/backend.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
  *
@@ -242,6 +242,7 @@ int endPV = -1;
 static int exiting = 0; /* [HGM] moved to top */
 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
 int startedFromPositionFile = FALSE; Board filePosition;       /* [HGM] loadPos */
+int rightsBoard[BOARD_RANKS][BOARD_FILES];                  /* [HGM] editrights */
 char endingGame = 0;    /* [HGM] crash: flag to prevent recursion of GameEnds() */
 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS     */
 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
@@ -5791,6 +5792,7 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
           click-click move is possible */
        if (toX == -2 || toY == -2) {
            boards[0][fromY][fromX] = EmptySquare;
+           rightsBoard[fromY][fromX] = 0;
            return AmbiguousMove;
        } else if (toX >= 0 && toY >= 0) {
            boards[0][toY][toX] = boards[0][fromY][fromX];
@@ -5807,6 +5809,7 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
                }
            } else
            boards[0][fromY][fromX] = EmptySquare;
+           rightsBoard[fromY][fromX] = rightsBoard[toY][toX] = 0;
            return AmbiguousMove;
        }
         return ImpossibleMove;
@@ -6096,19 +6099,6 @@ UserMoveEvent(fromX, fromY, toX, toY, promoChar)
     FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
 }
 
-#if 0
-void
-PromoDialog(int h, int w, Board board, Boolean clearBoard, char *title, int x, int y)
-{      // dummy routine to mimic with pseudo-popup what front-end should do:
-       // display a popup with h x w mini-board, and divert any mouse clicks
-       // on it to the back-end routines RightClick and LeftClick, just
-       // like the mouse event hadler of the board widget does now.
-       // (Note it would have to off-set x if holdings are displayed!)
-       DisplayMessage("Click on your piece of choice", "");
-       DrawPosition(TRUE, board);
-}
-#endif
-
 int hTab[(int)EmptySquare/2+1] = { 1,1,1,1,1,1,2,1,2,3,2,3,3,3,2,3,4,3,3,4,4,3,4 };
 int wTab[(int)EmptySquare/2+1] = { 1,1,2,3,4,5,3,7,4,3,5,4,4,5,7,5,4,6,6,5,5,7,6 };
 Board promoBoard;
@@ -6450,9 +6440,7 @@ int RightClick(ClickType action, int x, int y, int *xx, int *yy)
       xSqr = BOARD_WIDTH - 1 - xSqr;
     else
       ySqr = BOARD_HEIGHT - 1 - ySqr;
-    if(promotionChoice == 3 && action == Release
-        && promoBoard[ySqr][xSqr] != EmptySquare && (xSqr != fromX || ySqr != fromY) // not needed if separate window
-                                               ) {
+    if(promotionChoice == 3 && action == Release) {
        EditPositionMenuEvent(promoBoard[ySqr][xSqr], fromX, fromY);
        fromX = fromY = -1;
        promotionChoice = 0;
@@ -11975,7 +11963,6 @@ EditGameEvent()
     SetGameInfo();
 }
 
-
 void
 EditPositionEvent()
 {
@@ -11992,7 +11979,14 @@ EditPositionEvent()
     SetGameInfo();
     if (currentMove > 0)
       CopyBoard(boards[0], boards[currentMove]);
-
+    { int i, r, f; // [HGM] editrights: take note of existing rights
+      for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) rightsBoard[r][f] = 0;
+      for(i=0; i<nrCastlingRights; i++) {
+       if(boards[0][CASTLING][i] != NoRights)
+         rightsBoard [castlingRank[i]] [boards[0][CASTLING][i]] = 1 + (i == 2 || i == 5);
+      }
+    }
+    
     blackPlaysFirst = !WhiteOnMove(currentMove);
     ResetClocks();
     currentMove = forwardMostMove = backwardMostMove = 0;
@@ -12024,7 +12018,24 @@ EditPositionDone(Boolean fakeRights)
     startedFromSetupPosition = TRUE;
     InitChessProgram(&first, FALSE);
     if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
+      int i, r, f, kf, err;
+      for(i=0; i<nrCastlingRights; i++) boards[0][CASTLING][i] = NoRights;
+      kf = NoRights; err = 0;
+      for(f=BOARD_RGHT-1; f>=0; f--)
+       if(rightsBoard[0][f] == 2) { if(kf != NoRights) err=10; boards[0][CASTLING][2] = kf = f; }
+      if(kf == NoRights) kf = 4;
+      for(f=BOARD_RGHT-1; f>=0; f--)
+       if(rightsBoard[0][f] == 1) { err++; boards[0][CASTLING][f<kf] = f; }
+      kf = NoRights; err = 0;
+      for(f=BOARD_RGHT-1; f>=0; f--)
+       if(rightsBoard[BOARD_HEIGHT-1][f] == 2) { if(kf != NoRights) err=10; boards[0][CASTLING][5] = kf = f; }
+      if(kf == NoRights) kf = 4;
+      for(f=BOARD_RGHT-1; f>=0; f--)
+       if(rightsBoard[BOARD_HEIGHT-1][f] == 1) { err++; boards[0][CASTLING][3+(f<kf)] = f; }
+      if(err + 2 > nrCastlingRights) DisplayError("unclear castling rights", 0);
+
       boards[0][EP_STATUS] = EP_NONE;
+#if 0
       boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
     if(boards[0][0][BOARD_WIDTH>>1] == king) {
        boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
@@ -12034,6 +12045,7 @@ EditPositionDone(Boolean fakeRights)
        boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
        boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
       } else boards[0][CASTLING][5] = NoRights;
+#endif
     }
     SendToProgram("force\n", &first);
     if (blackPlaysFirst) {
@@ -12131,6 +12143,7 @@ EditPositionMenuEvent(selection, x, y)
 {
     char buf[MSG_SIZ];
     ChessSquare piece = boards[0][y][x];
+    int rights = 0; // [HGM] editrights: most new pieces get no castling rights
 
     if (gameMode != EditPosition && gameMode != IcsExamining) return;
 
@@ -12154,6 +12167,7 @@ EditPositionMenuEvent(selection, x, y)
                        }
                    } else {
                        boards[0][y][x] = p;
+                       rightsBoard[y][x] = 0; // [HGM] editrights: clear all castling rights
                    }
                }
            }
@@ -12171,6 +12185,18 @@ EditPositionMenuEvent(selection, x, y)
        SetBlackToPlayEvent();
        break;
 
+      case NoRights:
+       rightsBoard[y][x] = 0;
+        break;
+
+      case GrantRights:
+        { ChessSquare p = boards[0][y][x];
+         rightsBoard[y][x] = 1;
+         if(p == WhiteKing || p == WhiteUnicorn || p == BlackKing || p == BlackUnicorn)
+               rightsBoard[y][x] = 2;
+       }
+        break;
+
       case EmptySquare:
        if (gameMode == IcsExamining) {
             if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
@@ -12217,12 +12243,24 @@ EditPositionMenuEvent(selection, x, y)
             selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
         goto defaultlabel;
 
+      case WhiteRook: // [HGM] editrights: corner Rooks get castling rights by default
+       if(y == 0 && (x == BOARD_LEFT || x == BOARD_RGHT-1)) rights = 1;
+       goto defaultlabel;
+
+      case BlackRook:
+       if(y == BOARD_HEIGHT-1 && (x == BOARD_LEFT || x == BOARD_RGHT-1)) rights = 1;
+       goto defaultlabel;
+
       case WhiteKing:
       case BlackKing:
         if(gameInfo.variant == VariantXiangqi)
             selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
         if(gameInfo.variant == VariantKnightmate)
             selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
+      case WhiteUnicorn:
+      case BlackUnicorn:
+       if(y == (selection <= WhiteKing ? 0 : BOARD_HEIGHT-1) && (x == BOARD_WIDTH>>1)) 
+           rights = 2; // [HGM] editrights: King on right-center file gets rights
       default:
         defaultlabel:
        if (gameMode == IcsExamining) {
@@ -12247,6 +12285,7 @@ EditPositionMenuEvent(selection, x, y)
                 }
             } else
            boards[0][y][x] = selection;
+           rightsBoard[y][x] = rights; // [HGM] editrights: set default rights of created piece
            DrawPosition(TRUE, boards[0]);
        }
        break;