File: phalanx.h

package info (click to toggle)
phalanx 18-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 680 kB
  • ctags: 514
  • sloc: ansic: 6,408; makefile: 69; sh: 50
file content (510 lines) | stat: -rw-r--r-- 10,675 bytes parent folder | download
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
#ifndef PHALANX_INCLUDED
#define PHALANX_INCLUDED

#define VERSION "XVIII"

#ifdef GNUFUN
#include <getopt.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>

/*
 * COMPATIBILITY
 */

#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC CLK_TCK
#endif

/*
 * TYPES
 */

typedef unsigned char tsquare;

/*** Moves are generated into and executed from struct tmove ***/
typedef struct
{
  unsigned char
	from,		/* square */
	to,		/* destination square */
	special;	/* Castling or en passant capture, see moving.c */
  unsigned char
	in1,		/* moving piece */
	in2,		/* captured piece or 0 */
	in2a;		/* moving piece or promoted piece */
  signed short
	value;		/* heuristic value - the order key - before search */
			/* value returned - after search */
  short
	dch;		/* depth change -100...100 */
  short
	shift;		/* alphabeta window shift at root search */
} tmove;          /* 12 bytes */

typedef struct
{
  tmove
	m;
  unsigned
	hashboard;
  unsigned short
	rule50,		/* number of half-moves since last irreversible */
	castling:8,	/* 4 castling flags: */
			/*   white short, w. long, b. s., b. l. */
	check:8;
  short
	mtrl,		/* material count of side on move */
	xmtrl;		/* material count of opposite side */
} tgamenode;      /* 12+12 bytes */

typedef struct
{
  unsigned
	hashboard;
  short
	value,
	depth: 14;
  unsigned
	result: 2;
  unsigned short
	move,
	age;
} thashentry;     /* 12 bytes */

/* Positional features of one side */
typedef struct
{
  short hung;
  signed char khung, kshield;
  unsigned char p, n, b, r, q;   /* number of given pieces */
  unsigned char kp;              /* king position */
  unsigned char r7r;             /* # of rooks on 7-th row */
  signed char prune;             /* is it ok to prune search in this pos ? */
  signed char kstorm, qstorm;    /* diffside castling: king attack q/k side */
  unsigned char devel;           /* development needed?, 0 ... no */
  unsigned char castling;
  unsigned char bishopcolor,     /* square color of friendly bishops, or-red */
               xbishopcolor;     /* square color of enemy bishops */
  signed char worsebm;           /* mobility of the worse bishop */
} tknow;

typedef enum
	{ timecontrol, averagetime, fixedtime, fixeddepth }
tlevel;

typedef struct
{
  int	centiseconds;
  unsigned
	moves,            /* time levels */
	increment;
  int	depth;            /* fixed depth level */
  int
	noise;
  int resign;
  int
	bench;
  int
	machine_color,
	post,
	log,
	xboard,
	book,
	learn,
	cpu,           /* use cpu time */
	ponder,
	analyze,
	polling;       /* polling input */
  tlevel level;
  unsigned char easy;
} tflag;

typedef struct
{ unsigned char prev, next; }
tlist;

typedef struct
{ FILE *
	f;
  unsigned
	firstkey,
	lastkey;
  int
	filesize;
} tsbook;

typedef struct
{ FILE * f; int filesize; }
tpbook;

typedef struct
{ unsigned taxi, max, diag, min; }
tdist;



/*
 * DEFINITIONS
 */

#ifndef VERSION
# define VERSION "unknown"
#endif

/* use SAN for output */
#undef SANOUT

/* primary (text, small, user) book */
#ifndef PBOOK_FILE
#define PBOOK_FILE "pbook.phalanx"
#endif
#ifndef PBOOK_DIR
#define PBOOK_DIR "/usr/local/lib/phalanx"
#endif

/* secondary (binary, large, generated from pgn) book */
#ifndef SBOOK_FILE
#define SBOOK_FILE "sbook.phalanx"
#endif
#ifndef SBOOK_DIR
#define SBOOK_DIR "/usr/local/lib/phalanx"
#endif

