home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame Game Cube 2: Parlor / aztechhalloffamegamecubedisc2-.iso / gnuchess / initiali.c < prev    next >
C/C++ Source or Header  |  1995-02-22  |  14KB  |  491 lines

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