]> git.sesse.net Git - stockfish/blob - src/pawns.h
First attempt at tweaking UnpairedPawnsTable[] values
[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-2010 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
21 #if !defined(PAWNS_H_INCLUDED)
22 #define PAWNS_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "bitboard.h"
29 #include "scale.h"
30 #include "value.h"
31
32 ////
33 //// Types
34 ////
35
36 /// PawnInfo is a class which contains various information about a pawn
37 /// structure. Currently, it only includes a middle game and an end game
38 /// pawn structure evaluation, and a bitboard of passed pawns. We may want
39 /// to add further information in the future. A lookup to the pawn hash table
40 /// (performed by calling the get_pawn_info method in a PawnInfoTable object)
41 /// returns a pointer to a PawnInfo object.
42 class Position;
43
44 class PawnInfo {
45
46   friend class PawnInfoTable;
47
48 public:
49   PawnInfo() { clear(); }
50
51   Score pawns_value() const;
52   Value kingside_storm_value(Color c) const;
53   Value queenside_storm_value(Color c) const;
54   Bitboard pawn_attacks(Color c) const;
55   Bitboard passed_pawns() const;
56   ScaleFactor scale_factor(Color c) const;
57   int file_is_half_open(Color c, File f) const;
58   int has_open_file_to_left(Color c, File f) const;
59   int has_open_file_to_right(Color c, File f) const;
60   int get_king_shelter(const Position& pos, Color c, Square ksq);
61
62 private:
63   void clear();
64   int updateShelter(const Position& pos, Color c, Square ksq);
65
66   Key key;
67   Bitboard passedPawns;
68   Bitboard pawnAttacks[2];
69   Square kingSquares[2];
70   Score value;
71   int16_t ksStormValue[2], qsStormValue[2];
72   uint8_t halfOpenFiles[2], kingShelters[2], factor[2];
73 };
74
75 /// The PawnInfoTable class represents a pawn hash table.  It is basically
76 /// just an array of PawnInfo objects and a few methods for accessing these
77 /// objects.  The most important method is get_pawn_info, which looks up a
78 /// position in the table and returns a pointer to a PawnInfo object.
79
80 class PawnInfoTable {
81
82 public:
83   PawnInfoTable(unsigned numOfEntries);
84   ~PawnInfoTable();
85   PawnInfo* get_pawn_info(const Position& pos);
86
87 private:
88   template<Color Us>
89   Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi);
90
91   unsigned size;
92   PawnInfo* entries;
93 };
94
95
96 ////
97 //// Inline functions
98 ////
99
100 inline Score PawnInfo::pawns_value() const {
101   return value;
102 }
103
104 inline Bitboard PawnInfo::passed_pawns() const {
105   return passedPawns;
106 }
107
108 inline Bitboard PawnInfo::pawn_attacks(Color c) const {
109   return pawnAttacks[c];
110 }
111
112 inline Value PawnInfo::kingside_storm_value(Color c) const {
113   return Value(ksStormValue[c]);
114 }
115
116 inline Value PawnInfo::queenside_storm_value(Color c) const {
117   return Value(qsStormValue[c]);
118 }
119
120 inline ScaleFactor PawnInfo::scale_factor(Color c) const {
121   return ScaleFactor(factor[c]);
122 }
123
124 inline int PawnInfo::file_is_half_open(Color c, File f) const {
125   return (halfOpenFiles[c] & (1 << int(f)));
126 }
127
128 inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
129   return halfOpenFiles[c] & ((1 << int(f)) - 1);
130 }
131
132 inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
133   return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
134 }
135
136 inline int PawnInfo::get_king_shelter(const Position& pos, Color c, Square ksq) {
137   return (kingSquares[c] == ksq ? kingShelters[c] : updateShelter(pos, c, ksq));
138 }
139
140 #endif // !defined(PAWNS_H_INCLUDED)