/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * 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.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*>>> sanrst.c: SAN project report status routines */

/* Revised: 1993.04.07 */

#include <stdio.h>

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

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

/*--> SANStatus: return current status */
nonstatic
gsT SANStatus(void)
{
	gsT gs;

/* set initial return value */

	if(SANCensus() == cen_lgl) {
		gs = gs_norm;
	} else {
		gs = gs_bust;
	}

/* test halfmove clock */

	if(gs == gs_norm) {
		if(curr_e.e_hmvc >= hmvcL) {
			gs = gs_dhmc;
		}
	}

/* test for white checkmated */

	if(gs == gs_norm) {
		if(curr_e.e_actc == c_w) {
			SANMLEnsurePolice();
			if((curr_g.g_gmvc == 0) && (SANAttack(c_b, plv[c_w][kingslot]))) {
				gs = gs_wcmt;
			}
		}
	}

/* test for black checkmated */

	if(gs == gs_norm) {
		if(curr_e.e_actc == c_b) {
			SANMLEnsurePolice();
			if((curr_g.g_gmvc == 0) && (SANAttack(c_w, plv[c_b][kingslot]))) {
				gs = gs_bcmt;
			}
		}
	}

/* test for white stalemated */

	if(gs == gs_norm) {
		if((curr_e.e_actc == c_w) && SANDrawTestSmat()) {
			gs = gs_wsmt;
		}
	}

/* test for black stalemated */

	if(gs == gs_norm) {
		if((curr_e.e_actc == c_b) && SANDrawTestSmat()) {
			gs = gs_bsmt;
		}
	}

/* test for insufficient material */

	if(gs == gs_norm) {
		if(SANDrawTestIMat()) {
			gs = gs_dmat;
		}
	}

/* test for game overflow */

	if(gs == gs_norm) {
		if(curr_e.e_fmvn > fmpL) {
			gs = gs_over;
		}
	}

	return gs;
}

/*<<< sanrst.c: EOF */
