/*
   material evaluation
   */

#include "material.h"
#include "evaluate.h"

int isdrawn() {

	if (types[WP] + types[BP]) return 0;
	if (force[WHITE] > 6) return 0;
	if (force[BLACK] > 6) return 0;

	if((force[WHITE] < 4 || types[WN] == 2) && (force[BLACK] < 4 || types[BN] == 2)) return 1;
	return 0;
}

int material(int * drawn)
{

	int wmat = 0;
	int bmat = 0;

	int dw, db;

	int s, v, t;


	/* Insufficient material and/or drawish types */
	/* NB: if 'winning' side CANNOT win, then his advantage must be 0 */

	dw = (types[WP] == 0 && force[WHITE] < 10) * ((force[WHITE] < 4 || (force[WHITE] == 6 && types[WN] == 2))*8 + (force[WHITE]-force[BLACK] < 4));
	db = (types[BP] == 0 && force[BLACK] < 10) * ((force[BLACK] < 4 || (force[BLACK] == 6 && types[BN] == 2))*8 + (force[BLACK]-force[WHITE] < 4));

	*drawn = (dw << 4) | (db);

	if((dw&8) & (db&8)) return 0;


	/* Material */

	bmat = 100 * types[BP] + 325 * (types[BN] + types[BB]) + 500 * types[BR] + 975 * types[BQ];
	wmat = 100 * types[WP] + 325 * (types[WN] + types[WB]) + 500 * types[WR] + 975 * types[WQ];

	/* Kp v K ending */
	if(bmat + wmat == 100)
	{

		if((v = types[WP])) {

			for (t=1; t<16; ++t)
				if (board[pieces[1][t]] == WP) break;

			assert(board[pieces[1][t]] == WP);

			s = w_passer_wins(pieces[1][t]);
		}
		else {

			for (t=1; t<16; ++t)
				if (board[pieces[0][t]] == BP) break;

			assert(board[pieces[0][t]] == BP);

			s = b_passer_wins(pieces[0][t]);

		}

		*drawn = (8<<4)|8;

		if(s == -MATE)
		{
			return 0;
		}

		s = s + 100;

		v = (v&1)? s : -s;

		return v;
	}

	/*
	 * If we fail to identify drawish conditions [that otherwise seem won] many wins will be lost and many draws will be lost
	 *
	 * xpdnt only solves 4 more in ece3.epd with this code. wonder if it is worth it
	 */

	/* KBp v K ending [wrong bishop] */
	if((wmat+bmat) == 425  || (wmat+bmat) == 525 || (wmat+bmat) == 625) {
		if(types[WB]==1 && types[WP]==1) {

			v=s=0;
			for (t = 1; t < 16; ++t) {
				if (board[pieces[1][t]] == WP) v = t;
				if (board[pieces[1][t]] == WB) s = t;
			}

			assert(v > 0 && v < 16);
			assert(s > 0 && s < 16);

			if(FILE(pieces[1][v]) < 2) {
				if(distance[128+kingsq[0]-A1] < 2) { // pawn cannot promote or unknown
					if(sq_color[pieces[1][s]]==0) dw = 8;
				}
			}
			else if(FILE(pieces[1][v]) > 5) {
				if(distance[128+kingsq[0]-H1] < 2) { // pawn cannot promote or unknown
					if(sq_color[pieces[1][s]]==1) dw = 8;
				}
			}


		}
		else if(types[BB]==1 && types[BP]==1) {

			v=s=0;
			for (t = 1; t < 16; ++t) {
				if (board[pieces[0][t]] == BP) v = t;
				if (board[pieces[0][t]] == BB) s = t;
			}

			assert(v > 0 && v < 16);
			assert(s > 0 && s < 16);

			if(FILE(pieces[0][v]) < 2) {
				if(distance[128+kingsq[1]-A8] < 2) { // pawn cannot promote or unknown
					if(sq_color[pieces[0][s]]==1) db = 8;
				}
			}
			else if(FILE(pieces[0][v]) > 5) {
				if(distance[128+kingsq[1]-H8] < 2) { // pawn cannot promote or unknown
					if(sq_color[pieces[0][s]]==0) db = 8;
				}
			}


		}
	}

	*drawn = (dw << 4) | (db);

	//*/


	/* Bishop pair */

	wmat += BISHOP_PAIR * (types[WB] > 1);
	bmat += BISHOP_PAIR * (types[BB] > 1);

	/* Knights and pawns & Rooks*/

	wmat += 6 * types[WN] * (types[WP]-5);
	bmat += 6 * types[BN] * (types[BP]-5);

	wmat -= 12 * types[WR] * (types[WP]-5);
	bmat -= 12 * types[BR] * (types[BP]-5);

	/*
	   PAWN = 79 in open, 130 in end

	   p_curve[i] = {34 ... 17}
	   p_curve[] - 24 = {10 ... -7}
	   3 x (p_curve[] - 24) = {30 ... -21}
	   */

	//wmat += types[WP] * (p_curve[force[BLACK]]-34); 
	//bmat += types[BP] * (p_curve[force[WHITE]]-34); 
	wmat += types[WP] * (p_curve[force[BLACK]]-24)*3; 
	bmat += types[BP] * (p_curve[force[WHITE]]-24)*3;

	feature[WHITE][MATERIAL] = wmat;
	feature[BLACK][MATERIAL] = bmat;

	//return ((wmat-bmat) / ((wmat>bmat)? 1<<dw : 1<<db));
	return wmat - bmat;

}

