////
#include <cassert>
-#include <iostream>
+#include <cstring>
#include <fstream>
+#include <iostream>
#include "mersenne.h"
#include "movegen.h"
}
+/// Position::init_new_state() copies from the current state the fields
+/// that will be updated incrementally, skips the fields, like bitboards
+/// that will be recalculated form scratch anyway.
+
+void Position::init_new_state(StateInfo& newSt) {
+
+ newSt.key = st->key;
+ newSt.pawnKey = st->pawnKey;
+ newSt.materialKey = st->materialKey;
+ newSt.castleRights = st->castleRights;
+ newSt.rule50 = st->rule50;
+ newSt.epSquare = st->epSquare;
+ newSt.mgValue = st->mgValue;
+ newSt.egValue = st->egValue;
+ newSt.capture = NO_PIECE_TYPE;
+ newSt.previous = st;
+}
+
+
/// Position::do_move() makes a move, and saves all information necessary
/// to a StateInfo object. The move is assumed to be legal.
/// Pseudo-legal moves should be filtered out before this function is called.
assert(is_ok());
assert(move_is_ok(m));
- // Get now the current (pre-move) dc candidates that we will use
+ // Get now the current (before to move) dc candidates that we will use
// in update_checkers().
Bitboard oldDcCandidates = discovered_check_candidates(side_to_move());
- // Copy the old state to our new StateInfo object (except the
- // captured piece, which is taken care of later.
- // TODO do not copy pinners and checkersBB because are recalculated
- // anyway.
- newSt = *st;
- newSt.capture = NO_PIECE_TYPE;
- newSt.previous = st;
+ // Copy some fields of old state to our new StateInfo object (except the
+ // captured piece, which is taken care of later) and switch state pointer
+ // to point to the new, ready to be updated, state.
+ init_new_state(newSt);
st = &newSt;
// Save the current key to the history[] array, in order to be able to
void Position::clear() {
st = &startState;
- st->previous = NULL; // We should never dereference this
+ memset(st, 0, sizeof(StateInfo));
+ st->epSquare = SQ_NONE;
+
+ memset(index, 0, sizeof(int) * 64);
+ memset(byColorBB, 0, sizeof(Bitboard) * 2);
for (int i = 0; i < 64; i++)
- {
board[i] = EMPTY;
- index[i] = 0;
- }
-
- for (int i = 0; i < 2; i++)
- byColorBB[i] = EmptyBoardBB;
for (int i = 0; i < 7; i++)
{
pieceList[0][i][j] = pieceList[1][i][j] = SQ_NONE;
}
- st->checkersBB = EmptyBoardBB;
- for (Color c = WHITE; c <= BLACK; c++)
- st->pinners[c] = st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB;
-
sideToMove = WHITE;
gamePly = 0;
initialKFile = FILE_E;
initialKRFile = FILE_H;
initialQRFile = FILE_A;
-
- st->lastMove = MOVE_NONE;
- st->castleRights = NO_CASTLES;
- st->epSquare = SQ_NONE;
- st->rule50 = 0;
- st->previous = NULL;
}