~ubuntu-branches/ubuntu/precise/gcompris/precise

« back to all changes in this revision

Viewing changes to src/chess_computer-activity/gnuchess/common.h

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2010-06-27 22:51:30 UTC
  • mfrom: (1.1.16 upstream) (5.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100627225130-mf7h4m5r8m7bd9fb
Tags: 9.3-1
* New upstream release.
* Drop GTK_DISABLE_DEPRECATED patch, useless for now.
* Provide RELEASE_NOTE_9.3.txt downloaded from sourceforge.
* New voice package for Asturian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GNU Chess 5.0 - common.h - common symbolic definitions
 
2
   Copyright (c) 1999-2002 Free Software Foundation, Inc.
 
3
 
 
4
   GNU Chess is based on the two research programs 
 
5
   Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft.
 
6
 
 
7
   GNU Chess is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 2, or (at your option)
 
10
   any later version.
 
11
 
 
12
   GNU Chess is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with GNU Chess; see the file COPYING.  If not, write to
 
19
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
   Boston, MA 02111-1307, USA.
 
21
 
 
22
   Contact Info: 
 
23
     bug-gnu-chess@gnu.org
 
24
     cracraft@ai.mit.edu, cracraft@stanfordalumni.org, cracraft@earthlink.net
 
25
*/
 
26
 
 
27
#ifndef COMMON_H
 
28
#define COMMON_H
 
29
 
 
30
#include <config.h>
 
31
 
 
32
#ifndef __GNUC__
 
33
# define __attribute__(x)
 
34
#endif
 
35
 
 
36
/*
 
37
 * Include "uint64_t" and similar types using the ac_need_stdint_h ac macro
 
38
 */
 
39
 
 
40
#include <stdint.h>
 
41
#include <stdio.h>
 
42
#include <sys/types.h>
 
43
 
 
44
 /* 
 
45
  * Define time structures to get timeval for Timer 
 
46
  */
 
47
 
 
48
#include <sys/time.h>
 
49
 
 
50
 /*
 
51
  * Define macro for declaring 64bit constants for compilers not using ULL
 
52
  */
 
53
 
 
54
#ifdef _MSC_VER
 
55
   #define ULL(x) ((uint64_t)(x))
 
56
#else
 
57
   #define ULL(x) x ## ULL
 
58
#endif
 
59
 
 
60
/*
 
61
 * BitBoard is a key data type.  It is a 64-bit value, in which each
 
62
 * bit represents a square on the board as defined by "enum Square".
 
63
 * Thus, bit position 0 represents a fact about square a1, and bit
 
64
 * position 63 represents a fact about square h8.  For example, the
 
65
 * bitboard representing "white rook" positions will have a bit set
 
66
 * for every position occupied on the board by a white rook.
 
67
 */
 
68
 
 
69
typedef uint64_t BitBoard;
 
70
typedef uint64_t HashType;
 
71
typedef uint32_t KeyType;
 
72
 
 
73
/*
 
74
 * Board represents an entire board's state, and is structured to
 
75
 * simplify analysis:
 
76
 */
 
77
 
 
78
typedef struct 
 
79
{
 
80
   BitBoard b[2][7];      /* piece/pawn positions by side (0=white, 1=black)
 
81
                             and then by piece (1=pawn..6=king). For example,
 
82
                             b[white][knight] has bits set for every board
 
83
                             position occupied by a White Knight. */
 
84
   BitBoard friends[2];   /* Friendly (this side's) pieces */
 
85
   BitBoard blocker;
 
86
   BitBoard blockerr90;   /* rotated 90 degrees */
 
87
   BitBoard blockerr45;   /* rotated 45 degrees */
 
88
   BitBoard blockerr315;  /* rotated 315 degrees */
 
89
   short ep;              /* Location of en passant square */
 
90
   short flag;            /* Flags related to castle privileges */
 
91
   short side;            /* Color of side on move: 0=white, 1=black */
 
92
   short material[2];     /* Total material by side not inc. king */
 
93
   short pmaterial[2];    /* Total pawn material by side not inc. king */
 
94
   short castled[2];      /* True (1) if side is castled */
 
95
   short king[2];         /* Location of king 0 - a1 .. 63 - h8 */
 
96
} Board; 
 
97
 
 
98
/* leaf describes a leaf-level analysis result */
 
99
 
 
100
typedef struct
 
101
{
 
102
   int move;   /* the move that produced this particular board */
 
103
   int score;  /* the scored value of this leaf */
 
104
} leaf;
 
105
 
 
106
/*
 
107
 * GameRec records an individual move made in the game; an entire
 
108
 * Game is a set of GameRec's:
 
109
 */
 
110
 
 
111
#define SANSZ 8 /* longest move is "exf8=Q+" so 7+1 */
 
112
 
 
113
typedef struct
 
114
{
 
115
   int move;    /* The actual move made; this is NOT the move count! */
 
116
   short epsq;  /* en passant square */
 
117
   short bflag; /* Flags for castle privs, see Board.flag */
 
118
   short Game50; /* The last value of GameCnt (a count of half-moves)
 
119
                    that castled, captured, or moved a pawn */
 
120
   short mvboard;
 
121
   float et;     /* elapsed time */
 
122
   HashType hashkey;
 
123
   HashType phashkey;
 
124
   char SANmv[SANSZ];  /* The move in SAN notation */
 
125
   char *comments;
 
126
} GameRec;
 
127
 
 
128
typedef struct
 
129
{
 
130
   HashType key;    /* Full 64 bit hash */
 
131
   int move;        /* Best move */
 
132
   int score;       /* Estimated score, may be lower or upper bound */
 
133
   uint8_t flag;    /* Is this alpha, beta, quiescent or exact? */
 
134
   uint8_t depth;   /* Search depth */
 
135
} HashSlot;   
 
136
 
 
137
typedef struct
 
138
{
 
139
   KeyType pkey;  
 
140
   BitBoard passed;
 
141
   BitBoard weaked;
 
142
   int score;
 
143
   int phase;
 
144
} PawnSlot;
 
145
 
 
146
 
 
147
/*  MACRO definitions */
 
148
 
 
149
#ifndef MAX
 
150
#define MAX(a,b)     ((a) > (b) ? (a) : (b))
 
151
#endif
 
152
#ifndef MIN
 
153
#define MIN(a,b)     ((a) < (b) ? (a) : (b))
 
154
#endif
 
155
#define SET(a,b)                     \
 
156
  do {                               \
 
157
    (a) |= (b);                      \
 
158
    dbg_printf("Set   0x%x\n", (b)); \
 
159
  } while (0)
 
160
#define CLEAR(a,b)                   \
 
161
  do {                               \
 
162
    (a) &= ~(b);                     \
 
163
    dbg_printf("Clear 0x%x\n", (b)); \
 
164
  } while (0)
 
165
 
 
166
/* Draw score can be used to penalise draws if desired */
 
167
#define DRAWSCORE   0 
 
168
#define MATERIAL     (board.material[side] - board.material[1^side])
 
169
#define PHASE        (8 - (board.material[white]+board.material[black]) / 1150)
 
170
#define KEY(a)       (a >> 32) 
 
171
 
 
172
/*  Attack MACROS */
 
173
 
 
174
#define BishopAttack(sq) \
 
175
        (Bishop45Atak[sq][(board.blockerr45 >> Shift45[sq]) & Mask45[sq]] | \
 
176
         Bishop315Atak[sq][(board.blockerr315 >> Shift315[sq]) & Mask315[sq]])
 
177
#define RookAttack(sq)  \
 
178
        (Rook00Atak[sq][(board.blocker >> Shift00[sq]) & 0xFF] | \
 
179
         Rook90Atak[sq][(board.blockerr90 >> Shift90[sq]) & 0xFF])
 
180
#define QueenAttack(sq) \
 
181
        (BishopAttack(sq) | RookAttack(sq))
 
182
 
 
183
 
 
184
/*  Some bit macros  */
 
185
 
 
186
/*
 
187
 * gcc 2.95.4 completely screws up the macros with lookup tables 
 
188
 * with -O2 on PPC, maybe this check has to be refined. (I don't know
 
189
 * whether other architectures also suffer from this gcc bug.) However,
 
190
 * with gcc 3.0, the lookup tables are _much_ faster than this direct
 
191
 * calculation.
 
192
 */
 
193
#if defined(__GNUC__) && defined(__PPC__) && __GNUC__ < 3
 
194
#  define SETBIT(b,i)   ((b) |=  ((ULL(1)<<63)>>(i)))
 
195
#  define CLEARBIT(b,i) ((b) &= ~((ULL(1)<<63)>>(i)))
 
196
#else
 
197
#  define SETBIT(b,i)   ((b) |= BitPosArray[i])
 
198
#  define CLEARBIT(b,i) ((b) &= NotBitPosArray[i])
 
199
#endif
 
200
 
 
201
#define RANK(i) ((i) >> 3)
 
202
#define ROW(i) ((i) & 7)
 
203
#define trailz(b) (leadz ((b) & ((~b) + 1)))
 
204
 
 
205
/* Move Descriptions (moves) are represented internally as integers.
 
206
 * The lowest 6 bits define the destination ("TO") square, and
 
207
 * the next lowest 6 bits define the source ("FROM") square,
 
208
 * using the values defined by "enum Square" (0=a1, 63=h8).
 
209
 * Upper bits are used to identify other move information such as
 
210
 * a promotion (and to what), a capture (and of what),
 
211
 * CASTLING moves, and ENPASSANT moves; see the "constants for
 
212
 * move description" below for more information on these upper bits.
 
213
 */
 
214
#define PROMOTEPIECE(a) ((a >> 12) & 0x0007)
 
215
#define CAPTUREPIECE(a) ((a >> 15) & 0x0007)
 
216
#define TOSQ(a)         ((a) & 0x003F)
 
217
#define FROMSQ(a)       ((a >> 6) & 0x003F)
 
218
#define MOVE(a,b)       (((a) << 6) | (b))
 
219
 
 
220
/* constants for move description */
 
221
#define KNIGHTPRM     0x00002000
 
222
#define BISHOPPRM     0x00003000 
 
223
#define ROOKPRM       0x00004000
 
224
#define QUEENPRM      0x00005000
 
225
#define PROMOTION     0x00007000
 
226
#define PAWNCAP       0x00008000
 
227
#define KNIGHTCAP     0x00010000 
 
228
#define BISHOPCAP     0x00018000
 
229
#define ROOKCAP       0x00020000 
 
230
#define QUEENCAP      0x00028000 
 
231
#define CAPTURE       0x00038000 
 
232
#define NULLMOVE      0x00100000 
 
233
#define CASTLING      0x00200000 
 
234
#define ENPASSANT     0x00400000
 
235
#define MOVEMASK      (CASTLING | ENPASSANT | PROMOTION | 0x0FFF)
 
236
 
 
237
#define white  0
 
238
#define black  1
 
239
#define false  0
 
240
#define true   1
 
241
#define ks 0
 
242
#define qs 1
 
243
#define INFINITY  32767
 
244
#define MATE      32767
 
245
#define MATESCORE(a)    ((a) > MATE-255  || (a) < -MATE+255)
 
246
 
 
247
/* constants for Board */
 
248
#define WKINGCASTLE   0x0001
 
249
#define WQUEENCASTLE  0x0002
 
250
#define BKINGCASTLE   0x0004
 
251
#define BQUEENCASTLE  0x0008
 
252
#define WCASTLE       (WKINGCASTLE | WQUEENCASTLE)
 
253
#define BCASTLE       (BKINGCASTLE | BQUEENCASTLE)
 
254
 
 
255
/* Material values */
 
256
#define ValueP   100    
 
257
#define ValueN   350
 
258
#define ValueB   350
 
259
#define ValueR   550
 
260
#define ValueQ   1100
 
261
#define ValueK   2000
 
262
 
 
263
 
 
264
/*  Some special BitBoards  */
 
265
#define NULLBITBOARD  ( ULL(0x0000000000000000))
 
266
#define WHITESQUARES  ( ULL(0x55AA55AA55AA55AA))
 
267
#define BLACKSQUARES  ( ULL(0xAA55AA55AA55AA55))
 
268
#define CENTRESQUARES ( ULL(0x0000001818000000))
 
269
#define COMPUTERHALF  ( ULL(0xFFFFFFFF00000000))
 
270
#define OPPONENTHALF  ( ULL(0x00000000FFFFFFFF))
 
271
 
 
272
/*  Game flags */
 
273
#define QUIT    0x0001
 
274
#define TESTT   0x0002
 
275
#define THINK   0x0004
 
276
#define MANUAL  0x0008
 
277
#define TIMEOUT 0x0010
 
278
#define SPARE1  0x0020
 
279
#define ENDED   0x0040
 
280
#define USEHASH 0x0080
 
281
#define SOLVE   0x0100
 
282
#define USENULL 0x0200
 
283
#define XBOARD  0x0400
 
284
#define TIMECTL 0x0800
 
285
#define POST    0x1000
 
286
#define PONDER  0x2000 /* We are in pondering (during search) */
 
287
#define HARD    0x4000 /* Pondering is turned on */
 
288
#define ANALYZE 0x8000 /* In ANALYZE mode */
 
289
 
 
290
/*  Node types  */ 
 
291
#define PV  0
 
292
#define ALL 1
 
293
#define CUT 2
 
294
 
 
295
/*  Transposition table flags  */
 
296
#define EXACTSCORE  1
 
297
#define LOWERBOUND  2
 
298
#define UPPERBOUND  3
 
299
#define POORDRAFT   4
 
300
#define QUIESCENT   5
 
301
#define STICKY      8
 
302
 
 
303
/*  Book modes */
 
304
#define BOOKOFF 0
 
305
#define BOOKRAND 1
 
306
#define BOOKBEST 2
 
307
#define BOOKWORST 3
 
308
#define BOOKPREFER 4
 
309
 
 
310
/*  The various phases during move selection  */
 
311
#define PICKHASH    1
 
312
#define PICKGEN1    2
 
313
#define PICKCAPT    3
 
314
#define PICKKILL1   4
 
315
#define PICKKILL2   5
 
316
#define PICKGEN2    6
 
317
#define PICKHIST    7
 
318
#define PICKREST    8
 
319
#define PICKCOUNTER 9
 
320
 
 
321
#define MAXTREEDEPTH  2000
 
322
#define MAXPLYDEPTH   65
 
323
#define MAXGAMEDEPTH  600
 
324
 
 
325
/* 
 
326
   Smaller HASHSLOT defaults 20011017 to improve blitz play
 
327
   and make it easier to run on old machines
 
328
*/
 
329
#define HASHSLOTS 1024 
 
330
#define PAWNSLOTS 512
 
331
 
 
332
extern short distance[64][64];
 
333
extern short taxicab[64][64];
 
334
extern unsigned char lzArray[65536];
 
335
extern short Shift00[64];
 
336
extern short Shift90[64];
 
337
extern short Shift45[64];
 
338
extern short Shift315[64];
 
339
extern BitBoard DistMap[64][8];
 
340
extern BitBoard BitPosArray[64];
 
341
extern BitBoard NotBitPosArray[64];
 
342
extern BitBoard MoveArray[8][64];
 
343
extern BitBoard Ray[64][8];
 
344
extern BitBoard FromToRay[64][64];
 
345
extern BitBoard RankBit[8];
 
346
extern BitBoard FileBit[8];
 
347
extern BitBoard Ataks[2][7];
 
348
extern BitBoard PassedPawnMask[2][64];
 
349
extern BitBoard IsolaniMask[8];
 
350
extern BitBoard SquarePawnMask[2][64];
 
351
extern BitBoard Rook00Atak[64][256]; 
 
352
extern BitBoard Rook90Atak[64][256]; 
 
353
extern BitBoard Bishop45Atak[64][256];
 
354
extern BitBoard Bishop315Atak[64][256];
 
355
extern BitBoard pinned;
 
356
extern BitBoard rings[4];
 
357
extern BitBoard boxes[2];
 
358
extern BitBoard stonewall[2];
 
359
extern BitBoard pieces[2];
 
360
extern BitBoard mask_kr_trapped_w[3];
 
361
extern BitBoard mask_kr_trapped_b[3];
 
362
extern BitBoard mask_qr_trapped_w[3];
 
363
extern BitBoard mask_qr_trapped_b[3];
 
364
extern BitBoard boardhalf[2];
 
365
extern BitBoard boardside[2];
 
366
extern short directions[64][64];
 
367
extern unsigned char BitCount[65536];
 
368
extern leaf Tree[MAXTREEDEPTH];
 
369
extern leaf *TreePtr[MAXPLYDEPTH];
 
370
extern int RootPV;
 
371
extern GameRec Game[MAXGAMEDEPTH];
 
372
extern int RealGameCnt;
 
373
extern short RealSide;
 
374
extern int GameCnt;
 
375
extern int computer;
 
376
extern unsigned int flags;
 
377
extern unsigned int preanalyze_flags;
 
378
extern Board board;
 
379
extern int cboard[64];
 
380
extern int Mvboard[64];
 
381
extern HashType hashcode[2][7][64];
 
382
extern HashType ephash[64];
 
383
extern HashType WKCastlehash;
 
384
extern HashType WQCastlehash;
 
385
extern HashType BKCastlehash;
 
386
extern HashType BQCastlehash;
 
387
extern HashType Sidehash;
 
388
extern HashType HashKey;
 
389
extern HashType PawnHashKey;
 
390
extern HashSlot *HashTab[2];
 
391
extern PawnSlot *PawnTab[2];
 
392
extern int Idepth;
 
393
extern int SxDec;
 
394
extern int Game50;
 
395
extern int lazyscore[2];
 
396
extern int maxposnscore[2];
 
397
extern int rootscore;
 
398
extern int lastrootscore;
 
399
extern unsigned long GenCnt;
 
400
extern unsigned long NodeCnt;
 
401
extern unsigned long QuiesCnt;
 
402
extern unsigned long EvalCnt;
 
403
extern unsigned long EvalCall;
 
404
extern unsigned long ChkExtCnt;
 
405
extern unsigned long OneRepCnt;
 
406
extern unsigned long RcpExtCnt;
 
407
extern unsigned long PawnExtCnt;
 
408
extern unsigned long HorzExtCnt;
 
409
extern unsigned long ThrtExtCnt;
 
410
extern unsigned long KingExtCnt;
 
411
extern unsigned long NullCutCnt;
 
412
extern unsigned long FutlCutCnt;
 
413
extern unsigned long RazrCutCnt;
 
414
extern unsigned long TotalGetHashCnt;
 
415
extern unsigned long GoodGetHashCnt;
 
416
extern unsigned long TotalPutHashCnt;
 
417
extern unsigned long CollHashCnt;
 
418
extern unsigned long TotalPawnHashCnt;
 
419
extern unsigned long GoodPawnHashCnt;
 
420
extern unsigned long RepeatCnt;
 
421
extern unsigned HashSize;
 
422
extern unsigned long TTHashMask;
 
423
extern unsigned long PHashMask;
 
424
extern int slider[8];
 
425
extern int Value[7];
 
426
extern char SANmv[SANSZ];
 
427
extern unsigned long history[2][4096];
 
428
extern int killer1[MAXPLYDEPTH];
 
429
extern int killer2[MAXPLYDEPTH];
 
430
extern int ChkCnt[MAXPLYDEPTH];
 
431
extern int ThrtCnt[MAXPLYDEPTH];
 
432
extern char id[32];
 
433
extern char solution[64];
 
434
extern float SearchTime;
 
435
extern int SearchDepth;
 
436
extern int MoveLimit[2];
 
437
extern float TimeLimit[2];
 
438
extern int TCMove;
 
439
extern int TCinc;
 
440
extern float TCTime;
 
441
extern int hunged[2];
 
442
extern int phase;
 
443
extern int Hashmv[MAXPLYDEPTH];
 
444
extern short RootPieces;
 
445
extern short RootPawns;
 
446
extern short RootMaterial;
 
447
extern short RootAlpha;
 
448
extern short RootBeta;
 
449
extern short pickphase[MAXPLYDEPTH];
 
450
extern short InChk[MAXPLYDEPTH];
 
451
extern short KingThrt[2][MAXPLYDEPTH];
 
452
extern short KingSafety[2];
 
453
extern short pscore[64];
 
454
 
 
455
extern short bookmode;
 
456
extern short bookfirstlast;
 
457
 
 
458
extern int range[8];
 
459
extern int ptype[2];
 
460
extern char algbr[64][3];
 
461
extern char algbrfile[9];
 
462
extern char algbrrank[9];
 
463
extern char notation[8];
 
464
extern char lnotation[8];
 
465
extern int r90[64];
 
466
extern int r45[64];
 
467
extern int r315[64];
 
468
extern int Mask45[64];
 
469
extern int Mask315[64];
 
470
 
 
471
extern int rank6[2];
 
472
extern int rank7[2];
 
473
extern int rank8[2];
 
474
 
 
475
extern char *progname;
 
476
extern FILE *ofp;
 
477
extern int myrating, opprating, suddendeath;
 
478
 
 
479
#define MAXNAMESZ 50
 
480
extern char name[MAXNAMESZ];
 
481
extern int computerplays;
 
482
extern int wasbookmove;
 
483
extern int nmovesfrombook;
 
484
extern float maxtime;
 
485
extern int n;           /* Last mobility returned by CTL */
 
486
extern int ExchCnt[2];
 
487
extern int newpos, existpos;            /* For book statistics */
 
488
extern int bookloaded;
 
489
 
 
490
enum Piece { empty, pawn, knight, bishop, rook, queen, king, bpawn };
 
491
 
 
492
enum Square { A1, B1, C1, D1, E1, F1, G1, H1,
 
493
              A2, B2, C2, D2, E2, F2, G2, H2,
 
494
              A3, B3, C3, D3, E3, F3, G3, H3,
 
495
              A4, B4, C4, D4, E4, F4, G4, H4,
 
496
              A5, B5, C5, D5, E5, F5, G5, H5,
 
497
              A6, B6, C6, D6, E6, F6, G6, H6,
 
498
              A7, B7, C7, D7, E7, F7, G7, H7,
 
499
              A8, B8, C8, D8, E8, F8, G8, H8 };
 
500
 
 
501
enum File { A_FILE, B_FILE, C_FILE, D_FILE, E_FILE, F_FILE, G_FILE, H_FILE };
 
502
 
 
503
/****************************************************************************
 
504
 *
 
505
 *  The various function prototypes.  They are group into the *.c files
 
506
 *  in which they are defined.
 
507
 *
 
508
 ****************************************************************************/
 
509
 
 
510
/*
 
511
 * Explanation of the #ifdef NO_INLINE conditionals:
 
512
 *
 
513
 * Define NO_INLINE only if you really must, implementations will be
 
514
 * provided by the corresponding *.c files. The better solution is to
 
515
 * not define it, in which case inlines.h will be included which
 
516
 * provides static inline version of these functions.
 
517
 */
 
518
 
 
519
/*  The initialization routines  */
 
520
void Initialize (void);
 
521
void InitLzArray (void);
 
522
void InitBitPosArray (void);
 
523
void InitMoveArray (void);
 
524
void InitRay (void);
 
525
void InitFromToRay (void);
 
526
void InitRankFileBit (void);
 
527
void InitBitCount (void);
 
528
void InitPassedPawnMask (void);
 
529
void InitIsolaniMask (void);
 
530
void InitSquarePawnMask (void);
 
531
void InitRandomMasks (void);
 
532
void InitRotAtak (void);
 
533
void InitDistance (void);
 
534
void InitVars (void);
 
535
void InitHashCode (void);
 
536
void InitHashTable (void);
 
537
void CalcHashSize (int);
 
538
void NewPosition (void);
 
539
void InitFICS (void);
 
540
void InitInput (void);
 
541
 
 
542
/* Cleanup routines */
 
543
void CleanupInput(void);
 
544
 
 
545
/*  The book routines */
 
546
int BookQuery (int);
 
547
int BookBuilderOpen(void);
 
548
int BookBuilder (short result, uint8_t side);
 
549
int BookBuilderClose(void);
 
550
 
 
551
/*
 
552
 * Return values (errorcodes) for the book routines,
 
553
 * maybe one should have a global enum of errorcodes
 
554
 */
 
555
enum {
 
556
  BOOK_SUCCESS,
 
557
  BOOK_EFORMAT,  /* Wrong format (e.g. produced by older version) */
 
558
  BOOK_EMIDGAME, /* Move is past the opening book's move limit */ 
 
559
  BOOK_EIO,      /* I/O error, e.g. caused by wrong permissions */
 
560
  BOOK_EFULL,    /* Book hash is full, new position was not added. */
 
561
  BOOK_ENOBOOK,  /* No book present */
 
562
  BOOK_ENOMOVES, /* No moves found (in BookQuery() only) */
 
563
  BOOK_ENOMEM    /* Memory allocation failed */
 
564
};
 
565
 
 
566
/*  The move generation routines  */
 
567
void GenMoves (short);
 
568
void GenCaptures (short);
 
569
void GenNonCaptures (short);
 
570
void GenCheckEscapes (short);
 
571
void FilterIllegalMoves (short);
 
572
 
 
573
/*  The move routines  */
 
574
void MakeMove (int, int *);
 
575
void UnmakeMove (int, int *);
 
576
void MakeNullMove (int);
 
577
void UnmakeNullMove (int);
 
578
void SANMove (int, int);
 
579
leaf *ValidateMove (char *);
 
580
leaf *IsInMoveList (int, int, int, char);
 
581
int IsLegalMove (int);
 
582
char *AlgbrMove (int);
 
583
 
 
584
/*  The attack routines  */
 
585
short SqAtakd (short sq, short side);
 
586
void GenAtaks (void);
 
587
BitBoard AttackTo (int, int);
 
588
BitBoard AttackXTo (int, int);
 
589
BitBoard AttackFrom (int, int, int);
 
590
BitBoard AttackXFrom (int, int);
 
591
int PinnedOnKing (int, int);
 
592
void FindPins (BitBoard *);
 
593
int MateScan (int);
 
594
 
 
595
/*  The swap routines  */
 
596
int SwapOff (int);
 
597
void AddXrayPiece (int, int, int, BitBoard *, BitBoard *);
 
598
 
 
599
/*  The EPD routines  */
 
600
short ReadEPDFile (const char *, short);
 
601
int ParseEPD (char *);
 
602
void LoadEPD (char *);
 
603
void SaveEPD (char *);
 
604
 
 
605
/* Error codes for ParseEPD */
 
606
enum {
 
607
   EPD_SUCCESS,
 
608
   EPD_ERROR
 
609
};
 
610
 
 
611
void UpdateFriends (void);
 
612
void UpdateCBoard (void);
 
613
void UpdateMvboard (void);
 
614
void EndSearch (int);
 
615
short ValidateBoard (void);
 
616
 
 
617
/*  PGN routines  */
 
618
void PGNSaveToFile (const char *, const char *);
 
619
void PGNReadFromFile (const char *);
 
620
void BookPGNReadFromFile (const char *);
 
621
int IsTrustedPlayer(const char *name);
 
622
 
 
623
 
 
624
/*  The hash routines  */
 
625
void CalcHashKey (void);
 
626
void ShowHashKey (HashType);
 
627
 
 
628
/*  The evaluation routines  */
 
629
int ScoreP (short);
 
630
int ScoreN (short);
 
631
int ScoreB (short);
 
632
int ScoreR (short);
 
633
int ScoreQ (short);
 
634
int ScoreK (short);
 
635
int ScoreDev (short);
 
636
int Evaluate (int, int);
 
637
short EvaluateDraw (void);
 
638
 
 
639
/*  Hung routines  */
 
640
int EvalHung (int);
 
641
 
 
642
/*  The search routines  */
 
643
void Iterate (void);
 
644
int Search (uint8_t ply, short depth, int alpha, int beta, short nodetype);
 
645
int SearchRoot (short depth, int alpha, int beta);
 
646
int Quiesce (uint8_t ply, int alpha, int beta);
 
647
void pick (leaf *, short);
 
648
short Repeat (void);
 
649
void ShowLine (int, int, char);
 
650
 
 
651
/*
 
652
 * Set up a timer by first calling StartTiming(), saving
 
653
 * the return value and feeding it to GetElapsed() for
 
654
 * timings in seconds.
 
655
 */
 
656
typedef struct timeval Timer;
 
657
extern double ElapsedTime;
 
658
extern Timer StartTime;
 
659
Timer StartTiming (void);
 
660
double GetElapsed (Timer start);
 
661
 
 
662
/*  The transposition table routies */
 
663
void TTPut (uint8_t side, uint8_t depth, uint8_t ply, 
 
664
            int alpha, int beta, int score, int move);
 
665
/* Returns flag if it finds an entry, 0 otherwise */
 
666
uint8_t TTGet (uint8_t side, uint8_t depth, uint8_t ply,
 
667
             int *score, int *move);
 
668
short TTGetPV (uint8_t side, uint8_t ply, int score, int *move);
 
669
void TTClear (void);
 
670
void PTClear (void);
 
671
 
 
672
/*  Sorting routines  */
 
673
void SortCaptures (int);
 
674
void SortMoves (int);
 
675
void SortRoot (void);
 
676
int PhasePick (leaf **, int);
 
677
int PhasePick1 (leaf **, int);
 
678
 
 
679
/*  Some output routines */
 
680
void ShowMoveList (int);
 
681
void ShowSmallBoard (void);
 
682
void ShowBoard (void);
 
683
void ShowBitBoard (BitBoard *);
 
684
void ShowCBoard (void);
 
685
void ShowMvboard (void);
 
686
 
 
687
void ShowGame (void);
 
688
void ShowTime (void);
 
689
 
 
690
/*  Random numbers routines */
 
691
uint32_t Rand32 (void);
 
692
HashType Rand64 (void);
 
693
 
 
694
/*  Solver routines  */
 
695
void Solve (char *);
 
696
 
 
697
/*  Test routines  */
 
698
void TestMoveGenSpeed (void);
 
699
void TestNonCaptureGenSpeed (void);
 
700
void TestCaptureGenSpeed (void);
 
701
void TestMoveList (void);
 
702
void TestNonCaptureList (void);
 
703
void TestCaptureList (void);
 
704
void TestEvalSpeed (void);
 
705
void TestEval (void);
 
706
 
 
707
/* Player database */
 
708
void DBSortPlayer (const char *style);
 
709
void DBListPlayer (const char *style);  
 
710
void DBReadPlayer (void);       
 
711
void DBWritePlayer (void);
 
712
int DBSearchPlayer (const char *player);
 
713
void DBUpdatePlayer (const char *player, const char *resultstr);
 
714
void DBTest (void);
 
715
 
 
716
/* Input thread and thread function */
 
717
#include <pthread.h>
 
718
extern pthread_t input_thread;
 
719
void *input_func(void *);
 
720
 
 
721
/*
 
722
 * Status variable used by the input thread to signal
 
723
 * pending input. Thought about using flags for this
 
724
 * but this can be refined and is conceptually different
 
725
 * from flags.
 
726
 */
 
727
enum {
 
728
  INPUT_NONE,
 
729
  INPUT_AVAILABLE
 
730
};
 
731
extern volatile int input_status;
 
732
 
 
733
/*
 
734
 * Function to wake up the input thread, should be called after
 
735
 * input has been parsed.
 
736
 */
 
737
void input_wakeup(void);
 
738
 
 
739
/* Wait for input. */
 
740
 
 
741
void wait_for_input(void);
 
742
 
 
743
/*
 
744
 * Input routine, initialized to one of the specific
 
745
 * input routines. The given argument is the prompt.
 
746
 */
 
747
void (*getline_intrl) (char *);
 
748
 
 
749
#define MAXSTR 128
 
750
extern char inputstr[MAXSTR];
 
751
 
 
752
/* Input parser */
 
753
void parse_input(void);
 
754
 
 
755
/* Pondering */
 
756
void ponder(void);
 
757
 
 
758
/*  The command subroutines */
 
759
void ShowCmd (char *);
 
760
void BookCmd (char *);
 
761
void TestCmd (char *);
 
762
 
 
763
/* Commands from the input engine */
 
764
void cmd_accepted(void);
 
765
void cmd_activate(void); 
 
766
void cmd_analyze(void);
 
767
void cmd_bk(void);
 
768
void cmd_black(void);
 
769
void cmd_book(void);
 
770
void cmd_computer(void);
 
771
void cmd_depth(void);
 
772
void cmd_draw(void);
 
773
void cmd_easy(void);
 
774
void cmd_edit(void); 
 
775
void cmd_epd(void);
 
776
void cmd_exit(void);
 
777
void cmd_force(void);
 
778
void cmd_go(void);
 
779
void cmd_hard(void);
 
780
void cmd_hash(void);
 
781
void cmd_hashsize(void);
 
782
void cmd_help (void);
 
783
void cmd_hint(void);
 
784
void cmd_level(void);
 
785
void cmd_list(void);
 
786
void cmd_load(void);
 
787
void cmd_manual(void);
 
788
void cmd_movenow(void);
 
789
void cmd_name(void);
 
790
void cmd_new(void);
 
791
void cmd_nopost(void);
 
792
void cmd_null(void);
 
793
void cmd_otim(void);
 
794
void cmd_pgnload(void);
 
795
void cmd_pgnsave(void);
 
796
void cmd_ping(void);
 
797
void cmd_post(void);
 
798
void cmd_protover(void);
 
799
void cmd_quit(void);
 
800
void cmd_random(void);
 
801
void cmd_rating(void);
 
802
void cmd_rejected(void);
 
803
void cmd_remove(void);
 
804
void cmd_result(void);
 
805
void cmd_save(void);
 
806
void cmd_setboard(void);
 
807
void cmd_show (void);
 
808
void cmd_solve(void);
 
809
void cmd_st(void); 
 
810
void cmd_switch(void);
 
811
void cmd_test (void);
 
812
void cmd_time(void);
 
813
void cmd_undo(void);
 
814
void cmd_usage(void);
 
815
void cmd_variant(void);
 
816
void cmd_version(void);
 
817
void cmd_white(void);
 
818
void cmd_xboard(void);
 
819
 
 
820
/*
 
821
 * Define NO_INLINE only if you really must, implementations will be
 
822
 * provided by the corresponding *.c files. The better solution is to
 
823
 * not define it, in which case inlines.h will be included which
 
824
 * provides static inline version of these functions.
 
825
 */
 
826
 
 
827
/*  Some utility routines  */
 
828
#ifdef NO_INLINE
 
829
unsigned char leadz (BitBoard);
 
830
unsigned char nbits (BitBoard);
 
831
#else
 
832
# include "inlines.h"
 
833
#endif
 
834
 
 
835
/* More elaborate debugging output to logfile */
 
836
 
 
837
/* All the following functions are no-ops if DEBUG is not defined */
 
838
 
 
839
/*
 
840
 * dbg_open() can be called with NULL as argument, using a default
 
841
 * filename, defined in debug.c, for the debug log. Otherwise the
 
842
 * argument is the filename. If dbg_open() fails or is not called at
 
843
 * all, debugging output goes to stderr by default.
 
844
 */
 
845
int dbg_open(const char *name);
 
846
 
 
847
/* Same format rules as printf() */
 
848
int dbg_printf(const char *fmt, ...);
 
849
 
 
850
/* Closes the debugging log, if it is not stderr */
 
851
int dbg_close(void);
 
852
 
 
853
# ifdef DEBUG
 
854
#  include <assert.h>
 
855
#  define ASSERT(x) assert(x)
 
856
# else
 
857
#  define ASSERT(x)
 
858
# endif
 
859
 
 
860
#endif /* !COMMON_H */