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

/*>>> sandsp.c: SAN project simple display routines */

/* Revised: 1993.05.16 */

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

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

#include "sandsp.h"
#include "sanmne.h"
#include "sanmsc.h"
#ifdef CLI
#include "sanrst.h"
#endif
#include "sanutl.h"

#include <assert.h>

static void SANDspColorPiece(cpT cp);


/*--> SANDspChar: display a character */
nonstatic
void SANDspChar(char ch)
{
/* this is the choke point function for stdout */

	if(st_fptr != NULL) {
		fputc(ch, st_fptr);
	}
	if(do_fptr != NULL) {
		fputc(ch, do_fptr);
	}

	return;
}

/*--> SANDspStr: display a string */
nonstatic
void SANDspStr(charptrT s)
{
	if(st_fptr != NULL) {
		fputs(s, st_fptr);
	}
	if(do_fptr != NULL) {
		fputs(s, do_fptr);
	}

	return;
}

/*--> SANDspNL: display a newline */
nonstatic
void SANDspNL(void)
{
	SANDspChar('\n');

	return;
}

/*--> SANDspLine: display a string and a newline */
nonstatic
void SANDspLine(charptrT s)
{
	SANDspStr(s);
	SANDspNL();

	return;
}

/*--> SANDspSTV: display the scratch text vector */
nonstatic
void SANDspSTV(void)
{
	SANDspStr(stv);

	return;
}

/*--> SANDspSTVNL: display the scratch text vector and a newline */
nonstatic
void SANDspSTVNL(void)
{
	SANDspSTV();
	SANDspNL();

	return;
}

/*--> SANDspNote: display a notification */
nonstatic
void SANDspNote(charptrT s)
{
	SANDspStr("*** Note: ");
	SANDspStr(s);
	SANDspNL();

	return;
}

/*--> SANDspError: display an error */
nonstatic
void SANDspError(charptrT s)
{
	SANDspStr("*** Error: ");
	SANDspStr(s);
	SANDspNL();

	return;
}

/*--> SANDspErrorSTV: display an error from the STV */
nonstatic
void SANDspErrorSTV(void)
{
	SANDspError(stv);

	return;
}

/*--> SANDspSI: display a short integer */
nonstatic
void SANDspSI(siT si)
{
	char tv[32];

	(void) sprintf(tv, "%hd", si);
	SANDspStr(tv);

	return;
}

/*--> SANDspLI: display a long integer */
nonstatic
void SANDspLI(liT li)
{
	char tv[32];

	(void) sprintf(tv, "%d", li);
	SANDspStr(tv);

	return;
}

/*--> SANDspStrSI: display a string and a short integer */
nonstatic
void SANDspStrSI(charptrT s, siT si)
{
	SANDspStr(s);
	SANDspSI(si);

	return;
}

/*--> SANDspStrLI: display a string and a long integer */
nonstatic
void SANDspStrLI(charptrT s, liT li)
{
	SANDspStr(s);
	SANDspLI(li);

	return;
}

/*--> SANDspStrSINL: display a string, a short int, and a newline */
nonstatic
void SANDspStrSINL(charptrT s, siT si)
{
	SANDspStrSI(s, si);
	SANDspNL();

	return;
}

/*--> SANDspStrLINL: display a string, a long integer and a newline */
nonstatic
void SANDspStrLINL(charptrT s, liT li)
{
	SANDspStrLI(s, li);
	SANDspNL();

	return;
}

/*--> SANDspTimestamp: display the current timestamp */
nonstatic
void SANDspTimestamp(void)
{
	stampT stamp;

	SANTimestamp(stamp);
	SANDspLine(stamp);

	return;
}

/*--> SANDspSq: display a square */
nonstatic
void SANDspSq(sqT sq)
{
	SANDspChar(acfv[map_file_sq(sq)]);
	SANDspChar(acrv[map_rank_sq(sq)]);

	return;
}

/*--> SANDspColor: display color character */
nonstatic
void SANDspColor(cT c)
{
	SANDspChar(accv[c]);

	return;
}

/*--> SANDspPiece: display piece character */
nonstatic
void SANDspPiece(pT p)
{
	SANDspChar(acpv[p]);

	return;
}

/*--> SANDspColorPiece: display color+piece characters */
static
void SANDspColorPiece(cpT cp)
{
	SANDspColor(cv_c_cpv[cp]);
	SANDspPiece(cv_p_cpv[cp]);

	return;
}

/*--> SANDspSAN: SAN display */
nonstatic
void SANDspSAN(mptrT mptr)
{
	sanT san;

	SANEncode(mptr, san);
	SANDspStr(san);

	return;
}

/*--> SANDspSANTB: display SAN with trailing blanks */
nonstatic
void SANDspSANTB(mptrT mptr)
{
	siT i;
	sanT san;

	SANEncode(mptr, san);
	SANDspStr(san);

	i = strlen(san);
	while(i < sanL) {
		SANDspChar(' ');
		i++;
	}

	return;
}