#ifndef LEARN_FILE
#define LEARN_FILE "learn.phalanx"
#endif
#ifndef LEARN_DIR
#define LEARN_DIR "/var/local/lib/phalanx"
#endif

#define ENV_PBOOK "PHALANXPBOOKDIR"
#define ENV_SBOOK "PHALANXSBOOKDIR"
#define ENV_LEARN "PHALANXLEARNDIR"

#define max(A,B) ( A > B ? A : B )
#define min(A,B) ( A < B ? A : B )

#define NOVALUE 32123
#define CHECKMATE 30000
#define MAXPLY 40
#define MAXCOUNTER 1024 /* 512 moves */

#define HASH_COLOR ((unsigned)0xFDB97531)
#define no_cut     1
#define alpha_cut  2
#define beta_cut   3

/* Castling special move codes and flags */

#define SHORT_CASTLING 1
#define LONG_CASTLING 2

#define WSHORT 1 /* if set, white short castling is impossible */
#define WLONG  2
#define BSHORT 4
#define BLONG  8

/* Colors / sides */

#define WHITE 1
#define BLACK 2

#define empty(X) (((X)&3)==0)
#define white(X) (((X)&3)==WHITE)
#define black(X) (((X)&3)==BLACK)
#define color(X) ((X)&3)
#define enemy(X) ((X)^3)

/* Values */

#define P_VALUE  100
#define N_VALUE  350
#define B_VALUE  350
#define R_VALUE  550
#define Q_VALUE 1050

/* Pieces */

#define piece(X) ((X)&0xFC)

#define PAWN   0x10
#define KNIGHT 0x20
#define BISHOP 0x30
#define ROOK   0x40
#define QUEEN  0x50
#define KING   0x60

#define WP (WHITE|PAWN)
#define WN (WHITE|KNIGHT)
#define WB (WHITE|BISHOP)
#define WR (WHITE|ROOK)
#define WQ (WHITE|QUEEN)
#define WK (WHITE|KING)

#define BP (BLACK|PAWN)
#define BN (BLACK|KNIGHT)
#define BB (BLACK|BISHOP)
#define BR (BLACK|ROOK)
#define BQ (BLACK|QUEEN)
#define BK (BLACK|KING)

/*** Bit masks for power tables ***/

#define WPM 0x0001
#define WNM 0x0002
#define WBM 0x0004
#define WRM 0x0008
#define WQM 0x0010
#define WWW 0x0020

#define BPM 0x0100
#define BNM 0x0200
#define BBM 0x0400
#define BRM 0x0800
#define BQM 0x1000
#define BBB 0x2000

/* Files */

#define FILE_A 1
#define FILE_B 2
#define FILE_C 3
#define FILE_D 4
#define FILE_E 5
#define FILE_F 6
#define FILE_G 7
#define FILE_H 8

/* Squares */

#define A1 21
#define B1 22
#define C1 23
#define D1 24
#define E1 25
#define F1 26
#define G1 27
#define H1 28

#define A2 31
#define B2 32
#define C2 33
#define D2 34
#define E2 35
#define F2 36
#define G2 37
#define H2 38

#define A3 41
#define B3 42
#define C3 43
#define D3 44
#define E3 45
#define F3 46
#define G3 47
#define H3 48

#define A4 51
#define B4 52
#define C4 53
#define D4 54
#define E4 55
#define F4 56
#define G4 57
#define H4 58

#define A5 61
#define B5 62
#define C5 63
#define D5 64
#define E5 65
#define F5 66
#define G5 67
#define H5 68

#define A6 71
#define B6 72
#define C6 73
#define D6 74
#define E6 75
#define F6 76
#define G6 77
#define H6 78

#define A7 81
#define B7 82
#define C7 83
#define D7 84
#define E7 85
#define F7 86
#define G7 87
#define H7 88

#define A8 91
#define B8 92
#define C8 93
#define D8 94
#define E8 95
#define F8 96
#define G8 97
#define H8 98
#define H9 99 /* a bumper: we are out of board */



/*
 * VARIABLES
 */

extern tflag Flag;                    /* Misc. settings, see typedef of tflag */

