-
Notifications
You must be signed in to change notification settings - Fork 14
/
evaluate_pieces.go
343 lines (286 loc) · 10.8 KB
/
evaluate_pieces.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// 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) analyzePieces() {
p := e.position
var bonus, score Score
var knight, bishop, rook, queen, mobility Total
if engine.trace {
defer func() {
var our, their Score
e.checkpoint(`Mobility`, mobility)
e.checkpoint(`+Pieces`, Total{*our.add(knight.white).add(bishop.white).add(rook.white).add(queen.white),
*their.add(knight.black).add(bishop.black).add(rook.black).add(queen.black)})
e.checkpoint(`-Knights`, knight)
e.checkpoint(`-Bishops`, bishop)
e.checkpoint(`-Rooks`, rook)
e.checkpoint(`-Queens`, queen)
}()
}
// Mobility masks for both sides exclude squares attacked by opponent's pawns,
// king squares, pawns on first two ranks, and blocked pawns on other ranks.
var pawnExclusions = [2]Bitmask {
p.outposts[Pawn] & (maskRank[A2H2] | maskRank[A3H3] | p.board.up(Black)),
p.outposts[BlackPawn] & (maskRank[A7H7] | maskRank[A6H6] | p.board.up(White)),
}
// Initialize safe mobility zones for both sides.
var maskSafe = [2]Bitmask {
^(e.attacks[BlackPawn] | p.outposts[King] | pawnExclusions[White]),
^(e.attacks[Pawn] | p.outposts[BlackKing] | pawnExclusions[Black]),
}
// Initialize flags to see if kings for both sides require safety evaluation.
var isKingUnsafe = [2]bool { e.isKingUnsafe(White), e.isKingUnsafe(Black) }
// Initialize king fort bitmasks only when we need them.
if isKingUnsafe[White] {
e.safety[White].fort = e.setupFort(White)
}
if isKingUnsafe[Black] {
e.safety[Black].fort = e.setupFort(Black)
}
// Evaluate white pieces except the queen.
if p.outposts[Knight].any() {
knight.white, bonus = e.knights(White, maskSafe[White], isKingUnsafe[Black])
e.score.add(knight.white)
mobility.white.add(bonus)
}
if p.outposts[Bishop].any() {
bishop.white, bonus = e.bishops(White, maskSafe[White], isKingUnsafe[Black])
e.score.add(bishop.white)
mobility.white.add(bonus)
}
if p.outposts[Rook].any() {
rook.white, bonus = e.rooks(White, maskSafe[White], isKingUnsafe[Black])
e.score.add(rook.white)
mobility.white.add(bonus)
}
// Evaluate black pieces except the queen.
if p.outposts[BlackKnight].any() {
knight.black, bonus = e.knights(Black, maskSafe[Black], isKingUnsafe[White])
e.score.sub(knight.black)
mobility.black.add(bonus)
}
if p.outposts[BlackBishop].any() {
bishop.black, bonus = e.bishops(Black, maskSafe[Black], isKingUnsafe[White])
e.score.sub(bishop.black)
mobility.black.add(bonus)
}
if p.outposts[BlackRook].any() {
rook.black, bonus = e.rooks(Black, maskSafe[Black], isKingUnsafe[White])
e.score.sub(rook.black)
mobility.black.add(bonus)
}
// Now that we've built all attack bitmasks we can adjust mobility to exclude
// attacks by enemy's knights, bishops, and rooks and evaluate the queens.
if p.outposts[Queen].any() {
maskSafe[White] &= ^(e.attacks[BlackKnight] | e.attacks[BlackBishop] | e.attacks[BlackRook])
queen.white, bonus = e.queens(White, maskSafe[White], isKingUnsafe[Black])
e.score.add(queen.white)
mobility.white.add(bonus)
}
if p.outposts[BlackQueen].any() {
maskSafe[Black] &= ^(e.attacks[Knight] | e.attacks[Bishop] | e.attacks[Rook])
queen.black, bonus = e.queens(Black, maskSafe[Black], isKingUnsafe[White])
e.score.sub(queen.black)
mobility.black.add(bonus)
}
// Update attack bitmasks for both sides.
e.attacks[White] |= e.attacks[Knight] | e.attacks[Bishop] | e.attacks[Rook] | e.attacks[Queen]
e.attacks[Black] |= e.attacks[BlackKnight] | e.attacks[BlackBishop] | e.attacks[BlackRook] | e.attacks[BlackQueen]
// Calculate total mobility score applying mobility weight.
score.add(mobility.white).sub(mobility.black).apply(weightMobility)
e.score.add(score)
}
func (e *Evaluation) knights(our int, maskSafe Bitmask, unsafeKing bool) (score, mobility Score) {
p, their := e.position, our^1
for bm := p.outposts[knight(our)]; bm.any(); bm = bm.pop() {
square := bm.first()
attacks := Bitmask(0)
// Bonus for knight's mobility -- unless the knight is pinned.
if e.pins[our].off(square) {
attacks = p.attacks(square)
mobility.add(mobilityKnight[(attacks & maskSafe).count()])
}
// Penalty if knight is attacked by enemy's pawn.
if (maskPawn[their][square] & p.outposts[pawn(their)]).any() {
score.sub(penaltyPawnThreat[Knight/2])
}
// Bonus if knight is behind friendly pawn.
if rank(our, square) < 4 && p.outposts[pawn(our)].on(square + up[our]) {
score.add(behindPawn)
}
// Track if knight attacks squares around enemy's king.
if unsafeKing {
e.kingThreats(knight(our), attacks)
}
// Update attack bitmask for the knight.
e.attacks[knight(our)] |= attacks
}
return
}
func (e *Evaluation) bishops(our int, maskSafe Bitmask, unsafeKing bool) (score, mobility Score) {
p, their := e.position, our^1
for bm := p.outposts[bishop(our)]; bm.any(); bm = bm.pop() {
square := bm.first()
attacks := p.xrayAttacks(square)
// Bonus for bishop's mobility: if the bishop is pinned then restrict the attacks.
if e.pins[our].on(square) {
attacks &= maskLine[p.king[our]][square]
}
mobility.add(mobilityBishop[(attacks & maskSafe).count()])
// Penalty for light/dark-colored pawns restricting a bishop.
if count := (same(square) & p.outposts[pawn(our)]).count(); count > 0 {
score.sub(bishopPawn.times(count))
}
// Penalty if bishop is attacked by enemy's pawn.
if (maskPawn[their][square] & p.outposts[pawn(their)]).any() {
score.sub(penaltyPawnThreat[Bishop/2])
}
// Bonus if bishop is behind friendly pawn.
if rank(our, square) < 4 && p.outposts[pawn(our)].on(square + up[our]) {
score.add(behindPawn)
}
// Middle game penalty for boxed bishop.
if e.material.phase > 160 {
if our == White {
if (square == C1 && p.pieces[D2].isPawn() && p.pieces[D3] != 0) ||
(square == F1 && p.pieces[E2].isPawn() && p.pieces[E3] != 0) {
score.midgame -= bishopBoxed.midgame
}
} else {
if (square == C8 && p.pieces[D7].isPawn() && p.pieces[D6] != 0) ||
(square == F8 && p.pieces[E7].isPawn() && p.pieces[E6] != 0) {
score.midgame -= bishopBoxed.midgame
}
}
}
// Extra bonus if bishop is on central ranks.
extra := Score{0, 0}
if extra.midgame = extraBishop[flip(our, square)]; extra.midgame > 0 {
extra.endgame = extra.midgame / 2
score.add(extra)
}
// Track if bishop attacks squares around enemy's king.
if unsafeKing {
e.kingThreats(bishop(our), attacks)
}
// Update attack bitmask for the bishop.
e.attacks[bishop(our)] |= attacks
}
return
}
func (e *Evaluation) rooks(our int, maskSafe Bitmask, unsafeKing bool) (score, mobility Score) {
p, their := e.position, our^1
ourPawns := p.outposts[pawn(our)]
theirPawns := p.outposts[pawn(their)]
// Bonus if rook is on 7th rank and enemy's king trapped on 8th.
if bm := (p.outposts[rook(our)] & mask7th[our]); bm.any() && (p.outposts[king(their)] & mask8th[our]).any() {
score.add(rookOn7th.times(bm.count()))
}
for bm := p.outposts[rook(our)]; bm.any(); bm = bm.pop() {
square := bm.first()
attacks := p.xrayAttacks(square)
// Bonus for rook's mobility: if the rook is pinned then restrict the attacks.
if e.pins[our].on(square) {
attacks &= maskLine[p.king[our]][square]
}
safeSquares := (attacks & maskSafe).count()
mobility.add(mobilityRook[safeSquares])
// Penalty if rook is attacked by enemy's pawn.
if maskPawn[their][square] & theirPawns != 0 {
score.sub(penaltyPawnThreat[Rook/2])
}
// Bonus if rook is attacking enemy's pawns.
if rank(our, square) >= 4 {
if count := (attacks & theirPawns).count(); count > 0 {
score.add(rookOnPawn.times(count))
}
}
// Bonuses if rook is on open or semi-open file.
column := col(square)
isFileAjar := (ourPawns & maskFile[column] == 0)
if isFileAjar {
if theirPawns & maskFile[column] == 0 {
score.add(rookOnOpen)
} else {
score.add(rookOnSemiOpen)
}
}
// Middle game penalty if a rook is boxed. Extra penalty if castle
// rights have been lost.
if safeSquares <= 3 || !isFileAjar {
kingSquare := p.king[our]
kingColumn := col(kingSquare)
// Queenside box: king on D/C/B vs. rook on A/B/C files. Increase the
// the penalty since no castle is possible.
if column < kingColumn && rookBoxA[our].on(square) && kingBoxA[our].on(kingSquare) {
score.midgame -= (rookBoxed.midgame - safeSquares * 10) * 2
}
// Kingside box: king on E/F/G vs. rook on H/G/F files.
if column > kingColumn && rookBoxH[our].on(square) && kingBoxH[our].on(kingSquare) {
score.midgame -= (rookBoxed.midgame - safeSquares * 10)
if p.castles & castleKingside[our] == 0 {
score.midgame -= (rookBoxed.midgame - safeSquares * 10)
}
}
}
// Track if rook attacks squares around enemy's king.
if unsafeKing {
e.kingThreats(rook(our), attacks)
}
// Update attack bitmask for the rook.
e.attacks[rook(our)] |= attacks
}
return
}
func (e *Evaluation) queens(our int, maskSafe Bitmask, unsafeKing bool) (score, mobility Score) {
p, their := e.position, our^1
for bm := p.outposts[queen(our)]; bm.any(); bm = bm.pop() {
square := bm.first()
attacks := p.attacks(square)
// Bonus for queen's mobility: if the queen is pinned then restrict the attacks.
if e.pins[our].on(square) {
attacks &= maskLine[p.king[our]][square]
}
mobility.add(mobilityQueen[min(15, (attacks & maskSafe).count())])
// Penalty if queen is attacked by enemy's pawn.
if (maskPawn[their][square] & p.outposts[pawn(their)]).any() {
score.sub(penaltyPawnThreat[Queen/2])
}
// Track if queen attacks squares around enemy's king.
if unsafeKing {
e.kingThreats(queen(our), attacks)
}
// Update attack bitmask for the queen.
e.attacks[queen(our)] |= attacks
}
return
}
// Updates safety data used later on when evaluating king safety.
func (e *Evaluation) kingThreats(piece Piece, attacks Bitmask) {
their := piece.color()^1
if (attacks & e.safety[their].fort).any() {
e.safety[their].attackers++
e.safety[their].threats += kingThreat[piece.id()]
if bits := attacks & e.attacks[king(their)]; bits.any() {
e.safety[their].attacks += bits.count()
}
}
}
// Initializes the fort bitmask around king's square. For example, for a king on
// G1 the bitmask covers F1,F2,F3, G2,G3, and H1,H2,H3. For a king on a corner
// square, say H1, the bitmask covers F1,F2, G1,G2,G3, and H2,H3.
func (e *Evaluation) setupFort(our int) (bitmask Bitmask) {
bitmask = e.attacks[king(our)] | e.attacks[king(our)].up(our)
switch e.position.king[our] {
case A1, A8:
bitmask |= e.attacks[king(our)] << 1
case H1, H8:
bitmask |= e.attacks[king(our)] >> 1
}
return bitmask
}