Fix use of (COL|ROW)_NAME macros to compute numbers from names.
[gnushogi.git] / gnushogi / gnushogi.h
1 /*
2  * FILE: gnushogi.h
3  *
4  *     Main header file for GNU Shogi.
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
10  *
11  * GNU SHOGI is based on GNU CHESS
12  *
13  * Copyright (c) 1988, 1989, 1990 John Stanback
14  * Copyright (c) 1992 Free Software Foundation
15  *
16  * This file is part of GNU SHOGI.
17  *
18  * GNU Shogi is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 3 of the License,
21  * or (at your option) any later version.
22  *
23  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with GNU Shogi; see the file COPYING. If not, see
30  * <http://www.gnu.org/licenses/>.
31  * ----------------------------------------------------------------------
32  *
33  */
34
35 /* Hack for anal-retentive ANSI-compliance if desired: */
36 #define inline
37
38 /* FIXME: this file needs to be reorganized in some rational manner. */
39
40 #ifndef _GNUSHOGI_H_
41 #define _GNUSHOGI_H_
42
43 #include "config.h"        /* Portability #defines. */
44 #include "debug.h"
45 #include "opts.h"          /* Various option-setting #defines.  */
46
47 /*
48  * Display options.
49  */
50
51 typedef enum {
52   DISPLAY_RAW,
53 #ifdef HAVE_LIBCURSES
54   DISPLAY_CURSES,
55 #endif
56   DISPLAY_X
57 } display_t;
58 extern display_t display_type;
59
60 #define XSHOGI     (display_type == DISPLAY_X)
61 #define NOT_CURSES ((display_type == DISPLAY_X) \
62   || (display_type == DISPLAY_RAW))
63
64
65 /* Miscellaneous globals. */
66
67 extern short hard_time_limit; /* If you exceed time limit, you lose.   */
68 extern short barebones;       /* Don't print of stats for x interface. */
69 extern short nolist;          /* Don't list game after exit.           */
70
71
72 /*
73  * Options for various compilers/OSs.
74  */
75
76 /*
77  * type small_short must cover -128 .. 127.  In case of trouble,
78  * try commenting out "signed".  If this doesn't help, use short.
79  */
80
81 #define small_short  signed char
82 #define small_ushort unsigned char
83
84
85 typedef small_ushort   UBYTE;
86 typedef short          SHORT;
87 typedef unsigned short USHORT;
88 typedef int            INT;
89 typedef unsigned int   UINT;
90 typedef long           LONG;
91 typedef unsigned long  ULONG;
92
93
94 #if !defined(HAVE_MEMCPY) && !defined(HAVE_BCOPY)
95 #  define array_copy(src, dst, len) \
96    { \
97        long i; \
98        char  *psrc = (char  *)src, *pdst = (char  *)dst; \
99        for (i = len; i; pdst[--i] = psrc[i]); \
100    }
101 #  define array_zero(dst, len) \
102   { \
103       long i; \
104       char  *pdst = (char  *)dst; \
105       for (i = len; i; pdst[--i] = 0); \
106   }
107 #elif !defined(HAVE_MEMCPY)   /* BSD and derivatives */
108 #  define array_copy(src, dst, len) bcopy(src, dst, len)
109 #  define array_zero(dst, len)      bzero(dst, len)
110 #else /* System V and derivatives */
111 #  define array_copy(src, dst, len) memcpy(dst, src, len)
112 #  define array_zero(dst, len)      memset(dst, 0, len)
113 #endif
114
115
116 #ifndef __GNUC__
117 #  define inline
118 #endif
119
120
121 /*
122  * Standard header files.
123  */
124
125 #include <stdio.h>
126 #include <ctype.h>
127 #include <stdlib.h>
128 #include <assert.h>
129 #include <string.h>
130
131 #include <sys/param.h>
132 #include <sys/types.h>
133 #ifdef WIN32
134 #  include <windows.h>
135 #else
136    typedef small_short    BYTE;
137 #  include <sys/times.h>
138 #  include <sys/ioctl.h>
139 #endif
140
141 #if TIME_WITH_SYS_TIME
142 #  include <sys/time.h>
143 #  include <time.h>
144 #else
145 #  if HAVE_SYS_TIME_H
146 #    include <sys/time.h>
147 #  else
148 #    include <time.h>
149 #  endif
150 #endif
151
152 #define RWA_ACC "r+"
153 #define WA_ACC "w+"
154 #ifdef BINBOOK
155 extern char *binbookfile;
156 #endif
157
158 extern char *bookfile;
159 extern short ahead;
160 extern char  *xwin;
161 extern char  *Lang;
162 extern void movealgbr(short m, char *s);
163
164
165 #define SEEK_SET 0
166 #define SEEK_END 2
167
168 #ifdef MINISHOGI
169 #define NO_PIECES       11
170 #define MAX_CAPTURED    19
171 #define NO_PTYPE_PIECES 11
172 #define NO_COLS          5
173 #define NO_ROWS          5
174 #else
175 #define NO_PIECES       15
176 #define MAX_CAPTURED    19
177 #define NO_PTYPE_PIECES 15
178 #define NO_COLS          9
179 #define NO_ROWS          9
180 #endif
181 #define NO_SQUARES      (NO_COLS*NO_ROWS)
182
183 #define ROW_NAME(n) ('a' + NO_ROWS - 1 - n)
184 #define COL_NAME(n) ('1' + NO_COLS - 1 - n)
185 #define ROW_NUM(c) ('a' + NO_ROWS - 1 - c)
186 #define COL_NUM(c) ('1' + NO_COLS - 1 - c)
187
188 #if defined HASHFILE || defined CACHE
189 #  define PTBLBDSIZE (NO_SQUARES + NO_PIECES)
190 #endif
191
192 #include "eval.h"
193
194 #define SCORE_LIMIT 12000
195
196 /* masks into upper 16 bits of attacks array */
197 /* observe order of relative piece values */
198 #define CNT_MASK 0x000000FF
199 #define ctlP  0x00200000
200 #define ctlPp 0x00100000
201 #define ctlL  0x00080000
202 #define ctlN  0x00040000
203 #define ctlLp 0x00020000
204 #define ctlNp 0x00010000
205 #define ctlS  0x00008000
206 #define ctlSp 0x00004000
207 #define ctlG  0x00002000
208 #define ctlB  0x00001000
209 #define ctlBp 0x00000800
210 #define ctlR  0x00000400
211 #define ctlRp 0x00000200
212 #define ctlK  0x00000100
213
214 /* attack functions */
215 #define Pattack(c, u)   (attack[c][u] > ctlP)
216 #define Anyattack(c, u) (attack[c][u] != 0)
217
218 /* hashtable flags */
219 #define truescore   0x0001
220 #define lowerbound  0x0002
221 #define upperbound  0x0004
222 #define kingcastle  0x0008
223 #define queencastle 0x0010
224 #define evalflag    0x0020
225
226 /* King positions */
227 #define BlackKing PieceList[black][0]
228 #define WhiteKing PieceList[white][0]
229 #define OwnKing   PieceList[c1][0]
230 #define EnemyKing PieceList[c2][0]
231
232
233 /* board properties */
234 #ifndef MINISHOGI
235 #define InBlackCamp(sq) ((sq) < 27)
236 #define InWhiteCamp(sq) ((sq) > 53)
237 #else
238 #define InBlackCamp(sq) ((sq) < 5)
239 #define InWhiteCamp(sq) ((sq) > 19)
240 #endif
241 #define InPromotionZone(side, sq) \
242 (((side) == black) ? InWhiteCamp(sq) : InBlackCamp(sq))
243
244 /* constants */
245 /* FIXME ? */
246 #define OPENING_HINT 0x141d /* P7g-7f (20->29) */
247
248 /* truth values */
249 #ifndef false
250 #define false 0
251 #endif
252
253 #ifndef true
254 #define true  1
255 #endif
256
257 /* colors */
258 #define black   0
259 #define white   1
260 #define neutral 2
261
262 /* piece code defines */
263 enum {
264     no_piece = 0,
265     pawn,
266 #ifndef MINISHOGI
267     lance,
268     knight,
269 #endif
270     /* start of pieces that can be dropped at any square */
271     silver,
272     gold,
273     bishop,
274     rook,
275     ppawn,
276 #ifndef MINISHOGI
277     plance,
278     pknight,
279 #endif
280     psilver,
281     pbishop,
282     prook,
283     king
284 };
285
286 /* move types */
287 enum {
288     ptype_no_piece = 0,
289     ptype_pawn = 0,
290 #ifndef MINISHOGI
291     ptype_lance,
292     ptype_knight,
293 #endif
294     ptype_silver,
295     ptype_gold,
296     ptype_bishop,
297     ptype_rook,
298     ptype_pbishop,
299     ptype_prook,
300     ptype_king,
301     ptype_wpawn,
302 #ifndef MINISHOGI
303     ptype_wlance,
304     ptype_wknight,
305 #endif
306     ptype_wsilver,
307     ptype_wgold
308 };
309
310 /* node flags */
311 #define pmask        0x000f /*    15 */
312 #define promote      0x0010 /*    16 */
313 #define dropmask     0x0020 /*    32 */
314 #define exact        0x0040 /*    64 */
315 #define tesuji       0x0080 /*   128 */
316 #define check        0x0100 /*   256 */
317 #define capture      0x0200 /*   512 */
318 #define draw         0x0400 /*  1024 */
319 #define stupid       0x0800 /*  2048 */
320 #define questionable 0x1000 /*  4096 */
321 #define kingattack   0x2000 /*  8192 */
322 #define book         0x4000 /* 16384 */
323
324 /* move quality flags */
325 #define goodmove     tesuji
326 #define badmove      stupid
327 #ifdef EASY_OPENINGS
328 #define difficult    questionable
329 #endif
330
331 /* move symbols */
332 #ifndef MINISHOGI
333 #define pxx (" PLNSGBRPLNSBRK ")
334 #define qxx (" plnsgbrplnsbrk ")
335 #define rxx ("ihgfedcba")
336 #define cxx ("987654321")
337 #else
338 #define pxx (" PSGBRPSBRK ")
339 #define qxx (" psgbrpsbrk ")
340 #define rxx ("edcba")
341 #define cxx ("54321")
342 #endif
343
344 /***************** Table limits ********************************************/
345
346 /*
347  * ttblsz must be a power of 2. Setting ttblsz 0 removes the transposition
348  * tables.
349  */
350
351 #if defined NOTTABLE
352 #  define vttblsz 0
353 #elif defined SMALL_MEMORY
354 #  if defined SAVE_SSCORE
355 #    define vttblsz (1 << 12)
356 #  else
357 #    if defined EXTRA_2MB
358 #      define vttblsz (1 << 12)
359 #    else
360 #      define vttblsz (1 << 10)
361 #    endif
362 #  endif
363 #else
364 #  define vttblsz (100001)
365 #endif
366
367 #if defined SMALL_MEMORY
368 #  define MINTTABLE (0)
369 #else
370 #  define MINTTABLE (8000)    /* min ttable size -1 */
371 #endif
372
373 #define ttblsz vttblsz
374
375 #if defined SMALL_MEMORY
376 #  if !defined SAVE_SSCORE
377 #    define TREE 1500             /* max number of tree entries */
378 #  else
379 #    define TREE 2500             /* max number of tree entries */
380 #  endif
381 #else
382 #  define TREE 4000               /* max number of tree entries */
383 #endif
384
385 #define MAXDEPTH  40            /* max depth a search can be carried */
386 #define MINDEPTH   2            /* min search depth =1 (no hint), >1 hint */
387 #define MAXMOVES 300            /* max number of half moves in a game */
388 #define CPSIZE   241            /* size of lang file max */
389
390 #if defined SMALL_MEMORY
391 #  if defined SAVE_SSCORE
392 #    define ETABLE (1 << 10)      /* static eval cache */
393 #  else
394 #    if defined EXTRA_2MB
395 #      define ETABLE (1 << 10)    /* static eval cache */
396 #    else
397 #      define ETABLE (1 << 8)     /* static eval cache */
398 #    endif
399 #  endif
400 #else
401 #  define ETABLE (10001)          /* static eval cache */
402 #endif
403
404 /***************** tuning paramaters *******************/
405
406 #if defined VERY_SLOW_CPU
407 #  define MINRESPONSETIME 300
408 #elif defined SLOW_CPU
409 #  define MINRESPONSETIME 200
410 #else
411 #  define MINRESPONSETIME 100     /* 1 s */
412 #endif
413
414 #define MINGAMEIN  4
415 #define MINMOVES  15
416 #define CHKDEPTH   1   /* always look forward CHKDEPTH
417                         * half-moves if in check */
418
419 #if defined SLOW_CPU || defined VERY_SLOW_CPU
420 #  define DEPTHBEYOND 7     /* Max to go beyond Sdepth */
421 #else
422 #  define DEPTHBEYOND 11    /* Max to go beyond Sdepth */
423 #endif
424
425 #define HASHDEPTH      4  /* depth above which to use HashFile */
426 #define HASHMOVELIMIT 40  /* Use HashFile only for this many moves */
427 #define PTVALUE        0  /* material value below which pawn threats at
428                            * 5 & 3 are used */
429 #define ZDEPTH 3          /* depth beyond which to check
430                            * ZDELTA for extra time */
431 #define ZDELTA 10         /* score delta per ply to cause
432                            * extra time to be given */
433 #define BESTDELTA 90
434
435 /* about 1/2 second worth of nodes for your machine */
436 #if defined VERY_SLOW_CPU
437 /* check the time every ZNODES positions */
438 #  define ZNODES (flag.tsume ? 20 : 50)
439 #elif defined SLOW_CPU
440 #  define ZNODES (flag.tsume ? 40 : 100)
441 #else
442 #  define ZNODES (flag.tsume ? 400 : 1000)
443 #endif
444
445 #define MAXTCCOUNTX  10  /* max number of time clicks
446                           * per search to complete ply */
447 #define MAXTCCOUNTR   4  /* max number of time clicks
448                           * per search extensions*/
449 #define SCORESPLIM    8  /* Score space doesn't apply after this stage */
450 #define SDEPTHLIM    (Sdepth + 1)
451 #define HISTORYLIM 4096  /* Max value of history killer */
452
453 #ifdef EXACTHISTORY
454 #  if defined SMALL_MEMORY
455 #    define HISTORY_MASK 0x8000     /* mask to MSB of history index */
456 #    define HISTORY_SIZE 0x10000    /* size of history table */
457 #  else
458 #    define HISTORY_MASK (1 << 15)  /* mask to MSB of history index */
459 #    define HISTORY_SIZE (1 << 16)  /* size of history table */
460 #  endif
461 #else
462 /* smaller history table, but dangerous because of collisions */
463 #  define HISTORY_MASK 0x3fff     /* mask to significant bits
464                                    * of history index */
465 #  if defined SMALL_MEMORY
466 #    define HISTORY_SIZE 0x4000 /* size of history table */
467 #  else
468 #    define HISTORY_SIZE (1 << 14)  /* size of history table */
469 #  endif
470 #endif
471
472 #define sizeof_history (sizeof(unsigned short) * (size_t)HISTORY_SIZE)
473
474 #ifdef EXACTHISTORY
475 /* Map from.to (8bit.8bit) to from.to (0.7bit.8bit) */
476 #  define khmove(mv) (mv & 0x7fff)
477 #  define hmove(mv) ((mv & 0x7fff) ^ 0x5555)
478 #else
479 /* Map from.to (8bit.8bit) to from.to (00.7bit.7bit) */
480 /* Swap bits of ToSquare in case of promotions, hoping that
481    no catastrophic collision occurs. */
482 #  define khmove(mv) (((mv & 0x7f00) >> 1) | \
483            ((mv & 0x0080) ? ((mv & 0x007f) ^ 0x007f) : (mv & 0x007f)))
484 #  define hmove(mv) (khmove(mv) ^ 0x2aaa)
485 #endif
486
487 /* mask color to 15th bit */
488 #ifdef EXACTHISTORY
489 #  define hindex(c, mv) ((c ? HISTORY_MASK : 0) | hmove(mv))
490 #else
491 /* for white, swap bits, hoping that no catastrophic collision occurs. */
492 #  define hindex(c, mv) (c ? ((~hmove(mv)) & HISTORY_MASK) : hmove(mv))
493 #endif
494
495 #define EWNDW  10    /* Eval window to force position scoring at depth
496                       * greater than (Sdepth + 2)        */
497 #define WAWNDW 90    /* alpha window when computer black */
498 #define WBWNDW 90    /* beta window when computer black  */
499 #define BAWNDW 90    /* alpha window when computer white */
500 #define BBWNDW 90    /* beta window when computer white  */
501 #define BXWNDW 90    /* window to force position scoring at lower */
502 #define WXWNDW 90    /* window to force position scoring at lower */
503
504 #define DITHER  5    /* max amount random can alter a pos value */
505 #define LBONUS  1    /* points per stage value of L increases   */
506 #define BBONUS  2    /* points per stage value of B increases   */
507 #define RBONUS  2    /* points per stage value of R increases   */
508
509 #define QUESTIONABLE (valueK)   /* Penalty for questionable moves. */
510
511 #if defined STUPID
512 #  undef STUPID
513 #endif
514
515 #define STUPID (valueR << 1)    /* Penalty for stupid moves. */
516
517 #define KINGPOSLIMIT (-1)      /* King positional scoring limit */
518 #define KINGSAFETY  32
519 #define MAXrehash (7)
520
521 /******* parameters for Opening Book ****************/
522
523 #define BOOKSIZE   8000  /* Number of unique position/move
524                           * combinations allowed */
525 #define BOOKMAXPLY   40           /* Max plys to keep in book database */
526 #define BOOKFAIL (BOOKMAXPLY / 2) /* if no book move found for BOOKFAIL
527                                    * turns stop using book */
528 #define BOOKPOCKET          64
529 #define BOOKRAND          1000  /* used to select an opening move
530                                  * from a list */
531 #define BOOKENDPCT         950  /* 5 % chance a BOOKEND will stop the book */
532 #define DONTUSE         -32760  /* flag move as don't use */
533 #define ILLEGAL_TRAPPED -32761  /* flag move as illegal:
534                                  * no move from this square */
535 #define ILLEGAL_DOUBLED -32762  /* flag move as illegal:
536                                  * two pawns on one column */
537 #define ILLEGAL_MATE    -32763  /* flag move as illegal:
538                                  * pawn drop with mate */
539
540 /*****************************************************/
541
542 struct hashval
543 {
544     unsigned long key, bd;
545 };
546
547 struct hashentry
548 {
549     unsigned long hashbd;
550     unsigned short mv;
551     unsigned char depth; /* unsigned char saves some space */
552     unsigned char flags;
553 #ifdef notdef
554     unsigned short age;
555 #endif
556     short score;
557 #ifdef HASHTEST
558     unsigned char bd[PTBLBDSIZE];
559 #endif /* HASHTEST */
560 };
561
562 #if defined HASHFILE || defined CACHE
563 struct etable
564 {
565     unsigned long ehashbd;
566     short escore[2];
567 #if !defined SAVE_SSCORE
568     short sscore[NO_SQUARES];
569 #endif
570     short score;
571     small_short hung[2];
572 #ifdef CACHETEST
573     unsigned char bd[PTBLBDSIZE];
574 #endif /* CACHETEST */
575 };
576
577 #if defined CACHE
578 extern short use_etable;
579 typedef struct etable etable_field[ETABLE];
580 extern etable_field  *etab[2];
581 #endif
582
583 /*
584  * CHECKME! Is this valid?
585  *
586  * persistent transposition table. By default, the size is (1 << vfilesz).
587  * If you change the size, be sure to run gnushogi -c [vfilesz]
588  * before anything else.
589  */
590
591 #define frehash 6
592
593 #if defined SMALL_MEMORY
594 #  define vfilesz 10
595 #else
596 #  define vfilesz 14
597 #endif
598
599 struct fileentry
600 {
601     unsigned char bd[PTBLBDSIZE];
602     unsigned char f, t, flags, depth, sh, sl;
603 };
604
605 #endif /* HASHFILE */
606
607
608 struct leaf
609 {
610     small_ushort f, t;
611     short score, reply, width;
612     short INCscore;
613     unsigned short flags;
614 };
615
616
617 struct GameRec
618 {
619     unsigned short gmove;    /* this move */
620     short score;             /* score after this move */
621     short depth;             /* search depth this move */
622     long  time;               /* search time this move */
623     short fpiece;            /* moved or dropped piece */
624     short piece;             /* piece captured */
625     short color;             /* color */
626     short flags;             /* move flags capture, promote, castle */
627     short Game50;            /* flag for repetition */
628     long  nodes;              /* nodes searched for this move */
629     unsigned long hashkey, hashbd;   /* board key before this move */
630 };
631
632
633 struct TimeControlRec
634 {
635     short moves[2];
636     long  clock[2];
637 };
638
639
640 struct flags
641 {
642     short mate;              /* the game is over */
643     short post;              /* show principle variation */
644     short quit;              /* quit/exit */
645     short regularstart;      /* did the game start from standard
646                               * initial board ? */
647     short reverse;           /* reverse board display */
648     short bothsides;         /* computer plays both sides */
649     short hash;              /* enable/disable transposition table */
650     short force;             /* enter moves */
651     short easy;              /* disable thinking on opponents time */
652     short beep;              /* enable/disable beep */
653     short timeout;           /* time to make a move */
654     short musttimeout;       /* time to make a move */
655     short back;              /* time to make a move */
656     short rcptr;             /* enable/disable recapture heuristics */
657     short rv;                /* reverse video */
658     short stars;             /* add stars to uxdsp screen */
659     short coords;            /* add coords to visual screen */
660     short shade;
661     short material;          /* draw on lack of material */
662     short illegal;           /* illegal position */
663     short onemove;           /* timing is onemove */
664     short gamein;            /* timing is gamein */
665     short tsume;             /* first consider checks */
666 };
667
668 extern FILE *debugfile;
669
670 #ifndef EVALFILE
671 #define EVALFILE "/tmp/EVAL"
672 #endif
673
674 extern FILE *debug_eval_file;
675 extern short debug_moves;
676
677
678 #ifdef HISTORY
679 extern short use_history;
680 extern unsigned short  *history;
681 #endif
682
683 extern long znodes;
684
685 extern char ColorStr[2][10];
686
687 extern char mvstr[4][6];
688 extern unsigned short MV[MAXDEPTH];
689 extern int MSCORE;
690 extern int mycnt1, mycnt2;
691 extern short ahead;
692 extern struct leaf rootnode;
693 extern struct leaf  *Tree;
694 extern struct leaf  *root;
695 extern char savefile[], listfile[];
696 extern short TrPnt[];
697 extern small_short board[], color[];
698 extern const small_short sweep[NO_PIECES];
699 extern small_short PieceList[2][NO_SQUARES], PawnCnt[2][NO_COLS];
700 extern small_short Captured[2][NO_PIECES];
701
702 #ifndef HAVE_MEMSET
703 #  define ClearCaptured() \
704   { \
705       short piece, color; \
706       for (color = black; color <= white; color++) \
707           for (piece = 0; piece < NO_PIECES; piece++) \
708               Captured[color][piece] = 0; \
709   }
710 #else
711 #  define ClearCaptured() \
712   memset((char *)Captured, 0, (unsigned long)sizeof(Captured))
713 #endif /* HAVE_MEMSET */
714
715 extern small_short Mvboard[];
716
717 #if !defined SAVE_SVALUE
718 extern short svalue[NO_SQUARES];
719 #endif
720
721 extern short pscore[2]; /* eval.c */
722 extern int EADD; /* eval.c */
723 extern int EGET; /* eval.c */
724 extern struct flags flag;
725 extern short opponent, computer, INCscore;
726 extern short WAwindow, BAwindow, WBwindow, BBwindow;
727 extern short dither, player;
728 extern short xwndw, contempt;
729 extern long  ResponseTime, ExtraTime, TCleft,
730     MaxResponseTime, et, et0, time0, ft;
731 extern int   TCcount;
732
733 #ifdef INTERRUPT_TEST
734 extern long itime0, it;
735 #endif
736
737 extern long  reminus, replus;
738 extern long  GenCnt, NodeCnt, ETnodes, EvalNodes, HashAdd, HashCnt,
739     HashCol, THashCol, FHashCnt, FHashAdd;
740 extern short HashDepth, HashMoveLimit;
741 extern struct GameRec  *GameList;
742 extern short GameCnt, Game50;
743 extern short Sdepth, MaxSearchDepth;
744 extern int   Book;
745 extern struct TimeControlRec TimeControl;
746 extern int   TCadd;
747 extern short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
748 extern int   timecomp[MINGAMEIN], timeopp[MINGAMEIN];
749 extern int   compptr, oppptr;
750 extern short XCmore, XCmoves[], XCminutes[], XCseconds[], XC;
751 extern const short otherside[];
752 extern const small_short Stboard[];
753 extern const small_short Stcolor[];
754 extern unsigned short hint;
755 extern short TOflag;
756 extern short stage, stage2;
757
758 #define in_opening_stage    (!flag.tsume && (stage < 33))
759 #define in_middlegame_stage (!flag.tsume && (stage >= 33) && (stage <= 66))
760 #define in_endgame_stage    (flag.tsume || (stage > 66))
761
762 extern short ahead, hash;
763 extern short balance[2];
764 extern small_short ChkFlag[], CptrFlag[], TesujiFlag[];
765 extern short Pscore[], Tscore[];
766 extern /*unsigned*/ short rehash;  /* -1 is used as a flag --tpm */
767 extern unsigned int ttbllimit;
768 extern unsigned int TTadd;
769 extern unsigned int ttblsize;
770 extern short mtl[], hung[];
771 extern small_short Pindex[];
772 extern small_short PieceCnt[];
773 extern short FROMsquare, TOsquare;
774 extern small_short HasPiece[2][NO_PIECES];
775 extern const short kingP[];
776 extern unsigned short killr0[], killr1[];
777 extern unsigned short killr2[], killr3[];
778 extern unsigned short PrVar[MAXDEPTH];
779 extern unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
780 extern short mtl[2], pmtl[2], hung[2];
781 extern const small_short relative_value[];
782 extern const long control[];
783 extern small_short diagonal(short delta);
784 extern const small_short promoted[NO_PIECES], unpromoted[NO_PIECES];
785 extern const small_short is_promoted[NO_PIECES];
786
787 typedef unsigned char next_array[NO_SQUARES][NO_SQUARES];
788 typedef small_short distdata_array[NO_SQUARES][NO_SQUARES];
789
790 extern const small_short inunmap[NO_SQUARES];
791 #ifndef MINISHOGI
792 extern const small_short nunmap[(NO_COLS + 2)*(NO_ROWS + 4)];
793 #else
794 extern const small_short nunmap[(NO_COLS + 2)*(NO_ROWS + 2)];
795 #endif
796
797 #if defined SAVE_NEXTPOS
798 extern const small_short direc[NO_PTYPE_PIECES][8];
799 extern short first_direction(short ptyp, short *d, short sq);
800 extern short next_direction(short ptyp, short *d, short sq);
801 extern short next_position(short ptyp, short *d, short sq, short u);
802 #else
803 extern short use_nextpos;
804 extern next_array  *nextpos[NO_PTYPE_PIECES];
805 extern next_array  *nextdir[NO_PTYPE_PIECES];
806 #endif
807
808 extern value_array   *value;
809 extern fscore_array  *fscore;
810
811 #ifndef SAVE_DISTDATA
812 extern short use_distdata;
813 extern distdata_array  *distdata;
814 #endif
815
816 #ifndef SAVE_PTYPE_DISTDATA
817 extern short use_ptype_distdata;
818 extern distdata_array  *ptype_distdata[NO_PTYPE_PIECES];
819 #endif
820
821 extern const small_short ptype[2][NO_PIECES];
822
823 extern long filesz, hashmask, hashbase;
824 extern FILE *hashfile;
825 extern unsigned int starttime;
826
827 /* eval.c */
828 typedef small_short Mpiece_array[2][NO_SQUARES];
829 extern Mpiece_array *Mpiece[NO_PIECES];
830 extern short ADVNCM[NO_PIECES];
831
832 #define computed_distance(a, b) \
833     ((abs(column(a) - column(b)) > abs(row(a) - row(b))) \
834     ? abs(column(a) - column(b)) : abs(row(a) - row(b)))
835
836 extern short distance(short a, short b);
837 extern short ptype_distance(short ptyp, short f, short t);
838 extern short piece_distance(short side, short piece, short f, short t);
839
840 #if defined UNKNOWN
841 #  undef UNKNOWN
842 #endif
843
844 #define UNKNOWN      'U'
845 #define STATIC_ROOK  'S'
846 #define RANGING_ROOK 'R'
847
848 extern char GameType[2];
849 void ShowGameType(void);
850
851 extern unsigned short bookmaxply;
852 extern unsigned int bookcount;
853 extern unsigned int booksize;
854 extern unsigned long hashkey, hashbd;
855
856 typedef struct hashval hashcode_array[2][NO_PIECES][NO_SQUARES];
857 typedef struct hashval drop_hashcode_array[2][NO_PIECES][NO_SQUARES];
858
859 extern hashcode_array  *hashcode;
860 extern drop_hashcode_array  *drop_hashcode;
861
862 #ifdef QUIETBACKGROUND
863 extern short background;
864 #endif /* QUIETBACKGROUND */
865
866 #if ttblsz
867 extern short use_ttable;
868 extern struct hashentry  *ttable[2];
869 #endif
870
871 /*
872  * hashbd contains a 32 bit "signature" of the board position. hashkey
873  * contains a 16 bit code used to address the hash table. When a move is
874  * made, XOR'ing the hashcode of moved piece on the from and to squares with
875  * the hashbd and hashkey values keeps things current.
876  */
877
878 #define UpdateHashbd(side, piece, f, t) \
879 { \
880   if ((f) >= 0) \
881     { \
882       hashbd  ^= (*hashcode)[side][piece][f].bd;  \
883       hashkey ^= (*hashcode)[side][piece][f].key; \
884     } \
885  \
886   if ((t) >= 0) \
887     { \
888       hashbd  ^= (*hashcode)[side][piece][t].bd;  \
889       hashkey ^= (*hashcode)[side][piece][t].key; \
890     } \
891 }
892
893 #define UpdateDropHashbd(side, piece, count) \
894 { \
895   hashbd  ^= (*drop_hashcode)[side][piece][count].bd;  \
896   hashkey ^= (*drop_hashcode)[side][piece][count].key; \
897 }
898
899
900 extern short rpthash[2][256];
901 extern char *DRAW;
902
903 extern char* DRAW_REPETITION;
904 extern char *DRAW_MAXMOVES;
905 extern char *DRAW_JUSTDRAW;
906
907 #define row(a)     ((a) / NO_COLS)
908 #define column(a)  ((a) % NO_COLS)
909 #define locn(a, b) (((a) * NO_COLS) + b)
910
911 /* init external functions */
912 extern void InitConst(char *lang); /* init.c */
913 extern int  Initialize_data(void); /* init.c */
914 extern void Free_data(void);       /* init.c */
915 extern int  Lock_data(void);       /* init.c */
916 extern void Unlock_data(void);     /* init.c */
917 extern void Initialize_dist(void); /* init.c */
918 extern void Initialize_eval(void); /* eval.c */
919 extern void NewGame(void);
920 extern int  parse(FILE * fd, unsigned short *mv,
921                   short side, char *opening);
922 extern void GetOpenings(void);
923 extern int  OpeningBook(unsigned short *hint, short side);
924
925 typedef enum
926 {
927     REMOVE_PIECE = 1, ADD_PIECE
928 } UpdatePieceList_mode;
929
930 extern void
931 UpdatePieceList(short side, short sq, UpdatePieceList_mode iop);
932
933 typedef enum
934 {
935     FOREGROUND_MODE = 1, BACKGROUND_MODE
936 } SelectMove_mode;
937
938 extern void
939 SelectMove(short side, SelectMove_mode iop);
940
941 extern int
942 search(short side,
943        short ply,
944        short depth,
945        short alpha,
946        short beta,
947        unsigned short *bstline,
948        short *rpt);
949
950 #ifdef CACHE
951 void PutInEETable(short side, int score);
952 int  CheckEETable(short side);
953 int  ProbeEETable(short side, short *score);
954 #endif
955
956 #if ttblsz
957 extern int
958 ProbeTTable(short side,
959             short depth,
960             short ply,
961             short *alpha,
962             short *beta,
963             short *score);
964
965 extern int
966 PutInTTable(short side,
967             short score,
968             short depth,
969             short ply,
970             short alpha,
971             short beta,
972             unsigned short mv);
973
974 extern void ZeroTTable(void);
975 extern void ZeroRPT(void);
976 extern void Initialize_ttable(void);
977 extern unsigned int urand(void);
978
979 #  ifdef HASHFILE
980 extern void gsrand(unsigned int);
981
982 extern int
983 ProbeFTable(short side,
984             short depth,
985             short ply,
986             short *alpha,
987             short *beta,
988             short *score);
989
990 extern void
991 PutInFTable(short side,
992             short score,
993             short depth,
994             short ply,
995             short alpha,
996             short beta,
997             unsigned short f,
998             unsigned short t);
999
1000 #  endif /* HASHFILE */
1001 #endif /* ttblsz */
1002
1003 #if !defined SAVE_NEXTPOS
1004 extern void Initialize_moves(void);
1005 #endif
1006
1007 extern short generate_move_flags;
1008
1009 extern void MoveList(short side, short ply,
1010                      short in_check, short blockable);
1011 extern void CaptureList(short side, short ply,
1012                         short in_check, short blockable);
1013
1014 /* from attacks.c */
1015 extern int
1016 SqAttacked(short square, short side, short *blockable);
1017
1018 extern void
1019 MakeMove(short side,
1020          struct leaf  *node,
1021          short *tempb,
1022          short *tempc,
1023          short *tempsf,
1024          short *tempst,
1025          short *INCscore);
1026
1027 extern void
1028 UnmakeMove(short side,
1029            struct leaf  *node,
1030            short *tempb,
1031            short *tempc,
1032            short *tempsf,
1033            short *tempst);
1034
1035 extern void
1036 InitializeStats(void);
1037
1038 extern int
1039 evaluate(short side,
1040          short ply,
1041          short alpha,
1042          short beta,
1043          short INCscore,
1044          short *InChk,
1045          short *blockable);
1046
1047 extern short ScorePosition(short side);
1048 extern void  ExaminePosition(short side);
1049 extern short ScorePatternDistance(short side);
1050 extern void  DetermineStage(short side);
1051 extern void  UpdateWeights(short side);
1052 extern int   InitMain(void);
1053 extern void  ExitMain(void);
1054 extern void  Initialize(void);
1055 extern void  InputCommand(char *command);
1056 extern void  ExitShogi(void);
1057 extern void  ClearScreen(void);
1058 extern void  SetTimeControl(void);
1059 extern void  SelectLevel(char *sx);
1060
1061 extern void
1062 UpdateDisplay(short f,
1063               short t,
1064               short flag,
1065               short iscastle);
1066
1067 typedef enum
1068 {
1069     COMPUTE_AND_INIT_MODE = 1, COMPUTE_MODE
1070 #ifdef INTERRUPT_TEST
1071     , INIT_INTERRUPT_MODE, COMPUTE_INTERRUPT_MODE
1072 #endif
1073 } ElapsedTime_mode;
1074
1075 extern void  SetResponseTime(short side);
1076 extern void  CheckForTimeout(int score, int globalscore,
1077                              int Jscore, int zwndw);
1078 extern void  ShowSidetoMove(void);
1079 extern void  ShowResponseTime(void);
1080 extern void  ShowPatternCount(short side, short n);
1081 extern void  SearchStartStuff(short side);
1082 extern void  ShowDepth(char ch);
1083 extern void  TerminateSearch(int);
1084 extern void  ShowResults(short score, unsigned short *bstline, char ch);
1085 extern void  SetupBoard(void);
1086 extern void  algbr(short f, short t, short flag);
1087 extern void  OutputMove(void);
1088 extern void  ShowCurrentMove(short pnt, short f, short t);
1089 extern void  ListGame(void);
1090 extern void  ShowMessage(char *s);
1091 extern void  ClearScreen(void);
1092 extern void  DoDebug(void);
1093 extern void  DoTable(short table[NO_SQUARES]);
1094 extern void  ShowPostnValues(void);
1095 extern void  ChangeXwindow(void);
1096 extern void  SetContempt(void);
1097 extern void  ChangeHashDepth(void);
1098 extern void  ChangeBetaWindow(void);
1099 extern void  GiveHint(void);
1100 extern void  ShowPrompt(void);
1101 extern void  EditBoard(void);
1102 extern void  help(void);
1103 extern void  ChangeSearchDepth(void);
1104 extern void  skip(void);
1105 extern void  skipb(void);
1106 extern void  EnPassant(short xside, short f, short t, short iop);
1107 extern void  ShowNodeCnt(long NodeCnt);
1108 extern void  ShowLine(unsigned short *bstline);
1109 extern int   pick(short p1, short p2);
1110 extern short repetition(void);
1111 extern void  TimeCalc(void);
1112 extern void  ElapsedTime(ElapsedTime_mode iop);
1113
1114 extern short
1115 DropPossible(short piece, short side, short sq); /* genmoves.c */
1116
1117 extern short
1118 IsCheckmate(short side, short in_check,
1119             short blockable); /* genmoves.c */
1120
1121
1122 typedef enum
1123 {
1124     VERIFY_AND_MAKE_MODE, VERIFY_AND_TRY_MODE, UNMAKE_MODE
1125 } VerifyMove_mode;
1126
1127 extern int VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv);
1128 extern unsigned short TTage;
1129
1130 #include "dspwrappers.h"   /* Display functions. */
1131
1132 #endif /* _GNUSHOGI_H_ */