Prevent multi-path moves from parsing as ambiguous
[xboard.git] / moves.c
diff --git a/moves.c b/moves.c
index e3afe36..29638b8 100644 (file)
--- a/moves.c
+++ b/moves.c
@@ -247,33 +247,41 @@ MovesFromString (Board board, int flags, int f, int r, char *desc, MoveCallback
        if(*desc == 'c') mode |= his, desc++;
        if(*desc == 'd') mode |= mine, desc++;
        if(*desc == 'e') mode |= 8, desc++;
+       if(!mode) mode = his + 4;// no mode spec, use default = mc
+       if(*desc == 'p') mode |= 32, desc++;
+       if(*desc == 'g') mode |= 64, desc++;
+       if(*desc == 'o') mode |= 128, desc++;
        if(*desc == 'n') jump = 0, desc++;
        while(*desc == 'j') jump++, desc++;
-       if(!mode) mode = his + 4;// no mode spec, use default = mc
        dx = xStep[*p-'A'] - '0';                     // step vector of atom
        dy = yStep[*p-'A'] - '0';
        if(isdigit(*++p)) expo = atoi(p++);           // read exponent
        if(expo > 9) p++;                             // allow double-digit
        desc = p;                                     // this is start of next move
-       if(initial && board[r][f] != initialPosition[r][f]) continue;
+       if(initial && (board[r][f] != initialPosition[r][f] ||
+                      r == 0              && board[TOUCHED_W] & 1<<f ||
+                      r == BOARD_HEIGHT-1 && board[TOUCHED_B] & 1<<f   ) ) continue;
        if(expo > 1 && dx == 0 && dy == 0) {          // castling indicated by O + number
            mode |= 16; dy = 1;
        }
         do {
          for(dir=0, bit=1; dir<8; dir++, bit += bit) { // loop over directions
-           int i = expo, vx, vy;
+           int i = expo, hop = mode, vx, vy;
            if(!(bit & dirSet)) continue;             // does not move in this direction
            vx = dx*rot[dir][0] + dy*rot[dir][1];     // rotate step vector
            vy = dx*rot[dir][2] + dy*rot[dir][3];
            x = f; y = r;                             // start square
            do {
                x += vx; y += vy;                     // step to next square
-               if(y < 0 || y >= BOARD_HEIGHT || x < BOARD_LEFT || x >= BOARD_RGHT) break;
+               if(y < 0 || y >= BOARD_HEIGHT) break; // vertically off-board: always done
+               if(x <  BOARD_LEFT) { if(mode & 128) x += BOARD_RGHT - BOARD_LEFT; else break; }
+               if(x >= BOARD_RGHT) { if(mode & 128) x -= BOARD_RGHT - BOARD_LEFT; else break; }
                if(!jump    && board[y - vy + vy/2][x - vx + vx/2] != EmptySquare) break; // blocked
                if(jump > 1 && board[y - vy + vy/2][x - vx + vx/2] == EmptySquare) break; // no hop
                if(board[y][x] < BlackPawn)   occup = 1; else
                if(board[y][x] < EmptySquare) occup = 2; else
                                              occup = 4;
+               if(hop & 32+64) { if(occup != 4) { if(hop & 64 && i != 1) i = 2; hop &= 31; } continue; } // hopper
                if(mode & 8 && y == board[EP_RANK] && occup == 4 && board[EP_FILE] == x) { // to e.p. square
                    cb(board, flags, mine == 1 ? WhiteCapturesEnPassant : BlackCapturesEnPassant, r, f, y, x, cl);
                }
@@ -1807,6 +1815,8 @@ DisambiguateCallback (Board board, int flags, ChessMove kind, int rf, int ff, in
        (cl->rtIn == -1 || cl->rtIn == rt || wildCard) &&
        (cl->ftIn == -1 || cl->ftIn == ft || wildCard)) {
 
+       if(cl->count && rf == cl->rf && ff == cl->ff) return; // duplicate move
+
        cl->count++;
        if(cl->count == 1 || board[rt][ft] != EmptySquare) {
          // [HGM] oneclick: if multiple moves, be sure we remember capture