File: book.c

package info (click to toggle)
phalanx 22%2Bd051004-14
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,444 kB
  • ctags: 973
  • sloc: ansic: 11,916; sh: 80; makefile: 44
file content (520 lines) | stat: -rw-r--r-- 11,335 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include "phalanx.h"

#undef SHOWREADS
#undef SHOWDUPS

tsbook Sbook;
tpbook Pbook;

int Bookout=0;

/*
 *	myfwrite(), myfread() to fix the endianess problem.
 */

#	define ISI ((int)sizeof(int))

void myfwrite( void *ptr, int size, FILE *stream )
{
	int i,j;
	for( i=0; i<size-ISI; i+=ISI )
	{ for( j=0; j!=ISI; j++ ) fwrite( (char*)ptr+i+j, 1, 1, stream ); }
	for( j=0; i+j<size; j++ ) fwrite( (char*)ptr+i+j, 1, 1, stream );
}

void myfread(  void *ptr, int size, FILE *stream )
{
	int i,j;
	for( i=0; i<size-ISI; i+=ISI )
	{ for( j=0; j!=ISI; j++ ) fread( (char*)ptr+i+j, 1, 1, stream ); }
	for( j=0; i+j<size; j++ ) fread( (char*)ptr+i+j, 1, 1, stream );
}

/*
 *	write position into string s, to be saved in book or
 *	compared with another position string.
 *	Format: pieces as usual, black pieces by capital P,N,B,...
 *	empty squares are stored as numbers, e.g. '24' means
 *	24 empty sq.
 */
void postr(char *s)
{
	int i, counter, lenght;
	const char P[14]=
	{'-','-','p','P','n','N','b','B','r','R','q','Q','k','K'};

	sprintf( s, "%1X%1X", (Color==BLACK), G[Counter].castling & 0xF );

	counter=0; lenght=2;
	for(i=A1;i!=H9;i++) if( B[i] && B[i] != 3 )
	{
		if(counter)
		{
			sprintf(&s[lenght],"%i",counter);
			if(counter>9) lenght++;
			lenght++; counter=0;
		}
		sprintf(&s[lenght],"%c",P[B[i]/8+(color(B[i])==BLACK)]);
		lenght++;
	}
	else if( B[i]==0 ) counter++;

	if(counter) sprintf(&s[lenght],"%i ",counter);
	else sprintf(&s[lenght]," ");
}




/*
 *	Compare position strings p1 and p2.
 *	p1==p2 ... returns 0
 *	p1>p2 ... 1; p1<p2 ... -1
 */
int poscmp(char *p1, char *p2)
{
	register int i;
	for(i=0; i!=80; i++)
	{
		if(p1[i]=='\0') p1[i]=' ';
		if(p2[i]=='\0') p2[i]=' ';

		if(p1[i]<p2[i]) return -1;
		if(p1[i]>p2[i]) return  1;
		if(p1[i]==' ') return 0;
	}
	fprintf(stderr,"Phalanx error: Opening book corrupted.\n");
	exit(2);
}



/*
 *	Now the book searching functions.  If there is less than
 *	512 bytes to search, the book is searched linearly.
 */
int linSB(FILE *f, long from, long to, char *p, char *s)
{
	if( fseek(f,from,SEEK_SET)!=0 ) { return -1; }
	if(!feof(f)) fgets(s,80,f);	/* skip to the first \n */

	while(ftell(f)<=to && !feof(f))
	{
		fgets(s,100,f);
		if( poscmp(p,s)==0 ) return 0;
	}

	return -1;
}



/*
 *	There is more than 512 bytes to search -> use binary search.
 */
int binSB(FILE *f, long from, long to, char *p, char *s)
{
	long mid;

	if( to-from < 512 ) return( linSB(f,from,to,p,s) );

	mid=(to+from)/2;
	if( fseek(f,mid,SEEK_SET)!=0 ) return -1;
	if(!feof(f)) fgets(s,80,f);	/* skip to the first \n */

	if(!feof(f)) fgets(s,100,f);

	switch(poscmp(p,s))
	{
		case  0: return(0);
		case  1: return( binSB(f,mid,to,p,s) );
		case -1: return( binSB(f,from,mid,p,s) );
		default: return -1;
	}
}



