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