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