int parsemove( char *inp, tmove *m, int n )
{
	int special, from, to, in2a, i;

	special = 0;
	from = inp[0]-'a'+1 + 10*(inp[1]-'1') + 20;
	to =   inp[2]-'a'+1 + 10*(inp[3]-'1') + 20;
	in2a = 0;

	if(    strncmp(inp,"o-o-o",5)==0
	    || strncmp(inp,"0-0-0",5)==0
	    || strncmp(inp,"O-O-O",5)==0
	    || ( B[E1]==WK && strncmp(inp,"e1c1",4)==0 )
	    || ( B[E8]==BK && strncmp(inp,"e8c8",4)==0 ) )
		special = LONG_CASTLING;
	else if(    strncmp(inp,"o-o",3)==0
	         || strncmp(inp,"0-0",3)==0
	         || strncmp(inp,"O-O",3)==0
	         || ( B[E1]==WK && strncmp(inp,"e1g1",4)==0 )
	         || ( B[E8]==BK && strncmp(inp,"e8g8",4)==0 ) )
		special = SHORT_CASTLING;

	switch(inp[4])
	{
	case 'Q': case 'q': in2a=QUEEN+Color; break;
	case 'R': case 'r': in2a=ROOK+Color; break;
	case 'B': case 'b': in2a=BISHOP+Color; break;
	case 'N': case 'n': in2a=KNIGHT+Color; break;
	default : in2a=B[from];
	}

	for(i=0; i!=n; i++)
	{
	if( ( special>0 && special<A1 && m[i].special == special ) ||
	    ( (m[i].from==from) && (m[i].to==to) && (m[i].in2a==in2a) )
	  ) return(i);
	}

	return(-1);
}



/* binary book */
int sbookmoves( int *moves, int *values, tmove *m, int n )
{
	int index, i;
	int64 first, last=0, middle=0;
	unsigned fkey, lkey;
	unsigned pos;
	long booksize; 	/* filesize in bytes */
	FILE *f;
	int hits=0;

	if( (f=Sbook.f) == NULL ) return -1;

	if( G[Counter].hashboard < Sbook.firstkey
	 || G[Counter].hashboard > Sbook.lastkey ) return -1;

	fkey = Sbook.firstkey; lkey = Sbook.lastkey;

	booksize = Sbook.filesize/6;

	first = 0; last = booksize-1;
	if( G[Counter].hashboard == Sbook.firstkey ) middle=0;
	else if( G[Counter].hashboard == Sbook.lastkey ) middle=booksize-1;
	else
	while( first < last )
	{
		/* middle = (first+last) / 2; */

		middle = (G[Counter].hashboard-fkey)*(last-first)
		       / (lkey-fkey) + first;

		if(middle==first) middle++; if(middle==last) middle--;
		if(middle==first)
		{
#ifdef SHOWREADS
			printf("notfoundinbook %i\n", hits );
#endif
			return -1;
		}

		if( fseek(f,middle*6,SEEK_SET)!=0 ) return -1;
		myfread( &pos, sizeof(unsigned), f ); hits++;

		if( pos < G[Counter].hashboard ) { first=middle; fkey=pos; }
		else if( pos == G[Counter].hashboard ) break;
		else { last=middle; lkey=pos; }
	}

/*
printf("[%i %i %i %i] [%08X %08X]\n",
        first,middle,last,booksize,pos,G[Counter].hashboard);
*/

#ifdef SHOWREADS
	printf("yesfoundinbook %i\n",hits);
#endif

	if( middle == 0 ) pos = G[Counter].hashboard;
	else
	do
	{
		middle --;
		if( fseek(f,middle*6,SEEK_SET)!=0 ) return -1;
		myfread( &pos, sizeof(unsigned), f );
	} while( middle>0 && pos==G[Counter].hashboard );

	if(pos!=G[Counter].hashboard)
	{ middle ++; pos = G[Counter].hashboard; }

	/*** Position found in book! ***/
	index=0;
	if( fseek(f,middle*6+4,SEEK_SET)!=0 ) return -1;
	while( middle<booksize && pos==G[Counter].hashboard )
	{
		unsigned short sm;

		myfread( &sm, sizeof(unsigned short), f );
		for( i=0; i!=n; i++ ) if( sm == smove(m+i) )
		{ moves[index]=i; values[index]=100; index++; }

		myfread( &pos, sizeof(unsigned), f );
	}

	return index;
}



