Implement e.p.-less initial double-moves
[fairymax.git] / fairymax.c
index 8b7b45a..3b2db59 100644 (file)
@@ -18,7 +18,7 @@
      /*****************************************************************/\r
 \r
 #define MULTIPATH\r
-#define VERSION "4.8M"\r
+#define VERSION "4.8O"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -92,7 +92,10 @@ int GameNr;
 int Resign;\r
 int Threshold = 800;\r
 int Score;\r
-char piecename[32], piecetype[32];\r
+int makruk;\r
+int prom;\r
+char piecename[32], piecetype[32], blacktype[32];\r
+char selectedFairy[80];\r
 char *inifile = INI_FILE;\r
 \r
 int Ticks, tlim, Setup, SetupQ;\r
@@ -122,6 +125,9 @@ T[4104],                                       /* hash translation table   */
 centr[32],\r
 n[]=".*XKNBRQEWFMACHG?x+knbrqewfmachg";        /* piece symbols on printout*/\r
 \r
+int pv[10000],*sp=pv; // triangular array\r
+int margin;\r
+\r
 pboard()\r
 {int i;\r
  i=-1;W(++i<128)printf(" %c",(i&15)==BW&&(i+=15-BW)?10:n[b[i]&31]);\r
@@ -131,9 +137,10 @@ pboard()
 D(k,q,l,e,E,z,n)        /* recursive minimax search, k=moving side, n=depth*/\r
 int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/\r
 {                       /* e=score, z=prev.dest; J,Z=hashkeys; return score*/\r
- int j,r,m,v,d,h,i,F,G,P,V,f=J,g=Z,C,s,flag,FF;\r
+ int j,r,m,v,d,h,i,F,G,P,V,f=J,g=Z,C,s,flag,FF,*ps=sp;\r
  signed char t,p,u,x,y,X,Y,H,B;\r
  struct _*a=A+(J+(k+S)*E&U-1);                 /* lookup pos. in hash table*/\r
+ *sp++=0;\r
  q-=q<e;l-=l<=e;                               /* adj. window: delay bonus */\r
  d=a->D;m=a->V;X=a->F;Y=a->Y;                  /* resume at stored depth   */\r
  if(a->K-Z|z&S  |                              /* miss: other pos. or empty*/\r
@@ -179,8 +186,8 @@ int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
        v=d-1?e:i-p;                            /*** MVV/LVA scoring if d=1**/\r
        if(d-!t>1)                              /*** all captures if d=2  ***/\r
        {v=centr[p]?b[x+257]-b[y+257]:0;        /* center positional pts.   */\r
+        if(!(G&S))b[FF]=b[G],v+=50;            /* castling: put R & score  */\r
         b[G]=b[H]=b[x]=0;b[y]=u|32;            /* do move, set non-virgin  */\r
-        if(!(G&S))b[FF]=k+6,v+=50;             /* castling: put R & score  */\r
         v-=w[p]>0|R<EG?0:20;                   /*** freeze K in mid-game ***/\r
         if(p<3)                                /* pawns:                   */\r
         {v-=9*((x-2&M||b[x-2]-u)+              /* structure, undefended    */\r
@@ -188,47 +195,63 @@ int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
               +(w[b[x^16]&15]<0))              /*** cling to magnetic K ***/\r
               +(R-76>>2);                      /* end-game Pawn-push bonus */\r
          b[y]+=V=y+r+1&S?647-p:2*(u&y+16&32);  /* upgrade P or convert to Q*/\r
+         if(V&makruk)b[y]=u|7,V=480;           /* Makruk promotion on 6th  */\r
          V>>=sh;                               /* for Shatranj promo to F  */\r
          i+=V;                                 /* promotion / passer bonus */\r
         } if(z&S && GamePtr<6) v+=(rand()>>10&31)-16;\r
         J+=J(0);Z+=J(4)+G-S;\r
         SHAMAX( pl[k]-=!!t; )                  /* count pieces per side    */\r
         v+=e+i;V=m>q?m:q;                      /*** new eval & alpha    ****/\r
+        if(z&S)V=m-margin>q?m-margin:q;        /* multiPV                  */\r
         C=d-1-(d>5&p>2&!t&!h);                 /* nw depth, reduce non-cpt.*/\r
         C=R<EG|P-I|d<3||t&&p-3?C:d;            /* extend 1 ply if in-check */\r
         do\r
          s=C>2|v>V?-D(16-k,-l,-V,-v,/*** futility, recursive eval. of reply */\r
                                      F,y&255,C):v;\r
         W(s>q&++C<d); v=s;                     /* no fail:re-srch unreduced*/\r
+        if(v>V&v<l){int *p=sp;\r
+         sp=ps+1;\r
+         W(*sp++=*p++);\r
+         *ps=256*x+y;\r
+        }\r
         if(z&S&&K-I)                           /* move pending: check legal*/\r
         {if(v+I&&x==K&y==L)                    /*   if move found          */\r
-         {Q=-e-i;O=F;LL=L;\r
-          if(b[y]-u&15)b[y]-=PromPiece,        /* under-promotion, correct */\r
+         {Q=-e-i;O=F;LL=L;prom=0;\r
+          if(b[y]-u&15)prom=b[y]-=PromPiece,   /* under-promotion, correct */\r
                        J+=PromPiece;           /*  piece & invalidate hash */\r
           a->D=99;a->V=0;                      /* lock game in hash as draw*/\r
           R-=i/FAC;                            /*** total captd material ***/\r
           Fifty = t|p<3?0:Fifty+1;\r
+          sp=ps;\r
                      return l;}                /*   & not in check, signal */\r
          v=m;                                  /* (prevent fail-lows on    */\r
         }                                      /*   K-capt. replies)       */\r
         J=f;Z=g;\r
         SHAMAX( pl[k]+=!!t; )\r
-        b[G]=k+6;b[FF]=b[y]=0;b[x]=u;b[H]=t;   /* undo move,G can be dummy */\r
+        b[G]=b[FF];b[FF]=b[y]=0;b[x]=u;b[H]=t; /* undo move,G can be dummy */\r
        }                                       /*          if non-castling */\r
+       if(z&S&&Post&K==I&d>2&v>V&v<l){int *p=ps;char X,Y;\r
+        printf("%2d ",d-2);\r
+        printf("%6d ",v);\r
+        printf("%8d %10d",(GetTickCount()-Ticks)/10,N);\r
+        while(*p){X=*p>>8;Y=*p++;\r
+        printf(" %c%c%c%c",'a'+(X&15),'8'-(X>>4),'a'+(Y&15),'8'-(Y>>4&7));}\r
+        printf("\n");fflush(stdout);\r
+       }
        if(v>m)                                 /* new best, update max,best*/\r
         m=v,X=x,Y=y|S&F;                       /* mark non-double with S   */\r
        if(h){h=0;goto A;}                      /* redo after doing old best*/\r
       }\r
-      s=t;\r
+      s=t;v=r^flag>>12;                        /* calc. alternated vector  */\r
       if(flag&15^4|u&32||                      /* no double or moved before*/\r
          p>2&&                                 /* no P & no lateral K move,*/\r
-         (b[G=r<0?x&~15:BW-1|x&112]-k-6        /* no virgin R in corner G, */\r
-         ||b[G^1]|b[G^2]|b[FF=r<0?G+3:G-2])    /* no 2 empty sq. next to R */\r
+         ((b[G=r<0?x&~15:BW-1|x&112]^32)<33    /* no virgin R in corner G, */\r
+         ||b[G^1]|b[G^2]|b[FF=y+v-r])          /* no 2 empty sq. next to R */\r
         )t+=flag&4;                            /* fake capt. for nonsliding*/\r
-      else F=y;                                /* enable e.p.              */\r
+      else if(flag&64)t=flag&128?0:t,flag&=63;else F=y; /* enable e.p.     */\r
       if(s&&flag&8)t=0,flag^=flag>>4&15;       /* hoppers go to next phase */\r
       if(!(flag&S))                            /* zig-zag piece?           */\r
-       r^=flag>>12,flag^=flag>>4&15;           /* alternate vector & mode  */\r
+       r=v,flag^=flag>>4&15;                   /* alternate vector & mode  */\r
      }W(!t);                                   /* if not capt. continue ray*/\r
    }}\r
    if((++x&15)>=BW)x=x+16&112;                 /* next sqr. of board, wrap */\r
