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