home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / 32PMCHES.ZIP / INIT.C < prev    next >
Text File  |  1991-04-30  |  10KB  |  342 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 3.1 (PmChess)
  6. //
  7. //  Version:    1.02  1990-12-17
  8. //
  9. //   Module:    Initialization Logic (Init.c)
  10. //
  11. //   Porter:    Ported to Windows 3.0 by Darly Baker
  12. //
  13. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  14. //
  15. //   Update:    Fixes and enhancements by Benny N. Ormson
  16. //
  17. //   System:    OS2 1.2 using Microsoft C 6.0
  18. //
  19. //  Remarks:    This code converted from Windows to PM using a straight port
  20. //              method with some editing improvements.  Some of the logic
  21. //              required for poor old Windows has been removed since super-
  22. //              duper OS/2 is better with memory...
  23. //
  24. //  License:
  25. //
  26. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  27. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  28. //    the consequences of using it or for whether it serves any particular
  29. //    purpose or works at all, unless he says so in writing.  Refer to the
  30. //    CHESS General Public License for full details.
  31. //
  32. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  33. //    but only under the conditions described in the CHESS General Public
  34. //    License.  A copy of this license is supposed to have been given to you
  35. //    along with CHESS so you can know your rights and responsibilities.  It
  36. //    should be in a file named COPYING.  Among other things, the copyright
  37. //    notice and this notice must be preserved on all copies.
  38. //
  39.  
  40. #define INCL_DOS
  41. #define INCL_PM
  42. #include <os2.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <time.h>
  46. #include "PmChess.h"
  47. #include "GnuChess.h"
  48. #include "Defs.h"
  49.  
  50.  
  51. extern HWND hStats;
  52.  
  53. extern short FAR distdata[64][64], FAR taxidata[64][64];
  54.  
  55. extern FILE *hashfile;
  56. extern short stage, stage2, Developed[2];
  57. extern short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  58. extern short Pscore[maxdepth], Tscore[maxdepth];
  59. extern short rehash;
  60.  
  61. extern struct hashval FAR hashcode[2][7][64];
  62.  
  63. //extern unsigned char FAR * history;
  64.  
  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. }
  79.  
  80. static short Stboard[64] =
  81. {rook, knight, bishop, queen, king, bishop, knight, rook,
  82.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  83.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  86.  rook, knight, bishop, queen, king, bishop, knight, rook};
  87.  
  88. static short Stcolor[64] =
  89. {white, white, white, white, white, white, white, white,
  90.  white, white, white, white, white, white, white, white,
  91.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  92.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  93.  black, black, black, black, black, black, black, black,
  94.  black, black, black, black, black, black, black, black};
  95.  
  96. extern short board[64], color[64];
  97.  
  98. extern unsigned char FAR nextpos[8][64][64];
  99. extern unsigned char FAR nextdir[8][64][64];
  100. /*
  101.   ptype is used to separate white and black pawns, like this;
  102.   ptyp = ptype[side][piece]
  103.   piece can be used directly in nextpos/nextdir when generating moves
  104.   for pieces that are not black pawns.
  105. */
  106. static short ptype[2][8] =
  107. {
  108.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  109.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  110.  
  111. static short direc[8][8] =
  112. {
  113.   0, 0, 0, 0, 0, 0, 0, 0,
  114.   10, 9, 11, 0, 0, 0, 0, 0,
  115.   8, -8, 12, -12, 19, -19, 21, -21,
  116.   9, 11, -9, -11, 0, 0, 0, 0,
  117.   1, 10, -1, -10, 0, 0, 0, 0,
  118.   1, 10, -1, -10, 9, 11, -9, -11,
  119.   1, 10, -1, -10, 9, 11, -9, -11,
  120.   -10, -9, -11, 0, 0, 0, 0, 0};
  121.  
  122. static short max_steps[8] =
  123. {0, 2, 1, 7, 7, 7, 1, 2};
  124.  
  125. static short nunmap[120] =
  126. {
  127.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  128.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  129.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  130.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  131.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  132.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  133.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  134.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  135.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  136.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  137.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  138.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  139.  
  140.  
  141. void
  142. Initialize_moves (void)
  143.  
  144. /*
  145.   This procedure pre-calculates all moves for every piece from every square.
  146.   This data is stored in nextpos/nextdir and used later in the move generation
  147.   routines.
  148. */
  149.  
  150. {
  151.   short ptyp, po, p0, d, di, s, delta;
  152.   unsigned char FAR *ppos, FAR *pdir;
  153.   short dest[8][8];
  154.   short steps[8];
  155.   short sorted[8];
  156.  
  157.   for (ptyp = 0; ptyp < 8; ptyp++)
  158.     for (po = 0; po < 64; po++)
  159.       for (p0 = 0; p0 < 64; p0++)
  160.         {
  161.      nextpos[ptyp][po][p0] = (UCHAR)po;
  162.      nextdir[ptyp][po][p0] = (UCHAR)po;
  163.         }
  164.   for (ptyp = 1; ptyp < 8; ptyp++)
  165.     for (po = 21; po < 99; po++)
  166.       if (nunmap[po] >= 0)
  167.         {
  168.      ppos = nextpos[ptyp][nunmap[po]];
  169.      pdir = nextdir[ptyp][nunmap[po]];
  170.           /* dest is a function of direction and steps */
  171.           for (d = 0; d < 8; d++)
  172.             {
  173.               dest[d][0] = nunmap[po];
  174.               delta = direc[ptyp][d];
  175.               if (delta != 0)
  176.                 {
  177.                   p0 = po;
  178.                   for (s = 0; s < max_steps[ptyp]; s++)
  179.                     {
  180.                       p0 = p0 + delta;
  181.                       /*
  182.                         break if (off board) or
  183.                         (pawns only move two steps from home square)
  184.                       */
  185.                       if (nunmap[p0] < 0 || (ptyp == pawn || ptyp == bpawn)
  186.                           && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn))
  187.                         break;
  188.                       else
  189.                         dest[d][s] = nunmap[p0];
  190.                     }
  191.                 }
  192.               else
  193.                 s = 0;
  194.  
  195.               /*
  196.                 sort dest in number of steps order
  197.                 currently no sort is done due to compability with
  198.                 the move generation order in old gnu chess
  199.               */
  200.               steps[d] = s;
  201.               for (di = d; s > 0 && di > 0; di--)
  202.                 if (steps[sorted[di - 1]] == 0) /* should be: < s */
  203.                   sorted[di] = sorted[di - 1];
  204.                 else
  205.                   break;
  206.               sorted[di] = d;
  207.             }
  208.  
  209.           /*
  210.             update nextpos/nextdir,
  211.             pawns have two threads (capture and no capture)
  212.           */
  213.           p0 = nunmap[po];
  214.           if (ptyp == pawn || ptyp == bpawn)
  215.             {
  216.               for (s = 0; s < steps[0]; s++)
  217.                 {
  218.                   ppos[p0] = (unsigned char) dest[0][s];
  219.                   p0 = dest[0][s];
  220.                 }
  221.               p0 = nunmap[po];
  222.               for (d = 1; d < 3; d++)
  223.                 {
  224.                   pdir[p0] = (unsigned char) dest[d][0];
  225.                   p0 = dest[d][0];
  226.                 }
  227.             }
  228.           else
  229.             {
  230.               pdir[p0] = (unsigned char) dest[sorted[0]][0];
  231.               for (d = 0; d < 8; d++)
  232.                 for (s = 0; s < steps[sorted[d]]; s++)
  233.                   {
  234.                     ppos[p0] = (unsigned char) dest[sorted[d]][s];
  235.                     p0 = dest[sorted[d]][s];
  236.                     if (d < 7)
  237.                       pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  238.                     /* else is already initialized */
  239.                   }
  240.             }
  241.         }
  242. }
  243.  
  244.  
  245. /* hmm.... shouldn`t main be moved to the interface routines */
  246. int
  247. init_main (HWND hWnd)
  248. {
  249.   short int ahead = true, hash = true;
  250.  
  251.   Level = 0;
  252.   TCflag = false;
  253.   OperatorTime = 0;
  254.   Initialize ();
  255.   Initialize_dist ();
  256.   Initialize_moves ();
  257.   NewGame (hWnd);
  258.   GetOpenings ( hWnd);
  259.  
  260.   flag.easy = ahead;
  261.   flag.hash = hash;
  262.   hashfile = NULL;
  263.   return (0);
  264. }
  265.  
  266.  
  267. void
  268. NewGame (HWND hWnd)
  269.  
  270. /*
  271.   Reset the board and other variables to start a new game.
  272. */
  273.  
  274. {
  275.   short l, c, p;
  276.  
  277.   stage = stage2 = -1;          /* the game is not yet started */
  278.  
  279.   if ( flag.post ) {
  280.       WinSendMsg(hStats, WM_SYSCOMMAND, MPFROMSHORT(SC_CLOSE), 0);
  281.       flag.post = false;
  282.   }
  283.  
  284.   flag.mate = flag.quit = flag.reverse = flag.bothsides = false;
  285.   flag.force = false;
  286.   flag.hash = flag.easy = flag.rcptr = true;  /* Ormson 12/09/90     */
  287.   NodeCnt = et0 = epsquare = 0;
  288.   dither = 0;
  289.   Awindow = 90;
  290.   Bwindow = 90;
  291.   xwndw = 90;
  292.   MaxSearchDepth = 29;
  293.   contempt = 0;
  294.   GameCnt = 0;
  295.   Game50 = 1;
  296.   hint = 0x0C14;
  297.   ZeroRPT ();
  298.   Developed[white] = Developed[black] = false;
  299.   castld[white] = castld[black] = false;
  300.   PawnThreat[0] = CptrFlag[0] = false;
  301.   Pscore[0] = 12000;
  302.   Tscore[0] = 12000;
  303.   opponent = white;
  304.   computer = black;
  305.   for (l = 0; l < 2000; l++)
  306.       Tree[l].f = Tree[l].t = 0;
  307. #if ttblsz
  308.   rehash = 6;
  309.   ZeroTTable ();
  310.   srand ((unsigned int) 1);
  311.   for (c = white; c <= black; c++)
  312.     for (p = pawn; p <= king; p++)
  313.       for (l = 0; l < 64; l++)
  314.         {
  315.      hashcode[c][p][l].key = (((unsigned long) rand ()));
  316.      hashcode[c][p][l].key += (((unsigned long) rand ()) << 16);
  317.      hashcode[c][p][l].bd = (((unsigned long) rand ()));
  318.      hashcode[c][p][l].bd += (((unsigned long) rand ()) << 16);
  319.         }
  320. #endif /* ttblsz */
  321.   for (l = 0; l < 64; l++)
  322.     {
  323.       board[l] = Stboard[l];
  324.       color[l] = Stcolor[l];
  325.       Mvboard[l] = 0;
  326.     }
  327.   if (TCflag)
  328.     SetTimeControl ();
  329.   else if (Level == 0) {
  330.     OperatorTime = 1;
  331.     TCmoves = 60;
  332.     TCminutes = 5;
  333.     TCflag = (TCmoves > 1);
  334.     SetTimeControl ();
  335.   }
  336.   InitializeStats ();
  337.   time0 = time ((long *) 0);
  338.   ElapsedTime (1);
  339.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  340. }
  341. 
  342.