- // This is a castling move, but we have to translate it to the
- // internal "king captures rook" representation.
- SquareDelta delta = (to > from ? DELTA_E : DELTA_W);
- Square s = from + delta;
- while (relative_rank(us, s) == RANK_1 && pos.piece_on(s) != piece_of_color_and_type(us, ROOK))
- s += delta;
-
- return (relative_rank(us, s) == RANK_1 ? make_castle_move(from, s) : MOVE_NONE);
+ san = piece_type_to_char(pt);
+
+ // Collect all legal moves of piece type 'pt' with destination 'to'
+ MoveStack* last = generate<MV_LEGAL>(pos, mlist);
+ int f = 0, r = 0;
+
+ for (MoveStack* cur = mlist; cur != last; cur++)
+ if ( move_to(cur->move) == to
+ && pos.type_of_piece_on(move_from(cur->move)) == pt)
+ {
+ if (square_file(move_from(cur->move)) == square_file(from))
+ f++;
+
+ if (square_rank(move_from(cur->move)) == square_rank(from))
+ r++;
+ }
+
+ assert(f > 0 && r > 0);
+
+ // Disambiguation if we have more then one piece with destination 'to'
+ if (f == 1 && r > 1)
+ san += file_to_char(square_file(from));
+ else if (f > 1 && r == 1)
+ san += rank_to_char(square_rank(from));
+ else if (f > 1 && r > 1)
+ san += square_to_string(from);
+ }
+
+ if (pos.move_is_capture(m))
+ {
+ if (pt == PAWN)
+ san += file_to_char(square_file(from));
+
+ san += 'x';
+ }
+
+ san += square_to_string(to);
+
+ if (move_is_promotion(m))
+ {
+ san += '=';
+ san += piece_type_to_char(move_promotion_piece(m));