-
Notifications
You must be signed in to change notification settings - Fork 14
/
evaluate_safety.go
197 lines (162 loc) · 6.13 KB
/
evaluate_safety.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2014-2018 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.
package donna
func (e *Evaluation) analyzeSafety() {
var score Score
var cover, safety Total
if engine.trace {
defer func() {
var our, their Score
e.checkpoint(`+King`, Total{*our.add(cover.white).add(safety.white), *their.add(cover.black).add(safety.black)})
e.checkpoint(`-Cover`, cover)
e.checkpoint(`-Safety`, safety)
}()
}
// If any of the pawns or a king have moved then recalculate cover score.
if e.position.king[White] != e.pawns.king[White] {
e.pawns.cover[White] = e.kingCover(White)
e.pawns.king[White] = e.position.king[White]
}
if e.position.king[Black] != e.pawns.king[Black] {
e.pawns.cover[Black] = e.kingCover(Black)
e.pawns.king[Black] = e.position.king[Black]
}
// Fetch king cover score from the pawn cache.
cover.white.add(e.pawns.cover[White])
cover.black.add(e.pawns.cover[Black])
// Calculate king's safety for both sides.
if e.safety[White].threats > 0 {
safety.white = e.kingSafety(White)
}
if e.safety[Black].threats > 0 {
safety.black = e.kingSafety(Black)
}
// Calculate total king safety and pawn cover score.
score.add(safety.white).sub(safety.black).apply(weightSafety)
score.add(cover.white).sub(cover.black)
e.score.add(score)
}
func (e *Evaluation) kingSafety(our int) (score Score) {
p, their := e.position, our^1
safetyIndex, checkers, square := 0, 0, p.king[our]
// Find squares around the king that are being attacked by the
// enemy and defended by our king only.
defended := e.attacks[pawn(our)] | e.attacks[knight(our)] |
e.attacks[bishop(our)] | e.attacks[rook(our)] |
e.attacks[queen(our)]
weak := e.attacks[king(our)] & e.attacks[their] & ^defended
// Find possible queen checks on weak squares around the king.
// We only consider squares where the queen is protected and
// can't be captured by the king.
protected := e.attacks[pawn(their)] | e.attacks[knight(their)] |
e.attacks[bishop(their)] | e.attacks[rook(their)] |
e.attacks[king(their)]
checks := weak & e.attacks[queen(their)] & protected & ^p.outposts[their]
if checks.any() {
checkers++
safetyIndex += queenCheck * checks.count()
}
// Out of all squares available for enemy pieces select the ones
// that are not under our attack.
safe := ^(e.attacks[our] | p.outposts[their])
// Are there any safe squares from where enemy Knight could give
// us a check?
if checks := knightMoves[square] & safe & e.attacks[knight(their)]; checks.any() {
checkers++
safetyIndex += checks.count()
}
// Are there any safe squares from where enemy Bishop could give us a check?
safeBishopMoves := p.bishopMoves(square) & safe
if checks := safeBishopMoves & e.attacks[bishop(their)]; checks.any() {
checkers++
safetyIndex += checks.count()
}
// Are there any safe squares from where enemy Rook could give us a check?
safeRookMoves := p.rookMoves(square) & safe
if checks := safeRookMoves & e.attacks[rook(their)]; checks.any() {
checkers++
safetyIndex += checks.count()
}
// Are there any safe squares from where enemy Queen could give us a check?
if checks := (safeBishopMoves | safeRookMoves) & e.attacks[queen(their)]; checks.any() {
checkers++
safetyIndex += queenCheck / 2 * checks.count()
}
threatIndex := min(16, e.safety[our].attackers * e.safety[our].threats / 2) +
(e.safety[our].attacks + weak.count()) * 3 +
rank(our, square) - e.pawns.cover[our].midgame / 16
safetyIndex = min(63, max(0, safetyIndex + threatIndex))
score.midgame -= kingSafety[safetyIndex]
if checkers > 0 {
score.add(rightToMove)
if checkers > 1 {
score.add(rightToMove)
}
}
return score
}
func (e *Evaluation) kingCover(our int) (score Score) {
p, square := e.position, e.position.king[our]
// Don't bother with the cover if the king is too far out.
if rank(our, square) <= A3H3 {
// If we still have castle rights encourage castle pawns to stay intact
// by scoring least safe castle.
score.midgame = e.kingCoverBonus(our, square)
if p.castles & castleKingside[our] != 0 {
score.midgame = max(score.midgame, e.kingCoverBonus(our, homeKing[our] + 2))
}
if p.castles & castleQueenside[our] != 0 {
score.midgame = max(score.midgame, e.kingCoverBonus(our, homeKing[our] - 2))
}
}
score.endgame = e.kingPawnProximity(our, square)
return score
}
func (e *Evaluation) kingCoverBonus(our int, square int) (bonus int) {
bonus = onePawn + onePawn / 3
// Get pawns adjacent to and in front of the king.
row, col := coordinate(square)
area := maskRank[row] | maskPassed[our][square]
cover := e.position.outposts[pawn(our)] & area
storm := e.position.outposts[pawn(our^1)] & area
// For each of the cover files find the closest friendly pawn. The penalty
// is carried if the pawn is missing or is too far from the king.
from, to := max(B1, col) - 1, min(G1, col) + 1
for c := from; c <= to; c++ {
// Friendly pawns protecting the kings.
closest := 0
if pawns := (cover & maskFile[c]); pawns.any() {
closest = rank(our, pawns.closest(our))
}
bonus -= penaltyCover[closest]
// Enemy pawns facing the king.
if pawns := (storm & maskFile[c]); pawns.any() {
farthest := rank(our, pawns.farthest(our^1))
if closest == 0 { // No opposing friendly pawn.
bonus -= penaltyStorm[farthest]
} else if farthest == closest + 1 {
bonus -= penaltyStormBlocked[farthest]
} else {
bonus -= penaltyStormUnblocked[farthest]
}
}
}
return bonus
}
// Calculates endgame penalty to encourage a king stay closer to friendly pawns.
func (e *Evaluation) kingPawnProximity(our int, square int) (penalty int) {
pawns := e.position.outposts[pawn(our)]
if pawns.any() && (pawns & e.attacks[king(our)]).empty() {
proximity := 8
for bm := pawns; bm.any(); bm = bm.pop() {
proximity = min(proximity, distance[square][bm.first()])
}
penalty = -kingByPawn.endgame * (proximity - 1)
}
return penalty
}