/* Pepito v1.59  -- file: initpos.c --
   Copyright (c) 2000-02 Carlos del Cacho

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Contact the author: guggenjaimito@yahoo.com
*/

#ifndef UNIQUE_SOURCE_FILE
#  include "datos.h"
#endif

//----------------------------------------------------------------------------------
void            PosicionInicial()
{
    int             i;
    AlPaso = 64;
    Enroque = E_CORTO_B | E_CORTO_N | E_LARGO_B | E_LARGO_N;
    Turno = 1;

    Tablero[A1] = TORRE_B;
    Tablero[B1] = CABALLO_B;
    Tablero[C1] = ALFIL_B;
    Tablero[D1] = DAMA_B;
    Tablero[E1] = REY_B;
    Tablero[F1] = ALFIL_B;
    Tablero[G1] = CABALLO_B;
    Tablero[H1] = TORRE_B;

    for (i = H2; i > H3; i--)
        Tablero[i] = PEON_B;

    for (i = H3; i > H7; i--)
        Tablero[i] = VACIA;

    for (i = H7; i > H8; i--)
        Tablero[i] = PEON_N;

    Tablero[A8] = TORRE_N;
    Tablero[B8] = CABALLO_N;
    Tablero[C8] = ALFIL_N;
    Tablero[D8] = DAMA_N;
    Tablero[E8] = REY_N;
    Tablero[F8] = ALFIL_N;
    Tablero[G8] = CABALLO_N;
    Tablero[H8] = TORRE_N;
}
//----------------------------------------------------------------------------------
void            InitPos(int clear_hash)
{
    int             i;

    Rotado45 = Rotado315 = Rotado90 = CaballosB = CaballosN = 0;
    AlfilesB = AlfilesN = PeonesB = PeonesN = ReyB = ReyN = PiezasN = 0;
    DamasB = DamasN = TorresB = TorresN = Blancas = Negras = PiezasB = 0;
    HashGlob = HashPeones = MaterialB = MaterialN = NumJug = 0;
    NumPeonesB = NumPeonesN = 0;
    Historial[0].last_cap = 0;
    MaxPos[0] = MaxPos[1] = PEON_VAL;
    tab.jug_limit = 0;
    tab.opp_bk_limit = 0;
    tab.my_bk_limit = 0;
    RNumJug = 0;

    if (!Turno)
        HashGlob = ~HashGlob;

    if (clear_hash)
        ClearTables(1);

    for (i = 0; i < 64; i++) {

        if (Tablero[i] != VACIA) {
            Rotado45 |= MASK[ROTADO_45[i]];
            Rotado315 |= MASK[ROTADO_315[i]];
            Rotado90 |= MASK[ROTADO_90[i]];
            HashGlob ^= Zobrist(Tablero[i], i);

            if (Tablero[i] < PEON_N) {
                Blancas |= MASK[i];
                switch (Tablero[i]) {
                case DAMA_B:
                    DamasB |= MASK[i];
                    MaterialB += DAMA_VAL;
                    PiezasB++;
                    break;
                case TORRE_B:
                    TorresB |= MASK[i];
                    MaterialB += TORRE_VAL;
                    PiezasB++;
                    break;
                case ALFIL_B:
                    AlfilesB |= MASK[i];
                    MaterialB += ALFIL_VAL;
                    PiezasB++;
                    break;
                case CABALLO_B:
                    CaballosB |= MASK[i];
                    MaterialB += CABALLO_VAL;
                    PiezasB++;
                    break;
                case REY_B:
                    ReyB |= MASK[i];
                    PosReyB = i;
                    break;
                case PEON_B:
                    PeonesB |= MASK[i];
                    MaterialB += PEON_VAL;
                    HashPeones ^= Zobrist(PEON_B, i);
                    NumPeonesB++;
                    break;
                }               // switch
            }                   // if
            else {
                Negras |= MASK[i];
                switch (Tablero[i]) {
                case DAMA_N:
                    DamasN |= MASK[i];
                    MaterialN += DAMA_VAL;
                    PiezasN++;
                    break;
                case TORRE_N:
                    TorresN |= MASK[i];
                    MaterialN += TORRE_VAL;
                    PiezasN++;
                    break;
                case ALFIL_N:
                    AlfilesN |= MASK[i];
                    MaterialN += ALFIL_VAL;
                    PiezasN++;
                    break;
                case CABALLO_N:
                    CaballosN |= MASK[i];
                    MaterialN += CABALLO_VAL;
                    PiezasN++;
                    break;
                case REY_N:
                    ReyN |= MASK[i];
                    PosReyN = i;
                    break;
                case PEON_N:
                    PeonesN |= MASK[i];
                    MaterialN += PEON_VAL;
                    HashPeones ^= Zobrist(PEON_N, i);
                    NumPeonesN++;
                    break;
                }               // switch

            }                   // else
        }                       // if (no vacía)
    }                           // for

    tab.rpiezasB = PiezasB;
    tab.rpiezasN = PiezasN;
    tab.rmat_diff = MaterialB - MaterialN;


    if (ReyB & (entero64) 0xe000000000000000 && Tablero[H1] != TORRE_B)
        PosStatus |= REY_B_ENRQ_CORTO;
    if (ReyN & (entero64) 0xe0 && Tablero[H8] != TORRE_N)
        PosStatus |= REY_N_ENRQ_CORTO;
    if (ReyB & (entero64) 0x700000000000000 && Tablero[A1] != TORRE_B)
        PosStatus |= REY_B_ENRQ_LARGO;
    if (ReyN & (entero64) 0x7 && Tablero[A8] != TORRE_N)
        PosStatus |= REY_N_ENRQ_LARGO;

    if (Enroque & E_CORTO_B)
        HashGlob ^= HASH_E_CORTO_B;
    if (Enroque & E_LARGO_B)
        HashGlob ^= HASH_E_LARGO_B;
    if (Enroque & E_CORTO_N)
        HashGlob ^= HASH_E_CORTO_N;
    if (Enroque & E_LARGO_N)
        HashGlob ^= HASH_E_LARGO_N;

    if (AlPaso != 64) {
        if (AlPaso > 32)        // TurnoBlancas
            HashGlob ^= Zobrist(PEON_B, 56 + (AlPaso & 7));
        else
            HashGlob ^= Zobrist(PEON_N, (AlPaso & 7));
    }

    return;
}
