Line data Source code
1 : #pragma once
2 :
3 : #include "bitboardTools.hpp"
4 : #include "score.hpp"
5 :
6 : /*!
7 : * A material table implementation
8 : * Mostly inspired by Gull by Vadim Demichev
9 : * after a discussion on talkchess (http://talkchess.com/forum3/viewtopic.php?f=7&t=67558&p=763426)
10 : * but we allow 2 bishops of the same color
11 : */
12 :
13 : namespace MaterialHash {
14 :
15 : inline const int MatWQ = 1;
16 : inline const int MatBQ = 3;
17 : inline const int MatWR = (3 * 3);
18 : inline const int MatBR = (3 * 3 * 3);
19 : inline const int MatWL = (3 * 3 * 3 * 3);
20 : inline const int MatBL = (3 * 3 * 3 * 3 * 3);
21 : inline const int MatWD = (3 * 3 * 3 * 3 * 3 * 3);
22 : inline const int MatBD = (3 * 3 * 3 * 3 * 3 * 3 * 3);
23 : inline const int MatWN = (3 * 3 * 3 * 3 * 3 * 3 * 3 * 3);
24 : inline const int MatBN = (3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3);
25 : inline const int MatWP = (3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3);
26 : inline const int MatBP = (3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 9);
27 : #ifdef WITH_MATERIAL_TABLE
28 : inline const int TotalMat = ((2 * (MatWQ + MatBQ) + 2 * (MatWL + MatBL + MatWD + MatBD) + 2 * (MatWR + MatBR + MatWN + MatBN) + 8 * (MatWP + MatBP)) + 1);
29 : #else
30 : inline const int TotalMat = 1;
31 : #endif
32 :
33 : [[nodiscard]] Hash getMaterialHash(const Position::Material &mat);
34 : #ifndef WITH_MATERIAL_TABLE
35 : [[nodiscard]] Hash getMaterialHash2(const Position::Material &mat);
36 : #endif
37 :
38 : [[nodiscard]] Position::Material materialFromString(const std::string &strMat);
39 :
40 : ScoreType helperKXK (const Position &p, Color winningSide, ScoreType s, DepthType height);
41 : ScoreType helperKmmK(const Position &p, Color winningSide, ScoreType s, DepthType height);
42 : ScoreType helperKPK (const Position &p, Color winningSide, ScoreType s, DepthType height);
43 : ScoreType helperKBPK(const Position &p, Color winningSide, ScoreType s, DepthType height);
44 :
45 : enum Terminaison : uint8_t {
46 : Ter_Unknown = 0,
47 : Ter_WhiteWinWithHelper,
48 : Ter_WhiteWin,
49 : Ter_BlackWinWithHelper,
50 : Ter_BlackWin,
51 : Ter_Draw, // real FIDE draw KK, KBK, KNK, KLKD
52 : Ter_Draw3rep,
53 : Ter_Draw50,
54 : Ter_MaterialDraw,
55 : Ter_LikelyDraw,
56 : Ter_HardToWin,
57 : Ter_None
58 : };
59 :
60 : extern ScoreType (*helperTable[TotalMat])(const Position &, Color, ScoreType, DepthType);
61 :
62 : struct MaterialHashEntry {
63 : EvalScore score = {0, 0};
64 : uint8_t gp = 255;
65 : Terminaison t = Ter_Unknown;
66 4106486 : FORCE_FINLINE float gamePhase() const { return gp / 255.f; }
67 105225318 : FORCE_FINLINE void setGamePhase(float gpf) { gp = (uint8_t)(255 * gpf); }
68 : };
69 :
70 : extern array1d<MaterialHashEntry,TotalMat> materialHashTable;
71 :
72 : [[nodiscard]] EvalScore Imbalance(const Position::Material &mat, Color c);
73 :
74 : void InitMaterialScore(bool display = true);
75 :
76 : struct MaterialHashInitializer {
77 1760 : MaterialHashInitializer(const Position::Material &mat, Terminaison t) { materialHashTable[getMaterialHash(mat)].t = t; }
78 660 : MaterialHashInitializer(const Position::Material &mat, Terminaison t, ScoreType (*helper)(const Position &, Color, ScoreType, DepthType)) {
79 660 : materialHashTable[getMaterialHash(mat)].t = t;
80 660 : helperTable[getMaterialHash(mat)] = helper;
81 660 : }
82 : static void init();
83 : };
84 :
85 : [[nodiscard]] Terminaison probeMaterialHashTable(const Position::Material &mat);
86 :
87 : void updateMaterialOther(Position &p);
88 :
89 : void initMaterial(Position &p);
90 :
91 : void updateMaterialProm(Position &p, const Square toBeCaptured, const MType mt);
92 :
93 : } // namespace MaterialHash
|