home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / jeux / ArcBoard004.arc / !GNUChessX / src / c / init < prev    next >
Text File  |  1995-07-02  |  13KB  |  502 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. #include "ttable.h"
  25. #ifdef HAVE_GETTIMEOFDAY
  26. #include <sys/time.h>
  27. #endif
  28. extern SHORT notime;
  29. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  30.  
  31.  
  32. SHORT distdata[64][64], taxidata[64][64];
  33.  
  34. #ifdef KILLT
  35. /* put moves to the center first */
  36. void
  37. Initialize_killt (void)
  38. {
  39.   register UTSHORT f, t, s;
  40.   register SHORT d;
  41.   for (f = 0; f < 64; f++)
  42.     for (t = 0; t < 64; t++)
  43.       {
  44.     d = taxidata[f][0x1b];
  45.     if (taxidata[f][0x1c] < d)
  46.       d = taxidata[f][0x1c];
  47.     if (taxidata[f][0x23] < d)
  48.       d = taxidata[f][0x23];
  49.     if (taxidata[f][0x24] < d)
  50.       d = taxidata[f][0x24];
  51.     s = d;
  52.     d = taxidata[t][0x1b];
  53.     if (taxidata[t][0x1c] < d)
  54.       d = taxidata[t][0x1c];
  55.     if (taxidata[t][0x23] < d)
  56.       d = taxidata[t][0x23];
  57.     if (taxidata[t][0x24] < d)
  58.       d = taxidata[t][0x24];
  59.     s -= d;
  60.     killt[(f << 8) | t] = s;
  61.     killt[(f << 8) | t | 0x80] = s;
  62.       }
  63. }
  64. #endif
  65. void
  66. Initialize_dist (void)
  67. {
  68.   register SHORT a, b, d, di;
  69.  
  70.   for (a = 0; a < 64; a++)
  71.     for (b = 0; b < 64; b++)
  72.       {
  73.     d = abs (column (a) - column (b));
  74.     di = abs (row (a) - row (b));
  75.     taxidata[a][b] = d + di;
  76.     distdata[a][b] = (d > di ? d : di);
  77.       }
  78. #ifdef KILLT
  79.   Initialize_killt ();
  80. #endif
  81. }
  82.  
  83. const SHORT Stboard[64] =
  84. {rook, knight, bishop, queen, king, bishop, knight, rook,
  85.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  86.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  89.  rook, knight, bishop, queen, king, bishop, knight, rook};
  90. const SHORT Stcolor[64] =
  91. {white, white, white, white, white, white, white, white,
  92.  white, white, white, white, white, white, white, white,
  93.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  94.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  95.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  96.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  97.  black, black, black, black, black, black, black, black,
  98.  black, black, black, black, black, black, black, black};
  99. SHORT board[64], color[64];
  100.  
  101. /* given epsquare, from where can a pawn be taken? */
  102. const SHORT epmove1[64] =
  103. {0, 1, 2, 3, 4, 5, 6, 7,
  104.  8, 9, 10, 11, 12, 13, 14, 15,
  105.  16, 24, 25, 26, 27, 28, 29, 30,
  106.  24, 25, 26, 27, 28, 29, 30, 31,
  107.  32, 33, 34, 35, 36, 37, 38, 39,
  108.  40, 32, 33, 34, 35, 36, 37, 38,
  109.  48, 49, 50, 51, 52, 53, 54, 55,
  110.  56, 57, 58, 59, 60, 61, 62, 63};
  111. const SHORT epmove2[64] =
  112. {0, 1, 2, 3, 4, 5, 6, 7,
  113.  8, 9, 10, 11, 12, 13, 14, 15,
  114.  25, 26, 27, 28, 29, 30, 31, 23,
  115.  24, 25, 26, 27, 28, 29, 30, 31,
  116.  32, 33, 34, 35, 36, 37, 38, 39,
  117.  33, 34, 35, 36, 37, 38, 39, 47,
  118.  48, 49, 50, 51, 52, 53, 54, 55,
  119.  56, 57, 58, 59, 60, 61, 62, 63};
  120.  
  121.  
  122. /*
  123.  * nextpos[piece][from-square] , nextdir[piece][from-square] gives vector of
  124.  * positions reachable from from-square in ppos with piece such that the
  125.  * sequence    ppos = nextpos[piece][from-square]; pdir =
  126.  * nextdir[piece][from-square]; u = ppos[sq]; do { u = ppos[u]; if(color[u]
  127.  * != neutral) u = pdir[u]; } while (sq != u); will generate the sequence of
  128.  * all squares reachable from sq.
  129.  *
  130.  * If the path is blocked u = pdir[sq] will generate the continuation of the
  131.  * sequence in other directions.
  132.  */
  133.  
  134. UCHAR nextpos[8][64][64];
  135. UCHAR nextdir[8][64][64];
  136.  
  137. /*
  138.  * ptype is used to separate white and black pawns, like this; ptyp =
  139.  * ptype[side][piece] piece can be used directly in nextpos/nextdir when
  140.  * generating moves for pieces that are not black pawns.
  141.  */
  142. const SHORT ptype[2][8] =
  143. { { no_piece, pawn, knight, bishop, rook, queen, king, no_piece },
  144.   { no_piece, bpawn, knight, bishop, rook, queen, king, no_piece } };
  145.  
  146. /* data used to generate nextpos/nextdir */
  147. static const SHORT direc[8][8] =
  148. {
  149.    { 0, 0, 0, 0, 0, 0, 0, 0 },
  150.    { 10, 9, 11, 0, 0, 0, 0, 0 },
  151.    { 8, -8, 12, -12, 19, -19, 21, -21 },
  152.    { 9, 11, -9, -11, 0, 0, 0, 0 },
  153.    { 1, 10, -1, -10, 0, 0, 0, 0 },
  154.    { 1, 10, -1, -10, 9, 11, -9, -11 },
  155.    { 1, 10, -1, -10, 9, 11, -9, -11 },
  156.    { -10, -9, -11, 0, 0, 0, 0, 0 } };
  157. static const SHORT max_steps[8] =
  158. {0, 2, 1, 7, 7, 7, 1, 2};
  159. static const SHORT nunmap[120] =
  160. {
  161.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  162.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  163.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  164.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  165.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  166.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  167.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  168.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  169.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  170.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  171.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  172.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  173.  
  174. int InitFlag = false;
  175. void
  176. Initialize_moves (void)
  177.  
  178. /*
  179.  * This procedure pre-calculates all moves for every piece from every square.
  180.  * This data is stored in nextpos/nextdir and used later in the move
  181.  * generation routines.
  182.  */
  183.  
  184. {
  185.   SHORT ptyp, po, p0, d, di, s, delta;
  186.   UCHAR *ppos, *pdir;
  187.   SHORT dest[8][8];
  188.   SHORT steps[8];
  189.   SHORT sorted[8];
  190.  
  191.   for (ptyp = 0; ptyp < 8; ptyp++)
  192.     for (po = 0; po < 64; po++)
  193.       for (p0 = 0; p0 < 64; p0++)
  194.     {
  195.       nextpos[ptyp][po][p0] = (UCHAR) po;
  196.       nextdir[ptyp][po][p0] = (UCHAR) po;
  197.     }
  198.   for (ptyp = 1; ptyp < 8; ptyp++)
  199.     for (po = 21; po < 99; po++)
  200.       if (nunmap[po] >= 0)
  201.     {
  202.       ppos = nextpos[ptyp][nunmap[po]];
  203.       pdir = nextdir[ptyp][nunmap[po]];
  204.       /* dest is a function of direction and steps */
  205.       for (d = 0; d < 8; d++)
  206.         {
  207.           dest[d][0] = nunmap[po];
  208.           delta = direc[ptyp][d];
  209.           if (delta != 0)
  210.         {
  211.           p0 = po;
  212.           for (s = 0; s < max_steps[ptyp]; s++)
  213.             {
  214.               p0 = p0 + delta;
  215.  
  216.               /*
  217.                * break if (off board) or (pawns only move two
  218.                * steps from home square)
  219.                */
  220.               if ((nunmap[p0] < 0) || (((ptyp == pawn) || (ptyp == bpawn))
  221.                            && ((s > 0) && ((d > 0) || (Stboard[nunmap[po]] != pawn)))))
  222.             break;
  223.               else
  224.             dest[d][s] = nunmap[p0];
  225.             }
  226.         }
  227.           else
  228.         s = 0;
  229.  
  230.           /*
  231.            * sort dest in number of steps order currently no sort
  232.            * is done due to compability with the move generation
  233.            * order in old gnu chess
  234.            */
  235.           steps[d] = s;
  236.           for (di = d; s > 0 && di > 0; di--)
  237.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  238.           sorted[di] = sorted[di - 1];
  239.         else
  240.           break;
  241.           sorted[di] = d;
  242.         }
  243.  
  244.       /*
  245.        * update nextpos/nextdir, pawns have two threads (capture
  246.        * and no capture)
  247.        */
  248.       p0 = nunmap[po];
  249.       if (ptyp == pawn || ptyp == bpawn)
  250.         {
  251.           for (s = 0; s < steps[0]; s++)
  252.         {
  253.           ppos[p0] = (UCHAR) dest[0][s];
  254.           p0 = dest[0][s];
  255.         }
  256.           p0 = nunmap[po];
  257.           for (d = 1; d < 3; d++)
  258.         {
  259.           pdir[p0] = (UCHAR) dest[d][0];
  260.           p0 = dest[d][0];
  261.         }
  262.         }
  263.       else
  264.         {
  265.           pdir[p0] = (UCHAR) dest[sorted[0]][0];
  266.           for (d = 0; d < 8; d++)
  267.         for (s = 0; s < steps[sorted[d]]; s++)
  268.           {
  269.             ppos[p0] = (UCHAR) dest[sorted[d]][s];
  270.             p0 = dest[sorted[d]][s];
  271.             if (d < 7)
  272.               pdir[p0] = (UCHAR) dest[sorted[d + 1]][0];
  273.  
  274.             /*
  275.              * else is already initialized
  276.              */
  277.           }
  278.         }
  279.     }
  280. }
  281.  
  282. void
  283. NewGame (void)
  284.  
  285. /*
  286.  * Reset the board and other variables to start a new game.
  287.  */
  288.  
  289. {
  290.   SHORT l;
  291. #ifdef HAVE_GETTIMEOFDAY
  292.   struct timeval tv;
  293. #endif
  294. #ifdef CLIENT
  295.   if(GameCnt >0)ListGame();
  296.   fflush(stdout);
  297. #endif
  298.   compptr = oppptr = 0;
  299.   stage = stage2 = -1;        /* the game is not yet started */
  300.   notime = true;
  301. flag.illegal=flag.mate=flag.quit=flag.bothsides=flag.onemove=flag.force=flag.back=flag.musttimeout=false;
  302. flag.easy = true;
  303.  
  304. flag.verydeep= flag.neweval= flag.threat= flag.deepnull= flag.pvs = true; /* TomV */
  305. flag.pvs = false;
  306. #ifdef DEBUG
  307.  flag.nott = flag.noft = flag.nocache = false;
  308. #endif
  309.  
  310. #ifdef CLIENT
  311.   flag.gamein = true;
  312.   flag.post = true;
  313. #else 
  314.   flag.gamein = false;
  315. #endif
  316.   mycnt1 = mycnt2 = 0;
  317.   GenCnt = NodeCnt = et0 = epsquare =  dither =  XCmore = 0;
  318.   contempt = -200;
  319.   WAwindow = WAWNDW;
  320.   WBwindow = WBWNDW;
  321.   BAwindow = BAWNDW;
  322.   BBwindow = BBWNDW;
  323.   xwndw = BXWNDW;
  324.   if (!MaxSearchDepth)
  325.     MaxSearchDepth = MAXDEPTH - 1;
  326.   contempt = 0;
  327.   GameCnt = 0;
  328.   Game50 = 1;
  329.   hint = 0x0C14;
  330.   ZeroRPT ();
  331.   Developed[white] = Developed[black] = false;
  332.   castld[white] = castld[black] = false;
  333.   PawnThreat[0] = CptrFlag[0] = false;
  334.   Pscore[0] = Tscore[0] = 12000;
  335.   opponent = white;
  336.   computer = black;
  337.   for (l = 0; l < TREE; l++)
  338.     Tree[l].f = Tree[l].t = 0;
  339.   if (!InitFlag)
  340.     InitHashCode((unsigned int)1);
  341.   for (l = 0; l < 64; l++)
  342.     {
  343.       board[l] = Stboard[l];
  344.       color[l] = Stcolor[l];
  345.       Mvboard[l] = 0;
  346.     }
  347.   ClrScreen ();
  348.   InitializeStats ();
  349. #ifdef HAVE_GETTIMEOFDAY
  350.   gettimeofday(&tv, NULL);
  351.   time0 = tv.tv_sec*100+tv.tv_usec/10000;
  352. #else
  353.   time0 = time ((long *) 0);
  354. #endif
  355.   ElapsedTime (1);
  356.   flag.regularstart = true;
  357.   Book = BOOKFAIL;
  358.   TimeControl.clock[white] = TimeControl.clock[black] = 0;
  359.   SetTimeControl();
  360.   
  361.   if (!InitFlag)
  362.     {
  363.     CHAR sx[256];
  364.     strcpy(sx,CP[169]);
  365.       if (!TCflag && MaxResponseTime == 0) SelectLevel (sx);
  366.       UpdateDisplay (0, 0, 1, 0);
  367.       GetOpenings ();
  368. #ifdef CACHE
  369.     etab[0] = (struct etable *)malloc(ETABLE*sizeof(struct etable));
  370.     etab[1] = (struct etable *)malloc(ETABLE*sizeof(struct etable));
  371.     if(etab[0] == NULL || etab[1] == NULL){ perror("can't alloc etab");exit(1);}
  372. #endif
  373. #if ttblsz
  374.       Initialize_ttable();
  375. #endif
  376.       InitFlag = true;
  377.     }
  378. #if ttblsz
  379.     ZeroTTable(0);
  380. #endif /* ttblsz */
  381. #ifdef CACHE
  382.    memset ((CHAR *) etab[0], 0, ETABLE*sizeof(struct etable));
  383.    memset ((CHAR *) etab[1], 0, ETABLE*sizeof(struct etable));
  384. #endif
  385. #ifdef NODITHER
  386.   PCRASH = PCRASHS;
  387.   PCENTER = PCENTERS;
  388. #else
  389.   PCRASH = PCRASHS + (dither?(rand() % PCRASHV):0);
  390.   PCENTER = PCENTERS + (dither?(rand() % PCENTERV):0);
  391. #endif
  392.   return;
  393. }
  394.  
  395. void
  396. InitConst (CHAR *lang)
  397. {
  398.   FILE *constfile;
  399.   CHAR s[512];
  400.   CHAR sl[5];
  401.   int len, entry;
  402.   CHAR *p, *q;
  403.   constfile = fopen (LANGFILE, "r");
  404. flag.illegal=flag.mate=flag.post=flag.quit=flag.reverse=flag.bothsides=flag.onemove=flag.force=false;
  405. flag.material=flag.coords=flag.hash=flag.easy=flag.beep=flag.rcptr=true;
  406. flag.autolist=flag.stars=flag.shade=flag.back=flag.musttimeout=false;
  407. #ifdef CLIENT
  408.   flag.gamein = true;
  409.   flag.post = true;
  410. #else 
  411.   flag.gamein = false;
  412. #endif
  413. #if defined(MSDOS) && !defined(SEVENBIT)
  414.   flag.rv = false;
  415. #else
  416.   flag.rv = true;
  417. #endif /* MSDOS && !SEVENBIT */
  418.   if (!constfile)
  419.     {
  420.     constfile = fopen("../misc/gnuchess.lang", "r");
  421.     }
  422.   if (!constfile)
  423.     {
  424.       printf ("NO LANGFILE (file gnuchess.lang not found)\n");
  425.       exit (1);
  426.     }
  427.   while (fgets (s, sizeof (s), constfile))
  428.     {
  429.       if (s[0] == '!') continue;
  430.       len = strlen (s);
  431.       for (q = &s[len]; q > &s[8]; q--) if (*q == '}') break;
  432.       if (q == &s[8])
  433.     {
  434.       printf ("{ error in constfile\n");
  435.       exit (1);
  436.     }
  437.       *q = '\0';
  438.       if (s[3] != ':' || s[7] != ':' || s[8] != '{')
  439.     {
  440.       printf ("Langfile format error %s\n", s);
  441.       exit (1);
  442.     }
  443.       s[3] = s[7] = '\0';
  444.       if (lang == NULL)
  445.     {
  446.       lang = sl;
  447.       strcpy (sl, &s[4]);
  448.     }
  449.       if (strcmp (&s[4], lang))
  450.     continue;
  451.       entry = atoi (s);
  452.       if (entry < 0 || entry >= CPSIZE)
  453.     {
  454.       printf ("Langfile number error\n");
  455.       exit (1);
  456.     }
  457.       for (q = p = &s[9]; *p; p++)
  458.     {
  459.       if (*p != '\\')
  460.         {
  461.           *q++ = *p;
  462.         }
  463.       else if (*(p + 1) == 'n')
  464.         {
  465.           *q++ = '\n';
  466.           p++;
  467.         }
  468.     }
  469.       *q = '\0';
  470.       if (entry < 0 || entry > 255)
  471.     {
  472.       printf ("Langfile error %d\n", entry);
  473.       exit (0);
  474.     }
  475.       CP[entry] = (CHAR *) malloc ((unsigned) strlen (&s[9]) + 1);
  476.       if (CP[entry] == NULL)
  477.     {
  478.       perror ("malloc");
  479.       exit (0);
  480.     }
  481.       strcpy (CP[entry], &s[9]);
  482.  
  483.     }
  484.   fclose (constfile);
  485. }
  486. #if defined (ALLOCATE)
  487. void
  488. Initialize_mem()
  489. {
  490.         ALLOCATE(nextpos);
  491.         ALLOCATE(nextdir);
  492. /* #ifdef CACHE */
  493. /*        ALLOCATE(etab); */
  494. /*#endif */
  495. #ifdef HISTORY
  496.         ALLOCATE(history);
  497. #endif
  498.         ALLOCATE(Tree);
  499.         printf("Total free memory = %ld\n", FreeMem());
  500. }
  501. #endif /* ALLOCATE */
  502.