-
Notifications
You must be signed in to change notification settings - Fork 4
/
pawns.cpp
165 lines (137 loc) · 6.94 KB
/
pawns.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Created by Vincent on 01/06/2019.
//
#include "pawns.h"
#include "eval.h"
pawns::structure_t::structure_t(const processed_params_t ¶ms, U64 kp_hash, U64 w_pawns, U64 b_pawns, U64 w_king,
U64 b_king)
: hash(kp_hash) {
v4si_t score = {0, 0, 0, 0};
// Find pawns
U64 open[2] = {pawns::open_pawns<WHITE>(w_pawns, b_pawns), pawns::open_pawns<BLACK>(b_pawns, w_pawns)};
U64 isolated[2] = {pawns::isolated(w_pawns), pawns::isolated(b_pawns)};
U64 backwards[2] = {pawns::backward<WHITE>(w_pawns, b_pawns), pawns::backward<BLACK>(b_pawns, w_pawns)};
U64 semi_backwards[2] = {pawns::semi_backward<WHITE>(w_pawns, b_pawns),
pawns::semi_backward<BLACK>(b_pawns, w_pawns)};
U64 paired[2] = {pawns::paired(w_pawns), pawns::paired(b_pawns)};
U64 detached[2] = {pawns::detached<WHITE>(w_pawns, b_pawns), pawns::detached<BLACK>(b_pawns, w_pawns)};
U64 doubled[2] = {pawns::doubled<WHITE>(w_pawns), pawns::doubled<BLACK>(b_pawns)};
U64 chain[2] = {pawns::attacks<WHITE>(w_pawns) & w_pawns, pawns::attacks<BLACK>(b_pawns) & b_pawns};
U64 passed[2] = {pawns::passed<WHITE>(w_pawns, b_pawns), pawns::passed<BLACK>(b_pawns, w_pawns)};
U64 candidate[2] = {pawns::candidates<WHITE>(w_pawns, b_pawns), pawns::candidates<BLACK>(b_pawns, w_pawns)};
// Count isolated, backwards and doubled pawns
int isolated_counts[2][2] = {
{pop_count(isolated[WHITE] & ~open[WHITE]), pop_count(isolated[WHITE] & open[WHITE])},
{pop_count(isolated[BLACK] & ~open[BLACK]), pop_count(isolated[BLACK] & open[BLACK])}
}; // [TEAM][OPEN]
int backwards_counts[2][2] = {
{pop_count(backwards[WHITE] & ~open[WHITE]), pop_count(backwards[WHITE] & open[WHITE])},
{pop_count(backwards[BLACK] & ~open[BLACK]), pop_count(backwards[BLACK] & open[BLACK])}
}; // [TEAM][OPEN]
int semi_backwards_counts[2][2] = {
{pop_count(semi_backwards[WHITE] & ~open[WHITE]), pop_count(semi_backwards[WHITE] & open[WHITE])},
{pop_count(semi_backwards[BLACK] & ~open[BLACK]), pop_count(semi_backwards[BLACK] & open[BLACK])}
}; // [TEAM][OPEN]
int paired_counts[2][2] = {
{pop_count(paired[WHITE] & ~open[WHITE]), pop_count(paired[WHITE] & open[WHITE])},
{pop_count(paired[BLACK] & ~open[BLACK]), pop_count(paired[BLACK] & open[BLACK])}
}; // [TEAM][OPEN]
int detached_counts[2][2] = {
{pop_count(detached[WHITE] & ~open[WHITE]), pop_count(detached[WHITE] & open[WHITE])},
{pop_count(detached[BLACK] & ~open[BLACK]), pop_count(detached[BLACK] & open[BLACK])}
}; // [TEAM][OPEN]
int doubled_counts[2][2] = {
{pop_count(doubled[WHITE] & ~open[WHITE]), pop_count(doubled[WHITE] & open[WHITE])},
{pop_count(doubled[BLACK] & ~open[BLACK]), pop_count(doubled[BLACK] & open[BLACK])}
}; // [TEAM][OPEN]
// Add to the scores
score += (isolated_counts[WHITE][0] - isolated_counts[BLACK][0]) * params.isolated[0];
score += (isolated_counts[WHITE][1] - isolated_counts[BLACK][1]) * params.isolated[1];
score += (backwards_counts[WHITE][0] - backwards_counts[BLACK][0]) * params.backwards[0];
score += (backwards_counts[WHITE][1] - backwards_counts[BLACK][1]) * params.backwards[1];
score += (semi_backwards_counts[WHITE][0] - semi_backwards_counts[BLACK][0]) * params.semi_backwards[0];
score += (semi_backwards_counts[WHITE][1] - semi_backwards_counts[BLACK][1]) * params.semi_backwards[1];
score += (paired_counts[WHITE][0] - paired_counts[BLACK][0]) * params.paired[0];
score += (paired_counts[WHITE][1] - paired_counts[BLACK][1]) * params.paired[1];
score += (detached_counts[WHITE][0] - detached_counts[BLACK][0]) * params.detached[0];
score += (detached_counts[WHITE][1] - detached_counts[BLACK][1]) * params.detached[1];
score += (doubled_counts[WHITE][0] - doubled_counts[BLACK][0]) * params.doubled[0];
score += (doubled_counts[WHITE][1] - doubled_counts[BLACK][1]) * params.doubled[1];
U64 bb;
// King PST
int king_loc[2] = {bit_scan(w_king), bit_scan(b_king)};
score += params.pst[WHITE][KING][king_loc[WHITE]];
score -= params.pst[BLACK][KING][king_loc[BLACK]];
// Pawn PST
bb = w_pawns;
while (bb) {
uint8_t sq = pop_bit(bb);
score += params.pst[WHITE][PAWN][sq];
score += distance(king_loc[WHITE], sq) * params.king_tropism[0];
score += distance(king_loc[BLACK], sq) * params.king_tropism[1];
}
bb = b_pawns;
while (bb) {
uint8_t sq = pop_bit(bb);
score -= params.pst[BLACK][PAWN][sq];
score -= distance(king_loc[BLACK], sq) * params.king_tropism[0];
score -= distance(king_loc[WHITE], sq) * params.king_tropism[1];
}
// Chain
bb = chain[WHITE];
while (bb) {
uint8_t sq = pop_bit(bb);
uint8_t rank = rel_rank(WHITE, rank_index(sq));
score += params.chain[rank - 2];
}
bb = chain[BLACK];
while (bb) {
uint8_t sq = pop_bit(bb);
uint8_t rank = rel_rank(BLACK, rank_index(sq));
score -= params.chain[rank - 2];
}
// Passed
bb = passed[WHITE];
while (bb) {
uint8_t sq = pop_bit(bb);
uint8_t rank = rel_rank(WHITE, rank_index(sq));
score += params.passed[rank - 1];
score += distance(king_loc[WHITE], sq) * params.passer_tropism[0];
score += distance(king_loc[BLACK], sq) * params.passer_tropism[1];
}
bb = passed[BLACK];
while (bb) {
uint8_t sq = pop_bit(bb);
uint8_t rank = rel_rank(BLACK, rank_index(sq));
score -= params.passed[rank - 1];
score -= distance(king_loc[BLACK], sq) * params.passer_tropism[0];
score -= distance(king_loc[WHITE], sq) * params.passer_tropism[1];
}
// Candidates
bb = candidate[WHITE];
while (bb) {
uint8_t sq = pop_bit(bb);
uint8_t rank = rel_rank(WHITE, rank_index(sq));
score += params.candidate[rank - 1];
}
bb = candidate[BLACK];
while (bb) {
uint8_t sq = pop_bit(bb);
uint8_t rank = rel_rank(BLACK, rank_index(sq));
score -= params.candidate[rank - 1];
}
// Calculate tapering factor
int accumulator = params.pt_island * (pawns::island_count(w_pawns) + pawns::island_count(b_pawns));
U64 blocked_b_pawns = pawns::stop_squares<WHITE>(w_pawns) & b_pawns;
for (int file = 0; file < 8; file++) {
U64 on_file = file_mask(file) & (w_pawns | b_pawns);
if (blocked_b_pawns & on_file) { // Blocked file
accumulator += params.pt_blocked_file[file_edge_distance(file)];
} else if (!on_file) { // No pawns - open file
accumulator += params.pt_open_file[file_edge_distance(file)];
}
}
this->taper = (float) std::clamp(accumulator, 0, params.pt_max) / (float) params.pt_max;
this->eval_mg = taper * score[0] + (1 - taper) * score[1];
this->eval_eg = taper * score[2] + (1 - taper) * score[3];
}