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