#include "nsee.h"

/*
	
*/

int nsee(int start, int target)
{
	int attacks[2][16] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
	int *att[2];
	int *a;
	int i, side, sign, victim, dest, score, delta, offender, temp;
	
	att[0] = &attacks[0][0];
	att[1] = &attacks[1][0];
	
	/*
		Begin with cap we wish to gen score for
	*/
	
	side = stm^1;
	victim = mval[board[target]];
	temp = board[start];
	board[start] = MT;
	sign = -1;
	score = victim;
	victim = mval[temp];
	
	/*
		collect the rest of attacks upon sq from both sides
		least to greatest
	*/
	if(board[target+15] == WP) *(att[1]++) = target+15;
	if(board[target+17] == WP) *(att[1]++) = target+17;
	if(board[target-15] == BP) *(att[0]++) = target-15;
	if(board[target-17] == BP) *(att[0]++) = target-17;
	
	for(i = 0; i < 8; ++i)
	{
		if(board[target + dlta[i]] == WN) {*att[1] = target+dlta[i]; att[1]++;}
		if(board[target + dlta[i]] == BN) {*att[0] = target+dlta[i]; att[0]++;}
	}
	
	for(i = 8; i < 12; ++i)
	{
		dest = target;
		while(board[dest += dlta[i]] == MT);
		
		if(board[dest] == WB) {*att[1] = dest; att[1]++;}
		if(board[dest] == BB) {*att[0] = dest; att[0]++;}
	}
	
	for(i = 12; i < 16; ++i)
	{
		dest = target;
		while(board[dest += dlta[i]] == MT);
		
		if(board[dest] == WR) {*att[1] = dest; att[1]++;}
		if(board[dest] == BR) {*att[0] = dest; att[0]++;}
	}
	
	for(i = 8; i < 16; ++i)
	{
		dest = target;
		while(board[dest += dlta[i]] == MT);
		
		if(board[dest] == WQ) {*att[1] = dest; att[1]++;}
		if(board[dest] == BQ) {*att[0] = dest; att[0]++;}
	}
	
	for(i = 8; i < 16; ++i)
	{
		if(board[target+dlta[i]] == WK) {*att[1] = target+dlta[i]; att[1]++;}
		if(board[target+dlta[i]] == BK) {*att[0] = target+dlta[i]; att[0]++;}
	}
	
	board[start] = temp;
	
	
	/*  init to begin of caps */
	att[0] = &attacks[0][0];
	att[1] = &attacks[1][0];

	/*
	temp = side;
	while(*att[side])
	{
		printf("%d ", *att[side]);
		att[side]++;
		side ^= 1;
	}
	side = temp;
	att[0] = &attacks[0][0];
	att[1] = &attacks[1][0];
	*/
	
	/* alternate thru all caps. NB 1st capture is refutation of original for which we seek score */

	/*
	 * Qxp s=100
	 * -  nxQ s=-875
	 * +  Bxn s=-550	break
	 *
	 * Bxp s=100
	 * -  nxB s=-225
	 * +  Qxn s=+100
	 * -  bxQ s=-875
	 */   
	
	while(*att[side])
	{
		//if(sign * (score + sign * victim) < 0) break;
		
		score += victim * sign;
		
		//if(sign == 1 && score < 0) break; /* when score fails to restore or just equalizes*/
										/* other side can stand pat */

		if(victim > 975) break; /* king is captured */
		
		victim = mval[board[*att[side]]];
		
		delta = -vectors[136 + *att[side] - target];

		
		/* scan for slider behind if on ray */
		if(delta)
		{
			
			offender = (abs(delta) == 15 || abs(delta) == 17)? (BB) : (BR);
			temp = mval[offender];
			dest = *att[side] + delta;
			
			while(board[dest] == MT) dest += delta;
			
			if((board[dest]&14) == offender)
			{
				
				/* seek to end of cap list [color of new found piece] */
				
				a = att[board[dest]&1];
				while(*a) ++a;
				
				while(a != (att[side]+1) && a > att[board[dest]&1] && mval[board[*a]] > temp)
				{
					*a = *(a-1);
					--a;
				}
				
				*a = dest;
				
			}
			else if ((board[dest]&14) == BQ)
			{
				a = att[board[dest]&1];
				while(*a) ++a;

				temp = mval[BQ];
				
				while(a != (att[side]+1) && a > att[board[dest]&1] && mval[board[*a]] > temp)
				{
					*a = *(a-1);
					--a;
				}
				
				*a = dest;
				
			}
			
		}
		
		
		++att[side];
		sign = -sign;
		side ^= 1;
	}

	/*
	att[0] = &attacks[0][0];
	att[1] = &attacks[1][0];
	while(*att[side])
	{
		printf("%d ", *att[side]);
		att[side]++;
		side ^= 1;
	}
	*/

	return score;
}
	
	
		
		
