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