/*
 *	bookmoves() returns number of moves found in book, or 0,
 *	if no move is found.  moves[0],...,moves[index-1] are indexes
 *	to the bookmoves in m[], n is numer of moves.  Moves must
 *	be generated before.
 */
int bookmoves( int *moves, int *values, tmove *m, int n )
{
	char s[256], p[256];
	int index, i;
	long booksize; 	/* filesize in bytes */
	FILE *f;

	if( (f=Pbook.f) == NULL ) return -1;

	booksize = Pbook.filesize;

	postr(p);	/* Current position to string */
	if( binSB(f,0,booksize,p,s) == -1 )
	{ return -1; }

	/*** Position found in book! ***/
	index=0;
	for(i=0; s[i]!='\n'; i++)
	{
		int pm;
		while(s[i]!=' ' && s[i]!='\n')
		{
			if(s[i]=='!' && index) values[index-1]+=100;
			i++;
		}
		if(s[i]=='\n') break; i++; if(s[i]=='\n') break;
		if( (pm=parsemove(&s[i],m,n)) != -1 )
		{ moves[index]=pm; values[index]=100; index++; }
	}

	return index;
}



/*
 *	This function returns index of a random move found in book
 *	or -1, if no move was found.  Moves must be generated.
 */
int bookmove( tmove *m, int n )
{
	int moves[80], values[80], index;
	int foundtxt=0;

	index = bookmoves(moves,values,m,n);
	if( index <= 0 ) index = sbookmoves(moves,values,m,n);
	else
	{
#ifdef SHOWDUPS
		int moves2[80]; int values2[80], index2;
		index2 = sbookmoves(moves2,values,m,n);
		if( index2>=1 && index>=1 && Counter>8 )
		{
			char p[256]; int i;
			postr(p);
			puts("found in both sbook and pbook");
			printboard(NULL);
			puts(p);
			printf("   0      0       0      0  book2 ");
			for( i=0; i!=index2; i++ )
			if( i==0 || moves2[i-1]!=moves2[i] )
			{ printm( m[moves2[i]], NULL ); }
			puts("");
		}
#endif
		foundtxt=1;
	}

	if( index > 0 )
	{
		int ii, sumvalues=0, rn;
		for( ii=0; ii!=index; ii++ )
		{ sumvalues += values[ii]; }

		rn = rand()%sumvalues;

		sumvalues = 0;
		for( ii=0; ii!=index; ii++ )
		{ sumvalues += values[ii]; if( sumvalues >= rn ) break; }

		if( Flag.post )
		{	int i;
			char s[128];
			if( Flag.xboard )
			sprintf(s,"   0      0       0      0  book");
			else
			sprintf(s,"Book moves ");
			if( foundtxt ) sprintf(s+strlen(s),"1 ");
			else sprintf(s+strlen(s),"2 ");
			for( i=0; i!=index; i++ )
			{ printm( m[moves[i]], s+strlen(s) ); }
			sprintf(s+strlen(s),"\n");
			puts(s);
			if( Flag.log!=NULL && Flag.ponder<2 )
			{
				char sm[64];
				if(Flag.xboard) fputs(s+26,Flag.log);
				else            fputs(s,Flag.log);
				fputs("  selected move ",Flag.log);
				printm( m[moves[ii]], sm );
				fputs(sm,Flag.log);
				fputs("\n",Flag.log);
			}
		}
		return ( moves[ii] );
	}

	return( -1 );
}



