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