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