Initial attempt at minishogi.
[gnushogi.git] / gnushogi / init.c
index 6e4cfce..819e738 100644 (file)
@@ -2,11 +2,14 @@
  * FILE: init.c
  *
  * ----------------------------------------------------------------------
- *
- * Copyright (c) 2012 Free Software Foundation
+ * Copyright (c) 1993, 1994, 1995 Matthias Mutz
+ * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
  *
  * GNU SHOGI is based on GNU CHESS
  *
+ * Copyright (c) 1988, 1989, 1990 John Stanback
+ * Copyright (c) 1992 Free Software Foundation
+ *
  * This file is part of GNU SHOGI.
  *
  * GNU Shogi is free software; you can redistribute it and/or modify it
@@ -66,21 +69,40 @@ unsigned int ttbllimit;
 /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
 
 
+#ifndef WIN32
 #define max(a, b) (((a) < (b))?(b):(a))
+#endif
 #define odd(a) ((a) & 1)
 
 
 const small_short piece_of_ptype[NO_PTYPE_PIECES] =
 {
-    pawn, lance, knight, silver, gold, bishop, rook, pbishop, prook, king,
-    pawn, lance, knight, silver, gold
+    pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, gold, bishop, rook, pbishop, prook, king,
+    pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, gold
 };
 
 
+/* FIXME: all bishops and rooks are black ? */
 const small_short side_of_ptype[NO_PTYPE_PIECES] =
 {
-    black, black, black, black, black, black, black, black, black, black,
-    white, white, white, white, white
+    black,
+#ifndef MINISHOGI
+    black, black,
+#endif
+    black, black, black, black, black, black, black,
+    white,
+#ifndef MINISHOGI
+    white, white,
+#endif
+    white, white
 };
 
 #ifdef SAVE_NEXTPOS
@@ -93,29 +115,17 @@ const small_short psweep[NO_PTYPE_PIECES] =
 
 const small_short sweep[NO_PIECES] =
 {
-    false, false, true, false, false, false, true, true,
-    false, false, false, false, true, true, false
-};
-
-
-#if !defined EXTLANGFILE
-
-char *CP[CPSIZE] =
-
-{
-/* 000: eng: */ "",
-#ifdef LANGFILE
-#include LANGFILE
-#else
-#include "gnushogi.lng"
+    false, false,
+#ifndef MINISHOGI
+    true, false,
 #endif
-};
-
-#else
-
-char *CP[CPSIZE];
-
+    false, false, true, true,
+    false,
+#ifndef MINISHOGI
+    false, false,
 #endif
