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