4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * Copyright (c) 1988, 1989, 1990 John Stanback
11 * Copyright (c) 1992 Free Software Foundation
13 * This file is part of GNU SHOGI.
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.
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
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 * ----------------------------------------------------------------------
38 /* Hash table for preventing multiple scoring of the same position */
40 int EADD = 0; /* number of writes to the cache table */
41 int EGET = 0; /* number of hits to the cache table */
42 int PUTVAR = false; /* shall the current scoring be cached? */
45 /* Pieces and colors of initial board setup */
47 const small_short Stboard[NO_SQUARES] =
49 lance, knight, silver, gold, king, gold, silver, knight, lance,
50 0, bishop, 0, 0, 0, 0, 0, rook, 0,
51 pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
52 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
56 0, rook, 0, 0, 0, 0, 0, bishop, 0,
57 lance, knight, silver, gold, king, gold, silver, knight, lance
61 const small_short Stcolor[NO_SQUARES] =
63 black, black, black, black,
64 black, black, black, black, black,
65 neutral, black, neutral, neutral,
66 neutral, neutral, neutral, black, neutral,
67 black, black, black, black,
68 black, black, black, black, black,
69 neutral, neutral, neutral, neutral,
70 neutral, neutral, neutral, neutral, neutral,
71 neutral, neutral, neutral, neutral,
72 neutral, neutral, neutral, neutral, neutral,
73 neutral, neutral, neutral, neutral,
74 neutral, neutral, neutral, neutral, neutral,
75 white, white, white, white,
76 white, white, white, white, white,
77 neutral, white, neutral, neutral,
78 neutral, neutral, neutral, white, neutral,
79 white, white, white, white,
80 white, white, white, white, white
84 /* Actual pieces and colors */
86 small_short board[NO_SQUARES], color[NO_SQUARES];
89 /* relative piece values at the beginning of main stages */
93 static small_short ispvalue[NO_PIECES][MAIN_STAGES] =
95 { 0, 35, 70, 99 }, /* main stage borders */
96 /* ------------------------------------------ */
97 { 7, 7, 8, 10 }, /* Pawn */
98 { 20, 35, 45, 60 }, /* Lance */
99 { 20, 35, 45, 60 }, /* Knight */
100 { 35, 40, 60, 80 }, /* Silver */
101 { 35, 50, 65, 80 }, /* Gold */
102 { 90, 90, 90, 90 }, /* Bishop */
103 { 95, 95, 95, 95 }, /* Rook */
104 { 15, 25, 40, 65 }, /* promoted Pawn */
105 { 25, 45, 55, 65 }, /* promoted Lance */
106 { 25, 45, 55, 65 }, /* promoted Knight */
107 { 35, 55, 75, 75 }, /* promoted Silver */
108 { 99, 99, 99, 99 }, /* promoted Bishop */
109 { 97, 97, 99, 99 }, /* promoted Rook */
110 { 100, 100, 100, 100 }, /* King */
113 /* Features and Weights */
140 #define KNGPROTECT 25
148 static small_short weight[NO_FEATURES + 1][MAIN_STAGES + 2] =
150 { 80, 100, 100, 40, 10, 15 }, /* ATTACKED */
151 { 80, 100, 100, 50, 14, 10 }, /* HUNGP */
152 { 80, 100, 100, 50, 18, 12 }, /* HUNGX */
153 { 100, 50, 0, 0, 2, 1 }, /* CNTRL5TH */
154 { 100, 100, 60, 10, 4, 2 }, /* HOLES */
155 { 100, 50, 0, 0, 14, 7 }, /* PCASTLE */
156 { 100, 50, 0, 0, 6, 12 }, /* PATTACK */
157 { 10, 40, 70, 100, 10, 15 }, /* CTRLK */
158 { 100, 80, 50, 40, 2, 1 }, /* PROTECT */
159 { 40, 100, 40, 5, 4, 4 }, /* HCLSD */
160 { 80, 100, 80, 30, 10, 15 }, /* PINVAL */
161 { 80, 100, 60, 15, 6, 10 }, /* XRAY */
162 { 100, 50, 0, 0, 15, 15 }, /* OPENWRONG */
163 { 0, 40, 70, 100, 8, 12 }, /* SEED */
164 { 50, 100, 80, 20, 5, 3 }, /* LOOSE */
165 { 50, 100, 80, 50, 100, 100 }, /* MOBILITY (%) */
166 { 50, 100, 80, 50, 4, 8 }, /* TARGET */
167 { 50, 40, 100, 80, 8, 4 }, /* KSFTY */
168 { 80, 100, 60, 20, 5, 5 }, /* HOPN */
169 { 20, 40, 80, 100, 3, 6 }, /* PROMD */
170 { 20, 40, 80, 100, 4, 1 }, /* KINGOD */
171 { 5, 40, 100, 50, 0, 4 }, /* PWNDROP */
172 { 0, 20, 80, 100, 0, 4 }, /* DFFDROP */
173 { 20, 50, 100, 80, 0, 4 }, /* FCLATTACK */
174 { 0, 20, 80, 100, 0, 8 }, /* KNGATTACK */
175 { 40, 80, 100, 80, 6, 0 }, /* KNGPROTECT */
176 { 50, 100, 60, 10, 0, 8 }, /* DNGPC */
177 { 30, 100, 60, 5, 0, 6 }, /* LSATTACK */
178 { 0, 50, 80, 100, 0, 8 }, /* NIHATTACK */
179 { 50, 100, 80, 60, 8, 0 }, /* COHESION */
180 { 100, 100, 80, 60, 4, 4 }, /* OPPDROP */
184 short ADVNCM[NO_PIECES];
186 /* distance to enemy king */
187 static const short EnemyKingDistanceBonus[10] =
188 { 0, 6, 4, -1, -3, -4, -6, -8, -10, -12 };
190 /* distance to own king */
191 static const short OwnKingDistanceBonus[10] =
192 { 0, 5, 2, 1, 0, -1, -2, -3, -4, -5 };
194 /* distance to promotion zone */
195 static const int PromotionZoneDistanceBonus[NO_ROWS] =
196 { 0, 0, 0, 0, 2, 6, 6, 8, 8 };
198 #define MAX_BMBLTY 20
199 #define MAX_RMBLTY 20
202 /* Bishop mobility bonus indexed by # reachable squares */
203 static const short BMBLTY[MAX_BMBLTY] =
204 { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };
206 /* Rook mobility bonus indexed by # reachable squares */
207 static const short RMBLTY[MAX_RMBLTY] =
208 { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };
210 /* Lance mobility bonus indexed by # reachable squares */
211 static const short LMBLTY[MAX_LMBLTY] =
212 { 0, 0, 0, 0, 4, 6, 8, 10 };
214 static const short MBLTY[NO_PIECES] =
215 { 0, 2, 1, 10, 5, 5, 1, 1, 5, 5, 5, 5, 1, 1, 4 };
217 static const short KTHRT[36] =
218 { 0, -8, -20, -36, -52, -68, -80, -80, -80, -80, -80, -80,
219 -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80,
220 -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80 };
222 static small_short fvalue[2][NO_FEATURES];
224 long attack[2][NO_SQUARES]; /* threats to squares */
225 small_short sseed[NO_SQUARES]; /* square occupied by a seed piece? */
227 struct signature threats_signature[2] = /* statistics valid for position.. */
228 { { -1, -1 }, { -1, -1 } }; /* attack and sseed available */
230 small_short starget[2][NO_SQUARES]; /* significance as a target for a
231 * side of a square */
232 small_short sloose[NO_SQUARES]; /* square occupied by a loose piece? */
233 small_short shole[NO_SQUARES]; /* empty square a hole? */
234 small_short shung[NO_SQUARES]; /* hung piece? */
236 struct signature squares_signature = /* statistics valid for position ... */
237 { 0, 0 }; /* starget, sloose, shole, shung available */
239 short target[2], seed[2], loose[2], hole[2];
241 short captured[2]; /* number of captured pieces */
242 short dcaptured[2]; /* different types of captured pieces */
244 small_short Kdist[2][NO_SQUARES]; /* distance to king */
246 short MAXADIST, MAXCDIST; /* maximum half move distance to pattern */
248 char GameType[2] = { UNKNOWN, UNKNOWN }; /* chosen game type of each side */
251 static short attack_opening_sequence[2]; /* current castle patterns */
252 static short castle_opening_sequence[2]; /* current attack formations */
254 static small_short Mpawn [2][NO_SQUARES];
255 static small_short Msilver[2][NO_SQUARES];
256 static small_short Mgold [2][NO_SQUARES];
257 static small_short Mking [2][NO_SQUARES];
258 static small_short Mlance [2][NO_SQUARES];
259 static small_short Mknight[2][NO_SQUARES];
260 static small_short Mbishop[2][NO_SQUARES];
261 static small_short Mrook [2][NO_SQUARES];
263 static Mpiece_array Mpawn, Mlance, Mknight, Msilver, Mgold,
264 Mbishop, Mrook, Mking;
266 Mpiece_array *Mpiece[NO_PIECES] =
267 { NULL, &Mpawn, &Mlance, &Mknight, &Msilver, &Mgold, &Mbishop, &Mrook,
268 &Mgold, &Mgold, &Mgold, &Mgold, &Mbishop, &Mrook, &Mking };
272 static small_short *PC1, *PC2;
273 static small_short *fv1;
274 static long *atk1, *atk2;
277 #define csquare(side, sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
278 #define crow(side, sq) row(csquare(side, sq))
279 #define ccolumn(side, sq) column(csquare(side, sq))
283 on_csquare(short side, short piece, short square)
286 /* FIXME: un-obfuscate this! */
287 return ((board[sq = csquare(side, square)] == piece)
288 && (color[sq] == side));
293 on_column(short side, short piece, short c)
297 for (sq = c; sq < NO_SQUARES; sq += NO_COLS)
299 if (on_csquare(side, piece, sq))
307 #define empty_csquare(side, square) \
308 (board[csquare(side, square)] == no_piece)
311 on_left_side(short side, short piece)
315 for (c = 0; c < 4; c++)
317 if (on_column(side, piece, c))
326 on_right_side(short side, short piece)
330 for (c = 5; c < NO_COLS; c++)
332 if (on_column(side, piece, c))
340 short pscore[2]; /* piece score for each side */
346 * Fill array attack[side][] with info about attacks to a square. Bits
347 * 16-31 are set if the piece (king .. pawn) attacks the square. Bits 0-15
348 * contain a count of total attacks to the square. Fill array sseed[] with
349 * info about occupation by a seed piece.
361 unsigned char *ppos, *pdir;
363 short i, kd, piece, xside;
366 if (MatchSignature(threats_signature[side]))
368 /* data valid for current positional signature */
375 array_zero(a, NO_SQUARES * sizeof(a[0]));
377 PL = PieceList[side];
379 for (i = PieceCnt[side]; i >= 0; i--)
385 ptyp = ptype[side][piece];
388 u = first_direction(ptyp, &d, sq);
390 ppos = (*nextpos[ptyp])[sq];
391 pdir = (*nextdir[ptyp])[sq];
397 a[u] = ((a[u] + 1) | c);
399 if ((kd = Kdist[xside][u]) < 2)
406 u = ((color[u] == neutral)
407 ? next_position(ptyp, &d, sq, u)
408 : next_direction(ptyp, &d, sq));
410 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
416 /* data valid for current positional signature */
417 CopySignature(threats_signature[side]);
423 * Compute the board square with nunmap offset "id". If side == white,
424 * offset is negated. inunmap[sq] is the corresponding nunmap index isq.
425 * nunmap[isq + id] computes the board square. If negative, it is outside
430 add_target(short sq, short side, short id)
432 short isq, tsq, xside;
434 tsq = (side == black) ? nunmap[isq + id] : nunmap[isq - id];
438 target[xside = side^1]++;
440 if (attack[side][tsq])
441 starget[xside][tsq]++; /* protected target square */
443 starget[xside][tsq] += 2; /* unprotected target square */
450 * Target squares can be vertically ahead, diagonally ahead
451 * or diagonally behind.
455 CheckTargetPiece(short sq, short side)
459 case pawn: /* vertically ahead if unprotected */
460 if (!attack[side][sq])
461 add_target(sq, side, 11);
464 case king: /* diagonally and vertically ahead */
465 add_target(sq, side, 10);
466 add_target(sq, side, 11);
467 add_target(sq, side, 12);
470 case rook: /* diagonally ahead and behind */
471 add_target(sq, side, 10);
472 add_target(sq, side, 12);
473 add_target(sq, side, -10);
474 add_target(sq, side, -12);
477 case bishop: /* vertically ahead */
478 add_target(sq, side, 11);
481 case knight: /* vertically ahead if advanced */
482 /* FIXME: gotta love them magic numbers... */
483 if ((sq != 1) && (sq != 7) && (sq != 73) && (sq != 79))
484 add_target(sq, side, 11);
492 ScoreKingOpeningFeatures(void)
494 short s = 0, sq = OwnKing, ds;
496 if (GameType[c1] == STATIC_ROOK)
498 /* Penalty for king on right side or fifth file */
500 c = 4 - ccolumn(c1, sq);
502 if ((c < 0) || ((c == 0) && (sq != kingP[c1])))
503 s += (ds = -c - c - fv1[OPENWRONG]);
505 else if (GameType[c1] == RANGING_ROOK)
507 /* Penalty for king on left side or fifth file */
509 c = 4 - ccolumn(c1, sq);
511 if ((c > 0) || ((c == 0) && (sq != kingP[c1])))
513 s += (ds = -c - c - fv1[OPENWRONG]);
516 /* Penalty for king moved before rook switch */
519 if (on_csquare(c1, rook, 16))
521 s += (ds = -4 * fv1[OPENWRONG]);
526 /* Penalty for sitting king after rook switch */
527 if (!on_csquare(c1, rook, 16))
529 s += (ds = -2 * fv1[OPENWRONG]);
533 /* Penalty for defending general moved
534 * before king switch to right side */
535 if (ccolumn(c1, sq) < 6)
537 if (Mvboard[csquare(c1, 5)] || Mvboard[csquare(c1, 6)])
539 s += (ds = -2 * fv1[OPENWRONG]);
553 short sq, side, piece, n;
555 if (MatchSignature(squares_signature))
557 /* data valid for current positional signature */
561 array_zero(shole, sizeof(shole));
562 array_zero(sloose, sizeof(sloose));
563 array_zero(starget, sizeof(starget));
565 hole[0] = hole[1] = loose[0] = loose[1]
566 = target[0] = target[1] = 0;
568 for (sq = 0; sq < NO_SQUARES; sq++)
570 if ((side = color[sq]) == neutral)
574 if (!attack[white][sq])
580 else if (InBlackCamp(sq))
582 if (!attack[black][sq])
591 /* occupied by "side" piece */
592 if (!attack[side][sq])
598 CheckTargetPiece(sq, side);
602 for (side = black; side <= white; side++)
604 captured[side] = dcaptured[side] = 0;
606 for (piece = pawn; piece <= rook; piece++)
608 if ((n = Captured[side][piece]) != 0)
618 /* Data valid for current positional signature */
619 CopySignature(squares_signature);
624 /* ............ POSITIONAL EVALUATION ROUTINES ............ */
628 * mtl[side] - value of all material
629 * hung[side] - count of hung pieces
630 * Tscore[ply] - search tree score for ply ply
631 * Pscore[ply] - positional score for ply ply
632 * INCscore - bonus score or penalty for certain moves
633 * Sdepth - search goal depth
634 * xwndw - evaluation window about alpha/beta
635 * EWNDW - second evaluation window about alpha/beta
636 * ChkFlag[ply] - checking piece at level ply or 0 if no check
637 * TesujiFlag[ply] - 1 if tesuji move at level ply or 0 if no tesuji
638 * PC1[column] - # of my pawns in this column
639 * PC2[column] - # of opponents pawns in column
640 * PieceCnt[side] - just what it says
647 * Compute an estimate of the score by adding the positional score from the
648 * previous ply to the material difference. If this score falls inside a
649 * window which is 180 points wider than the alpha-beta window (or within a
650 * 50 point window during quiescence search) call ScorePosition() to
651 * determine a score, otherwise return the estimated score. "side" is the
652 * side which has to move.
661 short *InChk, /* output Check flag */
662 short *blockable) /* king threat blockable */
668 s = -Pscore[ply - 1] + mtl[side] - mtl[xside] /* - INCscore */;
669 hung[black] = hung[white] = 0;
671 /* should we use the estimete or score the position */
674 || (ply > Sdepth && s >= (alpha - 30) && s <= (beta + 30))
676 || (use_etable && CheckEETable(side))
682 /* score the position */
683 array_zero(sseed, sizeof(sseed));
685 seed[0] = seed[1] = 0;
688 if (Anyattack(side, sq = PieceList[xside][0]) && (board[sq] == king))
690 *InChk = (board[sq = PieceList[side][0]] == king)
691 ? SqAttacked(sq, xside, blockable)
694 return ((SCORE_LIMIT + 1001) - ply);
698 *InChk = (board[sq = PieceList[side][0]] == king)
699 ? Anyattack(xside, sq)
708 s = ScorePosition(side);
713 /* use the estimate but look at check */
716 *InChk = (board[sq = PieceList[side][0]] == king)
717 ? SqAttacked(sq, xside, blockable)
720 if ((board[sq = PieceList[xside][0]] == king)
721 && SqAttacked(sq, side, blockable))
723 return ((SCORE_LIMIT + 1001) - ply);
727 Pscore[ply] = s - mtl[side] + mtl[xside];
728 ChkFlag[ply - 1] = ((*InChk) ? Pindex[TOsquare] : 0);
736 value_of_weakest_attacker(long a2)
740 min_value = SCORE_LIMIT;
742 for (piece = pawn; piece <= king; piece++)
744 if (control[piece] & a2)
746 if (min_value > (v = (*value)[stage][piece]))
758 * Find (promoted) Bishop, (promoted) Rook, and Lance mobility, x-ray
759 * attacks, and pins. Let BRL be the bishop, rook, or lance. Let P be the
760 * first piece (not a king or a pawn) in a direction and let Q be the
761 * second piece in the same direction. If Q is an unprotected opponent's
762 * piece with bigger relative value than BRL, there is a pin if P is an
763 * opponent's piece and there is an x-ray attack if P belongs to this side.
764 * Increment the hung[] array if a pin is found.
768 BRLscan(short sq, short *mob)
773 unsigned char *ppos, *pdir;
777 short u, xu, pin, ptyp, csq = column(sq);
778 short piece, upiece, xupiece, rvalue, ds;
779 small_short *Kd = Kdist[c2];
784 rvalue = (*value)[stage][piece];
785 ptyp = ptype[c1][upiece = unpromoted[piece]];
786 rvalue = (*value)[stage][upiece];
789 u = first_direction(ptyp, &d, sq);
791 ppos = (*nextpos[ptyp])[sq];
792 pdir = (*nextdir[ptyp])[sq];
796 pin = -1; /* start new direction */
802 s += (ds = fv1[CTRLK] * (2 - Kd[u]));
805 if ((ds = starget[c1][u]) != 0)
807 /* threatening a target square */
808 if ((pin < 0) /* direct threat */
809 || (color[pin] == c2)) /* pin threat */
811 s += (ds *= fv1[TARGET]);
815 if ((ds = shole[u]) != 0)
817 /* attacking or protecting a hole */
818 s += (ds = fv1[HOLES]);
820 else if (InPromotionZone(c1, u))
822 /* attacking a square in promotion zone */
823 s += (ds = fv1[HOLES] / 2);
826 if (color[u] == neutral)
830 xu = next_position(ptyp, &d, sq, u);
832 if (xu == next_direction(ptyp, &dd, sq))
833 pin = -1; /* oops new direction */
836 if ((xu = ppos[u]) == pdir[u])
837 pin = -1; /* oops new direction */
844 /* there is a piece in current direction */
847 /* it's the first piece in the current direction */
850 /* own intercepting piece in x-ray attack */
854 if (board[u] == pawn)
856 s += (ds = 2*fv1[PROTECT]);
858 else if (in_opening_stage)
860 s += (ds = -2*fv1[PROTECT]);
865 /* bishop or rook x-ray */
866 if ((upiece == bishop) && (board[u] == pawn)
867 && (GameType[c1] == STATIC_ROOK))
869 s += (ds = -2*fv1[HCLSD]);
871 else if ((upiece == rook) && (board[u] == lance)
872 && (GameType[c1] == STATIC_ROOK)
873 && (column(u) == csq))
875 s += (ds = fv1[XRAY]);
881 /* enemy's intercepting piece in pin attack */
884 /* lance pin attack */
885 if (board[u] == pawn)
887 s += (ds = -2*fv1[PROTECT]);
889 else if (in_opening_stage)
891 s += (ds = 2 * fv1[PROTECT]);
896 /* bishop or rook pin attack */
897 if (board[u] == pawn)
899 s += (ds = -fv1[HCLSD]);
906 xu = next_position(ptyp, &d, sq, u);
908 if (xu != next_direction(ptyp, &dd, sq))
909 pin = u; /* not on the edge and on to find a pin */
911 if ((xu = ppos[u]) != pdir[u])
912 pin = u; /* not on the edge and on to find a pin */
918 /* it's the second piece in the current direction */
922 /* second piece is an own piece */
923 if ((upiece == bishop) && (board[u] == pawn)
924 && (GameType[c1] == STATIC_ROOK))
926 s += (ds = -fv1[HCLSD]);
931 /* second piece is an enemy piece */
932 if (upiece == bishop && board[u] == pawn)
934 s += (ds = -fv1[HCLSD]/2);
937 if (((*value)[stage][xupiece = unpromoted[board[u]]]
941 if (color[pin] == c2)
943 if ((xupiece == king) && (in_endgame_stage))
945 s += (ds = 2 * fv1[PINVAL]);
949 s += (ds = fv1[PINVAL]);
953 || (atk1[pin] > control[board[pin]] + 1))
963 s += (ds = fv1[XRAY] / 2);
967 s += (ds = fv1[XRAY]);
973 pin = -1; /* new direction */
976 u = next_direction(ptyp, &d, sq);
991 #define ctlSG (ctlS | ctlG | ctlPp | ctlLp | ctlNp | ctlSp)
996 * Assign penalties if king can be threatened by checks, if squares near
997 * the king are controlled by the enemy (especially by promoted pieces), or
998 * if there are no own generals near the king.
1000 * The following must be true:
1001 * board[sq] == king, c1 == color[sq], c2 == otherside[c1]
1004 #define SCORETHREAT \
1006 if (color[u] != c2) \
1008 if ((atk1[u] == 0) || ((atk2[u] & CNT_MASK) != 0)) \
1014 s += (ds = -fv1[CTRLK]); \
1030 unsigned char *ppos, *pdir;
1037 /* Penalties, if a king can be threatened by checks. */
1045 for (p = pawn; p < king; p++)
1047 if (HasPiece[c2][p] || Captured[c2][p])
1051 /* if a c1 piece can reach u from sq,
1052 * then a c2 piece can reach sq from u.
1053 * That means, each u is a square, from which a
1054 * piece of type p and color c2 threats square sq.
1056 ptyp = ptype[c1][p];
1059 u = first_direction(ptyp, &d, sq);
1061 ppos = (*nextpos[ptyp])[sq];
1062 pdir = (*nextdir[ptyp])[sq];
1068 /* If a p piece can reach (controls or can drop to)
1069 * square u, then score threat.
1072 if (atk2[u] & control[p])
1074 else if ((Captured[c2][p] && color[u]) == neutral)
1078 u = ((color[u] == neutral)
1079 ? next_position(ptyp, &d, sq, u)
1080 : next_direction(ptyp, &d, sq));
1082 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1090 s += (ds = fv1[KSFTY] * KTHRT[cnt] / 16);
1092 /* Penalties, if squares near king are controlled by enemy. */
1096 ptyp = ptype[c1][king];
1099 u = first_direction(ptyp, &d, sq);
1101 pdir = (*nextpos[ptyp])[sq];
1107 if (!ok && color[u] == c1)
1109 short ptype_piece = ptype[black][board[u]];
1111 if ((ptype_piece == ptype_silver) || (ptype_piece == ptype_gold))
1115 if (atk2[u] > atk1[u])
1119 if (atk2[u] & ctlSG)
1121 s += (ds = -fv1[KSFTY] / 2);
1126 u = next_direction(ptyp, &d, sq);
1133 if (!ok || (cnt > 1))
1136 s += (ds = -fv1[KSFTY]/2);
1138 s += (ds = -fv1[KSFTY]);
1145 static short checked_trapped;
1149 * See if the attacked piece has unattacked squares to move to. The following
1150 * must be true: c1 == color[sq] c2 == otherside[c1]
1160 unsigned char *ppos, *pdir;
1166 rvalue = (*value)[stage][piece];
1167 ptyp = ptype[c1][piece];
1170 u = first_direction(ptyp, &d, sq);
1172 ppos = (*nextpos[ptyp])[sq];
1173 pdir = (*nextdir[ptyp])[sq];
1181 if ((atk2[u] == 0) || ((*value)[stage][board[u]] >= rvalue))
1186 u = ((color[u] == neutral)
1187 ? next_position(ptyp, &d, sq, u)
1188 : next_direction(ptyp, &d, sq));
1190 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1195 checked_trapped = true;
1203 AttackedPieceValue(short sq, short side)
1209 ds = -fv1[HUNGP] * 2;
1225 OpenFileValue(short sq, short hopn, short hopnx)
1229 if (PC1[fyle = column(sq)] == 0)
1245 /* Distance bonus */
1247 #define PromotionZoneDistanceValue(sq, dd) \
1248 if ((ds = fv1[PROMD])) \
1250 s += (ds = ds * PromotionZoneDistanceBonus[crow(c1, sq)] * dd); \
1253 #define OwnKingDistanceValue(sq, dd, maxd) \
1254 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c1][sq]) <= maxd)) \
1256 s += (ds = ds * OwnKingDistanceBonus[ad] * dd); \
1259 #define EnemyKingDistanceValue(sq, dd, maxd) \
1260 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c2][sq]) <= maxd)) \
1262 s += (ds = ds * EnemyKingDistanceBonus[ad] * dd); \
1270 * Calculate the positional value for a pawn on 'sq'.
1274 PawnValue(short sq, short side)
1278 short ccol = ccolumn(c1, sq);
1280 PromotionZoneDistanceValue(sq, 3);
1283 if (color[(c1 == black) ? (sq + NO_COLS) : (sq - NO_COLS)] == neutral)
1285 s += (ds = MBLTY[pawn]);
1288 if ((a1 & ((ctlR | ctlRp) | ctlL)))
1290 s += (ds = fv1[ATTACKED]);
1293 if (in_opening_stage)
1295 if (crow(c1, sq) == 2) /* pawn on 3d rank */
1297 if (board[(c1 == black) ?
1298 (sq + 3*NO_COLS) : (sq - 3*NO_COLS)] == pawn)
1300 /* opposing pawn has been moved (even column == (sq & 1)) */
1308 m = (side == c1) ? 3 : 5;
1312 m = (side == c1) ? 2 : 3;
1316 m = (side == c1) ? 1 : 2;
1320 s += (ds = -m * MBLTY[pawn]);
1324 if ((GameType[c1] == STATIC_ROOK) && (sq == csquare(c1, 43)))
1326 if ((atk2[csquare(c1, 52)] & CNT_MASK) < 2)
1328 s += (ds = fv1[ATTACKED]);
1332 if ((GameType[c2] == STATIC_ROOK) && (ccol == 1))
1334 if (sq == csquare(c1, 28))
1336 s += (ds = -fv1[ATTACKED]);
1339 if (((atk1[csquare(c1, 19)] & CNT_MASK) < 2)
1340 && ((atk1[csquare(c1, 28)] & CNT_MASK) < 2))
1342 s += (ds = -2 * fv1[ATTACKED]);
1353 * Calculate the positional value for a lance on 'sq'.
1357 LanceValue(short sq, short side)
1359 short s = 0, ds, ad;
1361 OwnKingDistanceValue(sq, 1, 2);
1363 OpenFileValue(sq, -fv1[HOPN], fv1[HOPN]);
1365 if (!checked_trapped && (crow(c1, sq) > 2))
1367 if (in_opening_stage || trapped(sq))
1369 s += (ds = -3 * fv1[ATTACKED]);
1373 s += (ds = -2 * fv1[ATTACKED]);
1383 * Calculate the positional value for a knight on 'sq'.
1387 KnightValue(short sq, short side)
1390 short ds, checked_trapped = false;
1391 short c = column(sq);
1393 PromotionZoneDistanceValue(sq, 1);
1394 OwnKingDistanceValue(sq, 1, 2);
1396 if (!checked_trapped && (crow(c1, sq) > 2))
1400 s += (ds = -4 * fv1[ATTACKED]);
1404 s += (ds = -3 * fv1[ATTACKED]);
1408 if ((c == 0) || (c == 8))
1410 s += (ds = -fv1[ATTACKED]);
1419 * Calculate the positional value for a silver on 'sq'.
1423 SilverValue(short sq, short side)
1427 OwnKingDistanceValue(sq, 2, 3);
1429 if ((Kdist[c1][sq] < 3)
1430 && (atk1[sq] & (control[gold] | control[silver])))
1432 s += (ds = fv1[COHESION]);
1435 if (in_opening_stage)
1437 if (GameType[c1] == STATIC_ROOK)
1439 if (csquare(c1, sq) == 12)
1443 if ((board[csq = csquare(c1, 20)] == bishop)
1444 && (color[csq] == c1))
1446 s += (ds = -2 * fv1[OPENWRONG]);
1453 EnemyKingDistanceValue(sq, 2, 3);
1462 * Calculate the positional value for a gold on 'sq'.
1466 GoldValue(short sq, short side)
1468 short s = 0, ds, ad;
1470 OwnKingDistanceValue(sq, 2, 3);
1472 if ((Kdist[c1][sq] < 3)
1473 && (atk1[sq] & (control[gold] | control[silver])))
1475 s += (ds = fv1[COHESION]);
1478 if (in_opening_stage)
1480 if ((GameType[c1] == STATIC_ROOK) && (GameType[c2] != STATIC_ROOK))
1482 if (Mvboard[csquare(c1, 3)])
1484 s += (ds = -2 * fv1[OPENWRONG]);
1490 EnemyKingDistanceValue(sq, 2, 3);
1499 * Calculate the positional value for a bishop on 'sq'.
1503 BishopValue(short sq, short side)
1505 short s = 0, ds, ad;
1507 if (in_opening_stage)
1509 if (GameType[c1] == RANGING_ROOK)
1511 /* Bishops diagonal should not be open */
1512 if (!on_csquare(c1, pawn, 30))
1514 s += (ds = -fv1[OPENWRONG]);
1517 else if (GameType[c2] == RANGING_ROOK)
1519 /* Bishops diagonal should be open */
1520 if ((csquare(c1, sq) == 10)
1521 && (!empty_csquare(c1, 20) || !empty_csquare(c1, 30)))
1523 s += (ds = -fv1[OPENWRONG]);
1525 else if ((csquare(c1, sq) == 20) && !empty_csquare(c1, 30))
1527 s += (ds = -fv1[OPENWRONG]);
1533 EnemyKingDistanceValue(sq, 1, 3);
1542 * Calculate the positional value for a rook on 'sq'.
1546 RookValue(short sq, short side)
1548 short s = 0, ds, ad;
1550 OpenFileValue(sq, 2 * fv1[HOPN], 4*fv1[HOPN]);
1552 if (in_opening_stage)
1554 short WRONG = fv1[OPENWRONG], OPOK = WRONG / 3;
1556 if (GameType[c1] == STATIC_ROOK)
1558 short c = ccolumn(c1, sq);
1560 /* Bonus for rook on 8th file */
1567 * Bonus for rook on right side,
1568 * penalty for rook on left side
1576 s += (ds = c + c + OPOK);
1580 s += (ds = -c - c - WRONG);
1583 else if (GameType[c1] == RANGING_ROOK)
1585 /* Bonus for rook on left side and
1586 * bishops diagonal closed, penalty otherwise. */
1589 c = 4 - ccolumn(c1, sq);
1594 /* Bishops diagonal should not be open. */
1595 if (on_csquare(c1, pawn, 30))
1598 s += (ds = -c - c - WRONG);
1602 s += (ds = -c - c - WRONG);
1604 /* Penalty for king not on initial square. */
1605 if (!on_csquare(side, king, 4))
1615 EnemyKingDistanceValue(sq, 1, 3);
1624 * Calculate the positional value for a promoted pawn on 'sq'.
1628 PPawnValue(short sq, short side)
1630 short s = 0, ds, ad;
1632 EnemyKingDistanceValue(sq, 3, 10);
1640 * Calculate the positional value for a promoted lance on 'sq'.
1644 PLanceValue(short sq, short side)
1646 short s = 0, ds, ad;
1648 EnemyKingDistanceValue(sq, 3, 10);
1656 * Calculate the positional value for a promoted knight on 'sq'.
1660 PKnightValue(short sq, short side)
1662 short s = 0, ds, ad;
1664 EnemyKingDistanceValue(sq, 3, 10);
1672 * Calculate the positional value for a promoted silver on 'sq'.
1676 PSilverValue(short sq, short side)
1678 short s = 0, ds, ad;
1680 EnemyKingDistanceValue(sq, 3, 10);
1688 * Calculate the positional value for a promoted bishop on 'sq'.
1692 PBishopValue(short sq, short side)
1694 short s = 0, ds, ad;
1696 EnemyKingDistanceValue(sq, 3, 4);
1704 * Calculate the positional value for a promoted rook on 'sq'.
1708 PRookValue(short sq, short side)
1710 short s = 0, ds, ad;
1712 EnemyKingDistanceValue(sq, 3, 4);
1714 OpenFileValue(sq, 3 * fv1[HOPN], 2 * fv1[HOPN]);
1722 * Calculate the positional value for a king on 'sq'.
1726 KingValue(short sq, short side)
1730 if (fv1[KSFTY] != 0)
1733 if (in_opening_stage)
1735 if ((GameType[c1] != UNKNOWN) && (ccolumn(c1, sq) == 4))
1737 s += (ds = -fv1[OPENWRONG] / 3);
1739 else if ((GameType[c1] == STATIC_ROOK) && on_right_side(c1, sq))
1741 s += (ds = -fv1[OPENWRONG] / 2);
1743 else if ((GameType[c1] == RANGING_ROOK) && on_left_side(c1, sq))
1745 s += (ds = -fv1[OPENWRONG] / 2);
1749 /* CHECKME: is this correct? */
1750 if ((ds = fv1[HOPN]))
1752 s += OpenFileValue(sq, -2 * ds, -4 * ds);
1761 * Calculate the positional value for a piece on 'sq'.
1765 PieceValue(short sq, short side)
1772 if (piece == no_piece)
1775 s = (*Mpiece[piece])[c1][sq];
1777 checked_trapped = false;
1781 /* pin/x-ray attack and mobility for sweeping pieces */
1782 s += (ds = BRLscan(sq, &mob));
1784 if ((piece == bishop) || (piece == pbishop))
1785 s += (ds = BMBLTY[mob] * fv1[MOBILITY] / 100);
1786 else if ((piece == rook) || (piece == prook))
1787 s += (ds = RMBLTY[mob] * fv1[MOBILITY] / 100);
1789 s += (ds = LMBLTY[mob] * fv1[MOBILITY] / 100);
1793 /* mobility for non-sweeping pieces */
1801 /* opponent attacks piece */
1804 /* undefended piece */
1805 s += AttackedPieceValue(sq, side);
1809 /* defended piece */
1810 short attack_value = value_of_weakest_attacker(a2);
1811 short piece_value = (*value)[stage][piece];
1813 if (attack_value < piece_value)
1815 /* attacked by a weaker piece */
1816 s += AttackedPieceValue(sq, side) / 2;
1818 else if (abs(attack_value - piece_value) < 10)
1820 /* opponent has the option to exchange equal pieces */
1821 s += (ds = -fv1[ATTACKED]);
1831 /* piece is defended */
1832 s += (ds = (a1 & CNT_MASK) * fv1[PROTECT]);
1837 s += (ds = fv1[SEED]);
1842 s += (ds = -fv1[LOOSE]);
1845 if (starget[c1][sq])
1849 s -= (ds = -fv1[ATTACKED]/2);
1851 else if (piece == pawn)
1853 s += (ds = fv1[ATTACKED]);
1857 if (starget[c2][sq])
1861 s -= (ds = -fv1[ATTACKED] / 3);
1865 s += (ds = fv1[ATTACKED]);
1869 if (Kdist[c1][sq] == 1)
1871 s += (ds = fv1[KSFTY]);
1878 s += PawnValue(sq, side);
1882 s += LanceValue(sq, side);
1886 s += KnightValue(sq, side);
1890 s += SilverValue(sq, side);
1894 s += GoldValue(sq, side);
1898 s += BishopValue(sq, side);
1902 s += RookValue(sq, side);
1906 s += KingValue(sq, side);
1910 s += PPawnValue(sq, side);
1914 s += PLanceValue(sq, side);
1918 s += PKnightValue(sq, side);
1922 s += PSilverValue(sq, side);
1926 s += PBishopValue(sq, side);
1930 s += PRookValue(sq, side);
1940 * Score distance to pattern regarding the game type which side plays.
1944 ScorePatternDistance(short c1)
1947 small_short *fv1 = fvalue[c1];
1950 if ((MAXCDIST > 0) && (fv1[PCASTLE] != 0)
1951 && ((os = castle_opening_sequence[c1]) >= 0))
1953 ds = board_to_pattern_distance(c1, os, MAXCDIST, GameCnt);
1957 s += (ds *= fv1[PCASTLE]);
1961 if ((MAXADIST > 0) && (fv1[PATTACK] != 0)
1962 && ((os = attack_opening_sequence[c1]) >= 0))
1964 ds = board_to_pattern_distance(c1, os, MAXADIST, GameCnt);
1968 s += (ds *= fv1[PATTACK]);
1979 * Determine castle and attack pattern which should be reached next.
1980 * Only patterns are considered, which have not been reached yet.
1984 UpdatePatterns(short side, short GameCnt)
1987 short xside = side ^ 1;
1991 strcpy(s, "CASTLE_?_?");
1992 s[7] = GameType[side];
1993 s[9] = GameType[xside];
1994 castle_opening_sequence[side] = os
1995 = locate_opening_sequence(side, s, GameCnt);
1997 if (flag.post && (os != END_OF_SEQUENCES))
1999 for (j = 0; j < MAX_SEQUENCE; j++)
2001 for (k = OpeningSequence[os].first_pattern[j];
2002 k != END_OF_PATTERNS;
2003 k = Pattern[k].next_pattern)
2005 if (Pattern[k].distance[side] >= 0)
2011 if (os != END_OF_SEQUENCES)
2012 update_advance_bonus(side, os);
2014 strcpy(s, "ATTACK_?_?");
2015 s[7] = GameType[side];
2016 s[9] = GameType[xside];
2017 attack_opening_sequence[side] = os
2018 = locate_opening_sequence(side, s, GameCnt);
2020 if (flag.post && (os != END_OF_SEQUENCES))
2022 for (j = 0; j < MAX_SEQUENCE; j++)
2024 for (k = OpeningSequence[os].first_pattern[j];
2025 k != END_OF_PATTERNS;
2026 k = Pattern[k].next_pattern)
2028 if (Pattern[k].distance[side] >= 0)
2035 ShowPatternCount(side, n);
2037 if (os != END_OF_SEQUENCES)
2038 update_advance_bonus(side, os);
2046 short ds, col, n, m, piece;
2048 if ((n = Captured[c1][pawn]))
2052 for (col = 0; col < NO_COLS; col++)
2061 /* FIXME! another great obfuscated line... */
2062 pscore[c1] += (ds *= ((n > 2) ? 3 : n));
2067 for (piece = lance, n = 0; piece <= rook; piece++)
2069 if (Captured[c1][piece])
2073 pscore[c1] += (ds = m * fv1[DFFDROP]);
2076 for (piece = pawn, n = 0; piece <= rook; piece++)
2078 if (Captured[c1][piece])
2083 ds = BMBLTY[MAX_BMBLTY - 1];
2087 ds = RMBLTY[MAX_RMBLTY - 1];
2091 ds = LMBLTY[MAX_LMBLTY - 1];
2100 if (!Captured[c2][piece])
2101 n += relative_value[piece];
2107 pscore[c1] += (ds = -n * fv1[OPPDROP] / 2);
2116 * Perform normal static evaluation of board position. A score is generated
2117 * for each piece and these are summed to get a score for each side.
2121 ScorePosition(short side)
2130 UpdateWeights(side);
2132 hung[black] = hung[white] = pscore[black] = pscore[white] = 0;
2134 array_zero(shung, sizeof(shung));
2137 if (!(use_etable && ProbeEETable(side, &s)))
2140 for (c1 = black; c1 <= white; c1++)
2144 /* atk1 is array of attacks on squares by my side */
2147 /* atk2 is array of attacks on squares by other side */
2150 /* same for PC1 and PC2 */
2154 /* same for fv1 and fv2 */
2157 for (i = PieceCnt[c1]; i >= 0; i--)
2159 sq = PieceList[c1][i];
2161 #if defined SAVE_SVALUE
2162 pscore[c1] += PieceValue(sq, side);
2164 pscore[c1] += (svalue[sq] = PieceValue(sq, side));
2171 for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2177 /* Score fifth rank */
2178 for (sq = 36, n = 0; sq <= 44; sq++)
2180 if ((color[sq] == c1) || (attack[c1][sq] != 0))
2186 pscore[c1] += (ds = n * fv1[CNTRL5TH]);
2190 for (sq = ((c1 == black) ? 0 : 54), n = 0;
2191 sq <= ((c1 == black) ? 26 : 80);
2194 if (board[sq] == no_piece && attack[c1][sq] == 0)
2200 pscore[c1] += (ds = -n * fv1[HOLES]);
2205 pscore[c1] += (ds = -fv1[HUNGX]);
2208 /* Score opening features and
2209 * castle/attack pattern distances */
2211 if (in_opening_stage)
2213 pscore[c1] += (ds = ScoreKingOpeningFeatures());
2214 pscore[c1] += (ds = ScorePatternDistance(c1));
2218 score = mtl[side] - mtl[xside]
2219 + pscore[side] - pscore[xside] + 10;
2222 if (use_etable && PUTVAR)
2223 PutInEETable(side, score);
2227 #ifdef CACHE /* CHECKME: this looks bad */
2237 * Try to determine the game type of "side".
2241 GuessGameType(short side_to_move)
2244 short StaticRook[2] = { 0, 0 };
2245 short RangingRook[2] = { 0, 0 };
2247 for (side = black; side <= white; side++)
2249 /* computer should not change its mind */
2251 extern int bookflag;
2253 gt = GameType[side];
2255 if (!bookflag && (side == side_to_move))
2257 if (gt == STATIC_ROOK)
2258 StaticRook[side] += 4;
2259 else if (gt == RANGING_ROOK)
2260 RangingRook[side] += 4;
2263 /* static rook conditions */
2265 if (on_column(side, rook, 7))
2266 StaticRook[side] += 3;
2268 if (on_csquare(side, pawn, 34))
2269 StaticRook[side] += 6;
2270 else if (on_csquare(side, pawn, 43))
2271 StaticRook[side] += 4;
2272 else if (!on_column(side, pawn, 7))
2273 StaticRook[side] += 5;
2275 if (empty_csquare(side, 5) || empty_csquare(side, 6))
2276 StaticRook[side] += 2;
2278 if (on_left_side(side, king))
2279 StaticRook[side] += 2;
2281 /* ranging rook conditions */
2283 if (on_left_side(side, rook))
2284 RangingRook[side] += 5;
2285 else if (!on_column(side, rook, 7))
2286 RangingRook[side] += 3;
2288 if (on_csquare(side, pawn, 25))
2289 RangingRook[side] += 1;
2291 if (on_csquare(side, pawn, 30))
2292 RangingRook[side] += 1;
2294 RangingRook[side] -= 2;
2296 if (!on_right_side(side, rook))
2297 RangingRook[side] += 4;
2299 if (on_right_side(side, king))
2300 RangingRook[side] += 4;
2302 if (on_csquare(side, bishop, 20))
2304 if (on_csquare(side, silver, 11)
2305 || on_csquare(side, silver, 12)
2306 || on_csquare(side, silver, 21))
2308 RangingRook[side] += 3;
2312 if ((StaticRook[side] > 5) || (RangingRook[side] > 5))
2314 GameType[side] = (StaticRook[side] > RangingRook[side])
2315 ? STATIC_ROOK : RANGING_ROOK;
2319 GameType[side] = UNKNOWN;
2323 if ((GameType[black] == UNKNOWN) || (GameType[white] == UNKNOWN))
2325 for (side = black; side <= white; side++)
2327 if ((side == computer) && (GameType[side] == UNKNOWN))
2330 * Game type is UNKNOWN.
2331 * Make a decision what type of game to play.
2332 * To make computer games more interesting, make a
2336 if (!on_csquare(side, pawn, 25))
2338 /* Play static rook if rook pawn has been pushed! */
2339 GameType[side] = STATIC_ROOK;
2343 unsigned int random = urand() % 100;
2344 short d = StaticRook[side] - RangingRook[side];
2346 switch (GameType[side ^ 1])
2349 if (random < 35 + d)
2350 GameType[side] = STATIC_ROOK;
2351 else if (random < 95)
2352 GameType[side] = RANGING_ROOK;
2356 if (random < 75 + d)
2357 GameType[side] = STATIC_ROOK;
2358 else if (random < 95)
2359 GameType[side] = RANGING_ROOK;
2363 if (random < 33 + d)
2364 GameType[side] = STATIC_ROOK;
2365 else if (random < 66)
2366 GameType[side] = RANGING_ROOK;
2378 DetermineGameType(short side_to_move)
2382 GuessGameType(side_to_move);
2384 array_zero(Mpawn, sizeof(Mpawn));
2385 array_zero(Mlance, sizeof(Mlance));
2386 array_zero(Mknight, sizeof(Mknight));
2387 array_zero(Msilver, sizeof(Msilver));
2388 array_zero(Mgold, sizeof(Mgold));
2389 array_zero(Mbishop, sizeof(Mbishop));
2390 array_zero(Mrook, sizeof(Mrook));
2391 array_zero(Mking, sizeof(Mking));
2393 if (in_opening_stage)
2395 for (side = black; side <= white; side++)
2396 UpdatePatterns(side, GameCnt);
2400 ShowPatternCount(black, -1);
2401 ShowPatternCount(white, -1);
2408 * This is done one time before the search is started. Set up arrays
2409 * Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the SqValue()
2410 * function to determine the positional value of each piece.
2414 ExaminePosition(short side)
2416 short c1, piece, sq, i, bsq, wsq;
2418 /* Build enemy king distance tables. */
2420 for (sq = 0, bsq = BlackKing, wsq = WhiteKing; sq < NO_SQUARES; sq++)
2422 Kdist[black][sq] = distance(sq, bsq);
2423 Kdist[white][sq] = distance(sq, wsq);
2431 DetermineGameType(side);
2432 DetermineStage(side);
2434 UpdateWeights(side);
2436 array_zero(HasPiece, sizeof(HasPiece));
2438 for (c1 = black; c1 <= white; c1++)
2440 for (i = PieceCnt[c1]; i >= 0; i--)
2442 ++HasPiece[c1][piece = board[sq = PieceList[c1][i]]];
2450 DetermineStage(short side)
2452 short xside = side ^ 1, ds, db1, c1, c2, feature;
2454 /* Determine initial stage */
2456 balance[side] = balance[xside] = 50;
2458 if ((GameType[side] == STATIC_ROOK)
2459 && (GameType[xside] == STATIC_ROOK))
2463 else if (GameCnt < 60)
2465 else if (GameCnt < 80)
2470 else if ((GameType[side] == RANGING_ROOK)
2471 || (GameType[xside] == RANGING_ROOK))
2475 else if (GameCnt < 50)
2477 else if (GameCnt < 70)
2486 else if (GameCnt < 55)
2488 else if (GameCnt < 75)
2494 /* Update stage depending on board features and attack balance value */
2496 if (abs(ds = (mtl[side] - mtl[xside]))
2497 > (db1 = (*value)[stage][lance]))
2499 db1 = abs(4 * ds / db1);
2502 balance[side] += db1;
2504 balance[xside] += db1;
2506 stage += (ds = db1);
2509 for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2511 if ((ds = seed[c1]) > 2)
2513 balance[c1] += (db1 = ds * 2);
2522 if ((db1 = hung[c1]) > 2)
2524 balance[c1] -= (db1 *= 2);
2528 if ((db1 = loose[c1]) > 4)
2530 balance[c1] -= (db1 /= 2);
2535 if ((ds = hole[c1]))
2537 balance[c1] -= (db1 = ds);
2542 if ((db1 = target[c1]) > 3)
2544 balance[c1] += (db1 /= 3);
2546 stage += (ds = db1 / 4);
2549 stage += (ds = captured[c1] / 2);
2551 if ((db1 = captured[c1]) > 4)
2553 balance[c1] += (db1 /= 2);
2557 if ((db1 = dcaptured[c1]) > 3)
2563 if (balance[c1] > 99)
2565 else if (balance[c1] < 0)
2577 /* Determine stage dependant weights */
2579 ADVNCM[pawn] = 1; /* advanced pawn bonus increment */
2582 ADVNCM[silver] = 1; /* advanced silver bonus increment */
2583 ADVNCM[gold] = 1; /* advanced gold bonus increment */
2586 ADVNCM[king] = 1; /* advanced king bonus increment */
2588 MAXCDIST = (stage < 33) ? (33 - stage) / 4 : 0;
2589 MAXADIST = (stage < 30) ? (30 - stage) / 4 : 0;
2591 for (c1 = black; c1 <= white; c1++)
2593 for (feature = 0; feature < NO_FEATURES; feature++)
2595 fvalue[c1][feature] =
2596 ((((*fscore)[stage][feature][0] * (99 - balance[c1])) + 50)
2598 + ((((*fscore)[stage][feature][1] * balance[c1]) + 50)
2607 UpdateWeights(short stage)
2614 * Compute stage dependent relative material values assuming
2615 * linearity between the main stages:
2617 * minstage < stage < maxstage =>
2618 * stage - minstage value - minvalue
2619 * ------------------- = -------------------
2620 * maxstage - minstage maxvalue - minvalue
2625 linear_piece_value(short piece, short stage, short i, short j)
2627 short minvalue, maxvalue, minstage, maxstage;
2629 minstage = ispvalue[0][i];
2630 maxstage = ispvalue[0][j];
2631 minvalue = ispvalue[piece][i];
2632 maxvalue = ispvalue[piece][j];
2634 /* FIXME: set a variable then return it, puh-leeze! */
2635 return ((stage - minstage) * (maxvalue - minvalue)
2636 / (maxstage - minstage)) + minvalue;
2642 linear_feature_value(short feature, short stage, short i, short j)
2644 short minvalue, maxvalue, minstage, maxstage;
2646 minstage = ispvalue[0][i];
2647 maxstage = ispvalue[0][j];
2648 minvalue = weight[feature][i];
2649 maxvalue = weight[feature][j];
2651 /* FIXME: set a variable then return it, puh-leeze! */
2652 return ((stage - minstage) * (maxvalue - minvalue)
2653 / (maxstage - minstage)) + minvalue;
2658 * matweight = percentage_of_max_value * max_value(stage) / 100
2659 * max_value(0) = MAX_VALUE; max_value(100) = MIN_VALUE
2660 * => max_value(stage) = a * stage + b; b = MAX_VALUE,
2661 * a = (MIN_VALUE - MAX_VALUE)/100
2664 #define MIN_VALUE 300
2665 #define MAX_VALUE 1000
2667 #define max_value(stage) \
2668 ((long)(MIN_VALUE - MAX_VALUE) * stage + (long)100 * MAX_VALUE)
2669 #define matweight(value, stage) \
2670 ((long)max_value(stage) * value / 10000)
2675 Initialize_eval(void)
2677 short stage, piece, feature, i;
2679 for (stage = 0; stage < NO_STAGES; stage++)
2681 for (i = 0; i < MAIN_STAGES; i++)
2683 if (stage == ispvalue[0][i])
2685 for (piece = 0; piece < NO_PIECES; piece++)
2687 (*value)[stage][piece] =
2688 matweight(ispvalue[piece][i], stage);
2691 for (feature = 0; feature < NO_FEATURES; feature++)
2693 (*fscore)[stage][feature][0] =
2695 * weight[feature][MAIN_STAGES] + 50) / 100;
2697 (*fscore)[stage][feature][1] =
2699 * weight[feature][MAIN_STAGES + 1] + 50) / 100;
2705 if (stage < ispvalue[0][i + 1])
2707 for (piece = 0; piece < NO_PIECES; piece++)
2709 (*value)[stage][piece] =
2711 linear_piece_value(piece, stage, i, i + 1),
2715 for (feature = 0; feature < NO_FEATURES; feature++)
2717 (*fscore)[stage][feature][0] =
2718 (linear_feature_value(feature, stage, i, i + 1)
2719 * weight[feature][MAIN_STAGES] + 50) / 100;
2721 (*fscore)[stage][feature][1] =
2722 (linear_feature_value(feature, stage, i, i + 1)
2723 * weight[feature][MAIN_STAGES + 1] + 50) / 100;