/*--------------------------------------------------------------------------
    Pawny 0.3, chess engine (source code).
    Copyright (C) 2009 - 2011 by Mincho Georgiev.
    
    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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
    
    contact: pawnychess@gmail.com 
    web: http://www.pawny.netii.net/
----------------------------------------------------------------------------*/

#include "data.h"

bool is_sq_attacked(sq_t target, int color)
{
	int sq,dest,delta;
	int opcolor = color^1;
	attack_t *a = &attack_vector[target][0];

	//pawns:
	if(PieceType(target + pawn_delta[opcolor][0]) == (PAWN | (color) << 3))
		return true;
	if(PieceType(target + pawn_delta[opcolor][1]) == (PAWN | (color) << 3))
		return true;
	//queens:
	for(sq = PLS(QUEEN | color << 3); sq <= H8; sq = PLN(sq))
	{	if(a[sq].flags & R_ATK || a[sq].flags & B_ATK)
		{	delta = a[sq].delta;
			for(dest = sq + delta; IsEmpty(dest); dest += delta)
			{	if(dest == target) return true;
			}
			if(dest == target) return true;
		}
	}
	//rooks:
	for(sq = PLS(ROOK | color << 3); sq <= H8; sq = PLN(sq))
	{	if(a[sq].flags & R_ATK)
		{	delta = a[sq].delta;
			for(dest = sq + delta; IsEmpty(dest); dest += delta)
			{	if(dest == target) return true;
			}
			if(dest == target) return true;
		}
	}
	//bishops:
	for(sq = PLS(BISHOP | color << 3); sq <= H8; sq = PLN(sq))
	{	if(a[sq].flags & B_ATK)
		{	delta = a[sq].delta;
			for(dest = sq + delta; IsEmpty(dest); dest += delta)
			{	if(dest == target) return true;
			}
			if(dest == target) return true;
		}
	}
	//knights:
	for(sq = PLS(KNIGHT | color << 3); sq <= H8; sq = PLN(sq))
	{	if(a[sq].flags & N_ATK)
		{	if(sq + a[sq].delta == target)
				return true;
		}
	}
	//king:
	sq = King_Square(color);
	if(a[sq].flags & R_ATK || a[sq].flags & B_ATK)
	{	if(sq + a[sq].delta == target)
			return true;
	}
	return false;
}

bool is_in_check(int side)
{	if(is_sq_attacked(King_Square(side), (side^1))) return true;
	return false;
}

void init_attack_vector()
{
	int i,j,p,dest;
	const int *d;
	
	memset(&attack_vector,0,sizeof(attack_vector));
	for(i = A1; i <= H8; i++)
	{   if(IsOutside(i)) continue;
               for(j = A1; j <= H8; j++)
               {   if(IsOutside(j) || j == i) continue;
                   for(p = ROOK; p >= KNIGHT; p--)
                       for(d = dir_vect[p]; *d != 0; d++)
                           for(dest = i + *d; !IsOutside(dest); dest += *d)
                               if(dest == j)
                               {   attack_vector[i][j].flags |= atk_masks[p];
                                   attack_vector[i][j].delta = -(sint8)*d;
                               }
	       }
	}
}

