/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of SanKit -- A multiplatform set of C code
 * for reading and writing PGN formatted chess games. In addition
 * it can be used as an engine to determine in real time if proposed
 * moves are legal or not. It appears to generate near perfect PGN
 * from incorrectly formatted files.
 *
 * Copyright 1994 Steven J. Edwards All Rights Reserved.
 * Copyright 2009-2011 David Hoerl All Rights Reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*>>> sandrw.c: SAN project draw related utilities */

/* Revised: 1993.03.26 */

#include <stdio.h>
#include <stdlib.h>

#include "sandef.h"
#include "sanvar.h"

#include "sanatk.h"
#include "sandrw.h"
#include "sanmlm.h"

/*--> SANDrawTestSmat: test for stalemate draw */
nonstatic
siT SANDrawTestSmat(void)
{
	siT flag;

	SANMLEnsurePolice();
	if((curr_g.g_gmvc == 0) &&
	   !SANAttack(curr_e.e_pasc, plv[curr_e.e_actc][kingslot]))
	{
		flag = 1;
	} else {
		flag = 0;
	}

	return flag;
}

/*--> SANDrawTestIMat: test for insufficient material draw */
nonstatic
siT SANDrawTestIMat(void)
{
	siT flag;
	siT wsm, bsm, ncpv[cpL];
	cpT cp;
	sqT sq;

/* assume no draw */

	flag = 0;

/* scan */

	for(cp = 0; cp < cpL; cp++) {
		ncpv[cp] = 0;
	}
	for(sq = sq_a1; sq <= sq_h8; sq++) {
		ncpv[gb.bv[sq]]++;
	}
	if((ncpv[cp_wp] == 0) && (ncpv[cp_bp] == 0)) {
		if((ncpv[cp_wr] == 0) && (ncpv[cp_br] == 0)) {
			if((ncpv[cp_wq] == 0) && (ncpv[cp_bq] == 0)) {
				/* no pawns or major pieces */

				wsm = (ncpv[cp_wb] > 1) ||
					((ncpv[cp_wb] == 1) && (ncpv[cp_wn] > 0)) ||
					((ncpv[cp_wb] == 0) && (ncpv[cp_wn] > 2));
				bsm = (ncpv[cp_bb] > 1) ||
					((ncpv[cp_bb] == 1) && (ncpv[cp_bn] > 0)) ||
					((ncpv[cp_bb] == 0) && (ncpv[cp_bn] > 2));
				if(!wsm && !bsm) {
					flag = 1;
				}
			}
		}
	}

	return flag;
}

/*<<< sandrw.c: EOF */