@@ -237,13 +260,9 @@ C:FMAX( m=m+I|P==I?m:(X=Y=0); )                /* if stalemate, draw-score */
   if(a->D<99)                                  /* protect game history     */\r
    a->K=Z,a->V=m,a->D=d,a->X=X,                /* always store in hash tab */\r
    a->F=8*(m>q)|S*(m<l),a->Y=Y;                /* move, type (bound/exact),*/\r
-  if(z&S&&Post){\r
-    printf("%2d ",d-2);\r
-    printf("%6d ",m);\r
-    printf("%8d %10d %c%c%c%c\n",(GetTickCount()-Ticks)/10,N,\r
-       'a'+(X&15),'8'-(X>>4),'a'+(Y&15),'8'-(Y>>4&7)),fflush(stdout);\r
- }}                                            /*    encoded in X S,8 bits */\r
+  }                                            /*    encoded in X S,8 bits */\r
 if(z&4*S)K=X,L=Y&~S;\r
+ sp=ps;\r
  return m+=m<e;                                /* delayed-loss bonus       */\r
 }\r
 \r
@@ -302,10 +321,24 @@ int PrintResult(int s)
         }\r
         K=I;\r
         cnt = D(s,-I,I,Q,O,LL|4*S,3);\r
+#ifdef SHATRANJ\r
+        if(pl[s]==1 && pl[16-s]==1) {\r
+                printf("1/2-1/2 {Insufficient mating material}\n");\r
+                return 4;\r
+        }\r
+        if(pl[s]<=1 && pl[16-s]>1) {\r
+                if (s == BLACK)\r
+                        printf("0-1 {Bare King}\n");\r
+                else\r
+                        printf("1-0 {Bare King}\n");\r
+                return 5;\r
+        }\r
+#else\r
         if(cnt>-I+1 && K==0 && L==0) {\r
                 printf("1/2-1/2 {Stalemate}\n");\r
                 return 2;\r
         }\r
