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

/*>>> sanenu.c: SAN project position subtree enumeration */

/* Revised: 1993.04.11 */

#include <stdio.h>
#include <strings.h>

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

#include "sandsp.h"
#include "sanenu.h"
#include "sangen.h"
#include "sanmer.h"

#include "sanmne.h"


/*--> SANEnumerate: enumerate subtree nodes */
nonstatic
liT SANEnumerate(siT depth)
{
	liT count, msum;
	siT i, next;
	mptrT mptr;
//mT m;

	count = 0;

	if(an == 0) {
		SANGenMoves();
	} else {
		SANGenFullPL();
	}
	next = depth - 1;
	curr_g.g_curr = curr_g.g_base;

	for(i = 0; i < curr_g.g_gmvc; i++) {
		mptr = curr_g.g_curr;
//	m = *mptr;
		SANExecute();
		if(mptr->m_flag & mf_bust) {
			msum = 0;
		} else {
			if(next == 0) {
				msum = 1;
			} else {
				msum = SANEnumerate(next);
			}
			if(an == 1) {
				SANDspSANTB(mptr);
				SANDspStrLINL(": ", msum);
			}
			fflush(stdout);
		}
		SANRetract();
		curr_g.g_curr++;
		count += msum;
	}

	return count;
}

nonstatic
liT SANEnumerateCP(siT sq)
{
	SANGenMoves();

	curr_g.g_curr = curr_g.g_base;
	liT match = 0;
	for(siT i = 0; i < curr_g.g_gmvc && !match; i++) {
		mptrT mptr = curr_g.g_curr;
		SANExecute();
		if(!(mptr->m_flag & mf_bust)) {
			//printf("TEST %#2.2o == %#2.2o\n", sq, mptr->m_frsq);
			if(sq == mptr->m_frsq) {
				match=1;
			}
		}
		SANRetract();
		curr_g.g_curr++;
	}
	return match;
}

nonstatic
liT SANEnumerateCP2(siT fromSq, siT toSq, const char *suffix, sanT san)
{
	SANGenMoves();

	char tmpSan[sanL];

	curr_g.g_curr = curr_g.g_base;
	liT match = 0;
	for(siT i = 0; i < curr_g.g_gmvc; i++) {
		mptrT mptr = curr_g.g_curr;
		SANExecute();
		if(!(mptr->m_flag & mf_bust)) {
			if(fromSq == mptr->m_frsq && toSq == mptr->m_tosq) {
				int foundOne = 1;
				// printf("Found %s\n", san);
				SANEncode(mptr, tmpSan);
				if(suffix[0]) {
					foundOne = strstr(tmpSan, suffix) == NULL ? 0 : 1;	// san could have a trailing '+' or '#'
				}
				if(foundOne) {
					match++;
					strcpy(san, tmpSan);
				}
			}
		}
		SANRetract();
		curr_g.g_curr++;
	}
	return match;
}

siT str2sq(char *sq)
{
	siT rank = sq[0] - 'a';
	siT file = sq[1] - '1';

	return map_sq_rank_file(rank, file);
}

#if 0

#define map_rank_sq(sq) ((sq) >> fileQ)
#define map_file_sq(sq) ((sq) & fileM)
#define map_sq_rank_file(rank, file) (((rank) << fileQ) | (file))

/* squares */

typedef siT sqT, *sqptrT;
#define sqQ (rankQ + fileQ)
#define sqsqQ (2 * sqQ)
#define sqL (bit << sqQ)
#define sqM 0x3f
#define sq_nil (-1)

#endif

/*<<< sanenu.c: EOF */