extern tsquare B[120];         /* The chessboard */
extern tlist L[120];                  /* List of pieces */
#define WKP (L[1].next)               /* White king position */
#define BKP (L[2].next)               /* Black king position */

extern signed char * Th;              /* horizontal axe transf., (H8<->H1) */
extern signed char * Tv;              /* vertical axe tr., (H8<->A8) */

extern tdist dist[120*120];
extern tknow Wknow, Bknow;            /* Things i know about actual position */
extern int Color;                /* c. to move: 1 ... white, 2 ... black */
extern int LastIter;                /* result of last iteration */
extern int Depth;                   /* depth to be searched, in % of ply */
extern int Ply;
extern int FollowPV;
extern int Totmat;                  /* total mtrl sum on the board */
extern int Abort, NoAbort;          /* abort the search when SIG_INT */
extern long AllDepth;                 /* Statistics for average Depth */
extern long AllNPS;

extern int DrawScore;
#define DRAW ( Ply%2 ? -DrawScore : DrawScore )

extern tgamenode G[MAXCOUNTER];
extern int Counter;                 /* game counter, points to G[] */

extern long long Nodes;           /* statistics */
extern int Scoring;
extern int EasyMove;

extern long Otim;                     /* remaining opponent's time */

/* move generation tables */
extern int N_moves[8], RB_dirs[8];
#define K_moves RB_dirs

extern int Values[7];            /* piece values */
extern unsigned short P[120];      /* power table */

extern tmove PV[MAXPLY][MAXPLY];   /* principal variantion */
extern tmove Pondermove;

/* Hashing codes */
extern unsigned H[12][64];
extern thashentry *HT;             /* Hash table itself */
extern int * HS;                 /* square trans-table */
extern int HP[];                 /* piece trans-table */
extern unsigned SizeHT;            /* Size of the hash table */
extern unsigned Age;

/* Opening book */
extern tpbook Pbook;               /* primary book flags */
extern tpbook Learn;
extern tsbook Sbook;               /* secondary book flags */
extern int Bookout;

/* params that cannot be pushed thru SIGALM handler, */
/* must be pushed thru global variables */
extern int A_n, A_i, A_d;
extern tmove * A_m;



/*
 * FUNCTIONS
 */

extern int bcreate(int,char**);

extern void new_game(void);

/* input/output */
extern void printm(tmove,char*);
extern void printmSAN( tmove*, int, int, char* );
extern void printPV(int,int,char*);
extern void infoline(int,char*);
extern void verboseline(tmove*,int,int);
extern void printboard(void);
extern int setfen(char*);
extern void shell(void);
extern int command(void);
extern tmove * sandex(char*,tmove*,int);

/* signal handlers */
extern void interrupt(int);

extern int attacktest(int,int);
#define checktest(side) attacktest( (side)==WHITE ? WKP : BKP, enemy(side))

extern void generate_legal_moves(tmove*,int*,int);
extern void generate_legal_captures(tmove*,int*,int);
extern void generate_legal_checks(tmove*,int*);
extern int see( tsquare *, int, int );

extern int search(tmove*,int,int,int);
extern int csearch(tmove*,int,int,int,int);
extern tmove root_search(void);

extern int repetition(int);
extern int material_draw(void);
extern int evaluate(int,int);

extern int score_position(void);

/* endgame knowledge */
extern int pawns(void);
extern int e_nb(int);           /* endgame: knight and bishop */
extern int e_mp(void);            /*          minor piece and pawn */
extern int e_rpr(void);           /*          R+P vs. R */

extern void do_move( tmove * );
extern void undo_move( tmove * );

extern unsigned hashboard(void);
extern thashentry * seekHT(void);
extern void writeHT(int,int,int);

/* levels */
extern void l_level(char *);
extern void l_startsearch(void);
extern int l_iterate(void);
extern long ptime(void);
extern long LastTurn;

/* book */
extern int bookmove(tmove*,int);
extern unsigned smove(tmove*);

/* learning feature */
extern int rlearn(void);
extern void wlearn(int,int);

/* killer move utils */
extern void init_killers(void);
extern void write_killer( int, int );
extern void add_killer( tmove *, int, thashentry * );

#endif