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