/* Pepito v1.59  -- file: see.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
*/

/* falta descubrir ataques ocultos */

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

int             SSE(register entero32 jug)
{
    register int    hacia = (jug >> 6) & 0x3f;
    register entero64 ataques = AtaquesHacia(hacia);
    register int    turno = !Turno;
    register int    num_caps = 0,
                    casilla;
    int             ulti_cas = jug & 0x3f;
    int             eval[32];

    ataques &= ~MASK[ulti_cas];
    eval[0] = 0;

    if (jug & CAPTURA)
        eval[0] += VALOR[(jug >> 12) & 0xf];

    if (jug & PROMOCION)
        eval[0] += (VALOR[(jug >> 16) & 0xf] - PEON_VAL);

    while (ataques) {
        if (turno) {
            if (ataques & PeonesB)
                casilla = Msb(ataques & PeonesB);
            else if (ataques & CaballosB)
                casilla = Msb(ataques & CaballosB);
            else if (ataques & AlfilesB)
                casilla = Msb(ataques & AlfilesB);
            else if (ataques & TorresB)
                casilla = Msb(ataques & TorresB);
            else if (ataques & DamasB)
                casilla = Msb(ataques & DamasB);
            else if (ataques & ReyB)
                casilla = Msb(ataques & ReyB);
            else
                break;
        } else {
            if (ataques & PeonesN)
                casilla = Msb(ataques & PeonesN);
            else if (ataques & CaballosN)
                casilla = Msb(ataques & CaballosN);
            else if (ataques & AlfilesN)
                casilla = Msb(ataques & AlfilesN);
            else if (ataques & TorresN)
                casilla = Msb(ataques & TorresN);
            else if (ataques & DamasN)
                casilla = Msb(ataques & DamasN);
            else if (ataques & ReyN)
                casilla = Msb(ataques & ReyN);
            else
                break;
        }

        eval[num_caps + 1] = -eval[num_caps] + VALOR[Tablero[ulti_cas]];
        num_caps++;


// salimos si ya perdemos material y no nos toca mover (no nos
// interesa saber cuanto)

/*        if (eval[num_caps] < 0 && !(num_caps&1))
        {
          printf("patatas...\n");
          return eval[num_caps];
        }*/

        if (DIRECCION[hacia][casilla])
            ataques |= DescubreCap(casilla, DIRECCION[hacia][casilla]);


        ataques ^= MASK[casilla];

        ulti_cas = casilla;
        turno = !turno;
    }

    for (; num_caps; num_caps--) {
            if (eval[num_caps] > -eval[num_caps - 1])
                eval[num_caps - 1] = -eval[num_caps];
    }
    return (eval[0]);
}
//--------------------------------------------------------------------------------
entero64        DescubreCap(int casilla, int direcc)
{
    if (direcc) {
        switch (direcc) {
        case 1:
            return ((DamasB|DamasN|AlfilesB|AlfilesN) & RAYO_NE[casilla] & ATAQUES_ALFIL_45[casilla][255 & (Rotado45 >> SHIFT_45[casilla])]);
        case 2:
            return ((DamasB|DamasN|TorresB|TorresN) & RAYO_E[casilla] & ATAQUES_TORRE_FIL[casilla][255 & ((Blancas | Negras) >> 8 * DIV8(casilla))]);
        case 3:
            return ((DamasB|DamasN|AlfilesB|AlfilesN) & RAYO_SE[casilla] & ATAQUES_ALFIL_315[casilla][255 & (Rotado315 >> SHIFT_315[casilla])]);
        case 4:
            return ((DamasB|DamasN|TorresB|TorresN) & RAYO_S[casilla] & ATAQUES_TORRE_COL[casilla][255 & (Rotado90 >> 8 * MOD8(casilla))]);
        case 5:
            return ((DamasB|DamasN|AlfilesB|AlfilesN) & RAYO_SO[casilla] & ATAQUES_ALFIL_45[casilla][255 & (Rotado45 >> SHIFT_45[casilla])]);
        case 6:
            return ((DamasB|DamasN|TorresB|TorresN) & RAYO_O[casilla] & ATAQUES_TORRE_FIL[casilla][255 & ((Blancas | Negras) >> 8 * DIV8(casilla))]);
        case 7:
            return ((DamasB|DamasN|AlfilesB|AlfilesN) & RAYO_NO[casilla] & ATAQUES_ALFIL_315[casilla][255 & (Rotado315 >> SHIFT_315[casilla])]);
        case 8:
            return ((DamasB|DamasN|TorresB|TorresN) & RAYO_N[casilla] & ATAQUES_TORRE_COL[casilla][255 & (Rotado90 >> 8 * MOD8(casilla))]);
        }                       // switch
    }
    return 0;
}
