4 * ----------------------------------------------------------------------
6 * Copyright (c) 2012 Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * This file is part of GNU SHOGI.
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.
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
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 * ----------------------------------------------------------------------
35 /* Hash table for preventing multiple scoring of the same position */
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? */
42 /* Pieces and colors of initial board setup */
44 const small_short Stboard[NO_SQUARES] =
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
58 const small_short Stcolor[NO_SQUARES] =
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
81 /* Actual pieces and colors */
83 small_short board[NO_SQUARES], color[NO_SQUARES];
86 /* relative piece values at the beginning of main stages */
90 static small_short ispvalue[NO_PIECES][MAIN_STAGES] =
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 */
110 /* Features and Weights */
137 #define KNGPROTECT 25
145 static small_short weight[NO_FEATURES + 1][MAIN_STAGES + 2] =
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 */
181 short ADVNCM[NO_PIECES];
183 /* distance to enemy king */
184 static const short EnemyKingDistanceBonus[10] =
185 { 0, 6, 4, -1, -3, -4, -6, -8, -10, -12 };
187 /* distance to own king */
188 static const short OwnKingDistanceBonus[10] =
189 { 0, 5, 2, 1, 0, -1, -2, -3, -4, -5 };
191 /* distance to promotion zone */
192 static const int PromotionZoneDistanceBonus[NO_ROWS] =
193 { 0, 0, 0, 0, 2, 6, 6, 8, 8 };
195 #define MAX_BMBLTY 20
196 #define MAX_RMBLTY 20
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 };
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 };
207 /* Lance mobility bonus indexed by # reachable squares */
208 static const short LMBLTY[MAX_LMBLTY] =
209 { 0, 0, 0, 0, 4, 6, 8, 10 };
211 static const short MBLTY[NO_PIECES] =
212 { 0, 2, 1, 10, 5, 5, 1, 1, 5, 5, 5, 5, 1, 1, 4 };
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 };
219 static small_short fvalue[2][NO_FEATURES];
221 long attack[2][NO_SQUARES]; /* threats to squares */
222 small_short sseed[NO_SQUARES]; /* square occupied by a seed piece? */
224 struct signature threats_signature[2] = /* statistics valid for position.. */
225 { { -1, -1 }, { -1, -1 } }; /* attack and sseed available */
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? */
233 struct signature squares_signature = /* statistics valid for position ... */
234 { 0, 0 }; /* starget, sloose, shole, shung available */
236 short target[2], seed[2], loose[2], hole[2];
238 short captured[2]; /* number of captured pieces */
239 short dcaptured[2]; /* different types of captured pieces */
241 small_short Kdist[2][NO_SQUARES]; /* distance to king */
243 short MAXADIST, MAXCDIST; /* maximum half move distance to pattern */
245 char GameType[2] = { UNKNOWN, UNKNOWN }; /* chosen game type of each side */
248 static short attack_opening_sequence[2]; /* current castle patterns */
249 static short castle_opening_sequence[2]; /* current attack formations */
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];
260 static Mpiece_array Mpawn, Mlance, Mknight, Msilver, Mgold,
261 Mbishop, Mrook, Mking;
263 Mpiece_array *Mpiece[NO_PIECES] =
264 { NULL, &Mpawn, &Mlance, &Mknight, &Msilver, &Mgold, &Mbishop, &Mrook,
265 &Mgold, &Mgold, &Mgold, &Mgold, &Mbishop, &Mrook, &Mking };
269 static small_short *PC1, *PC2;
270 static small_short *fv1;
271 static long *atk1, *atk2;
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))
280 on_csquare(short side, short piece, short square)
283 /* FIXME: un-obfuscate this! */
284 return ((board[sq = csquare(side, square)] == piece)
285 && (color[sq] == side));
290 on_column(short side, short piece, short c)
294 for (sq = c; sq < NO_SQUARES; sq += NO_COLS)
296 if (on_csquare(side, piece, sq))
304 #define empty_csquare(side, square) \
305 (board[csquare(side, square)] == no_piece)
308 on_left_side(short side, short piece)
312 for (c = 0; c < 4; c++)
314 if (on_column(side, piece, c))
323 on_right_side(short side, short piece)
327 for (c = 5; c < NO_COLS; c++)
329 if (on_column(side, piece, c))
337 short pscore[2]; /* piece score for each side */
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.
358 unsigned char *ppos, *pdir;
360 short i, kd, piece, xside;
363 if (MatchSignature(threats_signature[side]))
365 /* data valid for current positional signature */
372 array_zero(a, NO_SQUARES * sizeof(a[0]));
374 PL = PieceList[side];
376 for (i = PieceCnt[side]; i >= 0; i--)
382 ptyp = ptype[side][piece];
385 u = first_direction(ptyp, &d, sq);
387 ppos = (*nextpos[ptyp])[sq];
388 pdir = (*nextdir[ptyp])[sq];
394 a[u] = ((a[u] + 1) | c);
396 if ((kd = Kdist[xside][u]) < 2)
403 u = ((color[u] == neutral)
404 ? next_position(ptyp, &d, sq, u)
405 : next_direction(ptyp, &d, sq));
407 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
413 /* data valid for current positional signature */
414 CopySignature(threats_signature[side]);
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
427 add_target(short sq, short side, short id)
429 short isq, tsq, xside;
431 tsq = (side == black) ? nunmap[isq + id] : nunmap[isq - id];
435 target[xside = side^1]++;
437 if (attack[side][tsq])
438 starget[xside][tsq]++; /* protected target square */
440 starget[xside][tsq] += 2; /* unprotected target square */
447 * Target squares can be vertically ahead, diagonally ahead
448 * or diagonally behind.
452 CheckTargetPiece(short sq, short side)
456 case pawn: /* vertically ahead if unprotected */
457 if (!attack[side][sq])
458 add_target(sq, side, 11);
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);
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);
474 case bishop: /* vertically ahead */
475 add_target(sq, side, 11);
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);
489 ScoreKingOpeningFeatures(void)
491 short s = 0, sq = OwnKing, ds;
493 if (GameType[c1] == STATIC_ROOK)
495 /* Penalty for king on right side or fifth file */
497 c = 4 - ccolumn(c1, sq);
499 if ((c < 0) || ((c == 0) && (sq != kingP[c1])))
500 s += (ds = -c - c - fv1[OPENWRONG]);
502 else if (GameType[c1] == RANGING_ROOK)
504 /* Penalty for king on left side or fifth file */
506 c = 4 - ccolumn(c1, sq);
508 if ((c > 0) || ((c == 0) && (sq != kingP[c1])))
510 s += (ds = -c - c - fv1[OPENWRONG]);
513 /* Penalty for king moved before rook switch */
516 if (on_csquare(c1, rook, 16))
518 s += (ds = -4 * fv1[OPENWRONG]);
523 /* Penalty for sitting king after rook switch */
524 if (!on_csquare(c1, rook, 16))
526 s += (ds = -2 * fv1[OPENWRONG]);
530 /* Penalty for defending general moved
531 * before king switch to right side */
532 if (ccolumn(c1, sq) < 6)
534 if (Mvboard[csquare(c1, 5)] || Mvboard[csquare(c1, 6)])
536 s += (ds = -2 * fv1[OPENWRONG]);
550 short sq, side, piece, n;
552 if (MatchSignature(squares_signature))
554 /* data valid for current positional signature */
558 array_zero(shole, sizeof(shole));
559 array_zero(sloose, sizeof(sloose));
560 array_zero(starget, sizeof(starget));
562 hole[0] = hole[1] = loose[0] = loose[1]
563 = target[0] = target[1] = 0;
565 for (sq = 0; sq < NO_SQUARES; sq++)
567 if ((side = color[sq]) == neutral)
571 if (!attack[white][sq])
577 else if (InBlackCamp(sq))
579 if (!attack[black][sq])
588 /* occupied by "side" piece */
589 if (!attack[side][sq])
595 CheckTargetPiece(sq, side);
599 for (side = black; side <= white; side++)
601 captured[side] = dcaptured[side] = 0;
603 for (piece = pawn; piece <= rook; piece++)
605 if ((n = Captured[side][piece]) != 0)
615 /* Data valid for current positional signature */
616 CopySignature(squares_signature);
621 /* ............ POSITIONAL EVALUATION ROUTINES ............ */
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
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.
658 short *InChk, /* output Check flag */
659 short *blockable) /* king threat blockable */
665 s = -Pscore[ply - 1] + mtl[side] - mtl[xside] /* - INCscore */;
666 hung[black] = hung[white] = 0;
668 /* should we use the estimete or score the position */
671 || (ply > Sdepth && s >= (alpha - 30) && s <= (beta + 30))
673 || (use_etable && CheckEETable(side))
679 /* score the position */
680 array_zero(sseed, sizeof(sseed));
682 seed[0] = seed[1] = 0;
685 if (Anyattack(side, sq = PieceList[xside][0]) && (board[sq] == king))
687 *InChk = (board[sq = PieceList[side][0]] == king)
688 ? SqAttacked(sq, xside, blockable)
691 return ((SCORE_LIMIT + 1001) - ply);
695 *InChk = (board[sq = PieceList[side][0]] == king)
696 ? Anyattack(xside, sq)
705 s = ScorePosition(side);
710 /* use the estimate but look at check */
713 *InChk = (board[sq = PieceList[side][0]] == king)
714 ? SqAttacked(sq, xside, blockable)
717 if ((board[sq = PieceList[xside][0]] == king)
718 && SqAttacked(sq, side, blockable))
720 return ((SCORE_LIMIT + 1001) - ply);
724 Pscore[ply] = s - mtl[side] + mtl[xside];
725 ChkFlag[ply - 1] = ((*InChk) ? Pindex[TOsquare] : 0);
733 value_of_weakest_attacker(long a2)
737 min_value = SCORE_LIMIT;
739 for (piece = pawn; piece <= king; piece++)
741 if (control[piece] & a2)
743 if (min_value > (v = (*value)[stage][piece]))
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.
765 BRLscan(short sq, short *mob)
770 unsigned char *ppos, *pdir;
774 short u, xu, pin, ptyp, csq = column(sq);
775 short piece, upiece, xupiece, rvalue, ds;
776 small_short *Kd = Kdist[c2];
781 rvalue = (*value)[stage][piece];
782 ptyp = ptype[c1][upiece = unpromoted[piece]];
783 rvalue = (*value)[stage][upiece];
786 u = first_direction(ptyp, &d, sq);
788 ppos = (*nextpos[ptyp])[sq];
789 pdir = (*nextdir[ptyp])[sq];
793 pin = -1; /* start new direction */
799 s += (ds = fv1[CTRLK] * (2 - Kd[u]));
802 if ((ds = starget[c1][u]) != 0)
804 /* threatening a target square */
805 if ((pin < 0) /* direct threat */
806 || (color[pin] == c2)) /* pin threat */
808 s += (ds *= fv1[TARGET]);
812 if ((ds = shole[u]) != 0)
814 /* attacking or protecting a hole */
815 s += (ds = fv1[HOLES]);
817 else if (InPromotionZone(c1, u))
819 /* attacking a square in promotion zone */
820 s += (ds = fv1[HOLES] / 2);
823 if (color[u] == neutral)
827 xu = next_position(ptyp, &d, sq, u);
829 if (xu == next_direction(ptyp, &dd, sq))
830 pin = -1; /* oops new direction */
833 if ((xu = ppos[u]) == pdir[u])
834 pin = -1; /* oops new direction */
841 /* there is a piece in current direction */
844 /* it's the first piece in the current direction */
847 /* own intercepting piece in x-ray attack */
851 if (board[u] == pawn)
853 s += (ds = 2*fv1[PROTECT]);
855 else if (in_opening_stage)
857 s += (ds = -2*fv1[PROTECT]);
862 /* bishop or rook x-ray */
863 if ((upiece == bishop) && (board[u] == pawn)
864 && (GameType[c1] == STATIC_ROOK))
866 s += (ds = -2*fv1[HCLSD]);
868 else if ((upiece == rook) && (board[u] == lance)
869 && (GameType[c1] == STATIC_ROOK)
870 && (column(u) == csq))
872 s += (ds = fv1[XRAY]);
878 /* enemy's intercepting piece in pin attack */
881 /* lance pin attack */
882 if (board[u] == pawn)
884 s += (ds = -2*fv1[PROTECT]);
886 else if (in_opening_stage)
888 s += (ds = 2 * fv1[PROTECT]);
893 /* bishop or rook pin attack */
894 if (board[u] == pawn)
896 s += (ds = -fv1[HCLSD]);
903 xu = next_position(ptyp, &d, sq, u);
905 if (xu != next_direction(ptyp, &dd, sq))
906 pin = u; /* not on the edge and on to find a pin */
908 if ((xu = ppos[u]) != pdir[u])
909 pin = u; /* not on the edge and on to find a pin */
915 /* it's the second piece in the current direction */
919 /* second piece is an own piece */
920 if ((upiece == bishop) && (board[u] == pawn)
921 && (GameType[c1] == STATIC_ROOK))
923 s += (ds = -fv1[HCLSD]);
928 /* second piece is an enemy piece */
929 if (upiece == bishop && board[u] == pawn)
931 s += (ds = -fv1[HCLSD]/2);
934 if (((*value)[stage][xupiece = unpromoted[board[u]]]
938 if (color[pin] == c2)
940 if ((xupiece == king) && (in_endgame_stage))
942 s += (ds = 2 * fv1[PINVAL]);
946 s += (ds = fv1[PINVAL]);
950 || (atk1[pin] > control[board[pin]] + 1))
960 s += (ds = fv1[XRAY] / 2);
964 s += (ds = fv1[XRAY]);
970 pin = -1; /* new direction */
973 u = next_direction(ptyp, &d, sq);
988 #define ctlSG (ctlS | ctlG | ctlPp | ctlLp | ctlNp | ctlSp)
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.
997 * The following must be true:
998 * board[sq] == king, c1 == color[sq], c2 == otherside[c1]
1001 #define SCORETHREAT \
1003 if (color[u] != c2) \
1005 if ((atk1[u] == 0) || ((atk2[u] & CNT_MASK) != 0)) \
1011 s += (ds = -fv1[CTRLK]); \
1027 unsigned char *ppos, *pdir;
1034 /* Penalties, if a king can be threatened by checks. */
1042 for (p = pawn; p < king; p++)
1044 if (HasPiece[c2][p] || Captured[c2][p])
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.
1053 ptyp = ptype[c1][p];
1056 u = first_direction(ptyp, &d, sq);
1058 ppos = (*nextpos[ptyp])[sq];
1059 pdir = (*nextdir[ptyp])[sq];
1065 /* If a p piece can reach (controls or can drop to)
1066 * square u, then score threat.
1069 if (atk2[u] & control[p])
1071 else if ((Captured[c2][p] && color[u]) == neutral)
1075 u = ((color[u] == neutral)
1076 ? next_position(ptyp, &d, sq, u)
1077 : next_direction(ptyp, &d, sq));
1079 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1087 s += (ds = fv1[KSFTY] * KTHRT[cnt] / 16);
1089 /* Penalties, if squares near king are controlled by enemy. */
1093 ptyp = ptype[c1][king];
1096 u = first_direction(ptyp, &d, sq);
1098 pdir = (*nextpos[ptyp])[sq];
1104 if (!ok && color[u] == c1)
1106 short ptype_piece = ptype[black][board[u]];
1108 if ((ptype_piece == ptype_silver) || (ptype_piece == ptype_gold))
1112 if (atk2[u] > atk1[u])
1116 if (atk2[u] & ctlSG)
1118 s += (ds = -fv1[KSFTY] / 2);
1123 u = next_direction(ptyp, &d, sq);
1130 if (!ok || (cnt > 1))
1133 s += (ds = -fv1[KSFTY]/2);
1135 s += (ds = -fv1[KSFTY]);
1142 static short checked_trapped;
1146 * See if the attacked piece has unattacked squares to move to. The following
1147 * must be true: c1 == color[sq] c2 == otherside[c1]
1157 unsigned char *ppos, *pdir;
1163 rvalue = (*value)[stage][piece];
1164 ptyp = ptype[c1][piece];
1167 u = first_direction(ptyp, &d, sq);
1169 ppos = (*nextpos[ptyp])[sq];
1170 pdir = (*nextdir[ptyp])[sq];
1178 if ((atk2[u] == 0) || ((*value)[stage][board[u]] >= rvalue))
1183 u = ((color[u] == neutral)
1184 ? next_position(ptyp, &d, sq, u)
1185 : next_direction(ptyp, &d, sq));
1187 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1192 checked_trapped = true;
1200 AttackedPieceValue(short sq, short side)
1206 ds = -fv1[HUNGP] * 2;
1222 OpenFileValue(short sq, short hopn, short hopnx)
1226 if (PC1[fyle = column(sq)] == 0)
1242 /* Distance bonus */
1244 #define PromotionZoneDistanceValue(sq, dd) \
1245 if ((ds = fv1[PROMD])) \
1247 s += (ds = ds * PromotionZoneDistanceBonus[crow(c1, sq)] * dd); \
1250 #define OwnKingDistanceValue(sq, dd, maxd) \
1251 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c1][sq]) <= maxd)) \
1253 s += (ds = ds * OwnKingDistanceBonus[ad] * dd); \
1256 #define EnemyKingDistanceValue(sq, dd, maxd) \
1257 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c2][sq]) <= maxd)) \
1259 s += (ds = ds * EnemyKingDistanceBonus[ad] * dd); \
1267 * Calculate the positional value for a pawn on 'sq'.
1271 PawnValue(short sq, short side)
1275 short ccol = ccolumn(c1, sq);
1277 PromotionZoneDistanceValue(sq, 3);
1280 if (color[(c1 == black) ? (sq + NO_COLS) : (sq - NO_COLS)] == neutral)
1282 s += (ds = MBLTY[pawn]);
1285 if ((a1 & ((ctlR | ctlRp) | ctlL)))
1287 s += (ds = fv1[ATTACKED]);
1290 if (in_opening_stage)
1292 if (crow(c1, sq) == 2) /* pawn on 3d rank */
1294 if (board[(c1 == black) ?
1295 (sq + 3*NO_COLS) : (sq - 3*NO_COLS)] == pawn)
1297 /* opposing pawn has been moved (even column == (sq & 1)) */
1305 m = (side == c1) ? 3 : 5;
1309 m = (side == c1) ? 2 : 3;
1313 m = (side == c1) ? 1 : 2;
1317 s += (ds = -m * MBLTY[pawn]);
1321 if ((GameType[c1] == STATIC_ROOK) && (sq == csquare(c1, 43)))
1323 if ((atk2[csquare(c1, 52)] & CNT_MASK) < 2)
1325 s += (ds = fv1[ATTACKED]);
1329 if ((GameType[c2] == STATIC_ROOK) && (ccol == 1))
1331 if (sq == csquare(c1, 28))
1333 s += (ds = -fv1[ATTACKED]);
1336 if (((atk1[csquare(c1, 19)] & CNT_MASK) < 2)
1337 && ((atk1[csquare(c1, 28)] & CNT_MASK) < 2))
1339 s += (ds = -2 * fv1[ATTACKED]);
1350 * Calculate the positional value for a lance on 'sq'.
1354 LanceValue(short sq, short side)
1356 short s = 0, ds, ad;
1358 OwnKingDistanceValue(sq, 1, 2);
1360 OpenFileValue(sq, -fv1[HOPN], fv1[HOPN]);
1362 if (!checked_trapped && (crow(c1, sq) > 2))
1364 if (in_opening_stage || trapped(sq))
1366 s += (ds = -3 * fv1[ATTACKED]);
1370 s += (ds = -2 * fv1[ATTACKED]);
1380 * Calculate the positional value for a knight on 'sq'.
1384 KnightValue(short sq, short side)
1387 short ds, checked_trapped = false;
1388 short c = column(sq);
1390 PromotionZoneDistanceValue(sq, 1);
1391 OwnKingDistanceValue(sq, 1, 2);
1393 if (!checked_trapped && (crow(c1, sq) > 2))
1397 s += (ds = -4 * fv1[ATTACKED]);
1401 s += (ds = -3 * fv1[ATTACKED]);
1405 if ((c == 0) || (c == 8))
1407 s += (ds = -fv1[ATTACKED]);
1416 * Calculate the positional value for a silver on 'sq'.
1420 SilverValue(short sq, short side)
1424 OwnKingDistanceValue(sq, 2, 3);
1426 if ((Kdist[c1][sq] < 3)
1427 && (atk1[sq] & (control[gold] | control[silver])))
1429 s += (ds = fv1[COHESION]);
1432 if (in_opening_stage)
1434 if (GameType[c1] == STATIC_ROOK)
1436 if (csquare(c1, sq) == 12)
1440 if ((board[csq = csquare(c1, 20)] == bishop)
1441 && (color[csq] == c1))
1443 s += (ds = -2 * fv1[OPENWRONG]);
1450 EnemyKingDistanceValue(sq, 2, 3);
1459 * Calculate the positional value for a gold on 'sq'.
1463 GoldValue(short sq, short side)
1465 short s = 0, ds, ad;
1467 OwnKingDistanceValue(sq, 2, 3);
1469 if ((Kdist[c1][sq] < 3)
1470 && (atk1[sq] & (control[gold] | control[silver])))
1472 s += (ds = fv1[COHESION]);
1475 if (in_opening_stage)
1477 if ((GameType[c1] == STATIC_ROOK) && (GameType[c2] != STATIC_ROOK))
1479 if (Mvboard[csquare(c1, 3)])
1481 s += (ds = -2 * fv1[OPENWRONG]);
1487 EnemyKingDistanceValue(sq, 2, 3);
1496 * Calculate the positional value for a bishop on 'sq'.
1500 BishopValue(short sq, short side)
1502 short s = 0, ds, ad;
1504 if (in_opening_stage)
1506 if (GameType[c1] == RANGING_ROOK)
1508 /* Bishops diagonal should not be open */
1509 if (!on_csquare(c1, pawn, 30))
1511 s += (ds = -fv1[OPENWRONG]);
1514 else if (GameType[c2] == RANGING_ROOK)
1516 /* Bishops diagonal should be open */
1517 if ((csquare(c1, sq) == 10)
1518 && (!empty_csquare(c1, 20) || !empty_csquare(c1, 30)))
1520 s += (ds = -fv1[OPENWRONG]);
1522 else if ((csquare(c1, sq) == 20) && !empty_csquare(c1, 30))
1524 s += (ds = -fv1[OPENWRONG]);
1530 EnemyKingDistanceValue(sq, 1, 3);
1539 * Calculate the positional value for a rook on 'sq'.
1543 RookValue(short sq, short side)
1545 short s = 0, ds, ad;
1547 OpenFileValue(sq, 2 * fv1[HOPN], 4*fv1[HOPN]);
1549 if (in_opening_stage)
1551 short WRONG = fv1[OPENWRONG], OPOK = WRONG / 3;
1553 if (GameType[c1] == STATIC_ROOK)
1555 short c = ccolumn(c1, sq);
1557 /* Bonus for rook on 8th file */
1564 * Bonus for rook on right side,
1565 * penalty for rook on left side
1573 s += (ds = c + c + OPOK);
1577 s += (ds = -c - c - WRONG);
1580 else if (GameType[c1] == RANGING_ROOK)
1582 /* Bonus for rook on left side and
1583 * bishops diagonal closed, penalty otherwise. */
1586 c = 4 - ccolumn(c1, sq);
1591 /* Bishops diagonal should not be open. */
1592 if (on_csquare(c1, pawn, 30))
1595 s += (ds = -c - c - WRONG);
1599 s += (ds = -c - c - WRONG);
1601 /* Penalty for king not on initial square. */
1602 if (!on_csquare(side, king, 4))
1612 EnemyKingDistanceValue(sq, 1, 3);
1621 * Calculate the positional value for a promoted pawn on 'sq'.
1625 PPawnValue(short sq, short side)
1627 short s = 0, ds, ad;
1629 EnemyKingDistanceValue(sq, 3, 10);
1637 * Calculate the positional value for a promoted lance on 'sq'.
1641 PLanceValue(short sq, short side)
1643 short s = 0, ds, ad;
1645 EnemyKingDistanceValue(sq, 3, 10);
1653 * Calculate the positional value for a promoted knight on 'sq'.
1657 PKnightValue(short sq, short side)
1659 short s = 0, ds, ad;
1661 EnemyKingDistanceValue(sq, 3, 10);
1669 * Calculate the positional value for a promoted silver on 'sq'.
1673 PSilverValue(short sq, short side)
1675 short s = 0, ds, ad;
1677 EnemyKingDistanceValue(sq, 3, 10);
1685 * Calculate the positional value for a promoted bishop on 'sq'.
1689 PBishopValue(short sq, short side)
1691 short s = 0, ds, ad;
1693 EnemyKingDistanceValue(sq, 3, 4);
1701 * Calculate the positional value for a promoted rook on 'sq'.
1705 PRookValue(short sq, short side)
1707 short s = 0, ds, ad;
1709 EnemyKingDistanceValue(sq, 3, 4);
1711 OpenFileValue(sq, 3 * fv1[HOPN], 2 * fv1[HOPN]);
1719 * Calculate the positional value for a king on 'sq'.
1723 KingValue(short sq, short side)
1727 if (fv1[KSFTY] != 0)
1730 if (in_opening_stage)
1732 if ((GameType[c1] != UNKNOWN) && (ccolumn(c1, sq) == 4))
1734 s += (ds = -fv1[OPENWRONG] / 3);
1736 else if ((GameType[c1] == STATIC_ROOK) && on_right_side(c1, sq))
1738 s += (ds = -fv1[OPENWRONG] / 2);
1740 else if ((GameType[c1] == RANGING_ROOK) && on_left_side(c1, sq))
1742 s += (ds = -fv1[OPENWRONG] / 2);
1746 /* CHECKME: is this correct? */
1747 if ((ds = fv1[HOPN]))
1749 s += OpenFileValue(sq, -2 * ds, -4 * ds);
1758 * Calculate the positional value for a piece on 'sq'.
1762 PieceValue(short sq, short side)
1769 if (piece == no_piece)
1772 s = (*Mpiece[piece])[c1][sq];
1774 checked_trapped = false;
1778 /* pin/x-ray attack and mobility for sweeping pieces */
1779 s += (ds = BRLscan(sq, &mob));
1781 if ((piece == bishop) || (piece == pbishop))
1782 s += (ds = BMBLTY[mob] * fv1[MOBILITY] / 100);
1783 else if ((piece == rook) || (piece == prook))
1784 s += (ds = RMBLTY[mob] * fv1[MOBILITY] / 100);
1786 s += (ds = LMBLTY[mob] * fv1[MOBILITY] / 100);
1790 /* mobility for non-sweeping pieces */
1798 /* opponent attacks piece */
1801 /* undefended piece */
1802 s += AttackedPieceValue(sq, side);
1806 /* defended piece */
1807 short attack_value = value_of_weakest_attacker(a2);
1808 short piece_value = (*value)[stage][piece];
1810 if (attack_value < piece_value)
1812 /* attacked by a weaker piece */
1813 s += AttackedPieceValue(sq, side) / 2;
1815 else if (abs(attack_value - piece_value) < 10)
1817 /* opponent has the option to exchange equal pieces */
1818 s += (ds = -fv1[ATTACKED]);
1828 /* piece is defended */
1829 s += (ds = (a1 & CNT_MASK) * fv1[PROTECT]);
1834 s += (ds = fv1[SEED]);
1839 s += (ds = -fv1[LOOSE]);
1842 if (starget[c1][sq])
1846 s -= (ds = -fv1[ATTACKED]/2);
1848 else if (piece == pawn)
1850 s += (ds = fv1[ATTACKED]);
1854 if (starget[c2][sq])
1858 s -= (ds = -fv1[ATTACKED] / 3);
1862 s += (ds = fv1[ATTACKED]);
1866 if (Kdist[c1][sq] == 1)
1868 s += (ds = fv1[KSFTY]);
1875 s += PawnValue(sq, side);
1879 s += LanceValue(sq, side);
1883 s += KnightValue(sq, side);
1887 s += SilverValue(sq, side);
1891 s += GoldValue(sq, side);
1895 s += BishopValue(sq, side);
1899 s += RookValue(sq, side);
1903 s += KingValue(sq, side);
1907 s += PPawnValue(sq, side);
1911 s += PLanceValue(sq, side);
1915 s += PKnightValue(sq, side);
1919 s += PSilverValue(sq, side);
1923 s += PBishopValue(sq, side);
1927 s += PRookValue(sq, side);
1937 * Score distance to pattern regarding the game type which side plays.
1941 ScorePatternDistance(short c1)
1944 small_short *fv1 = fvalue[c1];
1947 if ((MAXCDIST > 0) && (fv1[PCASTLE] != 0)
1948 && ((os = castle_opening_sequence[c1]) >= 0))
1950 ds = board_to_pattern_distance(c1, os, MAXCDIST, GameCnt);
1954 s += (ds *= fv1[PCASTLE]);
1958 if ((MAXADIST > 0) && (fv1[PATTACK] != 0)
1959 && ((os = attack_opening_sequence[c1]) >= 0))
1961 ds = board_to_pattern_distance(c1, os, MAXADIST, GameCnt);
1965 s += (ds *= fv1[PATTACK]);
1976 * Determine castle and attack pattern which should be reached next.
1977 * Only patterns are considered, which have not been reached yet.
1981 UpdatePatterns(short side, short GameCnt)
1984 short xside = side ^ 1;
1988 strcpy(s, "CASTLE_?_?");
1989 s[7] = GameType[side];
1990 s[9] = GameType[xside];
1991 castle_opening_sequence[side] = os
1992 = locate_opening_sequence(side, s, GameCnt);
1994 if (flag.post && (os != END_OF_SEQUENCES))
1996 for (j = 0; j < MAX_SEQUENCE; j++)
1998 for (k = OpeningSequence[os].first_pattern[j];
1999 k != END_OF_PATTERNS;
2000 k = Pattern[k].next_pattern)
2002 if (Pattern[k].distance[side] >= 0)
2008 if (os != END_OF_SEQUENCES)
2009 update_advance_bonus(side, os);
2011 strcpy(s, "ATTACK_?_?");
2012 s[7] = GameType[side];
2013 s[9] = GameType[xside];
2014 attack_opening_sequence[side] = os
2015 = locate_opening_sequence(side, s, GameCnt);
2017 if (flag.post && (os != END_OF_SEQUENCES))
2019 for (j = 0; j < MAX_SEQUENCE; j++)
2021 for (k = OpeningSequence[os].first_pattern[j];
2022 k != END_OF_PATTERNS;
2023 k = Pattern[k].next_pattern)
2025 if (Pattern[k].distance[side] >= 0)
2032 ShowPatternCount(side, n);
2034 if (os != END_OF_SEQUENCES)
2035 update_advance_bonus(side, os);
2043 short ds, col, n, m, piece;
2045 if ((n = Captured[c1][pawn]))
2049 for (col = 0; col < NO_COLS; col++)
2058 /* FIXME! another great obfuscated line... */
2059 pscore[c1] += (ds *= ((n > 2) ? 3 : n));
2064 for (piece = lance, n = 0; piece <= rook; piece++)
2066 if (Captured[c1][piece])
2070 pscore[c1] += (ds = m * fv1[DFFDROP]);
2073 for (piece = pawn, n = 0; piece <= rook; piece++)
2075 if (Captured[c1][piece])
2080 ds = BMBLTY[MAX_BMBLTY - 1];
2084 ds = RMBLTY[MAX_RMBLTY - 1];
2088 ds = LMBLTY[MAX_LMBLTY - 1];
2097 if (!Captured[c2][piece])
2098 n += relative_value[piece];
2104 pscore[c1] += (ds = -n * fv1[OPPDROP] / 2);
2113 * Perform normal static evaluation of board position. A score is generated
2114 * for each piece and these are summed to get a score for each side.
2118 ScorePosition(short side)
2127 UpdateWeights(side);
2129 hung[black] = hung[white] = pscore[black] = pscore[white] = 0;
2131 array_zero(shung, sizeof(shung));
2134 if (!(use_etable && ProbeEETable(side, &s)))
2137 for (c1 = black; c1 <= white; c1++)
2141 /* atk1 is array of attacks on squares by my side */
2144 /* atk2 is array of attacks on squares by other side */
2147 /* same for PC1 and PC2 */
2151 /* same for fv1 and fv2 */
2154 for (i = PieceCnt[c1]; i >= 0; i--)
2156 sq = PieceList[c1][i];
2158 #if defined SAVE_SVALUE
2159 pscore[c1] += PieceValue(sq, side);
2161 pscore[c1] += (svalue[sq] = PieceValue(sq, side));
2168 for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2174 /* Score fifth rank */
2175 for (sq = 36, n = 0; sq <= 44; sq++)
2177 if ((color[sq] == c1) || (attack[c1][sq] != 0))
2183 pscore[c1] += (ds = n * fv1[CNTRL5TH]);
2187 for (sq = ((c1 == black) ? 0 : 54), n = 0;
2188 sq <= ((c1 == black) ? 26 : 80);
2191 if (board[sq] == no_piece && attack[c1][sq] == 0)
2197 pscore[c1] += (ds = -n * fv1[HOLES]);
2202 pscore[c1] += (ds = -fv1[HUNGX]);
2205 /* Score opening features and
2206 * castle/attack pattern distances */
2208 if (in_opening_stage)
2210 pscore[c1] += (ds = ScoreKingOpeningFeatures());
2211 pscore[c1] += (ds = ScorePatternDistance(c1));
2215 score = mtl[side] - mtl[xside]
2216 + pscore[side] - pscore[xside] + 10;
2219 if (use_etable && PUTVAR)
2220 PutInEETable(side, score);
2224 #ifdef CACHE /* CHECKME: this looks bad */
2234 * Try to determine the game type of "side".
2238 GuessGameType(short side_to_move)
2241 short StaticRook[2] = { 0, 0 };
2242 short RangingRook[2] = { 0, 0 };
2244 for (side = black; side <= white; side++)
2246 /* computer should not change its mind */
2248 extern int bookflag;
2250 gt = GameType[side];
2252 if (!bookflag && (side == side_to_move))
2254 if (gt == STATIC_ROOK)
2255 StaticRook[side] += 4;
2256 else if (gt == RANGING_ROOK)
2257 RangingRook[side] += 4;
2260 /* static rook conditions */
2262 if (on_column(side, rook, 7))
2263 StaticRook[side] += 3;
2265 if (on_csquare(side, pawn, 34))
2266 StaticRook[side] += 6;
2267 else if (on_csquare(side, pawn, 43))
2268 StaticRook[side] += 4;
2269 else if (!on_column(side, pawn, 7))
2270 StaticRook[side] += 5;
2272 if (empty_csquare(side, 5) || empty_csquare(side, 6))
2273 StaticRook[side] += 2;
2275 if (on_left_side(side, king))
2276 StaticRook[side] += 2;
2278 /* ranging rook conditions */
2280 if (on_left_side(side, rook))
2281 RangingRook[side] += 5;
2282 else if (!on_column(side, rook, 7))
2283 RangingRook[side] += 3;
2285 if (on_csquare(side, pawn, 25))
2286 RangingRook[side] += 1;
2288 if (on_csquare(side, pawn, 30))
2289 RangingRook[side] += 1;
2291 RangingRook[side] -= 2;
2293 if (!on_right_side(side, rook))
2294 RangingRook[side] += 4;
2296 if (on_right_side(side, king))
2297 RangingRook[side] += 4;
2299 if (on_csquare(side, bishop, 20))
2301 if (on_csquare(side, silver, 11)
2302 || on_csquare(side, silver, 12)
2303 || on_csquare(side, silver, 21))
2305 RangingRook[side] += 3;
2309 if ((StaticRook[side] > 5) || (RangingRook[side] > 5))
2311 GameType[side] = (StaticRook[side] > RangingRook[side])
2312 ? STATIC_ROOK : RANGING_ROOK;
2316 GameType[side] = UNKNOWN;
2320 if ((GameType[black] == UNKNOWN) || (GameType[white] == UNKNOWN))
2322 for (side = black; side <= white; side++)
2324 if ((side == computer) && (GameType[side] == UNKNOWN))
2327 * Game type is UNKNOWN.
2328 * Make a decision what type of game to play.
2329 * To make computer games more interesting, make a
2333 if (!on_csquare(side, pawn, 25))
2335 /* Play static rook if rook pawn has been pushed! */
2336 GameType[side] = STATIC_ROOK;
2340 unsigned int random = urand() % 100;
2341 short d = StaticRook[side] - RangingRook[side];
2343 switch (GameType[side ^ 1])
2346 if (random < 35 + d)
2347 GameType[side] = STATIC_ROOK;
2348 else if (random < 95)
2349 GameType[side] = RANGING_ROOK;
2353 if (random < 75 + d)
2354 GameType[side] = STATIC_ROOK;
2355 else if (random < 95)
2356 GameType[side] = RANGING_ROOK;
2360 if (random < 33 + d)
2361 GameType[side] = STATIC_ROOK;
2362 else if (random < 66)
2363 GameType[side] = RANGING_ROOK;
2375 DetermineGameType(short side_to_move)
2379 GuessGameType(side_to_move);
2381 array_zero(Mpawn, sizeof(Mpawn));
2382 array_zero(Mlance, sizeof(Mlance));
2383 array_zero(Mknight, sizeof(Mknight));
2384 array_zero(Msilver, sizeof(Msilver));
2385 array_zero(Mgold, sizeof(Mgold));
2386 array_zero(Mbishop, sizeof(Mbishop));
2387 array_zero(Mrook, sizeof(Mrook));
2388 array_zero(Mking, sizeof(Mking));
2390 if (in_opening_stage)
2392 for (side = black; side <= white; side++)
2393 UpdatePatterns(side, GameCnt);
2397 ShowPatternCount(black, -1);
2398 ShowPatternCount(white, -1);
2405 * This is done one time before the search is started. Set up arrays
2406 * Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the SqValue()
2407 * function to determine the positional value of each piece.
2411 ExaminePosition(short side)
2413 short c1, piece, sq, i, bsq, wsq;
2415 /* Build enemy king distance tables. */
2417 for (sq = 0, bsq = BlackKing, wsq = WhiteKing; sq < NO_SQUARES; sq++)
2419 Kdist[black][sq] = distance(sq, bsq);
2420 Kdist[white][sq] = distance(sq, wsq);
2428 DetermineGameType(side);
2429 DetermineStage(side);
2431 UpdateWeights(side);
2433 array_zero(HasPiece, sizeof(HasPiece));
2435 for (c1 = black; c1 <= white; c1++)
2437 for (i = PieceCnt[c1]; i >= 0; i--)
2439 ++HasPiece[c1][piece = board[sq = PieceList[c1][i]]];
2447 DetermineStage(short side)
2449 short xside = side ^ 1, ds, db1, c1, c2, feature;
2451 /* Determine initial stage */
2453 balance[side] = balance[xside] = 50;
2455 if ((GameType[side] == STATIC_ROOK)
2456 && (GameType[xside] == STATIC_ROOK))
2460 else if (GameCnt < 60)
2462 else if (GameCnt < 80)
2467 else if ((GameType[side] == RANGING_ROOK)
2468 || (GameType[xside] == RANGING_ROOK))
2472 else if (GameCnt < 50)
2474 else if (GameCnt < 70)
2483 else if (GameCnt < 55)
2485 else if (GameCnt < 75)
2491 /* Update stage depending on board features and attack balance value */
2493 if (abs(ds = (mtl[side] - mtl[xside]))
2494 > (db1 = (*value)[stage][lance]))
2496 db1 = abs(4 * ds / db1);
2499 balance[side] += db1;
2501 balance[xside] += db1;
2503 stage += (ds = db1);
2506 for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2508 if ((ds = seed[c1]) > 2)
2510 balance[c1] += (db1 = ds * 2);
2519 if ((db1 = hung[c1]) > 2)
2521 balance[c1] -= (db1 *= 2);
2525 if ((db1 = loose[c1]) > 4)
2527 balance[c1] -= (db1 /= 2);
2532 if ((ds = hole[c1]))
2534 balance[c1] -= (db1 = ds);
2539 if ((db1 = target[c1]) > 3)
2541 balance[c1] += (db1 /= 3);
2543 stage += (ds = db1 / 4);
2546 stage += (ds = captured[c1] / 2);
2548 if ((db1 = captured[c1]) > 4)
2550 balance[c1] += (db1 /= 2);
2554 if ((db1 = dcaptured[c1]) > 3)
2560 if (balance[c1] > 99)
2562 else if (balance[c1] < 0)
2574 /* Determine stage dependant weights */
2576 ADVNCM[pawn] = 1; /* advanced pawn bonus increment */
2579 ADVNCM[silver] = 1; /* advanced silver bonus increment */
2580 ADVNCM[gold] = 1; /* advanced gold bonus increment */
2583 ADVNCM[king] = 1; /* advanced king bonus increment */
2585 MAXCDIST = (stage < 33) ? (33 - stage) / 4 : 0;
2586 MAXADIST = (stage < 30) ? (30 - stage) / 4 : 0;
2588 for (c1 = black; c1 <= white; c1++)
2590 for (feature = 0; feature < NO_FEATURES; feature++)
2592 fvalue[c1][feature] =
2593 ((((*fscore)[stage][feature][0] * (99 - balance[c1])) + 50)
2595 + ((((*fscore)[stage][feature][1] * balance[c1]) + 50)
2604 UpdateWeights(short stage)
2611 * Compute stage dependent relative material values assuming
2612 * linearity between the main stages:
2614 * minstage < stage < maxstage =>
2615 * stage - minstage value - minvalue
2616 * ------------------- = -------------------
2617 * maxstage - minstage maxvalue - minvalue
2622 linear_piece_value(short piece, short stage, short i, short j)
2624 short minvalue, maxvalue, minstage, maxstage;
2626 minstage = ispvalue[0][i];
2627 maxstage = ispvalue[0][j];
2628 minvalue = ispvalue[piece][i];
2629 maxvalue = ispvalue[piece][j];
2631 /* FIXME: set a variable then return it, puh-leeze! */
2632 return ((stage - minstage) * (maxvalue - minvalue)
2633 / (maxstage - minstage)) + minvalue;
2639 linear_feature_value(short feature, short stage, short i, short j)
2641 short minvalue, maxvalue, minstage, maxstage;
2643 minstage = ispvalue[0][i];
2644 maxstage = ispvalue[0][j];
2645 minvalue = weight[feature][i];
2646 maxvalue = weight[feature][j];
2648 /* FIXME: set a variable then return it, puh-leeze! */
2649 return ((stage - minstage) * (maxvalue - minvalue)
2650 / (maxstage - minstage)) + minvalue;
2655 * matweight = percentage_of_max_value * max_value(stage) / 100
2656 * max_value(0) = MAX_VALUE; max_value(100) = MIN_VALUE
2657 * => max_value(stage) = a * stage + b; b = MAX_VALUE,
2658 * a = (MIN_VALUE - MAX_VALUE)/100
2661 #define MIN_VALUE 300
2662 #define MAX_VALUE 1000
2664 #define max_value(stage) \
2665 ((long)(MIN_VALUE - MAX_VALUE) * stage + (long)100 * MAX_VALUE)
2666 #define matweight(value, stage) \
2667 ((long)max_value(stage) * value / 10000)
2672 Initialize_eval(void)
2674 short stage, piece, feature, i;
2676 for (stage = 0; stage < NO_STAGES; stage++)
2678 for (i = 0; i < MAIN_STAGES; i++)
2680 if (stage == ispvalue[0][i])
2682 for (piece = 0; piece < NO_PIECES; piece++)
2684 (*value)[stage][piece] =
2685 matweight(ispvalue[piece][i], stage);
2688 for (feature = 0; feature < NO_FEATURES; feature++)
2690 (*fscore)[stage][feature][0] =
2692 * weight[feature][MAIN_STAGES] + 50) / 100;
2694 (*fscore)[stage][feature][1] =
2696 * weight[feature][MAIN_STAGES + 1] + 50) / 100;
2702 if (stage < ispvalue[0][i + 1])
2704 for (piece = 0; piece < NO_PIECES; piece++)
2706 (*value)[stage][piece] =
2708 linear_piece_value(piece, stage, i, i + 1),
2712 for (feature = 0; feature < NO_FEATURES; feature++)
2714 (*fscore)[stage][feature][0] =
2715 (linear_feature_value(feature, stage, i, i + 1)
2716 * weight[feature][MAIN_STAGES] + 50) / 100;
2718 (*fscore)[stage][feature][1] =
2719 (linear_feature_value(feature, stage, i, i + 1)
2720 * weight[feature][MAIN_STAGES + 1] + 50) / 100;