#include "book.h"
uMOVE          *s2move (char *);
/*
read in simple book format and stuff into hash
currently using built from 2750.pgn found at Dann's site
*/
void 
readbook (void)
{
	FILE           *book;
	char            line[1792];
	char            mov[8] = {0, 0, 0, 0, 0, 0, 0, 0};
	int             i, l = 0;
	uMOVE          *m;
	
	assert(ply == 1);
	assert(mhs == move_history);
	assert((mhs->threat & 1) == inCheck (stm));
	
	if ((book = fopen ("obook.txt", "r")) == NULL) {
		printf ("Cannot open book.txt \n");
		return;
	}
	while (fgets (line, 1790, book)) {
		++l;
		if (line[0] == '#')
			continue;
		i = 0;
		while ((line[i] >= 'a' && line[i] <= 'h') && ply < MAXPLY) {
			mov[0] = line[i];
			mov[1] = line[i + 1];
			mov[2] = line[i + 2];
			mov[3] = line[i + 3];
			m = s2move (mov);
			if (m->asint) {
				domove (m);
				mhs->threat = (mhs->threat & 14) | inCheck (stm);
				gen ();
				i += 5;
			} else {
				printf ("Bad move in book: %c%c%c%c\n", line[i], line[i + 1], line[i + 2], line[i + 3]);
				display ();
				printmoves ();
				printf ("\n");
				getchar ();
				break;
			}
		}
		while (mhs > move_history) {
			storeb (63, 0);
			unmove ();
		};
	};
	fclose (book);
}
