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