+#endif\r
         if(cnt==-I+1) {\r
                 if (s == WHITE)\r
                         printf("0-1 {Black mates}\n");\r
@@ -317,19 +350,6 @@ int PrintResult(int s)
                 printf("1/2-1/2 {Draw by fifty move rule}\n");\r
                 return 4;\r
         }\r
-#ifdef SHATRANJ\r
-        if(pl[s]==1 && pl[16-s]==1) {\r
-                printf("1/2-1/2 {Insufficient mating material}\n");\r
-                return 4;\r
-        }\r
-        if(pl[s]<=1 && pl[16-s]>1) {\r
-                if (s == BLACK)\r
-                        printf("0-1 {Bare King}\n");\r
-                else\r
-                        printf("1-0 {Bare King}\n");\r
-                return 5;\r
-        }\r
-#endif\r
         return 0;\r
 }\r
 \r
@@ -368,9 +388,9 @@ void CopyBoard(int s)
             HistoryBoards[s][BW*i+j] = b[16*i+j]|64*(16*i+j==O);\r
 }\r
                                          \r
-void PrintVariants()\r
+void PrintVariants(int combo)\r
 {\r
-        int i, j, count=0; char c, buf[80];\r
+        int i, j, count=0, total=0; char c, buf[80];\r
         FILE *f;\r
 \r
         f = fopen(INI_FILE, "r");\r
@@ -381,11 +401,15 @@ void PrintVariants()
            while(fscanf(f, "Game: %s", buf) != 1 && c != EOF) \r
                while((c = fgetc(f)) != EOF && c != '\n');\r
            if(c == EOF) break;\r
-           if(count++) printf(",");\r
-           printf("%s", buf);\r
+           total++;\r
+           if(combo == (strstr(buf, "fairy/") != buf)) continue;\r
+           if(combo && count == 0) strcpy(selectedFairy, buf);\r
+           if(count++) printf(combo ? " /// " : ",");\r
+           printf("%s", combo ? buf+6 : buf);\r
         } while(c != EOF);\r
 \r
         fclose(f);\r
