home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk790.lha / UChess / UChessSrc.lha / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-18  |  13.7 KB  |  575 lines

  1. /*
  2.  * init.c - C source 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. #include "gnuchess.h"
  24.  
  25. #ifdef AMIGA
  26. #include <proto/exec.h>
  27. #include <proto/dos.h>
  28. #include <proto/intuition.h>
  29. extern int procpri;
  30. extern struct Process *myproc;
  31. #endif
  32.  
  33. unsigned int urand (void);
  34.  
  35.  
  36. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  37.  
  38.  
  39. extern unsigned int TTadd;
  40.  
  41. int FirstTime=1;
  42. extern int SupervisorMode;
  43. extern int IllegalMove;
  44. extern int CheckIllegal;
  45. #ifdef LONGINTS2
  46. INTSIZE  __far distdata[64][64];
  47. INTSIZE  __far taxidata[64][64];
  48. #else
  49. INTSIZE  distdata[64][64];
  50. INTSIZE  taxidata[64][64];
  51. #endif
  52.  
  53. #ifdef KILLT
  54. /* put moves to the center first */
  55. void
  56. Initialize_killt (void)
  57. {
  58.   REG unsigned INTSIZE f, t, s;
  59.   REG INTSIZE d;
  60.   for (f = 64; f--;)
  61.     for (t = 64; t--;)
  62.       {
  63.     d = taxidata[f][0x1b];
  64.     if (taxidata[f][0x1c] < d)
  65.       d = taxidata[f][0x1c];
  66.     if (taxidata[f][0x23] < d)
  67.       d = taxidata[f][0x23];
  68.     if (taxidata[f][0x24] < d)
  69.       d = taxidata[f][0x24];
  70.     s = d;
  71.     d = taxidata[t][0x1b];
  72.     if (taxidata[t][0x1c] < d)
  73.       d = taxidata[t][0x1c];
  74.     if (taxidata[t][0x23] < d)
  75.       d = taxidata[t][0x23];
  76.     if (taxidata[t][0x24] < d)
  77.       d = taxidata[t][0x24];
  78.     s -= d;
  79.     killt[(f << 8) | t] = s;
  80.     killt[(f << 8) | t | 0x80] = s;
  81.       }
  82. }
  83. #endif
  84. void
  85. Initialize_dist (void)
  86. {
  87.   REG INTSIZE a, b, d, di;
  88.  
  89.   for (a = 0; a < 64; a++)
  90.     for (b = 0; b < 64; b++)
  91.       {
  92.     d = abs (column (a) - column (b));
  93.     di = abs (row (a) - row (b));
  94.     taxidata[a][b] = d + di;
  95.     distdata[a][b] = (d > di ? d : di);
  96.       }
  97. #ifdef KILLT
  98.   Initialize_killt ();
  99. #endif
  100. }
  101.  
  102. const INTSIZE Stboard[64] =
  103. {rook, knight, bishop, queen, king, bishop, knight, rook,
  104.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  105.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  107.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  108.  rook, knight, bishop, queen, king, bishop, knight, rook};
  109. const INTSIZE Stcolor[64] =
  110. {white, white, white, white, white, white, white, white,
  111.  white, white, white, white, white, white, white, white,
  112.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  113.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  114.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  115.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  116.  black, black, black, black, black, black, black, black,
  117.  black, black, black, black, black, black, black, black};
  118. INTSIZE board[64], color[64];
  119.  
  120. /* given epsquare, from where can a pawn be taken? */
  121. const INTSIZE epmove1[64] =
  122. {0, 1, 2, 3, 4, 5, 6, 7,
  123.  8, 9, 10, 11, 12, 13, 14, 15,
  124.  16, 24, 25, 26, 27, 28, 29, 30,
  125.  24, 25, 26, 27, 28, 29, 30, 31,
  126.  32, 33, 34, 35, 36, 37, 38, 39,
  127.  40, 32, 33, 34, 35, 36, 37, 38,
  128.  48, 49, 50, 51, 52, 53, 54, 55,
  129.  56, 57, 58, 59, 60, 61, 62, 63};
  130. const INTSIZE epmove2[64] =
  131. {0, 1, 2, 3, 4, 5, 6, 7,
  132.  8, 9, 10, 11, 12, 13, 14, 15,
  133.  25, 26, 27, 28, 29, 30, 31, 23,
  134.  24, 25, 26, 27, 28, 29, 30, 31,
  135.  32, 33, 34, 35, 36, 37, 38, 39,
  136.  33, 34, 35, 36, 37, 38, 39, 47,
  137.  48, 49, 50, 51, 52, 53, 54, 55,
  138.  56, 57, 58, 59, 60, 61, 62, 63};
  139.  
  140.  
  141. /*
  142.  * nextpos[piece][from-square] , nextdir[piece][from-square] gives vector of
  143.  * positions reachable from from-square in ppos with piece such that the
  144.  * sequence    ppos = nextpos[piece][from-square]; pdir =
  145.  * nextdir[piece][from-square]; u = ppos[sq]; do { u = ppos[u]; if(color[u]
  146.  * != neutral) u = pdir[u]; } while (sq != u); will generate the sequence of
  147.  * all squares reachable from sq.
  148.  *
  149.  * If the path is blocked u = pdir[sq] will generate the continuation of the
  150.  * sequence in other directions.
  151.  */
  152.  
  153. unsigned char __far nextpos[8][64][64];
  154. unsigned char __far nextdir[8][64][64];
  155.  
  156. /*
  157.  * ptype is used to separate white and black pawns, like this; ptyp =
  158.  * ptype[side][piece] piece can be used directly in nextpos/nextdir when
  159.  * generating moves for pieces that are not black pawns.
  160.  */
  161. const INTSIZE ptype[2][8] =
  162. {
  163.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  164.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  165.  
  166. /* data used to generate nextpos/nextdir */
  167. static const INTSIZE direc[8][8] =
  168. {
  169.   0, 0, 0, 0, 0, 0, 0, 0,
  170.   10, 9, 11, 0, 0, 0, 0, 0,
  171.   8, -8, 12, -12, 19, -19, 21, -21,
  172.   9, 11, -9, -11, 0, 0, 0, 0,
  173.   1, 10, -1, -10, 0, 0, 0, 0,
  174.   1, 10, -1, -10, 9, 11, -9, -11,
  175.   1, 10, -1, -10, 9, 11, -9, -11,
  176.   -10, -9, -11, 0, 0, 0, 0, 0};
  177. static const INTSIZE max_steps[8] =
  178. {0, 2, 1, 7, 7, 7, 1, 2};
  179. static const INTSIZE nunmap[120] =
  180. {
  181.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  182.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  183.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  184.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  185.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  186.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  187.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  188.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  189.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  190.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  191.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  192.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  193.  
  194. int InitFlag = false;
  195. void
  196. Initialize_moves (void)
  197.  
  198. /*
  199.  * This procedure pre-calculates all moves for every piece from every square.
  200.  * This data is stored in nextpos/nextdir and used later in the move
  201.  * generation routines.
  202.  */
  203.  
  204. {
  205.   INTSIZE ptyp, po, p0, d, di, s, delta;
  206.   unsigned char *ppos, *pdir;
  207.   INTSIZE dest[8][8];
  208.   INTSIZE steps[8];
  209.   INTSIZE sorted[8];
  210.  
  211.   for (ptyp = 0; ptyp < 8; ptyp++)
  212.     for (po = 0; po < 64; po++)
  213.       for (p0 = 0; p0 < 64; p0++)
  214.     {
  215.       nextpos[ptyp][po][p0] = (unsigned char) po;
  216.       nextdir[ptyp][po][p0] = (unsigned char) po;
  217.     }
  218.   for (ptyp = 1; ptyp < 8; ptyp++)
  219.     for (po = 21; po < 99; po++)
  220.       if (nunmap[po] >= 0)
  221.     {
  222.       ppos = nextpos[ptyp][nunmap[po]];
  223.       pdir = nextdir[ptyp][nunmap[po]];
  224.       /* dest is a function of direction and steps */
  225.       for (d = 0; d < 8; d++)
  226.         {
  227.           dest[d][0] = nunmap[po];
  228.           delta = direc[ptyp][d];
  229.           if (delta != 0)
  230.         {
  231.           p0 = po;
  232.           for (s = 0; s < max_steps[ptyp]; s++)
  233.             {
  234.               p0 = p0 + delta;
  235.  
  236.               /*
  237.                * break if (off board) or (pawns only move two
  238.                * steps from home square)
  239.                */
  240.               if ((nunmap[p0] < 0) || (((ptyp == pawn) || (ptyp == bpawn))
  241.                            && ((s > 0) && ((d > 0) || (Stboard[nunmap[po]] != pawn)))))
  242.             break;
  243.               else
  244.             dest[d][s] = nunmap[p0];
  245.             }
  246.         }
  247.           else
  248.         s = 0;
  249.  
  250.           /*
  251.            * sort dest in number of steps order currently no sort
  252.            * is done due to compability with the move generation
  253.            * order in old gnu chess
  254.            */
  255.           steps[d] = s;
  256.           for (di = d; s > 0 && di > 0; di--)
  257.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  258.           sorted[di] = sorted[di - 1];
  259.         else
  260.           break;
  261.           sorted[di] = d;
  262.         }
  263.  
  264.       /*
  265.        * update nextpos/nextdir, pawns have two threads (capture
  266.        * and no capture)
  267.        */
  268.       p0 = nunmap[po];
  269.       if (ptyp == pawn || ptyp == bpawn)
  270.         {
  271.           for (s = 0; s < steps[0]; s++)
  272.         {
  273.           ppos[p0] = (unsigned char) dest[0][s];
  274.           p0 = dest[0][s];
  275.         }
  276.           p0 = nunmap[po];
  277.           for (d = 1; d < 3; d++)
  278.         {
  279.           pdir[p0] = (unsigned char) dest[d][0];
  280.           p0 = dest[d][0];
  281.         }
  282.         }
  283.       else
  284.         {
  285.           pdir[p0] = (unsigned char) dest[sorted[0]][0];
  286.           for (d = 0; d < 8; d++)
  287.         for (s = 0; s < steps[sorted[d]]; s++)
  288.           {
  289.             ppos[p0] = (unsigned char) dest[sorted[d]][s];
  290.             p0 = dest[sorted[d]][s];
  291.             if (d < 7)
  292.               pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  293.  
  294.             /*
  295.              * else is already initialized
  296.              */
  297.           }
  298.         }
  299.     }
  300. }
  301.  
  302. void
  303. NewGame (void)
  304.  
  305. /*
  306.  * Reset the board and other variables to start a new game.
  307.  */
  308.  
  309. {
  310.   INTSIZE l, c, p;
  311.   int tmp;
  312.   char tstr[32];
  313.  
  314.  if (AmigaStarted)
  315.   {
  316.    if (FirstTime)
  317.     {
  318.      FirstTime = 0;
  319.     }
  320.    else
  321.     {
  322.      if (!LoadFullBitMap())
  323.       {
  324.        ExitChess();
  325.       }
  326.      (void)SetTaskPri((struct Task *)myproc,procpri);
  327.     }
  328.   }
  329. /* SupervisorMode = 0;*/
  330.  CheckIllegal = IllegalMove = 0;
  331.   Mate = 0;
  332.  ThinkAheadDepth = ThinkAheadWorked = 0;
  333.  DrawnGame = 0;
  334.  MateString[0] = '\0';
  335.  
  336.   compptr = oppptr = -1;
  337.   stage = stage2 = -1;        /* the game is not yet started */
  338.   flag.illegal = flag.mate = flag.quit = flag.reverse = flag.bothsides = flag.onemove = flag.force = false;
  339.   flag.material = flag.coords = flag.hash = flag.beep = flag.rcptr = true;
  340.   flag.stars = flag.shade = flag.back = flag.musttimeout = false;
  341. #ifdef CLIENT
  342.   flag.gamein = true;
  343. #else 
  344.   flag.gamein = false;
  345. #endif
  346. #if defined(MSDOS) && !defined(SEVENBIT)
  347.   flag.rv = false;
  348. #else
  349.   flag.rv = true;
  350. #endif /* MSDOS && !SEVENBIT */
  351.  
  352.   mycnt1 = mycnt2 = 0;
  353.   GenCnt = NodeCnt = et0 = epsquare = XCmore = 0;
  354.   dither = 0;
  355.   WAwindow = WAWNDW;
  356.   WBwindow = WBWNDW;
  357.   BAwindow = BAWNDW;
  358.   BBwindow = BBWNDW;
  359.   xwndw = BXWNDW;
  360.   if (!MaxSearchDepth)
  361.     MaxSearchDepth = MAXDEPTH - 1;
  362.   contempt = 0;
  363.   GameCnt = 0;
  364.   Game50 = 1;
  365.   hint = 0x0C14;
  366.   ZeroRPT ();
  367.   Developed[white] = Developed[black] = false;
  368.   MouseDropped = 0;
  369.   Castled[white] = Castled[black] = 0;
  370.   myEnPassant[white] = myEnPassant[black] = 0;
  371.   castld[white] = castld[black] = false;
  372.   PawnThreat[0] = CptrFlag[0] = false;
  373.   Pscore[0] = 12000;
  374.   Tscore[0] = 12000;
  375.   opponent = white;
  376.   computer = black;
  377.   for (l = 0; l < TREE; l++)
  378.     Tree[l].f = Tree[l].t = 0;
  379.  gsrand ((unsigned int) 1);
  380. #ifndef CACHE
  381. #if ttblsz
  382.   ZeroTTable ();
  383. #endif /* ttblsz */
  384. #endif
  385.  srand ((unsigned int) 1);
  386.   if (!InitFlag)
  387.     {
  388.       for (c = white; c <= black; c++)
  389.     for (p = pawn; p <= king; p++)
  390.       for (l = 0; l < 64; l++)
  391.         {
  392.           hashcode[c][p][l].key = (((unsigned long) urand ()));
  393.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 16);
  394.           hashcode[c][p][l].bd = (((unsigned long) urand ()));
  395.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 16);
  396. #ifdef LONG64
  397.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 32);
  398.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 48);
  399.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 32);
  400.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 48);
  401. #endif
  402.         }
  403.     }
  404.   for (l = 0; l < 64; l++)
  405.     {
  406.       board[l] = Stboard[l];
  407.       color[l] = Stcolor[l];
  408.       Mvboard[l] = 0;
  409.     }
  410.   InitializeStats ();
  411.   time0 = time ((long *) 0);
  412.   ElapsedTime (1);
  413.   flag.regularstart = true;
  414.   Book = BOOKFAIL;
  415.   if (!InitFlag)
  416.     {
  417.       if (TCflag)
  418.        {
  419.         strcpy(tstr,"60 10");
  420.     SelectLevel (tstr);
  421.     SetTimeControl ();
  422.        }
  423.       else if (MaxResponseTime == 0)
  424.        {
  425.         strcpy(tstr,"60 10");
  426.     SelectLevel (tstr);
  427.     SetTimeControl ();
  428.        }
  429.       UpdateDisplay (0, 0, 1, 0);
  430.       GetOpenings ();
  431. #ifdef CACHE
  432.       Initialize_ttable();
  433. #endif
  434.       InitFlag = true;
  435.     }
  436.    else
  437.     {
  438.       if (TCflag)
  439.        {
  440.         sprintf(tstr,"%d %d",TCmoves,TCminutes);
  441.     SelectLevel (tstr);
  442.     SetTimeControl ();
  443.        }
  444.     }
  445.   hashbd = hashkey = 0;
  446. #ifdef AMIGA
  447.   tmp = player;
  448.   player = white;
  449.   UpdateClocks();
  450.   player = black;
  451.   UpdateClocks();
  452.   player = tmp;
  453. #endif
  454. #ifdef CACHE
  455. #if ttblsz
  456.   ZeroTTable ();
  457.   TTadd = 0;
  458. #endif /* ttblsz */
  459. #endif
  460. }
  461. void
  462. InitConst (char *lang)
  463. {
  464.   FILE *constfile;
  465.   char s[256];
  466.   char sl[5];
  467.   int len, entry;
  468.   char *p, *q;
  469.   constfile = fopen (LANGFILE, "r");
  470.   if (!constfile)
  471.     {
  472.      /* printf ("NO LANGFILE\n");*/
  473.       exit (1);
  474.     }
  475.   while (fgets (s, sizeof (s), constfile))
  476.     {
  477.       if (s[0] == '!')
  478.     continue;
  479.       len = strlen (s);
  480.       for (q = &s[len]; q > &s[8]; q--)
  481.     if (*q == '}')
  482.       break;
  483.       if (q == &s[8])
  484.     {
  485. /*      printf ("{ error in cinstfile\n");*/
  486.       exit (1);
  487.     }
  488.       *q = '\0';
  489.       if (s[3] != ':' || s[7] != ':' || s[8] != '{')
  490.     {
  491.     /*  printf ("Langfile format error %s\n", s);*/
  492.       exit (1);
  493.     }
  494.       s[3] = s[7] = '\0';
  495.       if (lang == NULL)
  496.     {
  497.       lang = sl;
  498.       strcpy (sl, &s[4]);
  499.     }
  500.       if (strcmp (&s[4], lang))
  501.     continue;
  502.       entry = atoi (s);
  503.       if (entry < 0 || entry >= CPSIZE)
  504.     {
  505.      /* printf ("Langfile number error\n");*/
  506.       exit (1);
  507.     }
  508.       for (q = p = &s[9]; *p; p++)
  509.     {
  510.       if (*p != '\\')
  511.         {
  512.           *q++ = *p;
  513.         }
  514.       else if (*(p + 1) == 'n')
  515.         {
  516.           *q++ = '\n';
  517.           p++;
  518.         }
  519.     }
  520.       *q = '\0';
  521.       if (entry < 0 || entry > 255)
  522.     {
  523.      /* printf ("Langfile error %d\n", entry);*/
  524.       exit (0);
  525.     }
  526.       CP[entry] = (char *) malloc ((unsigned) strlen (&s[9]) + 1);
  527.       if (CP[entry] == NULL)
  528.     {
  529.       perror ("malloc");
  530.       exit (0);
  531.     }
  532.       strcpy (CP[entry], &s[9]);
  533.  
  534.     }
  535.   fclose (constfile);
  536. }
  537.  
  538. #ifndef CACHE
  539. #ifdef ttblsz
  540. void
  541. Initialize_ttable ()
  542. {
  543.   if (rehash < 0)
  544.     rehash = MAXrehash - 1;
  545. }
  546.  
  547. #endif /* ttblsz */
  548.  
  549. #else
  550.  
  551. #ifdef ttblsz
  552. void
  553.  
  554. Initialize_ttable ()
  555. {
  556.   char astr[32];
  557.   int doit = true;
  558.   if (rehash < 0)
  559.     rehash = MAXrehash;
  560. while(doit && ttblsize > (1<<13)){
  561.   ttable[0] = (struct hashentry *)malloc((unsigned)(sizeof(struct hashentry))*(ttblsize+rehash));
  562.   ttable[1] = (struct hashentry *)malloc((unsigned)(sizeof(struct hashentry))*(ttblsize+rehash));
  563.   if(ttable[0] == NULL || ttable[1] == NULL){
  564.   if(ttable[0] != NULL)free(ttable[0]);
  565.   ttblsize = ttblsize>>1;
  566.   } else doit = false;
  567. }
  568.   if(ttable[0] == NULL || ttable[1] == NULL){ perror("memory alloc");exit(1);}
  569.   sprintf(astr,"transposition tbl is %d\n",ttblsize);
  570.   ShowMessage(astr);
  571. }
  572.  
  573. #endif /* ttblsz */
  574. #endif
  575.