+ if (is_castle(m))
+ san = (move_to(m) < move_from(m) ? "O-O-O" : "O-O");
+ else
+ {
+ if (pt != PAWN)
+ {
+ san = piece_type_to_char(pt);
+
+ // Disambiguation if we have more then one piece with destination 'to'
+ // note that for pawns is not needed because starting file is explicit.
+ attackers = pos.attackers_to(to) & pos.pieces(pt, pos.side_to_move());
+ clear_bit(&attackers, from);
+ ambiguousMove = ambiguousFile = ambiguousRank = false;
+
+ while (attackers)
+ {
+ sq = pop_1st_bit(&attackers);
+
+ // Pinned pieces are not included in the possible sub-set
+ if (!pos.pl_move_is_legal(make_move(sq, to), pos.pinned_pieces()))
+ continue;
+
+ if (file_of(sq) == file_of(from))
+ ambiguousFile = true;
+
+ if (rank_of(sq) == rank_of(from))
+ ambiguousRank = true;
+
+ ambiguousMove = true;
+ }
+
+ if (ambiguousMove)
+ {
+ if (!ambiguousFile)
+ san += file_to_char(file_of(from));
+ else if (!ambiguousRank)
+ san += rank_to_char(rank_of(from));
+ else
+ san += square_to_string(from);
+ }
+ }