/*--> SANDspSANNL: SAN display with newline */
nonstatic
void SANDspSANNL(mptrT mptr)
{
	SANDspSAN(mptr);
	SANDspNL();

	return;
}

/*--> SANDspFullMove: display full move */
nonstatic
void SANDspFullMove(siT n, mptrT mptr)
{
	sanT san;

	SANEncode(mptr, san);
	if(cv_c_cpv[mptr->m_frcp] == c_w) {
		(void) sprintf(stv, "%3hd. %s", n, san);
	} else {
		(void) sprintf(stv, "%3hd.  ...   %s", n, san);
	}
	SANDspSTVNL();

	return;
}

/*--> SANDspPath: display the current path */
nonstatic
void SANDspPath(void)
{
	siT i;

	SANDspStr("Path:");
	for(i = 0; i < an; i++) {
		SANDspChar(' ');
		if(av[i].a_g.g_curr != NULL) {
			SANDspSAN(av[i].a_g.g_curr);
		}
	}
	SANDspNL();


	return;
}

/*--> SANDspBoard: quick board display */
nonstatic
void SANDspBoard(void)
{
	rankT rank;
	fileT file;
	cpT cp;

	for(rank = rank_8; rank >= rank_1; rank--) {
		/* note that this is for a black text on a white backround display */

		for(file = file_a; file <= file_h; file++) {
			cp = gb.bm[rank][file];
			if(cp != cp_v0) {
				SANDspColorPiece(cp);
			} else
			if((rank % 2) == (file % 2)) {
				SANDspStr("::");
			} else {
				SANDspStr("  ");
			}
		}
		SANDspNL();
	}

	return;
}

void SANDspBoardDFH(void)
{
	rankT rank;
	fileT file;
	cpT cp;

	for(rank = rank_8; rank >= rank_1; rank--) {
		/* note that this is for a black text on a white backround display */

		for(file = file_a; file <= file_h; file++) {
			cp = gb.bm[rank][file];
			if(cp != cp_v0) {
				//SANDspColorPiece(cp);
				printf("%2.2x", cp);
			} else
			if((rank % 2) == (file % 2)) {
				SANDspStr("XX");
			} else {
				SANDspStr("  ");
			}
		}
		SANDspNL();
	}

	return;
}

/*--> SANDspOptions: active options display */
nonstatic
void SANDspOptions(void)
{
	optnT optn;
	siT col;

	SANDspLine("Active options:");
	col = 0;
	for(optn = 0; optn < optnL; optn++) {
		if(optnv[optn]) {
			if(col == 10) {
				SANDspNL();
				col = 0;
			}
			if(col > 0) {
				SANDspChar(' ');
			}
			SANDspStr(optnnamev[optn]);
			col++;
		}
	}
	if(col > 0) {
		SANDspNL();
	}
	SANDspNL();

	return;
}

/*--> SANDspStatus: quick status display */
nonstatic
void SANDspStatus(void)
{
	SANDspBoard();

	SANDspStrSI("actc: ", curr_e.e_actc);
	SANDspStrSI("   cast: ", curr_e.e_cast);
	SANDspStrSI("   epsq: ", curr_e.e_epsq);
	SANDspStrSI("   hmvc: ", curr_e.e_hmvc);
	SANDspStrSI("   fmvn: ", curr_e.e_fmvn);
	SANDspNL();

//SANDspStrSI("census: ", SANCensus());
//SANDspStrSI("  status: ", SANStatus());
	SANDspStrSI("  setup: ", setup);
	SANDspStrSI("  level: ", level);
	SANDspStrLI("  response time: ", (liT)(artsec));
	SANDspNL();

	SANDspOptions();

	return;
}

/*--> SANGateChar: gate a character output to a file */
nonstatic
void SANGateChar(FILEptrT fptr, char ch)
{
	if(fptr == stdout) {
		SANDspChar(ch);
	} else {
		(void) fputc(ch, fptr);
	}

	return;
}

/*--> SANGateStr: gate a string output to a file */
nonstatic
void SANGateStr(FILEptrT fptr, charptrT s)
{
	if(fptr == stdout) {
		SANDspStr(s);
	} else {
		(void) fputs(s, fptr);
	}

	return;
}

/*--> SANBadParmForm: issue "bad parameter form" diagnostic */
nonstatic
void SANBadParmForm(siT n)
{
	(void) sprintf(stv, "bad parameter form (%hd)", n);
	SANDspError(stv);

	return;
}

/*--> SANBadParmValue: issue "bad parameter value" diagnostic */
nonstatic
void SANBadParmValue(siT n)
{
	(void) sprintf(stv, "bad parameter value (%hd)", n);
	SANDspError(stv);

	return;
}

/*<<< sandsp.c: EOF */