+    false, true, true, false
+};
 
 
 /*
@@ -151,6 +161,7 @@ ptype_distance(short ptyp, short f, short t)
         else
             return drow;
 
+#ifndef MINISHOGI
     case lance:
         if ((dcol != 0) || (drow < 1))
             return CANNOT_REACH;
@@ -164,6 +175,7 @@ ptype_distance(short ptyp, short f, short t)
             return CANNOT_REACH;
         else
             return (drow / 2);
+#endif
 
     case silver:
         if (drow > 0)
@@ -190,8 +202,10 @@ ptype_distance(short ptyp, short f, short t)
 
     case gold:
     case ppawn:
+#ifndef MINISHOGI
     case pknight:
     case plance:
+#endif
     case psilver:
         if (abs(dcol) == 0)
             return (abs(drow));
@@ -310,12 +324,12 @@ Initialize_dist(void)
 
 
 /*
- * nextpos[piece][from-square], nextdir[piece][from-square] gives vector
- * of positions reachable from from-square in ppos with piece such that the
+ * nextpos[ptype][from-square], nextdir[ptype][from-square] gives vector
+ * of positions reachable from from-square in ppos with ptype such that the
  * sequence
  *
- *     ppos = nextpos[piece][from-square];
- *     pdir = nextdir[piece][from-square];
+ *     ppos = nextpos[ptype][from-square];
+ *     pdir = nextdir[ptype][from-square];
  *     u = ppos[sq];
  *
  *     do
@@ -343,38 +357,78 @@ Initialize_dist(void)
 const small_short ptype[2][NO_PIECES] =
 {
     {
-        ptype_no_piece, ptype_pawn,  ptype_lance,  ptype_knight,
+        ptype_no_piece, ptype_pawn,
+#ifndef MINISHOGI
+       ptype_lance,  ptype_knight,
+#endif
         ptype_silver,   ptype_gold,  ptype_bishop, ptype_rook,
-        ptype_gold,     ptype_gold,  ptype_gold,   ptype_gold,
+        ptype_gold,
+#ifndef MINISHOGI
+       ptype_gold,  ptype_gold,
+#endif
+       ptype_gold,
         ptype_pbishop,  ptype_prook, ptype_king
     },
     {
-        ptype_no_piece, ptype_wpawn, ptype_wlance, ptype_wknight,
+        ptype_no_piece, ptype_wpawn,
+#ifndef MINISHOGI
+       ptype_wlance, ptype_wknight,
+#endif
         ptype_wsilver,  ptype_wgold, ptype_bishop, ptype_rook,
-        ptype_wgold,    ptype_wgold, ptype_wgold,  ptype_wgold,
+        ptype_wgold,
+#ifndef MINISHOGI
+       ptype_wgold, ptype_wgold,
+#endif
+       ptype_wgold,
         ptype_pbishop,  ptype_prook, ptype_king
     },
 };
 
 const small_short promoted[NO_PIECES] =
 {
-    no_piece, ppawn, plance, pknight, psilver, gold, pbishop, prook,
-    ppawn, plance, pknight, psilver, pbishop, prook, king
+    no_piece, ppawn,
+#ifndef MINISHOGI
+    plance, pknight,
+#endif
+    psilver, gold, pbishop, prook,
+    ppawn,
+#ifndef MINISHOGI
+    plance, pknight,
+#endif
+    psilver, pbishop, prook, king
 };
 
 const small_short unpromoted[NO_PIECES] =
 {
-    no_piece, pawn, lance, knight, silver, gold, bishop, rook,
-    pawn, lance, knight, silver, bishop, rook, king
+    no_piece, pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, gold, bishop, rook,
+    pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, bishop, rook, king
 };
 
 const small_short is_promoted[NO_PIECES] =
 {
-    false, false, false, false, false, false, false, false,
-    true, true, true, true, true, true, false
+    false, false,
+#ifndef MINISHOGI
+    false, false,
+#endif
+    false, false, false, false,
+    true,
+#ifndef MINISHOGI
+    true, true,
+#endif
+    true, true, true, false
 };
 
 /* data used to generate nextpos/nextdir */
+#ifndef MINISHOGI
+/* FIXME: use predefined constants ! */
 #if !defined SAVE_NEXTPOS
 static
 #endif
@@ -394,22 +448,49 @@ const small_short direc[NO_PTYPE_PIECES][8] =
     { -11,   0,   0,   0,   0,   0,   0,   0 },   /* 11 ptype_wlance  */
     { -21, -23,   0,   0,   0,   0,   0,   0 },   /* 12 ptype_wknight */
     { -10, -11, -12,  12,  10,   0,   0,   0 },   /* 13 ptype_wsilver */
-    { -10, -11, -12,   1,  -1,  11,   0,   0 }
-};  /* 14 ptype_wgold */
+    { -10, -11, -12,   1,  -1,  11,   0,   0 }    /* 14 ptype_wgold */
+};
+#else
+#if !defined SAVE_NEXTPOS
+static
+#endif
+const small_short direc[NO_PTYPE_PIECES][8] =
+{
+    {   7,   0,   0,   0,   0,   0,   0,   0 },   /*  0 ptype_pawn    */
+    {   6,   7,   8,  -8,  -6,   0,   0,   0 },   /*  3 ptype_silver  */
+    {   6,   7,   8,  -1,   1,  -7,   0,   0 },   /*  4 ptype_gold    */
+    {   6,   8,  -8,  -6,   0,   0,   0,   0 },   /*  5 ptype_bishop  */
+    {   7,  -1,   1,  -7,   0,   0,   0,   0 },   /*  6 ptype_rook    */
+    {   6,   8,  -8,  -6,   7,  -1,   1,  -7 },   /*  7 ptype_pbishop */
+    {   7,  -1,   1,  -7,   6,   8,  -8,  -6 },   /*  8 ptype_prook   */
+    {   6,   7,   8,  -1,   1,  -8,  -7,  -6 },   /*  9 ptype_king    */
+    {  -7,   0,   0,   0,   0,   0,   0,   0 },   /* 10 ptype_wpawn   */
+    {  -6,  -7,  -8,   8,   6,   0,   0,   0 },   /* 13 ptype_wsilver */
+    {  -6,  -7,  -8,   1,  -1,   7,   0,   0 }    /* 14 ptype_wgold */
+};
+#endif
 
 
 small_short diagonal(short d)
 {
-    return (abs(d) == 10 || abs(d) == 12);
+  return (abs(d) == (NO_COLS+1) || abs(d) == (NO_COLS+3));
 }
 
 
