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