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