+#ifndef MINISHOGI
+/* FIXME */
 static const small_short max_steps[NO_PTYPE_PIECES] =
 {
     1, 8, 1, 1, 1, 8, 8, 8, 8, 1, 1, 8, 1, 1, 1
 };
+#else
+static const small_short max_steps[NO_PTYPE_PIECES] =
+{
+    1, 1, 1, 4, 4, 4, 4, 1, 1, 1, 1
+};
+#endif
 
-
+#ifndef MINISHOGI
 const small_short nunmap[(NO_COLS + 2)*(NO_ROWS + 4)] =
 {
     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -440,8 +521,29 @@ const small_short inunmap[NO_SQUARES] =
     100, 101, 102, 103, 104, 105, 106, 107, 108,
     111, 112, 113, 114, 115, 116, 117, 118, 119
 };
+#else
+const small_short nunmap[(NO_COLS + 2)*(NO_ROWS + 2)] =
+{
+    -1, -1, -1, -1, -1, -1, -1,
+    -1,  0,  1,  2,  3,  4, -1,
+    -1,  5,  6,  7,  8,  9, -1,
+    -1, 10, 11, 12, 13, 14, -1,
+    -1, 15, 16, 17, 18, 19, -1,
+    -1, 20, 21, 22, 23, 24, -1,
+    -1, -1, -1, -1, -1, -1, -1,
+};
 
 
+const small_short inunmap[NO_SQUARES] =
+{
+      8,   9,  10,  11,  12,
+     15,  16,  17,  18,  19,
+     22,  23,  24,  25,  26,
+     29,  30,  31,  32,  33,
+     36,  37,  38,  39,  40,
+};
+#endif
+
 int InitFlag = false;
 
 
@@ -512,8 +614,11 @@ Initialize_moves(void)
     short dest[8][9];
     short sorted[9];
     short steps[8];
-    short fpo = 23, tpo = 120;
+    short fpo = inunmap[0], tpo = 1 + inunmap[NO_SQUARES-1];
 
