Switch display-selection framework from dspwrappers to "struct display".
[gnushogi.git] / gnushogi / eval.c
1 /*
2  * FILE: eval.c
3  *
4  * ----------------------------------------------------------------------
5  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7  *
8  * GNU SHOGI is based on GNU CHESS
9  *
10  * Copyright (c) 1988, 1989, 1990 John Stanback
11  * Copyright (c) 1992 Free Software Foundation
12  *
13  * This file is part of GNU SHOGI.
14  *
15  * GNU Shogi is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the
17  * Free Software Foundation; either version 3 of the License,
18  * or (at your option) any later version.
19  *
20  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with GNU Shogi; see the file COPYING. If not, see
27  * <http://www.gnu.org/licenses/>.
28  * ----------------------------------------------------------------------
29  *
30  */
31
32 #include "gnushogi.h"
33 #include "pattern.h"
34
35 /* Hash table for preventing multiple scoring of the same position */
36
37 int EADD = 0;       /* number of writes to the cache table */
38 int EGET = 0;       /* number of hits to the cache table */
39 int PUTVAR = false; /* shall the current scoring be cached? */
40
41
42 /* Pieces and colors of initial board setup */
43
44 #ifndef MINISHOGI
45 const small_short Stboard[NO_SQUARES] =
46 {
47     lance, knight, silver,   gold,   king,   gold, silver, knight,  lance,
48     0, bishop,      0,      0,      0,      0,      0,   rook,      0,
49     pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,
50     0,      0,      0,      0,      0,      0,      0,      0,      0,
51     0,      0,      0,      0,      0,      0,      0,      0,      0,
52     0,      0,      0,      0,      0,      0,      0,      0,      0,
53     pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,
54     0,   rook,      0,      0,      0,      0,      0, bishop,      0,
55     lance, knight, silver,   gold,   king,   gold, silver, knight,  lance
56 };
57
58
59 const small_short Stcolor[NO_SQUARES] =
60 {
61     black,   black,   black,   black,
62     black,   black,   black,   black,   black,
63     neutral, black, neutral, neutral,
64     neutral, neutral, neutral,   black, neutral,
65     black,   black,   black,   black,
66     black,   black,   black,   black,   black,
67     neutral, neutral, neutral, neutral,
68     neutral, neutral, neutral, neutral, neutral,
69     neutral, neutral, neutral, neutral,
70     neutral, neutral, neutral, neutral, neutral,
71     neutral, neutral, neutral, neutral,
72     neutral, neutral, neutral, neutral, neutral,
73     white,   white,   white,   white,
74     white,   white,   white, white, white,
75     neutral, white, neutral, neutral,
76     neutral, neutral, neutral, white, neutral,
77     white,   white,   white,   white,
78     white,   white,   white, white, white
79 };
80 #else
81 const small_short Stboard[NO_SQUARES] =
82 {
83     king,   gold,   silver, bishop, rook,
84     pawn,   0,      0,      0,      0,
85     0,      0,      0,      0,      0,
86     0,      0,      0,      0,      pawn,
87     rook,   bishop, silver, gold,   king,
88 };
89
90
91 const small_short Stcolor[NO_SQUARES] =
92 {
93     black,   black,   black,   black,   black,
94     black,   neutral, neutral, neutral, neutral,
95     neutral, neutral, neutral, neutral, neutral,
96     neutral, neutral, neutral, neutral, white,
97     white,   white,   white,   white,   white
98 };
99 #endif
100
101 /* Actual pieces and colors */
102
103 small_short board[NO_SQUARES], color[NO_SQUARES];
104
105
106 /* relative piece values at the beginning of main stages */
107
108 #define MAIN_STAGES 4
109
110 static small_short ispvalue[NO_PIECES][MAIN_STAGES] =
111 {
112     {   0,  35,  70,  99 }, /* main stage borders */
113     /* ------------------------------------------ */
114     {   7,   7,   8,  10 }, /* Pawn               */
115 #ifndef MINISHOGI
116     {  20,  35,  45,  60 }, /* Lance              */
117     {  20,  35,  45,  60 }, /* Knight             */
118 #endif
119     {  35,  40,  60,  80 }, /* Silver             */
120     {  35,  50,  65,  80 }, /* Gold               */
121     {  90,  90,  90,  90 }, /* Bishop             */
122     {  95,  95,  95,  95 }, /* Rook               */
123     {  15,  25,  40,  65 }, /* promoted Pawn      */
124 #ifndef MINISHOGI
125     {  25,  45,  55,  65 }, /* promoted Lance     */
126     {  25,  45,  55,  65 }, /* promoted Knight    */
127 #endif
128     {  35,  55,  75,  75 }, /* promoted Silver    */
129     {  99,  99,  99,  99 }, /* promoted Bishop    */
130     {  97,  97,  99,  99 }, /* promoted Rook      */
131     { 100, 100, 100, 100 }, /* King               */
132 };
133
134 /* Features and Weights */
135
136 #define ATTACKED    0
137 #define HUNGP       1
138 #define HUNGX       2
139 #define CNTRL5TH    3
140 #define HOLES       4
141 #define PCASTLE     5
142 #define PATTACK     6
143 #define CTRLK       7
144 #define PROTECT     8
145 #define HCLSD       9
146 #define PINVAL     10
147 #define XRAY       11
148 #define OPENWRONG  12
149 #define SEED       13
150 #define LOOSE      14
151 #define MOBILITY   15
152 #define TARGET     16
153 #define KSFTY      17
154 #define HOPN       18
155 #define PROMD      19
156 #define KINGOD     20
157 #define PWNDROP    21
158 #define DFFDROP    22
159 #define FCLATTACK  23
160 #define KNGATTACK  24
161 #define KNGPROTECT 25
162 #define DNGLPC     26
163 #define LSATTACK   27
164 #define NIHATTACK  28
165 #define COHESION   29
166 #define OPPDROP    30
167
168
169 static small_short weight[NO_FEATURES + 1][MAIN_STAGES + 2] =
170 {
171     {  80, 100, 100,  40,  10,  15 },    /* ATTACKED      */
172     {  80, 100, 100,  50,  14,  10 },    /* HUNGP         */
173     {  80, 100, 100,  50,  18,  12 },    /* HUNGX         */
174     { 100,  50,   0,   0,   2,   1 },    /* CNTRL5TH      */
175     { 100, 100,  60,  10,   4,   2 },    /* HOLES         */
176     { 100,  50,   0,   0,  14,   7 },    /* PCASTLE       */
177     { 100,  50,   0,   0,   6,  12 },    /* PATTACK       */
178     {  10,  40,  70, 100,  10,  15 },    /* CTRLK         */
179     { 100,  80,  50,  40,   2,   1 },    /* PROTECT       */
180     {  40, 100,  40,   5,   4,   4 },    /* HCLSD         */
181     {  80, 100,  80,  30,  10,  15 },    /* PINVAL        */
182     {  80, 100,  60,  15,   6,  10 },    /* XRAY          */
183     { 100,  50,   0,   0,  15,  15 },    /* OPENWRONG     */
184     {   0,  40,  70, 100,   8,  12 },    /* SEED          */
185     {  50, 100,  80,  20,   5,   3 },    /* LOOSE         */
186     {  50, 100,  80,  50, 100, 100 },    /* MOBILITY (%)  */
187     {  50, 100,  80,  50,   4,   8 },    /* TARGET        */
188     {  50,  40, 100,  80,   8,   4 },    /* KSFTY         */
189     {  80, 100,  60,  20,   5,   5 },    /* HOPN          */
190     {  20,  40,  80, 100,   3,   6 },    /* PROMD         */
191     {  20,  40,  80, 100,   4,   1 },    /* KINGOD        */
192     {   5,  40, 100,  50,   0,   4 },    /* PWNDROP       */
193     {   0,  20,  80, 100,   0,   4 },    /* DFFDROP       */
194     {  20,  50, 100,  80,   0,   4 },    /* FCLATTACK     */
195     {   0,  20,  80, 100,   0,   8 },    /* KNGATTACK     */
196     {  40,  80, 100,  80,   6,   0 },    /* KNGPROTECT    */
197     {  50, 100,  60,  10,   0,   8 },    /* DNGPC         */
198     {  30, 100,  60,   5,   0,   6 },    /* LSATTACK      */
199     {   0,  50,  80, 100,   0,   8 },    /* NIHATTACK     */
200     {  50, 100,  80,  60,   8,   0 },    /* COHESION      */
201     { 100, 100,  80,  60,   4,   4 },    /* OPPDROP       */
202 };
203
204
205 short ADVNCM[NO_PIECES];
206
207 /* distance to enemy king */
208 static const short EnemyKingDistanceBonus[10] =
209 { 0, 6, 4, -1, -3, -4, -6, -8, -10, -12 };
210
211 /* distance to own king */
212 static const short OwnKingDistanceBonus[10] =
213 { 0, 5, 2, 1, 0, -1, -2, -3, -4, -5 };
214
215 /* distance to promotion zone */
216 #ifndef MINISHOGI
217 static const int PromotionZoneDistanceBonus[NO_ROWS] =
218 { 0, 0, 0, 0, 2, 6, 6, 8, 8 };
219 #else
220 static const int PromotionZoneDistanceBonus[NO_ROWS] =
221 { 0, 0, 2, 6, 8 };                                /* FIXME ? */
222 #endif
223
224 #define MAX_BMBLTY 20
225 #define MAX_RMBLTY 20
226 #define MAX_LMBLTY 8
227
228 /* Bishop mobility bonus indexed by # reachable squares */
229 static const short BMBLTY[MAX_BMBLTY] =
230 { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };
231
232 /* Rook mobility bonus indexed by # reachable squares */
233 static const short RMBLTY[MAX_RMBLTY] =
234 { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };
235
236 #ifndef MINISHOGI
237 /* Lance mobility bonus indexed by # reachable squares */
238 static const short LMBLTY[MAX_LMBLTY] =
239 { 0, 0, 0, 0, 4, 6, 8, 10 };
240 #endif
241
242 static const short MBLTY[NO_PIECES] =
243 { 0, 2,
244 #ifndef MINISHOGI
245   1, 10,
246 #endif
247   5, 5, 1, 1, 5,
248 #ifndef MINISHOGI
249   5, 5,
250 #endif
251   5, 1, 1, 4 };
252
253 static const short KTHRT[36] =
254 {   0,  -8, -20, -36, -52, -68, -80, -80, -80, -80, -80, -80,
255     -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80,
256     -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80 };
257
258 static small_short fvalue[2][NO_FEATURES];
259
260 long attack[2][NO_SQUARES];         /* threats to squares */
261 small_short sseed[NO_SQUARES];      /* square occupied by a seed piece? */
262
263 struct signature threats_signature[2] = /* statistics valid for position.. */
264 { { -1, -1 }, { -1, -1 } };             /* attack and sseed available */
265
266 small_short starget[2][NO_SQUARES]; /* significance as a target for a
267                                      * side of a square */
268 small_short sloose[NO_SQUARES];     /* square occupied by a loose piece? */
269 small_short shole[NO_SQUARES];      /* empty square a hole? */
270 small_short shung[NO_SQUARES];      /* hung piece? */
271
272 struct signature squares_signature =  /* statistics valid for position ... */
273 { 0, 0 };                /* starget, sloose, shole, shung available */
274
275 short target[2], seed[2], loose[2], hole[2];
276
277 short captured[2];  /* number of captured pieces */
278 short dcaptured[2]; /* different types of captured pieces */
279
280 small_short Kdist[2][NO_SQUARES];    /* distance to king */
281
282 short MAXADIST, MAXCDIST; /* maximum half move distance to pattern */
283
284 char GameType[2] = { UNKNOWN, UNKNOWN }; /* chosen game type of each side */
285
286
287 static short attack_opening_sequence[2];    /* current castle patterns */
288 static short castle_opening_sequence[2];    /* current attack formations */
289
290 static small_short Mpawn  [2][NO_SQUARES];
291 static small_short Msilver[2][NO_SQUARES];
292 static small_short Mgold  [2][NO_SQUARES];
293 static small_short Mking  [2][NO_SQUARES];
294 #ifndef MINISHOGI
295 static small_short Mlance [2][NO_SQUARES];
296 static small_short Mknight[2][NO_SQUARES];
297 #endif
298 static small_short Mbishop[2][NO_SQUARES];
299 static small_short Mrook  [2][NO_SQUARES];
300
301 static Mpiece_array Mpawn,
302 #ifndef MINISHOGI
303     Mlance, Mknight,
304 #endif
305     Msilver, Mgold, Mbishop, Mrook, Mking;
306
307 Mpiece_array *Mpiece[NO_PIECES] =
308 { NULL, &Mpawn,
309 #ifndef MINISHOGI
310   &Mlance, &Mknight,
311 #endif
312   &Msilver, &Mgold, &Mbishop, &Mrook,
313   &Mgold,
314 #ifndef MINISHOGI
315   &Mgold, &Mgold,
316 #endif
317   &Mgold, &Mbishop, &Mrook, &Mking };
318
319
320 static short c1, c2;
321 static small_short *PC1, *PC2;
322 static small_short *fv1;
323 static long *atk1, *atk2;
324 static long  a1, a2;
325
326 #define csquare(side, sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
327 #define crow(side, sq)    row(csquare(side, sq))
328 #define ccolumn(side, sq) column(csquare(side, sq))
329
330
331 inline static short
332 on_csquare(short side, short piece, short square)
333 {
334     short sq;
335     /* FIXME: un-obfuscate this! */
336     return ((board[sq = csquare(side, square)] == piece)
337             && (color[sq] == side));
338 }
339
340
341 inline static short
342 on_column(short side, short piece, short c)
343 {
344     short sq;
345
346     for (sq = c; sq < NO_SQUARES; sq += NO_COLS)
347     {
348         if (on_csquare(side, piece, sq))
349             return true;
350     }
351
352     return false;
353 }
354
355
356 #define empty_csquare(side, square) \
357   (board[csquare(side, square)] == no_piece)
358
359 inline static short
360 on_left_side(short side, short piece)
361 {
362     short c;
363
364     for (c = 0; c < 4; c++)
365     {
366         if (on_column(side, piece, c))
367             return true;
368     }
369
370     return false;
371 }
372
373
374 inline static short
375 on_right_side(short side, short piece)
376 {
377     short c;
378
379     for (c = 5; c < NO_COLS; c++)
380     {
381         if (on_column(side, piece, c))
382             return true;
383     }
384
385     return false;
386 }
387
388
389 short pscore[2];  /* piece score for each side */
390
391
392
393
394 /*
395  * Fill array attack[side][] with info about attacks to a square.  Bits
396  * 16-31 are set if the piece (king .. pawn) attacks the square.  Bits 0-15
397  * contain a count of total attacks to the square.  Fill array sseed[] with
398  * info about occupation by a seed piece.
399  */
400
401 void
402 threats(short side)
403 {
404     short  u, sq;
405     long   c;
406     long  *a;
407 #ifdef SAVE_NEXTPOS
408     short d;
409 #else
410     unsigned char  *ppos, *pdir;
411 #endif
412     short i, kd, piece, xside;
413     small_short *PL;
414
415     if (MatchSignature(threats_signature[side]))
416     {
417         /* data valid for current positional signature */
418         return;
419     }
420
421     a = attack[side];
422     xside = side ^ 1;
423
424     array_zero(a, NO_SQUARES * sizeof(a[0]));
425
426     PL = PieceList[side];
427
428     for (i = PieceCnt[side]; i >= 0; i--)
429     {
430         short ptyp;
431
432         sq = PL[i];
433         piece = board[sq];
434         ptyp = ptype[side][piece];
435         c = control[piece];
436 #ifdef SAVE_NEXTPOS
437         u = first_direction(ptyp, &d, sq);
438 #else
439         ppos = (*nextpos[ptyp])[sq];
440         pdir = (*nextdir[ptyp])[sq];
441         u = ppos[sq];
442 #endif
443
444         do
445         {
446             a[u] = ((a[u] + 1) | c);
447
448             if ((kd = Kdist[xside][u]) < 2)
449             {
450                 sseed[sq] += 2 - kd;
451                 seed[side]++;
452             }
453
454 #ifdef SAVE_NEXTPOS
455             u = ((color[u] == neutral)
456                  ? next_position(ptyp, &d, sq, u)
457                  : next_direction(ptyp, &d, sq));
458 #else
459             u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
460 #endif
461         }
462         while (u != sq);
463     }
464
465     /* data valid for current positional signature */
466     CopySignature(threats_signature[side]);
467 }
468
469
470
471 /*
472  * Compute the board square with nunmap offset "id".  If side == white,
473  * offset is negated.  inunmap[sq] is the corresponding nunmap index isq.
474  * nunmap[isq + id] computes the board square. If negative, it is outside
475  * the board.
476  */
477
478 static void
479 add_target(short sq, short side, short id)
480 {
481     short isq, tsq, xside;
482     isq = inunmap[sq];
483     tsq = (side == black) ? nunmap[isq + id] : nunmap[isq - id];
484
485     if (tsq >= 0)
486     {
487         target[xside = side^1]++;
488
489         if (attack[side][tsq])
490             starget[xside][tsq]++;  /* protected target square */
491         else
492             starget[xside][tsq] += 2; /* unprotected target square */
493     }
494 }
495
496
497
498 /*
499  * Target squares can be vertically ahead, diagonally ahead
500  * or diagonally behind.
501  */
502
503 static void
504 CheckTargetPiece(short sq, short side)
505 {
506     switch (board[sq])
507     {
508     case pawn: /* vertically ahead if unprotected */
509         if (!attack[side][sq])
510             add_target(sq, side, 11);
511         break;
512
513     case king: /* diagonally and vertically ahead */
514         add_target(sq, side, 10);
515         add_target(sq, side, 11);
516         add_target(sq, side, 12);
517         break;
518
519     case rook: /* diagonally ahead and behind */
520         add_target(sq, side, 10);
521         add_target(sq, side, 12);
522         add_target(sq, side, -10);
523         add_target(sq, side, -12);
524         break;
525
526     case bishop: /* vertically ahead */
527         add_target(sq, side, 11);
528         break;
529
530 #ifndef MINISHOGI
531     case knight: /* vertically ahead if advanced */
532         /* FIXME: gotta love them magic numbers... */
533         if ((sq != 1) && (sq != 7) && (sq != 73) && (sq != 79))
534             add_target(sq, side, 11);
535         break;
536 #endif
537     }
538 }
539
540
541
542 static short
543 ScoreKingOpeningFeatures(void)
544 {
545     short s = 0, sq = OwnKing, ds;
546
547     if (GameType[c1] == STATIC_ROOK)
548     {
549         /* Penalty for king on right side or fifth file */
550         short c;
551         c = 4 - ccolumn(c1, sq);
552
553         if ((c < 0) || ((c == 0) && (sq != kingP[c1])))
554             s += (ds = -c - c - fv1[OPENWRONG]);
555     }
556     else if (GameType[c1] == RANGING_ROOK)
557     {
558         /* Penalty for king on left side or fifth file */
559         short c;
560         c = 4 - ccolumn(c1, sq);
561
562         if ((c > 0) || ((c == 0) && (sq != kingP[c1])))
563         {
564             s += (ds = -c - c - fv1[OPENWRONG]);
565         }
566
567         /* Penalty for king moved before rook switch */
568         if (sq != kingP[c1])
569         {
570             if (on_csquare(c1, rook, 16))
571             {
572                 s += (ds = -4 * fv1[OPENWRONG]);
573             }
574         }
575         else
576         {
577             /* Penalty for sitting king after rook switch */
578             if (!on_csquare(c1, rook, 16))
579             {
580                 s += (ds = -2 * fv1[OPENWRONG]);
581             }
582         }
583
584         /* Penalty for defending general moved
585          * before king switch to right side */
586         if (ccolumn(c1, sq) < 6)
587         {
588             if (Mvboard[csquare(c1, 5)] || Mvboard[csquare(c1, 6)])
589             {
590                 s += (ds = -2 * fv1[OPENWRONG]);
591             }
592         }
593     }
594
595     return s;
596 }
597
598
599
600
601 inline static void
602 ExamineSquares(void)
603 {
604     short sq, side, piece, n;
605
606     if (MatchSignature(squares_signature))
607     {
608         /* data valid for current positional signature */
609         return;
610     }
611
612     array_zero(shole,   sizeof(shole));
613     array_zero(sloose,  sizeof(sloose));
614     array_zero(starget, sizeof(starget));
615
616     hole[0] = hole[1] = loose[0] = loose[1]
617         = target[0] = target[1] = 0;
618
619     for (sq = 0; sq < NO_SQUARES; sq++)
620     {
621         if ((side = color[sq]) == neutral)
622         {
623             if (InWhiteCamp(sq))
624             {
625                 if (!attack[white][sq])
626                 {
627                     shole[sq] = 1;
628                     hole[white]++;
629                 }
630             }
631             else if (InBlackCamp(sq))
632             {
633                 if (!attack[black][sq])
634                 {
635                     shole[sq] = 1;
636                     hole[black]++;
637                 }
638             }
639         }
640         else
641         {
642             /* occupied by "side" piece */
643             if (!attack[side][sq])
644             {
645                 sloose[sq] = 1;
646                 loose[side]++;
647             }
648
649             CheckTargetPiece(sq, side);
650         }
651     }
652
653     for (side = black; side <= white; side++)
654     {
655         captured[side] = dcaptured[side] = 0;
656
657         for (piece = pawn; piece <= rook; piece++)
658         {
659             if ((n = Captured[side][piece]) != 0)
660             {
661                 if (piece != pawn)
662                     captured[side] += n;
663
664                 dcaptured[side]++;
665             }
666         }
667     }
668
669     /* Data valid for current positional signature */
670     CopySignature(squares_signature);
671 }
672
673
674
675 /* ............    POSITIONAL EVALUATION ROUTINES    ............ */
676
677 /*
678  * Inputs are:
679  * mtl[side]       - value of all material
680  * hung[side]      - count of hung pieces
681  * Tscore[ply]     - search tree score for ply ply
682  * Pscore[ply]     - positional score for ply ply
683  * INCscore        - bonus score or penalty for certain moves
684  * Sdepth          - search goal depth
685  * xwndw           - evaluation window about alpha/beta
686  * EWNDW           - second evaluation window about alpha/beta
687  * ChkFlag[ply]    - checking piece at level ply or 0 if no check
688  * TesujiFlag[ply] - 1 if tesuji move at level ply or 0 if no tesuji
689  * PC1[column]     - # of my pawns in this column
690  * PC2[column]     - # of opponents pawns in column
691  * PieceCnt[side]  - just what it says
692  */
693
694
695
696
697 /*
698  * Compute an estimate of the score by adding the positional score from the
699  * previous ply to the material difference. If this score falls inside a
700  * window which is 180 points wider than the alpha-beta window (or within a
701  * 50 point window during quiescence search) call ScorePosition() to
702  * determine a score, otherwise return the estimated score.  "side" is the
703  * side which has to move.
704  */
705
706 int
707 evaluate(short side,
708          short ply,
709          short alpha,
710          short beta,
711          short INCscore,
712          short *InChk,     /* output Check flag     */
713          short *blockable) /* king threat blockable */
714 {
715     short xside;
716     short s;
717
718     xside = side ^ 1;
719     s = -Pscore[ply - 1] + mtl[side] - mtl[xside] /* - INCscore */;
720     hung[black] = hung[white] = 0;
721
722     /* should we use the estimete or score the position */
723     if ((ply == 1)
724         || (ply == Sdepth)
725         || (ply > Sdepth && s >= (alpha - 30) && s <= (beta + 30))
726 #ifdef CACHE
727         || (use_etable && CheckEETable(side))
728 #endif
729         )
730     {
731         short sq;
732
733         /* score the position */
734         array_zero(sseed, sizeof(sseed));
735
736         seed[0] = seed[1] = 0;
737         threats(side);
738
739         if (Anyattack(side, sq = PieceList[xside][0]) && (board[sq] == king))
740         {
741             *InChk = (board[sq = PieceList[side][0]] == king)
742                 ? SqAttacked(sq, xside, blockable)
743                 : false;
744
745             return ((SCORE_LIMIT + 1001) - ply);
746         }
747
748         threats(xside);
749         *InChk = (board[sq = PieceList[side][0]] == king)
750             ? Anyattack(xside, sq)
751             : false;
752         *blockable = true;
753         EvalNodes++;
754
755         if (ply > 4)
756             PUTVAR = true;
757
758         ExamineSquares();
759         s = ScorePosition(side);
760         PUTVAR = false;
761     }
762     else
763     {
764         /* use the estimate but look at check */
765         short sq;
766
767         *InChk = (board[sq = PieceList[side][0]] == king)
768             ? SqAttacked(sq, xside, blockable)
769             : false;
770
771         if ((board[sq = PieceList[xside][0]] == king)
772             && SqAttacked(sq, side, blockable))
773         {
774             return ((SCORE_LIMIT + 1001) - ply);
775         }
776     }
777
778     Pscore[ply] = s - mtl[side] + mtl[xside];
779     ChkFlag[ply - 1] = ((*InChk) ? Pindex[TOsquare] : 0);
780
781     return s;
782 }
783
784
785
786 static short
787 value_of_weakest_attacker(long a2)
788 {
789     short piece;
790     short min_value, v;
791     min_value = SCORE_LIMIT;
792
793     for (piece = pawn; piece <= king; piece++)
794     {
795         if (control[piece] & a2)
796         {
797             if (min_value > (v = (*value)[stage][piece]))
798                 min_value = v;
799         }
800     }
801
802     return min_value;
803 }
804
805
806
807
808 /*
809  * Find (promoted) Bishop, (promoted) Rook, and Lance mobility, x-ray
810  * attacks, and pins.  Let BRL be the bishop, rook, or lance.  Let P be the
811  * first piece (not a king or a pawn) in a direction and let Q be the
812  * second piece in the same direction.  If Q is an unprotected opponent's
813  * piece with bigger relative value than BRL, there is a pin if P is an
814  * opponent's piece and there is an x-ray attack if P belongs to this side.
815  * Increment the hung[] array if a pin is found.
816  */
817
818 inline int
819 BRLscan(short sq, short *mob)
820 {
821 #ifdef SAVE_NEXTPOS
822     short d, dd;
823 #else
824     unsigned char  *ppos, *pdir;
825 #endif
826
827     short s, mobx;
828     short u, xu, pin, ptyp, csq = column(sq);
829     short piece, upiece, xupiece, rvalue, ds;
830     small_short *Kd = Kdist[c2];
831
832     mobx = s = 0;
833     piece = board[sq];
834
835     rvalue = (*value)[stage][piece];
836     ptyp = ptype[c1][upiece = unpromoted[piece]];
837     rvalue = (*value)[stage][upiece];
838
839 #ifdef SAVE_NEXTPOS
840     u = first_direction(ptyp, &d, sq);
841 #else
842     ppos = (*nextpos[ptyp])[sq];
843     pdir = (*nextdir[ptyp])[sq];
844     u = ppos[sq];
845 #endif
846
847     pin = -1;           /* start new direction */
848
849     do
850     {
851         if (Kd[u] < 2)
852         {
853             s += (ds = fv1[CTRLK] * (2 - Kd[u]));
854         }
855
856         if ((ds = starget[c1][u]) != 0)
857         {
858             /* threatening a target square */
859             if ((pin < 0)               /* direct threat */
860                 || (color[pin] == c2))  /* pin threat    */
861             {
862                 s += (ds *= fv1[TARGET]);
863             }
864         }
865
866         if ((ds = shole[u]) != 0)
867         {
868             /* attacking or protecting a hole */
869             s += (ds = fv1[HOLES]);
870         }
871         else if (InPromotionZone(c1, u))
872         {
873             /* attacking a square in promotion zone */
874             s += (ds = fv1[HOLES] / 2);
875         }
876
877         if (color[u] == neutral)
878         {
879 #ifdef SAVE_NEXTPOS
880             dd = d;
881             xu = next_position(ptyp, &d, sq, u);
882
883             if (xu == next_direction(ptyp, &dd, sq))
884                 pin = -1;   /* oops new direction */
885 #else
886
887             if ((xu = ppos[u]) == pdir[u])
888                 pin = -1;   /* oops new direction */
889 #endif
890             u = xu;
891             mobx++;
892         }
893         else
894         {
895             /* there is a piece in current direction */
896             if (pin < 0)
897             {
898                 /* it's the first piece in the current direction */
899                 if (color[u] == c1)
900                 {
901 #ifndef MINISHOGI
902                     /* own intercepting piece in x-ray attack */
903                     if (upiece == lance)
904                     {
905                         /* lance x-ray */
906                         if (board[u] == pawn)
907                         {
908                             s += (ds = 2*fv1[PROTECT]);
909                         }
910                         else if (in_opening_stage)
911                         {
912                             s += (ds = -2*fv1[PROTECT]);
913                         }
914                     }
915                     else
916 #endif
917                     {
918                         /* bishop or rook x-ray */
919                         if ((upiece == bishop) && (board[u] == pawn)
920                             && (GameType[c1] == STATIC_ROOK))
921                         {
922                             s += (ds = -2*fv1[HCLSD]);
923                         }
924 #ifndef MINISHOGI
925                         else if ((upiece == rook) && (board[u] == lance)
926                                  && (GameType[c1] == STATIC_ROOK)
927                                  && (column(u) == csq))
928                         {
929                             s += (ds = fv1[XRAY]);
930                         }
931 #endif
932                     }
933                 }
934                 else
935                 {
936                     /* enemy's intercepting piece in pin attack */
937 #ifndef MINISHOGI
938                     if (upiece == lance)
939                     {
940                         /* lance pin attack */
941                         if (board[u] == pawn)
942                         {
943                             s += (ds = -2*fv1[PROTECT]);
944                         }
945                         else if (in_opening_stage)
946                         {
947                             s += (ds = 2 * fv1[PROTECT]);
948                         }
949                     }
950                     else
951 #endif
952                     {
953                         /* bishop or rook pin attack */
954                         if (board[u] == pawn)
955                         {
956                             s += (ds = -fv1[HCLSD]);
957                         }
958                     }
959                 }
960
961 #ifdef SAVE_NEXTPOS
962                 dd = d;
963                 xu = next_position(ptyp, &d, sq, u);
964
965                 if (xu != next_direction(ptyp, &dd, sq))
966                     pin = u;    /* not on the edge and on to find a pin */
967 #else
968                 if ((xu = ppos[u]) != pdir[u])
969                     pin = u;    /* not on the edge and on to find a pin */
970 #endif
971                 u = xu;
972             }
973             else
974             {
975                 /* it's the second piece in the current direction */
976
977                 if (color[u] == c1)
978                 {
979                     /* second piece is an own piece */
980                     if ((upiece == bishop) && (board[u] == pawn)
981                         && (GameType[c1] == STATIC_ROOK))
982                     {
983                         s += (ds = -fv1[HCLSD]);
984                     }
985                 }
986                 else
987                 {
988                     /* second piece is an enemy piece */
989                     if (upiece == bishop && board[u] == pawn)
990                     {
991                         s += (ds = -fv1[HCLSD]/2);
992                     }
993
994                     if (((*value)[stage][xupiece = unpromoted[board[u]]]
995                          > rvalue)
996                         || (atk2[u] == 0))
997                     {
998                         if (color[pin] == c2)
999                         {
1000                             if ((xupiece == king) && (in_endgame_stage))
1001                             {
1002                                 s += (ds = 2 * fv1[PINVAL]);
1003                             }
1004                             else
1005                             {
1006                                 s += (ds = fv1[PINVAL]);
1007                             }
1008
1009                             if ((atk2[pin] == 0)
1010                                 || (atk1[pin] > control[board[pin]] + 1))
1011                             {
1012                                 hung[c2]++;
1013                                 shung[u]++;
1014                             }
1015                         }
1016                         else
1017                         {
1018 #ifndef MINISHOGI
1019                             if (upiece == lance)
1020                             {
1021                                 s += (ds = fv1[XRAY] / 2);
1022                             }
1023                             else
1024 #endif
1025                             {
1026                                 s += (ds = fv1[XRAY]);
1027                             }
1028                         }
1029                     }
1030                 }
1031
1032                 pin = -1;   /* new direction */
1033
1034 #ifdef SAVE_NEXTPOS
1035                 u = next_direction(ptyp, &d, sq);
1036 #else
1037                 u = pdir[u];
1038 #endif
1039             }
1040         }
1041     }
1042     while (u != sq);
1043
1044     *mob = mobx;
1045
1046     return s;
1047 }
1048
1049
1050 #define ctlSG (ctlS | ctlG | ctlPp | ctlLp | ctlNp | ctlSp)
1051
1052
1053
1054 /*
1055  * Assign penalties if king can be threatened by checks, if squares near
1056  * the king are controlled by the enemy (especially by promoted pieces), or
1057  * if there are no own generals near the king.
1058  *
1059  * The following must be true:
1060  * board[sq] == king, c1 == color[sq], c2 == otherside[c1]
1061  */
1062
1063 #define SCORETHREAT \
1064 { \
1065     if (color[u] != c2) \
1066     { \
1067         if ((atk1[u] == 0) || ((atk2[u] & CNT_MASK) != 0)) \
1068         { \
1069             ++cnt; \
1070         } \
1071         else \
1072         { \
1073             s += (ds = -fv1[CTRLK]); \
1074         } \
1075     } \
1076 }
1077
1078
1079
1080
1081 inline short
1082 KingScan(short sq)
1083 {
1084     short cnt;
1085
1086 #ifdef SAVE_NEXTPOS
1087     short d;
1088 #else
1089     unsigned char  *ppos, *pdir;
1090 #endif
1091
1092     short s;
1093     short u, ptyp;
1094     short ok, ds;
1095
1096     /* Penalties, if a king can be threatened by checks. */
1097
1098     s = 0;
1099     cnt = 0;
1100
1101     {
1102         short p;
1103
1104         for (p = pawn; p < king; p++)
1105         {
1106             if (HasPiece[c2][p] || Captured[c2][p])
1107             {
1108                 short ptyp;
1109
1110                 /* if a c1 piece can reach u from sq,
1111                  * then a c2 piece can reach sq from u.
1112                  * That means, each u is a square, from which a
1113                  * piece of type p and color c2 threats square sq.
1114                  */
1115                 ptyp = ptype[c1][p];
1116
1117 #ifdef SAVE_NEXTPOS
1118                 u = first_direction(ptyp, &d, sq);
1119 #else
1120                 ppos = (*nextpos[ptyp])[sq];
1121                 pdir = (*nextdir[ptyp])[sq];
1122                 u = ppos[sq];
1123 #endif
1124
1125                 do
1126                 {
1127                     /* If a p piece can reach (controls or can drop to)
1128                      * square u, then score threat.
1129                      */
1130
1131                     if (atk2[u] & control[p])
1132                         SCORETHREAT
1133                     else if ((Captured[c2][p] && color[u]) == neutral)
1134                         SCORETHREAT
1135
1136 #ifdef SAVE_NEXTPOS
1137                     u = ((color[u] == neutral)
1138                          ? next_position(ptyp, &d, sq, u)
1139                          : next_direction(ptyp, &d, sq));
1140 #else
1141                     u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1142 #endif
1143                 }
1144                 while (u != sq);
1145             }
1146         }
1147     }
1148
1149     s += (ds = fv1[KSFTY] * KTHRT[cnt] / 16);
1150
1151     /* Penalties, if squares near king are controlled by enemy. */
1152
1153     cnt = 0;
1154     ok = false;
1155     ptyp = ptype[c1][king];
1156
1157 #ifdef SAVE_NEXTPOS
1158     u = first_direction(ptyp, &d, sq);
1159 #else
1160     pdir = (*nextpos[ptyp])[sq];
1161     u = pdir[sq];
1162 #endif
1163
1164     do
1165     {
1166         if (!ok && color[u] == c1)
1167         {
1168             short ptype_piece = ptype[black][board[u]];
1169
1170             if ((ptype_piece == ptype_silver) || (ptype_piece == ptype_gold))
1171                 ok = true;
1172         }
1173
1174         if (atk2[u] > atk1[u])
1175         {
1176             ++cnt;
1177
1178             if (atk2[u] & ctlSG)
1179             {
1180                 s += (ds = -fv1[KSFTY] / 2);
1181             }
1182         }
1183
1184 #ifdef SAVE_NEXTPOS
1185         u = next_direction(ptyp, &d, sq);
1186 #else
1187         u = pdir[u];
1188 #endif
1189     }
1190     while (u != sq);
1191
1192     if (!ok || (cnt > 1))
1193     {
1194         if (cnt > 1)
1195             s += (ds = -fv1[KSFTY]/2);
1196         else
1197             s += (ds = -fv1[KSFTY]);
1198     }
1199
1200     return s;
1201 }
1202
1203
1204 static short checked_trapped;
1205
1206
1207 /*
1208  * See if the attacked piece has unattacked squares to move to. The following
1209  * must be true: c1 == color[sq] c2 == otherside[c1]
1210  */
1211
1212 inline int
1213 trapped(short sq)
1214 {
1215     short u, ptyp;
1216 #ifdef SAVE_NEXTPOS
1217     short d;
1218 #else
1219     unsigned char *ppos, *pdir;
1220 #endif
1221     short piece;
1222     short rvalue;
1223
1224     piece  = board[sq];
1225     rvalue = (*value)[stage][piece];
1226     ptyp   = ptype[c1][piece];
1227
1228 #ifdef SAVE_NEXTPOS
1229     u = first_direction(ptyp, &d, sq);
1230 #else
1231     ppos = (*nextpos[ptyp])[sq];
1232     pdir = (*nextdir[ptyp])[sq];
1233     u = ppos[sq];
1234 #endif
1235
1236     do
1237     {
1238         if (color[u] != c1)
1239         {
1240             if ((atk2[u] == 0) || ((*value)[stage][board[u]] >= rvalue))
1241                 return false;
1242         }
1243
1244 #ifdef SAVE_NEXTPOS
1245         u = ((color[u] == neutral)
1246              ? next_position(ptyp, &d, sq, u)
1247              : next_direction(ptyp, &d, sq));
1248 #else
1249         u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1250 #endif
1251     }
1252     while (u != sq);
1253
1254     checked_trapped = true;
1255
1256     return true;
1257 }
1258
1259
1260
1261 static int
1262 AttackedPieceValue(short sq, short side)
1263 {
1264     short s, ds;
1265
1266     s = 0;
1267
1268         ds = -fv1[HUNGP] * 2;
1269     hung[c1]++;
1270     shung[sq]++;
1271
1272     if (trapped(sq))
1273     {
1274         hung[c1]  += 2;
1275         shung[sq] += 2;
1276     }
1277
1278     return s;
1279 }
1280
1281
1282
1283 static inline int
1284 OpenFileValue(short sq, short hopn, short hopnx)
1285 {
1286     short s = 0, fyle;
1287
1288     if (PC1[fyle = column(sq)] == 0)
1289     {
1290         s += hopn;
1291     }
1292
1293     if (PC2[fyle] == 0)
1294     {
1295         s += hopnx;
1296     }
1297
1298     return s;
1299 }
1300
1301
1302
1303
1304 /* Distance bonus */
1305
1306 #define PromotionZoneDistanceValue(sq, dd) \
1307 if ((ds = fv1[PROMD])) \
1308 { \
1309     s += (ds = ds * PromotionZoneDistanceBonus[crow(c1, sq)] * dd); \
1310 }
1311
1312 #define OwnKingDistanceValue(sq, dd, maxd) \
1313 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c1][sq]) <= maxd)) \
1314 { \
1315     s += (ds = ds * OwnKingDistanceBonus[ad] * dd); \
1316 }
1317
1318 #define EnemyKingDistanceValue(sq, dd, maxd) \
1319 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c2][sq]) <= maxd)) \
1320 { \
1321     s += (ds = ds * EnemyKingDistanceBonus[ad] * dd); \
1322 }
1323
1324
1325
1326
1327
1328 /*
1329  * Calculate the positional value for a pawn on 'sq'.
1330  */
1331
1332 static inline int
1333 PawnValue(short sq, short side)
1334 {
1335     short s = 0;
1336     short ds;
1337     short ccol = ccolumn(c1, sq);
1338
1339     PromotionZoneDistanceValue(sq, 3);
1340
1341     /* pawn mobility */
1342     if (color[(c1 == black) ? (sq + NO_COLS) : (sq - NO_COLS)] == neutral)
1343     {
1344         s += (ds = MBLTY[pawn]);
1345     }
1346
1347     if ((a1 & ((ctlR | ctlRp) | ctlL)))
1348     {
1349         s += (ds = fv1[ATTACKED]);
1350     }
1351
1352     if (in_opening_stage)
1353     {
1354 #ifndef MINISHOGI
1355 /* FIXME: [HGM] The 3rd-rank Pawn section is meaningless in mini-Shogi,
1356  * (which does not have opposing Pawns), and can do out-of-bound access,
1357  * as the promotion zone is only 1 rank, so Pawns can be closer than 3 ranks
1358  * to the board edge.
1359  */
1360         if (crow(c1, sq) == 2) /* pawn on 3d rank */
1361         {
1362             if (board[(c1 == black) ?
1363                       (sq + 3*NO_COLS) : (sq - 3*NO_COLS)] == pawn)
1364             {
1365                 /* opposing pawn has been moved (even column == (sq & 1)) */
1366
1367                 short m;
1368
1369                 switch (ccol)
1370                 {
1371                 case 0:
1372                 case 8:
1373                     m = (side == c1) ? 3 : 5;
1374                     break;
1375
1376                 case 4:
1377                     m = (side == c1) ? 2 : 3;
1378                     break;
1379
1380                 default:
1381                     m = (side == c1) ? 1 : 2;
1382                     break;
1383                 }
1384
1385                 s += (ds = -m * MBLTY[pawn]);
1386             }
1387         }
1388
1389 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1390  * board - and anyway we don't know the stage really :)
1391  */
1392         if ((GameType[c1] == STATIC_ROOK) && (sq == csquare(c1, 43)))
1393         {
1394             if ((atk2[csquare(c1, 52)] & CNT_MASK) < 2)
1395             {
1396                 s += (ds = fv1[ATTACKED]);
1397             }
1398         }
1399
1400         if ((GameType[c2] == STATIC_ROOK) && (ccol == 1))
1401         {
1402             if (sq == csquare(c1, 28))
1403             {
1404                 s += (ds = -fv1[ATTACKED]);
1405             }
1406
1407             if (((atk1[csquare(c1, 19)] & CNT_MASK) < 2)
1408                 && ((atk1[csquare(c1, 28)] & CNT_MASK) < 2))
1409             {
1410                 s += (ds = -2 * fv1[ATTACKED]);
1411             }
1412         }
1413 #endif
1414     }
1415
1416     return s;
1417 }
1418
1419
1420 #ifndef MINISHOGI
1421 /*
1422  * Calculate the positional value for a lance on 'sq'.
1423  */
1424
1425 static inline int
1426 LanceValue(short sq, short side)
1427 {
1428     short s = 0, ds, ad;
1429
1430     OwnKingDistanceValue(sq, 1, 2);
1431
1432     OpenFileValue(sq, -fv1[HOPN], fv1[HOPN]);
1433
1434     if (!checked_trapped && (crow(c1, sq) > 2))
1435     {
1436         if (in_opening_stage || trapped(sq))
1437         {
1438             s += (ds = -3 * fv1[ATTACKED]);
1439         }
1440         else
1441         {
1442             s += (ds = -2 * fv1[ATTACKED]);
1443         }
1444     }
1445
1446     return s;
1447 }
1448
1449
1450
1451 /*
1452  * Calculate the positional value for a knight on 'sq'.
1453  */
1454
1455 static inline int
1456 KnightValue(short sq, short side)
1457 {
1458     short s = 0, ad;
1459     short ds, checked_trapped = false;
1460     short c = column(sq);
1461
1462     PromotionZoneDistanceValue(sq, 1);
1463     OwnKingDistanceValue(sq, 1, 2);
1464
1465     if (!checked_trapped && (crow(c1, sq) > 2))
1466     {
1467         if (trapped(sq))
1468         {
1469             s += (ds = -4 * fv1[ATTACKED]);
1470         }
1471         else
1472         {
1473             s += (ds = -3 * fv1[ATTACKED]);
1474         }
1475     }
1476
1477     if ((c == 0) || (c == 8))
1478     {
1479         s += (ds = -fv1[ATTACKED]);
1480     }
1481
1482     return s;
1483 }
1484 #endif
1485
1486
1487
1488 /*
1489  * Calculate the positional value for a silver on 'sq'.
1490  */
1491
1492 static inline int
1493 SilverValue(short sq, short side)
1494 {
1495     short s= 0, ds, ad;
1496
1497     OwnKingDistanceValue(sq, 2, 3);
1498
1499     if ((Kdist[c1][sq] < 3)
1500         && (atk1[sq] & (control[gold] | control[silver])))
1501     {
1502         s += (ds = fv1[COHESION]);
1503     }
1504
1505     if (in_opening_stage)
1506     {
1507 #ifndef MINISHOGI
1508 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1509  * board - and anyway we don't know the stage really :)
1510  */
1511         if (GameType[c1] == STATIC_ROOK)
1512         {
1513             if (csquare(c1, sq) == 12)
1514             {
1515                 short csq;
1516
1517                 if ((board[csq = csquare(c1, 20)] == bishop)
1518                     && (color[csq] == c1))
1519                 {
1520                     s += (ds = -2 * fv1[OPENWRONG]);
1521                 }
1522             }
1523         }
1524 #endif
1525     }
1526     else
1527     {
1528         EnemyKingDistanceValue(sq, 2, 3);
1529     }
1530
1531     return s;
1532 }
1533
1534
1535
1536 /*
1537  * Calculate the positional value for a gold on 'sq'.
1538  */
1539
1540 static inline int
1541 GoldValue(short sq, short side)
1542 {
1543     short s = 0, ds, ad;
1544
1545     OwnKingDistanceValue(sq, 2, 3);
1546
1547     if ((Kdist[c1][sq] < 3)
1548         && (atk1[sq] & (control[gold] | control[silver])))
1549     {
1550         s += (ds = fv1[COHESION]);
1551     }
1552
1553     if (in_opening_stage)
1554     {
1555 #ifndef MINISHOGI
1556 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1557  * board - and anyway we don't know the stage really :)
1558  */
1559         if ((GameType[c1] == STATIC_ROOK) && (GameType[c2] != STATIC_ROOK))
1560         {
1561             if (Mvboard[csquare(c1, 3)])
1562             {
1563                 s += (ds = -2 * fv1[OPENWRONG]);
1564             }
1565         }
1566 #endif
1567     }
1568     else
1569     {
1570         EnemyKingDistanceValue(sq, 2, 3);
1571     }
1572
1573     return s;
1574 }
1575
1576
1577
1578 /*
1579  * Calculate the positional value for a bishop on 'sq'.
1580  */
1581
1582 static inline int
1583 BishopValue(short sq, short side)
1584 {
1585     short s = 0, ds, ad;
1586
1587     if (in_opening_stage)
1588     {
1589 #ifndef MINISHOGI
1590 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1591  * board - and anyway we don't know the stage really :)
1592  */
1593         if (GameType[c1] == RANGING_ROOK)
1594         {
1595             /* Bishops diagonal should not be open */
1596             if (!on_csquare(c1, pawn, 30))
1597             {
1598                 s += (ds = -fv1[OPENWRONG]);
1599             }
1600         }
1601         else if (GameType[c2] == RANGING_ROOK)
1602         {
1603             /* Bishops diagonal should be open */
1604             if ((csquare(c1, sq) == 10)
1605                 && (!empty_csquare(c1, 20) || !empty_csquare(c1, 30)))
1606             {
1607                 s += (ds = -fv1[OPENWRONG]);
1608             }
1609             else if ((csquare(c1, sq) == 20) && !empty_csquare(c1, 30))
1610             {
1611                 s += (ds = -fv1[OPENWRONG]);
1612             }
1613         }
1614 #endif
1615     }
1616     else
1617     {
1618         EnemyKingDistanceValue(sq, 1, 3);
1619     }
1620
1621     return s;
1622 }
1623
1624
1625
1626 /*
1627  * Calculate the positional value for a rook on 'sq'.
1628  */
1629
1630 static inline int
1631 RookValue(short sq, short side)
1632 {
1633     short s = 0, ds, ad;
1634
1635     OpenFileValue(sq, 2 * fv1[HOPN], 4*fv1[HOPN]);
1636
1637     if (in_opening_stage)
1638     {
1639 #ifndef MINISHOGI
1640 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1641  * board - and anyway we don't know the stage really :)
1642  */
1643         short WRONG = fv1[OPENWRONG], OPOK = WRONG / 3;
1644
1645         if (GameType[c1] == STATIC_ROOK)
1646         {
1647             short c = ccolumn(c1, sq);
1648
1649             /* Bonus for rook on 8th file */
1650             if (c == 7)
1651             {
1652                  s += (ds = OPOK);
1653             }
1654
1655             /*
1656              * Bonus for rook on right side,
1657              * penalty for rook on left side
1658              */
1659
1660             c = 4 - c;
1661             ds = 0;
1662
1663             if (c < 0)
1664             {
1665                 s += (ds = c + c + OPOK);
1666             }
1667             else if (c >= 0)
1668             {
1669                 s += (ds = -c - c - WRONG);
1670             }
1671         }
1672         else if (GameType[c1] == RANGING_ROOK)
1673         {
1674             /* Bonus for rook on left side and
1675              * bishops diagonal closed, penalty otherwise. */
1676
1677             short c;
1678             c = 4 - ccolumn(c1, sq);
1679             ds = 0;
1680
1681             if (c >= 0)
1682             {
1683                 /* Bishops diagonal should not be open. */
1684                 if (on_csquare(c1, pawn, 30))
1685                     s += (ds = OPOK);
1686                 else
1687                     s += (ds = -c - c - WRONG);
1688             }
1689             else if (c < 0)
1690             {
1691                 s += (ds = -c - c - WRONG);
1692
1693                 /* Penalty for king not on initial square. */
1694                 if (!on_csquare(side, king, 4))
1695                 {
1696                     s  += -4 * WRONG;
1697                     ds += -4 * WRONG;
1698                 }
1699             }
1700         }
1701 #endif
1702     }
1703     else
1704     {
1705         EnemyKingDistanceValue(sq, 1, 3);
1706     }
1707
1708     return s;
1709 }
1710
1711
1712
1713 /*
1714  * Calculate the positional value for a promoted pawn on 'sq'.
1715  */
1716
1717 static inline int
1718 PPawnValue(short sq, short side)
1719 {
1720     short s = 0, ds, ad;
1721
1722     EnemyKingDistanceValue(sq, 3, 10);
1723
1724     return s;
1725 }
1726
1727
1728
1729 #ifndef MINISHOGI
1730 /*
1731  * Calculate the positional value for a promoted lance on 'sq'.
1732  */
1733
1734 static inline int
1735 PLanceValue(short sq, short side)
1736 {
1737     short s = 0, ds, ad;
1738
1739     EnemyKingDistanceValue(sq, 3, 10);
1740
1741     return s;
1742 }
1743
1744
1745
1746 /*
1747  * Calculate the positional value for a promoted knight on 'sq'.
1748  */
1749
1750 static inline int
1751 PKnightValue(short sq, short side)
1752 {
1753     short s = 0, ds, ad;
1754
1755     EnemyKingDistanceValue(sq, 3, 10);
1756
1757     return s;
1758 }
1759 #endif
1760
1761
1762
1763 /*
1764  * Calculate the positional value for a promoted silver on 'sq'.
1765  */
1766
1767 static inline int
1768 PSilverValue(short sq, short side)
1769 {
1770     short s = 0, ds, ad;
1771
1772     EnemyKingDistanceValue(sq, 3, 10);
1773
1774     return s;
1775 }
1776
1777
1778
1779 /*
1780  * Calculate the positional value for a promoted bishop on 'sq'.
1781  */
1782
1783 static inline int
1784 PBishopValue(short sq, short side)
1785 {
1786     short s = 0, ds, ad;
1787
1788     EnemyKingDistanceValue(sq, 3, 4);
1789
1790     return s;
1791 }
1792
1793
1794
1795 /*
1796  * Calculate the positional value for a promoted rook on 'sq'.
1797  */
1798
1799 static inline int
1800 PRookValue(short sq, short side)
1801 {
1802     short s = 0, ds, ad;
1803
1804     EnemyKingDistanceValue(sq, 3, 4);
1805
1806     OpenFileValue(sq, 3 * fv1[HOPN], 2 * fv1[HOPN]);
1807
1808     return s;
1809 }
1810
1811
1812
1813 /*
1814  * Calculate the positional value for a king on 'sq'.
1815  */
1816
1817 static inline int
1818 KingValue(short sq, short side)
1819 {
1820     short s = 0, ds;
1821
1822     if (fv1[KSFTY] != 0)
1823         s += KingScan(sq);
1824
1825 #ifndef MINISHOGI
1826 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1827  * board - and anyway we don't know the stage really :)
1828  */
1829     if (in_opening_stage)
1830     {
1831         if ((GameType[c1] != UNKNOWN) && (ccolumn(c1, sq) == 4))
1832         {
1833             s += (ds = -fv1[OPENWRONG] / 3);
1834         }
1835         else if ((GameType[c1] == STATIC_ROOK) && on_right_side(c1, sq))
1836         {
1837             s += (ds = -fv1[OPENWRONG] / 2);
1838         }
1839         else if ((GameType[c1] == RANGING_ROOK) && on_left_side(c1, sq))
1840         {
1841             s += (ds = -fv1[OPENWRONG] / 2);
1842         }
1843     }
1844 #endif
1845
1846     /* CHECKME: is this correct? */
1847     if ((ds = fv1[HOPN]))
1848     {
1849         s += OpenFileValue(sq, -2 * ds, -4 * ds);
1850     }
1851
1852     return s;
1853 }
1854
1855
1856
1857 /*
1858  * Calculate the positional value for a piece on 'sq'.
1859  */
1860
1861 static inline int
1862 PieceValue(short sq, short side)
1863 {
1864     short s, piece, ds;
1865     short mob;
1866
1867     piece = board[sq];
1868
1869     if (piece == no_piece)
1870         return 0;
1871
1872     s = (*Mpiece[piece])[c1][sq];
1873
1874     checked_trapped = false;
1875
1876     if (sweep[piece])
1877     {
1878         /* pin/x-ray attack and mobility for sweeping pieces */
1879         s += (ds = BRLscan(sq, &mob));
1880
1881         if ((piece == bishop) || (piece == pbishop))
1882             s += (ds = BMBLTY[mob] * fv1[MOBILITY] / 100);
1883         else if ((piece == rook) || (piece == prook))
1884             s += (ds = RMBLTY[mob] * fv1[MOBILITY] / 100);
1885 #ifndef MINISHOGI
1886         else
1887             s += (ds = LMBLTY[mob] * fv1[MOBILITY] / 100);
1888 #endif
1889     }
1890     else
1891     {
1892         /* mobility for non-sweeping pieces */
1893     }
1894
1895     a2 = atk2[sq];
1896     a1 = atk1[sq];
1897
1898     if (a2 > 0)
1899     {
1900         /* opponent attacks piece */
1901         if (a1 == 0)
1902         {
1903             /* undefended piece */
1904             s += AttackedPieceValue(sq, side);
1905         }
1906         else
1907         {
1908             /* defended piece */
1909             short attack_value = value_of_weakest_attacker(a2);
1910             short piece_value = (*value)[stage][piece];
1911
1912             if (attack_value < piece_value)
1913             {
1914                 /* attacked by a weaker piece */
1915                 s += AttackedPieceValue(sq, side) / 2;
1916             }
1917             else if (abs(attack_value - piece_value) < 10)
1918             {
1919                 /* opponent has the option to exchange equal pieces */
1920                 s += (ds = -fv1[ATTACKED]);
1921             }
1922         }
1923     }
1924
1925     if (piece != king)
1926     {
1927
1928         if (a1 > 0)
1929         {
1930             /* piece is defended */
1931             s += (ds = (a1 & CNT_MASK) * fv1[PROTECT]);
1932         }
1933
1934         if (sseed[sq])
1935         {
1936             s += (ds = fv1[SEED]);
1937         }
1938
1939         if (sloose[sq])
1940         {
1941             s += (ds = -fv1[LOOSE]);
1942         }
1943
1944         if (starget[c1][sq])
1945         {
1946             if (sweep[piece])
1947             {
1948                 s -= (ds = -fv1[ATTACKED]/2);
1949             }
1950             else if (piece == pawn)
1951             {
1952                 s += (ds = fv1[ATTACKED]);
1953             }
1954         }
1955
1956         if (starget[c2][sq])
1957         {
1958             if (piece != pawn)
1959             {
1960                 s -= (ds = -fv1[ATTACKED] / 3);
1961             }
1962             else
1963             {
1964                 s += (ds = fv1[ATTACKED]);
1965             }
1966         }
1967
1968         if (Kdist[c1][sq] == 1)
1969         {
1970             s += (ds = fv1[KSFTY]);
1971         }
1972     }
1973
1974     switch (piece)
1975     {
1976     case pawn:
1977         s += PawnValue(sq, side);
1978         break;
1979
1980 #ifndef MINISHOGI
1981     case lance:
1982         s += LanceValue(sq, side);
1983         break;
1984
1985     case knight:
1986         s += KnightValue(sq, side);
1987         break;
1988 #endif
1989
1990     case silver:
1991         s += SilverValue(sq, side);
1992         break;
1993
1994     case gold:
1995         s += GoldValue(sq, side);
1996         break;
1997
1998     case bishop:
1999         s += BishopValue(sq, side);
2000         break;
2001
2002     case rook:
2003         s += RookValue(sq, side);
2004         break;
2005
2006     case king:
2007         s += KingValue(sq, side);
2008         break;
2009
2010     case ppawn:
2011         s += PPawnValue(sq, side);
2012         break;
2013
2014 #ifndef MINISHOGI
2015     case plance:
2016         s += PLanceValue(sq, side);
2017         break;
2018
2019     case pknight:
2020         s += PKnightValue(sq, side);
2021         break;
2022 #endif
2023
2024     case psilver:
2025         s += PSilverValue(sq, side);
2026         break;
2027
2028     case pbishop:
2029         s += PBishopValue(sq, side);
2030         break;
2031
2032     case prook:
2033         s += PRookValue(sq, side);
2034         break;
2035     }
2036
2037     return s;
2038 }
2039
2040
2041
2042 /*
2043  * Score distance to pattern regarding the game type which side plays.
2044  */
2045
2046 short
2047 ScorePatternDistance(short c1)
2048 {
2049     short ds, s = 0;
2050     small_short *fv1 = fvalue[c1];
2051     short os = 0;
2052
2053     if ((MAXCDIST > 0) && (fv1[PCASTLE] != 0)
2054         && ((os = castle_opening_sequence[c1]) >= 0))
2055     {
2056         ds = board_to_pattern_distance(c1, os, MAXCDIST, GameCnt);
2057
2058         if (ds != 0)
2059         {
2060             s += (ds *= fv1[PCASTLE]);
2061         }
2062     }
2063
2064     if ((MAXADIST > 0) && (fv1[PATTACK] != 0)
2065         && ((os = attack_opening_sequence[c1]) >= 0))
2066     {
2067         ds = board_to_pattern_distance(c1, os, MAXADIST, GameCnt);
2068
2069         if (ds != 0)
2070         {
2071             s += (ds *= fv1[PATTACK]);
2072         }
2073     }
2074
2075     return s;
2076 }
2077
2078
2079
2080
2081 /*
2082  * Determine castle and attack pattern which should be reached next.
2083  * Only patterns are considered, which have not been reached yet.
2084  */
2085
2086 static void
2087 UpdatePatterns(short side, short GameCnt)
2088 {
2089     char s[12];
2090     short xside = side ^ 1;
2091     short os;
2092     short j, k, n = 0;
2093
2094     strcpy(s, "CASTLE_?_?");
2095     s[7] = GameType[side];
2096     s[9] = GameType[xside];
2097     castle_opening_sequence[side] = os
2098         = locate_opening_sequence(side, s, GameCnt);
2099
2100     if (flag.post && (os != END_OF_SEQUENCES))
2101     {
2102         for (j = 0; j < MAX_SEQUENCE; j++)
2103         {
2104             for (k = OpeningSequence[os].first_pattern[j];
2105                  k != END_OF_PATTERNS;
2106                  k = Pattern[k].next_pattern)
2107             {
2108                 if (Pattern[k].distance[side] >= 0)
2109                     n++;
2110             }
2111         }
2112     }
2113
2114     if (os != END_OF_SEQUENCES)
2115         update_advance_bonus(side, os);
2116
2117     strcpy(s, "ATTACK_?_?");
2118     s[7] = GameType[side];
2119     s[9] = GameType[xside];
2120     attack_opening_sequence[side] = os
2121         = locate_opening_sequence(side, s, GameCnt);
2122
2123     if (flag.post && (os != END_OF_SEQUENCES))
2124     {
2125         for (j = 0; j < MAX_SEQUENCE; j++)
2126         {
2127             for (k = OpeningSequence[os].first_pattern[j];
2128                  k != END_OF_PATTERNS;
2129                  k = Pattern[k].next_pattern)
2130             {
2131                 if (Pattern[k].distance[side] >= 0)
2132                     n++;
2133             }
2134         }
2135     }
2136
2137     if (flag.post)
2138         dsp->ShowPatternCount(side, n);
2139
2140     if (os != END_OF_SEQUENCES)
2141         update_advance_bonus(side, os);
2142 }
2143
2144
2145
2146 static void
2147 ScoreCaptures(void)
2148 {
2149     short ds, col, n, m, piece;
2150
2151     if ((n = Captured[c1][pawn]))
2152     {
2153         ds = m = 0;
2154
2155         for (col = 0; col < NO_COLS; col++)
2156         {
2157             if (!PC1[col])
2158             {
2159                 m++;
2160                 ds += fv1[PWNDROP];
2161             }
2162         }
2163
2164         /* FIXME! another great obfuscated line... */
2165         pscore[c1] += (ds *= ((n > 2) ? 3 : n));
2166     }
2167
2168     if ((m = seed[c1]))
2169     {
2170         for (piece = pawn+1, n = 0; piece <= rook; piece++)
2171         {
2172             if (Captured[c1][piece])
2173                 n++;
2174         }
2175
2176         pscore[c1] += (ds = m * fv1[DFFDROP]);
2177     }
2178
2179     for (piece = pawn, n = 0; piece <= rook; piece++)
2180     {
2181         if (Captured[c1][piece])
2182         {
2183             switch (piece)
2184             {
2185             case bishop:
2186                 ds = BMBLTY[MAX_BMBLTY - 1];
2187                 break;
2188
2189             case rook:
2190                 ds = RMBLTY[MAX_RMBLTY - 1];
2191                 break;
2192
2193 #ifndef MINISHOGI
2194             case lance:
2195                 ds = LMBLTY[MAX_LMBLTY - 1];
2196                 break;
2197 #endif
2198
2199             default:
2200                 ds = MBLTY[piece];
2201             }
2202
2203             pscore[c1] += ds;
2204
2205             if (!Captured[c2][piece])
2206                 n += relative_value[piece];
2207         }
2208     }
2209
2210     if (n)
2211     {
2212         pscore[c1] += (ds = -n * fv1[OPPDROP] / 2);
2213     }
2214 }
2215
2216
2217
2218
2219
2220 /*
2221  * Perform normal static evaluation of board position. A score is generated
2222  * for each piece and these are summed to get a score for each side.
2223  */
2224
2225 short
2226 ScorePosition(short side)
2227 {
2228     short score;
2229     short sq, i, xside;
2230     short s;
2231     short ds;
2232
2233     xside = side ^ 1;
2234
2235     UpdateWeights(side);
2236
2237     hung[black] = hung[white] = pscore[black] = pscore[white] = 0;
2238
2239     array_zero(shung, sizeof(shung));
2240
2241 #ifdef CACHE
2242     if (!(use_etable && ProbeEETable(side, &s)))
2243     {
2244 #endif
2245         for (c1 = black; c1 <= white; c1++)
2246         {
2247             c2 = c1 ^ 1;
2248
2249             /* atk1 is array of attacks on squares by my side */
2250             atk1 = attack[c1];
2251
2252             /* atk2 is array of attacks on squares by other side */
2253             atk2 = attack[c2];
2254
2255             /* same for PC1 and PC2 */
2256             PC1 = PawnCnt[c1];
2257             PC2 = PawnCnt[c2];
2258
2259             /* same for fv1 and fv2 */
2260             fv1 = fvalue[c1];
2261
2262             for (i = PieceCnt[c1]; i >= 0; i--)
2263             {
2264                 sq = PieceList[c1][i];
2265
2266 #if defined SAVE_SVALUE
2267                 pscore[c1] += PieceValue(sq, side);
2268 #else
2269                 pscore[c1] += (svalue[sq] = PieceValue(sq, side));
2270 #endif
2271             }
2272
2273             ScoreCaptures();
2274         }
2275
2276 #ifndef MINISHOGI
2277 # define BLACKHOME_START  0
2278 # define BLACKHOME_END   26
2279 # define MIDDLEROW_START 36
2280 # define MIDDLEROW_END   44
2281 # define WHITEHOME_START 54
2282 # define WHITEHOME_END   80
2283 #else
2284 # define BLACKHOME_START  0
2285 # define BLACKHOME_END    4
2286 # define MIDDLEROW_START 10
2287 # define MIDDLEROW_END   14
2288 # define WHITEHOME_START 19
2289 # define WHITEHOME_END   24
2290 #endif
2291         for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2292         {
2293             short n;
2294
2295             fv1 = fvalue[c1];
2296
2297             /* Score fifth rank */
2298             for (sq = MIDDLEROW_START, n = 0; sq <= MIDDLEROW_END; sq++)
2299             {
2300                 if ((color[sq] == c1) || (attack[c1][sq] != 0))
2301                     n++;
2302             }
2303
2304             if (n != 0)
2305             {
2306                 pscore[c1] += (ds = n * fv1[CNTRL5TH]);
2307             }
2308
2309             /* Score holes */
2310             for (sq = ((c1 == black) ? BLACKHOME_START : WHITEHOME_START), n = 0;
2311                  sq <= ((c1 == black) ? BLACKHOME_END : WHITEHOME_END);
2312                  sq++)
2313             {
2314                 if (board[sq] == no_piece && attack[c1][sq] == 0)
2315                     n++;
2316             }
2317
2318             if (n != 0)
2319             {
2320                 pscore[c1] += (ds = -n * fv1[HOLES]);
2321             }
2322
2323             if (hung[c1] > 1)
2324             {
2325                 pscore[c1] += (ds = -fv1[HUNGX]);
2326             }
2327
2328             /* Score opening features and
2329              * castle/attack pattern distances */
2330
2331             if (in_opening_stage)
2332             {
2333                 pscore[c1] += (ds = ScoreKingOpeningFeatures());
2334                 pscore[c1] += (ds = ScorePatternDistance(c1));
2335             }
2336         }
2337
2338         score = mtl[side] - mtl[xside]
2339             + pscore[side] - pscore[xside] + 10;
2340
2341 #ifdef CACHE
2342         if (use_etable && PUTVAR)
2343             PutInEETable(side, score);
2344 #endif
2345
2346         return score;
2347 #ifdef CACHE /* CHECKME: this looks bad */
2348     }
2349
2350     return s;
2351 #endif
2352 }
2353
2354
2355
2356 /*
2357  * Try to determine the game type of "side".
2358  */
2359
2360 #ifndef MINISHOGI
2361 inline static void
2362 GuessGameType(short side_to_move)
2363 {
2364     short side, gt;
2365     short StaticRook[2]  = { 0, 0 };
2366     short RangingRook[2] = { 0, 0 };
2367
2368     for (side = black; side <= white; side++)
2369     {
2370         /* computer should not change its mind */
2371
2372         extern int bookflag;
2373
2374         gt = GameType[side];
2375
2376         if (!bookflag && (side == side_to_move))
2377         {
2378             if (gt == STATIC_ROOK)
2379                 StaticRook[side] += 4;
2380             else if (gt == RANGING_ROOK)
2381                 RangingRook[side] += 4;
2382         }
2383
2384         /* static rook conditions */
2385
2386         if (on_column(side, rook, 7))
2387             StaticRook[side] += 3;
2388
2389         if (on_csquare(side, pawn, 34))
2390             StaticRook[side] += 6;
2391         else if (on_csquare(side, pawn, 43))
2392             StaticRook[side] += 4;
2393         else if (!on_column(side, pawn, 7))
2394             StaticRook[side] += 5;
2395
2396         if (empty_csquare(side, 5) || empty_csquare(side, 6))
2397             StaticRook[side] += 2;
2398
2399         if (on_left_side(side, king))
2400             StaticRook[side] += 2;
2401
2402         /* ranging rook conditions */
2403
2404         if (on_left_side(side, rook))
2405             RangingRook[side] += 5;
2406         else if (!on_column(side, rook, 7))
2407             RangingRook[side] += 3;
2408
2409         if (on_csquare(side, pawn, 25))
2410             RangingRook[side] += 1;
2411
2412         if (on_csquare(side, pawn, 30))
2413             RangingRook[side] += 1;
2414         else
2415             RangingRook[side] -= 2;
2416
2417         if (!on_right_side(side, rook))
2418             RangingRook[side] += 4;
2419
2420         if (on_right_side(side, king))
2421             RangingRook[side] += 4;
2422
2423         if (on_csquare(side, bishop, 20))
2424         {
2425             if (on_csquare(side, silver, 11)
2426                 || on_csquare(side, silver, 12)
2427                 || on_csquare(side, silver, 21))
2428             {
2429                 RangingRook[side] += 3;
2430             }
2431         }
2432
2433         if ((StaticRook[side] > 5) || (RangingRook[side] > 5))
2434         {
2435             GameType[side] = (StaticRook[side] > RangingRook[side])
2436                 ? STATIC_ROOK : RANGING_ROOK;
2437         }
2438         else
2439         {
2440             GameType[side] = UNKNOWN;
2441         }
2442     }
2443
2444     if ((GameType[black] == UNKNOWN) || (GameType[white] == UNKNOWN))
2445     {
2446         for (side = black; side <= white; side++)
2447         {
2448             if ((side == computer) && (GameType[side] == UNKNOWN))
2449             {
2450                 /*
2451                  * Game type is UNKNOWN.
2452                  * Make a decision what type of game to play.
2453                  * To make computer games more interesting, make a
2454                  * random decision.
2455                  */
2456
2457                 if (!on_csquare(side, pawn, 25))
2458                 {
2459                     /* Play static rook if rook pawn has been pushed! */
2460                     GameType[side] = STATIC_ROOK;
2461                 }
2462                 else
2463                 {
2464                     unsigned int random = urand() % 100;
2465                     short d = StaticRook[side] - RangingRook[side];
2466
2467                     switch (GameType[side ^ 1])
2468                     {
2469                     case STATIC_ROOK:
2470                         if (random < 35 + d)
2471                             GameType[side] = STATIC_ROOK;
2472                         else if (random < 95)
2473                             GameType[side] = RANGING_ROOK;
2474                         break;
2475
2476                     case RANGING_ROOK:
2477                         if (random < 75 + d)
2478                             GameType[side] = STATIC_ROOK;
2479                         else if (random < 95)
2480                             GameType[side] = RANGING_ROOK;
2481                         break;
2482
2483                     default:
2484                         if (random < 33 + d)
2485                             GameType[side] = STATIC_ROOK;
2486                         else if (random < 66)
2487                             GameType[side] = RANGING_ROOK;
2488                     }
2489                 }
2490             }
2491         }
2492     }
2493 }
2494 #endif
2495
2496
2497
2498
2499 static void
2500 DetermineGameType(short side_to_move)
2501 {
2502     short side;
2503
2504 #ifndef MINISHOGI
2505     /* FIXME: calculations below are wrong for minishogi, all done for 9x9 */
2506     GuessGameType(side_to_move);
2507 #else
2508     GameType[black] = UNKNOWN;
2509     GameType[white] = UNKNOWN;
2510 #endif
2511
2512     array_zero(Mpawn,   sizeof(Mpawn));
2513 #ifndef MINISHOGI
2514     array_zero(Mlance,  sizeof(Mlance));
2515     array_zero(Mknight, sizeof(Mknight));
2516 #endif
2517     array_zero(Msilver, sizeof(Msilver));
2518     array_zero(Mgold,   sizeof(Mgold));
2519     array_zero(Mbishop, sizeof(Mbishop));
2520     array_zero(Mrook,   sizeof(Mrook));
2521     array_zero(Mking,   sizeof(Mking));
2522
2523     if (in_opening_stage)
2524     {
2525         for (side = black; side <= white; side++)
2526             UpdatePatterns(side, GameCnt);
2527     }
2528     else
2529     {
2530         dsp->ShowPatternCount(black, -1);
2531         dsp->ShowPatternCount(white, -1);
2532     }
2533 }
2534
2535
2536
2537 /*
2538  * This is done one time before the search is started. Set up arrays
2539  * Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the SqValue()
2540  * function to determine the positional value of each piece.
2541  */
2542
2543 void
2544 ExaminePosition(short side)
2545 {
2546     short c1, piece, sq, i, bsq, wsq;
2547
2548     /* Build enemy king distance tables. */
2549
2550     for (sq = 0, bsq = BlackKing, wsq = WhiteKing; sq < NO_SQUARES; sq++)
2551     {
2552         Kdist[black][sq] = distance(sq, bsq);
2553         Kdist[white][sq] = distance(sq, wsq);
2554     }
2555
2556     threats(black);
2557     threats(white);
2558
2559     ExamineSquares();
2560
2561     DetermineGameType(side);
2562     DetermineStage(side);
2563
2564     UpdateWeights(side);
2565
2566     array_zero(HasPiece, sizeof(HasPiece));
2567
2568     for (c1 = black; c1 <= white; c1++)
2569     {
2570         for (i = PieceCnt[c1]; i >= 0; i--)
2571         {
2572             ++HasPiece[c1][piece = board[sq = PieceList[c1][i]]];
2573         }
2574     }
2575 }
2576
2577
2578
2579 /* FIXME: calculations below are wrong for minishogi, all done for 9x9 */
2580 void
2581 DetermineStage(short side)
2582 {
2583     short xside = side ^ 1, ds, db1, c1, c2, feature;
2584
2585     /* Determine initial stage */
2586
2587     balance[side] = balance[xside] = 50;
2588
2589     if ((GameType[side] == STATIC_ROOK)
2590         && (GameType[xside] == STATIC_ROOK))
2591     {
2592         if (GameCnt < 40)
2593             stage = 0;
2594         else if (GameCnt < 60)
2595             stage = 15;
2596         else if (GameCnt < 80)
2597             stage = 25;
2598         else
2599             stage = 30;
2600     }
2601     else if ((GameType[side] == RANGING_ROOK)
2602              || (GameType[xside] == RANGING_ROOK))
2603     {
2604         if (GameCnt < 30)
2605             stage = 0;
2606         else if (GameCnt < 50)
2607             stage = 15;
2608         else if (GameCnt < 70)
2609             stage = 25;
2610         else
2611             stage = 30;
2612     }
2613     else
2614     {
2615         if (GameCnt < 35)
2616             stage = 0;
2617         else if (GameCnt < 55)
2618             stage = 15;
2619         else if (GameCnt < 75)
2620             stage = 25;
2621         else
2622             stage = 30;
2623     }
2624
2625 #ifndef MINISHOGI
2626     /* Update stage depending on board features and attack balance value */
2627
2628     if (abs(ds = (mtl[side] - mtl[xside]))
2629         > (db1 = (*value)[stage][lance]))
2630     {
2631         db1 = abs(4 * ds / db1);
2632
2633         if (ds < 0)
2634             balance[side] += db1;
2635         else if (ds > 0)
2636             balance[xside] += db1;
2637
2638         stage += (ds = db1);
2639     }
2640 #endif
2641
2642     for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2643     {
2644         if ((ds = seed[c1]) > 2)
2645         {
2646             balance[c1] += (db1 = ds * 2);
2647             balance[c2] -= db1;
2648
2649             if (stage < 30)
2650                 stage = 30;
2651
2652             stage += ds;
2653         }
2654
2655         if ((db1 = hung[c1]) > 2)
2656         {
2657             balance[c1] -= (db1 *= 2);
2658             balance[c2] += db1;
2659         }
2660
2661         if ((db1 = loose[c1]) > 4)
2662         {
2663             balance[c1] -= (db1 /= 2);
2664             balance[c2] += db1;
2665             stage += (ds = 1);
2666         }
2667
2668         if ((ds = hole[c1]))
2669         {
2670             balance[c1] -= (db1 = ds);
2671             balance[c2] += db1;
2672             stage += (ds /= 2);
2673         }
2674
2675         if ((db1 = target[c1]) > 3)
2676         {
2677             balance[c1] += (db1 /= 3);
2678             balance[c2] -= db1;
2679             stage += (ds = db1 / 4);
2680         }
2681
2682         stage += (ds = captured[c1] / 2);
2683
2684         if ((db1 = captured[c1]) > 4)
2685         {
2686             balance[c1] += (db1 /= 2);
2687             stage += (ds = 3);
2688         }
2689
2690         if ((db1 = dcaptured[c1]) > 3)
2691         {
2692             balance[c1] += db1;
2693             stage += (ds = 3);
2694         }
2695
2696         if (balance[c1] > 99)
2697             balance[c1] = 99;
2698         else if (balance[c1] < 0)
2699             balance[c1] = 0;
2700     }
2701
2702     if (stage > 99)
2703         stage = 99;
2704     else if (stage < 0)
2705         stage = 0;
2706
2707     if (flag.post)
2708         dsp->ShowStage();
2709
2710     /* Determine stage dependant weights */
2711
2712     ADVNCM[pawn]   = 1; /* advanced pawn bonus increment   */
2713 #ifndef MINISHOGI
2714     ADVNCM[lance]  = 1;
2715     ADVNCM[knight] = 1;
2716 #endif
2717     ADVNCM[silver] = 1; /* advanced silver bonus increment */
2718     ADVNCM[gold]   = 1; /* advanced gold bonus increment   */
2719     ADVNCM[bishop] = 1;
2720     ADVNCM[rook]   = 1;
2721     ADVNCM[king]   = 1; /* advanced king bonus increment   */
2722
2723     MAXCDIST = (stage < 33) ? (33 - stage) / 4 : 0;
2724     MAXADIST = (stage < 30) ? (30 - stage) / 4 : 0;
2725
2726     for (c1 = black; c1 <= white; c1++)
2727     {
2728         for (feature = 0; feature < NO_FEATURES; feature++)
2729         {
2730             fvalue[c1][feature] =
2731                 ((((*fscore)[stage][feature][0] * (99 - balance[c1])) + 50)
2732                  / 100)
2733                 + ((((*fscore)[stage][feature][1] * balance[c1]) + 50)
2734                    / 100);
2735         }
2736     }
2737 }
2738
2739
2740
2741 void
2742 UpdateWeights(short stage)
2743 {
2744 }
2745
2746
2747
2748 /*
2749  * Compute stage dependent relative material values assuming
2750  * linearity between the main stages:
2751  *
2752  *   minstage < stage < maxstage =>
2753  *          stage - minstage        value - minvalue
2754  *         -------------------  =  -------------------
2755  *         maxstage - minstage     maxvalue - minvalue
2756  */
2757
2758
2759 static short
2760 linear_piece_value(short piece, short stage, short i, short j)
2761 {
2762     short minvalue, maxvalue, minstage, maxstage;
2763
2764     minstage = ispvalue[0][i];
2765     maxstage = ispvalue[0][j];
2766     minvalue = ispvalue[piece][i];
2767     maxvalue = ispvalue[piece][j];
2768
2769     /* FIXME: set a variable then return it, puh-leeze! */
2770     return ((stage - minstage) * (maxvalue - minvalue)
2771             / (maxstage - minstage)) + minvalue;
2772 }
2773
2774
2775
2776 static short
2777 linear_feature_value(short feature, short stage, short i, short j)
2778 {
2779     short minvalue, maxvalue, minstage, maxstage;
2780
2781     minstage = ispvalue[0][i];
2782     maxstage = ispvalue[0][j];
2783     minvalue = weight[feature][i];
2784     maxvalue = weight[feature][j];
2785
2786     /* FIXME: set a variable then return it, puh-leeze! */
2787     return ((stage - minstage) * (maxvalue - minvalue)
2788             / (maxstage - minstage)) + minvalue;
2789 }
2790
2791
2792 /*
2793  * matweight = percentage_of_max_value * max_value(stage) / 100
2794  * max_value(0) = MAX_VALUE; max_value(100) = MIN_VALUE
2795  *  => max_value(stage) = a * stage + b; b = MAX_VALUE,
2796  *     a = (MIN_VALUE - MAX_VALUE)/100
2797  */
2798
2799 #define MIN_VALUE 300
2800 #define MAX_VALUE 1000
2801
2802 #define max_value(stage) \
2803 ((long)(MIN_VALUE - MAX_VALUE) * stage + (long)100 * MAX_VALUE)
2804 #define matweight(value, stage) \
2805 ((long)max_value(stage) * value / 10000)
2806
2807
2808
2809 void
2810 Initialize_eval(void)
2811 {
2812     short stage, piece, feature, i;
2813
2814     for (stage = 0; stage < NO_STAGES; stage++)
2815     {
2816         for (i = 0; i < MAIN_STAGES; i++)
2817         {
2818             if (stage == ispvalue[0][i])
2819             {
2820                 for (piece = 0; piece < NO_PIECES; piece++)
2821                 {
2822                     (*value)[stage][piece] =
2823                         matweight(ispvalue[piece][i], stage);
2824                 }
2825
2826                 for (feature = 0; feature < NO_FEATURES; feature++)
2827                 {
2828                     (*fscore)[stage][feature][0] =
2829                         (weight[feature][i]
2830                          * weight[feature][MAIN_STAGES] + 50) / 100;
2831
2832                     (*fscore)[stage][feature][1] =
2833                         (weight[feature][i]
2834                          * weight[feature][MAIN_STAGES + 1] + 50) / 100;
2835                 }
2836
2837                 break;
2838             }
2839
2840             if (stage < ispvalue[0][i + 1])
2841             {
2842                 for (piece = 0; piece < NO_PIECES; piece++)
2843                 {
2844                     (*value)[stage][piece] =
2845                         matweight(
2846                             linear_piece_value(piece, stage, i, i + 1),
2847                             stage);
2848                 }
2849
2850                 for (feature = 0; feature < NO_FEATURES; feature++)
2851                 {
2852                     (*fscore)[stage][feature][0] =
2853                         (linear_feature_value(feature, stage, i, i + 1)
2854                          * weight[feature][MAIN_STAGES] + 50) / 100;
2855
2856                     (*fscore)[stage][feature][1] =
2857                         (linear_feature_value(feature, stage, i, i + 1)
2858                          * weight[feature][MAIN_STAGES + 1] + 50) / 100;
2859                 }
2860
2861                 break;
2862             }
2863         }
2864     }
2865 }
2866