/*
 *	Print all book moves (command bk)
 */
FILE * Eco = NULL;
void bk( tmove *m, int n )
{
int moves[80], values[80], idx;
int pass;
int sumvalues=0;

#define SHOWECO
#ifdef SHOWECO
static int att=1;
static int seco=0;
if( att != 0 && Eco!=NULL )
{
	typedef struct teco { unsigned hashboard; unsigned point; } teco;
	static struct teco * peco = NULL;
	if( att==1 && Counter==0 && G[Counter].hashboard==0x39512910 /*init*/)
	{
		int c; tmove * move;
		peco = malloc(2048*sizeof(teco));
		if( peco == NULL )
		{ puts(" telluser cannot alloc memory for ECO");
		  att=0; goto abort; }

		printf("telluser creating ECO index, please wait\n");

		while( (c=fgetc(Eco))!='[' ) if( c==EOF ) goto doneinit;
		for(;;)
		{
		  tmove m[128];
		  char ms[32]; int msi; int n;
		  (peco+seco)->point=ftell(Eco)-1;
		  while( (c=getc(Eco))!=']' ) if( c==EOF ) goto doneinit;
		  if( c==EOF ) goto doneinit;
		  setfen("rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR/w");
		  for(;;)
		  {
		 	msi=0;
		  	while( (c=getc(Eco))==' ' || c=='\n' ) {}
			ungetc(c,Eco);
			while( (c=getc(Eco))!=' ' && c!='\n' && msi<=30 )
			{ ms[msi]=c; msi++;
			  if( c==EOF || c=='[' )
			  { ungetc(c,Eco); goto nextgame; }
			}
			ms[msi]='\0'; /* printf("%s ",ms); */
			generate_legal_moves( m, &n, checktest(Color) );
			move=sandex(ms,m,n);
			if( move==NULL ) goto nextgame;
			do_move(move);
		  	if( c==EOF ) goto doneinit;
		  }
		  nextgame:;
		  peco[seco].hashboard=G[Counter].hashboard;
		  seco++;
		  while( (c=fgetc(Eco))!='[' ) if( c==EOF ) goto doneinit;
		}

		doneinit:;
		printf(" parsed %i ECO records\n",seco);
		att=2;
		setfen("rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR/w");
	}

	if( Counter>0 && att>1 )
	{
		int counter=Counter;
		int text=-1;
		while( Counter > 0 )
		{
			int i;
			for(i=0;i!=seco;i++)
			if(peco[i].hashboard==G[Counter].hashboard)
			{ text=peco[i].point; goto foundeco; }
			undo_move( & G[Counter-1].m );
		}
		foundeco:;
		while( counter > Counter ) do_move( & G[Counter].m );
		if( text==-1 ) printf(" no eco found\n");
		else
		{ char t[128], * c;
		  fseek(Eco,text,SEEK_SET);
		  fgets(t,126,Eco);
		  t[127]='\0';
		  c=index(t,'['); if(c!=NULL) *c = ' ';
		  c=rindex(t,']'); if(c!=NULL) *c = '\0';
		  puts(t);
		}
	}
	abort:;
}
#endif

for( pass=0; pass!=2; pass++ )
{
	if( pass==0 )
	{
	int i;
	idx = bookmoves(moves,values,m,n);
	if( idx != -1 ) for(i=0;i!=idx;i++) sumvalues += values[i];
	printf(" primary book moves\n");
	}
	else
	{
	idx = sbookmoves(moves,values,m,n);
	printf(" secondary book moves\n");
	}

	if( idx > 0 )
	{	int i;
		for( i=0; i!=idx; i++ )
		if( i==0 || moves[i-1]!=moves[i] )
		{
			printf("   ");
			printm( m[moves[i]], NULL );
			if(pass==0)
				printf("%3d%%\n",values[i]*100/sumvalues);
			else	printf("\n");
		}
	}
	else
	printf("   no move found\n");

}

if( Flag.xboard>1 ) puts("");

{ char c[128]; postr(c); printf("%s\n",c); }

}