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