+        if(!combo && total != count) printf("%sfairy", count ? "," : "");\r
 }\r
 \r
 void PrintOptions()\r
@@ -393,9 +417,10 @@ void PrintOptions()
        printf("feature option=\"Resign -check %d\"\n", Resign);\r
        printf("feature option=\"Resign Threshold -spin %d 200 1200\"\n", Threshold);\r
        printf("feature option=\"Ini File -file %s\"\n", inifile);\r
-       printf("feature option=\"Playing Style ;-) -combo Brilliant /// *Brave /// Beautiful\"\n");\r
+       printf("feature option=\"Multi-PV Margin -spin %d 0 1000\"\n", margin);\r
+       printf("feature option=\"Variant fairy selects -combo "); PrintVariants(1); printf("\"\n");\r
        printf("feature option=\"Dummy Slider Example -slider 20 0 100\"\n");\r
-       printf("feature option=\"Dummy String Example -file happy birthday!\"\n");\r
+       printf("feature option=\"Dummy String Example -string happy birthday!\"\n");\r
        printf("feature option=\"Dummy Path Example -path .\"\n");\r
        printf("feature option=\"Clear Hash -button\"\n");\r
        printf("feature done=1\n");\r
@@ -417,6 +442,7 @@ int LoadGame(char *name)
 \r
         if(name != NULL)\r
         {  /* search for game name in definition file */\r
+           if(!strcmp(name, "fairy")) name = selectedFairy;\r
            while(fscanf(f, "Game: %s", buf)!=1 || strcmp(name, buf) ) {\r
                while((c = fgetc(f)) != EOF && c != '\n');\r
                count++;\r
@@ -437,14 +463,15 @@ int LoadGame(char *name)
         for(i=0; i<BW; i++) fscanf(f, "%d", oo+i+16);\r
         for(i= 0; i<=U; i++)\r
             A[i].K = A[i].D = A[i].X = A[i].Y = A[i].F = 0; /* clear hash */\r
-        for(i=0; i<32; i++) piecetype[i] = 0;\r
+        for(i=0; i<32; i++) piecetype[i] = blacktype[i] = 0;\r
 \r
         i=0; j=-1; c=0;\r
         while(fscanf(f, "%d,%x", o+j, of+j)==2 ||\r
                                       fscanf(f,"%c:%d",&c, w+i+1)==2)\r
         {   if(c)\r
             { od[++i]=j; centr[i] = c>='a';\r
-              piecetype[c&31]=i; piecename[i]=c&31;\r
+              blacktype[c&31]=i; piecename[i]=c&31;\r
+              if(piecetype[c&31]==0) piecetype[c&31]=i; // only first\r
             }\r
             j++; o[j]=0;\r
             /* printf("# c='%c' i=%d od[i]=%d j=%d (%3d,%8x)\n",c?c:' ',i,od[i],j,o[j-1],of[j-1]); /**/\r
@@ -453,6 +480,7 @@ int LoadGame(char *name)
 \r
         fclose(f);\r
        sh = w[7] < 250 ? 3 : 0;\r
+       makruk = w[7]==181 ? 64 : 0; // w[7] is used as kludge to enable makruk promotions\r
 }\r
 \r
 int main(int argc, char **argv)\r
@@ -508,6 +536,7 @@ int main(int argc, char **argv)
                             printf("move ");\r
                             printf("%c%c%c%c",'a'+(K&15),'0'+BH-(K>>4),\r
                                           'a'+(L&15),'0'+BH-(L>>4));\r
+                           if(prom)printf("%c",piecename[prom&15]+'a'-1);\r
                             printf("\n");\r
                             m = GetTickCount() - Ticks;\r
 \r
@@ -546,7 +575,7 @@ int main(int argc, char **argv)
                         printf("feature memory=1\n");\r
                         printf("feature setboard=0 ping=1 done=0\n");\r
                         printf("feature variants=\"");\r
-                        PrintVariants();\r
+                        PrintVariants(0);\r
                         printf("\"\n");\r
                        PrintOptions();\r
                         continue;\r
@@ -595,12 +624,14 @@ int main(int argc, char **argv)
                }\r
                if (!strcmp(command, "white")) {\r
                         /* set white to move in current position */\r
+                        if(Side == BLACK) Q = -Q;\r
                         Side     = WHITE;\r
                         Computer = BLACK;\r
                        continue;\r
                }\r
                if (!strcmp(command, "black")) {\r
                         /* set blck to move in current position */\r
+                        if(Side == WHITE) Q = -Q;\r
                         Side     = BLACK;\r
                         Computer = WHITE;\r
                        continue;\r
@@ -667,6 +698,8 @@ int main(int argc, char **argv)
                                inifile = filename; continue;\r
                        }\r
                        if(sscanf(line+7, "Clear Hash") == 1) for(i=0; i<U; i++) A->K = 0;\r
+                       if(sscanf(line+7, "MultiVariation Margin=%d", &margin) == 1) continue;\r
+                       if(sscanf(line+7, "Variant fairy selects=%s", selectedFairy+6) == 1) continue;\r
                        continue;\r
                }\r
                if (!strcmp(command, "go")) {\r
@@ -749,7 +782,7 @@ int main(int argc, char **argv)
                                     && line[1] >= 'a' && line[1] <= 'a'+BW-1\r
                                     && line[2] >= '1' && line[2] <= '0'+BH) {\r
                                         m = line[1]-16*line[2]+799;\r
-                                        switch(p = piecetype[line[0]&31])\r
+                                        switch(p = (color == WHITE ? piecetype : blacktype)[line[0]&31])\r
                                         {\r
                                         case 1:\r
                                         case 2:\r
@@ -759,23 +792,16 @@ int main(int argc, char **argv)
                                             else b[m]=(m&0x70)==0x10?18:50,\r
                                                  Q+=w[2];\r
                                             break;\r
-                                        case 3: // can castle, normally King\r
-                                            b[m]=3+color+32;\r
-                                            if(m==BW>>1        && color==BLACK ||\r
-                                               m==0x70+(BW>>1) && color==WHITE)\r
-                                                b[m] -= 32;\r
-                                            break;\r
-                                        case 6: // can castle, normally Rook\r
-                                            b[m]=6+color+32;\r
-                                            if((m==0x00 || m==BW-1   ) && color==BLACK ||\r
-                                               (m==0x70 || m==0x6F+BW) && color==WHITE)\r
-                                                b[m] -= 32;\r
-                                            Q+=w[6]; R+=w[6]/FAC;\r
-                                            break;\r
                                         default:\r
-                                            b[m]=p+color;\r
-                                            Q+=w[p]; R+=w[p]/FAC;\r
-                                                   case 0: // undefined piece, ignore\r
+                                            b[m]=p+color+32; // assume non-virgin\r
+                                            if(w[p]<0) { // Royal piece on original square: virgin
+                                               if(color==BLACK && m<0x10 && p==oo[m+16] ||\r
+                                                   color==WHITE && m>0x6F && p==oo[m-0x70]) b[m] -= 32;
+                                           } else { Q+=w[p]; R+=w[p]/FAC; }
+                                            if((m==0x00 || m==BW-1   ) && color==BLACK && p==oo[m+16] ||\r
+                                               (m==0x70 || m==0x6F+BW) && color==WHITE && p==oo[m-0x70])\r
+                                                b[m] &= ~32; // corner piece as in original setup: virgin\r
+                                       case 0: // undefined piece, ignore\r
                                             break;\r
                                         }\r
                                         pl[BLACK+WHITE-color]++;\r
@@ -794,7 +820,7 @@ int main(int argc, char **argv)
                 m = line[0]<'a' | line[0]>='a'+BW | line[1]<'1' | line[1]>='1'+BH |\r
                     line[2]<'a' | line[2]>='a'+BW | line[3]<'1' | line[3]>='1'+BH;\r
                 if(line[4] == '\n') line[4] = piecename[7];\r
-                PromPiece = 7 - piecetype[line[4]&31];\r
+                PromPiece = 7 - (Side == WHITE ? piecetype : blacktype)[line[4]&31];\r
                    if(PromPiece == 7) PromPiece = 0;\r
                 {char *c=line; K=c[0]-16*c[1]+799;L=c[2]-16*c[3]+799; }\r
                 if (m)\r