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