#include <fstream>
#include <iostream>
+#include "bitcount.h"
#include "mersenne.h"
#include "movegen.h"
#include "movepick.h"
/// Position::copy() creates a copy of the input position.
-void Position::copy(const Position &pos) {
+void Position::copy(const Position& pos) {
memcpy(this, &pos, sizeof(Position));
+ saveState(); // detach and copy state info
}
&& (direction_between_squares(from, ksq) != direction_between_squares(to, ksq)))
return true;
- if (move_promotion(m)) // Promotion with check?
+ if (move_is_promotion(m)) // Promotion with check?
{
Bitboard b = occupied_squares();
clear_bit(&b, from);
- switch (move_promotion(m))
+ switch (move_promotion_piece(m))
{
case KNIGHT:
return bit_is_set(piece_attacks<KNIGHT>(to), ksq);
if (move_is_castle(m))
do_castle_move(m);
- else if (move_promotion(m))
+ else if (move_is_promotion(m))
do_promotion_move(m);
else if (move_is_ep(m))
do_ep_move(m);
pieceList[us][piece][index[from]] = to;
index[to] = index[from];
- // Update castle rights
- st->key ^= zobCastle[st->castleRights];
- st->castleRights &= castleRightsMask[from];
- st->castleRights &= castleRightsMask[to];
- st->key ^= zobCastle[st->castleRights];
+ // Update castle rights, try to shortcut a common case
+ if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
+ {
+ st->key ^= zobCastle[st->castleRights];
+ st->castleRights &= castleRightsMask[from];
+ st->castleRights &= castleRightsMask[to];
+ st->key ^= zobCastle[st->castleRights];
+ }
// Update checkers bitboard, piece must be already moved
st->checkersBB = EmptyBoardBB;
board[from] = EMPTY;
// Insert promoted piece
- promotion = move_promotion(m);
+ promotion = move_promotion_piece(m);
assert(promotion >= KNIGHT && promotion <= QUEEN);
set_bit(&(byColorBB[us]), to);
set_bit(&(byTypeBB[promotion]), to);
if (move_is_castle(m))
undo_castle_move(m);
- else if (move_promotion(m))
+ else if (move_is_promotion(m))
undo_promotion_move(m);
else if (move_is_ep(m))
undo_ep_move(m);
assert(piece_on(from) == EMPTY);
// Remove promoted piece
- promotion = move_promotion(m);
+ promotion = move_promotion_piece(m);
assert(piece_on(to)==piece_of_color_and_type(us, promotion));
assert(promotion >= KNIGHT && promotion <= QUEEN);
clear_bit(&(byColorBB[us]), to);
if (pt == KING && stmAttackers)
{
assert(n < 32);
- swapList[n++] = 100;
+ swapList[n++] = QueenValueMidgame*10;
break;
}
} while (stmAttackers);
}
-/// Position::setStartState() copies the content of the argument
+/// Position::saveState() copies the content of the current state
/// inside startState and makes st point to it. This is needed
/// when the st pointee could become stale, as example because
/// the caller is about to going out of scope.
-void Position::setStartState(const StateInfo& s) {
+void Position::saveState() {
- startState = s;
+ startState = *st;
st = &startState;
+ st->previous = NULL; // as a safe guard
}
/// the white and black sides reversed. This is only useful for debugging,
/// especially for finding evaluation symmetry bugs.
-void Position::flipped_copy(const Position &pos) {
+void Position::flipped_copy(const Position& pos) {
assert(pos.is_ok());