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