-
-/// EndgameFunctions member definitions. This class is used to store the maps
-/// of end game and scaling functions that MaterialInfoTable will query for
-/// each key. The maps are constant and are populated only at construction,
-/// but are per-thread instead of globals to avoid expensive locks.
-
-EndgameFunctions::EndgameFunctions() {
-
- KNNKMaterialKey = buildKey("KNNK");
- KKNNMaterialKey = buildKey("KKNN");
-
- add("KPK", &EvaluateKPK);
- add("KKP", &EvaluateKKP);
- add("KBNK", &EvaluateKBNK);
- add("KKBN", &EvaluateKKBN);
- add("KRKP", &EvaluateKRKP);
- add("KPKR", &EvaluateKPKR);
- add("KRKB", &EvaluateKRKB);
- add("KBKR", &EvaluateKBKR);
- add("KRKN", &EvaluateKRKN);
- add("KNKR", &EvaluateKNKR);
- add("KQKR", &EvaluateKQKR);
- add("KRKQ", &EvaluateKRKQ);
- add("KBBKN", &EvaluateKBBKN);
- add("KNKBB", &EvaluateKNKBB);
-
- add("KNPK", WHITE, &ScaleKNPK);
- add("KKNP", BLACK, &ScaleKKNP);
- add("KRPKR", WHITE, &ScaleKRPKR);
- add("KRKRP", BLACK, &ScaleKRKRP);
- add("KBPKB", WHITE, &ScaleKBPKB);
- add("KBKBP", BLACK, &ScaleKBKBP);
- add("KBPPKB", WHITE, &ScaleKBPPKB);
- add("KBKBPP", BLACK, &ScaleKBKBPP);
- add("KBPKN", WHITE, &ScaleKBPKN);
- add("KNKBP", BLACK, &ScaleKNKBP);
- add("KRPPKRP", WHITE, &ScaleKRPPKRP);
- add("KRPKRPP", BLACK, &ScaleKRPKRPP);
- add("KRPPKRP", WHITE, &ScaleKRPPKRP);
- add("KRPKRPP", BLACK, &ScaleKRPKRPP);
+ // Zero or just one pawn makes it difficult to win, even with a small material
+ // advantage. This catches some trivial draws like KK, KBK and KNK and gives a
+ // drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
+ if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
+ e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
+ npm_b <= BishopValueMg ? 4 : 14);
+
+ if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
+ e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
+ npm_w <= BishopValueMg ? 4 : 14);
+
+ // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
+ // for the bishop pair "extended piece", which allows us to be more flexible
+ // in defining bishop pair bonuses.
+ const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = {
+ { pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
+ pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
+ { pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
+ pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
+
+ e->value = int16_t((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
+ return e;