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

/*>>> sanmpu.c: SAN project move play/unplay routines */

/* Revised: 1994.02.10 */

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

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

#include "sandsp.h"
#include "sangtc.h"
#include "sanhst.h"
#include "sanmer.h"
#include "sanmne.h"
#include "sanmpu.h"
#include "sanmsc.h"
#include "sansrt.h"
#ifdef CLI
#include "sansss.h"
#endif
#include "sanutl.h"
#include "sanval.h"

/*--> SANPlayMove: play a move */
nonstatic
void SANPlayMove(mptrT mptr)
{
/* locate move in current list */

	curr_g.g_curr = SANFindMove(mptr);
	if(curr_g.g_curr == NULL) {
		SANFatal("SANPlayMove: can't find move");
		exit(0);// kch
	}

/* echo move */

	if(!optnv[optn_namd]) {
		SANDspFullMove(curr_e.e_fmvn, curr_g.g_curr);
	}

/* save transition information */

	prev_e = curr_e;
	prev_m = *curr_g.g_curr;
	prev_b = gb;

/* push to history */

	SANHistPush(SANLocateOrdinal(mptr));

/* execute the move */

	SANExecute();

/* drop analysis index (current ply) */

	an--;

/* regenerate current level */

	SANUpdate();

/* handle game termination */

	SANGTCAutoSet();

/* board display */

	if(!optnv[optn_nabd]) {
		SANDspBoard(); // SANDspBoardDFH();
	}

/* strategies */

#ifdef CLI
	SANSSS(sep_play);
#endif
	return;
}

/*--> SANUnplayMove: unplay the last played move */
nonstatic
void SANUnplayMove(void)
{
/* save transition information (except for the move) */

	prev_e = curr_e;
	prev_b = gb;

/* "Activate the WayBack Machine, Sherman!" */

	SANHistPop();

/* save transition move information */
	prev_m = *curr_g.g_curr;

/* echo move */

	if(!optnv[optn_namd]) {
		SANDspFullMove(curr_e.e_fmvn, curr_g.g_curr);
	}

/* board display */

	if(!optnv[optn_nabd]) {
		SANDspBoard();
	}

/* undo game termination */

	SANGTCSet(gtc_norm);

/* strategies */

#ifdef CLI
	SANSSS(sep_unplay);
#endif
	return;
}

/*<<< sanmpu.c: EOF */
