home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / SPEL / GC_OS2_M / SRC-M.ZIP / gnuchess.h < prev    next >
Text File  |  1994-02-09  |  19KB  |  584 lines

  1. /*
  2.  * gnuchess.h - Header file for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #if !defined(__STDC__) && !defined(MSDOS)
  25. #define const
  26. #endif
  27.  
  28. #ifndef __GNUC__
  29. #define inline
  30. #endif
  31.  
  32. #define huge
  33.  
  34. #include <stdio.h>
  35.  
  36. #define SEEK_SET 0
  37. #define SEEK_END 2
  38.  
  39. #include <ctype.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #include <time.h>
  43. #include <malloc.h>
  44. #define RWA_ACC "r+b"
  45. #define WA_ACC "w+b"
  46. #define printz printf
  47. #define scanz scanf
  48.  
  49. /* Piece values */
  50. #define valueP 100
  51. #define valueN 350
  52. #define valueB 355
  53. #define valueR 550
  54. #define valueQ 1100
  55. #define valueK 1200
  56. /* masks into upper 8 bits of ataks array */
  57. #define ctlP 0x4000
  58. #define ctlN 0x2800
  59. #define ctlB 0x1800
  60. #define ctlR 0x0400
  61. #define ctlQ 0x0200
  62. #define ctlK 0x0100
  63. #define ctlBQ 0x1200
  64. #define ctlBN 0x0800
  65. #define ctlRQ 0x0600
  66. #define ctlNN 0x2000
  67. /* attack functions */
  68. #define Patak(c, u) (atak[c][u] > ctlP)
  69. #define Anyatak(c, u) (atak[c][u] > 0)
  70. /* distance function */
  71. #define taxicab(a,b) taxidata[a][b]
  72. /* hashtable flags */
  73. #define truescore 0x0001
  74. #define lowerbound 0x0002
  75. #define upperbound 0x0004
  76. #define kingcastle 0x0008
  77. #define queencastle 0x0010
  78. #define evalflag 0x0020
  79. /* king positions */
  80. #define wking PieceList[white][0]
  81. #define bking PieceList[black][0]
  82. #define EnemyKing PieceList[c2][0]
  83. /* constants */
  84. /* castle moves */
  85. #define BLACKCASTLE    0x3C3E
  86. #define WHITECASTLE    0x0406
  87. #define LONGBLACKCASTLE    0x3C3A
  88. #define LONGWHITECASTLE    0x0402
  89. /* truth values */
  90. #define false 0
  91. #define true 1
  92. /* colors */
  93. #define white 0
  94. #define black 1
  95. #define neutral 2
  96. /* piece code defines */
  97. #define no_piece 0
  98. #define pawn 1
  99. #define knight 2
  100. #define bishop 3
  101. #define rook 4
  102. #define queen 5
  103. #define king 6
  104. #define bpawn 7
  105. /* node flags */
  106. #define pmask 0x0007
  107. #define promote 0x0008
  108. #define cstlmask 0x0010
  109. #define epmask 0x0020
  110. #define exact 0x0040
  111. #define pwnthrt 0x0080
  112. #define check 0x0100
  113. #define capture 0x0200
  114. #define draw 0x0400
  115. #define book 0x1000
  116. /* move symbols */
  117. #define pxx (CP[2])
  118. #define qxx (CP[1])
  119. #define rxx (CP[4])
  120. #define cxx (CP[3])
  121. /* for everything that can't use the above */
  122. #define Qxx " pnbrqk"
  123. #define Pxx " PNBRQK"
  124. #define Cxx "abcdefgh"
  125. #define Rxx "12345678"
  126. /***************************************************************************/
  127. /***************** Table limits ********************************************/
  128. /*
  129.  * ttblsz can be a any size. Setting ttblsz 0 removes the transposition
  130.  * tables.
  131.  */
  132. #ifdef MSDOS
  133. #define vttblsz (8001)
  134. #else
  135. #define vttblsz (200001)
  136. #endif /* MSDOS */
  137.  
  138. #define ttblsz vttblsz
  139. #define TREE 1500        /* max number of tree entries */
  140. #define MAXDEPTH 35        /* max depth a search can be carried */
  141. #define MINDEPTH 2        /* min search depth =1 (no hint), >1 hint */
  142. #define MINTTABLE (8000)    /* min ttable size -1 */
  143. #define MAXMOVES 400        /* max number of half moves in a game */
  144. #if defined MSDOS
  145. #define BOOKSIZE 10000        /* Number of unique position/move combinations allowed */
  146. #else
  147. #define BOOKSIZE 250000        /* Number of unique position/move combinations allowed */
  148. #endif
  149. #define CPSIZE 235        /* size of lang file max */
  150. #define ETABLE (10001)        /* static eval cache */
  151. /***************** tuning paramaters **********************************************/
  152. #define MINGAMEIN 4
  153. #define MINMOVES 15
  154. #define CHKDEPTH 1        /* always look forward CHKDEPTH half-moves if in check */
  155. #define DEPTHBEYOND 11        /* Max to go beyond Sdepth */
  156. #define HASHDEPTH 4        /* depth above which to use HashFile */
  157. #define HASHMOVELIMIT 40    /* Use HashFile only for this many moves */
  158. #define PTVALUE 0            /* material value below which pawn threats at 5 & 3 are used */
  159. #define ZDEPTH 3        /* depth beyond which to check ZDELTA for extra time */
  160. #define ZDELTA 40        /* score delta per ply to cause extra time to be given */
  161. #define BESTDELTA 90
  162. /* about 1/2 second worth of nodes for your machine */
  163. #define ZNODES 3000        /* check the time every ZNODES positions */
  164. #define MAXTCCOUNTX  19    /* max number of time clicks per search to complete ply*/
  165. #define MAXTCCOUNTR 8        /* max number of time clicks per search extensions*/
  166. #define SDEPTHLIM Sdepth+2
  167. #define HISTORYLIM 4096        /* Max value of history killer */
  168. #define EWNDW 10        /* Eval window to force position scoring at depth greater than Sdepth + 2 */
  169. #define WAWNDW 90        /* alpha window when computer white*/
  170. #define WBWNDW 90        /* beta window when computer white*/
  171. #define BAWNDW 90        /* alpha window when computer black*/
  172. #define BBWNDW 90        /* beta window when computer black*/
  173. #define BXWNDW 90        /* window to force position scoring at lower */
  174. #define WXWNDW 90        /* window to force position scoring at lower */
  175. #ifdef SMALLRANDY
  176. #define DITHER 2        /* max amount random can alter a pos value */
  177. #else
  178. #define DITHER 5
  179. #endif
  180. #define BBONUS 2        /* points per stage value of B increases */
  181. #define RBONUS 6        /* points per stage value of R increases */
  182. #define KINGPOSLIMIT ( -1)    /* King positional scoring limit */
  183. #define KINGSAFETY  40
  184. #define MAXrehash (7)
  185. #define NULLMOVELIM 4000    /* below this total material on board don't use null move */
  186.  
  187. #if defined AG0
  188. #define WHITEAG0
  189. #define BLACKAG0
  190.  
  191. #elif defined AG1
  192. #define WHITEAG1
  193. #define BLACKAG1
  194.  
  195. #elif defined AG2
  196. #define WHITEAG2
  197. #define BLACKAG2
  198.  
  199. #elif defined AG3
  200. #define WHITEAG3
  201. #define BLACKAG3
  202.  
  203. #elif defined AGB
  204. #define WHITEAG2
  205. #define BLACKAG2
  206.  
  207. #elif defined AG4
  208. #define WHITEAG4
  209. #define BLACKAG4
  210. #endif
  211. /************************* parameters for Opening Book *********************************/
  212. #define BOOKFAIL 8        /* if no book move found for BOOKFAIL turns stop using book */
  213. #define BOOKMAXPLY 28        /* Max plys to keep in book database */
  214. #define BOOKPOCKET 64
  215. #define BOOKRAND 1000        /* used to select an opening move from a list */
  216. #define BOOKENDPCT 950        /* 5 % chance a BOOKEND will stop the book */
  217. #define DONTUSE -32768        /* flag move as don't use */
  218. /*************************** Book access defines ****************************************/
  219. #define SIDEMASK 0x1
  220. #define LASTMOVE 0x4000        /* means this is the last move of an opening */
  221. #define BADMOVE 0x8000        /* means this is a bad move in this position */
  222. /****************************************************************************************/
  223.      struct hashval
  224.      {
  225.        unsigned long key, bd;
  226.      };
  227.      struct hashentry
  228.      {
  229.        unsigned long hashbd;
  230.        unsigned char flags, depth;    /* char saves some space */
  231.        short score;
  232.        unsigned short mv;
  233.  
  234.      };
  235.  
  236. #ifdef HASHFILE
  237.     struct etable
  238.     { unsigned long ehashbd;
  239.         short int escore[2];
  240.         short int sscore[64];
  241.         short int score;
  242.         short int hung[2];
  243.     } ;
  244.  
  245. /*
  246.  * persistent transposition table. The size must be a power of 2. If you
  247.  * change the size, be sure to run gnuchess -c before anything else.
  248.  */
  249. #define frehash 6
  250. #ifdef MSDOS
  251. #define Deffilesz (1 << 11) -1
  252. #else
  253. #define Deffilesz (1 << 17) -1
  254. #endif /* MSDOS */
  255.      struct fileentry
  256.      {
  257.        unsigned char bd[32];
  258.        unsigned char f, t, flags, depth, sh, sl;
  259.      };
  260.  
  261. #endif /* HASHFILE */
  262.  
  263.  
  264.      struct leaf
  265.      {
  266.            short int f;
  267.     short int t;
  268.     short int  score;
  269.     short int  reply;
  270.     short int  width;
  271.            unsigned short flags;
  272.      };
  273.      struct GameRec
  274.      {
  275.        unsigned short gmove;    /* this move */
  276.        short score;        /* score after this move */
  277.        short depth;        /* search depth this move */
  278.        long time;               /* search time this move */
  279.        short piece;        /* piece captured */
  280.        short color;        /* color */
  281.        short flags;        /* move flags capture, promote, castle */
  282.        short Game50;        /* flag for repetition */
  283.        long nodes;        /* nodes searched for this move */
  284.        unsigned long hashkey, hashbd;    /* board key before this move */
  285.        short epssq;        /* epssquare before this move */
  286.      };
  287.      struct TimeControlRec
  288.      {
  289.        short moves[2];
  290.        long clock[2];
  291.      };
  292.  
  293.      struct flags
  294.      {
  295.        short mate;        /* the game is over */
  296.        short post;        /* show principle variation */
  297.        short quit;        /* quit/exit */
  298.        short regularstart;    /* did the game start from standard
  299.                  * initial board ? */
  300.        short reverse;        /* reverse board display */
  301.        short bothsides;        /* computer plays both sides */
  302.        short hash;        /* enable/disable transposition table */
  303.        short force;        /* enter moves */
  304.        short easy;        /* disable thinking on opponents time */
  305.        short beep;        /* enable/disable beep */
  306.        short timeout;        /* time to make a move */
  307.        short musttimeout;    /* time to make a move */
  308.        short back;        /* time to make a move */
  309.        short rcptr;        /* enable/disable recapture heuristics */
  310.        short rv;        /* reverse video */
  311.        short stars;        /* add stars to uxdsp screen */
  312.        short coords;        /* add coords to visual screen */
  313.        short shade;
  314.        short material;        /* draw on lack of material */
  315.        short illegal;        /* illegal position */
  316.        short onemove;        /* timing is onemove */
  317.        short gamein;        /* timing is gamein */
  318.      };
  319.  
  320. #ifdef HISTORY
  321.      extern unsigned short history[32768];
  322. #endif
  323.      extern char *ColorStr[2];
  324.      extern unsigned short int MV[MAXDEPTH];
  325.      extern int MSCORE;
  326.      extern int mycnt1, mycnt2;
  327.      extern short int ahead;
  328.      extern short int chesstool;
  329.      extern struct leaf Tree[], *root,rootnode;
  330.      extern char savefile[], listfile[];
  331.      extern short TrPnt[];
  332.      extern short board[], color[];
  333.      extern short PieceList[2][64], PawnCnt[2][8];
  334.      extern short castld[], Mvboard[];
  335.      extern short svalue[64];
  336.      extern struct flags flag;
  337.      extern short opponent, computer, INCscore;
  338.      extern short WAwindow, BAwindow, WBwindow, BBwindow;
  339.      extern short dither, player;
  340.      extern short xwndw, epsquare, contempt;
  341.      extern long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
  342.      extern long reminus, replus;
  343.      extern unsigned long GenCnt, NodeCnt, ETnodes, EvalNodes, HashCnt, HashAdd, FHashCnt, FHashAdd,
  344.      HashCol, THashCol, filesz, hashmask, hashbase;
  345.      extern short HashDepth, HashMoveLimit;
  346.      extern struct GameRec GameList[];
  347.      extern short GameCnt, Game50;
  348.      extern short Sdepth, MaxSearchDepth;
  349.      extern int Book;
  350.      extern struct TimeControlRec TimeControl;
  351.      extern int TCadd;
  352.      extern short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
  353.      extern int timecomp[MINGAMEIN], timeopp[MINGAMEIN];
  354.      extern int compptr,oppptr;
  355.      extern short XCmore, XCmoves[], XCminutes[], XCseconds[], XC;
  356.      extern const short otherside[];
  357.      extern const short Stboard[];
  358.      extern const short Stcolor[];
  359.      extern unsigned short hint;
  360.      extern short int TOflag;
  361.      extern short stage, stage2, Developed[];
  362.      extern short ChkFlag[], CptrFlag[], PawnThreat[];
  363.      extern short Pscore[], Tscore[];
  364.      extern /*unsigned*/ short rehash;  /* -1 is used as a flag --tpm */
  365.      extern unsigned int ttblsize;
  366.      extern short mtl[], pmtl[], hung[], emtl[];
  367.      extern short Pindex[];
  368.      extern short PieceCnt[];
  369.      extern short FROMsquare, TOsquare;
  370.      extern short HasKnight[], HasBishop[], HasRook[], HasQueen[];
  371.      extern const short qrook[];
  372.      extern const short krook[];
  373.      extern const short kingP[];
  374.      extern const short rank7[];
  375.      extern const short sweep[];
  376.      extern const short epmove1[], epmove2[];
  377.      extern unsigned short killr0[], killr1[];
  378.      extern unsigned short killr2[], killr3[];
  379.      extern unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
  380.      extern short killt[0x4000];
  381.      extern short mtl[2], pmtl[2], hung[2];
  382.      extern const short value[];
  383.      extern const short control[];
  384.      extern unsigned char nextpos[8][64][64];
  385.      extern unsigned char nextdir[8][64][64];
  386.      extern const short ptype[2][8];
  387.      extern FILE *hashfile;
  388.      extern unsigned int starttime;
  389.      extern short distdata[64][64], taxidata[64][64];
  390.      extern union U admin;
  391.      extern short unsigned bookmaxply;
  392.      extern int unsigned bookcount;
  393.      extern int unsigned booksize;
  394.      extern unsigned long hashkey, hashbd;
  395.      extern struct hashval hashcode[2][7][64];
  396.      extern char *CP[];
  397. #ifdef QUIETBACKGROUND
  398.      extern short background;
  399. #endif /* QUIETBACKGROUND */
  400.  
  401. #ifdef ttblsz
  402.      extern struct hashentry *ttable[2];
  403.  
  404. #endif
  405.  
  406. /*
  407.  * hashbd contains a 32 bit "signature" of the board position. hashkey
  408.  * contains a 16 bit code used to address the hash table. When a move is
  409.  * made, XOR'ing the hashcode of moved piece on the from and to squares with
  410.  * the hashbd and hashkey values keeps things current.
  411.  */
  412. #define UpdateHashbd(side, piece, f, t) \
  413. {\
  414.   if ((f) >= 0)\
  415.     {\
  416.       hashbd ^= hashcode[side][piece][f].bd;\
  417.       hashkey ^= hashcode[side][piece][f].key;\
  418.     }\
  419.   if ((t) >= 0)\
  420.     {\
  421.       hashbd ^= hashcode[side][piece][t].bd;\
  422.       hashkey ^= hashcode[side][piece][t].key;\
  423.     }\
  424. }
  425.  
  426.  
  427.      extern short rpthash[2][256];
  428.      extern char *DRAW;
  429.  
  430. #define distance(a,b) distdata[a][b]
  431. #define row(a) ((a) >> 3)
  432. #define column(a) ((a) & 7)
  433. #define locn(a,b) (((a) << 3) | (b))
  434.      extern short distdata[64][64];
  435.  
  436. /* init external functions */
  437.      extern void InitConst (char *lang);
  438.      extern void Initialize_dist (void);
  439.      extern void NewGame (void);
  440.      extern int parse (FILE * fd, short unsigned int *mv, short int side, char *opening);
  441.      extern void GetOpenings (void);
  442.      extern int OpeningBook (unsigned short int *hint, short int side);
  443.      extern void SelectMove (short int side, short int iop);
  444.      extern int
  445.       search (short int side,
  446.            short int ply,
  447.            short int depth,
  448.            short int alpha,
  449.            short int beta,
  450.            short unsigned int *bstline,
  451.            short int *rpt);
  452. #if ttblsz
  453.      extern int
  454.       ProbeTTable (short int side,
  455.             short int depth,
  456.             short int ply,
  457.             short int *alpha,
  458.             short int *beta,
  459.             short int *score);
  460.      extern int
  461.       PutInTTable (short int side,
  462.             short int score,
  463.             short int depth,
  464.             short int ply,
  465.             short int alpha,
  466.             short int beta,
  467.             short unsigned int mv);
  468.      extern void ZeroTTable (void);
  469.      extern void ZeroRPT (void);
  470.      extern void Initialize_ttable (void);
  471.      extern unsigned int urand (void);
  472. #ifdef HASHFILE
  473.      extern void gsrand (unsigned int);
  474.      extern int
  475.       ProbeFTable (short int side,
  476.             short int depth,
  477.             short int ply,
  478.             short int *alpha,
  479.             short int *beta,
  480.             short int *score);
  481.      extern void
  482.       PutInFTable (short int side,
  483.             short int score,
  484.             short int depth,
  485.             short int ply,
  486.             short int alpha,
  487.             short int beta,
  488.             short unsigned int f,
  489.             short unsigned int t);
  490.  
  491. #endif /* HASHFILE */
  492. #endif /* ttblsz */
  493.      extern void Initialize_moves (void);
  494.      extern void MoveList (short int side, short int ply);
  495.      extern void CaptureList (short int side, short int ply);
  496.      extern int castle (short int side, short int kf, short int kt, short int iop);
  497.      extern void ataks (short int side, short int *a);
  498.      extern void
  499.       MakeMove (short int side,
  500.          struct leaf * node,
  501.          short int *tempb,
  502.          short int *tempc,
  503.          short int *tempsf,
  504.          short int *tempst,
  505.          short int *INCscore);
  506.      extern void
  507.       UnmakeMove (short int side,
  508.            struct leaf * node,
  509.            short int *tempb,
  510.            short int *tempc,
  511.            short int *tempsf,
  512.            short int *tempst);
  513.      extern void InitializeStats (void);
  514.      extern int
  515.       evaluate (short int side,
  516.          short int ply,
  517.          short int alpha,
  518.          short int beta,
  519.          short int INCscore,
  520.          short int *InChk);
  521.      extern short int ScorePosition (short int side);
  522.      extern void ExaminePosition (void);
  523.      extern void UpdateWeights (void);
  524.      extern void Initialize (void);
  525.      extern void InputCommand (void);
  526.      extern void ExitChess (void);
  527.      extern void ClrScreen (void);
  528.      extern void SetTimeControl (void);
  529.      extern void SelectLevel (char *sx);
  530.      extern void
  531.       UpdateDisplay (short int f,
  532.               short int t,
  533.               short int flag,
  534.               short int iscastle);
  535.      extern void ElapsedTime (short int iop);
  536.      extern void ShowSidetoMove (void);
  537.      extern void SearchStartStuff (short int side);
  538.      extern void ShowDepth (char ch);
  539.      extern void TerminateSearch (int);
  540.      extern void
  541.       ShowResults (short int score,
  542.             short unsigned int *bstline,
  543.             char ch);
  544.      extern void PromptForMove (void);
  545.      extern void SetupBoard (void);
  546.      extern void algbr (short int f, short int t, short int flag);
  547.      extern void OutputMove (void);
  548.      extern void ShowCurrentMove (short int pnt, short int f, short int t);
  549.      extern void ListGame (void);
  550.      extern void ShowMessage (char *s);
  551.      extern void ClrScreen (void);
  552.      extern void gotoXY (short int x, short int y);
  553.      extern void ClrEoln (void);
  554.      extern void DrawPiece (short int sq);
  555.      extern void UpdateClocks (void);
  556.      extern void DoDebug (void);
  557.      extern void DoTable (short table[64]);
  558.      extern void ShowPostnValues (void);
  559.      extern void ChangeXwindow (void);
  560.      extern void SetContempt (void);
  561.      extern void ChangeHashDepth (void);
  562.      extern void ChangeBetaWindow (void);
  563.      extern void ChangeAlphaWindow (void);
  564.      extern void GiveHint (void);
  565.      extern void ShowPrompt (void);
  566.      extern void EditBoard (void);
  567.      extern void help (void);
  568.      extern void ChangeSearchDepth (void);
  569.      extern void skip (void);
  570.      extern void skipb (void);
  571.      extern void EnPassant (short int xside, short int f, short int t, short int iop);
  572.      extern void ShowNodeCnt (long int NodeCnt);
  573.      extern void ShowLine (short unsigned int *bstline);
  574.      extern int pick (short int p1, short int p2);
  575.      extern int VerifyMove (char *s, short int inp, unsigned short *mv);
  576.      extern void AgeTT();
  577.      extern unsigned short TTage;
  578.      extern struct gdxadmin B;
  579.  
  580. int ms_status(int *x, int *y);
  581. int ms_init(void);
  582. void ms_on(void);
  583. void ms_off(void);
  584.