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