home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / init.c < prev    next >
Text File  |  1994-04-20  |  14KB  |  482 lines

  1. //
  2. //  Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3. //  Copyright (c) 1988, 1989, 1990  John Stanback
  4. //
  5. //  Project:    OS/2 PM Port of GNU CHESS 4.0 (PmChess)
  6. //
  7. //  Version:    1994-4-17
  8. //
  9. //   Porter:    Ported to Windows 3.0 by Darly Baker
  10. //
  11. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  12. //
  13. //   Porter:    Revised and ported to OS/2 2.1 by Yibing Fan
  14. //
  15. //   System:    OS2 2.1 using emx0.8g 
  16. ////
  17. //  Remarks:    This code converted from Windows to PM using a straight port
  18. //              method with some editing improvements.  Some of the logic
  19. //              required for poor old Windows has been removed since super-
  20. //              duper OS/2 is better with memory...
  21. //
  22. //  Fuctions defined in this module:
  23. //      Initialize_dist (void)
  24. //      Initialize_moves (void)
  25. //      int init_main (HWND hWnd)
  26. //      void NewGame (HWND hWnd)
  27. //
  28. //  License:
  29. //
  30. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  31. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  32. //    the consequences of using it or for whether it serves any particular
  33. //    purpose or works at all, unless he says so in writing.  Refer to the
  34. //    CHESS General Public License for full details.
  35. //
  36. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  37. //    but only under the conditions described in the CHESS General Public
  38. //    License.  A copy of this license is supposed to have been given to you
  39. //    along with CHESS so you can know your rights and responsibilities.  It
  40. //    should be in a file named COPYING.  Among other things, the copyright
  41. //    notice and this notice must be preserved on all copies.
  42. //
  43.  
  44. #define INCL_DOS
  45. #define INCL_PM
  46. #include <os2.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <time.h>
  50. #include "PmChess.h"
  51. #include "GnuChess.h"
  52. #include "Defs.h"
  53.  
  54.  
  55. extern HWND hStats;
  56.  
  57. extern short   distdata[64][64],   taxidata[64][64];
  58.  
  59. extern FILE *hashfile;
  60. extern short stage, stage2, Developed[2];
  61. extern short ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], PawnThreat[MAXDEPTH];
  62. extern short Pscore[MAXDEPTH], Tscore[MAXDEPTH];
  63. extern short rehash, BAwindow, BBwindow, WAwindow, WBwindow;
  64. extern long  Level;
  65. extern unsigned int TTadd;
  66. unsigned int ttbllimit;
  67.  
  68. extern int UseBook;
  69. extern struct hashval   hashcode[2][7][64];
  70.  
  71. //extern unsigned char   * history;
  72.  
  73. extern short board[64], color[64];
  74.  
  75. extern unsigned char   nextpos[8][64][64];
  76. extern unsigned char   nextdir[8][64][64];
  77. /*
  78.   ptype is used to separate white and black pawns, like this;
  79.   ptyp = ptype[side][piece]
  80.   piece can be used directly in nextpos/nextdir when generating moves
  81.   for pieces that are not black pawns.
  82. */
  83. const short ptype[2][8] =
  84. {
  85.   {no_piece, pawn, knight, bishop, rook, queen, king, no_piece},
  86.   {no_piece, bpawn, knight, bishop, rook, queen, king, no_piece}};
  87.  
  88. static short direc[8][8] =
  89. {
  90.   {0, 0, 0, 0, 0, 0, 0, 0},
  91.   {10, 9, 11, 0, 0, 0, 0, 0},
  92.   {8, -8, 12, -12, 19, -19, 21, -21},
  93.   {9, 11, -9, -11, 0, 0, 0, 0},
  94.   {1, 10, -1, -10, 0, 0, 0, 0},
  95.   {1, 10, -1, -10, 9, 11, -9, -11},
  96.   {1, 10, -1, -10, 9, 11, -9, -11},
  97.   {-10, -9, -11, 0, 0, 0, 0, 0}};
  98.  
  99. static short max_steps[8] =
  100. {0, 2, 1, 7, 7, 7, 1, 2};
  101.  
  102. static short nunmap[120] =
  103. {
  104.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  105.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  106.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  107.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  108.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  109.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  110.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  111.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  112.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  113.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  114.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  115.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  116.  
  117. const short Stboard[64] =
  118. {rook, knight, bishop, queen, king, bishop, knight, rook,
  119.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  120.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  121.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  122.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  123.  rook, knight, bishop, queen, king, bishop, knight, rook};
  124.  
  125. const short Stcolor[64] =
  126. {white, white, white, white, white, white, white, white,
  127.  white, white, white, white, white, white, white, white,
  128.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  129.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  130.  black, black, black, black, black, black, black, black,
  131.  black, black, black, black, black, black, black, black};
  132.  
  133. /* given epsquare, from where can a pawn be taken? */
  134. const short epmove1[64] =
  135. {0, 1, 2, 3, 4, 5, 6, 7,
  136.  8, 9, 10, 11, 12, 13, 14, 15,
  137.  16, 24, 25, 26, 27, 28, 29, 30,
  138.  24, 25, 26, 27, 28, 29, 30, 31,
  139.  32, 33, 34, 35, 36, 37, 38, 39,
  140.  40, 32, 33, 34, 35, 36, 37, 38,
  141.  48, 49, 50, 51, 52, 53, 54, 55,
  142.  56, 57, 58, 59, 60, 61, 62, 63};
  143. const short epmove2[64] =
  144. {0, 1, 2, 3, 4, 5, 6, 7,
  145.  8, 9, 10, 11, 12, 13, 14, 15,
  146.  25, 26, 27, 28, 29, 30, 31, 23,
  147.  24, 25, 26, 27, 28, 29, 30, 31,
  148.  32, 33, 34, 35, 36, 37, 38, 39,
  149.  33, 34, 35, 36, 37, 38, 39, 47,
  150.  48, 49, 50, 51, 52, 53, 54, 55,
  151.  56, 57, 58, 59, 60, 61, 62, 63};
  152.  
  153. void
  154. Initialize_dist (void)
  155. {
  156.   register short a, b, d, di;
  157.  
  158.   for (a = 0; a < 64; a++)
  159.     for (b = 0; b < 64; b++)
  160.       {
  161.         d = abs (column (a) - column (b));
  162.         di = abs (row (a) - row (b));
  163.         taxidata[a][b] = d + di;
  164.         distdata[a][b] = (d > di ? d : di);
  165.       }
  166. }
  167.  
  168. int InitFlag = false;
  169. void
  170. Initialize_moves (void)
  171.  
  172. /*
  173.   This procedure pre-calculates all moves for every piece from every square.
  174.   This data is stored in nextpos/nextdir and used later in the move generation
  175.   routines.
  176. */
  177.  
  178. {
  179.   short ptyp, po, p0, d, di, s, delta;
  180.   unsigned char   *ppos,   *pdir;
  181.   short dest[8][8];
  182.   short steps[8];
  183.   short sorted[8];
  184.  
  185.   for (ptyp = 0; ptyp < 8; ptyp++)
  186.     for (po = 0; po < 64; po++)
  187.       for (p0 = 0; p0 < 64; p0++)
  188.         {
  189.       nextpos[ptyp][po][p0] = (UCHAR)po;
  190.       nextdir[ptyp][po][p0] = (UCHAR)po;
  191.         }
  192.   for (ptyp = 1; ptyp < 8; ptyp++)
  193.     for (po = 21; po < 99; po++)
  194.       if (nunmap[po] >= 0)
  195.         {
  196.       ppos = nextpos[ptyp][nunmap[po]];
  197.       pdir = nextdir[ptyp][nunmap[po]];
  198.           /* dest is a function of direction and steps */
  199.           for (d = 0; d < 8; d++)
  200.             {
  201.               dest[d][0] = nunmap[po];
  202.               delta = direc[ptyp][d];
  203.               if (delta != 0)
  204.                 {
  205.                   p0 = po;
  206.                   for (s = 0; s < max_steps[ptyp]; s++)
  207.                     {
  208.                       p0 = p0 + delta;
  209.                       /*
  210.                         break if (off board) or
  211.                         (pawns only move two steps from home square)
  212.                       */
  213.                       if (nunmap[p0] < 0 || ((ptyp == pawn || ptyp == bpawn)
  214.                           && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn)))
  215.                         break;
  216.                       else
  217.                         dest[d][s] = nunmap[p0];
  218.                     }
  219.                 }
  220.               else
  221.                 s = 0;
  222.  
  223.               /*
  224.                 sort dest in number of steps order
  225.                 currently no sort is done due to compability with
  226.                 the move generation order in old gnu chess
  227.               */
  228.               steps[d] = s;
  229.               for (di = d; s > 0 && di > 0; di--)
  230.                 if (steps[sorted[di - 1]] == 0) /* should be: < s */
  231.                   sorted[di] = sorted[di - 1];
  232.                 else
  233.                   break;
  234.               sorted[di] = d;
  235.             }
  236.  
  237.           /*
  238.             update nextpos/nextdir,
  239.             pawns have two threads (capture and no capture)
  240.           */
  241.           p0 = nunmap[po];
  242.           if (ptyp == pawn || ptyp == bpawn)
  243.             {
  244.               for (s = 0; s < steps[0]; s++)
  245.                 {
  246.                   ppos[p0] = (unsigned char) dest[0][s];
  247.                   p0 = dest[0][s];
  248.                 }
  249.               p0 = nunmap[po];
  250.               for (d = 1; d < 3; d++)
  251.                 {
  252.                   pdir[p0] = (unsigned char) dest[d][0];
  253.                   p0 = dest[d][0];
  254.                 }
  255.             }
  256.           else
  257.             {
  258.               pdir[p0] = (unsigned char) dest[sorted[0]][0];
  259.               for (d = 0; d < 8; d++)
  260.                 for (s = 0; s < steps[sorted[d]]; s++)
  261.                   {
  262.                     ppos[p0] = (unsigned char) dest[sorted[d]][s];
  263.                     p0 = dest[sorted[d]][s];
  264.                     if (d < 7)
  265.                       pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  266.                     /* else is already initialized */
  267.                   }
  268.             }
  269.         }
  270. }
  271.  
  272.  
  273. /* hmm.... shouldn`t main be moved to the interface routines */
  274. int init_main (HWND hWnd)
  275. {
  276.   short int ahead = true, hash = true;
  277.   CHAR *Lang=NULL;
  278.   Level = 0;
  279.   TCflag = false;
  280.   OperatorTime = 0;
  281.   Initialize ();
  282.   InitConst (Lang);
  283.   Initialize_dist ();
  284.   Initialize_moves ();
  285.   NewGame (hWnd);
  286.  
  287.   flag.easy = ahead;
  288.   flag.hash = hash;
  289.   hashfile = NULL;
  290.   return (0);
  291. }
  292.  
  293.  
  294. void NewGame (HWND hWnd)
  295.  
  296. /*
  297.   Reset the board and other variables to start a new game.
  298. */
  299.  
  300. {
  301.   short l, c, p;
  302.   compptr = oppptr = 0;
  303.  
  304.   stage = stage2 = -1;          /* the game is not yet started */
  305.  
  306.   if ( flag.post ) {
  307.       WinSendMsg(hStats, WM_SYSCOMMAND, MPFROMSHORT(SC_CLOSE), 0);
  308.       flag.post = false;
  309.   }
  310.  
  311.   flag.mate = flag.quit = flag.reverse = flag.bothsides = false;
  312.   flag.force = false;
  313.   flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  314.   NodeCnt = et0 = epsquare = 0;
  315.   dither = 0;
  316.   WAwindow = 90;
  317.   WBwindow = 90;
  318.   BAwindow = 90;
  319.   BBwindow = 90;
  320.   xwndw = 90;
  321.   MaxSearchDepth = 29;
  322.   contempt = 0;
  323.   GameCnt = 0;
  324.   Game50 = 1;
  325.   hint = 0x0C14;
  326.   ZeroRPT ();
  327.   Developed[white] = Developed[black] = false;
  328.   castld[white] = castld[black] = false;
  329.   PawnThreat[0] = CptrFlag[0] = false;
  330.   Pscore[0] = 12000;
  331.   Tscore[0] = 12000;
  332.   opponent = white;
  333.   computer = black;
  334.   for (l = 0; l < 2000; l++)
  335.       Tree[l].f = Tree[l].t = 0;
  336.   srand ((unsigned int) 1);
  337.   if (!InitFlag)
  338.     {
  339.   for (c = white; c <= black; c++)
  340.     for (p = pawn; p <= king; p++)
  341.       for (l = 0; l < 64; l++)
  342.         {
  343.       hashcode[c][p][l].key = (((unsigned long) urand ()));
  344.       hashcode[c][p][l].key += (((unsigned long) urand ()) << 16);
  345.       hashcode[c][p][l].bd = (((unsigned long) urand ()));
  346.       hashcode[c][p][l].bd += (((unsigned long) urand ()) << 16);
  347.         }
  348.      }
  349.   for (l = 0; l < 64; l++)
  350.     {
  351.       board[l] = Stboard[l];
  352.       color[l] = Stcolor[l];
  353.       Mvboard[l] = 0;
  354.     }
  355.   InitializeStats ();
  356.   time0 = time ((long *) 0);
  357.   ElapsedTime (1);
  358.   flag.regularstart = true;
  359.   if (UseBook) { Book = BOOKFAIL; }
  360.           else { Book = 0; } 
  361.           /* endif */
  362.  
  363.   if (!InitFlag)
  364.     {
  365.       if (TCflag) SetTimeControl ();
  366.       else if (MaxResponseTime == 0)
  367.       {
  368.         OperatorTime = 1;
  369.         TCmoves = 60;
  370.         TCminutes = 5;
  371.         TCflag = (TCmoves > 1);
  372.         SetTimeControl ();
  373.       }
  374.  
  375.       UpdateDisplay (hWnd, 0, 0, 1, 0);
  376.       GetOpenings ( hWnd);
  377.       Initialize_ttable();
  378.       InitFlag = true;
  379.     }
  380.   if(TTadd){ZeroTTable (); TTadd = 0;}
  381. }
  382.  
  383. #ifdef ttblsz
  384.  
  385. void Initialize_ttable ()
  386. {
  387.   int doit = true;
  388.   if (rehash < 0) rehash = MAXrehash;
  389. while(doit && ttblsize > MINTTABLE){
  390.   ttable[0] = (struct hashentry *)malloc((unsigned)(sizeof(struct hashentry)*(ttblsize+rehash)));
  391.   ttable[1] = (struct hashentry *)malloc((unsigned)(sizeof(struct hashentry)*(ttblsize+rehash)));
  392.   if(ttable[0] == NULL || ttable[1] == NULL){
  393.   if(ttable[0] != NULL)free(ttable[0]);
  394.   ttblsize = ttblsize>>1;
  395.   } else doit = false;
  396. }
  397.   if(ttable[0] == NULL || ttable[1] == NULL){ perror("memory alloc");exit(1);}
  398. /*
  399.   printf("transposition table is %d\n",ttblsize);
  400. */
  401.   ttbllimit = (ttblsize<<1) - (ttblsize>>2);
  402. }
  403.  
  404. #endif /* ttblsz */
  405.  
  406. void
  407. InitConst (char *lang)
  408. {
  409.   FILE *constfile;
  410.   char s[256];
  411.   char sl[5];
  412.   int len, entry;
  413.   char *p, *q;
  414.   constfile = fopen (LANGFILE, "r");
  415.   if (!constfile)
  416.     {
  417.       printf ("NO LANGFILE\n");
  418.       exit (1);
  419.     }
  420.   while (fgets (s, sizeof (s), constfile))
  421.     {
  422.       if (s[0] == '!')
  423.     continue;
  424.       len = strlen (s);
  425.       for (q = &s[len]; q > &s[8]; q--)
  426.     if (*q == '}')
  427.       break;
  428.       if (q == &s[8])
  429.     {
  430.       printf ("{ error in cinstfile\n");
  431.       exit (1);
  432.     }
  433.       *q = '\0';
  434.       if (s[3] != ':' || s[7] != ':' || s[8] != '{')
  435.     {
  436.       printf ("Langfile format error %s\n", s);
  437.       exit (1);
  438.     }
  439.       s[3] = s[7] = '\0';
  440.       if (lang == NULL)
  441.     {
  442.       lang = sl;
  443.       strcpy (sl, &s[4]);
  444.     }
  445.       if (strcmp (&s[4], lang))
  446.     continue;
  447.       entry = atoi (s);
  448.       if (entry < 0 || entry >= CPSIZE)
  449.     {
  450.       printf ("Langfile number error\n");
  451.       exit (1);
  452.     }
  453.       for (q = p = &s[9]; *p; p++)
  454.     {
  455.       if (*p != '\\')
  456.         {
  457.           *q++ = *p;
  458.         }
  459.       else if (*(p + 1) == 'n')
  460.         {
  461.           *q++ = '\n';
  462.           p++;
  463.         }
  464.     }
  465.       *q = '\0';
  466.       if (entry < 0 || entry > 255)
  467.     {
  468.       printf ("Langfile error %d\n", entry);
  469.       exit (0);
  470.     }
  471.       CP[entry] = (char *) malloc ((unsigned) strlen (&s[9]) + 1);
  472.       if (CP[entry] == NULL)
  473.     {
  474.       perror ("malloc");
  475.       exit (0);
  476.     }
  477.       strcpy (CP[entry], &s[9]);
  478.  
  479.     }
  480.   fclose (constfile);
  481. }
  482.