]> git.sesse.net Git - stockfish/blob - src/pawns.h
Reparent to latest
[stockfish] / src / pawns.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #if !defined(PAWNS_H_INCLUDED)
21 #define PAWNS_H_INCLUDED
22
23 #include "misc.h"
24 #include "position.h"
25 #include "types.h"
26
27 const int PawnTableSize = 16384;
28
29 /// PawnEntry is a class which contains various information about a pawn
30 /// structure. Currently, it only includes a middle game and an end game
31 /// pawn structure evaluation, and a bitboard of passed pawns. We may want
32 /// to add further information in the future. A lookup to the pawn hash
33 /// table (performed by calling the probe method in a PawnTable object)
34 /// returns a pointer to a PawnEntry object.
35
36 class PawnEntry {
37
38   friend struct PawnTable;
39
40 public:
41   Score pawns_value() const;
42   Bitboard pawn_attacks(Color c) const;
43   Bitboard passed_pawns(Color c) const;
44   int file_is_half_open(Color c, File f) const;
45   int has_open_file_to_left(Color c, File f) const;
46   int has_open_file_to_right(Color c, File f) const;
47
48   template<Color Us>
49   Score king_safety(const Position& pos, Square ksq);
50
51 private:
52   template<Color Us>
53   Score update_safety(const Position& pos, Square ksq);
54
55   template<Color Us>
56   Value shelter_storm(const Position& pos, Square ksq);
57
58   Key key;
59   Bitboard passedPawns[2];
60   Bitboard pawnAttacks[2];
61   Square kingSquares[2];
62   Score value;
63   int halfOpenFiles[2];
64   Score kingSafety[2];
65 };
66
67
68 /// The PawnTable class represents a pawn hash table. The most important
69 /// method is probe, which returns a pointer to a PawnEntry object.
70
71 struct PawnTable {
72
73   PawnEntry* probe(const Position& pos);
74
75   template<Color Us>
76   static Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
77                               Bitboard theirPawns, PawnEntry* e);
78
79   HashTable<PawnEntry, PawnTableSize> entries;
80 };
81
82
83 inline Score PawnEntry::pawns_value() const {
84   return value;
85 }
86
87 inline Bitboard PawnEntry::pawn_attacks(Color c) const {
88   return pawnAttacks[c];
89 }
90
91 inline Bitboard PawnEntry::passed_pawns(Color c) const {
92   return passedPawns[c];
93 }
94
95 inline int PawnEntry::file_is_half_open(Color c, File f) const {
96   return halfOpenFiles[c] & (1 << int(f));
97 }
98
99 inline int PawnEntry::has_open_file_to_left(Color c, File f) const {
100   return halfOpenFiles[c] & ((1 << int(f)) - 1);
101 }
102
103 inline int PawnEntry::has_open_file_to_right(Color c, File f) const {
104   return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
105 }
106
107 template<Color Us>
108 inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
109   return kingSquares[Us] == ksq ? kingSafety[Us] : update_safety<Us>(pos, ksq);
110 }
111
112 #endif // !defined(PAWNS_H_INCLUDED)