+    /* pre-fill nextpos and nextdir with source position, probably so
+     * (color[u] == neutral) stops to match once all moves have been seen
+     */
     for (ptyp = 0; ptyp < NO_PTYPE_PIECES; ptyp++)
     {
         for (po = 0; po < NO_SQUARES; po++)
@@ -759,7 +864,7 @@ NewGame(void)
     if (!InitFlag)
     {
         char sx[256];
-        strcpy(sx, CP[169]);
+        strcpy(sx, "level");
 
         if (TCflag)
             SetTimeControl();
@@ -1021,231 +1126,6 @@ Initialize_data(void)
 }
 
 
-#if defined EXTLANGFILE
-
-#ifdef OLDLANGFILE
-
-void
-InitConst(char *lang)
-{
-    FILE *constfile;
-    char s[256];
-    char sl[5];
-    char buffer[120];
-    int len, entry;
-    char *p, *q;
-    constfile = fopen(LANGFILE, "r");
-
-    if (!constfile)
-    {
-        ShowMessage("NO LANGFILE");
-        exit(1);
-    }
-
-    while (fgets(s, sizeof(s), constfile))
-    {
-        if (s[0] == '!')
-            continue;
-
-        len = strlen(s);
-
-        for (q = &s[len]; q > &s[8]; q--)
-            if (*q == '}')
-                break;
-
-        if (q == &s[8])
-        {
-            ShowMessage("{ error in cinstfile");
-            exit(1);
-        }
-
-        *q = '\0';
-
-        if ((s[3] != ':') || (s[7] != ':') || (s[8] != '{'))
-        {
-            sprintf(buffer, "Langfile format error %s", s);
-            ShowMessage(buffer);
-            exit(1);
-        }
-
-        s[3] = s[7] = '\0';
-
-        if (lang == NULL)
-        {
-            lang = sl;
-            strcpy(sl, &s[4]);
-        }
-
-        if (strcmp(&s[4], lang))
-            continue;
-
-        entry = atoi(s);
-
-        if ((entry < 0) || (entry >= CPSIZE))
-        {
-            ShowMessage("Langfile number error");
-            exit(1);
-        }
-
-        for (q = p = &s[9]; *p; p++)
-        {
-            if (*p != '\\')
-            {
-                *q++ = *p;
-            }
-            else if (*(p + 1) == 'n')
-            {
-                *q++ = '\n';
-                p++;
-            }
-        }
-
-        *q = '\0';
-
-        if ((entry < 0) || (entry > 255))
-        {
-            sprintf(buffer, "Langfile error %d\n", entry);
-            ShowMessage(buffer);
-            exit(0);
-        }
-
-        CP[entry] = (char *)GLOBAL_ALLOC((unsigned) strlen(&s[9]) + 1);
-
-        if (CP[entry] == NULL)
-        {
-            char buffer[80];
-            sprintf(buffer, "CP MALLOC, entry %d", entry);
-            perror(buffer);
-            exit(0);
-        }
-
-        strcpy(CP[entry], &s[9]);
-    }
-
-    fclose(constfile);
-}
-
-#else
-
-void
-InitConst(char *lang)
-{
-    FILE *constfile;
-    char s[256];
-    char sl[5];
-    char buffer[120];
-    int len, entry;
-    char *p, *q;
-    constfile = fopen(LANGFILE, "r");
-
-    if (!constfile)
-    {
-        ShowMessage("NO LANGFILE");
-        exit(1);
-    }
-
-    while (fgets(s, sizeof(s), constfile))
-    {
-        if (s[0] == '!')
-            continue;
-
-        len = strlen(s);
-
-        if ((len > 3) && (s[3] == ':') || (len > 7) && (s[7] == ':'))
-        {
-            ShowMessage("old Langfile error");
-            exit(1);
-        }
-
-        if (len <= 15)
-        {
-            ShowMessage("length error in Langfile");
-            exit(1);
-        }
-
-        for (q = &s[len]; q > &s[15]; q--)
-        {
-            if (*q == '"')
-                break;
-        }
-
-        if (q == &s[15])
-        {
-            ShowMessage("\" error in Langfile");
-            exit(1);
-        }
-
-        *q = '\0';
-
-        if ((s[6] != ':') || (s[10] != ':') || (s[15] != '"'))
-        {
-            sprintf(buffer, "Langfile format error %s", s);
-            ShowMessage(buffer);
-            exit(1);
-        }
-
-        s[6] = s[10] = '\0';
-
-        if (lang == NULL)
-        {
-            lang = sl;
-            strcpy(sl, &s[7]);
-        }
-
-        if (strcmp(&s[7], lang))
-            continue;
-
-        entry = atoi(&s[3]);
-
-        if ((entry < 0) || (entry >= CPSIZE))
-        {
-            ShowMessage("Langfile number error");
-            exit(1);
-        }
-
-        for (q = p = &s[16]; *p; p++)
-        {
-            if (*p != '\\')
-            {
-                *q++ = *p;
-            }
-            else if (*(p + 1) == 'n')
-            {
-                *q++ = '\n';
-                p++;
-            }
-        }
-
-        *q = '\0';
-
-        if ((entry < 0) || (entry > 255))
-        {
-            sprintf(buffer, "Langfile error %d\n", entry);
-            ShowMessage(buffer);
-            exit(0);
-        }
-
-        CP[entry] = (char *)GLOBAL_ALLOC((unsigned)strlen(&s[16]) + 1);
-
-        if (CP[entry] == NULL)
-        {
-            char buffer[80];
-            sprintf(buffer, "CP MALLOC, entry %d", entry);
-            perror(buffer);
-            exit(0);
-        }
-
-        strcpy(CP[entry], &s[16]);
-    }
-
-    fclose(constfile);
-}
-
-#endif
-
-#endif
-
-
 int
 InitMain(void)
 {
@@ -1259,20 +1139,14 @@ InitMain(void)
     if (Initialize_data() != 0)
         return 1;
 
-#if defined EXTLANGFILE
-    InitConst(Lang);
-#endif
-
-    strcpy(ColorStr[0], CP[118]);
-    strcpy(ColorStr[1], CP[119]);
+    strcpy(ColorStr[0], "Black");
+    strcpy(ColorStr[1], "White");
 
     XC = 0;
     MaxResponseTime = 0;
 
     if (XSHOGI)
     {
-        signal(SIGINT, TerminateSearch);
-
         TCmoves      = 40;
         TCminutes    = 5;
         TCseconds    = 0;